]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
pidfs: simplify PIDFD_GET_<type>_NAMESPACE ioctls
authorChristian Brauner <brauner@kernel.org>
Mon, 17 Nov 2025 12:36:13 +0000 (13:36 +0100)
committerChristian Brauner <brauner@kernel.org>
Mon, 17 Nov 2025 15:23:13 +0000 (16:23 +0100)
We have reworked namespaces sufficiently that all this special-casing
shouldn't be needed anymore

Link: https://patch.msgid.link/20251117-eidesstattlich-apotheke-36d2e644079f@brauner
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/pidfs.c

index db236427fc2cee8603fb52f5278d0d114437ab2d..78dee3c201afb944496e04b3a820c33e7e936c58 100644 (file)
@@ -454,7 +454,6 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
        struct task_struct *task __free(put_task) = NULL;
        struct nsproxy *nsp __free(put_nsproxy) = NULL;
        struct ns_common *ns_common = NULL;
-       struct pid_namespace *pid_ns;
 
        if (!pidfs_ioctl_valid(cmd))
                return -ENOIOCTLCMD;
@@ -496,66 +495,64 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
        switch (cmd) {
        /* Namespaces that hang of nsproxy. */
        case PIDFD_GET_CGROUP_NAMESPACE:
-               if (IS_ENABLED(CONFIG_CGROUPS)) {
-                       get_cgroup_ns(nsp->cgroup_ns);
-                       ns_common = to_ns_common(nsp->cgroup_ns);
-               }
+               if (!ns_ref_get(nsp->cgroup_ns))
+                       break;
+               ns_common = to_ns_common(nsp->cgroup_ns);
                break;
        case PIDFD_GET_IPC_NAMESPACE:
-               if (IS_ENABLED(CONFIG_IPC_NS)) {
-                       get_ipc_ns(nsp->ipc_ns);
-                       ns_common = to_ns_common(nsp->ipc_ns);
-               }
+               if (!ns_ref_get(nsp->ipc_ns))
+                       break;
+               ns_common = to_ns_common(nsp->ipc_ns);
                break;
        case PIDFD_GET_MNT_NAMESPACE:
-               get_mnt_ns(nsp->mnt_ns);
+               if (!ns_ref_get(nsp->mnt_ns))
+                       break;
                ns_common = to_ns_common(nsp->mnt_ns);
                break;
        case PIDFD_GET_NET_NAMESPACE:
-               if (IS_ENABLED(CONFIG_NET_NS)) {
-                       ns_common = to_ns_common(nsp->net_ns);
-                       get_net_ns(ns_common);
-               }
+               if (!ns_ref_get(nsp->net_ns))
+                       break;
+               ns_common = to_ns_common(nsp->net_ns);
                break;
        case PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE:
-               if (IS_ENABLED(CONFIG_PID_NS)) {
-                       get_pid_ns(nsp->pid_ns_for_children);
-                       ns_common = to_ns_common(nsp->pid_ns_for_children);
-               }
+               if (!ns_ref_get(nsp->pid_ns_for_children))
+                       break;
+               ns_common = to_ns_common(nsp->pid_ns_for_children);
                break;
        case PIDFD_GET_TIME_NAMESPACE:
-               if (IS_ENABLED(CONFIG_TIME_NS)) {
-                       get_time_ns(nsp->time_ns);
-                       ns_common = to_ns_common(nsp->time_ns);
-               }
+               if (!ns_ref_get(nsp->time_ns))
+                       break;
+               ns_common = to_ns_common(nsp->time_ns);
                break;
        case PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE:
-               if (IS_ENABLED(CONFIG_TIME_NS)) {
-                       get_time_ns(nsp->time_ns_for_children);
-                       ns_common = to_ns_common(nsp->time_ns_for_children);
-               }
+               if (!ns_ref_get(nsp->time_ns_for_children))
+                       break;
+               ns_common = to_ns_common(nsp->time_ns_for_children);
                break;
        case PIDFD_GET_UTS_NAMESPACE:
-               if (IS_ENABLED(CONFIG_UTS_NS)) {
-                       get_uts_ns(nsp->uts_ns);
-                       ns_common = to_ns_common(nsp->uts_ns);
-               }
+               if (!ns_ref_get(nsp->uts_ns))
+                       break;
+               ns_common = to_ns_common(nsp->uts_ns);
                break;
        /* Namespaces that don't hang of nsproxy. */
        case PIDFD_GET_USER_NAMESPACE:
-               if (IS_ENABLED(CONFIG_USER_NS)) {
-                       rcu_read_lock();
-                       ns_common = to_ns_common(get_user_ns(task_cred_xxx(task, user_ns)));
-                       rcu_read_unlock();
+               scoped_guard(rcu) {
+                       struct user_namespace *user_ns;
+
+                       user_ns = task_cred_xxx(task, user_ns);
+                       if (!ns_ref_get(user_ns))
+                               break;
+                       ns_common = to_ns_common(user_ns);
                }
                break;
        case PIDFD_GET_PID_NAMESPACE:
-               if (IS_ENABLED(CONFIG_PID_NS)) {
-                       rcu_read_lock();
+               scoped_guard(rcu) {
+                       struct pid_namespace *pid_ns;
+
                        pid_ns = task_active_pid_ns(task);
-                       if (pid_ns)
-                               ns_common = to_ns_common(get_pid_ns(pid_ns));
-                       rcu_read_unlock();
+                       if (!ns_ref_get(pid_ns))
+                               break;
+                       ns_common = to_ns_common(pid_ns);
                }
                break;
        default: