1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * fs/inotify_user.c - inotify support for userspace
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Authors:
6*4882a593Smuzhiyun * John McCutchan <ttb@tentacle.dhs.org>
7*4882a593Smuzhiyun * Robert Love <rml@novell.com>
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Copyright (C) 2005 John McCutchan
10*4882a593Smuzhiyun * Copyright 2006 Hewlett-Packard Development Company, L.P.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Copyright (C) 2009 Eric Paris <Red Hat Inc>
13*4882a593Smuzhiyun * inotify was largely rewriten to make use of the fsnotify infrastructure
14*4882a593Smuzhiyun */
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <linux/dcache.h> /* d_unlinked */
17*4882a593Smuzhiyun #include <linux/fs.h> /* struct inode */
18*4882a593Smuzhiyun #include <linux/fsnotify_backend.h>
19*4882a593Smuzhiyun #include <linux/inotify.h>
20*4882a593Smuzhiyun #include <linux/path.h> /* struct path */
21*4882a593Smuzhiyun #include <linux/slab.h> /* kmem_* */
22*4882a593Smuzhiyun #include <linux/types.h>
23*4882a593Smuzhiyun #include <linux/sched.h>
24*4882a593Smuzhiyun #include <linux/sched/user.h>
25*4882a593Smuzhiyun #include <linux/sched/mm.h>
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include "inotify.h"
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun * Check if 2 events contain the same information.
31*4882a593Smuzhiyun */
event_compare(struct fsnotify_event * old_fsn,struct fsnotify_event * new_fsn)32*4882a593Smuzhiyun static bool event_compare(struct fsnotify_event *old_fsn,
33*4882a593Smuzhiyun struct fsnotify_event *new_fsn)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun struct inotify_event_info *old, *new;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun old = INOTIFY_E(old_fsn);
38*4882a593Smuzhiyun new = INOTIFY_E(new_fsn);
39*4882a593Smuzhiyun if (old->mask & FS_IN_IGNORED)
40*4882a593Smuzhiyun return false;
41*4882a593Smuzhiyun if ((old->mask == new->mask) &&
42*4882a593Smuzhiyun (old->wd == new->wd) &&
43*4882a593Smuzhiyun (old->name_len == new->name_len) &&
44*4882a593Smuzhiyun (!old->name_len || !strcmp(old->name, new->name)))
45*4882a593Smuzhiyun return true;
46*4882a593Smuzhiyun return false;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
inotify_merge(struct list_head * list,struct fsnotify_event * event)49*4882a593Smuzhiyun static int inotify_merge(struct list_head *list,
50*4882a593Smuzhiyun struct fsnotify_event *event)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun struct fsnotify_event *last_event;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun last_event = list_entry(list->prev, struct fsnotify_event, list);
55*4882a593Smuzhiyun return event_compare(last_event, event);
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun
inotify_handle_inode_event(struct fsnotify_mark * inode_mark,u32 mask,struct inode * inode,struct inode * dir,const struct qstr * name,u32 cookie)58*4882a593Smuzhiyun int inotify_handle_inode_event(struct fsnotify_mark *inode_mark, u32 mask,
59*4882a593Smuzhiyun struct inode *inode, struct inode *dir,
60*4882a593Smuzhiyun const struct qstr *name, u32 cookie)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun struct inotify_inode_mark *i_mark;
63*4882a593Smuzhiyun struct inotify_event_info *event;
64*4882a593Smuzhiyun struct fsnotify_event *fsn_event;
65*4882a593Smuzhiyun struct fsnotify_group *group = inode_mark->group;
66*4882a593Smuzhiyun int ret;
67*4882a593Smuzhiyun int len = 0;
68*4882a593Smuzhiyun int alloc_len = sizeof(struct inotify_event_info);
69*4882a593Smuzhiyun struct mem_cgroup *old_memcg;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun if (name) {
72*4882a593Smuzhiyun len = name->len;
73*4882a593Smuzhiyun alloc_len += len + 1;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun pr_debug("%s: group=%p mark=%p mask=%x\n", __func__, group, inode_mark,
77*4882a593Smuzhiyun mask);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun i_mark = container_of(inode_mark, struct inotify_inode_mark,
80*4882a593Smuzhiyun fsn_mark);
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /*
83*4882a593Smuzhiyun * Whoever is interested in the event, pays for the allocation. Do not
84*4882a593Smuzhiyun * trigger OOM killer in the target monitoring memcg as it may have
85*4882a593Smuzhiyun * security repercussion.
86*4882a593Smuzhiyun */
87*4882a593Smuzhiyun old_memcg = set_active_memcg(group->memcg);
88*4882a593Smuzhiyun event = kmalloc(alloc_len, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
89*4882a593Smuzhiyun set_active_memcg(old_memcg);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun if (unlikely(!event)) {
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun * Treat lost event due to ENOMEM the same way as queue
94*4882a593Smuzhiyun * overflow to let userspace know event was lost.
95*4882a593Smuzhiyun */
96*4882a593Smuzhiyun fsnotify_queue_overflow(group);
97*4882a593Smuzhiyun return -ENOMEM;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun * We now report FS_ISDIR flag with MOVE_SELF and DELETE_SELF events
102*4882a593Smuzhiyun * for fanotify. inotify never reported IN_ISDIR with those events.
103*4882a593Smuzhiyun * It looks like an oversight, but to avoid the risk of breaking
104*4882a593Smuzhiyun * existing inotify programs, mask the flag out from those events.
105*4882a593Smuzhiyun */
106*4882a593Smuzhiyun if (mask & (IN_MOVE_SELF | IN_DELETE_SELF))
107*4882a593Smuzhiyun mask &= ~IN_ISDIR;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun fsn_event = &event->fse;
110*4882a593Smuzhiyun fsnotify_init_event(fsn_event, 0);
111*4882a593Smuzhiyun event->mask = mask;
112*4882a593Smuzhiyun event->wd = i_mark->wd;
113*4882a593Smuzhiyun event->sync_cookie = cookie;
114*4882a593Smuzhiyun event->name_len = len;
115*4882a593Smuzhiyun if (len)
116*4882a593Smuzhiyun strcpy(event->name, name->name);
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun ret = fsnotify_add_event(group, fsn_event, inotify_merge);
119*4882a593Smuzhiyun if (ret) {
120*4882a593Smuzhiyun /* Our event wasn't used in the end. Free it. */
121*4882a593Smuzhiyun fsnotify_destroy_event(group, fsn_event);
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun if (inode_mark->mask & IN_ONESHOT)
125*4882a593Smuzhiyun fsnotify_destroy_mark(inode_mark, group);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun return 0;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
inotify_freeing_mark(struct fsnotify_mark * fsn_mark,struct fsnotify_group * group)130*4882a593Smuzhiyun static void inotify_freeing_mark(struct fsnotify_mark *fsn_mark, struct fsnotify_group *group)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun inotify_ignored_and_remove_idr(fsn_mark, group);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /*
136*4882a593Smuzhiyun * This is NEVER supposed to be called. Inotify marks should either have been
137*4882a593Smuzhiyun * removed from the idr when the watch was removed or in the
138*4882a593Smuzhiyun * fsnotify_destroy_mark_by_group() call when the inotify instance was being
139*4882a593Smuzhiyun * torn down. This is only called if the idr is about to be freed but there
140*4882a593Smuzhiyun * are still marks in it.
141*4882a593Smuzhiyun */
idr_callback(int id,void * p,void * data)142*4882a593Smuzhiyun static int idr_callback(int id, void *p, void *data)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun struct fsnotify_mark *fsn_mark;
145*4882a593Smuzhiyun struct inotify_inode_mark *i_mark;
146*4882a593Smuzhiyun static bool warned = false;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun if (warned)
149*4882a593Smuzhiyun return 0;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun warned = true;
152*4882a593Smuzhiyun fsn_mark = p;
153*4882a593Smuzhiyun i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun WARN(1, "inotify closing but id=%d for fsn_mark=%p in group=%p still in "
156*4882a593Smuzhiyun "idr. Probably leaking memory\n", id, p, data);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /*
159*4882a593Smuzhiyun * I'm taking the liberty of assuming that the mark in question is a
160*4882a593Smuzhiyun * valid address and I'm dereferencing it. This might help to figure
161*4882a593Smuzhiyun * out why we got here and the panic is no worse than the original
162*4882a593Smuzhiyun * BUG() that was here.
163*4882a593Smuzhiyun */
164*4882a593Smuzhiyun if (fsn_mark)
165*4882a593Smuzhiyun printk(KERN_WARNING "fsn_mark->group=%p wd=%d\n",
166*4882a593Smuzhiyun fsn_mark->group, i_mark->wd);
167*4882a593Smuzhiyun return 0;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
inotify_free_group_priv(struct fsnotify_group * group)170*4882a593Smuzhiyun static void inotify_free_group_priv(struct fsnotify_group *group)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun /* ideally the idr is empty and we won't hit the BUG in the callback */
173*4882a593Smuzhiyun idr_for_each(&group->inotify_data.idr, idr_callback, group);
174*4882a593Smuzhiyun idr_destroy(&group->inotify_data.idr);
175*4882a593Smuzhiyun if (group->inotify_data.ucounts)
176*4882a593Smuzhiyun dec_inotify_instances(group->inotify_data.ucounts);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
inotify_free_event(struct fsnotify_event * fsn_event)179*4882a593Smuzhiyun static void inotify_free_event(struct fsnotify_event *fsn_event)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun kfree(INOTIFY_E(fsn_event));
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /* ding dong the mark is dead */
inotify_free_mark(struct fsnotify_mark * fsn_mark)185*4882a593Smuzhiyun static void inotify_free_mark(struct fsnotify_mark *fsn_mark)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun struct inotify_inode_mark *i_mark;
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun kmem_cache_free(inotify_inode_mark_cachep, i_mark);
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun const struct fsnotify_ops inotify_fsnotify_ops = {
195*4882a593Smuzhiyun .handle_inode_event = inotify_handle_inode_event,
196*4882a593Smuzhiyun .free_group_priv = inotify_free_group_priv,
197*4882a593Smuzhiyun .free_event = inotify_free_event,
198*4882a593Smuzhiyun .freeing_mark = inotify_freeing_mark,
199*4882a593Smuzhiyun .free_mark = inotify_free_mark,
200*4882a593Smuzhiyun };
201