fanotify_user.c 23 KB

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