Skip to contentSkip to conversation

ipv4,ipv6: convert the getsockopt switches to sockopt_t

PATCH Applied Breno Leitao 5 participants 22 messages Nov 3 – Nov 6

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

v4 cover ipv4,ipv6: convert the getsockopt switches to sockopt_t

This series continues the migration to sockopt_t, as described in [1]. do_ip_getsockopt() and do_ipv6_getsockopt() still take a sockptr_t pair, and everything they forward to now takes a sockopt_t, so convert them both.

Patch 1 settles what a negative optlen means on SOL_IPV6 before the conversion makes it matter. do_ip_getsockopt() rejects it, but do_ipv6_getsockopt() never has, and the int options answer it as 4. Treat it as 4, as David Laight suggested, instead of failing callers that rely on it.

Patch 2 fixes a latent bug in sockopt_expand_out() [2] itself: it caps the expansion at INT_MAX, but iov_iter_revert() refuses to unroll past the smaller MAX_RW_COUNT.

MCAST_MSFILTER has to move before the switches above it. It is the last option with the quirk sockopt_expand_out() was added for [2]: optlen only covers the fixed part, and the real reply size comes from the gf_numsrc field inside it. Patches 3 and 4 convert it on both families, and store the reply length only after the final copy succeeds, so a copy failure can't report a changed optlen.

Patches 5 and 6 convert the switches themselves. Each wrapper keeps its __user prototype and builds the sockopt_t, so the proto layer is untouched. ipv6_getsockopt() cannot use sockopt_init_user(), which rejects a negative optlen, so patch 6 moves the clamp of patch 1 up there and builds the sockopt_t after it.

Patch 7 grows the selftest with an ip and an ipv6 fixture, pinning the returned length, errno and reply bytes.

The temporary sockptr_t-to-sockopt_t glue can go away once the higher level callers take sockopt_t too. For now, the series keeps the proto callbacks unchanged and converts only the leaf helpers.

To: David Ahern <[email protected]>
To: Ido Schimmel <[email protected]>
To: David S. Miller <[email protected]>
To: Eric Dumazet <[email protected]>
To: Jakub Kicinski <[email protected]>
To: Paolo Abeni <[email protected]>
To: Simon Horman <[email protected]>
To: Alexei Starovoitov <[email protected]>
To: Daniel Borkmann <[email protected]>
To: Andrii Nakryiko <[email protected]>
To: Eduard Zingerman <[email protected]>
To: Kumar Kartikeya Dwivedi <[email protected]>
To: Martin KaFai Lau <[email protected]>
To: Song Liu <[email protected]>
To: Yonghong Song <[email protected]>
To: Jiri Olsa <[email protected]>
To: Emil Tsalapatis <[email protected]>
To: Ihor Solodrai <[email protected]>
To: John Fastabend <[email protected]>
To: Stanislav Fomichev <[email protected]>
To: Shuah Khan <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
To: Stanislav Fomichev <[email protected]>
Cc: [email protected]

---
Changes in v4:
- Selftest: add CONFIG_IP_MULTICAST=y, which CONFIG_IP_MROUTE depends on,
per Paolo Abeni
- Collect Acked-by from Ido Schimmel
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Fix sashiko findings:
- MRT6_* keeps rejecting a negative optlen instead of answering 4
- Selftest: add CONFIG_IP_MROUTE=y and an IPV6_PATHMTU negative optlen
test
- Fixing random commit message issues reported by sashiko. Trying to
get sashiko happy.
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Treat a negative optlen as 4 instead of rejecting it with
-EINVAL as v1 did, per David Laight and Stanislav Fomichev.
- Fix sashiko findings
- Link to v1: https://patch.msgid.link/[email protected]

To: David Ahern <[email protected]>
To: Ido Schimmel <[email protected]>
To: "David S. Miller" <[email protected]>
To: Eric Dumazet <[email protected]>
To: Jakub Kicinski <[email protected]>
To: Paolo Abeni <[email protected]>
To: Simon Horman <[email protected]>
To: Daniel Borkmann <[email protected]>
To: John Fastabend <[email protected]>
To: Stanislav Fomichev <[email protected]>
To: Martin KaFai Lau <[email protected]>
To: Alexei Starovoitov <[email protected]>
To: Andrii Nakryiko <[email protected]>
To: Eduard Zingerman <[email protected]>
To: Kumar Kartikeya Dwivedi <[email protected]>
To: Song Liu <[email protected]>
To: Yonghong Song <[email protected]>
To: Jiri Olsa <[email protected]>
To: Emil Tsalapatis <[email protected]>
To: Ihor Solodrai <[email protected]>
To: Shuah Khan <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
Breno Leitao (7):
ipv6: treat a negative optlen as 4 in do_ipv6_getsockopt()
net: cap sockopt_expand_out() at MAX_RW_COUNT
ipv6: mcast: convert ip6_mc_msfget() to sockopt_t
ipv4: igmp: convert ip_mc_gsfget() to sockopt_t
ipv4: convert do_ip_getsockopt() to sockopt_t
ipv6: convert do_ipv6_getsockopt() to sockopt_t
selftests: net: getsockopt_iter: cover ip and ipv6

 include/linux/igmp.h                          |   2 +-
 include/linux/mroute.h                        |   4 +-
 include/linux/mroute6.h                       |   5 +-
 include/linux/net.h                           |  30 ++-
 include/net/ip.h                              |   3 +-
 include/net/ipv6.h                            |   4 +-
 net/core/filter.c                             |  36 ++-
 net/ipv4/igmp.c                               |  23 +-
 net/ipv4/ip_sockglue.c                        | 118 ++++----
 net/ipv4/ipmr.c                               |  11 +-
 net/ipv6/ip6mr.c                              |  13 +-
 net/ipv6/ipv6_sockglue.c                      | 120 +++++----
 net/ipv6/mcast.c                              |  19 +-
 tools/testing/selftests/net/config            |   2 +
 tools/testing/selftests/net/getsockopt_iter.c | 372 ++++++++++++++++++++++++++
 15 files changed, 597 insertions(+), 165 deletions(-)

---
base-commit: 2637bdec0535a2044cd830eba9a79363651d9043
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master
change-id: 20260924-sockopt_expand_out_v2-ee4bc7ba4cea

Best regards,

Signature · 1 line
Breno Leitao <[email protected]>
#1 lore raw

PATCH v4 1/7 ipv6: treat a negative optlen as 4 in do_ipv6_getsockopt()

do_ipv6_getsockopt() reads optlen into an int and never checks its sign. Most of what consumes len compares it as unsigned, so a negative optlen reads as a huge buffer, and the reply depends on the option.

The int options clamp it to their own size:

	len = min_t(unsigned int, sizeof(int), len);

so getsockopt(fd, SOL_IPV6, IPV6_TCLASS, buf, &len) with len set to -1 answers 4 bytes and reports 4, rather than failing.

The other options see a huge buffer. The sticky options reply with their whole header, IPV6_2292PKTOPTIONS writes every pending control message without a limit, and IPV6_PATHMTU and IPV6_FLOWLABEL_MGR pass their length check.

do_ip_getsockopt() rejects a negative optlen, but doing that here would turn a call that works today into an error for a caller that passes an uninitialized length. Answer it as 4, as the int options do, for every option this function handles itself.

That leaves the int options as they are. The sticky headers are cut to 4 bytes, IPV6_2292PKTOPTIONS gets a 4 byte control buffer, and IPV6_PATHMTU and IPV6_FLOWLABEL_MGR fail with -EINVAL, as they do for any other undersized buffer.

