fanotify_user.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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/types.h>
  14. #include <linux/uaccess.h>
  15. #include <asm/ioctls.h>
  16. extern const struct fsnotify_ops fanotify_fsnotify_ops;
  17. static struct kmem_cache *fanotify_mark_cache __read_mostly;
  18. /*
  19. * Get an fsnotify notification event if one exists and is small
  20. * enough to fit in "count". Return an error pointer if the count
  21. * is not large enough.
  22. *
  23. * Called with the group->notification_mutex held.
  24. */
  25. static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
  26. size_t count)
  27. {
  28. BUG_ON(!mutex_is_locked(&group->notification_mutex));
  29. pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
  30. if (fsnotify_notify_queue_is_empty(group))
  31. return NULL;
  32. if (FAN_EVENT_METADATA_LEN > count)
  33. return ERR_PTR(-EINVAL);
  34. /* held the notification_mutex the whole time, so this is the
  35. * same event we peeked above */
  36. return fsnotify_remove_notify_event(group);
  37. }
  38. static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
  39. {
  40. int client_fd;
  41. struct dentry *dentry;
  42. struct vfsmount *mnt;
  43. struct file *new_file;
  44. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  45. client_fd = get_unused_fd();
  46. if (client_fd < 0)
  47. return client_fd;
  48. if (event->data_type != FSNOTIFY_EVENT_PATH) {
  49. WARN_ON(1);
  50. put_unused_fd(client_fd);
  51. return -EINVAL;
  52. }
  53. /*
  54. * we need a new file handle for the userspace program so it can read even if it was
  55. * originally opened O_WRONLY.
  56. */
  57. dentry = dget(event->path.dentry);
  58. mnt = mntget(event->path.mnt);
  59. /* it's possible this event was an overflow event. in that case dentry and mnt
  60. * are NULL; That's fine, just don't call dentry open */
  61. if (dentry && mnt)
  62. new_file = dentry_open(dentry, mnt,
  63. O_RDONLY | O_LARGEFILE | FMODE_NONOTIFY,
  64. current_cred());
  65. else
  66. new_file = ERR_PTR(-EOVERFLOW);
  67. if (IS_ERR(new_file)) {
  68. /*
  69. * we still send an event even if we can't open the file. this
  70. * can happen when say tasks are gone and we try to open their
  71. * /proc files or we try to open a WRONLY file like in sysfs
  72. * we just send the errno to userspace since there isn't much
  73. * else we can do.
  74. */
  75. put_unused_fd(client_fd);
  76. client_fd = PTR_ERR(new_file);
  77. } else {
  78. fd_install(client_fd, new_file);
  79. }
  80. return client_fd;
  81. }
  82. static ssize_t fill_event_metadata(struct fsnotify_group *group,
  83. struct fanotify_event_metadata *metadata,
  84. struct fsnotify_event *event)
  85. {
  86. pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
  87. group, metadata, event);
  88. metadata->event_len = FAN_EVENT_METADATA_LEN;
  89. metadata->vers = FANOTIFY_METADATA_VERSION;
  90. metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS;
  91. metadata->pid = pid_vnr(event->tgid);
  92. metadata->fd = create_fd(group, event);
  93. return metadata->fd;
  94. }
  95. static ssize_t copy_event_to_user(struct fsnotify_group *group,
  96. struct fsnotify_event *event,
  97. char __user *buf)
  98. {
  99. struct fanotify_event_metadata fanotify_event_metadata;
  100. int ret;
  101. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  102. ret = fill_event_metadata(group, &fanotify_event_metadata, event);
  103. if (ret < 0)
  104. return ret;
  105. if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
  106. return -EFAULT;
  107. return FAN_EVENT_METADATA_LEN;
  108. }
  109. /* intofiy userspace file descriptor functions */
  110. static unsigned int fanotify_poll(struct file *file, poll_table *wait)
  111. {
  112. struct fsnotify_group *group = file->private_data;
  113. int ret = 0;
  114. poll_wait(file, &group->notification_waitq, wait);
  115. mutex_lock(&group->notification_mutex);
  116. if (!fsnotify_notify_queue_is_empty(group))
  117. ret = POLLIN | POLLRDNORM;
  118. mutex_unlock(&group->notification_mutex);
  119. return ret;
  120. }
  121. static ssize_t fanotify_read(struct file *file, char __user *buf,
  122. size_t count, loff_t *pos)
  123. {
  124. struct fsnotify_group *group;
  125. struct fsnotify_event *kevent;
  126. char __user *start;
  127. int ret;
  128. DEFINE_WAIT(wait);
  129. start = buf;
  130. group = file->private_data;
  131. pr_debug("%s: group=%p\n", __func__, group);
  132. while (1) {
  133. prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
  134. mutex_lock(&group->notification_mutex);
  135. kevent = get_one_event(group, count);
  136. mutex_unlock(&group->notification_mutex);
  137. if (kevent) {
  138. ret = PTR_ERR(kevent);
  139. if (IS_ERR(kevent))
  140. break;
  141. ret = copy_event_to_user(group, kevent, buf);
  142. fsnotify_put_event(kevent);
  143. if (ret < 0)
  144. break;
  145. buf += ret;
  146. count -= ret;
  147. continue;
  148. }
  149. ret = -EAGAIN;
  150. if (file->f_flags & O_NONBLOCK)
  151. break;
  152. ret = -EINTR;
  153. if (signal_pending(current))
  154. break;
  155. if (start != buf)
  156. break;
  157. schedule();
  158. }
  159. finish_wait(&group->notification_waitq, &wait);
  160. if (start != buf && ret != -EFAULT)
  161. ret = buf - start;
  162. return ret;
  163. }
  164. static int fanotify_release(struct inode *ignored, struct file *file)
  165. {
  166. struct fsnotify_group *group = file->private_data;
  167. pr_debug("%s: file=%p group=%p\n", __func__, file, group);
  168. /* matches the fanotify_init->fsnotify_alloc_group */
  169. fsnotify_put_group(group);
  170. return 0;
  171. }
  172. static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  173. {
  174. struct fsnotify_group *group;
  175. struct fsnotify_event_holder *holder;
  176. void __user *p;
  177. int ret = -ENOTTY;
  178. size_t send_len = 0;
  179. group = file->private_data;
  180. p = (void __user *) arg;
  181. switch (cmd) {
  182. case FIONREAD:
  183. mutex_lock(&group->notification_mutex);
  184. list_for_each_entry(holder, &group->notification_list, event_list)
  185. send_len += FAN_EVENT_METADATA_LEN;
  186. mutex_unlock(&group->notification_mutex);
  187. ret = put_user(send_len, (int __user *) p);
  188. break;
  189. }
  190. return ret;
  191. }
  192. static const struct file_operations fanotify_fops = {
  193. .poll = fanotify_poll,
  194. .read = fanotify_read,
  195. .fasync = NULL,
  196. .release = fanotify_release,
  197. .unlocked_ioctl = fanotify_ioctl,
  198. .compat_ioctl = fanotify_ioctl,
  199. };
  200. static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
  201. {
  202. kmem_cache_free(fanotify_mark_cache, fsn_mark);
  203. }
  204. static int fanotify_find_path(int dfd, const char __user *filename,
  205. struct path *path, unsigned int flags)
  206. {
  207. int ret;
  208. pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
  209. dfd, filename, flags);
  210. if (filename == NULL) {
  211. struct file *file;
  212. int fput_needed;
  213. ret = -EBADF;
  214. file = fget_light(dfd, &fput_needed);
  215. if (!file)
  216. goto out;
  217. ret = -ENOTDIR;
  218. if ((flags & FAN_MARK_ONLYDIR) &&
  219. !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
  220. fput_light(file, fput_needed);
  221. goto out;
  222. }
  223. *path = file->f_path;
  224. path_get(path);
  225. fput_light(file, fput_needed);
  226. } else {
  227. unsigned int lookup_flags = 0;
  228. if (!(flags & FAN_MARK_DONT_FOLLOW))
  229. lookup_flags |= LOOKUP_FOLLOW;
  230. if (flags & FAN_MARK_ONLYDIR)
  231. lookup_flags |= LOOKUP_DIRECTORY;
  232. ret = user_path_at(dfd, filename, lookup_flags, path);
  233. if (ret)
  234. goto out;
  235. }
  236. /* you can only watch an inode if you have read permissions on it */
  237. ret = inode_permission(path->dentry->d_inode, MAY_READ);
  238. if (ret)
  239. path_put(path);
  240. out:
  241. return ret;
  242. }
  243. static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
  244. __u32 mask,
  245. unsigned int flags)
  246. {
  247. __u32 oldmask;
  248. spin_lock(&fsn_mark->lock);
  249. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  250. oldmask = fsn_mark->mask;
  251. fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
  252. } else {
  253. oldmask = fsn_mark->ignored_mask;
  254. fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
  255. }
  256. spin_unlock(&fsn_mark->lock);
  257. if (!(oldmask & ~mask))
  258. fsnotify_destroy_mark(fsn_mark);
  259. return mask & oldmask;
  260. }
  261. static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
  262. struct vfsmount *mnt, __u32 mask,
  263. unsigned int flags)
  264. {
  265. struct fsnotify_mark *fsn_mark = NULL;
  266. __u32 removed;
  267. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  268. if (!fsn_mark)
  269. return -ENOENT;
  270. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
  271. fsnotify_put_mark(fsn_mark);
  272. if (removed & group->mask)
  273. fsnotify_recalc_group_mask(group);
  274. if (removed & mnt->mnt_fsnotify_mask)
  275. fsnotify_recalc_vfsmount_mask(mnt);
  276. return 0;
  277. }
  278. static int fanotify_remove_inode_mark(struct fsnotify_group *group,
  279. struct inode *inode, __u32 mask,
  280. unsigned int flags)
  281. {
  282. struct fsnotify_mark *fsn_mark = NULL;
  283. __u32 removed;
  284. fsn_mark = fsnotify_find_inode_mark(group, inode);
  285. if (!fsn_mark)
  286. return -ENOENT;
  287. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
  288. /* matches the fsnotify_find_inode_mark() */
  289. fsnotify_put_mark(fsn_mark);
  290. if (removed & group->mask)
  291. fsnotify_recalc_group_mask(group);
  292. if (removed & inode->i_fsnotify_mask)
  293. fsnotify_recalc_inode_mask(inode);
  294. return 0;
  295. }
  296. static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
  297. __u32 mask,
  298. unsigned int flags)
  299. {
  300. __u32 oldmask;
  301. spin_lock(&fsn_mark->lock);
  302. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  303. oldmask = fsn_mark->mask;
  304. fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
  305. } else {
  306. oldmask = fsn_mark->ignored_mask;
  307. fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask | mask));
  308. }
  309. spin_unlock(&fsn_mark->lock);
  310. return mask & ~oldmask;
  311. }
  312. static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
  313. struct vfsmount *mnt, __u32 mask,
  314. unsigned int flags)
  315. {
  316. struct fsnotify_mark *fsn_mark;
  317. __u32 added;
  318. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  319. if (!fsn_mark) {
  320. int ret;
  321. fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  322. if (!fsn_mark)
  323. return -ENOMEM;
  324. fsnotify_init_mark(fsn_mark, fanotify_free_mark);
  325. ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
  326. if (ret) {
  327. fanotify_free_mark(fsn_mark);
  328. return ret;
  329. }
  330. }
  331. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  332. fsnotify_put_mark(fsn_mark);
  333. if (added) {
  334. if (added & ~group->mask)
  335. fsnotify_recalc_group_mask(group);
  336. if (added & ~mnt->mnt_fsnotify_mask)
  337. fsnotify_recalc_vfsmount_mask(mnt);
  338. }
  339. return 0;
  340. }
  341. static int fanotify_add_inode_mark(struct fsnotify_group *group,
  342. struct inode *inode, __u32 mask,
  343. unsigned int flags)
  344. {
  345. struct fsnotify_mark *fsn_mark;
  346. __u32 added;
  347. pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
  348. fsn_mark = fsnotify_find_inode_mark(group, inode);
  349. if (!fsn_mark) {
  350. int ret;
  351. fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  352. if (!fsn_mark)
  353. return -ENOMEM;
  354. fsnotify_init_mark(fsn_mark, fanotify_free_mark);
  355. ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
  356. if (ret) {
  357. fanotify_free_mark(fsn_mark);
  358. return ret;
  359. }
  360. }
  361. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  362. fsnotify_put_mark(fsn_mark);
  363. if (added) {
  364. if (added & ~group->mask)
  365. fsnotify_recalc_group_mask(group);
  366. if (added & ~inode->i_fsnotify_mask)
  367. fsnotify_recalc_inode_mask(inode);
  368. }
  369. return 0;
  370. }
  371. /* fanotify syscalls */
  372. SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags,
  373. unsigned int, priority)
  374. {
  375. struct fsnotify_group *group;
  376. int f_flags, fd;
  377. pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n",
  378. __func__, flags, event_f_flags, priority);
  379. if (event_f_flags)
  380. return -EINVAL;
  381. if (priority)
  382. return -EINVAL;
  383. if (!capable(CAP_SYS_ADMIN))
  384. return -EACCES;
  385. if (flags & ~FAN_ALL_INIT_FLAGS)
  386. return -EINVAL;
  387. f_flags = (O_RDONLY | FMODE_NONOTIFY);
  388. if (flags & FAN_CLOEXEC)
  389. f_flags |= O_CLOEXEC;
  390. if (flags & FAN_NONBLOCK)
  391. f_flags |= O_NONBLOCK;
  392. /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
  393. group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
  394. if (IS_ERR(group))
  395. return PTR_ERR(group);
  396. fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
  397. if (fd < 0)
  398. goto out_put_group;
  399. return fd;
  400. out_put_group:
  401. fsnotify_put_group(group);
  402. return fd;
  403. }
  404. SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
  405. __u64 mask, int dfd,
  406. const char __user * pathname)
  407. {
  408. struct inode *inode = NULL;
  409. struct vfsmount *mnt = NULL;
  410. struct fsnotify_group *group;
  411. struct file *filp;
  412. struct path path;
  413. int ret, fput_needed;
  414. pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
  415. __func__, fanotify_fd, flags, dfd, pathname, mask);
  416. /* we only use the lower 32 bits as of right now. */
  417. if (mask & ((__u64)0xffffffff << 32))
  418. return -EINVAL;
  419. if (flags & ~FAN_ALL_MARK_FLAGS)
  420. return -EINVAL;
  421. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
  422. case FAN_MARK_ADD:
  423. case FAN_MARK_REMOVE:
  424. break;
  425. default:
  426. return -EINVAL;
  427. }
  428. if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
  429. return -EINVAL;
  430. filp = fget_light(fanotify_fd, &fput_needed);
  431. if (unlikely(!filp))
  432. return -EBADF;
  433. /* verify that this is indeed an fanotify instance */
  434. ret = -EINVAL;
  435. if (unlikely(filp->f_op != &fanotify_fops))
  436. goto fput_and_out;
  437. ret = fanotify_find_path(dfd, pathname, &path, flags);
  438. if (ret)
  439. goto fput_and_out;
  440. /* inode held in place by reference to path; group by fget on fd */
  441. if (!(flags & FAN_MARK_MOUNT))
  442. inode = path.dentry->d_inode;
  443. else
  444. mnt = path.mnt;
  445. group = filp->private_data;
  446. /* create/update an inode mark */
  447. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
  448. case FAN_MARK_ADD:
  449. if (flags & FAN_MARK_MOUNT)
  450. ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
  451. else
  452. ret = fanotify_add_inode_mark(group, inode, mask, flags);
  453. break;
  454. case FAN_MARK_REMOVE:
  455. if (flags & FAN_MARK_MOUNT)
  456. ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
  457. else
  458. ret = fanotify_remove_inode_mark(group, inode, mask, flags);
  459. break;
  460. default:
  461. ret = -EINVAL;
  462. }
  463. path_put(&path);
  464. fput_and_out:
  465. fput_light(filp, fput_needed);
  466. return ret;
  467. }
  468. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  469. asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
  470. long dfd, long pathname)
  471. {
  472. return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
  473. mask, (int) dfd,
  474. (const char __user *) pathname);
  475. }
  476. SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
  477. #endif
  478. /*
  479. * fanotify_user_setup - Our initialization function. Note that we cannnot return
  480. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  481. * must result in panic().
  482. */
  483. static int __init fanotify_user_setup(void)
  484. {
  485. fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
  486. return 0;
  487. }
  488. device_initcall(fanotify_user_setup);