fanotify_user.c 21 KB

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