]> www.infradead.org Git - users/hch/xfs.git/commitdiff
pidfs: improve ioctl handling
authorChristian Brauner <brauner@kernel.org>
Tue, 4 Feb 2025 13:51:20 +0000 (14:51 +0100)
committerChristian Brauner <brauner@kernel.org>
Fri, 7 Feb 2025 09:27:28 +0000 (10:27 +0100)
Pidfs supports extensible and non-extensible ioctls. The extensible
ioctls need to check for the ioctl number itself not just the ioctl
command otherwise both backward- and forward compatibility are broken.

The pidfs ioctl handler also needs to look at the type of the ioctl
command to guard against cases where "[...] a daemon receives some
random file descriptor from a (potentially less privileged) client and
expects the FD to be of some specific type, it might call ioctl() on
this FD with some type-specific command and expect the call to fail if
the FD is of the wrong type; but due to the missing type check, the
kernel instead performs some action that userspace didn't expect."
(cf. [1]]

Link: https://lore.kernel.org/r/20250204-work-pidfs-ioctl-v1-1-04987d239575@kernel.org
Link: https://lore.kernel.org/r/CAG48ez2K9A5GwtgqO31u9ZL292we8ZwAA=TJwwEv7wRuJ3j4Lw@mail.gmail.com
Fixes: 8ce352818820 ("pidfs: check for valid ioctl commands")
Acked-by: Luca Boccassi <luca.boccassi@gmail.com>
Reported-by: Jann Horn <jannh@google.com>
Cc: stable@vger.kernel.org # v6.13; please backport with 8ce352818820 ("pidfs: check for valid ioctl commands")
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/pidfs.c

index 049352f973de38967ad35c6440b9480e7e2e2775..63f9699ebac36873d83fbf253846ae09e87b91e7 100644 (file)
@@ -287,7 +287,6 @@ static bool pidfs_ioctl_valid(unsigned int cmd)
        switch (cmd) {
        case FS_IOC_GETVERSION:
        case PIDFD_GET_CGROUP_NAMESPACE:
-       case PIDFD_GET_INFO:
        case PIDFD_GET_IPC_NAMESPACE:
        case PIDFD_GET_MNT_NAMESPACE:
        case PIDFD_GET_NET_NAMESPACE:
@@ -300,6 +299,17 @@ static bool pidfs_ioctl_valid(unsigned int cmd)
                return true;
        }
 
+       /* Extensible ioctls require some more careful checks. */
+       switch (_IOC_NR(cmd)) {
+       case _IOC_NR(PIDFD_GET_INFO):
+               /*
+                * Try to prevent performing a pidfd ioctl when someone
+                * erronously mistook the file descriptor for a pidfd.
+                * This is not perfect but will catch most cases.
+                */
+               return (_IOC_TYPE(cmd) == _IOC_TYPE(PIDFD_GET_INFO));
+       }
+
        return false;
 }