xref: /linux/kernel/audit.h (revision 12d5fc96ebbb3b948861805b32a4c3a96105ccd9)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* audit -- definition of audit_context structure and supporting types
3  *
4  * Copyright 2003-2004 Red Hat, Inc.
5  * Copyright 2005 Hewlett-Packard Development Company, L.P.
6  * Copyright 2005 IBM Corporation
7  */
8 
9 #ifndef _KERNEL_AUDIT_H_
10 #define _KERNEL_AUDIT_H_
11 
12 #include <linux/fs.h>
13 #include <linux/audit.h>
14 #include <linux/security.h>
15 #include <linux/skbuff.h>
16 #include <uapi/linux/mqueue.h>
17 #include <linux/tty.h>
18 #include <uapi/linux/openat2.h> // struct open_how
19 
20 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
21  * for saving names from getname().  If we get more names we will allocate
22  * a name dynamically and also add those to the list anchored by names_list. */
23 #define AUDIT_NAMES	5
24 
25 /* At task start time, the audit_state is set in the audit_context using
26    a per-task filter.  At syscall entry, the audit_state is augmented by
27    the syscall filter. */
28 enum audit_state {
29 	AUDIT_STATE_DISABLED,	/* Do not create per-task audit_context.
30 				 * No syscall-specific audit records can
31 				 * be generated. */
32 	AUDIT_STATE_BUILD,	/* Create the per-task audit_context,
33 				 * and fill it in at syscall
34 				 * entry time.  This makes a full
35 				 * syscall record available if some
36 				 * other part of the kernel decides it
37 				 * should be recorded. */
38 	AUDIT_STATE_RECORD	/* Create the per-task audit_context,
39 				 * always fill it in at syscall entry
40 				 * time, and always write out the audit
41 				 * record at syscall exit time.  */
42 };
43 
44 /* Rule lists */
45 struct audit_watch;
46 struct audit_fsnotify_mark;
47 struct audit_tree;
48 struct audit_chunk;
49 
50 struct audit_entry {
51 	struct list_head	list;
52 	struct rcu_head		rcu;
53 	struct audit_krule	rule;
54 };
55 
56 struct audit_file_caps {
57 	kernel_cap_t		permitted;
58 	kernel_cap_t		inheritable;
59 	unsigned int		fE;		/* effective bit of file cap */
60 	kuid_t			rootid;
61 };
62 
63 struct audit_cap_data {
64 	kernel_cap_t		permitted;
65 	kernel_cap_t		inheritable;
66 	kernel_cap_t		effective;
67 	kernel_cap_t		ambient;
68 };
69 
70 /* When fs/namei.c:getname() is called, we store the pointer in name and bump
71  * the refcnt in the associated filename struct.
72  *
73  * Further, in fs/namei.c:path_lookup() we store the inode and device.
74  */
75 struct audit_names {
76 	struct list_head	list;		/* audit_context->names_list */
77 
78 	struct filename		*name;
79 	int			name_len;	/* number of chars to log */
80 	bool			hidden;		/* don't log this record */
81 
82 	u64			ino;
83 	dev_t			dev;
84 	umode_t			mode;
85 	kuid_t			uid;
86 	kgid_t			gid;
87 	dev_t			rdev;
88 	struct lsm_prop		oprop;
89 	struct audit_file_caps	fcap;
90 	unsigned int		fcap_ver;
91 	unsigned char		type;		/* record type */
92 	/*
93 	 * This was an allocated audit_names and not from the array of
94 	 * names allocated in the task audit context.  Thus this name
95 	 * should be freed on syscall exit.
96 	 */
97 	bool			should_free;
98 };
99 
100 struct audit_proctitle {
101 	int	len;	/* length of the cmdline field. */
102 	char	*value;	/* the cmdline field */
103 };
104 
105 /* A timestamp/serial pair to identify an event */
106 struct audit_stamp {
107 	struct timespec64	ctime;	/* time of syscall entry */
108 	unsigned int		serial;	/* serial number for record */
109 };
110 
111 /* The per-task audit context. */
112 struct audit_context {
113 	int		    dummy;	/* must be the first element */
114 	enum {
115 		AUDIT_CTX_UNUSED,	/* audit_context is currently unused */
116 		AUDIT_CTX_SYSCALL,	/* in use by syscall */
117 		AUDIT_CTX_URING,	/* in use by io_uring */
118 	} context;
119 	enum audit_state    state, current_state;
120 	struct audit_stamp  stamp;	/* event identifier */
121 	int		    major;      /* syscall number */
122 	int		    uring_op;   /* uring operation */
123 	unsigned long	    argv[4];    /* syscall arguments */
124 	long		    return_code;/* syscall return code */
125 	u64		    prio;
126 	int		    return_valid; /* return code is valid */
127 	/*
128 	 * The names_list is the list of all audit_names collected during this
129 	 * syscall.  The first AUDIT_NAMES entries in the names_list will
130 	 * actually be from the preallocated_names array for performance
131 	 * reasons.  Except during allocation they should never be referenced
132 	 * through the preallocated_names array and should only be found/used
133 	 * by running the names_list.
134 	 */
135 	struct audit_names  preallocated_names[AUDIT_NAMES];
136 	int		    name_count; /* total records in names_list */
137 	struct list_head    names_list;	/* struct audit_names->list anchor */
138 	char		    *filterkey;	/* key for rule that triggered record */
139 	struct path	    pwd;
140 	struct audit_aux_data *aux;
141 	struct audit_aux_data *aux_pids;
142 	struct sockaddr_storage *sockaddr;
143 	size_t sockaddr_len;
144 	/* Save things to print about task_struct */
145 	pid_t		    ppid;
146 	kuid_t		    uid, euid, suid, fsuid;
147 	kgid_t		    gid, egid, sgid, fsgid;
148 	unsigned long	    personality;
149 	int		    arch;
150 
151 	pid_t		    target_pid;
152 	kuid_t		    target_auid;
153 	kuid_t		    target_uid;
154 	unsigned int	    target_sessionid;
155 	struct lsm_prop	    target_ref;
156 	char		    target_comm[TASK_COMM_LEN];
157 
158 	struct audit_tree_refs *trees, *first_trees;
159 	struct list_head killed_trees;
160 	int tree_count;
161 
162 	int type;
163 	union {
164 		struct {
165 			int nargs;
166 			long args[6];
167 		} socketcall;
168 		struct {
169 			kuid_t			uid;
170 			kgid_t			gid;
171 			umode_t			mode;
172 			struct lsm_prop		oprop;
173 			int			has_perm;
174 			uid_t			perm_uid;
175 			gid_t			perm_gid;
176 			umode_t			perm_mode;
177 			unsigned long		qbytes;
178 		} ipc;
179 		struct {
180 			mqd_t			mqdes;
181 			struct mq_attr		mqstat;
182 		} mq_getsetattr;
183 		struct {
184 			mqd_t			mqdes;
185 			int			sigev_signo;
186 		} mq_notify;
187 		struct {
188 			mqd_t			mqdes;
189 			size_t			msg_len;
190 			unsigned int		msg_prio;
191 			struct timespec64	abs_timeout;
192 		} mq_sendrecv;
193 		struct {
194 			int			oflag;
195 			umode_t			mode;
196 			struct mq_attr		attr;
197 		} mq_open;
198 		struct {
199 			pid_t			pid;
200 			struct audit_cap_data	cap;
201 		} capset;
202 		struct {
203 			int			fd;
204 			int			flags;
205 		} mmap;
206 		struct open_how openat2;
207 		struct {
208 			int			argc;
209 		} execve;
210 		struct {
211 			const char		*name;
212 		} module;
213 		struct {
214 			struct audit_ntp_data	ntp_data;
215 			struct timespec64	tk_injoffset;
216 		} time;
217 	};
218 	int fds[2];
219 	struct audit_proctitle proctitle;
220 };
221 
222 extern bool audit_ever_enabled;
223 
224 extern void audit_log_session_info(struct audit_buffer *ab);
225 
226 extern int auditd_test_task(struct task_struct *task);
227 
228 #define AUDIT_INODE_BUCKETS	32
229 extern struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
230 
231 static inline int audit_hash_ino(u64 ino)
232 {
233 	return ((u32)ino & (AUDIT_INODE_BUCKETS-1));
234 }
235 
236 /* Indicates that audit should log the full pathname. */
237 #define AUDIT_NAME_FULL -1
238 
239 extern int audit_match_class(int class, unsigned int syscall);
240 extern int audit_comparator(const u32 left, const u32 op, const u32 right);
241 extern int audit_uid_comparator(kuid_t left, u32 op, kuid_t right);
242 extern int audit_gid_comparator(kgid_t left, u32 op, kgid_t right);
243 extern int parent_len(const char *path);
244 extern int audit_compare_dname_path(const struct qstr *dname, const char *path, int plen);
245 extern struct sk_buff *audit_make_reply(int seq, int type, int done, int multi,
246 					const void *payload, int size);
247 extern void		    audit_panic(const char *message);
248 
249 struct audit_netlink_list {
250 	__u32 portid;
251 	struct net *net;
252 	struct sk_buff_head q;
253 };
254 
255 int audit_send_list_thread(void *_dest);
256 
257 extern struct mutex audit_filter_mutex;
258 extern int audit_del_rule(struct audit_entry *entry);
259 extern void audit_free_rule_rcu(struct rcu_head *head);
260 extern struct list_head audit_filter_list[];
261 
262 struct audit_watch_ctx {
263 	struct inode *dir;
264 	struct inode *child;
265 };
266 
267 extern struct audit_entry *audit_dupe_rule(struct audit_krule *old,
268 					   struct audit_watch_ctx *ctx);
269 extern void audit_log_d_path_exe(struct audit_buffer *ab,
270 				 struct mm_struct *mm);
271 
272 extern struct tty_struct *audit_get_tty(void);
273 extern void audit_put_tty(struct tty_struct *tty);
274 
275 /* audit watch/mark/tree functions */
276 extern unsigned int audit_serial(void);
277 #ifdef CONFIG_AUDITSYSCALL
278 void audit_rule_account(const struct audit_krule *rule);
279 void audit_rule_unaccount(const struct audit_krule *rule);
280 
281 extern int auditsc_get_stamp(struct audit_context *ctx,
282 			     struct audit_stamp *stamp);
283 
284 extern void audit_put_watch(struct audit_watch *watch);
285 extern void audit_get_watch(struct audit_watch *watch);
286 extern int audit_to_watch(struct audit_krule *krule, char *path, int len,
287 			  u32 op);
288 extern int audit_add_watch(struct audit_krule *krule, struct list_head **list);
289 extern void audit_remove_watch_rule(struct audit_krule *krule);
290 extern char *audit_watch_path(struct audit_watch *watch);
291 extern int audit_watch_compare(struct audit_watch *watch, u64 ino, dev_t dev);
292 
293 extern struct audit_fsnotify_mark *audit_alloc_mark(struct audit_krule *krule,
294 						    char *pathname, int len,
295 						    struct audit_watch_ctx *ctx);
296 extern char *audit_mark_path(struct audit_fsnotify_mark *mark);
297 extern void audit_remove_mark(struct audit_fsnotify_mark *audit_mark);
298 extern void audit_remove_mark_rule(struct audit_krule *krule);
299 extern int audit_mark_compare(struct audit_fsnotify_mark *mark, u64 ino,
300 			      dev_t dev);
301 extern int audit_dupe_exe(struct audit_krule *new, struct audit_krule *old,
302 			  struct audit_watch_ctx *ctx);
303 extern int audit_exe_compare(struct task_struct *tsk,
304 			     struct audit_fsnotify_mark *mark);
305 
306 extern struct audit_chunk *audit_tree_lookup(const struct inode *inode);
307 extern void audit_put_chunk(struct audit_chunk *chunk);
308 extern bool audit_tree_match(struct audit_chunk *chunk,
309 			     struct audit_tree *tree);
310 extern int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op);
311 extern int audit_add_tree_rule(struct audit_krule *rule);
312 extern int audit_remove_tree_rule(struct audit_krule *rule);
313 extern void audit_trim_trees(void);
314 extern int audit_tag_tree(char *old, char *new);
315 extern const char *audit_tree_path(struct audit_tree *tree);
316 extern void audit_put_tree(struct audit_tree *tree);
317 extern void audit_kill_trees(struct audit_context *context);
318 
319 extern int audit_signal_info_syscall(struct task_struct *t);
320 extern void audit_filter_inodes(struct task_struct *tsk,
321 				struct audit_context *ctx);
322 extern struct list_head *audit_killed_trees(void);
323 #else /* CONFIG_AUDITSYSCALL */
324 #define audit_rule_account(...) do { } while (0)
325 #define audit_rule_unaccount(...) do { } while (0)
326 #define auditsc_get_stamp(c, s) 0
327 #define audit_put_watch(w) do { } while (0)
328 #define audit_get_watch(w) do { } while (0)
329 #define audit_to_watch(k, p, l, o) (-EINVAL)
330 #define audit_add_watch(k, l) (-EINVAL)
331 #define audit_remove_watch_rule(k) BUG()
332 #define audit_watch_path(w) ""
333 #define audit_watch_compare(w, i, d) 0
334 
335 #define audit_alloc_mark(k, p, l, c) (ERR_PTR(-EINVAL))
336 #define audit_mark_path(m) ""
337 #define audit_remove_mark(m) do { } while (0)
338 #define audit_remove_mark_rule(k) do { } while (0)
339 #define audit_mark_compare(m, i, d) 0
340 #define audit_exe_compare(t, m) (-EINVAL)
341 #define audit_dupe_exe(n, o, c) (-EINVAL)
342 
343 #define audit_remove_tree_rule(rule) BUG()
344 #define audit_add_tree_rule(rule) -EINVAL
345 #define audit_make_tree(rule, str, op) -EINVAL
346 #define audit_trim_trees() do { } while (0)
347 #define audit_put_tree(tree) do { } while (0)
348 #define audit_tag_tree(old, new) -EINVAL
349 #define audit_tree_path(rule) ""	/* never called */
350 #define audit_kill_trees(context) BUG()
351 
352 static inline int audit_signal_info_syscall(struct task_struct *t)
353 {
354 	return 0;
355 }
356 
357 #define audit_filter_inodes(t, c) do { } while (0)
358 #endif /* CONFIG_AUDITSYSCALL */
359 
360 extern char *audit_unpack_string(void **bufp, size_t *remain, size_t len);
361 
362 extern int audit_filter(int msgtype, unsigned int listtype);
363 
364 extern void audit_ctl_lock(void);
365 extern void audit_ctl_unlock(void);
366 
367 #endif
368