fanotify_user.c 13 KB

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