fanotify_user.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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_notify_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();
  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(wait);
  221. start = buf;
  222. group = file->private_data;
  223. pr_debug("%s: group=%p\n", __func__, group);
  224. while (1) {
  225. prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
  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. schedule();
  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. finish_wait(&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. spin_lock(&group->fanotify_data.access_lock);
  301. atomic_inc(&group->fanotify_data.bypass_perm);
  302. list_for_each_entry_safe(event, next, &group->fanotify_data.access_list,
  303. fae.fse.list) {
  304. pr_debug("%s: found group=%p event=%p\n", __func__, group,
  305. event);
  306. list_del_init(&event->fae.fse.list);
  307. event->response = FAN_ALLOW;
  308. }
  309. spin_unlock(&group->fanotify_data.access_lock);
  310. wake_up(&group->fanotify_data.access_waitq);
  311. #endif
  312. /* matches the fanotify_init->fsnotify_alloc_group */
  313. fsnotify_destroy_group(group);
  314. return 0;
  315. }
  316. static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  317. {
  318. struct fsnotify_group *group;
  319. struct fsnotify_event *fsn_event;
  320. void __user *p;
  321. int ret = -ENOTTY;
  322. size_t send_len = 0;
  323. group = file->private_data;
  324. p = (void __user *) arg;
  325. switch (cmd) {
  326. case FIONREAD:
  327. mutex_lock(&group->notification_mutex);
  328. list_for_each_entry(fsn_event, &group->notification_list, list)
  329. send_len += FAN_EVENT_METADATA_LEN;
  330. mutex_unlock(&group->notification_mutex);
  331. ret = put_user(send_len, (int __user *) p);
  332. break;
  333. }
  334. return ret;
  335. }
  336. static const struct file_operations fanotify_fops = {
  337. .show_fdinfo = fanotify_show_fdinfo,
  338. .poll = fanotify_poll,
  339. .read = fanotify_read,
  340. .write = fanotify_write,
  341. .fasync = NULL,
  342. .release = fanotify_release,
  343. .unlocked_ioctl = fanotify_ioctl,
  344. .compat_ioctl = fanotify_ioctl,
  345. .llseek = noop_llseek,
  346. };
  347. static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
  348. {
  349. kmem_cache_free(fanotify_mark_cache, fsn_mark);
  350. }
  351. static int fanotify_find_path(int dfd, const char __user *filename,
  352. struct path *path, unsigned int flags)
  353. {
  354. int ret;
  355. pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
  356. dfd, filename, flags);
  357. if (filename == NULL) {
  358. struct fd f = fdget(dfd);
  359. ret = -EBADF;
  360. if (!f.file)
  361. goto out;
  362. ret = -ENOTDIR;
  363. if ((flags & FAN_MARK_ONLYDIR) &&
  364. !(S_ISDIR(file_inode(f.file)->i_mode))) {
  365. fdput(f);
  366. goto out;
  367. }
  368. *path = f.file->f_path;
  369. path_get(path);
  370. fdput(f);
  371. } else {
  372. unsigned int lookup_flags = 0;
  373. if (!(flags & FAN_MARK_DONT_FOLLOW))
  374. lookup_flags |= LOOKUP_FOLLOW;
  375. if (flags & FAN_MARK_ONLYDIR)
  376. lookup_flags |= LOOKUP_DIRECTORY;
  377. ret = user_path_at(dfd, filename, lookup_flags, path);
  378. if (ret)
  379. goto out;
  380. }
  381. /* you can only watch an inode if you have read permissions on it */
  382. ret = inode_permission(path->dentry->d_inode, MAY_READ);
  383. if (ret)
  384. path_put(path);
  385. out:
  386. return ret;
  387. }
  388. static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
  389. __u32 mask,
  390. unsigned int flags,
  391. int *destroy)
  392. {
  393. __u32 oldmask;
  394. spin_lock(&fsn_mark->lock);
  395. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  396. oldmask = fsn_mark->mask;
  397. fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
  398. } else {
  399. oldmask = fsn_mark->ignored_mask;
  400. fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
  401. }
  402. spin_unlock(&fsn_mark->lock);
  403. *destroy = !(oldmask & ~mask);
  404. return mask & oldmask;
  405. }
  406. static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
  407. struct vfsmount *mnt, __u32 mask,
  408. unsigned int flags)
  409. {
  410. struct fsnotify_mark *fsn_mark = NULL;
  411. __u32 removed;
  412. int destroy_mark;
  413. mutex_lock(&group->mark_mutex);
  414. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  415. if (!fsn_mark) {
  416. mutex_unlock(&group->mark_mutex);
  417. return -ENOENT;
  418. }
  419. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
  420. &destroy_mark);
  421. if (destroy_mark)
  422. fsnotify_destroy_mark_locked(fsn_mark, group);
  423. mutex_unlock(&group->mark_mutex);
  424. fsnotify_put_mark(fsn_mark);
  425. if (removed & real_mount(mnt)->mnt_fsnotify_mask)
  426. fsnotify_recalc_vfsmount_mask(mnt);
  427. return 0;
  428. }
  429. static int fanotify_remove_inode_mark(struct fsnotify_group *group,
  430. struct inode *inode, __u32 mask,
  431. unsigned int flags)
  432. {
  433. struct fsnotify_mark *fsn_mark = NULL;
  434. __u32 removed;
  435. int destroy_mark;
  436. mutex_lock(&group->mark_mutex);
  437. fsn_mark = fsnotify_find_inode_mark(group, inode);
  438. if (!fsn_mark) {
  439. mutex_unlock(&group->mark_mutex);
  440. return -ENOENT;
  441. }
  442. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
  443. &destroy_mark);
  444. if (destroy_mark)
  445. fsnotify_destroy_mark_locked(fsn_mark, group);
  446. mutex_unlock(&group->mark_mutex);
  447. /* matches the fsnotify_find_inode_mark() */
  448. fsnotify_put_mark(fsn_mark);
  449. if (removed & inode->i_fsnotify_mask)
  450. fsnotify_recalc_inode_mask(inode);
  451. return 0;
  452. }
  453. static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
  454. __u32 mask,
  455. unsigned int flags)
  456. {
  457. __u32 oldmask = -1;
  458. spin_lock(&fsn_mark->lock);
  459. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  460. oldmask = fsn_mark->mask;
  461. fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
  462. } else {
  463. __u32 tmask = fsn_mark->ignored_mask | mask;
  464. fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask);
  465. if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
  466. fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
  467. }
  468. if (!(flags & FAN_MARK_ONDIR)) {
  469. __u32 tmask = fsn_mark->ignored_mask | FAN_ONDIR;
  470. fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask);
  471. }
  472. spin_unlock(&fsn_mark->lock);
  473. return mask & ~oldmask;
  474. }
  475. static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
  476. struct inode *inode,
  477. struct vfsmount *mnt)
  478. {
  479. struct fsnotify_mark *mark;
  480. int ret;
  481. if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
  482. return ERR_PTR(-ENOSPC);
  483. mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  484. if (!mark)
  485. return ERR_PTR(-ENOMEM);
  486. fsnotify_init_mark(mark, fanotify_free_mark);
  487. ret = fsnotify_add_mark_locked(mark, group, inode, mnt, 0);
  488. if (ret) {
  489. fsnotify_put_mark(mark);
  490. return ERR_PTR(ret);
  491. }
  492. return mark;
  493. }
  494. static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
  495. struct vfsmount *mnt, __u32 mask,
  496. unsigned int flags)
  497. {
  498. struct fsnotify_mark *fsn_mark;
  499. __u32 added;
  500. mutex_lock(&group->mark_mutex);
  501. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  502. if (!fsn_mark) {
  503. fsn_mark = fanotify_add_new_mark(group, NULL, mnt);
  504. if (IS_ERR(fsn_mark)) {
  505. mutex_unlock(&group->mark_mutex);
  506. return PTR_ERR(fsn_mark);
  507. }
  508. }
  509. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  510. mutex_unlock(&group->mark_mutex);
  511. if (added & ~real_mount(mnt)->mnt_fsnotify_mask)
  512. fsnotify_recalc_vfsmount_mask(mnt);
  513. fsnotify_put_mark(fsn_mark);
  514. return 0;
  515. }
  516. static int fanotify_add_inode_mark(struct fsnotify_group *group,
  517. struct inode *inode, __u32 mask,
  518. unsigned int flags)
  519. {
  520. struct fsnotify_mark *fsn_mark;
  521. __u32 added;
  522. pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
  523. /*
  524. * If some other task has this inode open for write we should not add
  525. * an ignored mark, unless that ignored mark is supposed to survive
  526. * modification changes anyway.
  527. */
  528. if ((flags & FAN_MARK_IGNORED_MASK) &&
  529. !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
  530. (atomic_read(&inode->i_writecount) > 0))
  531. return 0;
  532. mutex_lock(&group->mark_mutex);
  533. fsn_mark = fsnotify_find_inode_mark(group, inode);
  534. if (!fsn_mark) {
  535. fsn_mark = fanotify_add_new_mark(group, inode, NULL);
  536. if (IS_ERR(fsn_mark)) {
  537. mutex_unlock(&group->mark_mutex);
  538. return PTR_ERR(fsn_mark);
  539. }
  540. }
  541. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  542. mutex_unlock(&group->mark_mutex);
  543. if (added & ~inode->i_fsnotify_mask)
  544. fsnotify_recalc_inode_mask(inode);
  545. fsnotify_put_mark(fsn_mark);
  546. return 0;
  547. }
  548. /* fanotify syscalls */
  549. SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
  550. {
  551. struct fsnotify_group *group;
  552. int f_flags, fd;
  553. struct user_struct *user;
  554. struct fanotify_event_info *oevent;
  555. pr_debug("%s: flags=%d event_f_flags=%d\n",
  556. __func__, flags, event_f_flags);
  557. if (!capable(CAP_SYS_ADMIN))
  558. return -EPERM;
  559. if (flags & ~FAN_ALL_INIT_FLAGS)
  560. return -EINVAL;
  561. if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
  562. return -EINVAL;
  563. switch (event_f_flags & O_ACCMODE) {
  564. case O_RDONLY:
  565. case O_RDWR:
  566. case O_WRONLY:
  567. break;
  568. default:
  569. return -EINVAL;
  570. }
  571. user = get_current_user();
  572. if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
  573. free_uid(user);
  574. return -EMFILE;
  575. }
  576. f_flags = O_RDWR | FMODE_NONOTIFY;
  577. if (flags & FAN_CLOEXEC)
  578. f_flags |= O_CLOEXEC;
  579. if (flags & FAN_NONBLOCK)
  580. f_flags |= O_NONBLOCK;
  581. /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
  582. group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
  583. if (IS_ERR(group)) {
  584. free_uid(user);
  585. return PTR_ERR(group);
  586. }
  587. group->fanotify_data.user = user;
  588. atomic_inc(&user->fanotify_listeners);
  589. oevent = fanotify_alloc_event(NULL, FS_Q_OVERFLOW, NULL);
  590. if (unlikely(!oevent)) {
  591. fd = -ENOMEM;
  592. goto out_destroy_group;
  593. }
  594. group->overflow_event = &oevent->fse;
  595. if (force_o_largefile())
  596. event_f_flags |= O_LARGEFILE;
  597. group->fanotify_data.f_flags = event_f_flags;
  598. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  599. spin_lock_init(&group->fanotify_data.access_lock);
  600. init_waitqueue_head(&group->fanotify_data.access_waitq);
  601. INIT_LIST_HEAD(&group->fanotify_data.access_list);
  602. atomic_set(&group->fanotify_data.bypass_perm, 0);
  603. #endif
  604. switch (flags & FAN_ALL_CLASS_BITS) {
  605. case FAN_CLASS_NOTIF:
  606. group->priority = FS_PRIO_0;
  607. break;
  608. case FAN_CLASS_CONTENT:
  609. group->priority = FS_PRIO_1;
  610. break;
  611. case FAN_CLASS_PRE_CONTENT:
  612. group->priority = FS_PRIO_2;
  613. break;
  614. default:
  615. fd = -EINVAL;
  616. goto out_destroy_group;
  617. }
  618. if (flags & FAN_UNLIMITED_QUEUE) {
  619. fd = -EPERM;
  620. if (!capable(CAP_SYS_ADMIN))
  621. goto out_destroy_group;
  622. group->max_events = UINT_MAX;
  623. } else {
  624. group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
  625. }
  626. if (flags & FAN_UNLIMITED_MARKS) {
  627. fd = -EPERM;
  628. if (!capable(CAP_SYS_ADMIN))
  629. goto out_destroy_group;
  630. group->fanotify_data.max_marks = UINT_MAX;
  631. } else {
  632. group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
  633. }
  634. fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
  635. if (fd < 0)
  636. goto out_destroy_group;
  637. return fd;
  638. out_destroy_group:
  639. fsnotify_destroy_group(group);
  640. return fd;
  641. }
  642. SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
  643. __u64, mask, int, dfd,
  644. const char __user *, pathname)
  645. {
  646. struct inode *inode = NULL;
  647. struct vfsmount *mnt = NULL;
  648. struct fsnotify_group *group;
  649. struct fd f;
  650. struct path path;
  651. int ret;
  652. pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
  653. __func__, fanotify_fd, flags, dfd, pathname, mask);
  654. /* we only use the lower 32 bits as of right now. */
  655. if (mask & ((__u64)0xffffffff << 32))
  656. return -EINVAL;
  657. if (flags & ~FAN_ALL_MARK_FLAGS)
  658. return -EINVAL;
  659. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
  660. case FAN_MARK_ADD: /* fallthrough */
  661. case FAN_MARK_REMOVE:
  662. if (!mask)
  663. return -EINVAL;
  664. break;
  665. case FAN_MARK_FLUSH:
  666. if (flags & ~(FAN_MARK_MOUNT | FAN_MARK_FLUSH))
  667. return -EINVAL;
  668. break;
  669. default:
  670. return -EINVAL;
  671. }
  672. if (mask & FAN_ONDIR) {
  673. flags |= FAN_MARK_ONDIR;
  674. mask &= ~FAN_ONDIR;
  675. }
  676. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  677. if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
  678. #else
  679. if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
  680. #endif
  681. return -EINVAL;
  682. f = fdget(fanotify_fd);
  683. if (unlikely(!f.file))
  684. return -EBADF;
  685. /* verify that this is indeed an fanotify instance */
  686. ret = -EINVAL;
  687. if (unlikely(f.file->f_op != &fanotify_fops))
  688. goto fput_and_out;
  689. group = f.file->private_data;
  690. /*
  691. * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
  692. * allowed to set permissions events.
  693. */
  694. ret = -EINVAL;
  695. if (mask & FAN_ALL_PERM_EVENTS &&
  696. group->priority == FS_PRIO_0)
  697. goto fput_and_out;
  698. if (flags & FAN_MARK_FLUSH) {
  699. ret = 0;
  700. if (flags & FAN_MARK_MOUNT)
  701. fsnotify_clear_vfsmount_marks_by_group(group);
  702. else
  703. fsnotify_clear_inode_marks_by_group(group);
  704. goto fput_and_out;
  705. }
  706. ret = fanotify_find_path(dfd, pathname, &path, flags);
  707. if (ret)
  708. goto fput_and_out;
  709. /* inode held in place by reference to path; group by fget on fd */
  710. if (!(flags & FAN_MARK_MOUNT))
  711. inode = path.dentry->d_inode;
  712. else
  713. mnt = path.mnt;
  714. /* create/update an inode mark */
  715. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
  716. case FAN_MARK_ADD:
  717. if (flags & FAN_MARK_MOUNT)
  718. ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
  719. else
  720. ret = fanotify_add_inode_mark(group, inode, mask, flags);
  721. break;
  722. case FAN_MARK_REMOVE:
  723. if (flags & FAN_MARK_MOUNT)
  724. ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
  725. else
  726. ret = fanotify_remove_inode_mark(group, inode, mask, flags);
  727. break;
  728. default:
  729. ret = -EINVAL;
  730. }
  731. path_put(&path);
  732. fput_and_out:
  733. fdput(f);
  734. return ret;
  735. }
  736. #ifdef CONFIG_COMPAT
  737. COMPAT_SYSCALL_DEFINE6(fanotify_mark,
  738. int, fanotify_fd, unsigned int, flags,
  739. __u32, mask0, __u32, mask1, int, dfd,
  740. const char __user *, pathname)
  741. {
  742. return sys_fanotify_mark(fanotify_fd, flags,
  743. #ifdef __BIG_ENDIAN
  744. ((__u64)mask0 << 32) | mask1,
  745. #else
  746. ((__u64)mask1 << 32) | mask0,
  747. #endif
  748. dfd, pathname);
  749. }
  750. #endif
  751. /*
  752. * fanotify_user_setup - Our initialization function. Note that we cannot return
  753. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  754. * must result in panic().
  755. */
  756. static int __init fanotify_user_setup(void)
  757. {
  758. fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
  759. fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
  760. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  761. fanotify_perm_event_cachep = KMEM_CACHE(fanotify_perm_event_info,
  762. SLAB_PANIC);
  763. #endif
  764. return 0;
  765. }
  766. device_initcall(fanotify_user_setup);