xref: /linux/kernel/audit_fsnotify.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* audit_fsnotify.c -- tracking inodes
3  *
4  * Copyright 2003-2009,2014-2015 Red Hat, Inc.
5  * Copyright 2005 Hewlett-Packard Development Company, L.P.
6  * Copyright 2005 IBM Corporation
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/audit.h>
11 #include <linux/kthread.h>
12 #include <linux/mutex.h>
13 #include <linux/fs.h>
14 #include <linux/fsnotify_backend.h>
15 #include <linux/namei.h>
16 #include <linux/netlink.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/security.h>
20 #include "audit.h"
21 
22 /*
23  * this mark lives on the parent directory of the inode in question.
24  * but dev, ino, and path are about the child
25  */
26 struct audit_fsnotify_mark {
27 	dev_t dev;		/* associated superblock device */
28 	u64 ino;		/* associated inode number */
29 	char *path;		/* insertion path */
30 	struct fsnotify_mark mark; /* fsnotify mark on the inode */
31 	struct audit_krule *rule;
32 };
33 
34 /* fsnotify handle. */
35 static struct fsnotify_group *audit_fsnotify_group;
36 
37 /* fsnotify events we care about. */
38 #define AUDIT_FS_EVENTS (FS_MOVE | FS_CREATE | FS_DELETE | FS_DELETE_SELF |\
39 			 FS_MOVE_SELF)
40 
41 static void audit_fsnotify_mark_free(struct audit_fsnotify_mark *audit_mark)
42 {
43 	kfree(audit_mark->path);
44 	kfree(audit_mark);
45 }
46 
47 static void audit_fsnotify_free_mark(struct fsnotify_mark *mark)
48 {
49 	struct audit_fsnotify_mark *audit_mark;
50 
51 	audit_mark = container_of(mark, struct audit_fsnotify_mark, mark);
52 	audit_fsnotify_mark_free(audit_mark);
53 }
54 
55 char *audit_mark_path(struct audit_fsnotify_mark *mark)
56 {
57 	return mark->path;
58 }
59 
60 int audit_mark_compare(struct audit_fsnotify_mark *mark, u64 ino, dev_t dev)
61 {
62 	if (mark->ino == AUDIT_INO_UNSET)
63 		return 0;
64 	return (mark->ino == ino) && (mark->dev == dev);
65 }
66 
67 static void audit_update_mark(struct audit_fsnotify_mark *audit_mark,
68 			     const struct inode *inode)
69 {
70 	audit_mark->dev = inode ? inode->i_sb->s_dev : AUDIT_DEV_UNSET;
71 	audit_mark->ino = inode ? inode->i_ino : AUDIT_INO_UNSET;
72 }
73 
74 struct audit_fsnotify_mark *audit_alloc_mark(struct audit_krule *krule, char *pathname,
75 					     int len, struct audit_watch_ctx *ctx)
76 {
77 	struct audit_fsnotify_mark *audit_mark;
78 	struct path path;
79 	struct dentry *dentry;
80 	struct inode *dir, *child;
81 	int ret, allow_dups;
82 
83 	if (pathname[0] != '/' || pathname[len-1] == '/')
84 		return ERR_PTR(-EINVAL);
85 
86 	if (!ctx) {
87 		dentry = kern_path_parent(pathname, &path);
88 		if (IS_ERR(dentry))
89 			return ERR_CAST(dentry); /* returning an error */
90 		dir = d_inode(path.dentry);
91 		child = d_inode(dentry);
92 		allow_dups = 0;
93 	} else {
94 		dir = ctx->dir;
95 		child = ctx->child;
96 		allow_dups = 1;
97 	}
98 
99 	audit_mark = kzalloc_obj(*audit_mark);
100 	if (unlikely(!audit_mark)) {
101 		audit_mark = ERR_PTR(-ENOMEM);
102 		goto out;
103 	}
104 
105 	fsnotify_init_mark(&audit_mark->mark, audit_fsnotify_group);
106 	audit_mark->mark.mask = AUDIT_FS_EVENTS;
107 	audit_mark->path = pathname;
108 	audit_mark->rule = krule;
109 
110 	audit_update_mark(audit_mark, child);
111 	ret = fsnotify_add_inode_mark(&audit_mark->mark, dir, allow_dups);
112 
113 	if (ret < 0) {
114 		audit_mark->path = NULL;
115 		fsnotify_put_mark(&audit_mark->mark);
116 		audit_mark = ERR_PTR(ret);
117 	}
118 out:
119 	if (!ctx) {
120 		dput(dentry);
121 		path_put(&path);
122 	}
123 	return audit_mark;
124 }
125 
126 static void audit_mark_log_rule_change(struct audit_fsnotify_mark *audit_mark, char *op)
127 {
128 	struct audit_buffer *ab;
129 	struct audit_krule *rule = audit_mark->rule;
130 
131 	if (!audit_enabled)
132 		return;
133 	ab = audit_log_start(audit_context(), GFP_NOFS, AUDIT_CONFIG_CHANGE);
134 	if (unlikely(!ab))
135 		return;
136 	audit_log_session_info(ab);
137 	audit_log_format(ab, " op=%s path=", op);
138 	audit_log_untrustedstring(ab, audit_mark->path);
139 	audit_log_key(ab, rule->filterkey);
140 	audit_log_format(ab, " list=%d res=1", rule->listnr);
141 	audit_log_end(ab);
142 }
143 
144 void audit_remove_mark(struct audit_fsnotify_mark *audit_mark)
145 {
146 	fsnotify_destroy_mark(&audit_mark->mark, audit_fsnotify_group);
147 	fsnotify_put_mark(&audit_mark->mark);
148 }
149 
150 void audit_remove_mark_rule(struct audit_krule *krule)
151 {
152 	struct audit_fsnotify_mark *mark = krule->exe;
153 
154 	audit_remove_mark(mark);
155 }
156 
157 static void audit_autoremove_mark_rule(struct audit_fsnotify_mark *audit_mark)
158 {
159 	struct audit_krule *rule = audit_mark->rule;
160 	struct audit_entry *entry = container_of(rule, struct audit_entry, rule);
161 
162 	audit_mark_log_rule_change(audit_mark, "autoremove_rule");
163 	audit_del_rule(entry);
164 }
165 
166 /* Update mark data in audit rules based on fsnotify events. */
167 static int audit_mark_handle_event(struct fsnotify_mark *inode_mark, u32 mask,
168 				   struct inode *inode, struct inode *dir,
169 				   const struct qstr *dname, u32 cookie)
170 {
171 	struct audit_fsnotify_mark *audit_mark;
172 
173 	audit_mark = container_of(inode_mark, struct audit_fsnotify_mark, mark);
174 
175 	if (WARN_ON_ONCE(inode_mark->group != audit_fsnotify_group))
176 		return 0;
177 
178 	if (mask & (FS_CREATE|FS_MOVED_TO|FS_DELETE|FS_MOVED_FROM)) {
179 		if (audit_compare_dname_path(dname, audit_mark->path, AUDIT_NAME_FULL))
180 			return 0;
181 		audit_update_mark(audit_mark, inode);
182 	} else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF)) {
183 		audit_autoremove_mark_rule(audit_mark);
184 	}
185 
186 	return 0;
187 }
188 
189 static const struct fsnotify_ops audit_mark_fsnotify_ops = {
190 	.handle_inode_event = audit_mark_handle_event,
191 	.free_mark = audit_fsnotify_free_mark,
192 };
193 
194 static int __init audit_fsnotify_init(void)
195 {
196 	audit_fsnotify_group = fsnotify_alloc_group(&audit_mark_fsnotify_ops,
197 						    FSNOTIFY_GROUP_DUPS);
198 	if (IS_ERR(audit_fsnotify_group)) {
199 		audit_fsnotify_group = NULL;
200 		audit_panic("cannot create audit fsnotify group");
201 	}
202 	return 0;
203 }
204 device_initcall(audit_fsnotify_init);
205