fanotify.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/fsnotify_backend.h>
  3. #include <linux/path.h>
  4. #include <linux/slab.h>
  5. #include <linux/exportfs.h>
  6. #include <linux/hashtable.h>
  7. extern struct kmem_cache *fanotify_mark_cache;
  8. extern struct kmem_cache *fanotify_fid_event_cachep;
  9. extern struct kmem_cache *fanotify_path_event_cachep;
  10. extern struct kmem_cache *fanotify_perm_event_cachep;
  11. /* Possible states of the permission event */
  12. enum {
  13. FAN_EVENT_INIT,
  14. FAN_EVENT_REPORTED,
  15. FAN_EVENT_ANSWERED,
  16. FAN_EVENT_CANCELED,
  17. };
  18. /*
  19. * 3 dwords are sufficient for most local fs (64bit ino, 32bit generation).
  20. * fh buf should be dword aligned. On 64bit arch, the ext_buf pointer is
  21. * stored in either the first or last 2 dwords.
  22. */
  23. #define FANOTIFY_INLINE_FH_LEN (3 << 2)
  24. #define FANOTIFY_FH_HDR_LEN offsetof(struct fanotify_fh, buf)
  25. /* Fixed size struct for file handle */
  26. struct fanotify_fh {
  27. u8 type;
  28. u8 len;
  29. #define FANOTIFY_FH_FLAG_EXT_BUF 1
  30. u8 flags;
  31. u8 pad;
  32. unsigned char buf[];
  33. } __aligned(4);
  34. /* Variable size struct for dir file handle + child file handle + name */
  35. struct fanotify_info {
  36. /* size of dir_fh/file_fh including fanotify_fh hdr size */
  37. u8 dir_fh_totlen;
  38. u8 dir2_fh_totlen;
  39. u8 file_fh_totlen;
  40. u8 name_len;
  41. u8 name2_len;
  42. u8 pad[3];
  43. unsigned char buf[];
  44. /*
  45. * (struct fanotify_fh) dir_fh starts at buf[0]
  46. * (optional) dir2_fh starts at buf[dir_fh_totlen]
  47. * (optional) file_fh starts at buf[dir_fh_totlen + dir2_fh_totlen]
  48. * name starts at buf[dir_fh_totlen + dir2_fh_totlen + file_fh_totlen]
  49. * ...
  50. */
  51. #define FANOTIFY_DIR_FH_SIZE(info) ((info)->dir_fh_totlen)
  52. #define FANOTIFY_DIR2_FH_SIZE(info) ((info)->dir2_fh_totlen)
  53. #define FANOTIFY_FILE_FH_SIZE(info) ((info)->file_fh_totlen)
  54. #define FANOTIFY_NAME_SIZE(info) ((info)->name_len + 1)
  55. #define FANOTIFY_NAME2_SIZE(info) ((info)->name2_len + 1)
  56. #define FANOTIFY_DIR_FH_OFFSET(info) 0
  57. #define FANOTIFY_DIR2_FH_OFFSET(info) \
  58. (FANOTIFY_DIR_FH_OFFSET(info) + FANOTIFY_DIR_FH_SIZE(info))
  59. #define FANOTIFY_FILE_FH_OFFSET(info) \
  60. (FANOTIFY_DIR2_FH_OFFSET(info) + FANOTIFY_DIR2_FH_SIZE(info))
  61. #define FANOTIFY_NAME_OFFSET(info) \
  62. (FANOTIFY_FILE_FH_OFFSET(info) + FANOTIFY_FILE_FH_SIZE(info))
  63. #define FANOTIFY_NAME2_OFFSET(info) \
  64. (FANOTIFY_NAME_OFFSET(info) + FANOTIFY_NAME_SIZE(info))
  65. #define FANOTIFY_DIR_FH_BUF(info) \
  66. ((info)->buf + FANOTIFY_DIR_FH_OFFSET(info))
  67. #define FANOTIFY_DIR2_FH_BUF(info) \
  68. ((info)->buf + FANOTIFY_DIR2_FH_OFFSET(info))
  69. #define FANOTIFY_FILE_FH_BUF(info) \
  70. ((info)->buf + FANOTIFY_FILE_FH_OFFSET(info))
  71. #define FANOTIFY_NAME_BUF(info) \
  72. ((info)->buf + FANOTIFY_NAME_OFFSET(info))
  73. #define FANOTIFY_NAME2_BUF(info) \
  74. ((info)->buf + FANOTIFY_NAME2_OFFSET(info))
  75. } __aligned(4);
  76. static inline bool fanotify_fh_has_ext_buf(struct fanotify_fh *fh)
  77. {
  78. return (fh->flags & FANOTIFY_FH_FLAG_EXT_BUF);
  79. }
  80. static inline char **fanotify_fh_ext_buf_ptr(struct fanotify_fh *fh)
  81. {
  82. BUILD_BUG_ON(FANOTIFY_FH_HDR_LEN % 4);
  83. BUILD_BUG_ON(__alignof__(char *) - 4 + sizeof(char *) >
  84. FANOTIFY_INLINE_FH_LEN);
  85. return (char **)ALIGN((unsigned long)(fh->buf), __alignof__(char *));
  86. }
  87. static inline void *fanotify_fh_ext_buf(struct fanotify_fh *fh)
  88. {
  89. return *fanotify_fh_ext_buf_ptr(fh);
  90. }
  91. static inline void *fanotify_fh_buf(struct fanotify_fh *fh)
  92. {
  93. return fanotify_fh_has_ext_buf(fh) ? fanotify_fh_ext_buf(fh) : fh->buf;
  94. }
  95. static inline int fanotify_info_dir_fh_len(struct fanotify_info *info)
  96. {
  97. if (!info->dir_fh_totlen ||
  98. WARN_ON_ONCE(info->dir_fh_totlen < FANOTIFY_FH_HDR_LEN))
  99. return 0;
  100. return info->dir_fh_totlen - FANOTIFY_FH_HDR_LEN;
  101. }
  102. static inline struct fanotify_fh *fanotify_info_dir_fh(struct fanotify_info *info)
  103. {
  104. BUILD_BUG_ON(offsetof(struct fanotify_info, buf) % 4);
  105. return (struct fanotify_fh *)FANOTIFY_DIR_FH_BUF(info);
  106. }
  107. static inline int fanotify_info_dir2_fh_len(struct fanotify_info *info)
  108. {
  109. if (!info->dir2_fh_totlen ||
  110. WARN_ON_ONCE(info->dir2_fh_totlen < FANOTIFY_FH_HDR_LEN))
  111. return 0;
  112. return info->dir2_fh_totlen - FANOTIFY_FH_HDR_LEN;
  113. }
  114. static inline struct fanotify_fh *fanotify_info_dir2_fh(struct fanotify_info *info)
  115. {
  116. return (struct fanotify_fh *)FANOTIFY_DIR2_FH_BUF(info);
  117. }
  118. static inline int fanotify_info_file_fh_len(struct fanotify_info *info)
  119. {
  120. if (!info->file_fh_totlen ||
  121. WARN_ON_ONCE(info->file_fh_totlen < FANOTIFY_FH_HDR_LEN))
  122. return 0;
  123. return info->file_fh_totlen - FANOTIFY_FH_HDR_LEN;
  124. }
  125. static inline struct fanotify_fh *fanotify_info_file_fh(struct fanotify_info *info)
  126. {
  127. return (struct fanotify_fh *)FANOTIFY_FILE_FH_BUF(info);
  128. }
  129. static inline char *fanotify_info_name(struct fanotify_info *info)
  130. {
  131. if (!info->name_len)
  132. return NULL;
  133. return FANOTIFY_NAME_BUF(info);
  134. }
  135. static inline char *fanotify_info_name2(struct fanotify_info *info)
  136. {
  137. if (!info->name2_len)
  138. return NULL;
  139. return FANOTIFY_NAME2_BUF(info);
  140. }
  141. static inline void fanotify_info_init(struct fanotify_info *info)
  142. {
  143. BUILD_BUG_ON(FANOTIFY_FH_HDR_LEN + MAX_HANDLE_SZ > U8_MAX);
  144. BUILD_BUG_ON(NAME_MAX > U8_MAX);
  145. info->dir_fh_totlen = 0;
  146. info->dir2_fh_totlen = 0;
  147. info->file_fh_totlen = 0;
  148. info->name_len = 0;
  149. info->name2_len = 0;
  150. }
  151. /* These set/copy helpers MUST be called by order */
  152. static inline void fanotify_info_set_dir_fh(struct fanotify_info *info,
  153. unsigned int totlen)
  154. {
  155. if (WARN_ON_ONCE(info->dir2_fh_totlen > 0) ||
  156. WARN_ON_ONCE(info->file_fh_totlen > 0) ||
  157. WARN_ON_ONCE(info->name_len > 0) ||
  158. WARN_ON_ONCE(info->name2_len > 0))
  159. return;
  160. info->dir_fh_totlen = totlen;
  161. }
  162. static inline void fanotify_info_set_dir2_fh(struct fanotify_info *info,
  163. unsigned int totlen)
  164. {
  165. if (WARN_ON_ONCE(info->file_fh_totlen > 0) ||
  166. WARN_ON_ONCE(info->name_len > 0) ||
  167. WARN_ON_ONCE(info->name2_len > 0))
  168. return;
  169. info->dir2_fh_totlen = totlen;
  170. }
  171. static inline void fanotify_info_set_file_fh(struct fanotify_info *info,
  172. unsigned int totlen)
  173. {
  174. if (WARN_ON_ONCE(info->name_len > 0) ||
  175. WARN_ON_ONCE(info->name2_len > 0))
  176. return;
  177. info->file_fh_totlen = totlen;
  178. }
  179. static inline void fanotify_info_copy_name(struct fanotify_info *info,
  180. const struct qstr *name)
  181. {
  182. if (WARN_ON_ONCE(name->len > NAME_MAX) ||
  183. WARN_ON_ONCE(info->name2_len > 0))
  184. return;
  185. info->name_len = name->len;
  186. strcpy(fanotify_info_name(info), name->name);
  187. }
  188. static inline void fanotify_info_copy_name2(struct fanotify_info *info,
  189. const struct qstr *name)
  190. {
  191. if (WARN_ON_ONCE(name->len > NAME_MAX))
  192. return;
  193. info->name2_len = name->len;
  194. strcpy(fanotify_info_name2(info), name->name);
  195. }
  196. /*
  197. * Common structure for fanotify events. Concrete structs are allocated in
  198. * fanotify_handle_event() and freed when the information is retrieved by
  199. * userspace. The type of event determines how it was allocated, how it will
  200. * be freed and which concrete struct it may be cast to.
  201. */
  202. enum fanotify_event_type {
  203. FANOTIFY_EVENT_TYPE_FID, /* fixed length */
  204. FANOTIFY_EVENT_TYPE_FID_NAME, /* variable length */
  205. FANOTIFY_EVENT_TYPE_PATH,
  206. FANOTIFY_EVENT_TYPE_PATH_PERM,
  207. FANOTIFY_EVENT_TYPE_OVERFLOW, /* struct fanotify_event */
  208. FANOTIFY_EVENT_TYPE_FS_ERROR, /* struct fanotify_error_event */
  209. __FANOTIFY_EVENT_TYPE_NUM
  210. };
  211. #define FANOTIFY_EVENT_TYPE_BITS \
  212. (ilog2(__FANOTIFY_EVENT_TYPE_NUM - 1) + 1)
  213. #define FANOTIFY_EVENT_HASH_BITS \
  214. (32 - FANOTIFY_EVENT_TYPE_BITS)
  215. struct fanotify_event {
  216. struct fsnotify_event fse;
  217. struct hlist_node merge_list; /* List for hashed merge */
  218. u32 mask;
  219. struct {
  220. unsigned int type : FANOTIFY_EVENT_TYPE_BITS;
  221. unsigned int hash : FANOTIFY_EVENT_HASH_BITS;
  222. };
  223. struct pid *pid;
  224. };
  225. static inline void fanotify_init_event(struct fanotify_event *event,
  226. unsigned int hash, u32 mask)
  227. {
  228. fsnotify_init_event(&event->fse);
  229. INIT_HLIST_NODE(&event->merge_list);
  230. event->hash = hash;
  231. event->mask = mask;
  232. event->pid = NULL;
  233. }
  234. #define FANOTIFY_INLINE_FH(name, size) \
  235. struct { \
  236. struct fanotify_fh (name); \
  237. /* Space for object_fh.buf[] - access with fanotify_fh_buf() */ \
  238. unsigned char _inline_fh_buf[(size)]; \
  239. }
  240. struct fanotify_fid_event {
  241. struct fanotify_event fae;
  242. __kernel_fsid_t fsid;
  243. FANOTIFY_INLINE_FH(object_fh, FANOTIFY_INLINE_FH_LEN);
  244. };
  245. static inline struct fanotify_fid_event *
  246. FANOTIFY_FE(struct fanotify_event *event)
  247. {
  248. return container_of(event, struct fanotify_fid_event, fae);
  249. }
  250. struct fanotify_name_event {
  251. struct fanotify_event fae;
  252. __kernel_fsid_t fsid;
  253. struct fanotify_info info;
  254. };
  255. static inline struct fanotify_name_event *
  256. FANOTIFY_NE(struct fanotify_event *event)
  257. {
  258. return container_of(event, struct fanotify_name_event, fae);
  259. }
  260. struct fanotify_error_event {
  261. struct fanotify_event fae;
  262. s32 error; /* Error reported by the Filesystem. */
  263. u32 err_count; /* Suppressed errors count */
  264. __kernel_fsid_t fsid; /* FSID this error refers to. */
  265. FANOTIFY_INLINE_FH(object_fh, MAX_HANDLE_SZ);
  266. };
  267. static inline struct fanotify_error_event *
  268. FANOTIFY_EE(struct fanotify_event *event)
  269. {
  270. return container_of(event, struct fanotify_error_event, fae);
  271. }
  272. static inline __kernel_fsid_t *fanotify_event_fsid(struct fanotify_event *event)
  273. {
  274. if (event->type == FANOTIFY_EVENT_TYPE_FID)
  275. return &FANOTIFY_FE(event)->fsid;
  276. else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
  277. return &FANOTIFY_NE(event)->fsid;
  278. else if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR)
  279. return &FANOTIFY_EE(event)->fsid;
  280. else
  281. return NULL;
  282. }
  283. static inline struct fanotify_fh *fanotify_event_object_fh(
  284. struct fanotify_event *event)
  285. {
  286. if (event->type == FANOTIFY_EVENT_TYPE_FID)
  287. return &FANOTIFY_FE(event)->object_fh;
  288. else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
  289. return fanotify_info_file_fh(&FANOTIFY_NE(event)->info);
  290. else if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR)
  291. return &FANOTIFY_EE(event)->object_fh;
  292. else
  293. return NULL;
  294. }
  295. static inline struct fanotify_info *fanotify_event_info(
  296. struct fanotify_event *event)
  297. {
  298. if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
  299. return &FANOTIFY_NE(event)->info;
  300. else
  301. return NULL;
  302. }
  303. static inline int fanotify_event_object_fh_len(struct fanotify_event *event)
  304. {
  305. struct fanotify_info *info = fanotify_event_info(event);
  306. struct fanotify_fh *fh = fanotify_event_object_fh(event);
  307. if (info)
  308. return info->file_fh_totlen ? fh->len : 0;
  309. else
  310. return fh ? fh->len : 0;
  311. }
  312. static inline int fanotify_event_dir_fh_len(struct fanotify_event *event)
  313. {
  314. struct fanotify_info *info = fanotify_event_info(event);
  315. return info ? fanotify_info_dir_fh_len(info) : 0;
  316. }
  317. static inline int fanotify_event_dir2_fh_len(struct fanotify_event *event)
  318. {
  319. struct fanotify_info *info = fanotify_event_info(event);
  320. return info ? fanotify_info_dir2_fh_len(info) : 0;
  321. }
  322. static inline bool fanotify_event_has_object_fh(struct fanotify_event *event)
  323. {
  324. /* For error events, even zeroed fh are reported. */
  325. if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR)
  326. return true;
  327. return fanotify_event_object_fh_len(event) > 0;
  328. }
  329. static inline bool fanotify_event_has_dir_fh(struct fanotify_event *event)
  330. {
  331. return fanotify_event_dir_fh_len(event) > 0;
  332. }
  333. static inline bool fanotify_event_has_dir2_fh(struct fanotify_event *event)
  334. {
  335. return fanotify_event_dir2_fh_len(event) > 0;
  336. }
  337. static inline bool fanotify_event_has_any_dir_fh(struct fanotify_event *event)
  338. {
  339. return fanotify_event_has_dir_fh(event) ||
  340. fanotify_event_has_dir2_fh(event);
  341. }
  342. struct fanotify_path_event {
  343. struct fanotify_event fae;
  344. struct path path;
  345. };
  346. static inline struct fanotify_path_event *
  347. FANOTIFY_PE(struct fanotify_event *event)
  348. {
  349. return container_of(event, struct fanotify_path_event, fae);
  350. }
  351. /*
  352. * Structure for permission fanotify events. It gets allocated and freed in
  353. * fanotify_handle_event() since we wait there for user response. When the
  354. * information is retrieved by userspace the structure is moved from
  355. * group->notification_list to group->fanotify_data.access_list to wait for
  356. * user response.
  357. */
  358. struct fanotify_perm_event {
  359. struct fanotify_event fae;
  360. struct path path;
  361. unsigned short response; /* userspace answer to the event */
  362. unsigned short state; /* state of the event */
  363. int fd; /* fd we passed to userspace for this event */
  364. };
  365. static inline struct fanotify_perm_event *
  366. FANOTIFY_PERM(struct fanotify_event *event)
  367. {
  368. return container_of(event, struct fanotify_perm_event, fae);
  369. }
  370. static inline bool fanotify_is_perm_event(u32 mask)
  371. {
  372. return IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS) &&
  373. mask & FANOTIFY_PERM_EVENTS;
  374. }
  375. static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse)
  376. {
  377. return container_of(fse, struct fanotify_event, fse);
  378. }
  379. static inline bool fanotify_is_error_event(u32 mask)
  380. {
  381. return mask & FAN_FS_ERROR;
  382. }
  383. static inline const struct path *fanotify_event_path(struct fanotify_event *event)
  384. {
  385. if (event->type == FANOTIFY_EVENT_TYPE_PATH)
  386. return &FANOTIFY_PE(event)->path;
  387. else if (event->type == FANOTIFY_EVENT_TYPE_PATH_PERM)
  388. return &FANOTIFY_PERM(event)->path;
  389. else
  390. return NULL;
  391. }
  392. /*
  393. * Use 128 size hash table to speed up events merge.
  394. */
  395. #define FANOTIFY_HTABLE_BITS (7)
  396. #define FANOTIFY_HTABLE_SIZE (1 << FANOTIFY_HTABLE_BITS)
  397. #define FANOTIFY_HTABLE_MASK (FANOTIFY_HTABLE_SIZE - 1)
  398. /*
  399. * Permission events and overflow event do not get merged - don't hash them.
  400. */
  401. static inline bool fanotify_is_hashed_event(u32 mask)
  402. {
  403. return !(fanotify_is_perm_event(mask) ||
  404. fsnotify_is_overflow_event(mask));
  405. }
  406. static inline unsigned int fanotify_event_hash_bucket(
  407. struct fsnotify_group *group,
  408. struct fanotify_event *event)
  409. {
  410. return event->hash & FANOTIFY_HTABLE_MASK;
  411. }
  412. static inline unsigned int fanotify_mark_user_flags(struct fsnotify_mark *mark)
  413. {
  414. unsigned int mflags = 0;
  415. if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
  416. mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
  417. if (mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF)
  418. mflags |= FAN_MARK_EVICTABLE;
  419. if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
  420. mflags |= FAN_MARK_IGNORE;
  421. return mflags;
  422. }