Skip to content

Commit

Permalink
net: do not allow changing SO_REUSEADDR/SO_REUSEPORT on bound sockets
Browse files Browse the repository at this point in the history
It is not safe to do so because such sockets are already in the
hash tables and changing these options can result in invalidating
the tb->fastreuse(port) caching.

This can have later far reaching consequences wrt. bind conflict checks
which rely on these caches (for optimization purposes).

Not to mention that you can currently end up with two identical
non-reuseport listening sockets bound to the same local ip:port
by clearing reuseport on them after they've already both been bound.

There is unfortunately no EISBOUND error or anything similar,
and EISCONN seems to be misleading for a bound-but-not-connected
socket, so use EUCLEAN 'Structure needs cleaning' which AFAICT
is the closest you can get to meaning 'socket in bad state'.
(although perhaps EINVAL wouldn't be a bad choice either?)

This does unfortunately run the risk of breaking buggy
userspace programs...

Signed-off-by: Maciej Żenczykowski <[email protected]>
Cc: Eric Dumazet <[email protected]>
Change-Id: I77c2b3429b2fdf42671eee0fa7a8ba721c94963b
Reviewed-by: Eric Dumazet <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
zenczykowski authored and davem330 committed Jun 4, 2018
1 parent 79e9fed commit f396922
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion net/core/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,22 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
sock_valbool_flag(sk, SOCK_DBG, valbool);
break;
case SO_REUSEADDR:
sk->sk_reuse = (valbool ? SK_CAN_REUSE : SK_NO_REUSE);
val = (valbool ? SK_CAN_REUSE : SK_NO_REUSE);
if ((sk->sk_family == PF_INET || sk->sk_family == PF_INET6) &&
inet_sk(sk)->inet_num &&
(sk->sk_reuse != val)) {
ret = (sk->sk_state == TCP_ESTABLISHED) ? -EISCONN : -EUCLEAN;
break;
}
sk->sk_reuse = val;
break;
case SO_REUSEPORT:
if ((sk->sk_family == PF_INET || sk->sk_family == PF_INET6) &&
inet_sk(sk)->inet_num &&
(sk->sk_reuseport != valbool)) {
ret = (sk->sk_state == TCP_ESTABLISHED) ? -EISCONN : -EUCLEAN;
break;
}
sk->sk_reuseport = valbool;
break;
case SO_TYPE:
Expand Down

0 comments on commit f396922

Please sign in to comment.