[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <d482e207223f434f0d306d3158b2142dceac4631.1743449872.git.metze@samba.org>
Date: Mon, 31 Mar 2025 22:10:55 +0200
From: Stefan Metzmacher <metze@...ba.org>
To: Linus Torvalds <torvalds@...ux-foundation.org>,
Jens Axboe <axboe@...nel.dk>
Cc: Stefan Metzmacher <metze@...ba.org>,
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: [RFC PATCH 3/4] net: pass a kernel pointer via 'optlen_t' to proto[ops].getsockopt() hooks
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.
In order to avoid uapi changes regarding different error
code orders regarding -EFAULT, the real -EFAULT handling
is deferred to get_optlen() and put_optlen().
This allows io_uring_cmd_getsockopt() to remove the
SOL_SOCKET limitation.
Removing 'sockptr_t optlen' from existing code
is for patch for another day.
Link: https://lore.kernel.org/io-uring/86b1dce5-4bb4-4a0b-9cff-e72f488bf57d@samba.org/T/#t
Cc: Jens Axboe <axboe@...nel.dk>
Cc: Pavel Begunkov <asml.silence@...il.com>
Cc: Breno Leitao <leitao@...ian.org>
Cc: Linus Torvalds <torvalds@...ux-foundation.org>
Cc: Jakub Kicinski <kuba@...nel.org>
Cc: Christoph Hellwig <hch@....de>
Cc: Karsten Keil <isdn@...ux-pingi.de>
Cc: Ayush Sawal <ayush.sawal@...lsio.com>
Cc: Andrew Lunn <andrew+netdev@...n.ch>
Cc: "David S. Miller" <davem@...emloft.net>
Cc: Eric Dumazet <edumazet@...gle.com>
Cc: Paolo Abeni <pabeni@...hat.com>
Cc: Simon Horman <horms@...nel.org>
Cc: Kuniyuki Iwashima <kuniyu@...zon.com>
Cc: Willem de Bruijn <willemb@...gle.com>
Cc: David Ahern <dsahern@...nel.org>
Cc: Marcelo Ricardo Leitner <marcelo.leitner@...il.com>
Cc: Xin Long <lucien.xin@...il.com>
Cc: Neal Cardwell <ncardwell@...gle.com>
Cc: Joerg Reuter <jreuter@...na.de>
Cc: Marcel Holtmann <marcel@...tmann.org>
Cc: Johan Hedberg <johan.hedberg@...il.com>
Cc: Luiz Augusto von Dentz <luiz.dentz@...il.com>
Cc: Oliver Hartkopp <socketcan@...tkopp.net>
Cc: Marc Kleine-Budde <mkl@...gutronix.de>
Cc: Robin van der Gracht <robin@...tonic.nl>
Cc: Oleksij Rempel <o.rempel@...gutronix.de>
Cc: kernel@...gutronix.de
Cc: Alexander Aring <alex.aring@...il.com>
Cc: Stefan Schmidt <stefan@...enfreihafen.org>
Cc: Miquel Raynal <miquel.raynal@...tlin.com>
Cc: Alexandra Winter <wintera@...ux.ibm.com>
Cc: Thorsten Winkler <twinkler@...ux.ibm.com>
Cc: James Chapman <jchapman@...alix.com>
Cc: Jeremy Kerr <jk@...econstruct.com.au>
Cc: Matt Johnston <matt@...econstruct.com.au>
Cc: Matthieu Baerts <matttbe@...nel.org>
Cc: Mat Martineau <martineau@...nel.org>
Cc: Geliang Tang <geliang@...nel.org>
Cc: Krzysztof Kozlowski <krzk@...nel.org>
Cc: Remi Denis-Courmont <courmisch@...il.com>
Cc: Allison Henderson <allison.henderson@...cle.com>
Cc: David Howells <dhowells@...hat.com>
Cc: Marc Dionne <marc.dionne@...istor.com>
Cc: Wenjia Zhang <wenjia@...ux.ibm.com>
Cc: Jan Karcher <jaka@...ux.ibm.com>
Cc: "D. Wythe" <alibuda@...ux.alibaba.com>
Cc: Tony Lu <tonylu@...ux.alibaba.com>
Cc: Wen Gu <guwen@...ux.alibaba.com>
Cc: Jon Maloy <jmaloy@...hat.com>
Cc: Boris Pismenny <borisp@...dia.com>
Cc: John Fastabend <john.fastabend@...il.com>
Cc: Stefano Garzarella <sgarzare@...hat.com>
Cc: Martin Schiller <ms@....tdt.de>
Cc: "Björn Töpel" <bjorn@...nel.org>
Cc: Magnus Karlsson <magnus.karlsson@...el.com>
Cc: Maciej Fijalkowski <maciej.fijalkowski@...el.com>
Cc: Jonathan Lemon <jonathan.lemon@...il.com>
Cc: Alexei Starovoitov <ast@...nel.org>
Cc: Daniel Borkmann <daniel@...earbox.net>
Cc: Jesper Dangaard Brouer <hawk@...nel.org>
CC: Stefan Metzmacher <metze@...ba.org>
Cc: netdev@...r.kernel.org
Cc: linux-kernel@...r.kernel.org
Cc: linux-sctp@...r.kernel.org
Cc: linux-hams@...r.kernel.org
Cc: linux-bluetooth@...r.kernel.org
Cc: linux-can@...r.kernel.org
Cc: dccp@...r.kernel.org
Cc: linux-wpan@...r.kernel.org
Cc: linux-s390@...r.kernel.org
Cc: mptcp@...ts.linux.dev
Cc: linux-rdma@...r.kernel.org
Cc: rds-devel@....oracle.com
Cc: linux-afs@...ts.infradead.org
Cc: tipc-discussion@...ts.sourceforge.net
Cc: virtualization@...ts.linux.dev
Cc: linux-x25@...r.kernel.org
Cc: bpf@...r.kernel.org
Cc: isdn4linux@...tserv.isdn4linux.de
Cc: io-uring@...r.kernel.org
Signed-off-by: Stefan Metzmacher <metze@...ba.org>
---
include/linux/sockptr.h | 20 +++++++++++++++-----
net/socket.c | 31 +++++++++++++++++++++++++++++--
2 files changed, 44 insertions(+), 7 deletions(-)
diff --git a/include/linux/sockptr.h b/include/linux/sockptr.h
index 1baf66f26f4f..06ec7fd73028 100644
--- a/include/linux/sockptr.h
+++ b/include/linux/sockptr.h
@@ -170,20 +170,25 @@ static inline int check_zeroed_sockptr(sockptr_t src, size_t offset,
}
typedef struct {
- int __user *up;
+ int *kp;
} optlen_t;
#define __check_optlen_t(__optlen) \
({ \
optlen_t *__ptr __maybe_unused = &__optlen; \
- BUILD_BUG_ON(sizeof(*((__ptr)->up)) != sizeof(int)); \
+ BUILD_BUG_ON(sizeof(*((__ptr)->kp)) != sizeof(int)); \
})
#define get_optlen(__val, __optlen) \
({ \
long __err; \
__check_optlen_t(__optlen); \
- __err = get_user(__val, __optlen.up); \
+ if ((__optlen).kp != NULL) { \
+ (__val) = *((__optlen).kp); \
+ __err = 0; \
+ } else { \
+ __err = -EFAULT; \
+ } \
__err; \
})
@@ -191,13 +196,18 @@ typedef struct {
({ \
long __err; \
__check_optlen_t(__optlen); \
- __err = put_user(__val, __optlen.up); \
+ if ((__optlen).kp != NULL) { \
+ *((__optlen).kp) = (__val); \
+ __err = 0; \
+ } else { \
+ __err = -EFAULT; \
+ } \
__err; \
})
static inline sockptr_t OPTLEN_SOCKPTR(optlen_t optlen)
{
- return (sockptr_t) { .user = optlen.up, };
+ return (sockptr_t) { .kernel = optlen.kp, .is_kernel = true };
}
#endif /* _LINUX_SOCKPTR_H */
diff --git a/net/socket.c b/net/socket.c
index fa2de12c10e6..81e5c9767bbc 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -2350,15 +2350,42 @@ int do_sock_getsockopt(struct socket *sock, bool compat, int level,
} else if (unlikely(!ops->getsockopt)) {
err = -EOPNOTSUPP;
} else {
- optlen_t _optlen = { .up = NULL, };
+ optlen_t _optlen = { .kp = NULL, };
+ int koptlen;
if (WARN_ONCE(optval.is_kernel,
"Invalid argument type"))
return -EOPNOTSUPP;
- _optlen.up = optlen.user;
+ if (optlen.is_kernel) {
+ _optlen.kp = optlen.kernel;
+ } else if (optlen.user != NULL) {
+ /*
+ * If optlen.user is NULL,
+ * we pass _optlen.kp = NULL
+ * in order to avoid breaking
+ * any uapi for getsockopt()
+ * implementations that ignore
+ * the optlen pointer completely
+ * or do any level and optname
+ * checking before hitting a
+ * potential -EFAULT condition.
+ *
+ * Also when optlen.user is not NULL,
+ * but copy_from_sockptr() causes -EFAULT,
+ * we'll pass optlen.kp = NULL in order
+ * to defer a possible -EFAULT return
+ * to the caller to get_optlen() and put_optlen().
+ */
+ if (copy_from_sockptr(&koptlen, optlen, sizeof(koptlen)) == 0)
+ _optlen.kp = &koptlen;
+ }
err = ops->getsockopt(sock, level, optname, optval.user,
_optlen);
+ if (err != -EFAULT && _optlen.kp == &koptlen) {
+ if (copy_to_sockptr(optlen, &koptlen, sizeof(koptlen)))
+ return -EFAULT;
+ }
}
if (!compat)
--
2.34.1
Powered by blists - more mailing lists