Skip to contentSkip to conversation

pidfs: improve ioctl handling

PATCH In review Christian Brauner 5 participants 6 messages Feb 4, 2025 – Feb 7, 2025

Subsystems (2)
lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

PATCH 1/1 pidfs: improve ioctl handling

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]]

---
Hey,

Jann reported that the pidfs extensible ioctl checking has two issues:

(1) We check for the ioctl command, not the number breaking forward and backward compatibility.

(2) We don't verify the type encoded in the ioctl to guard against ioctl number collisions.

This adresses both.

Greg, when this patch (or a version thereof) lands upstream then it needs to please be backported to v6.13 together with the already upstream commit 8ce352818820 ("pidfs: check for valid ioctl commands").

Christian

---
 fs/pidfs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
fs/pidfs.c +11 -1
index 049352f973de..63f9699ebac3 100644
@@ -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;
 }
 

---
base-commit: 6470d2c6d4233a781c67f842d3c066bf1cfa4fdc
change-id: 20250204-work-pidfs-ioctl-6ef79a65e36b

#1 lore raw
Quoted 25 lines · Christian Brauner

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]]

---
Hey,

Jann reported that the pidfs extensible ioctl checking has two issues:

(1) We check for the ioctl command, not the number breaking forward and backward compatibility.

Quoted 8 lines

(2) We don't verify the type encoded in the ioctl to guard against ioctl number collisions.

This adresses both.

Greg, when this patch (or a version thereof) lands upstream then it needs to please be backported to v6.13 together with the already upstream commit 8ce352818820 ("pidfs: check for valid ioctl commands").

Will do, thanks for the heads up.

greg k-h

#2 lore raw
In reply to Christian Brauner
Quoted 4 lines · Christian Brauner

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.

Quoted 8 lines

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]]

Thanks, this looks good to me.

Acked-by on v1 1/1
#3 lore raw
In reply to Christian Brauner
Quoted 29 lines · Christian Brauner

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]]

---
Hey,

Jann reported that the pidfs extensible ioctl checking has two issues:

(1) We check for the ioctl command, not the number breaking forward and backward compatibility.

(2) We don't verify the type encoded in the ioctl to guard against ioctl number collisions.

Quoted 7 lines

This adresses both.

Greg, when this patch (or a version thereof) lands upstream then it needs to please be backported to v6.13 together with the already upstream commit 8ce352818820 ("pidfs: check for valid ioctl commands").

Christian

Thanks for the fixes, LGTM

#4 lore raw
In reply to Christian Brauner
Quoted 13 lines · Christian Brauner

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]]

Quoted 3 lines

This is not a proper Fixes: tag.

Quoted 40 lines

---
Hey,

Jann reported that the pidfs extensible ioctl checking has two issues:

(1) We check for the ioctl command, not the number breaking forward and backward compatibility.

(2) We don't verify the type encoded in the ioctl to guard against ioctl number collisions.

This adresses both.

Greg, when this patch (or a version thereof) lands upstream then it needs to please be backported to v6.13 together with the already upstream commit 8ce352818820 ("pidfs: check for valid ioctl commands").

Christian

---
 fs/pidfs.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
fs/pidfs.c +1 -1
index 049352f973de..63f9699ebac3 100644
@@ -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. */

fs/pidfs.c static bool pidfs_ioctl_valid(unsigned int cmd) · line 312

Quoted 10 lines
fs/pidfs.c +10 -0
+	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));
+	}
+

This double-checks _IOC_TYPE(cmd) against _IOC_TYPE(PIDFD_GET_INFO)) due to the switch() and case statements. A simple

	return _IOC_TYPE(cmd) == _IOC_TYPE(PIDFD_GET_INFO);

for the entire block would have had the same result. Or at least you might have used a simple

		return true;

in the case statement if more are expected in the future.

[ reported by coverity ]

Guenter

author maintainer
#5 lore raw
Quoted 14 lines · Guenter Roeck
Quoted 13 lines · Christian Brauner

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]]

Quoted 5 lines
Quoted 3 lines

This is not a proper Fixes: tag.

Fixed.