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 enum audit_nfcfgop { 98 AUDIT_XT_OP_REGISTER, 99 AUDIT_XT_OP_REPLACE, 100 }; 101 102 extern int is_audit_feature_set(int which); 103 104 extern int __init audit_register_class(int class, unsigned *list); 105 extern int audit_classify_syscall(int abi, unsigned syscall); 106 extern int audit_classify_arch(int arch); 107 /* only for compat system calls */ 108 extern unsigned compat_write_class[]; 109 extern unsigned compat_read_class[]; 110 extern unsigned compat_dir_class[]; 111 extern unsigned compat_chattr_class[]; 112 extern unsigned compat_signal_class[]; 113 114 extern int audit_classify_compat_syscall(int abi, unsigned syscall); 115 116 /* audit_names->type values */ 117 #define AUDIT_TYPE_UNKNOWN 0 /* we don't know yet */ 118 #define AUDIT_TYPE_NORMAL 1 /* a "normal" audit record */ 119 #define AUDIT_TYPE_PARENT 2 /* a parent audit record */ 120 #define AUDIT_TYPE_CHILD_DELETE 3 /* a child being deleted */ 121 #define AUDIT_TYPE_CHILD_CREATE 4 /* a child being created */ 122 123 /* maximized args number that audit_socketcall can process */ 124 #define AUDITSC_ARGS 6 125 126 /* bit values for ->signal->audit_tty */ 127 #define AUDIT_TTY_ENABLE BIT(0) 128 #define AUDIT_TTY_LOG_PASSWD BIT(1) 129 130 struct filename; 131 132 #define AUDIT_OFF 0 133 #define AUDIT_ON 1 134 #define AUDIT_LOCKED 2 135 #ifdef CONFIG_AUDIT 136 /* These are defined in audit.c */ 137 /* Public API */ 138 extern __printf(4, 5) 139 void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type, 140 const char *fmt, ...); 141 142 extern struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type); 143 extern __printf(2, 3) 144 void audit_log_format(struct audit_buffer *ab, const char *fmt, ...); 145 extern void audit_log_end(struct audit_buffer *ab); 146 extern bool audit_string_contains_control(const char *string, 147 size_t len); 148 extern void audit_log_n_hex(struct audit_buffer *ab, 149 const unsigned char *buf, 150 size_t len); 151 extern void audit_log_n_string(struct audit_buffer *ab, 152 const char *buf, 153 size_t n); 154 extern void audit_log_n_untrustedstring(struct audit_buffer *ab, 155 const char *string, 156 size_t n); 157 extern void audit_log_untrustedstring(struct audit_buffer *ab, 158 const char *string); 159 extern void audit_log_d_path(struct audit_buffer *ab, 160 const char *prefix, 161 const struct path *path); 162 extern void audit_log_key(struct audit_buffer *ab, 163 char *key); 164 extern void audit_log_path_denied(int type, 165 const char *operation); 166 extern void audit_log_lost(const char *message); 167 168 extern int audit_log_task_context(struct audit_buffer *ab); 169 extern void audit_log_task_info(struct audit_buffer *ab); 170 171 extern int audit_update_lsm_rules(void); 172 173 /* Private API (for audit.c only) */ 174 extern int audit_rule_change(int type, int seq, void *data, size_t datasz); 175 extern int audit_list_rules_send(struct sk_buff *request_skb, int seq); 176 177 extern int audit_set_loginuid(kuid_t loginuid); 178 179 static inline kuid_t audit_get_loginuid(struct task_struct *tsk) 180 { 181 return tsk->loginuid; 182 } 183 184 static inline unsigned int audit_get_sessionid(struct task_struct *tsk) 185 { 186 return tsk->sessionid; 187 } 188 189 extern u32 audit_enabled; 190 191 extern int audit_signal_info(int sig, struct task_struct *t); 192 193 #else /* CONFIG_AUDIT */ 194 static inline __printf(4, 5) 195 void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type, 196 const char *fmt, ...) 197 { } 198 static inline struct audit_buffer *audit_log_start(struct audit_context *ctx, 199 gfp_t gfp_mask, int type) 200 { 201 return NULL; 202 } 203 static inline __printf(2, 3) 204 void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) 205 { } 206 static inline void audit_log_end(struct audit_buffer *ab) 207 { } 208 static inline void audit_log_n_hex(struct audit_buffer *ab, 209 const unsigned char *buf, size_t len) 210 { } 211 static inline void audit_log_n_string(struct audit_buffer *ab, 212 const char *buf, size_t n) 213 { } 214 static inline void audit_log_n_untrustedstring(struct audit_buffer *ab, 215 const char *string, size_t n) 216 { } 217 static inline void audit_log_untrustedstring(struct audit_buffer *ab, 218 const char *string) 219 { } 220 static inline void audit_log_d_path(struct audit_buffer *ab, 221 const char *prefix, 222 const struct path *path) 223 { } 224 static inline void audit_log_key(struct audit_buffer *ab, char *key) 225 { } 226 static inline void audit_log_path_denied(int type, const char *operation) 227 { } 228 static inline int audit_log_task_context(struct audit_buffer *ab) 229 { 230 return 0; 231 } 232 static inline void audit_log_task_info(struct audit_buffer *ab) 233 { } 234 235 static inline kuid_t audit_get_loginuid(struct task_struct *tsk) 236 { 237 return INVALID_UID; 238 } 239 240 static inline unsigned int audit_get_sessionid(struct task_struct *tsk) 241 { 242 return AUDIT_SID_UNSET; 243 } 244 245 #define audit_enabled AUDIT_OFF 246 247 static inline int audit_signal_info(int sig, struct task_struct *t) 248 { 249 return 0; 250 } 251 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 #define AUDIT_INODE_PARENT 1 /* dentry represents the parent */ 261 #define AUDIT_INODE_HIDDEN 2 /* audit record should be hidden */ 262 #define AUDIT_INODE_NOEVAL 4 /* audit record incomplete */ 263 264 #ifdef CONFIG_AUDITSYSCALL 265 #include <asm/syscall.h> /* for syscall_get_arch() */ 266 267 /* These are defined in auditsc.c */ 268 /* Public API */ 269 extern int audit_alloc(struct task_struct *task); 270 extern void __audit_free(struct task_struct *task); 271 extern void __audit_syscall_entry(int major, unsigned long a0, unsigned long a1, 272 unsigned long a2, unsigned long a3); 273 extern void __audit_syscall_exit(int ret_success, long ret_value); 274 extern struct filename *__audit_reusename(const __user char *uptr); 275 extern void __audit_getname(struct filename *name); 276 277 extern void __audit_inode(struct filename *name, const struct dentry *dentry, 278 unsigned int flags); 279 extern void __audit_file(const struct file *); 280 extern void __audit_inode_child(struct inode *parent, 281 const struct dentry *dentry, 282 const unsigned char type); 283 extern void audit_seccomp(unsigned long syscall, long signr, int code); 284 extern void audit_seccomp_actions_logged(const char *names, 285 const char *old_names, int res); 286 extern void __audit_ptrace(struct task_struct *t); 287 288 static inline void audit_set_context(struct task_struct *task, struct audit_context *ctx) 289 { 290 task->audit_context = ctx; 291 } 292 293 static inline struct audit_context *audit_context(void) 294 { 295 return current->audit_context; 296 } 297 298 static inline bool audit_dummy_context(void) 299 { 300 void *p = audit_context(); 301 return !p || *(int *)p; 302 } 303 static inline void audit_free(struct task_struct *task) 304 { 305 if (unlikely(task->audit_context)) 306 __audit_free(task); 307 } 308 static inline void audit_syscall_entry(int major, unsigned long a0, 309 unsigned long a1, unsigned long a2, 310 unsigned long a3) 311 { 312 if (unlikely(audit_context())) 313 __audit_syscall_entry(major, a0, a1, a2, a3); 314 } 315 static inline void audit_syscall_exit(void *pt_regs) 316 { 317 if (unlikely(audit_context())) { 318 int success = is_syscall_success(pt_regs); 319 long return_code = regs_return_value(pt_regs); 320 321 __audit_syscall_exit(success, return_code); 322 } 323 } 324 static inline struct filename *audit_reusename(const __user char *name) 325 { 326 if (unlikely(!audit_dummy_context())) 327 return __audit_reusename(name); 328 return NULL; 329 } 330 static inline void audit_getname(struct filename *name) 331 { 332 if (unlikely(!audit_dummy_context())) 333 __audit_getname(name); 334 } 335 static inline void audit_inode(struct filename *name, 336 const struct dentry *dentry, 337 unsigned int aflags) { 338 if (unlikely(!audit_dummy_context())) 339 __audit_inode(name, dentry, aflags); 340 } 341 static inline void audit_file(struct file *file) 342 { 343 if (unlikely(!audit_dummy_context())) 344 __audit_file(file); 345 } 346 static inline void audit_inode_parent_hidden(struct filename *name, 347 const struct dentry *dentry) 348 { 349 if (unlikely(!audit_dummy_context())) 350 __audit_inode(name, dentry, 351 AUDIT_INODE_PARENT | AUDIT_INODE_HIDDEN); 352 } 353 static inline void audit_inode_child(struct inode *parent, 354 const struct dentry *dentry, 355 const unsigned char type) { 356 if (unlikely(!audit_dummy_context())) 357 __audit_inode_child(parent, dentry, type); 358 } 359 void audit_core_dumps(long signr); 360 361 static inline void audit_ptrace(struct task_struct *t) 362 { 363 if (unlikely(!audit_dummy_context())) 364 __audit_ptrace(t); 365 } 366 367 /* Private API (for audit.c only) */ 368 extern void __audit_ipc_obj(struct kern_ipc_perm *ipcp); 369 extern void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode); 370 extern void __audit_bprm(struct linux_binprm *bprm); 371 extern int __audit_socketcall(int nargs, unsigned long *args); 372 extern int __audit_sockaddr(int len, void *addr); 373 extern void __audit_fd_pair(int fd1, int fd2); 374 extern void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr); 375 extern void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec64 *abs_timeout); 376 extern void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification); 377 extern void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat); 378 extern int __audit_log_bprm_fcaps(struct linux_binprm *bprm, 379 const struct cred *new, 380 const struct cred *old); 381 extern void __audit_log_capset(const struct cred *new, const struct cred *old); 382 extern void __audit_mmap_fd(int fd, int flags); 383 extern void __audit_log_kern_module(char *name); 384 extern void __audit_fanotify(unsigned int response); 385 extern void __audit_tk_injoffset(struct timespec64 offset); 386 extern void __audit_ntp_log(const struct audit_ntp_data *ad); 387 extern void __audit_log_nfcfg(const char *name, u8 af, unsigned int nentries, 388 enum audit_nfcfgop op); 389 390 static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp) 391 { 392 if (unlikely(!audit_dummy_context())) 393 __audit_ipc_obj(ipcp); 394 } 395 static inline void audit_fd_pair(int fd1, int fd2) 396 { 397 if (unlikely(!audit_dummy_context())) 398 __audit_fd_pair(fd1, fd2); 399 } 400 static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode) 401 { 402 if (unlikely(!audit_dummy_context())) 403 __audit_ipc_set_perm(qbytes, uid, gid, mode); 404 } 405 static inline void audit_bprm(struct linux_binprm *bprm) 406 { 407 if (unlikely(!audit_dummy_context())) 408 __audit_bprm(bprm); 409 } 410 static inline int audit_socketcall(int nargs, unsigned long *args) 411 { 412 if (unlikely(!audit_dummy_context())) 413 return __audit_socketcall(nargs, args); 414 return 0; 415 } 416 417 static inline int audit_socketcall_compat(int nargs, u32 *args) 418 { 419 unsigned long a[AUDITSC_ARGS]; 420 int i; 421 422 if (audit_dummy_context()) 423 return 0; 424 425 for (i = 0; i < nargs; i++) 426 a[i] = (unsigned long)args[i]; 427 return __audit_socketcall(nargs, a); 428 } 429 430 static inline int audit_sockaddr(int len, void *addr) 431 { 432 if (unlikely(!audit_dummy_context())) 433 return __audit_sockaddr(len, addr); 434 return 0; 435 } 436 static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr) 437 { 438 if (unlikely(!audit_dummy_context())) 439 __audit_mq_open(oflag, mode, attr); 440 } 441 static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, const struct timespec64 *abs_timeout) 442 { 443 if (unlikely(!audit_dummy_context())) 444 __audit_mq_sendrecv(mqdes, msg_len, msg_prio, abs_timeout); 445 } 446 static inline void audit_mq_notify(mqd_t mqdes, const struct sigevent *notification) 447 { 448 if (unlikely(!audit_dummy_context())) 449 __audit_mq_notify(mqdes, notification); 450 } 451 static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat) 452 { 453 if (unlikely(!audit_dummy_context())) 454 __audit_mq_getsetattr(mqdes, mqstat); 455 } 456 457 static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm, 458 const struct cred *new, 459 const struct cred *old) 460 { 461 if (unlikely(!audit_dummy_context())) 462 return __audit_log_bprm_fcaps(bprm, new, old); 463 return 0; 464 } 465 466 static inline void audit_log_capset(const struct cred *new, 467 const struct cred *old) 468 { 469 if (unlikely(!audit_dummy_context())) 470 __audit_log_capset(new, old); 471 } 472 473 static inline void audit_mmap_fd(int fd, int flags) 474 { 475 if (unlikely(!audit_dummy_context())) 476 __audit_mmap_fd(fd, flags); 477 } 478 479 static inline void audit_log_kern_module(char *name) 480 { 481 if (!audit_dummy_context()) 482 __audit_log_kern_module(name); 483 } 484 485 static inline void audit_fanotify(unsigned int response) 486 { 487 if (!audit_dummy_context()) 488 __audit_fanotify(response); 489 } 490 491 static inline void audit_tk_injoffset(struct timespec64 offset) 492 { 493 /* ignore no-op events */ 494 if (offset.tv_sec == 0 && offset.tv_nsec == 0) 495 return; 496 497 if (!audit_dummy_context()) 498 __audit_tk_injoffset(offset); 499 } 500 501 static inline void audit_ntp_init(struct audit_ntp_data *ad) 502 { 503 memset(ad, 0, sizeof(*ad)); 504 } 505 506 static inline void audit_ntp_set_old(struct audit_ntp_data *ad, 507 enum audit_ntp_type type, long long val) 508 { 509 ad->vals[type].oldval = val; 510 } 511 512 static inline void audit_ntp_set_new(struct audit_ntp_data *ad, 513 enum audit_ntp_type type, long long val) 514 { 515 ad->vals[type].newval = val; 516 } 517 518 static inline void audit_ntp_log(const struct audit_ntp_data *ad) 519 { 520 if (!audit_dummy_context()) 521 __audit_ntp_log(ad); 522 } 523 524 static inline void audit_log_nfcfg(const char *name, u8 af, 525 unsigned int nentries, 526 enum audit_nfcfgop op) 527 { 528 if (audit_enabled) 529 __audit_log_nfcfg(name, af, nentries, op); 530 } 531 532 extern int audit_n_rules; 533 extern int audit_signals; 534 #else /* CONFIG_AUDITSYSCALL */ 535 static inline int audit_alloc(struct task_struct *task) 536 { 537 return 0; 538 } 539 static inline void audit_free(struct task_struct *task) 540 { } 541 static inline void audit_syscall_entry(int major, unsigned long a0, 542 unsigned long a1, unsigned long a2, 543 unsigned long a3) 544 { } 545 static inline void audit_syscall_exit(void *pt_regs) 546 { } 547 static inline bool audit_dummy_context(void) 548 { 549 return true; 550 } 551 static inline void audit_set_context(struct task_struct *task, struct audit_context *ctx) 552 { } 553 static inline struct audit_context *audit_context(void) 554 { 555 return NULL; 556 } 557 static inline struct filename *audit_reusename(const __user char *name) 558 { 559 return NULL; 560 } 561 static inline void audit_getname(struct filename *name) 562 { } 563 static inline void __audit_inode(struct filename *name, 564 const struct dentry *dentry, 565 unsigned int flags) 566 { } 567 static inline void __audit_inode_child(struct inode *parent, 568 const struct dentry *dentry, 569 const unsigned char type) 570 { } 571 static inline void audit_inode(struct filename *name, 572 const struct dentry *dentry, 573 unsigned int aflags) 574 { } 575 static inline void audit_file(struct file *file) 576 { 577 } 578 static inline void audit_inode_parent_hidden(struct filename *name, 579 const struct dentry *dentry) 580 { } 581 static inline void audit_inode_child(struct inode *parent, 582 const struct dentry *dentry, 583 const unsigned char type) 584 { } 585 static inline void audit_core_dumps(long signr) 586 { } 587 static inline void audit_seccomp(unsigned long syscall, long signr, int code) 588 { } 589 static inline void audit_seccomp_actions_logged(const char *names, 590 const char *old_names, int res) 591 { } 592 static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp) 593 { } 594 static inline void audit_ipc_set_perm(unsigned long qbytes, uid_t uid, 595 gid_t gid, umode_t mode) 596 { } 597 static inline void audit_bprm(struct linux_binprm *bprm) 598 { } 599 static inline int audit_socketcall(int nargs, unsigned long *args) 600 { 601 return 0; 602 } 603 604 static inline int audit_socketcall_compat(int nargs, u32 *args) 605 { 606 return 0; 607 } 608 609 static inline void audit_fd_pair(int fd1, int fd2) 610 { } 611 static inline int audit_sockaddr(int len, void *addr) 612 { 613 return 0; 614 } 615 static inline void audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr) 616 { } 617 static inline void audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, 618 unsigned int msg_prio, 619 const struct timespec64 *abs_timeout) 620 { } 621 static inline void audit_mq_notify(mqd_t mqdes, 622 const struct sigevent *notification) 623 { } 624 static inline void audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat) 625 { } 626 static inline int audit_log_bprm_fcaps(struct linux_binprm *bprm, 627 const struct cred *new, 628 const struct cred *old) 629 { 630 return 0; 631 } 632 static inline void audit_log_capset(const struct cred *new, 633 const struct cred *old) 634 { } 635 static inline void audit_mmap_fd(int fd, int flags) 636 { } 637 638 static inline void audit_log_kern_module(char *name) 639 { 640 } 641 642 static inline void audit_fanotify(unsigned int response) 643 { } 644 645 static inline void audit_tk_injoffset(struct timespec64 offset) 646 { } 647 648 static inline void audit_ntp_init(struct audit_ntp_data *ad) 649 { } 650 651 static inline void audit_ntp_set_old(struct audit_ntp_data *ad, 652 enum audit_ntp_type type, long long val) 653 { } 654 655 static inline void audit_ntp_set_new(struct audit_ntp_data *ad, 656 enum audit_ntp_type type, long long val) 657 { } 658 659 static inline void audit_ntp_log(const struct audit_ntp_data *ad) 660 { } 661 662 static inline void audit_ptrace(struct task_struct *t) 663 { } 664 665 static inline void audit_log_nfcfg(const char *name, u8 af, 666 unsigned int nentries, 667 enum audit_nfcfgop op) 668 { } 669 670 #define audit_n_rules 0 671 #define audit_signals 0 672 #endif /* CONFIG_AUDITSYSCALL */ 673 674 static inline bool audit_loginuid_set(struct task_struct *tsk) 675 { 676 return uid_valid(audit_get_loginuid(tsk)); 677 } 678 679 static inline void audit_log_string(struct audit_buffer *ab, const char *buf) 680 { 681 audit_log_n_string(ab, buf, strlen(buf)); 682 } 683 684 #endif 685