fanotify_user.c 19 KB

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