MRT6_* is dispatched earlier and keeps rejecting a negative optlen, and the netfilter fallback still sees the caller's value.

This changes what callers that pass a negative length see. It is a behaviour change, not a fix, so there is no Fixes: tag.

The conversion of this function to sockopt_t builds its buffers from optlen, which the standard helpers reject when negative, so the answer has to be settled before that.

---
 net/ipv6/ipv6_sockglue.c | 5 +++++
 1 file changed, 5 insertions(+)
net/ipv6/ipv6_sockglue.c +5 -0
index 4b3536571c9804..700133e2fd11c2 100644
@@ -1002,6 +1002,11 @@ int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
 
 	if (copy_from_sockptr(&len, optlen, sizeof(int)))
 		return -EFAULT;
+	/* Historic bug compatibility: take a negative optlen as 4, as the
+	 * int options always did.
+	 */
+	if (len < 0)
+		len = 4;
 	switch (optname) {
 	case MCAST_MSFILTER:
 		if (in_compat_syscall())
Signature · 1 line
2.53.0-Meta
Reviewed-by on v4 1/7
#2 lore raw

Re: [PATCH net-next v4 1/7] ipv6: treat a negative optlen as 4 in do_ipv6_getsockopt()

Quoted 36 lines · Breno Leitao

do_ipv6_getsockopt() reads optlen into an int and never checks its sign. Most of what consumes len compares it as unsigned, so a negative optlen reads as a huge buffer, and the reply depends on the option.

The int options clamp it to their own size:

	len = min_t(unsigned int, sizeof(int), len);

so getsockopt(fd, SOL_IPV6, IPV6_TCLASS, buf, &len) with len set to -1 answers 4 bytes and reports 4, rather than failing.

The other options see a huge buffer. The sticky options reply with their whole header, IPV6_2292PKTOPTIONS writes every pending control message without a limit, and IPV6_PATHMTU and IPV6_FLOWLABEL_MGR pass their length check.

do_ip_getsockopt() rejects a negative optlen, but doing that here would turn a call that works today into an error for a caller that passes an uninitialized length. Answer it as 4, as the int options do, for every option this function handles itself.

That leaves the int options as they are. The sticky headers are cut to 4 bytes, IPV6_2292PKTOPTIONS gets a 4 byte control buffer, and IPV6_PATHMTU and IPV6_FLOWLABEL_MGR fail with -EINVAL, as they do for any other undersized buffer.

MRT6_* is dispatched earlier and keeps rejecting a negative optlen, and the netfilter fallback still sees the caller's value.

This changes what callers that pass a negative length see. It is a behaviour change, not a fix, so there is no Fixes: tag.

The conversion of this function to sockopt_t builds its buffers from optlen, which the standard helpers reject when negative, so the answer has to be settled before that.

Quoted 8 lines
---
 net/ipv6/ipv6_sockglue.c | 5 +++++
 1 file changed, 5 insertions(+)
Reviewed-by on v4 1/7
#3 lore raw

Re: [PATCH net-next v4 1/7] ipv6: treat a negative optlen as 4 in do_ipv6_getsockopt()

In reply to Breno Leitao
Quoted 32 lines · Breno Leitao

do_ipv6_getsockopt() reads optlen into an int and never checks its sign. Most of what consumes len compares it as unsigned, so a negative optlen reads as a huge buffer, and the reply depends on the option.

The int options clamp it to their own size:

	len = min_t(unsigned int, sizeof(int), len);

so getsockopt(fd, SOL_IPV6, IPV6_TCLASS, buf, &len) with len set to -1 answers 4 bytes and reports 4, rather than failing.

The other options see a huge buffer. The sticky options reply with their whole header, IPV6_2292PKTOPTIONS writes every pending control message without a limit, and IPV6_PATHMTU and IPV6_FLOWLABEL_MGR pass their length check.

do_ip_getsockopt() rejects a negative optlen, but doing that here would turn a call that works today into an error for a caller that passes an uninitialized length. Answer it as 4, as the int options do, for every option this function handles itself.

That leaves the int options as they are. The sticky headers are cut to 4 bytes, IPV6_2292PKTOPTIONS gets a 4 byte control buffer, and IPV6_PATHMTU and IPV6_FLOWLABEL_MGR fail with -EINVAL, as they do for any other undersized buffer.

MRT6_* is dispatched earlier and keeps rejecting a negative optlen, and the netfilter fallback still sees the caller's value.

This changes what callers that pass a negative length see. It is a behaviour change, not a fix, so there is no Fixes: tag.

Quoted 8 lines

The conversion of this function to sockopt_t builds its buffers from optlen, which the standard helpers reject when negative, so the answer has to be settled before that.

Quoted 21 lines
---
 net/ipv6/ipv6_sockglue.c | 5 +++++
 1 file changed, 5 insertions(+)
net/ipv6/ipv6_sockglue.c +5 -0
index 4b3536571c9804..700133e2fd11c2 100644
@@ -1002,6 +1002,11 @@ int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
 
 	if (copy_from_sockptr(&len, optlen, sizeof(int)))
 		return -EFAULT;
+	/* Historic bug compatibility: take a negative optlen as 4, as the
+	 * int options always did.
+	 */
+	if (len < 0)
+		len = 4;
 	switch (optname) {
 	case MCAST_MSFILTER:
 		if (in_compat_syscall())
#4 lore raw

PATCH v4 2/7 net: cap sockopt_expand_out() at MAX_RW_COUNT

In reply to Breno Leitao

sockopt_expand_out() grows the output iterator up to INT_MAX, but the MCAST_MSFILTER getters converted by the next two patches rewind it with iov_iter_revert() once they know the real reply size. iov_iter_revert() refuses to unroll past MAX_RW_COUNT:

	if (WARN_ON(unroll > MAX_RW_COUNT))
		return;

MAX_RW_COUNT is INT_MAX rounded down to a page boundary, so a caller that expands into the (MAX_RW_COUNT, INT_MAX] range and then reverts hits that WARN_ON, and the iterator is left where it was instead of rewound. So does a caller whose optlen already covers such a size.

Cap the size at MAX_RW_COUNT instead, before the early return for an optlen that already covers it, so a size that could never be reverted later is rejected up front.

Sashiko found this while reviewing the conversions.

---
 include/linux/net.h | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
include/linux/net.h +9 -4
index e2a866fbcfa5ea..de56f89d475ee2 100644
@@ -80,16 +80,21 @@ static inline int sockopt_init_user(sockopt_t *opt, char __user *optval,
  *
  * Call it before writing through opt->iter_out: it re-anchors the iterator at
  * the head of optval. Only a user buffer can be longer than the optlen the
- * caller declared, so a kernel-backed optval is refused with -EINVAL.
+ * caller declared, so a kernel-backed optval is refused with -EINVAL. So is a
+ * @size above MAX_RW_COUNT, which iov_iter_revert() could not unroll.
  */
 static inline int sockopt_expand_out(sockopt_t *opt, size_t size)
 {
+	/* iov_iter_revert() refuses to unroll past MAX_RW_COUNT, so a size
+	 * beyond that could never be reverted back to the fixed part later,
+	 * even when optlen already covers it.
+	 */
+	if (size > MAX_RW_COUNT)
+		return -EINVAL;
+
 	if (size <= (size_t)opt->optlen)
 		return 0;
 
-	if (size > INT_MAX)
-		return -EINVAL;
-
 	/* Re-anchoring reads iter_out.ubuf, so the iterator has to be a user
 	 * buffer that nothing has written through yet.
 	 */
Signature · 1 line
2.53.0-Meta
Acked-by on v4 2/7
#5 lore raw

Re: [PATCH net-next v4 2/7] net: cap sockopt_expand_out() at MAX_RW_COUNT

Quoted 13 lines · Breno Leitao

sockopt_expand_out() grows the output iterator up to INT_MAX, but the MCAST_MSFILTER getters converted by the next two patches rewind it with iov_iter_revert() once they know the real reply size. iov_iter_revert() refuses to unroll past MAX_RW_COUNT:

	if (WARN_ON(unroll > MAX_RW_COUNT))
		return;

MAX_RW_COUNT is INT_MAX rounded down to a page boundary, so a caller that expands into the (MAX_RW_COUNT, INT_MAX] range and then reverts hits that WARN_ON, and the iterator is left where it was instead of rewound. So does a caller whose optlen already covers such a size.

Quoted 8 lines

Cap the size at MAX_RW_COUNT instead, before the early return for an optlen that already covers it, so a size that could never be reverted later is rejected up front.

Sashiko found this while reviewing the conversions.

Reviewed-by on v4 2/7
#6 lore raw

Re: [PATCH net-next v4 2/7] net: cap sockopt_expand_out() at MAX_RW_COUNT

In reply to Breno Leitao
Quoted 17 lines · Breno Leitao

sockopt_expand_out() grows the output iterator up to INT_MAX, but the MCAST_MSFILTER getters converted by the next two patches rewind it with iov_iter_revert() once they know the real reply size. iov_iter_revert() refuses to unroll past MAX_RW_COUNT:

	if (WARN_ON(unroll > MAX_RW_COUNT))
		return;

MAX_RW_COUNT is INT_MAX rounded down to a page boundary, so a caller that expands into the (MAX_RW_COUNT, INT_MAX] range and then reverts hits that WARN_ON, and the iterator is left where it was instead of rewound. So does a caller whose optlen already covers such a size.

Cap the size at MAX_RW_COUNT instead, before the early return for an optlen that already covers it, so a size that could never be reverted later is rejected up front.

Quoted 8 lines

Sashiko found this while reviewing the conversions.

---
 include/linux/net.h | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
Reviewed-by on v4 2/7
#7 lore raw

Re: [PATCH net-next v4 2/7] net: cap sockopt_expand_out() at MAX_RW_COUNT

In reply to Breno Leitao
Quoted 13 lines · Breno Leitao

sockopt_expand_out() grows the output iterator up to INT_MAX, but the MCAST_MSFILTER getters converted by the next two patches rewind it with iov_iter_revert() once they know the real reply size. iov_iter_revert() refuses to unroll past MAX_RW_COUNT:

	if (WARN_ON(unroll > MAX_RW_COUNT))
		return;

MAX_RW_COUNT is INT_MAX rounded down to a page boundary, so a caller that expands into the (MAX_RW_COUNT, INT_MAX] range and then reverts hits that WARN_ON, and the iterator is left where it was instead of rewound. So does a caller whose optlen already covers such a size.

Quoted 8 lines

Cap the size at MAX_RW_COUNT instead, before the early return for an optlen that already covers it, so a size that could never be reverted later is rejected up front.

Sashiko found this while reviewing the conversions.

Quoted 35 lines
---
 include/linux/net.h | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
include/linux/net.h +9 -4
index e2a866fbcfa5ea..de56f89d475ee2 100644
@@ -80,16 +80,21 @@ static inline int sockopt_init_user(sockopt_t *opt, char __user *optval,
  *
  * Call it before writing through opt->iter_out: it re-anchors the iterator at
  * the head of optval. Only a user buffer can be longer than the optlen the
- * caller declared, so a kernel-backed optval is refused with -EINVAL.
+ * caller declared, so a kernel-backed optval is refused with -EINVAL. So is a
+ * @size above MAX_RW_COUNT, which iov_iter_revert() could not unroll.
  */
 static inline int sockopt_expand_out(sockopt_t *opt, size_t size)
 {
+	/* iov_iter_revert() refuses to unroll past MAX_RW_COUNT, so a size
+	 * beyond that could never be reverted back to the fixed part later,
+	 * even when optlen already covers it.
+	 */
+	if (size > MAX_RW_COUNT)
+		return -EINVAL;
+
 	if (size <= (size_t)opt->optlen)
 		return 0;
 
-	if (size > INT_MAX)
-		return -EINVAL;
-
 	/* Re-anchoring reads iter_out.ubuf, so the iterator has to be a user
 	 * buffer that nothing has written through yet.
 	 */
#8 lore raw

PATCH v4 3/7 ipv6: mcast: convert ip6_mc_msfget() to sockopt_t

In reply to Breno Leitao

MCAST_MSFILTER reads its reply through ip6_mc_msfget(), reached from do_ipv6_getsockopt() and from nowhere else. Convert it, and build the sockopt_t at the call site for as long as the caller still carries a sockptr_t pair.

optlen only has to cover the fixed part, and the real reply size comes from the gf_numsrc field inside it. Userspace relies on that, so sockopt_expand_out() grows optval past optlen, for a user address only and only far enough for the sources the socket has.

ip6_mc_msfget() now advances over the fixed part and writes the source list through iter_out. Its callers rewind by the reply length they already compute, which is exactly what the callee consumed, and land back where they used to write: offset 0 for the native reply, gf_fmode for the compat one.

ipv6_get_msfilter() and compat_ipv6_get_msfilter() only store *optlen after their own copy succeeds, so a copy failure can't report a changed length: -EINVAL, -EADDRNOTAVAIL and -EFAULT all leave the caller's optlen word unchanged.

The old code stored *optlen before writing the fixed part, so an unwritable optlen word stopped the fixed part from being written. Now the fixed part is written first.

---
 include/net/ipv6.h       |  2 +-
 net/ipv6/ipv6_sockglue.c | 64 ++++++++++++++++++++++++++++++++----------------
 net/ipv6/mcast.c         | 19 +++++++++++---
 3 files changed, 59 insertions(+), 26 deletions(-)
include/net/ipv6.h +1 -1
index 3de07e738538f7..9bb68d75890364 100644
@@ -1195,7 +1195,7 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf,
 		  struct sockaddr_storage *list);
 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
-		  sockptr_t optval, size_t ss_offset);
+		  sockopt_t *opt, size_t ss_offset);
 
 #ifdef CONFIG_PROC_FS
 int ac6_proc_init(struct net *net);
net/ipv6/ipv6_sockglue.c +43 -21

119 lines not on this page · raw message

net/ipv6/mcast.c +15 -4
index ecef55f261890c..4ca2d77811f4eb 100644
@@ -600,14 +600,14 @@ int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf,
 }
 
 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
-		  sockptr_t optval, size_t ss_offset)
+		  sockopt_t *opt, size_t ss_offset)
 {
 	struct ipv6_pinfo *inet6 = inet6_sk(sk);
 	const struct in6_addr *group;
 	struct ipv6_mc_socklist *pmc;
 	struct ip6_sf_socklist *psl;
+	int i, copycount, err;
 	unsigned int count;
-	int i, copycount;
 
 	group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
 
@@ -629,6 +629,18 @@ int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
 
 	copycount = min(count, gsf->gf_numsrc);
 	gsf->gf_numsrc = count;
+
+	/* The source list is sized by the gf_numsrc the caller left in optval,
+	 * not by optlen, which only has to cover the fixed part.
+	 */
+	err = sockopt_expand_out(opt, ss_offset +
+				 copycount * sizeof(struct sockaddr_storage));
+	if (err)
+		return err;
+
+	/* The caller fills the fixed part in once it knows gf_numsrc. */
+	iov_iter_advance(&opt->iter_out, ss_offset);
+
 	for (i = 0; i < copycount; i++) {
 		struct sockaddr_in6 *psin6;
 		struct sockaddr_storage ss;
@@ -637,9 +649,8 @@ int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
 		memset(&ss, 0, sizeof(ss));
 		psin6->sin6_family = AF_INET6;
 		psin6->sin6_addr = psl->sl_addr[i];
-		if (copy_to_sockptr_offset(optval, ss_offset, &ss, sizeof(ss)))
+		if (copy_to_iter(&ss, sizeof(ss), &opt->iter_out) != sizeof(ss))
 			return -EFAULT;
-		ss_offset += sizeof(ss);
 	}
 	return 0;
 }
