Blame view

kernel/audit_watch.c 13.7 KB
1a59d1b8e   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
cfcad62c7   Eric Paris   audit: seperate a...
2
3
4
5
6
  /* audit_watch.c -- watching inodes
   *
   * Copyright 2003-2009 Red Hat, Inc.
   * Copyright 2005 Hewlett-Packard Development Company, L.P.
   * Copyright 2005 IBM Corporation
cfcad62c7   Eric Paris   audit: seperate a...
7
   */
5efc24434   Mateusz Guzik   audit: fix exe_fi...
8
  #include <linux/file.h>
cfcad62c7   Eric Paris   audit: seperate a...
9
10
11
12
13
  #include <linux/kernel.h>
  #include <linux/audit.h>
  #include <linux/kthread.h>
  #include <linux/mutex.h>
  #include <linux/fs.h>
e9fd702a5   Eric Paris   audit: convert au...
14
  #include <linux/fsnotify_backend.h>
cfcad62c7   Eric Paris   audit: seperate a...
15
16
  #include <linux/namei.h>
  #include <linux/netlink.h>
bd120ded6   Elena Reshetova   audit: convert au...
17
  #include <linux/refcount.h>
cfcad62c7   Eric Paris   audit: seperate a...
18
  #include <linux/sched.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
19
  #include <linux/slab.h>
cfcad62c7   Eric Paris   audit: seperate a...
20
21
22
23
24
25
  #include <linux/security.h>
  #include "audit.h"
  
  /*
   * Reference counting:
   *
e9fd702a5   Eric Paris   audit: convert au...
26
   * audit_parent: lifetime is from audit_init_parent() to receipt of an FS_IGNORED
cfcad62c7   Eric Paris   audit: seperate a...
27
28
29
30
31
32
33
34
35
   * 	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 {
bd120ded6   Elena Reshetova   audit: convert au...
36
  	refcount_t		count;	/* reference count */
cfcad62c7   Eric Paris   audit: seperate a...
37
  	dev_t			dev;	/* associated superblock device */
e08b061ec   Eric Paris   Audit: reorganize...
38
  	char			*path;	/* insertion path */
cfcad62c7   Eric Paris   audit: seperate a...
39
40
41
  	unsigned long		ino;	/* associated inode number */
  	struct audit_parent	*parent; /* associated parent */
  	struct list_head	wlist;	/* entry in parent->watches list */
ae7b8f410   Eric Paris   Audit: clean up t...
42
  	struct list_head	rules;	/* anchor for krule->rlist */
cfcad62c7   Eric Paris   audit: seperate a...
43
44
45
  };
  
  struct audit_parent {
ae7b8f410   Eric Paris   Audit: clean up t...
46
  	struct list_head	watches; /* anchor for audit_watch->wlist */
e61ce8673   Eric Paris   fsnotify: rename ...
47
  	struct fsnotify_mark mark; /* fsnotify mark on the inode */
cfcad62c7   Eric Paris   audit: seperate a...
48
  };
e9fd702a5   Eric Paris   audit: convert au...
49
  /* fsnotify handle. */
b8800aa5d   Stephen Hemminger   audit: make funct...
50
  static struct fsnotify_group *audit_watch_group;
cfcad62c7   Eric Paris   audit: seperate a...
51

e9fd702a5   Eric Paris   audit: convert au...
52
53
  /* fsnotify events we care about. */
  #define AUDIT_FS_WATCH (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
7dbe60801   Amir Goldstein   audit: do not set...
54
  			FS_MOVE_SELF | FS_UNMOUNT)
cfcad62c7   Eric Paris   audit: seperate a...
55

ae7b8f410   Eric Paris   Audit: clean up t...
56
57
58
59
60
  static void audit_free_parent(struct audit_parent *parent)
  {
  	WARN_ON(!list_empty(&parent->watches));
  	kfree(parent);
  }
e61ce8673   Eric Paris   fsnotify: rename ...
61
  static void audit_watch_free_mark(struct fsnotify_mark *entry)
cfcad62c7   Eric Paris   audit: seperate a...
62
63
  {
  	struct audit_parent *parent;
e9fd702a5   Eric Paris   audit: convert au...
64
  	parent = container_of(entry, struct audit_parent, mark);
ae7b8f410   Eric Paris   Audit: clean up t...
65
  	audit_free_parent(parent);
cfcad62c7   Eric Paris   audit: seperate a...
66
  }
