Skip to contentSkip to conversation

keys: request_key_auth shows a global pid in /proc/keys across pid namespaces

PATCH Maoyi Xie 3 participants 5 messages Aug 9 – Aug 20

Subsystems (2)
lore
Share & tools
lore

Maoyi Xie

author
#0 lore raw

Hi all,

I think request_key_auth leaks a global pid into /proc/keys. I would appreciate it if you could take a look.

request_key_auth_describe() prints the requestor's pid:

	if (key_is_positive(key))
		seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);

rka->pid is current->pid, captured in request_key_auth_new() when the auth key is made. It is a raw pid_t in the init pid namespace. But /proc/keys is read by whoever opens it, and their pid namespace can differ. The uid and gid on that line already go through seq_user_ns(m). The pid does not. So a reader in another pid namespace gets a number that means nothing there, or worse lands on a different task.

Not everyone can read the line. The owner uid must map into the reader's user namespace, and the reader needs VIEW on the key. The auth key gives VIEW to the requestor's own uid and to a possessor. So the reader has to be the requestor's uid, in a different pid namespace. That fits a sandbox at the same uid, made with unshare, systemd-nspawn or bubblewrap. A default Docker container does not hit it, since it hides /proc/keys.

I found this with a static check for ids that reach userspace with no namespace translation. I reproduced it in a qemu VM on 7.1-rc1 as a normal user, no hardware or kernel changes needed. The reproducer calls request_key, then reads /proc/keys from a sibling pid namespace. Before the patch it reads the requestor's global pid, which does not exist in that namespace. After the patch it reads pid 0, since the requestor has no pid there. There is no keyrings selftest in the tree, so this is the only test.

I am happy to send a patch. The raw pid_t cannot be translated later. So I stored a struct pid and print it with pid_nr_ns, the way the uid is already scoped.

Thanks,
Maoyi
https://maoyixie.com/

---

include/keys/request_key_auth-type.h +1 -1
index 01e42ee5f4099..464636278c4f8 100644
@@ -22,7 +22,7 @@ struct request_key_auth {
 	const struct cred	*cred;
 	void			*callout_info;
 	size_t			callout_len;
-	pid_t			pid;
+	struct pid		*pid;
 	char			op[8];
 } __randomize_layout;
 
security/keys/request_key_auth.c +9 -3
index 282e09d8fa46c..e8d1037526b19 100644
@@ -13,6 +13,8 @@
 #include <linux/slab.h>
 #include <linux/uaccess.h>
 #include "internal.h"
+#include <linux/pid_namespace.h>
+#include <linux/proc_fs.h>
 #include <keys/request_key_auth-type.h>
 
 static int request_key_auth_preparse(struct key_preparsed_payload *);
@@ -73,7 +75,10 @@ static void request_key_auth_describe(const struct key *key,
 	seq_puts(m, "key:");
 	seq_puts(m, key->description);
 	if (key_is_positive(key))
-		seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);
+		seq_printf(m, " pid:%d ci:%zu",
+			   pid_nr_ns(rka->pid,
+				     proc_pid_ns(file_inode(m->file)->i_sb)),
+			   rka->callout_len);
 }
 
 /*
@@ -113,6 +118,7 @@ static void free_request_key_auth(struct request_key_auth *rka)
 	if (rka->cred)
 		put_cred(rka->cred);
 	kfree(rka->callout_info);
+	put_pid(rka->pid);
 	kfree(rka);
 }
 
@@ -226,14 +232,14 @@ struct key *request_key_auth_new(struct key *target, const char *op,
 
 		irka = cred->request_key_auth->payload.data[0];
 		rka->cred = get_cred(irka->cred);
-		rka->pid = irka->pid;
+		rka->pid = get_pid(irka->pid);
 
 		up_read(&cred->request_key_auth->sem);
 	}
 	else {
 		/* it isn't - use this process as the context */
 		rka->cred = get_cred(cred);
-		rka->pid = current->pid;
+		rka->pid = get_pid(task_pid(current));
 	}
 
 	rka->target_key = key_get(target);
maintainer
#1 lore raw
Quoted 97 lines · Maoyi Xie

Hi all,

I think request_key_auth leaks a global pid into /proc/keys. I would appreciate it if you could take a look.

request_key_auth_describe() prints the requestor's pid:

	if (key_is_positive(key))
		seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);

rka->pid is current->pid, captured in request_key_auth_new() when the auth key is made. It is a raw pid_t in the init pid namespace. But /proc/keys is read by whoever opens it, and their pid namespace can differ. The uid and gid on that line already go through seq_user_ns(m). The pid does not. So a reader in another pid namespace gets a number that means nothing there, or worse lands on a different task.

