fanotify_user.c 21 KB

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