fanotify_user.c 22 KB

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