fs, lsm: mediate O_PATH fd creation in file_open hook
Subsystems (7)
cover fs, lsm: mediate O_PATH fd creation in file_open hook
Calls to the openat(2) family of syscalls are mediated by the file_open LSM hook, but the opening of O_PATH file descriptors completely bypasses LSM mediation, preventing LSMs from initializing LSM file security context blobs for such file descriptors for use in other mediation hooks.
This patchset enables mediation of O_PATH file descriptors through the file_open hook and updates the LSMs using that hook to unconditionally allow creation of O_PATH fds, in order to preserve the existing behavior. However, the LSM patches are primarily meant as a starting point for discussions on how each one wants to handle O_PATH fd creation.
Ryan Lee (6):
fs: invoke LSM file_open hook in do_dentry_open for O_PATH fds as well
apparmor: explicitly skip mediation of O_PATH file descriptors
landlock: explicitly skip mediation of O_PATH file descriptors
selinux: explicitly skip mediation of O_PATH file descriptors
smack: explicitly skip mediation of O_PATH file descriptors
tomoyo: explicitly skip mediation of O_PATH file descriptors
fs/open.c | 7 ++++++- security/apparmor/lsm.c | 10 ++++++++++ security/landlock/fs.c | 8 ++++++++ security/selinux/hooks.c | 5 +++++ security/smack/smack_lsm.c | 4 ++++ security/tomoyo/file.c | 4 ++++ 6 files changed, 37 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
RFC PATCH 1/6 fs: invoke LSM file_open hook in do_dentry_open for O_PATH fds as well
Currently, opening O_PATH file descriptors completely bypasses the LSM infrastructure. Invoking the LSM file_open hook for O_PATH fds will be necessary for e.g. mediating the fsmount() syscall.
--- fs/open.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
fs/open.c +6 -1
@@ -921,8 +921,13 @@ static int do_dentry_open(struct file *f,if (unlikely (f->f_flags &O_PATH )) { f->f_mode =FMODE_PATH |FMODE_OPENED ;file_set_fsnotify_mode (f,FMODE_NONOTIFY ); f->f_op = &empty_fops; -return 0 ; +/* +* do_o_path in fs/namei.c unconditionally invokes path_put +* after this function returns, so don't path_put the path +* upon LSM rejection of O_PATH opening +*/ +return security_file_open (f); }if ((f->f_mode & (FMODE_READ |FMODE_WRITE )) ==FMODE_READ ) {
Signature · 3 lines
2.43.0 base-kernel: v6.14-rc6
Al Viro
maintainerRe: [RFC PATCH 1/6] fs: invoke LSM file_open hook in do_dentry_open for O_PATH fds as well
Quoted 15 lines · Ryan Lee
Currently, opening
O_PATHfile descriptors completely bypasses the LSM infrastructure. Invoking the LSMfile_openhook forO_PATHfds will be necessary for e.g. mediating thefsmount()syscall.Signed-off-by Ryan Lee <[email protected]>--- fs/open.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)fs/open.c +0
-0@@ -921,8 +921,13 @@ static int do_dentry_open(struct file *f,if (unlikely (f->f_flags &O_PATH )) {
fs/open.c static int do_dentry_open(struct file *f, · line 930
Quoted 10 lines
fs/open.c +6
-1f->f_mode =FMODE_PATH |FMODE_OPENED ;file_set_fsnotify_mode (f,FMODE_NONOTIFY ); f->f_op = &empty_fops; -return 0 ; +/* +* do_o_path in fs/namei.c unconditionally invokes path_put +* after this function returns, so don't path_put the path +* upon LSM rejection of O_PATH opening +*/ +return security_file_open (f);
Unconditional path_put() in do_o_path() has nothing to do with that - what gets dropped there is the reference acquired there; the reference acquired (and not dropped) here is the one that went into ->f_path. Since you are leaving FMODE_OPENED set, you will have __fput() drop that reference.
Basically, you are simulating behaviour on the O_DIRECT open of something that does not support O_DIRECT - return an error, with ->f_path and FMODE_OPENED left in place.
Said that, what I do not understand is the point of that exercise - why does LSM need to veto anything for those and why is security_file_open() the right place for such checks?
The second part is particularly interesting...
Christian Brauner
maintainerRe: [RFC PATCH 1/6] fs: invoke LSM file_open hook in do_dentry_open for O_PATH fds as well
Quoted 4 lines · Al Viro
Quoted 3 lines · Ryan Lee
Currently, opening
O_PATHfile descriptors completely bypasses the LSM infrastructure. Invoking the LSMfile_openhook forO_PATHfds will be necessary for e.g. mediating thefsmount()syscall.
LSM mediation for the mount api should be done by adding appropriate hooks to the new mount api.
Quoted 29 lines
Quoted 12 lines
Signed-off-by Ryan Lee <[email protected]>--- fs/open.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)fs/open.c +0
-0@@ -921,8 +921,13 @@ static int do_dentry_open(struct file *f,if (unlikely (f->f_flags &O_PATH )) {fs/open.c static int do_dentry_open(struct file *f, · line 930
Quoted 10 lines
fs/open.c +6
-1f->f_mode =FMODE_PATH |FMODE_OPENED ;file_set_fsnotify_mode (f,FMODE_NONOTIFY ); f->f_op = &empty_fops; -return 0 ; +/* +* do_o_path in fs/namei.c unconditionally invokes path_put +* after this function returns, so don't path_put the path +* upon LSM rejection of O_PATH opening +*/ +return security_file_open (f);Unconditional
path_put()indo_o_path()has nothing to do with that - what gets dropped there is the reference acquired there; the reference acquired (and not dropped) here is the one that went into ->f_path. Since you are leavingFMODE_OPENEDset, you will have__fput()drop that reference.
Quoted 6 lines
Basically, you are simulating behaviour on the
O_DIRECTopen of something that does not supportO_DIRECT- return an error, with ->f_path andFMODE_OPENEDleft in place.Said that, what I do not understand is the point of that exercise - why does LSM need to veto anything for those and why is
security_file_open()
I really think this is misguided. This should be done via proper hooks into apis that use O_PATH file descriptors for specific purposes but not for the generic open() path.
Quoted 1 line
the right place for such checks?
It isn't.
Quoted 2 lines
The second part is particularly interesting...
Paul Moore
maintainerRe: [RFC PATCH 1/6] fs: invoke LSM file_open hook in do_dentry_open for O_PATH fds as well
Quoted 42 lines · Christian Brauner
Quoted 4 lines · Al Viro
Quoted 3 lines · Ryan Lee
Currently, opening
O_PATHfile descriptors completely bypasses the LSM infrastructure. Invoking the LSMfile_openhook forO_PATHfds will be necessary for e.g. mediating thefsmount()syscall.LSM mediation for the mount api should be done by adding appropriate hooks to the new mount api.
Quoted 33 lines
Quoted 12 lines
Signed-off-by Ryan Lee <[email protected]>--- fs/open.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)fs/open.c +0
-0@@ -921,8 +921,13 @@ static int do_dentry_open(struct file *f,if (unlikely (f->f_flags &O_PATH )) {fs/open.c static int do_dentry_open(struct file *f, · line 930
Quoted 10 lines
fs/open.c +6
-1f->f_mode =FMODE_PATH |FMODE_OPENED ;file_set_fsnotify_mode (f,FMODE_NONOTIFY ); f->f_op = &empty_fops; -return 0 ; +/* +* do_o_path in fs/namei.c unconditionally invokes path_put +* after this function returns, so don't path_put the path +* upon LSM rejection of O_PATH opening +*/ +return security_file_open (f);Unconditional
path_put()indo_o_path()has nothing to do with that - what gets dropped there is the reference acquired there; the reference acquired (and not dropped) here is the one that went into ->f_path. Since you are leavingFMODE_OPENEDset, you will have__fput()drop that reference.Basically, you are simulating behaviour on the
O_DIRECTopen of something that does not supportO_DIRECT- return an error, with ->f_path andFMODE_OPENEDleft in place.
Quoted 6 lines
Quoted 2 lines
Said that, what I do not understand is the point of that exercise - why does LSM need to veto anything for those and why is
security_file_open()I really think this is misguided. This should be done via proper hooks into apis that use
O_PATHfile descriptors for specific purposes but not for the genericopen()path.
I agree that this patchset is at best incomplete, we don't add LSM hooks without at least one in-tree LSM demonstrating a need for it, and we don't see any of the LSMs actually making use of this new hook placement in this patchset. In the future Ryan, please ensure that the patchset actually does "something" visible, e.g. new functionality, bug fixes, etc. I understand part of your intent was to spark some discussion around O_PATH files, but without some initial code to do something meaningful, it's hard to have any real discussion that doesn't get lost in some rathole or tangent.
Beyond that, I can only speculate on Ryan's intent here, but based on some off-list discussions, it's possible Ryan is (re)using the security_file_open() hook in the O_PATH case not necessarily to gate the creation of an O_PATH file, but rather to capture some of the context when the O_PATH file is created. However, if that was the case I would think Ryan should be able to do that using the security_file_alloc() hook, although we would need to pass the file flags to that hook if Ryan wanted to do any special handling around O_PATH. Regardless, it's just guessing at this point and I've got enough things asking for attention that I can't spend any more time on this patchset simply guessing ...
Signature · 1 line
paul-moore.com
RFC PATCH 2/6 apparmor: explicitly skip mediation of O_PATH file descriptors
In reply to Ryan LeePreviously, we saw O_PATH fds only when mediating fd inheritance on exec, but because they would have no request associated with them, we would unconditionally let them be inherited. Until we have better handling of O_PATH fds, preserve the existing behavior of unconditionally allowing them.
--- security/apparmor/lsm.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
security/apparmor/lsm.c +10 -0
@@ -647,6 +647,16 @@ static int apparmor_file_open(struct file *file)return 0 ; } +/* +* Preserve the behavior of O_PATH fd creation not being mediated. +* +* TODO: we weren't handling O_PATH fds in aa_inherit_files anyways +* (all-zero request -> fds unconditionally inherited), so proper +* mediation of those will require changes in multiple places. +*/ +if (file->f_flags &O_PATH ) +return 0 ; + label =aa_get_newest_cred_label_condref (file->f_cred, &needput);if (!unconfined (label)) {struct mnt_idmap *idmap =file_mnt_idmap (file);
Signature · 3 lines
2.43.0 base-kernel: v6.14-rc6
RFC PATCH 3/6 landlock: explicitly skip mediation of O_PATH file descriptors
In reply to Ryan LeeLandlock currently does not have handling of O_PATH fds. Now that they are being passed to the file_open hook, explicitly skip mediation of them until we can handle them.
--- security/landlock/fs.c | 8 ++++++++ 1 file changed, 8 insertions(+)
security/landlock/fs.c +8 -0
@@ -1522,6 +1522,14 @@ static int hook_file_open(struct file *const file)if (!dom)return 0 ; +/* +* Preserve the behavior of O_PATH fd creation not being mediated, for +* now. Remove this when the comment below about handling O_PATH fds +* is resolved. +*/ +if (file->f_flags &O_PATH ) +return 0 ; +/* * Because a file may be opened with O_PATH, get_required_file_open_access() * may return 0. This case will be handled with a future Landlock
Signature · 3 lines
2.43.0 base-kernel: v6.14-rc6
RFC PATCH 4/6 selinux: explicitly skip mediation of O_PATH file descriptors
In reply to Ryan LeeNow that O_PATH fds are being passed to the file_open hook, unconditionally skip mediation of them to preserve existing behavior.
--- security/selinux/hooks.c | 5 +++++ 1 file changed, 5 insertions(+)
security/selinux/hooks.c +5 -0
@@ -4009,6 +4009,11 @@ static int selinux_file_open(struct file *file)*/ fsec->isid = isec->sid; fsec->pseqno =avc_policy_seqno (); + +/* Preserve the behavior of O_PATH fd creation not being mediated */ +if (file->f_flags &O_PATH ) +return 0 ; +/* * Since the inode label or policy seqno may have changed * between the selinux_inode_permission check and the saving
Signature · 3 lines
2.43.0 base-kernel: v6.14-rc6
RFC PATCH 5/6 smack: explicitly skip mediation of O_PATH file descriptors
In reply to Ryan LeeNow that O_PATH fds are being passed to the file_open hook, unconditionally skip mediation of them to preserve existing behavior.
--- security/smack/smack_lsm.c | 4 ++++ 1 file changed, 4 insertions(+)
security/smack/smack_lsm.c +4 -0
@@ -2062,6 +2062,10 @@ static int smack_file_open(struct file *file)struct smk_audit_info ad;int rc; +/* Preserve the behavior of O_PATH fd creation not being mediated */ +if (file->f_flags &O_PATH ) +return 0 ; +smk_ad_init (&ad, __func__,LSM_AUDIT_DATA_PATH );smk_ad_setfield_u_fs_path (&ad, file->f_path); rc =smk_tskacc (tsp,smk_of_inode (inode),MAY_READ , &ad);
Signature · 3 lines
2.43.0 base-kernel: v6.14-rc6
Casey Schaufler
maintainerRe: [RFC PATCH 5/6] smack: explicitly skip mediation of O_PATH file descriptors
Quoted 13 lines · Ryan Lee
Now that
O_PATHfds are being passed to thefile_openhook, unconditionally skip mediation of them to preserve existing behavior.Signed-off-by Ryan Lee <[email protected]>--- security/smack/smack_lsm.c | 4 ++++ 1 file changed, 4 insertions(+)security/smack/smack_lsm.c +0
-0
security/smack/smack_lsm.c static int smack_file_open(struct file *file) · line 2065
Quoted 4 lines
security/smack/smack_lsm.c +1
-0@@ -2062,6 +2062,10 @@ static int smack_file_open(struct file *file)struct smk_audit_info ad;int rc; +/* Preserve the behavior of O_PATH fd creation not being mediated */
In Smack the single line comment is discouraged. Please use
+ /*
+ * Preserve the behavior of O_PATH fd creation not being mediated
+ */
Quoted 6 lines
security/smack/smack_lsm.c +3
-0+if (file->f_flags &O_PATH ) +return 0 ; +smk_ad_init (&ad, __func__,LSM_AUDIT_DATA_PATH );smk_ad_setfield_u_fs_path (&ad, file->f_path); rc =smk_tskacc (tsp,smk_of_inode (inode),MAY_READ , &ad);
RFC PATCH 6/6 tomoyo: explicitly skip mediation of O_PATH file descriptors
In reply to Ryan LeeNow that O_PATH fds are being passed to the file_open hook, unconditionally skip mediation of them to preserve existing behavior.
--- security/tomoyo/file.c | 4 ++++ 1 file changed, 4 insertions(+)
security/tomoyo/file.c +4 -0
@@ -762,6 +762,10 @@ int tomoyo_check_open_permission(struct tomoyo_domain_info *domain, };int idx; +/* Preserve the behavior of O_PATH fd creation not being mediated */ +if (flag &O_PATH ) +return 0 ; + buf.name =NULL ; r.mode =TOMOYO_CONFIG_DISABLED ; idx =tomoyo_read_lock ();
Signature · 3 lines
2.43.0 base-kernel: v6.14-rc6