struct filename, io_uring and audit troubles
Al Viro
maintainer Looks like things like async unlink might fuck the audit
very badly. io_uring does getname() in originating thread and uses the result at the time of operation, which can happen in a different thread. Moreover, by that time the original syscall might have very well returned.
The trouble is, getname() establishes linkage between the struct
filename and struct audit_name; filename->aname and audit_name->name respectively. struct filename can get moved from one thread to another; struct audit_name is very much tied to audit_context, which is per-thread
- first few (5, currently) audit_name instances are embedded intoaudit_context. The rest gets allocated dynamically, but all of them
are placed on audit_context::names_list.
At audit_free_names() they are all wiped out - references
back to filename instances are dropped, dynamically allocated ones are freed, and while embedded ones survive, they are zeroed out on reuse by audit_alloc_name(). audit_free_names() is called on each audit_reset_context(), which is done by __audit_syscall_exit() and (in states other than AUDIT_SYSCALL_CONTEXT) __audit_uring_exit().
Linkage from filename to audit_name is used by __audit_inode().
It definitely expects the reference back to filename to be stable. And in situation when io_uring has offloaded a directory operation to helper thread, that is not guaranteed.
Another fun bit is that both audit_inode() and audit_inode_child()
may bump the refcount on struct filename. Which can get really fishy if they get called by helper thread while the originator is exiting the syscall - putname() from audit_free_names() in originator vs. refcount increment in helper is Not Nice(tm), what with the refcount not being atomic.
Potential solutions:
* don't bother with audit_name creation and linkage in getname(); do that when we start using the sucker. Doing that from __set_nameidata() will catch the majority of the stuff that ever gets audit_inode* called for it (the only exceptions are mq_open(2) and mq_unlink(2)). Unfortunately, each audit_name instance gets spewed into logs, so we would need to bring the rest of that shite in, including the things like symlink bodies (note that for io_uring-originating symlink we'd need that done in do_symlinkat()), etc. Unpleasant, that.
* make refcount atomic, add a pointer to audit_context or even task_struct in audit_name, have the "use name->aname if the type is acceptable" logics in audit_inode dependent upon the name->aname->owner matching what we want. With some locking to make the check itself safe.
* make refcount atomic, get rid of ->aname and have audit_inode() scan the names_list for entries with matching ->name and type - and that before the existing scan with ->name->name comparisons.
* something else?
Suggestions _not_ involving creative uses of TARDIS would
be welcome.
Al Viro
maintainerQuoted 8 lines · Al Viro
* don't bother with
audit_namecreation and linkage ingetname(); do that when we start using the sucker. Doing that from__set_nameidata()will catch the majority of the stuff that ever gets audit_inode* called for it (the only exceptions aremq_open(2)andmq_unlink(2)). Unfortunately, eachaudit_nameinstance gets spewed into logs, so we would need to bring the rest of that shite in, including the things like symlink bodies (note that for io_uring-originating symlink we'd need that done indo_symlinkat()), etc. Unpleasant, that.
BTW, how much is exact order and number of PATH items in audit logs cast in stone?
For example,
char s[2][20] = {"/tmp/blah/x", "/tmp/blah/x"};
rename(s[0], s[1]);
rename(s[0], s[0]);produces 2 items (both for /tmp/blah) on the first call, and only 1 on the second. Does anything care about preserving that distinction?
And what in audit_inode{,_child}() behaviour can be changed? I mean, does anything care about the loop directions when we pick the candidate audit_name for conversion, etc.?
It's been a long time since I've touched audit, and I have done my best to purge memories of the specifications ;-/
Al Viro
maintainerQuoted 22 lines · Al Viro
Quoted 8 lines · Al Viro
* don't bother with
audit_namecreation and linkage ingetname(); do that when we start using the sucker. Doing that from__set_nameidata()will catch the majority of the stuff that ever gets audit_inode* called for it (the only exceptions aremq_open(2)andmq_unlink(2)). Unfortunately, eachaudit_nameinstance gets spewed into logs, so we would need to bring the rest of that shite in, including the things like symlink bodies (note that for io_uring-originating symlink we'd need that done indo_symlinkat()), etc. Unpleasant, that.BTW, how much is exact order and number of PATH items in audit logs cast in stone?
For example,
char s[2][20] = {"/tmp/blah/x", "/tmp/blah/x"}; rename(s[0], s[1]); rename(s[0], s[0]);produces 2 items (both for
/tmp/blah) on the first call, and only 1 on the second. Does anything care about preserving that distinction?
Quoted 6 lines
And what in audit_inode{,_child}() behaviour can be changed? I mean, does anything care about the loop directions when we pick the candidate
audit_namefor conversion, etc.?It's been a long time since I've touched audit, and I have done my best to purge memories of the specifications ;-/
Speaking of which, is there anything in specs that would require -F obj_uid=0 to give a match on symlink("foo", "bar") done by non-root in non-root-owned directory, but not on symlink("foo", "foo") in the same conditions?
Put it another way, could we possibly make the predicates that refer to inode state *not* evaluate to true on audit_name instances that had never been associated with any inodes? Currently, symlink body has such an audit_name instance, except when that instance had been picked by audit_inode_parent() for conversion (or had been shared from the very beginning in case of symlink(2) that had identical pointers passed in both arguments).
Al, carefully abstaining from any comments on the sanity of said specifications - it's a government document, after all, so those should be obvious...
Al Viro
maintainerQuoted 6 lines · Al Viro
Another fun bit is that both
audit_inode()andaudit_inode_child()
may bump the refcount on struct filename. Which can get really fishy if they get called by helper thread while the originator is exiting the syscall -putname()fromaudit_free_names()in originator vs. refcount increment in helper is Not Nice(tm), what with the refcount not being atomic.
*blink*
OK, I really wonder which version had I been reading at the time; refcount is, indeed, atomic these days.
Other problems (->aname pointing to other thread's struct audit_names and outliving reuse of those, as well as insane behaviour of audit predicates on symlink(2)) are, unfortunately, quite real - on the current mainline.
Jens Axboe
maintainerQuoted 9 lines · Al Viro
Quoted 6 lines · Al Viro
Another fun bit is that both
audit_inode()andaudit_inode_child()
may bump the refcount on struct filename. Which can get really fishy if they get called by helper thread while the originator is exiting the syscall -putname()fromaudit_free_names()in originator vs. refcount increment in helper is Not Nice(tm), what with the refcount not being atomic.
Quoted 8 lines
*blink*
OK, I really wonder which version had I been reading at the time; refcount is, indeed, atomic these days.
Other problems (->aname pointing to other thread's struct
audit_namesand outliving reuse of those, as well as insane behaviour of audit predicates on symlink(2)) are, unfortunately, quite real - on the current mainline.
Traveling but took a quick look. As far as I can tell, for the "reuse someone elses aname", we could do either:
1) Just don't reuse the entry. Then we can drop the struct filename->aname completely as well. Yes that might incur an extra alloc for the odd case of audit_enabled and being deep enough that the preallocated names have been used, but doesn't anyone really care? It'll be noise in the overhead anyway. Side note - that would unalign struct filename again. Would be nice to drop audit_names from a core fs struct...
2) Add a ref to struct audit_names, RCU kfree it when it drops to zero. This would mean dropping struct audit_context->preallocated_names, as otherwise we'd run into trouble there if a context gets blown away while someone else has a ref to that audit_names struct. We could do this without a ref as well, as long as we can store an audit_context pointer in struct audit_names and be able to validate it under RCU. If ctx doesn't match, don't use it.
And probably other ways too, those were just the two immediate ones I thought it. Seems like option 1 is simpler and just fine? Quick hack:
fs/namei.c +0 -2
@@ -206,7 +206,6 @@ getname_flags(const char __user *filename, int flags)atomic_set (&result->refcnt,1 ); result->uptr = filename; - result->aname =NULL ;audit_getname (result);return result; }
@@ -254,7 +253,6 @@ getname_kernel(const char * filename) }memcpy ((char *)result->name, filename, len); result->uptr =NULL ; - result->aname =NULL ;atomic_set (&result->refcnt,1 );audit_getname (result);
include/linux/fs.h +0 -2
@@ -2685,10 +2685,8 @@ struct filename {const char *name;/* pointer to actual string */ const __user char *uptr;/* original userland pointer */ atomic_t refcnt; -struct audit_names *aname;const char iname[]; }; -static_assert (offsetof(struct filename , iname) %sizeof (long ) ==0 );static inline struct mnt_idmap *file_mnt_idmap (const struct file *file) {
kernel/auditsc.c +0 -17
@@ -2240,7 +2240,6 @@ void __audit_getname(struct filename *name) n->name = name; n->name_len =AUDIT_NAME_FULL ; - name->aname = n;atomic_inc (&name->refcnt); }
@@ -2325,22 +2324,6 @@ void __audit_inode(struct filename *name, const struct dentry *dentry,if (!name) goto out_alloc; -/* -* If we have a pointer to an audit_names entry already, then we can -* just use it directly if the type is correct. -*/ - n = name->aname; -if (n) { -if (parent) { -if (n->type ==AUDIT_TYPE_PARENT || - n->type ==AUDIT_TYPE_UNKNOWN ) - goto out; - }else { -if (n->type !=AUDIT_TYPE_PARENT ) - goto out; - } - } -list_for_each_entry_reverse (n, &context->names_list, list) {if (n->ino) {/* valid inode number, use that for the comparison */
Signature · 1 line
Jens Axboe
Paul Moore
maintainerQuoted 38 lines · Jens Axboe
Quoted 9 lines · Al Viro
Quoted 6 lines · Al Viro
Another fun bit is that both
audit_inode()andaudit_inode_child()
may bump the refcount on struct filename. Which can get really fishy if they get called by helper thread while the originator is exiting the syscall -putname()fromaudit_free_names()in originator vs. refcount increment in helper is Not Nice(tm), what with the refcount not being atomic.Quoted 8 lines
*blink*
OK, I really wonder which version had I been reading at the time; refcount is, indeed, atomic these days.
Other problems (->aname pointing to other thread's struct
audit_namesand outliving reuse of those, as well as insane behaviour of audit predicates on symlink(2)) are, unfortunately, quite real - on the current mainline.Traveling but took a quick look. As far as I can tell, for the "reuse someone elses aname", we could do either:
1) Just don't reuse the entry. Then we can drop the struct
filename->anamecompletely as well. Yes that might incur an extra alloc for the odd case ofaudit_enabledand being deep enough that the preallocated names have been used, but doesn't anyone really care? It'll be noise in the overhead anyway. Side note - that would unalign struct filename again. Would be nice to dropaudit_namesfrom a core fs struct...2) Add a ref to struct
audit_names, RCU kfree it when it drops to zero. This would mean dropping structaudit_context->preallocated_names, as otherwise we'd run into trouble there if a context gets blown away while someone else has a ref to thataudit_namesstruct. We could do this without a ref as well, as long as we can store anaudit_contextpointer in structaudit_namesand be able to validate it under RCU. If ctx doesn't match, don't use it.
Quoted 2 lines
And probably other ways too, those were just the two immediate ones I thought it. Seems like option 1 is simpler and just fine? Quick hack:
[Sorry for the delay, between flying back home, and just not wanting to think about the kernel for a day, I took the weekend "off".]
Jens and I have talked about similar issues in the past, and I think the only real solution to ensure the correctness of the audit records and provide some consistency between the io_uring approach and traditional syscalls, is to introduce a mechanism where we create/clone an audit_context in the io_uring prep stage to capture things like PATH records, stash that audit_context in the io_kiocb struct, and then restore it later when io_uring does/finishes the operation. I'm reasonably confident that we don't need to do it for all of the io_uring ops, just the !audit_skip case.
I'm always open to ideas, but everything else I can think of is either far too op-specific to be maintainable long term, a performance nightmare, or just plain wrong with respect to the audit records.
I keep hoping to have some time to code it up properly, but so far this year has been an exercise in "I'll just put this fire over here with the other fire". Believe it or not, this is at the top of my TODO list, perhaps this week I can dedicate some time to this.
Signature · 1 line
paul-moore.com
Al Viro
maintainerQuoted 13 lines · Paul Moore
[Sorry for the delay, between flying back home, and just not wanting to think about the kernel for a day, I took the weekend "off".]
Jens and I have talked about similar issues in the past, and I think the only real solution to ensure the correctness of the audit records and provide some consistency between the
io_uringapproach and traditional syscalls, is to introduce a mechanism where we create/clone anaudit_contextin theio_uringprep stage to capture things like PATH records, stash thataudit_contextin theio_kiocbstruct, and then restore it later whenio_uringdoes/finishes the operation. I'm reasonably confident that we don't need to do it for all of theio_uringops, just the !audit_skip case.
Quoted 8 lines
I'm always open to ideas, but everything else I can think of is either far too op-specific to be maintainable long term, a performance nightmare, or just plain wrong with respect to the audit records.
I keep hoping to have some time to code it up properly, but so far this year has been an exercise in "I'll just put this fire over here with the other fire". Believe it or not, this is at the top of my TODO list, perhaps this week I can dedicate some time to this.
What are the requirements regarding the order of audit_names in the ->names_list? I really don't like the idea of having struct filename tied to audit_context - io_uring is not the only context where it might make sense to treat struct filename as first-class citizens.
And having everything that passed through getname()/getname_kernel() shoved into ->names_list leads to very odd behaviour, especially with audit_names conversions in audit_inode()/audit_inode_child().
Look at the handling of AUDIT_DEV{MAJOR,MINOR} or AUDIT_OBJ_{UID,GID} or AUDIT_COMPARE_..._TO_OBJ; should they really apply to audit_names resulting from copying the symlink body into the kernel? And if they should be applied to audit_names instance that had never been associated with any inode, should that depend upon the string in those being equal to another argument of the same syscall?
I'm going through the kernel/auditsc.c right now, but it's more of a "document what it does" - I don't have the specs and I certainly don't remember such details.
Al Viro
maintainerQuoted 7 lines · Jens Axboe
1) Just don't reuse the entry. Then we can drop the struct
filename->anamecompletely as well. Yes that might incur an extra alloc for the odd case ofaudit_enabledand being deep enough that the preallocated names have been used, but doesn't anyone really care? It'll be noise in the overhead anyway. Side note - that would unalign struct filename again. Would be nice to dropaudit_namesfrom a core fs struct...
You'll get different output in logs, though. Whether that breaks userland setups/invalidates certifications/etc.... fuck knows.
If anything, a loop through the list, searching for matching entry would be safer in that respect. Order of the items... might or might not be an issue - see above.
Quoted 2 lines
2) Add a ref to struct
audit_names, RCU kfree it when it drops to zero. This would mean dropping structaudit_context->preallocated_names, as
Costly, that.
Quoted 5 lines
otherwise we'd run into trouble there if a context gets blown away while someone else has a ref to that
audit_namesstruct. We could do this without a ref as well, as long as we can store anaudit_contextpointer in structaudit_namesand be able to validate it under RCU. If ctx doesn't match, don't use it.
That's one of the variants I mentioned upthread...
Paul Moore
maintainerQuoted 18 lines · Al Viro
Quoted 17 lines · Paul Moore
[Sorry for the delay, between flying back home, and just not wanting to think about the kernel for a day, I took the weekend "off".]
Jens and I have talked about similar issues in the past, and I think the only real solution to ensure the correctness of the audit records and provide some consistency between the
io_uringapproach and traditional syscalls, is to introduce a mechanism where we create/clone anaudit_contextin theio_uringprep stage to capture things like PATH records, stash thataudit_contextin theio_kiocbstruct, and then restore it later whenio_uringdoes/finishes the operation. I'm reasonably confident that we don't need to do it for all of theio_uringops, just the !audit_skip case.I'm always open to ideas, but everything else I can think of is either far too op-specific to be maintainable long term, a performance nightmare, or just plain wrong with respect to the audit records.
Quoted 7 lines
Quoted 4 lines
I keep hoping to have some time to code it up properly, but so far this year has been an exercise in "I'll just put this fire over here with the other fire". Believe it or not, this is at the top of my TODO list, perhaps this week I can dedicate some time to this.
What are the requirements regarding the order of
audit_namesin the ->names_list?
Uncertain. As things currently stand there isn't really an explicit ordering between the PATH records and the syscall, there is the implicit order in which the PATH records appear in the event, but I don't know that I would read too much into that.
From my point of view, stuff like that is largely driven by enterprise distros chasing 3rd party security certifications so they can sell products/services to a certain class of users. These are the same enterprise distros that haven't really bothered to contribute a lot to the upstream Linux kernel's audit subsystem lately so I'm not going to worry too much about them at this point.
Quoted 3 lines
I really don't like the idea of having struct filename
tied toaudit_context-io_uringis not the only context where it might make sense to treat struct filename as first-class citizens.
From an audit perspective this is larger than just path walks, if I recall previous conversations correctly, there was also an issue with sockaddrs (slightly different problem, same solution).
I'm not opposed to seeing better support for struct filename, I think in a lot of cases that would make things easier for audit (especially where I would like to take audit ... eventually). Assuming your ideas for struct filename don't significantly break audit you can consider me supportive so long as we still have a way to take a struct filename reference inside the audit_context; we need to keep that ref until syscall/io_uring-op exit time as we can't be certain if we need to log the PATH until we know the success/fail status of the operation (among other things).
Quoted 11 lines
And having everything that passed through getname()/getname_kernel() shoved into ->names_list leads to very odd behaviour, especially with
audit_namesconversions in audit_inode()/audit_inode_child().Look at the handling of AUDIT_DEV{MAJOR,MINOR} or AUDIT_OBJ_{UID,GID} or AUDIT_COMPARE_..._TO_OBJ; should they really apply to
audit_namesresulting from copying the symlink body into the kernel? And if they should be applied toaudit_namesinstance that had never been associated with any inode, should that depend upon the string in those being equal to another argument of the same syscall?
Quoted 3 lines
I'm going through the
kernel/auditsc.cright now, but it's more of a "document what it does" - I don't have the specs and I certainly don't remember such details.
My approach to audit is "do what makes sense for a normal person", if somebody needs silly behavior to satisfy some security cert then they can get involved in upstream development and send me patches that don't suck.
Signature · 1 line
paul-moore.com
Al Viro
maintainerQuoted 11 lines · Paul Moore
Quoted 11 lines
And having everything that passed through getname()/getname_kernel() shoved into ->names_list leads to very odd behaviour, especially with
audit_namesconversions in audit_inode()/audit_inode_child().Look at the handling of AUDIT_DEV{MAJOR,MINOR} or AUDIT_OBJ_{UID,GID} or AUDIT_COMPARE_..._TO_OBJ; should they really apply to
audit_namesresulting from copying the symlink body into the kernel? And if they should be applied toaudit_namesinstance that had never been associated with any inode, should that depend upon the string in those being equal to another argument of the same syscall?
Quoted 8 lines
Quoted 3 lines
I'm going through the
kernel/auditsc.cright now, but it's more of a "document what it does" - I don't have the specs and I certainly don't remember such details.My approach to audit is "do what makes sense for a normal person", if somebody needs silly behavior to satisfy some security cert then they can get involved in upstream development and send me patches that don't suck.
root@kvm1:/tmp# auditctl -l -a always,exit -S all -F dir=/tmp/blah -F perm=rwxa -F obj_uid=0 root@kvm1:/tmp# su - al al@kvm1:~$ cd /tmp/blah al@kvm1:/tmp/blah$ ln -s a a al@kvm1:/tmp/blah$ ln -s c b al@kvm1:/tmp/blah$ ls -l total 0 lrwxrwxrwx 1 al al 1 Sep 23 13:44 a -> a lrwxrwxrwx 1 al al 1 Sep 23 13:44 b -> c al@kvm1:/tmp/blah$ ls -ld drwxr-xr-x 2 al al 4096 Sep 23 13:44 .
resulting in
type=USER_START msg=audit(1727113434.684:497): pid=3433 uid=0 auid=0 ses=1 subj=unconfined msg='op=PAM:session_open grantors=pam_key
init,pam_env,pam_env,pam_mail,pam_limits,pam_permit,pam_unix,pam_ecryptfs acct="al" exe="/usr/bin/su" hostname=? addr=? terminal=/de
v/pts/0 res=success'^]UID="root" AUID="root"
type=SYSCALL msg=audit(1727113466.464:498): arch=c000003e syscall=266 success=yes exit=0 a0=7ffc441ad843 a1=ffffff9c a2=7ffc441ad845 a3=7f10e4816a90 items=3 ppid=3434 pid=3449 auid=0 uid=1000 gid=1000 euid=1000 suid=1000 fsuid=1000 egid=1000 sgid=1000 fsgid=1000 tty=pts0 ses=1 comm="ln" exe="/usr/bin/ln" subj=unconfined key=(null)^]ARCH=x86_64 SYSCALL=symlinkat AUID="root" UID="al" GID="al" EUID="al" SUID="al" FSUID="al" EGID="al" SGID="al" FSGID="al" type=CWD msg=audit(1727113466.464:498): cwd="/tmp/blah"
type=PATH msg=audit(1727113466.464:498): item=0 name="/tmp/blah" inode=135460 dev=08:14 mode=041777 ouid=1000 ogid=1000 rdev=00:00 nametype=PARENT cap_fp=0 cap_fi=0 cap_fe=0 cap_fver=0 cap_frootid=0^]OUID="al" OGID="al"
type=PATH msg=audit(1727113466.464:498): item=1 name="c" nametype=UNKNOWN cap_fp=0 cap_fi=0 cap_fe=0 cap_fver=0 cap_frootid=0
type=PATH msg=audit(1727113466.464:498): item=2 name="b" inode=135497 dev=08:14 mode=0120777 ouid=1000 ogid=1000 rdev=00:00 nametype=CREATE cap_fp=0 cap_fi=0 cap_fe=0 cap_fver=0 cap_frootid=0^]OUID="al" OGID="al"
type=PROCTITLE msg=audit(1727113466.464:498): proctitle=6C6E002D7300630062
type=USER_END msg=audit(1727113560.852:499): pid=3433 uid=0 auid=0 ses=1 subj=unconfined msg='op=PAM:session_close grantors=pam_keyinit,pam_env,pam_env,pam_mail,pam_limits,pam_permit,pam_unix,pam_ecryptfs acct="al" exe="/usr/bin/su" hostname=? addr=? terminal=/dev/pts/0 res=success'^]UID="root" AUID="root"
Note that
* none of the filesystem objects involved have UID 0
* of two calls of symlinkat(), only the second one triggers the rule
... and removing the -F obj_uid=0 from the rule catches both (as expected), with the first one getting *two* items instead of 3 - there's PARENT (identical to the second call), there's CREATE (for "a" instead of "b", obviously) and there's no UNKNOWN with the symlink body.
Does the behaviour above make sense for a normal person, by your definition?
What happens is that we start with two UNKNOWN (for oldname and newname arguments of symlinkat(); incidentally, for the order is undefined - it's up to compiler which argument of do_symlinkat(getname(oldname), newdfd, getname(newname)) gets evaluated first). When we get through the call of __filename_parentat(), we get the newname's audit_names instance hit by audit_inode(...., AUDIT_INODE_PARENT). That converts the one for newname from UNKNOWN to PARENT. Then, in may_create(), we hit it with audit_inode_child() for AUDIT_TYPE_CHILD_CREATING. Which finds the parent (newname's audit_names, converted to PARENT by that point) and goes looking for child.
That's where the execution paths diverge - for symlink("a", "a") the oldname's audit_names instance looks like a valid candidate and gets converted to CHILD_CREATING. For symlink("c", "b") such candidate is there, so we get a new item allocated - CHILD_CREATING for "b". Name reference gets cloned from PARENT.
Now, ->symlink() is done, it succeeds and fsnotify_create(dir, dentry) gets called. We get an identical audit_inode_child() call, except that now dentry is positive. It finds PARENT instance, finds CHILD_CREATING one, and slaps the inode metadata into it, setting ->uid to non-zero.
As the result, in one case you are left with PARENT, UNKNOWN, CHILD_CREATING and in another - PARENT, CHILD_CREATING. Modulo reordering first two items if compiler decides to evaluate the first argument of do_symlinkat() call before the third one... In any case, AUDIT_OBJ_UID(0) gives a match on UNKNOWN - if it's there. Since both PARENT and CHILD_CREATING refer to non-root-owned inodes, AUDIT_OBJ_UID(0) does not match either...
Al Viro
maintainerOn Mon, Sep 23, 2024 at 12:14:29PM -0400, Paul Moore wrote:
[ordering and number of PATH items for syscall]
Quoted 6 lines
Quoted 1 line
From my point of view, stuff like that is largely driven by enterprise
distros chasing 3rd party security certifications so they can sell products/services to a certain class of users. These are the same enterprise distros that haven't really bothered to contribute a lot to the upstream Linux kernel's audit subsystem lately so I'm not going to worry too much about them at this point.
Umm... IIRC, sgrubb had been involved in the spec-related horrors, but that was a long time ago...
Quoted 7 lines
where I would like to take audit ... eventually). Assuming your ideas for struct filename don't significantly break audit you can consider me supportive so long as we still have a way to take a struct filename reference inside the
audit_context; we need to keep that ref until syscall/io_uring-op exit time as we can't be certain if we need to log the PATH until we know the success/fail status of the operation (among other things).
OK... As for what I would like to do:
* go through the VFS side of things and make sure we have a consistent
set of helpers that would take struct filename * - *not* the ad-hoc mix we have right now, when that's basically driven by io_uring borging them in one by one - or duplicates them without bothering to share helpers. E.g. mkdirat(2) does getname() and passes it to do_mkdirat(), which consumes the sucker; so does mknodat(2), but do_mknodat() is static. OTOH, path_setxattr() does setxattr_copy(), then retry_estale loop with user_path_at() + mnt_want_write() + do_setxattr() + mnt_drop_write() + path_put() as a body, while on io_uring side we have retry_estale loop with filename_lookup() + (io_uring helper that does mnt_want_write() + do_setxattr() + mnt_drop_write()) + path_put().
Sure, that user_path_at() call is getname() + filename_lookup() + putname(),
so they are equivalent, but keeping that shite in sync is going to be trouble.
* get rid of the "repeated getname() on the same address is going to
give you the same object" - that can't be relied upon without audit, for one thing and for another... having a syscall that takes two pathnames that gives different audit log (if not predicate evaluation) in cases when those are identical pointers vs. strings with identical contenst is, IMO, somewhat undesirable. That kills filename->uaddr.
* looking at the users of that stuff, I would probably prefer to
separate getname*() from insertion into audit context. It's not that tricky - __set_nameidata() catches *everything* that uses nd->name (i.e. all that audit_inode() calls in fs/namei.c use). What remains is
do_symlinkat() for symlink body
fs_index() on the argument (if we want to bother - it's a part
of weird Missed'em'V sysfs(2) syscall; I sincerely doubt that there's anybody who'd use it)
fsconfig(2) FSCONFIG_SET_PATH handling.
mq_open(2) and mq_unlink(2) - those bypass the normal pathwalk
logics, so __set_nameidata() won't catch them.
_maybe_ alpha osf_mount(2) devname argument; or we could get rid
of that stupidity and have it use copy_mount_string() like mount(2) does, instead of messing with getname().
That's all it takes. With that done, we can kill ->aname;
just look in the ->names_list for the first entry with given ->name - as in, given struct filename * value, no need to look inside.
Paul Moore
maintainerQuoted 34 lines · Al Viro
Quoted 11 lines · Paul Moore
Quoted 11 lines
And having everything that passed through getname()/getname_kernel() shoved into ->names_list leads to very odd behaviour, especially with
audit_namesconversions in audit_inode()/audit_inode_child().Look at the handling of AUDIT_DEV{MAJOR,MINOR} or AUDIT_OBJ_{UID,GID} or AUDIT_COMPARE_..._TO_OBJ; should they really apply to
audit_namesresulting from copying the symlink body into the kernel? And if they should be applied toaudit_namesinstance that had never been associated with any inode, should that depend upon the string in those being equal to another argument of the same syscall?Quoted 8 lines
Quoted 3 lines
I'm going through the
kernel/auditsc.cright now, but it's more of a "document what it does" - I don't have the specs and I certainly don't remember such details.My approach to audit is "do what makes sense for a normal person", if somebody needs silly behavior to satisfy some security cert then they can get involved in upstream development and send me patches that don't suck.
root@kvm1:/tmp# auditctl -l -a always,exit -S all -F dir=/tmp/blah -F perm=rwxa -F obj_uid=0 root@kvm1:/tmp# su - al al@kvm1:~$ cd /tmp/blah al@kvm1:/tmp/blah$ ln -s a a al@kvm1:/tmp/blah$ ln -s c b al@kvm1:/tmp/blah$ ls -l total 0 lrwxrwxrwx 1 al al 1 Sep 23 13:44 a -> a lrwxrwxrwx 1 al al 1 Sep 23 13:44 b -> c al@kvm1:/tmp/blah$ ls -ld drwxr-xr-x 2 al al 4096 Sep 23 13:44 .
...
Quoted 9 lines
Note that
* none of the filesystem objects involved have UID 0
* of two calls ofsymlinkat(), only the second one triggers the rule
... and removing the -F obj_uid=0 from the rule catches both (as expected), with the first one getting *two* items instead of 3 - there's PARENT (identical to the second call), there's CREATE (for "a" instead of "b", obviously) and there's no UNKNOWN with the symlink body.Does the behaviour above make sense for a normal person, by your definition?
I never said that there aren't plenty of bugs in the current implementation. Really. Audit is a train wreck for so many reasons, most of my time spent with audit is spent making sure it doesn't panic, or some other subsystem hasn't wrecked it even further.
If you're looking for someone to justify audit's behavior here, you'll need to keep looking. Personally I've got plans to burn the whole thing down if I ever get enough time to work on it, but in the meantime I want to try and make it as usable as possible. Patches are always welcome.
Signature · 1 line
paul-moore.com
Paul Moore
maintainerQuoted 5 lines · Al Viro
On Mon, Sep 23, 2024 at 12:14:29PM -0400, Paul Moore wrote:
[ordering and number of PATH items for syscall]
Quoted 1 line
Quoted 1 line
From my point of view, stuff like that is largely driven by enterprise
Quoted 8 lines
Quoted 5 lines
distros chasing 3rd party security certifications so they can sell products/services to a certain class of users. These are the same enterprise distros that haven't really bothered to contribute a lot to the upstream Linux kernel's audit subsystem lately so I'm not going to worry too much about them at this point.
Umm... IIRC, sgrubb had been involved in the spec-related horrors, but that was a long time ago...
Yep, he was. Last I spoke to Steve a year or so ago, audit was no longer part of his job description; Steve still maintains his userspace audit tools, but that is a nights/weekends job as far as I understand.
The last time I was involved in any audit/CC spec related work was well over a decade ago now, and all of those CC protection profiles have long since expired and been replaced.
Quoted 10 lines
Quoted 7 lines
where I would like to take audit ... eventually). Assuming your ideas for struct filename don't significantly break audit you can consider me supportive so long as we still have a way to take a struct filename reference inside the
audit_context; we need to keep that ref until syscall/io_uring-op exit time as we can't be certain if we need to log the PATH until we know the success/fail status of the operation (among other things).OK... As for what I would like to do:
Quoted 13 lines
* go through the VFS side of things and make sure we have a consistent
set of helpers that would take struct filename * - *not* the ad-hoc mix we have right now, when that's basically driven byio_uringborging them in one by one - or duplicates them without bothering to share helpers. E.g. mkdirat(2) doesgetname()and passes it todo_mkdirat(), which consumes the sucker; so does mknodat(2), butdo_mknodat()is static. OTOH,path_setxattr()doessetxattr_copy(), thenretry_estaleloop withuser_path_at()+mnt_want_write()+do_setxattr()+mnt_drop_write()+path_put()as a body, while onio_uringside we haveretry_estaleloop withfilename_lookup()+ (io_uringhelper that doesmnt_want_write()+do_setxattr()+mnt_drop_write()) +path_put().
Sure, thatuser_path_at()call isgetname()+filename_lookup()+putname(),
so they are equivalent, but keeping that shite in sync is going to be trouble.
I obviously trust you to do the right thing with the VFS bits, and having a well defined struct filename interface sounds like a good thing from an audit perspective. I don't believe it completely solves the audit/io_uring issue, but it should make things easier and hopefully will result in less chance of breakage in the future.
Quoted 6 lines
* get rid of the "repeated
getname()on the same address is going to
give you the same object" - that can't be relied upon without audit, for one thing and for another... having a syscall that takes two pathnames that gives different audit log (if not predicate evaluation) in cases when those are identical pointers vs. strings with identical contenst is, IMO, somewhat undesirable. That killsfilename->uaddr.
/uaddr/uptr/ if I'm following you correctly, but yeah, that all seems good.
Quoted 4 lines
* looking at the users of that stuff, I would probably prefer to
separate getname*() from insertion into audit context. It's not that tricky -__set_nameidata()catches *everything* that usesnd->name(i.e. all thataudit_inode()calls infs/namei.cuse).
That should be a pretty significant simplification, that sounds good to me.
Quoted 5 lines
... What remains is
do_symlinkat()for symlink body
fs_index()on the argument (if we want to bother - it's a part
of weird Missed'em'V sysfs(2) syscall; I sincerely doubt that there's anybody who'd use it)
We probably should bother, folks that really care about audit don't like blind spots. Perhaps make it a separate patch if it isn't too ugly to split it out.
Quoted 9 lines
fsconfig(2)
FSCONFIG_SET_PATHhandling.
mq_open(2)andmq_unlink(2)- those bypass the normal pathwalk
logics, so__set_nameidata()won't catch them.
_maybe_ alphaosf_mount(2)devname argument; or we could get rid
of that stupidity and have it usecopy_mount_string()like mount(2) does, instead of messing withgetname().That's all it takes. With that done, we can kill ->aname;just look in the ->names_list for the first entry with given ->name - as in, given struct filename * value, no need to look inside.
Seems reasonable to me. I can't imagine these special cases being any worse than what we have now in fs/namei.c, and if nothing else having a single catch point for the bulk of the VFS lookups makes it worth it as far as I'm concerned.
Signature · 1 line
paul-moore.com
Al Viro
maintainerQuoted 11 lines · Paul Moore
Quoted 2 lines
Umm... IIRC, sgrubb had been involved in the spec-related horrors, but that was a long time ago...
Yep, he was. Last I spoke to Steve a year or so ago, audit was no longer part of his job description; Steve still maintains his userspace audit tools, but that is a nights/weekends job as far as I understand.
The last time I was involved in any audit/CC spec related work was well over a decade ago now, and all of those CC protection profiles have long since expired and been replaced.
Interesting... I guess eparis would be the next victim^Wpossible source of information.
Quoted 8 lines
Quoted 6 lines
* get rid of the "repeated
getname()on the same address is going to
give you the same object" - that can't be relied upon without audit, for one thing and for another... having a syscall that takes two pathnames that gives different audit log (if not predicate evaluation) in cases when those are identical pointers vs. strings with identical contenst is, IMO, somewhat undesirable. That killsfilename->uaddr./uaddr/uptr/ if I'm following you correctly, but yeah, that all seems good.
*nod*
Quoted 13 lines
Quoted 4 lines
* looking at the users of that stuff, I would probably prefer to
separate getname*() from insertion into audit context. It's not that tricky -__set_nameidata()catches *everything* that usesnd->name(i.e. all thataudit_inode()calls infs/namei.cuse).That should be a pretty significant simplification, that sounds good to me.
Quoted 5 lines
... What remains is
do_symlinkat()for symlink body
fs_index()on the argument (if we want to bother - it's a part
of weird Missed'em'V sysfs(2) syscall; I sincerely doubt that there's anybody who'd use it)
Quoted 3 lines
We probably should bother, folks that really care about audit don't like blind spots. Perhaps make it a separate patch if it isn't too ugly to split it out.
Heh... I suggest you to look at the manpage of that thing.
sysfs(1, "ext2") => echo $((sed -ne "/\text2$/=" </proc/filesystems - 1)) sysfs(2, 10) => sed -ne "11s/.*\t//p" </proc/filesystems sysfs(3) => wc -l </proc/filesystems
Yes, really - find position of filesystem type in the list of registered filesystems by name (0-based numeration), find the name of filesystem type by position and find the number of registered filesystem types. Missed'em'V had no synthetic filesystems...
And the string is, of course, not a pathname of any sort, so I'd argue that spewing PATH record into audit log is a bug. Not that the number you get from sysfs(1, something) had been usable for anything other than passing it to sysfs(2, number) and getting the same string back - you can't pass that number to mount(2) or anything of that kind. I suspect that the only reason this syscall exists is some binary emulation - introduced in 1.1.11, not supported by glibc at least since 2014 (and almost certainly way before that).
Quoted 8 lines
Quoted 3 lines
That's all it takes. With that done, we can kill ->aname;just look in the ->names_list for the first entry with given ->name - as in, given struct filename * value, no need to look inside.
Seems reasonable to me. I can't imagine these special cases being any worse than what we have now in
fs/namei.c, and if nothing else having a single catch point for the bulk of the VFS lookups makes it worth it as far as I'm concerned.
Huh? Right now we allocate audit_names at getname_flags()/getname_kernel() time; grep for audit_getname() - that's as centralized as it gets. What I want to do is somewhat _de_centralize it; that way they would not go anywhere other than audit_context of the thread actually doing the work.
There is a lot of calls of audit_inode(), but I'm not planning to touch any of those.
Jens Axboe
maintainerQuoted 10 lines · Al Viro
Quoted 7 lines · Jens Axboe
1) Just don't reuse the entry. Then we can drop the struct
filename->anamecompletely as well. Yes that might incur an extra alloc for the odd case ofaudit_enabledand being deep enough that the preallocated names have been used, but doesn't anyone really care? It'll be noise in the overhead anyway. Side note - that would unalign struct filename again. Would be nice to dropaudit_namesfrom a core fs struct...
Quoted 2 lines
You'll get different output in logs, though. Whether that breaks userland setups/invalidates certifications/etc.... fuck knows.
No idea about that... But I'd say without strong evidence that this breaks userland for something as odd as audit, well... And honestly really a layering problem that struct filename has an audit link in there.
Quoted 8 lines
If anything, a loop through the list, searching for matching entry would be safer in that respect. Order of the items... might or might not be an issue - see above.
Quoted 2 lines
2) Add a ref to struct
audit_names, RCU kfree it when it drops to zero. This would mean dropping structaudit_context->preallocated_names, asCostly, that.
For sure. And you could keep preallocated_names if you rcu free the context too. But I strongly believe that approach #1 is, by far, the cheaper alternative. If we can tolerate the ordering potentially changing.
Quoted 7 lines
Quoted 5 lines
otherwise we'd run into trouble there if a context gets blown away while someone else has a ref to that
audit_namesstruct. We could do this without a ref as well, as long as we can store anaudit_contextpointer in structaudit_namesand be able to validate it under RCU. If ctx doesn't match, don't use it.That's one of the variants I mentioned upthread...
Sorry, still away on travels and conferences, so haven't been keeping up on replies.
Signature · 1 line
Jens Axboe
Al Viro
maintainerQuoted 5 lines · Al Viro
* go through the VFS side of things and make sure we have a consistent
set of helpers that would take struct filename * - *not* the ad-hoc mix we have right now, when that's basically driven byio_uringborging them in one by one - or duplicates them without bothering to share helpers. E.g. mkdirat(2) doesgetname()and passes it todo_mkdirat(), which
Quoted 8 lines
consumes the sucker; so does mknodat(2), but
do_mknodat()is static. OTOH,path_setxattr()doessetxattr_copy(), thenretry_estaleloop withuser_path_at()+mnt_want_write()+do_setxattr()+mnt_drop_write()+path_put()as a body, while onio_uringside we haveretry_estaleloop withfilename_lookup()+ (io_uringhelper that doesmnt_want_write()+do_setxattr()+mnt_drop_write()) +path_put().
Sure, thatuser_path_at()call isgetname()+filename_lookup()+putname(),
so they are equivalent, but keeping that shite in sync is going to be trouble.
BTW, re mess around xattr:
static int __io_getxattr_prep(struct io_kiocb *req,
const struct io_uring_sqe *sqe)
{
...
ix->ctx.cvalue = u64_to_user_ptr(READ_ONCE(sqe->addr2));
ix->ctx.size = READ_ONCE(sqe->len);
...
ret = strncpy_from_user(ix->ctx.kname->name, name,
sizeof(ix->ctx.kname->name));}
int io_fgetxattr(struct io_kiocb *req, unsigned int issue_flags) {
...
ret = do_getxattr(file_mnt_idmap(req->file), req->file->f_path.dentry, &ix->ctx);
...
}
ssize_t
do_getxattr(struct mnt_idmap *idmap, struct dentry *d,
struct xattr_ctx *ctx)
{
...
if (error > 0) {
if (ctx->size && copy_to_user(ctx->value, ctx->kvalue, error))...
}
and we have
struct xattr_ctx {
/* Value of attribute */
union {
const void __user *cvalue;
void __user *value;
};
...
}Undefined behaviour aside, there's something odd going on here:
why do we bother with copy-in in ->prep() when we do copy-out in ->issue() anyway? ->issue() does run with initiator's ->mm in use, right?
IOW, what's the io_uring policy on what gets copied in ->prep() vs. in ->issue()?
Paul Moore
maintainerQuoted 9 lines · Al Viro
Quoted 8 lines · Paul Moore
Quoted 2 lines
Umm... IIRC, sgrubb had been involved in the spec-related horrors, but that was a long time ago...
Yep, he was. Last I spoke to Steve a year or so ago, audit was no longer part of his job description; Steve still maintains his userspace audit tools, but that is a nights/weekends job as far as I understand.
Quoted 6 lines
Quoted 3 lines
The last time I was involved in any audit/CC spec related work was well over a decade ago now, and all of those CC protection profiles have long since expired and been replaced.
Interesting... I guess eparis would be the next victim^Wpossible source of information.
Eric was the one who dumped the audit subsystem in my lap ~10 years ago so he could run off and play with containers. Fortunately for Eric I was much more trusting back then and didn't read all the fine print before agreeing to look after audit.
Quoted 13 lines
Quoted 13 lines
Quoted 4 lines
* looking at the users of that stuff, I would probably prefer to
separate getname*() from insertion into audit context. It's not that tricky -__set_nameidata()catches *everything* that usesnd->name(i.e. all thataudit_inode()calls infs/namei.cuse).That should be a pretty significant simplification, that sounds good to me.
Quoted 5 lines
... What remains is
do_symlinkat()for symlink body
fs_index()on the argument (if we want to bother - it's a part
of weird Missed'em'V sysfs(2) syscall; I sincerely doubt that there's anybody who'd use it)
Quoted 5 lines
Quoted 3 lines
We probably should bother, folks that really care about audit don't like blind spots. Perhaps make it a separate patch if it isn't too ugly to split it out.
Heh... I suggest you to look at the manpage of that thing.
Ooof. That's something isn't it? Yeah, that's ugly, and since it doesn't really return any user or sensitive system information I think it's safe to skip - my mistake.
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines
That's all it takes. With that done, we can kill ->aname;just look in the ->names_list for the first entry with given ->name - as in, given struct filename * value, no need to look inside.
Seems reasonable to me. I can't imagine these special cases being any worse than what we have now in
fs/namei.c, and if nothing else having a single catch point for the bulk of the VFS lookups makes it worth it as far as I'm concerned.
Quoted 8 lines
Huh? Right now we allocate
audit_namesat getname_flags()/getname_kernel() time; grep foraudit_getname()- that's as centralized as it gets. What I want to do is somewhat_de_centralizeit; that way they would not go anywhere other thanaudit_contextof the thread actually doing the work.There is a lot of calls of
audit_inode(), but I'm not planning to touch any of those.
Sorry. I'm a bit foggy from traveling, and trying to play catch-up back at home. I'm sure it will make more sense once I see the patches.
Signature · 1 line
paul-moore.com
Jens Axboe
maintainerQuoted 58 lines · Al Viro
Quoted 5 lines · Al Viro
* go through the VFS side of things and make sure we have a consistent
set of helpers that would take struct filename * - *not* the ad-hoc mix we have right now, when that's basically driven byio_uringborging them in one by one - or duplicates them without bothering to share helpers. E.g. mkdirat(2) doesgetname()and passes it todo_mkdirat(), whichQuoted 8 lines
consumes the sucker; so does mknodat(2), but
do_mknodat()is static. OTOH,path_setxattr()doessetxattr_copy(), thenretry_estaleloop withuser_path_at()+mnt_want_write()+do_setxattr()+mnt_drop_write()+path_put()as a body, while onio_uringside we haveretry_estaleloop withfilename_lookup()+ (io_uringhelper that doesmnt_want_write()+do_setxattr()+mnt_drop_write()) +path_put().
Sure, thatuser_path_at()call isgetname()+filename_lookup()+putname(),
so they are equivalent, but keeping that shite in sync is going to be trouble.BTW, re mess around xattr:
static int __io_getxattr_prep(structio_kiocb*req,
const structio_uring_sqe*sqe)
{
...ix->ctx.cvalue = u64_to_user_ptr(READ_ONCE(sqe->addr2)); ix->ctx.size = READ_ONCE(sqe->len); ... ret = strncpy_from_user(ix->ctx.kname->name, name, sizeof(ix->ctx.kname->name));}
int
io_fgetxattr(struct io_kiocb *req, unsigned int issue_flags){
...ret = do_getxattr(file_mnt_idmap(req->file), req->file->f_path.dentry, &ix->ctx);...
}
ssize_t
do_getxattr(structmnt_idmap*idmap, struct dentry *d,
structxattr_ctx*ctx)
{
...if (error > 0) { if (ctx->size && copy_to_user(ctx->value, ctx->kvalue, error))...
}and we have
struct xattr_ctx { /* Value of attribute */ union { const void __user *cvalue; void __user *value; }; ... }
Quoted 7 lines
Undefined behaviour aside, there's something odd going on here:
why do we bother with copy-in in ->prep() when we do copy-out in ->issue() anyway? ->issue() does run with initiator's ->mm in use, right?IOW, what's the
io_uringpolicy on what gets copied in ->prep() vs. in ->issue()?
The normal policy is that anything that is read-only should remain stable after ->prep() has been called, so that ->issue() can use it. That means the application can keep it on-stack as long as it's valid until io_uring_submit() returns. For structs/buffers that are copied to after IO, those the application obviously need to keep around until they see a completion for that request. So yes, for the xattr cases where the struct is copied to at completion time, those do not need to be stable after ->prep(), could be handled purely on the ->issue() side.
Signature · 1 line
Jens Axboe
Al Viro
maintainerQuoted 8 lines · Jens Axboe
The normal policy is that anything that is read-only should remain stable after ->prep() has been called, so that ->issue() can use it. That means the application can keep it on-stack as long as it's valid until
io_uring_submit()returns. For structs/buffers that are copied to after IO, those the application obviously need to keep around until they see a completion for that request. So yes, for the xattr cases where the struct is copied to at completion time, those do not need to be stable after ->prep(), could be handled purely on the ->issue() side.
Hmm... Nothing in xattr is copied in both directions, actually.
AFAICS, the only copy-in you leave to ->issue() is the data for write and sendmsg and ->msg_control for sendmsg. Wait, there's that ioctl-like mess you've got, so anything that feels like doing (seems to include at least setsockopt)... Oh, well...
Jens Axboe
maintainerQuoted 11 lines · Al Viro
Quoted 8 lines · Jens Axboe
The normal policy is that anything that is read-only should remain stable after ->prep() has been called, so that ->issue() can use it. That means the application can keep it on-stack as long as it's valid until
io_uring_submit()returns. For structs/buffers that are copied to after IO, those the application obviously need to keep around until they see a completion for that request. So yes, for the xattr cases where the struct is copied to at completion time, those do not need to be stable after ->prep(), could be handled purely on the ->issue() side.
Quoted 1 line
Hmm... Nothing in xattr is copied in both directions, actually.
Even if it's just copied out at the end, it does not need to remain stable after ->prep(). But most things will still do prep appropriate things at prep time, just to keep it consistent across different op types. Regardless, other things read from the sqe need to be stable after prep anyway, so it's just the best place to do copy-in of structs too, even if they are written at completion time and could defer reading it in until issue. The ->issue() side is mostly just about that, issuing a request and posting a completion - or deferring a retry if it's not ready for issue just yet.
Quoted 4 lines
AFAICS, the only copy-in you leave to ->issue() is the data for write and sendmsg and ->msg_control for sendmsg. Wait, there's that ioctl-like mess you've got, so anything that feels like doing (seems to include at least setsockopt)... Oh, well...
Right, things that obviously write to buffers will do that there, as that's the context where the completion happens anyway. The setsockopt parts should just copy out at issue time.
Signature · 1 line
Jens Axboe
Al Viro
maintainerQuoted 8 lines · Paul Moore
Quoted 6 lines
* get rid of the "repeated
getname()on the same address is going to
give you the same object" - that can't be relied upon without audit, for one thing and for another... having a syscall that takes two pathnames that gives different audit log (if not predicate evaluation) in cases when those are identical pointers vs. strings with identical contenst is, IMO, somewhat undesirable. That killsfilename->uaddr./uaddr/uptr/ if I'm following you correctly, but yeah, that all seems good.
BTW, what should we do when e.g. mkdir(2) manages to get to the parent, calls audit_inode() to memorize that one and then gets -ESTALE from nfs_mkdir()? We repeat the pathwalk, this time with LOOKUP_REVAL (i.e. make sure to ask the server about each NFS directory we are visiting, even if it had been seen recently) and arrive to a different directory, which is not stale and where subdirectory creation succeeds.
The thing is, we call audit_inode(...., AUDIT_INODE_PARENT) twice. With the same name, but with different inodes. Should we log both, or should the latter call cannibalize the audit_names instance from the earlier?
Paul Moore
maintainerQuoted 9 lines · Al Viro
Quoted 7 lines · Paul Moore
Quoted 6 lines
* get rid of the "repeated
getname()on the same address is going to
give you the same object" - that can't be relied upon without audit, for one thing and for another... having a syscall that takes two pathnames that gives different audit log (if not predicate evaluation) in cases when those are identical pointers vs. strings with identical contenst is, IMO, somewhat undesirable. That killsfilename->uaddr.
Quoted 8 lines
Quoted 1 line
/uaddr/uptr/ if I'm following you correctly, but yeah, that all seems good.
BTW, what should we do when e.g. mkdir(2) manages to get to the parent, calls
audit_inode()to memorize that one and then gets-ESTALEfromnfs_mkdir()? We repeat the pathwalk, this time withLOOKUP_REVAL(i.e. make sure to ask the server about each NFS directory we are visiting, even if it had been seen recently) and arrive to a different directory, which is not stale and where subdirectory creation succeeds.
Ah, that's fun. I'm guessing we could run into similar issues with other network filesystems, or is this specific to NFS?
Quoted 3 lines
The thing is, we call
audit_inode(...., AUDIT_INODE_PARENT)twice. With the same name, but with different inodes. Should we log both, or should the latter call cannibalize theaudit_namesinstance from the earlier?
I think the proper behavior is to have the second call cannibalize the state from the first. The intent of logging is to capture the state when/where the new directory is created, since we never created a directory off the -ESTALE path I don't see why we would need to log it.
Signature · 1 line
paul-moore.com
Al Viro
maintainerQuoted 8 lines · Jens Axboe
The normal policy is that anything that is read-only should remain stable after ->prep() has been called, so that ->issue() can use it. That means the application can keep it on-stack as long as it's valid until
io_uring_submit()returns. For structs/buffers that are copied to after IO, those the application obviously need to keep around until they see a completion for that request. So yes, for the xattr cases where the struct is copied to at completion time, those do not need to be stable after ->prep(), could be handled purely on the ->issue() side.
Looks like io_fsetxattr() was missing audit_file()... Anyway, in a local branch I've added two helpers -
int file_setxattr(struct file *file, struct xattr_ctx *ctx); int filename_setxattr(int dfd, struct filename *filename,
struct xattr_ctx *ctx, unsigned int lookup_flags);
and converted both fs/xattr.c and io_uring/xattr.c to those.
Completely untested delta follows; it's _not_ in the final form, it misses getxattr side, etc.
BTW, I think fs/internal.h is a very wrong place for that, as well as for do_mkdirat() et.al. include/linux/marshalled_syscalls.h, perhaps?
fs/internal.h +3 -2
@@ -285,8 +285,9 @@ ssize_t do_getxattr(struct mnt_idmap *idmap, struct xattr_ctx *ctx);int setxattr_copy (const char __user *name,struct xattr_ctx *ctx); -int do_setxattr (struct mnt_idmap *idmap,struct dentry *dentry, -struct xattr_ctx *ctx); +int file_setxattr (struct file *file,struct xattr_ctx *ctx); +int filename_setxattr (int dfd,struct filename *filename, +struct xattr_ctx *ctx,unsigned int lookup_flags);int may_write_xattr (struct mnt_idmap *idmap,struct inode *inode);#ifdef CONFIG_FS_POSIX_ACL
fs/xattr.c +42 -25
104 lines not on this page · raw message
io_uring/xattr.c +3 -28
@@ -203,28 +203,14 @@ int io_fsetxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)return __io_setxattr_prep (req, sqe); } -static int __io_setxattr (struct io_kiocb *req,unsigned int issue_flags, -const struct path *path) -{ - struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr); -int ret; - - ret =mnt_want_write (path->mnt); -if (!ret) { - ret =do_setxattr (mnt_idmap (path->mnt), path->dentry, &ix->ctx); -mnt_drop_write (path->mnt); - } - -return ret; -} -int io_fsetxattr (struct io_kiocb *req,unsigned int issue_flags) { + struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);int ret;WARN_ON_ONCE (issue_flags &IO_URING_F_NONBLOCK ); - ret =__io_setxattr (req, issue_flags, &req->file->f_path); + ret =file_setxattr (req->file, &ix->ctx);io_xattr_finish (req, ret);return IOU_OK ; }
@@ -232,22 +218,11 @@ int io_fsetxattr(struct io_kiocb *req, unsigned int issue_flags)int io_setxattr (struct io_kiocb *req,unsigned int issue_flags) { struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr); -unsigned int lookup_flags =LOOKUP_FOLLOW ; -struct path path;int ret;WARN_ON_ONCE (issue_flags &IO_URING_F_NONBLOCK ); -retry: - ret =filename_lookup (AT_FDCWD , ix->filename, lookup_flags, &path,NULL ); -if (!ret) { - ret =__io_setxattr (req, issue_flags, &path); -path_put (&path); -if (retry_estale (ret, lookup_flags)) { - lookup_flags |=LOOKUP_REVAL ; - goto retry; - } - } + ret =filename_setxattr (AT_FDCWD , ix->filename, &ix->ctx,LOOKUP_FOLLOW );io_xattr_finish (req, ret);return IOU_OK ;