fanotify_user.c 24 KB

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