net: Ensure net namespace isolation of sysctls
authorJonathon Reinhart <jonathon.reinhart@gmail.com>
Mon, 12 Apr 2021 04:24:52 +0000 (00:24 -0400)
committerDavid S. Miller <davem@davemloft.net>
Mon, 12 Apr 2021 20:27:11 +0000 (13:27 -0700)
commit31c4d2f160eb7b17cbead24dc6efed06505a3fee
treed95a8359a40bbc2151f3d2eacbb15e21f1c1f81f
parenta115d24a636e892ddd1ae58f8e23c78a0390cb68
net: Ensure net namespace isolation of sysctls

This adds an ensure_safe_net_sysctl() check during register_net_sysctl()
to validate that sysctl table entries for a non-init_net netns are
sufficiently isolated. To be netns-safe, an entry must adhere to at
least (and usually exactly) one of these rules:

1. It is marked read-only inside the netns.
2. Its data pointer does not point to kernel/module global data.

An entry which fails both of these checks is indicative of a bug,
whereby a child netns can affect global net sysctl values.

If such an entry is found, this code will issue a warning to the kernel
log, and force the entry to be read-only to prevent a leak.

To test, simply create a new netns:

    $ sudo ip netns add dummy

As it sits now, this patch will WARN for two sysctls which will be
addressed in a subsequent patch:
- /proc/sys/net/netfilter/nf_conntrack_max
- /proc/sys/net/netfilter/nf_conntrack_expect_max

Signed-off-by: Jonathon Reinhart <Jonathon.Reinhart@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/sysctl_net.c