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