fanotify_user.c 23 KB

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