Skip to contentSkip to conversation

net/io_uring: pass a kernel pointer via optlen_t to proto[_ops].getsockopt()

RFC PATCH In review Stefan Metzmacher 7 participants 31 messages Mar 31, 2025 – Apr 2, 2025

Subsystems (36)
lore
Share & tools
lore
#0 lore raw

cover net/io_uring: pass a kernel pointer via optlen_t to proto[_ops].getsockopt()

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 as optlen to do_sock_getsockopt() and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

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.

The patchset does the transformation in 3
easy to review steps:

1/4: introduces get_optlen(len, optlen) and put_optlen(len, optlen) helpers
on top of the existing get_user(len, optlen) and put_user(len, optlen) usages.

2/4: introduces a simple optlen_t that just contains 'int __user *up;'
that makes sure get_optlen and put_optlen get a typesafe optlen argument and they are the only functions looking at optlen.
(The existing sockptr_t optlen code gets OPTLEN_SOCKPTR(optlen) passed)

3/4: The changes do_sock_getsockopt() to pass a kernel pointer instead
of a __user pointer via optlen_t. This is a bit tricky as directly failing the copy_from_sockptr(&koptlen, optlen, sizeof(koptlen) with -EFAULT might change the uapi, as some getsockopt() hooks doesn't even touch optlen at all. And userspace could do something like this:

feature_x_supported = true;
ret = getsockopt(fd, level, optname, NULL, NULL); if (ret == -1 && errno == ENOTSUPP) {
feature_x_supported = false;
}

And this should not give -EFAULT after the changes, so optlen.kp is passed down as NULL, so that -EFAULT is deferred to get_optlen() and put_optlen().

4/4: Removes the SOL_SOCKET restriction for io-uring.

This patchset doesn't touch any existing getsockopt() that was already converted to sockptr_t optlen, that's something for a later cleanup.

Cc Jens Axboe <[email protected]>
Cc Pavel Begunkov <[email protected]>
Cc Breno Leitao <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Karsten Keil <[email protected]>
Cc Ayush Sawal <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Simon Horman <[email protected]>
Cc Kuniyuki Iwashima <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc David Ahern <[email protected]>
Cc Marcelo Ricardo Leitner <[email protected]>
Cc Xin Long <[email protected]>
Cc Neal Cardwell <[email protected]>
Cc Joerg Reuter <[email protected]>
Cc Marcel Holtmann <[email protected]>
Cc Johan Hedberg <[email protected]>
Cc Luiz Augusto von Dentz <[email protected]>
Cc Oliver Hartkopp <[email protected]>
Cc Marc Kleine-Budde <[email protected]>
Cc Robin van der Gracht <[email protected]>
Cc Oleksij Rempel <[email protected]>
Cc Alexander Aring <[email protected]>
Cc Stefan Schmidt <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Alexandra Winter <[email protected]>
Cc Thorsten Winkler <[email protected]>
Cc James Chapman <[email protected]>
Cc Jeremy Kerr <[email protected]>
Cc Matt Johnston <[email protected]>
Cc Matthieu Baerts <[email protected]>
Cc Mat Martineau <[email protected]>
Cc Geliang Tang <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Remi Denis-Courmont <[email protected]>
Cc Allison Henderson <[email protected]>
Cc David Howells <[email protected]>
Cc Marc Dionne <[email protected]>
Cc Wenjia Zhang <[email protected]>
Cc Jan Karcher <[email protected]>
Cc "D. Wythe" <[email protected]>
Cc Tony Lu <[email protected]>
Cc Wen Gu <[email protected]>
Cc Jon Maloy <[email protected]>
Cc Boris Pismenny <[email protected]>
Cc John Fastabend <[email protected]>
Cc Stefano Garzarella <[email protected]>
Cc Martin Schiller <[email protected]>
Cc "Björn Töpel" <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maciej Fijalkowski <[email protected]>
Cc Jonathan Lemon <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
CC Stefan Metzmacher <[email protected]>

Stefan Metzmacher (4):
net: introduce get_optlen() and put_optlen() helpers
net: pass 'optlen_t' to proto[ops].getsockopt() hooks
net: pass a kernel pointer via 'optlen_t' to proto[ops].getsockopt()
hooks
io_uring: let io_uring_cmd_getsockopt() allow level other than
SOL_SOCKET

 drivers/isdn/mISDN/socket.c                   |   4 +-
 .../chelsio/inline_crypto/chtls/chtls_main.c  |   4 +-
 include/linux/net.h                           |   2 +-
 include/linux/sockptr.h                       |  41 ++++
 include/net/inet_connection_sock.h            |   2 +-
 include/net/ip.h                              |   2 +-
 include/net/ipv6.h                            |   2 +-
 include/net/sctp/structs.h                    |   2 +-
 include/net/sock.h                            |   4 +-
 include/net/tcp.h                             |   2 +-
 include/net/udp.h                             |   2 +-
 io_uring/uring_cmd.c                          |   3 -
 net/atm/common.c                              |   4 +-
 net/atm/common.h                              |   2 +-
 net/atm/pvc.c                                 |   2 +-
 net/atm/svc.c                                 |   4 +-
 net/ax25/af_ax25.c                            |   6 +-
 net/bluetooth/hci_sock.c                      |   6 +-
 net/bluetooth/iso.c                           |   6 +-
 net/bluetooth/l2cap_sock.c                    |   8 +-
 net/bluetooth/rfcomm/sock.c                   |   8 +-
 net/bluetooth/sco.c                           |  10 +-
 net/can/isotp.c                               |   6 +-
 net/can/j1939/socket.c                        |   6 +-
 net/can/raw.c                                 |  14 +-
 net/core/sock.c                               |   2 +-
 net/dccp/ccid.c                               |   4 +-
 net/dccp/ccid.h                               |  10 +-
 net/dccp/ccids/ccid3.c                        |   8 +-
 net/dccp/dccp.h                               |   2 +-
 net/dccp/proto.c                              |  12 +-
 net/ieee802154/socket.c                       |   8 +-
 net/ipv4/ip_sockglue.c                        |   8 +-
 net/ipv4/raw.c                                |  10 +-
 net/ipv4/tcp.c                                |   4 +-
 net/ipv4/udp.c                                |   8 +-
 net/ipv4/udp_impl.h                           |   2 +-
 net/ipv6/ipv6_sockglue.c                      |   8 +-
 net/ipv6/raw.c                                |  14 +-
 net/ipv6/udp.c                                |   2 +-
 net/ipv6/udp_impl.h                           |   2 +-
 net/iucv/af_iucv.c                            |   6 +-
 net/kcm/kcmsock.c                             |   6 +-
 net/l2tp/l2tp_ppp.c                           |   6 +-
 net/llc/af_llc.c                              |   6 +-
 net/mctp/af_mctp.c                            |   4 +-
 net/mptcp/protocol.h                          |   2 +-
 net/mptcp/sockopt.c                           |  48 ++--
 net/netlink/af_netlink.c                      |   8 +-
 net/netrom/af_netrom.c                        |   6 +-
 net/nfc/llcp_sock.c                           |   6 +-
 net/packet/af_packet.c                        |   6 +-
 net/phonet/pep.c                              |   6 +-
 net/rds/af_rds.c                              |   8 +-
 net/rds/info.c                                |   6 +-
 net/rds/info.h                                |   2 +-
 net/rose/af_rose.c                            |   6 +-
 net/rxrpc/af_rxrpc.c                          |   6 +-
 net/sctp/socket.c                             | 220 +++++++++---------
 net/smc/af_smc.c                              |   8 +-
 net/smc/smc.h                                 |   2 +-
 net/socket.c                                  |  34 ++-
 net/tipc/socket.c                             |   8 +-
 net/tls/tls_main.c                            |  18 +-
 net/vmw_vsock/af_vsock.c                      |   6 +-
 net/x25/af_x25.c                              |   6 +-
 net/xdp/xsk.c                                 |  10 +-
 67 files changed, 387 insertions(+), 319 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

RFC PATCH 1/4 net: introduce get_optlen() and put_optlen() helpers

The motivation for this is to remove the SOL_SOCKET limitation from io_uring_cmd_getsockopt().

As a first step this adds get_optlen() and put_optlen() helper macros make it relatively easy to review and check the behaviour is most likely unchanged, before the 'int __user *optlen' of the low level .getsockopt() hooks will be changed into a kernel pointer.

Cc Jens Axboe <[email protected]>
Cc Pavel Begunkov <[email protected]>
Cc Breno Leitao <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Karsten Keil <[email protected]>
Cc Ayush Sawal <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Simon Horman <[email protected]>
Cc Kuniyuki Iwashima <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc David Ahern <[email protected]>
Cc Marcelo Ricardo Leitner <[email protected]>
Cc Xin Long <[email protected]>
Cc Neal Cardwell <[email protected]>
Cc Joerg Reuter <[email protected]>
Cc Marcel Holtmann <[email protected]>
Cc Johan Hedberg <[email protected]>
Cc Luiz Augusto von Dentz <[email protected]>
Cc Oliver Hartkopp <[email protected]>
Cc Marc Kleine-Budde <[email protected]>
Cc Robin van der Gracht <[email protected]>
Cc Oleksij Rempel <[email protected]>
Cc Alexander Aring <[email protected]>
Cc Stefan Schmidt <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Alexandra Winter <[email protected]>
Cc Thorsten Winkler <[email protected]>
Cc James Chapman <[email protected]>
Cc Jeremy Kerr <[email protected]>
Cc Matt Johnston <[email protected]>
Cc Matthieu Baerts <[email protected]>
Cc Mat Martineau <[email protected]>
Cc Geliang Tang <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Remi Denis-Courmont <[email protected]>
Cc Allison Henderson <[email protected]>
Cc David Howells <[email protected]>
Cc Marc Dionne <[email protected]>
Cc Wenjia Zhang <[email protected]>
Cc Jan Karcher <[email protected]>
Cc "D. Wythe" <[email protected]>
Cc Tony Lu <[email protected]>
Cc Wen Gu <[email protected]>
Cc Jon Maloy <[email protected]>
Cc Boris Pismenny <[email protected]>
Cc John Fastabend <[email protected]>
Cc Stefano Garzarella <[email protected]>
Cc Martin Schiller <[email protected]>
Cc "Björn Töpel" <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maciej Fijalkowski <[email protected]>
Cc Jonathan Lemon <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
CC Stefan Metzmacher <[email protected]>
---
 drivers/isdn/mISDN/socket.c |   2 +-
 include/linux/sockptr.h     |  22 ++++++++
 net/atm/common.c            |   2 +-
 net/atm/svc.c               |   2 +-
 net/ax25/af_ax25.c          |   4 +-
 net/bluetooth/hci_sock.c    |   2 +-
 net/bluetooth/iso.c         |   4 +-
 net/bluetooth/l2cap_sock.c  |   4 +-
 net/bluetooth/rfcomm/sock.c |   4 +-
 net/bluetooth/sco.c         |   6 +-
 net/can/isotp.c             |   4 +-
 net/can/j1939/socket.c      |   4 +-
 net/can/raw.c               |  12 ++--
 net/dccp/ccid.c             |   2 +-
 net/dccp/ccids/ccid3.c      |   4 +-
 net/dccp/proto.c            |   6 +-
 net/ieee802154/socket.c     |   4 +-
 net/ipv4/ip_sockglue.c      |   4 +-
 net/ipv4/raw.c              |   4 +-
 net/ipv4/udp.c              |   4 +-
 net/ipv6/ipv6_sockglue.c    |   4 +-
 net/ipv6/raw.c              |   8 +--
 net/iucv/af_iucv.c          |   4 +-
 net/kcm/kcmsock.c           |   4 +-
 net/l2tp/l2tp_ppp.c         |   4 +-
 net/llc/af_llc.c            |   4 +-
 net/mctp/af_mctp.c          |   2 +-
 net/mptcp/sockopt.c         |  18 +++---
 net/netlink/af_netlink.c    |   6 +-
 net/netrom/af_netrom.c      |   4 +-
 net/nfc/llcp_sock.c         |   4 +-
 net/packet/af_packet.c      |   4 +-
 net/phonet/pep.c            |   4 +-
 net/rds/af_rds.c            |   6 +-
 net/rds/info.c              |   4 +-
 net/rose/af_rose.c          |   4 +-
 net/rxrpc/af_rxrpc.c        |   4 +-
 net/sctp/socket.c           | 108 ++++++++++++++++++------------------
 net/smc/af_smc.c            |   4 +-
 net/tipc/socket.c           |   6 +-
 net/tls/tls_main.c          |   8 +--
 net/vmw_vsock/af_vsock.c    |   4 +-
 net/x25/af_x25.c            |   4 +-
 net/xdp/xsk.c               |   8 +--
 44 files changed, 176 insertions(+), 154 deletions(-)
drivers/isdn/mISDN/socket.c +1 -1
index b215b28cad7b..b750cc0dfa4a 100644
@@ -438,7 +438,7 @@ static int data_sock_getsockopt(struct socket *sock, int level, int optname,
 	struct sock *sk = sock->sk;
 	int len, opt;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	if (len != sizeof(char))
include/linux/sockptr.h +22 -0
index 3e6c8e9d67ae..28dbc3e27374 100644
@@ -169,4 +169,26 @@ static inline int check_zeroed_sockptr(sockptr_t src, size_t offset,
 	return memchr_inv(src.kernel + offset, 0, size) == NULL;
 }
 
+#define __check_optlen_t(__optlen)				\
+({								\
+	int __user *__ptr __maybe_unused = __optlen; 		\
+	BUILD_BUG_ON(sizeof(*(__ptr)) != sizeof(int));		\
+})
+
+#define get_optlen(__val, __optlen)				\
+({								\
+	long __err;						\
+	__check_optlen_t(__optlen);				\
+	__err = get_user(__val, __optlen);			\
+	__err;							\
+})
+
+#define put_optlen(__val, __optlen) 				\
+({								\
+	long __err;						\
+	__check_optlen_t(__optlen);				\
+	__err = put_user(__val, __optlen);			\
+	__err;							\
+})
+
 #endif /* _LINUX_SOCKPTR_H */
net/atm/common.c +1 -1
index 9b75699992ff..e95371abd705 100644
@@ -792,7 +792,7 @@ int vcc_getsockopt(struct socket *sock, int level, int optname,
 	struct atm_vcc *vcc;
 	int len;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 	if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
 		return -EINVAL;
net/atm/svc.c +1 -1
index f8137ae693b0..a706c5f77d8e 100644
@@ -511,7 +511,7 @@ static int svc_getsockopt(struct socket *sock, int level, int optname,
 		error = vcc_getsockopt(sock, level, optname, optval, optlen);
 		goto out;
 	}
-	if (get_user(len, optlen)) {
+	if (get_optlen(len, optlen)) {
 		error = -EFAULT;
 		goto out;
 	}
net/ax25/af_ax25.c +2 -2
index 3ee7dba34310..b184e2cb4b50 100644
@@ -728,7 +728,7 @@ static int ax25_getsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_AX25)
 		return -ENOPROTOOPT;
 
-	if (get_user(maxlen, optlen))
+	if (get_optlen(maxlen, optlen))
 		return -EFAULT;
 
 	if (maxlen < 1)
@@ -805,7 +805,7 @@ static int ax25_getsockopt(struct socket *sock, int level, int optname,
 	}
 	release_sock(sk);
 
-	if (put_user(length, optlen))
+	if (put_optlen(length, optlen))
 		return -EFAULT;
 
 	return copy_to_user(optval, valptr, length) ? -EFAULT : 0;
net/bluetooth/hci_sock.c +1 -1
index 022b86797acd..4b4e476e4e7c 100644
@@ -2061,7 +2061,7 @@ static int hci_sock_getsockopt_old(struct socket *sock, int level, int optname,
 
 	BT_DBG("sk %p, opt %d", sk, optname);
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	lock_sock(sk);
net/bluetooth/iso.c +2 -2
index 3501a991f1c6..f6624b6e5485 100644
@@ -1731,7 +1731,7 @@ static int iso_sock_getsockopt(struct socket *sock, int level, int optname,
 
 	BT_DBG("sk %p", sk);
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	lock_sock(sk);
@@ -1777,7 +1777,7 @@ static int iso_sock_getsockopt(struct socket *sock, int level, int optname,
 		len = min_t(unsigned int, len, base_len);
 		if (copy_to_user(optval, base, len))
 			err = -EFAULT;
-		if (put_user(len, optlen))
+		if (put_optlen(len, optlen))
 			err = -EFAULT;
 
 		break;
net/bluetooth/l2cap_sock.c +2 -2
index 5aa55fa69594..8ae25a918fd3 100644
@@ -445,7 +445,7 @@ static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
 
 	BT_DBG("sk %p", sk);
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	lock_sock(sk);
@@ -570,7 +570,7 @@ static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_BLUETOOTH)
 		return -ENOPROTOOPT;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	lock_sock(sk);
net/bluetooth/rfcomm/sock.c +2 -2
index 913402806fa0..785894b79dd8 100644
@@ -734,7 +734,7 @@ static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __u
 
 	BT_DBG("sk %p", sk);
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	lock_sock(sk);
@@ -813,7 +813,7 @@ static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, c
 	if (level != SOL_BLUETOOTH)
 		return -ENOPROTOOPT;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	lock_sock(sk);
net/bluetooth/sco.c +3 -3
index 2945d27e75dc..25910eca759c 100644
@@ -1047,7 +1047,7 @@ static int sco_sock_getsockopt_old(struct socket *sock, int optname,
 
 	BT_DBG("sk %p", sk);
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	lock_sock(sk);
@@ -1117,7 +1117,7 @@ static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
 	if (level == SOL_SCO)
 		return sco_sock_getsockopt_old(sock, optname, optval, optlen);
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	lock_sock(sk);
@@ -1266,7 +1266,7 @@ static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
 
 		lock_sock(sk);
 
-		if (!err && put_user(buf_len, optlen))
+		if (!err && put_optlen(buf_len, optlen))
 			err = -EFAULT;
 
 		break;
net/can/isotp.c +2 -2
index 1efa377f002e..aa9ab87d5e14 100644
@@ -1495,7 +1495,7 @@ static int isotp_getsockopt(struct socket *sock, int level, int optname,
 
 	if (level != SOL_CAN_ISOTP)
 		return -EINVAL;
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 	if (len < 0)
 		return -EINVAL;
@@ -1530,7 +1530,7 @@ static int isotp_getsockopt(struct socket *sock, int level, int optname,
 		return -ENOPROTOOPT;
 	}
 
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 	if (copy_to_user(optval, val, len))
 		return -EFAULT;
net/can/j1939/socket.c +2 -2
index 17226b2341d0..b2b538528c2e 100644
@@ -766,7 +766,7 @@ static int j1939_sk_getsockopt(struct socket *sock, int level, int optname,
 
 	if (level != SOL_CAN_J1939)
 		return -EINVAL;
-	if (get_user(ulen, optlen))
+	if (get_optlen(ulen, optlen))
 		return -EFAULT;
 	if (ulen < 0)
 		return -EINVAL;
@@ -793,7 +793,7 @@ static int j1939_sk_getsockopt(struct socket *sock, int level, int optname,
 	 */
 	if (len > ulen)
 		ret = -EFAULT;
-	else if (put_user(len, optlen))
+	else if (put_optlen(len, optlen))
 		ret = -EFAULT;
 	else if (copy_to_user(optval, val, len))
 		ret = -EFAULT;
net/can/raw.c +6 -6
index 020f21430b1d..8d1a1626d1a4 100644
@@ -762,7 +762,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
 
 	if (level != SOL_CAN_RAW)
 		return -EINVAL;
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 	if (len < 0)
 		return -EINVAL;
@@ -779,7 +779,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
 			if (len < fsize) {
 				/* return -ERANGE and needed space in optlen */
 				err = -ERANGE;
-				if (put_user(fsize, optlen))
+				if (put_optlen(fsize, optlen))
 					err = -EFAULT;
 			} else {
 				if (len > fsize)
@@ -793,7 +793,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
 		release_sock(sk);
 
 		if (!err)
-			err = put_user(len, optlen);
+			err = put_optlen(len, optlen);
 		return err;
 	}
 	case CAN_RAW_ERR_FILTER:
@@ -833,7 +833,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
 		if (len < sizeof(ro->raw_vcid_opts)) {
 			/* return -ERANGE and needed space in optlen */
 			err = -ERANGE;
-			if (put_user(sizeof(ro->raw_vcid_opts), optlen))
+			if (put_optlen(sizeof(ro->raw_vcid_opts), optlen))
 				err = -EFAULT;
 		} else {
 			if (len > sizeof(ro->raw_vcid_opts))
@@ -842,7 +842,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
 				err = -EFAULT;
 		}
 		if (!err)
-			err = put_user(len, optlen);
+			err = put_optlen(len, optlen);
 		return err;
 	}
 	case CAN_RAW_JOIN_FILTERS:
@@ -855,7 +855,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
 		return -ENOPROTOOPT;
 	}
 
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 	if (copy_to_user(optval, val, len))
 		return -EFAULT;
net/dccp/ccid.c +1 -1
index 6beac5d348e2..6f495ffb1d60 100644
@@ -66,7 +66,7 @@ int ccid_getsockopt_builtin_ccids(struct sock *sk, int len,
 	if (ccid_get_builtin_ccids(&ccid_array, &array_len))
 		return -ENOBUFS;
 
-	if (put_user(array_len, optlen))
+	if (put_optlen(array_len, optlen))
 		err = -EFAULT;
 	else if (len > 0 && copy_to_user(optval, ccid_array,
 					 len > array_len ? array_len : len))
net/dccp/ccids/ccid3.c +2 -2
index f349d16dd8f6..648aa5270f37 100644
@@ -543,7 +543,7 @@ static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len,
 		return -ENOPROTOOPT;
 	}
 
-	if (put_user(len, optlen) || copy_to_user(optval, val, len))
+	if (put_optlen(len, optlen) || copy_to_user(optval, val, len))
 		return -EFAULT;
 
 	return 0;
@@ -833,7 +833,7 @@ static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len,
 		return -ENOPROTOOPT;
 	}
 
-	if (put_user(len, optlen) || copy_to_user(optval, val, len))
+	if (put_optlen(len, optlen) || copy_to_user(optval, val, len))
 		return -EFAULT;
 
 	return 0;
net/dccp/proto.c +3 -3
index fcc5c9d64f46..8d6461ef8b50 100644
@@ -603,7 +603,7 @@ static int dccp_getsockopt_service(struct sock *sk, int len,
 		goto out;
 
 	err = 0;
-	if (put_user(total_len, optlen) ||
+	if (put_optlen(total_len, optlen) ||
 	    put_user(dp->dccps_service, optval) ||
 	    (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
 		err = -EFAULT;
@@ -618,7 +618,7 @@ static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
 	struct dccp_sock *dp;
 	int val, len;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	if (len < (int)sizeof(int))
@@ -674,7 +674,7 @@ static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
 	}
 
 	len = sizeof(val);
-	if (put_user(len, optlen) || copy_to_user(optval, &val, len))
+	if (put_optlen(len, optlen) || copy_to_user(optval, &val, len))
 		return -EFAULT;
 
 	return 0;
net/ieee802154/socket.c +2 -2
index 18d267921bb5..cc1788853c08 100644
@@ -841,7 +841,7 @@ static int dgram_getsockopt(struct sock *sk, int level, int optname,
 	if (level != SOL_IEEE802154)
 		return -EOPNOTSUPP;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	len = min_t(unsigned int, len, sizeof(int));
@@ -871,7 +871,7 @@ static int dgram_getsockopt(struct sock *sk, int level, int optname,
 		return -ENOPROTOOPT;
 	}
 
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 	if (copy_to_user(optval, &val, len))
 		return -EFAULT;
net/ipv4/ip_sockglue.c +2 -2
index 6d9c5c20b1c4..4d372f76b317 100644
@@ -1771,12 +1771,12 @@ int ip_getsockopt(struct sock *sk, int level,
 			!ip_mroute_opt(optname)) {
 		int len;
 
-		if (get_user(len, optlen))
+		if (get_optlen(len, optlen))
 			return -EFAULT;
 
 		err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
 		if (err >= 0)
-			err = put_user(len, optlen);
+			err = put_optlen(len, optlen);
 		return err;
 	}
 #endif
net/ipv4/raw.c +2 -2
index 6aace4d55733..89d70acdacdc 100644
@@ -811,7 +811,7 @@ static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *o
 {
 	int len, ret = -EFAULT;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		goto out;
 	ret = -EINVAL;
 	if (len < 0)
@@ -819,7 +819,7 @@ static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *o
 	if (len > sizeof(struct icmp_filter))
 		len = sizeof(struct icmp_filter);
 	ret = -EFAULT;
-	if (put_user(len, optlen) ||
+	if (put_optlen(len, optlen) ||
 	    copy_to_user(optval, &raw_sk(sk)->filter, len))
 		goto out;
 	ret = 0;
net/ipv4/udp.c +2 -2
index d0bffcfa56d8..17c3fb1acb30 100644
@@ -3058,7 +3058,7 @@ int udp_lib_getsockopt(struct sock *sk, int level, int optname,
 	struct udp_sock *up = udp_sk(sk);
 	int val, len;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	if (len < 0)
@@ -3105,7 +3105,7 @@ int udp_lib_getsockopt(struct sock *sk, int level, int optname,
 		return -ENOPROTOOPT;
 	}
 
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 	if (copy_to_user(optval, &val, len))
 		return -EFAULT;
net/ipv6/ipv6_sockglue.c +2 -2
index 1e225e6489ea..9b1843288035 100644
@@ -1487,12 +1487,12 @@ int ipv6_getsockopt(struct sock *sk, int level, int optname,
 	if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
 		int len;
 
-		if (get_user(len, optlen))
+		if (get_optlen(len, optlen))
 			return -EFAULT;
 
 		err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
 		if (err >= 0)
-			err = put_user(len, optlen);
+			err = put_optlen(len, optlen);
 	}
 #endif
 	return err;
net/ipv6/raw.c +4 -4
index fda640ebd53f..90216d7e2af6 100644
@@ -957,13 +957,13 @@ static int rawv6_geticmpfilter(struct sock *sk, int optname,
 
 	switch (optname) {
 	case ICMPV6_FILTER:
-		if (get_user(len, optlen))
+		if (get_optlen(len, optlen))
 			return -EFAULT;
 		if (len < 0)
 			return -EINVAL;
 		if (len > sizeof(struct icmp6_filter))
 			len = sizeof(struct icmp6_filter);
-		if (put_user(len, optlen))
+		if (put_optlen(len, optlen))
 			return -EFAULT;
 		if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
 			return -EFAULT;
@@ -1055,7 +1055,7 @@ static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
 	struct raw6_sock *rp = raw6_sk(sk);
 	int val, len;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	switch (optname) {
@@ -1080,7 +1080,7 @@ static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
 
 	len = min_t(unsigned int, sizeof(int), len);
 
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 	if (copy_to_user(optval, &val, len))
 		return -EFAULT;
net/iucv/af_iucv.c +2 -2
index cc2b3c44bc05..ce0c68c9513c 100644
@@ -1543,7 +1543,7 @@ static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_IUCV)
 		return -ENOPROTOOPT;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	if (len < 0)
@@ -1572,7 +1572,7 @@ static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
 		return -ENOPROTOOPT;
 	}
 
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 	if (copy_to_user(optval, &val, len))
 		return -EFAULT;
net/kcm/kcmsock.c +2 -2
index 24aec295a51c..68b6a8bd0cdb 100644
@@ -1153,7 +1153,7 @@ static int kcm_getsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_KCM)
 		return -ENOPROTOOPT;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	if (len < 0)
@@ -1169,7 +1169,7 @@ static int kcm_getsockopt(struct socket *sock, int level, int optname,
 		return -ENOPROTOOPT;
 	}
 
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 	if (copy_to_user(optval, &val, len))
 		return -EFAULT;
net/l2tp/l2tp_ppp.c +2 -2
index fc5c2fd8f34c..aa3e34ef6b5c 100644
@@ -1343,7 +1343,7 @@ static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_PPPOL2TP)
 		return -EINVAL;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	if (len < 0)
@@ -1374,7 +1374,7 @@ static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
 	}
 
 	err = -EFAULT;
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		goto end_put_sess;
 
 	if (copy_to_user((void __user *)optval, &val, len))
net/llc/af_llc.c +2 -2
index 0259cde394ba..a8e5d6eb5ad1 100644
@@ -1179,7 +1179,7 @@ static int llc_ui_getsockopt(struct socket *sock, int level, int optname,
 	lock_sock(sk);
 	if (unlikely(level != SOL_LLC))
 		goto out;
-	rc = get_user(len, optlen);
+	rc = get_optlen(len, optlen);
 	if (rc)
 		goto out;
 	rc = -EINVAL;
@@ -1210,7 +1210,7 @@ static int llc_ui_getsockopt(struct socket *sock, int level, int optname,
 		goto out;
 	}
 	rc = 0;
-	if (put_user(len, optlen) || copy_to_user(optval, &val, len))
+	if (put_optlen(len, optlen) || copy_to_user(optval, &val, len))
 		rc = -EFAULT;
 out:
 	release_sock(sk);
net/mctp/af_mctp.c +1 -1
index f6de136008f6..2cff81d47b76 100644
@@ -335,7 +335,7 @@ static int mctp_getsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_MCTP)
 		return -EINVAL;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	if (optname == MCTP_OPT_ADDR_EXT) {
net/mptcp/sockopt.c +9 -9
index 3caa0a9d3b38..25b780598888 100644
@@ -996,7 +996,7 @@ static int mptcp_getsockopt_info(struct mptcp_sock *msk, char __user *optval, in
 	struct mptcp_info m_info;
 	int len;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	/* When used only to check if a fallback to TCP happened. */
@@ -1007,7 +1007,7 @@ static int mptcp_getsockopt_info(struct mptcp_sock *msk, char __user *optval, in
 
 	mptcp_diag_fill_info(msk, &m_info);
 
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 
 	if (copy_to_user(optval, &m_info, len))
@@ -1028,7 +1028,7 @@ static int mptcp_put_subflow_data(struct mptcp_subflow_data *sfd,
 	else
 		copied = copylen;
 
-	if (put_user(copied, optlen))
+	if (put_optlen(copied, optlen))
 		return -EFAULT;
 
 	if (copy_to_user(optval, sfd, copylen))
@@ -1043,7 +1043,7 @@ static int mptcp_get_subflow_data(struct mptcp_subflow_data *sfd,
 {
 	int len, copylen;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	/* if mptcp_subflow_data size is changed, need to adjust
@@ -1229,7 +1229,7 @@ static int mptcp_get_full_info(struct mptcp_full_info *mfi,
 	BUILD_BUG_ON(offsetof(struct mptcp_full_info, mptcp_info) !=
 		     MIN_FULL_INFO_OPTLEN_SIZE);
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	if (len < MIN_FULL_INFO_OPTLEN_SIZE)
@@ -1257,7 +1257,7 @@ static int mptcp_put_full_info(struct mptcp_full_info *mfi,
 			       int __user *optlen)
 {
 	copylen += MIN_FULL_INFO_OPTLEN_SIZE;
-	if (put_user(copylen, optlen))
+	if (put_optlen(copylen, optlen))
 		return -EFAULT;
 
 	if (copy_to_user(optval, mfi, copylen))
@@ -1344,7 +1344,7 @@ static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optval,
 {
 	int len;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 	if (len < 0)
 		return -EINVAL;
@@ -1353,13 +1353,13 @@ static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optval,
 		unsigned char ucval = (unsigned char)val;
 
 		len = 1;
-		if (put_user(len, optlen))
+		if (put_optlen(len, optlen))
 			return -EFAULT;
 		if (copy_to_user(optval, &ucval, 1))
 			return -EFAULT;
 	} else {
 		len = min_t(unsigned int, len, sizeof(int));
-		if (put_user(len, optlen))
+		if (put_optlen(len, optlen))
 			return -EFAULT;
 		if (copy_to_user(optval, &val, len))
 			return -EFAULT;
net/netlink/af_netlink.c +3 -3
index e8972a857e51..3cde0f15deed 100644
@@ -1715,7 +1715,7 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_NETLINK)
 		return -ENOPROTOOPT;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 	if (len < 0)
 		return -EINVAL;
@@ -1746,7 +1746,7 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
 				break;
 			}
 		}
-		if (put_user(ALIGN(BITS_TO_BYTES(nlk->ngroups), sizeof(u32)), optlen))
+		if (put_optlen(ALIGN(BITS_TO_BYTES(nlk->ngroups), sizeof(u32)), optlen))
 			err = -EFAULT;
 		netlink_unlock_table();
 		return err;
@@ -1773,7 +1773,7 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
 	len = sizeof(int);
 	val = test_bit(flag, &nlk->flags);
 
-	if (put_user(len, optlen) ||
+	if (put_optlen(len, optlen) ||
 	    copy_to_user(optval, &val, len))
 		return -EFAULT;
 
net/netrom/af_netrom.c +2 -2
index 6ee148f0e6d0..6039b5219460 100644
@@ -356,7 +356,7 @@ static int nr_getsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_NETROM)
 		return -ENOPROTOOPT;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	if (len < 0)
@@ -389,7 +389,7 @@ static int nr_getsockopt(struct socket *sock, int level, int optname,
 
 	len = min_t(unsigned int, len, sizeof(int));
 
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 
 	return copy_to_user(optval, &val, len) ? -EFAULT : 0;
net/nfc/llcp_sock.c +2 -2
index 57a2f97004e1..5e588640c22f 100644
@@ -316,7 +316,7 @@ static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_NFC)
 		return -ENOPROTOOPT;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	local = llcp_sock->local;
@@ -372,7 +372,7 @@ static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname,
 
 	release_sock(sk);
 
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 
 	return err;
net/packet/af_packet.c +2 -2
index 3e9ddf72cd03..f35ab96fbcad 100644
@@ -4117,7 +4117,7 @@ static int packet_getsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_PACKET)
 		return -ENOPROTOOPT;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	if (len < 0)
@@ -4223,7 +4223,7 @@ static int packet_getsockopt(struct socket *sock, int level, int optname,
 
 	if (len > lv)
 		len = lv;
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 	if (copy_to_user(optval, data, len))
 		return -EFAULT;
net/phonet/pep.c +2 -2
index 53a858478e22..78b269ddf28b 100644
@@ -1070,7 +1070,7 @@ static int pep_getsockopt(struct sock *sk, int level, int optname,
 
 	if (level != SOL_PNPIPE)
 		return -ENOPROTOOPT;
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	switch (optname) {
@@ -1097,7 +1097,7 @@ static int pep_getsockopt(struct sock *sk, int level, int optname,
 	}
 
 	len = min_t(unsigned int, sizeof(int), len);
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 	if (put_user(val, (int __user *) optval))
 		return -EFAULT;
net/rds/af_rds.c +3 -3
index 8435a20968ef..3395062245c5 100644
@@ -490,7 +490,7 @@ static int rds_getsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_RDS)
 		goto out;
 
-	if (get_user(len, optlen)) {
+	if (get_optlen(len, optlen)) {
 		ret = -EFAULT;
 		goto out;
 	}
@@ -506,7 +506,7 @@ static int rds_getsockopt(struct socket *sock, int level, int optname,
 			ret = -EINVAL;
 		else
 		if (put_user(rs->rs_recverr, (int __user *) optval) ||
-		    put_user(sizeof(int), optlen))
+		    put_optlen(sizeof(int), optlen))
 			ret = -EFAULT;
 		else
 			ret = 0;
@@ -519,7 +519,7 @@ static int rds_getsockopt(struct socket *sock, int level, int optname,
 		trans = (rs->rs_transport ? rs->rs_transport->t_type :
 			 RDS_TRANS_NONE); /* unbound */
 		if (put_user(trans, (int __user *)optval) ||
-		    put_user(sizeof(int), optlen))
+		    put_optlen(sizeof(int), optlen))
 			ret = -EFAULT;
 		else
 			ret = 0;
net/rds/info.c +2 -2
index b6b46a8214a0..1990d068f6ee 100644
@@ -168,7 +168,7 @@ int rds_info_getsockopt(struct socket *sock, int optname, char __user *optval,
 	int len;
 	int total;
 
-	if (get_user(len, optlen)) {
+	if (get_optlen(len, optlen)) {
 		ret = -EFAULT;
 		goto out;
 	}
@@ -230,7 +230,7 @@ int rds_info_getsockopt(struct socket *sock, int optname, char __user *optval,
 		ret = lens.each;
 	}
 
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		ret = -EFAULT;
 
 out:
net/rose/af_rose.c +2 -2
index a4a668b88a8f..a1299e9dd3e6 100644
@@ -463,7 +463,7 @@ static int rose_getsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_ROSE)
 		return -ENOPROTOOPT;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	if (len < 0)
@@ -504,7 +504,7 @@ static int rose_getsockopt(struct socket *sock, int level, int optname,
 
 	len = min_t(unsigned int, len, sizeof(int));
 
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 
 	return copy_to_user(optval, &val, len) ? -EFAULT : 0;
net/rxrpc/af_rxrpc.c +2 -2
index 86873399f7d5..a88c635888fd 100644
@@ -763,7 +763,7 @@ static int rxrpc_getsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_RXRPC)
 		return -EOPNOTSUPP;
 
-	if (get_user(optlen, _optlen))
+	if (get_optlen(optlen, _optlen))
 		return -EFAULT;
 
 	switch (optname) {
@@ -771,7 +771,7 @@ static int rxrpc_getsockopt(struct socket *sock, int level, int optname,
 		if (optlen < sizeof(int))
 			return -ETOOSMALL;
 		if (put_user(RXRPC__SUPPORTED - 1, (int __user *)optval) ||
-		    put_user(sizeof(int), _optlen))
+		    put_optlen(sizeof(int), _optlen))
 			return -EFAULT;
 		return 0;
 
net/sctp/socket.c +54 -54

432 lines not on this page · raw message

net/smc/af_smc.c +2 -2
index 3e6cb35baf25..405c0bff7121 100644
@@ -2996,7 +2996,7 @@ static int __smc_getsockopt(struct socket *sock, int level, int optname,
 
 	smc = smc_sk(sock->sk);
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	len = min_t(int, len, sizeof(int));
@@ -3012,7 +3012,7 @@ static int __smc_getsockopt(struct socket *sock, int level, int optname,
 		return -EOPNOTSUPP;
 	}
 
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 	if (copy_to_user(optval, &val, len))
 		return -EFAULT;
net/tipc/socket.c +3 -3
index 65dcbb54f55d..23822d9230e4 100644
@@ -3239,10 +3239,10 @@ static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
 	int res;
 
 	if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
-		return put_user(0, ol);
+		return put_optlen(0, ol);
 	if (lvl != SOL_TIPC)
 		return -ENOPROTOOPT;
-	res = get_user(len, ol);
+	res = get_optlen(len, ol);
 	if (res)
 		return res;
 
@@ -3292,7 +3292,7 @@ static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
 	if (copy_to_user(ov, &value, sizeof(value)))
 		return -EFAULT;
 
-	return put_user(sizeof(value), ol);
+	return put_optlen(sizeof(value), ol);
 }
 
 static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
net/tls/tls_main.c +4 -4
index cb86b0bf9a53..f4e87b4295b4 100644
@@ -445,7 +445,7 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
 	struct cipher_context *cctx;
 	int len;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	if (!optval || (len < sizeof(*crypto_info))) {
@@ -503,7 +503,7 @@ static int do_tls_getsockopt_tx_zc(struct sock *sk, char __user *optval,
 	unsigned int value;
 	int len;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	if (len != sizeof(value))
@@ -525,7 +525,7 @@ static int do_tls_getsockopt_no_pad(struct sock *sk, char __user *optval,
 	if (ctx->prot_info.version != TLS_1_3_VERSION)
 		return -EINVAL;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 	if (len < sizeof(value))
 		return -EINVAL;
@@ -536,7 +536,7 @@ static int do_tls_getsockopt_no_pad(struct sock *sk, char __user *optval,
 	if (value < 0)
 		return value;
 
-	if (put_user(sizeof(value), optlen))
+	if (put_optlen(sizeof(value), optlen))
 		return -EFAULT;
 	if (copy_to_user(optval, &value, sizeof(value)))
 		return -EFAULT;
net/vmw_vsock/af_vsock.c +2 -2
index 7e3db87ae433..c21a3bfcdd75 100644
@@ -1884,7 +1884,7 @@ static int vsock_connectible_getsockopt(struct socket *sock,
 	if (level != AF_VSOCK)
 		return -ENOPROTOOPT;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 
 	memset(&v, 0, sizeof(v));
@@ -1919,7 +1919,7 @@ static int vsock_connectible_getsockopt(struct socket *sock,
 	if (copy_to_user(optval, &v, len))
 		return -EFAULT;
 
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		return -EFAULT;
 
 	return 0;
net/x25/af_x25.c +2 -2
index 8dda4178497c..4eb65c05b3b9 100644
@@ -457,7 +457,7 @@ static int x25_getsockopt(struct socket *sock, int level, int optname,
 		goto out;
 
 	rc = -EFAULT;
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		goto out;
 
 	rc = -EINVAL;
@@ -467,7 +467,7 @@ static int x25_getsockopt(struct socket *sock, int level, int optname,
 	len = min_t(unsigned int, len, sizeof(int));
 
 	rc = -EFAULT;
-	if (put_user(len, optlen))
+	if (put_optlen(len, optlen))
 		goto out;
 
 	val = test_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
net/xdp/xsk.c +4 -4
index e5d104ce7b82..7cae6f4114b5 100644
@@ -1470,7 +1470,7 @@ static int xsk_getsockopt(struct socket *sock, int level, int optname,
 	if (level != SOL_XDP)
 		return -ENOPROTOOPT;
 
-	if (get_user(len, optlen))
+	if (get_optlen(len, optlen))
 		return -EFAULT;
 	if (len < 0)
 		return -EINVAL;
@@ -1507,7 +1507,7 @@ static int xsk_getsockopt(struct socket *sock, int level, int optname,
 
 		if (copy_to_user(optval, &stats, stats_size))
 			return -EFAULT;
-		if (put_user(stats_size, optlen))
+		if (put_optlen(stats_size, optlen))
 			return -EFAULT;
 
 		return 0;
@@ -1559,7 +1559,7 @@ static int xsk_getsockopt(struct socket *sock, int level, int optname,
 
 		if (copy_to_user(optval, to_copy, len))
 			return -EFAULT;
-		if (put_user(len, optlen))
+		if (put_optlen(len, optlen))
 			return -EFAULT;
 
 		return 0;
@@ -1579,7 +1579,7 @@ static int xsk_getsockopt(struct socket *sock, int level, int optname,
 		len = sizeof(opts);
 		if (copy_to_user(optval, &opts, len))
 			return -EFAULT;
-		if (put_user(len, optlen))
+		if (put_optlen(len, optlen))
 			return -EFAULT;
 
 		return 0;
Signature · 1 line
2.34.1
#5 lore raw

RFC PATCH 2/4 net: pass 'optlen_t' to proto[ops].getsockopt() hooks

In reply to Stefan Metzmacher

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.

This step only introduces 'optlen_t' as a trivial wrapper of the 'int __user *', it makes sure that the optlen argument is only ever used by get_optval(), put_optval.
For some corner cases OPTLEN_SOCKPTR().

We still expect a __user pointer, so this should be easy to review and don't change the logic.

Cc Jens Axboe <[email protected]>
Cc Pavel Begunkov <[email protected]>
Cc Breno Leitao <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Karsten Keil <[email protected]>
Cc Ayush Sawal <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Simon Horman <[email protected]>
Cc Kuniyuki Iwashima <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc David Ahern <[email protected]>
Cc Marcelo Ricardo Leitner <[email protected]>
Cc Xin Long <[email protected]>
Cc Neal Cardwell <[email protected]>
Cc Joerg Reuter <[email protected]>
Cc Marcel Holtmann <[email protected]>
Cc Johan Hedberg <[email protected]>
Cc Luiz Augusto von Dentz <[email protected]>
Cc Oliver Hartkopp <[email protected]>
Cc Marc Kleine-Budde <[email protected]>
Cc Robin van der Gracht <[email protected]>
Cc Oleksij Rempel <[email protected]>
Cc Alexander Aring <[email protected]>
Cc Stefan Schmidt <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Alexandra Winter <[email protected]>
Cc Thorsten Winkler <[email protected]>
Cc James Chapman <[email protected]>
Cc Jeremy Kerr <[email protected]>
Cc Matt Johnston <[email protected]>
Cc Matthieu Baerts <[email protected]>
Cc Mat Martineau <[email protected]>
Cc Geliang Tang <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Remi Denis-Courmont <[email protected]>
Cc Allison Henderson <[email protected]>
Cc David Howells <[email protected]>
Cc Marc Dionne <[email protected]>
Cc Wenjia Zhang <[email protected]>
Cc Jan Karcher <[email protected]>
Cc "D. Wythe" <[email protected]>
Cc Tony Lu <[email protected]>
Cc Wen Gu <[email protected]>
Cc Jon Maloy <[email protected]>
Cc Boris Pismenny <[email protected]>
Cc John Fastabend <[email protected]>
Cc Stefano Garzarella <[email protected]>
Cc Martin Schiller <[email protected]>
Cc "Björn Töpel" <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maciej Fijalkowski <[email protected]>
Cc Jonathan Lemon <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
CC Stefan Metzmacher <[email protected]>
---
 drivers/isdn/mISDN/socket.c                   |   2 +-
 .../chelsio/inline_crypto/chtls/chtls_main.c  |   4 +-
 include/linux/net.h                           |   2 +-
 include/linux/sockptr.h                       |  17 ++-
 include/net/inet_connection_sock.h            |   2 +-
 include/net/ip.h                              |   2 +-
 include/net/ipv6.h                            |   2 +-
 include/net/sctp/structs.h                    |   2 +-
 include/net/sock.h                            |   4 +-
 include/net/tcp.h                             |   2 +-
 include/net/udp.h                             |   2 +-
 net/atm/common.c                              |   2 +-
 net/atm/common.h                              |   2 +-
 net/atm/pvc.c                                 |   2 +-
 net/atm/svc.c                                 |   2 +-
 net/ax25/af_ax25.c                            |   2 +-
 net/bluetooth/hci_sock.c                      |   4 +-
 net/bluetooth/iso.c                           |   2 +-
 net/bluetooth/l2cap_sock.c                    |   4 +-
 net/bluetooth/rfcomm/sock.c                   |   4 +-
 net/bluetooth/sco.c                           |   4 +-
 net/can/isotp.c                               |   2 +-
 net/can/j1939/socket.c                        |   2 +-
 net/can/raw.c                                 |   2 +-
 net/core/sock.c                               |   2 +-
 net/dccp/ccid.c                               |   2 +-
 net/dccp/ccid.h                               |  10 +-
 net/dccp/ccids/ccid3.c                        |   4 +-
 net/dccp/dccp.h                               |   2 +-
 net/dccp/proto.c                              |   6 +-
 net/ieee802154/socket.c                       |   4 +-
 net/ipv4/ip_sockglue.c                        |   4 +-
 net/ipv4/raw.c                                |   6 +-
 net/ipv4/tcp.c                                |   4 +-
 net/ipv4/udp.c                                |   4 +-
 net/ipv4/udp_impl.h                           |   2 +-
 net/ipv6/ipv6_sockglue.c                      |   4 +-
 net/ipv6/raw.c                                |   6 +-
 net/ipv6/udp.c                                |   2 +-
 net/ipv6/udp_impl.h                           |   2 +-
 net/iucv/af_iucv.c                            |   2 +-
 net/kcm/kcmsock.c                             |   2 +-
 net/l2tp/l2tp_ppp.c                           |   2 +-
 net/llc/af_llc.c                              |   2 +-
 net/mctp/af_mctp.c                            |   2 +-
 net/mptcp/protocol.h                          |   2 +-
 net/mptcp/sockopt.c                           |  30 ++---
 net/netlink/af_netlink.c                      |   2 +-
 net/netrom/af_netrom.c                        |   2 +-
 net/nfc/llcp_sock.c                           |   2 +-
 net/packet/af_packet.c                        |   2 +-
 net/phonet/pep.c                              |   2 +-
 net/rds/af_rds.c                              |   2 +-
 net/rds/info.c                                |   2 +-
 net/rds/info.h                                |   2 +-
 net/rose/af_rose.c                            |   2 +-
 net/rxrpc/af_rxrpc.c                          |   2 +-
 net/sctp/socket.c                             | 112 +++++++++---------
 net/smc/af_smc.c                              |   4 +-
 net/smc/smc.h                                 |   2 +-
 net/socket.c                                  |   7 +-
 net/tipc/socket.c                             |   2 +-
 net/tls/tls_main.c                            |  10 +-
 net/vmw_vsock/af_vsock.c                      |   2 +-
 net/x25/af_x25.c                              |   2 +-
 net/xdp/xsk.c                                 |   2 +-
 66 files changed, 178 insertions(+), 166 deletions(-)
drivers/isdn/mISDN/socket.c +1 -1
index b750cc0dfa4a..233426f24ab6 100644
@@ -433,7 +433,7 @@ static int data_sock_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int data_sock_getsockopt(struct socket *sock, int level, int optname,
-				char __user *optval, int __user *optlen)
+				char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	int len, opt;
drivers/net/ethernet/chelsio/inline_crypto/chtls/chtls_main.c +2 -2
index daa1ebaef511..ac73ed4fc5d4 100644
@@ -466,7 +466,7 @@ static int chtls_uld_rx_handler(void *handle, const __be64 *rsp,
 }
 
 static int do_chtls_getsockopt(struct sock *sk, char __user *optval,
-			       int __user *optlen)
+			       optlen_t optlen)
 {
 	struct tls_crypto_info crypto_info = { 0 };
 
@@ -477,7 +477,7 @@ static int do_chtls_getsockopt(struct sock *sk, char __user *optval,
 }
 
 static int chtls_getsockopt(struct sock *sk, int level, int optname,
-			    char __user *optval, int __user *optlen)
+			    char __user *optval, optlen_t optlen)
 {
 	struct tls_context *ctx = tls_get_ctx(sk);
 
include/linux/net.h +1 -1
index 0ff950eecc6b..f8b2728f993c 100644
@@ -193,7 +193,7 @@ struct proto_ops {
 				      int optname, sockptr_t optval,
 				      unsigned int optlen);
 	int		(*getsockopt)(struct socket *sock, int level,
-				      int optname, char __user *optval, int __user *optlen);
+				      int optname, char __user *optval, optlen_t optlen);
 	void		(*show_fdinfo)(struct seq_file *m, struct socket *sock);
 	int		(*sendmsg)   (struct socket *sock, struct msghdr *m,
 				      size_t total_len);
include/linux/sockptr.h +13 -4
index 28dbc3e27374..1baf66f26f4f 100644
@@ -169,17 +169,21 @@ static inline int check_zeroed_sockptr(sockptr_t src, size_t offset,
 	return memchr_inv(src.kernel + offset, 0, size) == NULL;
 }
 
+typedef struct {
+	int __user *up;
+} optlen_t;
+
 #define __check_optlen_t(__optlen)				\
 ({								\
-	int __user *__ptr __maybe_unused = __optlen; 		\
-	BUILD_BUG_ON(sizeof(*(__ptr)) != sizeof(int));		\
+	optlen_t *__ptr __maybe_unused = &__optlen; \
+	BUILD_BUG_ON(sizeof(*((__ptr)->up)) != sizeof(int));	\
 })
 
 #define get_optlen(__val, __optlen)				\
 ({								\
 	long __err;						\
 	__check_optlen_t(__optlen);				\
-	__err = get_user(__val, __optlen);			\
+	__err = get_user(__val, __optlen.up);			\
 	__err;							\
 })
 
@@ -187,8 +191,13 @@ static inline int check_zeroed_sockptr(sockptr_t src, size_t offset,
 ({								\
 	long __err;						\
 	__check_optlen_t(__optlen);				\
-	__err = put_user(__val, __optlen);			\
+	__err = put_user(__val, __optlen.up);			\
 	__err;							\
 })
 
+static inline sockptr_t OPTLEN_SOCKPTR(optlen_t optlen)
+{
+	return (sockptr_t) { .user = optlen.up, };
+}
+
 #endif /* _LINUX_SOCKPTR_H */
include/net/inet_connection_sock.h +1 -1
index 1735db332aab..3a3d03308611 100644
@@ -47,7 +47,7 @@ struct inet_connection_sock_af_ops {
 	int	    (*setsockopt)(struct sock *sk, int level, int optname,
 				  sockptr_t optval, unsigned int optlen);
 	int	    (*getsockopt)(struct sock *sk, int level, int optname,
-				  char __user *optval, int __user *optlen);
+				  char __user *optval, optlen_t optlen);
 	void	    (*mtu_reduced)(struct sock *sk);
 };
 
include/net/ip.h +1 -1
index 8a48ade24620..9f725642a42e 100644
@@ -802,7 +802,7 @@ int ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
 int do_ip_getsockopt(struct sock *sk, int level, int optname,
 		     sockptr_t optval, sockptr_t optlen);
 int ip_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
-		  int __user *optlen);
+		  optlen_t optlen);
 int ip_ra_control(struct sock *sk, unsigned char on,
 		  void (*destructor)(struct sock *));
 
include/net/ipv6.h +1 -1
index 2ccdf85f34f1..99e655db1dde 100644
@@ -1186,7 +1186,7 @@ int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
 int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
 		       sockptr_t optval, sockptr_t optlen);
 int ipv6_getsockopt(struct sock *sk, int level, int optname,
-		    char __user *optval, int __user *optlen);
+		    char __user *optval, optlen_t optlen);
 
 int __ip6_datagram_connect(struct sock *sk, struct sockaddr *addr,
 			   int addr_len);
include/net/sctp/structs.h +1 -1
index 31248cfdfb23..15f9b9ece3ea 100644
@@ -441,7 +441,7 @@ struct sctp_af {
 					 int level,
 					 int optname,
 					 char __user *optval,
-					 int __user *optlen);
+					 optlen_t optlen);
 	void		(*get_dst)	(struct sctp_transport *t,
 					 union sctp_addr *saddr,
 					 struct flowi *fl,
include/net/sock.h +2 -2
index 8daf1b3b12c6..94c0e90d8901 100644
@@ -1248,7 +1248,7 @@ struct proto {
 					unsigned int optlen);
 	int			(*getsockopt)(struct sock *sk, int level,
 					int optname, char __user *optval,
-					int __user *option);
+					optlen_t optlen);
 	void			(*keepalive)(struct sock *sk, int valbool);
 #ifdef CONFIG_COMPAT
 	int			(*compat_ioctl)(struct sock *sk,
@@ -1856,7 +1856,7 @@ int sock_no_mmap(struct file *file, struct socket *sock,
  * uses the inet style.
  */
 int sock_common_getsockopt(struct socket *sock, int level, int optname,
-				  char __user *optval, int __user *optlen);
+				  char __user *optval, optlen_t optlen);
 int sock_common_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
 			int flags);
 int sock_common_setsockopt(struct socket *sock, int level, int optname,
include/net/tcp.h +1 -1
index df04dc09c519..75e6bcd6eac4 100644
@@ -449,7 +449,7 @@ __poll_t tcp_poll(struct file *file, struct socket *sock,
 int do_tcp_getsockopt(struct sock *sk, int level,
 		      int optname, sockptr_t optval, sockptr_t optlen);
 int tcp_getsockopt(struct sock *sk, int level, int optname,
-		   char __user *optval, int __user *optlen);
+		   char __user *optval, optlen_t optlen);
 bool tcp_bpf_bypass_getsockopt(int level, int optname);
 int do_tcp_setsockopt(struct sock *sk, int level, int optname,
 		      sockptr_t optval, unsigned int optlen);
include/net/udp.h +1 -1
index 6e89520e100d..a846681ae497 100644
@@ -418,7 +418,7 @@ struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
 				       netdev_features_t features,
 				       bool is_ipv6);
 int udp_lib_getsockopt(struct sock *sk, int level, int optname,
-		       char __user *optval, int __user *optlen);
+		       char __user *optval, optlen_t optlen);
 int udp_lib_setsockopt(struct sock *sk, int level, int optname,
 		       sockptr_t optval, unsigned int optlen,
 		       int (*push_pending_frames)(struct sock *));
net/atm/common.c +1 -1
index e95371abd705..55844a930ccf 100644
@@ -787,7 +787,7 @@ int vcc_setsockopt(struct socket *sock, int level, int optname,
 }
 
 int vcc_getsockopt(struct socket *sock, int level, int optname,
-		   char __user *optval, int __user *optlen)
+		   char __user *optval, optlen_t optlen)
 {
 	struct atm_vcc *vcc;
 	int len;
net/atm/common.h +1 -1
index a1e56e8de698..67a25f92a929 100644
@@ -23,7 +23,7 @@ int vcc_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
 int vcc_setsockopt(struct socket *sock, int level, int optname,
 		   sockptr_t optval, unsigned int optlen);
 int vcc_getsockopt(struct socket *sock, int level, int optname,
-		   char __user *optval, int __user *optlen);
+		   char __user *optval, optlen_t optlen);
 void vcc_process_recv_queue(struct atm_vcc *vcc);
 
 int atmpvc_init(void);
net/atm/pvc.c +1 -1
index 66d9a9bd5896..2f01f862d0fb 100644
@@ -75,7 +75,7 @@ static int pvc_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int pvc_getsockopt(struct socket *sock, int level, int optname,
-			  char __user *optval, int __user *optlen)
+			  char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	int error;
net/atm/svc.c +1 -1
index a706c5f77d8e..600f7d381348 100644
@@ -501,7 +501,7 @@ static int svc_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int svc_getsockopt(struct socket *sock, int level, int optname,
-			  char __user *optval, int __user *optlen)
+			  char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	int error = 0, len;
net/ax25/af_ax25.c +1 -1
index b184e2cb4b50..0e069f2ceb12 100644
@@ -715,7 +715,7 @@ static int ax25_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int ax25_getsockopt(struct socket *sock, int level, int optname,
-	char __user *optval, int __user *optlen)
+	char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	ax25_cb *ax25;
net/bluetooth/hci_sock.c +2 -2
index 4b4e476e4e7c..90949b8dad2e 100644
@@ -2053,7 +2053,7 @@ static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int hci_sock_getsockopt_old(struct socket *sock, int level, int optname,
-				   char __user *optval, int __user *optlen)
+				   char __user *optval, optlen_t optlen)
 {
 	struct hci_ufilter uf;
 	struct sock *sk = sock->sk;
@@ -2119,7 +2119,7 @@ static int hci_sock_getsockopt_old(struct socket *sock, int level, int optname,
 }
 
 static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
-			       char __user *optval, int __user *optlen)
+			       char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	int err = 0;
net/bluetooth/iso.c +1 -1
index f6624b6e5485..72369c912161 100644
@@ -1721,7 +1721,7 @@ static int iso_sock_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int iso_sock_getsockopt(struct socket *sock, int level, int optname,
-			       char __user *optval, int __user *optlen)
+			       char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	int len, err = 0;
net/bluetooth/l2cap_sock.c +2 -2
index 8ae25a918fd3..ac36d0ec08d3 100644
@@ -433,7 +433,7 @@ static int l2cap_get_mode(struct l2cap_chan *chan)
 }
 
 static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
-				     char __user *optval, int __user *optlen)
+				     char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
@@ -553,7 +553,7 @@ static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
 }
 
 static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname,
-				 char __user *optval, int __user *optlen)
+				 char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
net/bluetooth/rfcomm/sock.c +2 -2
index 785894b79dd8..10ec25dc038d 100644
@@ -722,7 +722,7 @@ static int rfcomm_sock_setsockopt(struct socket *sock, int level, int optname,
 	return err;
 }
 
-static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
+static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct sock *l2cap_sk;
@@ -798,7 +798,7 @@ static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __u
 	return err;
 }
 
-static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
+static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct bt_security sec;
net/bluetooth/sco.c +2 -2
index 25910eca759c..e95d924fe41d 100644
@@ -1037,7 +1037,7 @@ static int sco_sock_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int sco_sock_getsockopt_old(struct socket *sock, int optname,
-				   char __user *optval, int __user *optlen)
+				   char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct sco_options opts;
@@ -1099,7 +1099,7 @@ static int sco_sock_getsockopt_old(struct socket *sock, int optname,
 }
 
 static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
-			       char __user *optval, int __user *optlen)
+			       char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	int len, err = 0;
net/can/isotp.c +1 -1
index aa9ab87d5e14..2a321400a9f8 100644
@@ -1486,7 +1486,7 @@ static int isotp_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int isotp_getsockopt(struct socket *sock, int level, int optname,
-			    char __user *optval, int __user *optlen)
+			    char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct isotp_sock *so = isotp_sk(sk);
net/can/j1939/socket.c +1 -1
index b2b538528c2e..606f8b3ac96f 100644
@@ -754,7 +754,7 @@ static int j1939_sk_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int j1939_sk_getsockopt(struct socket *sock, int level, int optname,
-			       char __user *optval, int __user *optlen)
+			       char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct j1939_sock *jsk = j1939_sk(sk);
net/can/raw.c +1 -1
index 8d1a1626d1a4..59c6d701db05 100644
@@ -753,7 +753,7 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int raw_getsockopt(struct socket *sock, int level, int optname,
-			  char __user *optval, int __user *optlen)
+			  char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct raw_sock *ro = raw_sk(sk);
net/core/sock.c +1 -1
index 323892066def..2cd7bd5b2a05 100644
@@ -3854,7 +3854,7 @@ EXPORT_SYMBOL(sock_recv_errqueue);
  *	this means if you specify SO_ERROR (otherwise what is the point of it).
  */
 int sock_common_getsockopt(struct socket *sock, int level, int optname,
-			   char __user *optval, int __user *optlen)
+			   char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 
net/dccp/ccid.c +1 -1
index 6f495ffb1d60..b5fad2a7e9a8 100644
@@ -58,7 +58,7 @@ int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len)
 }
 
 int ccid_getsockopt_builtin_ccids(struct sock *sk, int len,
-				  char __user *optval, int __user *optlen)
+				  char __user *optval, optlen_t optlen)
 {
 	u8 *ccid_array, array_len;
 	int err = 0;
net/dccp/ccid.h +5 -5
index 105f3734dadb..2758d760af36 100644
@@ -78,11 +78,11 @@ struct ccid_operations {
 	int		(*ccid_hc_rx_getsockopt)(struct sock *sk,
 						 const int optname, int len,
 						 u32 __user *optval,
-						 int __user *optlen);
+						 optlen_t optlen);
 	int		(*ccid_hc_tx_getsockopt)(struct sock *sk,
 						 const int optname, int len,
 						 u32 __user *optval,
-						 int __user *optlen);
+						 optlen_t optlen);
 };
 
 extern struct ccid_operations ccid2_ops;
@@ -106,7 +106,7 @@ static inline void *ccid_priv(const struct ccid *ccid)
 bool ccid_support_check(u8 const *ccid_array, u8 array_len);
 int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len);
 int ccid_getsockopt_builtin_ccids(struct sock *sk, int len,
-				  char __user *, int __user *);
+				  char __user *, optlen_t );
 
 struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx);
 
@@ -240,7 +240,7 @@ static inline void ccid_hc_tx_get_info(struct ccid *ccid, struct sock *sk,
 
 static inline int ccid_hc_rx_getsockopt(struct ccid *ccid, struct sock *sk,
 					const int optname, int len,
-					u32 __user *optval, int __user *optlen)
+					u32 __user *optval, optlen_t optlen)
 {
 	int rc = -ENOPROTOOPT;
 	if (ccid != NULL && ccid->ccid_ops->ccid_hc_rx_getsockopt != NULL)
@@ -251,7 +251,7 @@ static inline int ccid_hc_rx_getsockopt(struct ccid *ccid, struct sock *sk,
 
 static inline int ccid_hc_tx_getsockopt(struct ccid *ccid, struct sock *sk,
 					const int optname, int len,
-					u32 __user *optval, int __user *optlen)
+					u32 __user *optval, optlen_t optlen)
 {
 	int rc = -ENOPROTOOPT;
 	if (ccid != NULL && ccid->ccid_ops->ccid_hc_tx_getsockopt != NULL)
net/dccp/ccids/ccid3.c +2 -2
index 648aa5270f37..8e4f16ccba57 100644
@@ -518,7 +518,7 @@ static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
 }
 
 static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len,
-				  u32 __user *optval, int __user *optlen)
+				  u32 __user *optval, optlen_t optlen)
 {
 	const struct ccid3_hc_tx_sock *hc = ccid3_hc_tx_sk(sk);
 	struct tfrc_tx_info tfrc;
@@ -813,7 +813,7 @@ static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
 }
 
 static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len,
-				  u32 __user *optval, int __user *optlen)
+				  u32 __user *optval, optlen_t optlen)
 {
 	const struct ccid3_hc_rx_sock *hc = ccid3_hc_rx_sk(sk);
 	struct tfrc_rx_info rx_info;
net/dccp/dccp.h +1 -1
index 1f748ed1279d..f6d99913e1ca 100644
@@ -289,7 +289,7 @@ struct sk_buff *dccp_make_response(const struct sock *sk, struct dst_entry *dst,
 int dccp_connect(struct sock *sk);
 int dccp_disconnect(struct sock *sk, int flags);
 int dccp_getsockopt(struct sock *sk, int level, int optname,
-		    char __user *optval, int __user *optlen);
+		    char __user *optval, optlen_t optlen);
 int dccp_setsockopt(struct sock *sk, int level, int optname,
 		    sockptr_t optval, unsigned int optlen);
 int dccp_ioctl(struct sock *sk, int cmd, int *karg);
net/dccp/proto.c +3 -3
index 8d6461ef8b50..2255f359058d 100644
@@ -586,7 +586,7 @@ EXPORT_SYMBOL_GPL(dccp_setsockopt);
 
 static int dccp_getsockopt_service(struct sock *sk, int len,
 				   __be32 __user *optval,
-				   int __user *optlen)
+				   optlen_t optlen)
 {
 	const struct dccp_sock *dp = dccp_sk(sk);
 	const struct dccp_service_list *sl;
@@ -613,7 +613,7 @@ static int dccp_getsockopt_service(struct sock *sk, int len,
 }
 
 static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
-		    char __user *optval, int __user *optlen)
+		    char __user *optval, optlen_t optlen)
 {
 	struct dccp_sock *dp;
 	int val, len;
@@ -681,7 +681,7 @@ static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
 }
 
 int dccp_getsockopt(struct sock *sk, int level, int optname,
-		    char __user *optval, int __user *optlen)
+		    char __user *optval, optlen_t optlen)
 {
 	if (level != SOL_DCCP)
 		return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
net/ieee802154/socket.c +2 -2
index cc1788853c08..7fb31054c0ad 100644
@@ -380,7 +380,7 @@ static void ieee802154_raw_deliver(struct net_device *dev, struct sk_buff *skb)
 }
 
 static int raw_getsockopt(struct sock *sk, int level, int optname,
-			  char __user *optval, int __user *optlen)
+			  char __user *optval, optlen_t optlen)
 {
 	return -EOPNOTSUPP;
 }
@@ -832,7 +832,7 @@ static int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
 }
 
 static int dgram_getsockopt(struct sock *sk, int level, int optname,
-			    char __user *optval, int __user *optlen)
+			    char __user *optval, optlen_t optlen)
 {
 	struct dgram_sock *ro = dgram_sk(sk);
 
net/ipv4/ip_sockglue.c +2 -2
index 4d372f76b317..6757c8d12778 100644
@@ -1758,12 +1758,12 @@ int do_ip_getsockopt(struct sock *sk, int level, int optname,
 }
 
 int ip_getsockopt(struct sock *sk, int level,
-		  int optname, char __user *optval, int __user *optlen)
+		  int optname, char __user *optval, optlen_t optlen)
 {
 	int err;
 
 	err = do_ip_getsockopt(sk, level, optname,
-			       USER_SOCKPTR(optval), USER_SOCKPTR(optlen));
+			       USER_SOCKPTR(optval), OPTLEN_SOCKPTR(optlen));
 
 #ifdef CONFIG_NETFILTER
 	/* we need to exclude all possible ENOPROTOOPTs except default case */
