fanotify_user.c 21 KB

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