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 */
26struct audit_fsnotify_mark {
27 dev_t dev; /* associated superblock device */
28 unsigned long 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. */
35static 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
41static void audit_fsnotify_mark_free(struct audit_fsnotify_mark *audit_mark)
42{
43 kfree(objp: audit_mark->path);
44 kfree(objp: audit_mark);
45}
46
47static 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
55char *audit_mark_path(struct audit_fsnotify_mark *mark)
56{
57 return mark->path;
58}
59
60int audit_mark_compare(struct audit_fsnotify_mark *mark, unsigned long 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
67static 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
74struct audit_fsnotify_mark *audit_alloc_mark(struct audit_krule *krule, char *pathname, int len)
75{
76 struct audit_fsnotify_mark *audit_mark;
77 struct path path;
78 struct dentry *dentry;
79 int ret;
80
81 if (pathname[0] != '/' || pathname[len-1] == '/')
82 return ERR_PTR(error: -EINVAL);
83
84 dentry = kern_path_parent(name: pathname, parent: &path);
85 if (IS_ERR(ptr: dentry))
86 return ERR_CAST(ptr: dentry); /* returning an error */
87 if (d_really_is_negative(dentry)) {
88 audit_mark = ERR_PTR(error: -ENOENT);
89 goto out;
90 }
91
92 audit_mark = kzalloc(sizeof(*audit_mark), GFP_KERNEL);
93 if (unlikely(!audit_mark)) {
94 audit_mark = ERR_PTR(error: -ENOMEM);
95 goto out;
96 }
97
98 fsnotify_init_mark(mark: &audit_mark->mark, group: audit_fsnotify_group);
99 audit_mark->mark.mask = AUDIT_FS_EVENTS;
100 audit_mark->path = pathname;
101 audit_update_mark(audit_mark, inode: dentry->d_inode);
102 audit_mark->rule = krule;
103
104 ret = fsnotify_add_inode_mark(mark: &audit_mark->mark, inode: path.dentry->d_inode, add_flags: 0);
105 if (ret < 0) {
106 audit_mark->path = NULL;
107 fsnotify_put_mark(mark: &audit_mark->mark);
108 audit_mark = ERR_PTR(error: ret);
109 }
110out:
111 dput(dentry);
112 path_put(&path);
113 return audit_mark;
114}
115
116static void audit_mark_log_rule_change(struct audit_fsnotify_mark *audit_mark, char *op)
117{
118 struct audit_buffer *ab;
119 struct audit_krule *rule = audit_mark->rule;
120
121 if (!audit_enabled)
122 return;
123 ab = audit_log_start(ctx: audit_context(), GFP_NOFS, AUDIT_CONFIG_CHANGE);
124 if (unlikely(!ab))
125 return;
126 audit_log_session_info(ab);
127 audit_log_format(ab, fmt: " op=%s path=", op);
128 audit_log_untrustedstring(ab, string: audit_mark->path);
129 audit_log_key(ab, key: rule->filterkey);
130 audit_log_format(ab, fmt: " list=%d res=1", rule->listnr);
131 audit_log_end(ab);
132}
133
134void audit_remove_mark(struct audit_fsnotify_mark *audit_mark)
135{
136 fsnotify_destroy_mark(mark: &audit_mark->mark, group: audit_fsnotify_group);
137 fsnotify_put_mark(mark: &audit_mark->mark);
138}
139
140void audit_remove_mark_rule(struct audit_krule *krule)
141{
142 struct audit_fsnotify_mark *mark = krule->exe;
143
144 audit_remove_mark(audit_mark: mark);
145}
146
147static void audit_autoremove_mark_rule(struct audit_fsnotify_mark *audit_mark)
148{
149 struct audit_krule *rule = audit_mark->rule;
150 struct audit_entry *entry = container_of(rule, struct audit_entry, rule);
151
152 audit_mark_log_rule_change(audit_mark, op: "autoremove_rule");
153 audit_del_rule(entry);
154}
155
156/* Update mark data in audit rules based on fsnotify events. */
157static int audit_mark_handle_event(struct fsnotify_mark *inode_mark, u32 mask,
158 struct inode *inode, struct inode *dir,
159 const struct qstr *dname, u32 cookie)
160{
161 struct audit_fsnotify_mark *audit_mark;
162
163 audit_mark = container_of(inode_mark, struct audit_fsnotify_mark, mark);
164
165 if (WARN_ON_ONCE(inode_mark->group != audit_fsnotify_group))
166 return 0;
167
168 if (mask & (FS_CREATE|FS_MOVED_TO|FS_DELETE|FS_MOVED_FROM)) {
169 if (audit_compare_dname_path(dname, path: audit_mark->path, AUDIT_NAME_FULL))
170 return 0;
171 audit_update_mark(audit_mark, inode);
172 } else if (mask & (FS_DELETE_SELF|FS_UNMOUNT|FS_MOVE_SELF)) {
173 audit_autoremove_mark_rule(audit_mark);
174 }
175
176 return 0;
177}
178
179static const struct fsnotify_ops audit_mark_fsnotify_ops = {
180 .handle_inode_event = audit_mark_handle_event,
181 .free_mark = audit_fsnotify_free_mark,
182};
183
184static int __init audit_fsnotify_init(void)
185{
186 audit_fsnotify_group = fsnotify_alloc_group(ops: &audit_mark_fsnotify_ops,
187 FSNOTIFY_GROUP_DUPS);
188 if (IS_ERR(ptr: audit_fsnotify_group)) {
189 audit_fsnotify_group = NULL;
190 audit_panic(message: "cannot create audit fsnotify group");
191 }
192 return 0;
193}
194device_initcall(audit_fsnotify_init);
195