Signature · 1 line
2.53.0-Meta
Reviewed-by on v4 3/7
#9 lore raw

Re: [PATCH net-next v4 3/7] ipv6: mcast: convert ip6_mc_msfget() to sockopt_t

Quoted 25 lines · Breno Leitao

MCAST_MSFILTER reads its reply through ip6_mc_msfget(), reached from do_ipv6_getsockopt() and from nowhere else. Convert it, and build the sockopt_t at the call site for as long as the caller still carries a sockptr_t pair.

optlen only has to cover the fixed part, and the real reply size comes from the gf_numsrc field inside it. Userspace relies on that, so sockopt_expand_out() grows optval past optlen, for a user address only and only far enough for the sources the socket has.

ip6_mc_msfget() now advances over the fixed part and writes the source list through iter_out. Its callers rewind by the reply length they already compute, which is exactly what the callee consumed, and land back where they used to write: offset 0 for the native reply, gf_fmode for the compat one.

ipv6_get_msfilter() and compat_ipv6_get_msfilter() only store *optlen after their own copy succeeds, so a copy failure can't report a changed length: -EINVAL, -EADDRNOTAVAIL and -EFAULT all leave the caller's optlen word unchanged.

The old code stored *optlen before writing the fixed part, so an unwritable optlen word stopped the fixed part from being written. Now the fixed part is written first.