Not everyone can read the line. The owner uid must map into the reader's user namespace, and the reader needs VIEW on the key. The auth key gives VIEW to the requestor's own uid and to a possessor. So the reader has to be the requestor's uid, in a different pid namespace. That fits a sandbox at the same uid, made with unshare, systemd-nspawn or bubblewrap. A default Docker container does not hit it, since it hides /proc/keys.

I found this with a static check for ids that reach userspace with no namespace translation. I reproduced it in a qemu VM on 7.1-rc1 as a normal user, no hardware or kernel changes needed. The reproducer calls request_key, then reads /proc/keys from a sibling pid namespace. Before the patch it reads the requestor's global pid, which does not exist in that namespace. After the patch it reads pid 0, since the requestor has no pid there. There is no keyrings selftest in the tree, so this is the only test.

I am happy to send a patch. The raw pid_t cannot be translated later. So I stored a struct pid and print it with pid_nr_ns, the way the uid is already scoped.

Thanks,
Maoyi
https://maoyixie.com/

---

include/keys/request_key_auth-type.h +1 -1
index 01e42ee5f4099..464636278c4f8 100644
@@ -22,7 +22,7 @@ struct request_key_auth {
 	const struct cred	*cred;
 	void			*callout_info;
 	size_t			callout_len;
-	pid_t			pid;
+	struct pid		*pid;
 	char			op[8];
 } __randomize_layout;
 
security/keys/request_key_auth.c +8 -2
index 282e09d8fa46c..e8d1037526b19 100644
@@ -13,6 +13,8 @@
 #include <linux/slab.h>
 #include <linux/uaccess.h>
 #include "internal.h"
+#include <linux/pid_namespace.h>
+#include <linux/proc_fs.h>
 #include <keys/request_key_auth-type.h>
 
 static int request_key_auth_preparse(struct key_preparsed_payload *);
@@ -73,7 +75,10 @@ static void request_key_auth_describe(const struct key *key,
 	seq_puts(m, "key:");
 	seq_puts(m, key->description);
 	if (key_is_positive(key))
-		seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);
+		seq_printf(m, " pid:%d ci:%zu",
+			   pid_nr_ns(rka->pid,
+				     proc_pid_ns(file_inode(m->file)->i_sb)),
+			   rka->callout_len);
 }
 
 /*
@@ -113,6 +118,7 @@ static void free_request_key_auth(struct request_key_auth *rka)
 	if (rka->cred)
 		put_cred(rka->cred);
 	kfree(rka->callout_info);
+	put_pid(rka->pid);
 	kfree(rka);
 }
 
@@ -226,14 +232,14 @@ struct key *request_key_auth_new(struct key *target, const char *op,
 
 		irka = cred->request_key_auth->payload.data[0];
 		rka->cred = get_cred(irka->cred);
-		rka->pid = irka->pid;
+		rka->pid = get_pid(irka->pid);
 

security/keys/request_key_auth.c struct key *request_key_auth_new(struct key *target, const char *op, · line 245

Quoted 10 lines
security/keys/request_key_auth.c +1 -1
 		up_read(&cred->request_key_auth->sem);
 	}
 	else {
 		/* it isn't - use this process as the context */
 		rka->cred = get_cred(cred);
-		rka->pid = current->pid;
+		rka->pid = get_pid(task_pid(current));
 	}
 
 	rka->target_key = key_get(target);

For me this looks legit change based on the description. I'm just questioning myself given how plain sight it has been :-)

David, what do you think?

BR, Jarkko

#2 lore raw
In reply to Maoyi Xie
Quoted 122 lines · Maoyi Xie

Hi all,

I think request_key_auth leaks a global pid into /proc/keys. I would appreciate it if you could take a look.

request_key_auth_describe() prints the requestor's pid:

	if (key_is_positive(key))
		seq_printf(m, " pid:%d ci:%zu", rka->pid, rka-
Quoted 1 line

callout_len);

rka->pid is current->pid, captured in request_key_auth_new() when the auth key is made. It is a raw pid_t in the init pid namespace. But /proc/keys is read by whoever opens it, and their pid namespace can differ. The uid and gid on that line already go through seq_user_ns(m).
The pid does not. So a reader in another pid namespace gets a number that means nothing there, or worse lands on a different task.

Not everyone can read the line. The owner uid must map into the reader's
user namespace, and the reader needs VIEW on the key. The auth key gives
VIEW to the requestor's own uid and to a possessor. So the reader has to
be the requestor's uid, in a different pid namespace. That fits a sandbox at the same uid, made with unshare, systemd-nspawn or bubblewrap. A default Docker container does not hit it, since it hides
/proc/keys.

