Skip to contentSkip to conversation

net: call getsockopt_iter if available

PATCH Applied Emanuele Ghidoli 4 participants 4 messages Oct 22 – Oct 26

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

PATCH 1/1 net: call getsockopt_iter if available

From: Breno Leitao <[email protected]>

commit 5bd0dec150f56b6307d599132dcb7c01007bbecc upstream.

Update do_sock_getsockopt() to use the new getsockopt_iter callback when available. Add do_sock_getsockopt_iter() helper that:

1. Reads optlen from user/kernel space
2. Initializes a sockopt_t with the appropriate iov_iter (kvec for kernel, ubuf for user buffers) and sets opt.optlen
3. Calls the protocol's getsockopt_iter callback
4. Writes opt.optlen back to user/kernel space

The optlen is always written back, even on failure. Some protocols (e.g. CAN raw) return -ERANGE and set optlen to the required buffer size so userspace knows how much to allocate.

The callback is responsible for setting opt.optlen to indicate the returned data size.

Important to say that iov_out does not need to be copied back in do_sock_getsockopt().

When optval is not kernel (the userspace path), sockptr_to_sockopt() sets up opt->iter_out as a ITER_DEST ubuf iterator pointing directly at the userspace buffer (optval.user). So when getsockopt_iter implementations call copy_to_iter(..., &opt->iter_out), the data is written directly to userspace — no intermediate kernel buffer is involved.

When optval.is_kernel is true (the in-kernel path, e.g. from io_uring), the kvec points at the already-provided kernel buffer (optval.kernel), so the data lands in the caller's buffer directly via the kvec-backed iterator.

In both cases the iterator writes to the final destination in-place at protocol callback. There's nothing to copy back — only optlen needs to be written back.

---
Clean cherry-pick of the upstream commit, no changes. Needed in 6.12.y only.

v6.12.114 picked up commit 41fbf726915b ("net: add getsockopt_iter callback to proto_ops", upstream 67fab22a7adc) and commit 22ebe6ab0b67 ("Bluetooth: L2CAP: convert to getsockopt_iter", upstream bf01627cc04b) as dependencies of commit 3af72bf8f73c ("Bluetooth: L2CAP: access chan->conn safely in get/setsockopt", upstream ca2c4c264986), but not this patch, which is the one making do_sock_getsockopt() actually call ->getsockopt_iter().

Since l2cap_sock_ops no longer provides ->getsockopt, every getsockopt() on an L2CAP socket at SOL_L2CAP/SOL_BLUETOOTH level fails with -EOPNOTSUPP on v6.12.114. As a result bluetoothd never registers the adapter and bluetoothctl reports "No default controller available":
BlueZ's btio calls getsockopt(L2CAP_OPTIONS) while bluetoothd sets up the BR/EDR ATT listening socket of the GATT database, and the adapter is dropped when that fails. Our CI sees this on every Bluetooth-enabled board running v6.12.114 (i.MX 8M Plus and AM62 based, BlueZ 5.72), while v6.12.113 was fine. The same regression was reported with an Intel AX201 on Debian 13:
https://bugzilla.kernel.org/show_bug.cgi?id=222177

L2CAP is the only user of ->getsockopt_iter in 6.12.y. The other stable trees either did not get these backports or already contain the upstream commit. The patch applies cleanly on v6.12.114.

Tested on a Verdin iMX8M Plus with our v6.12.114 based kernel plus this patch.

The root cause analysis (comparing CI logs, reviewing the v6.12.113..v6.12.114 changes and reading the BlueZ code) was done with the help of an AI coding assistant (Claude); I reviewed the analysis and the backport.

 net/socket.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 51 insertions(+), 3 deletions(-)
net/socket.c +51 -3
index 723bc3a1ba5c..b8d662e19310 100644
@@ -77,6 +77,7 @@
 #include <linux/mount.h>
 #include <linux/pseudo_fs.h>
 #include <linux/security.h>
+#include <linux/uio.h>
 #include <linux/syscalls.h>
 #include <linux/compat.h>
 #include <linux/kmod.h>
@@ -2345,11 +2346,45 @@ SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
 INDIRECT_CALLABLE_DECLARE(bool tcp_bpf_bypass_getsockopt(int level,
 							 int optname));
 
