mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com>
To: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: mtk.manpages@gmail.com, "Serge E. Hallyn" <serge@hallyn.com>,
	linux-api@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, Andrey Vagin <avagin@openvz.org>,
	James Bottomley <James.Bottomley@hansenpartnership.com>,
	"W. Trevor King" <wking@tremily.us>,
	Alexander Viro <viro@zeniv.linux.org.uk>
Subject: [PATCH v4 2/2] nsfs: Add an ioctl() to return owner UID of a userns
Date: Wed, 25 Jan 2017 14:04:15 +1300	[thread overview]
Message-ID: <628f8630-6631-8292-1841-2a45e74ae96f@gmail.com> (raw)
In-Reply-To: <2c27a76e-336d-e2ad-4b30-22e29249c2e9@gmail.com>

I'd like to write code that discovers the user namespace hierarchy on a
running system, and also shows who owns the various user namespaces.
Currently, there is no way of getting the owner UID of a user namespace.
Therefore, this patch adds a new NS_GET_CREATOR_UID ioctl() that fetches
the UID (as seen in the user namespace of the caller) 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. If the owner UID does
not have a mapping in the caller's user namespace, the operation
fails with the error EOVERFLOW. (Failing with EOVERFLOW, rather than
returning the overflow UID, is probably easier to deal with in
user-space applications.)

Acked-by: Andrey Vagin <avagin@openvz.org>
Signed-off-by: Michael Kerrisk <mtk-manpages@gmail.com>

---

V4 changes:
* Improve comment describing NS_GET_OWNER_UID (thanks to
  W. Trevor King)
* Use uid_t (rather than unsigned int) as type for ioctl() argument.
* Rather than returning the overflow UID if the owner UID does not have
  a mapping in the caller's user namespace, fail with the error
  EOVERFLOW. This makes user-space coding a little easier.
  (Thanks to the suggestion from Eric Biederman.)

V3 changes:
* Fixed data type of local variable 'uid'; thanks to Andrei Vagin.

V2 changes:
* Renamed ioctl() from NS_GET_CREATOR_UID to NS_GET_OWNER_UID, at the
  suggestion of Eric Biederman.
* Make ioctl() return UID via buffer pointed to by argp. (Returning
  the UID via the result value could lead to problems since a large
  unsigned int UID might be misinterpreted as an error.) Thanks to
  Andrei Vagin for pointing this out.
---
 fs/nsfs.c                 | 13 +++++++++++++
 include/uapi/linux/nsfs.h |  8 +++++---
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/fs/nsfs.c b/fs/nsfs.c
index 5d53476..4842617 100644
--- a/fs/nsfs.c
+++ b/fs/nsfs.c
@@ -7,6 +7,7 @@
 #include <linux/seq_file.h>
 #include <linux/user_namespace.h>
 #include <linux/nsfs.h>
+#include <linux/uaccess.h>
 
 static struct vfsmount *nsfs_mnt;
 
@@ -163,7 +164,10 @@ 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));
+	uid_t __user *argp;
+	uid_t uid;
 
 	switch (ioctl) {
 	case NS_GET_USERNS:
@@ -174,6 +178,15 @@ 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_OWNER_UID:
+		if (ns->ops->type != CLONE_NEWUSER)
+			return -EINVAL;
+		user_ns = container_of(ns, struct user_namespace, ns);
+		argp = (uid_t __user *) arg;
+		uid = from_kuid(current_user_ns(), user_ns->owner);
+		if (uid == (uid_t)-1)
+			return -EOVERFLOW;
+		return put_user(uid, argp);
 	default:
 		return -ENOTTY;
 	}
diff --git a/include/uapi/linux/nsfs.h b/include/uapi/linux/nsfs.h
index 2b48df1..1a3ca79 100644
--- a/include/uapi/linux/nsfs.h
+++ b/include/uapi/linux/nsfs.h
@@ -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 owner UID (in the caller's user namespace) for a user namespace */
+#define NS_GET_OWNER_UID	_IO(NSIO, 0x4)
 
 #endif /* __LINUX_NSFS_H */
-- 
2.5.5

      parent reply	other threads:[~2017-01-25  1:05 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-23  9:54 [PATCH v2 0/2] Add further ioctl() operations for namespace discovery Michael Kerrisk (man-pages)
2017-01-24 21:34 ` Michael Kerrisk (man-pages)
2017-01-24 22:41   ` Eric W. Biederman
2017-01-25  0:39     ` Michael Kerrisk (man-pages)
2017-01-25  1:03 ` [PATCH v4 " Michael Kerrisk (man-pages)
2017-01-25  1:58   ` Eric W. Biederman
2017-01-25  2:24     ` Michael Kerrisk (man-pages)
2017-01-25  2:26       ` Eric W. Biederman
2017-01-25  2:28         ` Eric W. Biederman
2017-01-25  3:50           ` Michael Kerrisk (man-pages)
2017-01-26  4:23             ` Eric W. Biederman
2017-02-03  2:34               ` Eric W. Biederman
2017-02-08 14:13                 ` Michael Kerrisk (man-pages)
     [not found] ` <2c27a76e-336d-e2ad-4b30-22e29249c2e9@gmail.com>
2017-01-25  1:03   ` [PATCH v4 1/2] nsfs: Add an ioctl() to return the namespace type Michael Kerrisk (man-pages)
2017-01-25  1:04   ` Michael Kerrisk (man-pages) [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=628f8630-6631-8292-1841-2a45e74ae96f@gmail.com \
    --to=mtk.manpages@gmail.com \
    --cc=James.Bottomley@hansenpartnership.com \
    --cc=avagin@openvz.org \
    --cc=ebiederm@xmission.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=serge@hallyn.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=wking@tremily.us \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®