fanotify_user.c 21 KB

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