+/*
+ * Initialize a sockopt_t from sockptr optval/optlen, setting up iov_iter
+ * for both input and output directions.
+ * It is important to remember that both iov points to the same data, but,
+ * .iter_in is read-only and .iter_out is write-only by the protocol callbacks
+ */
+static int sockptr_to_sockopt(sockopt_t *opt, sockptr_t optval,
+			      sockptr_t optlen, struct kvec *kvec)
+{
+	int koptlen;
+
+	if (copy_from_sockptr(&koptlen, optlen, sizeof(int)))
+		return -EFAULT;
+
+	if (koptlen < 0)
+		return -EINVAL;
+
+	if (optval.is_kernel) {
+		kvec->iov_base = optval.kernel;
+		kvec->iov_len = koptlen;
+		iov_iter_kvec(&opt->iter_out, ITER_DEST, kvec, 1, koptlen);
+		iov_iter_kvec(&opt->iter_in, ITER_SOURCE, kvec, 1, koptlen);
+	} else {
+		iov_iter_ubuf(&opt->iter_out, ITER_DEST, optval.user, koptlen);
+		iov_iter_ubuf(&opt->iter_in, ITER_SOURCE, optval.user,
+			      koptlen);
+	}
+	opt->optlen = koptlen;
+
+	return 0;
+}
+
 int do_sock_getsockopt(struct socket *sock, bool compat, int level,
 		       int optname, sockptr_t optval, sockptr_t optlen)
 {
 	int max_optlen __maybe_unused = 0;
 	const struct proto_ops *ops;
+	struct kvec kvec;
+	sockopt_t opt;
 	int err;
 
 	err = security_socket_getsockopt(sock, level, optname);
@@ -2362,15 +2397,28 @@ int do_sock_getsockopt(struct socket *sock, bool compat, int level,
 	ops = READ_ONCE(sock->ops);
 	if (level == SOL_SOCKET) {
 		err = sk_getsockopt(sock->sk, level, optname, optval, optlen);
-	} else if (unlikely(!ops->getsockopt)) {
-		err = -EOPNOTSUPP;
-	} else {
+	} else if (ops->getsockopt_iter) {
+		err = sockptr_to_sockopt(&opt, optval, optlen, &kvec);
+		if (err)
+			return err;
+
+		err = ops->getsockopt_iter(sock, level, optname, &opt);
+
+		/* Always write back optlen, even on failure. Some protocols
+		 * (e.g. CAN raw) return -ERANGE and set optlen to the
+		 * required buffer size so userspace can discover it.
+		 */
+		if (copy_to_sockptr(optlen, &opt.optlen, sizeof(int)))
+			return -EFAULT;
+	} else if (ops->getsockopt) {
 		if (WARN_ONCE(optval.is_kernel || optlen.is_kernel,
 			      "Invalid argument type"))
 			return -EOPNOTSUPP;
 
 		err = ops->getsockopt(sock, level, optname, optval.user,
 				      optlen.user);
+	} else {
+		err = -EOPNOTSUPP;
 	}
 
 	if (!compat)

base-commit: 85d82e0d7c2dfd3e75d34f35b7ebbf41f45c9c4e

Signature · 1 line
2.53.0
#1 lore raw

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: net/socket.c:77
error: net/socket.c: patch does not apply
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"

Please resolve the issue and submit the patches again.

---
Regards,
Linux Bluetooth

maintainer Applied
#2 lore raw
In reply to Emanuele Ghidoli
Quoted 2 lines

Update do_sock_getsockopt() to use the new getsockopt_iter callback when available. Add do_sock_getsockopt_iter() helper that:

Queued for 6.12, thanks.

Signature · 2 lines
Thanks,
Sasha
#3 lore raw
In reply to Emanuele Ghidoli
Quoted 8 lines · Emanuele Ghidoli

v6.12.114 picked up commit 41fbf726915b ("net: add getsockopt_iter callback to proto_ops", upstream 67fab22a7adc) and commit 22ebe6ab0b67 ("Bluetooth: L2CAP: convert to getsockopt_iter", upstream bf01627cc04b) as dependencies of commit 3af72bf8f73c ("Bluetooth: L2CAP: access chan->conn safely in get/setsockopt", upstream ca2c4c264986), but not this patch, which is the one making do_sock_getsockopt() actually call ->getsockopt_iter().

Right, if you got the protocol's conversion to getsockopt iterator, you need this one as well.