e9fd702a5   Eric Paris   audit: convert au...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  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;
e61ce8673   Eric Paris   fsnotify: rename ...
86
  	struct fsnotify_mark *entry;
e9fd702a5   Eric Paris   audit: convert au...
87

b1362edfe   Jan Kara   fsnotify: Remove ...
88
  	entry = fsnotify_find_mark(&inode->i_fsnotify_marks, audit_watch_group);
e9fd702a5   Eric Paris   audit: convert au...
89
90
91
92
93
  	if (entry)
  		parent = container_of(entry, struct audit_parent, mark);
  
  	return parent;
  }
cfcad62c7   Eric Paris   audit: seperate a...
94
95
  void audit_get_watch(struct audit_watch *watch)
  {
bd120ded6   Elena Reshetova   audit: convert au...
96
  	refcount_inc(&watch->count);
cfcad62c7   Eric Paris   audit: seperate a...
97
98
99
100
  }
  
  void audit_put_watch(struct audit_watch *watch)
  {
bd120ded6   Elena Reshetova   audit: convert au...
101
  	if (refcount_dec_and_test(&watch->count)) {
cfcad62c7   Eric Paris   audit: seperate a...
102
103
104
105
106
107
  		WARN_ON(watch->parent);
  		WARN_ON(!list_empty(&watch->rules));
  		kfree(watch->path);
  		kfree(watch);
  	}
  }
b8800aa5d   Stephen Hemminger   audit: make funct...
108
  static void audit_remove_watch(struct audit_watch *watch)
cfcad62c7   Eric Paris   audit: seperate a...
109
110
  {
  	list_del(&watch->wlist);
e9fd702a5   Eric Paris   audit: convert au...
111
  	audit_put_parent(watch->parent);
cfcad62c7   Eric Paris   audit: seperate a...
112
113
114
115
116
117
118
119
  	watch->parent = NULL;
  	audit_put_watch(watch); /* match initial get */
  }
  
  char *audit_watch_path(struct audit_watch *watch)
  {
  	return watch->path;
  }
ae7b8f410   Eric Paris   Audit: clean up t...
120
  int audit_watch_compare(struct audit_watch *watch, unsigned long ino, dev_t dev)
cfcad62c7   Eric Paris   audit: seperate a...
121
  {
84cb777e6   Richard Guy Briggs   audit: use macros...
122
  	return (watch->ino != AUDIT_INO_UNSET) &&
ae7b8f410   Eric Paris   Audit: clean up t...
123
124
  		(watch->ino == ino) &&
  		(watch->dev == dev);
cfcad62c7   Eric Paris   audit: seperate a...
125
126
127
  }
  
  /* Initialize a parent watch entry. */
15a9155fe   Al Viro   fix race in audit...
128
  static struct audit_parent *audit_init_parent(struct path *path)
cfcad62c7   Eric Paris   audit: seperate a...
129
  {
3b362157b   David Howells   VFS: audit: d_bac...
130
  	struct inode *inode = d_backing_inode(path->dentry);
cfcad62c7   Eric Paris   audit: seperate a...
131
  	struct audit_parent *parent;
e9fd702a5   Eric Paris   audit: convert au...
132
  	int ret;
cfcad62c7   Eric Paris   audit: seperate a...
133
134
135
136
137
138
  
  	parent = kzalloc(sizeof(*parent), GFP_KERNEL);
  	if (unlikely(!parent))
  		return ERR_PTR(-ENOMEM);
  
  	INIT_LIST_HEAD(&parent->watches);
cfcad62c7   Eric Paris   audit: seperate a...
139

054c636e5   Jan Kara   fsnotify: Move ->...
140
  	fsnotify_init_mark(&parent->mark, audit_watch_group);
e9fd702a5   Eric Paris   audit: convert au...
141
  	parent->mark.mask = AUDIT_FS_WATCH;
b249f5be6   Amir Goldstein   fsnotify: add fsn...
142
  	ret = fsnotify_add_inode_mark(&parent->mark, inode, 0);
e9fd702a5   Eric Paris   audit: convert au...
143
  	if (ret < 0) {
ae7b8f410   Eric Paris   Audit: clean up t...
144
  		audit_free_parent(parent);
e9fd702a5   Eric Paris   audit: convert au...
145
  		return ERR_PTR(ret);
cfcad62c7   Eric Paris   audit: seperate a...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  	}
  
  	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);
