1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FS_NOTIFY_H
3 #define _LINUX_FS_NOTIFY_H
4 
5 /*
6  * include/linux/fsnotify.h - generic hooks for filesystem notification, to
7  * reduce in-source duplication from both dnotify and inotify.
8  *
9  * We don't compile any of this away in some complicated menagerie of ifdefs.
10  * Instead, we rely on the code inside to optimize away as needed.
11  *
12  * (C) Copyright 2005 Robert Love
13  */
14 
15 #include <linux/fsnotify_backend.h>
16 #include <linux/audit.h>
17 #include <linux/slab.h>
18 #include <linux/bug.h>
19 
20 #undef CREATE_TRACE_POINTS
21 #include <trace/hooks/fsnotify.h>
22 
23 /* Are there any inode/mount/sb objects watched with priority prio or above? */
fsnotify_sb_has_priority_watchers(struct super_block * sb,int prio)24 static inline bool fsnotify_sb_has_priority_watchers(struct super_block *sb,
25 						     int prio)
26 {
27 	struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(sb);
28 
29 	/* Were any marks ever added to any object on this sb? */
30 	if (!sbinfo)
31 		return false;
32 
33 	return atomic_long_read(&sbinfo->watched_objects[prio]);
34 }
35 
36 /* Are there any inode/mount/sb objects that are being watched at all? */
fsnotify_sb_has_watchers(struct super_block * sb)37 static inline bool fsnotify_sb_has_watchers(struct super_block *sb)
38 {
39 	return fsnotify_sb_has_priority_watchers(sb, 0);
40 }
41 
42 /*
43  * Notify this @dir inode about a change in a child directory entry.
44  * The directory entry may have turned positive or negative or its inode may
45  * have changed (i.e. renamed over).
46  *
47  * Unlike fsnotify_parent(), the event will be reported regardless of the
48  * FS_EVENT_ON_CHILD mask on the parent inode and will not be reported if only
49  * the child is interested and not the parent.
50  */
fsnotify_name(__u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * name,u32 cookie)51 static inline int fsnotify_name(__u32 mask, const void *data, int data_type,
52 				struct inode *dir, const struct qstr *name,
53 				u32 cookie)
54 {
55 	if (!fsnotify_sb_has_watchers(dir->i_sb))
56 		return 0;
57 
58 	return fsnotify(mask, data, data_type, dir, name, NULL, cookie);
59 }
60 
fsnotify_dirent(struct inode * dir,struct dentry * dentry,__u32 mask)61 static inline void fsnotify_dirent(struct inode *dir, struct dentry *dentry,
62 				   __u32 mask)
63 {
64 	fsnotify_name(mask, dentry, FSNOTIFY_EVENT_DENTRY, dir, &dentry->d_name, 0);
65 }
66 
fsnotify_inode(struct inode * inode,__u32 mask)67 static inline void fsnotify_inode(struct inode *inode, __u32 mask)
68 {
69 	if (!fsnotify_sb_has_watchers(inode->i_sb))
70 		return;
71 
72 	if (S_ISDIR(inode->i_mode))
73 		mask |= FS_ISDIR;
74 
75 	fsnotify(mask, inode, FSNOTIFY_EVENT_INODE, NULL, NULL, inode, 0);
76 }
77 
78 /* Notify this dentry's parent about a child's events. */
fsnotify_parent(struct dentry * dentry,__u32 mask,const void * data,int data_type)79 static inline int fsnotify_parent(struct dentry *dentry, __u32 mask,
80 				  const void *data, int data_type)
81 {
82 	struct inode *inode = d_inode(dentry);
83 
84 	if (!fsnotify_sb_has_watchers(inode->i_sb))
85 		return 0;
86 
87 	if (S_ISDIR(inode->i_mode)) {
88 		mask |= FS_ISDIR;
89 
90 		/* sb/mount marks are not interested in name of directory */
91 		if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
92 			goto notify_child;
93 	}
94 
95 	/* disconnected dentry cannot notify parent */
96 	if (IS_ROOT(dentry))
97 		goto notify_child;
98 
99 	return __fsnotify_parent(dentry, mask, data, data_type);
100 
101 notify_child:
102 	return fsnotify(mask, data, data_type, NULL, NULL, inode, 0);
103 }
104 
105 /*
106  * Simple wrappers to consolidate calls to fsnotify_parent() when an event
107  * is on a file/dentry.
108  */
fsnotify_dentry(struct dentry * dentry,__u32 mask)109 static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)
110 {
111 	fsnotify_parent(dentry, mask, dentry, FSNOTIFY_EVENT_DENTRY);
112 }
113 
fsnotify_file(struct file * file,__u32 mask)114 static inline int fsnotify_file(struct file *file, __u32 mask)
115 {
116 	const struct path *path;
117 
118 	/*
119 	 * FMODE_NONOTIFY are fds generated by fanotify itself which should not
120 	 * generate new events. We also don't want to generate events for
121 	 * FMODE_PATH fds (involves open & close events) as they are just
122 	 * handle creation / destruction events and not "real" file events.
123 	 */
124 	if (file->f_mode & (FMODE_NONOTIFY | FMODE_PATH))
125 		return 0;
126 
127 	path = &file->f_path;
128 	/* Permission events require group prio >= FSNOTIFY_PRIO_CONTENT */
129 	if (mask & ALL_FSNOTIFY_PERM_EVENTS &&
130 	    !fsnotify_sb_has_priority_watchers(path->dentry->d_sb,
131 					       FSNOTIFY_PRIO_CONTENT))
132 		return 0;
133 
134 	/*
135 	 * Open calls notify early on, so lower file system must be notified
136 	 */
137 	if (mask & FS_OPEN) {
138 		if (path->dentry->d_op &&
139 		    path->dentry->d_op->d_canonical_path) {
140 			struct path lower_path;
141 			int ret;
142 
143 			ret = path->dentry->d_op->d_canonical_path(path,
144 								   &lower_path);
145 			if (ret != -ENOSYS) {
146 				if (ret)
147 					return ret;
148 
149 				ret = fsnotify_parent(lower_path.dentry, mask,
150 						      &lower_path, FSNOTIFY_EVENT_PATH);
151 				path_put(&lower_path);
152 
153 				if (ret)
154 					return ret;
155 			}
156 		}
157 	}
158 
159 	return fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH);
160 }
161 
162 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
163 /*
164  * fsnotify_file_area_perm - permission hook before access to file range
165  */
fsnotify_file_area_perm(struct file * file,int perm_mask,const loff_t * ppos,size_t count)166 static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
167 					  const loff_t *ppos, size_t count)
168 {
169 	__u32 fsnotify_mask = FS_ACCESS_PERM;
170 
171 	/*
172 	 * filesystem may be modified in the context of permission events
173 	 * (e.g. by HSM filling a file on access), so sb freeze protection
174 	 * must not be held.
175 	 */
176 	lockdep_assert_once(file_write_not_started(file));
177 
178 	if (!(perm_mask & MAY_READ))
179 		return 0;
180 
181 	return fsnotify_file(file, fsnotify_mask);
182 }
183 
184 /*
185  * fsnotify_file_perm - permission hook before file access
186  */
fsnotify_file_perm(struct file * file,int perm_mask)187 static inline int fsnotify_file_perm(struct file *file, int perm_mask)
188 {
189 	return fsnotify_file_area_perm(file, perm_mask, NULL, 0);
190 }
191 
192 /*
193  * fsnotify_open_perm - permission hook before file open
194  */
fsnotify_open_perm(struct file * file)195 static inline int fsnotify_open_perm(struct file *file)
196 {
197 	int ret;
198 
199 	if (file->f_flags & __FMODE_EXEC) {
200 		ret = fsnotify_file(file, FS_OPEN_EXEC_PERM);
201 		if (ret)
202 			return ret;
203 	}
204 
205 	return fsnotify_file(file, FS_OPEN_PERM);
206 }
207 
208 #else
fsnotify_file_area_perm(struct file * file,int perm_mask,const loff_t * ppos,size_t count)209 static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
210 					  const loff_t *ppos, size_t count)
211 {
212 	return 0;
213 }
214 
fsnotify_file_perm(struct file * file,int perm_mask)215 static inline int fsnotify_file_perm(struct file *file, int perm_mask)
216 {
217 	return 0;
218 }
219 
fsnotify_open_perm(struct file * file)220 static inline int fsnotify_open_perm(struct file *file)
221 {
222 	return 0;
223 }
224 #endif
225 
226 /*
227  * fsnotify_link_count - inode's link count changed
228  */
fsnotify_link_count(struct inode * inode)229 static inline void fsnotify_link_count(struct inode *inode)
230 {
231 	fsnotify_inode(inode, FS_ATTRIB);
232 }
233 
234 /*
235  * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
236  */
fsnotify_move(struct inode * old_dir,struct inode * new_dir,const struct qstr * old_name,int isdir,struct inode * target,struct dentry * moved)237 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
238 				 const struct qstr *old_name,
239 				 int isdir, struct inode *target,
240 				 struct dentry *moved)
241 {
242 	struct inode *source = moved->d_inode;
243 	u32 fs_cookie = fsnotify_get_cookie();
244 	__u32 old_dir_mask = FS_MOVED_FROM;
245 	__u32 new_dir_mask = FS_MOVED_TO;
246 	__u32 rename_mask = FS_RENAME;
247 	const struct qstr *new_name = &moved->d_name;
248 
249 	if (isdir) {
250 		old_dir_mask |= FS_ISDIR;
251 		new_dir_mask |= FS_ISDIR;
252 		rename_mask |= FS_ISDIR;
253 	}
254 
255 	/* Event with information about both old and new parent+name */
256 	fsnotify_name(rename_mask, moved, FSNOTIFY_EVENT_DENTRY,
257 		      old_dir, old_name, 0);
258 
259 	fsnotify_name(old_dir_mask, source, FSNOTIFY_EVENT_INODE,
260 		      old_dir, old_name, fs_cookie);
261 	fsnotify_name(new_dir_mask, source, FSNOTIFY_EVENT_INODE,
262 		      new_dir, new_name, fs_cookie);
263 
264 	if (target)
265 		fsnotify_link_count(target);
266 	fsnotify_inode(source, FS_MOVE_SELF);
267 	audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
268 }
269 
270 /*
271  * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
272  */
fsnotify_inode_delete(struct inode * inode)273 static inline void fsnotify_inode_delete(struct inode *inode)
274 {
275 	__fsnotify_inode_delete(inode);
276 }
277 
278 /*
279  * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
280  */
fsnotify_vfsmount_delete(struct vfsmount * mnt)281 static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
282 {
283 	__fsnotify_vfsmount_delete(mnt);
284 }
285 
286 /*
287  * fsnotify_inoderemove - an inode is going away
288  */
fsnotify_inoderemove(struct inode * inode)289 static inline void fsnotify_inoderemove(struct inode *inode)
290 {
291 	fsnotify_inode(inode, FS_DELETE_SELF);
292 	__fsnotify_inode_delete(inode);
293 }
294 
295 /*
296  * fsnotify_create - 'name' was linked in
297  *
298  * Caller must make sure that dentry->d_name is stable.
299  * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate
300  * ->d_inode later
301  */
fsnotify_create(struct inode * dir,struct dentry * dentry)302 static inline void fsnotify_create(struct inode *dir, struct dentry *dentry)
303 {
304 	audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE);
305 
306 	fsnotify_dirent(dir, dentry, FS_CREATE);
307 }
308 
309 /*
310  * fsnotify_link - new hardlink in 'inode' directory
311  *
312  * Caller must make sure that new_dentry->d_name is stable.
313  * Note: We have to pass also the linked inode ptr as some filesystems leave
314  *   new_dentry->d_inode NULL and instantiate inode pointer later
315  */
fsnotify_link(struct inode * dir,struct inode * inode,struct dentry * new_dentry)316 static inline void fsnotify_link(struct inode *dir, struct inode *inode,
317 				 struct dentry *new_dentry)
318 {
319 	fsnotify_link_count(inode);
320 	audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
321 
322 	fsnotify_name(FS_CREATE, inode, FSNOTIFY_EVENT_INODE,
323 		      dir, &new_dentry->d_name, 0);
324 }
325 
326 /*
327  * fsnotify_delete - @dentry was unlinked and unhashed
328  *
329  * Caller must make sure that dentry->d_name is stable.
330  *
331  * Note: unlike fsnotify_unlink(), we have to pass also the unlinked inode
332  * as this may be called after d_delete() and old_dentry may be negative.
333  */
fsnotify_delete(struct inode * dir,struct inode * inode,struct dentry * dentry)334 static inline void fsnotify_delete(struct inode *dir, struct inode *inode,
335 				   struct dentry *dentry)
336 {
337 	__u32 mask = FS_DELETE;
338 
339 	if (S_ISDIR(inode->i_mode))
340 		mask |= FS_ISDIR;
341 
342 	fsnotify_name(mask, inode, FSNOTIFY_EVENT_INODE, dir, &dentry->d_name,
343 		      0);
344 }
345 
346 /**
347  * d_delete_notify - delete a dentry and call fsnotify_delete()
348  * @dentry: The dentry to delete
349  *
350  * This helper is used to guaranty that the unlinked inode cannot be found
351  * by lookup of this name after fsnotify_delete() event has been delivered.
352  */
d_delete_notify(struct inode * dir,struct dentry * dentry)353 static inline void d_delete_notify(struct inode *dir, struct dentry *dentry)
354 {
355 	struct inode *inode = d_inode(dentry);
356 
357 	ihold(inode);
358 	d_delete(dentry);
359 	fsnotify_delete(dir, inode, dentry);
360 	iput(inode);
361 }
362 
363 /*
364  * fsnotify_unlink - 'name' was unlinked
365  *
366  * Caller must make sure that dentry->d_name is stable.
367  */
fsnotify_unlink(struct inode * dir,struct dentry * dentry)368 static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry)
369 {
370 	if (WARN_ON_ONCE(d_is_negative(dentry)))
371 		return;
372 
373 	fsnotify_delete(dir, d_inode(dentry), dentry);
374 }
375 
376 /*
377  * fsnotify_mkdir - directory 'name' was created
378  *
379  * Caller must make sure that dentry->d_name is stable.
380  * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate
381  * ->d_inode later
382  */
fsnotify_mkdir(struct inode * dir,struct dentry * dentry)383 static inline void fsnotify_mkdir(struct inode *dir, struct dentry *dentry)
384 {
385 	audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE);
386 
387 	fsnotify_dirent(dir, dentry, FS_CREATE | FS_ISDIR);
388 }
389 
390 /*
391  * fsnotify_rmdir - directory 'name' was removed
392  *
393  * Caller must make sure that dentry->d_name is stable.
394  */
fsnotify_rmdir(struct inode * dir,struct dentry * dentry)395 static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry)
396 {
397 	if (WARN_ON_ONCE(d_is_negative(dentry)))
398 		return;
399 
400 	fsnotify_delete(dir, d_inode(dentry), dentry);
401 }
402 
403 /*
404  * fsnotify_access - file was read
405  */
fsnotify_access(struct file * file)406 static inline void fsnotify_access(struct file *file)
407 {
408 	fsnotify_file(file, FS_ACCESS);
409 }
410 
411 /*
412  * fsnotify_modify - file was modified
413  */
fsnotify_modify(struct file * file)414 static inline void fsnotify_modify(struct file *file)
415 {
416 	fsnotify_file(file, FS_MODIFY);
417 }
418 
419 /*
420  * fsnotify_open - file was opened
421  */
fsnotify_open(struct file * file)422 static inline void fsnotify_open(struct file *file)
423 {
424 	__u32 mask = FS_OPEN;
425 
426 	if (file->f_flags & __FMODE_EXEC)
427 		mask |= FS_OPEN_EXEC;
428 
429 	trace_android_vh_fsnotify_open(file, &mask);
430 	fsnotify_file(file, mask);
431 }
432 
433 /*
434  * fsnotify_close - file was closed
435  */
fsnotify_close(struct file * file)436 static inline void fsnotify_close(struct file *file)
437 {
438 	__u32 mask = (file->f_mode & FMODE_WRITE) ? FS_CLOSE_WRITE :
439 						    FS_CLOSE_NOWRITE;
440 
441 	fsnotify_file(file, mask);
442 }
443 
444 /*
445  * fsnotify_xattr - extended attributes were changed
446  */
fsnotify_xattr(struct dentry * dentry)447 static inline void fsnotify_xattr(struct dentry *dentry)
448 {
449 	fsnotify_dentry(dentry, FS_ATTRIB);
450 }
451 
452 /*
453  * fsnotify_change - notify_change event.  file was modified and/or metadata
454  * was changed.
455  */
fsnotify_change(struct dentry * dentry,unsigned int ia_valid)456 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
457 {
458 	__u32 mask = 0;
459 
460 	if (ia_valid & ATTR_UID)
461 		mask |= FS_ATTRIB;
462 	if (ia_valid & ATTR_GID)
463 		mask |= FS_ATTRIB;
464 	if (ia_valid & ATTR_SIZE)
465 		mask |= FS_MODIFY;
466 
467 	/* both times implies a utime(s) call */
468 	if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
469 		mask |= FS_ATTRIB;
470 	else if (ia_valid & ATTR_ATIME)
471 		mask |= FS_ACCESS;
472 	else if (ia_valid & ATTR_MTIME)
473 		mask |= FS_MODIFY;
474 
475 	if (ia_valid & ATTR_MODE)
476 		mask |= FS_ATTRIB;
477 
478 	if (mask)
479 		fsnotify_dentry(dentry, mask);
480 }
481 
fsnotify_sb_error(struct super_block * sb,struct inode * inode,int error)482 static inline int fsnotify_sb_error(struct super_block *sb, struct inode *inode,
483 				    int error)
484 {
485 	struct fs_error_report report = {
486 		.error = error,
487 		.inode = inode,
488 		.sb = sb,
489 	};
490 
491 	return fsnotify(FS_ERROR, &report, FSNOTIFY_EVENT_ERROR,
492 			NULL, NULL, NULL, 0);
493 }
494 
495 #endif	/* _LINUX_FS_NOTIFY_H */
496