fanotify_user.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. #include <linux/fanotify.h>
  2. #include <linux/fcntl.h>
  3. #include <linux/file.h>
  4. #include <linux/fs.h>
  5. #include <linux/anon_inodes.h>
  6. #include <linux/fsnotify_backend.h>
  7. #include <linux/init.h>
  8. #include <linux/mount.h>
  9. #include <linux/namei.h>
  10. #include <linux/poll.h>
  11. #include <linux/security.h>
  12. #include <linux/syscalls.h>
  13. #include <linux/slab.h>
  14. #include <linux/types.h>
  15. #include <linux/uaccess.h>
  16. #include <asm/ioctls.h>
  17. extern const struct fsnotify_ops fanotify_fsnotify_ops;
  18. static struct kmem_cache *fanotify_mark_cache __read_mostly;
  19. static struct kmem_cache *fanotify_response_event_cache __read_mostly;
  20. struct fanotify_response_event {
  21. struct list_head list;
  22. __s32 fd;
  23. struct fsnotify_event *event;
  24. };
  25. /*
  26. * Get an fsnotify notification event if one exists and is small
  27. * enough to fit in "count". Return an error pointer if the count
  28. * is not large enough.
  29. *
  30. * Called with the group->notification_mutex held.
  31. */
  32. static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
  33. size_t count)
  34. {
  35. BUG_ON(!mutex_is_locked(&group->notification_mutex));
  36. pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
  37. if (fsnotify_notify_queue_is_empty(group))
  38. return NULL;
  39. if (FAN_EVENT_METADATA_LEN > count)
  40. return ERR_PTR(-EINVAL);
  41. /* held the notification_mutex the whole time, so this is the
  42. * same event we peeked above */
  43. return fsnotify_remove_notify_event(group);
  44. }
  45. static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
  46. {
  47. int client_fd;
  48. struct dentry *dentry;
  49. struct vfsmount *mnt;
  50. struct file *new_file;
  51. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  52. client_fd = get_unused_fd();
  53. if (client_fd < 0)
  54. return client_fd;
  55. if (event->data_type != FSNOTIFY_EVENT_PATH) {
  56. WARN_ON(1);
  57. put_unused_fd(client_fd);
  58. return -EINVAL;
  59. }
  60. /*
  61. * we need a new file handle for the userspace program so it can read even if it was
  62. * originally opened O_WRONLY.
  63. */
  64. dentry = dget(event->path.dentry);
  65. mnt = mntget(event->path.mnt);
  66. /* it's possible this event was an overflow event. in that case dentry and mnt
  67. * are NULL; That's fine, just don't call dentry open */
  68. if (dentry && mnt)
  69. new_file = dentry_open(dentry, mnt,
  70. group->fanotify_data.f_flags | FMODE_NONOTIFY,
  71. current_cred());
  72. else
  73. new_file = ERR_PTR(-EOVERFLOW);
  74. if (IS_ERR(new_file)) {
  75. /*
  76. * we still send an event even if we can't open the file. this
  77. * can happen when say tasks are gone and we try to open their
  78. * /proc files or we try to open a WRONLY file like in sysfs
  79. * we just send the errno to userspace since there isn't much
  80. * else we can do.
  81. */
  82. put_unused_fd(client_fd);
  83. client_fd = PTR_ERR(new_file);
  84. } else {
  85. fd_install(client_fd, new_file);
  86. }
  87. return client_fd;
  88. }
  89. static ssize_t fill_event_metadata(struct fsnotify_group *group,
  90. struct fanotify_event_metadata *metadata,
  91. struct fsnotify_event *event)
  92. {
  93. pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
  94. group, metadata, event);
  95. metadata->event_len = FAN_EVENT_METADATA_LEN;
  96. metadata->vers = FANOTIFY_METADATA_VERSION;
  97. metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS;
  98. metadata->pid = pid_vnr(event->tgid);
  99. metadata->fd = create_fd(group, event);
  100. return metadata->fd;
  101. }
  102. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  103. static struct fanotify_response_event *dequeue_re(struct fsnotify_group *group,
  104. __s32 fd)
  105. {
  106. struct fanotify_response_event *re, *return_re = NULL;
  107. mutex_lock(&group->fanotify_data.access_mutex);
  108. list_for_each_entry(re, &group->fanotify_data.access_list, list) {
  109. if (re->fd != fd)
  110. continue;
  111. list_del_init(&re->list);
  112. return_re = re;
  113. break;
  114. }
  115. mutex_unlock(&group->fanotify_data.access_mutex);
  116. pr_debug("%s: found return_re=%p\n", __func__, return_re);
  117. return return_re;
  118. }
  119. static int process_access_response(struct fsnotify_group *group,
  120. struct fanotify_response *response_struct)
  121. {
  122. struct fanotify_response_event *re;
  123. __s32 fd = response_struct->fd;
  124. __u32 response = response_struct->response;
  125. pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
  126. fd, response);
  127. /*
  128. * make sure the response is valid, if invalid we do nothing and either
  129. * userspace can send a valid responce or we will clean it up after the
  130. * timeout
  131. */
  132. switch (response) {
  133. case FAN_ALLOW:
  134. case FAN_DENY:
  135. break;
  136. default:
  137. return -EINVAL;
  138. }
  139. if (fd < 0)
  140. return -EINVAL;
  141. re = dequeue_re(group, fd);
  142. if (!re)
  143. return -ENOENT;
  144. re->event->response = response;
  145. wake_up(&group->fanotify_data.access_waitq);
  146. kmem_cache_free(fanotify_response_event_cache, re);
  147. return 0;
  148. }
  149. static int prepare_for_access_response(struct fsnotify_group *group,
  150. struct fsnotify_event *event,
  151. __s32 fd)
  152. {
  153. struct fanotify_response_event *re;
  154. if (!(event->mask & FAN_ALL_PERM_EVENTS))
  155. return 0;
  156. re = kmem_cache_alloc(fanotify_response_event_cache, GFP_KERNEL);
  157. if (!re)
  158. return -ENOMEM;
  159. re->event = event;
  160. re->fd = fd;
  161. mutex_lock(&group->fanotify_data.access_mutex);
  162. list_add_tail(&re->list, &group->fanotify_data.access_list);
  163. mutex_unlock(&group->fanotify_data.access_mutex);
  164. return 0;
  165. }
  166. static void remove_access_response(struct fsnotify_group *group,
  167. struct fsnotify_event *event,
  168. __s32 fd)
  169. {
  170. struct fanotify_response_event *re;
  171. if (!(event->mask & FAN_ALL_PERM_EVENTS))
  172. return;
  173. re = dequeue_re(group, fd);
  174. if (!re)
  175. return;
  176. BUG_ON(re->event != event);
  177. kmem_cache_free(fanotify_response_event_cache, re);
  178. return;
  179. }
  180. #else
  181. static int prepare_for_access_response(struct fsnotify_group *group,
  182. struct fsnotify_event *event,
  183. __s32 fd)
  184. {
  185. return 0;
  186. }
  187. static void remove_access_response(struct fsnotify_group *group,
  188. struct fsnotify_event *event,
  189. __s32 fd)
  190. {
  191. return;
  192. }
  193. #endif
  194. static ssize_t copy_event_to_user(struct fsnotify_group *group,
  195. struct fsnotify_event *event,
  196. char __user *buf)
  197. {
  198. struct fanotify_event_metadata fanotify_event_metadata;
  199. int fd, ret;
  200. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  201. fd = fill_event_metadata(group, &fanotify_event_metadata, event);
  202. if (fd < 0)
  203. return fd;
  204. ret = prepare_for_access_response(group, event, fd);
  205. if (ret)
  206. goto out_close_fd;
  207. ret = -EFAULT;
  208. if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
  209. goto out_kill_access_response;
  210. return FAN_EVENT_METADATA_LEN;
  211. out_kill_access_response:
  212. remove_access_response(group, event, fd);
  213. out_close_fd:
  214. sys_close(fd);
  215. return ret;
  216. }
  217. /* intofiy userspace file descriptor functions */
  218. static unsigned int fanotify_poll(struct file *file, poll_table *wait)
  219. {
  220. struct fsnotify_group *group = file->private_data;
  221. int ret = 0;
  222. poll_wait(file, &group->notification_waitq, wait);
  223. mutex_lock(&group->notification_mutex);
  224. if (!fsnotify_notify_queue_is_empty(group))
  225. ret = POLLIN | POLLRDNORM;
  226. mutex_unlock(&group->notification_mutex);
  227. return ret;
  228. }
  229. static ssize_t fanotify_read(struct file *file, char __user *buf,
  230. size_t count, loff_t *pos)
  231. {
  232. struct fsnotify_group *group;
  233. struct fsnotify_event *kevent;
  234. char __user *start;
  235. int ret;
  236. DEFINE_WAIT(wait);
  237. start = buf;
  238. group = file->private_data;
  239. pr_debug("%s: group=%p\n", __func__, group);
  240. while (1) {
  241. prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
  242. mutex_lock(&group->notification_mutex);
  243. kevent = get_one_event(group, count);
  244. mutex_unlock(&group->notification_mutex);
  245. if (kevent) {
  246. ret = PTR_ERR(kevent);
  247. if (IS_ERR(kevent))
  248. break;
  249. ret = copy_event_to_user(group, kevent, buf);
  250. fsnotify_put_event(kevent);
  251. if (ret < 0)
  252. break;
  253. buf += ret;
  254. count -= ret;
  255. continue;
  256. }
  257. ret = -EAGAIN;
  258. if (file->f_flags & O_NONBLOCK)
  259. break;
  260. ret = -EINTR;
  261. if (signal_pending(current))
  262. break;
  263. if (start != buf)
  264. break;
  265. schedule();
  266. }
  267. finish_wait(&group->notification_waitq, &wait);
  268. if (start != buf && ret != -EFAULT)
  269. ret = buf - start;
  270. return ret;
  271. }
  272. static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  273. {
  274. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  275. struct fanotify_response response = { .fd = -1, .response = -1 };
  276. struct fsnotify_group *group;
  277. int ret;
  278. group = file->private_data;
  279. if (count > sizeof(response))
  280. count = sizeof(response);
  281. pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
  282. if (copy_from_user(&response, buf, count))
  283. return -EFAULT;
  284. ret = process_access_response(group, &response);
  285. if (ret < 0)
  286. count = ret;
  287. return count;
  288. #else
  289. return -EINVAL;
  290. #endif
  291. }
  292. static int fanotify_release(struct inode *ignored, struct file *file)
  293. {
  294. struct fsnotify_group *group = file->private_data;
  295. pr_debug("%s: file=%p group=%p\n", __func__, file, group);
  296. /* matches the fanotify_init->fsnotify_alloc_group */
  297. fsnotify_put_group(group);
  298. return 0;
  299. }
  300. static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  301. {
  302. struct fsnotify_group *group;
  303. struct fsnotify_event_holder *holder;
  304. void __user *p;
  305. int ret = -ENOTTY;
  306. size_t send_len = 0;
  307. group = file->private_data;
  308. p = (void __user *) arg;
  309. switch (cmd) {
  310. case FIONREAD:
  311. mutex_lock(&group->notification_mutex);
  312. list_for_each_entry(holder, &group->notification_list, event_list)
  313. send_len += FAN_EVENT_METADATA_LEN;
  314. mutex_unlock(&group->notification_mutex);
  315. ret = put_user(send_len, (int __user *) p);
  316. break;
  317. }
  318. return ret;
  319. }
  320. static const struct file_operations fanotify_fops = {
  321. .poll = fanotify_poll,
  322. .read = fanotify_read,
  323. .write = fanotify_write,
  324. .fasync = NULL,
  325. .release = fanotify_release,
  326. .unlocked_ioctl = fanotify_ioctl,
  327. .compat_ioctl = fanotify_ioctl,
  328. };
  329. static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
  330. {
  331. kmem_cache_free(fanotify_mark_cache, fsn_mark);
  332. }
  333. static int fanotify_find_path(int dfd, const char __user *filename,
  334. struct path *path, unsigned int flags)
  335. {
  336. int ret;
  337. pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
  338. dfd, filename, flags);
  339. if (filename == NULL) {
  340. struct file *file;
  341. int fput_needed;
  342. ret = -EBADF;
  343. file = fget_light(dfd, &fput_needed);
  344. if (!file)
  345. goto out;
  346. ret = -ENOTDIR;
  347. if ((flags & FAN_MARK_ONLYDIR) &&
  348. !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
  349. fput_light(file, fput_needed);
  350. goto out;
  351. }
  352. *path = file->f_path;
  353. path_get(path);
  354. fput_light(file, fput_needed);
  355. } else {
  356. unsigned int lookup_flags = 0;
  357. if (!(flags & FAN_MARK_DONT_FOLLOW))
  358. lookup_flags |= LOOKUP_FOLLOW;
  359. if (flags & FAN_MARK_ONLYDIR)
  360. lookup_flags |= LOOKUP_DIRECTORY;
  361. ret = user_path_at(dfd, filename, lookup_flags, path);
  362. if (ret)
  363. goto out;
  364. }
  365. /* you can only watch an inode if you have read permissions on it */
  366. ret = inode_permission(path->dentry->d_inode, MAY_READ);
  367. if (ret)
  368. path_put(path);
  369. out:
  370. return ret;
  371. }
  372. static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
  373. __u32 mask,
  374. unsigned int flags)
  375. {
  376. __u32 oldmask;
  377. spin_lock(&fsn_mark->lock);
  378. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  379. oldmask = fsn_mark->mask;
  380. fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
  381. } else {
  382. oldmask = fsn_mark->ignored_mask;
  383. fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
  384. }
  385. spin_unlock(&fsn_mark->lock);
  386. if (!(oldmask & ~mask))
  387. fsnotify_destroy_mark(fsn_mark);
  388. return mask & oldmask;
  389. }
  390. static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
  391. struct vfsmount *mnt, __u32 mask,
  392. unsigned int flags)
  393. {
  394. struct fsnotify_mark *fsn_mark = NULL;
  395. __u32 removed;
  396. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  397. if (!fsn_mark)
  398. return -ENOENT;
  399. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
  400. fsnotify_put_mark(fsn_mark);
  401. if (removed & mnt->mnt_fsnotify_mask)
  402. fsnotify_recalc_vfsmount_mask(mnt);
  403. return 0;
  404. }
  405. static int fanotify_remove_inode_mark(struct fsnotify_group *group,
  406. struct inode *inode, __u32 mask,
  407. unsigned int flags)
  408. {
  409. struct fsnotify_mark *fsn_mark = NULL;
  410. __u32 removed;
  411. fsn_mark = fsnotify_find_inode_mark(group, inode);
  412. if (!fsn_mark)
  413. return -ENOENT;
  414. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
  415. /* matches the fsnotify_find_inode_mark() */
  416. fsnotify_put_mark(fsn_mark);
  417. if (removed & inode->i_fsnotify_mask)
  418. fsnotify_recalc_inode_mask(inode);
  419. return 0;
  420. }
  421. static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
  422. __u32 mask,
  423. unsigned int flags)
  424. {
  425. __u32 oldmask;
  426. spin_lock(&fsn_mark->lock);
  427. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  428. oldmask = fsn_mark->mask;
  429. fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
  430. } else {
  431. oldmask = fsn_mark->ignored_mask;
  432. fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask | mask));
  433. if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
  434. fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
  435. }
  436. spin_unlock(&fsn_mark->lock);
  437. return mask & ~oldmask;
  438. }
  439. static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
  440. struct vfsmount *mnt, __u32 mask,
  441. unsigned int flags)
  442. {
  443. struct fsnotify_mark *fsn_mark;
  444. __u32 added;
  445. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  446. if (!fsn_mark) {
  447. int ret;
  448. fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  449. if (!fsn_mark)
  450. return -ENOMEM;
  451. fsnotify_init_mark(fsn_mark, fanotify_free_mark);
  452. ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
  453. if (ret) {
  454. fanotify_free_mark(fsn_mark);
  455. return ret;
  456. }
  457. }
  458. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  459. fsnotify_put_mark(fsn_mark);
  460. if (added & ~mnt->mnt_fsnotify_mask)
  461. fsnotify_recalc_vfsmount_mask(mnt);
  462. return 0;
  463. }
  464. static int fanotify_add_inode_mark(struct fsnotify_group *group,
  465. struct inode *inode, __u32 mask,
  466. unsigned int flags)
  467. {
  468. struct fsnotify_mark *fsn_mark;
  469. __u32 added;
  470. pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
  471. fsn_mark = fsnotify_find_inode_mark(group, inode);
  472. if (!fsn_mark) {
  473. int ret;
  474. fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  475. if (!fsn_mark)
  476. return -ENOMEM;
  477. fsnotify_init_mark(fsn_mark, fanotify_free_mark);
  478. ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
  479. if (ret) {
  480. fanotify_free_mark(fsn_mark);
  481. return ret;
  482. }
  483. }
  484. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  485. fsnotify_put_mark(fsn_mark);
  486. if (added & ~inode->i_fsnotify_mask)
  487. fsnotify_recalc_inode_mask(inode);
  488. return 0;
  489. }
  490. /* fanotify syscalls */
  491. SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
  492. {
  493. struct fsnotify_group *group;
  494. int f_flags, fd;
  495. pr_debug("%s: flags=%d event_f_flags=%d\n",
  496. __func__, flags, event_f_flags);
  497. if (!capable(CAP_SYS_ADMIN))
  498. return -EACCES;
  499. if (flags & ~FAN_ALL_INIT_FLAGS)
  500. return -EINVAL;
  501. f_flags = O_RDWR | FMODE_NONOTIFY;
  502. if (flags & FAN_CLOEXEC)
  503. f_flags |= O_CLOEXEC;
  504. if (flags & FAN_NONBLOCK)
  505. f_flags |= O_NONBLOCK;
  506. /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
  507. group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
  508. if (IS_ERR(group))
  509. return PTR_ERR(group);
  510. group->fanotify_data.f_flags = event_f_flags;
  511. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  512. mutex_init(&group->fanotify_data.access_mutex);
  513. init_waitqueue_head(&group->fanotify_data.access_waitq);
  514. INIT_LIST_HEAD(&group->fanotify_data.access_list);
  515. #endif
  516. fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
  517. if (fd < 0)
  518. goto out_put_group;
  519. return fd;
  520. out_put_group:
  521. fsnotify_put_group(group);
  522. return fd;
  523. }
  524. SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
  525. __u64 mask, int dfd,
  526. const char __user * pathname)
  527. {
  528. struct inode *inode = NULL;
  529. struct vfsmount *mnt = NULL;
  530. struct fsnotify_group *group;
  531. struct file *filp;
  532. struct path path;
  533. int ret, fput_needed;
  534. pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
  535. __func__, fanotify_fd, flags, dfd, pathname, mask);
  536. /* we only use the lower 32 bits as of right now. */
  537. if (mask & ((__u64)0xffffffff << 32))
  538. return -EINVAL;
  539. if (flags & ~FAN_ALL_MARK_FLAGS)
  540. return -EINVAL;
  541. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
  542. case FAN_MARK_ADD:
  543. case FAN_MARK_REMOVE:
  544. case FAN_MARK_FLUSH:
  545. break;
  546. default:
  547. return -EINVAL;
  548. }
  549. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  550. if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
  551. #else
  552. if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
  553. #endif
  554. return -EINVAL;
  555. filp = fget_light(fanotify_fd, &fput_needed);
  556. if (unlikely(!filp))
  557. return -EBADF;
  558. /* verify that this is indeed an fanotify instance */
  559. ret = -EINVAL;
  560. if (unlikely(filp->f_op != &fanotify_fops))
  561. goto fput_and_out;
  562. ret = fanotify_find_path(dfd, pathname, &path, flags);
  563. if (ret)
  564. goto fput_and_out;
  565. /* inode held in place by reference to path; group by fget on fd */
  566. if (!(flags & FAN_MARK_MOUNT))
  567. inode = path.dentry->d_inode;
  568. else
  569. mnt = path.mnt;
  570. group = filp->private_data;
  571. /* create/update an inode mark */
  572. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
  573. case FAN_MARK_ADD:
  574. if (flags & FAN_MARK_MOUNT)
  575. ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
  576. else
  577. ret = fanotify_add_inode_mark(group, inode, mask, flags);
  578. break;
  579. case FAN_MARK_REMOVE:
  580. if (flags & FAN_MARK_MOUNT)
  581. ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
  582. else
  583. ret = fanotify_remove_inode_mark(group, inode, mask, flags);
  584. break;
  585. case FAN_MARK_FLUSH:
  586. if (flags & FAN_MARK_MOUNT)
  587. fsnotify_clear_vfsmount_marks_by_group(group);
  588. else
  589. fsnotify_clear_inode_marks_by_group(group);
  590. break;
  591. default:
  592. ret = -EINVAL;
  593. }
  594. path_put(&path);
  595. fput_and_out:
  596. fput_light(filp, fput_needed);
  597. return ret;
  598. }
  599. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  600. asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
  601. long dfd, long pathname)
  602. {
  603. return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
  604. mask, (int) dfd,
  605. (const char __user *) pathname);
  606. }
  607. SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
  608. #endif
  609. /*
  610. * fanotify_user_setup - Our initialization function. Note that we cannnot return
  611. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  612. * must result in panic().
  613. */
  614. static int __init fanotify_user_setup(void)
  615. {
  616. fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
  617. fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event,
  618. SLAB_PANIC);
  619. return 0;
  620. }
  621. device_initcall(fanotify_user_setup);