]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - include/linux/audit.h
Merge tag 'powerpc-5.2-1' of ssh://gitolite.kernel.org/pub/scm/linux/kernel/git/power...
[mirror_ubuntu-focal-kernel.git] / include / linux / audit.h
1 /* audit.h -- Auditing support
2 *
3 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
4 * All Rights Reserved.
5 *
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.
10 *
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.
15 *
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
19 *
20 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
21 *
22 */
23 #ifndef _LINUX_AUDIT_H_
24 #define _LINUX_AUDIT_H_
25
26 #include <linux/sched.h>
27 #include <linux/ptrace.h>
28 #include <linux/namei.h> /* LOOKUP_* */
29 #include <uapi/linux/audit.h>
30
31 #define AUDIT_INO_UNSET ((unsigned long)-1)
32 #define AUDIT_DEV_UNSET ((dev_t)-1)
33
34 struct audit_sig_info {
35 uid_t uid;
36 pid_t pid;
37 char ctx[0];
38 };
39
40 struct audit_buffer;
41 struct audit_context;
42 struct inode;
43 struct netlink_skb_parms;
44 struct path;
45 struct linux_binprm;
46 struct mq_attr;
47 struct mqstat;
48 struct audit_watch;
49 struct audit_tree;
50 struct sk_buff;
51
52 struct audit_krule {
53 u32 pflags;
54 u32 flags;
55 u32 listnr;
56 u32 action;
57 u32 mask[AUDIT_BITMASK_SIZE];
58 u32 buflen; /* for data alloc on list rules */
59 u32 field_count;
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 */
69 u64 prio;
70 };
71
72 /* Flag to indicate legacy AUDIT_LOGINUID unset usage */
73 #define AUDIT_LOGINUID_LEGACY 0x1
74
75 struct audit_field {
76 u32 type;
77 union {
78 u32 val;
79 kuid_t uid;
80 kgid_t gid;
81 struct {
82 char *lsm_str;
83 void *lsm_rule;
84 };
85 };
86 u32 op;
87 };
88
89 enum audit_ntp_type {
90 AUDIT_NTP_OFFSET,
91 AUDIT_NTP_FREQ,
92 AUDIT_NTP_STATUS,
93 AUDIT_NTP_TAI,
94 AUDIT_NTP_TICK,
95 AUDIT_NTP_ADJUST,
96
97 AUDIT_NTP_NVALS /* count */
98 };
99
100 #ifdef CONFIG_AUDITSYSCALL
101 struct audit_ntp_val {
102 long long oldval, newval;
103 };
104
105 struct audit_ntp_data {
106 struct audit_ntp_val vals[AUDIT_NTP_NVALS];
107 };
108 #else
109 struct audit_ntp_data {};
110 #endif
111
112 extern int is_audit_feature_set(int which);
113
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[];
123
124 extern int audit_classify_compat_syscall(int abi, unsigned syscall);
125
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 */
132
133 /* maximized args number that audit_socketcall can process */
134 #define AUDITSC_ARGS 6
135
136 /* bit values for ->signal->audit_tty */
137 #define AUDIT_TTY_ENABLE BIT(0)
138 #define AUDIT_TTY_LOG_PASSWD BIT(1)
139
140 struct filename;
141
142 #define AUDIT_OFF 0
143 #define AUDIT_ON 1
144 #define AUDIT_LOCKED 2
145 #ifdef CONFIG_AUDIT
146 /* These are defined in audit.c */
147 /* Public API */
148 extern __printf(4, 5)
149 void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
150 const char *fmt, ...);
151
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,
157 size_t len);
158 extern void audit_log_n_hex(struct audit_buffer *ab,
159 const unsigned char *buf,
160 size_t len);
161 extern void audit_log_n_string(struct audit_buffer *ab,
162 const char *buf,
163 size_t n);
164 extern void audit_log_n_untrustedstring(struct audit_buffer *ab,
165 const char *string,
166 size_t n);
167 extern void audit_log_untrustedstring(struct audit_buffer *ab,
168 const char *string);
169 extern void audit_log_d_path(struct audit_buffer *ab,
170 const char *prefix,
171 const struct path *path);
172 extern void audit_log_key(struct audit_buffer *ab,
173 char *key);
174 extern void audit_log_link_denied(const char *operation);
175 extern void audit_log_lost(const char *message);
176
177 extern int audit_log_task_context(struct audit_buffer *ab);
178 extern void audit_log_task_info(struct audit_buffer *ab);
179
180 extern int audit_update_lsm_rules(void);
181
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);
185
186 extern int audit_set_loginuid(kuid_t loginuid);
187
188 static inline kuid_t audit_get_loginuid(struct task_struct *tsk)
189 {
190 return tsk->loginuid;
191 }
192
193 static inline unsigned int audit_get_sessionid(struct task_struct *tsk)
194 {
195 return tsk->sessionid;
196 }
197
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, ...)
203 { }
204 static inline struct audit_buffer *audit_log_start(struct audit_context *ctx,
205 gfp_t gfp_mask, int type)
206 {
207 return NULL;
208 }
209 static inline __printf(2, 3)
210 void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
211 { }
212 static inline void audit_log_end(struct audit_buffer *ab)
213 { }
214 static inline void audit_log_n_hex(struct audit_buffer *ab,
215 const unsigned char *buf, size_t len)
216 { }
217 static inline void audit_log_n_string(struct audit_buffer *ab,
218 const char *buf, size_t n)
219 { }
220 static inline void audit_log_n_untrustedstring(struct audit_buffer *ab,
221 const char *string, size_t n)
222 { }
223 static inline void audit_log_untrustedstring(struct audit_buffer *ab,
224 const char *string)
225 { }
226 static inline void audit_log_d_path(struct audit_buffer *ab,
227 const char *prefix,
228 const struct path *path)
229 { }
230 static inline void audit_log_key(struct audit_buffer *ab, char *key)
231 { }
232 static inline void audit_log_link_denied(const char *string)
233 { }
234 static inline int audit_log_task_context(struct audit_buffer *ab)
235 {
236 return 0;
237 }
238 static inline void audit_log_task_info(struct audit_buffer *ab)
239 { }
240
241 static inline kuid_t audit_get_loginuid(struct task_struct *tsk)
242 {
243 return INVALID_UID;
244 }
245
246 static inline unsigned int audit_get_sessionid(struct task_struct *tsk)
247 {
248 return AUDIT_SID_UNSET;
249 }
250
251 #define audit_enabled AUDIT_OFF
252 #endif /* CONFIG_AUDIT */
253
254 #ifdef CONFIG_AUDIT_COMPAT_GENERIC
255 #define audit_is_compat(arch) (!((arch) & __AUDIT_ARCH_64BIT))
256 #else
257 #define audit_is_compat(arch) false
258 #endif
259
260 #ifdef CONFIG_AUDITSYSCALL
261 #include <asm/syscall.h> /* for syscall_get_arch() */
262
263 /* These are defined in auditsc.c */
264 /* Public API */
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);
272
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,
277 unsigned int flags);
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);
286
287 static inline void audit_set_context(struct task_struct *task, struct audit_context *ctx)
288 {
289 task->audit_context = ctx;
290 }
291
292 static inline struct audit_context *audit_context(void)
293 {
294 return current->audit_context;
295 }
296
297 static inline bool audit_dummy_context(void)
298 {
299 void *p = audit_context();
300 return !p || *(int *)p;
301 }
302 static inline void audit_free(struct task_struct *task)
303 {
304 if (unlikely(task->audit_context))
305 __audit_free(task);
306 }
307 static inline void audit_syscall_entry(int major, unsigned long a0,
308 unsigned long a1, unsigned long a2,
309 unsigned long a3)
310 {
311 if (unlikely(audit_context()))
312 __audit_syscall_entry(major, a0, a1, a2, a3);
313 }
314 static inline void audit_syscall_exit(void *pt_regs)
315 {
316 if (unlikely(audit_context())) {
317 int success = is_syscall_success(pt_regs);
318 long return_code = regs_return_value(pt_regs);
319
320 __audit_syscall_exit(success, return_code);
321 }
322 }
323 static inline struct filename *audit_reusename(const __user char *name)
324 {
325 if (unlikely(!audit_dummy_context()))
326 return __audit_reusename(name);
327 return NULL;
328 }
329 static inline void audit_getname(struct filename *name)
330 {
331 if (unlikely(!audit_dummy_context()))
332 __audit_getname(name);
333 }
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;
339
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);
345 }
346 }
347 static inline void audit_file(struct file *file)
348 {
349 if (unlikely(!audit_dummy_context()))
350 __audit_file(file);
351 }
352 static inline void audit_inode_parent_hidden(struct filename *name,
353 const struct dentry *dentry)
354 {
355 if (unlikely(!audit_dummy_context()))
356 __audit_inode(name, dentry,
357 AUDIT_INODE_PARENT | AUDIT_INODE_HIDDEN);
358 }
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);
364 }
365 void audit_core_dumps(long signr);
366
367 static inline void audit_ptrace(struct task_struct *t)
368 {
369 if (unlikely(!audit_dummy_context()))
370 __audit_ptrace(t);
371 }
372
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);
393
394 static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
395 {
396 if (unlikely(!audit_dummy_context()))
397 __audit_ipc_obj(ipcp);
398 }
399 static inline void audit_fd_pair(int fd1, int fd2)
400 {
401 if (unlikely(!audit_dummy_context()))
402 __audit_fd_pair(fd1, fd2);
403 }
404 static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
405 {
406 if (unlikely(!audit_dummy_context()))
407 __audit_ipc_set_perm(qbytes, uid, gid, mode);
408 }
409 static inline void audit_bprm(struct linux_binprm *bprm)
410 {
411 if (unlikely(!audit_dummy_context()))
412 __audit_bprm(bprm);
413 }
414 static inline int audit_socketcall(int nargs, unsigned long *args)
415 {
416 if (unlikely(!audit_dummy_context()))
417 return __audit_socketcall(nargs, args);
418 return 0;
419 }
420
421 static inline int audit_socketcall_compat(int nargs, u32 *args)
422 {
423 unsigned long a[AUDITSC_ARGS];
424 int i;
425
426 if (audit_dummy_context())
427 return 0;
428
429 for (i = 0; i < nargs; i++)
430 a[i] = (unsigned long)args[i];
431 return __audit_socketcall(nargs, a);
432 }
433
434 static inline int audit_sockaddr(int len, void *addr)
435 {
436 if (unlikely(!audit_dummy_context()))
437 return __audit_sockaddr(len, addr);
438 return 0;
439 }
440 static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
441 {
442 if (unlikely(!audit_dummy_context()))
443 __audit_mq_open(oflag, mode, attr);
444 }
445 static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec64 *abs_timeout)
446 {
447 if (unlikely(!audit_dummy_context()))
448 __audit_mq_sendrecv(mqdes, msg_len, msg_prio, abs_timeout);
449 }
450 static inline void audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
451 {
452 if (unlikely(!audit_dummy_context()))
453 __audit_mq_notify(mqdes, notification);
454 }
455 static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
456 {
457 if (unlikely(!audit_dummy_context()))
458 __audit_mq_getsetattr(mqdes, mqstat);
459 }
460
461 static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
462 const struct cred *new,
463 const struct cred *old)
464 {
465 if (unlikely(!audit_dummy_context()))
466 return __audit_log_bprm_fcaps(bprm, new, old);
467 return 0;
468 }
469
470 static inline void audit_log_capset(const struct cred *new,
471 const struct cred *old)
472 {
473 if (unlikely(!audit_dummy_context()))
474 __audit_log_capset(new, old);
475 }
476
477 static inline void audit_mmap_fd(int fd, int flags)
478 {
479 if (unlikely(!audit_dummy_context()))
480 __audit_mmap_fd(fd, flags);
481 }
482
483 static inline void audit_log_kern_module(char *name)
484 {
485 if (!audit_dummy_context())
486 __audit_log_kern_module(name);
487 }
488
489 static inline void audit_fanotify(unsigned int response)
490 {
491 if (!audit_dummy_context())
492 __audit_fanotify(response);
493 }
494
495 static inline void audit_tk_injoffset(struct timespec64 offset)
496 {
497 /* ignore no-op events */
498 if (offset.tv_sec == 0 && offset.tv_nsec == 0)
499 return;
500
501 if (!audit_dummy_context())
502 __audit_tk_injoffset(offset);
503 }
504
505 static inline void audit_ntp_init(struct audit_ntp_data *ad)
506 {
507 memset(ad, 0, sizeof(*ad));
508 }
509
510 static inline void audit_ntp_set_old(struct audit_ntp_data *ad,
511 enum audit_ntp_type type, long long val)
512 {
513 ad->vals[type].oldval = val;
514 }
515
516 static inline void audit_ntp_set_new(struct audit_ntp_data *ad,
517 enum audit_ntp_type type, long long val)
518 {
519 ad->vals[type].newval = val;
520 }
521
522 static inline void audit_ntp_log(const struct audit_ntp_data *ad)
523 {
524 if (!audit_dummy_context())
525 __audit_ntp_log(ad);
526 }
527
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)
532 {
533 return 0;
534 }
535 static inline void audit_free(struct task_struct *task)
536 { }
537 static inline void audit_syscall_entry(int major, unsigned long a0,
538 unsigned long a1, unsigned long a2,
539 unsigned long a3)
540 { }
541 static inline void audit_syscall_exit(void *pt_regs)
542 { }
543 static inline bool audit_dummy_context(void)
544 {
545 return true;
546 }
547 static inline void audit_set_context(struct task_struct *task, struct audit_context *ctx)
548 { }
549 static inline struct audit_context *audit_context(void)
550 {
551 return NULL;
552 }
553 static inline struct filename *audit_reusename(const __user char *name)
554 {
555 return NULL;
556 }
557 static inline void audit_getname(struct filename *name)
558 { }
559 static inline void __audit_inode(struct filename *name,
560 const struct dentry *dentry,
561 unsigned int flags)
562 { }
563 static inline void __audit_inode_child(struct inode *parent,
564 const struct dentry *dentry,
565 const unsigned char type)
566 { }
567 static inline void audit_inode(struct filename *name,
568 const struct dentry *dentry,
569 unsigned int parent)
570 { }
571 static inline void audit_file(struct file *file)
572 {
573 }
574 static inline void audit_inode_parent_hidden(struct filename *name,
575 const struct dentry *dentry)
576 { }
577 static inline void audit_inode_child(struct inode *parent,
578 const struct dentry *dentry,
579 const unsigned char type)
580 { }
581 static inline void audit_core_dumps(long signr)
582 { }
583 static inline void audit_seccomp(unsigned long syscall, long signr, int code)
584 { }
585 static inline void audit_seccomp_actions_logged(const char *names,
586 const char *old_names, int res)
587 { }
588 static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
589 { }
590 static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid,
591 gid_t gid, umode_t mode)
592 { }
593 static inline void audit_bprm(struct linux_binprm *bprm)
594 { }
595 static inline int audit_socketcall(int nargs, unsigned long *args)
596 {
597 return 0;
598 }
599
600 static inline int audit_socketcall_compat(int nargs, u32 *args)
601 {
602 return 0;
603 }
604
605 static inline void audit_fd_pair(int fd1, int fd2)
606 { }
607 static inline int audit_sockaddr(int len, void *addr)
608 {
609 return 0;
610 }
611 static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
612 { }
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)
616 { }
617 static inline void audit_mq_notify(mqd_t mqdes,
618 const struct sigevent *notification)
619 { }
620 static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
621 { }
622 static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm,
623 const struct cred *new,
624 const struct cred *old)
625 {
626 return 0;
627 }
628 static inline void audit_log_capset(const struct cred *new,
629 const struct cred *old)
630 { }
631 static inline void audit_mmap_fd(int fd, int flags)
632 { }
633
634 static inline void audit_log_kern_module(char *name)
635 {
636 }
637
638 static inline void audit_fanotify(unsigned int response)
639 { }
640
641 static inline void audit_tk_injoffset(struct timespec64 offset)
642 { }
643
644 static inline void audit_ntp_init(struct audit_ntp_data *ad)
645 { }
646
647 static inline void audit_ntp_set_old(struct audit_ntp_data *ad,
648 enum audit_ntp_type type, long long val)
649 { }
650
651 static inline void audit_ntp_set_new(struct audit_ntp_data *ad,
652 enum audit_ntp_type type, long long val)
653 { }
654
655 static inline void audit_ntp_log(const struct audit_ntp_data *ad)
656 { }
657
658 static inline void audit_ptrace(struct task_struct *t)
659 { }
660 #define audit_n_rules 0
661 #define audit_signals 0
662 #endif /* CONFIG_AUDITSYSCALL */
663
664 static inline bool audit_loginuid_set(struct task_struct *tsk)
665 {
666 return uid_valid(audit_get_loginuid(tsk));
667 }
668
669 static inline void audit_log_string(struct audit_buffer *ab, const char *buf)
670 {
671 audit_log_n_string(ab, buf, strlen(buf));
672 }
673
674 #endif