fanotify_user.c 24 KB

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