xref: /linux-6.15/include/uapi/linux/fanotify.h (revision 0f46d81f)
16f52b16cSGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2607ca46eSDavid Howells #ifndef _UAPI_LINUX_FANOTIFY_H
3607ca46eSDavid Howells #define _UAPI_LINUX_FANOTIFY_H
4607ca46eSDavid Howells 
5607ca46eSDavid Howells #include <linux/types.h>
6607ca46eSDavid Howells 
7607ca46eSDavid Howells /* the following events that user-space can register for */
8607ca46eSDavid Howells #define FAN_ACCESS		0x00000001	/* File was accessed */
9607ca46eSDavid Howells #define FAN_MODIFY		0x00000002	/* File was modified */
10235328d1SAmir Goldstein #define FAN_ATTRIB		0x00000004	/* Metadata changed */
118c2c2549SVicki Pfau #define FAN_CLOSE_WRITE		0x00000008	/* Writable file closed */
128c2c2549SVicki Pfau #define FAN_CLOSE_NOWRITE	0x00000010	/* Unwritable file closed */
13607ca46eSDavid Howells #define FAN_OPEN		0x00000020	/* File was opened */
14235328d1SAmir Goldstein #define FAN_MOVED_FROM		0x00000040	/* File was moved from X */
15235328d1SAmir Goldstein #define FAN_MOVED_TO		0x00000080	/* File was moved to Y */
16235328d1SAmir Goldstein #define FAN_CREATE		0x00000100	/* Subfile was created */
17235328d1SAmir Goldstein #define FAN_DELETE		0x00000200	/* Subfile was deleted */
18235328d1SAmir Goldstein #define FAN_DELETE_SELF		0x00000400	/* Self was deleted */
19235328d1SAmir Goldstein #define FAN_MOVE_SELF		0x00000800	/* Self was moved */
209b076f1cSMatthew Bobrowski #define FAN_OPEN_EXEC		0x00001000	/* File was opened for exec */
21607ca46eSDavid Howells 
22607ca46eSDavid Howells #define FAN_Q_OVERFLOW		0x00004000	/* Event queued overflowed */
238d11a4f4SGabriel Krisman Bertazi #define FAN_FS_ERROR		0x00008000	/* Filesystem error */
24607ca46eSDavid Howells 
25607ca46eSDavid Howells #define FAN_OPEN_PERM		0x00010000	/* File open in perm check */
26607ca46eSDavid Howells #define FAN_ACCESS_PERM		0x00020000	/* File accessed in perm check */
2766917a31SMatthew Bobrowski #define FAN_OPEN_EXEC_PERM	0x00040000	/* File open/exec in perm check */
280a076036SAmir Goldstein /* #define FAN_DIR_MODIFY	0x00080000 */	/* Deprecated (reserved) */
29607ca46eSDavid Howells 
304f8afa33SAmir Goldstein #define FAN_PRE_ACCESS		0x00100000	/* Pre-content access hook */
31*0f46d81fSMiklos Szeredi #define FAN_MNT_ATTACH		0x01000000	/* Mount was attached */
32*0f46d81fSMiklos Szeredi #define FAN_MNT_DETACH		0x02000000	/* Mount was detached */
334f8afa33SAmir Goldstein 
346473ea76SAmir Goldstein #define FAN_EVENT_ON_CHILD	0x08000000	/* Interested in child events */
35607ca46eSDavid Howells 
363982534bSAmir Goldstein #define FAN_RENAME		0x10000000	/* File was renamed */
373982534bSAmir Goldstein 
386473ea76SAmir Goldstein #define FAN_ONDIR		0x40000000	/* Event occurred against dir */
39607ca46eSDavid Howells 
40607ca46eSDavid Howells /* helper events */
41607ca46eSDavid Howells #define FAN_CLOSE		(FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE) /* close */
42235328d1SAmir Goldstein #define FAN_MOVE		(FAN_MOVED_FROM | FAN_MOVED_TO) /* moves */
43607ca46eSDavid Howells 
44607ca46eSDavid Howells /* flags used for fanotify_init() */
45607ca46eSDavid Howells #define FAN_CLOEXEC		0x00000001
46607ca46eSDavid Howells #define FAN_NONBLOCK		0x00000002
47607ca46eSDavid Howells 
48d54f4fbaSAmir Goldstein /* These are NOT bitwise flags.  Both bits are used together.  */
49607ca46eSDavid Howells #define FAN_CLASS_NOTIF		0x00000000
50607ca46eSDavid Howells #define FAN_CLASS_CONTENT	0x00000004
51607ca46eSDavid Howells #define FAN_CLASS_PRE_CONTENT	0x00000008
5223c9deebSAmir Goldstein 
5323c9deebSAmir Goldstein /* Deprecated - do not use this in programs and do not add new flags here! */
54607ca46eSDavid Howells #define FAN_ALL_CLASS_BITS	(FAN_CLASS_NOTIF | FAN_CLASS_CONTENT | \
55607ca46eSDavid Howells 				 FAN_CLASS_PRE_CONTENT)
56607ca46eSDavid Howells 
57607ca46eSDavid Howells #define FAN_UNLIMITED_QUEUE	0x00000010
58607ca46eSDavid Howells #define FAN_UNLIMITED_MARKS	0x00000020
59de8cd83eSSteve Grubb #define FAN_ENABLE_AUDIT	0x00000040
60607ca46eSDavid Howells 
61d0a6a87eSAmir Goldstein /* Flags to determine fanotify event format */
62af579bebSMatthew Bobrowski #define FAN_REPORT_PIDFD	0x00000080	/* Report pidfd for event->pid */
63d0a6a87eSAmir Goldstein #define FAN_REPORT_TID		0x00000100	/* event->pid is thread id */
64e9e0c890SAmir Goldstein #define FAN_REPORT_FID		0x00000200	/* Report unique file id */
6583b7a598SAmir Goldstein #define FAN_REPORT_DIR_FID	0x00000400	/* Report unique directory id */
66929943b3SAmir Goldstein #define FAN_REPORT_NAME		0x00000800	/* Report events with name */
67d61fd650SAmir Goldstein #define FAN_REPORT_TARGET_FID	0x00001000	/* Report dirent target id  */
68522249f0SAmir Goldstein #define FAN_REPORT_FD_ERROR	0x00002000	/* event->fd can report error */
69*0f46d81fSMiklos Szeredi #define FAN_REPORT_MNT		0x00004000	/* Report mount events */
70929943b3SAmir Goldstein 
71929943b3SAmir Goldstein /* Convenience macro - FAN_REPORT_NAME requires FAN_REPORT_DIR_FID */
72929943b3SAmir Goldstein #define FAN_REPORT_DFID_NAME	(FAN_REPORT_DIR_FID | FAN_REPORT_NAME)
73d61fd650SAmir Goldstein /* Convenience macro - FAN_REPORT_TARGET_FID requires all other FID flags */
74d61fd650SAmir Goldstein #define FAN_REPORT_DFID_NAME_TARGET (FAN_REPORT_DFID_NAME | \
75d61fd650SAmir Goldstein 				     FAN_REPORT_FID | FAN_REPORT_TARGET_FID)
76d0a6a87eSAmir Goldstein 
7723c9deebSAmir Goldstein /* Deprecated - do not use this in programs and do not add new flags here! */
78607ca46eSDavid Howells #define FAN_ALL_INIT_FLAGS	(FAN_CLOEXEC | FAN_NONBLOCK | \
79607ca46eSDavid Howells 				 FAN_ALL_CLASS_BITS | FAN_UNLIMITED_QUEUE |\
80607ca46eSDavid Howells 				 FAN_UNLIMITED_MARKS)
81607ca46eSDavid Howells 
82607ca46eSDavid Howells /* flags used for fanotify_modify_mark() */
83607ca46eSDavid Howells #define FAN_MARK_ADD		0x00000001
84607ca46eSDavid Howells #define FAN_MARK_REMOVE		0x00000002
85607ca46eSDavid Howells #define FAN_MARK_DONT_FOLLOW	0x00000004
86607ca46eSDavid Howells #define FAN_MARK_ONLYDIR	0x00000008
87d54f4fbaSAmir Goldstein /* FAN_MARK_MOUNT is		0x00000010 */
88607ca46eSDavid Howells #define FAN_MARK_IGNORED_MASK	0x00000020
89607ca46eSDavid Howells #define FAN_MARK_IGNORED_SURV_MODIFY	0x00000040
90607ca46eSDavid Howells #define FAN_MARK_FLUSH		0x00000080
91d54f4fbaSAmir Goldstein /* FAN_MARK_FILESYSTEM is	0x00000100 */
927d5e005dSAmir Goldstein #define FAN_MARK_EVICTABLE	0x00000200
93e252f2edSAmir Goldstein /* This bit is mutually exclusive with FAN_MARK_IGNORED_MASK bit */
94e252f2edSAmir Goldstein #define FAN_MARK_IGNORE		0x00000400
95d54f4fbaSAmir Goldstein 
96d54f4fbaSAmir Goldstein /* These are NOT bitwise flags.  Both bits can be used togther.  */
97d54f4fbaSAmir Goldstein #define FAN_MARK_INODE		0x00000000
98d54f4fbaSAmir Goldstein #define FAN_MARK_MOUNT		0x00000010
99d54f4fbaSAmir Goldstein #define FAN_MARK_FILESYSTEM	0x00000100
100*0f46d81fSMiklos Szeredi #define FAN_MARK_MNTNS		0x00000110
101607ca46eSDavid Howells 
102e252f2edSAmir Goldstein /*
103e252f2edSAmir Goldstein  * Convenience macro - FAN_MARK_IGNORE requires FAN_MARK_IGNORED_SURV_MODIFY
104e252f2edSAmir Goldstein  * for non-inode mark types.
105e252f2edSAmir Goldstein  */
106e252f2edSAmir Goldstein #define FAN_MARK_IGNORE_SURV	(FAN_MARK_IGNORE | FAN_MARK_IGNORED_SURV_MODIFY)
107e252f2edSAmir Goldstein 
10823c9deebSAmir Goldstein /* Deprecated - do not use this in programs and do not add new flags here! */
109607ca46eSDavid Howells #define FAN_ALL_MARK_FLAGS	(FAN_MARK_ADD |\
110607ca46eSDavid Howells 				 FAN_MARK_REMOVE |\
111607ca46eSDavid Howells 				 FAN_MARK_DONT_FOLLOW |\
112607ca46eSDavid Howells 				 FAN_MARK_ONLYDIR |\
11323c9deebSAmir Goldstein 				 FAN_MARK_MOUNT |\
114607ca46eSDavid Howells 				 FAN_MARK_IGNORED_MASK |\
115607ca46eSDavid Howells 				 FAN_MARK_IGNORED_SURV_MODIFY |\
11623c9deebSAmir Goldstein 				 FAN_MARK_FLUSH)
117607ca46eSDavid Howells 
11823c9deebSAmir Goldstein /* Deprecated - do not use this in programs and do not add new flags here! */
119607ca46eSDavid Howells #define FAN_ALL_EVENTS (FAN_ACCESS |\
120607ca46eSDavid Howells 			FAN_MODIFY |\
121607ca46eSDavid Howells 			FAN_CLOSE |\
122607ca46eSDavid Howells 			FAN_OPEN)
123607ca46eSDavid Howells 
124607ca46eSDavid Howells /*
125607ca46eSDavid Howells  * All events which require a permission response from userspace
126607ca46eSDavid Howells  */
12723c9deebSAmir Goldstein /* Deprecated - do not use this in programs and do not add new flags here! */
128607ca46eSDavid Howells #define FAN_ALL_PERM_EVENTS (FAN_OPEN_PERM |\
129607ca46eSDavid Howells 			     FAN_ACCESS_PERM)
130607ca46eSDavid Howells 
13123c9deebSAmir Goldstein /* Deprecated - do not use this in programs and do not add new flags here! */
132607ca46eSDavid Howells #define FAN_ALL_OUTGOING_EVENTS	(FAN_ALL_EVENTS |\
133607ca46eSDavid Howells 				 FAN_ALL_PERM_EVENTS |\
134607ca46eSDavid Howells 				 FAN_Q_OVERFLOW)
135607ca46eSDavid Howells 
136607ca46eSDavid Howells #define FANOTIFY_METADATA_VERSION	3
137607ca46eSDavid Howells 
138607ca46eSDavid Howells struct fanotify_event_metadata {
139607ca46eSDavid Howells 	__u32 event_len;
140607ca46eSDavid Howells 	__u8 vers;
141607ca46eSDavid Howells 	__u8 reserved;
142607ca46eSDavid Howells 	__u16 metadata_len;
143607ca46eSDavid Howells 	__aligned_u64 mask;
144607ca46eSDavid Howells 	__s32 fd;
145607ca46eSDavid Howells 	__s32 pid;
146607ca46eSDavid Howells };
147607ca46eSDavid Howells 
1485e469c83SAmir Goldstein #define FAN_EVENT_INFO_TYPE_FID		1
14944d705b0SAmir Goldstein #define FAN_EVENT_INFO_TYPE_DFID_NAME	2
15083b7a598SAmir Goldstein #define FAN_EVENT_INFO_TYPE_DFID	3
151af579bebSMatthew Bobrowski #define FAN_EVENT_INFO_TYPE_PIDFD	4
152130a3c74SGabriel Krisman Bertazi #define FAN_EVENT_INFO_TYPE_ERROR	5
153870499bcSAmir Goldstein #define FAN_EVENT_INFO_TYPE_RANGE	6
154*0f46d81fSMiklos Szeredi #define FAN_EVENT_INFO_TYPE_MNT		7
1555e469c83SAmir Goldstein 
1567326e382SAmir Goldstein /* Special info types for FAN_RENAME */
1577326e382SAmir Goldstein #define FAN_EVENT_INFO_TYPE_OLD_DFID_NAME	10
1587326e382SAmir Goldstein /* Reserved for FAN_EVENT_INFO_TYPE_OLD_DFID	11 */
1597326e382SAmir Goldstein #define FAN_EVENT_INFO_TYPE_NEW_DFID_NAME	12
1607326e382SAmir Goldstein /* Reserved for FAN_EVENT_INFO_TYPE_NEW_DFID	13 */
1617326e382SAmir Goldstein 
1625e469c83SAmir Goldstein /* Variable length info record following event metadata */
1635e469c83SAmir Goldstein struct fanotify_event_info_header {
1645e469c83SAmir Goldstein 	__u8 info_type;
1655e469c83SAmir Goldstein 	__u8 pad;
1665e469c83SAmir Goldstein 	__u16 len;
1675e469c83SAmir Goldstein };
1685e469c83SAmir Goldstein 
16944d705b0SAmir Goldstein /*
17083b7a598SAmir Goldstein  * Unique file identifier info record.
17183b7a598SAmir Goldstein  * This structure is used for records of types FAN_EVENT_INFO_TYPE_FID,
17283b7a598SAmir Goldstein  * FAN_EVENT_INFO_TYPE_DFID and FAN_EVENT_INFO_TYPE_DFID_NAME.
17383b7a598SAmir Goldstein  * For FAN_EVENT_INFO_TYPE_DFID_NAME there is additionally a null terminated
17483b7a598SAmir Goldstein  * name immediately after the file handle.
17544d705b0SAmir Goldstein  */
1765e469c83SAmir Goldstein struct fanotify_event_info_fid {
1775e469c83SAmir Goldstein 	struct fanotify_event_info_header hdr;
1785e469c83SAmir Goldstein 	__kernel_fsid_t fsid;
1795e469c83SAmir Goldstein 	/*
1805e469c83SAmir Goldstein 	 * Following is an opaque struct file_handle that can be passed as
1815e469c83SAmir Goldstein 	 * an argument to open_by_handle_at(2).
1825e469c83SAmir Goldstein 	 */
18394dfc73eSGustavo A. R. Silva 	unsigned char handle[];
1845e469c83SAmir Goldstein };
1855e469c83SAmir Goldstein 
186af579bebSMatthew Bobrowski /*
187af579bebSMatthew Bobrowski  * This structure is used for info records of type FAN_EVENT_INFO_TYPE_PIDFD.
188af579bebSMatthew Bobrowski  * It holds a pidfd for the pid that was responsible for generating an event.
189af579bebSMatthew Bobrowski  */
190af579bebSMatthew Bobrowski struct fanotify_event_info_pidfd {
191af579bebSMatthew Bobrowski 	struct fanotify_event_info_header hdr;
192af579bebSMatthew Bobrowski 	__s32 pidfd;
193af579bebSMatthew Bobrowski };
194af579bebSMatthew Bobrowski 
195130a3c74SGabriel Krisman Bertazi struct fanotify_event_info_error {
196130a3c74SGabriel Krisman Bertazi 	struct fanotify_event_info_header hdr;
197130a3c74SGabriel Krisman Bertazi 	__s32 error;
198130a3c74SGabriel Krisman Bertazi 	__u32 error_count;
199130a3c74SGabriel Krisman Bertazi };
200130a3c74SGabriel Krisman Bertazi 
201870499bcSAmir Goldstein struct fanotify_event_info_range {
202870499bcSAmir Goldstein 	struct fanotify_event_info_header hdr;
203870499bcSAmir Goldstein 	__u32 pad;
204870499bcSAmir Goldstein 	__u64 offset;
205870499bcSAmir Goldstein 	__u64 count;
206870499bcSAmir Goldstein };
207870499bcSAmir Goldstein 
208*0f46d81fSMiklos Szeredi struct fanotify_event_info_mnt {
209*0f46d81fSMiklos Szeredi 	struct fanotify_event_info_header hdr;
210*0f46d81fSMiklos Szeredi 	__u64 mnt_id;
211*0f46d81fSMiklos Szeredi };
212*0f46d81fSMiklos Szeredi 
21370529a19SRichard Guy Briggs /*
21470529a19SRichard Guy Briggs  * User space may need to record additional information about its decision.
21570529a19SRichard Guy Briggs  * The extra information type records what kind of information is included.
21670529a19SRichard Guy Briggs  * The default is none. We also define an extra information buffer whose
21770529a19SRichard Guy Briggs  * size is determined by the extra information type.
21870529a19SRichard Guy Briggs  *
21970529a19SRichard Guy Briggs  * If the information type is Audit Rule, then the information following
22070529a19SRichard Guy Briggs  * is the rule number that triggered the user space decision that
22170529a19SRichard Guy Briggs  * requires auditing.
22270529a19SRichard Guy Briggs  */
22370529a19SRichard Guy Briggs 
22470529a19SRichard Guy Briggs #define FAN_RESPONSE_INFO_NONE		0
22570529a19SRichard Guy Briggs #define FAN_RESPONSE_INFO_AUDIT_RULE	1
22670529a19SRichard Guy Briggs 
227607ca46eSDavid Howells struct fanotify_response {
228607ca46eSDavid Howells 	__s32 fd;
229607ca46eSDavid Howells 	__u32 response;
230607ca46eSDavid Howells };
231607ca46eSDavid Howells 
23270529a19SRichard Guy Briggs struct fanotify_response_info_header {
23370529a19SRichard Guy Briggs 	__u8 type;
23470529a19SRichard Guy Briggs 	__u8 pad;
23570529a19SRichard Guy Briggs 	__u16 len;
23670529a19SRichard Guy Briggs };
23770529a19SRichard Guy Briggs 
23870529a19SRichard Guy Briggs struct fanotify_response_info_audit_rule {
23970529a19SRichard Guy Briggs 	struct fanotify_response_info_header hdr;
24070529a19SRichard Guy Briggs 	__u32 rule_number;
24170529a19SRichard Guy Briggs 	__u32 subj_trust;
24270529a19SRichard Guy Briggs 	__u32 obj_trust;
24370529a19SRichard Guy Briggs };
24470529a19SRichard Guy Briggs 
245607ca46eSDavid Howells /* Legit userspace responses to a _PERM event */
246607ca46eSDavid Howells #define FAN_ALLOW	0x01
247607ca46eSDavid Howells #define FAN_DENY	0x02
248b4b2ff4fSAmir Goldstein /* errno other than EPERM can specified in upper byte of deny response */
249b4b2ff4fSAmir Goldstein #define FAN_ERRNO_BITS	8
250b4b2ff4fSAmir Goldstein #define FAN_ERRNO_SHIFT (32 - FAN_ERRNO_BITS)
251b4b2ff4fSAmir Goldstein #define FAN_ERRNO_MASK	((1 << FAN_ERRNO_BITS) - 1)
252b4b2ff4fSAmir Goldstein #define FAN_DENY_ERRNO(err) \
253b4b2ff4fSAmir Goldstein 	(FAN_DENY | ((((__u32)(err)) & FAN_ERRNO_MASK) << FAN_ERRNO_SHIFT))
254b4b2ff4fSAmir Goldstein 
255de8cd83eSSteve Grubb #define FAN_AUDIT	0x10	/* Bitmask to create audit record for result */
25670529a19SRichard Guy Briggs #define FAN_INFO	0x20	/* Bitmask to indicate additional information */
257de8cd83eSSteve Grubb 
258607ca46eSDavid Howells /* No fd set in event */
259607ca46eSDavid Howells #define FAN_NOFD	-1
260af579bebSMatthew Bobrowski #define FAN_NOPIDFD	FAN_NOFD
261af579bebSMatthew Bobrowski #define FAN_EPIDFD	-2
262607ca46eSDavid Howells 
263607ca46eSDavid Howells /* Helper functions to deal with fanotify_event_metadata buffers */
264607ca46eSDavid Howells #define FAN_EVENT_METADATA_LEN (sizeof(struct fanotify_event_metadata))
265607ca46eSDavid Howells 
266607ca46eSDavid Howells #define FAN_EVENT_NEXT(meta, len) ((len) -= (meta)->event_len, \
267607ca46eSDavid Howells 				   (struct fanotify_event_metadata*)(((char *)(meta)) + \
268607ca46eSDavid Howells 				   (meta)->event_len))
269607ca46eSDavid Howells 
270607ca46eSDavid Howells #define FAN_EVENT_OK(meta, len)	((long)(len) >= (long)FAN_EVENT_METADATA_LEN && \
271607ca46eSDavid Howells 				(long)(meta)->event_len >= (long)FAN_EVENT_METADATA_LEN && \
272607ca46eSDavid Howells 				(long)(meta)->event_len <= (long)(len))
273607ca46eSDavid Howells 
274607ca46eSDavid Howells #endif /* _UAPI_LINUX_FANOTIFY_H */
275