1 /* audit.h -- Auditing support
3 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
23 #ifndef _LINUX_AUDIT_H_
24 #define _LINUX_AUDIT_H_
26 #include <linux/sched.h>
27 #include <linux/ptrace.h>
28 #include <linux/namei.h> /* LOOKUP_* */
29 #include <uapi/linux/audit.h>
31 #define AUDIT_INO_UNSET ((unsigned long)-1)
32 #define AUDIT_DEV_UNSET ((dev_t)-1)
34 struct audit_sig_info {
43 struct netlink_skb_parms;
57 u32 mask[AUDIT_BITMASK_SIZE];
58 u32 buflen; /* for data alloc on list rules */
60 char *filterkey; /* ties events to rules */
61 struct audit_field *fields;
62 struct audit_field *arch_f; /* quick access to arch field */
63 struct audit_field *inode_f; /* quick access to an inode field */
64 struct audit_watch *watch; /* associated watch */
65 struct audit_tree *tree; /* associated watched tree */
66 struct audit_fsnotify_mark *exe;
67 struct list_head rlist; /* entry in audit_{watch,tree}.rules list */
68 struct list_head list; /* for AUDIT_LIST* purposes only */
72 /* Flag to indicate legacy AUDIT_LOGINUID unset usage */
73 #define AUDIT_LOGINUID_LEGACY 0x1
97 AUDIT_NTP_NVALS /* count */
100 #ifdef CONFIG_AUDITSYSCALL
101 struct audit_ntp_val {
102 long long oldval, newval;
105 struct audit_ntp_data {
106 struct audit_ntp_val vals[AUDIT_NTP_NVALS];
109 struct audit_ntp_data {};
112 extern int is_audit_feature_set(int which);
114 extern int __init audit_register_class(int class, unsigned *list);
115 extern int audit_classify_syscall(int abi, unsigned syscall);
116 extern int audit_classify_arch(int arch);
117 /* only for compat system calls */
118 extern unsigned compat_write_class[];
119 extern unsigned compat_read_class[];
120 extern unsigned compat_dir_class[];
121 extern unsigned compat_chattr_class[];
122 extern unsigned compat_signal_class[];
124 extern int audit_classify_compat_syscall(int abi, unsigned syscall);
126 /* audit_names->type values */
127 #define AUDIT_TYPE_UNKNOWN 0 /* we don't know yet */
128 #define AUDIT_TYPE_NORMAL 1 /* a "normal" audit record */
129 #define AUDIT_TYPE_PARENT 2 /* a parent audit record */
130 #define AUDIT_TYPE_CHILD_DELETE 3 /* a child being deleted */
131 #define AUDIT_TYPE_CHILD_CREATE 4 /* a child being created */
133 /* maximized args number that audit_socketcall can process */
134 #define AUDITSC_ARGS 6
136 /* bit values for ->signal->audit_tty */
137 #define AUDIT_TTY_ENABLE BIT(0)
138 #define AUDIT_TTY_LOG_PASSWD BIT(1)
144 #define AUDIT_LOCKED 2
146 /* These are defined in audit.c */
148 extern __printf(4, 5)
149 void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
150 const char *fmt, ...);
152 extern struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type);
153 extern __printf(2, 3)
154 void audit_log_format(struct audit_buffer *ab, const char *fmt, ...);
155 extern void audit_log_end(struct audit_buffer *ab);
156 extern bool audit_string_contains_control(const char *string,
158 extern void audit_log_n_hex(struct audit_buffer *ab,
159 const unsigned char *buf,
161 extern void audit_log_n_string(struct audit_buffer *ab,
164 extern void audit_log_n_untrustedstring(struct audit_buffer *ab,
167 extern void audit_log_untrustedstring(struct audit_buffer *ab,
169 extern void audit_log_d_path(struct audit_buffer *ab,
171 const struct path *path);
172 extern void audit_log_key(struct audit_buffer *ab,
174 extern void audit_log_link_denied(const char *operation);
175 extern void audit_log_lost(const char *message);
177 extern int audit_log_task_context(struct audit_buffer *ab);
178 extern void audit_log_task_info(struct audit_buffer *ab);
180 extern int audit_update_lsm_rules(void);
182 /* Private API (for audit.c only) */
183 extern int audit_rule_change(int type, int seq, void *data, size_t datasz);
184 extern int audit_list_rules_send(struct sk_buff *request_skb, int seq);
186 extern int audit_set_loginuid(kuid_t loginuid);
188 static inline kuid_t audit_get_loginuid(struct task_struct *tsk)
190 return tsk->loginuid;
193 static inline unsigned int audit_get_sessionid(struct task_struct *tsk)
195 return tsk->sessionid;
198 extern u32 audit_enabled;
199 #else /* CONFIG_AUDIT */
200 static inline __printf(4, 5)
201 void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
202 const char *fmt, ...)
204 static inline struct audit_buffer *audit_log_start(struct audit_context *ctx,
205 gfp_t gfp_mask, int type)
209 static inline __printf(2, 3)
210 void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
212 static inline void audit_log_end(struct audit_buffer *ab)
214 static inline void audit_log_n_hex(struct audit_buffer *ab,
215 const unsigned char *buf, size_t len)
217 static inline void audit_log_n_string(struct audit_buffer *ab,
218 const char *buf, size_t n)
220 static inline void audit_log_n_untrustedstring(struct audit_buffer *ab,
221 const char *string, size_t n)
223 static inline void audit_log_untrustedstring(struct audit_buffer *ab,
226 static inline void audit_log_d_path(struct audit_buffer *ab,
228 const struct path *path)
230 static inline void audit_log_key(struct audit_buffer *ab, char *key)
232 static inline void audit_log_link_denied(const char *string)
234 static inline int audit_log_task_context(struct audit_buffer *ab)
238 static inline void audit_log_task_info(struct audit_buffer *ab)
241 static inline kuid_t audit_get_loginuid(struct task_struct *tsk)
246 static inline unsigned int audit_get_sessionid(struct task_struct *tsk)
248 return AUDIT_SID_UNSET;
251 #define audit_enabled AUDIT_OFF
252 #endif /* CONFIG_AUDIT */
254 #ifdef CONFIG_AUDIT_COMPAT_GENERIC
255 #define audit_is_compat(arch) (!((arch) & __AUDIT_ARCH_64BIT))
257 #define audit_is_compat(arch) false
260 #ifdef CONFIG_AUDITSYSCALL
261 #include <asm/syscall.h> /* for syscall_get_arch() */
263 /* These are defined in auditsc.c */
265 extern int audit_alloc(struct task_struct *task);
266 extern void __audit_free(struct task_struct *task);
267 extern void __audit_syscall_entry(int major, unsigned long a0, unsigned long a1,
268 unsigned long a2, unsigned long a3);
269 extern void __audit_syscall_exit(int ret_success, long ret_value);
270 extern struct filename *__audit_reusename(const __user char *uptr);
271 extern void __audit_getname(struct filename *name);
273 #define AUDIT_INODE_PARENT 1 /* dentry represents the parent */
274 #define AUDIT_INODE_HIDDEN 2 /* audit record should be hidden */
275 #define AUDIT_INODE_NOEVAL 4 /* audit record incomplete */
276 extern void __audit_inode(struct filename *name, const struct dentry *dentry,
278 extern void __audit_file(const struct file *);
279 extern void __audit_inode_child(struct inode *parent,
280 const struct dentry *dentry,
281 const unsigned char type);
282 extern void audit_seccomp(unsigned long syscall, long signr, int code);
283 extern void audit_seccomp_actions_logged(const char *names,
284 const char *old_names, int res);
285 extern void __audit_ptrace(struct task_struct *t);
287 static inline void audit_set_context(struct task_struct *task, struct audit_context *ctx)
289 task->audit_context = ctx;
292 static inline struct audit_context *audit_context(void)
294 return current->audit_context;
297 static inline bool audit_dummy_context(void)
299 void *p = audit_context();
300 return !p || *(int *)p;
302 static inline void audit_free(struct task_struct *task)
304 if (unlikely(task->audit_context))
307 static inline void audit_syscall_entry(int major, unsigned long a0,
308 unsigned long a1, unsigned long a2,
311 if (unlikely(audit_context()))
312 __audit_syscall_entry(major, a0, a1, a2, a3);
314 static inline void audit_syscall_exit(void *pt_regs)
316 if (unlikely(audit_context())) {
317 int success = is_syscall_success(pt_regs);
318 long return_code = regs_return_value(pt_regs);
320 __audit_syscall_exit(success, return_code);
323 static inline struct filename *audit_reusename(const __user char *name)
325 if (unlikely(!audit_dummy_context()))
326 return __audit_reusename(name);
329 static inline void audit_getname(struct filename *name)
331 if (unlikely(!audit_dummy_context()))
332 __audit_getname(name);
334 static inline void audit_inode(struct filename *name,
335 const struct dentry *dentry,
336 unsigned int flags) {
337 if (unlikely(!audit_dummy_context())) {
338 unsigned int aflags = 0;
340 if (flags & LOOKUP_PARENT)
341 aflags |= AUDIT_INODE_PARENT;
342 if (flags & LOOKUP_NO_EVAL)
343 aflags |= AUDIT_INODE_NOEVAL;
344 __audit_inode(name, dentry, aflags);
347 static inline void audit_file(struct file *file)
349 if (unlikely(!audit_dummy_context()))
352 static inline void audit_inode_parent_hidden(struct filename *name,
353 const struct dentry *dentry)
355 if (unlikely(!audit_dummy_context()))
356 __audit_inode(name, dentry,
357 AUDIT_INODE_PARENT | AUDIT_INODE_HIDDEN);
359 static inline void audit_inode_child(struct inode *parent,
360 const struct dentry *dentry,
361 const unsigned char type) {
362 if (unlikely(!audit_dummy_context()))
363 __audit_inode_child(parent, dentry, type);
365 void audit_core_dumps(long signr);
367 static inline void audit_ptrace(struct task_struct *t)
369 if (unlikely(!audit_dummy_context()))
373 /* Private API (for audit.c only) */
374 extern void __audit_ipc_obj(struct kern_ipc_perm *ipcp);
375 extern void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode);
376 extern void __audit_bprm(struct linux_binprm *bprm);
377 extern int __audit_socketcall(int nargs, unsigned long *args);
378 extern int __audit_sockaddr(int len, void *addr);
379 extern void __audit_fd_pair(int fd1, int fd2);
380 extern void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr);
381 extern void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec64 *abs_timeout);
382 extern void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification);
383 extern void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat);
384 extern int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
385 const struct cred *new,
386 const struct cred *old);
387 extern void __audit_log_capset(const struct cred *new, const struct cred *old);
388 extern void __audit_mmap_fd(int fd, int flags);
389 extern void __audit_log_kern_module(char *name);
390 extern void __audit_fanotify(unsigned int response);
391 extern void __audit_tk_injoffset(struct timespec64 offset);
392 extern void __audit_ntp_log(const struct audit_ntp_data *ad);
394 static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
396 if (unlikely(!audit_dummy_context()))
397 __audit_ipc_obj(ipcp);
399 static inline void audit_fd_pair(int fd1, int fd2)
401 if (unlikely(!audit_dummy_context()))
402 __audit_fd_pair(fd1, fd2);
404 static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
406 if (unlikely(!audit_dummy_context()))
407 __audit_ipc_set_perm(qbytes, uid, gid, mode);
409 static inline void audit_bprm(struct linux_binprm *bprm)
411 if (unlikely(!audit_dummy_context()))
414 static inline int audit_socketcall(int nargs, unsigned long *args)
416 if (unlikely(!audit_dummy_context()))
417 return __audit_socketcall(nargs, args);
421 static inline int audit_socketcall_compat(int nargs, u32 *args)
423 unsigned long a[AUDITSC_ARGS];
426 if (audit_dummy_context())
429 for (i = 0; i < nargs; i++)
430 a[i] = (unsigned long)args[i];
431 return __audit_socketcall(nargs, a);
434 static inline int audit_sockaddr(int len, void *addr)
436 if (unlikely(!audit_dummy_context()))
437 return __audit_sockaddr(len, addr);
440 static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
442 if (unlikely(!audit_dummy_context()))
443 __audit_mq_open(oflag, mode, attr);
445 static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec64 *abs_timeout)
447 if (unlikely(!audit_dummy_context()))
448 __audit_mq_sendrecv(mqdes, msg_len, msg_prio, abs_timeout);
450 static inline void audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
452 if (unlikely(!audit_dummy_context()))
453 __audit_mq_notify(mqdes, notification);
455 static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
457 if (unlikely(!audit_dummy_context()))
458 __audit_mq_getsetattr(mqdes, mqstat);
461 static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
462 const struct cred *new,
463 const struct cred *old)
465 if (unlikely(!audit_dummy_context()))
466 return __audit_log_bprm_fcaps(bprm, new, old);
470 static inline void audit_log_capset(const struct cred *new,
471 const struct cred *old)
473 if (unlikely(!audit_dummy_context()))
474 __audit_log_capset(new, old);
477 static inline void audit_mmap_fd(int fd, int flags)
479 if (unlikely(!audit_dummy_context()))
480 __audit_mmap_fd(fd, flags);
483 static inline void audit_log_kern_module(char *name)
485 if (!audit_dummy_context())
486 __audit_log_kern_module(name);
489 static inline void audit_fanotify(unsigned int response)
491 if (!audit_dummy_context())
492 __audit_fanotify(response);
495 static inline void audit_tk_injoffset(struct timespec64 offset)
497 /* ignore no-op events */
498 if (offset.tv_sec == 0 && offset.tv_nsec == 0)
501 if (!audit_dummy_context())
502 __audit_tk_injoffset(offset);
505 static inline void audit_ntp_init(struct audit_ntp_data *ad)
507 memset(ad, 0, sizeof(*ad));
510 static inline void audit_ntp_set_old(struct audit_ntp_data *ad,
511 enum audit_ntp_type type, long long val)
513 ad->vals[type].oldval = val;
516 static inline void audit_ntp_set_new(struct audit_ntp_data *ad,
517 enum audit_ntp_type type, long long val)
519 ad->vals[type].newval = val;
522 static inline void audit_ntp_log(const struct audit_ntp_data *ad)
524 if (!audit_dummy_context())
528 extern int audit_n_rules;
529 extern int audit_signals;
530 #else /* CONFIG_AUDITSYSCALL */
531 static inline int audit_alloc(struct task_struct *task)
535 static inline void audit_free(struct task_struct *task)
537 static inline void audit_syscall_entry(int major, unsigned long a0,
538 unsigned long a1, unsigned long a2,
541 static inline void audit_syscall_exit(void *pt_regs)
543 static inline bool audit_dummy_context(void)
547 static inline void audit_set_context(struct task_struct *task, struct audit_context *ctx)
549 static inline struct audit_context *audit_context(void)
553 static inline struct filename *audit_reusename(const __user char *name)
557 static inline void audit_getname(struct filename *name)
559 static inline void __audit_inode(struct filename *name,
560 const struct dentry *dentry,
563 static inline void __audit_inode_child(struct inode *parent,
564 const struct dentry *dentry,
565 const unsigned char type)
567 static inline void audit_inode(struct filename *name,
568 const struct dentry *dentry,
571 static inline void audit_file(struct file *file)
574 static inline void audit_inode_parent_hidden(struct filename *name,
575 const struct dentry *dentry)
577 static inline void audit_inode_child(struct inode *parent,
578 const struct dentry *dentry,
579 const unsigned char type)
581 static inline void audit_core_dumps(long signr)
583 static inline void audit_seccomp(unsigned long syscall, long signr, int code)
585 static inline void audit_seccomp_actions_logged(const char *names,
586 const char *old_names, int res)
588 static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
590 static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid,
591 gid_t gid, umode_t mode)
593 static inline void audit_bprm(struct linux_binprm *bprm)
595 static inline int audit_socketcall(int nargs, unsigned long *args)
600 static inline int audit_socketcall_compat(int nargs, u32 *args)
605 static inline void audit_fd_pair(int fd1, int fd2)
607 static inline int audit_sockaddr(int len, void *addr)
611 static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
613 static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len,
614 unsigned int msg_prio,
615 const struct timespec64 *abs_timeout)
617 static inline void audit_mq_notify(mqd_t mqdes,
618 const struct sigevent *notification)
620 static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
622 static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
623 const struct cred *new,
624 const struct cred *old)
628 static inline void audit_log_capset(const struct cred *new,
629 const struct cred *old)
631 static inline void audit_mmap_fd(int fd, int flags)
634 static inline void audit_log_kern_module(char *name)
638 static inline void audit_fanotify(unsigned int response)
641 static inline void audit_tk_injoffset(struct timespec64 offset)
644 static inline void audit_ntp_init(struct audit_ntp_data *ad)
647 static inline void audit_ntp_set_old(struct audit_ntp_data *ad,
648 enum audit_ntp_type type, long long val)
651 static inline void audit_ntp_set_new(struct audit_ntp_data *ad,
652 enum audit_ntp_type type, long long val)
655 static inline void audit_ntp_log(const struct audit_ntp_data *ad)
658 static inline void audit_ptrace(struct task_struct *t)
660 #define audit_n_rules 0
661 #define audit_signals 0
662 #endif /* CONFIG_AUDITSYSCALL */
664 static inline bool audit_loginuid_set(struct task_struct *tsk)
666 return uid_valid(audit_get_loginuid(tsk));
669 static inline void audit_log_string(struct audit_buffer *ab, const char *buf)
671 audit_log_n_string(ab, buf, strlen(buf));