fanotify.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. extern struct kmem_cache *fanotify_mark_cache;
  7. extern struct kmem_cache *fanotify_fid_event_cachep;
  8. extern struct kmem_cache *fanotify_path_event_cachep;
  9. extern struct kmem_cache *fanotify_perm_event_cachep;
  10. /* Possible states of the permission event */
  11. enum {
  12. FAN_EVENT_INIT,
  13. FAN_EVENT_REPORTED,
  14. FAN_EVENT_ANSWERED,
  15. FAN_EVENT_CANCELED,
  16. };
  17. /*
  18. * 3 dwords are sufficient for most local fs (64bit ino, 32bit generation).
  19. * fh buf should be dword aligned. On 64bit arch, the ext_buf pointer is
  20. * stored in either the first or last 2 dwords.
  21. */
  22. #define FANOTIFY_INLINE_FH_LEN (3 << 2)
  23. #define FANOTIFY_FH_HDR_LEN offsetof(struct fanotify_fh, buf)
  24. /* Fixed size struct for file handle */
  25. struct fanotify_fh {
  26. u8 type;
  27. u8 len;
  28. #define FANOTIFY_FH_FLAG_EXT_BUF 1
  29. u8 flags;
  30. u8 pad;
  31. unsigned char buf[];
  32. } __aligned(4);
  33. /* Variable size struct for dir file handle + child file handle + name */
  34. struct fanotify_info {
  35. /* size of dir_fh/file_fh including fanotify_fh hdr size */
  36. u8 dir_fh_totlen;
  37. u8 file_fh_totlen;
  38. u8 name_len;
  39. u8 pad;
  40. unsigned char buf[];
  41. /*
  42. * (struct fanotify_fh) dir_fh starts at buf[0]
  43. * (optional) file_fh starts at buf[dir_fh_totlen]
  44. * name starts at buf[dir_fh_totlen + file_fh_totlen]
  45. */
  46. } __aligned(4);
  47. static inline bool fanotify_fh_has_ext_buf(struct fanotify_fh *fh)
  48. {
  49. return (fh->flags & FANOTIFY_FH_FLAG_EXT_BUF);
  50. }
  51. static inline char **fanotify_fh_ext_buf_ptr(struct fanotify_fh *fh)
  52. {
  53. BUILD_BUG_ON(FANOTIFY_FH_HDR_LEN % 4);
  54. BUILD_BUG_ON(__alignof__(char *) - 4 + sizeof(char *) >
  55. FANOTIFY_INLINE_FH_LEN);
  56. return (char **)ALIGN((unsigned long)(fh->buf), __alignof__(char *));
  57. }
  58. static inline void *fanotify_fh_ext_buf(struct fanotify_fh *fh)
  59. {
  60. return *fanotify_fh_ext_buf_ptr(fh);
  61. }
  62. static inline void *fanotify_fh_buf(struct fanotify_fh *fh)
  63. {
  64. return fanotify_fh_has_ext_buf(fh) ? fanotify_fh_ext_buf(fh) : fh->buf;
  65. }
  66. static inline int fanotify_info_dir_fh_len(struct fanotify_info *info)
  67. {
  68. if (!info->dir_fh_totlen ||
  69. WARN_ON_ONCE(info->dir_fh_totlen < FANOTIFY_FH_HDR_LEN))
  70. return 0;
  71. return info->dir_fh_totlen - FANOTIFY_FH_HDR_LEN;
  72. }
  73. static inline struct fanotify_fh *fanotify_info_dir_fh(struct fanotify_info *info)
  74. {
  75. BUILD_BUG_ON(offsetof(struct fanotify_info, buf) % 4);
  76. return (struct fanotify_fh *)info->buf;
  77. }
  78. static inline int fanotify_info_file_fh_len(struct fanotify_info *info)
  79. {
  80. if (!info->file_fh_totlen ||
  81. WARN_ON_ONCE(info->file_fh_totlen < FANOTIFY_FH_HDR_LEN))
  82. return 0;
  83. return info->file_fh_totlen - FANOTIFY_FH_HDR_LEN;
  84. }
  85. static inline struct fanotify_fh *fanotify_info_file_fh(struct fanotify_info *info)
  86. {
  87. return (struct fanotify_fh *)(info->buf + info->dir_fh_totlen);
  88. }
  89. static inline const char *fanotify_info_name(struct fanotify_info *info)
  90. {
  91. return info->buf + info->dir_fh_totlen + info->file_fh_totlen;
  92. }
  93. static inline void fanotify_info_init(struct fanotify_info *info)
  94. {
  95. info->dir_fh_totlen = 0;
  96. info->file_fh_totlen = 0;
  97. info->name_len = 0;
  98. }
  99. static inline void fanotify_info_copy_name(struct fanotify_info *info,
  100. const struct qstr *name)
  101. {
  102. info->name_len = name->len;
  103. strcpy(info->buf + info->dir_fh_totlen + info->file_fh_totlen,
  104. name->name);
  105. }
  106. /*
  107. * Common structure for fanotify events. Concrete structs are allocated in
  108. * fanotify_handle_event() and freed when the information is retrieved by
  109. * userspace. The type of event determines how it was allocated, how it will
  110. * be freed and which concrete struct it may be cast to.
  111. */
  112. enum fanotify_event_type {
  113. FANOTIFY_EVENT_TYPE_FID, /* fixed length */
  114. FANOTIFY_EVENT_TYPE_FID_NAME, /* variable length */
  115. FANOTIFY_EVENT_TYPE_PATH,
  116. FANOTIFY_EVENT_TYPE_PATH_PERM,
  117. FANOTIFY_EVENT_TYPE_OVERFLOW, /* struct fanotify_event */
  118. };
  119. struct fanotify_event {
  120. struct fsnotify_event fse;
  121. u32 mask;
  122. enum fanotify_event_type type;
  123. struct pid *pid;
  124. };
  125. static inline void fanotify_init_event(struct fanotify_event *event,
  126. unsigned long id, u32 mask)
  127. {
  128. fsnotify_init_event(&event->fse, id);
  129. event->mask = mask;
  130. event->pid = NULL;
  131. }
  132. struct fanotify_fid_event {
  133. struct fanotify_event fae;
  134. __kernel_fsid_t fsid;
  135. struct fanotify_fh object_fh;
  136. /* Reserve space in object_fh.buf[] - access with fanotify_fh_buf() */
  137. unsigned char _inline_fh_buf[FANOTIFY_INLINE_FH_LEN];
  138. };
  139. static inline struct fanotify_fid_event *
  140. FANOTIFY_FE(struct fanotify_event *event)
  141. {
  142. return container_of(event, struct fanotify_fid_event, fae);
  143. }
  144. struct fanotify_name_event {
  145. struct fanotify_event fae;
  146. __kernel_fsid_t fsid;
  147. struct fanotify_info info;
  148. };
  149. static inline struct fanotify_name_event *
  150. FANOTIFY_NE(struct fanotify_event *event)
  151. {
  152. return container_of(event, struct fanotify_name_event, fae);
  153. }
  154. static inline __kernel_fsid_t *fanotify_event_fsid(struct fanotify_event *event)
  155. {
  156. if (event->type == FANOTIFY_EVENT_TYPE_FID)
  157. return &FANOTIFY_FE(event)->fsid;
  158. else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
  159. return &FANOTIFY_NE(event)->fsid;
  160. else
  161. return NULL;
  162. }
  163. static inline struct fanotify_fh *fanotify_event_object_fh(
  164. struct fanotify_event *event)
  165. {
  166. if (event->type == FANOTIFY_EVENT_TYPE_FID)
  167. return &FANOTIFY_FE(event)->object_fh;
  168. else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
  169. return fanotify_info_file_fh(&FANOTIFY_NE(event)->info);
  170. else
  171. return NULL;
  172. }
  173. static inline struct fanotify_info *fanotify_event_info(
  174. struct fanotify_event *event)
  175. {
  176. if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
  177. return &FANOTIFY_NE(event)->info;
  178. else
  179. return NULL;
  180. }
  181. static inline int fanotify_event_object_fh_len(struct fanotify_event *event)
  182. {
  183. struct fanotify_info *info = fanotify_event_info(event);
  184. struct fanotify_fh *fh = fanotify_event_object_fh(event);
  185. if (info)
  186. return info->file_fh_totlen ? fh->len : 0;
  187. else
  188. return fh ? fh->len : 0;
  189. }
  190. static inline int fanotify_event_dir_fh_len(struct fanotify_event *event)
  191. {
  192. struct fanotify_info *info = fanotify_event_info(event);
  193. return info ? fanotify_info_dir_fh_len(info) : 0;
  194. }
  195. struct fanotify_path_event {
  196. struct fanotify_event fae;
  197. struct path path;
  198. };
  199. static inline struct fanotify_path_event *
  200. FANOTIFY_PE(struct fanotify_event *event)
  201. {
  202. return container_of(event, struct fanotify_path_event, fae);
  203. }
  204. /*
  205. * Structure for permission fanotify events. It gets allocated and freed in
  206. * fanotify_handle_event() since we wait there for user response. When the
  207. * information is retrieved by userspace the structure is moved from
  208. * group->notification_list to group->fanotify_data.access_list to wait for
  209. * user response.
  210. */
  211. struct fanotify_perm_event {
  212. struct fanotify_event fae;
  213. struct path path;
  214. unsigned short response; /* userspace answer to the event */
  215. unsigned short state; /* state of the event */
  216. int fd; /* fd we passed to userspace for this event */
  217. };
  218. static inline struct fanotify_perm_event *
  219. FANOTIFY_PERM(struct fanotify_event *event)
  220. {
  221. return container_of(event, struct fanotify_perm_event, fae);
  222. }
  223. static inline bool fanotify_is_perm_event(u32 mask)
  224. {
  225. return IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS) &&
  226. mask & FANOTIFY_PERM_EVENTS;
  227. }
  228. static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse)
  229. {
  230. return container_of(fse, struct fanotify_event, fse);
  231. }
  232. static inline bool fanotify_event_has_path(struct fanotify_event *event)
  233. {
  234. return event->type == FANOTIFY_EVENT_TYPE_PATH ||
  235. event->type == FANOTIFY_EVENT_TYPE_PATH_PERM;
  236. }
  237. static inline struct path *fanotify_event_path(struct fanotify_event *event)
  238. {
  239. if (event->type == FANOTIFY_EVENT_TYPE_PATH)
  240. return &FANOTIFY_PE(event)->path;
  241. else if (event->type == FANOTIFY_EVENT_TYPE_PATH_PERM)
  242. return &FANOTIFY_PERM(event)->path;
  243. else
  244. return NULL;
  245. }