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>
7 extern struct kmem_cache *fanotify_mark_cache;
8 extern struct kmem_cache *fanotify_fid_event_cachep;
9 extern struct kmem_cache *fanotify_path_event_cachep;
10 extern struct kmem_cache *fanotify_perm_event_cachep;
12 /* Possible states of the permission event */
21 * 3 dwords are sufficient for most local fs (64bit ino, 32bit generation).
22 * fh buf should be dword aligned. On 64bit arch, the ext_buf pointer is
23 * stored in either the first or last 2 dwords.
25 #define FANOTIFY_INLINE_FH_LEN (3 << 2)
26 #define FANOTIFY_FH_HDR_LEN offsetof(struct fanotify_fh, buf)
28 /* Fixed size struct for file handle */
32 #define FANOTIFY_FH_FLAG_EXT_BUF 1
38 /* Variable size struct for dir file handle + child file handle + name */
39 struct fanotify_info {
40 /* size of dir_fh/file_fh including fanotify_fh hdr size */
47 * (struct fanotify_fh) dir_fh starts at buf[0]
48 * (optional) file_fh starts at buf[dir_fh_totlen]
49 * name starts at buf[dir_fh_totlen + file_fh_totlen]
53 static inline bool fanotify_fh_has_ext_buf(struct fanotify_fh *fh)
55 return (fh->flags & FANOTIFY_FH_FLAG_EXT_BUF);
58 static inline char **fanotify_fh_ext_buf_ptr(struct fanotify_fh *fh)
60 BUILD_BUG_ON(FANOTIFY_FH_HDR_LEN % 4);
61 BUILD_BUG_ON(__alignof__(char *) - 4 + sizeof(char *) >
62 FANOTIFY_INLINE_FH_LEN);
63 return (char **)ALIGN((unsigned long)(fh->buf), __alignof__(char *));
66 static inline void *fanotify_fh_ext_buf(struct fanotify_fh *fh)
68 return *fanotify_fh_ext_buf_ptr(fh);
71 static inline void *fanotify_fh_buf(struct fanotify_fh *fh)
73 return fanotify_fh_has_ext_buf(fh) ? fanotify_fh_ext_buf(fh) : fh->buf;
76 static inline int fanotify_info_dir_fh_len(struct fanotify_info *info)
78 if (!info->dir_fh_totlen ||
79 WARN_ON_ONCE(info->dir_fh_totlen < FANOTIFY_FH_HDR_LEN))
82 return info->dir_fh_totlen - FANOTIFY_FH_HDR_LEN;
85 static inline struct fanotify_fh *fanotify_info_dir_fh(struct fanotify_info *info)
87 BUILD_BUG_ON(offsetof(struct fanotify_info, buf) % 4);
89 return (struct fanotify_fh *)info->buf;
92 static inline int fanotify_info_file_fh_len(struct fanotify_info *info)
94 if (!info->file_fh_totlen ||
95 WARN_ON_ONCE(info->file_fh_totlen < FANOTIFY_FH_HDR_LEN))
98 return info->file_fh_totlen - FANOTIFY_FH_HDR_LEN;
101 static inline struct fanotify_fh *fanotify_info_file_fh(struct fanotify_info *info)
103 return (struct fanotify_fh *)(info->buf + info->dir_fh_totlen);
106 static inline const char *fanotify_info_name(struct fanotify_info *info)
108 return info->buf + info->dir_fh_totlen + info->file_fh_totlen;
111 static inline void fanotify_info_init(struct fanotify_info *info)
113 info->dir_fh_totlen = 0;
114 info->file_fh_totlen = 0;
118 static inline unsigned int fanotify_info_len(struct fanotify_info *info)
120 return info->dir_fh_totlen + info->file_fh_totlen + info->name_len;
123 static inline void fanotify_info_copy_name(struct fanotify_info *info,
124 const struct qstr *name)
126 info->name_len = name->len;
127 strcpy(info->buf + info->dir_fh_totlen + info->file_fh_totlen,
132 * Common structure for fanotify events. Concrete structs are allocated in
133 * fanotify_handle_event() and freed when the information is retrieved by
134 * userspace. The type of event determines how it was allocated, how it will
135 * be freed and which concrete struct it may be cast to.
137 enum fanotify_event_type {
138 FANOTIFY_EVENT_TYPE_FID, /* fixed length */
139 FANOTIFY_EVENT_TYPE_FID_NAME, /* variable length */
140 FANOTIFY_EVENT_TYPE_PATH,
141 FANOTIFY_EVENT_TYPE_PATH_PERM,
142 FANOTIFY_EVENT_TYPE_OVERFLOW, /* struct fanotify_event */
143 __FANOTIFY_EVENT_TYPE_NUM
146 #define FANOTIFY_EVENT_TYPE_BITS \
147 (ilog2(__FANOTIFY_EVENT_TYPE_NUM - 1) + 1)
148 #define FANOTIFY_EVENT_HASH_BITS \
149 (32 - FANOTIFY_EVENT_TYPE_BITS)
151 struct fanotify_event {
152 struct fsnotify_event fse;
155 unsigned int type : FANOTIFY_EVENT_TYPE_BITS;
156 unsigned int hash : FANOTIFY_EVENT_HASH_BITS;
161 static inline void fanotify_init_event(struct fanotify_event *event,
162 unsigned int hash, u32 mask)
164 fsnotify_init_event(&event->fse);
170 struct fanotify_fid_event {
171 struct fanotify_event fae;
172 __kernel_fsid_t fsid;
173 struct fanotify_fh object_fh;
174 /* Reserve space in object_fh.buf[] - access with fanotify_fh_buf() */
175 unsigned char _inline_fh_buf[FANOTIFY_INLINE_FH_LEN];
178 static inline struct fanotify_fid_event *
179 FANOTIFY_FE(struct fanotify_event *event)
181 return container_of(event, struct fanotify_fid_event, fae);
184 struct fanotify_name_event {
185 struct fanotify_event fae;
186 __kernel_fsid_t fsid;
187 struct fanotify_info info;
190 static inline struct fanotify_name_event *
191 FANOTIFY_NE(struct fanotify_event *event)
193 return container_of(event, struct fanotify_name_event, fae);
196 static inline __kernel_fsid_t *fanotify_event_fsid(struct fanotify_event *event)
198 if (event->type == FANOTIFY_EVENT_TYPE_FID)
199 return &FANOTIFY_FE(event)->fsid;
200 else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
201 return &FANOTIFY_NE(event)->fsid;
206 static inline struct fanotify_fh *fanotify_event_object_fh(
207 struct fanotify_event *event)
209 if (event->type == FANOTIFY_EVENT_TYPE_FID)
210 return &FANOTIFY_FE(event)->object_fh;
211 else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
212 return fanotify_info_file_fh(&FANOTIFY_NE(event)->info);
217 static inline struct fanotify_info *fanotify_event_info(
218 struct fanotify_event *event)
220 if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
221 return &FANOTIFY_NE(event)->info;
226 static inline int fanotify_event_object_fh_len(struct fanotify_event *event)
228 struct fanotify_info *info = fanotify_event_info(event);
229 struct fanotify_fh *fh = fanotify_event_object_fh(event);
232 return info->file_fh_totlen ? fh->len : 0;
234 return fh ? fh->len : 0;
237 static inline int fanotify_event_dir_fh_len(struct fanotify_event *event)
239 struct fanotify_info *info = fanotify_event_info(event);
241 return info ? fanotify_info_dir_fh_len(info) : 0;
244 struct fanotify_path_event {
245 struct fanotify_event fae;
249 static inline struct fanotify_path_event *
250 FANOTIFY_PE(struct fanotify_event *event)
252 return container_of(event, struct fanotify_path_event, fae);
256 * Structure for permission fanotify events. It gets allocated and freed in
257 * fanotify_handle_event() since we wait there for user response. When the
258 * information is retrieved by userspace the structure is moved from
259 * group->notification_list to group->fanotify_data.access_list to wait for
262 struct fanotify_perm_event {
263 struct fanotify_event fae;
265 unsigned short response; /* userspace answer to the event */
266 unsigned short state; /* state of the event */
267 int fd; /* fd we passed to userspace for this event */
270 static inline struct fanotify_perm_event *
271 FANOTIFY_PERM(struct fanotify_event *event)
273 return container_of(event, struct fanotify_perm_event, fae);
276 static inline bool fanotify_is_perm_event(u32 mask)
278 return IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS) &&
279 mask & FANOTIFY_PERM_EVENTS;
282 static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse)
284 return container_of(fse, struct fanotify_event, fse);
287 static inline bool fanotify_event_has_path(struct fanotify_event *event)
289 return event->type == FANOTIFY_EVENT_TYPE_PATH ||
290 event->type == FANOTIFY_EVENT_TYPE_PATH_PERM;
293 static inline struct path *fanotify_event_path(struct fanotify_event *event)
295 if (event->type == FANOTIFY_EVENT_TYPE_PATH)
296 return &FANOTIFY_PE(event)->path;
297 else if (event->type == FANOTIFY_EVENT_TYPE_PATH_PERM)
298 return &FANOTIFY_PERM(event)->path;