Skip to contentSkip to conversation

Expose x_tables /proc entries as 0444 not 0440

PATCH In review Philip Whineray 6 participants 19 messages Nov 7, 2015 – Nov 25, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Expose x_tables /proc entries as 0444 not 0440

Reading these files is impossible in an unprivileged user namespace, interfering with various firewall tools. For instance, iptables-save relies on reading /proc/net/ip_tables_names to dump only loaded tables.

Hiding the contents from non-root users does not achieve anything practical. Possible values are well-known and the specifics can be inferred from a list of loaded modules on most systems.

---
An alternate might be to change the ownership of the files within the namespace when it is created:

https://lists.linuxcontainers.org/pipermail/lxc-users/2014-November/008110.html

I do not see that there is much advantage to this, it just ties the ability to read the files to the ability to create an unprivileged namespace.

 net/netfilter/x_tables.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/netfilter/x_tables.c +3 -3
index 9b42b5e..c05adde 100644
@@ -1236,21 +1236,21 @@ int xt_proto_init(struct net *net, u_int8_t af)
 #ifdef CONFIG_PROC_FS
 	strlcpy(buf, xt_prefix[af], sizeof(buf));
 	strlcat(buf, FORMAT_TABLES, sizeof(buf));
-	proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
+	proc = proc_create_data(buf, 0444, net->proc_net, &xt_table_ops,
 				(void *)(unsigned long)af);
 	if (!proc)
 		goto out;
 
 	strlcpy(buf, xt_prefix[af], sizeof(buf));
 	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
-	proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
+	proc = proc_create_data(buf, 0444, net->proc_net, &xt_match_ops,
 				(void *)(unsigned long)af);
 	if (!proc)
 		goto out_remove_tables;
 
 	strlcpy(buf, xt_prefix[af], sizeof(buf));
 	strlcat(buf, FORMAT_TARGETS, sizeof(buf));
-	proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
+	proc = proc_create_data(buf, 0444, net->proc_net, &xt_target_ops,
 				(void *)(unsigned long)af);
 	if (!proc)
 		goto out_remove_matches;
Signature · 1 line
2.1.4
#1 lore raw
Quoted 13 lines · Philip Whineray

Reading these files is impossible in an unprivileged user namespace, interfering with various firewall tools. For instance, iptables-save relies on reading /proc/net/ip_tables_names to dump only loaded tables.

Hiding the contents from non-root users does not achieve anything practical. Possible values are well-known and the specifics can be inferred from a list of loaded modules on most systems.

---
An alternate might be to change the ownership of the files within the namespace when it is created:

Quoted 5 lines

https://lists.linuxcontainers.org/pipermail/lxc-users/2014-November/008110.html

I do not see that there is much advantage to this, it just ties the ability to read the files to the ability to create an unprivileged namespace.

So I understood this correctly, this approach would set the ownership of the /proc entry to the corresponding root uid mapping from the unpriviledged namespace, right? If so, I would prefer that approach.

This is partially leaking the filtering policy to non-root users as it contains what modules are being used, so you can at least infer how complex your ruleset is.

And I guess it will not be long time until someone else will follow up with a similar patch later on to expose the content of /proc/net/nf_conntrack to get this working on unpriviledged namespaces too.

Thanks.

#2 lore raw
Quoted 24 lines · Pablo Neira Ayuso
Quoted 13 lines · Philip Whineray

Reading these files is impossible in an unprivileged user namespace, interfering with various firewall tools. For instance, iptables-save relies on reading /proc/net/ip_tables_names to dump only loaded tables.

Hiding the contents from non-root users does not achieve anything practical. Possible values are well-known and the specifics can be inferred from a list of loaded modules on most systems.

---
An alternate might be to change the ownership of the files within the namespace when it is created:

Quoted 5 lines

https://lists.linuxcontainers.org/pipermail/lxc-users/2014-November/008110.html

I do not see that there is much advantage to this, it just ties the ability to read the files to the ability to create an unprivileged namespace.

So I understood this correctly, this approach would set the ownership of the /proc entry to the corresponding root uid mapping from the unpriviledged namespace, right? If so, I would prefer that approach.

Quoted 8 lines

This is partially leaking the filtering policy to non-root users as it contains what modules are being used, so you can at least infer how complex your ruleset is.

And I guess it will not be long time until someone else will follow up with a similar patch later on to expose the content of /proc/net/nf_conntrack to get this working on unpriviledged namespaces too.