bd120ded6   Elena Reshetova   audit: convert au...
161
  	refcount_set(&watch->count, 1);
cfcad62c7   Eric Paris   audit: seperate a...
162
  	watch->path = path;
84cb777e6   Richard Guy Briggs   audit: use macros...
163
164
  	watch->dev = AUDIT_DEV_UNSET;
  	watch->ino = AUDIT_INO_UNSET;
cfcad62c7   Eric Paris   audit: seperate a...
165
166
167
  
  	return watch;
  }
fd97646b0   Wei Yuan   audit: Fix typo i...
168
  /* Translate a watch string to kernel representation. */
cfcad62c7   Eric Paris   audit: seperate a...
169
170
171
  int audit_to_watch(struct audit_krule *krule, char *path, int len, u32 op)
  {
  	struct audit_watch *watch;
e9fd702a5   Eric Paris   audit: convert au...
172
  	if (!audit_watch_group)
cfcad62c7   Eric Paris   audit: seperate a...
173
174
175
176
177
178
179
180
181
182
183
  		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);
cfcad62c7   Eric Paris   audit: seperate a...
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  	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;
e9fd702a5   Eric Paris   audit: convert au...
208
  	audit_get_parent(old->parent);
cfcad62c7   Eric Paris   audit: seperate a...
209
210
211
212
213
214
215
216
  	new->parent = old->parent;
  
  out:
  	return new;
  }
  
  static void audit_watch_log_rule_change(struct audit_krule *r, struct audit_watch *w, char *op)
  {
4fa7f0869   Richard Guy Briggs   audit: simplify a...
217
218
219
220
  	struct audit_buffer *ab;
  
  	if (!audit_enabled)
  		return;
626abcd13   Richard Guy Briggs   audit: add syscal...
221
  	ab = audit_log_start(audit_context(), GFP_NOFS, AUDIT_CONFIG_CHANGE);
4fa7f0869   Richard Guy Briggs   audit: simplify a...
222
223
  	if (!ab)
  		return;
a2c97da11   Richard Guy Briggs   audit: use sessio...
224
  	audit_log_session_info(ab);
d0a3f18a7   Paul Moore   audit: minimize o...
225
  	audit_log_format(ab, "op=%s path=", op);
4fa7f0869   Richard Guy Briggs   audit: simplify a...
226
227
228
229
  	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);
cfcad62c7   Eric Paris   audit: seperate a...
230
231
232
233
  }
  
  /* Update inode info in audit rules based on filesystem event. */
  static void audit_update_watch(struct audit_parent *parent,
6921d4ebe   Al Viro   audit_update_watc...
234
  			       const struct qstr *dname, dev_t dev,
cfcad62c7   Eric Paris   audit: seperate a...
235
236
237
238
239
240
241
  			       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);
ae7b8f410   Eric Paris   Audit: clean up t...
242
243
  	/* Run all of the watches on this parent looking for the one that
  	 * matches the given dname */
cfcad62c7   Eric Paris   audit: seperate a...
244
  	list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
795d673af   Al Viro   audit_compare_dna...
245
  		if (audit_compare_dname_path(dname, owatch->path,
e3d6b07b8   Jeff Layton   audit: optimize a...
246
  					     AUDIT_NAME_FULL))
cfcad62c7   Eric Paris   audit: seperate a...
247
248
249
250
  			continue;
  
  		/* If the update involves invalidating rules, do the inode-based
  		 * filtering now, so we don't omit records. */
ae7b8f410   Eric Paris   Audit: clean up t...
251
  		if (invalidating && !audit_dummy_context())
cdfb6b341   Richard Guy Briggs   audit: use inline...
252
  			audit_filter_inodes(current, audit_context());
cfcad62c7   Eric Paris   audit: seperate a...
253

ae7b8f410   Eric Paris   Audit: clean up t...
254
255
  		/* updating ino will likely change which audit_hash_list we
  		 * are on so we need a new watch for the new list */
cfcad62c7   Eric Paris   audit: seperate a...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
  		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);
ae7b8f410   Eric Paris   Audit: clean up t...
270
  			nentry = audit_dupe_rule(&oentry->rule);
