Skip to content

Commit c86b300

Browse files
committed
fs: add kern_path_locked_negative()
The audit code relies on the fact that kern_path_locked() returned a path even for a negative dentry. If it doesn't find a valid dentry it immediately calls: audit_find_parent(d_backing_inode(parent_path.dentry)); which assumes that parent_path.dentry is still valid. But it isn't since kern_path_locked() has been changed to path_put() also for a negative dentry. Fix this by adding a helper that implements the required audit semantics and allows us to fix the immediate bleeding. We can find a unified solution for this afterwards. Link: https://lore.kernel.org/20250414-rennt-wimmeln-f186c3a780f1@brauner Fixes: 1c3cb50 ("VFS: change kern_path_locked() and user_path_locked_at() to never return negative dentry") Reported-and-tested-by: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent ddee68c commit c86b300

File tree

3 files changed

+60
-22
lines changed

3 files changed

+60
-22
lines changed

fs/namei.c

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,27 +1665,20 @@ static struct dentry *lookup_dcache(const struct qstr *name,
16651665
return dentry;
16661666
}
16671667

1668-
/*
1669-
* Parent directory has inode locked exclusive. This is one
1670-
* and only case when ->lookup() gets called on non in-lookup
1671-
* dentries - as the matter of fact, this only gets called
1672-
* when directory is guaranteed to have no in-lookup children
1673-
* at all.
1674-
* Will return -ENOENT if name isn't found and LOOKUP_CREATE wasn't passed.
1675-
* Will return -EEXIST if name is found and LOOKUP_EXCL was passed.
1676-
*/
1677-
struct dentry *lookup_one_qstr_excl(const struct qstr *name,
1678-
struct dentry *base,
1679-
unsigned int flags)
1668+
static struct dentry *lookup_one_qstr_excl_raw(const struct qstr *name,
1669+
struct dentry *base,
1670+
unsigned int flags)
16801671
{
1681-
struct dentry *dentry = lookup_dcache(name, base, flags);
1672+
struct dentry *dentry;
16821673
struct dentry *old;
1683-
struct inode *dir = base->d_inode;
1674+
struct inode *dir;
16841675

1676+
dentry = lookup_dcache(name, base, flags);
16851677
if (dentry)
1686-
goto found;
1678+
return dentry;
16871679

16881680
/* Don't create child dentry for a dead directory. */
1681+
dir = base->d_inode;
16891682
if (unlikely(IS_DEADDIR(dir)))
16901683
return ERR_PTR(-ENOENT);
16911684

@@ -1698,7 +1691,24 @@ struct dentry *lookup_one_qstr_excl(const struct qstr *name,
16981691
dput(dentry);
16991692
dentry = old;
17001693
}
1701-
found:
1694+
return dentry;
1695+
}
1696+
1697+
/*
1698+
* Parent directory has inode locked exclusive. This is one
1699+
* and only case when ->lookup() gets called on non in-lookup
1700+
* dentries - as the matter of fact, this only gets called
1701+
* when directory is guaranteed to have no in-lookup children
1702+
* at all.
1703+
* Will return -ENOENT if name isn't found and LOOKUP_CREATE wasn't passed.
1704+
* Will return -EEXIST if name is found and LOOKUP_EXCL was passed.
1705+
*/
1706+
struct dentry *lookup_one_qstr_excl(const struct qstr *name,
1707+
struct dentry *base, unsigned int flags)
1708+
{
1709+
struct dentry *dentry;
1710+
1711+
dentry = lookup_one_qstr_excl_raw(name, base, flags);
17021712
if (IS_ERR(dentry))
17031713
return dentry;
17041714
if (d_is_negative(dentry) && !(flags & LOOKUP_CREATE)) {
@@ -2762,6 +2772,29 @@ static struct dentry *__kern_path_locked(int dfd, struct filename *name, struct
27622772
return d;
27632773
}
27642774

2775+
struct dentry *kern_path_locked_negative(const char *name, struct path *path)
2776+
{
2777+
struct filename *filename __free(putname) = getname_kernel(name);
2778+
struct dentry *d;
2779+
struct qstr last;
2780+
int type, error;
2781+
2782+
error = filename_parentat(AT_FDCWD, filename, 0, path, &last, &type);
2783+
if (error)
2784+
return ERR_PTR(error);
2785+
if (unlikely(type != LAST_NORM)) {
2786+
path_put(path);
2787+
return ERR_PTR(-EINVAL);
2788+
}
2789+
inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
2790+
d = lookup_one_qstr_excl_raw(&last, path->dentry, 0);
2791+
if (IS_ERR(d)) {
2792+
inode_unlock(path->dentry->d_inode);
2793+
path_put(path);
2794+
}
2795+
return d;
2796+
}
2797+
27652798
struct dentry *kern_path_locked(const char *name, struct path *path)
27662799
{
27672800
struct filename *filename = getname_kernel(name);

include/linux/namei.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ extern struct dentry *kern_path_create(int, const char *, struct path *, unsigne
6262
extern struct dentry *user_path_create(int, const char __user *, struct path *, unsigned int);
6363
extern void done_path_create(struct path *, struct dentry *);
6464
extern struct dentry *kern_path_locked(const char *, struct path *);
65+
extern struct dentry *kern_path_locked_negative(const char *, struct path *);
6566
extern struct dentry *user_path_locked_at(int , const char __user *, struct path *);
6667
int vfs_path_parent_lookup(struct filename *filename, unsigned int flags,
6768
struct path *parent, struct qstr *last, int *type,

kernel/audit_watch.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,17 @@ static void audit_remove_parent_watches(struct audit_parent *parent)
347347
/* Get path information necessary for adding watches. */
348348
static int audit_get_nd(struct audit_watch *watch, struct path *parent)
349349
{
350-
struct dentry *d = kern_path_locked(watch->path, parent);
350+
struct dentry *d;
351+
352+
d = kern_path_locked_negative(watch->path, parent);
351353
if (IS_ERR(d))
352354
return PTR_ERR(d);
353-
/* update watch filter fields */
354-
watch->dev = d->d_sb->s_dev;
355-
watch->ino = d_backing_inode(d)->i_ino;
355+
356+
if (d_is_positive(d)) {
357+
/* update watch filter fields */
358+
watch->dev = d->d_sb->s_dev;
359+
watch->ino = d_backing_inode(d)->i_ino;
360+
}
356361

357362
inode_unlock(d_backing_inode(parent->dentry));
358363
dput(d);
@@ -418,11 +423,10 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list)
418423
/* caller expects mutex locked */
419424
mutex_lock(&audit_filter_mutex);
420425

421-
if (ret && ret != -ENOENT) {
426+
if (ret) {
422427
audit_put_watch(watch);
423428
return ret;
424429
}
425-
ret = 0;
426430

427431
/* either find an old parent or attach a new one */
428432
parent = audit_find_parent(d_backing_inode(parent_path.dentry));

0 commit comments

Comments
 (0)