Can't it be worked out by using CAP_NET_ADMIN? I don't really like the idea to expose the files to non root users or users with not sufficient capabilities.

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : Wigner Research Centre for Physics, Hungarian Academy of Sciences

          H-1525 Budapest 114, POB. 49, Hungary
#4 lore raw
Quoted 4 lines · Florian Westphal
Quoted 3 lines · Pablo Neira Ayuso
Quoted 3 lines

Hiding the contents from non-root users does not achieve anything practical. Possible values are well-known and the specifics can be inferred from a list of loaded modules on most systems.

Conversely, an administrator could just load all modules to give a false impression. Since the adversary can in turn expect it, he knows as little as before. In particular, containerized environments will have it such that many modules are loaded, but each container still has their own ruleset.
So yeah, hiding the contents is not going to achieve anything - nor is showing. (I am concurring here with the other respondents.)

#5 lore raw
Quoted 6 lines · Jan Engelhardt
Quoted 4 lines · Florian Westphal
Quoted 3 lines · Pablo Neira Ayuso
Quoted 3 lines

Hiding the contents from non-root users does not achieve anything practical. Possible values are well-known and the specifics can be inferred from a list of loaded modules on most systems.

Quoted 7 lines

Conversely, an administrator could just load all modules to give a false impression. Since the adversary can in turn expect it, he knows as little as before. In particular, containerized environments will have it such that many modules are loaded, but each container still has their own ruleset.
So yeah, hiding the contents is not going to achieve anything - nor is showing. (I am concurring here with the other respondents.)

Sorry - I've gotten confused about who thinks what exactly. I hope the below isn't wasting everyone's time.

Unhiding /proc/ip_tables_names content achieves something specific: it is used by iptables-save to determine what it should write out (in the absence of a specific table being asked for, which I believe is the norm).

I currently have a workaround using a horrific series of bind mounts which substitutes in a normal file with the values iptables-save expects.

Two alternates on the table are:

