1
0

fanotify.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. #ifndef _UAPI_LINUX_FANOTIFY_H
  3. #define _UAPI_LINUX_FANOTIFY_H
  4. #include <linux/types.h>
  5. /* the following events that user-space can register for */
  6. #define FAN_ACCESS 0x00000001 /* File was accessed */
  7. #define FAN_MODIFY 0x00000002 /* File was modified */
  8. #define FAN_ATTRIB 0x00000004 /* Metadata changed */
  9. #define FAN_CLOSE_WRITE 0x00000008 /* Writable file closed */
  10. #define FAN_CLOSE_NOWRITE 0x00000010 /* Unwritable file closed */
  11. #define FAN_OPEN 0x00000020 /* File was opened */
  12. #define FAN_MOVED_FROM 0x00000040 /* File was moved from X */
  13. #define FAN_MOVED_TO 0x00000080 /* File was moved to Y */
  14. #define FAN_CREATE 0x00000100 /* Subfile was created */
  15. #define FAN_DELETE 0x00000200 /* Subfile was deleted */
  16. #define FAN_DELETE_SELF 0x00000400 /* Self was deleted */
  17. #define FAN_MOVE_SELF 0x00000800 /* Self was moved */
  18. #define FAN_OPEN_EXEC 0x00001000 /* File was opened for exec */
  19. #define FAN_Q_OVERFLOW 0x00004000 /* Event queued overflowed */
  20. #define FAN_FS_ERROR 0x00008000 /* Filesystem error */
  21. #define FAN_OPEN_PERM 0x00010000 /* File open in perm check */
  22. #define FAN_ACCESS_PERM 0x00020000 /* File accessed in perm check */
  23. #define FAN_OPEN_EXEC_PERM 0x00040000 /* File open/exec in perm check */
  24. /* #define FAN_DIR_MODIFY 0x00080000 */ /* Deprecated (reserved) */
  25. #define FAN_PRE_ACCESS 0x00100000 /* Pre-content access hook */
  26. #define FAN_MNT_ATTACH 0x01000000 /* Mount was attached */
  27. #define FAN_MNT_DETACH 0x02000000 /* Mount was detached */
  28. #define FAN_EVENT_ON_CHILD 0x08000000 /* Interested in child events */
  29. #define FAN_RENAME 0x10000000 /* File was renamed */
  30. #define FAN_ONDIR 0x40000000 /* Event occurred against dir */
  31. /* helper events */
  32. #define FAN_CLOSE (FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE) /* close */
  33. #define FAN_MOVE (FAN_MOVED_FROM | FAN_MOVED_TO) /* moves */
  34. /* flags used for fanotify_init() */
  35. #define FAN_CLOEXEC 0x00000001
  36. #define FAN_NONBLOCK 0x00000002
  37. /* These are NOT bitwise flags. Both bits are used together. */
  38. #define FAN_CLASS_NOTIF 0x00000000
  39. #define FAN_CLASS_CONTENT 0x00000004
  40. #define FAN_CLASS_PRE_CONTENT 0x00000008
  41. /* Deprecated - do not use this in programs and do not add new flags here! */
  42. #define FAN_ALL_CLASS_BITS (FAN_CLASS_NOTIF | FAN_CLASS_CONTENT | \
  43. FAN_CLASS_PRE_CONTENT)
  44. #define FAN_UNLIMITED_QUEUE 0x00000010
  45. #define FAN_UNLIMITED_MARKS 0x00000020
  46. #define FAN_ENABLE_AUDIT 0x00000040
  47. /* Flags to determine fanotify event format */
  48. #define FAN_REPORT_PIDFD 0x00000080 /* Report pidfd for event->pid */
  49. #define FAN_REPORT_TID 0x00000100 /* event->pid is thread id */
  50. #define FAN_REPORT_FID 0x00000200 /* Report unique file id */
  51. #define FAN_REPORT_DIR_FID 0x00000400 /* Report unique directory id */
  52. #define FAN_REPORT_NAME 0x00000800 /* Report events with name */
  53. #define FAN_REPORT_TARGET_FID 0x00001000 /* Report dirent target id */
  54. #define FAN_REPORT_FD_ERROR 0x00002000 /* event->fd can report error */
  55. #define FAN_REPORT_MNT 0x00004000 /* Report mount events */
  56. /* Convenience macro - FAN_REPORT_NAME requires FAN_REPORT_DIR_FID */
  57. #define FAN_REPORT_DFID_NAME (FAN_REPORT_DIR_FID | FAN_REPORT_NAME)
  58. /* Convenience macro - FAN_REPORT_TARGET_FID requires all other FID flags */
  59. #define FAN_REPORT_DFID_NAME_TARGET (FAN_REPORT_DFID_NAME | \
  60. FAN_REPORT_FID | FAN_REPORT_TARGET_FID)
  61. /* Deprecated - do not use this in programs and do not add new flags here! */
  62. #define FAN_ALL_INIT_FLAGS (FAN_CLOEXEC | FAN_NONBLOCK | \
  63. FAN_ALL_CLASS_BITS | FAN_UNLIMITED_QUEUE |\
  64. FAN_UNLIMITED_MARKS)
  65. /* flags used for fanotify_modify_mark() */
  66. #define FAN_MARK_ADD 0x00000001
  67. #define FAN_MARK_REMOVE 0x00000002
  68. #define FAN_MARK_DONT_FOLLOW 0x00000004
  69. #define FAN_MARK_ONLYDIR 0x00000008
  70. /* FAN_MARK_MOUNT is 0x00000010 */
  71. #define FAN_MARK_IGNORED_MASK 0x00000020
  72. #define FAN_MARK_IGNORED_SURV_MODIFY 0x00000040
  73. #define FAN_MARK_FLUSH 0x00000080
  74. /* FAN_MARK_FILESYSTEM is 0x00000100 */
  75. #define FAN_MARK_EVICTABLE 0x00000200
  76. /* This bit is mutually exclusive with FAN_MARK_IGNORED_MASK bit */
  77. #define FAN_MARK_IGNORE 0x00000400
  78. /* These are NOT bitwise flags. Both bits can be used togther. */
  79. #define FAN_MARK_INODE 0x00000000
  80. #define FAN_MARK_MOUNT 0x00000010
  81. #define FAN_MARK_FILESYSTEM 0x00000100
  82. #define FAN_MARK_MNTNS 0x00000110
  83. /*
  84. * Convenience macro - FAN_MARK_IGNORE requires FAN_MARK_IGNORED_SURV_MODIFY
  85. * for non-inode mark types.
  86. */
  87. #define FAN_MARK_IGNORE_SURV (FAN_MARK_IGNORE | FAN_MARK_IGNORED_SURV_MODIFY)
  88. /* Deprecated - do not use this in programs and do not add new flags here! */
  89. #define FAN_ALL_MARK_FLAGS (FAN_MARK_ADD |\
  90. FAN_MARK_REMOVE |\
  91. FAN_MARK_DONT_FOLLOW |\
  92. FAN_MARK_ONLYDIR |\
  93. FAN_MARK_MOUNT |\
  94. FAN_MARK_IGNORED_MASK |\
  95. FAN_MARK_IGNORED_SURV_MODIFY |\
  96. FAN_MARK_FLUSH)
  97. /* Deprecated - do not use this in programs and do not add new flags here! */
  98. #define FAN_ALL_EVENTS (FAN_ACCESS |\
  99. FAN_MODIFY |\
  100. FAN_CLOSE |\
  101. FAN_OPEN)
  102. /*
  103. * All events which require a permission response from userspace
  104. */
  105. /* Deprecated - do not use this in programs and do not add new flags here! */
  106. #define FAN_ALL_PERM_EVENTS (FAN_OPEN_PERM |\
  107. FAN_ACCESS_PERM)
  108. /* Deprecated - do not use this in programs and do not add new flags here! */
  109. #define FAN_ALL_OUTGOING_EVENTS (FAN_ALL_EVENTS |\
  110. FAN_ALL_PERM_EVENTS |\
  111. FAN_Q_OVERFLOW)
  112. #define FANOTIFY_METADATA_VERSION 3
  113. struct fanotify_event_metadata {
  114. __u32 event_len;
  115. __u8 vers;
  116. __u8 reserved;
  117. __u16 metadata_len;
  118. __aligned_u64 mask;
  119. __s32 fd;
  120. __s32 pid;
  121. };
  122. #define FAN_EVENT_INFO_TYPE_FID 1
  123. #define FAN_EVENT_INFO_TYPE_DFID_NAME 2
  124. #define FAN_EVENT_INFO_TYPE_DFID 3
  125. #define FAN_EVENT_INFO_TYPE_PIDFD 4
  126. #define FAN_EVENT_INFO_TYPE_ERROR 5
  127. #define FAN_EVENT_INFO_TYPE_RANGE 6
  128. #define FAN_EVENT_INFO_TYPE_MNT 7
  129. /* Special info types for FAN_RENAME */
  130. #define FAN_EVENT_INFO_TYPE_OLD_DFID_NAME 10
  131. /* Reserved for FAN_EVENT_INFO_TYPE_OLD_DFID 11 */
  132. #define FAN_EVENT_INFO_TYPE_NEW_DFID_NAME 12
  133. /* Reserved for FAN_EVENT_INFO_TYPE_NEW_DFID 13 */
  134. /* Variable length info record following event metadata */
  135. struct fanotify_event_info_header {
  136. __u8 info_type;
  137. __u8 pad;
  138. __u16 len;
  139. };
  140. /*
  141. * Unique file identifier info record.
  142. * This structure is used for records of types FAN_EVENT_INFO_TYPE_FID,
  143. * FAN_EVENT_INFO_TYPE_DFID and FAN_EVENT_INFO_TYPE_DFID_NAME.
  144. * For FAN_EVENT_INFO_TYPE_DFID_NAME there is additionally a null terminated
  145. * name immediately after the file handle.
  146. */
  147. struct fanotify_event_info_fid {
  148. struct fanotify_event_info_header hdr;
  149. __kernel_fsid_t fsid;
  150. /*
  151. * Following is an opaque struct file_handle that can be passed as
  152. * an argument to open_by_handle_at(2).
  153. */
  154. unsigned char handle[];
  155. };
  156. /*
  157. * This structure is used for info records of type FAN_EVENT_INFO_TYPE_PIDFD.
  158. * It holds a pidfd for the pid that was responsible for generating an event.
  159. */
  160. struct fanotify_event_info_pidfd {
  161. struct fanotify_event_info_header hdr;
  162. __s32 pidfd;
  163. };
  164. struct fanotify_event_info_error {
  165. struct fanotify_event_info_header hdr;
  166. __s32 error;
  167. __u32 error_count;
  168. };
  169. struct fanotify_event_info_range {
  170. struct fanotify_event_info_header hdr;
  171. __u32 pad;
  172. __u64 offset;
  173. __u64 count;
  174. };
  175. struct fanotify_event_info_mnt {
  176. struct fanotify_event_info_header hdr;
  177. __u64 mnt_id;
  178. };
  179. /*
  180. * User space may need to record additional information about its decision.
  181. * The extra information type records what kind of information is included.
  182. * The default is none. We also define an extra information buffer whose
  183. * size is determined by the extra information type.
  184. *
  185. * If the information type is Audit Rule, then the information following
  186. * is the rule number that triggered the user space decision that
  187. * requires auditing.
  188. */
  189. #define FAN_RESPONSE_INFO_NONE 0
  190. #define FAN_RESPONSE_INFO_AUDIT_RULE 1
  191. struct fanotify_response {
  192. __s32 fd;
  193. __u32 response;
  194. };
  195. struct fanotify_response_info_header {
  196. __u8 type;
  197. __u8 pad;
  198. __u16 len;
  199. };
  200. struct fanotify_response_info_audit_rule {
  201. struct fanotify_response_info_header hdr;
  202. __u32 rule_number;
  203. __u32 subj_trust;
  204. __u32 obj_trust;
  205. };
  206. /* Legit userspace responses to a _PERM event */
  207. #define FAN_ALLOW 0x01
  208. #define FAN_DENY 0x02
  209. /* errno other than EPERM can specified in upper byte of deny response */
  210. #define FAN_ERRNO_BITS 8
  211. #define FAN_ERRNO_SHIFT (32 - FAN_ERRNO_BITS)
  212. #define FAN_ERRNO_MASK ((1 << FAN_ERRNO_BITS) - 1)
  213. #define FAN_DENY_ERRNO(err) \
  214. (FAN_DENY | ((((__u32)(err)) & FAN_ERRNO_MASK) << FAN_ERRNO_SHIFT))
  215. #define FAN_AUDIT 0x10 /* Bitmask to create audit record for result */
  216. #define FAN_INFO 0x20 /* Bitmask to indicate additional information */
  217. /* No fd set in event */
  218. #define FAN_NOFD -1
  219. #define FAN_NOPIDFD FAN_NOFD
  220. #define FAN_EPIDFD -2
  221. /* Helper functions to deal with fanotify_event_metadata buffers */
  222. #define FAN_EVENT_METADATA_LEN (sizeof(struct fanotify_event_metadata))
  223. #define FAN_EVENT_NEXT(meta, len) ((len) -= (meta)->event_len, \
  224. (struct fanotify_event_metadata*)(((char *)(meta)) + \
  225. (meta)->event_len))
  226. #define FAN_EVENT_OK(meta, len) ((long)(len) >= (long)FAN_EVENT_METADATA_LEN && \
  227. (long)(meta)->event_len >= (long)FAN_EVENT_METADATA_LEN && \
  228. (long)(meta)->event_len <= (long)(len))
  229. #endif /* _UAPI_LINUX_FANOTIFY_H */