[PATCH V2] SELinux: Check correct permissions for FS_IOC32_*

From: Alfred Piccioni
Date: Wed Sep 06 2023 - 07:59:47 EST


Some ioctl commands do not require ioctl permission, but are routed to
other permissions such as FILE_GETATTR or FILE_SETATTR. This routing is
done by comparing the ioctl cmd to a set of 64-bit flags (FS_IOC_*).

However, if a 32-bit process is running on a 64-bit kernel, it emits
32-bit flags (FS_IOC32_*) for certain ioctl operations. These flags are
being checked erroneously, which leads to these ioctl operations being
routed to the ioctl permission, rather than the correct file permissions.

Two possible solutions exist:

- Trim parameter "cmd" to a u16 so that only the last two bytes are
checked in the case statement.

- Explicitly add the FS_IOC32_* codes to the case statement.

Solution 2 was chosen because it is a minimal explicit change. Solution
1 is a more elegant change, but is less explicit, as the switch
statement appears to only check the FS_IOC_* codes upon first reading.

Fixes: 0b24dcb7f2f7 ("Revert "selinux: simplify ioctl checking"")
Signed-off-by: Alfred Piccioni <alpic@xxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
V1->V2: Cleaned up some typos and added tag for -stable tree inclusion.

security/selinux/hooks.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index d06e350fedee..bba83f437a1d 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -3644,11 +3644,15 @@ static int selinux_file_ioctl(struct file *file, unsigned int cmd,
case FIGETBSZ:
case FS_IOC_GETFLAGS:
case FS_IOC_GETVERSION:
+ case FS_IOC32_GETFLAGS:
+ case FS_IOC32_GETVERSION:
error = file_has_perm(cred, file, FILE__GETATTR);
break;

case FS_IOC_SETFLAGS:
case FS_IOC_SETVERSION:
+ case FS_IOC32_SETFLAGS:
+ case FS_IOC32_SETVERSION:
error = file_has_perm(cred, file, FILE__SETATTR);
break;


base-commit: 50a510a78287c15cee644f345ef8bac8977986a7
--
2.42.0.283.g2d96d420d3-goog