From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751014AbdAXWjj (ORCPT ); Tue, 24 Jan 2017 17:39:39 -0500 Received: from out03.mta.xmission.com ([166.70.13.233]:47441 "EHLO out03.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750902AbdAXWjh (ORCPT ); Tue, 24 Jan 2017 17:39:37 -0500 From: ebiederm@xmission.com (Eric W. Biederman) To: "Michael Kerrisk \(man-pages\)" Cc: "Serge E. Hallyn" , linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Andrey Vagin , James Bottomley , "W. Trevor King" , Alexander Viro References: <69550fe9-5347-309c-b421-79c16a6300f6@gmail.com> Date: Wed, 25 Jan 2017 11:35:15 +1300 In-Reply-To: (Michael Kerrisk's message of "Tue, 17 Jan 2017 14:03:29 +1300") Message-ID: <87sho8gk7w.fsf@xmission.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1cW9kO-0003XZ-VN;;;mid=<87sho8gk7w.fsf@xmission.com>;;;hst=in01.mta.xmission.com;;;ip=101.100.131.98;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX1+lJZCWEqFEpR9jw8Xa29gaejFxTqDWv7c= X-SA-Exim-Connect-IP: 101.100.131.98 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 0.7 XMSubLong Long Subject * 0.0 T_TM2_M_HEADER_IN_MSG BODY: No description available. * 0.8 BAYES_50 BODY: Bayes spam probability is 40 to 60% * [score: 0.5000] * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa04 1397; Body=1 Fuz1=1 Fuz2=1] * 0.0 T_TooManySym_01 4+ unique symbols in subject * 0.0 T_TooManySym_02 5+ unique symbols in subject X-Spam-DCC: XMission; sa04 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ;"Michael Kerrisk \(man-pages\)" X-Spam-Relay-Country: X-Spam-Timing: total 5313 ms - load_scoreonly_sql: 0.05 (0.0%), signal_user_changed: 8 (0.2%), b_tie_ro: 7 (0.1%), parse: 1.23 (0.0%), extract_message_metadata: 16 (0.3%), get_uri_detail_list: 3.7 (0.1%), tests_pri_-1000: 6 (0.1%), tests_pri_-950: 1.52 (0.0%), tests_pri_-900: 1.17 (0.0%), tests_pri_-400: 29 (0.6%), check_bayes: 28 (0.5%), b_tokenize: 11 (0.2%), b_tok_get_all: 9 (0.2%), b_comp_prob: 2.7 (0.1%), b_tok_touch_all: 3.1 (0.1%), b_finish: 0.66 (0.0%), tests_pri_0: 421 (7.9%), check_dkim_signature: 0.56 (0.0%), check_dkim_adsp: 2.8 (0.1%), tests_pri_500: 4825 (90.8%), poll_dns_idle: 4818 (90.7%), rewrite_mail: 0.00 (0.0%) Subject: Re: [PATCH v3 2/2] nsfs: Add an ioctl() to return owner UID of a userns X-Spam-Flag: No X-SA-Exim-Version: 4.2.1 (built Thu, 05 May 2016 13:38:54 -0600) X-SA-Exim-Scanned: Yes (on in01.mta.xmission.com) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org "Michael Kerrisk (man-pages)" writes: > 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 an NS_GET_CREATOR_UID ioctl() > that fetches 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. > > Acked-by: Andrey Vagin > Signed-off-by: Michael Kerrisk > > --- > Open questions: > > Should the type for the ioctl() argument be changed? I mean, > make the following changes to the patch below: > > - unsigned int __user *argp; > + uid_t __user *argp; > > And further below, change: > > - argp = (unsigned int __user *) arg; > + argp = (uid_t __user *) arg; > > ? > > V3 changes: > * Fixed data type of local variable 'uid'; thanks to Andrei Vagin > > V2 changes: > * Renamed ioctl() from NS_CREATOR_UID to NS_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 | 11 +++++++++++ > include/uapi/linux/nsfs.h | 8 +++++--- > 2 files changed, 16 insertions(+), 3 deletions(-) > > diff --git a/fs/nsfs.c b/fs/nsfs.c > index 5d53476..63a4ad4 100644 > --- a/fs/nsfs.c > +++ b/fs/nsfs.c > @@ -7,6 +7,7 @@ > #include > #include > #include > +#include > > 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)); > + unsigned int __user *argp; > + uid_t uid; > > switch (ioctl) { > case NS_GET_USERNS: > @@ -174,6 +178,13 @@ 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 = (unsigned int __user *) arg; > + uid = from_kuid_munged(current_user_ns(), user_ns->owner); > + return put_user(uid, argp); There is a question of how do we want to handle a uid that does not map. For most interfaces that return uids the uid is one small part of it so it is better if return the unmapped uid value. Does this applie to NS_GET_OWNER_UID or do we perhaps want to do: user_ns = container_of(ns, struct user_namespace, ns); argp = (unsigned int __user *) arg; uid = from_kuid(current_user_ns(), user_ns->owner); if (uid == (uid_t)-1) return -EOVERFLOW; return put_user(uid, argp); Which of these variants would make handling this error easiest in your introspection code? I suspect return -EOVERFLOW would be easiest to handle. Eric > default: > return -ENOTTY; > } > diff --git a/include/uapi/linux/nsfs.h b/include/uapi/linux/nsfs.h > index 2b48df1..c4a925e 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 for a user namespace */ > +#define NS_GET_OWNER_UID _IO(NSIO, 0x4) > > #endif /* __LINUX_NSFS_H */