Commit 0c9f79be authored by Xi Wang's avatar Xi Wang Committed by David S. Miller

ipv4: avoid undefined behavior in do_ip_setsockopt()

(1<<optname) is undefined behavior in C with a negative optname or
optname larger than 31.  In those cases the result of the shift is
not necessarily zero (e.g., on x86).

This patch simplifies the code with a switch statement on optname.
It also allows the compiler to generate better code (e.g., using a
64-bit mask).
Signed-off-by: default avatarXi Wang <xi.wang@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 77b67063
...@@ -457,19 +457,28 @@ static int do_ip_setsockopt(struct sock *sk, int level, ...@@ -457,19 +457,28 @@ static int do_ip_setsockopt(struct sock *sk, int level,
struct inet_sock *inet = inet_sk(sk); struct inet_sock *inet = inet_sk(sk);
int val = 0, err; int val = 0, err;
if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) | switch (optname) {
(1<<IP_RECVOPTS) | (1<<IP_RECVTOS) | case IP_PKTINFO:
(1<<IP_RETOPTS) | (1<<IP_TOS) | case IP_RECVTTL:
(1<<IP_TTL) | (1<<IP_HDRINCL) | case IP_RECVOPTS:
(1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) | case IP_RECVTOS:
(1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) | case IP_RETOPTS:
(1<<IP_PASSSEC) | (1<<IP_TRANSPARENT) | case IP_TOS:
(1<<IP_MINTTL) | (1<<IP_NODEFRAG))) || case IP_TTL:
optname == IP_UNICAST_IF || case IP_HDRINCL:
optname == IP_MULTICAST_TTL || case IP_MTU_DISCOVER:
optname == IP_MULTICAST_ALL || case IP_RECVERR:
optname == IP_MULTICAST_LOOP || case IP_ROUTER_ALERT:
optname == IP_RECVORIGDSTADDR) { case IP_FREEBIND:
case IP_PASSSEC:
case IP_TRANSPARENT:
case IP_MINTTL:
case IP_NODEFRAG:
case IP_UNICAST_IF:
case IP_MULTICAST_TTL:
case IP_MULTICAST_ALL:
case IP_MULTICAST_LOOP:
case IP_RECVORIGDSTADDR:
if (optlen >= sizeof(int)) { if (optlen >= sizeof(int)) {
if (get_user(val, (int __user *) optval)) if (get_user(val, (int __user *) optval))
return -EFAULT; return -EFAULT;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment