fanotify_user.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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. };
  349. static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
  350. {
  351. kmem_cache_free(fanotify_mark_cache, fsn_mark);
  352. }
  353. static int fanotify_find_path(int dfd, const char __user *filename,
  354. struct path *path, unsigned int flags)
  355. {
  356. int ret;
  357. pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
  358. dfd, filename, flags);
  359. if (filename == NULL) {
  360. struct file *file;
  361. int fput_needed;
  362. ret = -EBADF;
  363. file = fget_light(dfd, &fput_needed);
  364. if (!file)
  365. goto out;
  366. ret = -ENOTDIR;
  367. if ((flags & FAN_MARK_ONLYDIR) &&
  368. !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
  369. fput_light(file, fput_needed);
  370. goto out;
  371. }
  372. *path = file->f_path;
  373. path_get(path);
  374. fput_light(file, fput_needed);
  375. } else {
  376. unsigned int lookup_flags = 0;
  377. if (!(flags & FAN_MARK_DONT_FOLLOW))
  378. lookup_flags |= LOOKUP_FOLLOW;
  379. if (flags & FAN_MARK_ONLYDIR)
  380. lookup_flags |= LOOKUP_DIRECTORY;
  381. ret = user_path_at(dfd, filename, lookup_flags, path);
  382. if (ret)
  383. goto out;
  384. }
  385. /* you can only watch an inode if you have read permissions on it */
  386. ret = inode_permission(path->dentry->d_inode, MAY_READ);
  387. if (ret)
  388. path_put(path);
  389. out:
  390. return ret;
  391. }
  392. static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
  393. __u32 mask,
  394. unsigned int flags)
  395. {
  396. __u32 oldmask;
  397. spin_lock(&fsn_mark->lock);
  398. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  399. oldmask = fsn_mark->mask;
  400. fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
  401. } else {
  402. oldmask = fsn_mark->ignored_mask;
  403. fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
  404. }
  405. spin_unlock(&fsn_mark->lock);
  406. if (!(oldmask & ~mask))
  407. fsnotify_destroy_mark(fsn_mark);
  408. return mask & oldmask;
  409. }
  410. static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
  411. struct vfsmount *mnt, __u32 mask,
  412. unsigned int flags)
  413. {
  414. struct fsnotify_mark *fsn_mark = NULL;
  415. __u32 removed;
  416. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  417. if (!fsn_mark)
  418. return -ENOENT;
  419. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
  420. fsnotify_put_mark(fsn_mark);
  421. if (removed & mnt->mnt_fsnotify_mask)
  422. fsnotify_recalc_vfsmount_mask(mnt);
  423. return 0;
  424. }
  425. static int fanotify_remove_inode_mark(struct fsnotify_group *group,
  426. struct inode *inode, __u32 mask,
  427. unsigned int flags)
  428. {
  429. struct fsnotify_mark *fsn_mark = NULL;
  430. __u32 removed;
  431. fsn_mark = fsnotify_find_inode_mark(group, inode);
  432. if (!fsn_mark)
  433. return -ENOENT;
  434. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
  435. /* matches the fsnotify_find_inode_mark() */
  436. fsnotify_put_mark(fsn_mark);
  437. if (removed & inode->i_fsnotify_mask)
  438. fsnotify_recalc_inode_mask(inode);
  439. return 0;
  440. }
  441. static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
  442. __u32 mask,
  443. unsigned int flags)
  444. {
  445. __u32 oldmask;
  446. spin_lock(&fsn_mark->lock);
  447. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  448. oldmask = fsn_mark->mask;
  449. fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
  450. } else {
  451. oldmask = fsn_mark->ignored_mask;
  452. fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask | mask));
  453. if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
  454. fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
  455. }
  456. spin_unlock(&fsn_mark->lock);
  457. return mask & ~oldmask;
  458. }
  459. static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
  460. struct vfsmount *mnt, __u32 mask,
  461. unsigned int flags)
  462. {
  463. struct fsnotify_mark *fsn_mark;
  464. __u32 added;
  465. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  466. if (!fsn_mark) {
  467. int ret;
  468. fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  469. if (!fsn_mark)
  470. return -ENOMEM;
  471. fsnotify_init_mark(fsn_mark, fanotify_free_mark);
  472. ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
  473. if (ret) {
  474. fanotify_free_mark(fsn_mark);
  475. return ret;
  476. }
  477. }
  478. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  479. fsnotify_put_mark(fsn_mark);
  480. if (added & ~mnt->mnt_fsnotify_mask)
  481. fsnotify_recalc_vfsmount_mask(mnt);
  482. return 0;
  483. }
  484. static int fanotify_add_inode_mark(struct fsnotify_group *group,
  485. struct inode *inode, __u32 mask,
  486. unsigned int flags)
  487. {
  488. struct fsnotify_mark *fsn_mark;
  489. __u32 added;
  490. pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
  491. fsn_mark = fsnotify_find_inode_mark(group, inode);
  492. if (!fsn_mark) {
  493. int ret;
  494. fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  495. if (!fsn_mark)
  496. return -ENOMEM;
  497. fsnotify_init_mark(fsn_mark, fanotify_free_mark);
  498. ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
  499. if (ret) {
  500. fanotify_free_mark(fsn_mark);
  501. return ret;
  502. }
  503. }
  504. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  505. fsnotify_put_mark(fsn_mark);
  506. if (added & ~inode->i_fsnotify_mask)
  507. fsnotify_recalc_inode_mask(inode);
  508. return 0;
  509. }
  510. /* fanotify syscalls */
  511. SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
  512. {
  513. struct fsnotify_group *group;
  514. int f_flags, fd;
  515. pr_debug("%s: flags=%d event_f_flags=%d\n",
  516. __func__, flags, event_f_flags);
  517. if (!capable(CAP_SYS_ADMIN))
  518. return -EPERM;
  519. if (flags & ~FAN_ALL_INIT_FLAGS)
  520. return -EINVAL;
  521. f_flags = O_RDWR | FMODE_NONOTIFY;
  522. if (flags & FAN_CLOEXEC)
  523. f_flags |= O_CLOEXEC;
  524. if (flags & FAN_NONBLOCK)
  525. f_flags |= O_NONBLOCK;
  526. /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
  527. group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
  528. if (IS_ERR(group))
  529. return PTR_ERR(group);
  530. group->fanotify_data.f_flags = event_f_flags;
  531. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  532. mutex_init(&group->fanotify_data.access_mutex);
  533. init_waitqueue_head(&group->fanotify_data.access_waitq);
  534. INIT_LIST_HEAD(&group->fanotify_data.access_list);
  535. #endif
  536. fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
  537. if (fd < 0)
  538. goto out_put_group;
  539. return fd;
  540. out_put_group:
  541. fsnotify_put_group(group);
  542. return fd;
  543. }
  544. SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
  545. __u64 mask, int dfd,
  546. const char __user * pathname)
  547. {
  548. struct inode *inode = NULL;
  549. struct vfsmount *mnt = NULL;
  550. struct fsnotify_group *group;
  551. struct file *filp;
  552. struct path path;
  553. int ret, fput_needed;
  554. pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
  555. __func__, fanotify_fd, flags, dfd, pathname, mask);
  556. /* we only use the lower 32 bits as of right now. */
  557. if (mask & ((__u64)0xffffffff << 32))
  558. return -EINVAL;
  559. if (flags & ~FAN_ALL_MARK_FLAGS)
  560. return -EINVAL;
  561. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
  562. case FAN_MARK_ADD:
  563. case FAN_MARK_REMOVE:
  564. case FAN_MARK_FLUSH:
  565. break;
  566. default:
  567. return -EINVAL;
  568. }
  569. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  570. if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
  571. #else
  572. if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
  573. #endif
  574. return -EINVAL;
  575. filp = fget_light(fanotify_fd, &fput_needed);
  576. if (unlikely(!filp))
  577. return -EBADF;
  578. /* verify that this is indeed an fanotify instance */
  579. ret = -EINVAL;
  580. if (unlikely(filp->f_op != &fanotify_fops))
  581. goto fput_and_out;
  582. ret = fanotify_find_path(dfd, pathname, &path, flags);
  583. if (ret)
  584. goto fput_and_out;
  585. /* inode held in place by reference to path; group by fget on fd */
  586. if (!(flags & FAN_MARK_MOUNT))
  587. inode = path.dentry->d_inode;
  588. else
  589. mnt = path.mnt;
  590. group = filp->private_data;
  591. /* create/update an inode mark */
  592. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
  593. case FAN_MARK_ADD:
  594. if (flags & FAN_MARK_MOUNT)
  595. ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
  596. else
  597. ret = fanotify_add_inode_mark(group, inode, mask, flags);
  598. break;
  599. case FAN_MARK_REMOVE:
  600. if (flags & FAN_MARK_MOUNT)
  601. ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
  602. else
  603. ret = fanotify_remove_inode_mark(group, inode, mask, flags);
  604. break;
  605. case FAN_MARK_FLUSH:
  606. if (flags & FAN_MARK_MOUNT)
  607. fsnotify_clear_vfsmount_marks_by_group(group);
  608. else
  609. fsnotify_clear_inode_marks_by_group(group);
  610. break;
  611. default:
  612. ret = -EINVAL;
  613. }
  614. path_put(&path);
  615. fput_and_out:
  616. fput_light(filp, fput_needed);
  617. return ret;
  618. }
  619. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  620. asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
  621. long dfd, long pathname)
  622. {
  623. return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
  624. mask, (int) dfd,
  625. (const char __user *) pathname);
  626. }
  627. SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
  628. #endif
  629. /*
  630. * fanotify_user_setup - Our initialization function. Note that we cannnot return
  631. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  632. * must result in panic().
  633. */
  634. static int __init fanotify_user_setup(void)
  635. {
  636. fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
  637. fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event,
  638. SLAB_PANIC);
  639. return 0;
  640. }
  641. device_initcall(fanotify_user_setup);