fanotify_user.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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, __u32 mask)
  244. {
  245. __u32 oldmask;
  246. spin_lock(&fsn_mark->lock);
  247. oldmask = fsn_mark->mask;
  248. fsn_mark->mask = oldmask & ~mask;
  249. spin_unlock(&fsn_mark->lock);
  250. if (!(oldmask & ~mask))
  251. fsnotify_destroy_mark(fsn_mark);
  252. return mask & oldmask;
  253. }
  254. static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
  255. struct vfsmount *mnt, __u32 mask)
  256. {
  257. struct fsnotify_mark *fsn_mark = NULL;
  258. __u32 removed;
  259. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  260. if (!fsn_mark)
  261. return -ENOENT;
  262. removed = fanotify_mark_remove_from_mask(fsn_mark, mask);
  263. fsnotify_put_mark(fsn_mark);
  264. if (removed & group->mask)
  265. fsnotify_recalc_group_mask(group);
  266. if (removed & mnt->mnt_fsnotify_mask)
  267. fsnotify_recalc_vfsmount_mask(mnt);
  268. return 0;
  269. }
  270. static int fanotify_remove_inode_mark(struct fsnotify_group *group,
  271. struct inode *inode, __u32 mask)
  272. {
  273. struct fsnotify_mark *fsn_mark = NULL;
  274. __u32 removed;
  275. fsn_mark = fsnotify_find_inode_mark(group, inode);
  276. if (!fsn_mark)
  277. return -ENOENT;
  278. removed = fanotify_mark_remove_from_mask(fsn_mark, mask);
  279. /* matches the fsnotify_find_inode_mark() */
  280. fsnotify_put_mark(fsn_mark);
  281. if (removed & group->mask)
  282. fsnotify_recalc_group_mask(group);
  283. if (removed & inode->i_fsnotify_mask)
  284. fsnotify_recalc_inode_mask(inode);
  285. return 0;
  286. }
  287. static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, __u32 mask)
  288. {
  289. __u32 oldmask;
  290. spin_lock(&fsn_mark->lock);
  291. oldmask = fsn_mark->mask;
  292. fsn_mark->mask = oldmask | mask;
  293. spin_unlock(&fsn_mark->lock);
  294. return mask & ~oldmask;
  295. }
  296. static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
  297. struct vfsmount *mnt, __u32 mask)
  298. {
  299. struct fsnotify_mark *fsn_mark;
  300. __u32 added;
  301. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  302. if (!fsn_mark) {
  303. int ret;
  304. fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  305. if (!fsn_mark)
  306. return -ENOMEM;
  307. fsnotify_init_mark(fsn_mark, fanotify_free_mark);
  308. ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
  309. if (ret) {
  310. fanotify_free_mark(fsn_mark);
  311. return ret;
  312. }
  313. }
  314. added = fanotify_mark_add_to_mask(fsn_mark, mask);
  315. fsnotify_put_mark(fsn_mark);
  316. if (added) {
  317. if (added & ~group->mask)
  318. fsnotify_recalc_group_mask(group);
  319. if (added & ~mnt->mnt_fsnotify_mask)
  320. fsnotify_recalc_vfsmount_mask(mnt);
  321. }
  322. return 0;
  323. }
  324. static int fanotify_add_inode_mark(struct fsnotify_group *group,
  325. struct inode *inode, __u32 mask)
  326. {
  327. struct fsnotify_mark *fsn_mark;
  328. __u32 added;
  329. pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
  330. fsn_mark = fsnotify_find_inode_mark(group, inode);
  331. if (!fsn_mark) {
  332. int ret;
  333. fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  334. if (!fsn_mark)
  335. return -ENOMEM;
  336. fsnotify_init_mark(fsn_mark, fanotify_free_mark);
  337. ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
  338. if (ret) {
  339. fanotify_free_mark(fsn_mark);
  340. return ret;
  341. }
  342. }
  343. added = fanotify_mark_add_to_mask(fsn_mark, mask);
  344. fsnotify_put_mark(fsn_mark);
  345. if (added) {
  346. if (added & ~group->mask)
  347. fsnotify_recalc_group_mask(group);
  348. if (added & ~inode->i_fsnotify_mask)
  349. fsnotify_recalc_inode_mask(inode);
  350. }
  351. return 0;
  352. }
  353. /* fanotify syscalls */
  354. SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags,
  355. unsigned int, priority)
  356. {
  357. struct fsnotify_group *group;
  358. int f_flags, fd;
  359. pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n",
  360. __func__, flags, event_f_flags, priority);
  361. if (event_f_flags)
  362. return -EINVAL;
  363. if (priority)
  364. return -EINVAL;
  365. if (!capable(CAP_SYS_ADMIN))
  366. return -EACCES;
  367. if (flags & ~FAN_ALL_INIT_FLAGS)
  368. return -EINVAL;
  369. f_flags = (O_RDONLY | FMODE_NONOTIFY);
  370. if (flags & FAN_CLOEXEC)
  371. f_flags |= O_CLOEXEC;
  372. if (flags & FAN_NONBLOCK)
  373. f_flags |= O_NONBLOCK;
  374. /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
  375. group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
  376. if (IS_ERR(group))
  377. return PTR_ERR(group);
  378. fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
  379. if (fd < 0)
  380. goto out_put_group;
  381. return fd;
  382. out_put_group:
  383. fsnotify_put_group(group);
  384. return fd;
  385. }
  386. SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
  387. __u64 mask, int dfd,
  388. const char __user * pathname)
  389. {
  390. struct inode *inode = NULL;
  391. struct vfsmount *mnt = NULL;
  392. struct fsnotify_group *group;
  393. struct file *filp;
  394. struct path path;
  395. int ret, fput_needed;
  396. pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
  397. __func__, fanotify_fd, flags, dfd, pathname, mask);
  398. /* we only use the lower 32 bits as of right now. */
  399. if (mask & ((__u64)0xffffffff << 32))
  400. return -EINVAL;
  401. if (flags & ~FAN_ALL_MARK_FLAGS)
  402. return -EINVAL;
  403. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
  404. case FAN_MARK_ADD:
  405. case FAN_MARK_REMOVE:
  406. break;
  407. default:
  408. return -EINVAL;
  409. }
  410. if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
  411. return -EINVAL;
  412. filp = fget_light(fanotify_fd, &fput_needed);
  413. if (unlikely(!filp))
  414. return -EBADF;
  415. /* verify that this is indeed an fanotify instance */
  416. ret = -EINVAL;
  417. if (unlikely(filp->f_op != &fanotify_fops))
  418. goto fput_and_out;
  419. ret = fanotify_find_path(dfd, pathname, &path, flags);
  420. if (ret)
  421. goto fput_and_out;
  422. /* inode held in place by reference to path; group by fget on fd */
  423. if (!(flags & FAN_MARK_MOUNT))
  424. inode = path.dentry->d_inode;
  425. else
  426. mnt = path.mnt;
  427. group = filp->private_data;
  428. /* create/update an inode mark */
  429. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
  430. case FAN_MARK_ADD:
  431. if (flags & FAN_MARK_MOUNT)
  432. ret = fanotify_add_vfsmount_mark(group, mnt, mask);
  433. else
  434. ret = fanotify_add_inode_mark(group, inode, mask);
  435. break;
  436. case FAN_MARK_REMOVE:
  437. if (flags & FAN_MARK_MOUNT)
  438. ret = fanotify_remove_vfsmount_mark(group, mnt, mask);
  439. else
  440. ret = fanotify_remove_inode_mark(group, inode, mask);
  441. break;
  442. default:
  443. ret = -EINVAL;
  444. }
  445. path_put(&path);
  446. fput_and_out:
  447. fput_light(filp, fput_needed);
  448. return ret;
  449. }
  450. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  451. asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
  452. long dfd, long pathname)
  453. {
  454. return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
  455. mask, (int) dfd,
  456. (const char __user *) pathname);
  457. }
  458. SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
  459. #endif
  460. /*
  461. * fanotify_user_setup - Our initialization function. Note that we cannnot return
  462. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  463. * must result in panic().
  464. */
  465. static int __init fanotify_user_setup(void)
  466. {
  467. fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
  468. return 0;
  469. }
  470. device_initcall(fanotify_user_setup);