1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/fsnotify_backend.h>
3 #include <linux/path.h>
4 #include <linux/slab.h>
5 #include <linux/exportfs.h>
6 #include <linux/hashtable.h>
7
8 extern struct kmem_cache *fanotify_mark_cache;
9 extern struct kmem_cache *fanotify_fid_event_cachep;
10 extern struct kmem_cache *fanotify_path_event_cachep;
11 extern struct kmem_cache *fanotify_perm_event_cachep;
12 extern struct kmem_cache *fanotify_mnt_event_cachep;
13
14 /* Possible states of the permission event */
15 enum {
16 FAN_EVENT_INIT,
17 FAN_EVENT_REPORTED,
18 FAN_EVENT_ANSWERED,
19 FAN_EVENT_CANCELED,
20 };
21
22 /*
23 * 3 dwords are sufficient for most local fs (64bit ino, 32bit generation).
24 * fh buf should be dword aligned. On 64bit arch, the ext_buf pointer is
25 * stored in either the first or last 2 dwords.
26 */
27 #define FANOTIFY_INLINE_FH_LEN (3 << 2)
28 #define FANOTIFY_FH_HDR_LEN offsetof(struct fanotify_fh, buf)
29
30 /* Fixed size struct for file handle */
31 struct fanotify_fh {
32 u8 type;
33 u8 len;
34 #define FANOTIFY_FH_FLAG_EXT_BUF 1
35 u8 flags;
36 u8 pad;
37 unsigned char buf[];
38 } __aligned(4);
39
40 /* Variable size struct for dir file handle + child file handle + name */
41 struct fanotify_info {
42 /* size of dir_fh/file_fh including fanotify_fh hdr size */
43 u8 dir_fh_totlen;
44 u8 dir2_fh_totlen;
45 u8 file_fh_totlen;
46 u8 name_len;
47 u8 name2_len;
48 u8 pad[3];
49 unsigned char buf[];
50 /*
51 * (struct fanotify_fh) dir_fh starts at buf[0]
52 * (optional) dir2_fh starts at buf[dir_fh_totlen]
53 * (optional) file_fh starts at buf[dir_fh_totlen + dir2_fh_totlen]
54 * name starts at buf[dir_fh_totlen + dir2_fh_totlen + file_fh_totlen]
55 * ...
56 */
57 #define FANOTIFY_DIR_FH_SIZE(info) ((info)->dir_fh_totlen)
58 #define FANOTIFY_DIR2_FH_SIZE(info) ((info)->dir2_fh_totlen)
59 #define FANOTIFY_FILE_FH_SIZE(info) ((info)->file_fh_totlen)
60 #define FANOTIFY_NAME_SIZE(info) ((info)->name_len + 1)
61 #define FANOTIFY_NAME2_SIZE(info) ((info)->name2_len + 1)
62
63 #define FANOTIFY_DIR_FH_OFFSET(info) 0
64 #define FANOTIFY_DIR2_FH_OFFSET(info) \
65 (FANOTIFY_DIR_FH_OFFSET(info) + FANOTIFY_DIR_FH_SIZE(info))
66 #define FANOTIFY_FILE_FH_OFFSET(info) \
67 (FANOTIFY_DIR2_FH_OFFSET(info) + FANOTIFY_DIR2_FH_SIZE(info))
68 #define FANOTIFY_NAME_OFFSET(info) \
69 (FANOTIFY_FILE_FH_OFFSET(info) + FANOTIFY_FILE_FH_SIZE(info))
70 #define FANOTIFY_NAME2_OFFSET(info) \
71 (FANOTIFY_NAME_OFFSET(info) + FANOTIFY_NAME_SIZE(info))
72
73 #define FANOTIFY_DIR_FH_BUF(info) \
74 ((info)->buf + FANOTIFY_DIR_FH_OFFSET(info))
75 #define FANOTIFY_DIR2_FH_BUF(info) \
76 ((info)->buf + FANOTIFY_DIR2_FH_OFFSET(info))
77 #define FANOTIFY_FILE_FH_BUF(info) \
78 ((info)->buf + FANOTIFY_FILE_FH_OFFSET(info))
79 #define FANOTIFY_NAME_BUF(info) \
80 ((info)->buf + FANOTIFY_NAME_OFFSET(info))
81 #define FANOTIFY_NAME2_BUF(info) \
82 ((info)->buf + FANOTIFY_NAME2_OFFSET(info))
83 } __aligned(4);
84
fanotify_fh_has_ext_buf(struct fanotify_fh * fh)85 static inline bool fanotify_fh_has_ext_buf(struct fanotify_fh *fh)
86 {
87 return (fh->flags & FANOTIFY_FH_FLAG_EXT_BUF);
88 }
89
fanotify_fh_ext_buf_ptr(struct fanotify_fh * fh)90 static inline char **fanotify_fh_ext_buf_ptr(struct fanotify_fh *fh)
91 {
92 BUILD_BUG_ON(FANOTIFY_FH_HDR_LEN % 4);
93 BUILD_BUG_ON(__alignof__(char *) - 4 + sizeof(char *) >
94 FANOTIFY_INLINE_FH_LEN);
95 return (char **)ALIGN((unsigned long)(fh->buf), __alignof__(char *));
96 }
97
fanotify_fh_ext_buf(struct fanotify_fh * fh)98 static inline void *fanotify_fh_ext_buf(struct fanotify_fh *fh)
99 {
100 return *fanotify_fh_ext_buf_ptr(fh);
101 }
102
fanotify_fh_buf(struct fanotify_fh * fh)103 static inline void *fanotify_fh_buf(struct fanotify_fh *fh)
104 {
105 return fanotify_fh_has_ext_buf(fh) ? fanotify_fh_ext_buf(fh) : fh->buf;
106 }
107
fanotify_info_dir_fh_len(struct fanotify_info * info)108 static inline int fanotify_info_dir_fh_len(struct fanotify_info *info)
109 {
110 if (!info->dir_fh_totlen ||
111 WARN_ON_ONCE(info->dir_fh_totlen < FANOTIFY_FH_HDR_LEN))
112 return 0;
113
114 return info->dir_fh_totlen - FANOTIFY_FH_HDR_LEN;
115 }
116
fanotify_info_dir_fh(struct fanotify_info * info)117 static inline struct fanotify_fh *fanotify_info_dir_fh(struct fanotify_info *info)
118 {
119 BUILD_BUG_ON(offsetof(struct fanotify_info, buf) % 4);
120
121 return (struct fanotify_fh *)FANOTIFY_DIR_FH_BUF(info);
122 }
123
fanotify_info_dir2_fh_len(struct fanotify_info * info)124 static inline int fanotify_info_dir2_fh_len(struct fanotify_info *info)
125 {
126 if (!info->dir2_fh_totlen ||
127 WARN_ON_ONCE(info->dir2_fh_totlen < FANOTIFY_FH_HDR_LEN))
128 return 0;
129
130 return info->dir2_fh_totlen - FANOTIFY_FH_HDR_LEN;
131 }
132
fanotify_info_dir2_fh(struct fanotify_info * info)133 static inline struct fanotify_fh *fanotify_info_dir2_fh(struct fanotify_info *info)
134 {
135 return (struct fanotify_fh *)FANOTIFY_DIR2_FH_BUF(info);
136 }
137
fanotify_info_file_fh_len(struct fanotify_info * info)138 static inline int fanotify_info_file_fh_len(struct fanotify_info *info)
139 {
140 if (!info->file_fh_totlen ||
141 WARN_ON_ONCE(info->file_fh_totlen < FANOTIFY_FH_HDR_LEN))
142 return 0;
143
144 return info->file_fh_totlen - FANOTIFY_FH_HDR_LEN;
145 }
146
fanotify_info_file_fh(struct fanotify_info * info)147 static inline struct fanotify_fh *fanotify_info_file_fh(struct fanotify_info *info)
148 {
149 return (struct fanotify_fh *)FANOTIFY_FILE_FH_BUF(info);
150 }
151
fanotify_info_name(struct fanotify_info * info)152 static inline char *fanotify_info_name(struct fanotify_info *info)
153 {
154 if (!info->name_len)
155 return NULL;
156
157 return FANOTIFY_NAME_BUF(info);
158 }
159
fanotify_info_name2(struct fanotify_info * info)160 static inline char *fanotify_info_name2(struct fanotify_info *info)
161 {
162 if (!info->name2_len)
163 return NULL;
164
165 return FANOTIFY_NAME2_BUF(info);
166 }
167
fanotify_info_init(struct fanotify_info * info)168 static inline void fanotify_info_init(struct fanotify_info *info)
169 {
170 BUILD_BUG_ON(FANOTIFY_FH_HDR_LEN + MAX_HANDLE_SZ > U8_MAX);
171 BUILD_BUG_ON(NAME_MAX > U8_MAX);
172
173 info->dir_fh_totlen = 0;
174 info->dir2_fh_totlen = 0;
175 info->file_fh_totlen = 0;
176 info->name_len = 0;
177 info->name2_len = 0;
178 }
179
180 /* These set/copy helpers MUST be called by order */
fanotify_info_set_dir_fh(struct fanotify_info * info,unsigned int totlen)181 static inline void fanotify_info_set_dir_fh(struct fanotify_info *info,
182 unsigned int totlen)
183 {
184 if (WARN_ON_ONCE(info->dir2_fh_totlen > 0) ||
185 WARN_ON_ONCE(info->file_fh_totlen > 0) ||
186 WARN_ON_ONCE(info->name_len > 0) ||
187 WARN_ON_ONCE(info->name2_len > 0))
188 return;
189
190 info->dir_fh_totlen = totlen;
191 }
192
fanotify_info_set_dir2_fh(struct fanotify_info * info,unsigned int totlen)193 static inline void fanotify_info_set_dir2_fh(struct fanotify_info *info,
194 unsigned int totlen)
195 {
196 if (WARN_ON_ONCE(info->file_fh_totlen > 0) ||
197 WARN_ON_ONCE(info->name_len > 0) ||
198 WARN_ON_ONCE(info->name2_len > 0))
199 return;
200
201 info->dir2_fh_totlen = totlen;
202 }
203
fanotify_info_set_file_fh(struct fanotify_info * info,unsigned int totlen)204 static inline void fanotify_info_set_file_fh(struct fanotify_info *info,
205 unsigned int totlen)
206 {
207 if (WARN_ON_ONCE(info->name_len > 0) ||
208 WARN_ON_ONCE(info->name2_len > 0))
209 return;
210
211 info->file_fh_totlen = totlen;
212 }
213
fanotify_info_copy_name(struct fanotify_info * info,const struct qstr * name)214 static inline void fanotify_info_copy_name(struct fanotify_info *info,
215 const struct qstr *name)
216 {
217 if (WARN_ON_ONCE(name->len > NAME_MAX) ||
218 WARN_ON_ONCE(info->name2_len > 0))
219 return;
220
221 info->name_len = name->len;
222 strcpy(fanotify_info_name(info), name->name);
223 }
224
fanotify_info_copy_name2(struct fanotify_info * info,const struct qstr * name)225 static inline void fanotify_info_copy_name2(struct fanotify_info *info,
226 const struct qstr *name)
227 {
228 if (WARN_ON_ONCE(name->len > NAME_MAX))
229 return;
230
231 info->name2_len = name->len;
232 strcpy(fanotify_info_name2(info), name->name);
233 }
234
235 /*
236 * Common structure for fanotify events. Concrete structs are allocated in
237 * fanotify_handle_event() and freed when the information is retrieved by
238 * userspace. The type of event determines how it was allocated, how it will
239 * be freed and which concrete struct it may be cast to.
240 */
241 enum fanotify_event_type {
242 FANOTIFY_EVENT_TYPE_FID, /* fixed length */
243 FANOTIFY_EVENT_TYPE_FID_NAME, /* variable length */
244 FANOTIFY_EVENT_TYPE_PATH,
245 FANOTIFY_EVENT_TYPE_PATH_PERM,
246 FANOTIFY_EVENT_TYPE_OVERFLOW, /* struct fanotify_event */
247 FANOTIFY_EVENT_TYPE_FS_ERROR, /* struct fanotify_error_event */
248 FANOTIFY_EVENT_TYPE_MNT,
249 __FANOTIFY_EVENT_TYPE_NUM
250 };
251
252 #define FANOTIFY_EVENT_TYPE_BITS \
253 (ilog2(__FANOTIFY_EVENT_TYPE_NUM - 1) + 1)
254 #define FANOTIFY_EVENT_HASH_BITS \
255 (32 - FANOTIFY_EVENT_TYPE_BITS)
256
257 struct fanotify_event {
258 struct fsnotify_event fse;
259 struct hlist_node merge_list; /* List for hashed merge */
260 u32 mask;
261 struct {
262 unsigned int type : FANOTIFY_EVENT_TYPE_BITS;
263 unsigned int hash : FANOTIFY_EVENT_HASH_BITS;
264 };
265 struct pid *pid;
266 };
267
fanotify_init_event(struct fanotify_event * event,unsigned int hash,u32 mask)268 static inline void fanotify_init_event(struct fanotify_event *event,
269 unsigned int hash, u32 mask)
270 {
271 fsnotify_init_event(&event->fse);
272 INIT_HLIST_NODE(&event->merge_list);
273 event->hash = hash;
274 event->mask = mask;
275 event->pid = NULL;
276 }
277
278 #define FANOTIFY_INLINE_FH(name, size) \
279 struct { \
280 struct fanotify_fh name; \
281 /* Space for object_fh.buf[] - access with fanotify_fh_buf() */ \
282 unsigned char _inline_fh_buf[size]; \
283 }
284
285 struct fanotify_fid_event {
286 struct fanotify_event fae;
287 __kernel_fsid_t fsid;
288
289 FANOTIFY_INLINE_FH(object_fh, FANOTIFY_INLINE_FH_LEN);
290 };
291
292 static inline struct fanotify_fid_event *
FANOTIFY_FE(struct fanotify_event * event)293 FANOTIFY_FE(struct fanotify_event *event)
294 {
295 return container_of(event, struct fanotify_fid_event, fae);
296 }
297
298 struct fanotify_name_event {
299 struct fanotify_event fae;
300 __kernel_fsid_t fsid;
301 struct fanotify_info info;
302 };
303
304 static inline struct fanotify_name_event *
FANOTIFY_NE(struct fanotify_event * event)305 FANOTIFY_NE(struct fanotify_event *event)
306 {
307 return container_of(event, struct fanotify_name_event, fae);
308 }
309
310 struct fanotify_error_event {
311 struct fanotify_event fae;
312 s32 error; /* Error reported by the Filesystem. */
313 u32 err_count; /* Suppressed errors count */
314
315 __kernel_fsid_t fsid; /* FSID this error refers to. */
316
317 FANOTIFY_INLINE_FH(object_fh, MAX_HANDLE_SZ);
318 };
319
320 static inline struct fanotify_error_event *
FANOTIFY_EE(struct fanotify_event * event)321 FANOTIFY_EE(struct fanotify_event *event)
322 {
323 return container_of(event, struct fanotify_error_event, fae);
324 }
325
fanotify_event_fsid(struct fanotify_event * event)326 static inline __kernel_fsid_t *fanotify_event_fsid(struct fanotify_event *event)
327 {
328 if (event->type == FANOTIFY_EVENT_TYPE_FID)
329 return &FANOTIFY_FE(event)->fsid;
330 else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
331 return &FANOTIFY_NE(event)->fsid;
332 else if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR)
333 return &FANOTIFY_EE(event)->fsid;
334 else
335 return NULL;
336 }
337
fanotify_event_object_fh(struct fanotify_event * event)338 static inline struct fanotify_fh *fanotify_event_object_fh(
339 struct fanotify_event *event)
340 {
341 if (event->type == FANOTIFY_EVENT_TYPE_FID)
342 return &FANOTIFY_FE(event)->object_fh;
343 else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
344 return fanotify_info_file_fh(&FANOTIFY_NE(event)->info);
345 else if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR)
346 return &FANOTIFY_EE(event)->object_fh;
347 else
348 return NULL;
349 }
350
fanotify_event_info(struct fanotify_event * event)351 static inline struct fanotify_info *fanotify_event_info(
352 struct fanotify_event *event)
353 {
354 if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
355 return &FANOTIFY_NE(event)->info;
356 else
357 return NULL;
358 }
359
fanotify_event_object_fh_len(struct fanotify_event * event)360 static inline int fanotify_event_object_fh_len(struct fanotify_event *event)
361 {
362 struct fanotify_info *info = fanotify_event_info(event);
363 struct fanotify_fh *fh = fanotify_event_object_fh(event);
364
365 if (info)
366 return info->file_fh_totlen ? fh->len : 0;
367 else
368 return fh ? fh->len : 0;
369 }
370
fanotify_event_dir_fh_len(struct fanotify_event * event)371 static inline int fanotify_event_dir_fh_len(struct fanotify_event *event)
372 {
373 struct fanotify_info *info = fanotify_event_info(event);
374
375 return info ? fanotify_info_dir_fh_len(info) : 0;
376 }
377
fanotify_event_dir2_fh_len(struct fanotify_event * event)378 static inline int fanotify_event_dir2_fh_len(struct fanotify_event *event)
379 {
380 struct fanotify_info *info = fanotify_event_info(event);
381
382 return info ? fanotify_info_dir2_fh_len(info) : 0;
383 }
384
fanotify_event_has_object_fh(struct fanotify_event * event)385 static inline bool fanotify_event_has_object_fh(struct fanotify_event *event)
386 {
387 /* For error events, even zeroed fh are reported. */
388 if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR)
389 return true;
390 return fanotify_event_object_fh_len(event) > 0;
391 }
392
fanotify_event_has_dir_fh(struct fanotify_event * event)393 static inline bool fanotify_event_has_dir_fh(struct fanotify_event *event)
394 {
395 return fanotify_event_dir_fh_len(event) > 0;
396 }
397
fanotify_event_has_dir2_fh(struct fanotify_event * event)398 static inline bool fanotify_event_has_dir2_fh(struct fanotify_event *event)
399 {
400 return fanotify_event_dir2_fh_len(event) > 0;
401 }
402
fanotify_event_has_any_dir_fh(struct fanotify_event * event)403 static inline bool fanotify_event_has_any_dir_fh(struct fanotify_event *event)
404 {
405 return fanotify_event_has_dir_fh(event) ||
406 fanotify_event_has_dir2_fh(event);
407 }
408
409 struct fanotify_path_event {
410 struct fanotify_event fae;
411 struct path path;
412 };
413
414 struct fanotify_mnt_event {
415 struct fanotify_event fae;
416 u64 mnt_id;
417 };
418
419 static inline struct fanotify_path_event *
FANOTIFY_PE(struct fanotify_event * event)420 FANOTIFY_PE(struct fanotify_event *event)
421 {
422 return container_of(event, struct fanotify_path_event, fae);
423 }
424
425 static inline struct fanotify_mnt_event *
FANOTIFY_ME(struct fanotify_event * event)426 FANOTIFY_ME(struct fanotify_event *event)
427 {
428 return container_of(event, struct fanotify_mnt_event, fae);
429 }
430
431 /*
432 * Structure for permission fanotify events. It gets allocated and freed in
433 * fanotify_handle_event() since we wait there for user response. When the
434 * information is retrieved by userspace the structure is moved from
435 * group->notification_list to group->fanotify_data.access_list to wait for
436 * user response.
437 */
438 struct fanotify_perm_event {
439 struct fanotify_event fae;
440 struct path path;
441 const loff_t *ppos; /* optional file range info */
442 size_t count;
443 u32 response; /* userspace answer to the event */
444 unsigned short state; /* state of the event */
445 int fd; /* fd we passed to userspace for this event */
446 union {
447 struct fanotify_response_info_header hdr;
448 struct fanotify_response_info_audit_rule audit_rule;
449 };
450 };
451
452 static inline struct fanotify_perm_event *
FANOTIFY_PERM(struct fanotify_event * event)453 FANOTIFY_PERM(struct fanotify_event *event)
454 {
455 return container_of(event, struct fanotify_perm_event, fae);
456 }
457
fanotify_is_perm_event(u32 mask)458 static inline bool fanotify_is_perm_event(u32 mask)
459 {
460 return IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS) &&
461 mask & FANOTIFY_PERM_EVENTS;
462 }
463
fanotify_event_has_access_range(struct fanotify_event * event)464 static inline bool fanotify_event_has_access_range(struct fanotify_event *event)
465 {
466 if (!(event->mask & FANOTIFY_PRE_CONTENT_EVENTS))
467 return false;
468
469 return FANOTIFY_PERM(event)->ppos;
470 }
471
FANOTIFY_E(struct fsnotify_event * fse)472 static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse)
473 {
474 return container_of(fse, struct fanotify_event, fse);
475 }
476
fanotify_is_error_event(u32 mask)477 static inline bool fanotify_is_error_event(u32 mask)
478 {
479 return mask & FAN_FS_ERROR;
480 }
481
fanotify_is_mnt_event(u32 mask)482 static inline bool fanotify_is_mnt_event(u32 mask)
483 {
484 return mask & (FAN_MNT_ATTACH | FAN_MNT_DETACH);
485 }
486
fanotify_event_path(struct fanotify_event * event)487 static inline const struct path *fanotify_event_path(struct fanotify_event *event)
488 {
489 if (event->type == FANOTIFY_EVENT_TYPE_PATH)
490 return &FANOTIFY_PE(event)->path;
491 else if (event->type == FANOTIFY_EVENT_TYPE_PATH_PERM)
492 return &FANOTIFY_PERM(event)->path;
493 else
494 return NULL;
495 }
496
497 /*
498 * Use 128 size hash table to speed up events merge.
499 */
500 #define FANOTIFY_HTABLE_BITS (7)
501 #define FANOTIFY_HTABLE_SIZE (1 << FANOTIFY_HTABLE_BITS)
502 #define FANOTIFY_HTABLE_MASK (FANOTIFY_HTABLE_SIZE - 1)
503
504 /*
505 * Permission events and overflow event do not get merged - don't hash them.
506 */
fanotify_is_hashed_event(u32 mask)507 static inline bool fanotify_is_hashed_event(u32 mask)
508 {
509 return !(fanotify_is_perm_event(mask) ||
510 fsnotify_is_overflow_event(mask));
511 }
512
fanotify_event_hash_bucket(struct fsnotify_group * group,struct fanotify_event * event)513 static inline unsigned int fanotify_event_hash_bucket(
514 struct fsnotify_group *group,
515 struct fanotify_event *event)
516 {
517 return event->hash & FANOTIFY_HTABLE_MASK;
518 }
519
520 struct fanotify_mark {
521 struct fsnotify_mark fsn_mark;
522 __kernel_fsid_t fsid;
523 };
524
FANOTIFY_MARK(struct fsnotify_mark * mark)525 static inline struct fanotify_mark *FANOTIFY_MARK(struct fsnotify_mark *mark)
526 {
527 return container_of(mark, struct fanotify_mark, fsn_mark);
528 }
529
fanotify_fsid_equal(__kernel_fsid_t * fsid1,__kernel_fsid_t * fsid2)530 static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1,
531 __kernel_fsid_t *fsid2)
532 {
533 return fsid1->val[0] == fsid2->val[0] && fsid1->val[1] == fsid2->val[1];
534 }
535
fanotify_mark_user_flags(struct fsnotify_mark * mark)536 static inline unsigned int fanotify_mark_user_flags(struct fsnotify_mark *mark)
537 {
538 unsigned int mflags = 0;
539
540 if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
541 mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
542 if (mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)
543 mflags |= FAN_MARK_EVICTABLE;
544 if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
545 mflags |= FAN_MARK_IGNORE;
546
547 return mflags;
548 }
549
fanotify_get_response_errno(int res)550 static inline u32 fanotify_get_response_errno(int res)
551 {
552 return (res >> FAN_ERRNO_SHIFT) & FAN_ERRNO_MASK;
553 }
554