Skip to contentSkip to conversation

netlink: Use copy_to_user() for optval in netlink_getsockopt().

PATCH Superseded Kuniyuki Iwashima 3 participants 9 messages Apr 19, 2023 – Apr 20, 2023 1 message not in the archive

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netlink: Use copy_to_user() for optval in netlink_getsockopt().

Brad Spencer provided a detailed report that when calling getsockopt() for AF_NETLINK, some SOL_NETLINK options set only 1 byte even though such options require more than int as length.

The options return a flag value that fits into 1 byte, but such behaviour confuses users who do not strictly check the value as char.

Currently, netlink_getsockopt() uses put_user() to copy data to optlen and optval, but put_user() casts the data based on the pointer, char *optval. So, only 1 byte is set to optval.

To avoid this behaviour, we need to use copy_to_user() or cast optval for put_user().

Now getsockopt() accepts char as optval as the flags are only 1 byte.

Fixes 9a4595bc7e67 ("[NETLINK]: Add set/getsockopt options to support more than 32 groups")
Fixes be0c22a46cfb ("netlink: add NETLINK_BROADCAST_ERROR socket option")
Fixes 38938bfe3489 ("netlink: add NETLINK_NO_ENOBUFS socket flag")
Fixes 0a6a3a23ea6e ("netlink: add NETLINK_CAP_ACK socket option")
Fixes 2d4bc93368f5 ("netlink: extended ACK reporting")
Fixes 89d35528d17d ("netlink: Add new socket option to enable strict checking on dumps")
---
 net/netlink/af_netlink.c | 56 +++++++---------------------------------
 1 file changed, 10 insertions(+), 46 deletions(-)
