Skip to contentSkip to conversation

Clarify the rootid_owns_currentns

PATCH In review Serge E. Hallyn 3 participants 7 messages Nov 14, 2025 – Nov 19, 2025

Subsystems (2)
lore
Share & tools
lore

Serge E. Hallyn

author maintainer
#0 lore raw

PATCH 1/1 Clarify the rootid_owns_currentns

Split most of the rootid_owns_currentns() functionality into a more generic rootid_owns_ns() function which will be easier to write tests for.

Rename the functions and variables to make clear that the ids being tested could be any uid.

CC Ryan Foster <[email protected]>
CC Christian Brauner <[email protected]>
---
 security/commoncap.c | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)
security/commoncap.c +23 -12
index 6bd4adeb4795..8a81fdc12cbe 100644
@@ -358,17 +358,18 @@ int cap_inode_killpriv(struct mnt_idmap *idmap, struct dentry *dentry)
 	return error;
 }
 
-static bool rootid_owns_currentns(vfsuid_t rootvfsuid)
+/**
+ * kuid_root_in_ns - check whether the given kuid is root in the given ns
+ *
+ * @kuid - the kuid to be tested
+ * @ns - the user namespace to test against
+ *
+ * Returns true if @kuid represents the root user in @ns, false otherwise.
+ */
+static bool kuid_root_in_ns(kuid_t kuid, struct user_namespace *ns)
 {
-	struct user_namespace *ns;
-	kuid_t kroot;
-
-	if (!vfsuid_valid(rootvfsuid))
-		return false;
-
-	kroot = vfsuid_into_kuid(rootvfsuid);
-	for (ns = current_user_ns();; ns = ns->parent) {
-		if (from_kuid(ns, kroot) == 0)
+	for (;; ns = ns->parent) {
+		if (from_kuid(ns, kuid) == 0)
 			return true;
 		if (ns == &init_user_ns)
 			break;
@@ -377,6 +378,16 @@ static bool rootid_owns_currentns(vfsuid_t rootvfsuid)
 	return false;
 }
 
+static bool vfsuid_root_in_currentns(vfsuid_t vfsuid)
+{
+	kuid_t kuid;
+
+	if (!vfsuid_valid(vfsuid))
+		return false;
+	kuid = vfsuid_into_kuid(vfsuid);
+	return kuid_root_in_ns(kuid, current_user_ns());
+}
+
 static __u32 sansflags(__u32 m)
 {
 	return m & ~VFS_CAP_FLAGS_EFFECTIVE;
@@ -481,7 +492,7 @@ int cap_inode_getsecurity(struct mnt_idmap *idmap,
 		goto out_free;
 	}
 
-	if (!rootid_owns_currentns(vfsroot)) {
+	if (!vfsuid_root_in_currentns(vfsroot)) {
 		size = -EOVERFLOW;
 		goto out_free;
 	}
@@ -722,7 +733,7 @@ int get_vfs_caps_from_disk(struct mnt_idmap *idmap,
 	/* Limit the caps to the mounter of the filesystem
 	 * or the more limited uid specified in the xattr.
 	 */
-	if (!rootid_owns_currentns(rootvfsuid))
+	if (!vfsuid_root_in_currentns(rootvfsuid))
 		return -ENODATA;
 
 	cpu_caps->permitted.val = le32_to_cpu(caps->data[0].permitted);
Signature · 1 line
2.34.1
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Serge,

kernel test robot noticed the following build warnings:

[auto build test WARNING on linus/master]
[also build test WARNING on brauner-vfs/vfs.all v6.18-rc5 next-20251114] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Serge-E-Hallyn/Clarify-the-rootid_owns_currentns/20251115-053655
base:   linus/master
patch link:    https://lore.kernel.org/r/aRegH8P4cPlzzlX9%40mail.hallyn.com
patch subject: [PATCH] Clarify the rootid_owns_currentns
config: alpha-allnoconfig (https://download.01.org/0day-ci/archive/20251115/[email protected]/config)
compiler: alpha-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251115/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> Warning: security/commoncap.c:369 function parameter 'kuid' not described in 'kuid_root_in_ns'
>> Warning: security/commoncap.c:369 function parameter 'ns' not described in 'kuid_root_in_ns'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#2 lore raw
In reply to Serge E. Hallyn
Quoted 10 lines · Serge E. Hallyn

Split most of the rootid_owns_currentns() functionality into a more generic rootid_owns_ns() function which will be easier to write tests for.

Rename the functions and variables to make clear that the ids being tested could be any uid.

CC Ryan Foster <[email protected]>
CC Christian Brauner <[email protected]>

Paul, Christian, let me know if you have any objections, else I will queue this up in caps-next.

Ryan, based on this you would be able to do more useful unit ktests:
you could create some simple user namespaces with mappings which do or do not have uid 0 in ns mapped to the kuid you are querying.

Quoted 75 lines
---
 security/commoncap.c | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)
security/commoncap.c +23 -12
index 6bd4adeb4795..8a81fdc12cbe 100644
@@ -358,17 +358,18 @@ int cap_inode_killpriv(struct mnt_idmap *idmap, struct dentry *dentry)
 	return error;
 }
 
-static bool rootid_owns_currentns(vfsuid_t rootvfsuid)
+/**
+ * kuid_root_in_ns - check whether the given kuid is root in the given ns
+ *
+ * @kuid - the kuid to be tested
+ * @ns - the user namespace to test against
+ *
+ * Returns true if @kuid represents the root user in @ns, false otherwise.
+ */
+static bool kuid_root_in_ns(kuid_t kuid, struct user_namespace *ns)
 {
-	struct user_namespace *ns;
-	kuid_t kroot;
-
-	if (!vfsuid_valid(rootvfsuid))
-		return false;
-
-	kroot = vfsuid_into_kuid(rootvfsuid);
-	for (ns = current_user_ns();; ns = ns->parent) {
-		if (from_kuid(ns, kroot) == 0)
+	for (;; ns = ns->parent) {
+		if (from_kuid(ns, kuid) == 0)
 			return true;
 		if (ns == &init_user_ns)
 			break;
@@ -377,6 +378,16 @@ static bool rootid_owns_currentns(vfsuid_t rootvfsuid)
 	return false;
 }
 
+static bool vfsuid_root_in_currentns(vfsuid_t vfsuid)
+{
+	kuid_t kuid;
+
+	if (!vfsuid_valid(vfsuid))
+		return false;
+	kuid = vfsuid_into_kuid(vfsuid);
+	return kuid_root_in_ns(kuid, current_user_ns());
+}
+
 static __u32 sansflags(__u32 m)
 {
 	return m & ~VFS_CAP_FLAGS_EFFECTIVE;
@@ -481,7 +492,7 @@ int cap_inode_getsecurity(struct mnt_idmap *idmap,
 		goto out_free;
 	}
 
-	if (!rootid_owns_currentns(vfsroot)) {
+	if (!vfsuid_root_in_currentns(vfsroot)) {
 		size = -EOVERFLOW;
 		goto out_free;
 	}
@@ -722,7 +733,7 @@ int get_vfs_caps_from_disk(struct mnt_idmap *idmap,
 	/* Limit the caps to the mounter of the filesystem
 	 * or the more limited uid specified in the xattr.
 	 */
-	if (!rootid_owns_currentns(rootvfsuid))
+	if (!vfsuid_root_in_currentns(rootvfsuid))
 		return -ENODATA;
 
 	cpu_caps->permitted.val = le32_to_cpu(caps->data[0].permitted);
Signature · 1 line
2.34.1
maintainer
#3 lore raw
Quoted 8 lines · Serge E. Hallyn
Quoted 7 lines · Serge E. Hallyn

Split most of the rootid_owns_currentns() functionality into a more generic rootid_owns_ns() function which will be easier to write tests for.

Rename the functions and variables to make clear that the ids being tested could be any uid.

Quoted 6 lines
Quoted 3 lines
CC Ryan Foster <[email protected]>
CC Christian Brauner <[email protected]>

Paul, Christian, let me know if you have any objections, else I will queue this up in caps-next.

Seems reasonable to me, but it would be good to fix the parameter doc bug that the kernel test robot identified. I suspect it is just the extra vertical comment space between the top one line summary and the parameter list.

Signature · 1 line
paul-moore.com
author maintainer
#4 lore raw
Quoted 13 lines · Paul Moore
Quoted 12 lines · Serge E. Hallyn
Quoted 10 lines · Serge E. Hallyn

Split most of the rootid_owns_currentns() functionality into a more generic rootid_owns_ns() function which will be easier to write tests for.

Rename the functions and variables to make clear that the ids being tested could be any uid.

CC Ryan Foster <[email protected]>
CC Christian Brauner <[email protected]>
Quoted 7 lines
Quoted 2 lines

Paul, Christian, let me know if you have any objections, else I will queue this up in caps-next.

Seems reasonable to me, but it would be good to fix the parameter doc bug that the kernel test robot identified. I suspect it is just the extra vertical comment space between the top one line summary and the parameter list.

ooh, is that it. I couldn't for the life of me figure out what it was complaining about. Will try that, thanks.

author maintainer
#5 lore raw
In reply to Paul Moore
Quoted 13 lines · Paul Moore
Quoted 12 lines · Serge E. Hallyn
Quoted 10 lines · Serge E. Hallyn

Split most of the rootid_owns_currentns() functionality into a more generic rootid_owns_ns() function which will be easier to write tests for.

Rename the functions and variables to make clear that the ids being tested could be any uid.

CC Ryan Foster <[email protected]>
CC Christian Brauner <[email protected]>
Quoted 7 lines
Quoted 2 lines

Paul, Christian, let me know if you have any objections, else I will queue this up in caps-next.

Seems reasonable to me, but it would be good to fix the parameter doc bug that the kernel test robot identified. I suspect it is just the extra vertical comment space between the top one line summary and the parameter list.

Actually I think it was probably the use of - instead of : after the parameter name, but I went ahead and changed both, thanks.

Pushed the below patch for linux-next.

Subject: [PATCH 1/1] Clarify the rootid_owns_currentns

Split most of the rootid_owns_currentns() functionality into a more generic rootid_owns_ns() function which will be easier to write tests for.

Rename the functions and variables to make clear that the ids being tested could be any uid.

CC Ryan Foster <[email protected]>
CC Christian Brauner <[email protected]>

---
v2: change the function parameter documentation to mollify the bot.

---
 security/commoncap.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)
security/commoncap.c +22 -12
index 6bd4adeb4795..496e054c5d37 100644
@@ -358,17 +358,17 @@ int cap_inode_killpriv(struct mnt_idmap *idmap, struct dentry *dentry)
 	return error;
 }
 
-static bool rootid_owns_currentns(vfsuid_t rootvfsuid)
+/**
+ * kuid_root_in_ns - check whether the given kuid is root in the given ns
+ * @kuid: the kuid to be tested
+ * @ns: the user namespace to test against
+ *
+ * Returns true if @kuid represents the root user in @ns, false otherwise.
+ */
+static bool kuid_root_in_ns(kuid_t kuid, struct user_namespace *ns)
 {
-	struct user_namespace *ns;
-	kuid_t kroot;
-
-	if (!vfsuid_valid(rootvfsuid))
-		return false;
-
-	kroot = vfsuid_into_kuid(rootvfsuid);
-	for (ns = current_user_ns();; ns = ns->parent) {
-		if (from_kuid(ns, kroot) == 0)
+	for (;; ns = ns->parent) {
+		if (from_kuid(ns, kuid) == 0)
 			return true;
 		if (ns == &init_user_ns)
 			break;
@@ -377,6 +377,16 @@ static bool rootid_owns_currentns(vfsuid_t rootvfsuid)
 	return false;
 }
 
+static bool vfsuid_root_in_currentns(vfsuid_t vfsuid)
+{
+	kuid_t kuid;
+
+	if (!vfsuid_valid(vfsuid))
+		return false;
+	kuid = vfsuid_into_kuid(vfsuid);
+	return kuid_root_in_ns(kuid, current_user_ns());
+}
+
 static __u32 sansflags(__u32 m)
 {
 	return m & ~VFS_CAP_FLAGS_EFFECTIVE;
@@ -481,7 +491,7 @@ int cap_inode_getsecurity(struct mnt_idmap *idmap,
 		goto out_free;
 	}
 
-	if (!rootid_owns_currentns(vfsroot)) {
+	if (!vfsuid_root_in_currentns(vfsroot)) {
 		size = -EOVERFLOW;
 		goto out_free;
 	}
@@ -722,7 +732,7 @@ int get_vfs_caps_from_disk(struct mnt_idmap *idmap,
 	/* Limit the caps to the mounter of the filesystem
 	 * or the more limited uid specified in the xattr.
 	 */
-	if (!rootid_owns_currentns(rootvfsuid))
+	if (!vfsuid_root_in_currentns(rootvfsuid))
 		return -ENODATA;
 
 	cpu_caps->permitted.val = le32_to_cpu(caps->data[0].permitted);
Signature · 1 line
2.34.1
maintainer
#6 lore raw
Quoted 17 lines · Serge E. Hallyn
Quoted 16 lines · Paul Moore
Quoted 14 lines · Serge E. Hallyn
Quoted 10 lines · Serge E. Hallyn

Split most of the rootid_owns_currentns() functionality into a more generic rootid_owns_ns() function which will be easier to write tests for.

Rename the functions and variables to make clear that the ids being tested could be any uid.

CC Ryan Foster <[email protected]>
CC Christian Brauner <[email protected]>

Paul, Christian, let me know if you have any objections, else I will queue this up in caps-next.

Quoted 7 lines
Quoted 4 lines

Seems reasonable to me, but it would be good to fix the parameter doc bug that the kernel test robot identified. I suspect it is just the extra vertical comment space between the top one line summary and the parameter list.

Actually I think it was probably the use of - instead of : after the parameter name, but I went ahead and changed both, thanks.

Ah, yeah, at least one of those was it I'm sure.

Quoted 1 line

Pushed the below patch for linux-next.

Thanks.

Signature · 1 line
paul-moore.com