fanotify_user.c 24 KB

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