net/ipv4/raw.c +3 -3
index 89d70acdacdc..de8c67c08c20 100644
@@ -807,7 +807,7 @@ static int raw_seticmpfilter(struct sock *sk, sockptr_t optval, int optlen)
 	return 0;
 }
 
-static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen)
+static int raw_geticmpfilter(struct sock *sk, char __user *optval, optlen_t optlen)
 {
 	int len, ret = -EFAULT;
 
@@ -847,7 +847,7 @@ static int raw_setsockopt(struct sock *sk, int level, int optname,
 }
 
 static int do_raw_getsockopt(struct sock *sk, int optname,
-			     char __user *optval, int __user *optlen)
+			     char __user *optval, optlen_t optlen)
 {
 	if (optname == ICMP_FILTER) {
 		if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
@@ -859,7 +859,7 @@ static int do_raw_getsockopt(struct sock *sk, int optname,
 }
 
 static int raw_getsockopt(struct sock *sk, int level, int optname,
-			  char __user *optval, int __user *optlen)
+			  char __user *optval, optlen_t optlen)
 {
 	if (level != SOL_RAW)
 		return ip_getsockopt(sk, level, optname, optval, optlen);
net/ipv4/tcp.c +2 -2
index ea8de00f669d..89d7a5b0364e 100644
@@ -4732,7 +4732,7 @@ bool tcp_bpf_bypass_getsockopt(int level, int optname)
 EXPORT_IPV6_MOD(tcp_bpf_bypass_getsockopt);
 
 int tcp_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
-		   int __user *optlen)
+		   optlen_t optlen)
 {
 	struct inet_connection_sock *icsk = inet_csk(sk);
 
@@ -4741,7 +4741,7 @@ int tcp_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
 		return READ_ONCE(icsk->icsk_af_ops)->getsockopt(sk, level, optname,
 								optval, optlen);
 	return do_tcp_getsockopt(sk, level, optname, USER_SOCKPTR(optval),
-				 USER_SOCKPTR(optlen));
+				 OPTLEN_SOCKPTR(optlen));
 }
 EXPORT_IPV6_MOD(tcp_getsockopt);
 
