[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250331224946.13899fcf@pumpkin>
Date: Mon, 31 Mar 2025 22:49:46 +0100
From: David Laight <david.laight.linux@...il.com>
To: Stefan Metzmacher <metze@...ba.org>
Cc: Linus Torvalds <torvalds@...ux-foundation.org>, Jens Axboe
<axboe@...nel.dk>, Pavel Begunkov <asml.silence@...il.com>, Breno Leitao
<leitao@...ian.org>, Jakub Kicinski <kuba@...nel.org>, Christoph Hellwig
<hch@....de>, Karsten Keil <isdn@...ux-pingi.de>, Ayush Sawal
<ayush.sawal@...lsio.com>, Andrew Lunn <andrew+netdev@...n.ch>, "David S.
Miller" <davem@...emloft.net>, Eric Dumazet <edumazet@...gle.com>, Paolo
Abeni <pabeni@...hat.com>, Simon Horman <horms@...nel.org>, Kuniyuki
Iwashima <kuniyu@...zon.com>, Willem de Bruijn <willemb@...gle.com>, David
Ahern <dsahern@...nel.org>, Marcelo Ricardo Leitner
<marcelo.leitner@...il.com>, Xin Long <lucien.xin@...il.com>, Neal Cardwell
<ncardwell@...gle.com>, Joerg Reuter <jreuter@...na.de>, Marcel Holtmann
<marcel@...tmann.org>, Johan Hedberg <johan.hedberg@...il.com>, Luiz
Augusto von Dentz <luiz.dentz@...il.com>, Oliver Hartkopp
<socketcan@...tkopp.net>, Marc Kleine-Budde <mkl@...gutronix.de>, Robin van
der Gracht <robin@...tonic.nl>, Oleksij Rempel <o.rempel@...gutronix.de>,
kernel@...gutronix.de, Alexander Aring <alex.aring@...il.com>, Stefan
Schmidt <stefan@...enfreihafen.org>, Miquel Raynal
<miquel.raynal@...tlin.com>, Alexandra Winter <wintera@...ux.ibm.com>,
Thorsten Winkler <twinkler@...ux.ibm.com>, James Chapman
<jchapman@...alix.com>, Jeremy Kerr <jk@...econstruct.com.au>, Matt
Johnston <matt@...econstruct.com.au>, Matthieu Baerts <matttbe@...nel.org>,
Mat Martineau <martineau@...nel.org>, Geliang Tang <geliang@...nel.org>,
Krzysztof Kozlowski <krzk@...nel.org>, Remi Denis-Courmont
<courmisch@...il.com>, Allison Henderson <allison.henderson@...cle.com>,
David Howells <dhowells@...hat.com>, Marc Dionne
<marc.dionne@...istor.com>, Wenjia Zhang <wenjia@...ux.ibm.com>, Jan
Karcher <jaka@...ux.ibm.com>, "D. Wythe" <alibuda@...ux.alibaba.com>, Tony
Lu <tonylu@...ux.alibaba.com>, Wen Gu <guwen@...ux.alibaba.com>, Jon Maloy
<jmaloy@...hat.com>, Boris Pismenny <borisp@...dia.com>, John Fastabend
<john.fastabend@...il.com>, Stefano Garzarella <sgarzare@...hat.com>,
Martin Schiller <ms@....tdt.de>, Björn Töpel
<bjorn@...nel.org>, Magnus Karlsson <magnus.karlsson@...el.com>, Maciej
Fijalkowski <maciej.fijalkowski@...el.com>, Jonathan Lemon
<jonathan.lemon@...il.com>, Alexei Starovoitov <ast@...nel.org>, Daniel
Borkmann <daniel@...earbox.net>, Jesper Dangaard Brouer <hawk@...nel.org>,
netdev@...r.kernel.org, linux-kernel@...r.kernel.org,
linux-sctp@...r.kernel.org, linux-hams@...r.kernel.org,
linux-bluetooth@...r.kernel.org, linux-can@...r.kernel.org,
dccp@...r.kernel.org, linux-wpan@...r.kernel.org,
linux-s390@...r.kernel.org, mptcp@...ts.linux.dev,
linux-rdma@...r.kernel.org, rds-devel@....oracle.com,
linux-afs@...ts.infradead.org, tipc-discussion@...ts.sourceforge.net,
virtualization@...ts.linux.dev, linux-x25@...r.kernel.org,
bpf@...r.kernel.org, isdn4linux@...tserv.isdn4linux.de,
io-uring@...r.kernel.org
Subject: Re: [RFC PATCH 3/4] net: pass a kernel pointer via 'optlen_t' to
proto[ops].getsockopt() hooks
On Mon, 31 Mar 2025 22:10:55 +0200
Stefan Metzmacher <metze@...ba.org> wrote:
> The motivation for this is to remove the SOL_SOCKET limitation
> from io_uring_cmd_getsockopt().
>
> The reason for this limitation is that io_uring_cmd_getsockopt()
> passes a kernel pointer.
>
> The first idea would be to change the optval and optlen arguments
> to the protocol specific hooks also to sockptr_t, as that
> is already used for setsockopt() and also by do_sock_getsockopt()
> sk_getsockopt() and BPF_CGROUP_RUN_PROG_GETSOCKOPT().
>
> But as Linus don't like 'sockptr_t' I used a different approach.
>
> Instead of passing the optlen as user or kernel pointer,
> we only ever pass a kernel pointer and do the
> translation from/to userspace in do_sock_getsockopt().
>
> The simple solution would be to just remove the
> '__user' from the int *optlen argument, but it
> seems the compiler doesn't complain about
> '__user' vs. without it, so instead I used
> a helper struct in order to make sure everything
> compiles with a typesafe change.
>
> That together with get_optlen() and put_optlen() helper
> macros make it relatively easy to review and check the
> behaviour is most likely unchanged.
I've looked into this before (and fallen down the patch rabbit hole).
I think the best (final) solution is to pass a validated non-negative
'optlen' into all getsockopt() functions and to have them usually return
either -errno or the modified length.
This simplifies 99% of the functions.
The problem case is functions that want to update the length and return
an error.
By best solution is to support return values of -errno << 20 | length
(as well as -errno and length).
There end up being some slight behaviour changes.
- Some code tries to 'undo' actions if the length can't be updated.
I'm sure this is unnecessary and the recovery path is untested and
could be buggy. Provided the kernel data is consistent there is
no point trying to get code to recover from EFAULT.
The 'length' has been read - so would also need to be readonly
or unmapped by a second thread!
- A lot of getsockopt functions actually treat a negative length as 4.
I think this 'bug' needs to preserved to avoid breaking applications.
The changes are mechanical but very widespread.
They also give the option of not writing back the length if unchanged.
David
Powered by blists - more mailing lists