cfcad62c7   Eric Paris   audit: seperate a...
271
272
273
274
275
  			if (IS_ERR(nentry)) {
  				list_del(&oentry->rule.list);
  				audit_panic("error updating watch, removing");
  			} else {
  				int h = audit_hash_ino((u32)ino);
ae7b8f410   Eric Paris   Audit: clean up t...
276
277
278
279
280
281
282
283
284
  
  				/*
  				 * 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;
cfcad62c7   Eric Paris   audit: seperate a...
285
286
287
288
289
  				list_add(&nentry->rule.rlist, &nwatch->rules);
  				list_add_rcu(&nentry->list, &audit_inode_hash[h]);
  				list_replace(&oentry->rule.list,
  					     &nentry->rule.list);
  			}
34d99af52   Richard Guy Briggs   audit: implement ...
290
291
  			if (oentry->rule.exe)
  				audit_remove_mark(oentry->rule.exe);
cfcad62c7   Eric Paris   audit: seperate a...
292

cfcad62c7   Eric Paris   audit: seperate a...
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  			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);
cfcad62c7   Eric Paris   audit: seperate a...
316
317
318
  	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);
e7df61f4d   Burn Alting   audit: invalid op...
319
  			audit_watch_log_rule_change(r, w, "remove_rule");
34d99af52   Richard Guy Briggs   audit: implement ...
320
321
  			if (e->rule.exe)
  				audit_remove_mark(e->rule.exe);
cfcad62c7   Eric Paris   audit: seperate a...
322
323
324
325
326
327
328
329
  			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);
e9fd702a5   Eric Paris   audit: convert au...
330

e2a29943e   Lino Sanfilippo   fsnotify: pass gr...
331
  	fsnotify_destroy_mark(&parent->mark, audit_watch_group);
cfcad62c7   Eric Paris   audit: seperate a...
332
  }
cfcad62c7   Eric Paris   audit: seperate a...
333
  /* Get path information necessary for adding watches. */
15a9155fe   Al Viro   fix race in audit...
334
  static int audit_get_nd(struct audit_watch *watch, struct path *parent)
cfcad62c7   Eric Paris   audit: seperate a...
335
  {
79714f72d   Al Viro   get rid of kern_p...
336
337
  	struct dentry *d = kern_path_locked(watch->path, parent);
  	if (IS_ERR(d))
15a9155fe   Al Viro   fix race in audit...
338
  		return PTR_ERR(d);
3b362157b   David Howells   VFS: audit: d_bac...
339
  	if (d_is_positive(d)) {
15a9155fe   Al Viro   fix race in audit...
340
  		/* update watch filter fields */
fc64005c9   Al Viro   don't bother with...
341
  		watch->dev = d->d_sb->s_dev;
3b362157b   David Howells   VFS: audit: d_bac...
342
  		watch->ino = d_backing_inode(d)->i_ino;
cfcad62c7   Eric Paris   audit: seperate a...
343
  	}
69924b896   Al Viro   audit_get_nd(): d...
344
  	inode_unlock(d_backing_inode(parent->dentry));
15a9155fe   Al Viro   fix race in audit...
345
  	dput(d);
cfcad62c7   Eric Paris   audit: seperate a...
346
347
  	return 0;
  }
e9fd702a5   Eric Paris   audit: convert au...
348
  /* Associate the given rule with an existing parent.
cfcad62c7   Eric Paris   audit: seperate a...
349
350
351
352
353
354
   * 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;
e9fd702a5   Eric Paris   audit: convert au...
355
  	BUG_ON(!mutex_is_locked(&audit_filter_mutex));
cfcad62c7   Eric Paris   audit: seperate a...
356
357
358
359
360
  	list_for_each_entry(w, &parent->watches, wlist) {
  		if (strcmp(watch->path, w->path))
  			continue;
  
  		watch_found = 1;
f8259b262   Richard Guy Briggs   audit: eliminate ...
361
  		/* put krule's ref to temporary watch */
cfcad62c7   Eric Paris   audit: seperate a...
362
363
364
365
  		audit_put_watch(watch);
  
  		audit_get_watch(w);
  		krule->watch = watch = w;
aa7c043d9   Richard Guy Briggs   audit: eliminate ...
366
367
  
  		audit_put_parent(parent);
cfcad62c7   Eric Paris   audit: seperate a...
368
369
370
371
  		break;
  	}
  
  	if (!watch_found) {
cfcad62c7   Eric Paris   audit: seperate a...
372
  		watch->parent = parent;
f8259b262   Richard Guy Briggs   audit: eliminate ...
373
  		audit_get_watch(watch);
cfcad62c7   Eric Paris   audit: seperate a...
374
375
376
377
378
379
380
  		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. */
ae7b8f410   Eric Paris   Audit: clean up t...
381
  int audit_add_watch(struct audit_krule *krule, struct list_head **list)
cfcad62c7   Eric Paris   audit: seperate a...
382
383
  {
  	struct audit_watch *watch = krule->watch;
cfcad62c7   Eric Paris   audit: seperate a...
384
  	struct audit_parent *parent;
15a9155fe   Al Viro   fix race in audit...
385
  	struct path parent_path;
ae7b8f410   Eric Paris   Audit: clean up t...
386
  	int h, ret = 0;
cfcad62c7   Eric Paris   audit: seperate a...
387

baa2a4fdd   Ronny Chevalier   audit: fix use-af...
388
389
390
391
392
393
  	/*
  	 * 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);
35fe4d0b1   Eric Paris   Audit: move audit...
394
395
396
  	mutex_unlock(&audit_filter_mutex);
  
  	/* Avoid calling path_lookup under audit_filter_mutex. */
15a9155fe   Al Viro   fix race in audit...
397
  	ret = audit_get_nd(watch, &parent_path);
35fe4d0b1   Eric Paris   Audit: move audit...
398

15a9155fe   Al Viro   fix race in audit...
399
  	/* caller expects mutex locked */
e118e9c56   Eric Paris   audit: redo audit...
400
  	mutex_lock(&audit_filter_mutex);
baa2a4fdd   Ronny Chevalier   audit: fix use-af...
401
402
  	if (ret) {
  		audit_put_watch(watch);
15a9155fe   Al Viro   fix race in audit...
403
  		return ret;
baa2a4fdd   Ronny Chevalier   audit: fix use-af...
404
  	}
cfcad62c7   Eric Paris   audit: seperate a...
405

e118e9c56   Eric Paris   audit: redo audit...
406
  	/* either find an old parent or attach a new one */
3b362157b   David Howells   VFS: audit: d_bac...
407
  	parent = audit_find_parent(d_backing_inode(parent_path.dentry));
e9fd702a5   Eric Paris   audit: convert au...
408
  	if (!parent) {
15a9155fe   Al Viro   fix race in audit...
409
  		parent = audit_init_parent(&parent_path);
cfcad62c7   Eric Paris   audit: seperate a...
410
  		if (IS_ERR(parent)) {
35fe4d0b1   Eric Paris   Audit: move audit...
411
412
  			ret = PTR_ERR(parent);
  			goto error;
cfcad62c7   Eric Paris   audit: seperate a...
413
  		}
e9fd702a5   Eric Paris   audit: convert au...
414
  	}
cfcad62c7   Eric Paris   audit: seperate a...
415

e118e9c56   Eric Paris   audit: redo audit...
416
  	audit_add_to_parent(krule, parent);
cfcad62c7   Eric Paris   audit: seperate a...
417

ae7b8f410   Eric Paris   Audit: clean up t...
418
419
  	h = audit_hash_ino((u32)watch->ino);
  	*list = &audit_inode_hash[h];
35fe4d0b1   Eric Paris   Audit: move audit...
420
  error:
15a9155fe   Al Viro   fix race in audit...
421
  	path_put(&parent_path);
baa2a4fdd   Ronny Chevalier   audit: fix use-af...
422
  	audit_put_watch(watch);
cfcad62c7   Eric Paris   audit: seperate a...
423
424
  	return ret;
  }
a05fb6cc5   Eric Paris   audit: do not get...
425
  void audit_remove_watch_rule(struct audit_krule *krule)
cfcad62c7   Eric Paris   audit: seperate a...
426
427
428
429
430
431
432
  {
  	struct audit_watch *watch = krule->watch;
  	struct audit_parent *parent = watch->parent;
  
  	list_del(&krule->rlist);
  
  	if (list_empty(&watch->rules)) {
d76036ab4   Jan Kara   audit: Fix use af...
433
434
435
436
437
  		/*
  		 * audit_remove_watch() drops our reference to 'parent' which
  		 * can get freed. Grab our own reference to be safe.
  		 */
  		audit_get_parent(parent);
cfcad62c7   Eric Paris   audit: seperate a...
438
  		audit_remove_watch(watch);
d76036ab4   Jan Kara   audit: Fix use af...
439
  		if (list_empty(&parent->watches))
e2a29943e   Lino Sanfilippo   fsnotify: pass gr...
440
  			fsnotify_destroy_mark(&parent->mark, audit_watch_group);
d76036ab4   Jan Kara   audit: Fix use af...
441
  		audit_put_parent(parent);
cfcad62c7   Eric Paris   audit: seperate a...
442
443
  	}
  }
e9fd702a5   Eric Paris   audit: convert au...
444
  /* Update watch data in audit rules based on fsnotify events. */
b9a1b9772   Amir Goldstein   fsnotify: create ...
445
446
  static int audit_watch_handle_event(struct fsnotify_mark *inode_mark, u32 mask,
  				    struct inode *inode, struct inode *dir,
c9be99c86   Amir Goldstein   fsnotify: general...
447
  				    const struct qstr *dname, u32 cookie)
e9fd702a5   Eric Paris   audit: convert au...
448
  {
cfcad62c7   Eric Paris   audit: seperate a...
449
  	struct audit_parent *parent;
ce8f76fb7   Eric Paris   fsnotify: pass bo...
450
  	parent = container_of(inode_mark, struct audit_parent, mark);
e9fd702a5   Eric Paris   audit: convert au...
451

b9a1b9772   Amir Goldstein   fsnotify: create ...
452
453
454
  	if (WARN_ON_ONCE(inode_mark->group != audit_watch_group) ||
  	    WARN_ON_ONCE(!inode))
  		return 0;
cfcad62c7   Eric Paris   audit: seperate a...
455

e9fd702a5   Eric Paris   audit: convert au...
456
  	if (mask & (FS_CREATE|FS_MOVED_TO) && inode)
6921d4ebe   Al Viro   audit_update_watc...
457
  		audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0);
e9fd702a5   Eric Paris   audit: convert au...
458
  	else if (mask & (FS_DELETE|FS_MOVED_FROM))
6921d4ebe   Al Viro   audit_update_watc...
459
  		audit_update_watch(parent, dname, AUDIT_DEV_UNSET, AUDIT_INO_UNSET, 1);
e9fd702a5   Eric Paris   audit: convert au...
460
  	else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF))