net/ipv4/udp.c +2 -2
index 17c3fb1acb30..b27954698f5e 100644
@@ -3053,7 +3053,7 @@ int udp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
 }
 
 int udp_lib_getsockopt(struct sock *sk, int level, int optname,
-		       char __user *optval, int __user *optlen)
+		       char __user *optval, optlen_t optlen)
 {
 	struct udp_sock *up = udp_sk(sk);
 	int val, len;
@@ -3114,7 +3114,7 @@ int udp_lib_getsockopt(struct sock *sk, int level, int optname,
 EXPORT_IPV6_MOD(udp_lib_getsockopt);
 
 int udp_getsockopt(struct sock *sk, int level, int optname,
-		   char __user *optval, int __user *optlen)
+		   char __user *optval, optlen_t optlen)
 {
 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
net/ipv4/udp_impl.h +1 -1
index e1ff3a375996..67a01fd5154f 100644
@@ -15,7 +15,7 @@ void udp_v4_rehash(struct sock *sk);
 int udp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
 		   unsigned int optlen);
 int udp_getsockopt(struct sock *sk, int level, int optname,
-		   char __user *optval, int __user *optlen);
+		   char __user *optval, optlen_t optlen);
 
 int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags,
 		int *addr_len);
net/ipv6/ipv6_sockglue.c +2 -2
index 9b1843288035..253e420802ca 100644
@@ -1470,7 +1470,7 @@ int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
 }
 
 int ipv6_getsockopt(struct sock *sk, int level, int optname,
-		    char __user *optval, int __user *optlen)
+		    char __user *optval, optlen_t optlen)
 {
 	int err;
 
@@ -1481,7 +1481,7 @@ int ipv6_getsockopt(struct sock *sk, int level, int optname,
 		return -ENOPROTOOPT;
 
 	err = do_ipv6_getsockopt(sk, level, optname,
-				 USER_SOCKPTR(optval), USER_SOCKPTR(optlen));
+				 USER_SOCKPTR(optval), OPTLEN_SOCKPTR(optlen));
 #ifdef CONFIG_NETFILTER
 	/* we need to exclude all possible ENOPROTOOPTs except default case */
 	if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
net/ipv6/raw.c +3 -3
index 90216d7e2af6..679ba4799c79 100644
@@ -951,7 +951,7 @@ static int rawv6_seticmpfilter(struct sock *sk, int optname,
 }
 
 static int rawv6_geticmpfilter(struct sock *sk, int optname,
-			       char __user *optval, int __user *optlen)
+			       char __user *optval, optlen_t optlen)
 {
 	int len;
 
@@ -1050,7 +1050,7 @@ static int rawv6_setsockopt(struct sock *sk, int level, int optname,
 }
 
 static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
-			    char __user *optval, int __user *optlen)
+			    char __user *optval, optlen_t optlen)
 {
 	struct raw6_sock *rp = raw6_sk(sk);
 	int val, len;
@@ -1088,7 +1088,7 @@ static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
 }
 
 static int rawv6_getsockopt(struct sock *sk, int level, int optname,
-			  char __user *optval, int __user *optlen)
+			  char __user *optval, optlen_t optlen)
 {
 	switch (level) {
 	case SOL_RAW:
net/ipv6/udp.c +1 -1
index 024458ef163c..861691ba88cd 100644
@@ -1843,7 +1843,7 @@ int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
 }
 
 int udpv6_getsockopt(struct sock *sk, int level, int optname,
-		     char __user *optval, int __user *optlen)
+		     char __user *optval, optlen_t optlen)
 {
 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
net/ipv6/udp_impl.h +1 -1
index 0590f566379d..a23db3c3ca9a 100644
@@ -17,7 +17,7 @@ int udp_v6_get_port(struct sock *sk, unsigned short snum);
 void udp_v6_rehash(struct sock *sk);
 
 int udpv6_getsockopt(struct sock *sk, int level, int optname,
-		     char __user *optval, int __user *optlen);
+		     char __user *optval, optlen_t optlen);
 int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
 		     unsigned int optlen);
 int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len);