I found this with a static check for ids that reach userspace with no namespace translation. I reproduced it in a qemu VM on 7.1-rc1 as a normal user, no hardware or kernel changes needed. The reproducer calls
request_key, then reads /proc/keys from a sibling pid namespace. Before
the patch it reads the requestor's global pid, which does not exist in
that namespace. After the patch it reads pid 0, since the requestor has
no pid there. There is no keyrings selftest in the tree, so this is the
only test.

I am happy to send a patch. The raw pid_t cannot be translated later. So
I stored a struct pid and print it with pid_nr_ns, the way the uid is already scoped.

Thanks,
Maoyi
https://maoyixie.com/

---

include/keys/request_key_auth-type.h +1 -1
index 01e42ee5f4099..464636278c4f8 100644
@@ -22,7 +22,7 @@ struct request_key_auth {
  	const struct cred	*cred;
  	void			*callout_info;
  	size_t			callout_len;
-	pid_t			pid;
+	struct pid		*pid;
  	char			op[8];
  } __randomize_layout;

security/keys/request_key_auth.c +2 -0
index 282e09d8fa46c..e8d1037526b19 100644
@@ -13,6 +13,8 @@
  #include <linux/slab.h>
  #include <linux/uaccess.h>
  #include "internal.h"
+#include <linux/pid_namespace.h>
+#include <linux/proc_fs.h>
  #include <keys/request_key_auth-type.h>


 static int request_key_auth_preparse(struct key_preparsed_payload *);

security/keys/request_key_auth.c +4 -1
@@ -73,7 +75,10 @@ static void request_key_auth_describe(const struct
 key *key,
  	seq_puts(m, "key:");
  	seq_puts(m, key->description);
  	if (key_is_positive(key))
-		seq_printf(m, " pid:%d ci:%zu", rka->pid, rka-
 >callout_len);
+		seq_printf(m, " pid:%d ci:%zu",
+			   pid_nr_ns(rka->pid,
+				     proc_pid_ns(file_inode(m-
 >file)->i_sb)),
+			   rka->callout_len);

 }

 /*

security/keys/request_key_auth.c +1 -0
@@ -113,6 +118,7 @@ static void free_request_key_auth(struct
 request_key_auth *rka)
  	if (rka->cred)
  		put_cred(rka->cred);
  	kfree(rka->callout_info);
+	put_pid(rka->pid);
  	kfree(rka);
  }

security/keys/request_key_auth.c +0 -0
@@ -226,14 +232,14 @@ struct key *request_key_auth_new(struct key
 *target, const char *op,


  irka = cred->request_key_auth->payload.data[0]; rka->cred = get_cred(irka->cred);

security/keys/request_key_auth.c +1 -1
-		rka->pid = irka->pid;
+		rka->pid = get_pid(irka->pid);


  up_read(&cred->request_key_auth->sem); }
  else {
  /* it isn't - use this process as the context */ rka->cred = get_cred(cred);

security/keys/request_key_auth.c struct key *request_key_auth_new(struct key *target, const char *op, · line 242

Quoted 2 lines
security/keys/request_key_auth.c +1 -1
-		rka->pid = current->pid;
+		rka->pid = get_pid(task_pid(current));

I'm afraid this get_pid/put_pid doesn't look right because what it will do is pin the pid in the creator pid namespace. Unfortunately, some keys and keyrings aren't bound to a pid namespace, so if I create a key in my user keyring and then enter a new pid namespace, with this change it will still show up as the pid number of the init namespace (pinning the struct pid there) and vice versa, which still sounds like the wrong behaviour.

I think if you really want to fix this, you have to keep the pid as is, but make sure it always records the pid in the init_pid_ns (i.e. always translate back to init_ns) and then translate to the current pid_ns in the output generator of /proc/keys.

Regards,

James

author
#3 lore raw
Quoted 7 lines · James Bottomley

I'm afraid this get_pid/put_pid doesn't look right because what it will do is pin the pid in the creator pid namespace. Unfortunately, some keys and keyrings aren't bound to a pid namespace, so if I create a key in my user keyring and then enter a new pid namespace, with this change it will still show up as the pid number of the init namespace (pinning the struct pid there) and vice versa, which still sounds like the wrong behaviour.

You're right that get_pid() keeps the creator's pid_namespace alive for the auth key's lifetime. But the pin does not change the printed number. describe() renders through the reader's proc pid_ns with pid_nr_ns(). That returns 0 when the task is not visible there. A key made in init_ns and read from a child pid_ns shows 0, not the init number. It never shows a foreign pid. In your example the pin only holds init_pid_ns, which is permanent, so it changes nothing. Auth keys are short-lived, so a container-created key holds that pin only until request_key finishes.

Quoted 4 lines

I think if you really want to fix this, you have to keep the pid as is, but make sure it always records the pid in the init_pid_ns (i.e. always translate back to init_ns) and then translate to the current pid_ns in the output generator of /proc/keys.

current->pid is already the init_pid_ns number. Keeping a struct pid lets pid_nr_ns translate it for each reader, the way pidfd_show_fdinfo() already does. A raw pid_t would instead need a find_pid_ns() lookup at read time. That can land on a reused number.

Thanks,
Maoyi

maintainer
#4 lore raw
In reply to Maoyi Xie
Quoted 11 lines · Maoyi Xie

Hi all,

I think request_key_auth leaks a global pid into /proc/keys. I would appreciate it if you could take a look.

request_key_auth_describe() prints the requestor's pid:

	if (key_is_positive(key))
		seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);

