LSM: Remove double path_rename hook calls for RENAME_EXCHANGE
Subsystems (4)
Mickaël Salaün
maintainerRFC PATCH 1/1 LSM: Remove double path_rename hook calls for RENAME_EXCHANGE
From: Mickaël Salaün <[email protected]>
In order to be able to identify a file exchange with renameat2(2) and RENAME_EXCHANGE, which will be useful for Landlock [1], propagate the rename flags to LSMs. This may also improve performance because of the switch from two set of LSM hook calls to only one, and because LSMs using this hook may optimize the double check (e.g. only one lock, reduce the number of path walks).
AppArmor, Landlock and Tomoyo are updated to leverage this change. This should not change the current behavior (same check order), except (different level of) speed boosts.
[1] https://lore.kernel.org/r/[email protected]
--- include/linux/lsm_hook_defs.h | 2 +- include/linux/lsm_hooks.h | 1 + security/apparmor/lsm.c | 30 +++++++++++++++++++++++++----- security/landlock/fs.c | 12 ++++++++++-- security/security.c | 9 +-------- security/tomoyo/tomoyo.c | 11 ++++++++++- 6 files changed, 48 insertions(+), 17 deletions(-)
include/linux/lsm_hook_defs.h +1 -1
@@ -100,7 +100,7 @@ LSM_HOOK(int, 0, path_link, struct dentry *old_dentry, const struct path *new_dir, struct dentry *new_dentry) LSM_HOOK(int, 0, path_rename, const struct path *old_dir, struct dentry *old_dentry, const struct path *new_dir, - struct dentry *new_dentry) + struct dentry *new_dentry, unsigned int flags) LSM_HOOK(int, 0, path_chmod, const struct path *path, umode_t mode) LSM_HOOK(int, 0, path_chown, const struct path *path, kuid_t uid, kgid_t gid) LSM_HOOK(int, 0, path_chroot, const struct path *path)
include/linux/lsm_hooks.h +1 -0
@@ -358,6 +358,7 @@* @old_dentry contains the dentry structure of the old link. * @new_dir contains the path structure for parent of the new link. * @new_dentry contains the dentry structure of the new link. +* @flags may contain rename options such as RENAME_EXCHANGE. * Return 0 if permission is granted. * @path_chmod: * Check for permission to change a mode of the file @path. The new
security/apparmor/lsm.c +25 -5
@@ -354,13 +354,16 @@ static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_ }static int apparmor_path_rename (const struct path *old_dir,struct dentry *old_dentry, -const struct path *new_dir,struct dentry *new_dentry) +const struct path *new_dir,struct dentry *new_dentry, +const unsigned int flags) {struct aa_label *label;int error =0 ;if (!path_mediated_fs (old_dentry))return 0 ; +if ((flags &RENAME_EXCHANGE ) && !path_mediated_fs (new_dentry)) +return 0 ; label =begin_current_label_crit_section ();if (!unconfined (label)) {
@@ -374,10 +377,27 @@ static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dd_backing_inode (old_dentry)->i_mode }; - error =aa_path_perm (OP_RENAME_SRC , label, &old_path,0 , -MAY_READ |AA_MAY_GETATTR |MAY_WRITE | -AA_MAY_SETATTR |AA_MAY_DELETE , - &cond); +if (flags &RENAME_EXCHANGE ) { +struct path_cond cond_exchange = { +i_uid_into_mnt (mnt_userns,d_backing_inode (new_dentry)), +d_backing_inode (new_dentry)->i_mode + }; + + error =aa_path_perm (OP_RENAME_SRC , label, &new_path,0 , +MAY_READ |AA_MAY_GETATTR |MAY_WRITE | +AA_MAY_SETATTR |AA_MAY_DELETE , + &cond_exchange); +if (!error) + error =aa_path_perm (OP_RENAME_DEST , label, &old_path, +0 ,MAY_WRITE |AA_MAY_SETATTR | +AA_MAY_CREATE , &cond_exchange); + } + +if (!error) + error =aa_path_perm (OP_RENAME_SRC , label, &old_path,0 , +MAY_READ |AA_MAY_GETATTR |MAY_WRITE | +AA_MAY_SETATTR |AA_MAY_DELETE , + &cond);if (!error) error = aa_path_perm(OP_RENAME_DEST, label, &new_path, 0, MAY_WRITE | AA_MAY_SETATTR |
security/landlock/fs.c +10 -2
@@ -574,10 +574,12 @@ static inline u32 maybe_remove(const struct dentry *const dentry)static int hook_path_rename (const struct path *const old_dir,struct dentry *const old_dentry,const struct path *const new_dir, -struct dentry *const new_dentry) +struct dentry *const new_dentry, +const unsigned int flags) {const struct landlock_ruleset *const dom =landlock_get_current_domain (); +u32 exchange_access =0 ;if (!dom)return 0 ;
@@ -585,11 +587,17 @@ static int hook_path_rename(const struct path *const old_dir,if (old_dir->dentry != new_dir->dentry)/* Gracefully forbids reparenting. */ return -EXDEV ; +if (flags &RENAME_EXCHANGE ) { +if (unlikely (d_is_negative (new_dentry))) +return -ENOENT ; + exchange_access = +get_mode_access (d_backing_inode (new_dentry)->i_mode); + }if (unlikely (d_is_negative (old_dentry)))return -ENOENT ;/* RENAME_EXCHANGE is handled because directories are the same. */ return check_access_path (dom, old_dir,maybe_remove (old_dentry) | -maybe_remove (new_dentry) | +maybe_remove (new_dentry) | exchange_access |get_mode_access (d_backing_inode (old_dentry)->i_mode)); }
security/security.c +1 -8
@@ -1184,15 +1184,8 @@ int security_path_rename(const struct path *old_dir, struct dentry *old_dentry, (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))return 0 ; -if (flags &RENAME_EXCHANGE ) { -int err =call_int_hook (path_rename,0 , new_dir, new_dentry, - old_dir, old_dentry); -if (err) -return err; - } -return call_int_hook (path_rename,0 , old_dir, old_dentry, new_dir, - new_dentry); + new_dentry, flags); }EXPORT_SYMBOL (security_path_rename);
security/tomoyo/tomoyo.c +10 -1
@@ -264,17 +264,26 @@ static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_di* @old_dentry: Pointer to "struct dentry". * @new_parent: Pointer to "struct path". * @new_dentry: Pointer to "struct dentry". +* @flags: Rename options. * * Returns 0 on success, negative value otherwise. */ static int tomoyo_path_rename (const struct path *old_parent,struct dentry *old_dentry,const struct path *new_parent, -struct dentry *new_dentry) +struct dentry *new_dentry, +const unsigned int flags) {struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry };struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry }; +if (flags &RENAME_EXCHANGE ) { +const int err =tomoyo_path2_perm (TOMOYO_TYPE_RENAME , &path2, + &path1); + +if (err) +return err; + }return tomoyo_path2_perm (TOMOYO_TYPE_RENAME , &path1, &path2); }
base-commit: cfb92440ee71adcc2105b0890bb01ac3cddb8507
Signature · 1 line
2.35.1
Mickaël Salaün
maintainerAny comment? John, Tetsuo, does it look OK for AppArmor and Tomoyo?
Quoted 202 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
In order to be able to identify a file exchange with renameat2(2) and
RENAME_EXCHANGE, which will be useful for Landlock [1], propagate the rename flags to LSMs. This may also improve performance because of the switch from two set of LSM hook calls to only one, and because LSMs using this hook may optimize the double check (e.g. only one lock, reduce the number of path walks).AppArmor, Landlock and Tomoyo are updated to leverage this change. This should not change the current behavior (same check order), except (different level of) speed boosts.
[1] https://lore.kernel.org/r/[email protected]
Cc James Morris <[email protected]>Cc John Johansen <[email protected]>Cc Kentaro Takeda <[email protected]>Cc Serge E. Hallyn <[email protected]>Cc Tetsuo Handa <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>--- include/linux/lsm_hook_defs.h | 2 +- include/linux/lsm_hooks.h | 1 + security/apparmor/lsm.c | 30 +++++++++++++++++++++++++----- security/landlock/fs.c | 12 ++++++++++-- security/security.c | 9 +-------- security/tomoyo/tomoyo.c | 11 ++++++++++- 6 files changed, 48 insertions(+), 17 deletions(-)include/linux/lsm_hook_defs.h +1
-1@@ -100,7 +100,7 @@ LSM_HOOK(int, 0, path_link, struct dentry *old_dentry, const struct path *new_dir, struct dentry *new_dentry) LSM_HOOK(int, 0, path_rename, const struct path *old_dir, struct dentry *old_dentry, const struct path *new_dir, - struct dentry *new_dentry) + struct dentry *new_dentry, unsigned int flags) LSM_HOOK(int, 0, path_chmod, const struct path *path, umode_t mode) LSM_HOOK(int, 0, path_chown, const struct path *path, kuid_t uid, kgid_t gid) LSM_HOOK(int, 0, path_chroot, const struct path *path)include/linux/lsm_hooks.h +1
-0@@ -358,6 +358,7 @@* @old_dentry contains the dentry structure of the old link. * @new_dir contains the path structure for parent of the new link. * @new_dentry contains the dentry structure of the new link. +* @flags may contain rename options such as RENAME_EXCHANGE. * Return 0 if permission is granted. * @path_chmod: * Check for permission to change a mode of the file @path. The new security/apparmor/lsm.c +25
-5@@ -354,13 +354,16 @@ static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_ }static int apparmor_path_rename (const struct path *old_dir,struct dentry *old_dentry, -const struct path *new_dir,struct dentry *new_dentry) +const struct path *new_dir,struct dentry *new_dentry, +const unsigned int flags) {struct aa_label *label;int error =0 ;if (!path_mediated_fs (old_dentry))return 0 ; +if ((flags &RENAME_EXCHANGE ) && !path_mediated_fs (new_dentry)) +return 0 ; label =begin_current_label_crit_section ();if (!unconfined (label)) {@@ -374,10 +377,27 @@ static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dd_backing_inode (old_dentry)->i_mode }; - error =aa_path_perm (OP_RENAME_SRC , label, &old_path,0 , -MAY_READ |AA_MAY_GETATTR |MAY_WRITE | -AA_MAY_SETATTR |AA_MAY_DELETE , - &cond); +if (flags &RENAME_EXCHANGE ) { +struct path_cond cond_exchange = { +i_uid_into_mnt (mnt_userns,d_backing_inode (new_dentry)), +d_backing_inode (new_dentry)->i_mode + }; + + error =aa_path_perm (OP_RENAME_SRC , label, &new_path,0 , +MAY_READ |AA_MAY_GETATTR |MAY_WRITE | +AA_MAY_SETATTR |AA_MAY_DELETE , + &cond_exchange); +if (!error) + error =aa_path_perm (OP_RENAME_DEST , label, &old_path, +0 ,MAY_WRITE |AA_MAY_SETATTR | +AA_MAY_CREATE , &cond_exchange); + } + +if (!error) + error =aa_path_perm (OP_RENAME_SRC , label, &old_path,0 , +MAY_READ |AA_MAY_GETATTR |MAY_WRITE | +AA_MAY_SETATTR |AA_MAY_DELETE , + &cond);if (!error) error = aa_path_perm(OP_RENAME_DEST, label, &new_path, 0, MAY_WRITE | AA_MAY_SETATTR |security/landlock/fs.c +10
-2@@ -574,10 +574,12 @@ static inline u32 maybe_remove(const struct dentry *const dentry)static int hook_path_rename (const struct path *const old_dir,struct dentry *const old_dentry,const struct path *const new_dir, -struct dentry *const new_dentry) +struct dentry *const new_dentry, +const unsigned int flags) {const struct landlock_ruleset *const dom =landlock_get_current_domain (); +u32 exchange_access =0 ;if (!dom)return 0 ;@@ -585,11 +587,17 @@ static int hook_path_rename(const struct path *const old_dir,if (old_dir->dentry != new_dir->dentry)/* Gracefully forbids reparenting. */ return -EXDEV ; +if (flags &RENAME_EXCHANGE ) { +if (unlikely (d_is_negative (new_dentry))) +return -ENOENT ; + exchange_access = +get_mode_access (d_backing_inode (new_dentry)->i_mode); + }if (unlikely (d_is_negative (old_dentry)))return -ENOENT ;/* RENAME_EXCHANGE is handled because directories are the same. */ return check_access_path (dom, old_dir,maybe_remove (old_dentry) | -maybe_remove (new_dentry) | +maybe_remove (new_dentry) | exchange_access |get_mode_access (d_backing_inode (old_dentry)->i_mode)); }security/security.c +1
-8@@ -1184,15 +1184,8 @@ int security_path_rename(const struct path *old_dir, struct dentry *old_dentry, (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))return 0 ; -if (flags &RENAME_EXCHANGE ) { -int err =call_int_hook (path_rename,0 , new_dir, new_dentry, - old_dir, old_dentry); -if (err) -return err; - } -return call_int_hook (path_rename,0 , old_dir, old_dentry, new_dir, - new_dentry); + new_dentry, flags); }EXPORT_SYMBOL (security_path_rename);security/tomoyo/tomoyo.c +10
-1@@ -264,17 +264,26 @@ static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_di* @old_dentry: Pointer to "struct dentry". * @new_parent: Pointer to "struct path". * @new_dentry: Pointer to "struct dentry". +* @flags: Rename options. * * Returns 0 on success, negative value otherwise. */ static int tomoyo_path_rename (const struct path *old_parent,struct dentry *old_dentry,const struct path *new_parent, -struct dentry *new_dentry) +struct dentry *new_dentry, +const unsigned int flags) {struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry };struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry }; +if (flags &RENAME_EXCHANGE ) { +const int err =tomoyo_path2_perm (TOMOYO_TYPE_RENAME , &path2, + &path1); + +if (err) +return err; + }return tomoyo_path2_perm (TOMOYO_TYPE_RENAME , &path1, &path2); }base-commit:
cfb92440ee71adcc2105b0890bb01ac3cddb8507
Quoted 1 line · Mickaël Salaün
Any comment? John, Tetsuo, does it look OK for AppArmor and Tomoyo?
I'm OK with this change regarding TOMOYO part.
Thank you.
Looks good to me. Of course now I am going to have to create a follow-up patch to optimize the apparmor mediation.
Quoted 195 lines · Mickaël Salaün
Any comment? John, Tetsuo, does it look OK for AppArmor and Tomoyo?
Quoted 192 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
In order to be able to identify a file exchange with renameat2(2) and
RENAME_EXCHANGE, which will be useful for Landlock [1], propagate the rename flags to LSMs. This may also improve performance because of the switch from two set of LSM hook calls to only one, and because LSMs using this hook may optimize the double check (e.g. only one lock, reduce the number of path walks).AppArmor, Landlock and Tomoyo are updated to leverage this change. This should not change the current behavior (same check order), except (different level of) speed boosts.
[1] https://lore.kernel.org/r/[email protected]
Cc James Morris <[email protected]>Cc John Johansen <[email protected]>Cc Kentaro Takeda <[email protected]>Cc Serge E. Hallyn <[email protected]>Cc Tetsuo Handa <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
include/linux/lsm_hook_defs.h| 2 +- include/linux/lsm_hooks.h | 1 + security/apparmor/lsm.c | 30 +++++++++++++++++++++++++----- security/landlock/fs.c | 12 ++++++++++-- security/security.c | 9 +-------- security/tomoyo/tomoyo.c | 11 ++++++++++-6 files changed, 48 insertions(+), 17 deletions(-)include/linux/lsm_hook_defs.h +1
-1@@ -100,7 +100,7 @@ LSM_HOOK(int, 0, path_link, struct dentry *old_dentry, const struct path *new_dir, struct dentry *new_dentry) LSM_HOOK(int, 0, path_rename, const struct path *old_dir, struct dentry *old_dentry, const struct path *new_dir, - struct dentry *new_dentry) + struct dentry *new_dentry, unsigned int flags) LSM_HOOK(int, 0, path_chmod, const struct path *path, umode_t mode) LSM_HOOK(int, 0, path_chown, const struct path *path, kuid_t uid, kgid_t gid) LSM_HOOK(int, 0, path_chroot, const struct path *path)include/linux/lsm_hooks.h +1
-0@@ -358,6 +358,7 @@ * @old_dentry contains the dentry structure of the old link. * @new_dir contains the path structure for parent of the new link. * @new_dentry contains the dentry structure of the new link. + * @flags may contain rename options such as RENAME_EXCHANGE. * Return 0 if permission is granted. * @path_chmod: * Check for permission to change a mode of the file @path. The newsecurity/apparmor/lsm.c +4
-1@@ -354,13 +354,16 @@ static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_ } static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry, - const struct path *new_dir, struct dentry *new_dentry) + const struct path *new_dir, struct dentry *new_dentry, + const unsigned int flags) { struct aa_label *label; int error = 0; if (!path_mediated_fs(old_dentry)) return 0; + if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry)) + return 0; label = begin_current_label_crit_section(); if (!unconfined(label)) {@@ -374,10 +377,27 @@ static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dd_backing_inode(old_dentry)->i_mode
};
- error = aa_path_perm(OP_RENAME_SRC, label,&old_path, 0, -MAY_READ|AA_MAY_GETATTR|MAY_WRITE| -AA_MAY_SETATTR|AA_MAY_DELETE,
- &cond);
+ if (flags &RENAME_EXCHANGE) {
+ structpath_condcond_exchange= {
+i_uid_into_mnt(mnt_userns, d_backing_inode(new_dentry)), + d_backing_inode(new_dentry)->i_mode
+ };security/apparmor/lsm.c +16
-0+ + error = aa_path_perm(OP_RENAME_SRC, label, &new_path, 0, + MAY_READ | AA_MAY_GETATTR | MAY_WRITE | + AA_MAY_SETATTR | AA_MAY_DELETE, + &cond_exchange); + if (!error) + error = aa_path_perm(OP_RENAME_DEST, label, &old_path, + 0, MAY_WRITE | AA_MAY_SETATTR | + AA_MAY_CREATE, &cond_exchange); + } + + if (!error) + error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0, + MAY_READ | AA_MAY_GETATTR | MAY_WRITE | + AA_MAY_SETATTR | AA_MAY_DELETE, + &cond);if (!error)
error = aa_path_perm(OP_RENAME_DEST, label,&new_path, 0,MAY_WRITE|AA_MAY_SETATTR|security/landlock/fs.c +2
-1@@ -574,10 +574,12 @@ static inline u32 maybe_remove(const struct dentry *const dentry) static int hook_path_rename(const struct path *const old_dir, struct dentry *const old_dentry, const struct path *const new_dir, - struct dentry *const new_dentry) + struct dentry *const new_dentry, + const unsigned int flags) {const struct
landlock_ruleset*const dom =
landlock_get_current_domain();
+ u32exchange_access= 0;
if (!dom)
return 0;security/landlock/fs.c +6
-0@@ -585,11 +587,17 @@ static int hook_path_rename(const struct path *const old_dir, if (old_dir->dentry != new_dir->dentry) /* Gracefully forbids reparenting. */ return -EXDEV; + if (flags & RENAME_EXCHANGE) { + if (unlikely(d_is_negative(new_dentry))) + return -ENOENT; + exchange_access = + get_mode_access(d_backing_inode(new_dentry)->i_mode); + } if (unlikely(d_is_negative(old_dentry))) return -ENOENT; /* RENAME_EXCHANGE is handled because directories are the same. */return check_access_path(dom,
old_dir,maybe_remove(old_dentry)| -maybe_remove(new_dentry)|
+maybe_remove(new_dentry)|exchange_access| get_mode_access(d_backing_inode(old_dentry)->i_mode)); }
diff --gita/security/security.cb/security/security.cindex22261d79f333..8634da4cfd46100644security/security.c +1
-7@@ -1184,15 +1184,8 @@ int security_path_rename(const struct path *old_dir, struct dentry *old_dentry, (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry))))) return 0; - if (flags & RENAME_EXCHANGE) { - int err = call_int_hook(path_rename, 0, new_dir, new_dentry, - old_dir, old_dentry); - if (err) - return err; - } - return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir, - new_dentry); + new_dentry, flags); } EXPORT_SYMBOL(security_path_rename);diff --git
a/security/tomoyo/tomoyo.cb/security/tomoyo/tomoyo.cindexb6a31901f289..71e82d855ebf100644security/tomoyo/tomoyo.c +9
-1@@ -264,17 +264,26 @@ static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_di* @old_dentry: Pointer to "struct dentry". * @new_parent: Pointer to "struct path". * @new_dentry: Pointer to "struct dentry". +* @flags: Rename options. * * Returns 0 on success, negative value otherwise. */ static int tomoyo_path_rename(const struct path *old_parent, struct dentry *old_dentry, const struct path *new_parent, - struct dentry *new_dentry) + struct dentry *new_dentry, + const unsigned int flags) { struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry }; struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry }; + if (flags & RENAME_EXCHANGE) { + const int err = tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path2, + &path1); + + if (err) + return err; + } return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2); }
base-commit:cfb92440ee71adcc2105b0890bb01ac3cddb8507
Mickaël Salaün
maintainerThanks Tetsuo and John. I'll include this patch in the rename/link Landlock series to fix the remaining issue: https://lore.kernel.org/r/[email protected]
Quoted 202 lines · John Johansen
Looks good to me. Of course now I am going to have to create a follow-up patch to optimize the apparmor mediation.
Acked-by John Johansen <[email protected]>Quoted 195 lines · Mickaël Salaün
Any comment? John, Tetsuo, does it look OK for AppArmor and Tomoyo?
Quoted 192 lines · Mickaël Salaün
From: Mickaël Salaün <[email protected]>
In order to be able to identify a file exchange with renameat2(2) and
RENAME_EXCHANGE, which will be useful for Landlock [1], propagate the rename flags to LSMs. This may also improve performance because of the switch from two set of LSM hook calls to only one, and because LSMs using this hook may optimize the double check (e.g. only one lock, reduce the number of path walks).AppArmor, Landlock and Tomoyo are updated to leverage this change. This should not change the current behavior (same check order), except (different level of) speed boosts.
[1] https://lore.kernel.org/r/[email protected]
Cc James Morris <[email protected]>Cc John Johansen <[email protected]>Cc Kentaro Takeda <[email protected]>Cc Serge E. Hallyn <[email protected]>Cc Tetsuo Handa <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>--- include/linux/lsm_hook_defs.h | 2 +- include/linux/lsm_hooks.h | 1 + security/apparmor/lsm.c | 30 +++++++++++++++++++++++++----- security/landlock/fs.c | 12 ++++++++++-- security/security.c | 9 +-------- security/tomoyo/tomoyo.c | 11 ++++++++++- 6 files changed, 48 insertions(+), 17 deletions(-)include/linux/lsm_hook_defs.h +1
-1@@ -100,7 +100,7 @@ LSM_HOOK(int, 0, path_link, struct dentry *old_dentry, const struct path *new_dir, struct dentry *new_dentry) LSM_HOOK(int, 0, path_rename, const struct path *old_dir, struct dentry *old_dentry, const struct path *new_dir, - struct dentry *new_dentry) + struct dentry *new_dentry, unsigned int flags) LSM_HOOK(int, 0, path_chmod, const struct path *path, umode_t mode) LSM_HOOK(int, 0, path_chown, const struct path *path, kuid_t uid, kgid_t gid) LSM_HOOK(int, 0, path_chroot, const struct path *path)include/linux/lsm_hooks.h +1
-0@@ -358,6 +358,7 @@ * @old_dentry contains the dentry structure of the old link. * @new_dir contains the path structure for parent of the new link. * @new_dentry contains the dentry structure of the new link. + * @flags may contain rename options such as RENAME_EXCHANGE. * Return 0 if permission is granted. * @path_chmod: * Check for permission to change a mode of the file @path. The newsecurity/apparmor/lsm.c +25
-4@@ -354,13 +354,16 @@ static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_ } static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry, - const struct path *new_dir, struct dentry *new_dentry) + const struct path *new_dir, struct dentry *new_dentry, + const unsigned int flags) { struct aa_label *label; int error = 0; if (!path_mediated_fs(old_dentry)) return 0; + if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry)) + return 0; label = begin_current_label_crit_section(); if (!unconfined(label)) {@@ -374,10 +377,27 @@ static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_d d_backing_inode(old_dentry)->i_mode }; - error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0, - MAY_READ | AA_MAY_GETATTR | MAY_WRITE | - AA_MAY_SETATTR | AA_MAY_DELETE, - &cond); + if (flags & RENAME_EXCHANGE) { + struct path_cond cond_exchange = { + i_uid_into_mnt(mnt_userns, d_backing_inode(new_dentry)), + d_backing_inode(new_dentry)->i_mode + }; + + error = aa_path_perm(OP_RENAME_SRC, label, &new_path, 0, + MAY_READ | AA_MAY_GETATTR | MAY_WRITE | + AA_MAY_SETATTR | AA_MAY_DELETE, + &cond_exchange); + if (!error) + error = aa_path_perm(OP_RENAME_DEST, label, &old_path, + 0, MAY_WRITE | AA_MAY_SETATTR | + AA_MAY_CREATE, &cond_exchange); + } + + if (!error) + error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0, + MAY_READ | AA_MAY_GETATTR | MAY_WRITE | + AA_MAY_SETATTR | AA_MAY_DELETE, + &cond); if (!error) error = aa_path_perm(OP_RENAME_DEST, label, &new_path, 0, MAY_WRITE | AA_MAY_SETATTR |security/landlock/fs.c +10
-2@@ -574,10 +574,12 @@ static inline u32 maybe_remove(const struct dentry *const dentry) static int hook_path_rename(const struct path *const old_dir, struct dentry *const old_dentry, const struct path *const new_dir, - struct dentry *const new_dentry) + struct dentry *const new_dentry, + const unsigned int flags) { const struct landlock_ruleset *const dom = landlock_get_current_domain(); + u32 exchange_access = 0; if (!dom) return 0;@@ -585,11 +587,17 @@ static int hook_path_rename(const struct path *const old_dir, if (old_dir->dentry != new_dir->dentry) /* Gracefully forbids reparenting. */ return -EXDEV; + if (flags & RENAME_EXCHANGE) { + if (unlikely(d_is_negative(new_dentry))) + return -ENOENT; + exchange_access = + get_mode_access(d_backing_inode(new_dentry)->i_mode); + } if (unlikely(d_is_negative(old_dentry))) return -ENOENT; /* RENAME_EXCHANGE is handled because directories are the same. */ return check_access_path(dom, old_dir, maybe_remove(old_dentry) | - maybe_remove(new_dentry) | + maybe_remove(new_dentry) | exchange_access | get_mode_access(d_backing_inode(old_dentry)->i_mode)); } diff --git a/security/security.c b/security/security.cindex
22261d79f333..8634da4cfd46100644security/security.c +1
-7@@ -1184,15 +1184,8 @@ int security_path_rename(const struct path *old_dir, struct dentry *old_dentry, (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry))))) return 0; - if (flags & RENAME_EXCHANGE) { - int err = call_int_hook(path_rename, 0, new_dir, new_dentry, - old_dir, old_dentry); - if (err) - return err; - } - return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir, - new_dentry); + new_dentry, flags); } EXPORT_SYMBOL(security_path_rename); diff --git a/security/tomoyo/tomoyo.c b/security/tomoyo/tomoyo.cindex
b6a31901f289..71e82d855ebf100644security/tomoyo/tomoyo.c +9
-1@@ -264,17 +264,26 @@ static int tomoyo_path_link(struct dentry *old_dentry, const struct path *new_di* @old_dentry: Pointer to "struct dentry". * @new_parent: Pointer to "struct path". * @new_dentry: Pointer to "struct dentry". +* @flags: Rename options. * * Returns 0 on success, negative value otherwise. */ static int tomoyo_path_rename(const struct path *old_parent, struct dentry *old_dentry, const struct path *new_parent, - struct dentry *new_dentry) + struct dentry *new_dentry, + const unsigned int flags) { struct path path1 = { .mnt = old_parent->mnt, .dentry = old_dentry }; struct path path2 = { .mnt = new_parent->mnt, .dentry = new_dentry }; + if (flags & RENAME_EXCHANGE) { + const int err = tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path2, + &path1); + + if (err) + return err; + } return tomoyo_path2_perm(TOMOYO_TYPE_RENAME, &path1, &path2); }base-commit:
cfb92440ee71adcc2105b0890bb01ac3cddb8507