net/netlink/af_netlink.c +10 -46
index f365dfdd672d..3106f09d5f1c 100644
@@ -1742,7 +1742,7 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
 {
 	struct sock *sk = sock->sk;
 	struct netlink_sock *nlk = nlk_sk(sk);
-	int len, val, err;
+	int len, val;
 
 	if (level != SOL_NETLINK)
 		return -ENOPROTOOPT;
@@ -1754,39 +1754,17 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
 
 	switch (optname) {
 	case NETLINK_PKTINFO:
-		if (len < sizeof(int))
-			return -EINVAL;
-		len = sizeof(int);
 		val = nlk->flags & NETLINK_F_RECV_PKTINFO ? 1 : 0;
-		if (put_user(len, optlen) ||
-		    put_user(val, optval))
-			return -EFAULT;
-		err = 0;
 		break;
 	case NETLINK_BROADCAST_ERROR:
-		if (len < sizeof(int))
-			return -EINVAL;
-		len = sizeof(int);
 		val = nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR ? 1 : 0;
-		if (put_user(len, optlen) ||
-		    put_user(val, optval))
-			return -EFAULT;
-		err = 0;
 		break;
 	case NETLINK_NO_ENOBUFS:
-		if (len < sizeof(int))
-			return -EINVAL;
-		len = sizeof(int);
 		val = nlk->flags & NETLINK_F_RECV_NO_ENOBUFS ? 1 : 0;
-		if (put_user(len, optlen) ||
-		    put_user(val, optval))
-			return -EFAULT;
-		err = 0;
 		break;
 	case NETLINK_LIST_MEMBERSHIPS: {
-		int pos, idx, shift;
+		int pos, idx, shift, err = 0;
 
-		err = 0;
 		netlink_lock_table();
 		for (pos = 0; pos * 8 < nlk->ngroups; pos += sizeof(u32)) {
 			if (len - pos < sizeof(u32))
@@ -1803,40 +1781,26 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
 		if (put_user(ALIGN(nlk->ngroups / 8, sizeof(u32)), optlen))
 			err = -EFAULT;
 		netlink_unlock_table();
-		break;
+		return err;
 	}
 	case NETLINK_CAP_ACK:
-		if (len < sizeof(int))
-			return -EINVAL;
-		len = sizeof(int);
 		val = nlk->flags & NETLINK_F_CAP_ACK ? 1 : 0;
-		if (put_user(len, optlen) ||
-		    put_user(val, optval))
-			return -EFAULT;
-		err = 0;
 		break;
 	case NETLINK_EXT_ACK:
-		if (len < sizeof(int))
-			return -EINVAL;
-		len = sizeof(int);
 		val = nlk->flags & NETLINK_F_EXT_ACK ? 1 : 0;
-		if (put_user(len, optlen) || put_user(val, optval))
-			return -EFAULT;
-		err = 0;
 		break;
 	case NETLINK_GET_STRICT_CHK:
-		if (len < sizeof(int))
-			return -EINVAL;
-		len = sizeof(int);
 		val = nlk->flags & NETLINK_F_STRICT_CHK ? 1 : 0;
-		if (put_user(len, optlen) || put_user(val, optval))
-			return -EFAULT;
-		err = 0;
 		break;
 	default:
-		err = -ENOPROTOOPT;
+		return -ENOPROTOOPT;
 	}
-	return err;
+
+	if (put_user(len, optlen) ||
+	    copy_to_user(optval, &val, len))
+		return -EFAULT;
+
+	return 0;
 }
 
 static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
Signature · 1 line
2.30.2
maintainer
#1 lore raw
Quoted 7 lines · On Tue, 18 Apr 2023 17:42:46 -0700 Kuniyuki Iwashima wrote:

Brad Spencer provided a detailed report that when calling getsockopt() for AF_NETLINK, some SOL_NETLINK options set only 1 byte even though such options require more than int as length.

The options return a flag value that fits into 1 byte, but such behaviour confuses users who do not strictly check the value as char.

Quoted 8 lines

Currently, netlink_getsockopt() uses put_user() to copy data to optlen and optval, but put_user() casts the data based on the pointer, char *optval. So, only 1 byte is set to optval.

To avoid this behaviour, we need to use copy_to_user() or cast optval for put_user().

Now getsockopt() accepts char as optval as the flags are only 1 byte.

I think it's worth doing, but it will change the return value on big endian, right?

#2 lore raw
From:   Jakub Kicinski <[email protected]>
Date:   Tue, 18 Apr 2023 20:33:18 -0700
Quoted 12 lines · On Tue, 18 Apr 2023 17:42:46 -0700 Kuniyuki Iwashima wrote:
Quoted 11 lines

Brad Spencer provided a detailed report that when calling getsockopt() for AF_NETLINK, some SOL_NETLINK options set only 1 byte even though such options require more than int as length.

The options return a flag value that fits into 1 byte, but such behaviour confuses users who do not strictly check the value as char.

Currently, netlink_getsockopt() uses put_user() to copy data to optlen and optval, but put_user() casts the data based on the pointer, char *optval. So, only 1 byte is set to optval.

Quoted 7 lines
Quoted 4 lines

To avoid this behaviour, we need to use copy_to_user() or cast optval for put_user().

Now getsockopt() accepts char as optval as the flags are only 1 byte.

I think it's worth doing, but it will change the return value on big endian, right?

Ah, I missed that point, yes. I hope big endian users always initialise the value to avoid the tricky behaviour.

#3 lore raw
In reply to Kuniyuki Iwashima
Quoted 6 lines · Kuniyuki Iwashima

Brad Spencer provided a detailed report that when calling getsockopt() for AF_NETLINK, some SOL_NETLINK options set only 1 byte even though such options require more than int as length.

The options return a flag value that fits into 1 byte, but such behaviour confuses users who do not strictly check the value as char.

Yeah that's iffy. I guess nobody really leaves it uninitialized.

Quoted 3 lines

Currently, netlink_getsockopt() uses put_user() to copy data to optlen and optval, but put_user() casts the data based on the pointer, char *optval. So, only 1 byte is set to optval.

Which also means it only ever sets to "1" on little endian systems, on big endian it would set to "0x0100'0000" (if initialized to 0 first), right?

Quoted 2 lines

To avoid this behaviour, we need to use copy_to_user() or cast optval for put_user().

Right.

Quoted 1 line

Now getsockopt() accepts char as optval as the flags are only 1 byte.

Personally, I don't think we should allow his. We document (*) and check this as taking an int, and while it would _fit_, I don't really think it's a good idea to weaken this and allow different types.
I don't see value in it either, certainly not now since nobody can be using it, and not really in the future either since you're not going to pack these things in memory, and having an int vs. char on the stack really makes basically no difference.
And when we start seeing code that actually uses this, it'll just be more things to support in the userspace API, be more confusing with socket options that aren't just flags, etc.

IOW, I think you should keep the size checks.

(*) man 7 netlink:
"Unless otherwise noted, optval is a pointer to an int."

net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1759

Quoted 7 lines
net/netlink/af_netlink.c +0 -3
@@ -1754,39 +1754,17 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
 
        switch (optname) {
        case NETLINK_PKTINFO:
-               if (len < sizeof(int))
-                       return -EINVAL;
-               len = sizeof(int);

On the other hand, this is actually accepting say a u64 now, and then sets only 4 bytes of it, though at least it also sets the size to what it wrote out.

So I guess here we can argue either
1) keep writing len to 4 and set 4 bytes of the output
2) keep the length as is and set all bytes of the output

