From: David Laight <David.Laight@ACULAB.COM>
To: 'Qianqiang Liu' <qianqiang.liu@163.com>,
"xiyou.wangcong@gmail.com" <xiyou.wangcong@gmail.com>,
"davem@davemloft.net" <davem@davemloft.net>,
"edumazet@google.com" <edumazet@google.com>,
"kuba@kernel.org" <kuba@kernel.org>,
"pabeni@redhat.com" <pabeni@redhat.com>
Cc: "netdev@vger.kernel.org" <netdev@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: RE: [PATCH] net: check the return value of the copy_from_sockptr
Date: Wed, 18 Sep 2024 13:11:34 +0000 [thread overview]
Message-ID: <0858b2bde3f54c9da0f655a09bab7dab@AcuMS.aculab.com> (raw)
In-Reply-To: <20240911050435.53156-1-qianqiang.liu@163.com>
From: Qianqiang Liu
> Sent: 11 September 2024 06:05
>
> We must check the return value of the copy_from_sockptr. Otherwise, it
> may cause some weird issues.
Actually there is no point doing the copy for optlen in absolutely every function.
'optlen' can be passed as a kernel address and any user copy done by the caller.
Someone should have spotted that before sockptr_t was used for optlen.
The wrapper code can then also do a correct check for (optlen >= 0) which has
been pretty much broken in most protocols for ~ever.
(I wonder if any (important) userspace relies on a negative optlen
being treated as 4.)
It might mean that the final 'copy_to_user' are done in the opposite
order so that an kernel side side effects aren't reversed if the length
can't be copied out - but I suspect that doesn't matter and the paths
are likely to be untested and buggy.
I have toyed with making the getsockopt() functions return a +ve length
or -ve error - but there are a few strange places that need to update
the length and return an error.
David
>
> Signed-off-by: Qianqiang Liu <qianqiang.liu@163.com>
> ---
> net/socket.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/net/socket.c b/net/socket.c
> index 0a2bd22ec105..6b9a414d01d5 100644
> --- a/net/socket.c
> +++ b/net/socket.c
> @@ -2370,8 +2370,11 @@ int do_sock_getsockopt(struct socket *sock, bool compat, int level,
> if (err)
> return err;
>
> - if (!compat)
> - copy_from_sockptr(&max_optlen, optlen, sizeof(int));
> + if (!compat) {
> + err = copy_from_sockptr(&max_optlen, optlen, sizeof(int));
> + if (err)
> + return -EFAULT;
> + }
>
> ops = READ_ONCE(sock->ops);
> if (level == SOL_SOCKET) {
> --
> 2.39.2
>
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
prev parent reply other threads:[~2024-09-18 13:12 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-11 5:04 [PATCH] net: check the return value of the copy_from_sockptr Qianqiang Liu
2024-09-11 6:51 ` D. Wythe
2024-09-11 7:13 ` Eric Dumazet
2024-09-11 8:23 ` Qianqiang Liu
2024-09-11 9:12 ` Eric Dumazet
2024-09-11 10:24 ` Qianqiang Liu
2024-09-11 12:40 ` Eric Dumazet
2024-09-11 16:58 ` Cong Wang
2024-09-11 17:15 ` Eric Dumazet
2024-09-11 17:35 ` Cong Wang
2024-09-11 18:49 ` Stanislav Fomichev
2024-09-11 19:48 ` Cong Wang
2024-09-11 20:05 ` Stanislav Fomichev
2024-09-14 0:49 ` Cong Wang
2024-09-14 18:01 ` Stanislav Fomichev
2024-09-18 13:11 ` David Laight [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0858b2bde3f54c9da0f655a09bab7dab@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=qianqiang.liu@163.com \
--cc=xiyou.wangcong@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox