When the user specifies a directory to delete with the suffix '/',
the audit record fails to collect the filename, resulting in the
following logs:

 type=PATH msg=audit(10/30/2024 14:11:17.796:6304) : item=2 name=(null)
 type=PATH msg=audit(10/30/2024 14:11:17.796:6304) : item=1 name=(null)

It happens because the value of the variables dname, and n->name->name
in __audit_inode_child() differ only by the suffix '/'. This commit
treats this corner case by cleaning the input and passing the correct
filename to audit_compare_dname_path().

Steps to reproduce the issue:

 # auditctl -w /tmp
 $ mkdir /tmp/foo
 $ rm -r /tmp/foo/ or rmdir /tmp/foo/
 # ausearch -i | grep PATH | tail -3

This patch is based on a GitHub patch/PR by user @hqh2010.
https://github.com/linux-audit/audit-kernel/pull/148

Signed-off-by: Ricardo Robaina <[email protected]>
---
 kernel/auditsc.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/kernel/auditsc.c b/kernel/auditsc.c
index 6f0d6fb6523f..d4fbac6b71a8 100644
--- a/kernel/auditsc.c
+++ b/kernel/auditsc.c
@@ -2419,7 +2419,8 @@ void __audit_inode_child(struct inode *parent,
        struct audit_names *n, *found_parent = NULL, *found_child = NULL;
        struct audit_entry *e;
        struct list_head *list = &audit_filter_list[AUDIT_FILTER_FS];
-       int i;
+       int i, dlen, nlen;
+       char *fn = NULL;
 
        if (context->context == AUDIT_CTX_UNUSED)
                return;
@@ -2443,6 +2444,7 @@ void __audit_inode_child(struct inode *parent,
        if (inode)
                handle_one(inode);
 
+       dlen = strlen(dname->name);
        /* look for a parent entry first */
        list_for_each_entry(n, &context->names_list, list) {
                if (!n->name ||
@@ -2450,15 +2452,27 @@ void __audit_inode_child(struct inode *parent,
                     n->type != AUDIT_TYPE_UNKNOWN))
                        continue;
 
+               /* special case, entry name has the sufix "/" */
+               nlen = strlen(n->name->name);
+               if (dname->name[dlen - 1] != '/' && n->name->name[nlen - 1] == 
'/') {
+                       fn = kmalloc(PATH_MAX, GFP_KERNEL);
+                       if (!fn) {
+                               audit_panic("out of memory in 
__audit_inode_child()");
+                               return;
+                       }
+                       strscpy(fn, n->name->name, nlen);
+               }
+
                if (n->ino == parent->i_ino && n->dev == parent->i_sb->s_dev &&
                    !audit_compare_dname_path(dname,
-                                             n->name->name, n->name_len)) {
+                                             fn ? fn : n->name->name, 
n->name_len)) {
                        if (n->type == AUDIT_TYPE_UNKNOWN)
                                n->type = AUDIT_TYPE_PARENT;
                        found_parent = n;
                        break;
                }
        }
+       kfree(fn);
 
        cond_resched();
 
-- 
2.47.0


Reply via email to