Skip to contentSkip to conversation

nsfs: Add an ioctl() to return the namespace type

PATCH Superseded Michael Kerrisk (man-pages) 3 participants 5 messages Dec 19, 2016 – Dec 22, 2016 1 message not in the archive

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/2 nsfs: Add an ioctl() to return the namespace type

Linux 4.9 added two ioctl() operations that can be used to discover:

* the parental relationships for hierarchical namespaces (user and PID) [NS_GET_PARENT]
* the user namespaces that owns a specified non-user-namespace [NS_GET_USERNS]

For no good reason that I can glean, NS_GET_USERNS was made synonymous with NS_GET_PARENT for user namespaces. It might have been better if NS_GET_USERNS had returned an error if the supplied file descriptor referred to a user namespace, since it suggests that the caller may be confused. More particularly, if it had generated an error, then I wouldn't need the new ioctl() operation proposed here. (On the other hand, what I propose here may be more generally useful.)

I would like to write code that can answer the question: "what capabilities does process X have in namespace Y"? (where Y is defined by a file descriptor referring to one of the /proc/PID/ns/xxxx files). The rules that determine the answer to this question are described in the capabilities(7) manual page and involve working out the chain of relationships between the user namespace of process X and the namespace Y.

Namespace Y might be a user namespace (in which case my code would just use Y) or a non-user namespace (in which case my code needs to use NS_GET_USERNS to get the user namespace associated with Y). The problem is that there is no way to tell the difference by looking at the file descriptor (and if I try to use NS_GET_USERNS on a Y that is a user namespace, I get the parent user namespace of Y, which is not what I want).

This patch therefore adds a new ioctl(), NS_GET_NSTYPE, which, given a file descriptor that refers to a user namespace, returns the namespace type (one of the CLONE_NEW* constants).

---
 fs/nsfs.c                 | 2 ++
 include/uapi/linux/nsfs.h | 3 +++
 2 files changed, 5 insertions(+)
fs/nsfs.c +2 -0
index 8c9fb29..5d53476 100644
@@ -172,6 +172,8 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
 		if (!ns->ops->get_parent)
 			return -EINVAL;
 		return open_related_ns(ns, ns->ops->get_parent);
+	case NS_GET_NSTYPE:
+		return ns->ops->type;
 	default:
 		return -ENOTTY;
 	}
include/uapi/linux/nsfs.h +3 -0
index 3af6172..2b48df1 100644
@@ -9,5 +9,8 @@
 #define NS_GET_USERNS	_IO(NSIO, 0x1)
 /* Returns a file descriptor that refers to a parent namespace */
 #define NS_GET_PARENT	_IO(NSIO, 0x2)
+/* Returns the type of namespace (CLONE_NEW* value) referred to by
+   file descriptor */
+#define NS_GET_NSTYPE	_IO(NSIO, 0x3)
 
 #endif /* __LINUX_NSFS_H */
Signature · 1 line
2.5.5
#2 lore raw

PATCH 2/2 nsfs: Add an ioctl() to return creator UID of a userns

In reply to a message not in the archive

# Some open questions about this patch below.
#
One of the rules regarding capabilities is:

A process that resides in the parent of the user namespace and whose effective user ID matches the owner of the namespace has all capabilities in the namespace.

Therefore, in order to write code that discovers whether process X has capabilities in namespace Y, we need a way to find out who the creator of a user namespace is. This patch adds an NS_GET_CREATOR_UID ioctl() that returns the (munged) UID of the creator of the user namespace referred to by the specified file descriptor.

If the supplied file descriptor does not refer to a user namespace, the operation fails with the error EINVAL.

---
 fs/nsfs.c                 | 6 ++++++
 include/uapi/linux/nsfs.h | 8 +++++---
 2 files changed, 11 insertions(+), 3 deletions(-)

Open questions:

* Would it be preferabe to separate the logic for NS_GET_CREATOR_UID into a small helper function?
* Is this a correct use of container_of()? I did not immediately see another way to get to the user_namespace struct, but I may well have missed something.