Quoted 9 lines
---
 include/net/ipv6.h       |  2 +-
 net/ipv6/ipv6_sockglue.c | 64 ++++++++++++++++++++++++++++++++----------------
 net/ipv6/mcast.c         | 19 +++++++++++---
 3 files changed, 59 insertions(+), 26 deletions(-)
Reviewed-by on v4 3/7
#10 lore raw

Re: [PATCH net-next v4 3/7] ipv6: mcast: convert ip6_mc_msfget() to sockopt_t

In reply to Breno Leitao
Quoted 21 lines · Breno Leitao

MCAST_MSFILTER reads its reply through ip6_mc_msfget(), reached from do_ipv6_getsockopt() and from nowhere else. Convert it, and build the sockopt_t at the call site for as long as the caller still carries a sockptr_t pair.

optlen only has to cover the fixed part, and the real reply size comes from the gf_numsrc field inside it. Userspace relies on that, so sockopt_expand_out() grows optval past optlen, for a user address only and only far enough for the sources the socket has.

ip6_mc_msfget() now advances over the fixed part and writes the source list through iter_out. Its callers rewind by the reply length they already compute, which is exactly what the callee consumed, and land back where they used to write: offset 0 for the native reply, gf_fmode for the compat one.

ipv6_get_msfilter() and compat_ipv6_get_msfilter() only store *optlen after their own copy succeeds, so a copy failure can't report a changed length: -EINVAL, -EADDRNOTAVAIL and -EFAULT all leave the caller's optlen word unchanged.

Quoted 7 lines

The old code stored *optlen before writing the fixed part, so an unwritable optlen word stopped the fixed part from being written. Now the fixed part is written first.

#11 lore raw

PATCH v4 4/7 ipv4: igmp: convert ip_mc_gsfget() to sockopt_t

In reply to Breno Leitao

MCAST_MSFILTER reads its reply through ip_mc_gsfget(), reached from do_ip_getsockopt() and from nowhere else. Convert it the same way as the ipv6 side, and build the sockopt_t at the call site for as long as the caller still carries a sockptr_t pair.

optlen here only has to cover the fixed part, and the real reply size comes from the gf_numsrc field inside it. This is nasty, but userspace relies on it, so sockopt_expand_out() preserves the same mechanism: it grows optval only for a user address, and only far enough for the sources the socket has.

ip_mc_gsfget() writes the source list, and its two callers write the fixed part afterwards, at the head of optval. iter_out only moves forward, so the callee advances over the fixed part rather than addressing each source by offset.

The callers then rewind. The reply length they already compute is exactly what the callee consumed, ss_offset plus the sources it wrote, so both land back where they used to write: offset 0 for the native reply, gf_fmode for the compat one. The bytes are the same.

ip_get_mcast_msfilter() and compat_ip_get_mcast_msfilter() only store *optlen after their own copy succeeds, so a copy failure can't report a changed length: -EINVAL, -EADDRNOTAVAIL and -EFAULT all leave the caller's optlen word unchanged.

The old code stored *optlen before writing the fixed part, so an unwritable optlen word stopped the fixed part from being written. Now the fixed part is written first.

---
 include/linux/igmp.h   |  2 +-
 net/ipv4/igmp.c        | 20 +++++++++++++-----
 net/ipv4/ip_sockglue.c | 57 +++++++++++++++++++++++++++++++-------------------
 3 files changed, 52 insertions(+), 27 deletions(-)
include/linux/igmp.h +1 -1
index e075611344ef3b..0a1abf3552d2bb 100644
@@ -276,7 +276,7 @@ extern int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf,int ifindex);
 extern int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
 			sockopt_t *opt);
 extern int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
-			sockptr_t optval, size_t offset);
+			sockopt_t *opt, size_t offset);
 extern int ip_mc_sf_allow(const struct sock *sk, __be32 local, __be32 rmt,
 			  int dif, int sdif);
 extern void ip_mc_init_dev(struct in_device *);
net/ipv4/igmp.c +15 -5
index 144fca158adcb0..d573c5bf8f038b 100644
@@ -2775,9 +2775,9 @@ int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, sockopt_t *opt)
 }
 
 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
-		 sockptr_t optval, size_t ss_offset)
+		 sockopt_t *opt, size_t ss_offset)
 {
-	int i, count, copycount;
+	int i, count, copycount, err;
 	struct sockaddr_in *psin;
 	__be32 addr;
 	struct ip_mc_socklist *pmc;
@@ -2805,6 +2805,18 @@ int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
 	count = psl ? psl->sl_count : 0;
 	copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
 	gsf->gf_numsrc = count;
+
+	/* The source list is sized by the gf_numsrc the caller left in optval,
+	 * not by optlen, which only has to cover the fixed part.
+	 */
+	err = sockopt_expand_out(opt, ss_offset +
+				 copycount * sizeof(struct sockaddr_storage));
+	if (err)
+		return err;
+
+	/* The caller fills the fixed part in once it knows gf_numsrc. */
+	iov_iter_advance(&opt->iter_out, ss_offset);
+
 	for (i = 0; i < copycount; i++) {
 		struct sockaddr_storage ss;
 
@@ -2812,10 +2824,8 @@ int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
 		memset(&ss, 0, sizeof(ss));
 		psin->sin_family = AF_INET;
 		psin->sin_addr.s_addr = psl->sl_addr[i];
-		if (copy_to_sockptr_offset(optval, ss_offset,
-					   &ss, sizeof(ss)))
+		if (copy_to_iter(&ss, sizeof(ss), &opt->iter_out) != sizeof(ss))
 			return -EFAULT;
-		ss_offset += sizeof(ss);
 	}
 	return 0;
 }
