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