but (2) gets confusing if you say used 6 bytes buffer as input? I mean, yeah, I'd really hope nobody does that.

If Jakub is feeling adventurous maybe we should attempt to see if we break anything by accepting only == sizeof(int) rather than >= ... :-)

net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1799

Quoted 1 line
net/netlink/af_netlink.c +1 -0
+       if (put_user(len, optlen) ||

You never change len now, so there's no point writing it back? Or do we somehow need to make sure this is writable? But what for?

net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1800

Quoted 1 line
net/netlink/af_netlink.c +1 -0
+           copy_to_user(optval, &val, len))

There's some magic in copy_to_user() now, but I don't think it will save you here - to me this seems really wrong now because 'len' is controlled by the user, and sizeof(val) is only 4 bytes - so wouldn't this overrun even in the case I mentioned above where the user used a u64 and 'len' is actually 8, not 4?

Also, as Jakub points out, even in the case where len *is* 4, you've now changed the behaviour on big endian.

I think that's probably *fine* since the bug meant you basically had to initialise to 0 and then check the entire value though, but maybe that warrants some discussion in the commit log.

So my 2 cents:
* I wouldn't remove the checks that the size is at least sizeof(int)
* I'd - even if it's not strictly backwards compatible - think about restricting to *exactly* sizeof(int), which would make the issue with the copy_to_user() go away as well (**)
* if we don't restrict the input length, then need to be much more careful about the copy_to_user() I think, but then what if someone specifies something really huge as the size?

(**) I only worry slightly that today somebody could've used an uninitialised value as the optlen and gotten away with it, but I hope that's not the case, that'd be a strange pattern, and if you ever hit 0 it fails anyway. I'm not really worried someone explicitly wanted to use a bigger buffer.

johannes

#4 lore raw
From:   Johannes Berg <[email protected]>
Date:   Wed, 19 Apr 2023 09:17:37 +0200
Quoted 10 lines · Kuniyuki Iwashima
Quoted 6 lines

Brad Spencer provided a detailed report that when calling getsockopt() for AF_NETLINK, some SOL_NETLINK options set only 1 byte even though such options require more than int as length.

The options return a flag value that fits into 1 byte, but such behaviour confuses users who do not strictly check the value as char.

Yeah that's iffy. I guess nobody really leaves it uninitialized.

Quoted 7 lines
Quoted 3 lines

Currently, netlink_getsockopt() uses put_user() to copy data to optlen and optval, but put_user() casts the data based on the pointer, char *optval. So, only 1 byte is set to optval.

Which also means it only ever sets to "1" on little endian systems, on big endian it would set to "0x0100'0000" (if initialized to 0 first), right?

Yes.

Quoted 19 lines
Quoted 3 lines

To avoid this behaviour, we need to use copy_to_user() or cast optval for put_user().

Right.

Quoted 1 line

Now getsockopt() accepts char as optval as the flags are only 1 byte.

Personally, I don't think we should allow his. We document (*) and check this as taking an int, and while it would _fit_, I don't really think it's a good idea to weaken this and allow different types.
I don't see value in it either, certainly not now since nobody can be using it, and not really in the future either since you're not going to pack these things in memory, and having an int vs. char on the stack really makes basically no difference.
And when we start seeing code that actually uses this, it'll just be more things to support in the userspace API, be more confusing with socket options that aren't just flags, etc.

Quoted 5 lines

IOW, I think you should keep the size checks.

(*) man 7 netlink:
"Unless otherwise noted, optval is a pointer to an int."

Oh, I didn't know it's documented.

I tried to follow other SOL_XXX handlers, for example, we can get SO_REUSEADDR as char. But I'm fine to keep the size check.

