[PATCH] net: always initialize sysctl ownership

From: Thomas Weißschuh
Date: Fri Mar 15 2024 - 12:20:47 EST


The sysctl core does not initialize these fields when the set_ownership
callback is present.
So always do it in the callback.

Fixes: e79c6a4fc923 ("net: make net namespace sysctls belong to container's owner")
Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
---
net/sysctl_net.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/net/sysctl_net.c b/net/sysctl_net.c
index 051ed5f6fc93..03e320ddacc9 100644
--- a/net/sysctl_net.c
+++ b/net/sysctl_net.c
@@ -62,12 +62,10 @@ static void net_ctl_set_ownership(struct ctl_table_header *head,
kgid_t ns_root_gid;

ns_root_uid = make_kuid(net->user_ns, 0);
- if (uid_valid(ns_root_uid))
- *uid = ns_root_uid;
+ *uid = uid_valid(ns_root_uid) ? ns_root_uid : GLOBAL_ROOT_UID;

ns_root_gid = make_kgid(net->user_ns, 0);
- if (gid_valid(ns_root_gid))
- *gid = ns_root_gid;
+ *gid = gid_valid(ns_root_gid) ? ns_root_gid : GLOBAL_ROOT_GID;
}

static struct ctl_table_root net_sysctl_root = {

---
base-commit: e5eb28f6d1afebed4bb7d740a797d0390bd3a357
change-id: 20240315-sysctl-net-ownership-bc4e17eaeea6

Best regards,
--
Thomas Weißschuh <linux@xxxxxxxxxxxxxx>