2019-05-27 14:55:05 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2009-06-12 02:31:36 +08:00
|
|
|
/* audit_watch.c -- watching inodes
|
|
|
|
*
|
|
|
|
* Copyright 2003-2009 Red Hat, Inc.
|
|
|
|
* Copyright 2005 Hewlett-Packard Development Company, L.P.
|
|
|
|
* Copyright 2005 IBM Corporation
|
|
|
|
*/
|
|
|
|
|
2016-08-23 22:20:39 +08:00
|
|
|
#include <linux/file.h>
|
2009-06-12 02:31:36 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/audit.h>
|
|
|
|
#include <linux/kthread.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/fs.h>
|
2009-12-18 09:12:04 +08:00
|
|
|
#include <linux/fsnotify_backend.h>
|
2009-06-12 02:31:36 +08:00
|
|
|
#include <linux/namei.h>
|
|
|
|
#include <linux/netlink.h>
|
2017-05-02 22:16:05 +08:00
|
|
|
#include <linux/refcount.h>
|
2009-06-12 02:31:36 +08:00
|
|
|
#include <linux/sched.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2009-06-12 02:31:36 +08:00
|
|
|
#include <linux/security.h>
|
|
|
|
#include "audit.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reference counting:
|
|
|
|
*
|
2009-12-18 09:12:04 +08:00
|
|
|
* audit_parent: lifetime is from audit_init_parent() to receipt of an FS_IGNORED
|
2009-06-12 02:31:36 +08:00
|
|
|
* event. Each audit_watch holds a reference to its associated parent.
|
|
|
|
*
|
|
|
|
* audit_watch: if added to lists, lifetime is from audit_init_watch() to
|
|
|
|
* audit_remove_watch(). Additionally, an audit_watch may exist
|
|
|
|
* temporarily to assist in searching existing filter data. Each
|
|
|
|
* audit_krule holds a reference to its associated watch.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct audit_watch {
|
2017-05-02 22:16:05 +08:00
|
|
|
refcount_t count; /* reference count */
|
2009-06-12 02:31:36 +08:00
|
|
|
dev_t dev; /* associated superblock device */
|
2009-08-08 04:54:23 +08:00
|
|
|
char *path; /* insertion path */
|
2009-06-12 02:31:36 +08:00
|
|
|
unsigned long ino; /* associated inode number */
|
|
|
|
struct audit_parent *parent; /* associated parent */
|
|
|
|
struct list_head wlist; /* entry in parent->watches list */
|
2009-12-18 09:12:04 +08:00
|
|
|
struct list_head rules; /* anchor for krule->rlist */
|
2009-06-12 02:31:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct audit_parent {
|
2009-12-18 09:12:04 +08:00
|
|
|
struct list_head watches; /* anchor for audit_watch->wlist */
|
2009-12-18 10:24:24 +08:00
|
|
|
struct fsnotify_mark mark; /* fsnotify mark on the inode */
|
2009-06-12 02:31:36 +08:00
|
|
|
};
|
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
/* fsnotify handle. */
|
2010-10-21 08:23:50 +08:00
|
|
|
static struct fsnotify_group *audit_watch_group;
|
2009-06-12 02:31:36 +08:00
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
/* fsnotify events we care about. */
|
|
|
|
#define AUDIT_FS_WATCH (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
|
2017-08-15 19:00:37 +08:00
|
|
|
FS_MOVE_SELF | FS_EVENT_ON_CHILD | FS_UNMOUNT)
|
2009-06-12 02:31:36 +08:00
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
static void audit_free_parent(struct audit_parent *parent)
|
|
|
|
{
|
|
|
|
WARN_ON(!list_empty(&parent->watches));
|
|
|
|
kfree(parent);
|
|
|
|
}
|
|
|
|
|
2009-12-18 10:24:24 +08:00
|
|
|
static void audit_watch_free_mark(struct fsnotify_mark *entry)
|
2009-06-12 02:31:36 +08:00
|
|
|
{
|
|
|
|
struct audit_parent *parent;
|
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
parent = container_of(entry, struct audit_parent, mark);
|
2009-12-18 09:12:04 +08:00
|
|
|
audit_free_parent(parent);
|
2009-06-12 02:31:36 +08:00
|
|
|
}
|
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
static void audit_get_parent(struct audit_parent *parent)
|
|
|
|
{
|
|
|
|
if (likely(parent))
|
|
|
|
fsnotify_get_mark(&parent->mark);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audit_put_parent(struct audit_parent *parent)
|
|
|
|
{
|
|
|
|
if (likely(parent))
|
|
|
|
fsnotify_put_mark(&parent->mark);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find and return the audit_parent on the given inode. If found a reference
|
|
|
|
* is taken on this parent.
|
|
|
|
*/
|
|
|
|
static inline struct audit_parent *audit_find_parent(struct inode *inode)
|
|
|
|
{
|
|
|
|
struct audit_parent *parent = NULL;
|
2009-12-18 10:24:24 +08:00
|
|
|
struct fsnotify_mark *entry;
|
2009-12-18 09:12:04 +08:00
|
|
|
|
2016-12-21 23:28:45 +08:00
|
|
|
entry = fsnotify_find_mark(&inode->i_fsnotify_marks, audit_watch_group);
|
2009-12-18 09:12:04 +08:00
|
|
|
if (entry)
|
|
|
|
parent = container_of(entry, struct audit_parent, mark);
|
|
|
|
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
2009-06-12 02:31:36 +08:00
|
|
|
void audit_get_watch(struct audit_watch *watch)
|
|
|
|
{
|
2017-05-02 22:16:05 +08:00
|
|
|
refcount_inc(&watch->count);
|
2009-06-12 02:31:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void audit_put_watch(struct audit_watch *watch)
|
|
|
|
{
|
2017-05-02 22:16:05 +08:00
|
|
|
if (refcount_dec_and_test(&watch->count)) {
|
2009-06-12 02:31:36 +08:00
|
|
|
WARN_ON(watch->parent);
|
|
|
|
WARN_ON(!list_empty(&watch->rules));
|
|
|
|
kfree(watch->path);
|
|
|
|
kfree(watch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-21 08:23:50 +08:00
|
|
|
static void audit_remove_watch(struct audit_watch *watch)
|
2009-06-12 02:31:36 +08:00
|
|
|
{
|
|
|
|
list_del(&watch->wlist);
|
2009-12-18 09:12:04 +08:00
|
|
|
audit_put_parent(watch->parent);
|
2009-06-12 02:31:36 +08:00
|
|
|
watch->parent = NULL;
|
|
|
|
audit_put_watch(watch); /* match initial get */
|
|
|
|
}
|
|
|
|
|
|
|
|
char *audit_watch_path(struct audit_watch *watch)
|
|
|
|
{
|
|
|
|
return watch->path;
|
|
|
|
}
|
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
int audit_watch_compare(struct audit_watch *watch, unsigned long ino, dev_t dev)
|
2009-06-12 02:31:36 +08:00
|
|
|
{
|
2015-08-06 11:48:20 +08:00
|
|
|
return (watch->ino != AUDIT_INO_UNSET) &&
|
2009-12-18 09:12:04 +08:00
|
|
|
(watch->ino == ino) &&
|
|
|
|
(watch->dev == dev);
|
2009-06-12 02:31:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize a parent watch entry. */
|
2011-02-17 04:08:54 +08:00
|
|
|
static struct audit_parent *audit_init_parent(struct path *path)
|
2009-06-12 02:31:36 +08:00
|
|
|
{
|
2015-03-18 06:26:21 +08:00
|
|
|
struct inode *inode = d_backing_inode(path->dentry);
|
2009-06-12 02:31:36 +08:00
|
|
|
struct audit_parent *parent;
|
2009-12-18 09:12:04 +08:00
|
|
|
int ret;
|
2009-06-12 02:31:36 +08:00
|
|
|
|
|
|
|
parent = kzalloc(sizeof(*parent), GFP_KERNEL);
|
|
|
|
if (unlikely(!parent))
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&parent->watches);
|
|
|
|
|
2016-12-22 01:06:12 +08:00
|
|
|
fsnotify_init_mark(&parent->mark, audit_watch_group);
|
2009-12-18 09:12:04 +08:00
|
|
|
parent->mark.mask = AUDIT_FS_WATCH;
|
2018-04-21 07:10:55 +08:00
|
|
|
ret = fsnotify_add_inode_mark(&parent->mark, inode, 0);
|
2009-12-18 09:12:04 +08:00
|
|
|
if (ret < 0) {
|
2009-12-18 09:12:04 +08:00
|
|
|
audit_free_parent(parent);
|
2009-12-18 09:12:04 +08:00
|
|
|
return ERR_PTR(ret);
|
2009-06-12 02:31:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize a watch entry. */
|
|
|
|
static struct audit_watch *audit_init_watch(char *path)
|
|
|
|
{
|
|
|
|
struct audit_watch *watch;
|
|
|
|
|
|
|
|
watch = kzalloc(sizeof(*watch), GFP_KERNEL);
|
|
|
|
if (unlikely(!watch))
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&watch->rules);
|
2017-05-02 22:16:05 +08:00
|
|
|
refcount_set(&watch->count, 1);
|
2009-06-12 02:31:36 +08:00
|
|
|
watch->path = path;
|
2015-08-06 11:48:20 +08:00
|
|
|
watch->dev = AUDIT_DEV_UNSET;
|
|
|
|
watch->ino = AUDIT_INO_UNSET;
|
2009-06-12 02:31:36 +08:00
|
|
|
|
|
|
|
return watch;
|
|
|
|
}
|
|
|
|
|
2016-02-06 15:39:47 +08:00
|
|
|
/* Translate a watch string to kernel representation. */
|
2009-06-12 02:31:36 +08:00
|
|
|
int audit_to_watch(struct audit_krule *krule, char *path, int len, u32 op)
|
|
|
|
{
|
|
|
|
struct audit_watch *watch;
|
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
if (!audit_watch_group)
|
2009-06-12 02:31:36 +08:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
if (path[0] != '/' || path[len-1] == '/' ||
|
|
|
|
krule->listnr != AUDIT_FILTER_EXIT ||
|
|
|
|
op != Audit_equal ||
|
|
|
|
krule->inode_f || krule->watch || krule->tree)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
watch = audit_init_watch(path);
|
|
|
|
if (IS_ERR(watch))
|
|
|
|
return PTR_ERR(watch);
|
|
|
|
|
|
|
|
krule->watch = watch;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Duplicate the given audit watch. The new watch's rules list is initialized
|
|
|
|
* to an empty list and wlist is undefined. */
|
|
|
|
static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
struct audit_watch *new;
|
|
|
|
|
|
|
|
path = kstrdup(old->path, GFP_KERNEL);
|
|
|
|
if (unlikely(!path))
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
new = audit_init_watch(path);
|
|
|
|
if (IS_ERR(new)) {
|
|
|
|
kfree(path);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
new->dev = old->dev;
|
|
|
|
new->ino = old->ino;
|
2009-12-18 09:12:04 +08:00
|
|
|
audit_get_parent(old->parent);
|
2009-06-12 02:31:36 +08:00
|
|
|
new->parent = old->parent;
|
|
|
|
|
|
|
|
out:
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audit_watch_log_rule_change(struct audit_krule *r, struct audit_watch *w, char *op)
|
|
|
|
{
|
2018-06-15 04:20:06 +08:00
|
|
|
struct audit_buffer *ab;
|
|
|
|
|
|
|
|
if (!audit_enabled)
|
|
|
|
return;
|
2019-01-19 06:42:48 +08:00
|
|
|
ab = audit_log_start(audit_context(), GFP_NOFS, AUDIT_CONFIG_CHANGE);
|
2018-06-15 04:20:06 +08:00
|
|
|
if (!ab)
|
|
|
|
return;
|
2018-11-17 05:30:10 +08:00
|
|
|
audit_log_session_info(ab);
|
2018-08-03 05:56:50 +08:00
|
|
|
audit_log_format(ab, "op=%s path=", op);
|
2018-06-15 04:20:06 +08:00
|
|
|
audit_log_untrustedstring(ab, w->path);
|
|
|
|
audit_log_key(ab, r->filterkey);
|
|
|
|
audit_log_format(ab, " list=%d res=1", r->listnr);
|
|
|
|
audit_log_end(ab);
|
2009-06-12 02:31:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update inode info in audit rules based on filesystem event. */
|
|
|
|
static void audit_update_watch(struct audit_parent *parent,
|
2019-04-27 02:09:49 +08:00
|
|
|
const struct qstr *dname, dev_t dev,
|
2009-06-12 02:31:36 +08:00
|
|
|
unsigned long ino, unsigned invalidating)
|
|
|
|
{
|
|
|
|
struct audit_watch *owatch, *nwatch, *nextw;
|
|
|
|
struct audit_krule *r, *nextr;
|
|
|
|
struct audit_entry *oentry, *nentry;
|
|
|
|
|
|
|
|
mutex_lock(&audit_filter_mutex);
|
2009-12-18 09:12:04 +08:00
|
|
|
/* Run all of the watches on this parent looking for the one that
|
|
|
|
* matches the given dname */
|
2009-06-12 02:31:36 +08:00
|
|
|
list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
|
2019-04-27 02:11:05 +08:00
|
|
|
if (audit_compare_dname_path(dname, owatch->path,
|
2012-10-11 03:25:25 +08:00
|
|
|
AUDIT_NAME_FULL))
|
2009-06-12 02:31:36 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* If the update involves invalidating rules, do the inode-based
|
|
|
|
* filtering now, so we don't omit records. */
|
2009-12-18 09:12:04 +08:00
|
|
|
if (invalidating && !audit_dummy_context())
|
2018-05-13 09:58:20 +08:00
|
|
|
audit_filter_inodes(current, audit_context());
|
2009-06-12 02:31:36 +08:00
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
/* updating ino will likely change which audit_hash_list we
|
|
|
|
* are on so we need a new watch for the new list */
|
2009-06-12 02:31:36 +08:00
|
|
|
nwatch = audit_dupe_watch(owatch);
|
|
|
|
if (IS_ERR(nwatch)) {
|
|
|
|
mutex_unlock(&audit_filter_mutex);
|
|
|
|
audit_panic("error updating watch, skipping");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nwatch->dev = dev;
|
|
|
|
nwatch->ino = ino;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
|
|
|
|
|
|
|
|
oentry = container_of(r, struct audit_entry, rule);
|
|
|
|
list_del(&oentry->rule.rlist);
|
|
|
|
list_del_rcu(&oentry->list);
|
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
nentry = audit_dupe_rule(&oentry->rule);
|
2009-06-12 02:31:36 +08:00
|
|
|
if (IS_ERR(nentry)) {
|
|
|
|
list_del(&oentry->rule.list);
|
|
|
|
audit_panic("error updating watch, removing");
|
|
|
|
} else {
|
|
|
|
int h = audit_hash_ino((u32)ino);
|
2009-12-18 09:12:04 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* nentry->rule.watch == oentry->rule.watch so
|
|
|
|
* we must drop that reference and set it to our
|
|
|
|
* new watch.
|
|
|
|
*/
|
|
|
|
audit_put_watch(nentry->rule.watch);
|
|
|
|
audit_get_watch(nwatch);
|
|
|
|
nentry->rule.watch = nwatch;
|
2009-06-12 02:31:36 +08:00
|
|
|
list_add(&nentry->rule.rlist, &nwatch->rules);
|
|
|
|
list_add_rcu(&nentry->list, &audit_inode_hash[h]);
|
|
|
|
list_replace(&oentry->rule.list,
|
|
|
|
&nentry->rule.list);
|
|
|
|
}
|
2015-08-06 04:29:37 +08:00
|
|
|
if (oentry->rule.exe)
|
|
|
|
audit_remove_mark(oentry->rule.exe);
|
2009-06-12 02:31:36 +08:00
|
|
|
|
|
|
|
call_rcu(&oentry->rcu, audit_free_rule_rcu);
|
|
|
|
}
|
|
|
|
|
|
|
|
audit_remove_watch(owatch);
|
|
|
|
goto add_watch_to_parent; /* event applies to a single watch */
|
|
|
|
}
|
|
|
|
mutex_unlock(&audit_filter_mutex);
|
|
|
|
return;
|
|
|
|
|
|
|
|
add_watch_to_parent:
|
|
|
|
list_add(&nwatch->wlist, &parent->watches);
|
|
|
|
mutex_unlock(&audit_filter_mutex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove all watches & rules associated with a parent that is going away. */
|
|
|
|
static void audit_remove_parent_watches(struct audit_parent *parent)
|
|
|
|
{
|
|
|
|
struct audit_watch *w, *nextw;
|
|
|
|
struct audit_krule *r, *nextr;
|
|
|
|
struct audit_entry *e;
|
|
|
|
|
|
|
|
mutex_lock(&audit_filter_mutex);
|
|
|
|
list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
|
|
|
|
list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
|
|
|
|
e = container_of(r, struct audit_entry, rule);
|
2014-04-04 13:00:38 +08:00
|
|
|
audit_watch_log_rule_change(r, w, "remove_rule");
|
2015-08-06 04:29:37 +08:00
|
|
|
if (e->rule.exe)
|
|
|
|
audit_remove_mark(e->rule.exe);
|
2009-06-12 02:31:36 +08:00
|
|
|
list_del(&r->rlist);
|
|
|
|
list_del(&r->list);
|
|
|
|
list_del_rcu(&e->list);
|
|
|
|
call_rcu(&e->rcu, audit_free_rule_rcu);
|
|
|
|
}
|
|
|
|
audit_remove_watch(w);
|
|
|
|
}
|
|
|
|
mutex_unlock(&audit_filter_mutex);
|
2009-12-18 09:12:04 +08:00
|
|
|
|
2011-06-14 23:29:51 +08:00
|
|
|
fsnotify_destroy_mark(&parent->mark, audit_watch_group);
|
2009-06-12 02:31:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get path information necessary for adding watches. */
|
2011-02-17 04:08:54 +08:00
|
|
|
static int audit_get_nd(struct audit_watch *watch, struct path *parent)
|
2009-06-12 02:31:36 +08:00
|
|
|
{
|
2012-06-15 07:01:42 +08:00
|
|
|
struct dentry *d = kern_path_locked(watch->path, parent);
|
|
|
|
if (IS_ERR(d))
|
2011-02-17 04:08:54 +08:00
|
|
|
return PTR_ERR(d);
|
2015-03-18 06:26:21 +08:00
|
|
|
if (d_is_positive(d)) {
|
2011-02-17 04:08:54 +08:00
|
|
|
/* update watch filter fields */
|
2016-04-10 13:33:30 +08:00
|
|
|
watch->dev = d->d_sb->s_dev;
|
2015-03-18 06:26:21 +08:00
|
|
|
watch->ino = d_backing_inode(d)->i_ino;
|
2009-06-12 02:31:36 +08:00
|
|
|
}
|
2019-11-03 01:11:41 +08:00
|
|
|
inode_unlock(d_backing_inode(parent->dentry));
|
2011-02-17 04:08:54 +08:00
|
|
|
dput(d);
|
2009-06-12 02:31:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
/* Associate the given rule with an existing parent.
|
2009-06-12 02:31:36 +08:00
|
|
|
* Caller must hold audit_filter_mutex. */
|
|
|
|
static void audit_add_to_parent(struct audit_krule *krule,
|
|
|
|
struct audit_parent *parent)
|
|
|
|
{
|
|
|
|
struct audit_watch *w, *watch = krule->watch;
|
|
|
|
int watch_found = 0;
|
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
BUG_ON(!mutex_is_locked(&audit_filter_mutex));
|
|
|
|
|
2009-06-12 02:31:36 +08:00
|
|
|
list_for_each_entry(w, &parent->watches, wlist) {
|
|
|
|
if (strcmp(watch->path, w->path))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
watch_found = 1;
|
|
|
|
|
2015-08-02 03:41:12 +08:00
|
|
|
/* put krule's ref to temporary watch */
|
2009-06-12 02:31:36 +08:00
|
|
|
audit_put_watch(watch);
|
|
|
|
|
|
|
|
audit_get_watch(w);
|
|
|
|
krule->watch = watch = w;
|
audit: eliminate unnecessary extra layer of watch parent references
The audit watch parent count was imbalanced, adding an unnecessary layer of
watch parent references. Decrement the additional parent reference when a
watch is reused, already having a reference to the parent.
audit_find_parent() gets a reference to the parent, if the parent is
already known. This additional parental reference is not needed if the
watch is subsequently found by audit_add_to_parent(), and consumed if
the watch does not already exist, so we need to put the parent if the
watch is found, and do nothing if this new watch is added to the parent.
If the parent wasn't already known, it is created with a refcount of 1
and added to the audit_watch_group, then incremented by one to be
subsequently consumed by the newly created watch in
audit_add_to_parent().
The rule points to the watch, not to the parent, so the rule's refcount
gets bumped, not the parent's.
See LKML, 2015-07-16
Signed-off-by: Richard Guy Briggs <rgb@redhat.com>
Signed-off-by: Paul Moore <pmoore@redhat.com>
2015-08-02 03:41:13 +08:00
|
|
|
|
|
|
|
audit_put_parent(parent);
|
2009-06-12 02:31:36 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!watch_found) {
|
|
|
|
watch->parent = parent;
|
|
|
|
|
2015-08-02 03:41:12 +08:00
|
|
|
audit_get_watch(watch);
|
2009-06-12 02:31:36 +08:00
|
|
|
list_add(&watch->wlist, &parent->watches);
|
|
|
|
}
|
|
|
|
list_add(&krule->rlist, &watch->rules);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find a matching watch entry, or add this one.
|
|
|
|
* Caller must hold audit_filter_mutex. */
|
2009-12-18 09:12:04 +08:00
|
|
|
int audit_add_watch(struct audit_krule *krule, struct list_head **list)
|
2009-06-12 02:31:36 +08:00
|
|
|
{
|
|
|
|
struct audit_watch *watch = krule->watch;
|
|
|
|
struct audit_parent *parent;
|
2011-02-17 04:08:54 +08:00
|
|
|
struct path parent_path;
|
2009-12-18 09:12:04 +08:00
|
|
|
int h, ret = 0;
|
2009-06-12 02:31:36 +08:00
|
|
|
|
2018-07-11 20:39:37 +08:00
|
|
|
/*
|
|
|
|
* When we will be calling audit_add_to_parent, krule->watch might have
|
|
|
|
* been updated and watch might have been freed.
|
|
|
|
* So we need to keep a reference of watch.
|
|
|
|
*/
|
|
|
|
audit_get_watch(watch);
|
|
|
|
|
2009-06-12 02:31:36 +08:00
|
|
|
mutex_unlock(&audit_filter_mutex);
|
|
|
|
|
|
|
|
/* Avoid calling path_lookup under audit_filter_mutex. */
|
2011-02-17 04:08:54 +08:00
|
|
|
ret = audit_get_nd(watch, &parent_path);
|
2009-06-12 02:31:36 +08:00
|
|
|
|
2011-02-17 04:08:54 +08:00
|
|
|
/* caller expects mutex locked */
|
2009-12-18 09:12:04 +08:00
|
|
|
mutex_lock(&audit_filter_mutex);
|
|
|
|
|
2018-07-11 20:39:37 +08:00
|
|
|
if (ret) {
|
|
|
|
audit_put_watch(watch);
|
2011-02-17 04:08:54 +08:00
|
|
|
return ret;
|
2018-07-11 20:39:37 +08:00
|
|
|
}
|
2009-06-12 02:31:36 +08:00
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
/* either find an old parent or attach a new one */
|
2015-03-18 06:26:21 +08:00
|
|
|
parent = audit_find_parent(d_backing_inode(parent_path.dentry));
|
2009-12-18 09:12:04 +08:00
|
|
|
if (!parent) {
|
2011-02-17 04:08:54 +08:00
|
|
|
parent = audit_init_parent(&parent_path);
|
2009-06-12 02:31:36 +08:00
|
|
|
if (IS_ERR(parent)) {
|
2009-06-12 02:31:36 +08:00
|
|
|
ret = PTR_ERR(parent);
|
|
|
|
goto error;
|
2009-06-12 02:31:36 +08:00
|
|
|
}
|
2009-12-18 09:12:04 +08:00
|
|
|
}
|
2009-06-12 02:31:36 +08:00
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
audit_add_to_parent(krule, parent);
|
2009-06-12 02:31:36 +08:00
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
h = audit_hash_ino((u32)watch->ino);
|
|
|
|
*list = &audit_inode_hash[h];
|
2009-06-12 02:31:36 +08:00
|
|
|
error:
|
2011-02-17 04:08:54 +08:00
|
|
|
path_put(&parent_path);
|
2018-07-11 20:39:37 +08:00
|
|
|
audit_put_watch(watch);
|
2009-06-12 02:31:36 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-12-18 09:12:05 +08:00
|
|
|
void audit_remove_watch_rule(struct audit_krule *krule)
|
2009-06-12 02:31:36 +08:00
|
|
|
{
|
|
|
|
struct audit_watch *watch = krule->watch;
|
|
|
|
struct audit_parent *parent = watch->parent;
|
|
|
|
|
|
|
|
list_del(&krule->rlist);
|
|
|
|
|
|
|
|
if (list_empty(&watch->rules)) {
|
2017-08-15 19:00:36 +08:00
|
|
|
/*
|
|
|
|
* audit_remove_watch() drops our reference to 'parent' which
|
|
|
|
* can get freed. Grab our own reference to be safe.
|
|
|
|
*/
|
|
|
|
audit_get_parent(parent);
|
2009-06-12 02:31:36 +08:00
|
|
|
audit_remove_watch(watch);
|
2017-08-15 19:00:36 +08:00
|
|
|
if (list_empty(&parent->watches))
|
2011-06-14 23:29:51 +08:00
|
|
|
fsnotify_destroy_mark(&parent->mark, audit_watch_group);
|
2017-08-15 19:00:36 +08:00
|
|
|
audit_put_parent(parent);
|
2009-06-12 02:31:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
/* Update watch data in audit rules based on fsnotify events. */
|
2010-07-28 22:18:38 +08:00
|
|
|
static int audit_watch_handle_event(struct fsnotify_group *group,
|
2014-01-22 07:48:14 +08:00
|
|
|
struct inode *to_tell,
|
2016-11-21 09:19:09 +08:00
|
|
|
u32 mask, const void *data, int data_type,
|
2019-04-27 01:51:03 +08:00
|
|
|
const struct qstr *dname, u32 cookie,
|
2016-11-11 00:51:50 +08:00
|
|
|
struct fsnotify_iter_info *iter_info)
|
2009-12-18 09:12:04 +08:00
|
|
|
{
|
2018-04-21 07:10:50 +08:00
|
|
|
struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info);
|
2016-11-21 09:19:09 +08:00
|
|
|
const struct inode *inode;
|
2009-06-12 02:31:36 +08:00
|
|
|
struct audit_parent *parent;
|
|
|
|
|
2010-07-28 22:18:39 +08:00
|
|
|
parent = container_of(inode_mark, struct audit_parent, mark);
|
2009-12-18 09:12:04 +08:00
|
|
|
|
2010-07-28 22:18:38 +08:00
|
|
|
BUG_ON(group != audit_watch_group);
|
2009-12-18 09:12:04 +08:00
|
|
|
|
2014-01-22 07:48:14 +08:00
|
|
|
switch (data_type) {
|
2010-08-13 05:23:04 +08:00
|
|
|
case (FSNOTIFY_EVENT_PATH):
|
2016-11-21 09:19:09 +08:00
|
|
|
inode = d_backing_inode(((const struct path *)data)->dentry);
|
2009-12-18 09:12:04 +08:00
|
|
|
break;
|
|
|
|
case (FSNOTIFY_EVENT_INODE):
|
2016-11-21 09:19:09 +08:00
|
|
|
inode = (const struct inode *)data;
|
2009-12-18 09:12:04 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
inode = NULL;
|
|
|
|
break;
|
2017-05-02 22:16:04 +08:00
|
|
|
}
|
2009-06-12 02:31:36 +08:00
|
|
|
|
2009-12-18 09:12:04 +08:00
|
|
|
if (mask & (FS_CREATE|FS_MOVED_TO) && inode)
|
2019-04-27 02:09:49 +08:00
|
|
|
audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0);
|
2009-12-18 09:12:04 +08:00
|
|
|
else if (mask & (FS_DELETE|FS_MOVED_FROM))
|
2019-04-27 02:09:49 +08:00
|
|
|
audit_update_watch(parent, dname, AUDIT_DEV_UNSET, AUDIT_INO_UNSET, 1);
|
2009-12-18 09:12:04 +08:00
|
|
|
else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF))
|
2009-06-12 02:31:36 +08:00
|
|
|
audit_remove_parent_watches(parent);
|
2009-12-18 09:12:04 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct fsnotify_ops audit_watch_fsnotify_ops = {
|
|
|
|
.handle_event = audit_watch_handle_event,
|
2016-12-22 01:06:12 +08:00
|
|
|
.free_mark = audit_watch_free_mark,
|
2009-06-12 02:31:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __init audit_watch_init(void)
|
|
|
|
{
|
2009-12-18 10:24:22 +08:00
|
|
|
audit_watch_group = fsnotify_alloc_group(&audit_watch_fsnotify_ops);
|
2009-12-18 09:12:04 +08:00
|
|
|
if (IS_ERR(audit_watch_group)) {
|
|
|
|
audit_watch_group = NULL;
|
|
|
|
audit_panic("cannot create audit fsnotify group");
|
|
|
|
}
|
2009-06-12 02:31:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-12-18 09:12:06 +08:00
|
|
|
device_initcall(audit_watch_init);
|
2015-08-06 04:29:37 +08:00
|
|
|
|
|
|
|
int audit_dupe_exe(struct audit_krule *new, struct audit_krule *old)
|
|
|
|
{
|
|
|
|
struct audit_fsnotify_mark *audit_mark;
|
|
|
|
char *pathname;
|
|
|
|
|
|
|
|
pathname = kstrdup(audit_mark_path(old->exe), GFP_KERNEL);
|
|
|
|
if (!pathname)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
audit_mark = audit_alloc_mark(new, pathname, strlen(pathname));
|
|
|
|
if (IS_ERR(audit_mark)) {
|
|
|
|
kfree(pathname);
|
|
|
|
return PTR_ERR(audit_mark);
|
|
|
|
}
|
|
|
|
new->exe = audit_mark;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int audit_exe_compare(struct task_struct *tsk, struct audit_fsnotify_mark *mark)
|
|
|
|
{
|
2015-08-08 22:20:25 +08:00
|
|
|
struct file *exe_file;
|
|
|
|
unsigned long ino;
|
|
|
|
dev_t dev;
|
|
|
|
|
2016-08-23 22:20:39 +08:00
|
|
|
exe_file = get_task_exe_file(tsk);
|
|
|
|
if (!exe_file)
|
|
|
|
return 0;
|
2016-12-05 07:24:56 +08:00
|
|
|
ino = file_inode(exe_file)->i_ino;
|
|
|
|
dev = file_inode(exe_file)->i_sb->s_dev;
|
2016-08-23 22:20:39 +08:00
|
|
|
fput(exe_file);
|
2015-08-06 04:29:37 +08:00
|
|
|
return audit_mark_compare(mark, ino, dev);
|
|
|
|
}
|