Merge branch 'fsnotify' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
[sfrench/cifs-2.6.git] / fs / notify / fanotify / fanotify.c
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
14 #include "fanotify.h"
15
16 static bool should_merge(struct fsnotify_event *old_fsn,
17                          struct fsnotify_event *new_fsn)
18 {
19         struct fanotify_event_info *old, *new;
20
21         pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn);
22         old = FANOTIFY_E(old_fsn);
23         new = FANOTIFY_E(new_fsn);
24
25         if (old_fsn->inode == new_fsn->inode && old->tgid == new->tgid &&
26             old->path.mnt == new->path.mnt &&
27             old->path.dentry == new->path.dentry)
28                 return true;
29         return false;
30 }
31
32 /* and the list better be locked by something too! */
33 static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
34 {
35         struct fsnotify_event *test_event;
36
37         pr_debug("%s: list=%p event=%p\n", __func__, list, event);
38
39         /*
40          * Don't merge a permission event with any other event so that we know
41          * the event structure we have created in fanotify_handle_event() is the
42          * one we should check for permission response.
43          */
44         if (fanotify_is_perm_event(event->mask))
45                 return 0;
46
47         list_for_each_entry_reverse(test_event, list, list) {
48                 if (should_merge(test_event, event)) {
49                         test_event->mask |= event->mask;
50                         return 1;
51                 }
52         }
53
54         return 0;
55 }
56
57 static int fanotify_get_response(struct fsnotify_group *group,
58                                  struct fanotify_perm_event_info *event,
59                                  struct fsnotify_iter_info *iter_info)
60 {
61         int ret;
62
63         pr_debug("%s: group=%p event=%p\n", __func__, group, event);
64
65         wait_event(group->fanotify_data.access_waitq, event->response);
66
67         /* userspace responded, convert to something usable */
68         switch (event->response) {
69         case FAN_ALLOW:
70                 ret = 0;
71                 break;
72         case FAN_DENY:
73         default:
74                 ret = -EPERM;
75         }
76         event->response = 0;
77
78         pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
79                  group, event, ret);
80         
81         return ret;
82 }
83
84 static bool fanotify_should_send_event(struct fsnotify_mark *inode_mark,
85                                        struct fsnotify_mark *vfsmnt_mark,
86                                        u32 event_mask,
87                                        const void *data, int data_type)
88 {
89         __u32 marks_mask, marks_ignored_mask;
90         const struct path *path = data;
91
92         pr_debug("%s: inode_mark=%p vfsmnt_mark=%p mask=%x data=%p"
93                  " data_type=%d\n", __func__, inode_mark, vfsmnt_mark,
94                  event_mask, data, data_type);
95
96         /* if we don't have enough info to send an event to userspace say no */
97         if (data_type != FSNOTIFY_EVENT_PATH)
98                 return false;
99
100         /* sorry, fanotify only gives a damn about files and dirs */
101         if (!d_is_reg(path->dentry) &&
102             !d_can_lookup(path->dentry))
103                 return false;
104
105         if (inode_mark && vfsmnt_mark) {
106                 marks_mask = (vfsmnt_mark->mask | inode_mark->mask);
107                 marks_ignored_mask = (vfsmnt_mark->ignored_mask | inode_mark->ignored_mask);
108         } else if (inode_mark) {
109                 /*
110                  * if the event is for a child and this inode doesn't care about
111                  * events on the child, don't send it!
112                  */
113                 if ((event_mask & FS_EVENT_ON_CHILD) &&
114                     !(inode_mark->mask & FS_EVENT_ON_CHILD))
115                         return false;
116                 marks_mask = inode_mark->mask;
117                 marks_ignored_mask = inode_mark->ignored_mask;
118         } else if (vfsmnt_mark) {
119                 marks_mask = vfsmnt_mark->mask;
120                 marks_ignored_mask = vfsmnt_mark->ignored_mask;
121         } else {
122                 BUG();
123         }
124
125         if (d_is_dir(path->dentry) &&
126             !(marks_mask & FS_ISDIR & ~marks_ignored_mask))
127                 return false;
128
129         if (event_mask & FAN_ALL_OUTGOING_EVENTS & marks_mask &
130                                  ~marks_ignored_mask)
131                 return true;
132
133         return false;
134 }
135
136 struct fanotify_event_info *fanotify_alloc_event(struct inode *inode, u32 mask,
137                                                  const struct path *path)
138 {
139         struct fanotify_event_info *event;
140
141         if (fanotify_is_perm_event(mask)) {
142                 struct fanotify_perm_event_info *pevent;
143
144                 pevent = kmem_cache_alloc(fanotify_perm_event_cachep,
145                                           GFP_KERNEL);
146                 if (!pevent)
147                         return NULL;
148                 event = &pevent->fae;
149                 pevent->response = 0;
150                 goto init;
151         }
152         event = kmem_cache_alloc(fanotify_event_cachep, GFP_KERNEL);
153         if (!event)
154                 return NULL;
155 init: __maybe_unused
156         fsnotify_init_event(&event->fse, inode, mask);
157         event->tgid = get_pid(task_tgid(current));
158         if (path) {
159                 event->path = *path;
160                 path_get(&event->path);
161         } else {
162                 event->path.mnt = NULL;
163                 event->path.dentry = NULL;
164         }
165         return event;
166 }
167
168 static int fanotify_handle_event(struct fsnotify_group *group,
169                                  struct inode *inode,
170                                  struct fsnotify_mark *inode_mark,
171                                  struct fsnotify_mark *fanotify_mark,
172                                  u32 mask, const void *data, int data_type,
173                                  const unsigned char *file_name, u32 cookie,
174                                  struct fsnotify_iter_info *iter_info)
175 {
176         int ret = 0;
177         struct fanotify_event_info *event;
178         struct fsnotify_event *fsn_event;
179
180         BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
181         BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
182         BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
183         BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
184         BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
185         BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
186         BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
187         BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
188         BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
189         BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
190
191         if (!fanotify_should_send_event(inode_mark, fanotify_mark, mask, data,
192                                         data_type))
193                 return 0;
194
195         pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode,
196                  mask);
197
198         if (fanotify_is_perm_event(mask)) {
199                 /*
200                  * fsnotify_prepare_user_wait() fails if we race with mark
201                  * deletion.  Just let the operation pass in that case.
202                  */
203                 if (!fsnotify_prepare_user_wait(iter_info))
204                         return 0;
205         }
206
207         event = fanotify_alloc_event(inode, mask, data);
208         ret = -ENOMEM;
209         if (unlikely(!event))
210                 goto finish;
211
212         fsn_event = &event->fse;
213         ret = fsnotify_add_event(group, fsn_event, fanotify_merge);
214         if (ret) {
215                 /* Permission events shouldn't be merged */
216                 BUG_ON(ret == 1 && mask & FAN_ALL_PERM_EVENTS);
217                 /* Our event wasn't used in the end. Free it. */
218                 fsnotify_destroy_event(group, fsn_event);
219
220                 ret = 0;
221         } else if (fanotify_is_perm_event(mask)) {
222                 ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event),
223                                             iter_info);
224                 fsnotify_destroy_event(group, fsn_event);
225         }
226 finish:
227         if (fanotify_is_perm_event(mask))
228                 fsnotify_finish_user_wait(iter_info);
229
230         return ret;
231 }
232
233 static void fanotify_free_group_priv(struct fsnotify_group *group)
234 {
235         struct user_struct *user;
236
237         user = group->fanotify_data.user;
238         atomic_dec(&user->fanotify_listeners);
239         free_uid(user);
240 }
241
242 static void fanotify_free_event(struct fsnotify_event *fsn_event)
243 {
244         struct fanotify_event_info *event;
245
246         event = FANOTIFY_E(fsn_event);
247         path_put(&event->path);
248         put_pid(event->tgid);
249         if (fanotify_is_perm_event(fsn_event->mask)) {
250                 kmem_cache_free(fanotify_perm_event_cachep,
251                                 FANOTIFY_PE(fsn_event));
252                 return;
253         }
254         kmem_cache_free(fanotify_event_cachep, event);
255 }
256
257 static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
258 {
259         kmem_cache_free(fanotify_mark_cache, fsn_mark);
260 }
261
262 const struct fsnotify_ops fanotify_fsnotify_ops = {
263         .handle_event = fanotify_handle_event,
264         .free_group_priv = fanotify_free_group_priv,
265         .free_event = fanotify_free_event,
266         .free_mark = fanotify_free_mark,
267 };