blob: f35794b97e8e5cb5cc396af28206d69ca1202935 [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;
24 case (FSNOTIFY_EVENT_NONE):
25 return true;
26 default:
27 BUG();
28 };
29 }
30 return false;
31}
32
Eric Parisf70ab542010-07-28 10:18:37 -040033/* and the list better be locked by something too! */
34static struct fsnotify_event *fanotify_merge(struct list_head *list,
35 struct fsnotify_event *event)
Eric Paris767cd462009-12-17 21:24:25 -050036{
Eric Parisa12a7dd2009-12-17 21:24:25 -050037 struct fsnotify_event_holder *test_holder;
Eric Parisf70ab542010-07-28 10:18:37 -040038 struct fsnotify_event *test_event = NULL;
Eric Parisa12a7dd2009-12-17 21:24:25 -050039 struct fsnotify_event *new_event;
Eric Paris767cd462009-12-17 21:24:25 -050040
41 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
42
Eric Paris767cd462009-12-17 21:24:25 -050043
Eric Parisa12a7dd2009-12-17 21:24:25 -050044 list_for_each_entry_reverse(test_holder, list, event_list) {
Eric Parisf70ab542010-07-28 10:18:37 -040045 if (should_merge(test_holder->event, event)) {
46 test_event = test_holder->event;
Eric Parisa12a7dd2009-12-17 21:24:25 -050047 break;
48 }
49 }
Eric Parisf70ab542010-07-28 10:18:37 -040050
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;
Eric Paris767cd462009-12-17 21:24:25 -050085}
86
Eric Paris9e66e422009-12-17 21:24:34 -050087#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
Lino Sanfilippo09e5f142010-11-19 10:58:07 +010095 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;
Eric Paris9e66e422009-12-17 21:24:34 -0500100
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
Eric Parisb2d87902009-12-17 21:24:34 -0500114 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
115 group, event, ret);
116
Eric Paris9e66e422009-12-17 21:24:34 -0500117 return ret;
118}
119#endif
120
Eric Paris3a9b16b2010-07-28 10:18:38 -0400121static int fanotify_handle_event(struct fsnotify_group *group,
Eric Parisce8f76f2010-07-28 10:18:39 -0400122 struct fsnotify_mark *inode_mark,
123 struct fsnotify_mark *fanotify_mark,
Eric Paris3a9b16b2010-07-28 10:18:38 -0400124 struct fsnotify_event *event)
Eric Parisff0b16a2009-12-17 21:24:25 -0500125{
Eric Parisf70ab542010-07-28 10:18:37 -0400126 int ret = 0;
Eric Paris9e66e422009-12-17 21:24:34 -0500127 struct fsnotify_event *notify_event = NULL;
Eric Parisff0b16a2009-12-17 21:24:25 -0500128
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);
Eric Paris9e66e422009-12-17 21:24:34 -0500136 BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
137 BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
Eric Paris8fcd6522010-10-28 17:21:59 -0400138 BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
Eric Parisff0b16a2009-12-17 21:24:25 -0500139
140 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
141
Eric Parisf70ab542010-07-28 10:18:37 -0400142 notify_event = fsnotify_add_notify_event(group, event, NULL, fanotify_merge);
143 if (IS_ERR(notify_event))
144 return PTR_ERR(notify_event);
Eric Paris9e66e422009-12-17 21:24:34 -0500145
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
Eric Paris9e66e422009-12-17 21:24:34 -0500155 if (notify_event)
156 fsnotify_put_event(notify_event);
Eric Parisf70ab542010-07-28 10:18:37 -0400157
Eric Parisff0b16a2009-12-17 21:24:25 -0500158 return ret;
159}
160
Eric Parisce8f76f2010-07-28 10:18:39 -0400161static bool fanotify_should_send_event(struct fsnotify_group *group,
162 struct inode *to_tell,
Eric Parisce8f76f2010-07-28 10:18:39 -0400163 struct fsnotify_mark *inode_mark,
Eric Paris1968f5e2010-07-28 10:18:39 -0400164 struct fsnotify_mark *vfsmnt_mark,
165 __u32 event_mask, void *data, int data_type)
Eric Paris1c529062009-12-17 21:24:28 -0500166{
Eric Paris1968f5e2010-07-28 10:18:39 -0400167 __u32 marks_mask, marks_ignored_mask;
Eric Parise1c048b2010-10-28 17:21:58 -0400168 struct path *path = data;
Eric Paris1968f5e2010-07-28 10:18:39 -0400169
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
Eric Paris1c529062009-12-17 21:24:28 -0500174 /* if we don't have enough info to send an event to userspace say no */
Linus Torvalds20696012010-08-12 14:23:04 -0700175 if (data_type != FSNOTIFY_EVENT_PATH)
Eric Paris1c529062009-12-17 21:24:28 -0500176 return false;
177
Eric Parise1c048b2010-10-28 17:21:58 -0400178 /* 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
Eric Paris1968f5e2010-07-28 10:18:39 -0400183 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
Eric Paris8fcd6522010-10-28 17:21:59 -0400203 if (S_ISDIR(path->dentry->d_inode->i_mode) &&
204 (marks_ignored_mask & FS_ISDIR))
205 return false;
206
Eric Paris1968f5e2010-07-28 10:18:39 -0400207 if (event_mask & marks_mask & ~marks_ignored_mask)
208 return true;
209
210 return false;
Eric Paris1c529062009-12-17 21:24:28 -0500211}
212
Eric Paris4afeff82010-10-28 17:21:58 -0400213static 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
Eric Parisff0b16a2009-12-17 21:24:25 -0500222const struct fsnotify_ops fanotify_fsnotify_ops = {
223 .handle_event = fanotify_handle_event,
224 .should_send_event = fanotify_should_send_event,
Eric Paris4afeff82010-10-28 17:21:58 -0400225 .free_group_priv = fanotify_free_group_priv,
Eric Parisff0b16a2009-12-17 21:24:25 -0500226 .free_event_priv = NULL,
227 .freeing_mark = NULL,
228};