net/ipv4/ip_sockglue.c +36 -21

112 lines not on this page · raw message

Signature · 1 line
2.53.0-Meta
Reviewed-by on v4 4/7
#12 lore raw

Re: [PATCH net-next v4 4/7] ipv4: igmp: convert ip_mc_gsfget() to sockopt_t

Quoted 30 lines · Breno Leitao

MCAST_MSFILTER reads its reply through ip_mc_gsfget(), reached from do_ip_getsockopt() and from nowhere else. Convert it the same way as the ipv6 side, and build the sockopt_t at the call site for as long as the caller still carries a sockptr_t pair.

optlen here only has to cover the fixed part, and the real reply size comes from the gf_numsrc field inside it. This is nasty, but userspace relies on it, so sockopt_expand_out() preserves the same mechanism: it grows optval only for a user address, and only far enough for the sources the socket has.

ip_mc_gsfget() writes the source list, and its two callers write the fixed part afterwards, at the head of optval. iter_out only moves forward, so the callee advances over the fixed part rather than addressing each source by offset.

The callers then rewind. The reply length they already compute is exactly what the callee consumed, ss_offset plus the sources it wrote, so both land back where they used to write: offset 0 for the native reply, gf_fmode for the compat one. The bytes are the same.

ip_get_mcast_msfilter() and compat_ip_get_mcast_msfilter() only store *optlen after their own copy succeeds, so a copy failure can't report a changed length: -EINVAL, -EADDRNOTAVAIL and -EFAULT all leave the caller's optlen word unchanged.

The old code stored *optlen before writing the fixed part, so an unwritable optlen word stopped the fixed part from being written. Now the fixed part is written first.

Quoted 8 lines
---
 include/linux/igmp.h   |  2 +-
 net/ipv4/igmp.c        | 20 +++++++++++++-----
 net/ipv4/ip_sockglue.c | 57 +++++++++++++++++++++++++++++++-------------------
 3 files changed, 52 insertions(+), 27 deletions(-)
#13 lore raw

PATCH v4 5/7 ipv4: convert do_ip_getsockopt() to sockopt_t

In reply to Breno Leitao

Convert the SOL_IP switch and ip_mroute_getsockopt() to sockopt_t, its last leaf on a sockptr_t pair. The IP_MSFILTER and MCAST_MSFILTER bridges go away.

ip_getsockopt() builds the sockopt_t with sockopt_init_user() and writes optlen back unconditionally, because IP_OPTIONS and others report a length and can then fail. ip_mc_msfget() therefore stores its length only after its last copy, so a copy failure can't report a changed one.

The write-back also happens before the netfilter fallback, so an unwritable optlen turns any error into -EFAULT and skips the fallback. Likewise optlen is now read before the level check and the MRT_* dispatch, so an unreadable or negative one wins over -EOPNOTSUPP.

IP_OPTIONS with no options set and IP_PKTOPTIONS used to return the result of copy_to_sockptr(), the number of bytes not copied, so an unwritable optlen made getsockopt() return 4. They now return -EFAULT.

sol_ip_sockopt() builds the sockopt_t with sockptr_to_sockopt() and stores optlen back as well. This extra code will go away when we convert sol_ip_sockopt() to talk sockopt as well, but for now we are converting the leaves.

---
 include/linux/mroute.h |  4 +--
 include/net/ip.h       |  3 +-
 net/core/filter.c      | 18 ++++++++---
 net/ipv4/igmp.c        |  3 +-
 net/ipv4/ip_sockglue.c | 87 +++++++++++++++++++-------------------------------
 net/ipv4/ipmr.c        | 11 +++----
 6 files changed, 56 insertions(+), 70 deletions(-)
include/linux/mroute.h +2 -2
index 4c5003afee6c51..c1c21e61c81c37 100644
@@ -17,7 +17,7 @@ static inline int ip_mroute_opt(int opt)
 }
 
 int ip_mroute_setsockopt(struct sock *, int, sockptr_t, unsigned int);
-int ip_mroute_getsockopt(struct sock *, int, sockptr_t, sockptr_t);
+int ip_mroute_getsockopt(struct sock *sk, int optname, sockopt_t *opt);
 int ipmr_ioctl(struct sock *sk, int cmd, void *arg);
 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg);
 int ip_mr_init(void);
@@ -31,7 +31,7 @@ static inline int ip_mroute_setsockopt(struct sock *sock, int optname,
 }
 
 static inline int ip_mroute_getsockopt(struct sock *sk, int optname,
-				       sockptr_t optval, sockptr_t optlen)
+				       sockopt_t *opt)
 {
 	return -ENOPROTOOPT;
 }
include/net/ip.h +1 -2
index 0acc4ba5e5b340..5035b313a26b0d 100644
@@ -808,8 +808,7 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
 		     unsigned int optlen);
 int ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
 		  unsigned int optlen);
-int do_ip_getsockopt(struct sock *sk, int level, int optname,
-		     sockptr_t optval, sockptr_t optlen);
+int do_ip_getsockopt(struct sock *sk, int level, int optname, sockopt_t *sopt);
 int ip_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
 		  int __user *optlen);
 int ip_ra_control(struct sock *sk, unsigned char on,
net/core/filter.c +14 -4
index 9245009d9f1ebc..fc427876bde839 100644
@@ -5827,10 +5827,20 @@ static int sol_ip_sockopt(struct sock *sk, int optname,
 		return -EINVAL;
 	}
 
-	if (getopt)
-		return do_ip_getsockopt(sk, SOL_IP, optname,
-					KERNEL_SOCKPTR(optval),
-					KERNEL_SOCKPTR(optlen));
+	if (getopt) {
+		struct kvec kvec;
+		sockopt_t opt;
+		int err;
+
+		err = sockptr_to_sockopt(&opt, KERNEL_SOCKPTR(optval),
+					 KERNEL_SOCKPTR(optlen), &kvec);
+		if (err)
+			return err;
+
+		err = do_ip_getsockopt(sk, SOL_IP, optname, &opt);
+		*optlen = opt.optlen;
+		return err;
+	}
 
 	return do_ip_setsockopt(sk, SOL_IP, optname,
 				KERNEL_SOCKPTR(optval), *optlen);
net/ipv4/igmp.c +2 -1
index d573c5bf8f038b..bf00810cdd7a13 100644
@@ -2763,12 +2763,13 @@ int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, sockopt_t *opt)
 	if (err)
 		return err;
 
-	opt->optlen = msf_size;
 	if (copy_to_iter(msf, IP_MSFILTER_SIZE(0), &opt->iter_out) !=
 	    IP_MSFILTER_SIZE(0))
 		return -EFAULT;
 	if (len && copy_to_iter(psl->sl_addr, len, &opt->iter_out) != len)
 		return -EFAULT;
+
+	opt->optlen = msf_size;
 	return 0;
 done:
 	return err;
net/ipv4/ip_sockglue.c +33 -54

174 lines not on this page · raw message

net/ipv4/ipmr.c +4 -7
index e8f7d3b573d0f4..e4c9accbb13ea9 100644
@@ -1657,8 +1657,7 @@ int ipmr_sk_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
 }
 
 /* Getsock opt support for the multicast routing system. */
