fanotify_user.c 22 KB

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