Quoted 18 lines

net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1759

Quoted 9 lines
net/netlink/af_netlink.c +0 -3
@@ -1754,39 +1754,17 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
 
        switch (optname) {
        case NETLINK_PKTINFO:
-               if (len < sizeof(int))
-                       return -EINVAL;
-               len = sizeof(int);

On the other hand, this is actually accepting say a u64 now, and then sets only 4 bytes of it, though at least it also sets the size to what it wrote out.

So I guess here we can argue either
1) keep writing len to 4 and set 4 bytes of the output
2) keep the length as is and set all bytes of the output

Quoted 5 lines

but (2) gets confusing if you say used 6 bytes buffer as input? I mean, yeah, I'd really hope nobody does that.

If Jakub is feeling adventurous maybe we should attempt to see if we break anything by accepting only == sizeof(int) rather than >= ... :-)

Yes, this is another thing I concerned. I thought we would have the same report if we didn't clear the high 32 bits when a user passed u64.

If we want to avoid it, we have to use u64 as val in netlink_getsockopt(). This is even broken for a strange user who passes u128 though :P

Quoted 7 lines

net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1799

Quoted 3 lines
net/netlink/af_netlink.c +1 -0
+       if (put_user(len, optlen) ||

You never change len now, so there's no point writing it back? Or do we somehow need to make sure this is writable? But what for?

Quoted 7 lines

net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1800

Quoted 1 line
net/netlink/af_netlink.c +1 -0
+           copy_to_user(optval, &val, len))

There's some magic in copy_to_user() now, but I don't think it will save you here - to me this seems really wrong now because 'len' is controlled by the user, and sizeof(val) is only 4 bytes - so wouldn't this overrun even in the case I mentioned above where the user used a u64 and 'len' is actually 8, not 4?

You are right. I seem to be confused to try to accept char ~ u64 :/ Yes, at least we have to set upper bound for len based on val's actual size as we do in sk_getsockopt().

Quoted 7 lines

Also, as Jakub points out, even in the case where len *is* 4, you've now changed the behaviour on big endian.

I think that's probably *fine* since the bug meant you basically had to initialise to 0 and then check the entire value though, but maybe that warrants some discussion in the commit log.

Agreed.

Quoted 9 lines

So my 2 cents:
* I wouldn't remove the checks that the size is at least sizeof(int)
* I'd - even if it's not strictly backwards compatible - think about restricting to *exactly* sizeof(int), which would make the issue with the copy_to_user() go away as well (**)
* if we don't restrict the input length, then need to be much more careful about the copy_to_user() I think, but then what if someone specifies something really huge as the size?

I'm fine either, but I would prefer the latter using u64 for val and set limit for len as sizeof(u64).

Quoted 10 lines

(**) I only worry slightly that today somebody could've used an uninitialised value as the optlen and gotten away with it, but I hope that's not the case, that'd be a strange pattern, and if you ever hit 0 it fails anyway. I'm not really worried someone explicitly wanted to use a bigger buffer.

johannes

#5 lore raw
Quoted 7 lines · Kuniyuki Iwashima
Quoted 7 lines

So I guess here we can argue either
1) keep writing len to 4 and set 4 bytes of the output
2) keep the length as is and set all bytes of the output

but (2) gets confusing if you say used 6 bytes buffer as input? I mean, yeah, I'd really hope nobody does that.

Quoted 8 lines
Quoted 2 lines

If Jakub is feeling adventurous maybe we should attempt to see if we break anything by accepting only == sizeof(int) rather than >= ... :-)

Yes, this is another thing I concerned. I thought we would have the same report if we didn't clear the high 32 bits when a user passed u64.

If we want to avoid it, we have to use u64 as val in netlink_getsockopt(). This is even broken for a strange user who passes u128 though :P

Yeah ... hence I'm saying we shouldn't bother.

Quoted 9 lines
Quoted 8 lines

So my 2 cents:
* I wouldn't remove the checks that the size is at least sizeof(int)
* I'd - even if it's not strictly backwards compatible - think about restricting to *exactly* sizeof(int), which would make the issue with the copy_to_user() go away as well (**)
* if we don't restrict the input length, then need to be much more careful about the copy_to_user() I think, but then what if someone specifies something really huge as the size?