- change ownership of the file in the namespace (wherein a user runs "unshare -U -r -n cat /proc/net/ip_tables_names" to do the same as "cat /proc/net/ip_tables_names" with 0444 perms, so not adding much unless unprivileged namespaces are disabled.

- try to do something with capabilities (I'd need to research this)

Given /proc/modules is 0444 and the observation that loaded != used I don't think any significant information is leaked by the patch. I don't mind having a stab at an alternate implementation but is it worth it?

#6 lore raw
Quoted 15 lines · Phil Whineray
Quoted 6 lines · Jan Engelhardt
Quoted 4 lines · Florian Westphal
Quoted 3 lines · Pablo Neira Ayuso
Quoted 3 lines

Hiding the contents from non-root users does not achieve anything practical. Possible values are well-known and the specifics can be inferred from a list of loaded modules on most systems.

Quoted 7 lines

Conversely, an administrator could just load all modules to give a false impression. Since the adversary can in turn expect it, he knows as little as before. In particular, containerized environments will have it such that many modules are loaded, but each container still has their own ruleset.
So yeah, hiding the contents is not going to achieve anything - nor is showing. (I am concurring here with the other respondents.)

Quoted 2 lines

Sorry - I've gotten confused about who thinks what exactly. I hope the below isn't wasting everyone's time.

My opinion is that the files should not be exposed. Whenever it does not clash something vital and cannot be resolved I run kernels with grsec, where normal user cannot list the loaded in kernel modules. By exposing these files, some part of that data would then be leaked out.

Quoted 8 lines

Unhiding /proc/ip_tables_names content achieves something specific: it is used by iptables-save to determine what it should write out (in the absence of a specific table being asked for, which I believe is the norm).

I currently have a workaround using a horrific series of bind mounts which substitutes in a normal file with the values iptables-save expects.

Quoted 6 lines

Two alternates on the table are:

- change ownership of the file in the namespace (wherein a user runs "unshare -U -r -n cat /proc/net/ip_tables_names" to do the same as "cat /proc/net/ip_tables_names" with 0444 perms, so not adding much unless unprivileged namespaces are disabled.

I don't quite understand this: if the ownership of the files are changed, then why do you need to change the access right to 0444?

Quoted 6 lines

- try to do something with capabilities (I'd need to research this)

Given /proc/modules is 0444 and the observation that loaded != used I don't think any significant information is leaked by the patch. I don't mind having a stab at an alternate implementation but is it worth it?

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : Wigner Research Centre for Physics, Hungarian Academy of Sciences

          H-1525 Budapest 114, POB. 49, Hungary
#7 lore raw
Quoted 35 lines · Jozsef Kadlecsik
Quoted 15 lines · Phil Whineray
Quoted 6 lines · Jan Engelhardt
Quoted 4 lines · Florian Westphal
Quoted 3 lines · Pablo Neira Ayuso
Quoted 3 lines

Hiding the contents from non-root users does not achieve anything practical. Possible values are well-known and the specifics can be inferred from a list of loaded modules on most systems.

Quoted 7 lines

Conversely, an administrator could just load all modules to give a false impression. Since the adversary can in turn expect it, he knows as little as before. In particular, containerized environments will have it such that many modules are loaded, but each container still has their own ruleset.
So yeah, hiding the contents is not going to achieve anything - nor is showing. (I am concurring here with the other respondents.)

Quoted 2 lines

Sorry - I've gotten confused about who thinks what exactly. I hope the below isn't wasting everyone's time.

My opinion is that the files should not be exposed. Whenever it does not clash something vital and cannot be resolved I run kernels with grsec, where normal user cannot list the loaded in kernel modules. By exposing these files, some part of that data would then be leaked out.

Quoted 10 lines

Unhiding /proc/ip_tables_names content achieves something specific: it is used by iptables-save to determine what it should write out (in the absence of a specific table being asked for, which I believe is the norm).

I currently have a workaround using a horrific series of bind mounts which substitutes in a normal file with the values iptables-save expects.

Two alternates on the table are:

Quoted 7 lines
Quoted 4 lines

- change ownership of the file in the namespace (wherein a user runs "unshare -U -r -n cat /proc/net/ip_tables_names" to do the same as "cat /proc/net/ip_tables_names" with 0444 perms, so not adding much unless unprivileged namespaces are disabled.

I don't quite understand this: if the ownership of the files are changed, then why do you need to change the access right to 0444?

You're right, I don't. I was just noting point that if unprivileged namespaces are available, either suffices for a regular user to see the contents.

Cheers
Phil

#8 lore raw

PATCH v2 1/1 Root in namespace owns x_tables /proc entries

In reply to Pablo Neira Ayuso

Reading these files is impossible in an unprivileged user namespace, interfering with various firewall tools. For instance, iptables-save relies on reading /proc/net/ip_tables_names to dump only loaded tables.
---

Please don't apply in current form - it doesn't work. The namespace is only set up after the /proc entry is created so it keeps the original owner (an unshare within an unshare can work... mapping root to root).

Since it's in danger of getting quite complicate, would one or more of the following be acceptable?

- Choose permission in a module parameter

- Allow setting with sysctl e.g. net.netfilter.conf.xtable_proc_perms

- Match permissions of /proc/modules (grsec restricts these so we will gain the same policy).

I also worked on a capabilities patch but that made userspace much more complicated in a namespace than outside one. It would be simpler to patch programs to read /proc/modules or assume the contents if they can't read /proc/net/ip_tables_names.

 net/netfilter/x_tables.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/netfilter/x_tables.c +6 -0
index 9b42b5e..671654d 100644
@@ -1227,6 +1227,8 @@ int xt_proto_init(struct net *net, u_int8_t af)
 #ifdef CONFIG_PROC_FS
 	char buf[XT_FUNCTION_MAXNAMELEN];
 	struct proc_dir_entry *proc;
+	kuid_t root_uid;
+	kgid_t root_gid;
 #endif
 
 	if (af >= ARRAY_SIZE(xt_prefix))
@@ -1234,12 +1236,16 @@ int xt_proto_init(struct net *net, u_int8_t af)
 
 
 #ifdef CONFIG_PROC_FS
+	root_uid = make_kuid(current_user_ns(), 1000);
+	root_gid = make_kgid(current_user_ns(), 1000);
+
 	strlcpy(buf, xt_prefix[af], sizeof(buf));
 	strlcat(buf, FORMAT_TABLES, sizeof(buf));
 	proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
 				(void *)(unsigned long)af);
 	if (!proc)
 		goto out;
+	proc_set_user(proc, root_uid, root_gid);
 
 	strlcpy(buf, xt_prefix[af], sizeof(buf));
 	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Signature · 1 line
2.1.4
#9 lore raw

Re: [PATCH v2] Root in namespace owns x_tables /proc entries

Hi Philip,

Quoted 9 lines · Philip Whineray

Since it's in danger of getting quite complicate, would one or more of the following be acceptable?

- Choose permission in a module parameter

- Allow setting with sysctl e.g. net.netfilter.conf.xtable_proc_perms

- Match permissions of /proc/modules (grsec restricts these so we will gain the same policy).

In my opinion either one is good and I'd pick the sysctl setting. That way the permissions could be changed without reloading the module and independently of the permissions of /proc/modules.

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : Wigner Research Centre for Physics, Hungarian Academy of Sciences

          H-1525 Budapest 114, POB. 49, Hungary
#10 lore raw

Re: [PATCH v2] Root in namespace owns x_tables /proc entries

Quoted 9 lines · Jozsef Kadlecsik

Hi Philip,

Quoted 5 lines · Philip Whineray

Since it's in danger of getting quite complicate, would one or more of the following be acceptable?

- Choose permission in a module parameter

Quoted 8 lines
Quoted 4 lines

- Allow setting with sysctl e.g. net.netfilter.conf.xtable_proc_perms

- Match permissions of /proc/modules (grsec restricts these so we will gain the same policy).

In my opinion either one is good and I'd pick the sysctl setting. That way the permissions could be changed without reloading the module and independently of the permissions of /proc/modules.

I'd rather not to have a sysctl for this thing.

I suspect it will not take long until someone else will follow up with a similar patch /proc/net/nf_conntrack.

What is the plan of namespace people for unprivileged namespaces with non-world readable /proc entries?

#11 lore raw

Re: [PATCH v2] Root in namespace owns x_tables /proc entries

Quoted 15 lines · Pablo Neira Ayuso
Quoted 14 lines · Jozsef Kadlecsik

Hi Philip,

Quoted 9 lines · Philip Whineray

Since it's in danger of getting quite complicate, would one or more of the following be acceptable?

- Choose permission in a module parameter

- Allow setting with sysctl e.g. net.netfilter.conf.xtable_proc_perms

- Match permissions of /proc/modules (grsec restricts these so we will gain the same policy).

Quoted 8 lines
Quoted 3 lines

In my opinion either one is good and I'd pick the sysctl setting. That way the permissions could be changed without reloading the module and independently of the permissions of /proc/modules.

I'd rather not to have a sysctl for this thing.

I suspect it will not take long until someone else will follow up with a similar patch /proc/net/nf_conntrack.

That may be true. The two are not equivalent though: the nf_conntrack information is per-namespace (so setting owner to root in the current namespace would certainly be sensible), whereas the information in ip_tables_names is global and directly relates to modules loaded.

Quoted 2 lines

What is the plan of namespace people for unprivileged namespaces with non-world readable /proc entries?

It may not have come up as an issue: a quick survey on my systems suggests pretty much only only netfilter creates files which are group readable but not world readable:

$ sudo find /proc/ -perm /g=r -a \! -perm /o=r | sed \
     's:proc/[0-9]*/:proc/0/:' | sort -u | less
/proc/0/net/ip6_tables_matches
/proc/0/net/ip6_tables_names
/proc/0/net/ip6_tables_targets
/proc/0/net/ip_tables_matches
/proc/0/net/ip_tables_names
/proc/0/net/ip_tables_targets
/proc/0/net/netfilter/nfnetlink_log
/proc/0/net/nf_conntrack
/proc/0/net/nf_conntrack_expect
#12 lore raw

Re: [PATCH v2] Root in namespace owns x_tables /proc entries

In reply to Pablo Neira Ayuso
Quoted 19 lines · Pablo Neira Ayuso
Quoted 9 lines · Jozsef Kadlecsik

Hi Philip,

Quoted 5 lines · Philip Whineray

Since it's in danger of getting quite complicate, would one or more of the following be acceptable?

- Choose permission in a module parameter

Quoted 8 lines
Quoted 4 lines

- Allow setting with sysctl e.g. net.netfilter.conf.xtable_proc_perms

- Match permissions of /proc/modules (grsec restricts these so we will gain the same policy).

In my opinion either one is good and I'd pick the sysctl setting. That way the permissions could be changed without reloading the module and independently of the permissions of /proc/modules.

Quoted 7 lines

I'd rather not to have a sysctl for this thing.

I suspect it will not take long until someone else will follow up with a similar patch /proc/net/nf_conntrack.

What is the plan of namespace people for unprivileged namespaces with non-world readable /proc entries?

If it makes sense to have them per namespace and allow manipulation by the user namespace root, the plan is to make the files owned by the root user in the user namespace that created the network namespace.

That preserves all of the existing logic.

If we don't trust the user namespace root to read/write the file we probably want to just avoid creating the file altogether if (net->user_ns != init_user_ns).

Eric

#13 lore raw

Re: [PATCH v2] Root in namespace owns x_tables /proc entries

In reply to Philip Whineray
Quoted 41 lines · Philip Whineray

Reading these files is impossible in an unprivileged user namespace, interfering with various firewall tools. For instance, iptables-save relies on reading /proc/net/ip_tables_names to dump only loaded tables.
---

Please don't apply in current form - it doesn't work. The namespace is only set up after the /proc entry is created so it keeps the original owner (an unshare within an unshare can work... mapping root to root).

Since it's in danger of getting quite complicate, would one or more of the following be acceptable?

- Choose permission in a module parameter

- Allow setting with sysctl e.g. net.netfilter.conf.xtable_proc_perms

- Match permissions of /proc/modules (grsec restricts these so we will gain the same policy).

I also worked on a capabilities patch but that made userspace much more complicated in a namespace than outside one. It would be simpler to patch programs to read /proc/modules or assume the contents if they can't read /proc/net/ip_tables_names.

 net/netfilter/x_tables.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/netfilter/x_tables.c +2 -0
index 9b42b5e..671654d 100644
@@ -1227,6 +1227,8 @@ int xt_proto_init(struct net *net, u_int8_t af)
 #ifdef CONFIG_PROC_FS
 	char buf[XT_FUNCTION_MAXNAMELEN];
 	struct proc_dir_entry *proc;
+	kuid_t root_uid;
+	kgid_t root_gid;
 #endif
 
 	if (af >= ARRAY_SIZE(xt_prefix))

net/netfilter/x_tables.c int xt_proto_init(struct net *net, u_int8_t af) · line 1240

Quoted 5 lines
net/netfilter/x_tables.c +2 -0
@@ -1234,12 +1236,16 @@ int xt_proto_init(struct net *net, u_int8_t af)
 
 
 #ifdef CONFIG_PROC_FS
+	root_uid = make_kuid(current_user_ns(), 1000);
+	root_gid = make_kgid(current_user_ns(), 1000);

These lines are wrong. They should be:

	root_uid = make_kuid(net->user_ns, 0);
        root_gid = make_kgid(net->user_ns, 0);
        if (!uid_valid(root_uid) || !gid_valid(root_gid))
        	goto out;
Quoted 10 lines
net/netfilter/x_tables.c +1 -0
 	strlcpy(buf, xt_prefix[af], sizeof(buf));
 	strlcat(buf, FORMAT_TABLES, sizeof(buf));
 	proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
 				(void *)(unsigned long)af);
 	if (!proc)
 		goto out;
+	proc_set_user(proc, root_uid, root_gid);
 
 	strlcpy(buf, xt_prefix[af], sizeof(buf));
 	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
#14 lore raw

Re: [PATCH v2] Root in namespace owns x_tables /proc entries

Quoted 13 lines · Eric W. Biederman
Quoted 3 lines · Philip Whineray

Reading these files is impossible in an unprivileged user namespace, interfering with various firewall tools. For instance, iptables-save relies on reading /proc/net/ip_tables_names to dump only loaded tables.

These lines are wrong. They should be:

        root_uid = make_kuid(net->user_ns, 0);
        root_gid = make_kgid(net->user_ns, 0);
        if (!uid_valid(root_uid) || !gid_valid(root_gid))
        	goto out;
Quoted 7 lines

net/netfilter/x_tables.c int xt_proto_init(struct net *net, u_int8_t af) · line 1248

Quoted 7 lines
net/netfilter/x_tables.c +1 -0
 	strlcpy(buf, xt_prefix[af], sizeof(buf));
 	strlcat(buf, FORMAT_TABLES, sizeof(buf));
 	proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
 				(void *)(unsigned long)af);
 	if (!proc)
 		goto out;
+	proc_set_user(proc, root_uid, root_gid);

Thanks for the pointer Eric. As written it doesn't quite work because out is an error path. unshare(CLONE_NEWUSER|CLONE_NEWNET) always fails due to there not being a mapping for the user yet. Instead:

       root_uid = make_kuid(net->user_ns, 0);
       root_gid = make_kgid(net->user_ns, 0);

followed by:

       if (!proc)
              goto out;
       if (uid_valid(root_uid) && gid_valid(root_gid))
              proc_set_user(proc, root_uid, root_gid);

would preserve the current behaviour but allow the files to be correctly mapped by first unsharing the user namespace, then setting the gid map and finally unsharing the namespace.

Or, is it sane to bypass all the above and jump straight to:

        proc_set_user(proc, net->user_ns->owner, net->user_ns->group);

Cheers
Phil

#15 lore raw

Re: [PATCH v2] Root in namespace owns x_tables /proc entries

Quoted 14 lines · Phil Whineray
Quoted 13 lines · Eric W. Biederman
Quoted 3 lines · Philip Whineray

Reading these files is impossible in an unprivileged user namespace, interfering with various firewall tools. For instance, iptables-save relies on reading /proc/net/ip_tables_names to dump only loaded tables.

These lines are wrong. They should be:

        root_uid = make_kuid(net->user_ns, 0);
        root_gid = make_kgid(net->user_ns, 0);
        if (!uid_valid(root_uid) || !gid_valid(root_gid))
        	goto out;
Quoted 11 lines
Quoted 7 lines

net/netfilter/x_tables.c int xt_proto_init(struct net *net, u_int8_t af) · line 1248

Quoted 7 lines
net/netfilter/x_tables.c +1 -0
 	strlcpy(buf, xt_prefix[af], sizeof(buf));
 	strlcat(buf, FORMAT_TABLES, sizeof(buf));
 	proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
 				(void *)(unsigned long)af);
 	if (!proc)
 		goto out;
+	proc_set_user(proc, root_uid, root_gid);

Thanks for the pointer Eric. As written it doesn't quite work because out is an error path. unshare(CLONE_NEWUSER|CLONE_NEWNET) always fails due to there not being a mapping for the user yet. Instead:

Point.

Although CLONE_NEWUSER|CLONE_NEWNET are not required to happen simultaneously.

So you can make what I was suggesting work with:
unshare(CLONE_NEWUSER);
/* Setup the mapping */
unshare(CLONE_NEWNET);

The simplest version of this I can think of is to just not change the user if the mapping is not setup at the time the proc files are created.

Certainly that would not be worse than what we have today.

Quoted 5 lines
       root_uid = make_kuid(net->user_ns, 0);
       root_gid = make_kgid(net->user_ns, 0);

followed by:

Quoted 8 lines
       if (!proc)
              goto out;
       if (uid_valid(root_uid) && gid_valid(root_gid))
              proc_set_user(proc, root_uid, root_gid);

would preserve the current behaviour but allow the files to be correctly mapped by first unsharing the user namespace, then setting the gid map and finally unsharing the namespace.

I am not quite certain what you are suggesting above. It looks and sounds like my suggest to only call proc_set_user if a mapping exists. Which is fine.

Quoted 3 lines

Or, is it sane to bypass all the above and jump straight to:

        proc_set_user(proc, net->user_ns->owner, net->user_ns->group);

It is not. There are many reasons why typically user_ns->owner and
user_ns->group are rarely mapped in a user namespace.

Eric

#16 lore raw

Re: [PATCH v2] Root in namespace owns x_tables /proc entries

Quoted 53 lines · Eric W. Biederman
Quoted 14 lines · Phil Whineray
Quoted 13 lines · Eric W. Biederman
Quoted 3 lines · Philip Whineray

Reading these files is impossible in an unprivileged user namespace, interfering with various firewall tools. For instance, iptables-save relies on reading /proc/net/ip_tables_names to dump only loaded tables.

These lines are wrong. They should be:

        root_uid = make_kuid(net->user_ns, 0);
        root_gid = make_kgid(net->user_ns, 0);
        if (!uid_valid(root_uid) || !gid_valid(root_gid))
        	goto out;
Quoted 11 lines
Quoted 7 lines

net/netfilter/x_tables.c int xt_proto_init(struct net *net, u_int8_t af) · line 1248

Quoted 7 lines
net/netfilter/x_tables.c +1 -0
 	strlcpy(buf, xt_prefix[af], sizeof(buf));
 	strlcat(buf, FORMAT_TABLES, sizeof(buf));
 	proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
 				(void *)(unsigned long)af);
 	if (!proc)
 		goto out;
