fanotify_user.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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 file *file;
  365. int fput_needed;
  366. ret = -EBADF;
  367. file = fget_light(dfd, &fput_needed);
  368. if (!file)
  369. goto out;
  370. ret = -ENOTDIR;
  371. if ((flags & FAN_MARK_ONLYDIR) &&
  372. !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
  373. fput_light(file, fput_needed);
  374. goto out;
  375. }
  376. *path = file->f_path;
  377. path_get(path);
  378. fput_light(file, fput_needed);
  379. } else {
  380. unsigned int lookup_flags = 0;
  381. if (!(flags & FAN_MARK_DONT_FOLLOW))
  382. lookup_flags |= LOOKUP_FOLLOW;
  383. if (flags & FAN_MARK_ONLYDIR)
  384. lookup_flags |= LOOKUP_DIRECTORY;
  385. ret = user_path_at(dfd, filename, lookup_flags, path);
  386. if (ret)
  387. goto out;
  388. }
  389. /* you can only watch an inode if you have read permissions on it */
  390. ret = inode_permission(path->dentry->d_inode, MAY_READ);
  391. if (ret)
  392. path_put(path);
  393. out:
  394. return ret;
  395. }
  396. static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
  397. __u32 mask,
  398. unsigned int flags)
  399. {
  400. __u32 oldmask;
  401. spin_lock(&fsn_mark->lock);
  402. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  403. oldmask = fsn_mark->mask;
  404. fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
  405. } else {
  406. oldmask = fsn_mark->ignored_mask;
  407. fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
  408. }
  409. spin_unlock(&fsn_mark->lock);
  410. if (!(oldmask & ~mask))
  411. fsnotify_destroy_mark(fsn_mark);
  412. return mask & oldmask;
  413. }
  414. static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
  415. struct vfsmount *mnt, __u32 mask,
  416. unsigned int flags)
  417. {
  418. struct fsnotify_mark *fsn_mark = NULL;
  419. __u32 removed;
  420. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  421. if (!fsn_mark)
  422. return -ENOENT;
  423. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
  424. fsnotify_put_mark(fsn_mark);
  425. if (removed & real_mount(mnt)->mnt_fsnotify_mask)
  426. fsnotify_recalc_vfsmount_mask(mnt);
  427. return 0;
  428. }
  429. static int fanotify_remove_inode_mark(struct fsnotify_group *group,
  430. struct inode *inode, __u32 mask,
  431. unsigned int flags)
  432. {
  433. struct fsnotify_mark *fsn_mark = NULL;
  434. __u32 removed;
  435. fsn_mark = fsnotify_find_inode_mark(group, inode);
  436. if (!fsn_mark)
  437. return -ENOENT;
  438. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
  439. /* matches the fsnotify_find_inode_mark() */
  440. fsnotify_put_mark(fsn_mark);
  441. if (removed & inode->i_fsnotify_mask)
  442. fsnotify_recalc_inode_mask(inode);
  443. return 0;
  444. }
  445. static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
  446. __u32 mask,
  447. unsigned int flags)
  448. {
  449. __u32 oldmask = -1;
  450. spin_lock(&fsn_mark->lock);
  451. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  452. oldmask = fsn_mark->mask;
  453. fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
  454. } else {
  455. __u32 tmask = fsn_mark->ignored_mask | mask;
  456. fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask);
  457. if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
  458. fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
  459. }
  460. if (!(flags & FAN_MARK_ONDIR)) {
  461. __u32 tmask = fsn_mark->ignored_mask | FAN_ONDIR;
  462. fsnotify_set_mark_ignored_mask_locked(fsn_mark, tmask);
  463. }
  464. spin_unlock(&fsn_mark->lock);
  465. return mask & ~oldmask;
  466. }
  467. static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
  468. struct vfsmount *mnt, __u32 mask,
  469. unsigned int flags)
  470. {
  471. struct fsnotify_mark *fsn_mark;
  472. __u32 added;
  473. int ret = 0;
  474. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  475. if (!fsn_mark) {
  476. if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
  477. return -ENOSPC;
  478. fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  479. if (!fsn_mark)
  480. return -ENOMEM;
  481. fsnotify_init_mark(fsn_mark, fanotify_free_mark);
  482. ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
  483. if (ret)
  484. goto err;
  485. }
  486. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  487. if (added & ~real_mount(mnt)->mnt_fsnotify_mask)
  488. fsnotify_recalc_vfsmount_mask(mnt);
  489. err:
  490. fsnotify_put_mark(fsn_mark);
  491. return ret;
  492. }
  493. static int fanotify_add_inode_mark(struct fsnotify_group *group,
  494. struct inode *inode, __u32 mask,
  495. unsigned int flags)
  496. {
  497. struct fsnotify_mark *fsn_mark;
  498. __u32 added;
  499. int ret = 0;
  500. pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
  501. /*
  502. * If some other task has this inode open for write we should not add
  503. * an ignored mark, unless that ignored mark is supposed to survive
  504. * modification changes anyway.
  505. */
  506. if ((flags & FAN_MARK_IGNORED_MASK) &&
  507. !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
  508. (atomic_read(&inode->i_writecount) > 0))
  509. return 0;
  510. fsn_mark = fsnotify_find_inode_mark(group, inode);
  511. if (!fsn_mark) {
  512. if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
  513. return -ENOSPC;
  514. fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  515. if (!fsn_mark)
  516. return -ENOMEM;
  517. fsnotify_init_mark(fsn_mark, fanotify_free_mark);
  518. ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
  519. if (ret)
  520. goto err;
  521. }
  522. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  523. if (added & ~inode->i_fsnotify_mask)
  524. fsnotify_recalc_inode_mask(inode);
  525. err:
  526. fsnotify_put_mark(fsn_mark);
  527. return ret;
  528. }
  529. /* fanotify syscalls */
  530. SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
  531. {
  532. struct fsnotify_group *group;
  533. int f_flags, fd;
  534. struct user_struct *user;
  535. pr_debug("%s: flags=%d event_f_flags=%d\n",
  536. __func__, flags, event_f_flags);
  537. if (!capable(CAP_SYS_ADMIN))
  538. return -EPERM;
  539. if (flags & ~FAN_ALL_INIT_FLAGS)
  540. return -EINVAL;
  541. user = get_current_user();
  542. if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
  543. free_uid(user);
  544. return -EMFILE;
  545. }
  546. f_flags = O_RDWR | FMODE_NONOTIFY;
  547. if (flags & FAN_CLOEXEC)
  548. f_flags |= O_CLOEXEC;
  549. if (flags & FAN_NONBLOCK)
  550. f_flags |= O_NONBLOCK;
  551. /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
  552. group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
  553. if (IS_ERR(group)) {
  554. free_uid(user);
  555. return PTR_ERR(group);
  556. }
  557. group->fanotify_data.user = user;
  558. atomic_inc(&user->fanotify_listeners);
  559. group->fanotify_data.f_flags = event_f_flags;
  560. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  561. mutex_init(&group->fanotify_data.access_mutex);
  562. init_waitqueue_head(&group->fanotify_data.access_waitq);
  563. INIT_LIST_HEAD(&group->fanotify_data.access_list);
  564. atomic_set(&group->fanotify_data.bypass_perm, 0);
  565. #endif
  566. switch (flags & FAN_ALL_CLASS_BITS) {
  567. case FAN_CLASS_NOTIF:
  568. group->priority = FS_PRIO_0;
  569. break;
  570. case FAN_CLASS_CONTENT:
  571. group->priority = FS_PRIO_1;
  572. break;
  573. case FAN_CLASS_PRE_CONTENT:
  574. group->priority = FS_PRIO_2;
  575. break;
  576. default:
  577. fd = -EINVAL;
  578. goto out_put_group;
  579. }
  580. if (flags & FAN_UNLIMITED_QUEUE) {
  581. fd = -EPERM;
  582. if (!capable(CAP_SYS_ADMIN))
  583. goto out_put_group;
  584. group->max_events = UINT_MAX;
  585. } else {
  586. group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
  587. }
  588. if (flags & FAN_UNLIMITED_MARKS) {
  589. fd = -EPERM;
  590. if (!capable(CAP_SYS_ADMIN))
  591. goto out_put_group;
  592. group->fanotify_data.max_marks = UINT_MAX;
  593. } else {
  594. group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
  595. }
  596. fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
  597. if (fd < 0)
  598. goto out_put_group;
  599. return fd;
  600. out_put_group:
  601. fsnotify_put_group(group);
  602. return fd;
  603. }
  604. SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
  605. __u64 mask, int dfd,
  606. const char __user * pathname)
  607. {
  608. struct inode *inode = NULL;
  609. struct vfsmount *mnt = NULL;
  610. struct fsnotify_group *group;
  611. struct file *filp;
  612. struct path path;
  613. int ret, fput_needed;
  614. pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
  615. __func__, fanotify_fd, flags, dfd, pathname, mask);
  616. /* we only use the lower 32 bits as of right now. */
  617. if (mask & ((__u64)0xffffffff << 32))
  618. return -EINVAL;
  619. if (flags & ~FAN_ALL_MARK_FLAGS)
  620. return -EINVAL;
  621. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
  622. case FAN_MARK_ADD: /* fallthrough */
  623. case FAN_MARK_REMOVE:
  624. if (!mask)
  625. return -EINVAL;
  626. case FAN_MARK_FLUSH:
  627. break;
  628. default:
  629. return -EINVAL;
  630. }
  631. if (mask & FAN_ONDIR) {
  632. flags |= FAN_MARK_ONDIR;
  633. mask &= ~FAN_ONDIR;
  634. }
  635. #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
  636. if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
  637. #else
  638. if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
  639. #endif
  640. return -EINVAL;
  641. filp = fget_light(fanotify_fd, &fput_needed);
  642. if (unlikely(!filp))
  643. return -EBADF;
  644. /* verify that this is indeed an fanotify instance */
  645. ret = -EINVAL;
  646. if (unlikely(filp->f_op != &fanotify_fops))
  647. goto fput_and_out;
  648. group = filp->private_data;
  649. /*
  650. * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
  651. * allowed to set permissions events.
  652. */
  653. ret = -EINVAL;
  654. if (mask & FAN_ALL_PERM_EVENTS &&
  655. group->priority == FS_PRIO_0)
  656. goto fput_and_out;
  657. ret = fanotify_find_path(dfd, pathname, &path, flags);
  658. if (ret)
  659. goto fput_and_out;
  660. /* inode held in place by reference to path; group by fget on fd */
  661. if (!(flags & FAN_MARK_MOUNT))
  662. inode = path.dentry->d_inode;
  663. else
  664. mnt = path.mnt;
  665. /* create/update an inode mark */
  666. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
  667. case FAN_MARK_ADD:
  668. if (flags & FAN_MARK_MOUNT)
  669. ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
  670. else
  671. ret = fanotify_add_inode_mark(group, inode, mask, flags);
  672. break;
  673. case FAN_MARK_REMOVE:
  674. if (flags & FAN_MARK_MOUNT)
  675. ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
  676. else
  677. ret = fanotify_remove_inode_mark(group, inode, mask, flags);
  678. break;
  679. case FAN_MARK_FLUSH:
  680. if (flags & FAN_MARK_MOUNT)
  681. fsnotify_clear_vfsmount_marks_by_group(group);
  682. else
  683. fsnotify_clear_inode_marks_by_group(group);
  684. break;
  685. default:
  686. ret = -EINVAL;
  687. }
  688. path_put(&path);
  689. fput_and_out:
  690. fput_light(filp, fput_needed);
  691. return ret;
  692. }
  693. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  694. asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
  695. long dfd, long pathname)
  696. {
  697. return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
  698. mask, (int) dfd,
  699. (const char __user *) pathname);
  700. }
  701. SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
  702. #endif
  703. /*
  704. * fanotify_user_setup - Our initialization function. Note that we cannot return
  705. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  706. * must result in panic().
  707. */
  708. static int __init fanotify_user_setup(void)
  709. {
  710. fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
  711. fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event,
  712. SLAB_PANIC);
  713. return 0;
  714. }
  715. device_initcall(fanotify_user_setup);