Skip to content

Commit 74adf9e

Browse files
committed
Merge tag 'vfs-6.15-rc1.nsfs' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
Pull vfs nsfs updates from Christian Brauner: "This contains non-urgent fixes for nsfs to validate ioctls before performing any relevant operations. We alredy did this for a few other filesystems last cycle" * tag 'vfs-6.15-rc1.nsfs' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs: selftests/nsfs: add ioctl validation tests nsfs: validate ioctls
2 parents aaca83f + 58c6cbd commit 74adf9e

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

fs/nsfs.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,49 @@ static int copy_ns_info_to_user(const struct mnt_namespace *mnt_ns,
151151
return 0;
152152
}
153153

154+
static bool nsfs_ioctl_valid(unsigned int cmd)
155+
{
156+
switch (cmd) {
157+
case NS_GET_USERNS:
158+
case NS_GET_PARENT:
159+
case NS_GET_NSTYPE:
160+
case NS_GET_OWNER_UID:
161+
case NS_GET_MNTNS_ID:
162+
case NS_GET_PID_FROM_PIDNS:
163+
case NS_GET_TGID_FROM_PIDNS:
164+
case NS_GET_PID_IN_PIDNS:
165+
case NS_GET_TGID_IN_PIDNS:
166+
return (_IOC_TYPE(cmd) == _IOC_TYPE(cmd));
167+
}
168+
169+
/* Extensible ioctls require some extra handling. */
170+
switch (_IOC_NR(cmd)) {
171+
case _IOC_NR(NS_MNT_GET_INFO):
172+
case _IOC_NR(NS_MNT_GET_NEXT):
173+
case _IOC_NR(NS_MNT_GET_PREV):
174+
return (_IOC_TYPE(cmd) == _IOC_TYPE(cmd));
175+
}
176+
177+
return false;
178+
}
179+
154180
static long ns_ioctl(struct file *filp, unsigned int ioctl,
155181
unsigned long arg)
156182
{
157183
struct user_namespace *user_ns;
158184
struct pid_namespace *pid_ns;
159185
struct task_struct *tsk;
160-
struct ns_common *ns = get_proc_ns(file_inode(filp));
186+
struct ns_common *ns;
161187
struct mnt_namespace *mnt_ns;
162188
bool previous = false;
163189
uid_t __user *argp;
164190
uid_t uid;
165191
int ret;
166192

193+
if (!nsfs_ioctl_valid(ioctl))
194+
return -ENOIOCTLCMD;
195+
196+
ns = get_proc_ns(file_inode(filp));
167197
switch (ioctl) {
168198
case NS_GET_USERNS:
169199
return open_related_ns(ns, ns_get_owner);

tools/testing/selftests/filesystems/nsfs/iterate_mntns.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#define _GNU_SOURCE
55
#include <fcntl.h>
6+
#include <linux/auto_dev-ioctl.h>
7+
#include <linux/errno.h>
68
#include <sched.h>
79
#include <stdio.h>
810
#include <string.h>
@@ -146,4 +148,16 @@ TEST_F(iterate_mount_namespaces, iterate_backward)
146148
}
147149
}
148150

151+
TEST_F(iterate_mount_namespaces, nfs_valid_ioctl)
152+
{
153+
ASSERT_NE(ioctl(self->fd_mnt_ns[0], AUTOFS_DEV_IOCTL_OPENMOUNT, NULL), 0);
154+
ASSERT_EQ(errno, ENOTTY);
155+
156+
ASSERT_NE(ioctl(self->fd_mnt_ns[0], AUTOFS_DEV_IOCTL_CLOSEMOUNT, NULL), 0);
157+
ASSERT_EQ(errno, ENOTTY);
158+
159+
ASSERT_NE(ioctl(self->fd_mnt_ns[0], AUTOFS_DEV_IOCTL_READY, NULL), 0);
160+
ASSERT_EQ(errno, ENOTTY);
161+
}
162+
149163
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)