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-e72f488bf...@samba.org/T/#t
Cc: Jens Axboe <ax...@kernel.dk>
Cc: Pavel Begunkov <asml.sile...@gmail.com>
Cc: Breno Leitao <lei...@debian.org>
Cc: Linus Torvalds <torva...@linux-foundation.org>
Cc: Jakub Kicinski <k...@kernel.org>
Cc: Christoph Hellwig <h...@lst.de>
Cc: Karsten Keil <i...@linux-pingi.de>
Cc: Ayush Sawal <ayush.sa...@chelsio.com>
Cc: Andrew Lunn <andrew+net...@lunn.ch>
Cc: "David S. Miller" <da...@davemloft.net>
Cc: Eric Dumazet <eduma...@google.com>
Cc: Paolo Abeni <pab...@redhat.com>
Cc: Simon Horman <ho...@kernel.org>
Cc: Kuniyuki Iwashima <kun...@amazon.com>
Cc: Willem de Bruijn <will...@google.com>
Cc: David Ahern <dsah...@kernel.org>
Cc: Marcelo Ricardo Leitner <marcelo.leit...@gmail.com>
Cc: Xin Long <lucien....@gmail.com>
Cc: Neal Cardwell <ncardw...@google.com>
Cc: Joerg Reuter <jreu...@yaina.de>
Cc: Marcel Holtmann <mar...@holtmann.org>
Cc: Johan Hedberg <johan.hedb...@gmail.com>
Cc: Luiz Augusto von Dentz <luiz.de...@gmail.com>
Cc: Oliver Hartkopp <socket...@hartkopp.net>
Cc: Marc Kleine-Budde <m...@pengutronix.de>
Cc: Robin van der Gracht <ro...@protonic.nl>
Cc: Oleksij Rempel <o.rem...@pengutronix.de>
Cc: ker...@pengutronix.de
Cc: Alexander Aring <alex.ar...@gmail.com>
Cc: Stefan Schmidt <ste...@datenfreihafen.org>
Cc: Miquel Raynal <miquel.ray...@bootlin.com>
Cc: Alexandra Winter <wint...@linux.ibm.com>
Cc: Thorsten Winkler <twink...@linux.ibm.com>
Cc: James Chapman <jchap...@katalix.com>
Cc: Jeremy Kerr <j...@codeconstruct.com.au>
Cc: Matt Johnston <m...@codeconstruct.com.au>
Cc: Matthieu Baerts <matt...@kernel.org>
Cc: Mat Martineau <martin...@kernel.org>
Cc: Geliang Tang <geli...@kernel.org>
Cc: Krzysztof Kozlowski <k...@kernel.org>
Cc: Remi Denis-Courmont <courmi...@gmail.com>
Cc: Allison Henderson <allison.hender...@oracle.com>
Cc: David Howells <dhowe...@redhat.com>
Cc: Marc Dionne <marc.dio...@auristor.com>
Cc: Wenjia Zhang <wen...@linux.ibm.com>
Cc: Jan Karcher <j...@linux.ibm.com>
Cc: "D. Wythe" <alib...@linux.alibaba.com>
Cc: Tony Lu <ton...@linux.alibaba.com>
Cc: Wen Gu <gu...@linux.alibaba.com>
Cc: Jon Maloy <jma...@redhat.com>
Cc: Boris Pismenny <bor...@nvidia.com>
Cc: John Fastabend <john.fastab...@gmail.com>
Cc: Stefano Garzarella <sgarz...@redhat.com>
Cc: Martin Schiller <m...@dev.tdt.de>
Cc: "Björn Töpel" <bj...@kernel.org>
Cc: Magnus Karlsson <magnus.karls...@intel.com>
Cc: Maciej Fijalkowski <maciej.fijalkow...@intel.com>
Cc: Jonathan Lemon <jonathan.le...@gmail.com>
Cc: Alexei Starovoitov <a...@kernel.org>
Cc: Daniel Borkmann <dan...@iogearbox.net>
Cc: Jesper Dangaard Brouer <h...@kernel.org>
CC: Stefan Metzmacher <me...@samba.org>
Cc: net...@vger.kernel.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-s...@vger.kernel.org
Cc: linux-h...@vger.kernel.org
Cc: linux-blueto...@vger.kernel.org
Cc: linux-...@vger.kernel.org
Cc: d...@vger.kernel.org
Cc: linux-w...@vger.kernel.org
Cc: linux-s...@vger.kernel.org
Cc: mp...@lists.linux.dev
Cc: linux-r...@vger.kernel.org
Cc: rds-de...@oss.oracle.com
Cc: linux-...@lists.infradead.org
Cc: tipc-discussion@lists.sourceforge.net
Cc: virtualizat...@lists.linux.dev
Cc: linux-...@vger.kernel.org
Cc: b...@vger.kernel.org
Cc: isdn4li...@listserv.isdn4linux.de
Cc: io-ur...@vger.kernel.org
Signed-off-by: Stefan Metzmacher <me...@samba.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


Reply via email to