net/iucv/af_iucv.c +1 -1
index ce0c68c9513c..00b6ba3f23ff 100644
@@ -1533,7 +1533,7 @@ static int iucv_sock_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int iucv_sock_getsockopt(struct socket *sock, int level, int optname,
-				char __user *optval, int __user *optlen)
+				char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct iucv_sock *iucv = iucv_sk(sk);
net/kcm/kcmsock.c +1 -1
index 68b6a8bd0cdb..ddc46864643e 100644
@@ -1145,7 +1145,7 @@ static int kcm_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int kcm_getsockopt(struct socket *sock, int level, int optname,
-			  char __user *optval, int __user *optlen)
+			  char __user *optval, optlen_t optlen)
 {
 	struct kcm_sock *kcm = kcm_sk(sock->sk);
 	int val, len;
net/l2tp/l2tp_ppp.c +1 -1
index aa3e34ef6b5c..c9effe687625 100644
@@ -1332,7 +1332,7 @@ static int pppol2tp_session_getsockopt(struct sock *sk,
  * or the special tunnel type.
  */
 static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
-			       char __user *optval, int __user *optlen)
+			       char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct l2tp_session *session;
net/llc/af_llc.c +1 -1
index a8e5d6eb5ad1..08ac78fb2b5d 100644
@@ -1170,7 +1170,7 @@ static int llc_ui_setsockopt(struct socket *sock, int level, int optname,
  *	Get connection specific socket information.
  */
 static int llc_ui_getsockopt(struct socket *sock, int level, int optname,
-			     char __user *optval, int __user *optlen)
+			     char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct llc_sock *llc = llc_sk(sk);
net/mctp/af_mctp.c +1 -1
index 2cff81d47b76..ec34e4dc0642 100644
@@ -327,7 +327,7 @@ static int mctp_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int mctp_getsockopt(struct socket *sock, int level, int optname,
-			   char __user *optval, int __user *optlen)
+			   char __user *optval, optlen_t optlen)
 {
 	struct mctp_sock *msk = container_of(sock->sk, struct mctp_sock, sk);
 	int len, val;
net/mptcp/protocol.h +1 -1
index d409586b5977..7187d7583e7c 100644
@@ -862,7 +862,7 @@ bool mptcp_schedule_work(struct sock *sk);
 int mptcp_setsockopt(struct sock *sk, int level, int optname,
 		     sockptr_t optval, unsigned int optlen);
 int mptcp_getsockopt(struct sock *sk, int level, int optname,
-		     char __user *optval, int __user *option);
+		     char __user *optval, optlen_t option);
 
 u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq);
 static inline u64 mptcp_expand_seq(u64 old_seq, u64 cur_seq, bool use_64bit)
net/mptcp/sockopt.c +15 -15
index 25b780598888..88480bbcfd39 100644
@@ -906,7 +906,7 @@ int mptcp_setsockopt(struct sock *sk, int level, int optname,
 }
 
 static int mptcp_getsockopt_first_sf_only(struct mptcp_sock *msk, int level, int optname,
-					  char __user *optval, int __user *optlen)
+					  char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = (struct sock *)msk;
 	struct sock *ssk;
@@ -991,7 +991,7 @@ void mptcp_diag_fill_info(struct mptcp_sock *msk, struct mptcp_info *info)
 }
 EXPORT_SYMBOL_GPL(mptcp_diag_fill_info);
 
-static int mptcp_getsockopt_info(struct mptcp_sock *msk, char __user *optval, int __user *optlen)
+static int mptcp_getsockopt_info(struct mptcp_sock *msk, char __user *optval, optlen_t optlen)
 {
 	struct mptcp_info m_info;
 	int len;
@@ -1019,7 +1019,7 @@ static int mptcp_getsockopt_info(struct mptcp_sock *msk, char __user *optval, in
 static int mptcp_put_subflow_data(struct mptcp_subflow_data *sfd,
 				  char __user *optval,
 				  u32 copied,
-				  int __user *optlen)
+				  optlen_t optlen)
 {
 	u32 copylen = min_t(u32, sfd->size_subflow_data, sizeof(*sfd));
 
@@ -1039,7 +1039,7 @@ static int mptcp_put_subflow_data(struct mptcp_subflow_data *sfd,
 
 static int mptcp_get_subflow_data(struct mptcp_subflow_data *sfd,
 				  char __user *optval,
-				  int __user *optlen)
+				  optlen_t optlen)
 {
 	int len, copylen;
 
@@ -1076,7 +1076,7 @@ static int mptcp_get_subflow_data(struct mptcp_subflow_data *sfd,
 }
 
 static int mptcp_getsockopt_tcpinfo(struct mptcp_sock *msk, char __user *optval,
-				    int __user *optlen)
+				    optlen_t optlen)
 {
 	struct mptcp_subflow_context *subflow;
 	struct sock *sk = (struct sock *)msk;
@@ -1168,7 +1168,7 @@ static void mptcp_get_sub_addrs(const struct sock *sk, struct mptcp_subflow_addr
 }
 
 static int mptcp_getsockopt_subflow_addrs(struct mptcp_sock *msk, char __user *optval,
-					  int __user *optlen)
+					  optlen_t optlen)
 {
 	struct mptcp_subflow_context *subflow;
 	struct sock *sk = (struct sock *)msk;
@@ -1222,7 +1222,7 @@ static int mptcp_getsockopt_subflow_addrs(struct mptcp_sock *msk, char __user *o
 
 static int mptcp_get_full_info(struct mptcp_full_info *mfi,
 			       char __user *optval,
-			       int __user *optlen)
+			       optlen_t optlen)
 {
 	int len;
 
@@ -1254,7 +1254,7 @@ static int mptcp_get_full_info(struct mptcp_full_info *mfi,
 static int mptcp_put_full_info(struct mptcp_full_info *mfi,
 			       char __user *optval,
 			       u32 copylen,
-			       int __user *optlen)
+			       optlen_t optlen)
 {
 	copylen += MIN_FULL_INFO_OPTLEN_SIZE;
 	if (put_optlen(copylen, optlen))
@@ -1266,7 +1266,7 @@ static int mptcp_put_full_info(struct mptcp_full_info *mfi,
 }
 
 static int mptcp_getsockopt_full_info(struct mptcp_sock *msk, char __user *optval,
-				      int __user *optlen)
+				      optlen_t optlen)
 {
 	unsigned int sfcount = 0, copylen = 0;
 	struct mptcp_subflow_context *subflow;
@@ -1340,7 +1340,7 @@ static int mptcp_getsockopt_full_info(struct mptcp_sock *msk, char __user *optva
 }
 
 static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optval,
-				int __user *optlen, int val)
+				optlen_t optlen, int val)
 {
 	int len;
 
@@ -1369,7 +1369,7 @@ static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optval,
 }
 
 static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
-				    char __user *optval, int __user *optlen)
+				    char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = (void *)msk;
 
@@ -1412,7 +1412,7 @@ static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
 }
 
 static int mptcp_getsockopt_v4(struct mptcp_sock *msk, int optname,
-			       char __user *optval, int __user *optlen)
+			       char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = (void *)msk;
 
@@ -1437,7 +1437,7 @@ static int mptcp_getsockopt_v4(struct mptcp_sock *msk, int optname,
 }
 
 static int mptcp_getsockopt_v6(struct mptcp_sock *msk, int optname,
-			       char __user *optval, int __user *optlen)
+			       char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = (void *)msk;
 
@@ -1457,7 +1457,7 @@ static int mptcp_getsockopt_v6(struct mptcp_sock *msk, int optname,
 }
 
 static int mptcp_getsockopt_sol_mptcp(struct mptcp_sock *msk, int optname,
-				      char __user *optval, int __user *optlen)
+				      char __user *optval, optlen_t optlen)
 {
 	switch (optname) {
 	case MPTCP_INFO:
@@ -1474,7 +1474,7 @@ static int mptcp_getsockopt_sol_mptcp(struct mptcp_sock *msk, int optname,
 }
 
 int mptcp_getsockopt(struct sock *sk, int level, int optname,
-		     char __user *optval, int __user *option)
+		     char __user *optval, optlen_t option)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	struct sock *ssk;
net/netlink/af_netlink.c +1 -1
index 3cde0f15deed..ffb6ff92abc9 100644
@@ -1705,7 +1705,7 @@ static int netlink_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int netlink_getsockopt(struct socket *sock, int level, int optname,
-			      char __user *optval, int __user *optlen)
+			      char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct netlink_sock *nlk = nlk_sk(sk);
net/netrom/af_netrom.c +1 -1
index 6039b5219460..21335e970f4c 100644
@@ -346,7 +346,7 @@ static int nr_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int nr_getsockopt(struct socket *sock, int level, int optname,
-	char __user *optval, int __user *optlen)
+	char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct nr_sock *nr = nr_sk(sk);
net/nfc/llcp_sock.c +1 -1
index 5e588640c22f..a19bd39a0329 100644
@@ -302,7 +302,7 @@ static int nfc_llcp_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int nfc_llcp_getsockopt(struct socket *sock, int level, int optname,
-			       char __user *optval, int __user *optlen)
+			       char __user *optval, optlen_t optlen)
 {
 	struct nfc_llcp_local *local;
 	struct sock *sk = sock->sk;
net/packet/af_packet.c +1 -1
index f35ab96fbcad..6afa989386e4 100644
@@ -4103,7 +4103,7 @@ packet_setsockopt(struct socket *sock, int level, int optname, sockptr_t optval,
 }
 
 static int packet_getsockopt(struct socket *sock, int level, int optname,
-			     char __user *optval, int __user *optlen)
+			     char __user *optval, optlen_t optlen)
 {
 	int len;
 	int val, lv = sizeof(val);
net/phonet/pep.c +1 -1
index 78b269ddf28b..d66776ed765e 100644
@@ -1063,7 +1063,7 @@ static int pep_setsockopt(struct sock *sk, int level, int optname,
 }
 
 static int pep_getsockopt(struct sock *sk, int level, int optname,
-				char __user *optval, int __user *optlen)
+				char __user *optval, optlen_t optlen)
 {
 	struct pep_sock *pn = pep_sk(sk);
 	int len, val;
net/rds/af_rds.c +1 -1
index 3395062245c5..12bc0352ba6e 100644
@@ -481,7 +481,7 @@ static int rds_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int rds_getsockopt(struct socket *sock, int level, int optname,
-			  char __user *optval, int __user *optlen)
+			  char __user *optval, optlen_t optlen)
 {
 	struct rds_sock *rs = rds_sk_to_rs(sock->sk);
 	int ret = -ENOPROTOOPT, len;
net/rds/info.c +1 -1
index 1990d068f6ee..b0d594026a48 100644
@@ -156,7 +156,7 @@ EXPORT_SYMBOL_GPL(rds_info_copy);
  * in the snapshot.
  */
 int rds_info_getsockopt(struct socket *sock, int optname, char __user *optval,
-			int __user *optlen)
+			optlen_t optlen)
 {
 	struct rds_info_iterator iter;
 	struct rds_info_lengths lens;
net/rds/info.h +1 -1
index a069b51c4679..aa25aaeb154f 100644
@@ -22,7 +22,7 @@ typedef void (*rds_info_func)(struct socket *sock, unsigned int len,
 void rds_info_register_func(int optname, rds_info_func func);
 void rds_info_deregister_func(int optname, rds_info_func func);
 int rds_info_getsockopt(struct socket *sock, int optname, char __user *optval,
-			int __user *optlen);
+			optlen_t optlen);
 void rds_info_copy(struct rds_info_iterator *iter, void *data,
 		   unsigned long bytes);
 void rds_info_iter_unmap(struct rds_info_iterator *iter);
net/rose/af_rose.c +1 -1
index a1299e9dd3e6..481279525981 100644
@@ -453,7 +453,7 @@ static int rose_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int rose_getsockopt(struct socket *sock, int level, int optname,
-	char __user *optval, int __user *optlen)
+	char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct rose_sock *rose = rose_sk(sk);
net/rxrpc/af_rxrpc.c +1 -1
index a88c635888fd..0373aa629885 100644
@@ -756,7 +756,7 @@ static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
  * Get socket options.
  */
 static int rxrpc_getsockopt(struct socket *sock, int level, int optname,
-			    char __user *optval, int __user *_optlen)
+			    char __user *optval, optlen_t _optlen)
 {
 	int optlen;
 
net/sctp/socket.c +56 -56

448 lines not on this page · raw message

net/smc/af_smc.c +2 -2
index 405c0bff7121..da20193e2925 100644
@@ -2989,7 +2989,7 @@ int smc_shutdown(struct socket *sock, int how)
 }
 
 static int __smc_getsockopt(struct socket *sock, int level, int optname,
-			    char __user *optval, int __user *optlen)
+			    char __user *optval, optlen_t optlen)
 {
 	struct smc_sock *smc;
 	int val, len;
@@ -3141,7 +3141,7 @@ int smc_setsockopt(struct socket *sock, int level, int optname,
 }
 
 int smc_getsockopt(struct socket *sock, int level, int optname,
-		   char __user *optval, int __user *optlen)
+		   char __user *optval, optlen_t optlen)
 {
 	struct smc_sock *smc;
 	int rc;
net/smc/smc.h +1 -1
index 78ae10d06ed2..abcf01824fa8 100644
@@ -59,7 +59,7 @@ int smc_shutdown(struct socket *sock, int how);
 int smc_setsockopt(struct socket *sock, int level, int optname,
 		   sockptr_t optval, unsigned int optlen);
 int smc_getsockopt(struct socket *sock, int level, int optname,
-		   char __user *optval, int __user *optlen);
+		   char __user *optval, optlen_t optlen);
 int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len);
 int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
 		int flags);
