fanotify_user.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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. #include "fanotify.h"
  21. #define FANOTIFY_DEFAULT_MAX_EVENTS 16384
  22. #define FANOTIFY_DEFAULT_MAX_MARKS 8192
  23. #define FANOTIFY_DEFAULT_MAX_LISTENERS 128
  24. extern const struct fsnotify_ops fanotify_fsnotify_ops;
  25. static struct kmem_cache *fanotify_mark_cache __read_mostly;
  26. static struct kmem_cache *fanotify_response_event_cache __read_mostly;
  27. struct kmem_cache *fanotify_event_cachep __read_mostly;
  28. struct fanotify_response_event {
  29. struct list_head list;
  30. __s32 fd;
  31. struct fanotify_event_info *event;
  32. };
  33. /*
  34. * Get an fsnotify notification event if one exists and is small
  35. * enough to fit in "count". Return an error pointer if the count
  36. * is not large enough.
  37. *
  38. * Called with the group->notification_mutex held.
  39. */
  40. static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
  41. size_t count)
  42. {
  43. BUG_ON(!mutex_is_locked(&group->notification_mutex));
  44. pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
  45. if (fsnotify_notify_queue_is_empty(group))
  46. return NULL;
  47. if (FAN_EVENT_METADATA_LEN > count)
  48. return ERR_PTR(-EINVAL);
  49. /* held the notification_mutex the whole time, so this is the
  50. * same event we peeked above */
  51. return fsnotify_remove_notify_event(group);
  52. }
  53. static int create_fd(struct fsnotify_group *group,
  54. struct fanotify_event_info *event,
  55. struct file **file)
  56. {
  57. int client_fd;
  58. struct file *new_file;
  59. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  60. client_fd = get_unused_fd();
  61. if (client_fd < 0)
  62. return client_fd;
  63. /*
  64. * we need a new file handle for the userspace program so it can read even if it was
  65. * originally opened O_WRONLY.
  66. */
  67. /* it's possible this event was an overflow event. in that case dentry and mnt
  68. * are NULL; That's fine, just don't call dentry open */
  69. if (event->path.dentry && event->path.mnt)
  70. new_file = dentry_open(&event->path,
  71. group->fanotify_data.f_flags | FMODE_NONOTIFY,
  72. current_cred());
  73. else
  74. new_file = ERR_PTR(-EOVERFLOW);
  75. if (IS_ERR(new_file)) {
  76. /*
  77. * we still send an event even if we can't open the file. this
  78. * can happen when say tasks are gone and we try to open their
  79. * /proc files or we try to open a WRONLY file like in sysfs
  80. * we just send the errno to userspace since there isn't much
  81. * else we can do.
  82. */
  83. put_unused_fd(client_fd);
  84. client_fd = PTR_ERR(new_file);
  85. } else {
  86. *file = new_file;
  87. }
  88. return client_fd;
  89. }
  90. static int fill_event_metadata(struct fsnotify_group *group,
  91. struct fanotify_event_metadata *metadata,
  92. struct fsnotify_event *fsn_event,
  93. struct file **file)
  94. {
  95. int ret = 0;
  96. struct fanotify_event_info *event;
  97. pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
  98. group, metadata, fsn_event);
  99. *file = NULL;
  100. event = container_of(fsn_event, struct fanotify_event_info, fse);
  101. metadata->event_len = FAN_EVENT_METADATA_LEN;
  102. metadata->metadata_len = FAN_EVENT_METADATA_LEN;
  103. metadata->vers = FANOTIFY_METADATA_VERSION;
  104. metadata->reserved = 0;
  105. metadata->mask = fsn_event->mask & FAN_ALL_OUTGOING_EVENTS;
  106. metadata->pid = pid_vnr(event->tgid);
  107. if (unlikely(fsn_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 = FANOTIFY_E(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. FANOTIFY_E(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. FANOTIFY_E(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. /*
  264. * Permission events get destroyed after we
  265. * receive response
  266. */
  267. if (!(kevent->mask & FAN_ALL_PERM_EVENTS))
  268. fsnotify_destroy_event(group, kevent);
  269. if (ret < 0)
  270. break;
  271. buf += ret;
  272. count -= ret;
  273. continue;
  274. }
  275. ret = -EAGAIN;
  276. if (file->f_flags & O_NONBLOCK)
  277. break;
  278. ret = -ERESTARTSYS;
  279. if (signal_pending(current))
  280. break;
  281. if (start != buf)
  282. break;
  283. schedule();
  284. }
  285. finish_wait(&group->notification_waitq, &wait);
  286. if (start != buf && ret != -EFAULT)
  287. ret = buf - start;
  288. return ret;
  289. }
  290. static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  291. {
  292. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  293. struct fanotify_response response = { .fd = -1, .response = -1 };
  294. struct fsnotify_group *group;
  295. int ret;
  296. group = file->private_data;
  297. if (count > sizeof(response))
  298. count = sizeof(response);
  299. pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
  300. if (copy_from_user(&response, buf, count))
  301. return -EFAULT;
  302. ret = process_access_response(group, &response);
  303. if (ret < 0)
  304. count = ret;
  305. return count;
  306. #else
  307. return -EINVAL;
  308. #endif
  309. }
  310. static int fanotify_release(struct inode *ignored, struct file *file)
  311. {
  312. struct fsnotify_group *group = file->private_data;
  313. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  314. struct fanotify_response_event *re, *lre;
  315. mutex_lock(&group->fanotify_data.access_mutex);
  316. atomic_inc(&group->fanotify_data.bypass_perm);
  317. list_for_each_entry_safe(re, lre, &group->fanotify_data.access_list, list) {
  318. pr_debug("%s: found group=%p re=%p event=%p\n", __func__, group,
  319. re, re->event);
  320. list_del_init(&re->list);
  321. re->event->response = FAN_ALLOW;
  322. kmem_cache_free(fanotify_response_event_cache, re);
  323. }
  324. mutex_unlock(&group->fanotify_data.access_mutex);
  325. wake_up(&group->fanotify_data.access_waitq);
  326. #endif
  327. /* matches the fanotify_init->fsnotify_alloc_group */
  328. fsnotify_destroy_group(group);
  329. return 0;
  330. }
  331. static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  332. {
  333. struct fsnotify_group *group;
  334. struct fsnotify_event *fsn_event;
  335. void __user *p;
  336. int ret = -ENOTTY;
  337. size_t send_len = 0;
  338. group = file->private_data;
  339. p = (void __user *) arg;
  340. switch (cmd) {
  341. case FIONREAD:
  342. mutex_lock(&group->notification_mutex);
  343. list_for_each_entry(fsn_event, &group->notification_list, list)
  344. send_len += FAN_EVENT_METADATA_LEN;
  345. mutex_unlock(&group->notification_mutex);
  346. ret = put_user(send_len, (int __user *) p);
  347. break;
  348. }
  349. return ret;
  350. }
  351. static const struct file_operations fanotify_fops = {
  352. .show_fdinfo = fanotify_show_fdinfo,
  353. .poll = fanotify_poll,
  354. .read = fanotify_read,
  355. .write = fanotify_write,
  356. .fasync = NULL,
  357. .release = fanotify_release,
  358. .unlocked_ioctl = fanotify_ioctl,
  359. .compat_ioctl = fanotify_ioctl,
  360. .llseek = noop_llseek,
  361. };
  362. static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
  363. {
  364. kmem_cache_free(fanotify_mark_cache, fsn_mark);
  365. }
  366. static int fanotify_find_path(int dfd, const char __user *filename,
  367. struct path *path, unsigned int flags)
  368. {
  369. int ret;
  370. pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
  371. dfd, filename, flags);
  372. if (filename == NULL) {
  373. struct fd f = fdget(dfd);
  374. ret = -EBADF;
  375. if (!f.file)
  376. goto out;
  377. ret = -ENOTDIR;
  378. if ((flags & FAN_MARK_ONLYDIR) &&
  379. !(S_ISDIR(file_inode(f.file)->i_mode))) {
  380. fdput(f);
  381. goto out;
  382. }
  383. *path = f.file->f_path;
  384. path_get(path);
  385. fdput(f);
  386. } else {
  387. unsigned int lookup_flags = 0;
  388. if (!(flags & FAN_MARK_DONT_FOLLOW))
  389. lookup_flags |= LOOKUP_FOLLOW;
  390. if (flags & FAN_MARK_ONLYDIR)
  391. lookup_flags |= LOOKUP_DIRECTORY;
  392. ret = user_path_at(dfd, filename, lookup_flags, path);
  393. if (ret)
  394. goto out;
  395. }
  396. /* you can only watch an inode if you have read permissions on it */
  397. ret = inode_permission(path->dentry->d_inode, MAY_READ);
  398. if (ret)
  399. path_put(path);
  400. out:
  401. return ret;
  402. }
  403. static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
  404. __u32 mask,
  405. unsigned int flags,
  406. int *destroy)
  407. {
  408. __u32 oldmask;
  409. spin_lock(&fsn_mark->lock);
  410. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  411. oldmask = fsn_mark->mask;
  412. fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
  413. } else {
  414. oldmask = fsn_mark->ignored_mask;
  415. fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
  416. }
  417. spin_unlock(&fsn_mark->lock);
  418. *destroy = !(oldmask & ~mask);
  419. return mask & oldmask;
  420. }
  421. static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
  422. struct vfsmount *mnt, __u32 mask,
  423. unsigned int flags)
  424. {
  425. struct fsnotify_mark *fsn_mark = NULL;
  426. __u32 removed;
  427. int destroy_mark;
  428. mutex_lock(&group->mark_mutex);
  429. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  430. if (!fsn_mark) {
  431. mutex_unlock(&group->mark_mutex);
  432. return -ENOENT;
  433. }
  434. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
  435. &destroy_mark);
  436. if (destroy_mark)
  437. fsnotify_destroy_mark_locked(fsn_mark, group);
  438. mutex_unlock(&group->mark_mutex);
  439. fsnotify_put_mark(fsn_mark);
  440. if (removed & real_mount(mnt)->mnt_fsnotify_mask)
  441. fsnotify_recalc_vfsmount_mask(mnt);
  442. return 0;
  443. }
  444. static int fanotify_remove_inode_mark(struct fsnotify_group *group,
  445. struct inode *inode, __u32 mask,
  446. unsigned int flags)
  447. {
  448. struct fsnotify_mark *fsn_mark = NULL;
  449. __u32 removed;
  450. int destroy_mark;
  451. mutex_lock(&group->mark_mutex);
  452. fsn_mark = fsnotify_find_inode_mark(group, inode);
  453. if (!fsn_mark) {
  454. mutex_unlock(&group->mark_mutex);
  455. return -ENOENT;
  456. }
  457. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
  458. &destroy_mark);
  459. if (destroy_mark)
  460. fsnotify_destroy_mark_locked(fsn_mark, group);
  461. mutex_unlock(&group->mark_mutex);
  462. /* matches the fsnotify_find_inode_mark() */
  463. fsnotify_put_mark(fsn_mark);
  464. if (removed & inode->i_fsnotify_mask)
  465. fsnotify_recalc_inode_mask(inode);
  466. return 0;
  467. }
  468. static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
  469. __u32 mask,
  470. unsigned int flags)
  471. {
  472. __u32 oldmask = -1;
  473. spin_lock(&fsn_mark->lock);
  474. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  475. oldmask = fsn_mark->mask;
  476. fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
  477. } else {
  478. __u32 tmask = fsn_mark->ignored_mask | mask;
  479. fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask);
  480. if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
  481. fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
  482. }
  483. if (!(flags & FAN_MARK_ONDIR)) {
  484. __u32 tmask = fsn_mark->ignored_mask | FAN_ONDIR;
  485. fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask);
  486. }
  487. spin_unlock(&fsn_mark->lock);
  488. return mask & ~oldmask;
  489. }
  490. static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
  491. struct inode *inode,
  492. struct vfsmount *mnt)
  493. {
  494. struct fsnotify_mark *mark;
  495. int ret;
  496. if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
  497. return ERR_PTR(-ENOSPC);
  498. mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  499. if (!mark)
  500. return ERR_PTR(-ENOMEM);
  501. fsnotify_init_mark(mark, fanotify_free_mark);
  502. ret = fsnotify_add_mark_locked(mark, group, inode, mnt, 0);
  503. if (ret) {
  504. fsnotify_put_mark(mark);
  505. return ERR_PTR(ret);
  506. }
  507. return mark;
  508. }
  509. static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
  510. struct vfsmount *mnt, __u32 mask,
  511. unsigned int flags)
  512. {
  513. struct fsnotify_mark *fsn_mark;
  514. __u32 added;
  515. mutex_lock(&group->mark_mutex);
  516. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  517. if (!fsn_mark) {
  518. fsn_mark = fanotify_add_new_mark(group, NULL, mnt);
  519. if (IS_ERR(fsn_mark)) {
  520. mutex_unlock(&group->mark_mutex);
  521. return PTR_ERR(fsn_mark);
  522. }
  523. }
  524. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  525. mutex_unlock(&group->mark_mutex);
  526. if (added & ~real_mount(mnt)->mnt_fsnotify_mask)
  527. fsnotify_recalc_vfsmount_mask(mnt);
  528. fsnotify_put_mark(fsn_mark);
  529. return 0;
  530. }
  531. static int fanotify_add_inode_mark(struct fsnotify_group *group,
  532. struct inode *inode, __u32 mask,
  533. unsigned int flags)
  534. {
  535. struct fsnotify_mark *fsn_mark;
  536. __u32 added;
  537. pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
  538. /*
  539. * If some other task has this inode open for write we should not add
  540. * an ignored mark, unless that ignored mark is supposed to survive
  541. * modification changes anyway.
  542. */
  543. if ((flags & FAN_MARK_IGNORED_MASK) &&
  544. !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
  545. (atomic_read(&inode->i_writecount) > 0))
  546. return 0;
  547. mutex_lock(&group->mark_mutex);
  548. fsn_mark = fsnotify_find_inode_mark(group, inode);
  549. if (!fsn_mark) {
  550. fsn_mark = fanotify_add_new_mark(group, inode, NULL);
  551. if (IS_ERR(fsn_mark)) {
  552. mutex_unlock(&group->mark_mutex);
  553. return PTR_ERR(fsn_mark);
  554. }
  555. }
  556. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  557. mutex_unlock(&group->mark_mutex);
  558. if (added & ~inode->i_fsnotify_mask)
  559. fsnotify_recalc_inode_mask(inode);
  560. fsnotify_put_mark(fsn_mark);
  561. return 0;
  562. }
  563. /* fanotify syscalls */
  564. SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
  565. {
  566. struct fsnotify_group *group;
  567. int f_flags, fd;
  568. struct user_struct *user;
  569. struct fanotify_event_info *oevent;
  570. pr_debug("%s: flags=%d event_f_flags=%d\n",
  571. __func__, flags, event_f_flags);
  572. if (!capable(CAP_SYS_ADMIN))
  573. return -EPERM;
  574. if (flags & ~FAN_ALL_INIT_FLAGS)
  575. return -EINVAL;
  576. user = get_current_user();
  577. if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
  578. free_uid(user);
  579. return -EMFILE;
  580. }
  581. f_flags = O_RDWR | FMODE_NONOTIFY;
  582. if (flags & FAN_CLOEXEC)
  583. f_flags |= O_CLOEXEC;
  584. if (flags & FAN_NONBLOCK)
  585. f_flags |= O_NONBLOCK;
  586. /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
  587. group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
  588. if (IS_ERR(group)) {
  589. free_uid(user);
  590. return PTR_ERR(group);
  591. }
  592. group->fanotify_data.user = user;
  593. atomic_inc(&user->fanotify_listeners);
  594. oevent = kmem_cache_alloc(fanotify_event_cachep, GFP_KERNEL);
  595. if (unlikely(!oevent)) {
  596. fd = -ENOMEM;
  597. goto out_destroy_group;
  598. }
  599. group->overflow_event = &oevent->fse;
  600. fsnotify_init_event(group->overflow_event, NULL, FS_Q_OVERFLOW);
  601. oevent->tgid = get_pid(task_tgid(current));
  602. oevent->path.mnt = NULL;
  603. oevent->path.dentry = NULL;
  604. group->fanotify_data.f_flags = event_f_flags;
  605. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  606. oevent->response = 0;
  607. mutex_init(&group->fanotify_data.access_mutex);
  608. init_waitqueue_head(&group->fanotify_data.access_waitq);
  609. INIT_LIST_HEAD(&group->fanotify_data.access_list);
  610. atomic_set(&group->fanotify_data.bypass_perm, 0);
  611. #endif
  612. switch (flags & FAN_ALL_CLASS_BITS) {
  613. case FAN_CLASS_NOTIF:
  614. group->priority = FS_PRIO_0;
  615. break;
  616. case FAN_CLASS_CONTENT:
  617. group->priority = FS_PRIO_1;
  618. break;
  619. case FAN_CLASS_PRE_CONTENT:
  620. group->priority = FS_PRIO_2;
  621. break;
  622. default:
  623. fd = -EINVAL;
  624. goto out_destroy_group;
  625. }
  626. if (flags & FAN_UNLIMITED_QUEUE) {
  627. fd = -EPERM;
  628. if (!capable(CAP_SYS_ADMIN))
  629. goto out_destroy_group;
  630. group->max_events = UINT_MAX;
  631. } else {
  632. group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
  633. }
  634. if (flags & FAN_UNLIMITED_MARKS) {
  635. fd = -EPERM;
  636. if (!capable(CAP_SYS_ADMIN))
  637. goto out_destroy_group;
  638. group->fanotify_data.max_marks = UINT_MAX;
  639. } else {
  640. group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
  641. }
  642. fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
  643. if (fd < 0)
  644. goto out_destroy_group;
  645. return fd;
  646. out_destroy_group:
  647. fsnotify_destroy_group(group);
  648. return fd;
  649. }
  650. SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
  651. __u64, mask, int, dfd,
  652. const char __user *, pathname)
  653. {
  654. struct inode *inode = NULL;
  655. struct vfsmount *mnt = NULL;
  656. struct fsnotify_group *group;
  657. struct fd f;
  658. struct path path;
  659. int ret;
  660. pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
  661. __func__, fanotify_fd, flags, dfd, pathname, mask);
  662. /* we only use the lower 32 bits as of right now. */
  663. if (mask & ((__u64)0xffffffff << 32))
  664. return -EINVAL;
  665. if (flags & ~FAN_ALL_MARK_FLAGS)
  666. return -EINVAL;
  667. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
  668. case FAN_MARK_ADD: /* fallthrough */
  669. case FAN_MARK_REMOVE:
  670. if (!mask)
  671. return -EINVAL;
  672. case FAN_MARK_FLUSH:
  673. break;
  674. default:
  675. return -EINVAL;
  676. }
  677. if (mask & FAN_ONDIR) {
  678. flags |= FAN_MARK_ONDIR;
  679. mask &= ~FAN_ONDIR;
  680. }
  681. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  682. if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
  683. #else
  684. if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
  685. #endif
  686. return -EINVAL;
  687. f = fdget(fanotify_fd);
  688. if (unlikely(!f.file))
  689. return -EBADF;
  690. /* verify that this is indeed an fanotify instance */
  691. ret = -EINVAL;
  692. if (unlikely(f.file->f_op != &fanotify_fops))
  693. goto fput_and_out;
  694. group = f.file->private_data;
  695. /*
  696. * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
  697. * allowed to set permissions events.
  698. */
  699. ret = -EINVAL;
  700. if (mask & FAN_ALL_PERM_EVENTS &&
  701. group->priority == FS_PRIO_0)
  702. goto fput_and_out;
  703. ret = fanotify_find_path(dfd, pathname, &path, flags);
  704. if (ret)
  705. goto fput_and_out;
  706. /* inode held in place by reference to path; group by fget on fd */
  707. if (!(flags & FAN_MARK_MOUNT))
  708. inode = path.dentry->d_inode;
  709. else
  710. mnt = path.mnt;
  711. /* create/update an inode mark */
  712. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
  713. case FAN_MARK_ADD:
  714. if (flags & FAN_MARK_MOUNT)
  715. ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
  716. else
  717. ret = fanotify_add_inode_mark(group, inode, mask, flags);
  718. break;
  719. case FAN_MARK_REMOVE:
  720. if (flags & FAN_MARK_MOUNT)
  721. ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
  722. else
  723. ret = fanotify_remove_inode_mark(group, inode, mask, flags);
  724. break;
  725. case FAN_MARK_FLUSH:
  726. if (flags & FAN_MARK_MOUNT)
  727. fsnotify_clear_vfsmount_marks_by_group(group);
  728. else
  729. fsnotify_clear_inode_marks_by_group(group);
  730. break;
  731. default:
  732. ret = -EINVAL;
  733. }
  734. path_put(&path);
  735. fput_and_out:
  736. fdput(f);
  737. return ret;
  738. }
  739. #ifdef CONFIG_COMPAT
  740. COMPAT_SYSCALL_DEFINE6(fanotify_mark,
  741. int, fanotify_fd, unsigned int, flags,
  742. __u32, mask0, __u32, mask1, int, dfd,
  743. const char __user *, pathname)
  744. {
  745. return sys_fanotify_mark(fanotify_fd, flags,
  746. #ifdef __BIG_ENDIAN
  747. ((__u64)mask0 << 32) | mask1,
  748. #else
  749. ((__u64)mask1 << 32) | mask0,
  750. #endif
  751. dfd, pathname);
  752. }
  753. #endif
  754. /*
  755. * fanotify_user_setup - Our initialization function. Note that we cannot return
  756. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  757. * must result in panic().
  758. */
  759. static int __init fanotify_user_setup(void)
  760. {
  761. fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
  762. fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event,
  763. SLAB_PANIC);
  764. fanotify_event_cachep = KMEM_CACHE(fanotify_event_info, SLAB_PANIC);
  765. return 0;
  766. }
  767. device_initcall(fanotify_user_setup);