Skip to content

Commit e120b3c

Browse files
committed
landlock: Factor out IOCTL hooks
Compat and non-compat IOCTL hooks are almost the same, except to compare the IOCTL command. Factor out these two IOCTL hooks to highlight the difference and minimize audit changes (see next commit). Cc: Günther Noack <gnoack@google.com> Link: https://lore.kernel.org/r/20250320190717.2287696-14-mic@digikod.net Signed-off-by: Mickaël Salaün <mic@digikod.net>
1 parent 2fc80c6 commit e120b3c

1 file changed

Lines changed: 11 additions & 21 deletions

File tree

  • security/landlock

security/landlock/fs.c

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,8 +1698,8 @@ static int hook_file_truncate(struct file *const file)
16981698
return -EACCES;
16991699
}
17001700

1701-
static int hook_file_ioctl(struct file *file, unsigned int cmd,
1702-
unsigned long arg)
1701+
static int hook_file_ioctl_common(const struct file *const file,
1702+
const unsigned int cmd, const bool is_compat)
17031703
{
17041704
access_mask_t allowed_access = landlock_file(file)->allowed_access;
17051705

@@ -1715,33 +1715,23 @@ static int hook_file_ioctl(struct file *file, unsigned int cmd,
17151715
if (!is_device(file))
17161716
return 0;
17171717

1718-
if (is_masked_device_ioctl(cmd))
1718+
if (unlikely(is_compat) ? is_masked_device_ioctl_compat(cmd) :
1719+
is_masked_device_ioctl(cmd))
17191720
return 0;
17201721

17211722
return -EACCES;
17221723
}
17231724

1725+
static int hook_file_ioctl(struct file *file, unsigned int cmd,
1726+
unsigned long arg)
1727+
{
1728+
return hook_file_ioctl_common(file, cmd, false);
1729+
}
1730+
17241731
static int hook_file_ioctl_compat(struct file *file, unsigned int cmd,
17251732
unsigned long arg)
17261733
{
1727-
access_mask_t allowed_access = landlock_file(file)->allowed_access;
1728-
1729-
/*
1730-
* It is the access rights at the time of opening the file which
1731-
* determine whether IOCTL can be used on the opened file later.
1732-
*
1733-
* The access right is attached to the opened file in hook_file_open().
1734-
*/
1735-
if (allowed_access & LANDLOCK_ACCESS_FS_IOCTL_DEV)
1736-
return 0;
1737-
1738-
if (!is_device(file))
1739-
return 0;
1740-
1741-
if (is_masked_device_ioctl_compat(cmd))
1742-
return 0;
1743-
1744-
return -EACCES;
1734+
return hook_file_ioctl_common(file, cmd, true);
17451735
}
17461736

17471737
/*

0 commit comments

Comments
 (0)