xref: /linux/kernel/audit_watch.c (revision 12d5fc96ebbb3b948861805b32a4c3a96105ccd9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* audit_watch.c -- watching inodes
3  *
4  * Copyright 2003-2009 Red Hat, Inc.
5  * Copyright 2005 Hewlett-Packard Development Company, L.P.
6  * Copyright 2005 IBM Corporation
7  */
8 
9 #include <linux/file.h>
10 #include <linux/kernel.h>
11 #include <linux/audit.h>
12 #include <linux/kthread.h>
13 #include <linux/mutex.h>
14 #include <linux/fs.h>
15 #include <linux/fsnotify_backend.h>
16 #include <linux/namei.h>
17 #include <linux/netlink.h>
18 #include <linux/refcount.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/security.h>
22 #include "audit.h"
23 
24 /*
25  * Reference counting:
26  *
27  * audit_parent: lifetime is from audit_init_parent() to receipt of an FS_IGNORED
28  * 	event.  Each audit_watch holds a reference to its associated parent.
29  *
30  * audit_watch: if added to lists, lifetime is from audit_init_watch() to
31  * 	audit_remove_watch().  Additionally, an audit_watch may exist
32  * 	temporarily to assist in searching existing filter data.  Each
33  * 	audit_krule holds a reference to its associated watch.
34  */
35 
36 struct audit_watch {
37 	refcount_t		count;	/* reference count */
38 	dev_t			dev;	/* associated superblock device */
39 	char			*path;	/* insertion path */
40 	u64			ino;	/* associated inode number */
41 	struct audit_parent	*parent; /* associated parent */
42 	struct list_head	wlist;	/* entry in parent->watches list */
43 	struct list_head	rules;	/* anchor for krule->rlist */
44 };
45 
46 struct audit_parent {
47 	struct list_head	watches; /* anchor for audit_watch->wlist */
48 	struct fsnotify_mark mark; /* fsnotify mark on the inode */
49 };
50 
51 /* fsnotify handle. */
52 static struct fsnotify_group *audit_watch_group;
53 
54 /* fsnotify events we care about. */
55 #define AUDIT_FS_WATCH (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
56 			FS_MOVE_SELF | FS_UNMOUNT)
57 
58 static void audit_free_parent(struct audit_parent *parent)
59 {
60 	WARN_ON(!list_empty(&parent->watches));
61 	kfree(parent);
62 }
63 
64 static void audit_watch_free_mark(struct fsnotify_mark *entry)
65 {
66 	struct audit_parent *parent;
67 
68 	parent = container_of(entry, struct audit_parent, mark);
69 	audit_free_parent(parent);
70 }
71 
72 static void audit_get_parent(struct audit_parent *parent)
73 {
74 	if (likely(parent))
75 		fsnotify_get_mark(&parent->mark);
76 }
77 
78 static void audit_put_parent(struct audit_parent *parent)
79 {
80 	if (likely(parent))
81 		fsnotify_put_mark(&parent->mark);
82 }
83 
84 /*
85  * Find and return the audit_parent on the given inode.  If found a reference
86  * is taken on this parent.
87  */
88 static inline struct audit_parent *audit_find_parent(struct inode *inode)
89 {
90 	struct audit_parent *parent = NULL;
91 	struct fsnotify_mark *entry;
92 
93 	entry = fsnotify_find_inode_mark(inode, audit_watch_group);
94 	if (entry)
95 		parent = container_of(entry, struct audit_parent, mark);
96 
97 	return parent;
98 }
99 
100 void audit_get_watch(struct audit_watch *watch)
101 {
102 	refcount_inc(&watch->count);
103 }
104 
105 void audit_put_watch(struct audit_watch *watch)
106 {
107 	if (refcount_dec_and_test(&watch->count)) {
108 		WARN_ON(watch->parent);
109 		WARN_ON(!list_empty(&watch->rules));
110 		kfree(watch->path);
111 		kfree(watch);
112 	}
113 }
114 
115 static void audit_remove_watch(struct audit_watch *watch)
116 {
117 	list_del(&watch->wlist);
118 	audit_put_parent(watch->parent);
119 	watch->parent = NULL;
120 	audit_put_watch(watch); /* match initial get */
121 }
122 
123 char *audit_watch_path(struct audit_watch *watch)
124 {
125 	return watch->path;
126 }
127 
128 int audit_watch_compare(struct audit_watch *watch, u64 ino, dev_t dev)
129 {
130 	return (watch->ino != AUDIT_INO_UNSET) &&
131 		(watch->ino == ino) &&
132 		(watch->dev == dev);
133 }
134 
135 /* Initialize a parent watch entry. */
136 static struct audit_parent *audit_init_parent(const struct path *path)
137 {
138 	struct inode *inode = d_backing_inode(path->dentry);
139 	struct audit_parent *parent;
140 	int ret;
141 
142 	parent = kzalloc_obj(*parent);
143 	if (unlikely(!parent))
144 		return ERR_PTR(-ENOMEM);
145 
146 	INIT_LIST_HEAD(&parent->watches);
147 
148 	fsnotify_init_mark(&parent->mark, audit_watch_group);
149 	parent->mark.mask = AUDIT_FS_WATCH;
150 	ret = fsnotify_add_inode_mark(&parent->mark, inode, 0);
151 	if (ret < 0) {
152 		audit_free_parent(parent);
153 		return ERR_PTR(ret);
154 	}
155 
156 	return parent;
157 }
158 
159 /* Initialize a watch entry. */
160 static struct audit_watch *audit_init_watch(char *path)
161 {
162 	struct audit_watch *watch;
163 
164 	watch = kzalloc_obj(*watch);
165 	if (unlikely(!watch))
166 		return ERR_PTR(-ENOMEM);
167 
168 	INIT_LIST_HEAD(&watch->rules);
169 	refcount_set(&watch->count, 1);
170 	watch->path = path;
171 	watch->dev = AUDIT_DEV_UNSET;
172 	watch->ino = AUDIT_INO_UNSET;
173 
174 	return watch;
175 }
176 
177 /* Translate a watch string to kernel representation. */
178 int audit_to_watch(struct audit_krule *krule, char *path, int len, u32 op)
179 {
180 	struct audit_watch *watch;
181 
182 	if (!audit_watch_group)
183 		return -EOPNOTSUPP;
184 
185 	if (path[0] != '/' || path[len-1] == '/' ||
186 	    (krule->listnr != AUDIT_FILTER_EXIT &&
187 	     krule->listnr != AUDIT_FILTER_URING_EXIT) ||
188 	    op != Audit_equal ||
189 	    krule->inode_f || krule->watch || krule->tree)
190 		return -EINVAL;
191 
192 	watch = audit_init_watch(path);
193 	if (IS_ERR(watch))
194 		return PTR_ERR(watch);
195 
196 	krule->watch = watch;
197 
198 	return 0;
199 }
200 
201 /* Duplicate the given audit watch.  The new watch's rules list is initialized
202  * to an empty list and wlist is undefined. */
203 static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
204 {
205 	char *path;
206 	struct audit_watch *new;
207 
208 	path = kstrdup(old->path, GFP_KERNEL);
209 	if (unlikely(!path))
210 		return ERR_PTR(-ENOMEM);
211 
212 	new = audit_init_watch(path);
213 	if (IS_ERR(new)) {
214 		kfree(path);
215 		goto out;
216 	}
217 
218 	new->dev = old->dev;
219 	new->ino = old->ino;
220 	audit_get_parent(old->parent);
221 	new->parent = old->parent;
222 
223 out:
224 	return new;
225 }
226 
227 static void audit_watch_log_rule_change(struct audit_krule *r, struct audit_watch *w, char *op)
228 {
229 	struct audit_buffer *ab;
230 
231 	if (!audit_enabled)
232 		return;
233 	ab = audit_log_start(audit_context(), GFP_NOFS, AUDIT_CONFIG_CHANGE);
234 	if (!ab)
235 		return;
236 	audit_log_session_info(ab);
237 	audit_log_format(ab, "op=%s path=", op);
238 	audit_log_untrustedstring(ab, w->path);
239 	audit_log_key(ab, r->filterkey);
240 	audit_log_format(ab, " list=%d res=1", r->listnr);
241 	audit_log_end(ab);
242 }
243 
244 /* Update inode info in audit rules based on filesystem event. */
245 static void audit_update_watch(struct audit_parent *parent,
246 			       const struct qstr *dname, dev_t dev,
247 			       u64 ino, unsigned int invalidating,
248 			       struct audit_watch_ctx *ctx)
249 {
250 	struct audit_watch *owatch, *nwatch, *nextw;
251 	struct audit_krule *r, *nextr;
252 	struct audit_entry *oentry, *nentry;
253 
254 	mutex_lock(&audit_filter_mutex);
255 	/* Run all of the watches on this parent looking for the one that
256 	 * matches the given dname */
257 	list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
258 		if (audit_compare_dname_path(dname, owatch->path,
259 					     AUDIT_NAME_FULL))
260 			continue;
261 
262 		/* If the update involves invalidating rules, do the inode-based
263 		 * filtering now, so we don't omit records. */
264 		if (invalidating && !audit_dummy_context())
265 			audit_filter_inodes(current, audit_context());
266 
267 		/* updating ino will likely change which audit_hash_list we
268 		 * are on so we need a new watch for the new list */
269 		nwatch = audit_dupe_watch(owatch);
270 		if (IS_ERR(nwatch)) {
271 			mutex_unlock(&audit_filter_mutex);
272 			audit_panic("error updating watch, skipping");
273 			return;
274 		}
275 		nwatch->dev = dev;
276 		nwatch->ino = ino;
277 
278 		list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
279 
280 			oentry = container_of(r, struct audit_entry, rule);
281 			list_del(&oentry->rule.rlist);
282 			list_del_rcu(&oentry->list);
283 
284 			nentry = audit_dupe_rule(&oentry->rule, ctx);
285 			if (IS_ERR(nentry)) {
286 				list_del(&oentry->rule.list);
287 				audit_rule_unaccount(r);
288 				audit_panic("error updating watch, removing");
289 			} else {
290 				int h = audit_hash_ino(ino);
291 
292 				/*
293 				 * nentry->rule.watch == oentry->rule.watch so
294 				 * we must drop that reference and set it to our
295 				 * new watch.
296 				 */
297 				audit_put_watch(nentry->rule.watch);
298 				audit_get_watch(nwatch);
299 				nentry->rule.watch = nwatch;
300 				list_add(&nentry->rule.rlist, &nwatch->rules);
301 				list_add_rcu(&nentry->list, &audit_inode_hash[h]);
302 				list_replace(&oentry->rule.list,
303 					     &nentry->rule.list);
304 			}
305 			if (oentry->rule.exe)
306 				audit_remove_mark(oentry->rule.exe);
307 
308 			call_rcu(&oentry->rcu, audit_free_rule_rcu);
309 		}
310 
311 		audit_remove_watch(owatch);
312 		goto add_watch_to_parent; /* event applies to a single watch */
313 	}
314 	mutex_unlock(&audit_filter_mutex);
315 	return;
316 
317 add_watch_to_parent:
318 	list_add(&nwatch->wlist, &parent->watches);
319 	mutex_unlock(&audit_filter_mutex);
320 	return;
321 }
322 
323 /* Remove all watches & rules associated with a parent that is going away. */
324 static void audit_remove_parent_watches(struct audit_parent *parent)
325 {
326 	struct audit_watch *w, *nextw;
327 	struct audit_krule *r, *nextr;
328 	struct audit_entry *e;
329 
330 	mutex_lock(&audit_filter_mutex);
331 	list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
332 		list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
333 			e = container_of(r, struct audit_entry, rule);
334 			audit_watch_log_rule_change(r, w, "remove_rule");
335 			if (e->rule.exe)
336 				audit_remove_mark(e->rule.exe);
337 			list_del(&r->rlist);
338 			list_del(&r->list);
339 			list_del_rcu(&e->list);
340 			audit_rule_unaccount(r);
341 			call_rcu(&e->rcu, audit_free_rule_rcu);
342 		}
343 		audit_remove_watch(w);
344 	}
345 	mutex_unlock(&audit_filter_mutex);
346 
347 	fsnotify_destroy_mark(&parent->mark, audit_watch_group);
348 }
349 
350 /* Get path information necessary for adding watches. */
351 static int audit_get_nd(struct audit_watch *watch, struct path *parent)
352 {
353 	struct dentry *d;
354 
355 	d = kern_path_parent(watch->path, parent);
356 	if (IS_ERR(d))
357 		return PTR_ERR(d);
358 
359 	if (d_is_positive(d)) {
360 		/* update watch filter fields */
361 		watch->dev = d->d_sb->s_dev;
362 		watch->ino = d_backing_inode(d)->i_ino;
363 	}
364 
365 	dput(d);
366 	return 0;
367 }
368 
369 /* Associate the given rule with an existing parent.
370  * Caller must hold audit_filter_mutex. */
371 static void audit_add_to_parent(struct audit_krule *krule,
372 				struct audit_parent *parent)
373 {
374 	struct audit_watch *w, *watch = krule->watch;
375 	int watch_found = 0;
376 
377 	lockdep_assert_held(&audit_filter_mutex);
378 
379 	list_for_each_entry(w, &parent->watches, wlist) {
380 		if (strcmp(watch->path, w->path))
381 			continue;
382 
383 		watch_found = 1;
384 
385 		/* put krule's ref to temporary watch */
386 		audit_put_watch(watch);
387 
388 		audit_get_watch(w);
389 		krule->watch = watch = w;
390 
391 		audit_put_parent(parent);
392 		break;
393 	}
394 
395 	if (!watch_found) {
396 		watch->parent = parent;
397 
398 		audit_get_watch(watch);
399 		list_add(&watch->wlist, &parent->watches);
400 	}
401 	list_add(&krule->rlist, &watch->rules);
402 }
403 
404 /* Find a matching watch entry, or add this one.
405  * Caller must hold audit_filter_mutex. */
406 int audit_add_watch(struct audit_krule *krule, struct list_head **list)
407 {
408 	struct audit_watch *watch = krule->watch;
409 	struct audit_parent *parent;
410 	struct path parent_path;
411 	int h, ret = 0;
412 
413 	/*
414 	 * When we will be calling audit_add_to_parent, krule->watch might have
415 	 * been updated and watch might have been freed.
416 	 * So we need to keep a reference of watch.
417 	 */
418 	audit_get_watch(watch);
419 
420 	mutex_unlock(&audit_filter_mutex);
421 
422 	/* Avoid calling path_lookup under audit_filter_mutex. */
423 	ret = audit_get_nd(watch, &parent_path);
424 
425 	/* caller expects mutex locked */
426 	mutex_lock(&audit_filter_mutex);
427 
428 	if (ret) {
429 		audit_put_watch(watch);
430 		return ret;
431 	}
432 
433 	/* either find an old parent or attach a new one */
434 	parent = audit_find_parent(d_backing_inode(parent_path.dentry));
435 	if (!parent) {
436 		parent = audit_init_parent(&parent_path);
437 		if (IS_ERR(parent)) {
438 			ret = PTR_ERR(parent);
439 			goto error;
440 		}
441 	}
442 
443 	audit_add_to_parent(krule, parent);
444 
445 	h = audit_hash_ino(watch->ino);
446 	*list = &audit_inode_hash[h];
447 error:
448 	path_put(&parent_path);
449 	audit_put_watch(watch);
450 	return ret;
451 }
452 
453 void audit_remove_watch_rule(struct audit_krule *krule)
454 {
455 	struct audit_watch *watch = krule->watch;
456 	struct audit_parent *parent = watch->parent;
457 
458 	list_del(&krule->rlist);
459 
460 	if (list_empty(&watch->rules)) {
461 		/*
462 		 * audit_remove_watch() drops our reference to 'parent' which
463 		 * can get freed. Grab our own reference to be safe.
464 		 */
465 		audit_get_parent(parent);
466 		audit_remove_watch(watch);
467 		if (list_empty(&parent->watches))
468 			fsnotify_destroy_mark(&parent->mark, audit_watch_group);
469 		audit_put_parent(parent);
470 	}
471 }
472 
473 /* Update watch data in audit rules based on fsnotify events. */
474 static int audit_watch_handle_event(struct fsnotify_mark *inode_mark, u32 mask,
475 				    struct inode *inode, struct inode *dir,
476 				    const struct qstr *dname, u32 cookie)
477 {
478 	struct audit_parent *parent;
479 
480 	parent = container_of(inode_mark, struct audit_parent, mark);
481 
482 	if (WARN_ON_ONCE(inode_mark->group != audit_watch_group))
483 		return 0;
484 
485 	if (mask & (FS_CREATE|FS_MOVED_TO) && inode) {
486 		struct audit_watch_ctx ctx = { .dir = dir, .child = inode };
487 
488 		audit_update_watch(parent, dname, inode->i_sb->s_dev, inode->i_ino, 0,
489 				   &ctx);
490 	} else if (mask & (FS_DELETE|FS_MOVED_FROM)) {
491 		struct audit_watch_ctx ctx = { .dir = dir, .child = NULL };
492 
493 		audit_update_watch(parent, dname, AUDIT_DEV_UNSET, AUDIT_INO_UNSET, 1,
494 				   &ctx);
495 	}
496 	else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF))
497 		audit_remove_parent_watches(parent);
498 
499 	return 0;
500 }
501 
502 static const struct fsnotify_ops audit_watch_fsnotify_ops = {
503 	.handle_inode_event =	audit_watch_handle_event,
504 	.free_mark =		audit_watch_free_mark,
505 };
506 
507 static int __init audit_watch_init(void)
508 {
509 	audit_watch_group = fsnotify_alloc_group(&audit_watch_fsnotify_ops, 0);
510 	if (IS_ERR(audit_watch_group)) {
511 		audit_watch_group = NULL;
512 		audit_panic("cannot create audit fsnotify group");
513 	}
514 	return 0;
515 }
516 device_initcall(audit_watch_init);
517 
518 int audit_dupe_exe(struct audit_krule *new, struct audit_krule *old,
519 		   struct audit_watch_ctx *ctx)
520 {
521 	struct audit_fsnotify_mark *audit_mark;
522 	char *pathname;
523 
524 	pathname = kstrdup(audit_mark_path(old->exe), GFP_KERNEL);
525 	if (!pathname)
526 		return -ENOMEM;
527 
528 	audit_mark = audit_alloc_mark(new, pathname, strlen(pathname), ctx);
529 	if (IS_ERR(audit_mark)) {
530 		kfree(pathname);
531 		return PTR_ERR(audit_mark);
532 	}
533 	new->exe = audit_mark;
534 
535 	return 0;
536 }
537 
538 int audit_exe_compare(struct task_struct *tsk, struct audit_fsnotify_mark *mark)
539 {
540 	struct file *exe_file;
541 	u64 ino;
542 	dev_t dev;
543 
544 	/* only do exe filtering if we are recording @current events/records */
545 	if (tsk != current)
546 		return 0;
547 
548 	if (!current->mm)
549 		return 0;
550 	exe_file = get_mm_exe_file(current->mm);
551 	if (!exe_file)
552 		return 0;
553 	ino = file_inode(exe_file)->i_ino;
554 	dev = file_inode(exe_file)->i_sb->s_dev;
555 	fput(exe_file);
556 
557 	return audit_mark_compare(mark, ino, dev);
558 }
559