11a59d1b8SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2fe7752baSDavid Woodhouse /* audit -- definition of audit_context structure and supporting types
3fe7752baSDavid Woodhouse *
4fe7752baSDavid Woodhouse * Copyright 2003-2004 Red Hat, Inc.
5fe7752baSDavid Woodhouse * Copyright 2005 Hewlett-Packard Development Company, L.P.
6fe7752baSDavid Woodhouse * Copyright 2005 IBM Corporation
7fe7752baSDavid Woodhouse */
8fe7752baSDavid Woodhouse
9d97e9938SMaYuming #ifndef _KERNEL_AUDIT_H_
10d97e9938SMaYuming #define _KERNEL_AUDIT_H_
11d97e9938SMaYuming
12fe7752baSDavid Woodhouse #include <linux/fs.h>
13fe7752baSDavid Woodhouse #include <linux/audit.h>
147183abccSCasey Schaufler #include <linux/security.h>
159044e6bcSAl Viro #include <linux/skbuff.h>
16b24a30a7SEric Paris #include <uapi/linux/mqueue.h>
173f5be2daSRichard Guy Briggs #include <linux/tty.h>
18571e5c0eSRichard Guy Briggs #include <uapi/linux/openat2.h> // struct open_how
19fe7752baSDavid Woodhouse
20b24a30a7SEric Paris /* AUDIT_NAMES is the number of slots we reserve in the audit_context
21b24a30a7SEric Paris * for saving names from getname(). If we get more names we will allocate
22b24a30a7SEric Paris * a name dynamically and also add those to the list anchored by names_list. */
23b24a30a7SEric Paris #define AUDIT_NAMES 5
24b24a30a7SEric Paris
25fe7752baSDavid Woodhouse /* At task start time, the audit_state is set in the audit_context using
26fe7752baSDavid Woodhouse a per-task filter. At syscall entry, the audit_state is augmented by
27fe7752baSDavid Woodhouse the syscall filter. */
28fe7752baSDavid Woodhouse enum audit_state {
29619ed58aSSergey Nazarov AUDIT_STATE_DISABLED, /* Do not create per-task audit_context.
30fe7752baSDavid Woodhouse * No syscall-specific audit records can
31fe7752baSDavid Woodhouse * be generated. */
32619ed58aSSergey Nazarov AUDIT_STATE_BUILD, /* Create the per-task audit_context,
33997f5b64SEric Paris * and fill it in at syscall
34fe7752baSDavid Woodhouse * entry time. This makes a full
35fe7752baSDavid Woodhouse * syscall record available if some
36fe7752baSDavid Woodhouse * other part of the kernel decides it
37fe7752baSDavid Woodhouse * should be recorded. */
38619ed58aSSergey Nazarov AUDIT_STATE_RECORD /* Create the per-task audit_context,
39fe7752baSDavid Woodhouse * always fill it in at syscall entry
40fe7752baSDavid Woodhouse * time, and always write out the audit
41fe7752baSDavid Woodhouse * record at syscall exit time. */
42fe7752baSDavid Woodhouse };
43fe7752baSDavid Woodhouse
44fe7752baSDavid Woodhouse /* Rule lists */
45cfcad62cSEric Paris struct audit_watch;
467f492942SRichard Guy Briggs struct audit_fsnotify_mark;
4774c3cbe3SAl Viro struct audit_tree;
4874c3cbe3SAl Viro struct audit_chunk;
4974c3cbe3SAl Viro
50fe7752baSDavid Woodhouse struct audit_entry {
51fe7752baSDavid Woodhouse struct list_head list;
52fe7752baSDavid Woodhouse struct rcu_head rcu;
5393315ed6SAmy Griffis struct audit_krule rule;
54fe7752baSDavid Woodhouse };
55fe7752baSDavid Woodhouse
56b24a30a7SEric Paris struct audit_cap_data {
57b24a30a7SEric Paris kernel_cap_t permitted;
58b24a30a7SEric Paris kernel_cap_t inheritable;
59b24a30a7SEric Paris union {
60b24a30a7SEric Paris unsigned int fE; /* effective bit of file cap */
61b24a30a7SEric Paris kernel_cap_t effective; /* effective set of process */
62b24a30a7SEric Paris };
637786f6b6SRichard Guy Briggs kernel_cap_t ambient;
642fec30e2SRichard Guy Briggs kuid_t rootid;
65b24a30a7SEric Paris };
66b24a30a7SEric Paris
6755422d0bSPaul Moore /* When fs/namei.c:getname() is called, we store the pointer in name and bump
6855422d0bSPaul Moore * the refcnt in the associated filename struct.
69b24a30a7SEric Paris *
70b24a30a7SEric Paris * Further, in fs/namei.c:path_lookup() we store the inode and device.
71b24a30a7SEric Paris */
72b24a30a7SEric Paris struct audit_names {
73b24a30a7SEric Paris struct list_head list; /* audit_context->names_list */
74b24a30a7SEric Paris
75b24a30a7SEric Paris struct filename *name;
76b24a30a7SEric Paris int name_len; /* number of chars to log */
7779f6530cSJeff Layton bool hidden; /* don't log this record */
78b24a30a7SEric Paris
79b24a30a7SEric Paris unsigned long ino;
80b24a30a7SEric Paris dev_t dev;
81b24a30a7SEric Paris umode_t mode;
82b24a30a7SEric Paris kuid_t uid;
83b24a30a7SEric Paris kgid_t gid;
84b24a30a7SEric Paris dev_t rdev;
85e0a8dcbdSCasey Schaufler struct lsm_prop oprop;
86b24a30a7SEric Paris struct audit_cap_data fcap;
87b24a30a7SEric Paris unsigned int fcap_ver;
88b24a30a7SEric Paris unsigned char type; /* record type */
89b24a30a7SEric Paris /*
90b24a30a7SEric Paris * This was an allocated audit_names and not from the array of
91b24a30a7SEric Paris * names allocated in the task audit context. Thus this name
92b24a30a7SEric Paris * should be freed on syscall exit.
93b24a30a7SEric Paris */
94b24a30a7SEric Paris bool should_free;
95b24a30a7SEric Paris };
96b24a30a7SEric Paris
973f1c8250SWilliam Roberts struct audit_proctitle {
983f1c8250SWilliam Roberts int len; /* length of the cmdline field. */
993f1c8250SWilliam Roberts char *value; /* the cmdline field */
1003f1c8250SWilliam Roberts };
1013f1c8250SWilliam Roberts
102b24a30a7SEric Paris /* The per-task audit context. */
103b24a30a7SEric Paris struct audit_context {
104b24a30a7SEric Paris int dummy; /* must be the first element */
10512c5e81dSPaul Moore enum {
10612c5e81dSPaul Moore AUDIT_CTX_UNUSED, /* audit_context is currently unused */
10712c5e81dSPaul Moore AUDIT_CTX_SYSCALL, /* in use by syscall */
1085bd2182dSPaul Moore AUDIT_CTX_URING, /* in use by io_uring */
10912c5e81dSPaul Moore } context;
110b24a30a7SEric Paris enum audit_state state, current_state;
111b24a30a7SEric Paris unsigned int serial; /* serial number for record */
112b24a30a7SEric Paris int major; /* syscall number */
1135bd2182dSPaul Moore int uring_op; /* uring operation */
1142115bb25SDeepa Dinamani struct timespec64 ctime; /* time of syscall entry */
115b24a30a7SEric Paris unsigned long argv[4]; /* syscall arguments */
116b24a30a7SEric Paris long return_code;/* syscall return code */
117b24a30a7SEric Paris u64 prio;
118b24a30a7SEric Paris int return_valid; /* return code is valid */
119b24a30a7SEric Paris /*
120b24a30a7SEric Paris * The names_list is the list of all audit_names collected during this
121b24a30a7SEric Paris * syscall. The first AUDIT_NAMES entries in the names_list will
122b24a30a7SEric Paris * actually be from the preallocated_names array for performance
123b24a30a7SEric Paris * reasons. Except during allocation they should never be referenced
124b24a30a7SEric Paris * through the preallocated_names array and should only be found/used
125b24a30a7SEric Paris * by running the names_list.
126b24a30a7SEric Paris */
127b24a30a7SEric Paris struct audit_names preallocated_names[AUDIT_NAMES];
128b24a30a7SEric Paris int name_count; /* total records in names_list */
129b24a30a7SEric Paris struct list_head names_list; /* struct audit_names->list anchor */
130b24a30a7SEric Paris char *filterkey; /* key for rule that triggered record */
131b24a30a7SEric Paris struct path pwd;
132b24a30a7SEric Paris struct audit_aux_data *aux;
133b24a30a7SEric Paris struct audit_aux_data *aux_pids;
134b24a30a7SEric Paris struct sockaddr_storage *sockaddr;
135b24a30a7SEric Paris size_t sockaddr_len;
136b24a30a7SEric Paris /* Save things to print about task_struct */
137e84d9f52SRichard Guy Briggs pid_t ppid;
138b24a30a7SEric Paris kuid_t uid, euid, suid, fsuid;
139b24a30a7SEric Paris kgid_t gid, egid, sgid, fsgid;
140b24a30a7SEric Paris unsigned long personality;
141b24a30a7SEric Paris int arch;
142b24a30a7SEric Paris
143b24a30a7SEric Paris pid_t target_pid;
144b24a30a7SEric Paris kuid_t target_auid;
145b24a30a7SEric Paris kuid_t target_uid;
146b24a30a7SEric Paris unsigned int target_sessionid;
147*13d826e5SCasey Schaufler struct lsm_prop target_ref;
148b24a30a7SEric Paris char target_comm[TASK_COMM_LEN];
149b24a30a7SEric Paris
150b24a30a7SEric Paris struct audit_tree_refs *trees, *first_trees;
151b24a30a7SEric Paris struct list_head killed_trees;
152b24a30a7SEric Paris int tree_count;
153b24a30a7SEric Paris
154b24a30a7SEric Paris int type;
155b24a30a7SEric Paris union {
156b24a30a7SEric Paris struct {
157b24a30a7SEric Paris int nargs;
158b24a30a7SEric Paris long args[6];
159b24a30a7SEric Paris } socketcall;
160b24a30a7SEric Paris struct {
161b24a30a7SEric Paris kuid_t uid;
162b24a30a7SEric Paris kgid_t gid;
163b24a30a7SEric Paris umode_t mode;
1647183abccSCasey Schaufler struct lsm_prop oprop;
165b24a30a7SEric Paris int has_perm;
166b24a30a7SEric Paris uid_t perm_uid;
167b24a30a7SEric Paris gid_t perm_gid;
168b24a30a7SEric Paris umode_t perm_mode;
169b24a30a7SEric Paris unsigned long qbytes;
170b24a30a7SEric Paris } ipc;
171b24a30a7SEric Paris struct {
172b24a30a7SEric Paris mqd_t mqdes;
173b24a30a7SEric Paris struct mq_attr mqstat;
174b24a30a7SEric Paris } mq_getsetattr;
175b24a30a7SEric Paris struct {
176b24a30a7SEric Paris mqd_t mqdes;
177b24a30a7SEric Paris int sigev_signo;
178b24a30a7SEric Paris } mq_notify;
179b24a30a7SEric Paris struct {
180b24a30a7SEric Paris mqd_t mqdes;
181b24a30a7SEric Paris size_t msg_len;
182b24a30a7SEric Paris unsigned int msg_prio;
183b9047726SDeepa Dinamani struct timespec64 abs_timeout;
184b24a30a7SEric Paris } mq_sendrecv;
185b24a30a7SEric Paris struct {
186b24a30a7SEric Paris int oflag;
187b24a30a7SEric Paris umode_t mode;
188b24a30a7SEric Paris struct mq_attr attr;
189b24a30a7SEric Paris } mq_open;
190b24a30a7SEric Paris struct {
191b24a30a7SEric Paris pid_t pid;
192b24a30a7SEric Paris struct audit_cap_data cap;
193b24a30a7SEric Paris } capset;
194b24a30a7SEric Paris struct {
195b24a30a7SEric Paris int fd;
196b24a30a7SEric Paris int flags;
197b24a30a7SEric Paris } mmap;
198571e5c0eSRichard Guy Briggs struct open_how openat2;
199d9cfea91SRichard Guy Briggs struct {
200d9cfea91SRichard Guy Briggs int argc;
201d9cfea91SRichard Guy Briggs } execve;
202ca86cad7SRichard Guy Briggs struct {
203ca86cad7SRichard Guy Briggs char *name;
204ca86cad7SRichard Guy Briggs } module;
205272ceeaeSRichard Guy Briggs struct {
206272ceeaeSRichard Guy Briggs struct audit_ntp_data ntp_data;
207272ceeaeSRichard Guy Briggs struct timespec64 tk_injoffset;
208272ceeaeSRichard Guy Briggs } time;
209b24a30a7SEric Paris };
210b24a30a7SEric Paris int fds[2];
2113f1c8250SWilliam Roberts struct audit_proctitle proctitle;
212b24a30a7SEric Paris };
213b24a30a7SEric Paris
214b3b4fdf6SPaul Moore extern bool audit_ever_enabled;
215c782f242SHarvey Harrison
2160fe3c7fcSRichard Guy Briggs extern void audit_log_session_info(struct audit_buffer *ab);
2170fe3c7fcSRichard Guy Briggs
218b6c7c115SPaul Moore extern int auditd_test_task(struct task_struct *task);
219fe7752baSDavid Woodhouse
220f368c07dSAmy Griffis #define AUDIT_INODE_BUCKETS 32
221f368c07dSAmy Griffis extern struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
222f368c07dSAmy Griffis
audit_hash_ino(u32 ino)223f368c07dSAmy Griffis static inline int audit_hash_ino(u32 ino)
224f368c07dSAmy Griffis {
225f368c07dSAmy Griffis return (ino & (AUDIT_INODE_BUCKETS-1));
226f368c07dSAmy Griffis }
227f368c07dSAmy Griffis
228e3d6b07bSJeff Layton /* Indicates that audit should log the full pathname. */
229e3d6b07bSJeff Layton #define AUDIT_NAME_FULL -1
230e3d6b07bSJeff Layton
23155669bfaSAl Viro extern int audit_match_class(int class, unsigned syscall);
232f368c07dSAmy Griffis extern int audit_comparator(const u32 left, const u32 op, const u32 right);
233ca57ec0fSEric W. Biederman extern int audit_uid_comparator(kuid_t left, u32 op, kuid_t right);
234ca57ec0fSEric W. Biederman extern int audit_gid_comparator(kgid_t left, u32 op, kgid_t right);
235bfcec708SJeff Layton extern int parent_len(const char *path);
236795d673aSAl Viro extern int audit_compare_dname_path(const struct qstr *dname, const char *path, int plen);
23745a0642bSPaul Moore extern struct sk_buff *audit_make_reply(int seq, int type, int done, int multi,
238b8800aa5SStephen Hemminger const void *payload, int size);
239fe7752baSDavid Woodhouse extern void audit_panic(const char *message);
2403dc7e315SDarrel Goeddel
2419044e6bcSAl Viro struct audit_netlink_list {
242f9441639SRichard Guy Briggs __u32 portid;
243638a0fd2SEric W. Biederman struct net *net;
2449044e6bcSAl Viro struct sk_buff_head q;
2459044e6bcSAl Viro };
2469044e6bcSAl Viro
2473054d067SPaul Moore int audit_send_list_thread(void *_dest);
2489044e6bcSAl Viro
24974c3cbe3SAl Viro extern struct mutex audit_filter_mutex;
250e4c1a0d1SDerek Robson extern int audit_del_rule(struct audit_entry *entry);
251e4c1a0d1SDerek Robson extern void audit_free_rule_rcu(struct rcu_head *head);
252c782f242SHarvey Harrison extern struct list_head audit_filter_list[];
25374c3cbe3SAl Viro
254939a67fcSEric Paris extern struct audit_entry *audit_dupe_rule(struct audit_krule *old);
255939a67fcSEric Paris
2564766b199SDavidlohr Bueso extern void audit_log_d_path_exe(struct audit_buffer *ab,
2574766b199SDavidlohr Bueso struct mm_struct *mm);
2584766b199SDavidlohr Bueso
2592a1fe215SPaul Moore extern struct tty_struct *audit_get_tty(void);
2603f5be2daSRichard Guy Briggs extern void audit_put_tty(struct tty_struct *tty);
2613f5be2daSRichard Guy Briggs
26205c7a9cbSRichard Guy Briggs /* audit watch/mark/tree functions */
263cd108b5cSRichard Guy Briggs extern unsigned int audit_serial(void);
264e455ca40SArnd Bergmann #ifdef CONFIG_AUDITSYSCALL
265cd108b5cSRichard Guy Briggs extern int auditsc_get_stamp(struct audit_context *ctx,
266cd108b5cSRichard Guy Briggs struct timespec64 *t, unsigned int *serial);
267cd108b5cSRichard Guy Briggs
268cfcad62cSEric Paris extern void audit_put_watch(struct audit_watch *watch);
269cfcad62cSEric Paris extern void audit_get_watch(struct audit_watch *watch);
27005c7a9cbSRichard Guy Briggs extern int audit_to_watch(struct audit_krule *krule, char *path, int len,
27105c7a9cbSRichard Guy Briggs u32 op);
272ae7b8f41SEric Paris extern int audit_add_watch(struct audit_krule *krule, struct list_head **list);
273a05fb6ccSEric Paris extern void audit_remove_watch_rule(struct audit_krule *krule);
274cfcad62cSEric Paris extern char *audit_watch_path(struct audit_watch *watch);
27505c7a9cbSRichard Guy Briggs extern int audit_watch_compare(struct audit_watch *watch, unsigned long ino,
27605c7a9cbSRichard Guy Briggs dev_t dev);
2777f492942SRichard Guy Briggs
27805c7a9cbSRichard Guy Briggs extern struct audit_fsnotify_mark *audit_alloc_mark(struct audit_krule *krule,
27905c7a9cbSRichard Guy Briggs char *pathname, int len);
2807f492942SRichard Guy Briggs extern char *audit_mark_path(struct audit_fsnotify_mark *mark);
2817f492942SRichard Guy Briggs extern void audit_remove_mark(struct audit_fsnotify_mark *audit_mark);
2827f492942SRichard Guy Briggs extern void audit_remove_mark_rule(struct audit_krule *krule);
28305c7a9cbSRichard Guy Briggs extern int audit_mark_compare(struct audit_fsnotify_mark *mark,
28405c7a9cbSRichard Guy Briggs unsigned long ino, dev_t dev);
28534d99af5SRichard Guy Briggs extern int audit_dupe_exe(struct audit_krule *new, struct audit_krule *old);
28605c7a9cbSRichard Guy Briggs extern int audit_exe_compare(struct task_struct *tsk,
28705c7a9cbSRichard Guy Briggs struct audit_fsnotify_mark *mark);
2887f492942SRichard Guy Briggs
28905c7a9cbSRichard Guy Briggs extern struct audit_chunk *audit_tree_lookup(const struct inode *inode);
29005c7a9cbSRichard Guy Briggs extern void audit_put_chunk(struct audit_chunk *chunk);
29105c7a9cbSRichard Guy Briggs extern bool audit_tree_match(struct audit_chunk *chunk,
29205c7a9cbSRichard Guy Briggs struct audit_tree *tree);
29305c7a9cbSRichard Guy Briggs extern int audit_make_tree(struct audit_krule *rule, char *pathname, u32 op);
29405c7a9cbSRichard Guy Briggs extern int audit_add_tree_rule(struct audit_krule *rule);
29505c7a9cbSRichard Guy Briggs extern int audit_remove_tree_rule(struct audit_krule *rule);
29605c7a9cbSRichard Guy Briggs extern void audit_trim_trees(void);
29705c7a9cbSRichard Guy Briggs extern int audit_tag_tree(char *old, char *new);
29805c7a9cbSRichard Guy Briggs extern const char *audit_tree_path(struct audit_tree *tree);
29905c7a9cbSRichard Guy Briggs extern void audit_put_tree(struct audit_tree *tree);
30005c7a9cbSRichard Guy Briggs extern void audit_kill_trees(struct audit_context *context);
30105c7a9cbSRichard Guy Briggs
302b48345aaSRichard Guy Briggs extern int audit_signal_info_syscall(struct task_struct *t);
30305c7a9cbSRichard Guy Briggs extern void audit_filter_inodes(struct task_struct *tsk,
30405c7a9cbSRichard Guy Briggs struct audit_context *ctx);
30505c7a9cbSRichard Guy Briggs extern struct list_head *audit_killed_trees(void);
30605c7a9cbSRichard Guy Briggs #else /* CONFIG_AUDITSYSCALL */
307cd108b5cSRichard Guy Briggs #define auditsc_get_stamp(c, t, s) 0
308d4ceb1d6SArnd Bergmann #define audit_put_watch(w) do { } while (0)
309d4ceb1d6SArnd Bergmann #define audit_get_watch(w) do { } while (0)
310939a67fcSEric Paris #define audit_to_watch(k, p, l, o) (-EINVAL)
311939a67fcSEric Paris #define audit_add_watch(k, l) (-EINVAL)
312939a67fcSEric Paris #define audit_remove_watch_rule(k) BUG()
313939a67fcSEric Paris #define audit_watch_path(w) ""
314939a67fcSEric Paris #define audit_watch_compare(w, i, d) 0
315939a67fcSEric Paris
3167f492942SRichard Guy Briggs #define audit_alloc_mark(k, p, l) (ERR_PTR(-EINVAL))
3177f492942SRichard Guy Briggs #define audit_mark_path(m) ""
318d4ceb1d6SArnd Bergmann #define audit_remove_mark(m) do { } while (0)
319d4ceb1d6SArnd Bergmann #define audit_remove_mark_rule(k) do { } while (0)
3207f492942SRichard Guy Briggs #define audit_mark_compare(m, i, d) 0
32134d99af5SRichard Guy Briggs #define audit_exe_compare(t, m) (-EINVAL)
32234d99af5SRichard Guy Briggs #define audit_dupe_exe(n, o) (-EINVAL)
323cfcad62cSEric Paris
32474c3cbe3SAl Viro #define audit_remove_tree_rule(rule) BUG()
32574c3cbe3SAl Viro #define audit_add_tree_rule(rule) -EINVAL
32674c3cbe3SAl Viro #define audit_make_tree(rule, str, op) -EINVAL
327d4ceb1d6SArnd Bergmann #define audit_trim_trees() do { } while (0)
328d4ceb1d6SArnd Bergmann #define audit_put_tree(tree) do { } while (0)
32974c3cbe3SAl Viro #define audit_tag_tree(old, new) -EINVAL
33074c3cbe3SAl Viro #define audit_tree_path(rule) "" /* never called */
3319e36a5d4SRichard Guy Briggs #define audit_kill_trees(context) BUG()
33205c7a9cbSRichard Guy Briggs
audit_signal_info_syscall(struct task_struct * t)333b48345aaSRichard Guy Briggs static inline int audit_signal_info_syscall(struct task_struct *t)
334b48345aaSRichard Guy Briggs {
335b48345aaSRichard Guy Briggs return 0;
336b48345aaSRichard Guy Briggs }
337b48345aaSRichard Guy Briggs
338bf983542SXiu Jianfeng #define audit_filter_inodes(t, c) do { } while (0)
33905c7a9cbSRichard Guy Briggs #endif /* CONFIG_AUDITSYSCALL */
34074c3cbe3SAl Viro
341e4c1a0d1SDerek Robson extern char *audit_unpack_string(void **bufp, size_t *remain, size_t len);
34274c3cbe3SAl Viro
34386b2efbeSRichard Guy Briggs extern int audit_filter(int msgtype, unsigned int listtype);
34486b2efbeSRichard Guy Briggs
345ce423631SPaul Moore extern void audit_ctl_lock(void);
346ce423631SPaul Moore extern void audit_ctl_unlock(void);
347d97e9938SMaYuming
348d97e9938SMaYuming #endif
349