rka->pid is current->pid, captured in request_key_auth_new() when the

auth key is made. It is a raw pid_t in the init pid namespace. But

Quoted 50 lines

/proc/keys is read by whoever opens it, and their pid namespace can differ. The uid and gid on that line already go through seq_user_ns(m). The pid does not. So a reader in another pid namespace gets a number that means nothing there, or worse lands on a different task.

Not everyone can read the line. The owner uid must map into the reader's user namespace, and the reader needs VIEW on the key. The auth key gives VIEW to the requestor's own uid and to a possessor. So the reader has to be the requestor's uid, in a different pid namespace. That fits a sandbox at the same uid, made with unshare, systemd-nspawn or bubblewrap. A default Docker container does not hit it, since it hides /proc/keys.

I found this with a static check for ids that reach userspace with no namespace translation. I reproduced it in a qemu VM on 7.1-rc1 as a normal user, no hardware or kernel changes needed. The reproducer calls request_key, then reads /proc/keys from a sibling pid namespace. Before the patch it reads the requestor's global pid, which does not exist in that namespace. After the patch it reads pid 0, since the requestor has no pid there. There is no keyrings selftest in the tree, so this is the only test.

I am happy to send a patch. The raw pid_t cannot be translated later. So I stored a struct pid and print it with pid_nr_ns, the way the uid is already scoped.

Thanks,
Maoyi
https://maoyixie.com/

---

include/keys/request_key_auth-type.h +1 -1
index 01e42ee5f4099..464636278c4f8 100644
@@ -22,7 +22,7 @@ struct request_key_auth {
 	const struct cred	*cred;
 	void			*callout_info;
 	size_t			callout_len;
-	pid_t			pid;
+	struct pid		*pid;
 	char			op[8];
 } __randomize_layout;
 
security/keys/request_key_auth.c +0 -0
index 282e09d8fa46c..e8d1037526b19 100644

security/keys/request_key_auth.c line 16

Quoted 4 lines
security/keys/request_key_auth.c +1 -0
@@ -13,6 +13,8 @@
 #include <linux/slab.h>
 #include <linux/uaccess.h>
 #include "internal.h"
+#include <linux/pid_namespace.h>

#include <linux/pid.h>

Quoted 31 lines
security/keys/request_key_auth.c +7 -2
+#include <linux/proc_fs.h>
 #include <keys/request_key_auth-type.h>
 
 static int request_key_auth_preparse(struct key_preparsed_payload *);
@@ -73,7 +75,10 @@ static void request_key_auth_describe(const struct key *key,
 	seq_puts(m, "key:");
 	seq_puts(m, key->description);
 	if (key_is_positive(key))
-		seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);
+		seq_printf(m, " pid:%d ci:%zu",
+			   pid_nr_ns(rka->pid,
+				     proc_pid_ns(file_inode(m->file)->i_sb)),
+			   rka->callout_len);
 }
 
 /*
@@ -113,6 +118,7 @@ static void free_request_key_auth(struct request_key_auth *rka)
 	if (rka->cred)
 		put_cred(rka->cred);
 	kfree(rka->callout_info);
+	put_pid(rka->pid);
 	kfree(rka);
 }
 
@@ -226,14 +232,14 @@ struct key *request_key_auth_new(struct key *target, const char *op,
 
 		irka = cred->request_key_auth->payload.data[0];
 		rka->cred = get_cred(irka->cred);
-		rka->pid = irka->pid;
+		rka->pid = get_pid(irka->pid);
 

security/keys/request_key_auth.c struct key *request_key_auth_new(struct key *target, const char *op, · line 245

Quoted 10 lines
security/keys/request_key_auth.c +1 -1
 		up_read(&cred->request_key_auth->sem);
 	}
 	else {
 		/* it isn't - use this process as the context */
 		rka->cred = get_cred(cred);
-		rka->pid = current->pid;
+		rka->pid = get_pid(task_pid(current));
 	}
 
 	rka->target_key = key_get(target);

Now I had time to read this with thought.

Please send a path with:

Fixes 78b7280cce23 ("KEYS: Improve /proc/keys")
Cc [email protected] # v5.10+

This definitely deserves a formal review process.

BR, Jarkko