+	proc_set_user(proc, root_uid, root_gid);

Thanks for the pointer Eric. As written it doesn't quite work because out is an error path. unshare(CLONE_NEWUSER|CLONE_NEWNET) always fails due to there not being a mapping for the user yet. Instead:

Point.

Although CLONE_NEWUSER|CLONE_NEWNET are not required to happen simultaneously.

So you can make what I was suggesting work with:
unshare(CLONE_NEWUSER);
/* Setup the mapping */
unshare(CLONE_NEWNET);

The simplest version of this I can think of is to just not change the user if the mapping is not setup at the time the proc files are created.

Certainly that would not be worse than what we have today.

Quoted 10 lines
       root_uid = make_kuid(net->user_ns, 0);
       root_gid = make_kgid(net->user_ns, 0);

followed by:

       if (!proc)
              goto out;
       if (uid_valid(root_uid) && gid_valid(root_gid))
              proc_set_user(proc, root_uid, root_gid);
Quoted 7 lines
Quoted 3 lines

would preserve the current behaviour but allow the files to be correctly mapped by first unsharing the user namespace, then setting the gid map and finally unsharing the namespace.

I am not quite certain what you are suggesting above. It looks and sounds like my suggest to only call proc_set_user if a mapping exists. Which is fine.

I was indeed trying to get at precisely the same as your suggestion. Thanks for the much more succinct description.

