]> git.baikalelectronics.ru Git - kernel.git/commit
netlink: Use copy_to_user() for optval in netlink_getsockopt().
authorKuniyuki Iwashima <kuniyu@amazon.com>
Fri, 21 Apr 2023 18:52:55 +0000 (11:52 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 17 May 2023 09:35:45 +0000 (11:35 +0200)
commit4561ec67927e42990626754cfc57e588e28a3e55
tree2684b77f50fb7490e0faba653656be556c864675
parentc7207edffc133fb25c47c3e2091095dd7a5db578
netlink: Use copy_to_user() for optval in netlink_getsockopt().

[ Upstream commit d913d32cc2707e9cd24fe6fa6d7d470e9c728980 ]

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.

Fixes: 30ce92a49239 ("[NETLINK]: Add set/getsockopt options to support more than 32 groups")
Fixes: 012f87f243c6 ("netlink: add NETLINK_BROADCAST_ERROR socket option")
Fixes: 269c23d3dc06 ("netlink: add NETLINK_NO_ENOBUFS socket flag")
Fixes: 1684d193f068 ("netlink: add NETLINK_CAP_ACK socket option")
Fixes: 5601c9fcd324 ("netlink: extended ACK reporting")
Fixes: 1c623d1863a2 ("netlink: Add new socket option to enable strict checking on dumps")
Reported-by: Brad Spencer <bspencer@blackberry.com>
Link: https://lore.kernel.org/netdev/ZD7VkNWFfp22kTDt@datsun.rim.net/
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Reviewed-by: Johannes Berg <johannes@sipsolutions.net>
Link: https://lore.kernel.org/r/20230421185255.94606-1-kuniyu@amazon.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
net/netlink/af_netlink.c