cfcad62c7   Eric Paris   audit: seperate a...
461
  		audit_remove_parent_watches(parent);
e9fd702a5   Eric Paris   audit: convert au...
462
463
464
  
  	return 0;
  }
e9fd702a5   Eric Paris   audit: convert au...
465
  static const struct fsnotify_ops audit_watch_fsnotify_ops = {
b9a1b9772   Amir Goldstein   fsnotify: create ...
466
  	.handle_inode_event =	audit_watch_handle_event,
054c636e5   Jan Kara   fsnotify: Move ->...
467
  	.free_mark =		audit_watch_free_mark,
cfcad62c7   Eric Paris   audit: seperate a...
468
469
470
471
  };
  
  static int __init audit_watch_init(void)
  {
0d2e2a1d0   Eric Paris   fsnotify: drop ma...
472
  	audit_watch_group = fsnotify_alloc_group(&audit_watch_fsnotify_ops);
e9fd702a5   Eric Paris   audit: convert au...
473
474
475
476
  	if (IS_ERR(audit_watch_group)) {
  		audit_watch_group = NULL;
  		audit_panic("cannot create audit fsnotify group");
  	}
cfcad62c7   Eric Paris   audit: seperate a...
477
478
  	return 0;
  }
1a3aedbce   Eric Paris   Audit: audit watc...
479
  device_initcall(audit_watch_init);
34d99af52   Richard Guy Briggs   audit: implement ...
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
  
  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)
  {
15ce414b8   Richard Guy Briggs   fixup: audit: imp...
502
503
504
  	struct file *exe_file;
  	unsigned long ino;
  	dev_t dev;
5efc24434   Mateusz Guzik   audit: fix exe_fi...
505
506
507
  	exe_file = get_task_exe_file(tsk);
  	if (!exe_file)
  		return 0;
450630975   Al Viro   don't open-code f...
508
509
  	ino = file_inode(exe_file)->i_ino;
  	dev = file_inode(exe_file)->i_sb->s_dev;
5efc24434   Mateusz Guzik   audit: fix exe_fi...
510
  	fput(exe_file);
34d99af52   Richard Guy Briggs   audit: implement ...
511
512
  	return audit_mark_compare(mark, ino, dev);
  }