-int ip_mroute_getsockopt(struct sock *sk, int optname, sockptr_t optval,
-			 sockptr_t optlen)
+int ip_mroute_getsockopt(struct sock *sk, int optname, sockopt_t *opt)
 {
 	int olr;
 	int val;
@@ -1689,16 +1688,14 @@ int ip_mroute_getsockopt(struct sock *sk, int optname, sockptr_t optval,
 		return -ENOPROTOOPT;
 	}
 
-	if (copy_from_sockptr(&olr, optlen, sizeof(int)))
-		return -EFAULT;
+	olr = opt->optlen;
 	if (olr < 0)
 		return -EINVAL;
 
 	olr = min_t(unsigned int, olr, sizeof(int));
 
-	if (copy_to_sockptr(optlen, &olr, sizeof(int)))
-		return -EFAULT;
-	if (copy_to_sockptr(optval, &val, olr))
+	opt->optlen = olr;
+	if (copy_to_iter(&val, olr, &opt->iter_out) != olr)
 		return -EFAULT;
 	return 0;
 }
Signature · 1 line
2.53.0-Meta
Reviewed-by on v4 5/7
#14 lore raw

Re: [PATCH net-next v4 5/7] ipv4: convert do_ip_getsockopt() to sockopt_t

Quoted 35 lines · Breno Leitao

Convert the SOL_IP switch and ip_mroute_getsockopt() to sockopt_t, its last leaf on a sockptr_t pair. The IP_MSFILTER and MCAST_MSFILTER bridges go away.

ip_getsockopt() builds the sockopt_t with sockopt_init_user() and writes optlen back unconditionally, because IP_OPTIONS and others report a length and can then fail. ip_mc_msfget() therefore stores its length only after its last copy, so a copy failure can't report a changed one.

The write-back also happens before the netfilter fallback, so an unwritable optlen turns any error into -EFAULT and skips the fallback. Likewise optlen is now read before the level check and the MRT_* dispatch, so an unreadable or negative one wins over -EOPNOTSUPP.

IP_OPTIONS with no options set and IP_PKTOPTIONS used to return the result of copy_to_sockptr(), the number of bytes not copied, so an unwritable optlen made getsockopt() return 4. They now return -EFAULT.

sol_ip_sockopt() builds the sockopt_t with sockptr_to_sockopt() and stores optlen back as well. This extra code will go away when we convert sol_ip_sockopt() to talk sockopt as well, but for now we are converting the leaves.

---
 include/linux/mroute.h |  4 +--
 include/net/ip.h       |  3 +-
 net/core/filter.c      | 18 ++++++++---
 net/ipv4/igmp.c        |  3 +-
 net/ipv4/ip_sockglue.c | 87 +++++++++++++++++++-------------------------------
 net/ipv4/ipmr.c        | 11 +++----
 6 files changed, 56 insertions(+), 70 deletions(-)

[...]

