fanotify_user.c 21 KB

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