audit: don't take task_lock() in audit_exe_compare() code path
Paul Moore
maintainerPATCH v3 1/1 audit: don't take task_lock() in audit_exe_compare() code path
The get_task_exe_file() function locks the given task with task_lock() which when used inside audit_exe_compare() can cause deadlocks on systems that generate audit records when the task_lock() is held. We resolve this problem with two changes: ignoring those cases where the task being audited is not the current task, and changing our approach to obtaining the executable file struct to not require task_lock().
With the intent of the audit exe filter being to filter on audit events generated by processes started by the specified executable, it makes sense that we would only want to use the exe filter on audit records associated with the currently executing process, e.g. @current. If we are asked to filter records using a non-@current task_struct we can safely ignore the exe filter without negatively impacting the admin's expectations for the exe filter.
Knowing that we only have to worry about filtering the currently executing task in audit_exe_compare() we can do away with the task_lock() and call get_mm_exe_file() with @current->mm directly.
---
- v3
* added a !current->mm check
- v2
* dropped mmget()/mmput()
- v1
* initial revision
--- kernel/audit_watch.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/audit_watch.c +8 -1
@@ -527,11 +527,18 @@ int audit_exe_compare(struct task_struct *tsk, struct audit_fsnotify_mark *mark)unsigned long ino;dev_t dev; - exe_file =get_task_exe_file (tsk); +/* only do exe filtering if we are recording @current events/records */ +if (tsk != current) +return 0 ; + +if (WARN_ON_ONCE (!current->mm)) +return 0 ; + exe_file =get_mm_exe_file (current->mm);if (!exe_file)return 0 ; ino =file_inode (exe_file)->i_ino; dev =file_inode (exe_file)->i_sb->s_dev;fput (exe_file); +return audit_mark_compare (mark, ino, dev); }
Signature · 1 line
2.42.0
Paul Moore
maintainerQuoted 16 lines · Paul Moore
The
get_task_exe_file()function locks the given task withtask_lock()which when used insideaudit_exe_compare()can cause deadlocks on systems that generate audit records when thetask_lock()is held. We resolve this problem with two changes: ignoring those cases where the task being audited is not the current task, and changing our approach to obtaining the executable file struct to not requiretask_lock().With the intent of the audit exe filter being to filter on audit events generated by processes started by the specified executable, it makes sense that we would only want to use the exe filter on audit records associated with the currently executing process, e.g. @current. If we are asked to filter records using a non-@current
task_structwe can safely ignore the exe filter without negatively impacting the admin's expectations for the exe filter.
Quoted 8 lines
Knowing that we only have to worry about filtering the currently executing task in
audit_exe_compare()we can do away with thetask_lock()and callget_mm_exe_file()with @current->mm directly.Cc <[email protected]>Fixes 5efc244346f9 ("audit: fix exe_file access in audit_exe_compare")Reported-by Andreas Steinmetz <[email protected]>Reviewed-by John Johansen <[email protected]>
John, I took the liberty of preserving your 'Reviewed-by' tag since I felt the current->mm check was a minor addition, but if you feel otherwise let me know and I'll remove it.
Quoted 11 lines
Signed-off-by Paul Moore <[email protected]>---
- v3
* added a !current->mm check
- v2
* dropped mmget()/mmput()
- v1
* initial revision--- kernel/audit_watch.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
Signature · 1 line
paul-moore.com
Mateusz Guzik
Reviewed-byThis does fix the bug, and with the caveat I can't vouch for ignoring non-current in the routine:
Thanks for fixing the bug. Pretty weird it took this long for it to surface.
Quoted 45 lines · Paul Moore
Quoted 16 lines · Paul Moore
The
get_task_exe_file()function locks the given task withtask_lock()which when used insideaudit_exe_compare()can cause deadlocks on systems that generate audit records when thetask_lock()is held. We resolve this problem with two changes: ignoring those cases where the task being audited is not the current task, and changing our approach to obtaining the executable file struct to not requiretask_lock().With the intent of the audit exe filter being to filter on audit events generated by processes started by the specified executable, it makes sense that we would only want to use the exe filter on audit records associated with the currently executing process, e.g. @current. If we are asked to filter records using a non-@current
task_structwe can safely ignore the exe filter without negatively impacting the admin's expectations for the exe filter.Quoted 8 lines
Knowing that we only have to worry about filtering the currently executing task in
audit_exe_compare()we can do away with thetask_lock()and callget_mm_exe_file()with @current->mm directly.Cc <[email protected]>Fixes 5efc244346f9 ("audit: fix exe_file access in audit_exe_compare")Reported-by Andreas Steinmetz <[email protected]>Reviewed-by John Johansen <[email protected]>John, I took the liberty of preserving your 'Reviewed-by' tag since I felt the
current->mmcheck was a minor addition, but if you feel otherwise let me know and I'll remove it.Quoted 11 lines
Signed-off-by Paul Moore <[email protected]>---
- v3
* added a !current->mm check
- v2
* dropped mmget()/mmput()
- v1
* initial revision--- kernel/audit_watch.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)--
paul-moore.com
Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
Paul Moore
maintainerQuoted 3 lines · Mateusz Guzik
This does fix the bug, and with the caveat I can't vouch for ignoring non-current in the routine:
Understood, I'm vouching for that bit.
Quoted 3 lines
Reviewed-by Mateusz Guzik <[email protected]>Thanks for fixing the bug. Pretty weird it took this long for it to surface.
Yeah, I thought the same thing. Regardless, thanks for taking the time to review the fix.
Quoted 48 lines · Paul Moore
Quoted 42 lines · Paul Moore
Quoted 16 lines
The
get_task_exe_file()function locks the given task withtask_lock()which when used insideaudit_exe_compare()can cause deadlocks on systems that generate audit records when thetask_lock()is held. We resolve this problem with two changes: ignoring those cases where the task being audited is not the current task, and changing our approach to obtaining the executable file struct to not requiretask_lock().With the intent of the audit exe filter being to filter on audit events generated by processes started by the specified executable, it makes sense that we would only want to use the exe filter on audit records associated with the currently executing process, e.g. @current. If we are asked to filter records using a non-@current
task_structwe can safely ignore the exe filter without negatively impacting the admin's expectations for the exe filter.Quoted 8 lines
Knowing that we only have to worry about filtering the currently executing task in
audit_exe_compare()we can do away with thetask_lock()and callget_mm_exe_file()with @current->mm directly.Cc <[email protected]>Fixes 5efc244346f9 ("audit: fix exe_file access in audit_exe_compare")Reported-by Andreas Steinmetz <[email protected]>Reviewed-by John Johansen <[email protected]>John, I took the liberty of preserving your 'Reviewed-by' tag since I felt the
current->mmcheck was a minor addition, but if you feel otherwise let me know and I'll remove it.Quoted 11 lines
Signed-off-by Paul Moore <[email protected]>---
- v3
* added a !current->mm check
- v2
* dropped mmget()/mmput()
- v1
* initial revision--- kernel/audit_watch.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)Quoted 2 lines
--
paul-moore.com--
Mateusz Guzik <mjguzik gmail.com>
Signature · 1 line
paul-moore.com
Paul Moore
maintainerQuoted 32 lines · Paul Moore
The
get_task_exe_file()function locks the given task withtask_lock()which when used insideaudit_exe_compare()can cause deadlocks on systems that generate audit records when thetask_lock()is held. We resolve this problem with two changes: ignoring those cases where the task being audited is not the current task, and changing our approach to obtaining the executable file struct to not requiretask_lock().With the intent of the audit exe filter being to filter on audit events generated by processes started by the specified executable, it makes sense that we would only want to use the exe filter on audit records associated with the currently executing process, e.g. @current. If we are asked to filter records using a non-@current
task_structwe can safely ignore the exe filter without negatively impacting the admin's expectations for the exe filter.Knowing that we only have to worry about filtering the currently executing task in
audit_exe_compare()we can do away with thetask_lock()and callget_mm_exe_file()with @current->mm directly.Cc <[email protected]>Fixes 5efc244346f9 ("audit: fix exe_file access in audit_exe_compare")Reported-by Andreas Steinmetz <[email protected]>Reviewed-by John Johansen <[email protected]>Signed-off-by Paul Moore <[email protected]>---
- v3
* added a !current->mm check
- v2
* dropped mmget()/mmput()
- v1
* initial revision
Quoted 3 lines
--- kernel/audit_watch.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
Considering that we are only a few days away from v6.6, I've gone and merged this into audit/dev and will send this up to Linus during the merge window next week.
Signature · 1 line
paul-moore.com