xref: /openbmc/linux/include/uapi/linux/fanotify.h (revision ffe1766a)
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_RENAME		0x10000000	/* File was renamed */
32 
33 #define FAN_ONDIR		0x40000000	/* Event occurred against dir */
34 
35 /* helper events */
36 #define FAN_CLOSE		(FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE) /* close */
37 #define FAN_MOVE		(FAN_MOVED_FROM | FAN_MOVED_TO) /* moves */
38 
39 /* flags used for fanotify_init() */
40 #define FAN_CLOEXEC		0x00000001
41 #define FAN_NONBLOCK		0x00000002
42 
43 /* These are NOT bitwise flags.  Both bits are used together.  */
44 #define FAN_CLASS_NOTIF		0x00000000
45 #define FAN_CLASS_CONTENT	0x00000004
46 #define FAN_CLASS_PRE_CONTENT	0x00000008
47 
48 /* Deprecated - do not use this in programs and do not add new flags here! */
49 #define FAN_ALL_CLASS_BITS	(FAN_CLASS_NOTIF | FAN_CLASS_CONTENT | \
50 				 FAN_CLASS_PRE_CONTENT)
51 
52 #define FAN_UNLIMITED_QUEUE	0x00000010
53 #define FAN_UNLIMITED_MARKS	0x00000020
54 #define FAN_ENABLE_AUDIT	0x00000040
55 
56 /* Flags to determine fanotify event format */
57 #define FAN_REPORT_PIDFD	0x00000080	/* Report pidfd for event->pid */
58 #define FAN_REPORT_TID		0x00000100	/* event->pid is thread id */
59 #define FAN_REPORT_FID		0x00000200	/* Report unique file id */
60 #define FAN_REPORT_DIR_FID	0x00000400	/* Report unique directory id */
61 #define FAN_REPORT_NAME		0x00000800	/* Report events with name */
62 #define FAN_REPORT_TARGET_FID	0x00001000	/* Report dirent target id  */
63 #define FAN_REPORT_FD_ERROR	0x00002000	/* event->fd can report error */
64 
65 /* Convenience macro - FAN_REPORT_NAME requires FAN_REPORT_DIR_FID */
66 #define FAN_REPORT_DFID_NAME	(FAN_REPORT_DIR_FID | FAN_REPORT_NAME)
67 /* Convenience macro - FAN_REPORT_TARGET_FID requires all other FID flags */
68 #define FAN_REPORT_DFID_NAME_TARGET (FAN_REPORT_DFID_NAME | \
69 				     FAN_REPORT_FID | FAN_REPORT_TARGET_FID)
70 
71 /* Deprecated - do not use this in programs and do not add new flags here! */
72 #define FAN_ALL_INIT_FLAGS	(FAN_CLOEXEC | FAN_NONBLOCK | \
73 				 FAN_ALL_CLASS_BITS | FAN_UNLIMITED_QUEUE |\
74 				 FAN_UNLIMITED_MARKS)
75 
76 /* flags used for fanotify_modify_mark() */
77 #define FAN_MARK_ADD		0x00000001
78 #define FAN_MARK_REMOVE		0x00000002
79 #define FAN_MARK_DONT_FOLLOW	0x00000004
80 #define FAN_MARK_ONLYDIR	0x00000008
81 /* FAN_MARK_MOUNT is		0x00000010 */
82 #define FAN_MARK_IGNORED_MASK	0x00000020
83 #define FAN_MARK_IGNORED_SURV_MODIFY	0x00000040
84 #define FAN_MARK_FLUSH		0x00000080
85 /* FAN_MARK_FILESYSTEM is	0x00000100 */
86 #define FAN_MARK_EVICTABLE	0x00000200
87 /* This bit is mutually exclusive with FAN_MARK_IGNORED_MASK bit */
88 #define FAN_MARK_IGNORE		0x00000400
89 
90 /* These are NOT bitwise flags.  Both bits can be used togther.  */
91 #define FAN_MARK_INODE		0x00000000
92 #define FAN_MARK_MOUNT		0x00000010
93 #define FAN_MARK_FILESYSTEM	0x00000100
94 
95 /*
96  * Convenience macro - FAN_MARK_IGNORE requires FAN_MARK_IGNORED_SURV_MODIFY
97  * for non-inode mark types.
98  */
99 #define FAN_MARK_IGNORE_SURV	(FAN_MARK_IGNORE | FAN_MARK_IGNORED_SURV_MODIFY)
100 
101 /* Deprecated - do not use this in programs and do not add new flags here! */
102 #define FAN_ALL_MARK_FLAGS	(FAN_MARK_ADD |\
103 				 FAN_MARK_REMOVE |\
104 				 FAN_MARK_DONT_FOLLOW |\
105 				 FAN_MARK_ONLYDIR |\
106 				 FAN_MARK_MOUNT |\
107 				 FAN_MARK_IGNORED_MASK |\
108 				 FAN_MARK_IGNORED_SURV_MODIFY |\
109 				 FAN_MARK_FLUSH)
110 
111 /* Deprecated - do not use this in programs and do not add new flags here! */
112 #define FAN_ALL_EVENTS (FAN_ACCESS |\
113 			FAN_MODIFY |\
114 			FAN_CLOSE |\
115 			FAN_OPEN)
116 
117 /*
118  * All events which require a permission response from userspace
119  */
120 /* Deprecated - do not use this in programs and do not add new flags here! */
121 #define FAN_ALL_PERM_EVENTS (FAN_OPEN_PERM |\
122 			     FAN_ACCESS_PERM)
123 
124 /* Deprecated - do not use this in programs and do not add new flags here! */
125 #define FAN_ALL_OUTGOING_EVENTS	(FAN_ALL_EVENTS |\
126 				 FAN_ALL_PERM_EVENTS |\
127 				 FAN_Q_OVERFLOW)
128 
129 #define FANOTIFY_METADATA_VERSION	3
130 
131 struct fanotify_event_metadata {
132 	__u32 event_len;
133 	__u8 vers;
134 	__u8 reserved;
135 	__u16 metadata_len;
136 	__aligned_u64 mask;
137 	__s32 fd;
138 	__s32 pid;
139 };
140 
141 #define FAN_EVENT_INFO_TYPE_FID		1
142 #define FAN_EVENT_INFO_TYPE_DFID_NAME	2
143 #define FAN_EVENT_INFO_TYPE_DFID	3
144 #define FAN_EVENT_INFO_TYPE_PIDFD	4
145 #define FAN_EVENT_INFO_TYPE_ERROR	5
146 
147 /* Special info types for FAN_RENAME */
148 #define FAN_EVENT_INFO_TYPE_OLD_DFID_NAME	10
149 /* Reserved for FAN_EVENT_INFO_TYPE_OLD_DFID	11 */
150 #define FAN_EVENT_INFO_TYPE_NEW_DFID_NAME	12
151 /* Reserved for FAN_EVENT_INFO_TYPE_NEW_DFID	13 */
152 
153 /* Variable length info record following event metadata */
154 struct fanotify_event_info_header {
155 	__u8 info_type;
156 	__u8 pad;
157 	__u16 len;
158 };
159 
160 /*
161  * Unique file identifier info record.
162  * This structure is used for records of types FAN_EVENT_INFO_TYPE_FID,
163  * FAN_EVENT_INFO_TYPE_DFID and FAN_EVENT_INFO_TYPE_DFID_NAME.
164  * For FAN_EVENT_INFO_TYPE_DFID_NAME there is additionally a null terminated
165  * name immediately after the file handle.
166  */
167 struct fanotify_event_info_fid {
168 	struct fanotify_event_info_header hdr;
169 	__kernel_fsid_t fsid;
170 	/*
171 	 * Following is an opaque struct file_handle that can be passed as
172 	 * an argument to open_by_handle_at(2).
173 	 */
174 	unsigned char handle[];
175 };
176 
177 /*
178  * This structure is used for info records of type FAN_EVENT_INFO_TYPE_PIDFD.
179  * It holds a pidfd for the pid that was responsible for generating an event.
180  */
181 struct fanotify_event_info_pidfd {
182 	struct fanotify_event_info_header hdr;
183 	__s32 pidfd;
184 };
185 
186 struct fanotify_event_info_error {
187 	struct fanotify_event_info_header hdr;
188 	__s32 error;
189 	__u32 error_count;
190 };
191 
192 /*
193  * User space may need to record additional information about its decision.
194  * The extra information type records what kind of information is included.
195  * The default is none. We also define an extra information buffer whose
196  * size is determined by the extra information type.
197  *
198  * If the information type is Audit Rule, then the information following
199  * is the rule number that triggered the user space decision that
200  * requires auditing.
201  */
202 
203 #define FAN_RESPONSE_INFO_NONE		0
204 #define FAN_RESPONSE_INFO_AUDIT_RULE	1
205 
206 struct fanotify_response {
207 	__s32 fd;
208 	__u32 response;
209 };
210 
211 struct fanotify_response_info_header {
212 	__u8 type;
213 	__u8 pad;
214 	__u16 len;
215 };
216 
217 struct fanotify_response_info_audit_rule {
218 	struct fanotify_response_info_header hdr;
219 	__u32 rule_number;
220 	__u32 subj_trust;
221 	__u32 obj_trust;
222 };
223 
224 /* Legit userspace responses to a _PERM event */
225 #define FAN_ALLOW	0x01
226 #define FAN_DENY	0x02
227 #define FAN_AUDIT	0x10	/* Bitmask to create audit record for result */
228 #define FAN_INFO	0x20	/* Bitmask to indicate additional information */
229 
230 /* No fd set in event */
231 #define FAN_NOFD	-1
232 #define FAN_NOPIDFD	FAN_NOFD
233 #define FAN_EPIDFD	-2
234 
235 /* Helper functions to deal with fanotify_event_metadata buffers */
236 #define FAN_EVENT_METADATA_LEN (sizeof(struct fanotify_event_metadata))
237 
238 #define FAN_EVENT_NEXT(meta, len) ((len) -= (meta)->event_len, \
239 				   (struct fanotify_event_metadata*)(((char *)(meta)) + \
240 				   (meta)->event_len))
241 
242 #define FAN_EVENT_OK(meta, len)	((long)(len) >= (long)FAN_EVENT_METADATA_LEN && \
243 				(long)(meta)->event_len >= (long)FAN_EVENT_METADATA_LEN && \
244 				(long)(meta)->event_len <= (long)(len))
245 
246 #endif /* _UAPI_LINUX_FANOTIFY_H */
247