fanotify_user.c 21 KB

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