net/socket.c +5 -2
index 9a0e720f0859..fa2de12c10e6 100644
@@ -2350,12 +2350,15 @@ int do_sock_getsockopt(struct socket *sock, bool compat, int level,
 	} else if (unlikely(!ops->getsockopt)) {
 		err = -EOPNOTSUPP;
 	} else {
-		if (WARN_ONCE(optval.is_kernel || optlen.is_kernel,
+		optlen_t _optlen = { .up = NULL, };
+
+		if (WARN_ONCE(optval.is_kernel,
 			      "Invalid argument type"))
 			return -EOPNOTSUPP;
 
+		_optlen.up = optlen.user;
 		err = ops->getsockopt(sock, level, optname, optval.user,
-				      optlen.user);
+				      _optlen);
 	}
 
 	if (!compat)
net/tipc/socket.c +1 -1
index 23822d9230e4..24db67f7f21a 100644
@@ -3229,7 +3229,7 @@ static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
  * Return: 0 on success, errno otherwise
  */
 static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
-			   char __user *ov, int __user *ol)
+			   char __user *ov, optlen_t ol)
 {
 	struct sock *sk = sock->sk;
 	struct tipc_sock *tsk = tipc_sk(sk);
net/tls/tls_main.c +5 -5
index f4e87b4295b4..1069c8ca5aad 100644
@@ -436,7 +436,7 @@ static __poll_t tls_sk_poll(struct file *file, struct socket *sock,
 }
 
 static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
-				  int __user *optlen, int tx)
+				  optlen_t optlen, int tx)
 {
 	int rc = 0;
 	const struct tls_cipher_desc *cipher_desc;
@@ -497,7 +497,7 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
 }
 
 static int do_tls_getsockopt_tx_zc(struct sock *sk, char __user *optval,
-				   int __user *optlen)
+				   optlen_t optlen)
 {
 	struct tls_context *ctx = tls_get_ctx(sk);
 	unsigned int value;
@@ -517,7 +517,7 @@ static int do_tls_getsockopt_tx_zc(struct sock *sk, char __user *optval,
 }
 
 static int do_tls_getsockopt_no_pad(struct sock *sk, char __user *optval,
-				    int __user *optlen)
+				    optlen_t optlen)
 {
 	struct tls_context *ctx = tls_get_ctx(sk);
 	int value, len;
@@ -545,7 +545,7 @@ static int do_tls_getsockopt_no_pad(struct sock *sk, char __user *optval,
 }
 
 static int do_tls_getsockopt(struct sock *sk, int optname,
-			     char __user *optval, int __user *optlen)
+			     char __user *optval, optlen_t optlen)
 {
 	int rc = 0;
 
@@ -574,7 +574,7 @@ static int do_tls_getsockopt(struct sock *sk, int optname,
 }
 
 static int tls_getsockopt(struct sock *sk, int level, int optname,
-			  char __user *optval, int __user *optlen)
+			  char __user *optval, optlen_t optlen)
 {
 	struct tls_context *ctx = tls_get_ctx(sk);
 
net/vmw_vsock/af_vsock.c +1 -1
index c21a3bfcdd75..5f4ff266cf5e 100644
@@ -1866,7 +1866,7 @@ static int vsock_connectible_setsockopt(struct socket *sock,
 static int vsock_connectible_getsockopt(struct socket *sock,
 					int level, int optname,
 					char __user *optval,
-					int __user *optlen)
+					optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct vsock_sock *vsk = vsock_sk(sk);
net/x25/af_x25.c +1 -1
index 4eb65c05b3b9..863819103621 100644
@@ -448,7 +448,7 @@ static int x25_setsockopt(struct socket *sock, int level, int optname,
 }
 
 static int x25_getsockopt(struct socket *sock, int level, int optname,
-			  char __user *optval, int __user *optlen)
+			  char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	int val, len, rc = -ENOPROTOOPT;
net/xdp/xsk.c +1 -1
index 7cae6f4114b5..7b75b6217ba1 100644
@@ -1461,7 +1461,7 @@ struct xdp_statistics_v1 {
 };
 
 static int xsk_getsockopt(struct socket *sock, int level, int optname,
-			  char __user *optval, int __user *optlen)
+			  char __user *optval, optlen_t optlen)
 {
 	struct sock *sk = sock->sk;
 	struct xdp_sock *xs = xdp_sk(sk);
Signature · 1 line
2.34.1
#7 lore raw

RFC PATCH 3/4 net: pass a kernel pointer via 'optlen_t' to proto[ops].getsockopt() hooks

In reply to Stefan Metzmacher

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.

Cc Jens Axboe <[email protected]>
Cc Pavel Begunkov <[email protected]>
Cc Breno Leitao <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Karsten Keil <[email protected]>
Cc Ayush Sawal <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Simon Horman <[email protected]>
Cc Kuniyuki Iwashima <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc David Ahern <[email protected]>
Cc Marcelo Ricardo Leitner <[email protected]>
Cc Xin Long <[email protected]>
Cc Neal Cardwell <[email protected]>
Cc Joerg Reuter <[email protected]>
Cc Marcel Holtmann <[email protected]>
Cc Johan Hedberg <[email protected]>
Cc Luiz Augusto von Dentz <[email protected]>
Cc Oliver Hartkopp <[email protected]>
Cc Marc Kleine-Budde <[email protected]>
Cc Robin van der Gracht <[email protected]>
Cc Oleksij Rempel <[email protected]>
Cc Alexander Aring <[email protected]>
Cc Stefan Schmidt <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Alexandra Winter <[email protected]>
Cc Thorsten Winkler <[email protected]>
Cc James Chapman <[email protected]>
Cc Jeremy Kerr <[email protected]>
Cc Matt Johnston <[email protected]>
Cc Matthieu Baerts <[email protected]>
Cc Mat Martineau <[email protected]>
Cc Geliang Tang <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Remi Denis-Courmont <[email protected]>
Cc Allison Henderson <[email protected]>
Cc David Howells <[email protected]>
Cc Marc Dionne <[email protected]>
Cc Wenjia Zhang <[email protected]>
Cc Jan Karcher <[email protected]>
Cc "D. Wythe" <[email protected]>
Cc Tony Lu <[email protected]>
Cc Wen Gu <[email protected]>
Cc Jon Maloy <[email protected]>
Cc Boris Pismenny <[email protected]>
Cc John Fastabend <[email protected]>
Cc Stefano Garzarella <[email protected]>
Cc Martin Schiller <[email protected]>
Cc "Björn Töpel" <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maciej Fijalkowski <[email protected]>
Cc Jonathan Lemon <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
CC Stefan Metzmacher <[email protected]>
---
 include/linux/sockptr.h | 20 +++++++++++++++-----
 net/socket.c            | 31 +++++++++++++++++++++++++++++--
 2 files changed, 44 insertions(+), 7 deletions(-)
include/linux/sockptr.h +15 -5
index 1baf66f26f4f..06ec7fd73028 100644
@@ -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 */
net/socket.c +29 -2
index fa2de12c10e6..81e5c9767bbc 100644
@@ -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)
Signature · 1 line
2.34.1
#10 lore raw

RFC PATCH 4/4 io_uring: let io_uring_cmd_getsockopt() allow level other than SOL_SOCKET

In reply to Stefan Metzmacher

do_sock_getsockopt() works with a kernel pointer for optlen now.

Cc Jens Axboe <[email protected]>
Cc Pavel Begunkov <[email protected]>
Cc Breno Leitao <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Karsten Keil <[email protected]>
Cc Ayush Sawal <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Simon Horman <[email protected]>
Cc Kuniyuki Iwashima <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc David Ahern <[email protected]>
Cc Marcelo Ricardo Leitner <[email protected]>
Cc Xin Long <[email protected]>
Cc Neal Cardwell <[email protected]>
Cc Joerg Reuter <[email protected]>
Cc Marcel Holtmann <[email protected]>
Cc Johan Hedberg <[email protected]>
Cc Luiz Augusto von Dentz <[email protected]>
Cc Oliver Hartkopp <[email protected]>
Cc Marc Kleine-Budde <[email protected]>
Cc Robin van der Gracht <[email protected]>
Cc Oleksij Rempel <[email protected]>
Cc Alexander Aring <[email protected]>
Cc Stefan Schmidt <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Alexandra Winter <[email protected]>
Cc Thorsten Winkler <[email protected]>
Cc James Chapman <[email protected]>
Cc Jeremy Kerr <[email protected]>
Cc Matt Johnston <[email protected]>
Cc Matthieu Baerts <[email protected]>
Cc Mat Martineau <[email protected]>
Cc Geliang Tang <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Remi Denis-Courmont <[email protected]>
Cc Allison Henderson <[email protected]>
Cc David Howells <[email protected]>
Cc Marc Dionne <[email protected]>
Cc Wenjia Zhang <[email protected]>
Cc Jan Karcher <[email protected]>
Cc "D. Wythe" <[email protected]>
Cc Tony Lu <[email protected]>
Cc Wen Gu <[email protected]>
Cc Jon Maloy <[email protected]>
Cc Boris Pismenny <[email protected]>
Cc John Fastabend <[email protected]>
Cc Stefano Garzarella <[email protected]>
Cc Martin Schiller <[email protected]>
Cc "Björn Töpel" <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maciej Fijalkowski <[email protected]>
Cc Jonathan Lemon <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
CC Stefan Metzmacher <[email protected]>
---
 io_uring/uring_cmd.c | 3 ---
 1 file changed, 3 deletions(-)
io_uring/uring_cmd.c +0 -3
index f2cfc371f3d0..8b0cc919a60c 100644
@@ -312,9 +312,6 @@ static inline int io_uring_cmd_getsockopt(struct socket *sock,
 	void __user *optval;
 
 	level = READ_ONCE(cmd->sqe->level);
-	if (level != SOL_SOCKET)
-		return -EOPNOTSUPP;
-
 	optval = u64_to_user_ptr(READ_ONCE(cmd->sqe->optval));
 	optname = READ_ONCE(cmd->sqe->optname);
 	optlen = READ_ONCE(cmd->sqe->optlen);
Signature · 1 line
2.34.1
#2 lore raw
In reply to Stefan Metzmacher

This is an automated email and please do not reply to this email.

Dear Submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
While preparing the CI tests, the patches you submitted couldn't be applied to the current HEAD of the repository.

----- Output -----

error: patch failed: include/net/inet_connection_sock.h:47
error: include/net/inet_connection_sock.h: patch does not apply
hint: Use 'git am --show-current-patch' to see the failed patch

Please resolve the issue and submit the patches again.

---
Regards,
Linux Bluetooth

#6 lore raw

Re: [RFC PATCH 2/4] net: pass 'optlen_t' to proto[ops].getsockopt() hooks

In reply to Stefan Metzmacher

net/socket.c int do_sock_getsockopt(struct socket *sock, bool compat, int level, · line 2355

Quoted 12 lines
net/socket.c +3 -1
index 9a0e720f0859..fa2de12c10e6 100644
@@ -2350,12 +2350,15 @@ int do_sock_getsockopt(struct socket *sock, bool compat, int level,
  	} else if (unlikely(!ops->getsockopt)) {
  		err = -EOPNOTSUPP;
  	} else {
-		if (WARN_ONCE(optval.is_kernel || optlen.is_kernel,
+		optlen_t _optlen = { .up = NULL, };
+
+		if (WARN_ONCE(optval.is_kernel,

Sorry, the removal of '|| optlen.is_kernel' should be in the next commit...

Quoted 2 lines
net/socket.c +0 -0
  			      "Invalid argument type"))
  			return -EOPNOTSUPP;
#11 lore raw
In reply to Stefan Metzmacher
Quoted 15 lines · Stefan Metzmacher

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 as optlen to do_sock_getsockopt() and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

Quoted 3 lines

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().

At this point why not just fully embrace iov_iter? You have the size now + the user (or kernel) pointer. Might as well do s/sockptr_t/iov_iter/ conversion?

#8 lore raw

Re: [RFC PATCH 3/4] net: pass a kernel pointer via 'optlen_t' to proto[ops].getsockopt() hooks

In reply to Stefan Metzmacher
Quoted 24 lines · On Mon, 31 Mar 2025 22:10:55 +0200 Stefan Metzmacher <[email protected]> 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.

Quoted 3 lines

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

#27 lore raw
In reply to Stefan Metzmacher

Hi Stefan,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal: Success! ✅
- KVM Validation: debug: Critical: Global Timeout ❌
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Unstable: 1 failed test(s): bpftest_test_progs_mptcp 🔴
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/14180807770

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/e5554f1e2602
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=948670

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)

#28 lore raw
In reply to Stefan Metzmacher

Hi Stefan,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal: Unstable: 1 failed test(s): packetdrill_sockopts 🔴
- KVM Validation: debug: Success! ✅
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/14180807770

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/e5554f1e2602
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=948670

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)

#12 lore raw
In reply to Stanislav Fomichev

Am 31.03.25 um 23:04 schrieb Stanislav Fomichev:

Quoted 19 lines · Stefan Metzmacher
Quoted 15 lines

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 as optlen to do_sock_getsockopt() and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

Quoted 7 lines
Quoted 3 lines

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().

At this point why not just fully embrace iov_iter? You have the size now + the user (or kernel) pointer. Might as well do s/sockptr_t/iov_iter/ conversion?

I think that would only be possible if we introduce proto[_ops].getsockopt_iter() and then convert the implementations step by step. Doing it all in one go has a lot of potential to break the uapi. I could try to convert things like socket, ip and tcp myself, but the rest needs to be converted by the maintainer of the specific protocol, as it needs to be tested. As there are crazy things happening in the existing implementations, e.g. some getsockopt() implementations use optval as in and out buffer.

I first tried to convert both optval and optlen of getsockopt to sockptr_t,
and that showed that touching the optval part starts to get complex very soon,
see https://git.samba.org/?p=metze/linux/wip.git;a=commitdiff;h=141912166473bf8843ec6ace76dc9c6945adafd1
(note it didn't converted everything, I gave up after hitting sctp_getsockopt_peer_addrs and sctp_getsockopt_local_addrs.
sctp_getsockopt_context, sctp_getsockopt_maxseg, sctp_getsockopt_associnfo and maybe more are the ones also doing both copy_from_user and copy_to_user on optval)

I come also across one implementation that returned -ERANGE because *optlen was too short and put the required length into *optlen, which means the returned *optlen is larger than the optval buffer given from userspace.

Because of all these strange things I tried to do a minimal change in order to get rid of the io_uring limitation and only converted optlen and leave optval as is.

In order to have a patchset that has a low risk to cause regressions.

But as alternative introducing a prototype like this:

int (*getsockopt_iter)(struct socket *sock, int level, int optname,
struct iov_iter *optval_iter);

That returns a non-negative value which can be placed into *optlen or negative value as error and *optlen will not be changed on error. optval_iter will get direction ITER_DEST, so it can only be written to.

Implementations could then opt in for the new interface and allow do_sock_getsockopt() work also for the io_uring case, while all others would still get -EOPNOTSUPP.

So what should be the way to go?

metze

#9 lore raw

Re: [RFC PATCH 3/4] net: pass a kernel pointer via 'optlen_t' to proto[ops].getsockopt() hooks

In reply to David Laight

Am 31.03.25 um 23:49 schrieb David Laight:

Quoted 27 lines
Quoted 24 lines · On Mon, 31 Mar 2025 22:10:55 +0200 Stefan Metzmacher <[email protected]> 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.

Quoted 5 lines
Quoted 3 lines

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).

Yes, if you want to change the logic at the same time as changing the kind of argument variable, then it get messy quite fast.

Quoted 4 lines

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.

Yes, maybe not 99%, but a lot.

Quoted 15 lines

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.

Quoted 3 lines

The changes are mechanical but very widespread.

They also give the option of not writing back the length if unchanged.

See my other mail regarding proto[_ops].getsockopt_iter(), where implementation could be converted step by step.

But we may still need to keep the current proto[ops].getsockopt() as proto[ops].getsockopt_legacy() in order to keep the insane uapi semantics alive.

metze

#3 lore raw

Re: [RFC PATCH 1/4] net: introduce get_optlen() and put_optlen() helpers

In reply to Stefan Metzmacher

Hello Stefan,

include/linux/sockptr.h static inline int check_zeroed_sockptr(sockptr_t src, size_t offset, · line 176

Quoted 11 lines · Stefan Metzmacher
include/linux/sockptr.h +5 -0
@@ -169,4 +169,26 @@ static inline int check_zeroed_sockptr(sockptr_t src, size_t offset,
 	return memchr_inv(src.kernel + offset, 0, size) == NULL;
 }
 
+#define __check_optlen_t(__optlen)				\
+({								\
+	int __user *__ptr __maybe_unused = __optlen; 		\
+	BUILD_BUG_ON(sizeof(*(__ptr)) != sizeof(int));		\
+})

I am a bit confused about this macro. I understand that this macro's goal is to check that __optlen is a pointer to an integer, otherwise failed to build.

It is unclear to me if that is what it does. Let's suppose that __optlen is not an integer pointer. Then:

include/linux/sockptr.h static inline int check_zeroed_sockptr(sockptr_t src, size_t offset, · line 177

Quoted 1 line
include/linux/sockptr.h +0 -0
 int __user *__ptr __maybe_unused = __optlen;

This will generate a compile failure/warning due invalid casting, depending on -Wincompatible-pointer-types.

Quoted 1 line

BUILD_BUG_ON(sizeof(*(__ptr)) != sizeof(int));

Then this comparison will always false, since __ptr is a pointer to int, and you are comparing the size of its content with the sizeof(int).

#4 lore raw

Re: [RFC PATCH 1/4] net: introduce get_optlen() and put_optlen() helpers

Hello Breno,

Quoted 22 lines · Stefan Metzmacher

include/linux/sockptr.h static inline int check_zeroed_sockptr(sockptr_t src, size_t offset, · line 176

Quoted 11 lines
include/linux/sockptr.h +5 -0
@@ -169,4 +169,26 @@ static inline int check_zeroed_sockptr(sockptr_t src, size_t offset,
  	return memchr_inv(src.kernel + offset, 0, size) == NULL;
  }
 
+#define __check_optlen_t(__optlen)				\
+({								\
+	int __user *__ptr __maybe_unused = __optlen; 		\
+	BUILD_BUG_ON(sizeof(*(__ptr)) != sizeof(int));		\
+})

I am a bit confused about this macro. I understand that this macro's goal is to check that __optlen is a pointer to an integer, otherwise failed to build.

It is unclear to me if that is what it does. Let's suppose that __optlen is not an integer pointer. Then:

include/linux/sockptr.h static inline int check_zeroed_sockptr(sockptr_t src, size_t offset, · line 177

Quoted 1 line
include/linux/sockptr.h +0 -0
 int __user *__ptr __maybe_unused = __optlen;
Quoted 7 lines

This will generate a compile failure/warning due invalid casting, depending on -Wincompatible-pointer-types.

Quoted 1 line

BUILD_BUG_ON(sizeof(*(__ptr)) != sizeof(int));

Then this comparison will always false, since __ptr is a pointer to int, and you are comparing the size of its content with the sizeof(int).

Yes, it redundant in the first patch, it gets little more useful in the 2nd and 3rd patch.

metze

#13 lore raw
In reply to Stefan Metzmacher

Am 01.04.25 um 10:19 schrieb Stefan Metzmacher:

Quoted 64 lines

Am 31.03.25 um 23:04 schrieb Stanislav Fomichev:

Quoted 19 lines · Stefan Metzmacher
Quoted 15 lines

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 as optlen to do_sock_getsockopt() and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

Quoted 7 lines
Quoted 3 lines

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().

At this point why not just fully embrace iov_iter? You have the size now + the user (or kernel) pointer. Might as well do s/sockptr_t/iov_iter/ conversion?

I think that would only be possible if we introduce proto[_ops].getsockopt_iter() and then convert the implementations step by step. Doing it all in one go has a lot of potential to break the uapi. I could try to convert things like socket, ip and tcp myself, but the rest needs to be converted by the maintainer of the specific protocol, as it needs to be tested. As there are crazy things happening in the existing implementations, e.g. some getsockopt() implementations use optval as in and out buffer.

I first tried to convert both optval and optlen of getsockopt to sockptr_t,
and that showed that touching the optval part starts to get complex very soon,
see https://git.samba.org/?p=metze/linux/wip.git;a=commitdiff;h=141912166473bf8843ec6ace76dc9c6945adafd1
(note it didn't converted everything, I gave up after hitting sctp_getsockopt_peer_addrs and sctp_getsockopt_local_addrs.
sctp_getsockopt_context, sctp_getsockopt_maxseg, sctp_getsockopt_associnfo and maybe more are the ones also doing both copy_from_user and copy_to_user on optval)

I come also across one implementation that returned -ERANGE because *optlen was too short and put the required length into *optlen, which means the returned *optlen is larger than the optval buffer given from userspace.

Because of all these strange things I tried to do a minimal change in order to get rid of the io_uring limitation and only converted optlen and leave optval as is.

In order to have a patchset that has a low risk to cause regressions.

But as alternative introducing a prototype like this:

        int (*getsockopt_iter)(struct socket *sock, int level, int optname, struct iov_iter *optval_iter);

That returns a non-negative value which can be placed into *optlen or negative value as error and *optlen will not be changed on error. optval_iter will get direction ITER_DEST, so it can only be written to.

Quoted 5 lines

Implementations could then opt in for the new interface and allow do_sock_getsockopt() work also for the io_uring case, while all others would still get -EOPNOTSUPP.

So what should be the way to go?

Ok, I've added the infrastructure for getsockopt_iter, see below, but the first part I wanted to convert was tcp_ao_copy_mkts_to_user() and that also reads from userspace before writing.

So we could go with the optlen_t approach, or we need logic for ITER_BOTH or pass two iov_iters one with ITER_SRC and one with ITER_DEST...

So who wants to decide?

Thanks!
metze

---
 include/linux/net.h |  4 +++
 include/net/sock.h  | 64 +++++++++++++++++++++++++++++++++++++++++++++
 net/core/sock.c     | 12 +++++++--
 net/socket.c        | 12 +++++++--
 4 files changed, 88 insertions(+), 4 deletions(-)
include/linux/net.h +4 -0
index 0ff950eecc6b..ceb9f9ed84b9 100644
@@ -194,6 +194,10 @@ struct proto_ops {
 				      unsigned int optlen);
 	int		(*getsockopt)(struct socket *sock, int level,
 				      int optname, char __user *optval, int __user *optlen);
+	int		(*getsockopt_iter)(struct socket *sock,
+					   int level,
+					   int optname,
+					   struct iov_iter *optval_iter);
 	void		(*show_fdinfo)(struct seq_file *m, struct socket *sock);
 	int		(*sendmsg)   (struct socket *sock, struct msghdr *m,
 				      size_t total_len);
include/net/sock.h +64 -0

76 lines not on this page · raw message

net/core/sock.c +10 -2
index 323892066def..61625060e724 100644
@@ -3857,9 +3857,17 @@ int sock_common_getsockopt(struct socket *sock, int level, int optname,
 			   char __user *optval, int __user *optlen)
 {
 	struct sock *sk = sock->sk;
-
 	/* IPV6_ADDRFORM can change sk->sk_prot under us. */
-	return READ_ONCE(sk->sk_prot)->getsockopt(sk, level, optname, optval, optlen);
+	struct proto *prot = READ_ONCE(sk->sk_prot);
+
+	if (prot->getsockopt_iter) {
+		return sk_wrap_getsockopt_iter(sk, level, optname,
+					       USER_SOCKPTR(optval),
+					       USER_SOCKPTR(optlen),
+					       prot->getsockopt_iter);
+	}
+
+	return prot->getsockopt(sk, level, optname, optval, optlen);
 }
 EXPORT_SYMBOL(sock_common_getsockopt);
 
net/socket.c +10 -2
index 9a0e720f0859..792cfd272611 100644
@@ -2335,6 +2335,7 @@ int do_sock_getsockopt(struct socket *sock, bool compat, int level,
 {
 	int max_optlen __maybe_unused = 0;
 	const struct proto_ops *ops;
+	const struct proto *prot;
 	int err;
 
 	err = security_socket_getsockopt(sock, level, optname);
@@ -2345,12 +2346,19 @@ int do_sock_getsockopt(struct socket *sock, bool compat, int level,
 		copy_from_sockptr(&max_optlen, optlen, sizeof(int));
 
 	ops = READ_ONCE(sock->ops);
+	prot = READ_ONCE(sock->sk->sk_prot);
 	if (level == SOL_SOCKET) {
 		err = sk_getsockopt(sock->sk, level, optname, optval, optlen);
-	} else if (unlikely(!ops->getsockopt)) {
+	} else if (ops->getsockopt_iter) {
+		err = sock_wrap_getsockopt_iter(sock, level, optname, optval, optlen,
+					        ops->getsockopt_iter);
+	} else if (ops->getsockopt == sock_common_getsockopt && prot->getsockopt_iter) {
+		err = sk_wrap_getsockopt_iter(sock->sk, level, optname, optval, optlen,
+					      prot->getsockopt_iter);
+	} else if (unlikely(!ops->getsockopt || optlen.is_kernel)) {
 		err = -EOPNOTSUPP;
 	} else {
-		if (WARN_ONCE(optval.is_kernel || optlen.is_kernel,
+		if (WARN_ONCE(optval.is_kernel,
 			      "Invalid argument type"))
 			return -EOPNOTSUPP;
 
Signature · 1 line
2.34.1
#14 lore raw

Am 01.04.25 um 15:37 schrieb Stefan Metzmacher:

Quoted 76 lines

Am 01.04.25 um 10:19 schrieb Stefan Metzmacher:

Quoted 64 lines

Am 31.03.25 um 23:04 schrieb Stanislav Fomichev:

Quoted 19 lines · Stefan Metzmacher
Quoted 15 lines

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 as optlen to do_sock_getsockopt() and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

Quoted 7 lines
Quoted 3 lines

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().

At this point why not just fully embrace iov_iter? You have the size now + the user (or kernel) pointer. Might as well do s/sockptr_t/iov_iter/ conversion?

I think that would only be possible if we introduce proto[_ops].getsockopt_iter() and then convert the implementations step by step. Doing it all in one go has a lot of potential to break the uapi. I could try to convert things like socket, ip and tcp myself, but the rest needs to be converted by the maintainer of the specific protocol, as it needs to be tested. As there are crazy things happening in the existing implementations, e.g. some getsockopt() implementations use optval as in and out buffer.

I first tried to convert both optval and optlen of getsockopt to sockptr_t,
and that showed that touching the optval part starts to get complex very soon,
see https://git.samba.org/?p=metze/linux/wip.git;a=commitdiff;h=141912166473bf8843ec6ace76dc9c6945adafd1
(note it didn't converted everything, I gave up after hitting sctp_getsockopt_peer_addrs and sctp_getsockopt_local_addrs.
sctp_getsockopt_context, sctp_getsockopt_maxseg, sctp_getsockopt_associnfo and maybe more are the ones also doing both copy_from_user and copy_to_user on optval)

I come also across one implementation that returned -ERANGE because *optlen was too short and put the required length into *optlen, which means the returned *optlen is larger than the optval buffer given from userspace.

Because of all these strange things I tried to do a minimal change in order to get rid of the io_uring limitation and only converted optlen and leave optval as is.

In order to have a patchset that has a low risk to cause regressions.

But as alternative introducing a prototype like this:

         int (*getsockopt_iter)(struct socket *sock, int level, int optname, struct iov_iter *optval_iter);

That returns a non-negative value which can be placed into *optlen or negative value as error and *optlen will not be changed on error. optval_iter will get direction ITER_DEST, so it can only be written to.

Quoted 5 lines

Implementations could then opt in for the new interface and allow do_sock_getsockopt() work also for the io_uring case, while all others would still get -EOPNOTSUPP.

So what should be the way to go?

Ok, I've added the infrastructure for getsockopt_iter, see below, but the first part I wanted to convert was tcp_ao_copy_mkts_to_user() and that also reads from userspace before writing.

Quoted 5 lines

So we could go with the optlen_t approach, or we need logic for ITER_BOTH or pass two iov_iters one with ITER_SRC and one with ITER_DEST...

So who wants to decide?

I just noticed that it's even possible in same cases
to pass in a short buffer to optval, but have a longer value in optlen, hci_sock_getsockopt() with SOL_BLUETOOTH completely ignores optlen.

This makes it really hard to believe that trying to use iov_iter for this is a good idea :-(

Any ideas beside just going with optlen_t?

metze

#15 lore raw
Quoted 81 lines · Stefan Metzmacher

Am 01.04.25 um 15:37 schrieb Stefan Metzmacher:

Quoted 80 lines

Am 01.04.25 um 10:19 schrieb Stefan Metzmacher:

Quoted 64 lines

Am 31.03.25 um 23:04 schrieb Stanislav Fomichev:

Quoted 19 lines · Stefan Metzmacher
Quoted 15 lines

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 as optlen to do_sock_getsockopt() and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

Quoted 7 lines
Quoted 3 lines

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().

At this point why not just fully embrace iov_iter? You have the size now + the user (or kernel) pointer. Might as well do s/sockptr_t/iov_iter/ conversion?

I think that would only be possible if we introduce proto[_ops].getsockopt_iter() and then convert the implementations step by step. Doing it all in one go has a lot of potential to break the uapi. I could try to convert things like socket, ip and tcp myself, but the rest needs to be converted by the maintainer of the specific protocol, as it needs to be tested. As there are crazy things happening in the existing implementations, e.g. some getsockopt() implementations use optval as in and out buffer.

I first tried to convert both optval and optlen of getsockopt to sockptr_t,
and that showed that touching the optval part starts to get complex very soon,
see https://git.samba.org/?p=metze/linux/wip.git;a=commitdiff;h=141912166473bf8843ec6ace76dc9c6945adafd1
(note it didn't converted everything, I gave up after hitting sctp_getsockopt_peer_addrs and sctp_getsockopt_local_addrs.
sctp_getsockopt_context, sctp_getsockopt_maxseg, sctp_getsockopt_associnfo and maybe more are the ones also doing both copy_from_user and copy_to_user on optval)

I come also across one implementation that returned -ERANGE because *optlen was too short and put the required length into *optlen, which means the returned *optlen is larger than the optval buffer given from userspace.

Because of all these strange things I tried to do a minimal change in order to get rid of the io_uring limitation and only converted optlen and leave optval as is.

In order to have a patchset that has a low risk to cause regressions.

But as alternative introducing a prototype like this:

         int (*getsockopt_iter)(struct socket *sock, int level, int optname, struct iov_iter *optval_iter);

That returns a non-negative value which can be placed into *optlen or negative value as error and *optlen will not be changed on error. optval_iter will get direction ITER_DEST, so it can only be written to.

Quoted 5 lines

Implementations could then opt in for the new interface and allow do_sock_getsockopt() work also for the io_uring case, while all others would still get -EOPNOTSUPP.

So what should be the way to go?

Ok, I've added the infrastructure for getsockopt_iter, see below, but the first part I wanted to convert was tcp_ao_copy_mkts_to_user() and that also reads from userspace before writing.

So we could go with the optlen_t approach, or we need logic for ITER_BOTH or pass two iov_iters one with ITER_SRC and one with ITER_DEST...

Quoted 8 lines
Quoted 1 line

So who wants to decide?

I just noticed that it's even possible in same cases
to pass in a short buffer to optval, but have a longer value in optlen, hci_sock_getsockopt() with SOL_BLUETOOTH completely ignores optlen.

This makes it really hard to believe that trying to use iov_iter for this is a good idea :-(

That was my finding as well a while ago, when I was planning to get the __user pointers converted to iov_iter. There are some weird ways of using optlen and optval, which makes them non-trivial to covert to iov_iter.

#16 lore raw
Quoted 88 lines · Breno Leitao
Quoted 87 lines · Stefan Metzmacher

Am 01.04.25 um 15:37 schrieb Stefan Metzmacher:

Quoted 76 lines

Am 01.04.25 um 10:19 schrieb Stefan Metzmacher:

Quoted 64 lines

Am 31.03.25 um 23:04 schrieb Stanislav Fomichev:

Quoted 19 lines · Stefan Metzmacher
Quoted 15 lines

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 as optlen to do_sock_getsockopt() and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

Quoted 7 lines
Quoted 3 lines

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().

At this point why not just fully embrace iov_iter? You have the size now + the user (or kernel) pointer. Might as well do s/sockptr_t/iov_iter/ conversion?

I think that would only be possible if we introduce proto[_ops].getsockopt_iter() and then convert the implementations step by step. Doing it all in one go has a lot of potential to break the uapi. I could try to convert things like socket, ip and tcp myself, but the rest needs to be converted by the maintainer of the specific protocol, as it needs to be tested. As there are crazy things happening in the existing implementations, e.g. some getsockopt() implementations use optval as in and out buffer.

I first tried to convert both optval and optlen of getsockopt to sockptr_t,
and that showed that touching the optval part starts to get complex very soon,
see https://git.samba.org/?p=metze/linux/wip.git;a=commitdiff;h=141912166473bf8843ec6ace76dc9c6945adafd1
(note it didn't converted everything, I gave up after hitting sctp_getsockopt_peer_addrs and sctp_getsockopt_local_addrs.
sctp_getsockopt_context, sctp_getsockopt_maxseg, sctp_getsockopt_associnfo and maybe more are the ones also doing both copy_from_user and copy_to_user on optval)

I come also across one implementation that returned -ERANGE because *optlen was too short and put the required length into *optlen, which means the returned *optlen is larger than the optval buffer given from userspace.

Because of all these strange things I tried to do a minimal change in order to get rid of the io_uring limitation and only converted optlen and leave optval as is.

In order to have a patchset that has a low risk to cause regressions.

But as alternative introducing a prototype like this:

         int (*getsockopt_iter)(struct socket *sock, int level, int optname, struct iov_iter *optval_iter);

That returns a non-negative value which can be placed into *optlen or negative value as error and *optlen will not be changed on error. optval_iter will get direction ITER_DEST, so it can only be written to.

Quoted 5 lines

Implementations could then opt in for the new interface and allow do_sock_getsockopt() work also for the io_uring case, while all others would still get -EOPNOTSUPP.

So what should be the way to go?

Ok, I've added the infrastructure for getsockopt_iter, see below, but the first part I wanted to convert was tcp_ao_copy_mkts_to_user() and that also reads from userspace before writing.

Quoted 5 lines

So we could go with the optlen_t approach, or we need logic for ITER_BOTH or pass two iov_iters one with ITER_SRC and one with ITER_DEST...

So who wants to decide?

I just noticed that it's even possible in same cases
to pass in a short buffer to optval, but have a longer value in optlen, hci_sock_getsockopt() with SOL_BLUETOOTH completely ignores optlen.

Quoted 7 lines
Quoted 2 lines

This makes it really hard to believe that trying to use iov_iter for this is a good idea :-(

That was my finding as well a while ago, when I was planning to get the __user pointers converted to iov_iter. There are some weird ways of using optlen and optval, which makes them non-trivial to covert to iov_iter.

Can we ignore all non-ip/tcp/udp cases for now? This should cover +90% of useful socket opts. See if there are any obvious problems with them and if not, try converting. The rest we can cover separately when/if needed.

#17 lore raw

Am 01.04.25 um 17:45 schrieb Stanislav Fomichev:

Quoted 97 lines · Breno Leitao
Quoted 88 lines · Stefan Metzmacher
Quoted 87 lines

Am 01.04.25 um 15:37 schrieb Stefan Metzmacher:

Quoted 76 lines

Am 01.04.25 um 10:19 schrieb Stefan Metzmacher:

Quoted 64 lines

Am 31.03.25 um 23:04 schrieb Stanislav Fomichev:

Quoted 19 lines · Stefan Metzmacher
Quoted 15 lines

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 as optlen to do_sock_getsockopt() and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

Quoted 7 lines
Quoted 3 lines

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().

At this point why not just fully embrace iov_iter? You have the size now + the user (or kernel) pointer. Might as well do s/sockptr_t/iov_iter/ conversion?

I think that would only be possible if we introduce proto[_ops].getsockopt_iter() and then convert the implementations step by step. Doing it all in one go has a lot of potential to break the uapi. I could try to convert things like socket, ip and tcp myself, but the rest needs to be converted by the maintainer of the specific protocol, as it needs to be tested. As there are crazy things happening in the existing implementations, e.g. some getsockopt() implementations use optval as in and out buffer.

I first tried to convert both optval and optlen of getsockopt to sockptr_t,
and that showed that touching the optval part starts to get complex very soon,
see https://git.samba.org/?p=metze/linux/wip.git;a=commitdiff;h=141912166473bf8843ec6ace76dc9c6945adafd1
(note it didn't converted everything, I gave up after hitting sctp_getsockopt_peer_addrs and sctp_getsockopt_local_addrs.
sctp_getsockopt_context, sctp_getsockopt_maxseg, sctp_getsockopt_associnfo and maybe more are the ones also doing both copy_from_user and copy_to_user on optval)

I come also across one implementation that returned -ERANGE because *optlen was too short and put the required length into *optlen, which means the returned *optlen is larger than the optval buffer given from userspace.

Because of all these strange things I tried to do a minimal change in order to get rid of the io_uring limitation and only converted optlen and leave optval as is.

In order to have a patchset that has a low risk to cause regressions.

But as alternative introducing a prototype like this:

         int (*getsockopt_iter)(struct socket *sock, int level, int optname, struct iov_iter *optval_iter);

That returns a non-negative value which can be placed into *optlen or negative value as error and *optlen will not be changed on error. optval_iter will get direction ITER_DEST, so it can only be written to.

Quoted 5 lines

Implementations could then opt in for the new interface and allow do_sock_getsockopt() work also for the io_uring case, while all others would still get -EOPNOTSUPP.

So what should be the way to go?

Ok, I've added the infrastructure for getsockopt_iter, see below, but the first part I wanted to convert was tcp_ao_copy_mkts_to_user() and that also reads from userspace before writing.

Quoted 5 lines

So we could go with the optlen_t approach, or we need logic for ITER_BOTH or pass two iov_iters one with ITER_SRC and one with ITER_DEST...

So who wants to decide?

I just noticed that it's even possible in same cases
to pass in a short buffer to optval, but have a longer value in optlen, hci_sock_getsockopt() with SOL_BLUETOOTH completely ignores optlen.

Quoted 7 lines
Quoted 2 lines

This makes it really hard to believe that trying to use iov_iter for this is a good idea :-(

That was my finding as well a while ago, when I was planning to get the __user pointers converted to iov_iter. There are some weird ways of using optlen and optval, which makes them non-trivial to covert to iov_iter.

Quoted 4 lines

Can we ignore all non-ip/tcp/udp cases for now? This should cover +90% of useful socket opts. See if there are any obvious problems with them and if not, try converting. The rest we can cover separately when/if needed.

That's what I tried, but it fails with
tcp_getsockopt ->
do_tcp_getsockopt ->

     tcp_ao_get_mkts ->
        tcp_ao_copy_mkts_to_user ->
           copy_struct_from_sockptr
     tcp_ao_get_sock_info ->
        copy_struct_from_sockptr

That's not possible with a ITER_DEST iov_iter.

metze

#18 lore raw
Quoted 112 lines · Stefan Metzmacher

Am 01.04.25 um 17:45 schrieb Stanislav Fomichev:

Quoted 97 lines · Breno Leitao
Quoted 88 lines · Stefan Metzmacher
Quoted 87 lines

Am 01.04.25 um 15:37 schrieb Stefan Metzmacher:

Quoted 76 lines

Am 01.04.25 um 10:19 schrieb Stefan Metzmacher:

Quoted 64 lines

Am 31.03.25 um 23:04 schrieb Stanislav Fomichev:

Quoted 19 lines · Stefan Metzmacher
Quoted 15 lines

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 as optlen to do_sock_getsockopt() and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

Quoted 7 lines
Quoted 3 lines

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().

At this point why not just fully embrace iov_iter? You have the size now + the user (or kernel) pointer. Might as well do s/sockptr_t/iov_iter/ conversion?

I think that would only be possible if we introduce proto[_ops].getsockopt_iter() and then convert the implementations step by step. Doing it all in one go has a lot of potential to break the uapi. I could try to convert things like socket, ip and tcp myself, but the rest needs to be converted by the maintainer of the specific protocol, as it needs to be tested. As there are crazy things happening in the existing implementations, e.g. some getsockopt() implementations use optval as in and out buffer.

I first tried to convert both optval and optlen of getsockopt to sockptr_t,
and that showed that touching the optval part starts to get complex very soon,
see https://git.samba.org/?p=metze/linux/wip.git;a=commitdiff;h=141912166473bf8843ec6ace76dc9c6945adafd1
(note it didn't converted everything, I gave up after hitting sctp_getsockopt_peer_addrs and sctp_getsockopt_local_addrs.
sctp_getsockopt_context, sctp_getsockopt_maxseg, sctp_getsockopt_associnfo and maybe more are the ones also doing both copy_from_user and copy_to_user on optval)

I come also across one implementation that returned -ERANGE because *optlen was too short and put the required length into *optlen, which means the returned *optlen is larger than the optval buffer given from userspace.

Because of all these strange things I tried to do a minimal change in order to get rid of the io_uring limitation and only converted optlen and leave optval as is.

In order to have a patchset that has a low risk to cause regressions.

But as alternative introducing a prototype like this:

         int (*getsockopt_iter)(struct socket *sock, int level, int optname, struct iov_iter *optval_iter);

That returns a non-negative value which can be placed into *optlen or negative value as error and *optlen will not be changed on error. optval_iter will get direction ITER_DEST, so it can only be written to.

Quoted 5 lines

Implementations could then opt in for the new interface and allow do_sock_getsockopt() work also for the io_uring case, while all others would still get -EOPNOTSUPP.

So what should be the way to go?

Ok, I've added the infrastructure for getsockopt_iter, see below, but the first part I wanted to convert was tcp_ao_copy_mkts_to_user() and that also reads from userspace before writing.

Quoted 5 lines

So we could go with the optlen_t approach, or we need logic for ITER_BOTH or pass two iov_iters one with ITER_SRC and one with ITER_DEST...

So who wants to decide?

I just noticed that it's even possible in same cases
to pass in a short buffer to optval, but have a longer value in optlen, hci_sock_getsockopt() with SOL_BLUETOOTH completely ignores optlen.

Quoted 7 lines
Quoted 2 lines

This makes it really hard to believe that trying to use iov_iter for this is a good idea :-(

That was my finding as well a while ago, when I was planning to get the __user pointers converted to iov_iter. There are some weird ways of using optlen and optval, which makes them non-trivial to covert to iov_iter.

Quoted 4 lines

Can we ignore all non-ip/tcp/udp cases for now? This should cover +90% of useful socket opts. See if there are any obvious problems with them and if not, try converting. The rest we can cover separately when/if needed.

That's what I tried, but it fails with
tcp_getsockopt ->
do_tcp_getsockopt ->

     tcp_ao_get_mkts ->
        tcp_ao_copy_mkts_to_user ->
           copy_struct_from_sockptr
     tcp_ao_get_sock_info ->
        copy_struct_from_sockptr
Quoted 3 lines

That's not possible with a ITER_DEST iov_iter.

metze

Can we create two iterators over the same memory? One for ITER_SOURCE and another for ITER_DEST. And then make getsockopt_iter accept optval_in and optval_out. We can also use optval_out position (iov_offset) as optlen output value. Don't see why it won't work, but I agree that's gonna be a messy conversion so let's see if someone else has better suggestions.

#19 lore raw

Am 02.04.25 um 00:04 schrieb Stanislav Fomichev:

Quoted 115 lines · Stefan Metzmacher
Quoted 114 lines

Am 01.04.25 um 17:45 schrieb Stanislav Fomichev:

Quoted 97 lines · Breno Leitao
Quoted 88 lines · Stefan Metzmacher
Quoted 87 lines

Am 01.04.25 um 15:37 schrieb Stefan Metzmacher:

Quoted 76 lines

Am 01.04.25 um 10:19 schrieb Stefan Metzmacher:

Quoted 64 lines

Am 31.03.25 um 23:04 schrieb Stanislav Fomichev:

Quoted 19 lines · Stefan Metzmacher
Quoted 15 lines

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 as optlen to do_sock_getsockopt() and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

Quoted 7 lines
Quoted 3 lines

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().

At this point why not just fully embrace iov_iter? You have the size now + the user (or kernel) pointer. Might as well do s/sockptr_t/iov_iter/ conversion?

I think that would only be possible if we introduce proto[_ops].getsockopt_iter() and then convert the implementations step by step. Doing it all in one go has a lot of potential to break the uapi. I could try to convert things like socket, ip and tcp myself, but the rest needs to be converted by the maintainer of the specific protocol, as it needs to be tested. As there are crazy things happening in the existing implementations, e.g. some getsockopt() implementations use optval as in and out buffer.

I first tried to convert both optval and optlen of getsockopt to sockptr_t,
and that showed that touching the optval part starts to get complex very soon,
see https://git.samba.org/?p=metze/linux/wip.git;a=commitdiff;h=141912166473bf8843ec6ace76dc9c6945adafd1
(note it didn't converted everything, I gave up after hitting sctp_getsockopt_peer_addrs and sctp_getsockopt_local_addrs.
sctp_getsockopt_context, sctp_getsockopt_maxseg, sctp_getsockopt_associnfo and maybe more are the ones also doing both copy_from_user and copy_to_user on optval)

I come also across one implementation that returned -ERANGE because *optlen was too short and put the required length into *optlen, which means the returned *optlen is larger than the optval buffer given from userspace.

Because of all these strange things I tried to do a minimal change in order to get rid of the io_uring limitation and only converted optlen and leave optval as is.

In order to have a patchset that has a low risk to cause regressions.

But as alternative introducing a prototype like this:

         int (*getsockopt_iter)(struct socket *sock, int level, int optname, struct iov_iter *optval_iter);

That returns a non-negative value which can be placed into *optlen or negative value as error and *optlen will not be changed on error. optval_iter will get direction ITER_DEST, so it can only be written to.

Quoted 5 lines

Implementations could then opt in for the new interface and allow do_sock_getsockopt() work also for the io_uring case, while all others would still get -EOPNOTSUPP.

So what should be the way to go?

Ok, I've added the infrastructure for getsockopt_iter, see below, but the first part I wanted to convert was tcp_ao_copy_mkts_to_user() and that also reads from userspace before writing.

Quoted 5 lines

So we could go with the optlen_t approach, or we need logic for ITER_BOTH or pass two iov_iters one with ITER_SRC and one with ITER_DEST...

So who wants to decide?

I just noticed that it's even possible in same cases
to pass in a short buffer to optval, but have a longer value in optlen, hci_sock_getsockopt() with SOL_BLUETOOTH completely ignores optlen.

Quoted 7 lines
Quoted 2 lines

This makes it really hard to believe that trying to use iov_iter for this is a good idea :-(

That was my finding as well a while ago, when I was planning to get the __user pointers converted to iov_iter. There are some weird ways of using optlen and optval, which makes them non-trivial to covert to iov_iter.

Quoted 4 lines

Can we ignore all non-ip/tcp/udp cases for now? This should cover +90% of useful socket opts. See if there are any obvious problems with them and if not, try converting. The rest we can cover separately when/if needed.

That's what I tried, but it fails with
tcp_getsockopt ->

    do_tcp_getsockopt ->
      tcp_ao_get_mkts ->
         tcp_ao_copy_mkts_to_user ->
            copy_struct_from_sockptr
      tcp_ao_get_sock_info ->
         copy_struct_from_sockptr

That's not possible with a ITER_DEST iov_iter.

Quoted 7 lines
Quoted 1 line

metze

Can we create two iterators over the same memory? One for ITER_SOURCE and another for ITER_DEST. And then make getsockopt_iter accept optval_in and optval_out. We can also use optval_out position (iov_offset) as optlen output value. Don't see why it won't work, but I agree that's gonna be a messy conversion so let's see if someone else has better suggestions.

Yes, that might work, but it would be good to get some feedback if this would be the way to go:

int (*getsockopt_iter)(struct socket *sock,
int level, int optname, struct iov_iter *optval_in, struct iov_iter *optval_out);

And *optlen = optval_out->iov_offset;

Any objection or better ideas? Linus would that be what you had in mind?

Thanks!
metze

#29 lore raw
In reply to Stefan Metzmacher

"

Quoted 2 lines · Stefan Metzmacher

But as Linus don't like 'sockptr_t' I used a different approach.

So the sockptr_t thing has already happened. I hate it, and I think it's ugly as hell, but it is what it is.

I think it's a complete hack and having that "kernel or user" pointer flag is disgusting.

Making things worse, the naming is disgusting too, talking about some random "socket pointer", when it has absolutely nothing to do with socket, and isn't even a pointer. It's something else.

It's literally called "socket" not because it has anything to do with sockets, but because it's a socket-specific hack that isn't acceptable anywhere else in the kernel.

So that "socket" part of the name is literally shorthand for "only sockets are disgusting enough to use this, and nobody else should ever touch this crap".

At least so far that part has mostly worked, even if there's some "sockptr_t" use in the crypto code. I didn't look closer, because I didn't want to lose my lunch.

I don't understand why the networking code uses that thing.

If you have a "fat pointer", you should damn well make it have the size of the area too, and do things *right*.

Instead of doing what sockptr_t does, which is a complete hack to just pass a kernel/user flag, and then passes the length *separately* because the socket code couldn't be arsed to do the right thing.

So I do still think "sockptr_t" should die.

As Stanislav says, if you actually want that "user or kernel" thing, just use an "iov_iter".

No, an "iov_iter" isn't exactly a pretty thing either, but at least it's the standard way to say "this pointer can have multiple different kinds of sources".

And it keeps the size of the thing it points to around, so it's at least a fat pointer with proper ranges, even if it isn't exactly "type safe" (yes, it's type safe in the sense that it stays as a "iov_iter", but it's still basically a "random pointer").

Quoted 1 line

@Linus, would that optlen_t approach fit better for you?

The optlen_t thing is slightly better mainly because it's more type-safe. At least it's not a "random misnamed user-or-kernel-pointer" thing where the name is about how nothing else is so broken as to use it.

So it's better because it's more limited, and it's better in that at least it has a type-safe pointer rather than a "void *" with no size or type associated with it.

That said, I don't think it's exactly great.

It's just another case of "networking can't just do it right, and uses a random hack with special flag values".

So I do think that it would be better to actually get rid of "sockptr_t optval, unsigned int optlen" ENTIRELY, and replace that with iov_iter and just make networking bite the bullet and do the RightThing(tm).

In fact, to make it *really* typesafe, it might be a good idea to wrap the iov_iter in another struct, something like

   typedef struct sockopt {
        struct iov_iter iter;
   } sockopt_t;

and make the networking functions make the typing very clear, and end up with an interface something like

int do_tcp_setsockopt(struct sock *sk,
int level, int optname,
sockopt_t *val);

where that "sockopt_t *val" replaces not just the "sockptr_t optval", but also the "unsigned int optlen" thing.

And no, I didn't look at how much churn that would be. Probably a lot. Maybe more than people are willing to do - even if I think some of it could be automated with coccinelle or whatever.

Linus

#20 lore raw
In reply to Stefan Metzmacher
Quoted 127 lines · On Wed, 2 Apr 2025 00:53:58 +0200 Stefan Metzmacher <[email protected]> wrote:

Am 02.04.25 um 00:04 schrieb Stanislav Fomichev:

Quoted 115 lines · Stefan Metzmacher
Quoted 114 lines

Am 01.04.25 um 17:45 schrieb Stanislav Fomichev:

Quoted 97 lines · Breno Leitao
Quoted 88 lines · Stefan Metzmacher
Quoted 87 lines

Am 01.04.25 um 15:37 schrieb Stefan Metzmacher:

Quoted 76 lines

Am 01.04.25 um 10:19 schrieb Stefan Metzmacher:

Quoted 64 lines

Am 31.03.25 um 23:04 schrieb Stanislav Fomichev:

Quoted 19 lines · Stefan Metzmacher
Quoted 15 lines

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 as optlen to do_sock_getsockopt() and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

Quoted 7 lines
Quoted 3 lines

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().

At this point why not just fully embrace iov_iter? You have the size now + the user (or kernel) pointer. Might as well do s/sockptr_t/iov_iter/ conversion?

I think that would only be possible if we introduce proto[_ops].getsockopt_iter() and then convert the implementations step by step. Doing it all in one go has a lot of potential to break the uapi. I could try to convert things like socket, ip and tcp myself, but the rest needs to be converted by the maintainer of the specific protocol, as it needs to be tested. As there are crazy things happening in the existing implementations, e.g. some getsockopt() implementations use optval as in and out buffer.

I first tried to convert both optval and optlen of getsockopt to sockptr_t,
and that showed that touching the optval part starts to get complex very soon,
see https://git.samba.org/?p=metze/linux/wip.git;a=commitdiff;h=141912166473bf8843ec6ace76dc9c6945adafd1
(note it didn't converted everything, I gave up after hitting sctp_getsockopt_peer_addrs and sctp_getsockopt_local_addrs.
sctp_getsockopt_context, sctp_getsockopt_maxseg, sctp_getsockopt_associnfo and maybe more are the ones also doing both copy_from_user and copy_to_user on optval)

I come also across one implementation that returned -ERANGE because *optlen was too short and put the required length into *optlen, which means the returned *optlen is larger than the optval buffer given from userspace.

Because of all these strange things I tried to do a minimal change in order to get rid of the io_uring limitation and only converted optlen and leave optval as is.

In order to have a patchset that has a low risk to cause regressions.

But as alternative introducing a prototype like this:

         int (*getsockopt_iter)(struct socket *sock, int level, int optname, struct iov_iter *optval_iter);

That returns a non-negative value which can be placed into *optlen or negative value as error and *optlen will not be changed on error. optval_iter will get direction ITER_DEST, so it can only be written to.

Quoted 5 lines

Implementations could then opt in for the new interface and allow do_sock_getsockopt() work also for the io_uring case, while all others would still get -EOPNOTSUPP.

So what should be the way to go?

Ok, I've added the infrastructure for getsockopt_iter, see below, but the first part I wanted to convert was tcp_ao_copy_mkts_to_user() and that also reads from userspace before writing.

Quoted 5 lines

So we could go with the optlen_t approach, or we need logic for ITER_BOTH or pass two iov_iters one with ITER_SRC and one with ITER_DEST...

So who wants to decide?

I just noticed that it's even possible in same cases
to pass in a short buffer to optval, but have a longer value in optlen, hci_sock_getsockopt() with SOL_BLUETOOTH completely ignores optlen.

Quoted 7 lines
Quoted 2 lines

This makes it really hard to believe that trying to use iov_iter for this is a good idea :-(

That was my finding as well a while ago, when I was planning to get the __user pointers converted to iov_iter. There are some weird ways of using optlen and optval, which makes them non-trivial to covert to iov_iter.

Quoted 4 lines

Can we ignore all non-ip/tcp/udp cases for now? This should cover +90% of useful socket opts. See if there are any obvious problems with them and if not, try converting. The rest we can cover separately when/if needed.

That's what I tried, but it fails with
tcp_getsockopt ->

    do_tcp_getsockopt ->
      tcp_ao_get_mkts ->
         tcp_ao_copy_mkts_to_user ->
            copy_struct_from_sockptr
      tcp_ao_get_sock_info ->
         copy_struct_from_sockptr

That's not possible with a ITER_DEST iov_iter.

Quoted 7 lines
Quoted 1 line

metze

Can we create two iterators over the same memory? One for ITER_SOURCE and another for ITER_DEST. And then make getsockopt_iter accept optval_in and optval_out. We can also use optval_out position (iov_offset) as optlen output value. Don't see why it won't work, but I agree that's gonna be a messy conversion so let's see if someone else has better suggestions.

Yes, that might work, but it would be good to get some feedback if this would be the way to go:

Quoted 8 lines

int (*getsockopt_iter)(struct socket *sock,
int level, int optname, struct iov_iter *optval_in, struct iov_iter *optval_out);

And *optlen = optval_out->iov_offset;

Any objection or better ideas? Linus would that be what you had in mind?

I'd worry about performance - yes I know 'iter' are used elsewhere but... Also look at the SCTP code.

How do you handle code that wants to return an updated length (often longer than the one provided) and an error code (eg ERRSIZE or similar).

There is also a very strange use (I think it is a sockopt rather than an ioctl) where the buffer length the application provides is only that of the header. The actual buffer length is contained in the header.
The return length is the amount written into the full buffer.

David

#30 lore raw
In reply to Linus Torvalds
Quoted 11 lines · On Tue, 1 Apr 2025 17:40:19 -0700 Linus Torvalds…

"

Quoted 2 lines · Stefan Metzmacher

But as Linus don't like 'sockptr_t' I used a different approach.

So the sockptr_t thing has already happened. I hate it, and I think it's ugly as hell, but it is what it is.

I think it's a complete hack and having that "kernel or user" pointer flag is disgusting.

I have proposed a patch which replaced it with a structure. That showed up some really hacky code in IIRC io_uring.

Using sockptr_t for the buffer was one thing, the generic code can't copy the buffer to/from user because code lies about the length.

But using for the length is just brain-dead.
That is fixed size and can be copied from/to user by the wrapper. The code bloat reduction will be significant.

David

#21 lore raw
In reply to David Laight
Quoted 135 lines · David Laight
Quoted 132 lines · On Wed, 2 Apr 2025 00:53:58 +0200 Stefan Metzmacher <[email protected]> wrote:

Am 02.04.25 um 00:04 schrieb Stanislav Fomichev:

Quoted 115 lines · Stefan Metzmacher
Quoted 114 lines

Am 01.04.25 um 17:45 schrieb Stanislav Fomichev:

Quoted 97 lines · Breno Leitao
Quoted 88 lines · Stefan Metzmacher
Quoted 87 lines

Am 01.04.25 um 15:37 schrieb Stefan Metzmacher:

Quoted 76 lines

Am 01.04.25 um 10:19 schrieb Stefan Metzmacher:

Quoted 64 lines

Am 31.03.25 um 23:04 schrieb Stanislav Fomichev:

Quoted 19 lines · Stefan Metzmacher
Quoted 15 lines

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 as optlen to do_sock_getsockopt() and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

Quoted 7 lines
Quoted 3 lines

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().

At this point why not just fully embrace iov_iter? You have the size now + the user (or kernel) pointer. Might as well do s/sockptr_t/iov_iter/ conversion?

I think that would only be possible if we introduce proto[_ops].getsockopt_iter() and then convert the implementations step by step. Doing it all in one go has a lot of potential to break the uapi. I could try to convert things like socket, ip and tcp myself, but the rest needs to be converted by the maintainer of the specific protocol, as it needs to be tested. As there are crazy things happening in the existing implementations, e.g. some getsockopt() implementations use optval as in and out buffer.

I first tried to convert both optval and optlen of getsockopt to sockptr_t,
and that showed that touching the optval part starts to get complex very soon,
see https://git.samba.org/?p=metze/linux/wip.git;a=commitdiff;h=141912166473bf8843ec6ace76dc9c6945adafd1
(note it didn't converted everything, I gave up after hitting sctp_getsockopt_peer_addrs and sctp_getsockopt_local_addrs.
sctp_getsockopt_context, sctp_getsockopt_maxseg, sctp_getsockopt_associnfo and maybe more are the ones also doing both copy_from_user and copy_to_user on optval)

I come also across one implementation that returned -ERANGE because *optlen was too short and put the required length into *optlen, which means the returned *optlen is larger than the optval buffer given from userspace.

Because of all these strange things I tried to do a minimal change in order to get rid of the io_uring limitation and only converted optlen and leave optval as is.

In order to have a patchset that has a low risk to cause regressions.

But as alternative introducing a prototype like this:

         int (*getsockopt_iter)(struct socket *sock, int level, int optname, struct iov_iter *optval_iter);

That returns a non-negative value which can be placed into *optlen or negative value as error and *optlen will not be changed on error. optval_iter will get direction ITER_DEST, so it can only be written to.

Quoted 5 lines

Implementations could then opt in for the new interface and allow do_sock_getsockopt() work also for the io_uring case, while all others would still get -EOPNOTSUPP.

So what should be the way to go?

Ok, I've added the infrastructure for getsockopt_iter, see below, but the first part I wanted to convert was tcp_ao_copy_mkts_to_user() and that also reads from userspace before writing.

Quoted 5 lines

So we could go with the optlen_t approach, or we need logic for ITER_BOTH or pass two iov_iters one with ITER_SRC and one with ITER_DEST...

So who wants to decide?

I just noticed that it's even possible in same cases
to pass in a short buffer to optval, but have a longer value in optlen, hci_sock_getsockopt() with SOL_BLUETOOTH completely ignores optlen.

Quoted 7 lines
Quoted 2 lines

This makes it really hard to believe that trying to use iov_iter for this is a good idea :-(

That was my finding as well a while ago, when I was planning to get the __user pointers converted to iov_iter. There are some weird ways of using optlen and optval, which makes them non-trivial to covert to iov_iter.

Quoted 4 lines

Can we ignore all non-ip/tcp/udp cases for now? This should cover +90% of useful socket opts. See if there are any obvious problems with them and if not, try converting. The rest we can cover separately when/if needed.

That's what I tried, but it fails with
tcp_getsockopt ->

    do_tcp_getsockopt ->
      tcp_ao_get_mkts ->
         tcp_ao_copy_mkts_to_user ->
            copy_struct_from_sockptr
      tcp_ao_get_sock_info ->
         copy_struct_from_sockptr

That's not possible with a ITER_DEST iov_iter.

Quoted 7 lines
Quoted 1 line

metze

Can we create two iterators over the same memory? One for ITER_SOURCE and another for ITER_DEST. And then make getsockopt_iter accept optval_in and optval_out. We can also use optval_out position (iov_offset) as optlen output value. Don't see why it won't work, but I agree that's gonna be a messy conversion so let's see if someone else has better suggestions.

Yes, that might work, but it would be good to get some feedback if this would be the way to go:

int (*getsockopt_iter)(struct socket *sock,
int level, int optname, struct iov_iter *optval_in, struct iov_iter *optval_out);

Quoted 6 lines
Quoted 3 lines

And *optlen = optval_out->iov_offset;

Any objection or better ideas? Linus would that be what you had in mind?

I'd worry about performance - yes I know 'iter' are used elsewhere but... Also look at the SCTP code.

Performance usually does not matter for set/getsockopts, there are a few exceptions that I know (TCP_ZEROCOPY_RECEIVE) and maybe recent devmem sockopts; we can special-case these if needed, or keep sockptr_t, idk. I'm skeptical we can convert everything though, that's why the suggestion to start with sk/ip/tcp/udp.

Quoted 7 lines

How do you handle code that wants to return an updated length (often longer than the one provided) and an error code (eg ERRSIZE or similar).

There is also a very strange use (I think it is a sockopt rather than an ioctl) where the buffer length the application provides is only that of the header. The actual buffer length is contained in the header.
The return length is the amount written into the full buffer.

Let's discuss these special cases as they come up? Worst case these places can always re-init iov_iter with a comment on why it is ok. But I do agree in general that there are a few places that do wild stuff.

#22 lore raw
Quoted 138 lines · On Wed, 2 Apr 2025 07:19:46 -0700 Stanislav Fomichev <[email protected]>…
Quoted 137 lines · David Laight
Quoted 134 lines · On Wed, 2 Apr 2025 00:53:58 +0200 Stefan Metzmacher <[email protected]> wrote:

Am 02.04.25 um 00:04 schrieb Stanislav Fomichev:

Quoted 115 lines · Stefan Metzmacher
Quoted 114 lines

Am 01.04.25 um 17:45 schrieb Stanislav Fomichev:

Quoted 97 lines · Breno Leitao
Quoted 88 lines · Stefan Metzmacher
Quoted 87 lines

Am 01.04.25 um 15:37 schrieb Stefan Metzmacher:

Quoted 76 lines

Am 01.04.25 um 10:19 schrieb Stefan Metzmacher:

Quoted 64 lines

Am 31.03.25 um 23:04 schrieb Stanislav Fomichev:

Quoted 19 lines · Stefan Metzmacher
Quoted 15 lines

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 as optlen to do_sock_getsockopt() and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

Quoted 7 lines
Quoted 3 lines

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().

At this point why not just fully embrace iov_iter? You have the size now + the user (or kernel) pointer. Might as well do s/sockptr_t/iov_iter/ conversion?

I think that would only be possible if we introduce proto[_ops].getsockopt_iter() and then convert the implementations step by step. Doing it all in one go has a lot of potential to break the uapi. I could try to convert things like socket, ip and tcp myself, but the rest needs to be converted by the maintainer of the specific protocol, as it needs to be tested. As there are crazy things happening in the existing implementations, e.g. some getsockopt() implementations use optval as in and out buffer.

I first tried to convert both optval and optlen of getsockopt to sockptr_t,
and that showed that touching the optval part starts to get complex very soon,
see https://git.samba.org/?p=metze/linux/wip.git;a=commitdiff;h=141912166473bf8843ec6ace76dc9c6945adafd1
(note it didn't converted everything, I gave up after hitting sctp_getsockopt_peer_addrs and sctp_getsockopt_local_addrs.
sctp_getsockopt_context, sctp_getsockopt_maxseg, sctp_getsockopt_associnfo and maybe more are the ones also doing both copy_from_user and copy_to_user on optval)

I come also across one implementation that returned -ERANGE because *optlen was too short and put the required length into *optlen, which means the returned *optlen is larger than the optval buffer given from userspace.

Because of all these strange things I tried to do a minimal change in order to get rid of the io_uring limitation and only converted optlen and leave optval as is.

In order to have a patchset that has a low risk to cause regressions.

But as alternative introducing a prototype like this:

         int (*getsockopt_iter)(struct socket *sock, int level, int optname, struct iov_iter *optval_iter);

That returns a non-negative value which can be placed into *optlen or negative value as error and *optlen will not be changed on error. optval_iter will get direction ITER_DEST, so it can only be written to.

Quoted 5 lines

Implementations could then opt in for the new interface and allow do_sock_getsockopt() work also for the io_uring case, while all others would still get -EOPNOTSUPP.

So what should be the way to go?

Ok, I've added the infrastructure for getsockopt_iter, see below, but the first part I wanted to convert was tcp_ao_copy_mkts_to_user() and that also reads from userspace before writing.

Quoted 5 lines

So we could go with the optlen_t approach, or we need logic for ITER_BOTH or pass two iov_iters one with ITER_SRC and one with ITER_DEST...

So who wants to decide?

I just noticed that it's even possible in same cases
to pass in a short buffer to optval, but have a longer value in optlen, hci_sock_getsockopt() with SOL_BLUETOOTH completely ignores optlen.

Quoted 7 lines
Quoted 2 lines

This makes it really hard to believe that trying to use iov_iter for this is a good idea :-(

That was my finding as well a while ago, when I was planning to get the __user pointers converted to iov_iter. There are some weird ways of using optlen and optval, which makes them non-trivial to covert to iov_iter.

Quoted 4 lines

Can we ignore all non-ip/tcp/udp cases for now? This should cover +90% of useful socket opts. See if there are any obvious problems with them and if not, try converting. The rest we can cover separately when/if needed.

That's what I tried, but it fails with
tcp_getsockopt ->

    do_tcp_getsockopt ->
      tcp_ao_get_mkts ->
         tcp_ao_copy_mkts_to_user ->
            copy_struct_from_sockptr
      tcp_ao_get_sock_info ->
         copy_struct_from_sockptr

That's not possible with a ITER_DEST iov_iter.

Quoted 7 lines
Quoted 1 line

metze

Can we create two iterators over the same memory? One for ITER_SOURCE and another for ITER_DEST. And then make getsockopt_iter accept optval_in and optval_out. We can also use optval_out position (iov_offset) as optlen output value. Don't see why it won't work, but I agree that's gonna be a messy conversion so let's see if someone else has better suggestions.

Yes, that might work, but it would be good to get some feedback if this would be the way to go:

int (*getsockopt_iter)(struct socket *sock,
int level, int optname, struct iov_iter *optval_in, struct iov_iter *optval_out);

And *optlen = optval_out->iov_offset;

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Any objection or better ideas? Linus would that be what you had in mind?

I'd worry about performance - yes I know 'iter' are used elsewhere but... Also look at the SCTP code.

Performance usually does not matter for set/getsockopts, there are a few exceptions that I know (TCP_ZEROCOPY_RECEIVE)

That might be the one that is really horrid and completely abuses the 'length' parameter.

Quoted 13 lines

and maybe recent
devmem sockopts; we can special-case these if needed, or keep sockptr_t, idk. I'm skeptical we can convert everything though, that's why the suggestion to start with sk/ip/tcp/udp.

Quoted 7 lines

How do you handle code that wants to return an updated length (often longer than the one provided) and an error code (eg ERRSIZE or similar).

There is also a very strange use (I think it is a sockopt rather than an ioctl) where the buffer length the application provides is only that of the header. The actual buffer length is contained in the header.
The return length is the amount written into the full buffer.

Quoted 4 lines

Let's discuss these special cases as they come up? Worst case these places can always re-init iov_iter with a comment on why it is ok. But I do agree in general that there are a few places that do wild stuff.

The problem is that the generic code has to deal with all the 'wild stuff'. It is also common to do non-sequential accesses - so iov_iter doesn't match at all.
There also isn't a requirement for scatter-gather.

For 'normal' getsockopt (and setsockopt) with short lengths it actually makes sense for the syscall wrapper to do the user copies.
But it would need to pass the user ptr+len as well as the kernel ptr+len to give the required flexibilty.
Then you have to work out whether the final copy to user is needed or not. (not that hard, but it all adds complication).

David

#23 lore raw
Quoted 5 lines · David Laight

The problem is that the generic code has to deal with all the 'wild stuff'. It is also common to do non-sequential accesses - so iov_iter doesn't match at all.
There also isn't a requirement for scatter-gather.

Note that the generic code has special cases for the simple stuff, which is all that the sockopt code would need.

Now, that's _particularly_ true for the "single user address range" thing, where there's a special ITER_UBUF thing.

We don't actually have a "single kernel range" version of that, but ITER_KVEC is simple to use, and the sockopt code could say "I only ever look at the first buffer".

It's ok to just not handle all the cases, and you don't *have* to use the generic "copy_from_iter()" routines if you don't want to.

In fact, I would expect that something like sockopt generally wouldn't want to use the normal iter copying routines, since those are basically all geared towards "copy and update the iter".

Linus

#24 lore raw
In reply to David Laight
Quoted 143 lines · David Laight
Quoted 140 lines · On Wed, 2 Apr 2025 07:19:46 -0700 Stanislav Fomichev <[email protected]>…
Quoted 139 lines · David Laight
Quoted 135 lines · On Wed, 2 Apr 2025 00:53:58 +0200 Stefan Metzmacher <[email protected]> wrote:

Am 02.04.25 um 00:04 schrieb Stanislav Fomichev:

Quoted 115 lines · Stefan Metzmacher
Quoted 114 lines

Am 01.04.25 um 17:45 schrieb Stanislav Fomichev:

Quoted 97 lines · Breno Leitao
Quoted 88 lines · Stefan Metzmacher
Quoted 87 lines

Am 01.04.25 um 15:37 schrieb Stefan Metzmacher:

Quoted 76 lines

Am 01.04.25 um 10:19 schrieb Stefan Metzmacher:

Quoted 64 lines

Am 31.03.25 um 23:04 schrieb Stanislav Fomichev:

Quoted 19 lines · Stefan Metzmacher
Quoted 15 lines

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 as optlen to do_sock_getsockopt()
and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

Quoted 7 lines
Quoted 3 lines

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().

At this point why not just fully embrace iov_iter? You have the size now + the user (or kernel) pointer. Might as well do s/sockptr_t/iov_iter/ conversion?

I think that would only be possible if we introduce proto[_ops].getsockopt_iter() and then convert the implementations step by step. Doing it all in one go has a lot of potential to break the uapi. I could try to convert things like socket, ip and tcp myself, but the rest needs to be converted by the maintainer of the specific protocol, as it needs to be tested. As there are crazy things happening in the existing implementations, e.g. some getsockopt() implementations use optval as in and out buffer.

I first tried to convert both optval and optlen of getsockopt to sockptr_t,
and that showed that touching the optval part starts to get complex very soon,
see https://git.samba.org/?p=metze/linux/wip.git;a=commitdiff;h=141912166473bf8843ec6ace76dc9c6945adafd1
(note it didn't converted everything, I gave up after hitting sctp_getsockopt_peer_addrs and sctp_getsockopt_local_addrs.
sctp_getsockopt_context, sctp_getsockopt_maxseg, sctp_getsockopt_associnfo and maybe more are the ones also doing both copy_from_user and copy_to_user on optval)

I come also across one implementation that returned -ERANGE because *optlen was too short and put the required length into *optlen, which means the returned *optlen is larger than the optval buffer given from userspace.

Because of all these strange things I tried to do a minimal change in order to get rid of the io_uring limitation and only converted optlen and leave optval as is.

In order to have a patchset that has a low risk to cause regressions.

But as alternative introducing a prototype like this:

         int (*getsockopt_iter)(struct socket *sock, int level, int optname, struct iov_iter *optval_iter);

That returns a non-negative value which can be placed into *optlen or negative value as error and *optlen will not be changed on error. optval_iter will get direction ITER_DEST, so it can only be written to.

Quoted 5 lines

Implementations could then opt in for the new interface and allow do_sock_getsockopt() work also for the io_uring case, while all others would still get -EOPNOTSUPP.

So what should be the way to go?

Ok, I've added the infrastructure for getsockopt_iter, see below, but the first part I wanted to convert was tcp_ao_copy_mkts_to_user() and that also reads from userspace before writing.

Quoted 5 lines

So we could go with the optlen_t approach, or we need logic for ITER_BOTH or pass two iov_iters one with ITER_SRC and one with ITER_DEST...

So who wants to decide?

I just noticed that it's even possible in same cases
to pass in a short buffer to optval, but have a longer value in optlen, hci_sock_getsockopt() with SOL_BLUETOOTH completely ignores optlen.

Quoted 7 lines
Quoted 2 lines

This makes it really hard to believe that trying to use iov_iter for this is a good idea :-(

That was my finding as well a while ago, when I was planning to get the __user pointers converted to iov_iter. There are some weird ways of using optlen and optval, which makes them non-trivial to covert to iov_iter.

Quoted 4 lines

Can we ignore all non-ip/tcp/udp cases for now? This should cover +90% of useful socket opts. See if there are any obvious problems with them and if not, try converting. The rest we can cover separately when/if needed.

That's what I tried, but it fails with
tcp_getsockopt ->

    do_tcp_getsockopt ->
      tcp_ao_get_mkts ->
         tcp_ao_copy_mkts_to_user ->
            copy_struct_from_sockptr
      tcp_ao_get_sock_info ->
         copy_struct_from_sockptr

That's not possible with a ITER_DEST iov_iter.

Quoted 7 lines
Quoted 1 line

metze

Can we create two iterators over the same memory? One for ITER_SOURCE and another for ITER_DEST. And then make getsockopt_iter accept optval_in and optval_out. We can also use optval_out position (iov_offset) as optlen output value. Don't see why it won't work, but I agree that's gonna be a messy conversion so let's see if someone else has better suggestions.

Yes, that might work, but it would be good to get some feedback if this would be the way to go:

int (*getsockopt_iter)(struct socket *sock,
int level, int optname, struct iov_iter *optval_in, struct iov_iter *optval_out);

And *optlen = optval_out->iov_offset;

Any objection or better ideas? Linus would that be what you had in mind?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I'd worry about performance - yes I know 'iter' are used elsewhere but... Also look at the SCTP code.

Performance usually does not matter for set/getsockopts, there are a few exceptions that I know (TCP_ZEROCOPY_RECEIVE)

That might be the one that is really horrid and completely abuses the 'length' parameter.

It is reading and writing, yes, but it's not a huge problem. And it does enforce the optlen (to copy back the same amount of bytes). It's not that bad, it's just an example of where we need to be extra careful.

Quoted 13 lines
Quoted 13 lines

and maybe recent
devmem sockopts; we can special-case these if needed, or keep sockptr_t, idk. I'm skeptical we can convert everything though, that's why the suggestion to start with sk/ip/tcp/udp.

Quoted 7 lines

How do you handle code that wants to return an updated length (often longer than the one provided) and an error code (eg ERRSIZE or similar).

There is also a very strange use (I think it is a sockopt rather than an ioctl) where the buffer length the application provides is only that of the header. The actual buffer length is contained in the header.
The return length is the amount written into the full buffer.

Quoted 6 lines
Quoted 4 lines

Let's discuss these special cases as they come up? Worst case these places can always re-init iov_iter with a comment on why it is ok. But I do agree in general that there are a few places that do wild stuff.

The problem is that the generic code has to deal with all the 'wild stuff'.

getsockopt_iter will have optval_in for the minority of socket options (like TCP_ZEROCOPY_RECEIVE) that want to read user's value as well as optval_out. The latter is what the majority of socket options will use to write their value. That doesn't seem too complicated to handle?

Quoted 2 lines

It is also common to do non-sequential accesses - so iov_iter doesn't match at all.

I disagree that it's 'common'. Searching for copy_from_sockptr_offset returns a few cases and they are mostly using read-with-offset because there is no sequential read (iterator) semantics with sockptr_t.

Quoted 8 lines

There also isn't a requirement for scatter-gather.

For 'normal' getsockopt (and setsockopt) with short lengths it actually makes sense for the syscall wrapper to do the user copies.
But it would need to pass the user ptr+len as well as the kernel ptr+len to give the required flexibilty.
Then you have to work out whether the final copy to user is needed or not. (not that hard, but it all adds complication).

Not sure I understand what's the problem. The user vs kernel part will be abstracted by iov_iter. The callers will have to write the optlen back. And there are two call sites we care about: io_uring and regular system call. What's your suggestion? Maybe I'm missing something. Do you prefer get_optlen/put_optlen?

#25 lore raw
Quoted 200 lines · On Wed, 2 Apr 2025 14:21:35 -0700 Stanislav Fomichev <[email protected]>…
Quoted 143 lines · David Laight
Quoted 140 lines · On Wed, 2 Apr 2025 07:19:46 -0700 Stanislav Fomichev <[email protected]>…
Quoted 139 lines · David Laight
Quoted 135 lines · On Wed, 2 Apr 2025 00:53:58 +0200 Stefan Metzmacher <[email protected]> wrote:

Am 02.04.25 um 00:04 schrieb Stanislav Fomichev:

Quoted 115 lines · Stefan Metzmacher
Quoted 114 lines

Am 01.04.25 um 17:45 schrieb Stanislav Fomichev:

Quoted 97 lines · Breno Leitao
Quoted 88 lines · Stefan Metzmacher
Quoted 87 lines

Am 01.04.25 um 15:37 schrieb Stefan Metzmacher:

Quoted 76 lines

Am 01.04.25 um 10:19 schrieb Stefan Metzmacher:

Quoted 64 lines

Am 31.03.25 um 23:04 schrieb Stanislav Fomichev:

Quoted 26 lines · Stefan Metzmacher

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 as optlen to do_sock_getsockopt()
and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

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().

At this point why not just fully embrace iov_iter? You have the size
now + the user (or kernel) pointer. Might as well do
s/sockptr_t/iov_iter/ conversion?

I think that would only be possible if we introduce proto[_ops].getsockopt_iter() and then convert the implementations step by step. Doing it all in one go has a lot of potential to break the uapi. I could try to convert things like socket, ip and tcp myself, but the rest needs to be converted by the maintainer of the specific protocol, as it needs to be tested. As there are crazy things happening in the existing implementations, e.g. some getsockopt() implementations use optval as in and out buffer.

I first tried to convert both optval and optlen of getsockopt to sockptr_t,
and that showed that touching the optval part starts to get complex very soon,
see https://git.samba.org/?p=metze/linux/wip.git;a=commitdiff;h=141912166473bf8843ec6ace76dc9c6945adafd1
(note it didn't converted everything, I gave up after hitting sctp_getsockopt_peer_addrs and sctp_getsockopt_local_addrs.
sctp_getsockopt_context, sctp_getsockopt_maxseg, sctp_getsockopt_associnfo and maybe more are the ones also doing both copy_from_user and copy_to_user on optval)

I come also across one implementation that returned -ERANGE because *optlen was too short and put the required length into *optlen, which means the returned *optlen is larger than the optval buffer given from userspace.

Because of all these strange things I tried to do a minimal change in order to get rid of the io_uring limitation and only converted optlen and leave optval as is.

In order to have a patchset that has a low risk to cause regressions.

But as alternative introducing a prototype like this:

         int (*getsockopt_iter)(struct socket *sock, int level, int optname, struct iov_iter *optval_iter);

That returns a non-negative value which can be placed into *optlen or negative value as error and *optlen will not be changed on error. optval_iter will get direction ITER_DEST, so it can only be written to.

Quoted 5 lines

Implementations could then opt in for the new interface and allow do_sock_getsockopt() work also for the io_uring case, while all others would still get -EOPNOTSUPP.

So what should be the way to go?

Ok, I've added the infrastructure for getsockopt_iter, see below, but the first part I wanted to convert was tcp_ao_copy_mkts_to_user() and that also reads from userspace before writing.

Quoted 5 lines

So we could go with the optlen_t approach, or we need logic for ITER_BOTH or pass two iov_iters one with ITER_SRC and one with ITER_DEST...

So who wants to decide?

I just noticed that it's even possible in same cases
to pass in a short buffer to optval, but have a longer value in optlen, hci_sock_getsockopt() with SOL_BLUETOOTH completely ignores optlen.

Quoted 7 lines
Quoted 2 lines

This makes it really hard to believe that trying to use iov_iter for this is a good idea :-(

That was my finding as well a while ago, when I was planning to get the __user pointers converted to iov_iter. There are some weird ways of using optlen and optval, which makes them non-trivial to covert to iov_iter.

Quoted 4 lines

Can we ignore all non-ip/tcp/udp cases for now? This should cover +90% of useful socket opts. See if there are any obvious problems with them and if not, try converting. The rest we can cover separately when/if needed.

That's what I tried, but it fails with
tcp_getsockopt ->

    do_tcp_getsockopt ->
      tcp_ao_get_mkts ->
         tcp_ao_copy_mkts_to_user ->
            copy_struct_from_sockptr
      tcp_ao_get_sock_info ->
         copy_struct_from_sockptr

That's not possible with a ITER_DEST iov_iter.

Quoted 7 lines
Quoted 1 line

metze

Can we create two iterators over the same memory? One for ITER_SOURCE and another for ITER_DEST. And then make getsockopt_iter accept optval_in and optval_out. We can also use optval_out position (iov_offset) as optlen output value. Don't see why it won't work, but I agree that's gonna be a messy conversion so let's see if someone else has better suggestions.

Yes, that might work, but it would be good to get some feedback if this would be the way to go:

int (*getsockopt_iter)(struct socket *sock,
int level, int optname, struct iov_iter *optval_in, struct iov_iter *optval_out);

And *optlen = optval_out->iov_offset;

Any objection or better ideas? Linus would that be what you had in mind?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I'd worry about performance - yes I know 'iter' are used elsewhere but... Also look at the SCTP code.

Performance usually does not matter for set/getsockopts, there are a few exceptions that I know (TCP_ZEROCOPY_RECEIVE)

That might be the one that is really horrid and completely abuses the 'length' parameter.

It is reading and writing, yes, but it's not a huge problem. And it does enforce the optlen (to copy back the same amount of bytes). It's not that bad, it's just an example of where we need to be extra careful.

Quoted 13 lines
Quoted 13 lines

and maybe recent
devmem sockopts; we can special-case these if needed, or keep sockptr_t, idk. I'm skeptical we can convert everything though, that's why the suggestion to start with sk/ip/tcp/udp.

Quoted 7 lines

How do you handle code that wants to return an updated length (often longer than the one provided) and an error code (eg ERRSIZE or similar).

There is also a very strange use (I think it is a sockopt rather than an ioctl) where the buffer length the application provides is only that of the header. The actual buffer length is contained in the header.
The return length is the amount written into the full buffer.

Quoted 6 lines
Quoted 4 lines

Let's discuss these special cases as they come up? Worst case these places can always re-init iov_iter with a comment on why it is ok. But I do agree in general that there are a few places that do wild stuff.

The problem is that the generic code has to deal with all the 'wild stuff'.

getsockopt_iter will have optval_in for the minority of socket options (like TCP_ZEROCOPY_RECEIVE) that want to read user's value as well as optval_out. The latter is what the majority of socket options will use to write their value. That doesn't seem too complicated to handle?

Quoted 2 lines

It is also common to do non-sequential accesses - so iov_iter doesn't match at all.

I disagree that it's 'common'. Searching for copy_from_sockptr_offset returns a few cases and they are mostly using read-with-offset because there is no sequential read (iterator) semantics with sockptr_t.

Quoted 8 lines

There also isn't a requirement for scatter-gather.

For 'normal' getsockopt (and setsockopt) with short lengths it actually makes sense for the syscall wrapper to do the user copies.
But it would need to pass the user ptr+len as well as the kernel ptr+len to give the required flexibilty.
Then you have to work out whether the final copy to user is needed or not. (not that hard, but it all adds complication).

Quoted 5 lines

Not sure I understand what's the problem. The user vs kernel part will be abstracted by iov_iter. The callers will have to write the optlen back. And there are two call sites we care about: io_uring and regular system call. What's your suggestion? Maybe I'm missing something. Do you prefer get_optlen/put_optlen?

I think the final aim should be to pass the user supplied length to the per-protocol code and have it return the length/error to be passed back to the user.

But in a lot of cases the syscall wrapper can do the buffer copies (as well as the length copies).
That would be restricted to short length (on stack).
So code that needed a long buffer (like some of the sctp options) would need to directly access the user buffer (or a long buffer provided by an in-kernel user).

But you'll find code that reads/writes well beyond the apparent size of the user buffer.
(And not just code that accesses 4 bytes without checking the length).

David

#26 lore raw
Quoted 209 lines · David Laight
Quoted 200 lines · On Wed, 2 Apr 2025 14:21:35 -0700 Stanislav Fomichev <[email protected]>…
Quoted 143 lines · David Laight
Quoted 140 lines · On Wed, 2 Apr 2025 07:19:46 -0700 Stanislav Fomichev <[email protected]>…
Quoted 139 lines · David Laight
Quoted 135 lines · On Wed, 2 Apr 2025 00:53:58 +0200 Stefan Metzmacher <[email protected]> wrote:

Am 02.04.25 um 00:04 schrieb Stanislav Fomichev:

Quoted 115 lines · Stefan Metzmacher
Quoted 114 lines

Am 01.04.25 um 17:45 schrieb Stanislav Fomichev:

Quoted 97 lines · Breno Leitao
Quoted 88 lines · Stefan Metzmacher
Quoted 87 lines

Am 01.04.25 um 15:37 schrieb Stefan Metzmacher:

Quoted 76 lines

Am 01.04.25 um 10:19 schrieb Stefan Metzmacher:

Quoted 69 lines

Am 31.03.25 um 23:04 schrieb Stanislav Fomichev:
On 03/31, Stefan Metzmacher 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 as optlen to do_sock_getsockopt()
and can't reach the ops->getsockopt() path.

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.

@Linus, would that optlen_t approach fit better for you?

[..]

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().

At this point why not just fully embrace iov_iter? You have the size
now + the user (or kernel) pointer. Might as well do
s/sockptr_t/iov_iter/ conversion?

I think that would only be possible if we introduce
proto[_ops].getsockopt_iter() and then convert the implementations
step by step. Doing it all in one go has a lot of potential to break
the uapi. I could try to convert things like socket, ip and tcp myself, but
the rest needs to be converted by the maintainer of the specific protocol,
as it needs to be tested. As there are crazy things happening in the existing
implementations, e.g. some getsockopt() implementations use optval as in and out
buffer.

I first tried to convert both optval and optlen of getsockopt to sockptr_t,
and that showed that touching the optval part starts to get complex very soon,
see https://git.samba.org/?p=metze/linux/wip.git;a=commitdiff;h=141912166473bf8843ec6ace76dc9c6945adafd1
(note it didn't converted everything, I gave up after hitting
sctp_getsockopt_peer_addrs and sctp_getsockopt_local_addrs.
sctp_getsockopt_context, sctp_getsockopt_maxseg, sctp_getsockopt_associnfo and maybe
more are the ones also doing both copy_from_user and copy_to_user on optval)

I come also across one implementation that returned -ERANGE because *optlen was
too short and put the required length into *optlen, which means the returned
*optlen is larger than the optval buffer given from userspace.

Because of all these strange things I tried to do a minimal change
in order to get rid of the io_uring limitation and only converted
optlen and leave optval as is.

In order to have a patchset that has a low risk to cause regressions.

But as alternative introducing a prototype like this:

         int (*getsockopt_iter)(struct socket *sock, int level, int optname,
                                struct iov_iter *optval_iter);

That returns a non-negative value which can be placed into *optlen
or negative value as error and *optlen will not be changed on error.
optval_iter will get direction ITER_DEST, so it can only be written to.

Implementations could then opt in for the new interface and
allow do_sock_getsockopt() work also for the io_uring case,
while all others would still get -EOPNOTSUPP.

So what should be the way to go?

Ok, I've added the infrastructure for getsockopt_iter, see below, but the first part I wanted to convert was tcp_ao_copy_mkts_to_user() and that also reads from userspace before writing.

Quoted 5 lines

So we could go with the optlen_t approach, or we need logic for ITER_BOTH or pass two iov_iters one with ITER_SRC and one with ITER_DEST...

So who wants to decide?

I just noticed that it's even possible in same cases
to pass in a short buffer to optval, but have a longer value in optlen, hci_sock_getsockopt() with SOL_BLUETOOTH completely ignores optlen.

Quoted 7 lines
Quoted 2 lines

This makes it really hard to believe that trying to use iov_iter for this is a good idea :-(

That was my finding as well a while ago, when I was planning to get the __user pointers converted to iov_iter. There are some weird ways of using optlen and optval, which makes them non-trivial to covert to iov_iter.

Quoted 4 lines

Can we ignore all non-ip/tcp/udp cases for now? This should cover +90% of useful socket opts. See if there are any obvious problems with them and if not, try converting. The rest we can cover separately when/if needed.

That's what I tried, but it fails with
tcp_getsockopt ->

    do_tcp_getsockopt ->
      tcp_ao_get_mkts ->
         tcp_ao_copy_mkts_to_user ->
            copy_struct_from_sockptr
      tcp_ao_get_sock_info ->
         copy_struct_from_sockptr

That's not possible with a ITER_DEST iov_iter.

Quoted 7 lines
Quoted 1 line

metze

Can we create two iterators over the same memory? One for ITER_SOURCE and another for ITER_DEST. And then make getsockopt_iter accept optval_in and optval_out. We can also use optval_out position (iov_offset) as optlen output value. Don't see why it won't work, but I agree that's gonna be a messy conversion so let's see if someone else has better suggestions.

Yes, that might work, but it would be good to get some feedback if this would be the way to go:

int (*getsockopt_iter)(struct socket *sock,
int level, int optname, struct iov_iter *optval_in, struct iov_iter *optval_out);

And *optlen = optval_out->iov_offset;

Any objection or better ideas? Linus would that be what you had in mind?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I'd worry about performance - yes I know 'iter' are used elsewhere but... Also look at the SCTP code.

Performance usually does not matter for set/getsockopts, there are a few exceptions that I know (TCP_ZEROCOPY_RECEIVE)

That might be the one that is really horrid and completely abuses the 'length' parameter.

It is reading and writing, yes, but it's not a huge problem. And it does enforce the optlen (to copy back the same amount of bytes). It's not that bad, it's just an example of where we need to be extra careful.

Quoted 13 lines
Quoted 13 lines

and maybe recent
devmem sockopts; we can special-case these if needed, or keep sockptr_t, idk. I'm skeptical we can convert everything though, that's why the suggestion to start with sk/ip/tcp/udp.

Quoted 7 lines

How do you handle code that wants to return an updated length (often longer than the one provided) and an error code (eg ERRSIZE or similar).

There is also a very strange use (I think it is a sockopt rather than an ioctl) where the buffer length the application provides is only that of the header. The actual buffer length is contained in the header.
The return length is the amount written into the full buffer.

Quoted 6 lines
Quoted 4 lines

Let's discuss these special cases as they come up? Worst case these places can always re-init iov_iter with a comment on why it is ok. But I do agree in general that there are a few places that do wild stuff.

The problem is that the generic code has to deal with all the 'wild stuff'.

getsockopt_iter will have optval_in for the minority of socket options (like TCP_ZEROCOPY_RECEIVE) that want to read user's value as well as optval_out. The latter is what the majority of socket options will use to write their value. That doesn't seem too complicated to handle?

Quoted 2 lines

It is also common to do non-sequential accesses - so iov_iter doesn't match at all.

I disagree that it's 'common'. Searching for copy_from_sockptr_offset returns a few cases and they are mostly using read-with-offset because there is no sequential read (iterator) semantics with sockptr_t.

Quoted 8 lines

There also isn't a requirement for scatter-gather.

For 'normal' getsockopt (and setsockopt) with short lengths it actually makes sense for the syscall wrapper to do the user copies.
But it would need to pass the user ptr+len as well as the kernel ptr+len to give the required flexibilty.
Then you have to work out whether the final copy to user is needed or not. (not that hard, but it all adds complication).

Quoted 5 lines

Not sure I understand what's the problem. The user vs kernel part will be abstracted by iov_iter. The callers will have to write the optlen back. And there are two call sites we care about: io_uring and regular system call. What's your suggestion? Maybe I'm missing something. Do you prefer get_optlen/put_optlen?

Quoted 3 lines

I think the final aim should be to pass the user supplied length to the per-protocol code and have it return the length/error to be passed back to the user.

Like what Stefan's patch 3 is doing? Or you're suggesting to change getsockopt handlers to handle length more explicitly? If we were to proceed with sockptr to iov_iter conversion we'll have to do it anyway (or pass the length as the size of iov_iter).

Quoted 6 lines

But in a lot of cases the syscall wrapper can do the buffer copies (as well as the length copies).
That would be restricted to short length (on stack).
So code that needed a long buffer (like some of the sctp options) would need to directly access the user buffer (or a long buffer provided by an in-kernel user).

This sounds similar to what we did with bpf hooks - copy (head of) the buffer and run bpf program on top of it. I remember iptables setsockopt begin problematic because of its huge size.. It is an option, yes (to convert protocol handler to kernel memory mostly).

Quoted 3 lines

But you'll find code that reads/writes well beyond the apparent size of the user buffer.
(And not just code that accesses 4 bytes without checking the length).

With can start with getsockopt_iter + sk_getsockopt to see if there are any issues with that approach. If not, adding ip/tcp/udp to the mix should be doable. We can explain and comment on special cases if needed. When other protocols are needed from io_uring, we can convert more. But at least the new code will use the correct abstractions.