fanotify_user.c 21 KB

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