Quoted 3 lines

I'm fine either, but I would prefer the latter using u64 for val and set limit for len as sizeof(u64).

That doesn't actually work on big endian, if you have a u64 value 1 that's 00 00 00 00 00 00 00 01, now if you just copy_to_user() that to an int value (that should've been used) you only get 00 00 00 00 out ... I guess with the semantics we could technically set it to ff ... ff, but then there's probably _someone_ out there who expects only 0 or 1 in an int, or something? So that means to allow a value larger than int (smaller isn't allowed now) you'd actually have to write some additional tricky code that checks what the size is and writes it accordingly ... or just like now writes only the 4 bytes out and sets optlen, but I suspects that really only works today anyway because everyone uses an int as documented (and smaller won't work).

So I still think it's better to at least try to just say it _has_ to be exactly an int, and write that out - and failing that, keep the behaviour we have today, but hopefully that won't be needed.

johannes

#6 lore raw
Quoted 10 lines · Johannes Berg
Quoted 10 lines
Quoted 9 lines

So my 2 cents:
* I wouldn't remove the checks that the size is at least sizeof(int)
* I'd - even if it's not strictly backwards compatible - think about restricting to *exactly* sizeof(int), which would make the issue with the copy_to_user() go away as well (**)
* if we don't restrict the input length, then need to be much more careful about the copy_to_user() I think, but then what if someone specifies something really huge as the size?

Quoted 5 lines
Quoted 3 lines

I'm fine either, but I would prefer the latter using u64 for val and set limit for len as sizeof(u64).

That doesn't actually work on big endian,

[snip]

and come to think of it, that also makes the code in your patch now only work for 'char' or 'short' on little endian, which is again another tricky gotcha, and also there another argument for not allowing that?

johannes

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw

From: Jakub Kicinski <[email protected]>
Date: Wed, 19 Apr 2023 16:09:08 -0700

Quoted 17 lines · On Wed, 19 Apr 2023 09:17:37 +0200 Johannes Berg wrote:
Quoted 16 lines

net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1759

Quoted 7 lines
net/netlink/af_netlink.c +0 -3
@@ -1754,39 +1754,17 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
 
        switch (optname) {
        case NETLINK_PKTINFO:
-               if (len < sizeof(int))
-                       return -EINVAL;
-               len = sizeof(int);

On the other hand, this is actually accepting say a u64 now, and then sets only 4 bytes of it, though at least it also sets the size to what it wrote out.

So I guess here we can argue either
1) keep writing len to 4 and set 4 bytes of the output
2) keep the length as is and set all bytes of the output

Quoted 8 lines
Quoted 5 lines

but (2) gets confusing if you say used 6 bytes buffer as input? I mean, yeah, I'd really hope nobody does that.

If Jakub is feeling adventurous maybe we should attempt to see if we break anything by accepting only == sizeof(int) rather than >= ... :-)

Can't think of a strong reason either way, so I'd keep the check and len setting as is.

Ok, I'll respin v2 with the existing check and len setting.

Thank you, Johannes and Jakub!

#9 lore raw
In reply to a message not in the archive
Quoted 17 lines · Jakub Kicinski
Quoted 16 lines · On Wed, 19 Apr 2023 09:17:37 +0200 Johannes Berg wrote:

net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1759

Quoted 7 lines
net/netlink/af_netlink.c +0 -3
@@ -1754,39 +1754,17 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
 
        switch (optname) {
        case NETLINK_PKTINFO:
-               if (len < sizeof(int))
-                       return -EINVAL;
-               len = sizeof(int);  

On the other hand, this is actually accepting say a u64 now, and then sets only 4 bytes of it, though at least it also sets the size to what it wrote out.

So I guess here we can argue either
1) keep writing len to 4 and set 4 bytes of the output
2) keep the length as is and set all bytes of the output

Quoted 8 lines
Quoted 5 lines

but (2) gets confusing if you say used 6 bytes buffer as input? I mean, yeah, I'd really hope nobody does that.

If Jakub is feeling adventurous maybe we should attempt to see if we break anything by accepting only == sizeof(int) rather than >= ... :-)

Can't think of a strong reason either way, so I'd keep the check and len setting as is.

Yeah, fair. The only reason really would be to make it very clear that using something bigger than an int isn't going to work right.

johannes