fs/nsfs.c +6 -0
index 5d53476..26f6d94 100644
@@ -163,6 +163,7 @@ int open_related_ns(struct ns_common *ns,
 static long ns_ioctl(struct file *filp, unsigned int ioctl,
 			unsigned long arg)
 {
+	struct user_namespace *user_ns;
 	struct ns_common *ns = get_proc_ns(file_inode(filp));
 
 	switch (ioctl) {
@@ -174,6 +175,11 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
 		return open_related_ns(ns, ns->ops->get_parent);
 	case NS_GET_NSTYPE:
 		return ns->ops->type;
+	case NS_GET_CREATOR_UID:
+		if (ns->ops->type != CLONE_NEWUSER)
+			return -EINVAL;
+		user_ns = container_of(ns, struct user_namespace, ns);
+		return from_kuid_munged(current_user_ns(), user_ns->owner);
 	default:
 		return -ENOTTY;
 	}
include/uapi/linux/nsfs.h +5 -3
index 2b48df1..b3c6c78 100644
@@ -6,11 +6,13 @@
 #define NSIO	0xb7
 
 /* Returns a file descriptor that refers to an owning user namespace */
-#define NS_GET_USERNS	_IO(NSIO, 0x1)
+#define NS_GET_USERNS		_IO(NSIO, 0x1)
 /* Returns a file descriptor that refers to a parent namespace */
-#define NS_GET_PARENT	_IO(NSIO, 0x2)
+#define NS_GET_PARENT		_IO(NSIO, 0x2)
 /* Returns the type of namespace (CLONE_NEW* value) referred to by
    file descriptor */
-#define NS_GET_NSTYPE	_IO(NSIO, 0x3)
+#define NS_GET_NSTYPE		_IO(NSIO, 0x3)
+/* Get creator UID for a user namespace */
+#define NS_GET_CREATOR_UID	_IO(NSIO, 0x4)
 
 #endif /* __LINUX_NSFS_H */
Signature · 1 line
2.5.5
#3 lore raw

Re: [PATCH 2/2] nsfs: Add an ioctl() to return creator UID of a userns

Quoted 44 lines · Michael Kerrisk (man-pages)

# Some open questions about this patch below.
#
One of the rules regarding capabilities is:

A process that resides in the parent of the user namespace and whose effective user ID matches the owner of the namespace has all capabilities in the namespace.

Therefore, in order to write code that discovers whether process X has capabilities in namespace Y, we need a way to find out who the creator of a user namespace is. This patch adds an NS_GET_CREATOR_UID ioctl() that returns the (munged) UID of the creator of the user namespace referred to by the specified file descriptor.

If the supplied file descriptor does not refer to a user namespace, the operation fails with the error EINVAL.

---
 fs/nsfs.c                 | 6 ++++++
 include/uapi/linux/nsfs.h | 8 +++++---
 2 files changed, 11 insertions(+), 3 deletions(-)

Open questions:

* Would it be preferabe to separate the logic for NS_GET_CREATOR_UID into a small helper function?
* Is this a correct use of container_of()? I did not immediately see another way to get to the user_namespace struct, but I may well have missed something.

fs/nsfs.c +1 -0
index 5d53476..26f6d94 100644
@@ -163,6 +163,7 @@ int open_related_ns(struct ns_common *ns,
 static long ns_ioctl(struct file *filp, unsigned int ioctl,
 			unsigned long arg)
 {
+	struct user_namespace *user_ns;
 	struct ns_common *ns = get_proc_ns(file_inode(filp));
 
 	switch (ioctl) {

fs/nsfs.c static long ns_ioctl(struct file *filp, unsigned int ioctl, · line 182

Quoted 8 lines
fs/nsfs.c +5 -0
@@ -174,6 +175,11 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
 		return open_related_ns(ns, ns->ops->get_parent);
 	case NS_GET_NSTYPE:
 		return ns->ops->type;
+	case NS_GET_CREATOR_UID:
+		if (ns->ops->type != CLONE_NEWUSER)
+			return -EINVAL;
+		user_ns = container_of(ns, struct user_namespace, ns);
+		return from_kuid_munged(current_user_ns(), user_ns->owner);

uid_t is "unsigned int", ioctl() returns long, so it may be hard to distinguish user id-s from errors on x32.

off-topic: What is about user_ns->group? I can't find where it is used...

Quoted 27 lines
fs/nsfs.c +0 -0
 	default:
 		return -ENOTTY;
 	}
include/uapi/linux/nsfs.h +5 -3
index 2b48df1..b3c6c78 100644
@@ -6,11 +6,13 @@
 #define NSIO	0xb7
 
 /* Returns a file descriptor that refers to an owning user namespace */
-#define NS_GET_USERNS	_IO(NSIO, 0x1)
+#define NS_GET_USERNS		_IO(NSIO, 0x1)
 /* Returns a file descriptor that refers to a parent namespace */
-#define NS_GET_PARENT	_IO(NSIO, 0x2)
+#define NS_GET_PARENT		_IO(NSIO, 0x2)
 /* Returns the type of namespace (CLONE_NEW* value) referred to by
    file descriptor */
-#define NS_GET_NSTYPE	_IO(NSIO, 0x3)
+#define NS_GET_NSTYPE		_IO(NSIO, 0x3)
+/* Get creator UID for a user namespace */
+#define NS_GET_CREATOR_UID	_IO(NSIO, 0x4)
 
 #endif /* __LINUX_NSFS_H */
Signature · 1 line
2.5.5
#4 lore raw

Re: [PATCH 2/2] nsfs: Add an ioctl() to return creator UID of a userns

Hi Andrei,

Quoted 43 lines · Andrei Vagin
Quoted 42 lines · Michael Kerrisk (man-pages)

# Some open questions about this patch below.
#
One of the rules regarding capabilities is:

A process that resides in the parent of the user namespace and whose effective user ID matches the owner of the namespace has all capabilities in the namespace.

Therefore, in order to write code that discovers whether process X has capabilities in namespace Y, we need a way to find out who the creator of a user namespace is. This patch adds an NS_GET_CREATOR_UID ioctl() that returns the (munged) UID of the creator of the user namespace referred to by the specified file descriptor.

If the supplied file descriptor does not refer to a user namespace, the operation fails with the error EINVAL.

---
 fs/nsfs.c                 | 6 ++++++
 include/uapi/linux/nsfs.h | 8 +++++---
 2 files changed, 11 insertions(+), 3 deletions(-)

Open questions:

* Would it be preferabe to separate the logic for NS_GET_CREATOR_UID into a small helper function?
* Is this a correct use of container_of()? I did not immediately see another way to get to the user_namespace struct, but I may well have missed something.

fs/nsfs.c +1 -0
index 5d53476..26f6d94 100644
@@ -163,6 +163,7 @@ int open_related_ns(struct ns_common *ns,
 static long ns_ioctl(struct file *filp, unsigned int ioctl,
 			unsigned long arg)
 {
+	struct user_namespace *user_ns;
 	struct ns_common *ns = get_proc_ns(file_inode(filp));
 
Quoted 13 lines

fs/nsfs.c static long ns_ioctl(struct file *filp, unsigned int ioctl, · line 182

Quoted 10 lines
fs/nsfs.c +5 -0
 	switch (ioctl) {
@@ -174,6 +175,11 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
 		return open_related_ns(ns, ns->ops->get_parent);
 	case NS_GET_NSTYPE:
 		return ns->ops->type;
+	case NS_GET_CREATOR_UID:
+		if (ns->ops->type != CLONE_NEWUSER)
+			return -EINVAL;
+		user_ns = container_of(ns, struct user_namespace, ns);
+		return from_kuid_munged(current_user_ns(), user_ns->owner);

uid_t is "unsigned int", ioctl() returns long, so it may be hard to distinguish user id-s from errors on x32.

Good point. So, we could instead return the UID via a buffer pointed to by the ioctl() arg. That would seem better, right?

Quoted 1 line

off-topic: What is about user_ns->group? I can't find where it is used...

I've no idea. Like you, I can't see any place where it's being used.

Cheers,

Michael

Quoted 28 lines
Quoted 27 lines
fs/nsfs.c +0 -0
 	default:
 		return -ENOTTY;
 	}
include/uapi/linux/nsfs.h +5 -3
index 2b48df1..b3c6c78 100644
@@ -6,11 +6,13 @@
 #define NSIO	0xb7
 
 /* Returns a file descriptor that refers to an owning user namespace */
-#define NS_GET_USERNS	_IO(NSIO, 0x1)
+#define NS_GET_USERNS		_IO(NSIO, 0x1)
 /* Returns a file descriptor that refers to a parent namespace */
-#define NS_GET_PARENT	_IO(NSIO, 0x2)
+#define NS_GET_PARENT		_IO(NSIO, 0x2)
 /* Returns the type of namespace (CLONE_NEW* value) referred to by
    file descriptor */
-#define NS_GET_NSTYPE	_IO(NSIO, 0x3)
+#define NS_GET_NSTYPE		_IO(NSIO, 0x3)
+/* Get creator UID for a user namespace */
+#define NS_GET_CREATOR_UID	_IO(NSIO, 0x4)
 
 #endif /* __LINUX_NSFS_H */
Signature · 1 line
2.5.5
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#5 lore raw

Re: [PATCH 2/2] nsfs: Add an ioctl() to return creator UID of a userns

In reply to Andrei Vagin
Quoted 13 lines · Andrei Vagin

fs/nsfs.c static long ns_ioctl(struct file *filp, unsigned int ioctl, · line 182

Quoted 9 lines · Michael Kerrisk (man-pages)
fs/nsfs.c +5 -0
@@ -174,6 +175,11 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
 		return open_related_ns(ns, ns->ops->get_parent);
 	case NS_GET_NSTYPE:
 		return ns->ops->type;
+	case NS_GET_CREATOR_UID:
+		if (ns->ops->type != CLONE_NEWUSER)
+			return -EINVAL;
+		user_ns = container_of(ns, struct user_namespace, ns);
+		return from_kuid_munged(current_user_ns(), user_ns->owner);

uid_t is "unsigned int", ioctl() returns long, so it may be hard to distinguish user id-s from errors on x32.

Very good point.

Quoted 2 lines

off-topic: What is about user_ns->group? I can't find where it is used...

Over design. I put it in because I thought it might be useful. It turns out it never was used so we can clean things up and remove it. The group has never been exposed to userspace so no one will care.

Eric