fanotify_user.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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. if (group->fanotify_data.bypass_perm) {
  163. mutex_unlock(&group->fanotify_data.access_mutex);
  164. kmem_cache_free(fanotify_response_event_cache, re);
  165. event->response = FAN_ALLOW;
  166. return 0;
  167. }
  168. list_add_tail(&re->list, &group->fanotify_data.access_list);
  169. mutex_unlock(&group->fanotify_data.access_mutex);
  170. return 0;
  171. }
  172. static void remove_access_response(struct fsnotify_group *group,
  173. struct fsnotify_event *event,
  174. __s32 fd)
  175. {
  176. struct fanotify_response_event *re;
  177. if (!(event->mask & FAN_ALL_PERM_EVENTS))
  178. return;
  179. re = dequeue_re(group, fd);
  180. if (!re)
  181. return;
  182. BUG_ON(re->event != event);
  183. kmem_cache_free(fanotify_response_event_cache, re);
  184. return;
  185. }
  186. #else
  187. static int prepare_for_access_response(struct fsnotify_group *group,
  188. struct fsnotify_event *event,
  189. __s32 fd)
  190. {
  191. return 0;
  192. }
  193. static void remove_access_response(struct fsnotify_group *group,
  194. struct fsnotify_event *event,
  195. __s32 fd)
  196. {
  197. return;
  198. }
  199. #endif
  200. static ssize_t copy_event_to_user(struct fsnotify_group *group,
  201. struct fsnotify_event *event,
  202. char __user *buf)
  203. {
  204. struct fanotify_event_metadata fanotify_event_metadata;
  205. int fd, ret;
  206. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  207. fd = fill_event_metadata(group, &fanotify_event_metadata, event);
  208. if (fd < 0)
  209. return fd;
  210. ret = prepare_for_access_response(group, event, fd);
  211. if (ret)
  212. goto out_close_fd;
  213. ret = -EFAULT;
  214. if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
  215. goto out_kill_access_response;
  216. return FAN_EVENT_METADATA_LEN;
  217. out_kill_access_response:
  218. remove_access_response(group, event, fd);
  219. out_close_fd:
  220. sys_close(fd);
  221. return ret;
  222. }
  223. /* intofiy userspace file descriptor functions */
  224. static unsigned int fanotify_poll(struct file *file, poll_table *wait)
  225. {
  226. struct fsnotify_group *group = file->private_data;
  227. int ret = 0;
  228. poll_wait(file, &group->notification_waitq, wait);
  229. mutex_lock(&group->notification_mutex);
  230. if (!fsnotify_notify_queue_is_empty(group))
  231. ret = POLLIN | POLLRDNORM;
  232. mutex_unlock(&group->notification_mutex);
  233. return ret;
  234. }
  235. static ssize_t fanotify_read(struct file *file, char __user *buf,
  236. size_t count, loff_t *pos)
  237. {
  238. struct fsnotify_group *group;
  239. struct fsnotify_event *kevent;
  240. char __user *start;
  241. int ret;
  242. DEFINE_WAIT(wait);
  243. start = buf;
  244. group = file->private_data;
  245. pr_debug("%s: group=%p\n", __func__, group);
  246. while (1) {
  247. prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
  248. mutex_lock(&group->notification_mutex);
  249. kevent = get_one_event(group, count);
  250. mutex_unlock(&group->notification_mutex);
  251. if (kevent) {
  252. ret = PTR_ERR(kevent);
  253. if (IS_ERR(kevent))
  254. break;
  255. ret = copy_event_to_user(group, kevent, buf);
  256. fsnotify_put_event(kevent);
  257. if (ret < 0)
  258. break;
  259. buf += ret;
  260. count -= ret;
  261. continue;
  262. }
  263. ret = -EAGAIN;
  264. if (file->f_flags & O_NONBLOCK)
  265. break;
  266. ret = -EINTR;
  267. if (signal_pending(current))
  268. break;
  269. if (start != buf)
  270. break;
  271. schedule();
  272. }
  273. finish_wait(&group->notification_waitq, &wait);
  274. if (start != buf && ret != -EFAULT)
  275. ret = buf - start;
  276. return ret;
  277. }
  278. static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  279. {
  280. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  281. struct fanotify_response response = { .fd = -1, .response = -1 };
  282. struct fsnotify_group *group;
  283. int ret;
  284. group = file->private_data;
  285. if (count > sizeof(response))
  286. count = sizeof(response);
  287. pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
  288. if (copy_from_user(&response, buf, count))
  289. return -EFAULT;
  290. ret = process_access_response(group, &response);
  291. if (ret < 0)
  292. count = ret;
  293. return count;
  294. #else
  295. return -EINVAL;
  296. #endif
  297. }
  298. static int fanotify_release(struct inode *ignored, struct file *file)
  299. {
  300. struct fsnotify_group *group = file->private_data;
  301. struct fanotify_response_event *re, *lre;
  302. pr_debug("%s: file=%p group=%p\n", __func__, file, group);
  303. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  304. mutex_lock(&group->fanotify_data.access_mutex);
  305. group->fanotify_data.bypass_perm = true;
  306. list_for_each_entry_safe(re, lre, &group->fanotify_data.access_list, list) {
  307. pr_debug("%s: found group=%p re=%p event=%p\n", __func__, group,
  308. re, re->event);
  309. list_del_init(&re->list);
  310. re->event->response = FAN_ALLOW;
  311. kmem_cache_free(fanotify_response_event_cache, re);
  312. }
  313. mutex_unlock(&group->fanotify_data.access_mutex);
  314. wake_up(&group->fanotify_data.access_waitq);
  315. #endif
  316. /* matches the fanotify_init->fsnotify_alloc_group */
  317. fsnotify_put_group(group);
  318. return 0;
  319. }
  320. static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  321. {
  322. struct fsnotify_group *group;
  323. struct fsnotify_event_holder *holder;
  324. void __user *p;
  325. int ret = -ENOTTY;
  326. size_t send_len = 0;
  327. group = file->private_data;
  328. p = (void __user *) arg;
  329. switch (cmd) {
  330. case FIONREAD:
  331. mutex_lock(&group->notification_mutex);
  332. list_for_each_entry(holder, &group->notification_list, event_list)
  333. send_len += FAN_EVENT_METADATA_LEN;
  334. mutex_unlock(&group->notification_mutex);
  335. ret = put_user(send_len, (int __user *) p);
  336. break;
  337. }
  338. return ret;
  339. }
  340. static const struct file_operations fanotify_fops = {
  341. .poll = fanotify_poll,
  342. .read = fanotify_read,
  343. .write = fanotify_write,
  344. .fasync = NULL,
  345. .release = fanotify_release,
  346. .unlocked_ioctl = fanotify_ioctl,
  347. .compat_ioctl = fanotify_ioctl,
  348. .llseek = noop_llseek,
  349. };
  350. static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
  351. {
  352. kmem_cache_free(fanotify_mark_cache, fsn_mark);
  353. }
  354. static int fanotify_find_path(int dfd, const char __user *filename,
  355. struct path *path, unsigned int flags)
  356. {
  357. int ret;
  358. pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
  359. dfd, filename, flags);
  360. if (filename == NULL) {
  361. struct file *file;
  362. int fput_needed;
  363. ret = -EBADF;
  364. file = fget_light(dfd, &fput_needed);
  365. if (!file)
  366. goto out;
  367. ret = -ENOTDIR;
  368. if ((flags & FAN_MARK_ONLYDIR) &&
  369. !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
  370. fput_light(file, fput_needed);
  371. goto out;
  372. }
  373. *path = file->f_path;
  374. path_get(path);
  375. fput_light(file, fput_needed);
  376. } else {
  377. unsigned int lookup_flags = 0;
  378. if (!(flags & FAN_MARK_DONT_FOLLOW))
  379. lookup_flags |= LOOKUP_FOLLOW;
  380. if (flags & FAN_MARK_ONLYDIR)
  381. lookup_flags |= LOOKUP_DIRECTORY;
  382. ret = user_path_at(dfd, filename, lookup_flags, path);
  383. if (ret)
  384. goto out;
  385. }
  386. /* you can only watch an inode if you have read permissions on it */
  387. ret = inode_permission(path->dentry->d_inode, MAY_READ);
  388. if (ret)
  389. path_put(path);
  390. out:
  391. return ret;
  392. }
  393. static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
  394. __u32 mask,
  395. unsigned int flags)
  396. {
  397. __u32 oldmask;
  398. spin_lock(&fsn_mark->lock);
  399. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  400. oldmask = fsn_mark->mask;
  401. fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
  402. } else {
  403. oldmask = fsn_mark->ignored_mask;
  404. fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
  405. }
  406. spin_unlock(&fsn_mark->lock);
  407. if (!(oldmask & ~mask))
  408. fsnotify_destroy_mark(fsn_mark);
  409. return mask & oldmask;
  410. }
  411. static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
  412. struct vfsmount *mnt, __u32 mask,
  413. unsigned int flags)
  414. {
  415. struct fsnotify_mark *fsn_mark = NULL;
  416. __u32 removed;
  417. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  418. if (!fsn_mark)
  419. return -ENOENT;
  420. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
  421. fsnotify_put_mark(fsn_mark);
  422. if (removed & mnt->mnt_fsnotify_mask)
  423. fsnotify_recalc_vfsmount_mask(mnt);
  424. return 0;
  425. }
  426. static int fanotify_remove_inode_mark(struct fsnotify_group *group,
  427. struct inode *inode, __u32 mask,
  428. unsigned int flags)
  429. {
  430. struct fsnotify_mark *fsn_mark = NULL;
  431. __u32 removed;
  432. fsn_mark = fsnotify_find_inode_mark(group, inode);
  433. if (!fsn_mark)
  434. return -ENOENT;
  435. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
  436. /* matches the fsnotify_find_inode_mark() */
  437. fsnotify_put_mark(fsn_mark);
  438. if (removed & inode->i_fsnotify_mask)
  439. fsnotify_recalc_inode_mask(inode);
  440. return 0;
  441. }
  442. static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
  443. __u32 mask,
  444. unsigned int flags)
  445. {
  446. __u32 oldmask;
  447. spin_lock(&fsn_mark->lock);
  448. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  449. oldmask = fsn_mark->mask;
  450. fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
  451. } else {
  452. oldmask = fsn_mark->ignored_mask;
  453. fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask | mask));
  454. if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
  455. fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
  456. }
  457. spin_unlock(&fsn_mark->lock);
  458. return mask & ~oldmask;
  459. }
  460. static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
  461. struct vfsmount *mnt, __u32 mask,
  462. unsigned int flags)
  463. {
  464. struct fsnotify_mark *fsn_mark;
  465. __u32 added;
  466. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  467. if (!fsn_mark) {
  468. int ret;
  469. fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  470. if (!fsn_mark)
  471. return -ENOMEM;
  472. fsnotify_init_mark(fsn_mark, fanotify_free_mark);
  473. ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
  474. if (ret) {
  475. fanotify_free_mark(fsn_mark);
  476. return ret;
  477. }
  478. }
  479. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  480. fsnotify_put_mark(fsn_mark);
  481. if (added & ~mnt->mnt_fsnotify_mask)
  482. fsnotify_recalc_vfsmount_mask(mnt);
  483. return 0;
  484. }
  485. static int fanotify_add_inode_mark(struct fsnotify_group *group,
  486. struct inode *inode, __u32 mask,
  487. unsigned int flags)
  488. {
  489. struct fsnotify_mark *fsn_mark;
  490. __u32 added;
  491. pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
  492. fsn_mark = fsnotify_find_inode_mark(group, inode);
  493. if (!fsn_mark) {
  494. int ret;
  495. fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  496. if (!fsn_mark)
  497. return -ENOMEM;
  498. fsnotify_init_mark(fsn_mark, fanotify_free_mark);
  499. ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
  500. if (ret) {
  501. fanotify_free_mark(fsn_mark);
  502. return ret;
  503. }
  504. }
  505. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  506. fsnotify_put_mark(fsn_mark);
  507. if (added & ~inode->i_fsnotify_mask)
  508. fsnotify_recalc_inode_mask(inode);
  509. return 0;
  510. }
  511. /* fanotify syscalls */
  512. SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
  513. {
  514. struct fsnotify_group *group;
  515. int f_flags, fd;
  516. pr_debug("%s: flags=%d event_f_flags=%d\n",
  517. __func__, flags, event_f_flags);
  518. if (!capable(CAP_SYS_ADMIN))
  519. return -EPERM;
  520. if (flags & ~FAN_ALL_INIT_FLAGS)
  521. return -EINVAL;
  522. f_flags = O_RDWR | FMODE_NONOTIFY;
  523. if (flags & FAN_CLOEXEC)
  524. f_flags |= O_CLOEXEC;
  525. if (flags & FAN_NONBLOCK)
  526. f_flags |= O_NONBLOCK;
  527. /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
  528. group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
  529. if (IS_ERR(group))
  530. return PTR_ERR(group);
  531. group->fanotify_data.f_flags = event_f_flags;
  532. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  533. mutex_init(&group->fanotify_data.access_mutex);
  534. init_waitqueue_head(&group->fanotify_data.access_waitq);
  535. INIT_LIST_HEAD(&group->fanotify_data.access_list);
  536. #endif
  537. fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
  538. if (fd < 0)
  539. goto out_put_group;
  540. return fd;
  541. out_put_group:
  542. fsnotify_put_group(group);
  543. return fd;
  544. }
  545. SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
  546. __u64 mask, int dfd,
  547. const char __user * pathname)
  548. {
  549. struct inode *inode = NULL;
  550. struct vfsmount *mnt = NULL;
  551. struct fsnotify_group *group;
  552. struct file *filp;
  553. struct path path;
  554. int ret, fput_needed;
  555. pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
  556. __func__, fanotify_fd, flags, dfd, pathname, mask);
  557. /* we only use the lower 32 bits as of right now. */
  558. if (mask & ((__u64)0xffffffff << 32))
  559. return -EINVAL;
  560. if (flags & ~FAN_ALL_MARK_FLAGS)
  561. return -EINVAL;
  562. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
  563. case FAN_MARK_ADD:
  564. case FAN_MARK_REMOVE:
  565. case FAN_MARK_FLUSH:
  566. break;
  567. default:
  568. return -EINVAL;
  569. }
  570. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  571. if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
  572. #else
  573. if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
  574. #endif
  575. return -EINVAL;
  576. filp = fget_light(fanotify_fd, &fput_needed);
  577. if (unlikely(!filp))
  578. return -EBADF;
  579. /* verify that this is indeed an fanotify instance */
  580. ret = -EINVAL;
  581. if (unlikely(filp->f_op != &fanotify_fops))
  582. goto fput_and_out;
  583. ret = fanotify_find_path(dfd, pathname, &path, flags);
  584. if (ret)
  585. goto fput_and_out;
  586. /* inode held in place by reference to path; group by fget on fd */
  587. if (!(flags & FAN_MARK_MOUNT))
  588. inode = path.dentry->d_inode;
  589. else
  590. mnt = path.mnt;
  591. group = filp->private_data;
  592. /* create/update an inode mark */
  593. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
  594. case FAN_MARK_ADD:
  595. if (flags & FAN_MARK_MOUNT)
  596. ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
  597. else
  598. ret = fanotify_add_inode_mark(group, inode, mask, flags);
  599. break;
  600. case FAN_MARK_REMOVE:
  601. if (flags & FAN_MARK_MOUNT)
  602. ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
  603. else
  604. ret = fanotify_remove_inode_mark(group, inode, mask, flags);
  605. break;
  606. case FAN_MARK_FLUSH:
  607. if (flags & FAN_MARK_MOUNT)
  608. fsnotify_clear_vfsmount_marks_by_group(group);
  609. else
  610. fsnotify_clear_inode_marks_by_group(group);
  611. break;
  612. default:
  613. ret = -EINVAL;
  614. }
  615. path_put(&path);
  616. fput_and_out:
  617. fput_light(filp, fput_needed);
  618. return ret;
  619. }
  620. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  621. asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
  622. long dfd, long pathname)
  623. {
  624. return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
  625. mask, (int) dfd,
  626. (const char __user *) pathname);
  627. }
  628. SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
  629. #endif
  630. /*
  631. * fanotify_user_setup - Our initialization function. Note that we cannnot return
  632. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  633. * must result in panic().
  634. */
  635. static int __init fanotify_user_setup(void)
  636. {
  637. fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
  638. fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event,
  639. SLAB_PANIC);
  640. return 0;
  641. }
  642. device_initcall(fanotify_user_setup);