fanotify.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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/sched/signal.h>
  12. #include <linux/types.h>
  13. #include <linux/wait.h>
  14. #include <linux/audit.h>
  15. #include <linux/sched/mm.h>
  16. #include <linux/statfs.h>
  17. #include "fanotify.h"
  18. static bool fanotify_path_equal(struct path *p1, struct path *p2)
  19. {
  20. return p1->mnt == p2->mnt && p1->dentry == p2->dentry;
  21. }
  22. static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1,
  23. __kernel_fsid_t *fsid2)
  24. {
  25. return fsid1->val[0] == fsid2->val[0] && fsid1->val[1] == fsid2->val[1];
  26. }
  27. static bool fanotify_fh_equal(struct fanotify_fh *fh1,
  28. struct fanotify_fh *fh2)
  29. {
  30. if (fh1->type != fh2->type || fh1->len != fh2->len)
  31. return false;
  32. return !fh1->len ||
  33. !memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len);
  34. }
  35. static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1,
  36. struct fanotify_fid_event *ffe2)
  37. {
  38. /* Do not merge fid events without object fh */
  39. if (!ffe1->object_fh.len)
  40. return false;
  41. return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) &&
  42. fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh);
  43. }
  44. static bool fanotify_info_equal(struct fanotify_info *info1,
  45. struct fanotify_info *info2)
  46. {
  47. if (info1->dir_fh_totlen != info2->dir_fh_totlen ||
  48. info1->file_fh_totlen != info2->file_fh_totlen ||
  49. info1->name_len != info2->name_len)
  50. return false;
  51. if (info1->dir_fh_totlen &&
  52. !fanotify_fh_equal(fanotify_info_dir_fh(info1),
  53. fanotify_info_dir_fh(info2)))
  54. return false;
  55. if (info1->file_fh_totlen &&
  56. !fanotify_fh_equal(fanotify_info_file_fh(info1),
  57. fanotify_info_file_fh(info2)))
  58. return false;
  59. return !info1->name_len ||
  60. !memcmp(fanotify_info_name(info1), fanotify_info_name(info2),
  61. info1->name_len);
  62. }
  63. static bool fanotify_name_event_equal(struct fanotify_name_event *fne1,
  64. struct fanotify_name_event *fne2)
  65. {
  66. struct fanotify_info *info1 = &fne1->info;
  67. struct fanotify_info *info2 = &fne2->info;
  68. /* Do not merge name events without dir fh */
  69. if (!info1->dir_fh_totlen)
  70. return false;
  71. if (!fanotify_fsid_equal(&fne1->fsid, &fne2->fsid))
  72. return false;
  73. return fanotify_info_equal(info1, info2);
  74. }
  75. static bool fanotify_should_merge(struct fsnotify_event *old_fsn,
  76. struct fsnotify_event *new_fsn)
  77. {
  78. struct fanotify_event *old, *new;
  79. pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn);
  80. old = FANOTIFY_E(old_fsn);
  81. new = FANOTIFY_E(new_fsn);
  82. if (old_fsn->objectid != new_fsn->objectid ||
  83. old->type != new->type || old->pid != new->pid)
  84. return false;
  85. /*
  86. * We want to merge many dirent events in the same dir (i.e.
  87. * creates/unlinks/renames), but we do not want to merge dirent
  88. * events referring to subdirs with dirent events referring to
  89. * non subdirs, otherwise, user won't be able to tell from a
  90. * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+
  91. * unlink pair or rmdir+create pair of events.
  92. */
  93. if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR))
  94. return false;
  95. switch (old->type) {
  96. case FANOTIFY_EVENT_TYPE_PATH:
  97. return fanotify_path_equal(fanotify_event_path(old),
  98. fanotify_event_path(new));
  99. case FANOTIFY_EVENT_TYPE_FID:
  100. return fanotify_fid_event_equal(FANOTIFY_FE(old),
  101. FANOTIFY_FE(new));
  102. case FANOTIFY_EVENT_TYPE_FID_NAME:
  103. return fanotify_name_event_equal(FANOTIFY_NE(old),
  104. FANOTIFY_NE(new));
  105. default:
  106. WARN_ON_ONCE(1);
  107. }
  108. return false;
  109. }
  110. /* Limit event merges to limit CPU overhead per event */
  111. #define FANOTIFY_MAX_MERGE_EVENTS 128
  112. /* and the list better be locked by something too! */
  113. static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
  114. {
  115. struct fsnotify_event *test_event;
  116. struct fanotify_event *new;
  117. int i = 0;
  118. pr_debug("%s: list=%p event=%p\n", __func__, list, event);
  119. new = FANOTIFY_E(event);
  120. /*
  121. * Don't merge a permission event with any other event so that we know
  122. * the event structure we have created in fanotify_handle_event() is the
  123. * one we should check for permission response.
  124. */
  125. if (fanotify_is_perm_event(new->mask))
  126. return 0;
  127. list_for_each_entry_reverse(test_event, list, list) {
  128. if (++i > FANOTIFY_MAX_MERGE_EVENTS)
  129. break;
  130. if (fanotify_should_merge(test_event, event)) {
  131. FANOTIFY_E(test_event)->mask |= new->mask;
  132. return 1;
  133. }
  134. }
  135. return 0;
  136. }
  137. /*
  138. * Wait for response to permission event. The function also takes care of
  139. * freeing the permission event (or offloads that in case the wait is canceled
  140. * by a signal). The function returns 0 in case access got allowed by userspace,
  141. * -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case
  142. * the wait got interrupted by a signal.
  143. */
  144. static int fanotify_get_response(struct fsnotify_group *group,
  145. struct fanotify_perm_event *event,
  146. struct fsnotify_iter_info *iter_info)
  147. {
  148. int ret;
  149. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  150. ret = wait_event_killable(group->fanotify_data.access_waitq,
  151. event->state == FAN_EVENT_ANSWERED);
  152. /* Signal pending? */
  153. if (ret < 0) {
  154. spin_lock(&group->notification_lock);
  155. /* Event reported to userspace and no answer yet? */
  156. if (event->state == FAN_EVENT_REPORTED) {
  157. /* Event will get freed once userspace answers to it */
  158. event->state = FAN_EVENT_CANCELED;
  159. spin_unlock(&group->notification_lock);
  160. return ret;
  161. }
  162. /* Event not yet reported? Just remove it. */
  163. if (event->state == FAN_EVENT_INIT)
  164. fsnotify_remove_queued_event(group, &event->fae.fse);
  165. /*
  166. * Event may be also answered in case signal delivery raced
  167. * with wakeup. In that case we have nothing to do besides
  168. * freeing the event and reporting error.
  169. */
  170. spin_unlock(&group->notification_lock);
  171. goto out;
  172. }
  173. /* userspace responded, convert to something usable */
  174. switch (event->response & ~FAN_AUDIT) {
  175. case FAN_ALLOW:
  176. ret = 0;
  177. break;
  178. case FAN_DENY:
  179. default:
  180. ret = -EPERM;
  181. }
  182. /* Check if the response should be audited */
  183. if (event->response & FAN_AUDIT)
  184. audit_fanotify(event->response & ~FAN_AUDIT);
  185. pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
  186. group, event, ret);
  187. out:
  188. fsnotify_destroy_event(group, &event->fae.fse);
  189. return ret;
  190. }
  191. /*
  192. * This function returns a mask for an event that only contains the flags
  193. * that have been specifically requested by the user. Flags that may have
  194. * been included within the event mask, but have not been explicitly
  195. * requested by the user, will not be present in the returned mask.
  196. */
  197. static u32 fanotify_group_event_mask(struct fsnotify_group *group,
  198. struct fsnotify_iter_info *iter_info,
  199. u32 event_mask, const void *data,
  200. int data_type, struct inode *dir)
  201. {
  202. __u32 marks_mask = 0, marks_ignored_mask = 0;
  203. __u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS |
  204. FANOTIFY_EVENT_FLAGS;
  205. const struct path *path = fsnotify_data_path(data, data_type);
  206. unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
  207. struct fsnotify_mark *mark;
  208. int type;
  209. pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n",
  210. __func__, iter_info->report_mask, event_mask, data, data_type);
  211. if (!fid_mode) {
  212. /* Do we have path to open a file descriptor? */
  213. if (!path)
  214. return 0;
  215. /* Path type events are only relevant for files and dirs */
  216. if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry))
  217. return 0;
  218. } else if (!(fid_mode & FAN_REPORT_FID)) {
  219. /* Do we have a directory inode to report? */
  220. if (!dir && !(event_mask & FS_ISDIR))
  221. return 0;
  222. }
  223. fsnotify_foreach_obj_type(type) {
  224. if (!fsnotify_iter_should_report_type(iter_info, type))
  225. continue;
  226. mark = iter_info->marks[type];
  227. /* Apply ignore mask regardless of ISDIR and ON_CHILD flags */
  228. marks_ignored_mask |= mark->ignored_mask;
  229. /*
  230. * If the event is on dir and this mark doesn't care about
  231. * events on dir, don't send it!
  232. */
  233. if (event_mask & FS_ISDIR && !(mark->mask & FS_ISDIR))
  234. continue;
  235. /*
  236. * If the event is on a child and this mark is on a parent not
  237. * watching children, don't send it!
  238. */
  239. if (type == FSNOTIFY_OBJ_TYPE_PARENT &&
  240. !(mark->mask & FS_EVENT_ON_CHILD))
  241. continue;
  242. marks_mask |= mark->mask;
  243. }
  244. test_mask = event_mask & marks_mask & ~marks_ignored_mask;
  245. /*
  246. * For dirent modification events (create/delete/move) that do not carry
  247. * the child entry name information, we report FAN_ONDIR for mkdir/rmdir
  248. * so user can differentiate them from creat/unlink.
  249. *
  250. * For backward compatibility and consistency, do not report FAN_ONDIR
  251. * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR
  252. * to user in fid mode for all event types.
  253. *
  254. * We never report FAN_EVENT_ON_CHILD to user, but we do pass it in to
  255. * fanotify_alloc_event() when group is reporting fid as indication
  256. * that event happened on child.
  257. */
  258. if (fid_mode) {
  259. /* Do not report event flags without any event */
  260. if (!(test_mask & ~FANOTIFY_EVENT_FLAGS))
  261. return 0;
  262. } else {
  263. user_mask &= ~FANOTIFY_EVENT_FLAGS;
  264. }
  265. return test_mask & user_mask;
  266. }
  267. /*
  268. * Check size needed to encode fanotify_fh.
  269. *
  270. * Return size of encoded fh without fanotify_fh header.
  271. * Return 0 on failure to encode.
  272. */
  273. static int fanotify_encode_fh_len(struct inode *inode)
  274. {
  275. int dwords = 0;
  276. if (!inode)
  277. return 0;
  278. exportfs_encode_inode_fh(inode, NULL, &dwords, NULL);
  279. return dwords << 2;
  280. }
  281. /*
  282. * Encode fanotify_fh.
  283. *
  284. * Return total size of encoded fh including fanotify_fh header.
  285. * Return 0 on failure to encode.
  286. */
  287. static int fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
  288. unsigned int fh_len, gfp_t gfp)
  289. {
  290. int dwords, type = 0;
  291. char *ext_buf = NULL;
  292. void *buf = fh->buf;
  293. int err;
  294. fh->type = FILEID_ROOT;
  295. fh->len = 0;
  296. fh->flags = 0;
  297. if (!inode)
  298. return 0;
  299. /*
  300. * !gpf means preallocated variable size fh, but fh_len could
  301. * be zero in that case if encoding fh len failed.
  302. */
  303. err = -ENOENT;
  304. if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4))
  305. goto out_err;
  306. /* No external buffer in a variable size allocated fh */
  307. if (gfp && fh_len > FANOTIFY_INLINE_FH_LEN) {
  308. /* Treat failure to allocate fh as failure to encode fh */
  309. err = -ENOMEM;
  310. ext_buf = kmalloc(fh_len, gfp);
  311. if (!ext_buf)
  312. goto out_err;
  313. *fanotify_fh_ext_buf_ptr(fh) = ext_buf;
  314. buf = ext_buf;
  315. fh->flags |= FANOTIFY_FH_FLAG_EXT_BUF;
  316. }
  317. dwords = fh_len >> 2;
  318. type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL);
  319. err = -EINVAL;
  320. if (!type || type == FILEID_INVALID || fh_len != dwords << 2)
  321. goto out_err;
  322. fh->type = type;
  323. fh->len = fh_len;
  324. return FANOTIFY_FH_HDR_LEN + fh_len;
  325. out_err:
  326. pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n",
  327. type, fh_len, err);
  328. kfree(ext_buf);
  329. *fanotify_fh_ext_buf_ptr(fh) = NULL;
  330. /* Report the event without a file identifier on encode error */
  331. fh->type = FILEID_INVALID;
  332. fh->len = 0;
  333. return 0;
  334. }
  335. /*
  336. * The inode to use as identifier when reporting fid depends on the event.
  337. * Report the modified directory inode on dirent modification events.
  338. * Report the "victim" inode otherwise.
  339. * For example:
  340. * FS_ATTRIB reports the child inode even if reported on a watched parent.
  341. * FS_CREATE reports the modified dir inode and not the created inode.
  342. */
  343. static struct inode *fanotify_fid_inode(u32 event_mask, const void *data,
  344. int data_type, struct inode *dir)
  345. {
  346. if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
  347. return dir;
  348. return fsnotify_data_inode(data, data_type);
  349. }
  350. /*
  351. * The inode to use as identifier when reporting dir fid depends on the event.
  352. * Report the modified directory inode on dirent modification events.
  353. * Report the "victim" inode if "victim" is a directory.
  354. * Report the parent inode if "victim" is not a directory and event is
  355. * reported to parent.
  356. * Otherwise, do not report dir fid.
  357. */
  358. static struct inode *fanotify_dfid_inode(u32 event_mask, const void *data,
  359. int data_type, struct inode *dir)
  360. {
  361. struct inode *inode = fsnotify_data_inode(data, data_type);
  362. if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
  363. return dir;
  364. if (S_ISDIR(inode->i_mode))
  365. return inode;
  366. return dir;
  367. }
  368. static struct fanotify_event *fanotify_alloc_path_event(const struct path *path,
  369. gfp_t gfp)
  370. {
  371. struct fanotify_path_event *pevent;
  372. pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
  373. if (!pevent)
  374. return NULL;
  375. pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH;
  376. pevent->path = *path;
  377. path_get(path);
  378. return &pevent->fae;
  379. }
  380. static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
  381. gfp_t gfp)
  382. {
  383. struct fanotify_perm_event *pevent;
  384. pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
  385. if (!pevent)
  386. return NULL;
  387. pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH_PERM;
  388. pevent->response = 0;
  389. pevent->state = FAN_EVENT_INIT;
  390. pevent->path = *path;
  391. path_get(path);
  392. return &pevent->fae;
  393. }
  394. static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id,
  395. __kernel_fsid_t *fsid,
  396. gfp_t gfp)
  397. {
  398. struct fanotify_fid_event *ffe;
  399. ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
  400. if (!ffe)
  401. return NULL;
  402. ffe->fae.type = FANOTIFY_EVENT_TYPE_FID;
  403. ffe->fsid = *fsid;
  404. fanotify_encode_fh(&ffe->object_fh, id, fanotify_encode_fh_len(id),
  405. gfp);
  406. return &ffe->fae;
  407. }
  408. static struct fanotify_event *fanotify_alloc_name_event(struct inode *id,
  409. __kernel_fsid_t *fsid,
  410. const struct qstr *file_name,
  411. struct inode *child,
  412. gfp_t gfp)
  413. {
  414. struct fanotify_name_event *fne;
  415. struct fanotify_info *info;
  416. struct fanotify_fh *dfh, *ffh;
  417. unsigned int dir_fh_len = fanotify_encode_fh_len(id);
  418. unsigned int child_fh_len = fanotify_encode_fh_len(child);
  419. unsigned int size;
  420. size = sizeof(*fne) + FANOTIFY_FH_HDR_LEN + dir_fh_len;
  421. if (child_fh_len)
  422. size += FANOTIFY_FH_HDR_LEN + child_fh_len;
  423. if (file_name)
  424. size += file_name->len + 1;
  425. fne = kmalloc(size, gfp);
  426. if (!fne)
  427. return NULL;
  428. fne->fae.type = FANOTIFY_EVENT_TYPE_FID_NAME;
  429. fne->fsid = *fsid;
  430. info = &fne->info;
  431. fanotify_info_init(info);
  432. dfh = fanotify_info_dir_fh(info);
  433. info->dir_fh_totlen = fanotify_encode_fh(dfh, id, dir_fh_len, 0);
  434. if (child_fh_len) {
  435. ffh = fanotify_info_file_fh(info);
  436. info->file_fh_totlen = fanotify_encode_fh(ffh, child, child_fh_len, 0);
  437. }
  438. if (file_name)
  439. fanotify_info_copy_name(info, file_name);
  440. pr_debug("%s: ino=%lu size=%u dir_fh_len=%u child_fh_len=%u name_len=%u name='%.*s'\n",
  441. __func__, id->i_ino, size, dir_fh_len, child_fh_len,
  442. info->name_len, info->name_len, fanotify_info_name(info));
  443. return &fne->fae;
  444. }
  445. static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
  446. u32 mask, const void *data,
  447. int data_type, struct inode *dir,
  448. const struct qstr *file_name,
  449. __kernel_fsid_t *fsid)
  450. {
  451. struct fanotify_event *event = NULL;
  452. gfp_t gfp = GFP_KERNEL_ACCOUNT;
  453. struct inode *id = fanotify_fid_inode(mask, data, data_type, dir);
  454. struct inode *dirid = fanotify_dfid_inode(mask, data, data_type, dir);
  455. const struct path *path = fsnotify_data_path(data, data_type);
  456. unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
  457. struct mem_cgroup *old_memcg;
  458. struct inode *child = NULL;
  459. bool name_event = false;
  460. if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) {
  461. /*
  462. * With both flags FAN_REPORT_DIR_FID and FAN_REPORT_FID, we
  463. * report the child fid for events reported on a non-dir child
  464. * in addition to reporting the parent fid and maybe child name.
  465. */
  466. if ((fid_mode & FAN_REPORT_FID) &&
  467. id != dirid && !(mask & FAN_ONDIR))
  468. child = id;
  469. id = dirid;
  470. /*
  471. * We record file name only in a group with FAN_REPORT_NAME
  472. * and when we have a directory inode to report.
  473. *
  474. * For directory entry modification event, we record the fid of
  475. * the directory and the name of the modified entry.
  476. *
  477. * For event on non-directory that is reported to parent, we
  478. * record the fid of the parent and the name of the child.
  479. *
  480. * Even if not reporting name, we need a variable length
  481. * fanotify_name_event if reporting both parent and child fids.
  482. */
  483. if (!(fid_mode & FAN_REPORT_NAME)) {
  484. name_event = !!child;
  485. file_name = NULL;
  486. } else if ((mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
  487. !(mask & FAN_ONDIR)) {
  488. name_event = true;
  489. }
  490. }
  491. /*
  492. * For queues with unlimited length lost events are not expected and
  493. * can possibly have security implications. Avoid losing events when
  494. * memory is short. For the limited size queues, avoid OOM killer in the
  495. * target monitoring memcg as it may have security repercussion.
  496. */
  497. if (group->max_events == UINT_MAX)
  498. gfp |= __GFP_NOFAIL;
  499. else
  500. gfp |= __GFP_RETRY_MAYFAIL;
  501. /* Whoever is interested in the event, pays for the allocation. */
  502. old_memcg = set_active_memcg(group->memcg);
  503. if (fanotify_is_perm_event(mask)) {
  504. event = fanotify_alloc_perm_event(path, gfp);
  505. } else if (name_event && (file_name || child)) {
  506. event = fanotify_alloc_name_event(id, fsid, file_name, child,
  507. gfp);
  508. } else if (fid_mode) {
  509. event = fanotify_alloc_fid_event(id, fsid, gfp);
  510. } else {
  511. event = fanotify_alloc_path_event(path, gfp);
  512. }
  513. if (!event)
  514. goto out;
  515. /*
  516. * Use the victim inode instead of the watching inode as the id for
  517. * event queue, so event reported on parent is merged with event
  518. * reported on child when both directory and child watches exist.
  519. */
  520. fanotify_init_event(event, (unsigned long)id, mask);
  521. if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
  522. event->pid = get_pid(task_pid(current));
  523. else
  524. event->pid = get_pid(task_tgid(current));
  525. out:
  526. set_active_memcg(old_memcg);
  527. return event;
  528. }
  529. /*
  530. * Get cached fsid of the filesystem containing the object from any connector.
  531. * All connectors are supposed to have the same fsid, but we do not verify that
  532. * here.
  533. */
  534. static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)
  535. {
  536. int type;
  537. __kernel_fsid_t fsid = {};
  538. fsnotify_foreach_obj_type(type) {
  539. struct fsnotify_mark_connector *conn;
  540. if (!fsnotify_iter_should_report_type(iter_info, type))
  541. continue;
  542. conn = READ_ONCE(iter_info->marks[type]->connector);
  543. /* Mark is just getting destroyed or created? */
  544. if (!conn)
  545. continue;
  546. if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID))
  547. continue;
  548. /* Pairs with smp_wmb() in fsnotify_add_mark_list() */
  549. smp_rmb();
  550. fsid = conn->fsid;
  551. if (WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1]))
  552. continue;
  553. return fsid;
  554. }
  555. return fsid;
  556. }
  557. static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,
  558. const void *data, int data_type,
  559. struct inode *dir,
  560. const struct qstr *file_name, u32 cookie,
  561. struct fsnotify_iter_info *iter_info)
  562. {
  563. int ret = 0;
  564. struct fanotify_event *event;
  565. struct fsnotify_event *fsn_event;
  566. __kernel_fsid_t fsid = {};
  567. BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
  568. BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
  569. BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB);
  570. BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
  571. BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
  572. BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
  573. BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO);
  574. BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM);
  575. BUILD_BUG_ON(FAN_CREATE != FS_CREATE);
  576. BUILD_BUG_ON(FAN_DELETE != FS_DELETE);
  577. BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF);
  578. BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF);
  579. BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
  580. BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
  581. BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
  582. BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
  583. BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
  584. BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC);
  585. BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM);
  586. BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 19);
  587. mask = fanotify_group_event_mask(group, iter_info, mask, data,
  588. data_type, dir);
  589. if (!mask)
  590. return 0;
  591. pr_debug("%s: group=%p mask=%x\n", __func__, group, mask);
  592. if (fanotify_is_perm_event(mask)) {
  593. /*
  594. * fsnotify_prepare_user_wait() fails if we race with mark
  595. * deletion. Just let the operation pass in that case.
  596. */
  597. if (!fsnotify_prepare_user_wait(iter_info))
  598. return 0;
  599. }
  600. if (FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS)) {
  601. fsid = fanotify_get_fsid(iter_info);
  602. /* Racing with mark destruction or creation? */
  603. if (!fsid.val[0] && !fsid.val[1])
  604. return 0;
  605. }
  606. event = fanotify_alloc_event(group, mask, data, data_type, dir,
  607. file_name, &fsid);
  608. ret = -ENOMEM;
  609. if (unlikely(!event)) {
  610. /*
  611. * We don't queue overflow events for permission events as
  612. * there the access is denied and so no event is in fact lost.
  613. */
  614. if (!fanotify_is_perm_event(mask))
  615. fsnotify_queue_overflow(group);
  616. goto finish;
  617. }
  618. fsn_event = &event->fse;
  619. ret = fsnotify_add_event(group, fsn_event, fanotify_merge);
  620. if (ret) {
  621. /* Permission events shouldn't be merged */
  622. BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS);
  623. /* Our event wasn't used in the end. Free it. */
  624. fsnotify_destroy_event(group, fsn_event);
  625. ret = 0;
  626. } else if (fanotify_is_perm_event(mask)) {
  627. ret = fanotify_get_response(group, FANOTIFY_PERM(event),
  628. iter_info);
  629. }
  630. finish:
  631. if (fanotify_is_perm_event(mask))
  632. fsnotify_finish_user_wait(iter_info);
  633. return ret;
  634. }
  635. static void fanotify_free_group_priv(struct fsnotify_group *group)
  636. {
  637. struct user_struct *user;
  638. user = group->fanotify_data.user;
  639. atomic_dec(&user->fanotify_listeners);
  640. free_uid(user);
  641. }
  642. static void fanotify_free_path_event(struct fanotify_event *event)
  643. {
  644. path_put(fanotify_event_path(event));
  645. kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event));
  646. }
  647. static void fanotify_free_perm_event(struct fanotify_event *event)
  648. {
  649. path_put(fanotify_event_path(event));
  650. kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event));
  651. }
  652. static void fanotify_free_fid_event(struct fanotify_event *event)
  653. {
  654. struct fanotify_fid_event *ffe = FANOTIFY_FE(event);
  655. if (fanotify_fh_has_ext_buf(&ffe->object_fh))
  656. kfree(fanotify_fh_ext_buf(&ffe->object_fh));
  657. kmem_cache_free(fanotify_fid_event_cachep, ffe);
  658. }
  659. static void fanotify_free_name_event(struct fanotify_event *event)
  660. {
  661. kfree(FANOTIFY_NE(event));
  662. }
  663. static void fanotify_free_event(struct fsnotify_event *fsn_event)
  664. {
  665. struct fanotify_event *event;
  666. event = FANOTIFY_E(fsn_event);
  667. put_pid(event->pid);
  668. switch (event->type) {
  669. case FANOTIFY_EVENT_TYPE_PATH:
  670. fanotify_free_path_event(event);
  671. break;
  672. case FANOTIFY_EVENT_TYPE_PATH_PERM:
  673. fanotify_free_perm_event(event);
  674. break;
  675. case FANOTIFY_EVENT_TYPE_FID:
  676. fanotify_free_fid_event(event);
  677. break;
  678. case FANOTIFY_EVENT_TYPE_FID_NAME:
  679. fanotify_free_name_event(event);
  680. break;
  681. case FANOTIFY_EVENT_TYPE_OVERFLOW:
  682. kfree(event);
  683. break;
  684. default:
  685. WARN_ON_ONCE(1);
  686. }
  687. }
  688. static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
  689. {
  690. kmem_cache_free(fanotify_mark_cache, fsn_mark);
  691. }
  692. const struct fsnotify_ops fanotify_fsnotify_ops = {
  693. .handle_event = fanotify_handle_event,
  694. .free_group_priv = fanotify_free_group_priv,
  695. .free_event = fanotify_free_event,
  696. .free_mark = fanotify_free_mark,
  697. };