1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* audit.h -- Auditing support
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
9 #ifndef _LINUX_AUDIT_H_
10 #define _LINUX_AUDIT_H_
12 #include <linux/sched.h>
13 #include <linux/ptrace.h>
14 #include <linux/audit_arch.h>
15 #include <uapi/linux/audit.h>
16 #include <uapi/linux/netfilter/nf_tables.h>
18 #define AUDIT_INO_UNSET ((unsigned long)-1)
19 #define AUDIT_DEV_UNSET ((dev_t)-1)
21 struct audit_sig_info {
30 struct netlink_skb_parms;
44 u32 mask[AUDIT_BITMASK_SIZE];
45 u32 buflen; /* for data alloc on list rules */
47 char *filterkey; /* ties events to rules */
48 struct audit_field *fields;
49 struct audit_field *arch_f; /* quick access to arch field */
50 struct audit_field *inode_f; /* quick access to an inode field */
51 struct audit_watch *watch; /* associated watch */
52 struct audit_tree *tree; /* associated watched tree */
53 struct audit_fsnotify_mark *exe;
54 struct list_head rlist; /* entry in audit_{watch,tree}.rules list */
55 struct list_head list; /* for AUDIT_LIST* purposes only */
59 /* Flag to indicate legacy AUDIT_LOGINUID unset usage */
60 #define AUDIT_LOGINUID_LEGACY 0x1
84 AUDIT_NTP_NVALS /* count */
87 #ifdef CONFIG_AUDITSYSCALL
88 struct audit_ntp_val {
89 long long oldval, newval;
92 struct audit_ntp_data {
93 struct audit_ntp_val vals[AUDIT_NTP_NVALS];
96 struct audit_ntp_data {};
100 AUDIT_XT_OP_REGISTER,
102 AUDIT_XT_OP_UNREGISTER,
103 AUDIT_NFT_OP_TABLE_REGISTER,
104 AUDIT_NFT_OP_TABLE_UNREGISTER,
105 AUDIT_NFT_OP_CHAIN_REGISTER,
106 AUDIT_NFT_OP_CHAIN_UNREGISTER,
107 AUDIT_NFT_OP_RULE_REGISTER,
108 AUDIT_NFT_OP_RULE_UNREGISTER,
109 AUDIT_NFT_OP_SET_REGISTER,
110 AUDIT_NFT_OP_SET_UNREGISTER,
111 AUDIT_NFT_OP_SETELEM_REGISTER,
112 AUDIT_NFT_OP_SETELEM_UNREGISTER,
113 AUDIT_NFT_OP_GEN_REGISTER,
114 AUDIT_NFT_OP_OBJ_REGISTER,
115 AUDIT_NFT_OP_OBJ_UNREGISTER,
116 AUDIT_NFT_OP_OBJ_RESET,
117 AUDIT_NFT_OP_FLOWTABLE_REGISTER,
118 AUDIT_NFT_OP_FLOWTABLE_UNREGISTER,
119 AUDIT_NFT_OP_INVALID,
122 extern int is_audit_feature_set(int which);
124 extern int __init audit_register_class(int class, unsigned *list);
125 extern int audit_classify_syscall(int abi, unsigned syscall);
126 extern int audit_classify_arch(int arch);
127 /* only for compat system calls */
128 extern unsigned compat_write_class[];
129 extern unsigned compat_read_class[];
130 extern unsigned compat_dir_class[];
131 extern unsigned compat_chattr_class[];
132 extern unsigned compat_signal_class[];
134 extern int audit_classify_compat_syscall(int abi, unsigned syscall);
136 /* audit_names->type values */
137 #define AUDIT_TYPE_UNKNOWN 0 /* we don't know yet */
138 #define AUDIT_TYPE_NORMAL 1 /* a "normal" audit record */
139 #define AUDIT_TYPE_PARENT 2 /* a parent audit record */
140 #define AUDIT_TYPE_CHILD_DELETE 3 /* a child being deleted */
141 #define AUDIT_TYPE_CHILD_CREATE 4 /* a child being created */
143 /* maximized args number that audit_socketcall can process */
144 #define AUDITSC_ARGS 6
146 /* bit values for ->signal->audit_tty */
147 #define AUDIT_TTY_ENABLE BIT(0)
148 #define AUDIT_TTY_LOG_PASSWD BIT(1)
154 #define AUDIT_LOCKED 2
156 /* These are defined in audit.c */
158 extern __printf(4, 5)
159 void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
160 const char *fmt, ...);
162 extern struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type);
163 extern __printf(2, 3)
164 void audit_log_format(struct audit_buffer *ab, const char *fmt, ...);
165 extern void audit_log_end(struct audit_buffer *ab);
166 extern bool audit_string_contains_control(const char *string,
168 extern void audit_log_n_hex(struct audit_buffer *ab,
169 const unsigned char *buf,
171 extern void audit_log_n_string(struct audit_buffer *ab,
174 extern void audit_log_n_untrustedstring(struct audit_buffer *ab,
177 extern void audit_log_untrustedstring(struct audit_buffer *ab,
179 extern void audit_log_d_path(struct audit_buffer *ab,
181 const struct path *path);
182 extern void audit_log_key(struct audit_buffer *ab,
184 extern void audit_log_path_denied(int type,
185 const char *operation);
186 extern void audit_log_lost(const char *message);
188 extern int audit_log_task_context(struct audit_buffer *ab);
189 extern void audit_log_task_info(struct audit_buffer *ab);
191 extern int audit_update_lsm_rules(void);
193 /* Private API (for audit.c only) */
194 extern int audit_rule_change(int type, int seq, void *data, size_t datasz);
195 extern int audit_list_rules_send(struct sk_buff *request_skb, int seq);
197 extern int audit_set_loginuid(kuid_t loginuid);
199 static inline kuid_t audit_get_loginuid(struct task_struct *tsk)
201 return tsk->loginuid;
204 static inline unsigned int audit_get_sessionid(struct task_struct *tsk)
206 return tsk->sessionid;
209 extern u32 audit_enabled;
211 extern int audit_signal_info(int sig, struct task_struct *t);
213 #else /* CONFIG_AUDIT */
214 static inline __printf(4, 5)
215 void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
216 const char *fmt, ...)
218 static inline struct audit_buffer *audit_log_start(struct audit_context *ctx,
219 gfp_t gfp_mask, int type)
223 static inline __printf(2, 3)
224 void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
226 static inline void audit_log_end(struct audit_buffer *ab)
228 static inline void audit_log_n_hex(struct audit_buffer *ab,
229 const unsigned char *buf, size_t len)
231 static inline void audit_log_n_string(struct audit_buffer *ab,
232 const char *buf, size_t n)
234 static inline void audit_log_n_untrustedstring(struct audit_buffer *ab,
235 const char *string, size_t n)
237 static inline void audit_log_untrustedstring(struct audit_buffer *ab,
240 static inline void audit_log_d_path(struct audit_buffer *ab,
242 const struct path *path)
244 static inline void audit_log_key(struct audit_buffer *ab, char *key)
246 static inline void audit_log_path_denied(int type, const char *operation)
248 static inline int audit_log_task_context(struct audit_buffer *ab)
252 static inline void audit_log_task_info(struct audit_buffer *ab)
255 static inline kuid_t audit_get_loginuid(struct task_struct *tsk)
260 static inline unsigned int audit_get_sessionid(struct task_struct *tsk)
262 return AUDIT_SID_UNSET;
265 #define audit_enabled AUDIT_OFF
267 static inline int audit_signal_info(int sig, struct task_struct *t)
272 #endif /* CONFIG_AUDIT */
274 #ifdef CONFIG_AUDIT_COMPAT_GENERIC
275 #define audit_is_compat(arch) (!((arch) & __AUDIT_ARCH_64BIT))
277 #define audit_is_compat(arch) false
280 #define AUDIT_INODE_PARENT 1 /* dentry represents the parent */
281 #define AUDIT_INODE_HIDDEN 2 /* audit record should be hidden */
282 #define AUDIT_INODE_NOEVAL 4 /* audit record incomplete */
284 #ifdef CONFIG_AUDITSYSCALL
285 #include <asm/syscall.h> /* for syscall_get_arch() */
287 /* These are defined in auditsc.c */
289 extern int audit_alloc(struct task_struct *task);
290 extern int audit_alloc_kernel(struct task_struct *task);
291 extern void __audit_free(struct task_struct *task);
292 extern void __audit_uring_entry(u8 op);
293 extern void __audit_uring_exit(int success, long code);
294 extern void __audit_syscall_entry(int major, unsigned long a0, unsigned long a1,
295 unsigned long a2, unsigned long a3);
296 extern void __audit_syscall_exit(int ret_success, long ret_value);
297 extern struct filename *__audit_reusename(const __user char *uptr);
298 extern void __audit_getname(struct filename *name);
299 extern void __audit_inode(struct filename *name, const struct dentry *dentry,
301 extern void __audit_file(const struct file *);
302 extern void __audit_inode_child(struct inode *parent,
303 const struct dentry *dentry,
304 const unsigned char type);
305 extern void audit_seccomp(unsigned long syscall, long signr, int code);
306 extern void audit_seccomp_actions_logged(const char *names,
307 const char *old_names, int res);
308 extern void __audit_ptrace(struct task_struct *t);
310 static inline void audit_set_context(struct task_struct *task, struct audit_context *ctx)
312 task->audit_context = ctx;
315 static inline struct audit_context *audit_context(void)
317 return current->audit_context;
320 static inline bool audit_dummy_context(void)
322 void *p = audit_context();
323 return !p || *(int *)p;
325 static inline void audit_free(struct task_struct *task)
327 if (unlikely(task->audit_context))
330 static inline void audit_uring_entry(u8 op)
333 * We intentionally check audit_context() before audit_enabled as most
334 * Linux systems (as of ~2021) rely on systemd which forces audit to
335 * be enabled regardless of the user's audit configuration.
337 if (unlikely(audit_context() && audit_enabled))
338 __audit_uring_entry(op);
340 static inline void audit_uring_exit(int success, long code)
342 if (unlikely(!audit_dummy_context()))
343 __audit_uring_exit(success, code);
345 static inline void audit_syscall_entry(int major, unsigned long a0,
346 unsigned long a1, unsigned long a2,
349 if (unlikely(audit_context()))
350 __audit_syscall_entry(major, a0, a1, a2, a3);
352 static inline void audit_syscall_exit(void *pt_regs)
354 if (unlikely(audit_context())) {
355 int success = is_syscall_success(pt_regs);
356 long return_code = regs_return_value(pt_regs);
358 __audit_syscall_exit(success, return_code);
361 static inline struct filename *audit_reusename(const __user char *name)
363 if (unlikely(!audit_dummy_context()))
364 return __audit_reusename(name);
367 static inline void audit_getname(struct filename *name)
369 if (unlikely(!audit_dummy_context()))
370 __audit_getname(name);
372 static inline void audit_inode(struct filename *name,
373 const struct dentry *dentry,
374 unsigned int aflags) {
375 if (unlikely(!audit_dummy_context()))
376 __audit_inode(name, dentry, aflags);
378 static inline void audit_file(struct file *file)
380 if (unlikely(!audit_dummy_context()))
383 static inline void audit_inode_parent_hidden(struct filename *name,
384 const struct dentry *dentry)
386 if (unlikely(!audit_dummy_context()))
387 __audit_inode(name, dentry,
388 AUDIT_INODE_PARENT | AUDIT_INODE_HIDDEN);
390 static inline void audit_inode_child(struct inode *parent,
391 const struct dentry *dentry,
392 const unsigned char type) {
393 if (unlikely(!audit_dummy_context()))
394 __audit_inode_child(parent, dentry, type);
396 void audit_core_dumps(long signr);
398 static inline void audit_ptrace(struct task_struct *t)
400 if (unlikely(!audit_dummy_context()))
404 /* Private API (for audit.c only) */
405 extern void __audit_ipc_obj(struct kern_ipc_perm *ipcp);
406 extern void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode);
407 extern void __audit_bprm(struct linux_binprm *bprm);
408 extern int __audit_socketcall(int nargs, unsigned long *args);
409 extern int __audit_sockaddr(int len, void *addr);
410 extern void __audit_fd_pair(int fd1, int fd2);
411 extern void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr);
412 extern void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec64 *abs_timeout);
413 extern void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification);
414 extern void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat);
415 extern int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
416 const struct cred *new,
417 const struct cred *old);
418 extern void __audit_log_capset(const struct cred *new, const struct cred *old);
419 extern void __audit_mmap_fd(int fd, int flags);
420 extern void __audit_openat2_how(struct open_how *how);
421 extern void __audit_log_kern_module(char *name);
422 extern void __audit_fanotify(unsigned int response);
423 extern void __audit_tk_injoffset(struct timespec64 offset);
424 extern void __audit_ntp_log(const struct audit_ntp_data *ad);
425 extern void __audit_log_nfcfg(const char *name, u8 af, unsigned int nentries,
426 enum audit_nfcfgop op, gfp_t gfp);
428 static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
430 if (unlikely(!audit_dummy_context()))
431 __audit_ipc_obj(ipcp);
433 static inline void audit_fd_pair(int fd1, int fd2)
435 if (unlikely(!audit_dummy_context()))
436 __audit_fd_pair(fd1, fd2);
438 static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
440 if (unlikely(!audit_dummy_context()))
441 __audit_ipc_set_perm(qbytes, uid, gid, mode);
443 static inline void audit_bprm(struct linux_binprm *bprm)
445 if (unlikely(!audit_dummy_context()))
448 static inline int audit_socketcall(int nargs, unsigned long *args)
450 if (unlikely(!audit_dummy_context()))
451 return __audit_socketcall(nargs, args);
455 static inline int audit_socketcall_compat(int nargs, u32 *args)
457 unsigned long a[AUDITSC_ARGS];
460 if (audit_dummy_context())
463 for (i = 0; i < nargs; i++)
464 a[i] = (unsigned long)args[i];
465 return __audit_socketcall(nargs, a);
468 static inline int audit_sockaddr(int len, void *addr)
470 if (unlikely(!audit_dummy_context()))
471 return __audit_sockaddr(len, addr);
474 static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
476 if (unlikely(!audit_dummy_context()))
477 __audit_mq_open(oflag, mode, attr);
479 static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec64 *abs_timeout)
481 if (unlikely(!audit_dummy_context()))
482 __audit_mq_sendrecv(mqdes, msg_len, msg_prio, abs_timeout);
484 static inline void audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
486 if (unlikely(!audit_dummy_context()))
487 __audit_mq_notify(mqdes, notification);
489 static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
491 if (unlikely(!audit_dummy_context()))
492 __audit_mq_getsetattr(mqdes, mqstat);
495 static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
496 const struct cred *new,
497 const struct cred *old)
499 if (unlikely(!audit_dummy_context()))
500 return __audit_log_bprm_fcaps(bprm, new, old);
504 static inline void audit_log_capset(const struct cred *new,
505 const struct cred *old)
507 if (unlikely(!audit_dummy_context()))
508 __audit_log_capset(new, old);
511 static inline void audit_mmap_fd(int fd, int flags)
513 if (unlikely(!audit_dummy_context()))
514 __audit_mmap_fd(fd, flags);
517 static inline void audit_openat2_how(struct open_how *how)
519 if (unlikely(!audit_dummy_context()))
520 __audit_openat2_how(how);
523 static inline void audit_log_kern_module(char *name)
525 if (!audit_dummy_context())
526 __audit_log_kern_module(name);
529 static inline void audit_fanotify(unsigned int response)
531 if (!audit_dummy_context())
532 __audit_fanotify(response);
535 static inline void audit_tk_injoffset(struct timespec64 offset)
537 /* ignore no-op events */
538 if (offset.tv_sec == 0 && offset.tv_nsec == 0)
541 if (!audit_dummy_context())
542 __audit_tk_injoffset(offset);
545 static inline void audit_ntp_init(struct audit_ntp_data *ad)
547 memset(ad, 0, sizeof(*ad));
550 static inline void audit_ntp_set_old(struct audit_ntp_data *ad,
551 enum audit_ntp_type type, long long val)
553 ad->vals[type].oldval = val;
556 static inline void audit_ntp_set_new(struct audit_ntp_data *ad,
557 enum audit_ntp_type type, long long val)
559 ad->vals[type].newval = val;
562 static inline void audit_ntp_log(const struct audit_ntp_data *ad)
564 if (!audit_dummy_context())
568 static inline void audit_log_nfcfg(const char *name, u8 af,
569 unsigned int nentries,
570 enum audit_nfcfgop op, gfp_t gfp)
573 __audit_log_nfcfg(name, af, nentries, op, gfp);
576 extern int audit_n_rules;
577 extern int audit_signals;
578 #else /* CONFIG_AUDITSYSCALL */
579 static inline int audit_alloc(struct task_struct *task)
583 static inline int audit_alloc_kernel(struct task_struct *task)
587 static inline void audit_free(struct task_struct *task)
589 static inline void audit_uring_entry(u8 op)
591 static inline void audit_uring_exit(int success, long code)
593 static inline void audit_syscall_entry(int major, unsigned long a0,
594 unsigned long a1, unsigned long a2,
597 static inline void audit_syscall_exit(void *pt_regs)
599 static inline bool audit_dummy_context(void)
603 static inline void audit_set_context(struct task_struct *task, struct audit_context *ctx)
605 static inline struct audit_context *audit_context(void)
609 static inline struct filename *audit_reusename(const __user char *name)
613 static inline void audit_getname(struct filename *name)
615 static inline void audit_inode(struct filename *name,
616 const struct dentry *dentry,
619 static inline void audit_file(struct file *file)
622 static inline void audit_inode_parent_hidden(struct filename *name,
623 const struct dentry *dentry)
625 static inline void audit_inode_child(struct inode *parent,
626 const struct dentry *dentry,
627 const unsigned char type)
629 static inline void audit_core_dumps(long signr)
631 static inline void audit_seccomp(unsigned long syscall, long signr, int code)
633 static inline void audit_seccomp_actions_logged(const char *names,
634 const char *old_names, int res)
636 static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
638 static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid,
639 gid_t gid, umode_t mode)
641 static inline void audit_bprm(struct linux_binprm *bprm)
643 static inline int audit_socketcall(int nargs, unsigned long *args)
648 static inline int audit_socketcall_compat(int nargs, u32 *args)
653 static inline void audit_fd_pair(int fd1, int fd2)
655 static inline int audit_sockaddr(int len, void *addr)
659 static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
661 static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len,
662 unsigned int msg_prio,
663 const struct timespec64 *abs_timeout)
665 static inline void audit_mq_notify(mqd_t mqdes,
666 const struct sigevent *notification)
668 static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
670 static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
671 const struct cred *new,
672 const struct cred *old)
676 static inline void audit_log_capset(const struct cred *new,
677 const struct cred *old)
679 static inline void audit_mmap_fd(int fd, int flags)
682 static inline void audit_openat2_how(struct open_how *how)
685 static inline void audit_log_kern_module(char *name)
689 static inline void audit_fanotify(unsigned int response)
692 static inline void audit_tk_injoffset(struct timespec64 offset)
695 static inline void audit_ntp_init(struct audit_ntp_data *ad)
698 static inline void audit_ntp_set_old(struct audit_ntp_data *ad,
699 enum audit_ntp_type type, long long val)
702 static inline void audit_ntp_set_new(struct audit_ntp_data *ad,
703 enum audit_ntp_type type, long long val)
706 static inline void audit_ntp_log(const struct audit_ntp_data *ad)
709 static inline void audit_ptrace(struct task_struct *t)
712 static inline void audit_log_nfcfg(const char *name, u8 af,
713 unsigned int nentries,
714 enum audit_nfcfgop op, gfp_t gfp)
717 #define audit_n_rules 0
718 #define audit_signals 0
719 #endif /* CONFIG_AUDITSYSCALL */
721 static inline bool audit_loginuid_set(struct task_struct *tsk)
723 return uid_valid(audit_get_loginuid(tsk));