Skip to content

Commit 75ddaa4

Browse files
committed
pidfs: protect PIDFD_GET_* ioctls() via ifdef
We originally protected PIDFD_GET_<ns-type>_NAMESPACE ioctls() through ifdefs and recent rework made it possible to drop them. There was an oversight though. When the relevant namespace is turned off ns->ops will be NULL so even though opening a file descriptor is perfectly legitimate it would fail during inode eviction when the file was closed. The simple fix would be to check ns->ops for NULL and continue allow to retrieve namespace fds from pidfds but we don't allow retrieving them when the relevant namespace type is turned off. So keep the simplification but add the ifdefs back in. Link: https://lore.kernel.org/20251222214907.GA189632@quark Link: https://patch.msgid.link/20251224-ununterbrochen-gagen-ea949b83f8f2@brauner Fixes: a71e4f1 ("pidfs: simplify PIDFD_GET_<type>_NAMESPACE ioctls") Tested-by: Brendan Jackman <jackmanb@kernel.org> Tested-by: Eric Biggers <ebiggers@kernel.org> Reported-by: Eric Biggers <ebiggers@kernel.org> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 78c8500 commit 75ddaa4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

fs/pidfs.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,47 +517,62 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
517517
switch (cmd) {
518518
/* Namespaces that hang of nsproxy. */
519519
case PIDFD_GET_CGROUP_NAMESPACE:
520+
#ifdef CONFIG_CGROUPS
520521
if (!ns_ref_get(nsp->cgroup_ns))
521522
break;
522523
ns_common = to_ns_common(nsp->cgroup_ns);
524+
#endif
523525
break;
524526
case PIDFD_GET_IPC_NAMESPACE:
527+
#ifdef CONFIG_IPC_NS
525528
if (!ns_ref_get(nsp->ipc_ns))
526529
break;
527530
ns_common = to_ns_common(nsp->ipc_ns);
531+
#endif
528532
break;
529533
case PIDFD_GET_MNT_NAMESPACE:
530534
if (!ns_ref_get(nsp->mnt_ns))
531535
break;
532536
ns_common = to_ns_common(nsp->mnt_ns);
533537
break;
534538
case PIDFD_GET_NET_NAMESPACE:
539+
#ifdef CONFIG_NET_NS
535540
if (!ns_ref_get(nsp->net_ns))
536541
break;
537542
ns_common = to_ns_common(nsp->net_ns);
543+
#endif
538544
break;
539545
case PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE:
546+
#ifdef CONFIG_PID_NS
540547
if (!ns_ref_get(nsp->pid_ns_for_children))
541548
break;
542549
ns_common = to_ns_common(nsp->pid_ns_for_children);
550+
#endif
543551
break;
544552
case PIDFD_GET_TIME_NAMESPACE:
553+
#ifdef CONFIG_TIME_NS
545554
if (!ns_ref_get(nsp->time_ns))
546555
break;
547556
ns_common = to_ns_common(nsp->time_ns);
557+
#endif
548558
break;
549559
case PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE:
560+
#ifdef CONFIG_TIME_NS
550561
if (!ns_ref_get(nsp->time_ns_for_children))
551562
break;
552563
ns_common = to_ns_common(nsp->time_ns_for_children);
564+
#endif
553565
break;
554566
case PIDFD_GET_UTS_NAMESPACE:
567+
#ifdef CONFIG_UTS_NS
555568
if (!ns_ref_get(nsp->uts_ns))
556569
break;
557570
ns_common = to_ns_common(nsp->uts_ns);
571+
#endif
558572
break;
559573
/* Namespaces that don't hang of nsproxy. */
560574
case PIDFD_GET_USER_NAMESPACE:
575+
#ifdef CONFIG_USER_NS
561576
scoped_guard(rcu) {
562577
struct user_namespace *user_ns;
563578

@@ -566,8 +581,10 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
566581
break;
567582
ns_common = to_ns_common(user_ns);
568583
}
584+
#endif
569585
break;
570586
case PIDFD_GET_PID_NAMESPACE:
587+
#ifdef CONFIG_PID_NS
571588
scoped_guard(rcu) {
572589
struct pid_namespace *pid_ns;
573590

@@ -576,6 +593,7 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
576593
break;
577594
ns_common = to_ns_common(pid_ns);
578595
}
596+
#endif
579597
break;
580598
default:
581599
return -ENOIOCTLCMD;

0 commit comments

Comments
 (0)