Skip to contentSkip to conversation

lsm: Replace security_sb_mount with granular mount hooks

PATCH Superseded Song Liu 2 participants 12 messages May 15 – May 27

Subsystems (8)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Song Liu

author reviewer
#0 lore raw

v4 cover lsm: Replace security_sb_mount with granular mount hooks

This series replaces the monolithic security_sb_mount() hook with per-operation mount hooks, addressing two main issues:

1. TOCTOU: security_sb_mount() receives dev_name as a string, which LSMs like AppArmor and Tomoyo re-resolve via kern_path(). The new hooks pass pre-resolved struct path pointers where possible (bind mount, move mount), eliminating the double-resolution.

2. Conflation: security_sb_mount() handles bind, new mount, remount, move, propagation changes, and mount reconfiguration through a single hook, requiring LSMs to dispatch on flags internally. The new hooks are called at the operation level with appropriate context.

The new hooks are:

  mount_bind        - bind mount (pre-resolved source path)
  mount_new         - new filesystem mount (with fs_context)
  mount_remount     - filesystem remount (with fs_context)

mount_reconfigure - mount flag reconfiguration (MS_REMOUNT|MS_BIND)
mount_move - move mount (pre-resolved paths)
mount_change_type - propagation type changes

mount_new and mount_remount are called after parse_monolithic_mount_data(), so LSMs have access to the fs_context with parsed mount options. They also receive the original mount(2) flags and data pointer for LSMs (AppArmor, Tomoyo) that need them for policy matching.

The series also replaces security_move_mount() with the new mount_move hook, unifying the old mount(2) MS_MOVE path with the move_mount(2) syscall path.

All existing LSM behaviors are preserved:
AppArmor: same policy matching, TOCTOU fixed for bind/move
SELinux: same permission checks (FILE__MOUNTON, FILESYSTEM__REMOUNT)
Landlock: same deny-all for sandboxed processes
Tomoyo: same policy matching, TOCTOU fixed for bind/move, unused
data_page parameter removed

This work is inspired by earlier discussions:

[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/linux-security-module/[email protected]/

Changes v3 => v4:
1. Move LSM_HOOK_INIT(move_mount, ...) removal from patch 7/7 to each per-LSM conversion patch (3/7, 4/7, 5/7). (Paul Moore)
2. Add kdoc comments to tomoyo mount hook functions and rename tomoyo_move_mount to tomoyo_mount_move in patch 6/7. (Tetsuo Handa)
3. Add Acked-by from Tetsuo Handa to patch 6/7.

v3: https://lore.kernel.org/linux-security-module/[email protected]/

Changes v2 => v3:
1. Rebase.
2. Move security_mount_move() call in vfs_move_mount() from patch 7/7 to patch 1/7. (Paul Moore)

v2: https://lore.kernel.org/linux-security-module/[email protected]/

Changes v1 => v2:
1. Rebase.
2. Add Reviewed-by and Tested-by from Stephen Smalley.

v1: https://lore.kernel.org/linux-security-module/[email protected]/

Song Liu (7):
lsm: Add granular mount hooks to replace security_sb_mount
apparmor: Remove redundant MS_MGC_MSK stripping in apparmor_sb_mount
apparmor: Convert from sb_mount to granular mount hooks
selinux: Convert from sb_mount to granular mount hooks
landlock: Convert from sb_mount to granular mount hooks
tomoyo: Convert from sb_mount to granular mount hooks
lsm: Remove security_sb_mount and security_move_mount

 fs/namespace.c                    |  41 +++++++---
 include/linux/lsm_hook_defs.h     |  14 +++-
 include/linux/security.h          |  56 +++++++++++---
 kernel/bpf/bpf_lsm.c              |   7 +-
 security/apparmor/include/mount.h |   5 +-
 security/apparmor/lsm.c           | 102 ++++++++++++++++++-------
 security/apparmor/mount.c         |  37 ++--------
 security/landlock/fs.c            |  41 ++++++++--
 security/security.c               | 119 +++++++++++++++++++++++-------
 security/selinux/hooks.c          |  49 ++++++++----
 security/tomoyo/common.h          |   2 +-
 security/tomoyo/mount.c           |  31 +++++---
 security/tomoyo/tomoyo.c          | 109 ++++++++++++++++++++++++---
 13 files changed, 457 insertions(+), 156 deletions(-)

--
2.53.0-Meta

author reviewer
#1 lore raw

PATCH v4 1/7 lsm: Add granular mount hooks to replace security_sb_mount

Add six new LSM hooks for mount operations:

- mount_bind(from, to, recurse): bind mount with pre-resolved struct path for source and destination.
- mount_new(fc, mp, mnt_flags, flags, data): new mount, called after mount options are parsed. The flags and data parameters carry the original mount(2) flags and data for LSMs that need them (AppArmor, Tomoyo).
- mount_remount(fc, mp, mnt_flags, flags, data): filesystem remount, called after mount options are parsed into the fs_context.
- mount_reconfigure(mp, mnt_flags, flags): mount flag reconfiguration (MS_REMOUNT|MS_BIND path).
- mount_move(from, to): move mount with pre-resolved paths.
- mount_change_type(mp, ms_flags): propagation type changes.

These replace the monolithic security_sb_mount() which conflates multiple distinct operations into a single hook, and suffers from TOCTOU issues where LSMs re-resolve string-based dev_name via kern_path().

The mount_move hook is added alongside the existing move_mount hook. During the transition, LSMs register for both hooks. The move_mount hook will be removed once all LSMs have been converted.

Some LSMs, such as apparmor and tomoyo, audit the original input passed in the mount syscall. To keep the same behavior, argument data and flags are passed in do_* functions. These can be removed if these LSMs no longer need these information.

All new hooks are registered as sleepable BPF LSM hooks.

Code generated with the assistance of Claude, reviewed by human.

---
 fs/namespace.c                |  39 +++++++++++--
 include/linux/lsm_hook_defs.h |  12 ++++
 include/linux/security.h      |  50 +++++++++++++++++
 kernel/bpf/bpf_lsm.c          |   7 +++
 security/security.c           | 101 ++++++++++++++++++++++++++++++++++
 5 files changed, 203 insertions(+), 6 deletions(-)
fs/namespace.c +33 -6
index fe919abd2f01..04e3bd7f6336 100644
@@ -2888,6 +2888,10 @@ static int do_change_type(const struct path *path, int ms_flags)
 	if (!type)
 		return -EINVAL;
 
+	err = security_mount_change_type(path, ms_flags);
+	if (err)
+		return err;
+
 	guard(namespace_excl)();
 
 	err = may_change_propagation(mnt);
@@ -3006,6 +3010,10 @@ static int do_loopback(const struct path *path, const char *old_name,
 	if (err)
 		return err;
 
+	err = security_mount_bind(&old_path, path, recurse);
+	if (err)
+		return err;
+
 	if (mnt_ns_loop(old_path.dentry))
 		return -EINVAL;
 
@@ -3328,7 +3336,8 @@ static void mnt_warn_timestamp_expiry(const struct path *mountpoint,
  * superblock it refers to.  This is triggered by specifying MS_REMOUNT|MS_BIND
  * to mount(2).
  */
-static int do_reconfigure_mnt(const struct path *path, unsigned int mnt_flags)
+static int do_reconfigure_mnt(const struct path *path, unsigned int mnt_flags,
+			      unsigned long flags)
 {
 	struct super_block *sb = path->mnt->mnt_sb;
 	struct mount *mnt = real_mount(path->mnt);
@@ -3343,6 +3352,10 @@ static int do_reconfigure_mnt(const struct path *path, unsigned int mnt_flags)
 	if (!can_change_locked_flags(mnt, mnt_flags))
 		return -EPERM;
 
+	ret = security_mount_reconfigure(path, mnt_flags, flags);
+	if (ret)
+		return ret;
+
 	/*
 	 * We're only checking whether the superblock is read-only not
 	 * changing it, so only take down_read(&sb->s_umount).
@@ -3366,7 +3379,7 @@ static int do_reconfigure_mnt(const struct path *path, unsigned int mnt_flags)
  * on it - tough luck.
  */
 static int do_remount(const struct path *path, int sb_flags,
-		      int mnt_flags, void *data)
+		      int mnt_flags, void *data, unsigned long flags)
 {
 	int err;
 	struct super_block *sb = path->mnt->mnt_sb;
@@ -3393,6 +3406,9 @@ static int do_remount(const struct path *path, int sb_flags,
 	fc->oldapi = true;
 
 	err = parse_monolithic_mount_data(fc, data);
+	if (!err)
+		err = security_mount_remount(fc, path, mnt_flags, flags,
+					    data);
 	if (!err) {
 		down_write(&sb->s_umount);
 		err = -EPERM;
@@ -3708,6 +3724,10 @@ static int do_move_mount_old(const struct path *path, const char *old_name)
 	if (err)
 		return err;
 
+	err = security_mount_move(&old_path, path);
+	if (err)
+		return err;
+
 	return do_move_mount(&old_path, path, 0);
 }
 
@@ -3786,7 +3806,7 @@ static int do_new_mount_fc(struct fs_context *fc, const struct path *mountpoint,
  */
 static int do_new_mount(const struct path *path, const char *fstype,
 			int sb_flags, int mnt_flags,
-			const char *name, void *data)
+			const char *name, void *data, unsigned long flags)
 {
 	struct file_system_type *type;
 	struct fs_context *fc;
@@ -3830,6 +3850,9 @@ static int do_new_mount(const struct path *path, const char *fstype,
 		err = parse_monolithic_mount_data(fc, data);
 	if (!err && !mount_capable(fc))
 		err = -EPERM;
+
+	if (!err)
+		err = security_mount_new(fc, path, mnt_flags, flags, data);
 	if (!err)
 		err = do_new_mount_fc(fc, path, mnt_flags);
 
@@ -4141,9 +4164,9 @@ int path_mount(const char *dev_name, const struct path *path,
 			    SB_I_VERSION);
 
 	if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
-		return do_reconfigure_mnt(path, mnt_flags);
+		return do_reconfigure_mnt(path, mnt_flags, flags);
 	if (flags & MS_REMOUNT)
-		return do_remount(path, sb_flags, mnt_flags, data_page);
+		return do_remount(path, sb_flags, mnt_flags, data_page, flags);
 	if (flags & MS_BIND)
 		return do_loopback(path, dev_name, flags & MS_REC);
 	if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
@@ -4152,7 +4175,7 @@ int path_mount(const char *dev_name, const struct path *path,
 		return do_move_mount_old(path, dev_name);
 
 	return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name,
-			    data_page);
+			    data_page, flags);
 }
 
 int do_mount(const char *dev_name, const char __user *dir_name,
@@ -4549,6 +4572,10 @@ static inline int vfs_move_mount(const struct path *from_path,
 	if (ret)
 		return ret;
 
+	ret = security_mount_move(from_path, to_path);
+	if (ret)
+		return ret;
+
 	if (mflags & MNT_TREE_PROPAGATION)
 		return do_set_group(from_path, to_path);
 
include/linux/lsm_hook_defs.h +12 -0
index 2b8dfb35caed..98f0fe382665 100644
@@ -81,6 +81,18 @@ LSM_HOOK(int, 0, sb_clone_mnt_opts, const struct super_block *oldsb,
 	 unsigned long *set_kern_flags)
 LSM_HOOK(int, 0, move_mount, const struct path *from_path,
 	 const struct path *to_path)
+LSM_HOOK(int, 0, mount_bind, const struct path *from, const struct path *to,
+	 bool recurse)
+LSM_HOOK(int, 0, mount_new, struct fs_context *fc, const struct path *mp,
+	 int mnt_flags, unsigned long flags, void *data)
+LSM_HOOK(int, 0, mount_remount, struct fs_context *fc,
+	 const struct path *mp, int mnt_flags, unsigned long flags,
+	 void *data)
+LSM_HOOK(int, 0, mount_reconfigure, const struct path *mp,
+	 unsigned int mnt_flags, unsigned long flags)
+LSM_HOOK(int, 0, mount_move, const struct path *from_path,
+	 const struct path *to_path)
+LSM_HOOK(int, 0, mount_change_type, const struct path *mp, int ms_flags)
 LSM_HOOK(int, -EOPNOTSUPP, dentry_init_security, struct dentry *dentry,
 	 int mode, const struct qstr *name, const char **xattr_name,
 	 struct lsm_context *cp)
include/linux/security.h +50 -0

62 lines not on this page · raw message

kernel/bpf/bpf_lsm.c +7 -0
index c5c925f00202..aa228372cfb4 100644
@@ -382,6 +382,13 @@ BTF_ID(func, bpf_lsm_task_setscheduler)
 BTF_ID(func, bpf_lsm_userns_create)
 BTF_ID(func, bpf_lsm_bdev_alloc_security)
 BTF_ID(func, bpf_lsm_bdev_setintegrity)
+BTF_ID(func, bpf_lsm_move_mount)
+BTF_ID(func, bpf_lsm_mount_bind)
+BTF_ID(func, bpf_lsm_mount_new)
+BTF_ID(func, bpf_lsm_mount_remount)
+BTF_ID(func, bpf_lsm_mount_reconfigure)
+BTF_ID(func, bpf_lsm_mount_move)
+BTF_ID(func, bpf_lsm_mount_change_type)
 BTF_SET_END(sleepable_lsm_hooks)
 
 BTF_SET_START(untrusted_lsm_hooks)
security/security.c +101 -0

107 lines not on this page · raw message

Signature · 1 line
2.53.0-Meta
#2 lore raw

Re: [PATCH v4 1/7] lsm: Add granular mount hooks to replace security_sb_mount

Quoted 6 lines · Song Liu

[...]

Please cleanly separate the preparatory work for introducing the new hooks from any changes to fs/namespace.c

Once you have all of the new machinery in place, switch fs/namespace.c over to the new hooks.

This will make it way easier to review and easier to distribute the

Signature · 1 line
Christian Brauner <[email protected]>
author reviewer
#3 lore raw

Re: [PATCH v4 1/7] lsm: Add granular mount hooks to replace security_sb_mount

Quoted 8 lines · Christian Brauner
Quoted 6 lines · Song Liu

[...]

^^^^^ I assume you didn't mean to add this SoB yet.

Quoted 6 lines

Please cleanly separate the preparatory work for introducing the new hooks from any changes to fs/namespace.c

Once you have all of the new machinery in place, switch fs/namespace.c over to the new hooks.

Could you please be more specific on how to arrange these changes? Currently, we have

1/7 adds new hooks:
lsm: Add granular mount hooks to replace security_sb_mount
2/7 through 6/7 migrate LSMs from old hooks to new hooks:
apparmor: Remove redundant MS_MGC_MSK stripping in apparmor_sb_mount
apparmor: Convert from sb_mount to granular mount hooks
selinux: Convert from sb_mount to granular mount hooks
landlock: Convert from sb_mount to granular mount hooks
tomoyo: Convert from sb_mount to granular mount hooks
7/7 removes old hooks:
lsm: Remove security_sb_mount and security_move_mount

Some ideas to change this:

Idea #1:
Do you mean to split 1/7 and 7/7 into two patches each: one for changes in fs/namespace.c, the other for everything else? IOW, the set of 9 patches will look like:

1/9 adds new hooks in security/ core code
2/9 adds calls to new hooks to fs/namespace.c 3/9 through 7/9 migrate LSMs from old hooks to new hooks 8/9 removes calls to old hooks from s/namespace.c 9/9 removes old hooks from security/ core code

Idea #2:
Or do you mean all the changes to fs/namespace.c have to stay in one commit? I am not sure how we can make the whole patchset clean with this approach. We will probably need 2+ commits for each of the 4 LSMs we update. IOW, the set will look like:

Add new hooks in security core code
Add new hooks in selinux
Add new hooks in apparmor
Add new hooks in landlock
Add new hooks in tamoyo
Replace old hooks with new hooks in fs/namespace.c Remove old hooks from selinux
Remove old hooks from apparmor
Remove old hooks from landlock
Remove old hooks from tamoyo
Remove old hooks in security core code

This will require at least 11 patches, and it is not really clean.

Idea #3:
Did I miss any other solutions?

Quoted 1 line

This will make it way easier to review and easier to distribute the

This sentence appears to be incomplete. Do you mean to distribute different patches in the patchset to different trees and merge them separately?

I actually think the patchset is clean as-is.

The overall change in fs/namespace.c is really minimal:

    fs/namespace.c                    |  41 +++++++---

We can clearly see what it changed in fs/namespace.c with "git diff HEAD~7 -- fs/" (attached below).

Therefore, I don't see a way to make the whole patchset more clean. If I missed anything, please let me know.

Thanks,
Song

$ git diff HEAD~7 -- fs/
fs/namespace.c +0 -0
index fe919abd2f01..43f22c5e2bf4 100644
@@ -2888,6 +2888,10 @@ static int do_change_type(const struct path

*path, int ms_flags)

        if (!type)
                return -EINVAL;

+ err = security_mount_change_type(path, ms_flags);
+ if (err)
+ return err;
+

        guard(namespace_excl)();

        err = may_change_propagation(mnt);
@@ -3006,6 +3010,10 @@ static int do_loopback(const struct path *path,
const char *old_name,
        if (err)
                return err;

+ err = security_mount_bind(&old_path, path, recurse);
+ if (err)
+ return err;
+

        if (mnt_ns_loop(old_path.dentry))
                return -EINVAL;

@@ -3328,7 +3336,8 @@ static void mnt_warn_timestamp_expiry(const struct path *mountpoint,
* superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND
* to mount(2).
*/
-static int do_reconfigure_mnt(const struct path *path, unsigned int mnt_flags) +static int do_reconfigure_mnt(const struct path *path, unsigned int mnt_flags,
+ unsigned long flags)

 {
        struct super_block *sb = path->mnt->mnt_sb;
        struct mount *mnt = real_mount(path->mnt);

@@ -3343,6 +3352,10 @@ static int do_reconfigure_mnt(const struct path *path, unsigned int mnt_flags)
if (!can_change_locked_flags(mnt, mnt_flags))
return -EPERM;

+ ret = security_mount_reconfigure(path, mnt_flags, flags);
+ if (ret)
+ return ret;
+
/*
* We're only checking whether the superblock is read-only not
* changing it, so only take down_read(&sb->s_umount).
@@ -3366,7 +3379,7 @@ static int do_reconfigure_mnt(const struct path *path, unsigned int mnt_flags)
* on it - tough luck.
*/
static int do_remount(const struct path *path, int sb_flags,
- int mnt_flags, void *data)
+ int mnt_flags, void *data, unsigned long flags)

 {
        int err;
        struct super_block *sb = path->mnt->mnt_sb;
@@ -3393,6 +3406,9 @@ static int do_remount(const struct path *path,
int sb_flags,
        fc->oldapi = true;

        err = parse_monolithic_mount_data(fc, data);
+       if (!err)
+               err = security_mount_remount(fc, path, mnt_flags, flags,
+                                           data);
        if (!err) {
                down_write(&sb->s_umount);
                err = -EPERM;
@@ -3708,6 +3724,10 @@ static int do_move_mount_old(const struct path
*path, const char *old_name)
        if (err)
                return err;

+ err = security_mount_move(&old_path, path);
+ if (err)
+ return err;
+

        return do_move_mount(&old_path, path, 0);

}

@@ -3786,7 +3806,7 @@ static int do_new_mount_fc(struct fs_context *fc, const struct path *mountpoint,
*/
static int do_new_mount(const struct path *path, const char *fstype,
int sb_flags, int mnt_flags,
- const char *name, void *data)
+ const char *name, void *data, unsigned long flags)

 {
        struct file_system_type *type;
        struct fs_context *fc;
@@ -3830,6 +3850,9 @@ static int do_new_mount(const struct path *path,
const char *fstype,
                err = parse_monolithic_mount_data(fc, data);
        if (!err && !mount_capable(fc))
                err = -EPERM;
+
+       if (!err)
+               err = security_mount_new(fc, path, mnt_flags, flags, data);
        if (!err)
                err = do_new_mount_fc(fc, path, mnt_flags);

@@ -4080,7 +4103,6 @@ int path_mount(const char *dev_name, const struct path *path,
const char *type_page, unsigned long flags, void *data_page)

 {
        unsigned int mnt_flags = 0, sb_flags;

- int ret;

        /* Discard magic */
        if ((flags & MS_MGC_MSK) == MS_MGC_VAL)

@@ -4093,9 +4115,6 @@ int path_mount(const char *dev_name, const struct path *path,
if (flags & MS_NOUSER)
return -EINVAL;

- ret = security_sb_mount(dev_name, path, type_page, flags, data_page);
- if (ret)

-               return ret;
        if (!may_mount())
                return -EPERM;
        if (flags & SB_MANDLOCK)
@@ -4141,9 +4160,9 @@ int path_mount(const char *dev_name, const
struct path *path,
                            SB_I_VERSION);

        if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))

- return do_reconfigure_mnt(path, mnt_flags);
+ return do_reconfigure_mnt(path, mnt_flags, flags);
if (flags & MS_REMOUNT)
- return do_remount(path, sb_flags, mnt_flags, data_page);
+ return do_remount(path, sb_flags, mnt_flags, data_page, flags);
if (flags & MS_BIND)
return do_loopback(path, dev_name, flags & MS_REC);
if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
@@ -4152,7 +4171,7 @@ int path_mount(const char *dev_name, const struct path *path,
return do_move_mount_old(path, dev_name);

return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name,
- data_page);
+ data_page, flags);
}

int do_mount(const char *dev_name, const char __user *dir_name,
@@ -4545,7 +4564,7 @@ static inline int vfs_move_mount(const struct path *from_path,

 {
        int ret;

- ret = security_move_mount(from_path, to_path);

+       ret = security_mount_move(from_path, to_path);
        if (ret)
                return ret;
#4 lore raw

Re: [PATCH v4 1/7] lsm: Add granular mount hooks to replace security_sb_mount

Quoted 11 lines · Song Liu
Quoted 8 lines · Christian Brauner
Quoted 6 lines · Song Liu

[...]

^^^^^ I assume you didn't mean to add this SoB yet.

No, the my SoB hook was run during b4 review apparently...

Quoted 20 lines
Quoted 5 lines

Please cleanly separate the preparatory work for introducing the new hooks from any changes to fs/namespace.c

Once you have all of the new machinery in place, switch fs/namespace.c over to the new hooks.

Could you please be more specific on how to arrange these changes? Currently, we have

1/7 adds new hooks:
lsm: Add granular mount hooks to replace security_sb_mount
2/7 through 6/7 migrate LSMs from old hooks to new hooks:
apparmor: Remove redundant MS_MGC_MSK stripping in apparmor_sb_mount
apparmor: Convert from sb_mount to granular mount hooks
selinux: Convert from sb_mount to granular mount hooks
landlock: Convert from sb_mount to granular mount hooks
tomoyo: Convert from sb_mount to granular mount hooks
7/7 removes old hooks:
lsm: Remove security_sb_mount and security_move_mount

Quoted 1 line

Some ideas to change this:

My thought had been:

* Add the new hooks to security/.
* add the individual lsm implementations.
* Now replace the old hooks with the new hooks in fs/namespace.c
* Delete the old hooks in security/

IOW, why the migration step? It is a full replacement anyway.

Quoted 216 lines

Idea #1:
Do you mean to split 1/7 and 7/7 into two patches each: one for changes in fs/namespace.c, the other for everything else? IOW, the set of 9 patches will look like:

1/9 adds new hooks in security/ core code
2/9 adds calls to new hooks to fs/namespace.c 3/9 through 7/9 migrate LSMs from old hooks to new hooks 8/9 removes calls to old hooks from s/namespace.c 9/9 removes old hooks from security/ core code

Idea #2:
Or do you mean all the changes to fs/namespace.c have to stay in one commit? I am not sure how we can make the whole patchset clean with this approach. We will probably need 2+ commits for each of the 4 LSMs we update. IOW, the set will look like:

Add new hooks in security core code
Add new hooks in selinux
Add new hooks in apparmor
Add new hooks in landlock
Add new hooks in tamoyo
Replace old hooks with new hooks in fs/namespace.c Remove old hooks from selinux
Remove old hooks from apparmor
Remove old hooks from landlock
Remove old hooks from tamoyo
Remove old hooks in security core code

This will require at least 11 patches, and it is not really clean.

Idea #3:
Did I miss any other solutions?

Quoted 1 line

This will make it way easier to review and easier to distribute the

This sentence appears to be incomplete. Do you mean to distribute different patches in the patchset to different trees and merge them separately?

I actually think the patchset is clean as-is.

The overall change in fs/namespace.c is really minimal:

    fs/namespace.c                    |  41 +++++++---

We can clearly see what it changed in fs/namespace.c with "git diff HEAD~7 -- fs/" (attached below).

Therefore, I don't see a way to make the whole patchset more clean. If I missed anything, please let me know.

Thanks,
Song

$ git diff HEAD~7 -- fs/
fs/namespace.c +4 -0
index fe919abd2f01..43f22c5e2bf4 100644
@@ -2888,6 +2888,10 @@ static int do_change_type(const struct path
 *path, int ms_flags)
        if (!type)
                return -EINVAL;
 
+       err = security_mount_change_type(path, ms_flags);
+       if (err)
+               return err;
+
        guard(namespace_excl)();
 
        err = may_change_propagation(mnt);
fs/namespace.c +6 -1
@@ -3006,6 +3010,10 @@ static int do_loopback(const struct path *path,
 const char *old_name,
        if (err)
                return err;
 
+       err = security_mount_bind(&old_path, path, recurse);
+       if (err)
+               return err;
+
        if (mnt_ns_loop(old_path.dentry))
                return -EINVAL;
@@ -3328,7 +3336,8 @@ static void mnt_warn_timestamp_expiry(const
 struct path *mountpoint,
  * superblock it refers to.  This is triggered by specifying MS_REMOUNT|MS_BIND
  * to mount(2).
  */
-static int do_reconfigure_mnt(const struct path *path, unsigned int mnt_flags)
+static int do_reconfigure_mnt(const struct path *path, unsigned int mnt_flags,
+                             unsigned long flags)
 {
        struct super_block *sb = path->mnt->mnt_sb;
        struct mount *mnt = real_mount(path->mnt);
fs/namespace.c +4 -0
@@ -3343,6 +3352,10 @@ static int do_reconfigure_mnt(const struct path
 *path, unsigned int mnt_flags)
        if (!can_change_locked_flags(mnt, mnt_flags))
                return -EPERM;
 
+       ret = security_mount_reconfigure(path, mnt_flags, flags);
+       if (ret)
+               return ret;
+
        /*
         * We're only checking whether the superblock is read-only not

* changing it, so only take down_read(&sb->s_umount).

fs/namespace.c +1 -1
@@ -3366,7 +3379,7 @@ static int do_reconfigure_mnt(const struct path
 *path, unsigned int mnt_flags)
  * on it - tough luck.
  */
 static int do_remount(const struct path *path, int sb_flags,
-                     int mnt_flags, void *data)
+                     int mnt_flags, void *data, unsigned long flags)
 {
        int err;
        struct super_block *sb = path->mnt->mnt_sb;
fs/namespace.c +3 -0
@@ -3393,6 +3406,9 @@ static int do_remount(const struct path *path,
 int sb_flags,
        fc->oldapi = true;
 
        err = parse_monolithic_mount_data(fc, data);
+       if (!err)
+               err = security_mount_remount(fc, path, mnt_flags, flags,
+                                           data);
        if (!err) {
                down_write(&sb->s_umount);
                err = -EPERM;
fs/namespace.c +5 -1
@@ -3708,6 +3724,10 @@ static int do_move_mount_old(const struct path
 *path, const char *old_name)
        if (err)
                return err;
 
+       err = security_mount_move(&old_path, path);
+       if (err)
+               return err;
+
        return do_move_mount(&old_path, path, 0);
 }
@@ -3786,7 +3806,7 @@ static int do_new_mount_fc(struct fs_context
 *fc, const struct path *mountpoint,
  */
 static int do_new_mount(const struct path *path, const char *fstype,
                        int sb_flags, int mnt_flags,
-                       const char *name, void *data)
+                       const char *name, void *data, unsigned long flags)
 {
        struct file_system_type *type;
        struct fs_context *fc;
fs/namespace.c +3 -1
@@ -3830,6 +3850,9 @@ static int do_new_mount(const struct path *path,
 const char *fstype,
                err = parse_monolithic_mount_data(fc, data);
        if (!err && !mount_capable(fc))
                err = -EPERM;
+
+       if (!err)
+               err = security_mount_new(fc, path, mnt_flags, flags, data);
        if (!err)
                err = do_new_mount_fc(fc, path, mnt_flags);
@@ -4080,7 +4103,6 @@ int path_mount(const char *dev_name, const
 struct path *path,
                const char *type_page, unsigned long flags, void *data_page)
 {
        unsigned int mnt_flags = 0, sb_flags;
-       int ret;
 
        /* Discard magic */
        if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
fs/namespace.c +0 -3
@@ -4093,9 +4115,6 @@ int path_mount(const char *dev_name, const
 struct path *path,
        if (flags & MS_NOUSER)
                return -EINVAL;
 
-       ret = security_sb_mount(dev_name, path, type_page, flags, data_page);
-       if (ret)
-               return ret;
        if (!may_mount())
                return -EPERM;

if (flags & SB_MANDLOCK)

fs/namespace.c +2 -2
@@ -4141,9 +4160,9 @@ int path_mount(const char *dev_name, const
 struct path *path,
                            SB_I_VERSION);
 
        if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
-               return do_reconfigure_mnt(path, mnt_flags);
+               return do_reconfigure_mnt(path, mnt_flags, flags);
        if (flags & MS_REMOUNT)
-               return do_remount(path, sb_flags, mnt_flags, data_page);
+               return do_remount(path, sb_flags, mnt_flags, data_page, flags);
        if (flags & MS_BIND)
                return do_loopback(path, dev_name, flags & MS_REC);

if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))

fs/namespace.c +1 -1
@@ -4152,7 +4171,7 @@ int path_mount(const char *dev_name, const
 struct path *path,
                return do_move_mount_old(path, dev_name);
 
        return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name,
-                           data_page);
+                           data_page, flags);
 }
 

int do_mount(const char *dev_name, const char __user *dir_name,

fs/namespace.c +1 -1
@@ -4545,7 +4564,7 @@ static inline int vfs_move_mount(const struct
 path *from_path,
 {
        int ret;
 
-       ret = security_move_mount(from_path, to_path);
+       ret = security_mount_move(from_path, to_path);
        if (ret)
                return ret;
author reviewer
#5 lore raw

Re: [PATCH v4 1/7] lsm: Add granular mount hooks to replace security_sb_mount

On Wed, May 27, 2026 at 5:17 AM Christian Brauner <[email protected]> wrote:
[...]

Quoted 13 lines
Quoted 11 lines

1/7 adds new hooks:
lsm: Add granular mount hooks to replace security_sb_mount
2/7 through 6/7 migrate LSMs from old hooks to new hooks:
apparmor: Remove redundant MS_MGC_MSK stripping in apparmor_sb_mount
apparmor: Convert from sb_mount to granular mount hooks
selinux: Convert from sb_mount to granular mount hooks
landlock: Convert from sb_mount to granular mount hooks
tomoyo: Convert from sb_mount to granular mount hooks
7/7 removes old hooks:
lsm: Remove security_sb_mount and security_move_mount

Quoted 1 line

Some ideas to change this:

Quoted 8 lines

My thought had been:

* Add the new hooks to security/.
* add the individual lsm implementations.
* Now replace the old hooks with the new hooks in fs/namespace.c
* Delete the old hooks in security/

IOW, why the migration step? It is a full replacement anyway.

I think having a migration like this doesn't really make review more difficult. But I am OK refactoring the patches as requested.

Paul, do you have a strong preference either way?

Thanks,
Song

author reviewer
#6 lore raw

PATCH v4 2/7 apparmor: Remove redundant MS_MGC_MSK stripping in apparmor_sb_mount

In reply to Song Liu

path_mount() already strips the magic number from flags before calling security_sb_mount(), so this check in apparmor_sb_mount() is a no-op. Remove it.

Code generated with the assistance of Claude, reviewed by human.

---
 security/apparmor/lsm.c | 4 ----
 1 file changed, 4 deletions(-)
security/apparmor/lsm.c +0 -4
index 3491e9f60194..4415bca5889c 100644
@@ -705,10 +705,6 @@ static int apparmor_sb_mount(const char *dev_name, const struct path *path,
 	int error = 0;
 	bool needput;
 
-	/* Discard magic */
-	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
-		flags &= ~MS_MGC_MSK;
-
 	flags &= ~AA_MS_IGNORE_MASK;
 
 	label = __begin_current_label_crit_section(&needput);
Signature · 1 line
2.53.0-Meta
author reviewer
#7 lore raw

PATCH v4 3/7 apparmor: Convert from sb_mount to granular mount hooks

In reply to Song Liu

Replace AppArmor's monolithic apparmor_sb_mount() with granular mount hooks.

Key changes:
- mount_bind: uses the pre-resolved struct path from VFS instead of re-resolving dev_name via kern_path(), eliminating a TOCTOU vulnerability. aa_bind_mount() now takes a struct path instead of a string for the source.
- mount_new, mount_remount: receive the original mount(2) flags and data parameters for policy matching via match_mnt_flags() and AA_MNT_CONT_MATCH data matching.
- mount_reconfigure: handles MS_REMOUNT|MS_BIND (mount attribute reconfiguration) which was previously handled as a remount.
- mount_move: reuses apparmor_move_mount() which already handles pre-resolved paths.
- mount_change_type: propagation type changes.

aa_move_mount_old() is removed since move mounts now go through security_mount_move() with pre-resolved struct path pointers for both the old mount(2) and new move_mount(2) APIs.

Code generated with the assistance of Claude, reviewed by human.

---
 security/apparmor/include/mount.h |   5 +-
 security/apparmor/lsm.c           | 100 +++++++++++++++++++++++-------
 security/apparmor/mount.c         |  37 ++---------
 3 files changed, 83 insertions(+), 59 deletions(-)
security/apparmor/include/mount.h +1 -4
index 46834f828179..088e2f938cc1 100644
@@ -31,16 +31,13 @@ int aa_remount(const struct cred *subj_cred,
 
 int aa_bind_mount(const struct cred *subj_cred,
 		  struct aa_label *label, const struct path *path,
-		  const char *old_name, unsigned long flags);
+		  const struct path *old_path, bool recurse);
 
 
 int aa_mount_change_type(const struct cred *subj_cred,
 			 struct aa_label *label, const struct path *path,
 			 unsigned long flags);
 
-int aa_move_mount_old(const struct cred *subj_cred,
-		      struct aa_label *label, const struct path *path,
-		      const char *old_name);
 int aa_move_mount(const struct cred *subj_cred,
 		  struct aa_label *label, const struct path *from_path,
 		  const struct path *to_path);
security/apparmor/lsm.c +77 -23

125 lines not on this page · raw message

security/apparmor/mount.c +5 -32
index 523570aa1a5a..38b40e16014f 100644
@@ -418,25 +418,17 @@ int aa_remount(const struct cred *subj_cred,
 }
 
 int aa_bind_mount(const struct cred *subj_cred,
-		  struct aa_label *label, const struct path *path,
-		  const char *dev_name, unsigned long flags)
+		       struct aa_label *label, const struct path *path,
+		       const struct path *old_path, bool recurse)
 {
 	struct aa_profile *profile;
 	char *buffer = NULL, *old_buffer = NULL;
-	struct path old_path;
+	unsigned long flags = MS_BIND | (recurse ? MS_REC : 0);
 	int error;
 
 	AA_BUG(!label);
 	AA_BUG(!path);
-
-	if (!dev_name || !*dev_name)
-		return -EINVAL;
-
-	flags &= MS_REC | MS_BIND;
-
-	error = kern_path(dev_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
-	if (error)
-		return error;
+	AA_BUG(!old_path);
 
 	buffer = aa_get_buffer(false);
 	old_buffer = aa_get_buffer(false);
@@ -445,12 +437,11 @@ int aa_bind_mount(const struct cred *subj_cred,
 		goto out;
 
 	error = fn_for_each_confined(label, profile,
-			match_mnt(subj_cred, profile, path, buffer, &old_path,
+			match_mnt(subj_cred, profile, path, buffer, old_path,
 				  old_buffer, NULL, flags, NULL, false));
 out:
 	aa_put_buffer(buffer);
 	aa_put_buffer(old_buffer);
-	path_put(&old_path);
 
 	return error;
 }
@@ -514,24 +505,6 @@ int aa_move_mount(const struct cred *subj_cred,
 	return error;
 }
 
-int aa_move_mount_old(const struct cred *subj_cred, struct aa_label *label,
-		      const struct path *path, const char *orig_name)
-{
-	struct path old_path;
-	int error;
-
-	if (!orig_name || !*orig_name)
-		return -EINVAL;
-	error = kern_path(orig_name, LOOKUP_FOLLOW, &old_path);
-	if (error)
-		return error;
-
-	error = aa_move_mount(subj_cred, label, &old_path, path);
-	path_put(&old_path);
-
-	return error;
-}
-
 int aa_new_mount(const struct cred *subj_cred, struct aa_label *label,
 		 const char *dev_name, const struct path *path,
 		 const char *type, unsigned long flags, void *data)
Signature · 1 line
2.53.0-Meta
author reviewer
#8 lore raw

PATCH v4 4/7 selinux: Convert from sb_mount to granular mount hooks

In reply to Song Liu

Replace selinux_mount() with granular mount hooks, preserving the same permission checks:

- mount_bind, mount_new, mount_change_type: FILE__MOUNTON
- mount_remount, mount_reconfigure: FILESYSTEM__REMOUNT
- mount_move: FILE__MOUNTON (reuses selinux_move_mount)

The flags and data parameters are unused by SELinux.

Code generated with the assistance of Claude, reviewed by human.

---
 security/selinux/hooks.c | 49 ++++++++++++++++++++++++++++------------
 1 file changed, 35 insertions(+), 14 deletions(-)
security/selinux/hooks.c +35 -14
index 0f704380a8c8..c8de175bde04 100644
@@ -2802,19 +2802,37 @@ static int selinux_sb_statfs(struct dentry *dentry)
 	return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
 }
 
-static int selinux_mount(const char *dev_name,
-			 const struct path *path,
-			 const char *type,
-			 unsigned long flags,
-			 void *data)
+static int selinux_mount_bind(const struct path *from, const struct path *to,
+			      bool recurse)
 {
-	const struct cred *cred = current_cred();
+	return path_has_perm(current_cred(), to, FILE__MOUNTON);
+}
 
-	if (flags & MS_REMOUNT)
-		return superblock_has_perm(cred, path->dentry->d_sb,
-					   FILESYSTEM__REMOUNT, NULL);
-	else
-		return path_has_perm(cred, path, FILE__MOUNTON);
+static int selinux_mount_new(struct fs_context *fc, const struct path *mp,
+			     int mnt_flags, unsigned long flags, void *data)
+{
+	return path_has_perm(current_cred(), mp, FILE__MOUNTON);
+}
+
+static int selinux_mount_remount(struct fs_context *fc, const struct path *mp,
+				 int mnt_flags, unsigned long flags,
+				 void *data)
+{
+	return superblock_has_perm(current_cred(), fc->root->d_sb,
+				   FILESYSTEM__REMOUNT, NULL);
+}
+
+static int selinux_mount_reconfigure(const struct path *mp,
+				     unsigned int mnt_flags,
+				     unsigned long flags)
+{
+	return superblock_has_perm(current_cred(), mp->dentry->d_sb,
+				   FILESYSTEM__REMOUNT, NULL);
+}
+
+static int selinux_mount_change_type(const struct path *mp, int ms_flags)
+{
+	return path_has_perm(current_cred(), mp, FILE__MOUNTON);
 }
 
 static int selinux_move_mount(const struct path *from_path,
@@ -7558,13 +7576,16 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
 	LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
 	LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
-	LSM_HOOK_INIT(sb_mount, selinux_mount),
+	LSM_HOOK_INIT(mount_bind, selinux_mount_bind),
+	LSM_HOOK_INIT(mount_new, selinux_mount_new),
+	LSM_HOOK_INIT(mount_remount, selinux_mount_remount),
+	LSM_HOOK_INIT(mount_reconfigure, selinux_mount_reconfigure),
+	LSM_HOOK_INIT(mount_change_type, selinux_mount_change_type),
+	LSM_HOOK_INIT(mount_move, selinux_move_mount),
 	LSM_HOOK_INIT(sb_umount, selinux_umount),
 	LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
 	LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
 
-	LSM_HOOK_INIT(move_mount, selinux_move_mount),
-
 	LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
 	LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
 
Signature · 1 line
2.53.0-Meta
author reviewer
#9 lore raw

PATCH v4 5/7 landlock: Convert from sb_mount to granular mount hooks

In reply to Song Liu

Replace hook_sb_mount() with granular mount hooks. Landlock denies all mount operations for sandboxed processes regardless of flags, so all new hooks share a common hook_mount_deny() helper. The mount_move hook reuses hook_move_mount().

Code generated with the assistance of Claude, reviewed by human.

---
 security/landlock/fs.c | 41 ++++++++++++++++++++++++++++++++++++-----
 1 file changed, 36 insertions(+), 5 deletions(-)
security/landlock/fs.c +36 -5
index c1ecfe239032..7377f22a165e 100644
@@ -1416,9 +1416,7 @@ static void log_fs_change_topology_dentry(
  * inherit these new constraints.  Anyway, for backward compatibility reasons,
  * a dedicated user space option would be required (e.g. as a ruleset flag).
  */
-static int hook_sb_mount(const char *const dev_name,
-			 const struct path *const path, const char *const type,
-			 const unsigned long flags, void *const data)
+static int hook_mount_deny(const struct path *const path)
 {
 	size_t handle_layer;
 	const struct landlock_cred_security *const subject =
@@ -1432,6 +1430,35 @@ static int hook_sb_mount(const char *const dev_name,
 	return -EPERM;
 }
 
+static int hook_mount_bind(const struct path *const from,
+			   const struct path *const to, bool recurse)
+{
+	return hook_mount_deny(to);
+}
+
+static int hook_mount_new(struct fs_context *fc, const struct path *const mp,
+			  int mnt_flags, unsigned long flags, void *data)
+{
+	return hook_mount_deny(mp);
+}
+
+static int hook_mount_remount(struct fs_context *fc, const struct path *mp,
+			      int mnt_flags, unsigned long flags, void *data)
+{
+	return hook_mount_deny(mp);
+}
+
+static int hook_mount_reconfigure(const struct path *const mp,
+				  unsigned int mnt_flags, unsigned long flags)
+{
+	return hook_mount_deny(mp);
+}
+
+static int hook_mount_change_type(const struct path *const mp, int ms_flags)
+{
+	return hook_mount_deny(mp);
+}
+
 static int hook_move_mount(const struct path *const from_path,
 			   const struct path *const to_path)
 {
@@ -1950,8 +1977,12 @@ static struct security_hook_list landlock_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(inode_free_security_rcu, hook_inode_free_security_rcu),
 
 	LSM_HOOK_INIT(sb_delete, hook_sb_delete),
-	LSM_HOOK_INIT(sb_mount, hook_sb_mount),
-	LSM_HOOK_INIT(move_mount, hook_move_mount),
+	LSM_HOOK_INIT(mount_bind, hook_mount_bind),
+	LSM_HOOK_INIT(mount_new, hook_mount_new),
+	LSM_HOOK_INIT(mount_remount, hook_mount_remount),
+	LSM_HOOK_INIT(mount_reconfigure, hook_mount_reconfigure),
+	LSM_HOOK_INIT(mount_change_type, hook_mount_change_type),
+	LSM_HOOK_INIT(mount_move, hook_move_mount),
 	LSM_HOOK_INIT(sb_umount, hook_sb_umount),
 	LSM_HOOK_INIT(sb_remount, hook_sb_remount),
 	LSM_HOOK_INIT(sb_pivotroot, hook_sb_pivotroot),
Signature · 1 line
2.53.0-Meta
author reviewer
#10 lore raw

PATCH v4 6/7 tomoyo: Convert from sb_mount to granular mount hooks

In reply to Song Liu

Replace tomoyo_sb_mount() with granular mount hooks. Each hook reconstructs the MS_* flags expected by tomoyo_mount_permission() using the original flags parameter where available.

Key changes:
- mount_bind: passes the pre-resolved source path to tomoyo_mount_acl() via a new dev_path parameter, instead of re-resolving dev_name via kern_path(). This eliminates a TOCTOU vulnerability.
- mount_new, mount_remount, mount_reconfigure: use the original mount(2) flags for policy matching.
- mount_move: passes pre-resolved paths for both source and destination.
- mount_change_type: passes raw ms_flags directly.

Also removes the unused data_page parameter from
tomoyo_mount_permission().

Code generated with the assistance of Claude, reviewed by human.

---
 security/tomoyo/common.h |   2 +-
 security/tomoyo/mount.c  |  31 +++++++----
 security/tomoyo/tomoyo.c | 109 +++++++++++++++++++++++++++++++++++----
 3 files changed, 121 insertions(+), 21 deletions(-)
security/tomoyo/common.h +1 -1
index d098cf8aae61..9241034cfede 100644
@@ -1013,7 +1013,7 @@ int tomoyo_mkdev_perm(const u8 operation, const struct path *path,
 		      const unsigned int mode, unsigned int dev);
 int tomoyo_mount_permission(const char *dev_name, const struct path *path,
 			    const char *type, unsigned long flags,
-			    void *data_page);
+			    const struct path *dev_path);
 int tomoyo_open_control(const u8 type, struct file *file);
 int tomoyo_path2_perm(const u8 operation, const struct path *path1,
 		      const struct path *path2);
security/tomoyo/mount.c +21 -10
index 322dfd188ada..82ffe7d02814 100644
@@ -70,6 +70,7 @@ static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
  * @dir:      Pointer to "struct path".
  * @type:     Name of filesystem type.
  * @flags:    Mount options.
+ * @dev_path: Pre-resolved device/source path. Maybe NULL.
  *
  * Returns 0 on success, negative value otherwise.
  *
@@ -78,11 +79,11 @@ static bool tomoyo_check_mount_acl(struct tomoyo_request_info *r,
 static int tomoyo_mount_acl(struct tomoyo_request_info *r,
 			    const char *dev_name,
 			    const struct path *dir, const char *type,
-			    unsigned long flags)
+			    unsigned long flags,
+			    const struct path *dev_path)
 	__must_hold_shared(&tomoyo_ss)
 {
 	struct tomoyo_obj_info obj = { };
-	struct path path;
 	struct file_system_type *fstype = NULL;
 	const char *requested_type = NULL;
 	const char *requested_dir_name = NULL;
@@ -134,13 +135,23 @@ static int tomoyo_mount_acl(struct tomoyo_request_info *r,
 			need_dev = 1;
 	}
 	if (need_dev) {
-		/* Get mount point or device file. */
-		if (!dev_name || kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
+		if (dev_path) {
+			/* Use pre-resolved path to avoid TOCTOU issues. */
+			obj.path1 = *dev_path;
+			path_get(&obj.path1);
+		} else if (!dev_name) {
 			error = -ENOENT;
 			goto out;
+		} else {
+			struct path path;
+
+			if (kern_path(dev_name, LOOKUP_FOLLOW, &path)) {
+				error = -ENOENT;
+				goto out;
+			}
+			obj.path1 = path;
 		}
-		obj.path1 = path;
-		requested_dev_name = tomoyo_realpath_from_path(&path);
+		requested_dev_name = tomoyo_realpath_from_path(&obj.path1);
 		if (!requested_dev_name) {
 			error = -ENOENT;
 			goto out;
@@ -173,7 +184,7 @@ static int tomoyo_mount_acl(struct tomoyo_request_info *r,
 	if (fstype)
 		put_filesystem(fstype);
 	kfree(requested_type);
-	/* Drop refcount obtained by kern_path(). */
+	/* Drop refcount obtained by kern_path() or path_get(). */
 	if (obj.path1.dentry)
 		path_put(&obj.path1);
 	return error;
@@ -186,13 +197,13 @@ static int tomoyo_mount_acl(struct tomoyo_request_info *r,
  * @path:      Pointer to "struct path".
  * @type:      Name of filesystem type. Maybe NULL.
  * @flags:     Mount options.
- * @data_page: Optional data. Maybe NULL.
+ * @dev_path:  Pre-resolved device/source path. Maybe NULL.
  *
  * Returns 0 on success, negative value otherwise.
  */
 int tomoyo_mount_permission(const char *dev_name, const struct path *path,
 			    const char *type, unsigned long flags,
-			    void *data_page)
+			    const struct path *dev_path)
 {
 	struct tomoyo_request_info r;
 	int error;
@@ -236,7 +247,7 @@ int tomoyo_mount_permission(const char *dev_name, const struct path *path,
 	if (!type)
 		type = "<NULL>";
 	idx = tomoyo_read_lock();
-	error = tomoyo_mount_acl(&r, dev_name, path, type, flags);
+	error = tomoyo_mount_acl(&r, dev_name, path, type, flags, dev_path);
 	tomoyo_read_unlock(idx);
 	return error;
 }
security/tomoyo/tomoyo.c +99 -10

132 lines not on this page · raw message

Signature · 1 line
2.53.0-Meta
author reviewer
#11 lore raw

PATCH v4 7/7 lsm: Remove security_sb_mount and security_move_mount

In reply to Song Liu

Now that all LSMs have been converted to granular mount hooks, remove the old hooks:

- security_sb_mount(): removed from lsm_hook_defs.h, security.h, security.c, and its call in path_mount().
- security_move_mount(): removed and replaced by security_mount_move() in do_move_mount(). All LSMs now use mount_move exclusively.

Code generated with the assistance of Claude, reviewed by human.

---
 fs/namespace.c                |  8 --------
 include/linux/lsm_hook_defs.h |  4 ----
 include/linux/security.h      | 16 ---------------
 kernel/bpf/bpf_lsm.c          |  2 --
 security/security.c           | 38 -----------------------------------
 5 files changed, 68 deletions(-)
fs/namespace.c +0 -8
index 04e3bd7f6336..43f22c5e2bf4 100644
@@ -4103,7 +4103,6 @@ int path_mount(const char *dev_name, const struct path *path,
 		const char *type_page, unsigned long flags, void *data_page)
 {
 	unsigned int mnt_flags = 0, sb_flags;
-	int ret;
 
 	/* Discard magic */
 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
@@ -4116,9 +4115,6 @@ int path_mount(const char *dev_name, const struct path *path,
 	if (flags & MS_NOUSER)
 		return -EINVAL;
 
-	ret = security_sb_mount(dev_name, path, type_page, flags, data_page);
-	if (ret)
-		return ret;
 	if (!may_mount())
 		return -EPERM;
 	if (flags & SB_MANDLOCK)
@@ -4568,10 +4564,6 @@ static inline int vfs_move_mount(const struct path *from_path,
 {
 	int ret;
 
-	ret = security_move_mount(from_path, to_path);
-	if (ret)
-		return ret;
-
 	ret = security_mount_move(from_path, to_path);
 	if (ret)
 		return ret;
include/linux/lsm_hook_defs.h +0 -4
index 98f0fe382665..c870260bf402 100644
@@ -69,8 +69,6 @@ LSM_HOOK(int, 0, sb_remount, struct super_block *sb, void *mnt_opts)
 LSM_HOOK(int, 0, sb_kern_mount, const struct super_block *sb)
 LSM_HOOK(int, 0, sb_show_options, struct seq_file *m, struct super_block *sb)
 LSM_HOOK(int, 0, sb_statfs, struct dentry *dentry)
-LSM_HOOK(int, 0, sb_mount, const char *dev_name, const struct path *path,
-	 const char *type, unsigned long flags, void *data)
 LSM_HOOK(int, 0, sb_umount, struct vfsmount *mnt, int flags)
 LSM_HOOK(int, 0, sb_pivotroot, const struct path *old_path,
 	 const struct path *new_path)
@@ -79,8 +77,6 @@ LSM_HOOK(int, 0, sb_set_mnt_opts, struct super_block *sb, void *mnt_opts,
 LSM_HOOK(int, 0, sb_clone_mnt_opts, const struct super_block *oldsb,
 	 struct super_block *newsb, unsigned long kern_flags,
 	 unsigned long *set_kern_flags)
-LSM_HOOK(int, 0, move_mount, const struct path *from_path,
-	 const struct path *to_path)
 LSM_HOOK(int, 0, mount_bind, const struct path *from, const struct path *to,
 	 bool recurse)
 LSM_HOOK(int, 0, mount_new, struct fs_context *fc, const struct path *mp,
include/linux/security.h +0 -16
index b1b3da51a88d..f1dcfc569cf2 100644
@@ -373,8 +373,6 @@ int security_sb_remount(struct super_block *sb, void *mnt_opts);
 int security_sb_kern_mount(const struct super_block *sb);
 int security_sb_show_options(struct seq_file *m, struct super_block *sb);
 int security_sb_statfs(struct dentry *dentry);
-int security_sb_mount(const char *dev_name, const struct path *path,
-		      const char *type, unsigned long flags, void *data);
 int security_sb_umount(struct vfsmount *mnt, int flags);
 int security_sb_pivotroot(const struct path *old_path, const struct path *new_path);
 int security_sb_set_mnt_opts(struct super_block *sb,
@@ -385,7 +383,6 @@ int security_sb_clone_mnt_opts(const struct super_block *oldsb,
 				struct super_block *newsb,
 				unsigned long kern_flags,
 				unsigned long *set_kern_flags);
-int security_move_mount(const struct path *from_path, const struct path *to_path);
 int security_mount_bind(const struct path *from, const struct path *to,
 			bool recurse);
 int security_mount_new(struct fs_context *fc, const struct path *mp,
@@ -825,13 +822,6 @@ static inline int security_sb_statfs(struct dentry *dentry)
 	return 0;
 }
 
-static inline int security_sb_mount(const char *dev_name, const struct path *path,
-				    const char *type, unsigned long flags,
-				    void *data)
-{
-	return 0;
-}
-
 static inline int security_sb_umount(struct vfsmount *mnt, int flags)
 {
 	return 0;
@@ -859,12 +849,6 @@ static inline int security_sb_clone_mnt_opts(const struct super_block *oldsb,
 	return 0;
 }
 
-static inline int security_move_mount(const struct path *from_path,
-				      const struct path *to_path)
-{
-	return 0;
-}
-
 static inline int security_mount_bind(const struct path *from,
 				      const struct path *to, bool recurse)
 {
kernel/bpf/bpf_lsm.c +0 -2
index aa228372cfb4..77371ca25d09 100644
@@ -350,7 +350,6 @@ BTF_ID(func, bpf_lsm_release_secctx)
 BTF_ID(func, bpf_lsm_sb_alloc_security)
 BTF_ID(func, bpf_lsm_sb_eat_lsm_opts)
 BTF_ID(func, bpf_lsm_sb_kern_mount)
-BTF_ID(func, bpf_lsm_sb_mount)
 BTF_ID(func, bpf_lsm_sb_remount)
 BTF_ID(func, bpf_lsm_sb_set_mnt_opts)
 BTF_ID(func, bpf_lsm_sb_show_options)
@@ -382,7 +381,6 @@ BTF_ID(func, bpf_lsm_task_setscheduler)
 BTF_ID(func, bpf_lsm_userns_create)
 BTF_ID(func, bpf_lsm_bdev_alloc_security)
 BTF_ID(func, bpf_lsm_bdev_setintegrity)
-BTF_ID(func, bpf_lsm_move_mount)
 BTF_ID(func, bpf_lsm_mount_bind)
 BTF_ID(func, bpf_lsm_mount_new)
 BTF_ID(func, bpf_lsm_mount_remount)
security/security.c +0 -38
index b7ec0ec7af26..bc55ee588c59 100644
@@ -1065,29 +1065,6 @@ int security_sb_statfs(struct dentry *dentry)
 	return call_int_hook(sb_statfs, dentry);
 }
 
-/**
- * security_sb_mount() - Check permission for mounting a filesystem
- * @dev_name: filesystem backing device
- * @path: mount point
- * @type: filesystem type
- * @flags: mount flags
- * @data: filesystem specific data
- *
- * Check permission before an object specified by @dev_name is mounted on the
- * mount point named by @nd.  For an ordinary mount, @dev_name identifies a
- * device if the file system type requires a device.  For a remount
- * (@flags & MS_REMOUNT), @dev_name is irrelevant.  For a loopback/bind mount
- * (@flags & MS_BIND), @dev_name identifies the	pathname of the object being
- * mounted.
- *
- * Return: Returns 0 if permission is granted.
- */
-int security_sb_mount(const char *dev_name, const struct path *path,
-		      const char *type, unsigned long flags, void *data)
-{
-	return call_int_hook(sb_mount, dev_name, path, type, flags, data);
-}
-
 /**
  * security_sb_umount() - Check permission for unmounting a filesystem
  * @mnt: mounted filesystem
@@ -1167,21 +1144,6 @@ int security_sb_clone_mnt_opts(const struct super_block *oldsb,
 }
 EXPORT_SYMBOL(security_sb_clone_mnt_opts);
 
-/**
- * security_move_mount() - Check permissions for moving a mount
- * @from_path: source mount point
- * @to_path: destination mount point
- *
- * Check permission before a mount is moved.
- *
- * Return: Returns 0 if permission is granted.
- */
-int security_move_mount(const struct path *from_path,
-			const struct path *to_path)
-{
-	return call_int_hook(move_mount, from_path, to_path);
-}
-
 /**
  * security_mount_bind() - Check permissions for a bind mount
  * @from: source path
Signature · 1 line
2.53.0-Meta