fanotify_user.c 22 KB

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