Linux Audio

Check our new training course

In-person Linux kernel drivers training

Jun 16-20, 2025
Register
Loading...
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2#include <linux/fanotify.h>
  3#include <linux/fdtable.h>
  4#include <linux/fsnotify_backend.h>
  5#include <linux/init.h>
  6#include <linux/jiffies.h>
  7#include <linux/kernel.h> /* UINT_MAX */
  8#include <linux/mount.h>
  9#include <linux/sched.h>
 10#include <linux/sched/user.h>
 11#include <linux/types.h>
 12#include <linux/wait.h>
 13#include <linux/audit.h>
 14
 15#include "fanotify.h"
 16
 17static bool should_merge(struct fsnotify_event *old_fsn,
 18			 struct fsnotify_event *new_fsn)
 19{
 20	struct fanotify_event_info *old, *new;
 21
 22	pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn);
 23	old = FANOTIFY_E(old_fsn);
 24	new = FANOTIFY_E(new_fsn);
 25
 26	if (old_fsn->inode == new_fsn->inode && old->tgid == new->tgid &&
 27	    old->path.mnt == new->path.mnt &&
 28	    old->path.dentry == new->path.dentry)
 29		return true;
 
 
 
 
 
 
 30	return false;
 31}
 32
 33/* and the list better be locked by something too! */
 34static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
 
 35{
 36	struct fsnotify_event *test_event;
 
 
 37
 38	pr_debug("%s: list=%p event=%p\n", __func__, list, event);
 39
 40	/*
 41	 * Don't merge a permission event with any other event so that we know
 42	 * the event structure we have created in fanotify_handle_event() is the
 43	 * one we should check for permission response.
 44	 */
 45	if (fanotify_is_perm_event(event->mask))
 46		return 0;
 47
 48	list_for_each_entry_reverse(test_event, list, list) {
 49		if (should_merge(test_event, event)) {
 50			test_event->mask |= event->mask;
 51			return 1;
 52		}
 53	}
 54
 55	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 56}
 57
 58static int fanotify_get_response(struct fsnotify_group *group,
 59				 struct fanotify_perm_event_info *event,
 60				 struct fsnotify_iter_info *iter_info)
 61{
 62	int ret;
 63
 64	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
 65
 66	wait_event(group->fanotify_data.access_waitq, event->response);
 
 
 
 
 67
 68	/* userspace responded, convert to something usable */
 69	switch (event->response & ~FAN_AUDIT) {
 
 70	case FAN_ALLOW:
 71		ret = 0;
 72		break;
 73	case FAN_DENY:
 74	default:
 75		ret = -EPERM;
 76	}
 77
 78	/* Check if the response should be audited */
 79	if (event->response & FAN_AUDIT)
 80		audit_fanotify(event->response & ~FAN_AUDIT);
 81
 82	event->response = 0;
 
 83
 84	pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
 85		 group, event, ret);
 86	
 87	return ret;
 88}
 89
 90static bool fanotify_should_send_event(struct fsnotify_mark *inode_mark,
 91				       struct fsnotify_mark *vfsmnt_mark,
 92				       u32 event_mask,
 93				       const void *data, int data_type)
 94{
 95	__u32 marks_mask = 0, marks_ignored_mask = 0;
 96	const struct path *path = data;
 97
 98	pr_debug("%s: inode_mark=%p vfsmnt_mark=%p mask=%x data=%p"
 99		 " data_type=%d\n", __func__, inode_mark, vfsmnt_mark,
100		 event_mask, data, data_type);
101
102	/* if we don't have enough info to send an event to userspace say no */
103	if (data_type != FSNOTIFY_EVENT_PATH)
104		return false;
105
106	/* sorry, fanotify only gives a damn about files and dirs */
107	if (!d_is_reg(path->dentry) &&
108	    !d_can_lookup(path->dentry))
109		return false;
110
111	/*
112	 * if the event is for a child and this inode doesn't care about
113	 * events on the child, don't send it!
114	 */
115	if (inode_mark &&
116	    (!(event_mask & FS_EVENT_ON_CHILD) ||
117	     (inode_mark->mask & FS_EVENT_ON_CHILD))) {
118		marks_mask |= inode_mark->mask;
119		marks_ignored_mask |= inode_mark->ignored_mask;
120	}
121
122	if (vfsmnt_mark) {
123		marks_mask |= vfsmnt_mark->mask;
124		marks_ignored_mask |= vfsmnt_mark->ignored_mask;
125	}
126
127	if (d_is_dir(path->dentry) &&
128	    !(marks_mask & FS_ISDIR & ~marks_ignored_mask))
129		return false;
130
131	if (event_mask & FAN_ALL_OUTGOING_EVENTS & marks_mask &
132				 ~marks_ignored_mask)
133		return true;
134
135	return false;
136}
137
138struct fanotify_event_info *fanotify_alloc_event(struct fsnotify_group *group,
139						 struct inode *inode, u32 mask,
140						 const struct path *path)
141{
142	struct fanotify_event_info *event;
143	gfp_t gfp = GFP_KERNEL;
144
145	/*
146	 * For queues with unlimited length lost events are not expected and
147	 * can possibly have security implications. Avoid losing events when
148	 * memory is short.
149	 */
150	if (group->max_events == UINT_MAX)
151		gfp |= __GFP_NOFAIL;
152
153	if (fanotify_is_perm_event(mask)) {
154		struct fanotify_perm_event_info *pevent;
155
156		pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
157		if (!pevent)
158			return NULL;
159		event = &pevent->fae;
160		pevent->response = 0;
161		goto init;
162	}
163	event = kmem_cache_alloc(fanotify_event_cachep, gfp);
164	if (!event)
165		return NULL;
166init: __maybe_unused
167	fsnotify_init_event(&event->fse, inode, mask);
168	event->tgid = get_pid(task_tgid(current));
169	if (path) {
170		event->path = *path;
171		path_get(&event->path);
172	} else {
173		event->path.mnt = NULL;
174		event->path.dentry = NULL;
175	}
176	return event;
177}
178
179static int fanotify_handle_event(struct fsnotify_group *group,
180				 struct inode *inode,
181				 struct fsnotify_mark *inode_mark,
182				 struct fsnotify_mark *fanotify_mark,
183				 u32 mask, const void *data, int data_type,
184				 const unsigned char *file_name, u32 cookie,
185				 struct fsnotify_iter_info *iter_info)
186{
187	int ret = 0;
188	struct fanotify_event_info *event;
189	struct fsnotify_event *fsn_event;
190
191	BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
192	BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
193	BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
194	BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
195	BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
196	BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
197	BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
198	BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
199	BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
200	BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
201
202	if (!fanotify_should_send_event(inode_mark, fanotify_mark, mask, data,
203					data_type))
204		return 0;
205
206	pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode,
207		 mask);
208
209	if (fanotify_is_perm_event(mask)) {
210		/*
211		 * fsnotify_prepare_user_wait() fails if we race with mark
212		 * deletion.  Just let the operation pass in that case.
213		 */
214		if (!fsnotify_prepare_user_wait(iter_info))
215			return 0;
 
 
 
216	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
218	event = fanotify_alloc_event(group, inode, mask, data);
219	ret = -ENOMEM;
220	if (unlikely(!event)) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221		/*
222		 * We don't queue overflow events for permission events as
223		 * there the access is denied and so no event is in fact lost.
224		 */
225		if (!fanotify_is_perm_event(mask))
226			fsnotify_queue_overflow(group);
227		goto finish;
 
 
 
 
 
 
 
228	}
229
230	fsn_event = &event->fse;
231	ret = fsnotify_add_event(group, fsn_event, fanotify_merge);
232	if (ret) {
233		/* Permission events shouldn't be merged */
234		BUG_ON(ret == 1 && mask & FAN_ALL_PERM_EVENTS);
235		/* Our event wasn't used in the end. Free it. */
236		fsnotify_destroy_event(group, fsn_event);
237
238		ret = 0;
239	} else if (fanotify_is_perm_event(mask)) {
240		ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event),
241					    iter_info);
242		fsnotify_destroy_event(group, fsn_event);
243	}
244finish:
245	if (fanotify_is_perm_event(mask))
246		fsnotify_finish_user_wait(iter_info);
247
248	return ret;
249}
250
251static void fanotify_free_group_priv(struct fsnotify_group *group)
252{
253	struct user_struct *user;
254
255	user = group->fanotify_data.user;
256	atomic_dec(&user->fanotify_listeners);
257	free_uid(user);
258}
259
260static void fanotify_free_event(struct fsnotify_event *fsn_event)
261{
262	struct fanotify_event_info *event;
263
264	event = FANOTIFY_E(fsn_event);
265	path_put(&event->path);
266	put_pid(event->tgid);
267	if (fanotify_is_perm_event(fsn_event->mask)) {
268		kmem_cache_free(fanotify_perm_event_cachep,
269				FANOTIFY_PE(fsn_event));
270		return;
271	}
272	kmem_cache_free(fanotify_event_cachep, event);
273}
274
275static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
276{
277	kmem_cache_free(fanotify_mark_cache, fsn_mark);
278}
279
280const struct fsnotify_ops fanotify_fsnotify_ops = {
281	.handle_event = fanotify_handle_event,
 
282	.free_group_priv = fanotify_free_group_priv,
283	.free_event = fanotify_free_event,
284	.free_mark = fanotify_free_mark,
285};
v3.1
 
  1#include <linux/fanotify.h>
  2#include <linux/fdtable.h>
  3#include <linux/fsnotify_backend.h>
  4#include <linux/init.h>
  5#include <linux/jiffies.h>
  6#include <linux/kernel.h> /* UINT_MAX */
  7#include <linux/mount.h>
  8#include <linux/sched.h>
 
  9#include <linux/types.h>
 10#include <linux/wait.h>
 
 11
 12static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new)
 
 
 
 13{
 14	pr_debug("%s: old=%p new=%p\n", __func__, old, new);
 15
 16	if (old->to_tell == new->to_tell &&
 17	    old->data_type == new->data_type &&
 18	    old->tgid == new->tgid) {
 19		switch (old->data_type) {
 20		case (FSNOTIFY_EVENT_PATH):
 21			if ((old->path.mnt == new->path.mnt) &&
 22			    (old->path.dentry == new->path.dentry))
 23				return true;
 24		case (FSNOTIFY_EVENT_NONE):
 25			return true;
 26		default:
 27			BUG();
 28		};
 29	}
 30	return false;
 31}
 32
 33/* and the list better be locked by something too! */
 34static struct fsnotify_event *fanotify_merge(struct list_head *list,
 35					     struct fsnotify_event *event)
 36{
 37	struct fsnotify_event_holder *test_holder;
 38	struct fsnotify_event *test_event = NULL;
 39	struct fsnotify_event *new_event;
 40
 41	pr_debug("%s: list=%p event=%p\n", __func__, list, event);
 42
 
 
 
 
 
 
 
 43
 44	list_for_each_entry_reverse(test_holder, list, event_list) {
 45		if (should_merge(test_holder->event, event)) {
 46			test_event = test_holder->event;
 47			break;
 48		}
 49	}
 50
 51	if (!test_event)
 52		return NULL;
 53
 54	fsnotify_get_event(test_event);
 55
 56	/* if they are exactly the same we are done */
 57	if (test_event->mask == event->mask)
 58		return test_event;
 59
 60	/*
 61	 * if the refcnt == 2 this is the only queue
 62	 * for this event and so we can update the mask
 63	 * in place.
 64	 */
 65	if (atomic_read(&test_event->refcnt) == 2) {
 66		test_event->mask |= event->mask;
 67		return test_event;
 68	}
 69
 70	new_event = fsnotify_clone_event(test_event);
 71
 72	/* done with test_event */
 73	fsnotify_put_event(test_event);
 74
 75	/* couldn't allocate memory, merge was not possible */
 76	if (unlikely(!new_event))
 77		return ERR_PTR(-ENOMEM);
 78
 79	/* build new event and replace it on the list */
 80	new_event->mask = (test_event->mask | event->mask);
 81	fsnotify_replace_event(test_holder, new_event);
 82
 83	/* we hold a reference on new_event from clone_event */
 84	return new_event;
 85}
 86
 87#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
 88static int fanotify_get_response_from_access(struct fsnotify_group *group,
 89					     struct fsnotify_event *event)
 90{
 91	int ret;
 92
 93	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
 94
 95	wait_event(group->fanotify_data.access_waitq, event->response ||
 96				atomic_read(&group->fanotify_data.bypass_perm));
 97
 98	if (!event->response) /* bypass_perm set */
 99		return 0;
100
101	/* userspace responded, convert to something usable */
102	spin_lock(&event->lock);
103	switch (event->response) {
104	case FAN_ALLOW:
105		ret = 0;
106		break;
107	case FAN_DENY:
108	default:
109		ret = -EPERM;
110	}
 
 
 
 
 
111	event->response = 0;
112	spin_unlock(&event->lock);
113
114	pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
115		 group, event, ret);
116	
117	return ret;
118}
119#endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
121static int fanotify_handle_event(struct fsnotify_group *group,
 
122				 struct fsnotify_mark *inode_mark,
123				 struct fsnotify_mark *fanotify_mark,
124				 struct fsnotify_event *event)
 
 
125{
126	int ret = 0;
127	struct fsnotify_event *notify_event = NULL;
 
128
129	BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
130	BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
131	BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
132	BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
133	BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
134	BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
135	BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
136	BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
137	BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
138	BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
139
140	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
 
 
 
 
 
141
142	notify_event = fsnotify_add_notify_event(group, event, NULL, fanotify_merge);
143	if (IS_ERR(notify_event))
144		return PTR_ERR(notify_event);
145
146#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
147	if (event->mask & FAN_ALL_PERM_EVENTS) {
148		/* if we merged we need to wait on the new event */
149		if (notify_event)
150			event = notify_event;
151		ret = fanotify_get_response_from_access(group, event);
152	}
153#endif
154
155	if (notify_event)
156		fsnotify_put_event(notify_event);
157
158	return ret;
159}
160
161static bool fanotify_should_send_event(struct fsnotify_group *group,
162				       struct inode *to_tell,
163				       struct fsnotify_mark *inode_mark,
164				       struct fsnotify_mark *vfsmnt_mark,
165				       __u32 event_mask, void *data, int data_type)
166{
167	__u32 marks_mask, marks_ignored_mask;
168	struct path *path = data;
169
170	pr_debug("%s: group=%p to_tell=%p inode_mark=%p vfsmnt_mark=%p "
171		 "mask=%x data=%p data_type=%d\n", __func__, group, to_tell,
172		 inode_mark, vfsmnt_mark, event_mask, data, data_type);
173
174	/* if we don't have enough info to send an event to userspace say no */
175	if (data_type != FSNOTIFY_EVENT_PATH)
176		return false;
177
178	/* sorry, fanotify only gives a damn about files and dirs */
179	if (!S_ISREG(path->dentry->d_inode->i_mode) &&
180	    !S_ISDIR(path->dentry->d_inode->i_mode))
181		return false;
182
183	if (inode_mark && vfsmnt_mark) {
184		marks_mask = (vfsmnt_mark->mask | inode_mark->mask);
185		marks_ignored_mask = (vfsmnt_mark->ignored_mask | inode_mark->ignored_mask);
186	} else if (inode_mark) {
187		/*
188		 * if the event is for a child and this inode doesn't care about
189		 * events on the child, don't send it!
190		 */
191		if ((event_mask & FS_EVENT_ON_CHILD) &&
192		    !(inode_mark->mask & FS_EVENT_ON_CHILD))
193			return false;
194		marks_mask = inode_mark->mask;
195		marks_ignored_mask = inode_mark->ignored_mask;
196	} else if (vfsmnt_mark) {
197		marks_mask = vfsmnt_mark->mask;
198		marks_ignored_mask = vfsmnt_mark->ignored_mask;
199	} else {
200		BUG();
201	}
202
203	if (S_ISDIR(path->dentry->d_inode->i_mode) &&
204	    (marks_ignored_mask & FS_ISDIR))
205		return false;
 
 
 
 
206
207	if (event_mask & marks_mask & ~marks_ignored_mask)
208		return true;
 
 
 
 
 
 
 
209
210	return false;
211}
212
213static void fanotify_free_group_priv(struct fsnotify_group *group)
214{
215	struct user_struct *user;
216
217	user = group->fanotify_data.user;
218	atomic_dec(&user->fanotify_listeners);
219	free_uid(user);
220}
221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222const struct fsnotify_ops fanotify_fsnotify_ops = {
223	.handle_event = fanotify_handle_event,
224	.should_send_event = fanotify_should_send_event,
225	.free_group_priv = fanotify_free_group_priv,
226	.free_event_priv = NULL,
227	.freeing_mark = NULL,
228};