Skip to content

Commit 15a9155

Browse files
author
Al Viro
committed
fix race in audit_get_nd()
don't rely on pathname resolution ending up twice at the same point... Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 586ce09 commit 15a9155

1 file changed

Lines changed: 32 additions & 53 deletions

File tree

‎kernel/audit_watch.c‎

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ int audit_watch_compare(struct audit_watch *watch, unsigned long ino, dev_t dev)
144144
}
145145

146146
/* Initialize a parent watch entry. */
147-
static struct audit_parent *audit_init_parent(struct nameidata *ndp)
147+
static struct audit_parent *audit_init_parent(struct path *path)
148148
{
149-
struct inode *inode = ndp->path.dentry->d_inode;
149+
struct inode *inode = path->dentry->d_inode;
150150
struct audit_parent *parent;
151151
int ret;
152152

@@ -353,53 +353,40 @@ static void audit_remove_parent_watches(struct audit_parent *parent)
353353
}
354354

355355
/* Get path information necessary for adding watches. */
356-
static int audit_get_nd(char *path, struct nameidata **ndp, struct nameidata **ndw)
356+
static int audit_get_nd(struct audit_watch *watch, struct path *parent)
357357
{
358-
struct nameidata *ndparent, *ndwatch;
358+
struct nameidata nd;
359+
struct dentry *d;
359360
int err;
360361

361-
ndparent = kmalloc(sizeof(*ndparent), GFP_KERNEL);
362-
if (unlikely(!ndparent))
363-
return -ENOMEM;
362+
err = path_lookup(watch->path, LOOKUP_PARENT, &nd);
363+
if (err)
364+
return err;
364365

365-
ndwatch = kmalloc(sizeof(*ndwatch), GFP_KERNEL);
366-
if (unlikely(!ndwatch)) {
367-
kfree(ndparent);
368-
return -ENOMEM;
366+
if (nd.last_type != LAST_NORM) {
367+
path_put(&nd.path);
368+
return -EINVAL;
369369
}
370370

371-
err = path_lookup(path, LOOKUP_PARENT, ndparent);
372-
if (err) {
373-
kfree(ndparent);
374-
kfree(ndwatch);
375-
return err;
371+
mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
372+
d = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
373+
if (IS_ERR(d)) {
374+
mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
375+
path_put(&nd.path);
376+
return PTR_ERR(d);
376377
}
377-
378-
err = path_lookup(path, 0, ndwatch);
379-
if (err) {
380-
kfree(ndwatch);
381-
ndwatch = NULL;
378+
if (d->d_inode) {
379+
/* update watch filter fields */
380+
watch->dev = d->d_inode->i_sb->s_dev;
381+
watch->ino = d->d_inode->i_ino;
382382
}
383+
mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
383384

384-
*ndp = ndparent;
385-
*ndw = ndwatch;
386-
385+
*parent = nd.path;
386+
dput(d);
387387
return 0;
388388
}
389389

390-
/* Release resources used for watch path information. */
391-
static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw)
392-
{
393-
if (ndp) {
394-
path_put(&ndp->path);
395-
kfree(ndp);
396-
}
397-
if (ndw) {
398-
path_put(&ndw->path);
399-
kfree(ndw);
400-
}
401-
}
402-
403390
/* Associate the given rule with an existing parent.
404391
* Caller must hold audit_filter_mutex. */
405392
static void audit_add_to_parent(struct audit_krule *krule,
@@ -440,31 +427,24 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list)
440427
{
441428
struct audit_watch *watch = krule->watch;
442429
struct audit_parent *parent;
443-
struct nameidata *ndp = NULL, *ndw = NULL;
430+
struct path parent_path;
444431
int h, ret = 0;
445432

446433
mutex_unlock(&audit_filter_mutex);
447434

448435
/* Avoid calling path_lookup under audit_filter_mutex. */
449-
ret = audit_get_nd(watch->path, &ndp, &ndw);
450-
if (ret) {
451-
/* caller expects mutex locked */
452-
mutex_lock(&audit_filter_mutex);
453-
goto error;
454-
}
436+
ret = audit_get_nd(watch, &parent_path);
455437

438+
/* caller expects mutex locked */
456439
mutex_lock(&audit_filter_mutex);
457440

458-
/* update watch filter fields */
459-
if (ndw) {
460-
watch->dev = ndw->path.dentry->d_inode->i_sb->s_dev;
461-
watch->ino = ndw->path.dentry->d_inode->i_ino;
462-
}
441+
if (ret)
442+
return ret;
463443

464444
/* either find an old parent or attach a new one */
465-
parent = audit_find_parent(ndp->path.dentry->d_inode);
445+
parent = audit_find_parent(parent_path.dentry->d_inode);
466446
if (!parent) {
467-
parent = audit_init_parent(ndp);
447+
parent = audit_init_parent(&parent_path);
468448
if (IS_ERR(parent)) {
469449
ret = PTR_ERR(parent);
470450
goto error;
@@ -479,9 +459,8 @@ int audit_add_watch(struct audit_krule *krule, struct list_head **list)
479459
h = audit_hash_ino((u32)watch->ino);
480460
*list = &audit_inode_hash[h];
481461
error:
482-
audit_put_nd(ndp, ndw); /* NULL args OK */
462+
path_put(&parent_path);
483463
return ret;
484-
485464
}
486465

487466
void audit_remove_watch_rule(struct audit_krule *krule)

0 commit comments

Comments
 (0)