1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 #ifndef _UAPI_LINUX_FANOTIFY_H
3 #define _UAPI_LINUX_FANOTIFY_H
4 
5 #include <linux/types.h>
6 
7 /* the following events that user-space can register for */
8 #define FAN_ACCESS		0x00000001	/* File was accessed */
9 #define FAN_MODIFY		0x00000002	/* File was modified */
10 #define FAN_ATTRIB		0x00000004	/* Metadata changed */
11 #define FAN_CLOSE_WRITE		0x00000008	/* Writtable file closed */
12 #define FAN_CLOSE_NOWRITE	0x00000010	/* Unwrittable file closed */
13 #define FAN_OPEN		0x00000020	/* File was opened */
14 #define FAN_MOVED_FROM		0x00000040	/* File was moved from X */
15 #define FAN_MOVED_TO		0x00000080	/* File was moved to Y */
16 #define FAN_CREATE		0x00000100	/* Subfile was created */
17 #define FAN_DELETE		0x00000200	/* Subfile was deleted */
18 #define FAN_DELETE_SELF		0x00000400	/* Self was deleted */
19 #define FAN_MOVE_SELF		0x00000800	/* Self was moved */
20 #define FAN_OPEN_EXEC		0x00001000	/* File was opened for exec */
21 
22 #define FAN_Q_OVERFLOW		0x00004000	/* Event queued overflowed */
23 #define FAN_FS_ERROR		0x00008000	/* Filesystem error */
24 
25 #define FAN_OPEN_PERM		0x00010000	/* File open in perm check */
26 #define FAN_ACCESS_PERM		0x00020000	/* File accessed in perm check */
27 #define FAN_OPEN_EXEC_PERM	0x00040000	/* File open/exec in perm check */
28 
29 #define FAN_EVENT_ON_CHILD	0x08000000	/* Interested in child events */
30 
31 #define FAN_ONDIR		0x40000000	/* Event occurred against dir */
32 
33 /* helper events */
34 #define FAN_CLOSE		(FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE) /* close */
35 #define FAN_MOVE		(FAN_MOVED_FROM | FAN_MOVED_TO) /* moves */
36 
37 /* flags used for fanotify_init() */
38 #define FAN_CLOEXEC		0x00000001
39 #define FAN_NONBLOCK		0x00000002
40 
41 /* These are NOT bitwise flags.  Both bits are used together.  */
42 #define FAN_CLASS_NOTIF		0x00000000
43 #define FAN_CLASS_CONTENT	0x00000004
44 #define FAN_CLASS_PRE_CONTENT	0x00000008
45 
46 /* Deprecated - do not use this in programs and do not add new flags here! */
47 #define FAN_ALL_CLASS_BITS	(FAN_CLASS_NOTIF | FAN_CLASS_CONTENT | \
48 				 FAN_CLASS_PRE_CONTENT)
49 
50 #define FAN_UNLIMITED_QUEUE	0x00000010
51 #define FAN_UNLIMITED_MARKS	0x00000020
52 #define FAN_ENABLE_AUDIT	0x00000040
53 
54 /* Flags to determine fanotify event format */
55 #define FAN_REPORT_PIDFD	0x00000080	/* Report pidfd for event->pid */
56 #define FAN_REPORT_TID		0x00000100	/* event->pid is thread id */
57 #define FAN_REPORT_FID		0x00000200	/* Report unique file id */
58 #define FAN_REPORT_DIR_FID	0x00000400	/* Report unique directory id */
59 #define FAN_REPORT_NAME		0x00000800	/* Report events with name */
60 
61 /* Convenience macro - FAN_REPORT_NAME requires FAN_REPORT_DIR_FID */
62 #define FAN_REPORT_DFID_NAME	(FAN_REPORT_DIR_FID | FAN_REPORT_NAME)
63 
64 /* Deprecated - do not use this in programs and do not add new flags here! */
65 #define FAN_ALL_INIT_FLAGS	(FAN_CLOEXEC | FAN_NONBLOCK | \
66 				 FAN_ALL_CLASS_BITS | FAN_UNLIMITED_QUEUE |\
67 				 FAN_UNLIMITED_MARKS)
68 
69 /* flags used for fanotify_modify_mark() */
70 #define FAN_MARK_ADD		0x00000001
71 #define FAN_MARK_REMOVE		0x00000002
72 #define FAN_MARK_DONT_FOLLOW	0x00000004
73 #define FAN_MARK_ONLYDIR	0x00000008
74 /* FAN_MARK_MOUNT is		0x00000010 */
75 #define FAN_MARK_IGNORED_MASK	0x00000020
76 #define FAN_MARK_IGNORED_SURV_MODIFY	0x00000040
77 #define FAN_MARK_FLUSH		0x00000080
78 /* FAN_MARK_FILESYSTEM is	0x00000100 */
79 
80 /* These are NOT bitwise flags.  Both bits can be used togther.  */
81 #define FAN_MARK_INODE		0x00000000
82 #define FAN_MARK_MOUNT		0x00000010
83 #define FAN_MARK_FILESYSTEM	0x00000100
84 
85 /* Deprecated - do not use this in programs and do not add new flags here! */
86 #define FAN_ALL_MARK_FLAGS	(FAN_MARK_ADD |\
87 				 FAN_MARK_REMOVE |\
88 				 FAN_MARK_DONT_FOLLOW |\
89 				 FAN_MARK_ONLYDIR |\
90 				 FAN_MARK_MOUNT |\
91 				 FAN_MARK_IGNORED_MASK |\
92 				 FAN_MARK_IGNORED_SURV_MODIFY |\
93 				 FAN_MARK_FLUSH)
94 
95 /* Deprecated - do not use this in programs and do not add new flags here! */
96 #define FAN_ALL_EVENTS (FAN_ACCESS |\
97 			FAN_MODIFY |\
98 			FAN_CLOSE |\
99 			FAN_OPEN)
100 
101 /*
102  * All events which require a permission response from userspace
103  */
104 /* Deprecated - do not use this in programs and do not add new flags here! */
105 #define FAN_ALL_PERM_EVENTS (FAN_OPEN_PERM |\
106 			     FAN_ACCESS_PERM)
107 
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 
113 #define FANOTIFY_METADATA_VERSION	3
114 
115 struct fanotify_event_metadata {
116 	__u32 event_len;
117 	__u8 vers;
118 	__u8 reserved;
119 	__u16 metadata_len;
120 	__aligned_u64 mask;
121 	__s32 fd;
122 	__s32 pid;
123 };
124 
125 #define FAN_EVENT_INFO_TYPE_FID		1
126 #define FAN_EVENT_INFO_TYPE_DFID_NAME	2
127 #define FAN_EVENT_INFO_TYPE_DFID	3
128 #define FAN_EVENT_INFO_TYPE_PIDFD	4
129 #define FAN_EVENT_INFO_TYPE_ERROR	5
130 
131 /* Variable length info record following event metadata */
132 struct fanotify_event_info_header {
133 	__u8 info_type;
134 	__u8 pad;
135 	__u16 len;
136 };
137 
138 /*
139  * Unique file identifier info record.
140  * This structure is used for records of types FAN_EVENT_INFO_TYPE_FID,
141  * FAN_EVENT_INFO_TYPE_DFID and FAN_EVENT_INFO_TYPE_DFID_NAME.
142  * For FAN_EVENT_INFO_TYPE_DFID_NAME there is additionally a null terminated
143  * name immediately after the file handle.
144  */
145 struct fanotify_event_info_fid {
146 	struct fanotify_event_info_header hdr;
147 	__kernel_fsid_t fsid;
148 	/*
149 	 * Following is an opaque struct file_handle that can be passed as
150 	 * an argument to open_by_handle_at(2).
151 	 */
152 	unsigned char handle[0];
153 };
154 
155 /*
156  * This structure is used for info records of type FAN_EVENT_INFO_TYPE_PIDFD.
157  * It holds a pidfd for the pid that was responsible for generating an event.
158  */
159 struct fanotify_event_info_pidfd {
160 	struct fanotify_event_info_header hdr;
161 	__s32 pidfd;
162 };
163 
164 struct fanotify_event_info_error {
165 	struct fanotify_event_info_header hdr;
166 	__s32 error;
167 	__u32 error_count;
168 };
169 
170 struct fanotify_response {
171 	__s32 fd;
172 	__u32 response;
173 };
174 
175 /* Legit userspace responses to a _PERM event */
176 #define FAN_ALLOW	0x01
177 #define FAN_DENY	0x02
178 #define FAN_AUDIT	0x10	/* Bit mask to create audit record for result */
179 
180 /* No fd set in event */
181 #define FAN_NOFD	-1
182 #define FAN_NOPIDFD	FAN_NOFD
183 #define FAN_EPIDFD	-2
184 
185 /* Helper functions to deal with fanotify_event_metadata buffers */
186 #define FAN_EVENT_METADATA_LEN (sizeof(struct fanotify_event_metadata))
187 
188 #define FAN_EVENT_NEXT(meta, len) ((len) -= (meta)->event_len, \
189 				   (struct fanotify_event_metadata*)(((char *)(meta)) + \
190 				   (meta)->event_len))
191 
192 #define FAN_EVENT_OK(meta, len)	((long)(len) >= (long)FAN_EVENT_METADATA_LEN && \
193 				(long)(meta)->event_len >= (long)FAN_EVENT_METADATA_LEN && \
194 				(long)(meta)->event_len <= (long)(len))
195 
196 #endif /* _UAPI_LINUX_FANOTIFY_H */
197