Patch in due course...

Phil

#17 lore raw

PATCH v3 1/1 Set /proc/net entries owner to root in namespace

In reply to Eric W. Biederman

Various files are owned by root with 0440 permission. Reading them is impossible in an unprivileged user namespace, interfering with firewall tools. For instance, iptables-save relies on /proc/net/ip_tables_names contents to dump only loaded tables.

This patch assigned ownership of the following files to root in the current namespace:

- /proc/net/*_tables_names
- /proc/net/*_tables_matches
- /proc/net/*_tables_targets
- /proc/net/nf_conntrack
- /proc/net/nf_conntrack_expect
- /proc/net/netfilter/nfnetlink_log

A mapping for root must be available, so this order should be followed:

unshare(CLONE_NEWUSER);
/* Setup the mapping */
unshare(CLONE_NEWNET);

---
In addition to the x_tables files ownership change, I added the connection tracking and netlink logging files which are 0440 since their contents are handled per-namespace.

I think this patch should address all comments to date.

Regards
Phil

 net/netfilter/nf_conntrack_expect.c     |  7 +++++++
 net/netfilter/nf_conntrack_standalone.c |  7 +++++++
 net/netfilter/nfnetlink_log.c           | 15 +++++++++++++--
 net/netfilter/x_tables.c                | 12 ++++++++++++
 4 files changed, 39 insertions(+), 2 deletions(-)
