]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
33d3dfff | 2 | #include <linux/fanotify.h> |
11637e4b | 3 | #include <linux/fcntl.h> |
2a3edf86 | 4 | #include <linux/file.h> |
11637e4b | 5 | #include <linux/fs.h> |
52c923dd | 6 | #include <linux/anon_inodes.h> |
11637e4b | 7 | #include <linux/fsnotify_backend.h> |
2a3edf86 | 8 | #include <linux/init.h> |
a1014f10 | 9 | #include <linux/mount.h> |
2a3edf86 | 10 | #include <linux/namei.h> |
a1014f10 | 11 | #include <linux/poll.h> |
11637e4b EP |
12 | #include <linux/security.h> |
13 | #include <linux/syscalls.h> | |
e4e047a2 | 14 | #include <linux/slab.h> |
2a3edf86 | 15 | #include <linux/types.h> |
a1014f10 | 16 | #include <linux/uaccess.h> |
91c2e0bc | 17 | #include <linux/compat.h> |
174cd4b1 | 18 | #include <linux/sched/signal.h> |
d46eb14b | 19 | #include <linux/memcontrol.h> |
a8b13aa2 AG |
20 | #include <linux/statfs.h> |
21 | #include <linux/exportfs.h> | |
a1014f10 EP |
22 | |
23 | #include <asm/ioctls.h> | |
11637e4b | 24 | |
c63181e6 | 25 | #include "../../mount.h" |
be77196b | 26 | #include "../fdinfo.h" |
7053aee2 | 27 | #include "fanotify.h" |
c63181e6 | 28 | |
2529a0df | 29 | #define FANOTIFY_DEFAULT_MAX_EVENTS 16384 |
5b8fea65 AG |
30 | #define FANOTIFY_OLD_DEFAULT_MAX_MARKS 8192 |
31 | #define FANOTIFY_DEFAULT_MAX_GROUPS 128 | |
32 | ||
33 | /* | |
34 | * Legacy fanotify marks limits (8192) is per group and we introduced a tunable | |
35 | * limit of marks per user, similar to inotify. Effectively, the legacy limit | |
36 | * of fanotify marks per user is <max marks per group> * <max groups per user>. | |
37 | * This default limit (1M) also happens to match the increased limit of inotify | |
38 | * max_user_watches since v5.10. | |
39 | */ | |
40 | #define FANOTIFY_DEFAULT_MAX_USER_MARKS \ | |
41 | (FANOTIFY_OLD_DEFAULT_MAX_MARKS * FANOTIFY_DEFAULT_MAX_GROUPS) | |
42 | ||
43 | /* | |
44 | * Most of the memory cost of adding an inode mark is pinning the marked inode. | |
45 | * The size of the filesystem inode struct is not uniform across filesystems, | |
46 | * so double the size of a VFS inode is used as a conservative approximation. | |
47 | */ | |
48 | #define INODE_MARK_COST (2 * sizeof(struct inode)) | |
49 | ||
50 | /* configurable via /proc/sys/fs/fanotify/ */ | |
51 | static int fanotify_max_queued_events __read_mostly; | |
52 | ||
53 | #ifdef CONFIG_SYSCTL | |
54 | ||
55 | #include <linux/sysctl.h> | |
56 | ||
57 | struct ctl_table fanotify_table[] = { | |
58 | { | |
59 | .procname = "max_user_groups", | |
60 | .data = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS], | |
61 | .maxlen = sizeof(int), | |
62 | .mode = 0644, | |
63 | .proc_handler = proc_dointvec_minmax, | |
64 | .extra1 = SYSCTL_ZERO, | |
65 | }, | |
66 | { | |
67 | .procname = "max_user_marks", | |
68 | .data = &init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS], | |
69 | .maxlen = sizeof(int), | |
70 | .mode = 0644, | |
71 | .proc_handler = proc_dointvec_minmax, | |
72 | .extra1 = SYSCTL_ZERO, | |
73 | }, | |
74 | { | |
75 | .procname = "max_queued_events", | |
76 | .data = &fanotify_max_queued_events, | |
77 | .maxlen = sizeof(int), | |
78 | .mode = 0644, | |
79 | .proc_handler = proc_dointvec_minmax, | |
80 | .extra1 = SYSCTL_ZERO | |
81 | }, | |
82 | { } | |
83 | }; | |
84 | #endif /* CONFIG_SYSCTL */ | |
2529a0df | 85 | |
48149e9d HS |
86 | /* |
87 | * All flags that may be specified in parameter event_f_flags of fanotify_init. | |
88 | * | |
89 | * Internal and external open flags are stored together in field f_flags of | |
90 | * struct file. Only external open flags shall be allowed in event_f_flags. | |
91 | * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be | |
92 | * excluded. | |
93 | */ | |
94 | #define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \ | |
95 | O_ACCMODE | O_APPEND | O_NONBLOCK | \ | |
96 | __O_SYNC | O_DSYNC | O_CLOEXEC | \ | |
97 | O_LARGEFILE | O_NOATIME ) | |
98 | ||
33d3dfff | 99 | extern const struct fsnotify_ops fanotify_fsnotify_ops; |
11637e4b | 100 | |
054c636e | 101 | struct kmem_cache *fanotify_mark_cache __read_mostly; |
7088f357 JK |
102 | struct kmem_cache *fanotify_fid_event_cachep __read_mostly; |
103 | struct kmem_cache *fanotify_path_event_cachep __read_mostly; | |
f083441b | 104 | struct kmem_cache *fanotify_perm_event_cachep __read_mostly; |
2a3edf86 | 105 | |
5e469c83 | 106 | #define FANOTIFY_EVENT_ALIGN 4 |
44d705b0 AG |
107 | #define FANOTIFY_INFO_HDR_LEN \ |
108 | (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle)) | |
5e469c83 | 109 | |
44d705b0 | 110 | static int fanotify_fid_info_len(int fh_len, int name_len) |
d766b553 | 111 | { |
44d705b0 AG |
112 | int info_len = fh_len; |
113 | ||
114 | if (name_len) | |
115 | info_len += name_len + 1; | |
116 | ||
117 | return roundup(FANOTIFY_INFO_HDR_LEN + info_len, FANOTIFY_EVENT_ALIGN); | |
d766b553 AG |
118 | } |
119 | ||
929943b3 AG |
120 | static int fanotify_event_info_len(unsigned int fid_mode, |
121 | struct fanotify_event *event) | |
5e469c83 | 122 | { |
f454fa61 AG |
123 | struct fanotify_info *info = fanotify_event_info(event); |
124 | int dir_fh_len = fanotify_event_dir_fh_len(event); | |
afc894c7 | 125 | int fh_len = fanotify_event_object_fh_len(event); |
f454fa61 | 126 | int info_len = 0; |
929943b3 | 127 | int dot_len = 0; |
f454fa61 | 128 | |
929943b3 | 129 | if (dir_fh_len) { |
f454fa61 | 130 | info_len += fanotify_fid_info_len(dir_fh_len, info->name_len); |
929943b3 AG |
131 | } else if ((fid_mode & FAN_REPORT_NAME) && (event->mask & FAN_ONDIR)) { |
132 | /* | |
133 | * With group flag FAN_REPORT_NAME, if name was not recorded in | |
134 | * event on a directory, we will report the name ".". | |
135 | */ | |
136 | dot_len = 1; | |
137 | } | |
afc894c7 | 138 | |
44d705b0 | 139 | if (fh_len) |
929943b3 | 140 | info_len += fanotify_fid_info_len(fh_len, dot_len); |
44d705b0 | 141 | |
44d705b0 | 142 | return info_len; |
5e469c83 AG |
143 | } |
144 | ||
94e00d28 AG |
145 | /* |
146 | * Remove an hashed event from merge hash table. | |
147 | */ | |
148 | static void fanotify_unhash_event(struct fsnotify_group *group, | |
149 | struct fanotify_event *event) | |
150 | { | |
151 | assert_spin_locked(&group->notification_lock); | |
152 | ||
153 | pr_debug("%s: group=%p event=%p bucket=%u\n", __func__, | |
154 | group, event, fanotify_event_hash_bucket(group, event)); | |
155 | ||
156 | if (WARN_ON_ONCE(hlist_unhashed(&event->merge_list))) | |
157 | return; | |
158 | ||
159 | hlist_del_init(&event->merge_list); | |
160 | } | |
161 | ||
a1014f10 | 162 | /* |
7088f357 | 163 | * Get an fanotify notification event if one exists and is small |
a1014f10 | 164 | * enough to fit in "count". Return an error pointer if the count |
40873284 JK |
165 | * is not large enough. When permission event is dequeued, its state is |
166 | * updated accordingly. | |
a1014f10 | 167 | */ |
7088f357 | 168 | static struct fanotify_event *get_one_event(struct fsnotify_group *group, |
a1014f10 EP |
169 | size_t count) |
170 | { | |
5e469c83 | 171 | size_t event_size = FAN_EVENT_METADATA_LEN; |
7088f357 | 172 | struct fanotify_event *event = NULL; |
6f73171e | 173 | struct fsnotify_event *fsn_event; |
929943b3 | 174 | unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); |
a1014f10 EP |
175 | |
176 | pr_debug("%s: group=%p count=%zd\n", __func__, group, count); | |
177 | ||
8c554466 | 178 | spin_lock(&group->notification_lock); |
6f73171e AG |
179 | fsn_event = fsnotify_peek_first_event(group); |
180 | if (!fsn_event) | |
8c554466 | 181 | goto out; |
a1014f10 | 182 | |
6f73171e AG |
183 | event = FANOTIFY_E(fsn_event); |
184 | if (fid_mode) | |
185 | event_size += fanotify_event_info_len(fid_mode, event); | |
5e469c83 | 186 | |
8c554466 | 187 | if (event_size > count) { |
7088f357 | 188 | event = ERR_PTR(-EINVAL); |
8c554466 JK |
189 | goto out; |
190 | } | |
6f73171e AG |
191 | |
192 | /* | |
193 | * Held the notification_lock the whole time, so this is the | |
194 | * same event we peeked above. | |
195 | */ | |
196 | fsnotify_remove_first_event(group); | |
7088f357 JK |
197 | if (fanotify_is_perm_event(event->mask)) |
198 | FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED; | |
94e00d28 AG |
199 | if (fanotify_is_hashed_event(event->mask)) |
200 | fanotify_unhash_event(group, event); | |
8c554466 JK |
201 | out: |
202 | spin_unlock(&group->notification_lock); | |
7088f357 | 203 | return event; |
a1014f10 EP |
204 | } |
205 | ||
a741c2fe | 206 | static int create_fd(struct fsnotify_group *group, struct path *path, |
7053aee2 | 207 | struct file **file) |
a1014f10 EP |
208 | { |
209 | int client_fd; | |
a1014f10 EP |
210 | struct file *new_file; |
211 | ||
0b37e097 | 212 | client_fd = get_unused_fd_flags(group->fanotify_data.f_flags); |
a1014f10 EP |
213 | if (client_fd < 0) |
214 | return client_fd; | |
215 | ||
a1014f10 EP |
216 | /* |
217 | * we need a new file handle for the userspace program so it can read even if it was | |
218 | * originally opened O_WRONLY. | |
219 | */ | |
a741c2fe JK |
220 | new_file = dentry_open(path, |
221 | group->fanotify_data.f_flags | FMODE_NONOTIFY, | |
222 | current_cred()); | |
a1014f10 EP |
223 | if (IS_ERR(new_file)) { |
224 | /* | |
225 | * we still send an event even if we can't open the file. this | |
226 | * can happen when say tasks are gone and we try to open their | |
227 | * /proc files or we try to open a WRONLY file like in sysfs | |
228 | * we just send the errno to userspace since there isn't much | |
229 | * else we can do. | |
230 | */ | |
231 | put_unused_fd(client_fd); | |
232 | client_fd = PTR_ERR(new_file); | |
233 | } else { | |
352e3b24 | 234 | *file = new_file; |
a1014f10 EP |
235 | } |
236 | ||
22aa425d | 237 | return client_fd; |
a1014f10 EP |
238 | } |
239 | ||
40873284 JK |
240 | /* |
241 | * Finish processing of permission event by setting it to ANSWERED state and | |
242 | * drop group->notification_lock. | |
243 | */ | |
244 | static void finish_permission_event(struct fsnotify_group *group, | |
245 | struct fanotify_perm_event *event, | |
246 | unsigned int response) | |
247 | __releases(&group->notification_lock) | |
248 | { | |
fabf7f29 JK |
249 | bool destroy = false; |
250 | ||
40873284 JK |
251 | assert_spin_locked(&group->notification_lock); |
252 | event->response = response; | |
fabf7f29 JK |
253 | if (event->state == FAN_EVENT_CANCELED) |
254 | destroy = true; | |
255 | else | |
256 | event->state = FAN_EVENT_ANSWERED; | |
40873284 | 257 | spin_unlock(&group->notification_lock); |
fabf7f29 JK |
258 | if (destroy) |
259 | fsnotify_destroy_event(group, &event->fae.fse); | |
40873284 JK |
260 | } |
261 | ||
b2d87909 EP |
262 | static int process_access_response(struct fsnotify_group *group, |
263 | struct fanotify_response *response_struct) | |
264 | { | |
33913997 | 265 | struct fanotify_perm_event *event; |
f083441b JK |
266 | int fd = response_struct->fd; |
267 | int response = response_struct->response; | |
b2d87909 EP |
268 | |
269 | pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group, | |
270 | fd, response); | |
271 | /* | |
272 | * make sure the response is valid, if invalid we do nothing and either | |
25985edc | 273 | * userspace can send a valid response or we will clean it up after the |
b2d87909 EP |
274 | * timeout |
275 | */ | |
de8cd83e | 276 | switch (response & ~FAN_AUDIT) { |
b2d87909 EP |
277 | case FAN_ALLOW: |
278 | case FAN_DENY: | |
279 | break; | |
280 | default: | |
281 | return -EINVAL; | |
282 | } | |
283 | ||
284 | if (fd < 0) | |
285 | return -EINVAL; | |
286 | ||
96a71f21 | 287 | if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT)) |
de8cd83e SG |
288 | return -EINVAL; |
289 | ||
af6a5113 JK |
290 | spin_lock(&group->notification_lock); |
291 | list_for_each_entry(event, &group->fanotify_data.access_list, | |
292 | fae.fse.list) { | |
293 | if (event->fd != fd) | |
294 | continue; | |
b2d87909 | 295 | |
af6a5113 | 296 | list_del_init(&event->fae.fse.list); |
40873284 | 297 | finish_permission_event(group, event, response); |
af6a5113 JK |
298 | wake_up(&group->fanotify_data.access_waitq); |
299 | return 0; | |
300 | } | |
301 | spin_unlock(&group->notification_lock); | |
b2d87909 | 302 | |
af6a5113 | 303 | return -ENOENT; |
b2d87909 | 304 | } |
b2d87909 | 305 | |
44d705b0 | 306 | static int copy_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh, |
83b7a598 | 307 | int info_type, const char *name, size_t name_len, |
44d705b0 | 308 | char __user *buf, size_t count) |
5e469c83 AG |
309 | { |
310 | struct fanotify_event_info_fid info = { }; | |
311 | struct file_handle handle = { }; | |
afc894c7 | 312 | unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf; |
cacfb956 | 313 | size_t fh_len = fh ? fh->len : 0; |
44d705b0 AG |
314 | size_t info_len = fanotify_fid_info_len(fh_len, name_len); |
315 | size_t len = info_len; | |
5e469c83 | 316 | |
44d705b0 AG |
317 | pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n", |
318 | __func__, fh_len, name_len, info_len, count); | |
319 | ||
83b7a598 | 320 | if (!fh_len) |
5e469c83 AG |
321 | return 0; |
322 | ||
44d705b0 | 323 | if (WARN_ON_ONCE(len < sizeof(info) || len > count)) |
5e469c83 AG |
324 | return -EFAULT; |
325 | ||
44d705b0 AG |
326 | /* |
327 | * Copy event info fid header followed by variable sized file handle | |
328 | * and optionally followed by variable sized filename. | |
329 | */ | |
83b7a598 AG |
330 | switch (info_type) { |
331 | case FAN_EVENT_INFO_TYPE_FID: | |
332 | case FAN_EVENT_INFO_TYPE_DFID: | |
333 | if (WARN_ON_ONCE(name_len)) | |
334 | return -EFAULT; | |
335 | break; | |
336 | case FAN_EVENT_INFO_TYPE_DFID_NAME: | |
337 | if (WARN_ON_ONCE(!name || !name_len)) | |
338 | return -EFAULT; | |
339 | break; | |
340 | default: | |
341 | return -EFAULT; | |
342 | } | |
343 | ||
344 | info.hdr.info_type = info_type; | |
5e469c83 | 345 | info.hdr.len = len; |
d766b553 | 346 | info.fsid = *fsid; |
5e469c83 AG |
347 | if (copy_to_user(buf, &info, sizeof(info))) |
348 | return -EFAULT; | |
349 | ||
350 | buf += sizeof(info); | |
351 | len -= sizeof(info); | |
44d705b0 AG |
352 | if (WARN_ON_ONCE(len < sizeof(handle))) |
353 | return -EFAULT; | |
354 | ||
afc894c7 | 355 | handle.handle_type = fh->type; |
5e469c83 AG |
356 | handle.handle_bytes = fh_len; |
357 | if (copy_to_user(buf, &handle, sizeof(handle))) | |
358 | return -EFAULT; | |
359 | ||
360 | buf += sizeof(handle); | |
361 | len -= sizeof(handle); | |
44d705b0 AG |
362 | if (WARN_ON_ONCE(len < fh_len)) |
363 | return -EFAULT; | |
364 | ||
b2d22b6b | 365 | /* |
44d705b0 AG |
366 | * For an inline fh and inline file name, copy through stack to exclude |
367 | * the copy from usercopy hardening protections. | |
b2d22b6b | 368 | */ |
afc894c7 | 369 | fh_buf = fanotify_fh_buf(fh); |
b2d22b6b | 370 | if (fh_len <= FANOTIFY_INLINE_FH_LEN) { |
afc894c7 JK |
371 | memcpy(bounce, fh_buf, fh_len); |
372 | fh_buf = bounce; | |
b2d22b6b | 373 | } |
afc894c7 | 374 | if (copy_to_user(buf, fh_buf, fh_len)) |
5e469c83 AG |
375 | return -EFAULT; |
376 | ||
5e469c83 AG |
377 | buf += fh_len; |
378 | len -= fh_len; | |
44d705b0 AG |
379 | |
380 | if (name_len) { | |
381 | /* Copy the filename with terminating null */ | |
382 | name_len++; | |
383 | if (WARN_ON_ONCE(len < name_len)) | |
384 | return -EFAULT; | |
385 | ||
386 | if (copy_to_user(buf, name, name_len)) | |
387 | return -EFAULT; | |
388 | ||
389 | buf += name_len; | |
390 | len -= name_len; | |
391 | } | |
392 | ||
393 | /* Pad with 0's */ | |
5e469c83 AG |
394 | WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN); |
395 | if (len > 0 && clear_user(buf, len)) | |
396 | return -EFAULT; | |
397 | ||
44d705b0 | 398 | return info_len; |
5e469c83 AG |
399 | } |
400 | ||
a1014f10 | 401 | static ssize_t copy_event_to_user(struct fsnotify_group *group, |
7088f357 | 402 | struct fanotify_event *event, |
5b03a472 | 403 | char __user *buf, size_t count) |
a1014f10 | 404 | { |
bb2f7b45 | 405 | struct fanotify_event_metadata metadata; |
7088f357 | 406 | struct path *path = fanotify_event_path(event); |
f454fa61 | 407 | struct fanotify_info *info = fanotify_event_info(event); |
83b7a598 | 408 | unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); |
bb2f7b45 | 409 | struct file *f = NULL; |
e9e0c890 | 410 | int ret, fd = FAN_NOFD; |
83b7a598 | 411 | int info_type = 0; |
a1014f10 | 412 | |
7088f357 | 413 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
a1014f10 | 414 | |
44d705b0 | 415 | metadata.event_len = FAN_EVENT_METADATA_LEN + |
929943b3 | 416 | fanotify_event_info_len(fid_mode, event); |
bb2f7b45 AG |
417 | metadata.metadata_len = FAN_EVENT_METADATA_LEN; |
418 | metadata.vers = FANOTIFY_METADATA_VERSION; | |
419 | metadata.reserved = 0; | |
420 | metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS; | |
421 | metadata.pid = pid_vnr(event->pid); | |
7cea2a3c AG |
422 | /* |
423 | * For an unprivileged listener, event->pid can be used to identify the | |
424 | * events generated by the listener process itself, without disclosing | |
425 | * the pids of other processes. | |
426 | */ | |
427 | if (!capable(CAP_SYS_ADMIN) && | |
428 | task_tgid(current) != event->pid) | |
429 | metadata.pid = 0; | |
bb2f7b45 | 430 | |
44d705b0 | 431 | if (path && path->mnt && path->dentry) { |
afc894c7 JK |
432 | fd = create_fd(group, path, &f); |
433 | if (fd < 0) | |
434 | return fd; | |
bb2f7b45 AG |
435 | } |
436 | metadata.fd = fd; | |
b2d87909 | 437 | |
b2d87909 | 438 | ret = -EFAULT; |
5b03a472 KC |
439 | /* |
440 | * Sanity check copy size in case get_one_event() and | |
c5e443cb | 441 | * event_len sizes ever get out of sync. |
5b03a472 | 442 | */ |
bb2f7b45 | 443 | if (WARN_ON_ONCE(metadata.event_len > count)) |
5b03a472 | 444 | goto out_close_fd; |
bb2f7b45 | 445 | |
5e469c83 | 446 | if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN)) |
352e3b24 AV |
447 | goto out_close_fd; |
448 | ||
44d705b0 AG |
449 | buf += FAN_EVENT_METADATA_LEN; |
450 | count -= FAN_EVENT_METADATA_LEN; | |
451 | ||
bb2f7b45 | 452 | if (fanotify_is_perm_event(event->mask)) |
7088f357 | 453 | FANOTIFY_PERM(event)->fd = fd; |
a1014f10 | 454 | |
44d705b0 | 455 | if (f) |
3587b1b0 | 456 | fd_install(fd, f); |
44d705b0 AG |
457 | |
458 | /* Event info records order is: dir fid + name, child fid */ | |
f454fa61 | 459 | if (fanotify_event_dir_fh_len(event)) { |
691d9763 AG |
460 | info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME : |
461 | FAN_EVENT_INFO_TYPE_DFID; | |
44d705b0 | 462 | ret = copy_info_to_user(fanotify_event_fsid(event), |
f454fa61 | 463 | fanotify_info_dir_fh(info), |
83b7a598 | 464 | info_type, fanotify_info_name(info), |
f454fa61 | 465 | info->name_len, buf, count); |
5e469c83 AG |
466 | if (ret < 0) |
467 | return ret; | |
44d705b0 AG |
468 | |
469 | buf += ret; | |
470 | count -= ret; | |
471 | } | |
472 | ||
473 | if (fanotify_event_object_fh_len(event)) { | |
929943b3 AG |
474 | const char *dot = NULL; |
475 | int dot_len = 0; | |
476 | ||
83b7a598 AG |
477 | if (fid_mode == FAN_REPORT_FID || info_type) { |
478 | /* | |
479 | * With only group flag FAN_REPORT_FID only type FID is | |
480 | * reported. Second info record type is always FID. | |
481 | */ | |
482 | info_type = FAN_EVENT_INFO_TYPE_FID; | |
929943b3 AG |
483 | } else if ((fid_mode & FAN_REPORT_NAME) && |
484 | (event->mask & FAN_ONDIR)) { | |
485 | /* | |
486 | * With group flag FAN_REPORT_NAME, if name was not | |
487 | * recorded in an event on a directory, report the | |
488 | * name "." with info type DFID_NAME. | |
489 | */ | |
490 | info_type = FAN_EVENT_INFO_TYPE_DFID_NAME; | |
491 | dot = "."; | |
492 | dot_len = 1; | |
83b7a598 AG |
493 | } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) || |
494 | (event->mask & FAN_ONDIR)) { | |
495 | /* | |
496 | * With group flag FAN_REPORT_DIR_FID, a single info | |
497 | * record has type DFID for directory entry modification | |
498 | * event and for event on a directory. | |
499 | */ | |
500 | info_type = FAN_EVENT_INFO_TYPE_DFID; | |
501 | } else { | |
502 | /* | |
503 | * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID, | |
504 | * a single info record has type FID for event on a | |
505 | * non-directory, when there is no directory to report. | |
506 | * For example, on FAN_DELETE_SELF event. | |
507 | */ | |
508 | info_type = FAN_EVENT_INFO_TYPE_FID; | |
509 | } | |
510 | ||
44d705b0 AG |
511 | ret = copy_info_to_user(fanotify_event_fsid(event), |
512 | fanotify_event_object_fh(event), | |
929943b3 | 513 | info_type, dot, dot_len, buf, count); |
44d705b0 AG |
514 | if (ret < 0) |
515 | return ret; | |
516 | ||
517 | buf += ret; | |
518 | count -= ret; | |
5e469c83 AG |
519 | } |
520 | ||
bb2f7b45 | 521 | return metadata.event_len; |
b2d87909 | 522 | |
b2d87909 | 523 | out_close_fd: |
352e3b24 AV |
524 | if (fd != FAN_NOFD) { |
525 | put_unused_fd(fd); | |
526 | fput(f); | |
527 | } | |
b2d87909 | 528 | return ret; |
a1014f10 EP |
529 | } |
530 | ||
531 | /* intofiy userspace file descriptor functions */ | |
076ccb76 | 532 | static __poll_t fanotify_poll(struct file *file, poll_table *wait) |
a1014f10 EP |
533 | { |
534 | struct fsnotify_group *group = file->private_data; | |
076ccb76 | 535 | __poll_t ret = 0; |
a1014f10 EP |
536 | |
537 | poll_wait(file, &group->notification_waitq, wait); | |
c21dbe20 | 538 | spin_lock(&group->notification_lock); |
a1014f10 | 539 | if (!fsnotify_notify_queue_is_empty(group)) |
a9a08845 | 540 | ret = EPOLLIN | EPOLLRDNORM; |
c21dbe20 | 541 | spin_unlock(&group->notification_lock); |
a1014f10 EP |
542 | |
543 | return ret; | |
544 | } | |
545 | ||
546 | static ssize_t fanotify_read(struct file *file, char __user *buf, | |
547 | size_t count, loff_t *pos) | |
548 | { | |
549 | struct fsnotify_group *group; | |
7088f357 | 550 | struct fanotify_event *event; |
a1014f10 EP |
551 | char __user *start; |
552 | int ret; | |
536ebe9c | 553 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
a1014f10 EP |
554 | |
555 | start = buf; | |
556 | group = file->private_data; | |
557 | ||
558 | pr_debug("%s: group=%p\n", __func__, group); | |
559 | ||
536ebe9c | 560 | add_wait_queue(&group->notification_waitq, &wait); |
a1014f10 | 561 | while (1) { |
47aaabde JK |
562 | /* |
563 | * User can supply arbitrarily large buffer. Avoid softlockups | |
564 | * in case there are lots of available events. | |
565 | */ | |
566 | cond_resched(); | |
7088f357 JK |
567 | event = get_one_event(group, count); |
568 | if (IS_ERR(event)) { | |
569 | ret = PTR_ERR(event); | |
d8aaab4f JK |
570 | break; |
571 | } | |
572 | ||
7088f357 | 573 | if (!event) { |
d8aaab4f JK |
574 | ret = -EAGAIN; |
575 | if (file->f_flags & O_NONBLOCK) | |
a1014f10 | 576 | break; |
d8aaab4f JK |
577 | |
578 | ret = -ERESTARTSYS; | |
579 | if (signal_pending(current)) | |
580 | break; | |
581 | ||
582 | if (start != buf) | |
a1014f10 | 583 | break; |
536ebe9c PZ |
584 | |
585 | wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); | |
a1014f10 EP |
586 | continue; |
587 | } | |
588 | ||
7088f357 | 589 | ret = copy_event_to_user(group, event, buf, count); |
4ff33aaf AG |
590 | if (unlikely(ret == -EOPENSTALE)) { |
591 | /* | |
592 | * We cannot report events with stale fd so drop it. | |
593 | * Setting ret to 0 will continue the event loop and | |
594 | * do the right thing if there are no more events to | |
595 | * read (i.e. return bytes read, -EAGAIN or wait). | |
596 | */ | |
597 | ret = 0; | |
598 | } | |
599 | ||
d8aaab4f JK |
600 | /* |
601 | * Permission events get queued to wait for response. Other | |
602 | * events can be destroyed now. | |
603 | */ | |
7088f357 JK |
604 | if (!fanotify_is_perm_event(event->mask)) { |
605 | fsnotify_destroy_event(group, &event->fse); | |
d507816b | 606 | } else { |
4ff33aaf | 607 | if (ret <= 0) { |
40873284 JK |
608 | spin_lock(&group->notification_lock); |
609 | finish_permission_event(group, | |
7088f357 | 610 | FANOTIFY_PERM(event), FAN_DENY); |
d507816b | 611 | wake_up(&group->fanotify_data.access_waitq); |
4ff33aaf AG |
612 | } else { |
613 | spin_lock(&group->notification_lock); | |
7088f357 | 614 | list_add_tail(&event->fse.list, |
4ff33aaf AG |
615 | &group->fanotify_data.access_list); |
616 | spin_unlock(&group->notification_lock); | |
d507816b | 617 | } |
d507816b | 618 | } |
4ff33aaf AG |
619 | if (ret < 0) |
620 | break; | |
d8aaab4f JK |
621 | buf += ret; |
622 | count -= ret; | |
a1014f10 | 623 | } |
536ebe9c | 624 | remove_wait_queue(&group->notification_waitq, &wait); |
a1014f10 | 625 | |
a1014f10 EP |
626 | if (start != buf && ret != -EFAULT) |
627 | ret = buf - start; | |
628 | return ret; | |
629 | } | |
630 | ||
b2d87909 EP |
631 | static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) |
632 | { | |
b2d87909 EP |
633 | struct fanotify_response response = { .fd = -1, .response = -1 }; |
634 | struct fsnotify_group *group; | |
635 | int ret; | |
636 | ||
6685df31 MS |
637 | if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) |
638 | return -EINVAL; | |
639 | ||
b2d87909 EP |
640 | group = file->private_data; |
641 | ||
5e23663b FF |
642 | if (count < sizeof(response)) |
643 | return -EINVAL; | |
644 | ||
645 | count = sizeof(response); | |
b2d87909 EP |
646 | |
647 | pr_debug("%s: group=%p count=%zu\n", __func__, group, count); | |
648 | ||
649 | if (copy_from_user(&response, buf, count)) | |
650 | return -EFAULT; | |
651 | ||
652 | ret = process_access_response(group, &response); | |
653 | if (ret < 0) | |
654 | count = ret; | |
655 | ||
656 | return count; | |
b2d87909 EP |
657 | } |
658 | ||
52c923dd EP |
659 | static int fanotify_release(struct inode *ignored, struct file *file) |
660 | { | |
661 | struct fsnotify_group *group = file->private_data; | |
6f73171e | 662 | struct fsnotify_event *fsn_event; |
19ba54f4 | 663 | |
5838d444 | 664 | /* |
96d41019 JK |
665 | * Stop new events from arriving in the notification queue. since |
666 | * userspace cannot use fanotify fd anymore, no event can enter or | |
667 | * leave access_list by now either. | |
5838d444 | 668 | */ |
96d41019 | 669 | fsnotify_group_stop_queueing(group); |
2eebf582 | 670 | |
96d41019 JK |
671 | /* |
672 | * Process all permission events on access_list and notification queue | |
673 | * and simulate reply from userspace. | |
674 | */ | |
073f6552 | 675 | spin_lock(&group->notification_lock); |
ca6f8699 | 676 | while (!list_empty(&group->fanotify_data.access_list)) { |
7088f357 JK |
677 | struct fanotify_perm_event *event; |
678 | ||
ca6f8699 JK |
679 | event = list_first_entry(&group->fanotify_data.access_list, |
680 | struct fanotify_perm_event, fae.fse.list); | |
f083441b | 681 | list_del_init(&event->fae.fse.list); |
40873284 JK |
682 | finish_permission_event(group, event, FAN_ALLOW); |
683 | spin_lock(&group->notification_lock); | |
2eebf582 | 684 | } |
2eebf582 | 685 | |
5838d444 | 686 | /* |
96d41019 JK |
687 | * Destroy all non-permission events. For permission events just |
688 | * dequeue them and set the response. They will be freed once the | |
689 | * response is consumed and fanotify_get_response() returns. | |
5838d444 | 690 | */ |
6f73171e AG |
691 | while ((fsn_event = fsnotify_remove_first_event(group))) { |
692 | struct fanotify_event *event = FANOTIFY_E(fsn_event); | |
7088f357 | 693 | |
7088f357 | 694 | if (!(event->mask & FANOTIFY_PERM_EVENTS)) { |
c21dbe20 | 695 | spin_unlock(&group->notification_lock); |
6f73171e | 696 | fsnotify_destroy_event(group, fsn_event); |
6685df31 | 697 | } else { |
7088f357 | 698 | finish_permission_event(group, FANOTIFY_PERM(event), |
40873284 | 699 | FAN_ALLOW); |
6685df31 | 700 | } |
40873284 | 701 | spin_lock(&group->notification_lock); |
96d41019 | 702 | } |
c21dbe20 | 703 | spin_unlock(&group->notification_lock); |
96d41019 JK |
704 | |
705 | /* Response for all permission events it set, wakeup waiters */ | |
2eebf582 | 706 | wake_up(&group->fanotify_data.access_waitq); |
0a6b6bd5 | 707 | |
52c923dd | 708 | /* matches the fanotify_init->fsnotify_alloc_group */ |
d8153d4d | 709 | fsnotify_destroy_group(group); |
52c923dd EP |
710 | |
711 | return 0; | |
712 | } | |
713 | ||
a1014f10 EP |
714 | static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
715 | { | |
716 | struct fsnotify_group *group; | |
7053aee2 | 717 | struct fsnotify_event *fsn_event; |
a1014f10 EP |
718 | void __user *p; |
719 | int ret = -ENOTTY; | |
720 | size_t send_len = 0; | |
721 | ||
722 | group = file->private_data; | |
723 | ||
724 | p = (void __user *) arg; | |
725 | ||
726 | switch (cmd) { | |
727 | case FIONREAD: | |
c21dbe20 | 728 | spin_lock(&group->notification_lock); |
7053aee2 | 729 | list_for_each_entry(fsn_event, &group->notification_list, list) |
a1014f10 | 730 | send_len += FAN_EVENT_METADATA_LEN; |
c21dbe20 | 731 | spin_unlock(&group->notification_lock); |
a1014f10 EP |
732 | ret = put_user(send_len, (int __user *) p); |
733 | break; | |
734 | } | |
735 | ||
736 | return ret; | |
737 | } | |
738 | ||
52c923dd | 739 | static const struct file_operations fanotify_fops = { |
be77196b | 740 | .show_fdinfo = fanotify_show_fdinfo, |
a1014f10 EP |
741 | .poll = fanotify_poll, |
742 | .read = fanotify_read, | |
b2d87909 | 743 | .write = fanotify_write, |
52c923dd EP |
744 | .fasync = NULL, |
745 | .release = fanotify_release, | |
a1014f10 | 746 | .unlocked_ioctl = fanotify_ioctl, |
1832f2d8 | 747 | .compat_ioctl = compat_ptr_ioctl, |
6038f373 | 748 | .llseek = noop_llseek, |
52c923dd EP |
749 | }; |
750 | ||
2a3edf86 | 751 | static int fanotify_find_path(int dfd, const char __user *filename, |
ac5656d8 AG |
752 | struct path *path, unsigned int flags, __u64 mask, |
753 | unsigned int obj_type) | |
2a3edf86 EP |
754 | { |
755 | int ret; | |
756 | ||
757 | pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__, | |
758 | dfd, filename, flags); | |
759 | ||
760 | if (filename == NULL) { | |
2903ff01 | 761 | struct fd f = fdget(dfd); |
2a3edf86 EP |
762 | |
763 | ret = -EBADF; | |
2903ff01 | 764 | if (!f.file) |
2a3edf86 EP |
765 | goto out; |
766 | ||
767 | ret = -ENOTDIR; | |
768 | if ((flags & FAN_MARK_ONLYDIR) && | |
496ad9aa | 769 | !(S_ISDIR(file_inode(f.file)->i_mode))) { |
2903ff01 | 770 | fdput(f); |
2a3edf86 EP |
771 | goto out; |
772 | } | |
773 | ||
2903ff01 | 774 | *path = f.file->f_path; |
2a3edf86 | 775 | path_get(path); |
2903ff01 | 776 | fdput(f); |
2a3edf86 EP |
777 | } else { |
778 | unsigned int lookup_flags = 0; | |
779 | ||
780 | if (!(flags & FAN_MARK_DONT_FOLLOW)) | |
781 | lookup_flags |= LOOKUP_FOLLOW; | |
782 | if (flags & FAN_MARK_ONLYDIR) | |
783 | lookup_flags |= LOOKUP_DIRECTORY; | |
784 | ||
785 | ret = user_path_at(dfd, filename, lookup_flags, path); | |
786 | if (ret) | |
787 | goto out; | |
788 | } | |
789 | ||
790 | /* you can only watch an inode if you have read permissions on it */ | |
02f92b38 | 791 | ret = path_permission(path, MAY_READ); |
ac5656d8 AG |
792 | if (ret) { |
793 | path_put(path); | |
794 | goto out; | |
795 | } | |
796 | ||
797 | ret = security_path_notify(path, mask, obj_type); | |
2a3edf86 EP |
798 | if (ret) |
799 | path_put(path); | |
ac5656d8 | 800 | |
2a3edf86 EP |
801 | out: |
802 | return ret; | |
803 | } | |
804 | ||
b9e4e3bd | 805 | static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, |
4ed6814a AG |
806 | __u32 mask, unsigned int flags, |
807 | __u32 umask, int *destroy) | |
088b09b0 | 808 | { |
d2c1874c | 809 | __u32 oldmask = 0; |
088b09b0 | 810 | |
4ed6814a AG |
811 | /* umask bits cannot be removed by user */ |
812 | mask &= ~umask; | |
088b09b0 | 813 | spin_lock(&fsn_mark->lock); |
b9e4e3bd EP |
814 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
815 | oldmask = fsn_mark->mask; | |
a72fd224 | 816 | fsn_mark->mask &= ~mask; |
b9e4e3bd | 817 | } else { |
a72fd224 | 818 | fsn_mark->ignored_mask &= ~mask; |
b9e4e3bd | 819 | } |
4ed6814a AG |
820 | /* |
821 | * We need to keep the mark around even if remaining mask cannot | |
822 | * result in any events (e.g. mask == FAN_ONDIR) to support incremenal | |
823 | * changes to the mask. | |
824 | * Destroy mark when only umask bits remain. | |
825 | */ | |
826 | *destroy = !((fsn_mark->mask | fsn_mark->ignored_mask) & ~umask); | |
088b09b0 AG |
827 | spin_unlock(&fsn_mark->lock); |
828 | ||
088b09b0 AG |
829 | return mask & oldmask; |
830 | } | |
831 | ||
eaa2c6b0 AG |
832 | static int fanotify_remove_mark(struct fsnotify_group *group, |
833 | fsnotify_connp_t *connp, __u32 mask, | |
4ed6814a | 834 | unsigned int flags, __u32 umask) |
88826276 EP |
835 | { |
836 | struct fsnotify_mark *fsn_mark = NULL; | |
088b09b0 | 837 | __u32 removed; |
6dfbd149 | 838 | int destroy_mark; |
88826276 | 839 | |
7b18527c | 840 | mutex_lock(&group->mark_mutex); |
eaa2c6b0 | 841 | fsn_mark = fsnotify_find_mark(connp, group); |
7b18527c LS |
842 | if (!fsn_mark) { |
843 | mutex_unlock(&group->mark_mutex); | |
f3640192 | 844 | return -ENOENT; |
7b18527c | 845 | } |
88826276 | 846 | |
6dfbd149 | 847 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags, |
4ed6814a | 848 | umask, &destroy_mark); |
3ac70bfc AG |
849 | if (removed & fsnotify_conn_mask(fsn_mark->connector)) |
850 | fsnotify_recalc_mask(fsn_mark->connector); | |
6dfbd149 | 851 | if (destroy_mark) |
4712e722 | 852 | fsnotify_detach_mark(fsn_mark); |
7b18527c | 853 | mutex_unlock(&group->mark_mutex); |
4712e722 JK |
854 | if (destroy_mark) |
855 | fsnotify_free_mark(fsn_mark); | |
6dfbd149 | 856 | |
eaa2c6b0 | 857 | /* matches the fsnotify_find_mark() */ |
f3640192 | 858 | fsnotify_put_mark(fsn_mark); |
f3640192 AG |
859 | return 0; |
860 | } | |
2a3edf86 | 861 | |
eaa2c6b0 AG |
862 | static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group, |
863 | struct vfsmount *mnt, __u32 mask, | |
4ed6814a | 864 | unsigned int flags, __u32 umask) |
eaa2c6b0 AG |
865 | { |
866 | return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks, | |
4ed6814a | 867 | mask, flags, umask); |
eaa2c6b0 AG |
868 | } |
869 | ||
d54f4fba | 870 | static int fanotify_remove_sb_mark(struct fsnotify_group *group, |
4ed6814a AG |
871 | struct super_block *sb, __u32 mask, |
872 | unsigned int flags, __u32 umask) | |
d54f4fba | 873 | { |
4ed6814a AG |
874 | return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask, |
875 | flags, umask); | |
d54f4fba AG |
876 | } |
877 | ||
f3640192 | 878 | static int fanotify_remove_inode_mark(struct fsnotify_group *group, |
b9e4e3bd | 879 | struct inode *inode, __u32 mask, |
4ed6814a | 880 | unsigned int flags, __u32 umask) |
f3640192 | 881 | { |
eaa2c6b0 | 882 | return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask, |
4ed6814a | 883 | flags, umask); |
2a3edf86 EP |
884 | } |
885 | ||
b9e4e3bd EP |
886 | static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, |
887 | __u32 mask, | |
888 | unsigned int flags) | |
912ee394 | 889 | { |
192ca4d1 | 890 | __u32 oldmask = -1; |
912ee394 AG |
891 | |
892 | spin_lock(&fsn_mark->lock); | |
b9e4e3bd EP |
893 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
894 | oldmask = fsn_mark->mask; | |
a72fd224 | 895 | fsn_mark->mask |= mask; |
b9e4e3bd | 896 | } else { |
a72fd224 | 897 | fsn_mark->ignored_mask |= mask; |
c9778a98 EP |
898 | if (flags & FAN_MARK_IGNORED_SURV_MODIFY) |
899 | fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY; | |
b9e4e3bd | 900 | } |
912ee394 AG |
901 | spin_unlock(&fsn_mark->lock); |
902 | ||
903 | return mask & ~oldmask; | |
904 | } | |
905 | ||
5e9c070c | 906 | static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, |
b812a9f5 | 907 | fsnotify_connp_t *connp, |
77115225 AG |
908 | unsigned int type, |
909 | __kernel_fsid_t *fsid) | |
5e9c070c | 910 | { |
5b8fea65 | 911 | struct ucounts *ucounts = group->fanotify_data.ucounts; |
5e9c070c LS |
912 | struct fsnotify_mark *mark; |
913 | int ret; | |
914 | ||
5b8fea65 AG |
915 | /* |
916 | * Enforce per user marks limits per user in all containing user ns. | |
917 | * A group with FAN_UNLIMITED_MARKS does not contribute to mark count | |
918 | * in the limited groups account. | |
919 | */ | |
920 | if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS) && | |
921 | !inc_ucount(ucounts->ns, ucounts->uid, UCOUNT_FANOTIFY_MARKS)) | |
5e9c070c LS |
922 | return ERR_PTR(-ENOSPC); |
923 | ||
924 | mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); | |
5b8fea65 AG |
925 | if (!mark) { |
926 | ret = -ENOMEM; | |
927 | goto out_dec_ucounts; | |
928 | } | |
5e9c070c | 929 | |
054c636e | 930 | fsnotify_init_mark(mark, group); |
77115225 | 931 | ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid); |
5e9c070c LS |
932 | if (ret) { |
933 | fsnotify_put_mark(mark); | |
5b8fea65 | 934 | goto out_dec_ucounts; |
5e9c070c LS |
935 | } |
936 | ||
937 | return mark; | |
5b8fea65 AG |
938 | |
939 | out_dec_ucounts: | |
940 | if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS)) | |
941 | dec_ucount(ucounts, UCOUNT_FANOTIFY_MARKS); | |
942 | return ERR_PTR(ret); | |
5e9c070c LS |
943 | } |
944 | ||
945 | ||
eaa2c6b0 AG |
946 | static int fanotify_add_mark(struct fsnotify_group *group, |
947 | fsnotify_connp_t *connp, unsigned int type, | |
77115225 AG |
948 | __u32 mask, unsigned int flags, |
949 | __kernel_fsid_t *fsid) | |
2a3edf86 EP |
950 | { |
951 | struct fsnotify_mark *fsn_mark; | |
912ee394 | 952 | __u32 added; |
2a3edf86 | 953 | |
7b18527c | 954 | mutex_lock(&group->mark_mutex); |
b812a9f5 | 955 | fsn_mark = fsnotify_find_mark(connp, group); |
88826276 | 956 | if (!fsn_mark) { |
77115225 | 957 | fsn_mark = fanotify_add_new_mark(group, connp, type, fsid); |
5e9c070c | 958 | if (IS_ERR(fsn_mark)) { |
7b18527c | 959 | mutex_unlock(&group->mark_mutex); |
5e9c070c | 960 | return PTR_ERR(fsn_mark); |
7b18527c | 961 | } |
88826276 | 962 | } |
b9e4e3bd | 963 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
3ac70bfc AG |
964 | if (added & ~fsnotify_conn_mask(fsn_mark->connector)) |
965 | fsnotify_recalc_mask(fsn_mark->connector); | |
c9747640 | 966 | mutex_unlock(&group->mark_mutex); |
5e9c070c | 967 | |
fa218ab9 | 968 | fsnotify_put_mark(fsn_mark); |
5e9c070c | 969 | return 0; |
88826276 EP |
970 | } |
971 | ||
eaa2c6b0 AG |
972 | static int fanotify_add_vfsmount_mark(struct fsnotify_group *group, |
973 | struct vfsmount *mnt, __u32 mask, | |
77115225 | 974 | unsigned int flags, __kernel_fsid_t *fsid) |
eaa2c6b0 AG |
975 | { |
976 | return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks, | |
77115225 | 977 | FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid); |
eaa2c6b0 AG |
978 | } |
979 | ||
d54f4fba | 980 | static int fanotify_add_sb_mark(struct fsnotify_group *group, |
77115225 AG |
981 | struct super_block *sb, __u32 mask, |
982 | unsigned int flags, __kernel_fsid_t *fsid) | |
d54f4fba AG |
983 | { |
984 | return fanotify_add_mark(group, &sb->s_fsnotify_marks, | |
77115225 | 985 | FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid); |
d54f4fba AG |
986 | } |
987 | ||
52202dfb | 988 | static int fanotify_add_inode_mark(struct fsnotify_group *group, |
b9e4e3bd | 989 | struct inode *inode, __u32 mask, |
77115225 | 990 | unsigned int flags, __kernel_fsid_t *fsid) |
88826276 | 991 | { |
88826276 | 992 | pr_debug("%s: group=%p inode=%p\n", __func__, group, inode); |
2a3edf86 | 993 | |
5322a59f EP |
994 | /* |
995 | * If some other task has this inode open for write we should not add | |
996 | * an ignored mark, unless that ignored mark is supposed to survive | |
997 | * modification changes anyway. | |
998 | */ | |
999 | if ((flags & FAN_MARK_IGNORED_MASK) && | |
1000 | !(flags & FAN_MARK_IGNORED_SURV_MODIFY) && | |
ac9498d6 | 1001 | inode_is_open_for_write(inode)) |
5322a59f EP |
1002 | return 0; |
1003 | ||
eaa2c6b0 | 1004 | return fanotify_add_mark(group, &inode->i_fsnotify_marks, |
77115225 | 1005 | FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid); |
88826276 | 1006 | } |
2a3edf86 | 1007 | |
b8a6c3a2 AG |
1008 | static struct fsnotify_event *fanotify_alloc_overflow_event(void) |
1009 | { | |
1010 | struct fanotify_event *oevent; | |
1011 | ||
1012 | oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT); | |
1013 | if (!oevent) | |
1014 | return NULL; | |
1015 | ||
1016 | fanotify_init_event(oevent, 0, FS_Q_OVERFLOW); | |
1017 | oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW; | |
1018 | ||
1019 | return &oevent->fse; | |
1020 | } | |
1021 | ||
94e00d28 AG |
1022 | static struct hlist_head *fanotify_alloc_merge_hash(void) |
1023 | { | |
1024 | struct hlist_head *hash; | |
1025 | ||
1026 | hash = kmalloc(sizeof(struct hlist_head) << FANOTIFY_HTABLE_BITS, | |
1027 | GFP_KERNEL_ACCOUNT); | |
1028 | if (!hash) | |
1029 | return NULL; | |
1030 | ||
1031 | __hash_init(hash, FANOTIFY_HTABLE_SIZE); | |
1032 | ||
1033 | return hash; | |
1034 | } | |
1035 | ||
52c923dd | 1036 | /* fanotify syscalls */ |
08ae8938 | 1037 | SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) |
11637e4b | 1038 | { |
52c923dd EP |
1039 | struct fsnotify_group *group; |
1040 | int f_flags, fd; | |
83b7a598 AG |
1041 | unsigned int fid_mode = flags & FANOTIFY_FID_BITS; |
1042 | unsigned int class = flags & FANOTIFY_CLASS_BITS; | |
52c923dd | 1043 | |
96a71f21 AG |
1044 | pr_debug("%s: flags=%x event_f_flags=%x\n", |
1045 | __func__, flags, event_f_flags); | |
52c923dd | 1046 | |
7cea2a3c AG |
1047 | if (!capable(CAP_SYS_ADMIN)) { |
1048 | /* | |
1049 | * An unprivileged user can setup an fanotify group with | |
1050 | * limited functionality - an unprivileged group is limited to | |
1051 | * notification events with file handles and it cannot use | |
1052 | * unlimited queue/marks. | |
1053 | */ | |
1054 | if ((flags & FANOTIFY_ADMIN_INIT_FLAGS) || !fid_mode) | |
1055 | return -EPERM; | |
1056 | } | |
52c923dd | 1057 | |
de8cd83e | 1058 | #ifdef CONFIG_AUDITSYSCALL |
23c9deeb | 1059 | if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT)) |
de8cd83e | 1060 | #else |
23c9deeb | 1061 | if (flags & ~FANOTIFY_INIT_FLAGS) |
de8cd83e | 1062 | #endif |
52c923dd EP |
1063 | return -EINVAL; |
1064 | ||
48149e9d HS |
1065 | if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS) |
1066 | return -EINVAL; | |
1067 | ||
1068 | switch (event_f_flags & O_ACCMODE) { | |
1069 | case O_RDONLY: | |
1070 | case O_RDWR: | |
1071 | case O_WRONLY: | |
1072 | break; | |
1073 | default: | |
1074 | return -EINVAL; | |
1075 | } | |
1076 | ||
83b7a598 | 1077 | if (fid_mode && class != FAN_CLASS_NOTIF) |
a8b13aa2 AG |
1078 | return -EINVAL; |
1079 | ||
929943b3 | 1080 | /* |
929943b3 | 1081 | * Child name is reported with parent fid so requires dir fid. |
691d9763 | 1082 | * We can report both child fid and dir fid with or without name. |
929943b3 | 1083 | */ |
691d9763 | 1084 | if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID)) |
83b7a598 | 1085 | return -EINVAL; |
83b7a598 | 1086 | |
b2d87909 | 1087 | f_flags = O_RDWR | FMODE_NONOTIFY; |
52c923dd EP |
1088 | if (flags & FAN_CLOEXEC) |
1089 | f_flags |= O_CLOEXEC; | |
1090 | if (flags & FAN_NONBLOCK) | |
1091 | f_flags |= O_NONBLOCK; | |
1092 | ||
1093 | /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ | |
ac7b79fd | 1094 | group = fsnotify_alloc_user_group(&fanotify_fsnotify_ops); |
26379198 | 1095 | if (IS_ERR(group)) { |
52c923dd | 1096 | return PTR_ERR(group); |
26379198 | 1097 | } |
52c923dd | 1098 | |
5b8fea65 AG |
1099 | /* Enforce groups limits per user in all containing user ns */ |
1100 | group->fanotify_data.ucounts = inc_ucount(current_user_ns(), | |
1101 | current_euid(), | |
1102 | UCOUNT_FANOTIFY_GROUPS); | |
1103 | if (!group->fanotify_data.ucounts) { | |
1104 | fd = -EMFILE; | |
1105 | goto out_destroy_group; | |
1106 | } | |
1107 | ||
96a71f21 | 1108 | group->fanotify_data.flags = flags; |
d46eb14b | 1109 | group->memcg = get_mem_cgroup_from_mm(current->mm); |
4afeff85 | 1110 | |
94e00d28 AG |
1111 | group->fanotify_data.merge_hash = fanotify_alloc_merge_hash(); |
1112 | if (!group->fanotify_data.merge_hash) { | |
1113 | fd = -ENOMEM; | |
1114 | goto out_destroy_group; | |
1115 | } | |
1116 | ||
b8a6c3a2 AG |
1117 | group->overflow_event = fanotify_alloc_overflow_event(); |
1118 | if (unlikely(!group->overflow_event)) { | |
ff57cd58 JK |
1119 | fd = -ENOMEM; |
1120 | goto out_destroy_group; | |
1121 | } | |
ff57cd58 | 1122 | |
1e2ee49f WW |
1123 | if (force_o_largefile()) |
1124 | event_f_flags |= O_LARGEFILE; | |
80af2588 | 1125 | group->fanotify_data.f_flags = event_f_flags; |
9e66e423 EP |
1126 | init_waitqueue_head(&group->fanotify_data.access_waitq); |
1127 | INIT_LIST_HEAD(&group->fanotify_data.access_list); | |
83b7a598 | 1128 | switch (class) { |
4231a235 EP |
1129 | case FAN_CLASS_NOTIF: |
1130 | group->priority = FS_PRIO_0; | |
1131 | break; | |
1132 | case FAN_CLASS_CONTENT: | |
1133 | group->priority = FS_PRIO_1; | |
1134 | break; | |
1135 | case FAN_CLASS_PRE_CONTENT: | |
1136 | group->priority = FS_PRIO_2; | |
1137 | break; | |
1138 | default: | |
1139 | fd = -EINVAL; | |
d8153d4d | 1140 | goto out_destroy_group; |
4231a235 | 1141 | } |
cb2d429f | 1142 | |
5dd03f55 EP |
1143 | if (flags & FAN_UNLIMITED_QUEUE) { |
1144 | fd = -EPERM; | |
1145 | if (!capable(CAP_SYS_ADMIN)) | |
d8153d4d | 1146 | goto out_destroy_group; |
5dd03f55 EP |
1147 | group->max_events = UINT_MAX; |
1148 | } else { | |
5b8fea65 | 1149 | group->max_events = fanotify_max_queued_events; |
5dd03f55 | 1150 | } |
2529a0df | 1151 | |
ac7e22dc EP |
1152 | if (flags & FAN_UNLIMITED_MARKS) { |
1153 | fd = -EPERM; | |
1154 | if (!capable(CAP_SYS_ADMIN)) | |
d8153d4d | 1155 | goto out_destroy_group; |
ac7e22dc | 1156 | } |
e7099d8a | 1157 | |
de8cd83e SG |
1158 | if (flags & FAN_ENABLE_AUDIT) { |
1159 | fd = -EPERM; | |
1160 | if (!capable(CAP_AUDIT_WRITE)) | |
1161 | goto out_destroy_group; | |
de8cd83e SG |
1162 | } |
1163 | ||
52c923dd EP |
1164 | fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags); |
1165 | if (fd < 0) | |
d8153d4d | 1166 | goto out_destroy_group; |
52c923dd EP |
1167 | |
1168 | return fd; | |
1169 | ||
d8153d4d LS |
1170 | out_destroy_group: |
1171 | fsnotify_destroy_group(group); | |
52c923dd | 1172 | return fd; |
11637e4b | 1173 | } |
bbaa4168 | 1174 | |
a8b13aa2 | 1175 | /* Check if filesystem can encode a unique fid */ |
73072283 | 1176 | static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid) |
a8b13aa2 | 1177 | { |
73072283 | 1178 | __kernel_fsid_t root_fsid; |
a8b13aa2 AG |
1179 | int err; |
1180 | ||
1181 | /* | |
1182 | * Make sure path is not in filesystem with zero fsid (e.g. tmpfs). | |
1183 | */ | |
73072283 | 1184 | err = vfs_get_fsid(path->dentry, fsid); |
a8b13aa2 AG |
1185 | if (err) |
1186 | return err; | |
1187 | ||
73072283 | 1188 | if (!fsid->val[0] && !fsid->val[1]) |
a8b13aa2 AG |
1189 | return -ENODEV; |
1190 | ||
1191 | /* | |
1192 | * Make sure path is not inside a filesystem subvolume (e.g. btrfs) | |
1193 | * which uses a different fsid than sb root. | |
1194 | */ | |
73072283 | 1195 | err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid); |
a8b13aa2 AG |
1196 | if (err) |
1197 | return err; | |
1198 | ||
73072283 AG |
1199 | if (root_fsid.val[0] != fsid->val[0] || |
1200 | root_fsid.val[1] != fsid->val[1]) | |
a8b13aa2 AG |
1201 | return -EXDEV; |
1202 | ||
1203 | /* | |
1204 | * We need to make sure that the file system supports at least | |
1205 | * encoding a file handle so user can use name_to_handle_at() to | |
1206 | * compare fid returned with event to the file handle of watched | |
1207 | * objects. However, name_to_handle_at() requires that the | |
1208 | * filesystem also supports decoding file handles. | |
1209 | */ | |
1210 | if (!path->dentry->d_sb->s_export_op || | |
1211 | !path->dentry->d_sb->s_export_op->fh_to_dentry) | |
1212 | return -EOPNOTSUPP; | |
1213 | ||
1214 | return 0; | |
1215 | } | |
1216 | ||
0b3b094a JK |
1217 | static int fanotify_events_supported(struct path *path, __u64 mask) |
1218 | { | |
1219 | /* | |
1220 | * Some filesystems such as 'proc' acquire unusual locks when opening | |
1221 | * files. For them fanotify permission events have high chances of | |
1222 | * deadlocking the system - open done when reporting fanotify event | |
1223 | * blocks on this "unusual" lock while another process holding the lock | |
1224 | * waits for fanotify permission event to be answered. Just disallow | |
1225 | * permission events for such filesystems. | |
1226 | */ | |
1227 | if (mask & FANOTIFY_PERM_EVENTS && | |
1228 | path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM) | |
1229 | return -EINVAL; | |
1230 | return 0; | |
1231 | } | |
1232 | ||
183caa3c DB |
1233 | static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask, |
1234 | int dfd, const char __user *pathname) | |
bbaa4168 | 1235 | { |
0ff21db9 EP |
1236 | struct inode *inode = NULL; |
1237 | struct vfsmount *mnt = NULL; | |
2a3edf86 | 1238 | struct fsnotify_group *group; |
2903ff01 | 1239 | struct fd f; |
2a3edf86 | 1240 | struct path path; |
73072283 | 1241 | __kernel_fsid_t __fsid, *fsid = NULL; |
bdd5a46f | 1242 | u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS; |
23c9deeb | 1243 | unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS; |
3ef86653 | 1244 | bool ignored = flags & FAN_MARK_IGNORED_MASK; |
d809daf1 | 1245 | unsigned int obj_type, fid_mode; |
85af5d92 | 1246 | u32 umask = 0; |
2903ff01 | 1247 | int ret; |
2a3edf86 EP |
1248 | |
1249 | pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n", | |
1250 | __func__, fanotify_fd, flags, dfd, pathname, mask); | |
1251 | ||
1252 | /* we only use the lower 32 bits as of right now. */ | |
22d483b9 | 1253 | if (upper_32_bits(mask)) |
2a3edf86 EP |
1254 | return -EINVAL; |
1255 | ||
23c9deeb | 1256 | if (flags & ~FANOTIFY_MARK_FLAGS) |
88380fe6 | 1257 | return -EINVAL; |
d54f4fba AG |
1258 | |
1259 | switch (mark_type) { | |
1260 | case FAN_MARK_INODE: | |
ac5656d8 AG |
1261 | obj_type = FSNOTIFY_OBJ_TYPE_INODE; |
1262 | break; | |
d54f4fba | 1263 | case FAN_MARK_MOUNT: |
ac5656d8 AG |
1264 | obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT; |
1265 | break; | |
d54f4fba | 1266 | case FAN_MARK_FILESYSTEM: |
ac5656d8 | 1267 | obj_type = FSNOTIFY_OBJ_TYPE_SB; |
d54f4fba AG |
1268 | break; |
1269 | default: | |
1270 | return -EINVAL; | |
1271 | } | |
1272 | ||
4d92604c | 1273 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
df561f66 | 1274 | case FAN_MARK_ADD: |
88380fe6 | 1275 | case FAN_MARK_REMOVE: |
1734dee4 LS |
1276 | if (!mask) |
1277 | return -EINVAL; | |
cc299a98 | 1278 | break; |
4d92604c | 1279 | case FAN_MARK_FLUSH: |
23c9deeb | 1280 | if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH)) |
cc299a98 | 1281 | return -EINVAL; |
88380fe6 AG |
1282 | break; |
1283 | default: | |
1284 | return -EINVAL; | |
1285 | } | |
8fcd6528 | 1286 | |
6685df31 | 1287 | if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) |
23c9deeb | 1288 | valid_mask |= FANOTIFY_PERM_EVENTS; |
6685df31 MS |
1289 | |
1290 | if (mask & ~valid_mask) | |
2a3edf86 EP |
1291 | return -EINVAL; |
1292 | ||
3ef86653 AG |
1293 | /* Event flags (ONDIR, ON_CHILD) are meaningless in ignored mask */ |
1294 | if (ignored) | |
1295 | mask &= ~FANOTIFY_EVENT_FLAGS; | |
1296 | ||
2903ff01 AV |
1297 | f = fdget(fanotify_fd); |
1298 | if (unlikely(!f.file)) | |
2a3edf86 EP |
1299 | return -EBADF; |
1300 | ||
1301 | /* verify that this is indeed an fanotify instance */ | |
1302 | ret = -EINVAL; | |
2903ff01 | 1303 | if (unlikely(f.file->f_op != &fanotify_fops)) |
2a3edf86 | 1304 | goto fput_and_out; |
2903ff01 | 1305 | group = f.file->private_data; |
4231a235 | 1306 | |
7cea2a3c AG |
1307 | /* |
1308 | * An unprivileged user is not allowed to watch a mount point nor | |
1309 | * a filesystem. | |
1310 | */ | |
1311 | ret = -EPERM; | |
1312 | if (!capable(CAP_SYS_ADMIN) && | |
1313 | mark_type != FAN_MARK_INODE) | |
1314 | goto fput_and_out; | |
1315 | ||
4231a235 EP |
1316 | /* |
1317 | * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not | |
1318 | * allowed to set permissions events. | |
1319 | */ | |
1320 | ret = -EINVAL; | |
23c9deeb | 1321 | if (mask & FANOTIFY_PERM_EVENTS && |
4231a235 EP |
1322 | group->priority == FS_PRIO_0) |
1323 | goto fput_and_out; | |
2a3edf86 | 1324 | |
235328d1 AG |
1325 | /* |
1326 | * Events with data type inode do not carry enough information to report | |
1327 | * event->fd, so we do not allow setting a mask for inode events unless | |
1328 | * group supports reporting fid. | |
1329 | * inode events are not supported on a mount mark, because they do not | |
1330 | * carry enough information (i.e. path) to be filtered by mount point. | |
1331 | */ | |
d809daf1 | 1332 | fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); |
235328d1 | 1333 | if (mask & FANOTIFY_INODE_EVENTS && |
d809daf1 | 1334 | (!fid_mode || mark_type == FAN_MARK_MOUNT)) |
235328d1 AG |
1335 | goto fput_and_out; |
1336 | ||
0a8dd2db HS |
1337 | if (flags & FAN_MARK_FLUSH) { |
1338 | ret = 0; | |
d54f4fba | 1339 | if (mark_type == FAN_MARK_MOUNT) |
0a8dd2db | 1340 | fsnotify_clear_vfsmount_marks_by_group(group); |
d54f4fba AG |
1341 | else if (mark_type == FAN_MARK_FILESYSTEM) |
1342 | fsnotify_clear_sb_marks_by_group(group); | |
0a8dd2db HS |
1343 | else |
1344 | fsnotify_clear_inode_marks_by_group(group); | |
1345 | goto fput_and_out; | |
1346 | } | |
1347 | ||
ac5656d8 AG |
1348 | ret = fanotify_find_path(dfd, pathname, &path, flags, |
1349 | (mask & ALL_FSNOTIFY_EVENTS), obj_type); | |
2a3edf86 EP |
1350 | if (ret) |
1351 | goto fput_and_out; | |
1352 | ||
0b3b094a JK |
1353 | if (flags & FAN_MARK_ADD) { |
1354 | ret = fanotify_events_supported(&path, mask); | |
1355 | if (ret) | |
1356 | goto path_put_and_out; | |
1357 | } | |
1358 | ||
d809daf1 | 1359 | if (fid_mode) { |
73072283 | 1360 | ret = fanotify_test_fid(&path, &__fsid); |
a8b13aa2 AG |
1361 | if (ret) |
1362 | goto path_put_and_out; | |
77115225 | 1363 | |
73072283 | 1364 | fsid = &__fsid; |
a8b13aa2 AG |
1365 | } |
1366 | ||
2a3edf86 | 1367 | /* inode held in place by reference to path; group by fget on fd */ |
d54f4fba | 1368 | if (mark_type == FAN_MARK_INODE) |
0ff21db9 EP |
1369 | inode = path.dentry->d_inode; |
1370 | else | |
1371 | mnt = path.mnt; | |
2a3edf86 | 1372 | |
85af5d92 AG |
1373 | /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */ |
1374 | if (mnt || !S_ISDIR(inode->i_mode)) { | |
1375 | mask &= ~FAN_EVENT_ON_CHILD; | |
1376 | umask = FAN_EVENT_ON_CHILD; | |
51280637 AG |
1377 | /* |
1378 | * If group needs to report parent fid, register for getting | |
1379 | * events with parent/name info for non-directory. | |
1380 | */ | |
1381 | if ((fid_mode & FAN_REPORT_DIR_FID) && | |
1382 | (flags & FAN_MARK_ADD) && !ignored) | |
1383 | mask |= FAN_EVENT_ON_CHILD; | |
85af5d92 AG |
1384 | } |
1385 | ||
2a3edf86 | 1386 | /* create/update an inode mark */ |
0a8dd2db | 1387 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) { |
c6223f46 | 1388 | case FAN_MARK_ADD: |
d54f4fba | 1389 | if (mark_type == FAN_MARK_MOUNT) |
77115225 AG |
1390 | ret = fanotify_add_vfsmount_mark(group, mnt, mask, |
1391 | flags, fsid); | |
d54f4fba | 1392 | else if (mark_type == FAN_MARK_FILESYSTEM) |
77115225 AG |
1393 | ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask, |
1394 | flags, fsid); | |
0ff21db9 | 1395 | else |
77115225 AG |
1396 | ret = fanotify_add_inode_mark(group, inode, mask, |
1397 | flags, fsid); | |
c6223f46 AG |
1398 | break; |
1399 | case FAN_MARK_REMOVE: | |
d54f4fba | 1400 | if (mark_type == FAN_MARK_MOUNT) |
77115225 | 1401 | ret = fanotify_remove_vfsmount_mark(group, mnt, mask, |
85af5d92 | 1402 | flags, umask); |
d54f4fba | 1403 | else if (mark_type == FAN_MARK_FILESYSTEM) |
77115225 | 1404 | ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask, |
85af5d92 | 1405 | flags, umask); |
f3640192 | 1406 | else |
77115225 | 1407 | ret = fanotify_remove_inode_mark(group, inode, mask, |
85af5d92 | 1408 | flags, umask); |
c6223f46 AG |
1409 | break; |
1410 | default: | |
1411 | ret = -EINVAL; | |
1412 | } | |
2a3edf86 | 1413 | |
a8b13aa2 | 1414 | path_put_and_out: |
2a3edf86 EP |
1415 | path_put(&path); |
1416 | fput_and_out: | |
2903ff01 | 1417 | fdput(f); |
2a3edf86 EP |
1418 | return ret; |
1419 | } | |
1420 | ||
2ca408d9 | 1421 | #ifndef CONFIG_ARCH_SPLIT_ARG64 |
183caa3c DB |
1422 | SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags, |
1423 | __u64, mask, int, dfd, | |
1424 | const char __user *, pathname) | |
1425 | { | |
1426 | return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname); | |
1427 | } | |
2ca408d9 | 1428 | #endif |
183caa3c | 1429 | |
2ca408d9 BG |
1430 | #if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT) |
1431 | SYSCALL32_DEFINE6(fanotify_mark, | |
91c2e0bc | 1432 | int, fanotify_fd, unsigned int, flags, |
2ca408d9 | 1433 | SC_ARG64(mask), int, dfd, |
91c2e0bc AV |
1434 | const char __user *, pathname) |
1435 | { | |
2ca408d9 BG |
1436 | return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask), |
1437 | dfd, pathname); | |
91c2e0bc AV |
1438 | } |
1439 | #endif | |
1440 | ||
2a3edf86 | 1441 | /* |
ae0e47f0 | 1442 | * fanotify_user_setup - Our initialization function. Note that we cannot return |
2a3edf86 EP |
1443 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here |
1444 | * must result in panic(). | |
1445 | */ | |
1446 | static int __init fanotify_user_setup(void) | |
1447 | { | |
5b8fea65 AG |
1448 | struct sysinfo si; |
1449 | int max_marks; | |
1450 | ||
1451 | si_meminfo(&si); | |
1452 | /* | |
1453 | * Allow up to 1% of addressable memory to be accounted for per user | |
1454 | * marks limited to the range [8192, 1048576]. mount and sb marks are | |
1455 | * a lot cheaper than inode marks, but there is no reason for a user | |
1456 | * to have many of those, so calculate by the cost of inode marks. | |
1457 | */ | |
1458 | max_marks = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) / | |
1459 | INODE_MARK_COST; | |
1460 | max_marks = clamp(max_marks, FANOTIFY_OLD_DEFAULT_MAX_MARKS, | |
1461 | FANOTIFY_DEFAULT_MAX_USER_MARKS); | |
1462 | ||
929943b3 | 1463 | BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 10); |
bdd5a46f AG |
1464 | BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9); |
1465 | ||
d46eb14b SB |
1466 | fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, |
1467 | SLAB_PANIC|SLAB_ACCOUNT); | |
7088f357 JK |
1468 | fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event, |
1469 | SLAB_PANIC); | |
1470 | fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event, | |
1471 | SLAB_PANIC); | |
6685df31 MS |
1472 | if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) { |
1473 | fanotify_perm_event_cachep = | |
33913997 | 1474 | KMEM_CACHE(fanotify_perm_event, SLAB_PANIC); |
6685df31 | 1475 | } |
2a3edf86 | 1476 | |
5b8fea65 AG |
1477 | fanotify_max_queued_events = FANOTIFY_DEFAULT_MAX_EVENTS; |
1478 | init_user_ns.ucount_max[UCOUNT_FANOTIFY_GROUPS] = | |
1479 | FANOTIFY_DEFAULT_MAX_GROUPS; | |
1480 | init_user_ns.ucount_max[UCOUNT_FANOTIFY_MARKS] = max_marks; | |
1481 | ||
2a3edf86 | 1482 | return 0; |
bbaa4168 | 1483 | } |
2a3edf86 | 1484 | device_initcall(fanotify_user_setup); |