fanotify_user.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/fanotify.h>
  3. #include <linux/fcntl.h>
  4. #include <linux/file.h>
  5. #include <linux/fs.h>
  6. #include <linux/anon_inodes.h>
  7. #include <linux/fsnotify_backend.h>
  8. #include <linux/init.h>
  9. #include <linux/mount.h>
  10. #include <linux/namei.h>
  11. #include <linux/poll.h>
  12. #include <linux/security.h>
  13. #include <linux/syscalls.h>
  14. #include <linux/slab.h>
  15. #include <linux/types.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/compat.h>
  18. #include <linux/sched/signal.h>
  19. #include <linux/memcontrol.h>
  20. #include <linux/statfs.h>
  21. #include <linux/exportfs.h>
  22. #include <asm/ioctls.h>
  23. #include "../../mount.h"
  24. #include "../fdinfo.h"
  25. #include "fanotify.h"
  26. #define FANOTIFY_DEFAULT_MAX_EVENTS 16384
  27. #define FANOTIFY_DEFAULT_MAX_MARKS 8192
  28. #define FANOTIFY_DEFAULT_MAX_LISTENERS 128
  29. /*
  30. * All flags that may be specified in parameter event_f_flags of fanotify_init.
  31. *
  32. * Internal and external open flags are stored together in field f_flags of
  33. * struct file. Only external open flags shall be allowed in event_f_flags.
  34. * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
  35. * excluded.
  36. */
  37. #define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
  38. O_ACCMODE | O_APPEND | O_NONBLOCK | \
  39. __O_SYNC | O_DSYNC | O_CLOEXEC | \
  40. O_LARGEFILE | O_NOATIME )
  41. extern const struct fsnotify_ops fanotify_fsnotify_ops;
  42. struct kmem_cache *fanotify_mark_cache __read_mostly;
  43. struct kmem_cache *fanotify_fid_event_cachep __read_mostly;
  44. struct kmem_cache *fanotify_path_event_cachep __read_mostly;
  45. struct kmem_cache *fanotify_perm_event_cachep __read_mostly;
  46. #define FANOTIFY_EVENT_ALIGN 4
  47. #define FANOTIFY_INFO_HDR_LEN \
  48. (sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
  49. static int fanotify_fid_info_len(int fh_len, int name_len)
  50. {
  51. int info_len = fh_len;
  52. if (name_len)
  53. info_len += name_len + 1;
  54. return roundup(FANOTIFY_INFO_HDR_LEN + info_len, FANOTIFY_EVENT_ALIGN);
  55. }
  56. static int fanotify_event_info_len(unsigned int fid_mode,
  57. struct fanotify_event *event)
  58. {
  59. struct fanotify_info *info = fanotify_event_info(event);
  60. int dir_fh_len = fanotify_event_dir_fh_len(event);
  61. int fh_len = fanotify_event_object_fh_len(event);
  62. int info_len = 0;
  63. int dot_len = 0;
  64. if (dir_fh_len) {
  65. info_len += fanotify_fid_info_len(dir_fh_len, info->name_len);
  66. } else if ((fid_mode & FAN_REPORT_NAME) && (event->mask & FAN_ONDIR)) {
  67. /*
  68. * With group flag FAN_REPORT_NAME, if name was not recorded in
  69. * event on a directory, we will report the name ".".
  70. */
  71. dot_len = 1;
  72. }
  73. if (fh_len)
  74. info_len += fanotify_fid_info_len(fh_len, dot_len);
  75. return info_len;
  76. }
  77. /*
  78. * Get an fanotify notification event if one exists and is small
  79. * enough to fit in "count". Return an error pointer if the count
  80. * is not large enough. When permission event is dequeued, its state is
  81. * updated accordingly.
  82. */
  83. static struct fanotify_event *get_one_event(struct fsnotify_group *group,
  84. size_t count)
  85. {
  86. size_t event_size = FAN_EVENT_METADATA_LEN;
  87. struct fanotify_event *event = NULL;
  88. unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
  89. pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
  90. spin_lock(&group->notification_lock);
  91. if (fsnotify_notify_queue_is_empty(group))
  92. goto out;
  93. if (fid_mode) {
  94. event_size += fanotify_event_info_len(fid_mode,
  95. FANOTIFY_E(fsnotify_peek_first_event(group)));
  96. }
  97. if (event_size > count) {
  98. event = ERR_PTR(-EINVAL);
  99. goto out;
  100. }
  101. event = FANOTIFY_E(fsnotify_remove_first_event(group));
  102. if (fanotify_is_perm_event(event->mask))
  103. FANOTIFY_PERM(event)->state = FAN_EVENT_REPORTED;
  104. out:
  105. spin_unlock(&group->notification_lock);
  106. return event;
  107. }
  108. static int create_fd(struct fsnotify_group *group, struct path *path,
  109. struct file **file)
  110. {
  111. int client_fd;
  112. struct file *new_file;
  113. client_fd = get_unused_fd_flags(group->fanotify_data.f_flags);
  114. if (client_fd < 0)
  115. return client_fd;
  116. /*
  117. * we need a new file handle for the userspace program so it can read even if it was
  118. * originally opened O_WRONLY.
  119. */
  120. new_file = dentry_open(path,
  121. group->fanotify_data.f_flags | FMODE_NONOTIFY,
  122. current_cred());
  123. if (IS_ERR(new_file)) {
  124. /*
  125. * we still send an event even if we can't open the file. this
  126. * can happen when say tasks are gone and we try to open their
  127. * /proc files or we try to open a WRONLY file like in sysfs
  128. * we just send the errno to userspace since there isn't much
  129. * else we can do.
  130. */
  131. put_unused_fd(client_fd);
  132. client_fd = PTR_ERR(new_file);
  133. } else {
  134. *file = new_file;
  135. }
  136. return client_fd;
  137. }
  138. /*
  139. * Finish processing of permission event by setting it to ANSWERED state and
  140. * drop group->notification_lock.
  141. */
  142. static void finish_permission_event(struct fsnotify_group *group,
  143. struct fanotify_perm_event *event,
  144. unsigned int response)
  145. __releases(&group->notification_lock)
  146. {
  147. bool destroy = false;
  148. assert_spin_locked(&group->notification_lock);
  149. event->response = response;
  150. if (event->state == FAN_EVENT_CANCELED)
  151. destroy = true;
  152. else
  153. event->state = FAN_EVENT_ANSWERED;
  154. spin_unlock(&group->notification_lock);
  155. if (destroy)
  156. fsnotify_destroy_event(group, &event->fae.fse);
  157. }
  158. static int process_access_response(struct fsnotify_group *group,
  159. struct fanotify_response *response_struct)
  160. {
  161. struct fanotify_perm_event *event;
  162. int fd = response_struct->fd;
  163. int response = response_struct->response;
  164. pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
  165. fd, response);
  166. /*
  167. * make sure the response is valid, if invalid we do nothing and either
  168. * userspace can send a valid response or we will clean it up after the
  169. * timeout
  170. */
  171. switch (response & ~FAN_AUDIT) {
  172. case FAN_ALLOW:
  173. case FAN_DENY:
  174. break;
  175. default:
  176. return -EINVAL;
  177. }
  178. if (fd < 0)
  179. return -EINVAL;
  180. if ((response & FAN_AUDIT) && !FAN_GROUP_FLAG(group, FAN_ENABLE_AUDIT))
  181. return -EINVAL;
  182. spin_lock(&group->notification_lock);
  183. list_for_each_entry(event, &group->fanotify_data.access_list,
  184. fae.fse.list) {
  185. if (event->fd != fd)
  186. continue;
  187. list_del_init(&event->fae.fse.list);
  188. finish_permission_event(group, event, response);
  189. wake_up(&group->fanotify_data.access_waitq);
  190. return 0;
  191. }
  192. spin_unlock(&group->notification_lock);
  193. return -ENOENT;
  194. }
  195. static int copy_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
  196. int info_type, const char *name, size_t name_len,
  197. char __user *buf, size_t count)
  198. {
  199. struct fanotify_event_info_fid info = { };
  200. struct file_handle handle = { };
  201. unsigned char bounce[FANOTIFY_INLINE_FH_LEN], *fh_buf;
  202. size_t fh_len = fh ? fh->len : 0;
  203. size_t info_len = fanotify_fid_info_len(fh_len, name_len);
  204. size_t len = info_len;
  205. pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
  206. __func__, fh_len, name_len, info_len, count);
  207. if (!fh_len)
  208. return 0;
  209. if (WARN_ON_ONCE(len < sizeof(info) || len > count))
  210. return -EFAULT;
  211. /*
  212. * Copy event info fid header followed by variable sized file handle
  213. * and optionally followed by variable sized filename.
  214. */
  215. switch (info_type) {
  216. case FAN_EVENT_INFO_TYPE_FID:
  217. case FAN_EVENT_INFO_TYPE_DFID:
  218. if (WARN_ON_ONCE(name_len))
  219. return -EFAULT;
  220. break;
  221. case FAN_EVENT_INFO_TYPE_DFID_NAME:
  222. if (WARN_ON_ONCE(!name || !name_len))
  223. return -EFAULT;
  224. break;
  225. default:
  226. return -EFAULT;
  227. }
  228. info.hdr.info_type = info_type;
  229. info.hdr.len = len;
  230. info.fsid = *fsid;
  231. if (copy_to_user(buf, &info, sizeof(info)))
  232. return -EFAULT;
  233. buf += sizeof(info);
  234. len -= sizeof(info);
  235. if (WARN_ON_ONCE(len < sizeof(handle)))
  236. return -EFAULT;
  237. handle.handle_type = fh->type;
  238. handle.handle_bytes = fh_len;
  239. if (copy_to_user(buf, &handle, sizeof(handle)))
  240. return -EFAULT;
  241. buf += sizeof(handle);
  242. len -= sizeof(handle);
  243. if (WARN_ON_ONCE(len < fh_len))
  244. return -EFAULT;
  245. /*
  246. * For an inline fh and inline file name, copy through stack to exclude
  247. * the copy from usercopy hardening protections.
  248. */
  249. fh_buf = fanotify_fh_buf(fh);
  250. if (fh_len <= FANOTIFY_INLINE_FH_LEN) {
  251. memcpy(bounce, fh_buf, fh_len);
  252. fh_buf = bounce;
  253. }
  254. if (copy_to_user(buf, fh_buf, fh_len))
  255. return -EFAULT;
  256. buf += fh_len;
  257. len -= fh_len;
  258. if (name_len) {
  259. /* Copy the filename with terminating null */
  260. name_len++;
  261. if (WARN_ON_ONCE(len < name_len))
  262. return -EFAULT;
  263. if (copy_to_user(buf, name, name_len))
  264. return -EFAULT;
  265. buf += name_len;
  266. len -= name_len;
  267. }
  268. /* Pad with 0's */
  269. WARN_ON_ONCE(len < 0 || len >= FANOTIFY_EVENT_ALIGN);
  270. if (len > 0 && clear_user(buf, len))
  271. return -EFAULT;
  272. return info_len;
  273. }
  274. static ssize_t copy_event_to_user(struct fsnotify_group *group,
  275. struct fanotify_event *event,
  276. char __user *buf, size_t count)
  277. {
  278. struct fanotify_event_metadata metadata;
  279. struct path *path = fanotify_event_path(event);
  280. struct fanotify_info *info = fanotify_event_info(event);
  281. unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
  282. struct file *f = NULL;
  283. int ret, fd = FAN_NOFD;
  284. int info_type = 0;
  285. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  286. metadata.event_len = FAN_EVENT_METADATA_LEN +
  287. fanotify_event_info_len(fid_mode, event);
  288. metadata.metadata_len = FAN_EVENT_METADATA_LEN;
  289. metadata.vers = FANOTIFY_METADATA_VERSION;
  290. metadata.reserved = 0;
  291. metadata.mask = event->mask & FANOTIFY_OUTGOING_EVENTS;
  292. metadata.pid = pid_vnr(event->pid);
  293. if (path && path->mnt && path->dentry) {
  294. fd = create_fd(group, path, &f);
  295. if (fd < 0)
  296. return fd;
  297. }
  298. metadata.fd = fd;
  299. ret = -EFAULT;
  300. /*
  301. * Sanity check copy size in case get_one_event() and
  302. * event_len sizes ever get out of sync.
  303. */
  304. if (WARN_ON_ONCE(metadata.event_len > count))
  305. goto out_close_fd;
  306. if (copy_to_user(buf, &metadata, FAN_EVENT_METADATA_LEN))
  307. goto out_close_fd;
  308. buf += FAN_EVENT_METADATA_LEN;
  309. count -= FAN_EVENT_METADATA_LEN;
  310. if (fanotify_is_perm_event(event->mask))
  311. FANOTIFY_PERM(event)->fd = fd;
  312. /* Event info records order is: dir fid + name, child fid */
  313. if (fanotify_event_dir_fh_len(event)) {
  314. info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
  315. FAN_EVENT_INFO_TYPE_DFID;
  316. ret = copy_info_to_user(fanotify_event_fsid(event),
  317. fanotify_info_dir_fh(info),
  318. info_type, fanotify_info_name(info),
  319. info->name_len, buf, count);
  320. if (ret < 0)
  321. goto out_close_fd;
  322. buf += ret;
  323. count -= ret;
  324. }
  325. if (fanotify_event_object_fh_len(event)) {
  326. const char *dot = NULL;
  327. int dot_len = 0;
  328. if (fid_mode == FAN_REPORT_FID || info_type) {
  329. /*
  330. * With only group flag FAN_REPORT_FID only type FID is
  331. * reported. Second info record type is always FID.
  332. */
  333. info_type = FAN_EVENT_INFO_TYPE_FID;
  334. } else if ((fid_mode & FAN_REPORT_NAME) &&
  335. (event->mask & FAN_ONDIR)) {
  336. /*
  337. * With group flag FAN_REPORT_NAME, if name was not
  338. * recorded in an event on a directory, report the
  339. * name "." with info type DFID_NAME.
  340. */
  341. info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
  342. dot = ".";
  343. dot_len = 1;
  344. } else if ((event->mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
  345. (event->mask & FAN_ONDIR)) {
  346. /*
  347. * With group flag FAN_REPORT_DIR_FID, a single info
  348. * record has type DFID for directory entry modification
  349. * event and for event on a directory.
  350. */
  351. info_type = FAN_EVENT_INFO_TYPE_DFID;
  352. } else {
  353. /*
  354. * With group flags FAN_REPORT_DIR_FID|FAN_REPORT_FID,
  355. * a single info record has type FID for event on a
  356. * non-directory, when there is no directory to report.
  357. * For example, on FAN_DELETE_SELF event.
  358. */
  359. info_type = FAN_EVENT_INFO_TYPE_FID;
  360. }
  361. ret = copy_info_to_user(fanotify_event_fsid(event),
  362. fanotify_event_object_fh(event),
  363. info_type, dot, dot_len, buf, count);
  364. if (ret < 0)
  365. goto out_close_fd;
  366. buf += ret;
  367. count -= ret;
  368. }
  369. if (f)
  370. fd_install(fd, f);
  371. return metadata.event_len;
  372. out_close_fd:
  373. if (fd != FAN_NOFD) {
  374. put_unused_fd(fd);
  375. fput(f);
  376. }
  377. return ret;
  378. }
  379. /* intofiy userspace file descriptor functions */
  380. static __poll_t fanotify_poll(struct file *file, poll_table *wait)
  381. {
  382. struct fsnotify_group *group = file->private_data;
  383. __poll_t ret = 0;
  384. poll_wait(file, &group->notification_waitq, wait);
  385. spin_lock(&group->notification_lock);
  386. if (!fsnotify_notify_queue_is_empty(group))
  387. ret = EPOLLIN | EPOLLRDNORM;
  388. spin_unlock(&group->notification_lock);
  389. return ret;
  390. }
  391. static ssize_t fanotify_read(struct file *file, char __user *buf,
  392. size_t count, loff_t *pos)
  393. {
  394. struct fsnotify_group *group;
  395. struct fanotify_event *event;
  396. char __user *start;
  397. int ret;
  398. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  399. start = buf;
  400. group = file->private_data;
  401. pr_debug("%s: group=%p\n", __func__, group);
  402. add_wait_queue(&group->notification_waitq, &wait);
  403. while (1) {
  404. /*
  405. * User can supply arbitrarily large buffer. Avoid softlockups
  406. * in case there are lots of available events.
  407. */
  408. cond_resched();
  409. event = get_one_event(group, count);
  410. if (IS_ERR(event)) {
  411. ret = PTR_ERR(event);
  412. break;
  413. }
  414. if (!event) {
  415. ret = -EAGAIN;
  416. if (file->f_flags & O_NONBLOCK)
  417. break;
  418. ret = -ERESTARTSYS;
  419. if (signal_pending(current))
  420. break;
  421. if (start != buf)
  422. break;
  423. wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  424. continue;
  425. }
  426. ret = copy_event_to_user(group, event, buf, count);
  427. if (unlikely(ret == -EOPENSTALE)) {
  428. /*
  429. * We cannot report events with stale fd so drop it.
  430. * Setting ret to 0 will continue the event loop and
  431. * do the right thing if there are no more events to
  432. * read (i.e. return bytes read, -EAGAIN or wait).
  433. */
  434. ret = 0;
  435. }
  436. /*
  437. * Permission events get queued to wait for response. Other
  438. * events can be destroyed now.
  439. */
  440. if (!fanotify_is_perm_event(event->mask)) {
  441. fsnotify_destroy_event(group, &event->fse);
  442. } else {
  443. if (ret <= 0) {
  444. spin_lock(&group->notification_lock);
  445. finish_permission_event(group,
  446. FANOTIFY_PERM(event), FAN_DENY);
  447. wake_up(&group->fanotify_data.access_waitq);
  448. } else {
  449. spin_lock(&group->notification_lock);
  450. list_add_tail(&event->fse.list,
  451. &group->fanotify_data.access_list);
  452. spin_unlock(&group->notification_lock);
  453. }
  454. }
  455. if (ret < 0)
  456. break;
  457. buf += ret;
  458. count -= ret;
  459. }
  460. remove_wait_queue(&group->notification_waitq, &wait);
  461. if (start != buf && ret != -EFAULT)
  462. ret = buf - start;
  463. return ret;
  464. }
  465. static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  466. {
  467. struct fanotify_response response = { .fd = -1, .response = -1 };
  468. struct fsnotify_group *group;
  469. int ret;
  470. if (!IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
  471. return -EINVAL;
  472. group = file->private_data;
  473. if (count < sizeof(response))
  474. return -EINVAL;
  475. count = sizeof(response);
  476. pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
  477. if (copy_from_user(&response, buf, count))
  478. return -EFAULT;
  479. ret = process_access_response(group, &response);
  480. if (ret < 0)
  481. count = ret;
  482. return count;
  483. }
  484. static int fanotify_release(struct inode *ignored, struct file *file)
  485. {
  486. struct fsnotify_group *group = file->private_data;
  487. /*
  488. * Stop new events from arriving in the notification queue. since
  489. * userspace cannot use fanotify fd anymore, no event can enter or
  490. * leave access_list by now either.
  491. */
  492. fsnotify_group_stop_queueing(group);
  493. /*
  494. * Process all permission events on access_list and notification queue
  495. * and simulate reply from userspace.
  496. */
  497. spin_lock(&group->notification_lock);
  498. while (!list_empty(&group->fanotify_data.access_list)) {
  499. struct fanotify_perm_event *event;
  500. event = list_first_entry(&group->fanotify_data.access_list,
  501. struct fanotify_perm_event, fae.fse.list);
  502. list_del_init(&event->fae.fse.list);
  503. finish_permission_event(group, event, FAN_ALLOW);
  504. spin_lock(&group->notification_lock);
  505. }
  506. /*
  507. * Destroy all non-permission events. For permission events just
  508. * dequeue them and set the response. They will be freed once the
  509. * response is consumed and fanotify_get_response() returns.
  510. */
  511. while (!fsnotify_notify_queue_is_empty(group)) {
  512. struct fanotify_event *event;
  513. event = FANOTIFY_E(fsnotify_remove_first_event(group));
  514. if (!(event->mask & FANOTIFY_PERM_EVENTS)) {
  515. spin_unlock(&group->notification_lock);
  516. fsnotify_destroy_event(group, &event->fse);
  517. } else {
  518. finish_permission_event(group, FANOTIFY_PERM(event),
  519. FAN_ALLOW);
  520. }
  521. spin_lock(&group->notification_lock);
  522. }
  523. spin_unlock(&group->notification_lock);
  524. /* Response for all permission events it set, wakeup waiters */
  525. wake_up(&group->fanotify_data.access_waitq);
  526. /* matches the fanotify_init->fsnotify_alloc_group */
  527. fsnotify_destroy_group(group);
  528. return 0;
  529. }
  530. static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  531. {
  532. struct fsnotify_group *group;
  533. struct fsnotify_event *fsn_event;
  534. void __user *p;
  535. int ret = -ENOTTY;
  536. size_t send_len = 0;
  537. group = file->private_data;
  538. p = (void __user *) arg;
  539. switch (cmd) {
  540. case FIONREAD:
  541. spin_lock(&group->notification_lock);
  542. list_for_each_entry(fsn_event, &group->notification_list, list)
  543. send_len += FAN_EVENT_METADATA_LEN;
  544. spin_unlock(&group->notification_lock);
  545. ret = put_user(send_len, (int __user *) p);
  546. break;
  547. }
  548. return ret;
  549. }
  550. static const struct file_operations fanotify_fops = {
  551. .show_fdinfo = fanotify_show_fdinfo,
  552. .poll = fanotify_poll,
  553. .read = fanotify_read,
  554. .write = fanotify_write,
  555. .fasync = NULL,
  556. .release = fanotify_release,
  557. .unlocked_ioctl = fanotify_ioctl,
  558. .compat_ioctl = compat_ptr_ioctl,
  559. .llseek = noop_llseek,
  560. };
  561. static int fanotify_find_path(int dfd, const char __user *filename,
  562. struct path *path, unsigned int flags, __u64 mask,
  563. unsigned int obj_type)
  564. {
  565. int ret;
  566. pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
  567. dfd, filename, flags);
  568. if (filename == NULL) {
  569. struct fd f = fdget(dfd);
  570. ret = -EBADF;
  571. if (!f.file)
  572. goto out;
  573. ret = -ENOTDIR;
  574. if ((flags & FAN_MARK_ONLYDIR) &&
  575. !(S_ISDIR(file_inode(f.file)->i_mode))) {
  576. fdput(f);
  577. goto out;
  578. }
  579. *path = f.file->f_path;
  580. path_get(path);
  581. fdput(f);
  582. } else {
  583. unsigned int lookup_flags = 0;
  584. if (!(flags & FAN_MARK_DONT_FOLLOW))
  585. lookup_flags |= LOOKUP_FOLLOW;
  586. if (flags & FAN_MARK_ONLYDIR)
  587. lookup_flags |= LOOKUP_DIRECTORY;
  588. ret = user_path_at(dfd, filename, lookup_flags, path);
  589. if (ret)
  590. goto out;
  591. }
  592. /* you can only watch an inode if you have read permissions on it */
  593. ret = inode_permission(path->dentry->d_inode, MAY_READ);
  594. if (ret) {
  595. path_put(path);
  596. goto out;
  597. }
  598. ret = security_path_notify(path, mask, obj_type);
  599. if (ret)
  600. path_put(path);
  601. out:
  602. return ret;
  603. }
  604. static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
  605. __u32 mask, unsigned int flags,
  606. __u32 umask, int *destroy)
  607. {
  608. __u32 oldmask = 0;
  609. /* umask bits cannot be removed by user */
  610. mask &= ~umask;
  611. spin_lock(&fsn_mark->lock);
  612. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  613. oldmask = fsn_mark->mask;
  614. fsn_mark->mask &= ~mask;
  615. } else {
  616. fsn_mark->ignored_mask &= ~mask;
  617. }
  618. /*
  619. * We need to keep the mark around even if remaining mask cannot
  620. * result in any events (e.g. mask == FAN_ONDIR) to support incremenal
  621. * changes to the mask.
  622. * Destroy mark when only umask bits remain.
  623. */
  624. *destroy = !((fsn_mark->mask | fsn_mark->ignored_mask) & ~umask);
  625. spin_unlock(&fsn_mark->lock);
  626. return mask & oldmask;
  627. }
  628. static int fanotify_remove_mark(struct fsnotify_group *group,
  629. fsnotify_connp_t *connp, __u32 mask,
  630. unsigned int flags, __u32 umask)
  631. {
  632. struct fsnotify_mark *fsn_mark = NULL;
  633. __u32 removed;
  634. int destroy_mark;
  635. mutex_lock(&group->mark_mutex);
  636. fsn_mark = fsnotify_find_mark(connp, group);
  637. if (!fsn_mark) {
  638. mutex_unlock(&group->mark_mutex);
  639. return -ENOENT;
  640. }
  641. removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
  642. umask, &destroy_mark);
  643. if (removed & fsnotify_conn_mask(fsn_mark->connector))
  644. fsnotify_recalc_mask(fsn_mark->connector);
  645. if (destroy_mark)
  646. fsnotify_detach_mark(fsn_mark);
  647. mutex_unlock(&group->mark_mutex);
  648. if (destroy_mark)
  649. fsnotify_free_mark(fsn_mark);
  650. /* matches the fsnotify_find_mark() */
  651. fsnotify_put_mark(fsn_mark);
  652. return 0;
  653. }
  654. static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
  655. struct vfsmount *mnt, __u32 mask,
  656. unsigned int flags, __u32 umask)
  657. {
  658. return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
  659. mask, flags, umask);
  660. }
  661. static int fanotify_remove_sb_mark(struct fsnotify_group *group,
  662. struct super_block *sb, __u32 mask,
  663. unsigned int flags, __u32 umask)
  664. {
  665. return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask,
  666. flags, umask);
  667. }
  668. static int fanotify_remove_inode_mark(struct fsnotify_group *group,
  669. struct inode *inode, __u32 mask,
  670. unsigned int flags, __u32 umask)
  671. {
  672. return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
  673. flags, umask);
  674. }
  675. static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
  676. __u32 mask,
  677. unsigned int flags)
  678. {
  679. __u32 oldmask = -1;
  680. spin_lock(&fsn_mark->lock);
  681. if (!(flags & FAN_MARK_IGNORED_MASK)) {
  682. oldmask = fsn_mark->mask;
  683. fsn_mark->mask |= mask;
  684. } else {
  685. fsn_mark->ignored_mask |= mask;
  686. if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
  687. fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
  688. }
  689. spin_unlock(&fsn_mark->lock);
  690. return mask & ~oldmask;
  691. }
  692. static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
  693. fsnotify_connp_t *connp,
  694. unsigned int type,
  695. __kernel_fsid_t *fsid)
  696. {
  697. struct fsnotify_mark *mark;
  698. int ret;
  699. if (atomic_read(&group->num_marks) > group->fanotify_data.max_marks)
  700. return ERR_PTR(-ENOSPC);
  701. mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
  702. if (!mark)
  703. return ERR_PTR(-ENOMEM);
  704. fsnotify_init_mark(mark, group);
  705. ret = fsnotify_add_mark_locked(mark, connp, type, 0, fsid);
  706. if (ret) {
  707. fsnotify_put_mark(mark);
  708. return ERR_PTR(ret);
  709. }
  710. return mark;
  711. }
  712. static int fanotify_add_mark(struct fsnotify_group *group,
  713. fsnotify_connp_t *connp, unsigned int type,
  714. __u32 mask, unsigned int flags,
  715. __kernel_fsid_t *fsid)
  716. {
  717. struct fsnotify_mark *fsn_mark;
  718. __u32 added;
  719. mutex_lock(&group->mark_mutex);
  720. fsn_mark = fsnotify_find_mark(connp, group);
  721. if (!fsn_mark) {
  722. fsn_mark = fanotify_add_new_mark(group, connp, type, fsid);
  723. if (IS_ERR(fsn_mark)) {
  724. mutex_unlock(&group->mark_mutex);
  725. return PTR_ERR(fsn_mark);
  726. }
  727. }
  728. added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
  729. if (added & ~fsnotify_conn_mask(fsn_mark->connector))
  730. fsnotify_recalc_mask(fsn_mark->connector);
  731. mutex_unlock(&group->mark_mutex);
  732. fsnotify_put_mark(fsn_mark);
  733. return 0;
  734. }
  735. static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
  736. struct vfsmount *mnt, __u32 mask,
  737. unsigned int flags, __kernel_fsid_t *fsid)
  738. {
  739. return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
  740. FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
  741. }
  742. static int fanotify_add_sb_mark(struct fsnotify_group *group,
  743. struct super_block *sb, __u32 mask,
  744. unsigned int flags, __kernel_fsid_t *fsid)
  745. {
  746. return fanotify_add_mark(group, &sb->s_fsnotify_marks,
  747. FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
  748. }
  749. static int fanotify_add_inode_mark(struct fsnotify_group *group,
  750. struct inode *inode, __u32 mask,
  751. unsigned int flags, __kernel_fsid_t *fsid)
  752. {
  753. pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
  754. /*
  755. * If some other task has this inode open for write we should not add
  756. * an ignored mark, unless that ignored mark is supposed to survive
  757. * modification changes anyway.
  758. */
  759. if ((flags & FAN_MARK_IGNORED_MASK) &&
  760. !(flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
  761. inode_is_open_for_write(inode))
  762. return 0;
  763. return fanotify_add_mark(group, &inode->i_fsnotify_marks,
  764. FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
  765. }
  766. static struct fsnotify_event *fanotify_alloc_overflow_event(void)
  767. {
  768. struct fanotify_event *oevent;
  769. oevent = kmalloc(sizeof(*oevent), GFP_KERNEL_ACCOUNT);
  770. if (!oevent)
  771. return NULL;
  772. fanotify_init_event(oevent, 0, FS_Q_OVERFLOW);
  773. oevent->type = FANOTIFY_EVENT_TYPE_OVERFLOW;
  774. return &oevent->fse;
  775. }
  776. /* fanotify syscalls */
  777. SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
  778. {
  779. struct fsnotify_group *group;
  780. int f_flags, fd;
  781. struct user_struct *user;
  782. unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
  783. unsigned int class = flags & FANOTIFY_CLASS_BITS;
  784. pr_debug("%s: flags=%x event_f_flags=%x\n",
  785. __func__, flags, event_f_flags);
  786. if (!capable(CAP_SYS_ADMIN))
  787. return -EPERM;
  788. #ifdef CONFIG_AUDITSYSCALL
  789. if (flags & ~(FANOTIFY_INIT_FLAGS | FAN_ENABLE_AUDIT))
  790. #else
  791. if (flags & ~FANOTIFY_INIT_FLAGS)
  792. #endif
  793. return -EINVAL;
  794. if (event_f_flags & ~FANOTIFY_INIT_ALL_EVENT_F_BITS)
  795. return -EINVAL;
  796. switch (event_f_flags & O_ACCMODE) {
  797. case O_RDONLY:
  798. case O_RDWR:
  799. case O_WRONLY:
  800. break;
  801. default:
  802. return -EINVAL;
  803. }
  804. if (fid_mode && class != FAN_CLASS_NOTIF)
  805. return -EINVAL;
  806. /*
  807. * Child name is reported with parent fid so requires dir fid.
  808. * We can report both child fid and dir fid with or without name.
  809. */
  810. if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID))
  811. return -EINVAL;
  812. user = get_current_user();
  813. if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {
  814. free_uid(user);
  815. return -EMFILE;
  816. }
  817. f_flags = O_RDWR | FMODE_NONOTIFY;
  818. if (flags & FAN_CLOEXEC)
  819. f_flags |= O_CLOEXEC;
  820. if (flags & FAN_NONBLOCK)
  821. f_flags |= O_NONBLOCK;
  822. /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
  823. group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
  824. if (IS_ERR(group)) {
  825. free_uid(user);
  826. return PTR_ERR(group);
  827. }
  828. group->fanotify_data.user = user;
  829. group->fanotify_data.flags = flags;
  830. atomic_inc(&user->fanotify_listeners);
  831. group->memcg = get_mem_cgroup_from_mm(current->mm);
  832. group->overflow_event = fanotify_alloc_overflow_event();
  833. if (unlikely(!group->overflow_event)) {
  834. fd = -ENOMEM;
  835. goto out_destroy_group;
  836. }
  837. if (force_o_largefile())
  838. event_f_flags |= O_LARGEFILE;
  839. group->fanotify_data.f_flags = event_f_flags;
  840. init_waitqueue_head(&group->fanotify_data.access_waitq);
  841. INIT_LIST_HEAD(&group->fanotify_data.access_list);
  842. switch (class) {
  843. case FAN_CLASS_NOTIF:
  844. group->priority = FS_PRIO_0;
  845. break;
  846. case FAN_CLASS_CONTENT:
  847. group->priority = FS_PRIO_1;
  848. break;
  849. case FAN_CLASS_PRE_CONTENT:
  850. group->priority = FS_PRIO_2;
  851. break;
  852. default:
  853. fd = -EINVAL;
  854. goto out_destroy_group;
  855. }
  856. if (flags & FAN_UNLIMITED_QUEUE) {
  857. fd = -EPERM;
  858. if (!capable(CAP_SYS_ADMIN))
  859. goto out_destroy_group;
  860. group->max_events = UINT_MAX;
  861. } else {
  862. group->max_events = FANOTIFY_DEFAULT_MAX_EVENTS;
  863. }
  864. if (flags & FAN_UNLIMITED_MARKS) {
  865. fd = -EPERM;
  866. if (!capable(CAP_SYS_ADMIN))
  867. goto out_destroy_group;
  868. group->fanotify_data.max_marks = UINT_MAX;
  869. } else {
  870. group->fanotify_data.max_marks = FANOTIFY_DEFAULT_MAX_MARKS;
  871. }
  872. if (flags & FAN_ENABLE_AUDIT) {
  873. fd = -EPERM;
  874. if (!capable(CAP_AUDIT_WRITE))
  875. goto out_destroy_group;
  876. }
  877. fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
  878. if (fd < 0)
  879. goto out_destroy_group;
  880. return fd;
  881. out_destroy_group:
  882. fsnotify_destroy_group(group);
  883. return fd;
  884. }
  885. /* Check if filesystem can encode a unique fid */
  886. static int fanotify_test_fid(struct path *path, __kernel_fsid_t *fsid)
  887. {
  888. __kernel_fsid_t root_fsid;
  889. int err;
  890. /*
  891. * Make sure path is not in filesystem with zero fsid (e.g. tmpfs).
  892. */
  893. err = vfs_get_fsid(path->dentry, fsid);
  894. if (err)
  895. return err;
  896. if (!fsid->val[0] && !fsid->val[1])
  897. return -ENODEV;
  898. /*
  899. * Make sure path is not inside a filesystem subvolume (e.g. btrfs)
  900. * which uses a different fsid than sb root.
  901. */
  902. err = vfs_get_fsid(path->dentry->d_sb->s_root, &root_fsid);
  903. if (err)
  904. return err;
  905. if (root_fsid.val[0] != fsid->val[0] ||
  906. root_fsid.val[1] != fsid->val[1])
  907. return -EXDEV;
  908. /*
  909. * We need to make sure that the file system supports at least
  910. * encoding a file handle so user can use name_to_handle_at() to
  911. * compare fid returned with event to the file handle of watched
  912. * objects. However, name_to_handle_at() requires that the
  913. * filesystem also supports decoding file handles.
  914. */
  915. if (!path->dentry->d_sb->s_export_op ||
  916. !path->dentry->d_sb->s_export_op->fh_to_dentry)
  917. return -EOPNOTSUPP;
  918. return 0;
  919. }
  920. static int fanotify_events_supported(struct path *path, __u64 mask)
  921. {
  922. /*
  923. * Some filesystems such as 'proc' acquire unusual locks when opening
  924. * files. For them fanotify permission events have high chances of
  925. * deadlocking the system - open done when reporting fanotify event
  926. * blocks on this "unusual" lock while another process holding the lock
  927. * waits for fanotify permission event to be answered. Just disallow
  928. * permission events for such filesystems.
  929. */
  930. if (mask & FANOTIFY_PERM_EVENTS &&
  931. path->mnt->mnt_sb->s_type->fs_flags & FS_DISALLOW_NOTIFY_PERM)
  932. return -EINVAL;
  933. return 0;
  934. }
  935. static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
  936. int dfd, const char __user *pathname)
  937. {
  938. struct inode *inode = NULL;
  939. struct vfsmount *mnt = NULL;
  940. struct fsnotify_group *group;
  941. struct fd f;
  942. struct path path;
  943. __kernel_fsid_t __fsid, *fsid = NULL;
  944. u32 valid_mask = FANOTIFY_EVENTS | FANOTIFY_EVENT_FLAGS;
  945. unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
  946. bool ignored = flags & FAN_MARK_IGNORED_MASK;
  947. unsigned int obj_type, fid_mode;
  948. u32 umask = 0;
  949. int ret;
  950. pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
  951. __func__, fanotify_fd, flags, dfd, pathname, mask);
  952. /* we only use the lower 32 bits as of right now. */
  953. if (mask & ((__u64)0xffffffff << 32))
  954. return -EINVAL;
  955. if (flags & ~FANOTIFY_MARK_FLAGS)
  956. return -EINVAL;
  957. switch (mark_type) {
  958. case FAN_MARK_INODE:
  959. obj_type = FSNOTIFY_OBJ_TYPE_INODE;
  960. break;
  961. case FAN_MARK_MOUNT:
  962. obj_type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
  963. break;
  964. case FAN_MARK_FILESYSTEM:
  965. obj_type = FSNOTIFY_OBJ_TYPE_SB;
  966. break;
  967. default:
  968. return -EINVAL;
  969. }
  970. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
  971. case FAN_MARK_ADD:
  972. case FAN_MARK_REMOVE:
  973. if (!mask)
  974. return -EINVAL;
  975. break;
  976. case FAN_MARK_FLUSH:
  977. if (flags & ~(FANOTIFY_MARK_TYPE_BITS | FAN_MARK_FLUSH))
  978. return -EINVAL;
  979. break;
  980. default:
  981. return -EINVAL;
  982. }
  983. if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS))
  984. valid_mask |= FANOTIFY_PERM_EVENTS;
  985. if (mask & ~valid_mask)
  986. return -EINVAL;
  987. /* Event flags (ONDIR, ON_CHILD) are meaningless in ignored mask */
  988. if (ignored)
  989. mask &= ~FANOTIFY_EVENT_FLAGS;
  990. f = fdget(fanotify_fd);
  991. if (unlikely(!f.file))
  992. return -EBADF;
  993. /* verify that this is indeed an fanotify instance */
  994. ret = -EINVAL;
  995. if (unlikely(f.file->f_op != &fanotify_fops))
  996. goto fput_and_out;
  997. group = f.file->private_data;
  998. /*
  999. * group->priority == FS_PRIO_0 == FAN_CLASS_NOTIF. These are not
  1000. * allowed to set permissions events.
  1001. */
  1002. ret = -EINVAL;
  1003. if (mask & FANOTIFY_PERM_EVENTS &&
  1004. group->priority == FS_PRIO_0)
  1005. goto fput_and_out;
  1006. /*
  1007. * Events with data type inode do not carry enough information to report
  1008. * event->fd, so we do not allow setting a mask for inode events unless
  1009. * group supports reporting fid.
  1010. * inode events are not supported on a mount mark, because they do not
  1011. * carry enough information (i.e. path) to be filtered by mount point.
  1012. */
  1013. fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
  1014. if (mask & FANOTIFY_INODE_EVENTS &&
  1015. (!fid_mode || mark_type == FAN_MARK_MOUNT))
  1016. goto fput_and_out;
  1017. if (flags & FAN_MARK_FLUSH) {
  1018. ret = 0;
  1019. if (mark_type == FAN_MARK_MOUNT)
  1020. fsnotify_clear_vfsmount_marks_by_group(group);
  1021. else if (mark_type == FAN_MARK_FILESYSTEM)
  1022. fsnotify_clear_sb_marks_by_group(group);
  1023. else
  1024. fsnotify_clear_inode_marks_by_group(group);
  1025. goto fput_and_out;
  1026. }
  1027. ret = fanotify_find_path(dfd, pathname, &path, flags,
  1028. (mask & ALL_FSNOTIFY_EVENTS), obj_type);
  1029. if (ret)
  1030. goto fput_and_out;
  1031. if (flags & FAN_MARK_ADD) {
  1032. ret = fanotify_events_supported(&path, mask);
  1033. if (ret)
  1034. goto path_put_and_out;
  1035. }
  1036. if (fid_mode) {
  1037. ret = fanotify_test_fid(&path, &__fsid);
  1038. if (ret)
  1039. goto path_put_and_out;
  1040. fsid = &__fsid;
  1041. }
  1042. /* inode held in place by reference to path; group by fget on fd */
  1043. if (mark_type == FAN_MARK_INODE)
  1044. inode = path.dentry->d_inode;
  1045. else
  1046. mnt = path.mnt;
  1047. /* Mask out FAN_EVENT_ON_CHILD flag for sb/mount/non-dir marks */
  1048. if (mnt || !S_ISDIR(inode->i_mode)) {
  1049. mask &= ~FAN_EVENT_ON_CHILD;
  1050. umask = FAN_EVENT_ON_CHILD;
  1051. /*
  1052. * If group needs to report parent fid, register for getting
  1053. * events with parent/name info for non-directory.
  1054. */
  1055. if ((fid_mode & FAN_REPORT_DIR_FID) &&
  1056. (flags & FAN_MARK_ADD) && !ignored)
  1057. mask |= FAN_EVENT_ON_CHILD;
  1058. }
  1059. /* create/update an inode mark */
  1060. switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
  1061. case FAN_MARK_ADD:
  1062. if (mark_type == FAN_MARK_MOUNT)
  1063. ret = fanotify_add_vfsmount_mark(group, mnt, mask,
  1064. flags, fsid);
  1065. else if (mark_type == FAN_MARK_FILESYSTEM)
  1066. ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
  1067. flags, fsid);
  1068. else
  1069. ret = fanotify_add_inode_mark(group, inode, mask,
  1070. flags, fsid);
  1071. break;
  1072. case FAN_MARK_REMOVE:
  1073. if (mark_type == FAN_MARK_MOUNT)
  1074. ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
  1075. flags, umask);
  1076. else if (mark_type == FAN_MARK_FILESYSTEM)
  1077. ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
  1078. flags, umask);
  1079. else
  1080. ret = fanotify_remove_inode_mark(group, inode, mask,
  1081. flags, umask);
  1082. break;
  1083. default:
  1084. ret = -EINVAL;
  1085. }
  1086. path_put_and_out:
  1087. path_put(&path);
  1088. fput_and_out:
  1089. fdput(f);
  1090. return ret;
  1091. }
  1092. #ifndef CONFIG_ARCH_SPLIT_ARG64
  1093. SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags,
  1094. __u64, mask, int, dfd,
  1095. const char __user *, pathname)
  1096. {
  1097. return do_fanotify_mark(fanotify_fd, flags, mask, dfd, pathname);
  1098. }
  1099. #endif
  1100. #if defined(CONFIG_ARCH_SPLIT_ARG64) || defined(CONFIG_COMPAT)
  1101. SYSCALL32_DEFINE6(fanotify_mark,
  1102. int, fanotify_fd, unsigned int, flags,
  1103. SC_ARG64(mask), int, dfd,
  1104. const char __user *, pathname)
  1105. {
  1106. return do_fanotify_mark(fanotify_fd, flags, SC_VAL64(__u64, mask),
  1107. dfd, pathname);
  1108. }
  1109. #endif
  1110. /*
  1111. * fanotify_user_setup - Our initialization function. Note that we cannot return
  1112. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  1113. * must result in panic().
  1114. */
  1115. static int __init fanotify_user_setup(void)
  1116. {
  1117. BUILD_BUG_ON(HWEIGHT32(FANOTIFY_INIT_FLAGS) != 10);
  1118. BUILD_BUG_ON(HWEIGHT32(FANOTIFY_MARK_FLAGS) != 9);
  1119. fanotify_mark_cache = KMEM_CACHE(fsnotify_mark,
  1120. SLAB_PANIC|SLAB_ACCOUNT);
  1121. fanotify_fid_event_cachep = KMEM_CACHE(fanotify_fid_event,
  1122. SLAB_PANIC);
  1123. fanotify_path_event_cachep = KMEM_CACHE(fanotify_path_event,
  1124. SLAB_PANIC);
  1125. if (IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS)) {
  1126. fanotify_perm_event_cachep =
  1127. KMEM_CACHE(fanotify_perm_event, SLAB_PANIC);
  1128. }
  1129. return 0;
  1130. }
  1131. device_initcall(fanotify_user_setup);