net/netfilter/nf_conntrack_expect.c +7 -0
index acf5c7b..278927a 100644
@@ -596,11 +596,18 @@ static int exp_proc_init(struct net *net)
 {
 #ifdef CONFIG_NF_CONNTRACK_PROCFS
 	struct proc_dir_entry *proc;
+	kuid_t root_uid;
+	kgid_t root_gid;
 
 	proc = proc_create("nf_conntrack_expect", 0440, net->proc_net,
 			   &exp_file_ops);
 	if (!proc)
 		return -ENOMEM;
+
+	root_uid = make_kuid(net->user_ns, 0);
+	root_gid = make_kgid(net->user_ns, 0);
+	if (uid_valid(root_uid) && gid_valid(root_gid))
+		proc_set_user(proc, root_uid, root_gid);
 #endif /* CONFIG_NF_CONNTRACK_PROCFS */
 	return 0;
 }
net/netfilter/nf_conntrack_standalone.c +7 -0
index 1fb3cac..0f1a45b 100644
@@ -392,11 +392,18 @@ static const struct file_operations ct_cpu_seq_fops = {
 static int nf_conntrack_standalone_init_proc(struct net *net)
 {
 	struct proc_dir_entry *pde;
+	kuid_t root_uid;
+	kgid_t root_gid;
 
 	pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
 	if (!pde)
 		goto out_nf_conntrack;
 
+	root_uid = make_kuid(net->user_ns, 0);
+	root_gid = make_kgid(net->user_ns, 0);
+	if (uid_valid(root_uid) && gid_valid(root_gid))
+		proc_set_user(pde, root_uid, root_gid);
+
 	pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
 			  &ct_cpu_seq_fops);
 	if (!pde)
net/netfilter/nfnetlink_log.c +13 -2
index 4670821..74d5117 100644
@@ -1041,15 +1041,26 @@ static int __net_init nfnl_log_net_init(struct net *net)
 {
 	unsigned int i;
 	struct nfnl_log_net *log = nfnl_log_pernet(net);
+#ifdef CONFIG_PROC_FS
+	struct proc_dir_entry *proc;
+	kuid_t root_uid;
+	kgid_t root_gid;
+#endif
 
 	for (i = 0; i < INSTANCE_BUCKETS; i++)
 		INIT_HLIST_HEAD(&log->instance_table[i]);
 	spin_lock_init(&log->instances_lock);
 
 #ifdef CONFIG_PROC_FS
-	if (!proc_create("nfnetlink_log", 0440,
-			 net->nf.proc_netfilter, &nful_file_ops))
+	proc = proc_create("nfnetlink_log", 0440,
+			   net->nf.proc_netfilter, &nful_file_ops);
+	if (!proc)
 		return -ENOMEM;
+
+	root_uid = make_kuid(net->user_ns, 0);
+	root_gid = make_kgid(net->user_ns, 0);
+	if (uid_valid(root_uid) && gid_valid(root_gid))
+		proc_set_user(proc, root_uid, root_gid);
 #endif
 	return 0;
 }
net/netfilter/x_tables.c +12 -0
index 9b42b5e..ff98127 100644
@@ -26,6 +26,7 @@
 #include <linux/mm.h>
 #include <linux/slab.h>
 #include <linux/audit.h>
+#include <linux/user_namespace.h>
 #include <net/net_namespace.h>
 
 #include <linux/netfilter/x_tables.h>
@@ -1227,6 +1228,8 @@ int xt_proto_init(struct net *net, u_int8_t af)
 #ifdef CONFIG_PROC_FS
 	char buf[XT_FUNCTION_MAXNAMELEN];
 	struct proc_dir_entry *proc;
+	kuid_t root_uid;
+	kgid_t root_gid;
 #endif
 
 	if (af >= ARRAY_SIZE(xt_prefix))
@@ -1234,12 +1237,17 @@ int xt_proto_init(struct net *net, u_int8_t af)
 
 
 #ifdef CONFIG_PROC_FS
+	root_uid = make_kuid(net->user_ns, 0);
+	root_gid = make_kgid(net->user_ns, 0);
+
 	strlcpy(buf, xt_prefix[af], sizeof(buf));
 	strlcat(buf, FORMAT_TABLES, sizeof(buf));
 	proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
 				(void *)(unsigned long)af);
 	if (!proc)
 		goto out;
