blob: a50636025364214176fe7cec0cb80757ddc27171 [file] [log] [blame]
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -05001#include <linux/fanotify.h>
Eric Parisff0b16a2009-12-17 21:24:25 -05002#include <linux/fdtable.h>
3#include <linux/fsnotify_backend.h>
4#include <linux/init.h>
Eric Paris9e66e422009-12-17 21:24:34 -05005#include <linux/jiffies.h>
Eric Parisff0b16a2009-12-17 21:24:25 -05006#include <linux/kernel.h> /* UINT_MAX */
Eric Paris1c529062009-12-17 21:24:28 -05007#include <linux/mount.h>
Eric Paris9e66e422009-12-17 21:24:34 -05008#include <linux/sched.h>
Eric Parisff0b16a2009-12-17 21:24:25 -05009#include <linux/types.h>
Eric Paris9e66e422009-12-17 21:24:34 -050010#include <linux/wait.h>
Eric Parisff0b16a2009-12-17 21:24:25 -050011
Eric Paris767cd462009-12-17 21:24:25 -050012static 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
Andreas Gruenbacher32c32632009-12-17 21:24:27 -050016 if (old->to_tell == new->to_tell &&
17 old->data_type == new->data_type &&
18 old->tgid == new->tgid) {
Eric Paris767cd462009-12-17 21:24:25 -050019 switch (old->data_type) {
Linus Torvalds20696012010-08-12 14:23:04 -070020 case (FSNOTIFY_EVENT_PATH):
21 if ((old->path.mnt == new->path.mnt) &&
22 (old->path.dentry == new->path.dentry))
Eric Paris767cd462009-12-17 21:24:25 -050023 return true;
Eric Paris848561d2012-11-08 15:53:37 -080024 break;
Eric Paris767cd462009-12-17 21:24:25 -050025 case (FSNOTIFY_EVENT_NONE):
26 return true;
27 default:
28 BUG();
29 };
30 }
31 return false;
32}
33
Eric Parisf70ab542010-07-28 10:18:37 -040034/* and the list better be locked by something too! */
35static struct fsnotify_event *fanotify_merge(struct list_head *list,
36 struct fsnotify_event *event)
Eric Paris767cd462009-12-17 21:24:25 -050037{
Eric Parisa12a7dd2009-12-17 21:24:25 -050038 struct fsnotify_event_holder *test_holder;
Eric Parisf70ab542010-07-28 10:18:37 -040039 struct fsnotify_event *test_event = NULL;
Eric Parisa12a7dd2009-12-17 21:24:25 -050040 struct fsnotify_event *new_event;
Eric Paris767cd462009-12-17 21:24:25 -050041
42 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
43
Eric Paris767cd462009-12-17 21:24:25 -050044
Eric Parisa12a7dd2009-12-17 21:24:25 -050045 list_for_each_entry_reverse(test_holder, list, event_list) {
Eric Parisf70ab542010-07-28 10:18:37 -040046 if (should_merge(test_holder->event, event)) {
47 test_event = test_holder->event;
Eric Parisa12a7dd2009-12-17 21:24:25 -050048 break;
49 }
50 }
Eric Parisf70ab542010-07-28 10:18:37 -040051
52 if (!test_event)
53 return NULL;
54
55 fsnotify_get_event(test_event);
56
57 /* if they are exactly the same we are done */
58 if (test_event->mask == event->mask)
59 return test_event;
60
61 /*
62 * if the refcnt == 2 this is the only queue
63 * for this event and so we can update the mask
64 * in place.
65 */
66 if (atomic_read(&test_event->refcnt) == 2) {
67 test_event->mask |= event->mask;
68 return test_event;
69 }
70
71 new_event = fsnotify_clone_event(test_event);
72
73 /* done with test_event */
74 fsnotify_put_event(test_event);
75
76 /* couldn't allocate memory, merge was not possible */
77 if (unlikely(!new_event))
78 return ERR_PTR(-ENOMEM);
79
80 /* build new event and replace it on the list */
81 new_event->mask = (test_event->mask | event->mask);
82 fsnotify_replace_event(test_holder, new_event);
83
84 /* we hold a reference on new_event from clone_event */
85 return new_event;
Eric Paris767cd462009-12-17 21:24:25 -050086}
87
Eric Paris9e66e422009-12-17 21:24:34 -050088#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
89static int fanotify_get_response_from_access(struct fsnotify_group *group,
90 struct fsnotify_event *event)
91{
92 int ret;
93
94 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
95
Lino Sanfilippo09e5f142010-11-19 10:58:07 +010096 wait_event(group->fanotify_data.access_waitq, event->response ||
97 atomic_read(&group->fanotify_data.bypass_perm));
98
99 if (!event->response) /* bypass_perm set */
100 return 0;
Eric Paris9e66e422009-12-17 21:24:34 -0500101
102 /* userspace responded, convert to something usable */
103 spin_lock(&event->lock);
104 switch (event->response) {
105 case FAN_ALLOW:
106 ret = 0;
107 break;
108 case FAN_DENY:
109 default:
110 ret = -EPERM;
111 }
112 event->response = 0;
113 spin_unlock(&event->lock);
114
Eric Parisb2d87902009-12-17 21:24:34 -0500115 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
116 group, event, ret);
117
Eric Paris9e66e422009-12-17 21:24:34 -0500118 return ret;
119}
120#endif
121
Eric Paris3a9b16b2010-07-28 10:18:38 -0400122static int fanotify_handle_event(struct fsnotify_group *group,
Eric Parisce8f76f2010-07-28 10:18:39 -0400123 struct fsnotify_mark *inode_mark,
124 struct fsnotify_mark *fanotify_mark,
Eric Paris3a9b16b2010-07-28 10:18:38 -0400125 struct fsnotify_event *event)
Eric Parisff0b16a2009-12-17 21:24:25 -0500126{
Eric Parisf70ab542010-07-28 10:18:37 -0400127 int ret = 0;
Eric Paris9e66e422009-12-17 21:24:34 -0500128 struct fsnotify_event *notify_event = NULL;
Eric Parisff0b16a2009-12-17 21:24:25 -0500129
130 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
131 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
132 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
133 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
134 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
135 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
136 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
Eric Paris9e66e422009-12-17 21:24:34 -0500137 BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
138 BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
Eric Paris8fcd6522010-10-28 17:21:59 -0400139 BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
Eric Parisff0b16a2009-12-17 21:24:25 -0500140
141 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
142
Eric Parisf70ab542010-07-28 10:18:37 -0400143 notify_event = fsnotify_add_notify_event(group, event, NULL, fanotify_merge);
144 if (IS_ERR(notify_event))
145 return PTR_ERR(notify_event);
Eric Paris9e66e422009-12-17 21:24:34 -0500146
147#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
148 if (event->mask & FAN_ALL_PERM_EVENTS) {
149 /* if we merged we need to wait on the new event */
150 if (notify_event)
151 event = notify_event;
152 ret = fanotify_get_response_from_access(group, event);
153 }
154#endif
155
Eric Paris9e66e422009-12-17 21:24:34 -0500156 if (notify_event)
157 fsnotify_put_event(notify_event);
Eric Parisf70ab542010-07-28 10:18:37 -0400158
Eric Parisff0b16a2009-12-17 21:24:25 -0500159 return ret;
160}
161
Eric Parisce8f76f2010-07-28 10:18:39 -0400162static bool fanotify_should_send_event(struct fsnotify_group *group,
163 struct inode *to_tell,
Eric Parisce8f76f2010-07-28 10:18:39 -0400164 struct fsnotify_mark *inode_mark,
Eric Paris1968f5e2010-07-28 10:18:39 -0400165 struct fsnotify_mark *vfsmnt_mark,
166 __u32 event_mask, void *data, int data_type)
Eric Paris1c529062009-12-17 21:24:28 -0500167{
Eric Paris1968f5e2010-07-28 10:18:39 -0400168 __u32 marks_mask, marks_ignored_mask;
Eric Parise1c048b2010-10-28 17:21:58 -0400169 struct path *path = data;
Eric Paris1968f5e2010-07-28 10:18:39 -0400170
171 pr_debug("%s: group=%p to_tell=%p inode_mark=%p vfsmnt_mark=%p "
172 "mask=%x data=%p data_type=%d\n", __func__, group, to_tell,
173 inode_mark, vfsmnt_mark, event_mask, data, data_type);
174
Eric Paris1c529062009-12-17 21:24:28 -0500175 /* if we don't have enough info to send an event to userspace say no */
Linus Torvalds20696012010-08-12 14:23:04 -0700176 if (data_type != FSNOTIFY_EVENT_PATH)
Eric Paris1c529062009-12-17 21:24:28 -0500177 return false;
178
Eric Parise1c048b2010-10-28 17:21:58 -0400179 /* sorry, fanotify only gives a damn about files and dirs */
180 if (!S_ISREG(path->dentry->d_inode->i_mode) &&
181 !S_ISDIR(path->dentry->d_inode->i_mode))
182 return false;
183
Eric Paris1968f5e2010-07-28 10:18:39 -0400184 if (inode_mark && vfsmnt_mark) {
185 marks_mask = (vfsmnt_mark->mask | inode_mark->mask);
186 marks_ignored_mask = (vfsmnt_mark->ignored_mask | inode_mark->ignored_mask);
187 } else if (inode_mark) {
188 /*
189 * if the event is for a child and this inode doesn't care about
190 * events on the child, don't send it!
191 */
192 if ((event_mask & FS_EVENT_ON_CHILD) &&
193 !(inode_mark->mask & FS_EVENT_ON_CHILD))
194 return false;
195 marks_mask = inode_mark->mask;
196 marks_ignored_mask = inode_mark->ignored_mask;
197 } else if (vfsmnt_mark) {
198 marks_mask = vfsmnt_mark->mask;
199 marks_ignored_mask = vfsmnt_mark->ignored_mask;
200 } else {
201 BUG();
202 }
203
Eric Paris8fcd6522010-10-28 17:21:59 -0400204 if (S_ISDIR(path->dentry->d_inode->i_mode) &&
205 (marks_ignored_mask & FS_ISDIR))
206 return false;
207
Eric Paris1968f5e2010-07-28 10:18:39 -0400208 if (event_mask & marks_mask & ~marks_ignored_mask)
209 return true;
210
211 return false;
Eric Paris1c529062009-12-17 21:24:28 -0500212}
213
Eric Paris4afeff82010-10-28 17:21:58 -0400214static void fanotify_free_group_priv(struct fsnotify_group *group)
215{
216 struct user_struct *user;
217
218 user = group->fanotify_data.user;
219 atomic_dec(&user->fanotify_listeners);
220 free_uid(user);
221}
222
Eric Parisff0b16a2009-12-17 21:24:25 -0500223const struct fsnotify_ops fanotify_fsnotify_ops = {
224 .handle_event = fanotify_handle_event,
225 .should_send_event = fanotify_should_send_event,
Eric Paris4afeff82010-10-28 17:21:58 -0400226 .free_group_priv = fanotify_free_group_priv,
Eric Parisff0b16a2009-12-17 21:24:25 -0500227 .free_event_priv = NULL,
228 .freeing_mark = NULL,
229};