blob: aeb5b5abbd4fcb9036b1b9e7391541c20d8b8d3c [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):
Lino Sanfilippo03a1cec2012-03-23 02:42:23 +010021#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
22 /* dont merge two permission events */
23 if ((old->mask & FAN_ALL_PERM_EVENTS) &&
24 (new->mask & FAN_ALL_PERM_EVENTS))
25 return false;
26#endif
Linus Torvalds20696012010-08-12 14:23:04 -070027 if ((old->path.mnt == new->path.mnt) &&
28 (old->path.dentry == new->path.dentry))
Eric Paris767cd462009-12-17 21:24:25 -050029 return true;
30 case (FSNOTIFY_EVENT_NONE):
31 return true;
32 default:
33 BUG();
34 };
35 }
36 return false;
37}
38
Eric Parisf70ab542010-07-28 10:18:37 -040039/* and the list better be locked by something too! */
40static struct fsnotify_event *fanotify_merge(struct list_head *list,
41 struct fsnotify_event *event)
Eric Paris767cd462009-12-17 21:24:25 -050042{
Eric Parisa12a7dd2009-12-17 21:24:25 -050043 struct fsnotify_event_holder *test_holder;
Eric Parisf70ab542010-07-28 10:18:37 -040044 struct fsnotify_event *test_event = NULL;
Eric Parisa12a7dd2009-12-17 21:24:25 -050045 struct fsnotify_event *new_event;
Eric Paris767cd462009-12-17 21:24:25 -050046
47 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
48
Eric Paris767cd462009-12-17 21:24:25 -050049
Eric Parisa12a7dd2009-12-17 21:24:25 -050050 list_for_each_entry_reverse(test_holder, list, event_list) {
Eric Parisf70ab542010-07-28 10:18:37 -040051 if (should_merge(test_holder->event, event)) {
52 test_event = test_holder->event;
Eric Parisa12a7dd2009-12-17 21:24:25 -050053 break;
54 }
55 }
Eric Parisf70ab542010-07-28 10:18:37 -040056
57 if (!test_event)
58 return NULL;
59
60 fsnotify_get_event(test_event);
61
62 /* if they are exactly the same we are done */
63 if (test_event->mask == event->mask)
64 return test_event;
65
66 /*
67 * if the refcnt == 2 this is the only queue
68 * for this event and so we can update the mask
69 * in place.
70 */
71 if (atomic_read(&test_event->refcnt) == 2) {
72 test_event->mask |= event->mask;
73 return test_event;
74 }
75
76 new_event = fsnotify_clone_event(test_event);
77
78 /* done with test_event */
79 fsnotify_put_event(test_event);
80
81 /* couldn't allocate memory, merge was not possible */
82 if (unlikely(!new_event))
83 return ERR_PTR(-ENOMEM);
84
85 /* build new event and replace it on the list */
86 new_event->mask = (test_event->mask | event->mask);
87 fsnotify_replace_event(test_holder, new_event);
88
89 /* we hold a reference on new_event from clone_event */
90 return new_event;
Eric Paris767cd462009-12-17 21:24:25 -050091}
92
Eric Paris9e66e422009-12-17 21:24:34 -050093#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
94static int fanotify_get_response_from_access(struct fsnotify_group *group,
95 struct fsnotify_event *event)
96{
97 int ret;
98
99 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
100
Lino Sanfilippo09e5f142010-11-19 10:58:07 +0100101 wait_event(group->fanotify_data.access_waitq, event->response ||
102 atomic_read(&group->fanotify_data.bypass_perm));
103
104 if (!event->response) /* bypass_perm set */
105 return 0;
Eric Paris9e66e422009-12-17 21:24:34 -0500106
107 /* userspace responded, convert to something usable */
108 spin_lock(&event->lock);
109 switch (event->response) {
110 case FAN_ALLOW:
111 ret = 0;
112 break;
113 case FAN_DENY:
114 default:
115 ret = -EPERM;
116 }
117 event->response = 0;
118 spin_unlock(&event->lock);
119
Eric Parisb2d87902009-12-17 21:24:34 -0500120 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
121 group, event, ret);
122
Eric Paris9e66e422009-12-17 21:24:34 -0500123 return ret;
124}
125#endif
126
Eric Paris3a9b16b2010-07-28 10:18:38 -0400127static int fanotify_handle_event(struct fsnotify_group *group,
Eric Parisce8f76f2010-07-28 10:18:39 -0400128 struct fsnotify_mark *inode_mark,
129 struct fsnotify_mark *fanotify_mark,
Eric Paris3a9b16b2010-07-28 10:18:38 -0400130 struct fsnotify_event *event)
Eric Parisff0b16a2009-12-17 21:24:25 -0500131{
Eric Parisf70ab542010-07-28 10:18:37 -0400132 int ret = 0;
Eric Paris9e66e422009-12-17 21:24:34 -0500133 struct fsnotify_event *notify_event = NULL;
Eric Parisff0b16a2009-12-17 21:24:25 -0500134
135 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
136 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
137 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
138 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
139 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
140 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
141 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
Eric Paris9e66e422009-12-17 21:24:34 -0500142 BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
143 BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
Eric Paris8fcd6522010-10-28 17:21:59 -0400144 BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
Eric Parisff0b16a2009-12-17 21:24:25 -0500145
146 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
147
Eric Parisf70ab542010-07-28 10:18:37 -0400148 notify_event = fsnotify_add_notify_event(group, event, NULL, fanotify_merge);
149 if (IS_ERR(notify_event))
150 return PTR_ERR(notify_event);
Eric Paris9e66e422009-12-17 21:24:34 -0500151
152#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
153 if (event->mask & FAN_ALL_PERM_EVENTS) {
154 /* if we merged we need to wait on the new event */
155 if (notify_event)
156 event = notify_event;
157 ret = fanotify_get_response_from_access(group, event);
158 }
159#endif
160
Eric Paris9e66e422009-12-17 21:24:34 -0500161 if (notify_event)
162 fsnotify_put_event(notify_event);
Eric Parisf70ab542010-07-28 10:18:37 -0400163
Eric Parisff0b16a2009-12-17 21:24:25 -0500164 return ret;
165}
166
Eric Parisce8f76f2010-07-28 10:18:39 -0400167static bool fanotify_should_send_event(struct fsnotify_group *group,
168 struct inode *to_tell,
Eric Parisce8f76f2010-07-28 10:18:39 -0400169 struct fsnotify_mark *inode_mark,
Eric Paris1968f5e2010-07-28 10:18:39 -0400170 struct fsnotify_mark *vfsmnt_mark,
171 __u32 event_mask, void *data, int data_type)
Eric Paris1c529062009-12-17 21:24:28 -0500172{
Eric Paris1968f5e2010-07-28 10:18:39 -0400173 __u32 marks_mask, marks_ignored_mask;
Eric Parise1c048b2010-10-28 17:21:58 -0400174 struct path *path = data;
Eric Paris1968f5e2010-07-28 10:18:39 -0400175
176 pr_debug("%s: group=%p to_tell=%p inode_mark=%p vfsmnt_mark=%p "
177 "mask=%x data=%p data_type=%d\n", __func__, group, to_tell,
178 inode_mark, vfsmnt_mark, event_mask, data, data_type);
179
Eric Paris1c529062009-12-17 21:24:28 -0500180 /* if we don't have enough info to send an event to userspace say no */
Linus Torvalds20696012010-08-12 14:23:04 -0700181 if (data_type != FSNOTIFY_EVENT_PATH)
Eric Paris1c529062009-12-17 21:24:28 -0500182 return false;
183
Eric Parise1c048b2010-10-28 17:21:58 -0400184 /* sorry, fanotify only gives a damn about files and dirs */
185 if (!S_ISREG(path->dentry->d_inode->i_mode) &&
186 !S_ISDIR(path->dentry->d_inode->i_mode))
187 return false;
188
Eric Paris1968f5e2010-07-28 10:18:39 -0400189 if (inode_mark && vfsmnt_mark) {
190 marks_mask = (vfsmnt_mark->mask | inode_mark->mask);
191 marks_ignored_mask = (vfsmnt_mark->ignored_mask | inode_mark->ignored_mask);
192 } else if (inode_mark) {
193 /*
194 * if the event is for a child and this inode doesn't care about
195 * events on the child, don't send it!
196 */
197 if ((event_mask & FS_EVENT_ON_CHILD) &&
198 !(inode_mark->mask & FS_EVENT_ON_CHILD))
199 return false;
200 marks_mask = inode_mark->mask;
201 marks_ignored_mask = inode_mark->ignored_mask;
202 } else if (vfsmnt_mark) {
203 marks_mask = vfsmnt_mark->mask;
204 marks_ignored_mask = vfsmnt_mark->ignored_mask;
205 } else {
206 BUG();
207 }
208
Eric Paris8fcd6522010-10-28 17:21:59 -0400209 if (S_ISDIR(path->dentry->d_inode->i_mode) &&
210 (marks_ignored_mask & FS_ISDIR))
211 return false;
212
Eric Paris1968f5e2010-07-28 10:18:39 -0400213 if (event_mask & marks_mask & ~marks_ignored_mask)
214 return true;
215
216 return false;
Eric Paris1c529062009-12-17 21:24:28 -0500217}
218
Eric Paris4afeff82010-10-28 17:21:58 -0400219static void fanotify_free_group_priv(struct fsnotify_group *group)
220{
221 struct user_struct *user;
222
223 user = group->fanotify_data.user;
224 atomic_dec(&user->fanotify_listeners);
225 free_uid(user);
226}
227
Eric Parisff0b16a2009-12-17 21:24:25 -0500228const struct fsnotify_ops fanotify_fsnotify_ops = {
229 .handle_event = fanotify_handle_event,
230 .should_send_event = fanotify_should_send_event,
Eric Paris4afeff82010-10-28 17:21:58 -0400231 .free_group_priv = fanotify_free_group_priv,
Eric Parisff0b16a2009-12-17 21:24:25 -0500232 .free_event_priv = NULL,
233 .freeing_mark = NULL,
234};