fanotify_user.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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 & FAN_ALL_OUTGOING_EVENTS;
  116. metadata->pid = pid_vnr(event->tgid);
  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) && !group->fanotify_data.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 & FAN_ALL_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. __u32 tmask = fsn_mark->mask & ~mask;
  423. if (flags & FAN_MARK_ONDIR)
  424. tmask &= ~FAN_ONDIR;
  425. oldmask = fsn_mark->mask;
  426. fsn_mark->mask = tmask;
  427. } else {
  428. __u32 tmask = fsn_mark->ignored_mask & ~mask;
  429. if (flags & FAN_MARK_ONDIR)
  430. tmask &= ~FAN_ONDIR;
  431. fsn_mark->ignored_mask = tmask;
  432. }
  433. *destroy = !(fsn_mark->mask | fsn_mark->ignored_mask);
  434. spin_unlock(&fsn_mark->lock);
  435. return mask & oldmask;
  436. }
  437. static int fanotify_remove_mark(struct fsnotify_group *group,
  438. fsnotify_connp_t *connp, __u32 mask,
  439. unsigned int flags)
  440. {
  441. struct fsnotify_mark *fsn_mark = NULL;
  442. __u32 removed;
  443. int destroy_mark;
  444. mutex_lock(&group->mark_mutex);
  445. fsn_mark = fsnotify_find_mark(connp, group);
  446. if (!fsn_mark) {
  447. mutex_unlock(&group->mark_mutex);
  448. return -ENOENT;
  449. }
  450. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
  451. &destroy_mark);
  452. if (removed & fsnotify_conn_mask(fsn_mark->connector))
  453. fsnotify_recalc_mask(fsn_mark->connector);
  454. if (destroy_mark)
  455. fsnotify_detach_mark(fsn_mark);
  456. mutex_unlock(&group->mark_mutex);
  457. if (destroy_mark)
  458. fsnotify_free_mark(fsn_mark);
  459. /* matches the fsnotify_find_mark() */
  460. fsnotify_put_mark(fsn_mark);
  461. return 0;
  462. }
  463. static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
  464. struct vfsmount *mnt, __u32 mask,
  465. unsigned int flags)
  466. {
  467. return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
  468. 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. __u32 tmask = fsn_mark->mask | mask;
  485. if (flags & FAN_MARK_ONDIR)
  486. tmask |= FAN_ONDIR;
  487. oldmask = fsn_mark->mask;
  488. fsn_mark->mask = tmask;
  489. } else {
  490. __u32 tmask = fsn_mark->ignored_mask | mask;
  491. if (flags & FAN_MARK_ONDIR)
  492. tmask |= FAN_ONDIR;
  493. fsn_mark->ignored_mask = tmask;
  494. if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
  495. fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
  496. }
  497. spin_unlock(&fsn_mark->lock);
  498. return mask & ~oldmask;
  499. }
  500. static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
  501. fsnotify_connp_t *connp,
  502. unsigned int type)
  503. {
  504. struct fsnotify_mark *mark;
  505. int ret;
  506. if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
  507. return ERR_PTR(-ENOSPC);
  508. mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  509. if (!mark)
  510. return ERR_PTR(-ENOMEM);
  511. fsnotify_init_mark(mark, group);
  512. ret = fsnotify_add_mark_locked(mark, connp, type, 0);
  513. if (ret) {
  514. fsnotify_put_mark(mark);
  515. return ERR_PTR(ret);
  516. }
  517. return mark;
  518. }
  519. static int fanotify_add_mark(struct fsnotify_group *group,
  520. fsnotify_connp_t *connp, unsigned int type,
  521. __u32 mask, unsigned int flags)
  522. {
  523. struct fsnotify_mark *fsn_mark;
  524. __u32 added;
  525. mutex_lock(&group->mark_mutex);
  526. fsn_mark = fsnotify_find_mark(connp, group);
  527. if (!fsn_mark) {
  528. fsn_mark = fanotify_add_new_mark(group, connp, type);
  529. if (IS_ERR(fsn_mark)) {
  530. mutex_unlock(&group->mark_mutex);
  531. return PTR_ERR(fsn_mark);
  532. }
  533. }
  534. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  535. if (added & ~fsnotify_conn_mask(fsn_mark->connector))
  536. fsnotify_recalc_mask(fsn_mark->connector);
  537. mutex_unlock(&group->mark_mutex);
  538. fsnotify_put_mark(fsn_mark);
  539. return 0;
  540. }
  541. static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
  542. struct vfsmount *mnt, __u32 mask,
  543. unsigned int flags)
  544. {
  545. return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
  546. FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags);
  547. }
  548. static int fanotify_add_inode_mark(struct fsnotify_group *group,
  549. struct inode *inode, __u32 mask,
  550. unsigned int flags)
  551. {
  552. pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
  553. /*
  554. * If some other task has this inode open for write we should not add
  555. * an ignored mark, unless that ignored mark is supposed to survive
  556. * modification changes anyway.
  557. */
  558. if ((flags & FAN_MARK_IGNORED_MASK) &&
  559. !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
  560. (atomic_read(&inode->i_writecount) > 0))
  561. return 0;
  562. return fanotify_add_mark(group, &inode->i_fsnotify_marks,
  563. FSNOTIFY_OBJ_TYPE_INODE, mask, flags);
  564. }
  565. /* fanotify syscalls */
  566. SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
  567. {
  568. struct fsnotify_group *group;
  569. int f_flags, fd;
  570. struct user_struct *user;
  571. struct fanotify_event_info *oevent;
  572. pr_debug("%s: flags=%d event_f_flags=%d\n",
  573. __func__, flags, event_f_flags);
  574. if (!capable(CAP_SYS_ADMIN))
  575. return -EPERM;
  576. #ifdef CONFIG_AUDITSYSCALL
  577. if (flags & ~(FAN_ALL_INIT_FLAGS | FAN_ENABLE_AUDIT))
  578. #else
  579. if (flags & ~FAN_ALL_INIT_FLAGS)
  580. #endif
  581. return -EINVAL;
  582. if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
  583. return -EINVAL;
  584. switch (event_f_flags & O_ACCMODE) {
  585. case O_RDONLY:
  586. case O_RDWR:
  587. case O_WRONLY:
  588. break;
  589. default:
  590. return -EINVAL;
  591. }
  592. user = get_current_user();
  593. if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
  594. free_uid(user);
  595. return -EMFILE;
  596. }
  597. f_flags = O_RDWR | FMODE_NONOTIFY;
  598. if (flags & FAN_CLOEXEC)
  599. f_flags |= O_CLOEXEC;
  600. if (flags & FAN_NONBLOCK)
  601. f_flags |= O_NONBLOCK;
  602. /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
  603. group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
  604. if (IS_ERR(group)) {
  605. free_uid(user);
  606. return PTR_ERR(group);
  607. }
  608. group->fanotify_data.user = user;
  609. atomic_inc(&user->fanotify_listeners);
  610. group->memcg = get_mem_cgroup_from_mm(current->mm);
  611. oevent = fanotify_alloc_event(group, NULL, FS_Q_OVERFLOW, NULL);
  612. if (unlikely(!oevent)) {
  613. fd = -ENOMEM;
  614. goto out_destroy_group;
  615. }
  616. group->overflow_event = &oevent->fse;
  617. if (force_o_largefile())
  618. event_f_flags |= O_LARGEFILE;
  619. group->fanotify_data.f_flags = event_f_flags;
  620. init_waitqueue_head(&group->fanotify_data.access_waitq);
  621. INIT_LIST_HEAD(&group->fanotify_data.access_list);
  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. if (flags & FAN_ENABLE_AUDIT) {
  653. fd = -EPERM;
  654. if (!capable(CAP_AUDIT_WRITE))
  655. goto out_destroy_group;
  656. group->fanotify_data.audit = true;
  657. }
  658. fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
  659. if (fd < 0)
  660. goto out_destroy_group;
  661. return fd;
  662. out_destroy_group:
  663. fsnotify_destroy_group(group);
  664. return fd;
  665. }
  666. static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
  667. int dfd, const char __user *pathname)
  668. {
  669. struct inode *inode = NULL;
  670. struct vfsmount *mnt = NULL;
  671. struct fsnotify_group *group;
  672. struct fd f;
  673. struct path path;
  674. u32 valid_mask = FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD;
  675. int ret;
  676. pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
  677. __func__, fanotify_fd, flags, dfd, pathname, mask);
  678. /* we only use the lower 32 bits as of right now. */
  679. if (mask & ((__u64)0xffffffff << 32))
  680. return -EINVAL;
  681. if (flags & ~FAN_ALL_MARK_FLAGS)
  682. return -EINVAL;
  683. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
  684. case FAN_MARK_ADD: /* fallthrough */
  685. case FAN_MARK_REMOVE:
  686. if (!mask)
  687. return -EINVAL;
  688. break;
  689. case FAN_MARK_FLUSH:
  690. if (flags & ~(FAN_MARK_MOUNT | FAN_MARK_FLUSH))
  691. return -EINVAL;
  692. break;
  693. default:
  694. return -EINVAL;
  695. }
  696. if (mask & FAN_ONDIR) {
  697. flags |= FAN_MARK_ONDIR;
  698. mask &= ~FAN_ONDIR;
  699. }
  700. if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
  701. valid_mask |= FAN_ALL_PERM_EVENTS;
  702. if (mask & ~valid_mask)
  703. return -EINVAL;
  704. f = fdget(fanotify_fd);
  705. if (unlikely(!f.file))
  706. return -EBADF;
  707. /* verify that this is indeed an fanotify instance */
  708. ret = -EINVAL;
  709. if (unlikely(f.file->f_op != &fanotify_fops))
  710. goto fput_and_out;
  711. group = f.file->private_data;
  712. /*
  713. * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
  714. * allowed to set permissions events.
  715. */
  716. ret = -EINVAL;
  717. if (mask & FAN_ALL_PERM_EVENTS &&
  718. group->priority == FS_PRIO_0)
  719. goto fput_and_out;
  720. if (flags & FAN_MARK_FLUSH) {
  721. ret = 0;
  722. if (flags & FAN_MARK_MOUNT)
  723. fsnotify_clear_vfsmount_marks_by_group(group);
  724. else
  725. fsnotify_clear_inode_marks_by_group(group);
  726. goto fput_and_out;
  727. }
  728. ret = fanotify_find_path(dfd, pathname, &path, flags);
  729. if (ret)
  730. goto fput_and_out;
  731. /* inode held in place by reference to path; group by fget on fd */
  732. if (!(flags & FAN_MARK_MOUNT))
  733. inode = path.dentry->d_inode;
  734. else
  735. mnt = path.mnt;
  736. /* create/update an inode mark */
  737. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
  738. case FAN_MARK_ADD:
  739. if (flags & FAN_MARK_MOUNT)
  740. ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
  741. else
  742. ret = fanotify_add_inode_mark(group, inode, mask, flags);
  743. break;
  744. case FAN_MARK_REMOVE:
  745. if (flags & FAN_MARK_MOUNT)
  746. ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
  747. else
  748. ret = fanotify_remove_inode_mark(group, inode, mask, flags);
  749. break;
  750. default:
  751. ret = -EINVAL;
  752. }
  753. path_put(&path);
  754. fput_and_out:
  755. fdput(f);
  756. return ret;
  757. }
  758. SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
  759. __u64, mask, int, dfd,
  760. const char __user *, pathname)
  761. {
  762. return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
  763. }
  764. #ifdef CONFIG_COMPAT
  765. COMPAT_SYSCALL_DEFINE6(fanotify_mark,
  766. int, fanotify_fd, unsigned int, flags,
  767. __u32, mask0, __u32, mask1, int, dfd,
  768. const char __user *, pathname)
  769. {
  770. return do_fanotify_mark(fanotify_fd, flags,
  771. #ifdef __BIG_ENDIAN
  772. ((__u64)mask0 << 32) | mask1,
  773. #else
  774. ((__u64)mask1 << 32) | mask0,
  775. #endif
  776. dfd, pathname);
  777. }
  778. #endif
  779. /*
  780. * fanotify_user_setup - Our initialization function. Note that we cannot return
  781. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  782. * must result in panic().
  783. */
  784. static int __init fanotify_user_setup(void)
  785. {
  786. fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
  787. SLAB_PANIC|SLAB_ACCOUNT);
  788. fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
  789. if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
  790. fanotify_perm_event_cachep =
  791. KMEM_CACHE(fanotify_perm_event_info, SLAB_PANIC);
  792. }
  793. return 0;
  794. }
  795. device_initcall(fanotify_user_setup);