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>
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;
13 /* Possible states of the permission event */
22 * 3 dwords are sufficient for most local fs (64bit ino, 32bit generation).
23 * fh buf should be dword aligned. On 64bit arch, the ext_buf pointer is
24 * stored in either the first or last 2 dwords.
26 #define FANOTIFY_INLINE_FH_LEN (3 << 2)
27 #define FANOTIFY_FH_HDR_LEN offsetof(struct fanotify_fh, buf)
29 /* Fixed size struct for file handle */
33 #define FANOTIFY_FH_FLAG_EXT_BUF 1
39 /* Variable size struct for dir file handle + child file handle + name */
40 struct fanotify_info {
41 /* size of dir_fh/file_fh including fanotify_fh hdr size */
50 * (struct fanotify_fh) dir_fh starts at buf[0]
51 * (optional) dir2_fh starts at buf[dir_fh_totlen]
52 * (optional) file_fh starts at buf[dir_fh_totlen + dir2_fh_totlen]
53 * name starts at buf[dir_fh_totlen + dir2_fh_totlen + file_fh_totlen]
56 #define FANOTIFY_DIR_FH_SIZE(info) ((info)->dir_fh_totlen)
57 #define FANOTIFY_DIR2_FH_SIZE(info) ((info)->dir2_fh_totlen)
58 #define FANOTIFY_FILE_FH_SIZE(info) ((info)->file_fh_totlen)
59 #define FANOTIFY_NAME_SIZE(info) ((info)->name_len + 1)
60 #define FANOTIFY_NAME2_SIZE(info) ((info)->name2_len + 1)
62 #define FANOTIFY_DIR_FH_OFFSET(info) 0
63 #define FANOTIFY_DIR2_FH_OFFSET(info) \
64 (FANOTIFY_DIR_FH_OFFSET(info) + FANOTIFY_DIR_FH_SIZE(info))
65 #define FANOTIFY_FILE_FH_OFFSET(info) \
66 (FANOTIFY_DIR2_FH_OFFSET(info) + FANOTIFY_DIR2_FH_SIZE(info))
67 #define FANOTIFY_NAME_OFFSET(info) \
68 (FANOTIFY_FILE_FH_OFFSET(info) + FANOTIFY_FILE_FH_SIZE(info))
69 #define FANOTIFY_NAME2_OFFSET(info) \
70 (FANOTIFY_NAME_OFFSET(info) + FANOTIFY_NAME_SIZE(info))
72 #define FANOTIFY_DIR_FH_BUF(info) \
73 ((info)->buf + FANOTIFY_DIR_FH_OFFSET(info))
74 #define FANOTIFY_DIR2_FH_BUF(info) \
75 ((info)->buf + FANOTIFY_DIR2_FH_OFFSET(info))
76 #define FANOTIFY_FILE_FH_BUF(info) \
77 ((info)->buf + FANOTIFY_FILE_FH_OFFSET(info))
78 #define FANOTIFY_NAME_BUF(info) \
79 ((info)->buf + FANOTIFY_NAME_OFFSET(info))
80 #define FANOTIFY_NAME2_BUF(info) \
81 ((info)->buf + FANOTIFY_NAME2_OFFSET(info))
84 static inline bool fanotify_fh_has_ext_buf(struct fanotify_fh *fh)
86 return (fh->flags & FANOTIFY_FH_FLAG_EXT_BUF);
89 static inline char **fanotify_fh_ext_buf_ptr(struct fanotify_fh *fh)
91 BUILD_BUG_ON(FANOTIFY_FH_HDR_LEN % 4);
92 BUILD_BUG_ON(__alignof__(char *) - 4 + sizeof(char *) >
93 FANOTIFY_INLINE_FH_LEN);
94 return (char **)ALIGN((unsigned long)(fh->buf), __alignof__(char *));
97 static inline void *fanotify_fh_ext_buf(struct fanotify_fh *fh)
99 return *fanotify_fh_ext_buf_ptr(fh);
102 static inline void *fanotify_fh_buf(struct fanotify_fh *fh)
104 return fanotify_fh_has_ext_buf(fh) ? fanotify_fh_ext_buf(fh) : fh->buf;
107 static inline int fanotify_info_dir_fh_len(struct fanotify_info *info)
109 if (!info->dir_fh_totlen ||
110 WARN_ON_ONCE(info->dir_fh_totlen < FANOTIFY_FH_HDR_LEN))
113 return info->dir_fh_totlen - FANOTIFY_FH_HDR_LEN;
116 static inline struct fanotify_fh *fanotify_info_dir_fh(struct fanotify_info *info)
118 BUILD_BUG_ON(offsetof(struct fanotify_info, buf) % 4);
120 return (struct fanotify_fh *)FANOTIFY_DIR_FH_BUF(info);
123 static inline int fanotify_info_dir2_fh_len(struct fanotify_info *info)
125 if (!info->dir2_fh_totlen ||
126 WARN_ON_ONCE(info->dir2_fh_totlen < FANOTIFY_FH_HDR_LEN))
129 return info->dir2_fh_totlen - FANOTIFY_FH_HDR_LEN;
132 static inline struct fanotify_fh *fanotify_info_dir2_fh(struct fanotify_info *info)
134 return (struct fanotify_fh *)FANOTIFY_DIR2_FH_BUF(info);
137 static inline int fanotify_info_file_fh_len(struct fanotify_info *info)
139 if (!info->file_fh_totlen ||
140 WARN_ON_ONCE(info->file_fh_totlen < FANOTIFY_FH_HDR_LEN))
143 return info->file_fh_totlen - FANOTIFY_FH_HDR_LEN;
146 static inline struct fanotify_fh *fanotify_info_file_fh(struct fanotify_info *info)
148 return (struct fanotify_fh *)FANOTIFY_FILE_FH_BUF(info);
151 static inline char *fanotify_info_name(struct fanotify_info *info)
156 return FANOTIFY_NAME_BUF(info);
159 static inline char *fanotify_info_name2(struct fanotify_info *info)
161 if (!info->name2_len)
164 return FANOTIFY_NAME2_BUF(info);
167 static inline void fanotify_info_init(struct fanotify_info *info)
169 BUILD_BUG_ON(FANOTIFY_FH_HDR_LEN + MAX_HANDLE_SZ > U8_MAX);
170 BUILD_BUG_ON(NAME_MAX > U8_MAX);
172 info->dir_fh_totlen = 0;
173 info->dir2_fh_totlen = 0;
174 info->file_fh_totlen = 0;
179 /* These set/copy helpers MUST be called by order */
180 static inline void fanotify_info_set_dir_fh(struct fanotify_info *info,
183 if (WARN_ON_ONCE(info->dir2_fh_totlen > 0) ||
184 WARN_ON_ONCE(info->file_fh_totlen > 0) ||
185 WARN_ON_ONCE(info->name_len > 0) ||
186 WARN_ON_ONCE(info->name2_len > 0))
189 info->dir_fh_totlen = totlen;
192 static inline void fanotify_info_set_dir2_fh(struct fanotify_info *info,
195 if (WARN_ON_ONCE(info->file_fh_totlen > 0) ||
196 WARN_ON_ONCE(info->name_len > 0) ||
197 WARN_ON_ONCE(info->name2_len > 0))
200 info->dir2_fh_totlen = totlen;
203 static inline void fanotify_info_set_file_fh(struct fanotify_info *info,
206 if (WARN_ON_ONCE(info->name_len > 0) ||
207 WARN_ON_ONCE(info->name2_len > 0))
210 info->file_fh_totlen = totlen;
213 static inline void fanotify_info_copy_name(struct fanotify_info *info,
214 const struct qstr *name)
216 if (WARN_ON_ONCE(name->len > NAME_MAX) ||
217 WARN_ON_ONCE(info->name2_len > 0))
220 info->name_len = name->len;
221 strcpy(fanotify_info_name(info), name->name);
224 static inline void fanotify_info_copy_name2(struct fanotify_info *info,
225 const struct qstr *name)
227 if (WARN_ON_ONCE(name->len > NAME_MAX))
230 info->name2_len = name->len;
231 strcpy(fanotify_info_name2(info), name->name);
235 * Common structure for fanotify events. Concrete structs are allocated in
236 * fanotify_handle_event() and freed when the information is retrieved by
237 * userspace. The type of event determines how it was allocated, how it will
238 * be freed and which concrete struct it may be cast to.
240 enum fanotify_event_type {
241 FANOTIFY_EVENT_TYPE_FID, /* fixed length */
242 FANOTIFY_EVENT_TYPE_FID_NAME, /* variable length */
243 FANOTIFY_EVENT_TYPE_PATH,
244 FANOTIFY_EVENT_TYPE_PATH_PERM,
245 FANOTIFY_EVENT_TYPE_OVERFLOW, /* struct fanotify_event */
246 FANOTIFY_EVENT_TYPE_FS_ERROR, /* struct fanotify_error_event */
247 __FANOTIFY_EVENT_TYPE_NUM
250 #define FANOTIFY_EVENT_TYPE_BITS \
251 (ilog2(__FANOTIFY_EVENT_TYPE_NUM - 1) + 1)
252 #define FANOTIFY_EVENT_HASH_BITS \
253 (32 - FANOTIFY_EVENT_TYPE_BITS)
255 struct fanotify_event {
256 struct fsnotify_event fse;
257 struct hlist_node merge_list; /* List for hashed merge */
260 unsigned int type : FANOTIFY_EVENT_TYPE_BITS;
261 unsigned int hash : FANOTIFY_EVENT_HASH_BITS;
266 static inline void fanotify_init_event(struct fanotify_event *event,
267 unsigned int hash, u32 mask)
269 fsnotify_init_event(&event->fse);
270 INIT_HLIST_NODE(&event->merge_list);
276 #define FANOTIFY_INLINE_FH(name, size) \
278 struct fanotify_fh (name); \
279 /* Space for object_fh.buf[] - access with fanotify_fh_buf() */ \
280 unsigned char _inline_fh_buf[(size)]; \
283 struct fanotify_fid_event {
284 struct fanotify_event fae;
285 __kernel_fsid_t fsid;
287 FANOTIFY_INLINE_FH(object_fh, FANOTIFY_INLINE_FH_LEN);
290 static inline struct fanotify_fid_event *
291 FANOTIFY_FE(struct fanotify_event *event)
293 return container_of(event, struct fanotify_fid_event, fae);
296 struct fanotify_name_event {
297 struct fanotify_event fae;
298 __kernel_fsid_t fsid;
299 struct fanotify_info info;
302 static inline struct fanotify_name_event *
303 FANOTIFY_NE(struct fanotify_event *event)
305 return container_of(event, struct fanotify_name_event, fae);
308 struct fanotify_error_event {
309 struct fanotify_event fae;
310 s32 error; /* Error reported by the Filesystem. */
311 u32 err_count; /* Suppressed errors count */
313 __kernel_fsid_t fsid; /* FSID this error refers to. */
315 FANOTIFY_INLINE_FH(object_fh, MAX_HANDLE_SZ);
318 static inline struct fanotify_error_event *
319 FANOTIFY_EE(struct fanotify_event *event)
321 return container_of(event, struct fanotify_error_event, fae);
324 static inline __kernel_fsid_t *fanotify_event_fsid(struct fanotify_event *event)
326 if (event->type == FANOTIFY_EVENT_TYPE_FID)
327 return &FANOTIFY_FE(event)->fsid;
328 else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
329 return &FANOTIFY_NE(event)->fsid;
330 else if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR)
331 return &FANOTIFY_EE(event)->fsid;
336 static inline struct fanotify_fh *fanotify_event_object_fh(
337 struct fanotify_event *event)
339 if (event->type == FANOTIFY_EVENT_TYPE_FID)
340 return &FANOTIFY_FE(event)->object_fh;
341 else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
342 return fanotify_info_file_fh(&FANOTIFY_NE(event)->info);
343 else if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR)
344 return &FANOTIFY_EE(event)->object_fh;
349 static inline struct fanotify_info *fanotify_event_info(
350 struct fanotify_event *event)
352 if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
353 return &FANOTIFY_NE(event)->info;
358 static inline int fanotify_event_object_fh_len(struct fanotify_event *event)
360 struct fanotify_info *info = fanotify_event_info(event);
361 struct fanotify_fh *fh = fanotify_event_object_fh(event);
364 return info->file_fh_totlen ? fh->len : 0;
366 return fh ? fh->len : 0;
369 static inline int fanotify_event_dir_fh_len(struct fanotify_event *event)
371 struct fanotify_info *info = fanotify_event_info(event);
373 return info ? fanotify_info_dir_fh_len(info) : 0;
376 static inline int fanotify_event_dir2_fh_len(struct fanotify_event *event)
378 struct fanotify_info *info = fanotify_event_info(event);
380 return info ? fanotify_info_dir2_fh_len(info) : 0;
383 static inline bool fanotify_event_has_object_fh(struct fanotify_event *event)
385 /* For error events, even zeroed fh are reported. */
386 if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR)
388 return fanotify_event_object_fh_len(event) > 0;
391 static inline bool fanotify_event_has_dir_fh(struct fanotify_event *event)
393 return fanotify_event_dir_fh_len(event) > 0;
396 static inline bool fanotify_event_has_dir2_fh(struct fanotify_event *event)
398 return fanotify_event_dir2_fh_len(event) > 0;
401 static inline bool fanotify_event_has_any_dir_fh(struct fanotify_event *event)
403 return fanotify_event_has_dir_fh(event) ||
404 fanotify_event_has_dir2_fh(event);
407 struct fanotify_path_event {
408 struct fanotify_event fae;
412 static inline struct fanotify_path_event *
413 FANOTIFY_PE(struct fanotify_event *event)
415 return container_of(event, struct fanotify_path_event, fae);
419 * Structure for permission fanotify events. It gets allocated and freed in
420 * fanotify_handle_event() since we wait there for user response. When the
421 * information is retrieved by userspace the structure is moved from
422 * group->notification_list to group->fanotify_data.access_list to wait for
425 struct fanotify_perm_event {
426 struct fanotify_event fae;
428 unsigned short response; /* userspace answer to the event */
429 unsigned short state; /* state of the event */
430 int fd; /* fd we passed to userspace for this event */
433 static inline struct fanotify_perm_event *
434 FANOTIFY_PERM(struct fanotify_event *event)
436 return container_of(event, struct fanotify_perm_event, fae);
439 static inline bool fanotify_is_perm_event(u32 mask)
441 return IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS) &&
442 mask & FANOTIFY_PERM_EVENTS;
445 static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse)
447 return container_of(fse, struct fanotify_event, fse);
450 static inline bool fanotify_is_error_event(u32 mask)
452 return mask & FAN_FS_ERROR;
455 static inline const struct path *fanotify_event_path(struct fanotify_event *event)
457 if (event->type == FANOTIFY_EVENT_TYPE_PATH)
458 return &FANOTIFY_PE(event)->path;
459 else if (event->type == FANOTIFY_EVENT_TYPE_PATH_PERM)
460 return &FANOTIFY_PERM(event)->path;
466 * Use 128 size hash table to speed up events merge.
468 #define FANOTIFY_HTABLE_BITS (7)
469 #define FANOTIFY_HTABLE_SIZE (1 << FANOTIFY_HTABLE_BITS)
470 #define FANOTIFY_HTABLE_MASK (FANOTIFY_HTABLE_SIZE - 1)
473 * Permission events and overflow event do not get merged - don't hash them.
475 static inline bool fanotify_is_hashed_event(u32 mask)
477 return !(fanotify_is_perm_event(mask) ||
478 fsnotify_is_overflow_event(mask));
481 static inline unsigned int fanotify_event_hash_bucket(
482 struct fsnotify_group *group,
483 struct fanotify_event *event)
485 return event->hash & FANOTIFY_HTABLE_MASK;
488 static inline unsigned int fanotify_mark_user_flags(struct fsnotify_mark *mark)
490 unsigned int mflags = 0;
492 if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
493 mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
494 if (mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)
495 mflags |= FAN_MARK_EVICTABLE;
496 if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
497 mflags |= FAN_MARK_IGNORE;