netlink: Use copy_to_user() for optval in netlink_getsockopt().
Subsystems (2)
PATCH v3 1/1 netlink: Use copy_to_user() for optval in netlink_getsockopt().
Brad Spencer provided a detailed report [0] that when calling getsockopt() for AF_NETLINK, some SOL_NETLINK options set only 1 byte even though such options require at least sizeof(int) as length.
The options return a flag value that fits into 1 byte, but such behaviour confuses users who do not initialise the variable before calling getsockopt() and do not strictly check the returned 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. As a result, only 1 byte is set to optval.
To avoid this behaviour, we need to use copy_to_user() or cast optval for put_user().
Note that this changes the behaviour on big-endian systems, but we document that the size of optval is int in the man page.
$ man 7 netlink
...
Socket options
To set or get a netlink socket option, call getsockopt(2) to read
or setsockopt(2) to write the option with the option level argument
set to SOL_NETLINK. Unless otherwise noted, optval is a pointer to
an int.---
v3:
* Keep errno for unknown opts if len < sizeof(int)
* Update changelog
v2: https://lore.kernel.org/netdev/[email protected]/
* Keep the len check and setting
* Update changelog
v1: https://lore.kernel.org/netdev/[email protected]/
--- net/netlink/af_netlink.c | 75 ++++++++++++---------------------------- 1 file changed, 23 insertions(+), 52 deletions(-)
net/netlink/af_netlink.c +23 -52
@@ -1742,7 +1742,8 @@ 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; +unsigned int flag; +int len, val;if (level !=SOL_NETLINK )return -ENOPROTOOPT ;
@@ -1754,39 +1755,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 ; + flag =NETLINK_F_RECV_PKTINFO ;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 ; + flag =NETLINK_F_BROADCAST_SEND_ERROR ;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 ; + flag =NETLINK_F_RECV_NO_ENOBUFS ;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 +1782,32 @@ 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 ; + flag =NETLINK_F_CAP_ACK ;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 ; + flag =NETLINK_F_EXT_ACK ;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 ; + flag =NETLINK_F_STRICT_CHK ;break ;default : - err = -ENOPROTOOPT ; +return -ENOPROTOOPT ; } -return err; + +if (len <sizeof (int )) +return -EINVAL ; + + len =sizeof (int ); + val = nlk->flags & flag ?1 :0 ; + +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
net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1746
Quoted 11 lines · Kuniyuki Iwashima
net/netlink/af_netlink.c +2
-1@@ -1742,7 +1742,8 @@ 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; +unsigned int flag; +int len, val;
len is not initialized here ...
net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1759
Quoted 10 lines
net/netlink/af_netlink.c +0
-3if (level !=SOL_NETLINK )return -ENOPROTOOPT ;@@ -1754,39 +1755,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 );
you remove all of these ...
Quoted 72 lines
net/netlink/af_netlink.c +7
-45- val = nlk->flags &NETLINK_F_RECV_PKTINFO ?1 :0 ; -if (put_user (len, optlen) || -put_user (val, optval)) -return -EFAULT ; - err =0 ; + flag =NETLINK_F_RECV_PKTINFO ;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 ; + flag =NETLINK_F_BROADCAST_SEND_ERROR ;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 ; + flag =NETLINK_F_RECV_NO_ENOBUFS ;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 +1782,32 @@ 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 ; + flag =NETLINK_F_CAP_ACK ;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 ; + flag =NETLINK_F_EXT_ACK ;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 ;
net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1800
Quoted 10 lines
net/netlink/af_netlink.c +4
-3- err =0 ; + flag =NETLINK_F_STRICT_CHK ;break ;default : - err = -ENOPROTOOPT ; +return -ENOPROTOOPT ; } -return err; + +if (len <sizeof (int ))
and then check len here.
net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1801
Quoted 1 line
net/netlink/af_netlink.c +1
-0+return -EINVAL ;
seems like this chunk (len < sizeof(int)) is not needed.
Quoted 12 lines
net/netlink/af_netlink.c +9
-0+ + len =sizeof (int ); + val = nlk->flags & flag ?1 :0 ; + +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)
Quoted 16 lines · David Ahern
net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1746
Quoted 11 lines · Kuniyuki Iwashima
net/netlink/af_netlink.c +2
-1@@ -1742,7 +1742,8 @@ 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; +unsigned int flag; +int len, val;len is not initialized here ...
Quoted 1 line
Quoted 5 lines
net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1756
Quoted 5 lines
if (level != SOL_NETLINK) return -ENOPROTOOPT;net/netlink/af_netlink.c +0
-0@@ -1754,39 +1755,17 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,switch (optname) {
It's initialized in the context here:
if (get_user(len, optlen))
return -EFAULT;
if (len < 0)
return -EINVAL;Quoted 9 lines
net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1800
Quoted 7 lines
net/netlink/af_netlink.c +3
-2default : - err = -ENOPROTOOPT ; +return -ENOPROTOOPT ; } -return err; + +if (len <sizeof (int ))and then check len here.
so that's OK. Note how the checks that were there before also were before the len assignments :)
Quoted 3 lines
net/netlink/af_netlink.c static int netlink_getsockopt(struct socket *sock, int level, int optname, · line 1801
Quoted 1 line
net/netlink/af_netlink.c +1
-0+return -EINVAL ;seems like this chunk (len < sizeof(int)) is not needed.
It is, if we get here we need at least the int size for the flags.
johannes
Quoted 26 lines · Kuniyuki Iwashima
Brad Spencer provided a detailed report [0] that when calling
getsockopt()forAF_NETLINK, someSOL_NETLINKoptions set only 1 byte even though such options require at least sizeof(int) as length.The options return a flag value that fits into 1 byte, but such behaviour confuses users who do not initialise the variable before calling
getsockopt()and do not strictly check the returned value as char.Currently,
netlink_getsockopt()usesput_user()to copy data to optlen and optval, butput_user()casts the data based on the pointer, char *optval. As a result, only 1 byte is set to optval.To avoid this behaviour, we need to use
copy_to_user()or cast optval forput_user().Note that this changes the behaviour on big-endian systems, but we document that the size of optval is int in the man page.
$ man 7 netlink ... Socket options To set or get a netlink socket option, call getsockopt(2) to read or setsockopt(2) to write the option with the option level argument set to SOL_NETLINK. Unless otherwise noted, optval is a pointer to an int.
Quoted 9 lines
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")Reported-by Brad Spencer <[email protected]>Signed-off-by Kuniyuki Iwashima <[email protected]>
Thanks!
johannes
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Fri, 21 Apr 2023 11:52:55 -0700 you wrote:
Brad Spencer provided a detailed report [0] that when calling
getsockopt()forAF_NETLINK, someSOL_NETLINKoptions set only 1 byte even though such options require at least sizeof(int) as length.The options return a flag value that fits into 1 byte, but such behaviour confuses users who do not initialise the variable before calling
getsockopt()and do not strictly check the returned value as char.[...]
Here is the summary with links:
- [v3,net] netlink: Use copy_to_user() for optval in netlink_getsockopt().
https://git.kernel.org/netdev/net/c/d913d32cc270
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html