+	if (uid_valid(root_uid) && gid_valid(root_gid))
+		proc_set_user(proc, root_uid, root_gid);
 
 	strlcpy(buf, xt_prefix[af], sizeof(buf));
 	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
@@ -1247,6 +1255,8 @@ int xt_proto_init(struct net *net, u_int8_t af)
 				(void *)(unsigned long)af);
 	if (!proc)
 		goto out_remove_tables;
+	if (uid_valid(root_uid) && gid_valid(root_gid))
+		proc_set_user(proc, root_uid, root_gid);
 
 	strlcpy(buf, xt_prefix[af], sizeof(buf));
 	strlcat(buf, FORMAT_TARGETS, sizeof(buf));
@@ -1254,6 +1264,8 @@ int xt_proto_init(struct net *net, u_int8_t af)
 				(void *)(unsigned long)af);
 	if (!proc)
 		goto out_remove_matches;
+	if (uid_valid(root_uid) && gid_valid(root_gid))
+		proc_set_user(proc, root_uid, root_gid);
 #endif
 
 	return 0;
Signature · 1 line
2.1.4
maintainer Applied
#18 lore raw

Re: [PATCH v3] Set /proc/net entries owner to root in namespace

Quoted 15 lines · Philip Whineray

Various files are owned by root with 0440 permission. Reading them is impossible in an unprivileged user namespace, interfering with firewall tools. For instance, iptables-save relies on /proc/net/ip_tables_names contents to dump only loaded tables.

This patch assigned ownership of the following files to root in the current namespace:

- /proc/net/*_tables_names
- /proc/net/*_tables_matches
- /proc/net/*_tables_targets
- /proc/net/nf_conntrack
- /proc/net/nf_conntrack_expect
- /proc/net/netfilter/nfnetlink_log

Quoted 5 lines

A mapping for root must be available, so this order should be followed:

unshare(CLONE_NEWUSER);
/* Setup the mapping */
unshare(CLONE_NEWNET);

Applied, thanks.