Skip to content

Commit 645b34a

Browse files
committed
Merge branch 'netns-sysctl-isolation'
Jonathon Reinhart says: ==================== Ensuring net sysctl isolation This patchset is the result of an audit of /proc/sys/net to prove that it is safe to be mouted read-write in a container when a net namespace is in use. See [1]. The first commit adds code to detect sysctls which are not netns-safe, and can "leak" changes to other net namespaces. My manual audit found, and the above feature confirmed, that there are two nf_conntrack sysctls which are in fact not netns-safe. I considered sending the latter to netfilter-devel, but I think it's better to have both together on net-next: Adding only the former causes undesirable warnings in the kernel log. [1]: opencontainers/runc#2826 ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents a115d24 + 2671fa4 commit 645b34a

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

net/netfilter/nf_conntrack_standalone.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,16 +1060,10 @@ static int nf_conntrack_standalone_init_sysctl(struct net *net)
10601060
nf_conntrack_standalone_init_dccp_sysctl(net, table);
10611061
nf_conntrack_standalone_init_gre_sysctl(net, table);
10621062

1063-
/* Don't allow unprivileged users to alter certain sysctls */
1064-
if (net->user_ns != &init_user_ns) {
1063+
/* Don't allow non-init_net ns to alter global sysctls */
1064+
if (!net_eq(&init_net, net)) {
10651065
table[NF_SYSCTL_CT_MAX].mode = 0444;
10661066
table[NF_SYSCTL_CT_EXPECT_MAX].mode = 0444;
1067-
table[NF_SYSCTL_CT_HELPER].mode = 0444;
1068-
#ifdef CONFIG_NF_CONNTRACK_EVENTS
1069-
table[NF_SYSCTL_CT_EVENTS].mode = 0444;
1070-
#endif
1071-
table[NF_SYSCTL_CT_BUCKETS].mode = 0444;
1072-
} else if (!net_eq(&init_net, net)) {
10731067
table[NF_SYSCTL_CT_BUCKETS].mode = 0444;
10741068
}
10751069

net/sysctl_net.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,57 @@ __init int net_sysctl_init(void)
115115
goto out;
116116
}
117117

118+
/* Verify that sysctls for non-init netns are safe by either:
119+
* 1) being read-only, or
120+
* 2) having a data pointer which points outside of the global kernel/module
121+
* data segment, and rather into the heap where a per-net object was
122+
* allocated.
123+
*/
124+
static void ensure_safe_net_sysctl(struct net *net, const char *path,
125+
struct ctl_table *table)
126+
{
127+
struct ctl_table *ent;
128+
129+
pr_debug("Registering net sysctl (net %p): %s\n", net, path);
130+
for (ent = table; ent->procname; ent++) {
131+
unsigned long addr;
132+
const char *where;
133+
134+
pr_debug(" procname=%s mode=%o proc_handler=%ps data=%p\n",
135+
ent->procname, ent->mode, ent->proc_handler, ent->data);
136+
137+
/* If it's not writable inside the netns, then it can't hurt. */
138+
if ((ent->mode & 0222) == 0) {
139+
pr_debug(" Not writable by anyone\n");
140+
continue;
141+
}
142+
143+
/* Where does data point? */
144+
addr = (unsigned long)ent->data;
145+
if (is_module_address(addr))
146+
where = "module";
147+
else if (core_kernel_data(addr))
148+
where = "kernel";
149+
else
150+
continue;
151+
152+
/* If it is writable and points to kernel/module global
153+
* data, then it's probably a netns leak.
154+
*/
155+
WARN(1, "sysctl %s/%s: data points to %s global data: %ps\n",
156+
path, ent->procname, where, ent->data);
157+
158+
/* Make it "safe" by dropping writable perms */
159+
ent->mode &= ~0222;
160+
}
161+
}
162+
118163
struct ctl_table_header *register_net_sysctl(struct net *net,
119164
const char *path, struct ctl_table *table)
120165
{
166+
if (!net_eq(net, &init_net))
167+
ensure_safe_net_sysctl(net, path, table);
168+
121169
return __register_sysctl_table(&net->sysctls, path, table);
122170
}
123171
EXPORT_SYMBOL_GPL(register_net_sysctl);

0 commit comments

Comments
 (0)