Quoted 4 lines
net/ipv4/ip_sockglue.c +0 -0
@@ -1711,43 +1707,25 @@ int do_ip_getsockopt(struct sock *sk, int level, int optname,
 	case IP_MSFILTER:
 	{
 		struct ip_msfilter msf;

net/ipv4/ip_sockglue.c int do_ip_getsockopt(struct sock *sk, int level, int optname, · line 1716

Quoted 10 lines
net/ipv4/ip_sockglue.c +2 -3
-		struct kvec kvec;
-		sockopt_t opt;
 
 		if (len < IP_MSFILTER_SIZE(0)) {
 			err = -EINVAL;
 			goto out;
 		}
-		if (copy_from_sockptr(&msf, optval, IP_MSFILTER_SIZE(0))) {
+		if (copy_from_iter(&msf, IP_MSFILTER_SIZE(0), &sopt->iter_in) !=
+		    IP_MSFILTER_SIZE(0)) {

The line wrapping is a little weird looking to me. Not sure if there is anything that can be done about it and not sure it would be worth a re-spin just because I think it's a little ugly :)

Reviewed-by on v4 5/7
#15 lore raw

Re: [PATCH net-next v4 5/7] ipv4: convert do_ip_getsockopt() to sockopt_t

In reply to Breno Leitao
Quoted 19 lines · Breno Leitao

Convert the SOL_IP switch and ip_mroute_getsockopt() to sockopt_t, its last leaf on a sockptr_t pair. The IP_MSFILTER and MCAST_MSFILTER bridges go away.

ip_getsockopt() builds the sockopt_t with sockopt_init_user() and writes optlen back unconditionally, because IP_OPTIONS and others report a length and can then fail. ip_mc_msfget() therefore stores its length only after its last copy, so a copy failure can't report a changed one.

The write-back also happens before the netfilter fallback, so an unwritable optlen turns any error into -EFAULT and skips the fallback. Likewise optlen is now read before the level check and the MRT_* dispatch, so an unreadable or negative one wins over -EOPNOTSUPP.

IP_OPTIONS with no options set and IP_PKTOPTIONS used to return the result of copy_to_sockptr(), the number of bytes not copied, so an unwritable optlen made getsockopt() return 4. They now return -EFAULT.

Quoted 8 lines

sol_ip_sockopt() builds the sockopt_t with sockptr_to_sockopt() and stores optlen back as well. This extra code will go away when we convert sol_ip_sockopt() to talk sockopt as well, but for now we are converting the leaves.

#16 lore raw

PATCH v4 6/7 ipv6: convert do_ipv6_getsockopt() to sockopt_t

In reply to Breno Leitao

Convert the SOL_IPV6 switch and its two remaining sockptr_t leaves, ip6_mroute_getsockopt() and ipv6_getsockopt_sticky().

ipv6_getsockopt() writes optlen back unconditionally, and sol_ipv6_sockopt() builds the sockopt_t with sockptr_to_sockopt() and stores optlen back as well.

ipv6_getsockopt() cannot use sockopt_init_user() as its IPv4 sibling does, since that rejects a negative optlen and this level answers it as
4. See "ipv6: treat a negative optlen as 4 in do_ipv6_getsockopt()".

Instead ipv6_getsockopt() applies that rule itself and builds the sockopt_t with the new sockopt_set_user(), which sockopt_init_user() now shares. MRT6_* is the exception: it has always rejected a negative optlen, and still does.

The write-back also happens before the netfilter fallback, so an unwritable optlen turns any error into -EFAULT and skips the fallback. Likewise optlen is now read before the MRT6_* dispatch, so an unreadable or negative one wins over -EOPNOTSUPP.

For the other options, a failing call that passed a negative optlen now leaves it as 4 instead of untouched, and the netfilter fallback sees that 4 too.

IPV6_2292PKTOPTIONS and the sticky options used to return the result of copy_to_sockptr(), the number of bytes not copied, so an unwritable optlen made getsockopt() return 4. They now return -EFAULT.

The extra code in sol_ipv6_sockopt() will go away when sol_ipv6_sockopt() receives sockopt_t, but, for now, it only moves the leaves.

---
 include/linux/mroute6.h  |  5 ++-
 include/linux/net.h      | 17 +++++++--
 include/net/ipv6.h       |  2 +-
 net/core/filter.c        | 18 +++++++---
 net/ipv6/ip6mr.c         | 13 +++----
 net/ipv6/ipv6_sockglue.c | 93 +++++++++++++++++++++---------------------------
 6 files changed, 76 insertions(+), 72 deletions(-)
include/linux/mroute6.h +2 -3
index fddafdc168f733..ab4d206fb32065 100644
@@ -27,7 +27,7 @@ struct sock;
 
 #ifdef CONFIG_IPV6_MROUTE
 extern int ip6_mroute_setsockopt(struct sock *, int, sockptr_t, unsigned int);
-extern int ip6_mroute_getsockopt(struct sock *, int, sockptr_t, sockptr_t);
+int ip6_mroute_getsockopt(struct sock *sk, int optname, sockopt_t *sopt);
 extern int ip6_mr_input(struct sk_buff *skb);
 extern int ip6mr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg);
 extern int ip6_mr_init(void);
@@ -42,8 +42,7 @@ static inline int ip6_mroute_setsockopt(struct sock *sock, int optname,
 }
 
 static inline
-int ip6_mroute_getsockopt(struct sock *sock,
-			  int optname, sockptr_t optval, sockptr_t optlen)
+int ip6_mroute_getsockopt(struct sock *sock, int optname, sockopt_t *sopt)
 {
 	return -ENOPROTOOPT;
 }
include/linux/net.h +14 -3
index de56f89d475ee2..1d5b3524b0846f 100644
@@ -50,6 +50,19 @@ typedef struct sockopt {
 int sockptr_to_sockopt(sockopt_t *opt, sockptr_t optval, sockptr_t optlen,
 		       struct kvec *kvec);
 
+/*
+ * Point a sockopt_t at a user-backed (optval, len) pair whose length is
+ * already known, e.g. because the caller applied its own optlen quirks
+ * before this point.
+ */
+static inline void sockopt_set_user(sockopt_t *opt, char __user *optval,
+				    int len)
+{
+	iov_iter_ubuf(&opt->iter_out, ITER_DEST, optval, len);
+	iov_iter_ubuf(&opt->iter_in, ITER_SOURCE, optval, len);
+	opt->optlen = len;
+}
+
 /*
  * Initialize a user-backed sockopt_t from the (optval, optlen) __user pair of
  * a getsockopt() callback. Used by transitional __user getsockopt wrappers
@@ -66,9 +79,7 @@ static inline int sockopt_init_user(sockopt_t *opt, char __user *optval,
 	if (len < 0)
 		return -EINVAL;
 
-	iov_iter_ubuf(&opt->iter_out, ITER_DEST, optval, len);
-	iov_iter_ubuf(&opt->iter_in, ITER_SOURCE, optval, len);
-	opt->optlen = len;
+	sockopt_set_user(opt, optval, len);
 
 	return 0;
 }
include/net/ipv6.h +1 -1
index 9bb68d75890364..a1e1de7da8c70d 100644
@@ -1142,7 +1142,7 @@ int do_ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval
 int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
 		    unsigned int optlen);
 int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
-		       sockptr_t optval, sockptr_t optlen);
+		       sockopt_t *sopt);
 int ipv6_getsockopt(struct sock *sk, int level, int optname,
 		    char __user *optval, int __user *optlen);
 
net/core/filter.c +14 -4
index fc427876bde839..8bf8b87e5d4fc2 100644
@@ -5864,10 +5864,20 @@ static int sol_ipv6_sockopt(struct sock *sk, int optname,
 		return -EINVAL;
 	}
 
-	if (getopt)
-		return do_ipv6_getsockopt(sk, SOL_IPV6, optname,
-					  KERNEL_SOCKPTR(optval),
-					  KERNEL_SOCKPTR(optlen));
+	if (getopt) {
+		struct kvec kvec;
+		sockopt_t opt;
+		int err;
+
+		err = sockptr_to_sockopt(&opt, KERNEL_SOCKPTR(optval),
+					 KERNEL_SOCKPTR(optlen), &kvec);
+		if (err)
+			return err;
+
+		err = do_ipv6_getsockopt(sk, SOL_IPV6, optname, &opt);
+		*optlen = opt.optlen;
+		return err;
+	}
 
 	return do_ipv6_setsockopt(sk, SOL_IPV6, optname,
 				  KERNEL_SOCKPTR(optval), *optlen);
net/ipv6/ip6mr.c +4 -9
index af968a2c2a7847..f14bd12f5d4b91 100644
@@ -1899,8 +1899,7 @@ int ip6_mroute_setsockopt(struct sock *sk, int optname, sockptr_t optval,
  *	Getsock opt support for the multicast routing system.
  */
 
-int ip6_mroute_getsockopt(struct sock *sk, int optname, sockptr_t optval,
-			  sockptr_t optlen)
+int ip6_mroute_getsockopt(struct sock *sk, int optname, sockopt_t *sopt)
 {
 	int olr;
 	int val;
@@ -1931,16 +1930,12 @@ int ip6_mroute_getsockopt(struct sock *sk, int optname, sockptr_t optval,
 		return -ENOPROTOOPT;
 	}
 
-	if (copy_from_sockptr(&olr, optlen, sizeof(int)))
-		return -EFAULT;
-
-	olr = min_t(int, olr, sizeof(int));
+	olr = min_t(int, sopt->optlen, sizeof(int));
 	if (olr < 0)
 		return -EINVAL;
 
-	if (copy_to_sockptr(optlen, &olr, sizeof(int)))
-		return -EFAULT;
-	if (copy_to_sockptr(optval, &val, olr))
+	sopt->optlen = olr;
+	if (copy_to_iter(&val, olr, &sopt->iter_out) != olr)
 		return -EFAULT;
 	return 0;
 }
net/ipv6/ipv6_sockglue.c +41 -52

185 lines not on this page · raw message

Signature · 1 line
2.53.0-Meta
Acked-by on v4 6/7
#17 lore raw

Re: [PATCH net-next v4 6/7] ipv6: convert do_ipv6_getsockopt() to sockopt_t

Quoted 29 lines · Breno Leitao

Convert the SOL_IPV6 switch and its two remaining sockptr_t leaves, ip6_mroute_getsockopt() and ipv6_getsockopt_sticky().

ipv6_getsockopt() writes optlen back unconditionally, and sol_ipv6_sockopt() builds the sockopt_t with sockptr_to_sockopt() and stores optlen back as well.

ipv6_getsockopt() cannot use sockopt_init_user() as its IPv4 sibling does, since that rejects a negative optlen and this level answers it as
4. See "ipv6: treat a negative optlen as 4 in do_ipv6_getsockopt()".

Instead ipv6_getsockopt() applies that rule itself and builds the sockopt_t with the new sockopt_set_user(), which sockopt_init_user() now shares. MRT6_* is the exception: it has always rejected a negative optlen, and still does.

The write-back also happens before the netfilter fallback, so an unwritable optlen turns any error into -EFAULT and skips the fallback. Likewise optlen is now read before the MRT6_* dispatch, so an unreadable or negative one wins over -EOPNOTSUPP.

For the other options, a failing call that passed a negative optlen now leaves it as 4 instead of untouched, and the netfilter fallback sees that 4 too.

IPV6_2292PKTOPTIONS and the sticky options used to return the result of copy_to_sockptr(), the number of bytes not copied, so an unwritable optlen made getsockopt() return 4. They now return -EFAULT.

Quoted 6 lines

The extra code in sol_ipv6_sockopt() will go away when sol_ipv6_sockopt() receives sockopt_t, but, for now, it only moves the leaves.

#18 lore raw

PATCH v4 7/7 selftests: net: getsockopt_iter: cover ip and ipv6

In reply to Breno Leitao

Add an ip and an ipv6 fixture, pinning the returned length and errno across buffer sizes, the branches that answer empty, an unknown optname and the options dispatched before the switch. The clamp tests also compare the bytes copied back, not just the length.

SOL_IP answers a sub-int buffer with one byte where SOL_IPV6 clamps the int. IP_PKTOPTIONS and IPV6_2292PKTOPTIONS want a stream socket; MRT_*/MRT6_* want a raw one.

ipv6.pathmtu_negative_optlen pins the clamp of a negative optlen: taken as 4, it is too short for IPV6_PATHMTU and fails with -EINVAL, where it used to pass the length check and fail with -ENOTCONN.

Only the two hop-by-hop tests set IPV6_HOPOPTS, which needs CAP_NET_RAW, so the other ipv6 tests run unprivileged.

The MRT_*/MRT6_* tests skip on a missing /proc/net/ip_mr_vif or ip6_mr_vif, rather than on the ENOPROTOOPT the switch also answers for an unrelated regression. A kernel with CONFIG_IP_MROUTE or CONFIG_IPV6_MROUTE built in no longer hides that regression behind a skip.

The config fragment only had CONFIG_IPV6_MROUTE=y, so add CONFIG_IP_MROUTE=y for the ip test, and CONFIG_IP_MULTICAST=y, which IP_MROUTE depends on.

MRT_VERSION and MRT6_VERSION are spelled out; linux/mroute.h does not coexist with netinet/in.h here.

---
 tools/testing/selftests/net/config            |   2 +
 tools/testing/selftests/net/getsockopt_iter.c | 372 ++++++++++++++++++++++++++
 2 files changed, 374 insertions(+)
tools/testing/selftests/net/config +2 -0
index d4416f8bdcfd1d..7c8bf84eaa6dfc 100644
@@ -29,6 +29,8 @@ CONFIG_IP6_NF_MATCH_RPFILTER=m
 CONFIG_IP6_NF_NAT=m
 CONFIG_IP6_NF_RAW=m
 CONFIG_IP6_NF_TARGET_REJECT=m
+CONFIG_IP_MROUTE=y
+CONFIG_IP_MULTICAST=y
 CONFIG_IP_NF_FILTER=m
 CONFIG_IP_NF_IPTABLES=m
 CONFIG_IP_NF_IPTABLES_LEGACY=m
tools/testing/selftests/net/getsockopt_iter.c +372 -0

384 lines not on this page · raw message

Signature · 1 line
2.53.0-Meta
Acked-by on v4 7/7
#19 lore raw

Re: [PATCH net-next v4 7/7] selftests: net: getsockopt_iter: cover ip and ipv6

Quoted 26 lines · Breno Leitao

Add an ip and an ipv6 fixture, pinning the returned length and errno across buffer sizes, the branches that answer empty, an unknown optname and the options dispatched before the switch. The clamp tests also compare the bytes copied back, not just the length.

SOL_IP answers a sub-int buffer with one byte where SOL_IPV6 clamps the int. IP_PKTOPTIONS and IPV6_2292PKTOPTIONS want a stream socket; MRT_*/MRT6_* want a raw one.

ipv6.pathmtu_negative_optlen pins the clamp of a negative optlen: taken as 4, it is too short for IPV6_PATHMTU and fails with -EINVAL, where it used to pass the length check and fail with -ENOTCONN.

Only the two hop-by-hop tests set IPV6_HOPOPTS, which needs CAP_NET_RAW, so the other ipv6 tests run unprivileged.

The MRT_*/MRT6_* tests skip on a missing /proc/net/ip_mr_vif or ip6_mr_vif, rather than on the ENOPROTOOPT the switch also answers for an unrelated regression. A kernel with CONFIG_IP_MROUTE or CONFIG_IPV6_MROUTE built in no longer hides that regression behind a skip.

The config fragment only had CONFIG_IPV6_MROUTE=y, so add CONFIG_IP_MROUTE=y for the ip test, and CONFIG_IP_MULTICAST=y, which IP_MROUTE depends on.

Quoted 5 lines

MRT_VERSION and MRT6_VERSION are spelled out; linux/mroute.h does not coexist with netinet/in.h here.

Reviewed-by on v4 7/7
#20 lore raw

Re: [PATCH net-next v4 7/7] selftests: net: getsockopt_iter: cover ip and ipv6

In reply to Breno Leitao
Quoted 29 lines · Breno Leitao

Add an ip and an ipv6 fixture, pinning the returned length and errno across buffer sizes, the branches that answer empty, an unknown optname and the options dispatched before the switch. The clamp tests also compare the bytes copied back, not just the length.

SOL_IP answers a sub-int buffer with one byte where SOL_IPV6 clamps the int. IP_PKTOPTIONS and IPV6_2292PKTOPTIONS want a stream socket; MRT_*/MRT6_* want a raw one.

ipv6.pathmtu_negative_optlen pins the clamp of a negative optlen: taken as 4, it is too short for IPV6_PATHMTU and fails with -EINVAL, where it used to pass the length check and fail with -ENOTCONN.

Only the two hop-by-hop tests set IPV6_HOPOPTS, which needs CAP_NET_RAW, so the other ipv6 tests run unprivileged.

The MRT_*/MRT6_* tests skip on a missing /proc/net/ip_mr_vif or ip6_mr_vif, rather than on the ENOPROTOOPT the switch also answers for an unrelated regression. A kernel with CONFIG_IP_MROUTE or CONFIG_IPV6_MROUTE built in no longer hides that regression behind a skip.

The config fragment only had CONFIG_IPV6_MROUTE=y, so add CONFIG_IP_MROUTE=y for the ip test, and CONFIG_IP_MULTICAST=y, which IP_MROUTE depends on.

MRT_VERSION and MRT6_VERSION are spelled out; linux/mroute.h does not coexist with netinet/in.h here.

Quoted 7 lines
---
 tools/testing/selftests/net/config            |   2 +
 tools/testing/selftests/net/getsockopt_iter.c | 372 ++++++++++++++++++++++++++
 2 files changed, 374 insertions(+)
#21 lore raw
In reply to Breno Leitao
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 5 lines · On Tue, 03 Nov 2026 03:00:59 -0800 you wrote:

This series continues the migration to sockopt_t, as described in [1]. do_ip_getsockopt() and do_ipv6_getsockopt() still take a sockptr_t pair, and everything they forward to now takes a sockopt_t, so convert them both.

Quoted 7 lines

Patch 1 settles what a negative optlen means on SOL_IPV6 before the conversion makes it matter. do_ip_getsockopt() rejects it, but do_ipv6_getsockopt() never has, and the int options answer it as 4. Treat it as 4, as David Laight suggested, instead of failing callers that rely on it.

[...]

Here is the summary with links:
- [net-next,v4,1/7] ipv6: treat a negative optlen as 4 in do_ipv6_getsockopt()

    https://git.kernel.org/netdev/net-next/c/61270b859fb5
  - [net-next,v4,2/7] net: cap sockopt_expand_out() at MAX_RW_COUNT
    https://git.kernel.org/netdev/net-next/c/6de30bde94b3
  - [net-next,v4,3/7] ipv6: mcast: convert ip6_mc_msfget() to sockopt_t
    https://git.kernel.org/netdev/net-next/c/11762c7f2191
  - [net-next,v4,4/7] ipv4: igmp: convert ip_mc_gsfget() to sockopt_t
    https://git.kernel.org/netdev/net-next/c/d845b553c6dc
  - [net-next,v4,5/7] ipv4: convert do_ip_getsockopt() to sockopt_t
    https://git.kernel.org/netdev/net-next/c/67582bdf5349
  - [net-next,v4,6/7] ipv6: convert do_ipv6_getsockopt() to sockopt_t
    https://git.kernel.org/netdev/net-next/c/52f243f4ff04
  - [net-next,v4,7/7] selftests: net: getsockopt_iter: cover ip and ipv6
    https://git.kernel.org/netdev/net-next/c/47e7aaf680a4

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html