at commit afb1ecfe
| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* audit.c -- Auditing support |
| 3 | * Gateway between the kernel (e.g., selinux) and the user-space audit daemon. |
| 4 | * System-call specific features have moved to auditsc.c |
| 5 | * |
| 6 | * Copyright 2003-2007 Red Hat Inc., Durham, North Carolina. |
| 7 | * All Rights Reserved. |
| 8 | * |
| 9 | * Written by Rickard E. (Rik) Faith <faith@redhat.com> |
| 10 | * |
| 11 | * Goals: 1) Integrate fully with Security Modules. |
| 12 | * 2) Minimal run-time overhead: |
| 13 | * a) Minimal when syscall auditing is disabled (audit_enable=0). |
| 14 | * b) Small when syscall auditing is enabled and no audit record |
| 15 | * is generated (defer as much work as possible to record |
| 16 | * generation time): |
| 17 | * i) context is allocated, |
| 18 | * ii) names from getname are stored without a copy, and |
| 19 | * iii) inode information stored from path_lookup. |
| 20 | * 3) Ability to disable syscall auditing at boot time (audit=0). |
| 21 | * 4) Usable by other parts of the kernel (if audit_log* is called, |
| 22 | * then a syscall record will be generated automatically for the |
| 23 | * current syscall). |
| 24 | * 5) Netlink interface to user-space. |
| 25 | * 6) Support low-overhead kernel-based filtering to minimize the |
| 26 | * information that must be passed to user-space. |
| 27 | * |
| 28 | * Audit userspace, documentation, tests, and bug/issue trackers: |
| 29 | * https://github.com/linux-audit |
| 30 | */ |
| 31 | |
| 32 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 33 | |
| 34 | #include <linux/file.h> |
| 35 | #include <linux/hex.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/types.h> |
| 38 | #include <linux/atomic.h> |
| 39 | #include <linux/mm.h> |
| 40 | #include <linux/export.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <linux/err.h> |
| 43 | #include <linux/kthread.h> |
| 44 | #include <linux/kernel.h> |
| 45 | #include <linux/syscalls.h> |
| 46 | #include <linux/spinlock.h> |
| 47 | #include <linux/rcupdate.h> |
| 48 | #include <linux/mutex.h> |
| 49 | #include <linux/gfp.h> |
| 50 | #include <linux/pid.h> |
| 51 | |
| 52 | #include <linux/audit.h> |
| 53 | |
| 54 | #include <net/sock.h> |
| 55 | #include <net/netlink.h> |
| 56 | #include <linux/skbuff.h> |
| 57 | #include <linux/security.h> |
| 58 | #include <linux/lsm_hooks.h> |
| 59 | #include <linux/freezer.h> |
| 60 | #include <linux/pid_namespace.h> |
| 61 | #include <net/netns/generic.h> |
| 62 | #include <net/ip.h> |
| 63 | #include <net/ipv6.h> |
| 64 | #include <linux/sctp.h> |
| 65 | #include <linux/overflow.h> |
| 66 | |
| 67 | #include "audit.h" |
| 68 | |
| 69 | /* No auditing will take place until audit_initialized == AUDIT_INITIALIZED. |
| 70 | * (Initialization happens after skb_init is called.) */ |
| 71 | #define AUDIT_DISABLED -1 |
| 72 | #define AUDIT_UNINITIALIZED 0 |
| 73 | #define AUDIT_INITIALIZED 1 |
| 74 | static int audit_initialized = AUDIT_UNINITIALIZED; |
| 75 | |
| 76 | u32 audit_enabled = AUDIT_OFF; |
| 77 | bool audit_ever_enabled = !!AUDIT_OFF; |
| 78 | |
| 79 | EXPORT_SYMBOL_GPL(audit_enabled); |
| 80 | |
| 81 | /* Default state when kernel boots without any parameters. */ |
| 82 | static u32 audit_default = AUDIT_OFF; |
| 83 | |
| 84 | /* If auditing cannot proceed, audit_failure selects what happens. */ |
| 85 | static u32 audit_failure = AUDIT_FAIL_PRINTK; |
| 86 | |
| 87 | /* private audit network namespace index */ |
| 88 | static unsigned int audit_net_id; |
| 89 | |
| 90 | /* Number of modules that provide a security context. |
| 91 | List of lsms that provide a security context */ |
| 92 | static u32 audit_subj_secctx_cnt; |
| 93 | static u32 audit_obj_secctx_cnt; |
| 94 | static const struct lsm_id *audit_subj_lsms[MAX_LSM_COUNT]; |
| 95 | static const struct lsm_id *audit_obj_lsms[MAX_LSM_COUNT]; |
| 96 | |
| 97 | /** |
| 98 | * struct audit_net - audit private network namespace data |
| 99 | * @sk: communication socket |
| 100 | */ |
| 101 | struct audit_net { |
| 102 | struct sock *sk; |
| 103 | }; |
| 104 | |
| 105 | /** |
| 106 | * struct auditd_connection - kernel/auditd connection state |
| 107 | * @pid: auditd PID |
| 108 | * @portid: netlink portid |
| 109 | * @net: the associated network namespace |
| 110 | * @rcu: RCU head |
| 111 | * |
| 112 | * Description: |
| 113 | * This struct is RCU protected; you must either hold the RCU lock for reading |
| 114 | * or the associated spinlock for writing. |
| 115 | */ |
| 116 | struct auditd_connection { |
| 117 | struct pid *pid; |
| 118 | u32 portid; |
| 119 | struct net *net; |
| 120 | struct rcu_head rcu; |
| 121 | }; |
| 122 | static struct auditd_connection __rcu *auditd_conn; |
| 123 | static DEFINE_SPINLOCK(auditd_conn_lock); |
| 124 | |
| 125 | /* If audit_rate_limit is non-zero, limit the rate of sending audit records |
| 126 | * to that number per second. This prevents DoS attacks, but results in |
| 127 | * audit records being dropped. */ |
| 128 | static u32 audit_rate_limit; |
| 129 | |
| 130 | /* Number of outstanding audit_buffers allowed. |
| 131 | * When set to zero, this means unlimited. */ |
| 132 | static u32 audit_backlog_limit = 64; |
| 133 | #define AUDIT_BACKLOG_WAIT_TIME (60 * HZ) |
| 134 | static u32 audit_backlog_wait_time = AUDIT_BACKLOG_WAIT_TIME; |
| 135 | |
| 136 | /* The identity of the user shutting down the audit system. */ |
| 137 | static kuid_t audit_sig_uid = INVALID_UID; |
| 138 | static pid_t audit_sig_pid = -1; |
| 139 | static struct lsm_prop audit_sig_lsm; |
| 140 | |
| 141 | /* Records can be lost in several ways: |
| 142 | 0) [suppressed in audit_alloc] |
| 143 | 1) out of memory in audit_log_start [kmalloc of struct audit_buffer] |
| 144 | 2) out of memory in audit_log_move [alloc_skb] |
| 145 | 3) suppressed due to audit_rate_limit |
| 146 | 4) suppressed due to audit_backlog_limit |
| 147 | */ |
| 148 | static atomic_t audit_lost = ATOMIC_INIT(0); |
| 149 | |
| 150 | /* Monotonically increasing sum of time the kernel has spent |
| 151 | * waiting while the backlog limit is exceeded. |
| 152 | */ |
| 153 | static atomic_t audit_backlog_wait_time_actual = ATOMIC_INIT(0); |
| 154 | |
| 155 | /* Hash for inode-based rules */ |
| 156 | struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS]; |
| 157 | |
| 158 | static struct kmem_cache *audit_buffer_cache; |
| 159 | |
| 160 | /* queue msgs to send via kauditd_task */ |
| 161 | static struct sk_buff_head audit_queue; |
| 162 | /* queue msgs due to temporary unicast send problems */ |
| 163 | static struct sk_buff_head audit_retry_queue; |
| 164 | /* queue msgs waiting for new auditd connection */ |
| 165 | static struct sk_buff_head audit_hold_queue; |
| 166 | |
| 167 | /* queue servicing thread */ |
| 168 | static struct task_struct *kauditd_task; |
| 169 | static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait); |
| 170 | |
| 171 | /* waitqueue for callers who are blocked on the audit backlog */ |
| 172 | static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait); |
| 173 | |
| 174 | static struct audit_features af = {.vers = AUDIT_FEATURE_VERSION, |
| 175 | .mask = -1, |
| 176 | .features = 0, |
| 177 | .lock = 0,}; |
| 178 | |
| 179 | static char *audit_feature_names[2] = { |
| 180 | "only_unset_loginuid", |
| 181 | "loginuid_immutable", |
| 182 | }; |
| 183 | |
| 184 | /** |
| 185 | * struct audit_ctl_mutex - serialize requests from userspace |
| 186 | * @lock: the mutex used for locking |
| 187 | * @owner: the task which owns the lock |
| 188 | * |
| 189 | * Description: |
| 190 | * This is the lock struct used to ensure we only process userspace requests |
| 191 | * in an orderly fashion. We can't simply use a mutex/lock here because we |
| 192 | * need to track lock ownership so we don't end up blocking the lock owner in |
| 193 | * audit_log_start() or similar. |
| 194 | */ |
| 195 | static struct audit_ctl_mutex { |
| 196 | struct mutex lock; |
| 197 | void *owner; |
| 198 | } audit_cmd_mutex; |
| 199 | |
| 200 | /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting |
| 201 | * audit records. Since printk uses a 1024 byte buffer, this buffer |
| 202 | * should be at least that large. */ |
| 203 | #define AUDIT_BUFSIZ 1024 |
| 204 | |
| 205 | /* The audit_buffer is used when formatting an audit record. The caller |
| 206 | * locks briefly to get the record off the freelist or to allocate the |
| 207 | * buffer, and locks briefly to send the buffer to the netlink layer or |
| 208 | * to place it on a transmit queue. Multiple audit_buffers can be in |
| 209 | * use simultaneously. */ |
| 210 | struct audit_buffer { |
| 211 | struct sk_buff *skb; /* the skb for audit_log functions */ |
| 212 | struct sk_buff_head skb_list; /* formatted skbs, ready to send */ |
| 213 | struct audit_context *ctx; /* NULL or associated context */ |
| 214 | struct audit_stamp stamp; /* audit stamp for these records */ |
| 215 | gfp_t gfp_mask; |
| 216 | }; |
| 217 | |
| 218 | struct audit_reply { |
| 219 | __u32 portid; |
| 220 | struct net *net; |
| 221 | struct sk_buff *skb; |
| 222 | }; |
| 223 | |
| 224 | /** |
| 225 | * auditd_test_task - Check to see if a given task is an audit daemon |
| 226 | * @task: the task to check |
| 227 | * |
| 228 | * Description: |
| 229 | * Return 1 if the task is a registered audit daemon, 0 otherwise. |
| 230 | */ |
| 231 | int auditd_test_task(struct task_struct *task) |
| 232 | { |
| 233 | int rc; |
| 234 | struct auditd_connection *ac; |
| 235 | |
| 236 | rcu_read_lock(); |
| 237 | ac = rcu_dereference(auditd_conn); |
| 238 | rc = (ac && ac->pid == task_tgid(task) ? 1 : 0); |
| 239 | rcu_read_unlock(); |
| 240 | |
| 241 | return rc; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * audit_ctl_lock - Take the audit control lock |
| 246 | */ |
| 247 | void audit_ctl_lock(void) |
| 248 | { |
| 249 | mutex_lock(&audit_cmd_mutex.lock); |
| 250 | audit_cmd_mutex.owner = current; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * audit_ctl_unlock - Drop the audit control lock |
| 255 | */ |
| 256 | void audit_ctl_unlock(void) |
| 257 | { |
| 258 | audit_cmd_mutex.owner = NULL; |
| 259 | mutex_unlock(&audit_cmd_mutex.lock); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * audit_ctl_owner_current - Test to see if the current task owns the lock |
| 264 | * |
| 265 | * Description: |
| 266 | * Return true if the current task owns the audit control lock, false if it |
| 267 | * doesn't own the lock. |
| 268 | */ |
| 269 | static bool audit_ctl_owner_current(void) |
| 270 | { |
| 271 | return (current == audit_cmd_mutex.owner); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * auditd_pid_vnr - Return the auditd PID relative to the namespace |
| 276 | * |
| 277 | * Description: |
| 278 | * Returns the PID in relation to the namespace, 0 on failure. |
| 279 | */ |
| 280 | static pid_t auditd_pid_vnr(void) |
| 281 | { |
| 282 | pid_t pid; |
| 283 | const struct auditd_connection *ac; |
| 284 | |
| 285 | rcu_read_lock(); |
| 286 | ac = rcu_dereference(auditd_conn); |
| 287 | if (!ac || !ac->pid) |
| 288 | pid = 0; |
| 289 | else |
| 290 | pid = pid_vnr(ac->pid); |
| 291 | rcu_read_unlock(); |
| 292 | |
| 293 | return pid; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * audit_cfg_lsm - Identify a security module as providing a secctx. |
| 298 | * @lsmid: LSM identity |
| 299 | * @flags: which contexts are provided |
| 300 | * |
| 301 | * Description: |
| 302 | * Increments the count of the security modules providing a secctx. |
| 303 | * If the LSM id is already in the list leave it alone. |
| 304 | */ |
| 305 | void audit_cfg_lsm(const struct lsm_id *lsmid, int flags) |
| 306 | { |
| 307 | int i; |
| 308 | |
| 309 | if (flags & AUDIT_CFG_LSM_SECCTX_SUBJECT) { |
| 310 | for (i = 0 ; i < audit_subj_secctx_cnt; i++) |
| 311 | if (audit_subj_lsms[i] == lsmid) |
| 312 | return; |
| 313 | audit_subj_lsms[audit_subj_secctx_cnt++] = lsmid; |
| 314 | } |
| 315 | if (flags & AUDIT_CFG_LSM_SECCTX_OBJECT) { |
| 316 | for (i = 0 ; i < audit_obj_secctx_cnt; i++) |
| 317 | if (audit_obj_lsms[i] == lsmid) |
| 318 | return; |
| 319 | audit_obj_lsms[audit_obj_secctx_cnt++] = lsmid; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * audit_get_sk - Return the audit socket for the given network namespace |
| 325 | * @net: the destination network namespace |
| 326 | * |
| 327 | * Description: |
| 328 | * Returns the sock pointer if valid, NULL otherwise. The caller must ensure |
| 329 | * that a reference is held for the network namespace while the sock is in use. |
| 330 | */ |
| 331 | static struct sock *audit_get_sk(const struct net *net) |
| 332 | { |
| 333 | struct audit_net *aunet; |
| 334 | |
| 335 | if (!net) |
| 336 | return NULL; |
| 337 | |
| 338 | aunet = net_generic(net, audit_net_id); |
| 339 | return aunet->sk; |
| 340 | } |
| 341 | |
| 342 | void audit_panic(const char *message) |
| 343 | { |
| 344 | switch (audit_failure) { |
| 345 | case AUDIT_FAIL_SILENT: |
| 346 | break; |
| 347 | case AUDIT_FAIL_PRINTK: |
| 348 | if (printk_ratelimit()) |
| 349 | pr_err("%s\n", message); |
| 350 | break; |
| 351 | case AUDIT_FAIL_PANIC: |
| 352 | panic("audit: %s\n", message); |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | static inline int audit_rate_check(void) |
| 358 | { |
| 359 | static unsigned long last_check; |
| 360 | static int messages; |
| 361 | static DEFINE_SPINLOCK(lock); |
| 362 | unsigned long flags; |
| 363 | unsigned long now; |
| 364 | int retval = 0; |
| 365 | |
| 366 | if (!audit_rate_limit) |
| 367 | return 1; |
| 368 | |
| 369 | spin_lock_irqsave(&lock, flags); |
| 370 | if (++messages < audit_rate_limit) { |
| 371 | retval = 1; |
| 372 | } else { |
| 373 | now = jiffies; |
| 374 | if (time_after(now, last_check + HZ)) { |
| 375 | last_check = now; |
| 376 | messages = 0; |
| 377 | retval = 1; |
| 378 | } |
| 379 | } |
| 380 | spin_unlock_irqrestore(&lock, flags); |
| 381 | |
| 382 | return retval; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * audit_log_lost - conditionally log lost audit message event |
| 387 | * @message: the message stating reason for lost audit message |
| 388 | * |
| 389 | * Emit at least 1 message per second, even if audit_rate_check is |
| 390 | * throttling. |
| 391 | * Always increment the lost messages counter. |
| 392 | */ |
| 393 | void audit_log_lost(const char *message) |
| 394 | { |
| 395 | static unsigned long last_msg; |
| 396 | static DEFINE_SPINLOCK(lock); |
| 397 | unsigned long flags; |
| 398 | unsigned long now; |
| 399 | int print; |
| 400 | |
| 401 | atomic_inc(&audit_lost); |
| 402 | |
| 403 | print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit); |
| 404 | |
| 405 | if (!print) { |
| 406 | spin_lock_irqsave(&lock, flags); |
| 407 | now = jiffies; |
| 408 | if (time_after(now, last_msg + HZ)) { |
| 409 | print = 1; |
| 410 | last_msg = now; |
| 411 | } |
| 412 | spin_unlock_irqrestore(&lock, flags); |
| 413 | } |
| 414 | |
| 415 | if (print) { |
| 416 | if (printk_ratelimit()) |
| 417 | pr_warn("audit_lost=%u audit_rate_limit=%u audit_backlog_limit=%u\n", |
| 418 | atomic_read(&audit_lost), |
| 419 | audit_rate_limit, |
| 420 | audit_backlog_limit); |
| 421 | audit_panic(message); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | static int audit_log_config_change(char *function_name, u32 new, u32 old, |
| 426 | int allow_changes) |
| 427 | { |
| 428 | struct audit_buffer *ab; |
| 429 | int rc = 0; |
| 430 | |
| 431 | ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_CONFIG_CHANGE); |
| 432 | if (unlikely(!ab)) |
| 433 | return rc; |
| 434 | audit_log_format(ab, "op=set %s=%u old=%u ", function_name, new, old); |
| 435 | audit_log_session_info(ab); |
| 436 | rc = audit_log_task_context(ab); |
| 437 | if (rc) |
| 438 | allow_changes = 0; /* Something weird, deny request */ |
| 439 | audit_log_format(ab, " res=%d", allow_changes); |
| 440 | audit_log_end(ab); |
| 441 | return rc; |
| 442 | } |
| 443 | |
| 444 | static int audit_do_config_change(char *function_name, u32 *to_change, u32 new) |
| 445 | { |
| 446 | int allow_changes, rc = 0; |
| 447 | u32 old = *to_change; |
| 448 | |
| 449 | /* check if we are locked */ |
| 450 | if (audit_enabled == AUDIT_LOCKED) |
| 451 | allow_changes = 0; |
| 452 | else |
| 453 | allow_changes = 1; |
| 454 | |
| 455 | if (audit_enabled != AUDIT_OFF) { |
| 456 | rc = audit_log_config_change(function_name, new, old, allow_changes); |
| 457 | if (rc) |
| 458 | allow_changes = 0; |
| 459 | } |
| 460 | |
| 461 | /* If we are allowed, make the change */ |
| 462 | if (allow_changes == 1) |
| 463 | *to_change = new; |
| 464 | /* Not allowed, update reason */ |
| 465 | else if (rc == 0) |
| 466 | rc = -EPERM; |
| 467 | return rc; |
| 468 | } |
| 469 | |
| 470 | static int audit_set_rate_limit(u32 limit) |
| 471 | { |
| 472 | return audit_do_config_change("audit_rate_limit", &audit_rate_limit, limit); |
| 473 | } |
| 474 | |
| 475 | static int audit_set_backlog_limit(u32 limit) |
| 476 | { |
| 477 | return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit, limit); |
| 478 | } |
| 479 | |
| 480 | static int audit_set_backlog_wait_time(u32 timeout) |
| 481 | { |
| 482 | return audit_do_config_change("audit_backlog_wait_time", |
| 483 | &audit_backlog_wait_time, timeout); |
| 484 | } |
| 485 | |
| 486 | static int audit_set_enabled(u32 state) |
| 487 | { |
| 488 | int rc; |
| 489 | if (state > AUDIT_LOCKED) |
| 490 | return -EINVAL; |
| 491 | |
| 492 | rc = audit_do_config_change("audit_enabled", &audit_enabled, state); |
| 493 | if (!rc) |
| 494 | audit_ever_enabled |= !!state; |
| 495 | |
| 496 | return rc; |
| 497 | } |
| 498 | |
| 499 | static int audit_set_failure(u32 state) |
| 500 | { |
| 501 | if (state != AUDIT_FAIL_SILENT |
| 502 | && state != AUDIT_FAIL_PRINTK |
| 503 | && state != AUDIT_FAIL_PANIC) |
| 504 | return -EINVAL; |
| 505 | |
| 506 | return audit_do_config_change("audit_failure", &audit_failure, state); |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * auditd_conn_free - RCU helper to release an auditd connection struct |
| 511 | * @rcu: RCU head |
| 512 | * |
| 513 | * Description: |
| 514 | * Drop any references inside the auditd connection tracking struct and free |
| 515 | * the memory. |
| 516 | */ |
| 517 | static void auditd_conn_free(struct rcu_head *rcu) |
| 518 | { |
| 519 | struct auditd_connection *ac; |
| 520 | |
| 521 | ac = container_of(rcu, struct auditd_connection, rcu); |
| 522 | put_pid(ac->pid); |
| 523 | put_net(ac->net); |
| 524 | kfree(ac); |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * auditd_set - Set/Reset the auditd connection state |
| 529 | * @pid: auditd PID |
| 530 | * @portid: auditd netlink portid |
| 531 | * @net: auditd network namespace pointer |
| 532 | * @skb: the netlink command from the audit daemon |
| 533 | * @ack: netlink ack flag, cleared if ack'd here |
| 534 | * |
| 535 | * Description: |
| 536 | * This function will obtain and drop network namespace references as |
| 537 | * necessary. Returns zero on success, negative values on failure. |
| 538 | */ |
| 539 | static int auditd_set(struct pid *pid, u32 portid, struct net *net, |
| 540 | struct sk_buff *skb, bool *ack) |
| 541 | { |
| 542 | unsigned long flags; |
| 543 | struct auditd_connection *ac_old, *ac_new; |
| 544 | struct nlmsghdr *nlh; |
| 545 | |
| 546 | if (!pid || !net) |
| 547 | return -EINVAL; |
| 548 | |
| 549 | ac_new = kzalloc_obj(*ac_new); |
| 550 | if (!ac_new) |
| 551 | return -ENOMEM; |
| 552 | ac_new->pid = get_pid(pid); |
| 553 | ac_new->portid = portid; |
| 554 | ac_new->net = get_net(net); |
| 555 | |
| 556 | /* send the ack now to avoid a race with the queue backlog */ |
| 557 | if (*ack) { |
| 558 | nlh = nlmsg_hdr(skb); |
| 559 | netlink_ack(skb, nlh, 0, NULL); |
| 560 | *ack = false; |
| 561 | } |
| 562 | |
| 563 | spin_lock_irqsave(&auditd_conn_lock, flags); |
| 564 | ac_old = rcu_dereference_protected(auditd_conn, |
| 565 | lockdep_is_held(&auditd_conn_lock)); |
| 566 | rcu_assign_pointer(auditd_conn, ac_new); |
| 567 | spin_unlock_irqrestore(&auditd_conn_lock, flags); |
| 568 | |
| 569 | if (ac_old) |
| 570 | call_rcu(&ac_old->rcu, auditd_conn_free); |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * kauditd_printk_skb - Print the audit record to the ring buffer |
| 577 | * @skb: audit record |
| 578 | * |
| 579 | * Whatever the reason, this packet may not make it to the auditd connection |
| 580 | * so write it via printk so the information isn't completely lost. |
| 581 | */ |
| 582 | static void kauditd_printk_skb(struct sk_buff *skb) |
| 583 | { |
| 584 | struct nlmsghdr *nlh = nlmsg_hdr(skb); |
| 585 | char *data = nlmsg_data(nlh); |
| 586 | |
| 587 | if (nlh->nlmsg_type != AUDIT_EOE && printk_ratelimit()) |
| 588 | pr_notice("type=%d %s\n", nlh->nlmsg_type, data); |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * kauditd_rehold_skb - Handle a audit record send failure in the hold queue |
| 593 | * @skb: audit record |
| 594 | * @error: error code (unused) |
| 595 | * |
| 596 | * Description: |
| 597 | * This should only be used by the kauditd_thread when it fails to flush the |
| 598 | * hold queue. |
| 599 | */ |
| 600 | static void kauditd_rehold_skb(struct sk_buff *skb, __always_unused int error) |
| 601 | { |
| 602 | /* put the record back in the queue */ |
| 603 | skb_queue_tail(&audit_hold_queue, skb); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * kauditd_hold_skb - Queue an audit record, waiting for auditd |
| 608 | * @skb: audit record |
| 609 | * @error: error code |
| 610 | * |
| 611 | * Description: |
| 612 | * Queue the audit record, waiting for an instance of auditd. When this |
| 613 | * function is called we haven't given up yet on sending the record, but things |
| 614 | * are not looking good. The first thing we want to do is try to write the |
| 615 | * record via printk and then see if we want to try and hold on to the record |
| 616 | * and queue it, if we have room. If we want to hold on to the record, but we |
| 617 | * don't have room, record a record lost message. |
| 618 | */ |
| 619 | static void kauditd_hold_skb(struct sk_buff *skb, int error) |
| 620 | { |
| 621 | /* at this point it is uncertain if we will ever send this to auditd so |
| 622 | * try to send the message via printk before we go any further */ |
| 623 | kauditd_printk_skb(skb); |
| 624 | |
| 625 | /* can we just silently drop the message? */ |
| 626 | if (!audit_default) |
| 627 | goto drop; |
| 628 | |
| 629 | /* the hold queue is only for when the daemon goes away completely, |
| 630 | * not -EAGAIN failures; if we are in a -EAGAIN state requeue the |
| 631 | * record on the retry queue unless it's full, in which case drop it |
| 632 | */ |
| 633 | if (error == -EAGAIN) { |
| 634 | if (!audit_backlog_limit || |
| 635 | skb_queue_len(&audit_retry_queue) < audit_backlog_limit) { |
| 636 | skb_queue_tail(&audit_retry_queue, skb); |
| 637 | return; |
| 638 | } |
| 639 | audit_log_lost("kauditd retry queue overflow"); |
| 640 | goto drop; |
| 641 | } |
| 642 | |
| 643 | /* if we have room in the hold queue, queue the message */ |
| 644 | if (!audit_backlog_limit || |
| 645 | skb_queue_len(&audit_hold_queue) < audit_backlog_limit) { |
| 646 | skb_queue_tail(&audit_hold_queue, skb); |
| 647 | return; |
| 648 | } |
| 649 | |
| 650 | /* we have no other options - drop the message */ |
| 651 | audit_log_lost("kauditd hold queue overflow"); |
| 652 | drop: |
| 653 | kfree_skb(skb); |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * kauditd_retry_skb - Queue an audit record, attempt to send again to auditd |
| 658 | * @skb: audit record |
| 659 | * @error: error code (unused) |
| 660 | * |
| 661 | * Description: |
| 662 | * Not as serious as kauditd_hold_skb() as we still have a connected auditd, |
| 663 | * but for some reason we are having problems sending it audit records so |
| 664 | * queue the given record and attempt to resend. |
| 665 | */ |
| 666 | static void kauditd_retry_skb(struct sk_buff *skb, __always_unused int error) |
| 667 | { |
| 668 | if (!audit_backlog_limit || |
| 669 | skb_queue_len(&audit_retry_queue) < audit_backlog_limit) { |
| 670 | skb_queue_tail(&audit_retry_queue, skb); |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | /* we have to drop the record, send it via printk as a last effort */ |
| 675 | kauditd_printk_skb(skb); |
| 676 | audit_log_lost("kauditd retry queue overflow"); |
| 677 | kfree_skb(skb); |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * auditd_reset - Disconnect the auditd connection |
| 682 | * @ac: auditd connection state |
| 683 | * |
| 684 | * Description: |
| 685 | * Break the auditd/kauditd connection and move all the queued records into the |
| 686 | * hold queue in case auditd reconnects. It is important to note that the @ac |
| 687 | * pointer should never be dereferenced inside this function as it may be NULL |
| 688 | * or invalid, you can only compare the memory address! If @ac is NULL then |
| 689 | * the connection will always be reset. |
| 690 | */ |
| 691 | static void auditd_reset(const struct auditd_connection *ac) |
| 692 | { |
| 693 | unsigned long flags; |
| 694 | struct sk_buff *skb; |
| 695 | struct auditd_connection *ac_old; |
| 696 | |
| 697 | /* if it isn't already broken, break the connection */ |
| 698 | spin_lock_irqsave(&auditd_conn_lock, flags); |
| 699 | ac_old = rcu_dereference_protected(auditd_conn, |
| 700 | lockdep_is_held(&auditd_conn_lock)); |
| 701 | if (ac && ac != ac_old) { |
| 702 | /* someone already registered a new auditd connection */ |
| 703 | spin_unlock_irqrestore(&auditd_conn_lock, flags); |
| 704 | return; |
| 705 | } |
| 706 | rcu_assign_pointer(auditd_conn, NULL); |
| 707 | spin_unlock_irqrestore(&auditd_conn_lock, flags); |
| 708 | |
| 709 | if (ac_old) |
| 710 | call_rcu(&ac_old->rcu, auditd_conn_free); |
| 711 | |
| 712 | /* flush the retry queue to the hold queue, but don't touch the main |
| 713 | * queue since we need to process that normally for multicast */ |
| 714 | while ((skb = skb_dequeue(&audit_retry_queue))) |
| 715 | kauditd_hold_skb(skb, -ECONNREFUSED); |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * auditd_send_unicast_skb - Send a record via unicast to auditd |
| 720 | * @skb: audit record |
| 721 | * |
| 722 | * Description: |
| 723 | * Send a skb to the audit daemon, returns positive/zero values on success and |
| 724 | * negative values on failure; in all cases the skb will be consumed by this |
| 725 | * function. If the send results in -ECONNREFUSED the connection with auditd |
| 726 | * will be reset. This function may sleep so callers should not hold any locks |
| 727 | * where this would cause a problem. |
| 728 | */ |
| 729 | static int auditd_send_unicast_skb(struct sk_buff *skb) |
| 730 | { |
| 731 | int rc; |
| 732 | u32 portid; |
| 733 | struct net *net; |
| 734 | struct sock *sk; |
| 735 | struct auditd_connection *ac; |
| 736 | |
| 737 | /* NOTE: we can't call netlink_unicast while in the RCU section so |
| 738 | * take a reference to the network namespace and grab local |
| 739 | * copies of the namespace, the sock, and the portid; the |
| 740 | * namespace and sock aren't going to go away while we hold a |
| 741 | * reference and if the portid does become invalid after the RCU |
| 742 | * section netlink_unicast() should safely return an error */ |
| 743 | |
| 744 | rcu_read_lock(); |
| 745 | ac = rcu_dereference(auditd_conn); |
| 746 | if (!ac) { |
| 747 | rcu_read_unlock(); |
| 748 | kfree_skb(skb); |
| 749 | rc = -ECONNREFUSED; |
| 750 | goto err; |
| 751 | } |
| 752 | net = get_net(ac->net); |
| 753 | sk = audit_get_sk(net); |
| 754 | portid = ac->portid; |
| 755 | rcu_read_unlock(); |
| 756 | |
| 757 | rc = netlink_unicast(sk, skb, portid, 0); |
| 758 | put_net(net); |
| 759 | if (rc < 0) |
| 760 | goto err; |
| 761 | |
| 762 | return rc; |
| 763 | |
| 764 | err: |
| 765 | if (ac && rc == -ECONNREFUSED) |
| 766 | auditd_reset(ac); |
| 767 | return rc; |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * kauditd_send_queue - Helper for kauditd_thread to flush skb queues |
| 772 | * @sk: the sending sock |
| 773 | * @portid: the netlink destination |
| 774 | * @queue: the skb queue to process |
| 775 | * @retry_limit: limit on number of netlink unicast failures |
| 776 | * @skb_hook: per-skb hook for additional processing |
| 777 | * @err_hook: hook called if the skb fails the netlink unicast send |
| 778 | * |
| 779 | * Description: |
| 780 | * Run through the given queue and attempt to send the audit records to auditd, |
| 781 | * returns zero on success, negative values on failure. It is up to the caller |
| 782 | * to ensure that the @sk is valid for the duration of this function. |
| 783 | * |
| 784 | */ |
| 785 | static int kauditd_send_queue(struct sock *sk, u32 portid, |
| 786 | struct sk_buff_head *queue, |
| 787 | unsigned int retry_limit, |
| 788 | void (*skb_hook)(struct sk_buff *skb), |
| 789 | void (*err_hook)(struct sk_buff *skb, int error)) |
| 790 | { |
| 791 | int rc = 0; |
| 792 | struct sk_buff *skb = NULL; |
| 793 | struct sk_buff *skb_tail; |
| 794 | unsigned int failed = 0; |
| 795 | |
| 796 | /* NOTE: kauditd_thread takes care of all our locking, we just use |
| 797 | * the netlink info passed to us (e.g. sk and portid) */ |
| 798 | |
| 799 | skb_tail = skb_peek_tail(queue); |
| 800 | while ((skb != skb_tail) && (skb = skb_dequeue(queue))) { |
| 801 | /* call the skb_hook for each skb we touch */ |
| 802 | if (skb_hook) |
| 803 | (*skb_hook)(skb); |
| 804 | |
| 805 | /* can we send to anyone via unicast? */ |
| 806 | if (!sk) { |
| 807 | if (err_hook) |
| 808 | (*err_hook)(skb, -ECONNREFUSED); |
| 809 | continue; |
| 810 | } |
| 811 | |
| 812 | retry: |
| 813 | /* grab an extra skb reference in case of error */ |
| 814 | skb_get(skb); |
| 815 | rc = netlink_unicast(sk, skb, portid, 0); |
| 816 | if (rc < 0) { |
| 817 | /* send failed - try a few times unless fatal error */ |
| 818 | if (++failed >= retry_limit || |
| 819 | rc == -ECONNREFUSED || rc == -EPERM) { |
| 820 | sk = NULL; |
| 821 | if (err_hook) |
| 822 | (*err_hook)(skb, rc); |
| 823 | if (rc == -EAGAIN) |
| 824 | rc = 0; |
| 825 | /* continue to drain the queue */ |
| 826 | continue; |
| 827 | } else |
| 828 | goto retry; |
| 829 | } else { |
| 830 | /* skb sent - drop the extra reference and continue */ |
| 831 | consume_skb(skb); |
| 832 | failed = 0; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | return (rc >= 0 ? 0 : rc); |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * kauditd_send_multicast_skb - Send a record to any multicast listeners |
| 841 | * @skb: audit record |
| 842 | * |
| 843 | * Description: |
| 844 | * Write a multicast message to anyone listening in the initial network |
| 845 | * namespace. This function doesn't consume an skb as might be expected since |
| 846 | * it has to copy it anyways. |
| 847 | */ |
| 848 | static void kauditd_send_multicast_skb(struct sk_buff *skb) |
| 849 | { |
| 850 | struct sk_buff *copy; |
| 851 | struct sock *sock = audit_get_sk(&init_net); |
| 852 | struct nlmsghdr *nlh; |
| 853 | |
| 854 | /* NOTE: we are not taking an additional reference for init_net since |
| 855 | * we don't have to worry about it going away */ |
| 856 | |
| 857 | if (!netlink_has_listeners(sock, AUDIT_NLGRP_READLOG)) |
| 858 | return; |
| 859 | |
| 860 | /* |
| 861 | * The seemingly wasteful skb_copy() rather than bumping the refcount |
| 862 | * using skb_get() is necessary because non-standard mods are made to |
| 863 | * the skb by the original kaudit unicast socket send routine. The |
| 864 | * existing auditd daemon assumes this breakage. Fixing this would |
| 865 | * require co-ordinating a change in the established protocol between |
| 866 | * the kaudit kernel subsystem and the auditd userspace code. There is |
| 867 | * no reason for new multicast clients to continue with this |
| 868 | * non-compliance. |
| 869 | */ |
| 870 | copy = skb_copy(skb, GFP_KERNEL); |
| 871 | if (!copy) |
| 872 | return; |
| 873 | nlh = nlmsg_hdr(copy); |
| 874 | nlh->nlmsg_len = skb->len; |
| 875 | |
| 876 | nlmsg_multicast(sock, copy, 0, AUDIT_NLGRP_READLOG, GFP_KERNEL); |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * kauditd_thread - Worker thread to send audit records to userspace |
| 881 | * @dummy: unused |
| 882 | */ |
| 883 | static int kauditd_thread(void *dummy) |
| 884 | { |
| 885 | int rc; |
| 886 | u32 portid = 0; |
| 887 | struct net *net = NULL; |
| 888 | struct sock *sk = NULL; |
| 889 | struct auditd_connection *ac; |
| 890 | |
| 891 | #define UNICAST_RETRIES 5 |
| 892 | |
| 893 | set_freezable(); |
| 894 | while (!kthread_should_stop()) { |
| 895 | /* NOTE: see the lock comments in auditd_send_unicast_skb() */ |
| 896 | rcu_read_lock(); |
| 897 | ac = rcu_dereference(auditd_conn); |
| 898 | if (!ac) { |
| 899 | rcu_read_unlock(); |
| 900 | goto main_queue; |
| 901 | } |
| 902 | net = get_net(ac->net); |
| 903 | sk = audit_get_sk(net); |
| 904 | portid = ac->portid; |
| 905 | rcu_read_unlock(); |
| 906 | |
| 907 | /* attempt to flush the hold queue */ |
| 908 | rc = kauditd_send_queue(sk, portid, |
| 909 | &audit_hold_queue, UNICAST_RETRIES, |
| 910 | NULL, kauditd_rehold_skb); |
| 911 | if (rc < 0) { |
| 912 | sk = NULL; |
| 913 | auditd_reset(ac); |
| 914 | goto main_queue; |
| 915 | } |
| 916 | |
| 917 | /* attempt to flush the retry queue */ |
| 918 | rc = kauditd_send_queue(sk, portid, |
| 919 | &audit_retry_queue, UNICAST_RETRIES, |
| 920 | NULL, kauditd_hold_skb); |
| 921 | if (rc < 0) { |
| 922 | sk = NULL; |
| 923 | auditd_reset(ac); |
| 924 | goto main_queue; |
| 925 | } |
| 926 | |
| 927 | main_queue: |
| 928 | /* process the main queue - do the multicast send and attempt |
| 929 | * unicast, dump failed record sends to the retry queue; if |
| 930 | * sk == NULL due to previous failures we will just do the |
| 931 | * multicast send and move the record to the hold queue */ |
| 932 | rc = kauditd_send_queue(sk, portid, &audit_queue, 1, |
| 933 | kauditd_send_multicast_skb, |
| 934 | (sk ? |
| 935 | kauditd_retry_skb : kauditd_hold_skb)); |
| 936 | if (ac && rc < 0) |
| 937 | auditd_reset(ac); |
| 938 | sk = NULL; |
| 939 | |
| 940 | /* drop our netns reference, no auditd sends past this line */ |
| 941 | if (net) { |
| 942 | put_net(net); |
| 943 | net = NULL; |
| 944 | } |
| 945 | |
| 946 | /* we have processed all the queues so wake everyone */ |
| 947 | wake_up(&audit_backlog_wait); |
| 948 | |
| 949 | /* NOTE: we want to wake up if there is anything on the queue, |
| 950 | * regardless of if an auditd is connected, as we need to |
| 951 | * do the multicast send and rotate records from the |
| 952 | * main queue to the retry/hold queues */ |
| 953 | wait_event_freezable(kauditd_wait, |
| 954 | (skb_queue_len_lockless(&audit_queue) ? 1 : 0)); |
| 955 | } |
| 956 | |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | int audit_send_list_thread(void *_dest) |
| 961 | { |
| 962 | struct audit_netlink_list *dest = _dest; |
| 963 | struct sk_buff *skb; |
| 964 | struct sock *sk = audit_get_sk(dest->net); |
| 965 | |
| 966 | /* wait for parent to finish and send an ACK */ |
| 967 | audit_ctl_lock(); |
| 968 | audit_ctl_unlock(); |
| 969 | |
| 970 | while ((skb = __skb_dequeue(&dest->q)) != NULL) |
| 971 | netlink_unicast(sk, skb, dest->portid, 0); |
| 972 | |
| 973 | put_net(dest->net); |
| 974 | kfree(dest); |
| 975 | |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | struct sk_buff *audit_make_reply(int seq, int type, int done, |
| 980 | int multi, const void *payload, int size) |
| 981 | { |
| 982 | struct sk_buff *skb; |
| 983 | struct nlmsghdr *nlh; |
| 984 | void *data; |
| 985 | int flags = multi ? NLM_F_MULTI : 0; |
| 986 | int t = done ? NLMSG_DONE : type; |
| 987 | |
| 988 | skb = nlmsg_new(size, GFP_KERNEL); |
| 989 | if (!skb) |
| 990 | return NULL; |
| 991 | |
| 992 | nlh = nlmsg_put(skb, 0, seq, t, size, flags); |
| 993 | if (!nlh) |
| 994 | goto out_kfree_skb; |
| 995 | data = nlmsg_data(nlh); |
| 996 | memcpy(data, payload, size); |
| 997 | return skb; |
| 998 | |
| 999 | out_kfree_skb: |
| 1000 | kfree_skb(skb); |
| 1001 | return NULL; |
| 1002 | } |
| 1003 | |
| 1004 | static void audit_free_reply(struct audit_reply *reply) |
| 1005 | { |
| 1006 | if (!reply) |
| 1007 | return; |
| 1008 | |
| 1009 | kfree_skb(reply->skb); |
| 1010 | if (reply->net) |
| 1011 | put_net(reply->net); |
| 1012 | kfree(reply); |
| 1013 | } |
| 1014 | |
| 1015 | static int audit_send_reply_thread(void *arg) |
| 1016 | { |
| 1017 | struct audit_reply *reply = (struct audit_reply *)arg; |
| 1018 | |
| 1019 | audit_ctl_lock(); |
| 1020 | audit_ctl_unlock(); |
| 1021 | |
| 1022 | /* Ignore failure. It'll only happen if the sender goes away, |
| 1023 | because our timeout is set to infinite. */ |
| 1024 | netlink_unicast(audit_get_sk(reply->net), reply->skb, reply->portid, 0); |
| 1025 | reply->skb = NULL; |
| 1026 | audit_free_reply(reply); |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * audit_send_reply - send an audit reply message via netlink |
| 1032 | * @request_skb: skb of request we are replying to (used to target the reply) |
| 1033 | * @seq: sequence number |
| 1034 | * @type: audit message type |
| 1035 | * @done: done (last) flag |
| 1036 | * @multi: multi-part message flag |
| 1037 | * @payload: payload data |
| 1038 | * @size: payload size |
| 1039 | * |
| 1040 | * Allocates a skb, builds the netlink message, and sends it to the port id. |
| 1041 | */ |
| 1042 | static void audit_send_reply(struct sk_buff *request_skb, int seq, int type, int done, |
| 1043 | int multi, const void *payload, int size) |
| 1044 | { |
| 1045 | struct task_struct *tsk; |
| 1046 | struct audit_reply *reply; |
| 1047 | |
| 1048 | reply = kzalloc_obj(*reply); |
| 1049 | if (!reply) |
| 1050 | return; |
| 1051 | |
| 1052 | reply->skb = audit_make_reply(seq, type, done, multi, payload, size); |
| 1053 | if (!reply->skb) |
| 1054 | goto err; |
| 1055 | reply->net = get_net(sock_net(NETLINK_CB(request_skb).sk)); |
| 1056 | reply->portid = NETLINK_CB(request_skb).portid; |
| 1057 | |
| 1058 | tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply"); |
| 1059 | if (IS_ERR(tsk)) |
| 1060 | goto err; |
| 1061 | |
| 1062 | return; |
| 1063 | |
| 1064 | err: |
| 1065 | audit_free_reply(reply); |
| 1066 | } |
| 1067 | |
| 1068 | /* |
| 1069 | * Check for appropriate CAP_AUDIT_ capabilities on incoming audit |
| 1070 | * control messages. |
| 1071 | */ |
| 1072 | static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type) |
| 1073 | { |
| 1074 | int err = 0; |
| 1075 | |
| 1076 | /* Only support initial user namespace for now. */ |
| 1077 | /* |
| 1078 | * We return ECONNREFUSED because it tricks userspace into thinking |
| 1079 | * that audit was not configured into the kernel. Lots of users |
| 1080 | * configure their PAM stack (because that's what the distro does) |
| 1081 | * to reject login if unable to send messages to audit. If we return |
| 1082 | * ECONNREFUSED the PAM stack thinks the kernel does not have audit |
| 1083 | * configured in and will let login proceed. If we return EPERM |
| 1084 | * userspace will reject all logins. This should be removed when we |
| 1085 | * support non init namespaces!! |
| 1086 | */ |
| 1087 | if (current_user_ns() != &init_user_ns) |
| 1088 | return -ECONNREFUSED; |
| 1089 | |
| 1090 | switch (msg_type) { |
| 1091 | case AUDIT_LIST: |
| 1092 | case AUDIT_ADD: |
| 1093 | case AUDIT_DEL: |
| 1094 | return -EOPNOTSUPP; |
| 1095 | case AUDIT_GET: |
| 1096 | case AUDIT_SET: |
| 1097 | case AUDIT_GET_FEATURE: |
| 1098 | case AUDIT_SET_FEATURE: |
| 1099 | case AUDIT_LIST_RULES: |
| 1100 | case AUDIT_ADD_RULE: |
| 1101 | case AUDIT_DEL_RULE: |
| 1102 | case AUDIT_SIGNAL_INFO: |
| 1103 | case AUDIT_TTY_GET: |
| 1104 | case AUDIT_TTY_SET: |
| 1105 | case AUDIT_TRIM: |
| 1106 | case AUDIT_MAKE_EQUIV: |
| 1107 | /* Only support auditd and auditctl in initial pid namespace |
| 1108 | * for now. */ |
| 1109 | if (task_active_pid_ns(current) != &init_pid_ns) |
| 1110 | return -EPERM; |
| 1111 | |
| 1112 | if (!netlink_capable(skb, CAP_AUDIT_CONTROL)) |
| 1113 | err = -EPERM; |
| 1114 | break; |
| 1115 | case AUDIT_USER: |
| 1116 | case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG: |
| 1117 | case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2: |
| 1118 | if (!netlink_capable(skb, CAP_AUDIT_WRITE)) |
| 1119 | err = -EPERM; |
| 1120 | break; |
| 1121 | default: /* bad msg */ |
| 1122 | err = -EINVAL; |
| 1123 | } |
| 1124 | |
| 1125 | return err; |
| 1126 | } |
| 1127 | |
| 1128 | static void audit_log_common_recv_msg(struct audit_context *context, |
| 1129 | struct audit_buffer **ab, u16 msg_type) |
| 1130 | { |
| 1131 | uid_t uid = from_kuid(&init_user_ns, current_uid()); |
| 1132 | pid_t pid = task_tgid_nr(current); |
| 1133 | |
| 1134 | if (!audit_enabled && msg_type != AUDIT_USER_AVC) { |
| 1135 | *ab = NULL; |
| 1136 | return; |
| 1137 | } |
| 1138 | |
| 1139 | *ab = audit_log_start(context, GFP_KERNEL, msg_type); |
| 1140 | if (unlikely(!*ab)) |
| 1141 | return; |
| 1142 | audit_log_format(*ab, "pid=%d uid=%u ", pid, uid); |
| 1143 | audit_log_session_info(*ab); |
| 1144 | audit_log_task_context(*ab); |
| 1145 | } |
| 1146 | |
| 1147 | static inline void audit_log_user_recv_msg(struct audit_buffer **ab, |
| 1148 | u16 msg_type) |
| 1149 | { |
| 1150 | audit_log_common_recv_msg(NULL, ab, msg_type); |
| 1151 | } |
| 1152 | |
| 1153 | static int is_audit_feature_set(int i) |
| 1154 | { |
| 1155 | return af.features & AUDIT_FEATURE_TO_MASK(i); |
| 1156 | } |
| 1157 | |
| 1158 | static int audit_get_feature(struct sk_buff *skb) |
| 1159 | { |
| 1160 | u32 seq; |
| 1161 | |
| 1162 | seq = nlmsg_hdr(skb)->nlmsg_seq; |
| 1163 | |
| 1164 | audit_send_reply(skb, seq, AUDIT_GET_FEATURE, 0, 0, &af, sizeof(af)); |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | static void audit_log_feature_change(int which, u32 old_feature, u32 new_feature, |
| 1170 | u32 old_lock, u32 new_lock, int res) |
| 1171 | { |
| 1172 | struct audit_buffer *ab; |
| 1173 | |
| 1174 | if (audit_enabled == AUDIT_OFF) |
| 1175 | return; |
| 1176 | |
| 1177 | ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_FEATURE_CHANGE); |
| 1178 | if (!ab) |
| 1179 | return; |
| 1180 | audit_log_task_info(ab); |
| 1181 | audit_log_format(ab, " feature=%s old=%u new=%u old_lock=%u new_lock=%u res=%d", |
| 1182 | audit_feature_names[which], !!old_feature, !!new_feature, |
| 1183 | !!old_lock, !!new_lock, res); |
| 1184 | audit_log_end(ab); |
| 1185 | } |
| 1186 | |
| 1187 | static int audit_set_feature(struct audit_features *uaf) |
| 1188 | { |
| 1189 | int i; |
| 1190 | |
| 1191 | BUILD_BUG_ON(AUDIT_LAST_FEATURE + 1 > ARRAY_SIZE(audit_feature_names)); |
| 1192 | |
| 1193 | /* if there is ever a version 2 we should handle that here */ |
| 1194 | |
| 1195 | for (i = 0; i <= AUDIT_LAST_FEATURE; i++) { |
| 1196 | u32 feature = AUDIT_FEATURE_TO_MASK(i); |
| 1197 | u32 old_feature, new_feature, old_lock, new_lock; |
| 1198 | |
| 1199 | /* if we are not changing this feature, move along */ |
| 1200 | if (!(feature & uaf->mask)) |
| 1201 | continue; |
| 1202 | |
| 1203 | old_feature = af.features & feature; |
| 1204 | new_feature = uaf->features & feature; |
| 1205 | new_lock = (uaf->lock | af.lock) & feature; |
| 1206 | old_lock = af.lock & feature; |
| 1207 | |
| 1208 | /* are we changing a locked feature? */ |
| 1209 | if (old_lock && (new_feature != old_feature)) { |
| 1210 | audit_log_feature_change(i, old_feature, new_feature, |
| 1211 | old_lock, new_lock, 0); |
| 1212 | return -EPERM; |
| 1213 | } |
| 1214 | } |
| 1215 | /* nothing invalid, do the changes */ |
| 1216 | for (i = 0; i <= AUDIT_LAST_FEATURE; i++) { |
| 1217 | u32 feature = AUDIT_FEATURE_TO_MASK(i); |
| 1218 | u32 old_feature, new_feature, old_lock, new_lock; |
| 1219 | |
| 1220 | /* if we are not changing this feature, move along */ |
| 1221 | if (!(feature & uaf->mask)) |
| 1222 | continue; |
| 1223 | |
| 1224 | old_feature = af.features & feature; |
| 1225 | new_feature = uaf->features & feature; |
| 1226 | old_lock = af.lock & feature; |
| 1227 | new_lock = (uaf->lock | af.lock) & feature; |
| 1228 | |
| 1229 | if (new_feature != old_feature) |
| 1230 | audit_log_feature_change(i, old_feature, new_feature, |
| 1231 | old_lock, new_lock, 1); |
| 1232 | |
| 1233 | if (new_feature) |
| 1234 | af.features |= feature; |
| 1235 | else |
| 1236 | af.features &= ~feature; |
| 1237 | af.lock |= new_lock; |
| 1238 | } |
| 1239 | |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | static int audit_replace(struct pid *pid) |
| 1244 | { |
| 1245 | pid_t pvnr; |
| 1246 | struct sk_buff *skb; |
| 1247 | |
| 1248 | pvnr = pid_vnr(pid); |
| 1249 | skb = audit_make_reply(0, AUDIT_REPLACE, 0, 0, &pvnr, sizeof(pvnr)); |
| 1250 | if (!skb) |
| 1251 | return -ENOMEM; |
| 1252 | return auditd_send_unicast_skb(skb); |
| 1253 | } |
| 1254 | |
| 1255 | static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 1256 | bool *ack) |
| 1257 | { |
| 1258 | u32 seq; |
| 1259 | void *data; |
| 1260 | int data_len; |
| 1261 | int err; |
| 1262 | struct audit_buffer *ab; |
| 1263 | u16 msg_type = nlh->nlmsg_type; |
| 1264 | struct audit_sig_info *sig_data; |
| 1265 | struct lsm_context lsmctx = { NULL, 0, 0 }; |
| 1266 | |
| 1267 | err = audit_netlink_ok(skb, msg_type); |
| 1268 | if (err) |
| 1269 | return err; |
| 1270 | |
| 1271 | seq = nlh->nlmsg_seq; |
| 1272 | data = nlmsg_data(nlh); |
| 1273 | data_len = nlmsg_len(nlh); |
| 1274 | |
| 1275 | switch (msg_type) { |
| 1276 | case AUDIT_GET: { |
| 1277 | struct audit_status s; |
| 1278 | memset(&s, 0, sizeof(s)); |
| 1279 | s.enabled = audit_enabled; |
| 1280 | s.failure = audit_failure; |
| 1281 | /* NOTE: use pid_vnr() so the PID is relative to the current |
| 1282 | * namespace */ |
| 1283 | s.pid = auditd_pid_vnr(); |
| 1284 | s.rate_limit = audit_rate_limit; |
| 1285 | s.backlog_limit = audit_backlog_limit; |
| 1286 | s.lost = atomic_read(&audit_lost); |
| 1287 | s.backlog = skb_queue_len_lockless(&audit_queue); |
| 1288 | s.feature_bitmap = AUDIT_FEATURE_BITMAP_ALL; |
| 1289 | s.backlog_wait_time = audit_backlog_wait_time; |
| 1290 | s.backlog_wait_time_actual = atomic_read(&audit_backlog_wait_time_actual); |
| 1291 | audit_send_reply(skb, seq, AUDIT_GET, 0, 0, &s, sizeof(s)); |
| 1292 | break; |
| 1293 | } |
| 1294 | case AUDIT_SET: { |
| 1295 | struct audit_status s; |
| 1296 | memset(&s, 0, sizeof(s)); |
| 1297 | /* guard against past and future API changes */ |
| 1298 | memcpy(&s, data, min_t(size_t, sizeof(s), data_len)); |
| 1299 | if (s.mask & ~AUDIT_STATUS_ALL) |
| 1300 | return -EINVAL; |
| 1301 | if (s.mask & AUDIT_STATUS_ENABLED) { |
| 1302 | err = audit_set_enabled(s.enabled); |
| 1303 | if (err < 0) |
| 1304 | return err; |
| 1305 | } |
| 1306 | if (s.mask & AUDIT_STATUS_FAILURE) { |
| 1307 | err = audit_set_failure(s.failure); |
| 1308 | if (err < 0) |
| 1309 | return err; |
| 1310 | } |
| 1311 | if (s.mask & AUDIT_STATUS_PID) { |
| 1312 | /* NOTE: we are using the vnr PID functions below |
| 1313 | * because the s.pid value is relative to the |
| 1314 | * namespace of the caller; at present this |
| 1315 | * doesn't matter much since you can really only |
| 1316 | * run auditd from the initial pid namespace, but |
| 1317 | * something to keep in mind if this changes */ |
| 1318 | pid_t new_pid = s.pid; |
| 1319 | pid_t auditd_pid; |
| 1320 | struct pid *req_pid = task_tgid(current); |
| 1321 | |
| 1322 | /* Sanity check - PID values must match. Setting |
| 1323 | * pid to 0 is how auditd ends auditing. */ |
| 1324 | if (new_pid && (new_pid != pid_vnr(req_pid))) |
| 1325 | return -EINVAL; |
| 1326 | |
| 1327 | /* test the auditd connection */ |
| 1328 | audit_replace(req_pid); |
| 1329 | |
| 1330 | auditd_pid = auditd_pid_vnr(); |
| 1331 | if (auditd_pid) { |
| 1332 | /* replacing a healthy auditd is not allowed */ |
| 1333 | if (new_pid) { |
| 1334 | audit_log_config_change("audit_pid", |
| 1335 | new_pid, auditd_pid, 0); |
| 1336 | return -EEXIST; |
| 1337 | } |
| 1338 | /* only current auditd can unregister itself */ |
| 1339 | if (pid_vnr(req_pid) != auditd_pid) { |
| 1340 | audit_log_config_change("audit_pid", |
| 1341 | new_pid, auditd_pid, 0); |
| 1342 | return -EACCES; |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | if (new_pid) { |
| 1347 | /* register a new auditd connection */ |
| 1348 | err = auditd_set(req_pid, |
| 1349 | NETLINK_CB(skb).portid, |
| 1350 | sock_net(NETLINK_CB(skb).sk), |
| 1351 | skb, ack); |
| 1352 | if (audit_enabled != AUDIT_OFF) |
| 1353 | audit_log_config_change("audit_pid", |
| 1354 | new_pid, |
| 1355 | auditd_pid, |
| 1356 | err ? 0 : 1); |
| 1357 | if (err) |
| 1358 | return err; |
| 1359 | |
| 1360 | /* try to process any backlog */ |
| 1361 | wake_up_interruptible(&kauditd_wait); |
| 1362 | } else { |
| 1363 | if (audit_enabled != AUDIT_OFF) |
| 1364 | audit_log_config_change("audit_pid", |
| 1365 | new_pid, |
| 1366 | auditd_pid, 1); |
| 1367 | |
| 1368 | /* unregister the auditd connection */ |
| 1369 | auditd_reset(NULL); |
| 1370 | } |
| 1371 | } |
| 1372 | if (s.mask & AUDIT_STATUS_RATE_LIMIT) { |
| 1373 | err = audit_set_rate_limit(s.rate_limit); |
| 1374 | if (err < 0) |
| 1375 | return err; |
| 1376 | } |
| 1377 | if (s.mask & AUDIT_STATUS_BACKLOG_LIMIT) { |
| 1378 | err = audit_set_backlog_limit(s.backlog_limit); |
| 1379 | if (err < 0) |
| 1380 | return err; |
| 1381 | } |
| 1382 | if (s.mask & AUDIT_STATUS_BACKLOG_WAIT_TIME) { |
| 1383 | if (sizeof(s) > (size_t)nlh->nlmsg_len) |
| 1384 | return -EINVAL; |
| 1385 | if (s.backlog_wait_time > 10*AUDIT_BACKLOG_WAIT_TIME) |
| 1386 | return -EINVAL; |
| 1387 | err = audit_set_backlog_wait_time(s.backlog_wait_time); |
| 1388 | if (err < 0) |
| 1389 | return err; |
| 1390 | } |
| 1391 | if (s.mask == AUDIT_STATUS_LOST) { |
| 1392 | u32 lost = atomic_xchg(&audit_lost, 0); |
| 1393 | |
| 1394 | audit_log_config_change("lost", 0, lost, 1); |
| 1395 | return lost; |
| 1396 | } |
| 1397 | if (s.mask == AUDIT_STATUS_BACKLOG_WAIT_TIME_ACTUAL) { |
| 1398 | u32 actual = atomic_xchg(&audit_backlog_wait_time_actual, 0); |
| 1399 | |
| 1400 | audit_log_config_change("backlog_wait_time_actual", 0, actual, 1); |
| 1401 | return actual; |
| 1402 | } |
| 1403 | break; |
| 1404 | } |
| 1405 | case AUDIT_GET_FEATURE: |
| 1406 | err = audit_get_feature(skb); |
| 1407 | if (err) |
| 1408 | return err; |
| 1409 | break; |
| 1410 | case AUDIT_SET_FEATURE: |
| 1411 | if (data_len < sizeof(struct audit_features)) |
| 1412 | return -EINVAL; |
| 1413 | err = audit_set_feature(data); |
| 1414 | if (err) |
| 1415 | return err; |
| 1416 | break; |
| 1417 | case AUDIT_USER: |
| 1418 | case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG: |
| 1419 | case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2: |
| 1420 | if (!audit_enabled && msg_type != AUDIT_USER_AVC) |
| 1421 | return 0; |
| 1422 | /* exit early if there isn't at least one character to print */ |
| 1423 | if (data_len < 2) |
| 1424 | return -EINVAL; |
| 1425 | |
| 1426 | err = audit_filter(msg_type, AUDIT_FILTER_USER); |
| 1427 | if (err == 1) { /* match or error */ |
| 1428 | char *str = data; |
| 1429 | |
| 1430 | err = 0; |
| 1431 | if (msg_type == AUDIT_USER_TTY) { |
| 1432 | err = tty_audit_push(); |
| 1433 | if (err) |
| 1434 | break; |
| 1435 | } |
| 1436 | audit_log_user_recv_msg(&ab, msg_type); |
| 1437 | if (msg_type != AUDIT_USER_TTY) { |
| 1438 | /* ensure NULL termination */ |
| 1439 | str[data_len - 1] = '\0'; |
| 1440 | audit_log_format(ab, " msg='%.*s'", |
| 1441 | AUDIT_MESSAGE_TEXT_MAX, |
| 1442 | str); |
| 1443 | } else { |
| 1444 | audit_log_format(ab, " data="); |
| 1445 | if (str[data_len - 1] == '\0') |
| 1446 | data_len--; |
| 1447 | audit_log_n_untrustedstring(ab, str, data_len); |
| 1448 | } |
| 1449 | audit_log_end(ab); |
| 1450 | } |
| 1451 | break; |
| 1452 | case AUDIT_ADD_RULE: |
| 1453 | case AUDIT_DEL_RULE: |
| 1454 | if (data_len < sizeof(struct audit_rule_data)) |
| 1455 | return -EINVAL; |
| 1456 | if (audit_enabled == AUDIT_LOCKED) { |
| 1457 | audit_log_common_recv_msg(audit_context(), &ab, |
| 1458 | AUDIT_CONFIG_CHANGE); |
| 1459 | audit_log_format(ab, " op=%s audit_enabled=%d res=0", |
| 1460 | msg_type == AUDIT_ADD_RULE ? |
| 1461 | "add_rule" : "remove_rule", |
| 1462 | audit_enabled); |
| 1463 | audit_log_end(ab); |
| 1464 | return -EPERM; |
| 1465 | } |
| 1466 | err = audit_rule_change(msg_type, seq, data, data_len); |
| 1467 | break; |
| 1468 | case AUDIT_LIST_RULES: |
| 1469 | err = audit_list_rules_send(skb, seq); |
| 1470 | break; |
| 1471 | case AUDIT_TRIM: |
| 1472 | if (audit_enabled == AUDIT_LOCKED) |
| 1473 | return -EPERM; |
| 1474 | audit_trim_trees(); |
| 1475 | audit_log_common_recv_msg(audit_context(), &ab, |
| 1476 | AUDIT_CONFIG_CHANGE); |
| 1477 | audit_log_format(ab, " op=trim res=1"); |
| 1478 | audit_log_end(ab); |
| 1479 | break; |
| 1480 | case AUDIT_MAKE_EQUIV: { |
| 1481 | void *bufp = data; |
| 1482 | u32 sizes[2]; |
| 1483 | size_t msglen = data_len; |
| 1484 | char *old, *new; |
| 1485 | |
| 1486 | if (audit_enabled == AUDIT_LOCKED) |
| 1487 | return -EPERM; |
| 1488 | err = -EINVAL; |
| 1489 | if (msglen < 2 * sizeof(u32)) |
| 1490 | break; |
| 1491 | memcpy(sizes, bufp, 2 * sizeof(u32)); |
| 1492 | bufp += 2 * sizeof(u32); |
| 1493 | msglen -= 2 * sizeof(u32); |
| 1494 | old = audit_unpack_string(&bufp, &msglen, sizes[0]); |
| 1495 | if (IS_ERR(old)) { |
| 1496 | err = PTR_ERR(old); |
| 1497 | break; |
| 1498 | } |
| 1499 | new = audit_unpack_string(&bufp, &msglen, sizes[1]); |
| 1500 | if (IS_ERR(new)) { |
| 1501 | err = PTR_ERR(new); |
| 1502 | kfree(old); |
| 1503 | break; |
| 1504 | } |
| 1505 | /* OK, here comes... */ |
| 1506 | err = audit_tag_tree(old, new); |
| 1507 | |
| 1508 | audit_log_common_recv_msg(audit_context(), &ab, |
| 1509 | AUDIT_CONFIG_CHANGE); |
| 1510 | audit_log_format(ab, " op=make_equiv old="); |
| 1511 | audit_log_untrustedstring(ab, old); |
| 1512 | audit_log_format(ab, " new="); |
| 1513 | audit_log_untrustedstring(ab, new); |
| 1514 | audit_log_format(ab, " res=%d", !err); |
| 1515 | audit_log_end(ab); |
| 1516 | kfree(old); |
| 1517 | kfree(new); |
| 1518 | break; |
| 1519 | } |
| 1520 | case AUDIT_SIGNAL_INFO: |
| 1521 | if (lsmprop_is_set(&audit_sig_lsm)) { |
| 1522 | err = security_lsmprop_to_secctx(&audit_sig_lsm, |
| 1523 | &lsmctx, LSM_ID_UNDEF); |
| 1524 | if (err < 0) |
| 1525 | return err; |
| 1526 | } |
| 1527 | sig_data = kmalloc_flex(*sig_data, ctx, lsmctx.len); |
| 1528 | if (!sig_data) { |
| 1529 | if (lsmprop_is_set(&audit_sig_lsm)) |
| 1530 | security_release_secctx(&lsmctx); |
| 1531 | return -ENOMEM; |
| 1532 | } |
| 1533 | sig_data->uid = from_kuid(&init_user_ns, audit_sig_uid); |
| 1534 | sig_data->pid = audit_sig_pid; |
| 1535 | if (lsmprop_is_set(&audit_sig_lsm)) { |
| 1536 | memcpy(sig_data->ctx, lsmctx.context, lsmctx.len); |
| 1537 | security_release_secctx(&lsmctx); |
| 1538 | } |
| 1539 | audit_send_reply(skb, seq, AUDIT_SIGNAL_INFO, 0, 0, |
| 1540 | sig_data, struct_size(sig_data, ctx, |
| 1541 | lsmctx.len)); |
| 1542 | kfree(sig_data); |
| 1543 | break; |
| 1544 | case AUDIT_TTY_GET: { |
| 1545 | struct audit_tty_status s; |
| 1546 | unsigned int t; |
| 1547 | |
| 1548 | t = READ_ONCE(current->signal->audit_tty); |
| 1549 | s.enabled = t & AUDIT_TTY_ENABLE; |
| 1550 | s.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD); |
| 1551 | |
| 1552 | audit_send_reply(skb, seq, AUDIT_TTY_GET, 0, 0, &s, sizeof(s)); |
| 1553 | break; |
| 1554 | } |
| 1555 | case AUDIT_TTY_SET: { |
| 1556 | struct audit_tty_status s, old; |
| 1557 | struct audit_buffer *ab; |
| 1558 | unsigned int t; |
| 1559 | |
| 1560 | memset(&s, 0, sizeof(s)); |
| 1561 | /* guard against past and future API changes */ |
| 1562 | memcpy(&s, data, min_t(size_t, sizeof(s), data_len)); |
| 1563 | /* check if new data is valid */ |
| 1564 | if ((s.enabled != 0 && s.enabled != 1) || |
| 1565 | (s.log_passwd != 0 && s.log_passwd != 1)) |
| 1566 | err = -EINVAL; |
| 1567 | |
| 1568 | if (err) |
| 1569 | t = READ_ONCE(current->signal->audit_tty); |
| 1570 | else { |
| 1571 | t = s.enabled | (-s.log_passwd & AUDIT_TTY_LOG_PASSWD); |
| 1572 | t = xchg(¤t->signal->audit_tty, t); |
| 1573 | } |
| 1574 | old.enabled = t & AUDIT_TTY_ENABLE; |
| 1575 | old.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD); |
| 1576 | |
| 1577 | audit_log_common_recv_msg(audit_context(), &ab, |
| 1578 | AUDIT_CONFIG_CHANGE); |
| 1579 | audit_log_format(ab, " op=tty_set old-enabled=%d new-enabled=%d" |
| 1580 | " old-log_passwd=%d new-log_passwd=%d res=%d", |
| 1581 | old.enabled, s.enabled, old.log_passwd, |
| 1582 | s.log_passwd, !err); |
| 1583 | audit_log_end(ab); |
| 1584 | break; |
| 1585 | } |
| 1586 | default: |
| 1587 | err = -EINVAL; |
| 1588 | break; |
| 1589 | } |
| 1590 | |
| 1591 | return err < 0 ? err : 0; |
| 1592 | } |
| 1593 | |
| 1594 | /** |
| 1595 | * audit_receive - receive messages from a netlink control socket |
| 1596 | * @skb: the message buffer |
| 1597 | * |
| 1598 | * Parse the provided skb and deal with any messages that may be present, |
| 1599 | * malformed skbs are discarded. |
| 1600 | */ |
| 1601 | static void audit_receive(struct sk_buff *skb) |
| 1602 | { |
| 1603 | struct nlmsghdr *nlh; |
| 1604 | bool ack; |
| 1605 | /* |
| 1606 | * len MUST be signed for nlmsg_next to be able to dec it below 0 |
| 1607 | * if the nlmsg_len was not aligned |
| 1608 | */ |
| 1609 | int len; |
| 1610 | int err; |
| 1611 | |
| 1612 | nlh = nlmsg_hdr(skb); |
| 1613 | len = skb->len; |
| 1614 | |
| 1615 | audit_ctl_lock(); |
| 1616 | while (nlmsg_ok(nlh, len)) { |
| 1617 | ack = nlh->nlmsg_flags & NLM_F_ACK; |
| 1618 | err = audit_receive_msg(skb, nlh, &ack); |
| 1619 | |
| 1620 | /* send an ack if the user asked for one and audit_receive_msg |
| 1621 | * didn't already do it, or if there was an error. */ |
| 1622 | if (ack || err) |
| 1623 | netlink_ack(skb, nlh, err, NULL); |
| 1624 | |
| 1625 | nlh = nlmsg_next(nlh, &len); |
| 1626 | } |
| 1627 | audit_ctl_unlock(); |
| 1628 | |
| 1629 | /* can't block with the ctrl lock, so penalize the sender now */ |
| 1630 | if (audit_backlog_limit && |
| 1631 | (skb_queue_len_lockless(&audit_queue) > audit_backlog_limit)) { |
| 1632 | DECLARE_WAITQUEUE(wait, current); |
| 1633 | |
| 1634 | /* wake kauditd to try and flush the queue */ |
| 1635 | wake_up_interruptible(&kauditd_wait); |
| 1636 | |
| 1637 | add_wait_queue_exclusive(&audit_backlog_wait, &wait); |
| 1638 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1639 | schedule_timeout(audit_backlog_wait_time); |
| 1640 | remove_wait_queue(&audit_backlog_wait, &wait); |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | /* Log information about who is connecting to the audit multicast socket */ |
| 1645 | static void audit_log_multicast(int group, const char *op, int err) |
| 1646 | { |
| 1647 | const struct cred *cred; |
| 1648 | struct tty_struct *tty; |
| 1649 | char comm[sizeof(current->comm)]; |
| 1650 | struct audit_buffer *ab; |
| 1651 | |
| 1652 | if (!audit_enabled) |
| 1653 | return; |
| 1654 | |
| 1655 | ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_EVENT_LISTENER); |
| 1656 | if (!ab) |
| 1657 | return; |
| 1658 | |
| 1659 | cred = current_cred(); |
| 1660 | tty = audit_get_tty(); |
| 1661 | audit_log_format(ab, "pid=%u uid=%u auid=%u tty=%s ses=%u", |
| 1662 | task_tgid_nr(current), |
| 1663 | from_kuid(&init_user_ns, cred->uid), |
| 1664 | from_kuid(&init_user_ns, audit_get_loginuid(current)), |
| 1665 | tty ? tty_name(tty) : "(none)", |
| 1666 | audit_get_sessionid(current)); |
| 1667 | audit_put_tty(tty); |
| 1668 | audit_log_task_context(ab); /* subj= */ |
| 1669 | audit_log_format(ab, " comm="); |
| 1670 | audit_log_untrustedstring(ab, get_task_comm(comm, current)); |
| 1671 | audit_log_d_path_exe(ab, current->mm); /* exe= */ |
| 1672 | audit_log_format(ab, " nl-mcgrp=%d op=%s res=%d", group, op, !err); |
| 1673 | audit_log_end(ab); |
| 1674 | } |
| 1675 | |
| 1676 | /* Run custom bind function on netlink socket group connect or bind requests. */ |
| 1677 | static int audit_multicast_bind(struct net *net, int group) |
| 1678 | { |
| 1679 | int err = 0; |
| 1680 | |
| 1681 | if (!capable(CAP_AUDIT_READ)) |
| 1682 | err = -EPERM; |
| 1683 | audit_log_multicast(group, "connect", err); |
| 1684 | return err; |
| 1685 | } |
| 1686 | |
| 1687 | static void audit_multicast_unbind(struct net *net, int group) |
| 1688 | { |
| 1689 | audit_log_multicast(group, "disconnect", 0); |
| 1690 | } |
| 1691 | |
| 1692 | static int __net_init audit_net_init(struct net *net) |
| 1693 | { |
| 1694 | struct netlink_kernel_cfg cfg = { |
| 1695 | .input = audit_receive, |
| 1696 | .bind = audit_multicast_bind, |
| 1697 | .unbind = audit_multicast_unbind, |
| 1698 | .flags = NL_CFG_F_NONROOT_RECV, |
| 1699 | .groups = AUDIT_NLGRP_MAX, |
| 1700 | }; |
| 1701 | |
| 1702 | struct audit_net *aunet = net_generic(net, audit_net_id); |
| 1703 | |
| 1704 | aunet->sk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg); |
| 1705 | if (aunet->sk == NULL) { |
| 1706 | audit_panic("cannot initialize netlink socket in namespace"); |
| 1707 | return -ENOMEM; |
| 1708 | } |
| 1709 | /* limit the timeout in case auditd is blocked/stopped */ |
| 1710 | aunet->sk->sk_sndtimeo = HZ / 10; |
| 1711 | |
| 1712 | return 0; |
| 1713 | } |
| 1714 | |
| 1715 | static void __net_exit audit_net_exit(struct net *net) |
| 1716 | { |
| 1717 | struct audit_net *aunet = net_generic(net, audit_net_id); |
| 1718 | |
| 1719 | /* NOTE: you would think that we would want to check the auditd |
| 1720 | * connection and potentially reset it here if it lives in this |
| 1721 | * namespace, but since the auditd connection tracking struct holds a |
| 1722 | * reference to this namespace (see auditd_set()) we are only ever |
| 1723 | * going to get here after that connection has been released */ |
| 1724 | |
| 1725 | netlink_kernel_release(aunet->sk); |
| 1726 | } |
| 1727 | |
| 1728 | static struct pernet_operations audit_net_ops __net_initdata = { |
| 1729 | .init = audit_net_init, |
| 1730 | .exit = audit_net_exit, |
| 1731 | .id = &audit_net_id, |
| 1732 | .size = sizeof(struct audit_net), |
| 1733 | }; |
| 1734 | |
| 1735 | /* Initialize audit support at boot time. */ |
| 1736 | static int __init audit_init(void) |
| 1737 | { |
| 1738 | int i; |
| 1739 | |
| 1740 | if (audit_initialized == AUDIT_DISABLED) |
| 1741 | return 0; |
| 1742 | |
| 1743 | audit_buffer_cache = KMEM_CACHE(audit_buffer, SLAB_PANIC); |
| 1744 | |
| 1745 | skb_queue_head_init(&audit_queue); |
| 1746 | skb_queue_head_init(&audit_retry_queue); |
| 1747 | skb_queue_head_init(&audit_hold_queue); |
| 1748 | |
| 1749 | for (i = 0; i < AUDIT_INODE_BUCKETS; i++) |
| 1750 | INIT_LIST_HEAD(&audit_inode_hash[i]); |
| 1751 | |
| 1752 | mutex_init(&audit_cmd_mutex.lock); |
| 1753 | audit_cmd_mutex.owner = NULL; |
| 1754 | |
| 1755 | pr_info("initializing netlink subsys (%s)\n", |
| 1756 | str_enabled_disabled(audit_default)); |
| 1757 | register_pernet_subsys(&audit_net_ops); |
| 1758 | |
| 1759 | audit_initialized = AUDIT_INITIALIZED; |
| 1760 | |
| 1761 | kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd"); |
| 1762 | if (IS_ERR(kauditd_task)) { |
| 1763 | int err = PTR_ERR(kauditd_task); |
| 1764 | panic("audit: failed to start the kauditd thread (%d)\n", err); |
| 1765 | } |
| 1766 | |
| 1767 | audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, |
| 1768 | "state=initialized audit_enabled=%u res=1", |
| 1769 | audit_enabled); |
| 1770 | |
| 1771 | return 0; |
| 1772 | } |
| 1773 | postcore_initcall(audit_init); |
| 1774 | |
| 1775 | /* |
| 1776 | * Process kernel command-line parameter at boot time. |
| 1777 | * audit={0|off} or audit={1|on}. |
| 1778 | */ |
| 1779 | static int __init audit_enable(char *str) |
| 1780 | { |
| 1781 | if (!strcasecmp(str, "off") || !strcmp(str, "0")) |
| 1782 | audit_default = AUDIT_OFF; |
| 1783 | else if (!strcasecmp(str, "on") || !strcmp(str, "1")) |
| 1784 | audit_default = AUDIT_ON; |
| 1785 | else { |
| 1786 | pr_err("audit: invalid 'audit' parameter value (%s)\n", str); |
| 1787 | audit_default = AUDIT_ON; |
| 1788 | } |
| 1789 | |
| 1790 | if (audit_default == AUDIT_OFF) |
| 1791 | audit_initialized = AUDIT_DISABLED; |
| 1792 | if (audit_set_enabled(audit_default)) |
| 1793 | pr_err("audit: error setting audit state (%d)\n", |
| 1794 | audit_default); |
| 1795 | |
| 1796 | pr_info("%s\n", audit_default ? |
| 1797 | "enabled (after initialization)" : "disabled (until reboot)"); |
| 1798 | |
| 1799 | return 1; |
| 1800 | } |
| 1801 | __setup("audit=", audit_enable); |
| 1802 | |
| 1803 | /* Process kernel command-line parameter at boot time. |
| 1804 | * audit_backlog_limit=<n> */ |
| 1805 | static int __init audit_backlog_limit_set(char *str) |
| 1806 | { |
| 1807 | u32 audit_backlog_limit_arg; |
| 1808 | |
| 1809 | pr_info("audit_backlog_limit: "); |
| 1810 | if (kstrtouint(str, 0, &audit_backlog_limit_arg)) { |
| 1811 | pr_cont("using default of %u, unable to parse %s\n", |
| 1812 | audit_backlog_limit, str); |
| 1813 | return 1; |
| 1814 | } |
| 1815 | |
| 1816 | audit_backlog_limit = audit_backlog_limit_arg; |
| 1817 | pr_cont("%d\n", audit_backlog_limit); |
| 1818 | |
| 1819 | return 1; |
| 1820 | } |
| 1821 | __setup("audit_backlog_limit=", audit_backlog_limit_set); |
| 1822 | |
| 1823 | static void audit_buffer_free(struct audit_buffer *ab) |
| 1824 | { |
| 1825 | struct sk_buff *skb; |
| 1826 | |
| 1827 | if (!ab) |
| 1828 | return; |
| 1829 | |
| 1830 | while ((skb = skb_dequeue(&ab->skb_list))) |
| 1831 | kfree_skb(skb); |
| 1832 | kmem_cache_free(audit_buffer_cache, ab); |
| 1833 | } |
| 1834 | |
| 1835 | static struct audit_buffer *audit_buffer_alloc(struct audit_context *ctx, |
| 1836 | gfp_t gfp_mask, int type) |
| 1837 | { |
| 1838 | struct audit_buffer *ab; |
| 1839 | |
| 1840 | ab = kmem_cache_alloc(audit_buffer_cache, gfp_mask); |
| 1841 | if (!ab) |
| 1842 | return NULL; |
| 1843 | |
| 1844 | skb_queue_head_init(&ab->skb_list); |
| 1845 | |
| 1846 | ab->skb = nlmsg_new(AUDIT_BUFSIZ, gfp_mask); |
| 1847 | if (!ab->skb) |
| 1848 | goto err; |
| 1849 | |
| 1850 | skb_queue_tail(&ab->skb_list, ab->skb); |
| 1851 | |
| 1852 | if (!nlmsg_put(ab->skb, 0, 0, type, 0, 0)) |
| 1853 | goto err; |
| 1854 | |
| 1855 | ab->ctx = ctx; |
| 1856 | ab->gfp_mask = gfp_mask; |
| 1857 | |
| 1858 | return ab; |
| 1859 | |
| 1860 | err: |
| 1861 | audit_buffer_free(ab); |
| 1862 | return NULL; |
| 1863 | } |
| 1864 | |
| 1865 | /** |
| 1866 | * audit_serial - compute a serial number for the audit record |
| 1867 | * |
| 1868 | * Compute a serial number for the audit record. Audit records are |
| 1869 | * written to user-space as soon as they are generated, so a complete |
| 1870 | * audit record may be written in several pieces. The timestamp of the |
| 1871 | * record and this serial number are used by the user-space tools to |
| 1872 | * determine which pieces belong to the same audit record. The |
| 1873 | * (timestamp,serial) tuple is unique for each syscall and is live from |
| 1874 | * syscall entry to syscall exit. |
| 1875 | * |
| 1876 | * NOTE: Another possibility is to store the formatted records off the |
| 1877 | * audit context (for those records that have a context), and emit them |
| 1878 | * all at syscall exit. However, this could delay the reporting of |
| 1879 | * significant errors until syscall exit (or never, if the system |
| 1880 | * halts). |
| 1881 | */ |
| 1882 | unsigned int audit_serial(void) |
| 1883 | { |
| 1884 | static atomic_t serial = ATOMIC_INIT(0); |
| 1885 | |
| 1886 | return atomic_inc_return(&serial); |
| 1887 | } |
| 1888 | |
| 1889 | static inline void audit_get_stamp(struct audit_context *ctx, |
| 1890 | struct audit_stamp *stamp) |
| 1891 | { |
| 1892 | if (!ctx || !auditsc_get_stamp(ctx, stamp)) { |
| 1893 | ktime_get_coarse_real_ts64(&stamp->ctime); |
| 1894 | stamp->serial = audit_serial(); |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | /** |
| 1899 | * audit_log_start - obtain an audit buffer |
| 1900 | * @ctx: audit_context (may be NULL) |
| 1901 | * @gfp_mask: type of allocation |
| 1902 | * @type: audit message type |
| 1903 | * |
| 1904 | * Returns audit_buffer pointer on success or NULL on error. |
| 1905 | * |
| 1906 | * Obtain an audit buffer. This routine does locking to obtain the |
| 1907 | * audit buffer, but then no locking is required for calls to |
| 1908 | * audit_log_*format. If the task (ctx) is a task that is currently in a |
| 1909 | * syscall, then the syscall is marked as auditable and an audit record |
| 1910 | * will be written at syscall exit. If there is no associated task, then |
| 1911 | * task context (ctx) should be NULL. |
| 1912 | */ |
| 1913 | struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, |
| 1914 | int type) |
| 1915 | { |
| 1916 | struct audit_buffer *ab; |
| 1917 | |
| 1918 | if (audit_initialized != AUDIT_INITIALIZED) |
| 1919 | return NULL; |
| 1920 | |
| 1921 | if (unlikely(!audit_filter(type, AUDIT_FILTER_EXCLUDE))) |
| 1922 | return NULL; |
| 1923 | |
| 1924 | /* NOTE: don't ever fail/sleep on these two conditions: |
| 1925 | * 1. auditd generated record - since we need auditd to drain the |
| 1926 | * queue; also, when we are checking for auditd, compare PIDs using |
| 1927 | * task_tgid_vnr() since auditd_pid is set in audit_receive_msg() |
| 1928 | * using a PID anchored in the caller's namespace |
| 1929 | * 2. generator holding the audit_cmd_mutex - we don't want to block |
| 1930 | * while holding the mutex, although we do penalize the sender |
| 1931 | * later in audit_receive() when it is safe to block |
| 1932 | */ |
| 1933 | if (!(auditd_test_task(current) || audit_ctl_owner_current())) { |
| 1934 | long stime = audit_backlog_wait_time; |
| 1935 | |
| 1936 | while (audit_backlog_limit && |
| 1937 | (skb_queue_len_lockless(&audit_queue) > audit_backlog_limit)) { |
| 1938 | /* wake kauditd to try and flush the queue */ |
| 1939 | wake_up_interruptible(&kauditd_wait); |
| 1940 | |
| 1941 | /* sleep if we are allowed and we haven't exhausted our |
| 1942 | * backlog wait limit */ |
| 1943 | if (gfpflags_allow_blocking(gfp_mask) && (stime > 0)) { |
| 1944 | long rtime = stime; |
| 1945 | |
| 1946 | DECLARE_WAITQUEUE(wait, current); |
| 1947 | |
| 1948 | add_wait_queue_exclusive(&audit_backlog_wait, |
| 1949 | &wait); |
| 1950 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1951 | stime = schedule_timeout(rtime); |
| 1952 | atomic_add(rtime - stime, &audit_backlog_wait_time_actual); |
| 1953 | remove_wait_queue(&audit_backlog_wait, &wait); |
| 1954 | } else { |
| 1955 | if (audit_rate_check() && printk_ratelimit()) |
| 1956 | pr_warn("audit_backlog=%d > audit_backlog_limit=%d\n", |
| 1957 | skb_queue_len_lockless(&audit_queue), |
| 1958 | audit_backlog_limit); |
| 1959 | audit_log_lost("backlog limit exceeded"); |
| 1960 | return NULL; |
| 1961 | } |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | ab = audit_buffer_alloc(ctx, gfp_mask, type); |
| 1966 | if (!ab) { |
| 1967 | audit_log_lost("out of memory in audit_log_start"); |
| 1968 | return NULL; |
| 1969 | } |
| 1970 | |
| 1971 | audit_get_stamp(ab->ctx, &ab->stamp); |
| 1972 | /* cancel dummy context to enable supporting records */ |
| 1973 | if (ctx) |
| 1974 | ctx->dummy = 0; |
| 1975 | audit_log_format(ab, "audit(%llu.%03lu:%u): ", |
| 1976 | (unsigned long long)ab->stamp.ctime.tv_sec, |
| 1977 | ab->stamp.ctime.tv_nsec/1000000, |
| 1978 | ab->stamp.serial); |
| 1979 | |
| 1980 | return ab; |
| 1981 | } |
| 1982 | |
| 1983 | /** |
| 1984 | * audit_expand - expand skb in the audit buffer |
| 1985 | * @ab: audit_buffer |
| 1986 | * @extra: space to add at tail of the skb |
| 1987 | * |
| 1988 | * Returns 0 (no space) on failed expansion, or available space if |
| 1989 | * successful. |
| 1990 | */ |
| 1991 | static inline int audit_expand(struct audit_buffer *ab, int extra) |
| 1992 | { |
| 1993 | struct sk_buff *skb = ab->skb; |
| 1994 | int oldtail = skb_tailroom(skb); |
| 1995 | int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask); |
| 1996 | int newtail = skb_tailroom(skb); |
| 1997 | |
| 1998 | if (ret < 0) { |
| 1999 | audit_log_lost("out of memory in audit_expand"); |
| 2000 | return 0; |
| 2001 | } |
| 2002 | |
| 2003 | skb->truesize += newtail - oldtail; |
| 2004 | return newtail; |
| 2005 | } |
| 2006 | |
| 2007 | /* |
| 2008 | * Format an audit message into the audit buffer. If there isn't enough |
| 2009 | * room in the audit buffer, more room will be allocated and vsnprint |
| 2010 | * will be called a second time. Currently, we assume that a printk |
| 2011 | * can't format message larger than 1024 bytes, so we don't either. |
| 2012 | */ |
| 2013 | static __printf(2, 0) |
| 2014 | void audit_log_vformat(struct audit_buffer *ab, const char *fmt, va_list args) |
| 2015 | { |
| 2016 | int len, avail; |
| 2017 | struct sk_buff *skb; |
| 2018 | va_list args2; |
| 2019 | |
| 2020 | if (!ab) |
| 2021 | return; |
| 2022 | |
| 2023 | BUG_ON(!ab->skb); |
| 2024 | skb = ab->skb; |
| 2025 | avail = skb_tailroom(skb); |
| 2026 | if (avail == 0) { |
| 2027 | avail = audit_expand(ab, AUDIT_BUFSIZ); |
| 2028 | if (!avail) |
| 2029 | goto out; |
| 2030 | } |
| 2031 | va_copy(args2, args); |
| 2032 | len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args); |
| 2033 | if (len >= avail) { |
| 2034 | /* The printk buffer is 1024 bytes long, so if we get |
| 2035 | * here and AUDIT_BUFSIZ is at least 1024, then we can |
| 2036 | * log everything that printk could have logged. */ |
| 2037 | avail = audit_expand(ab, |
| 2038 | max_t(unsigned int, AUDIT_BUFSIZ, 1+len-avail)); |
| 2039 | if (!avail) |
| 2040 | goto out_va_end; |
| 2041 | len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2); |
| 2042 | } |
| 2043 | if (len > 0) |
| 2044 | skb_put(skb, len); |
| 2045 | out_va_end: |
| 2046 | va_end(args2); |
| 2047 | out: |
| 2048 | return; |
| 2049 | } |
| 2050 | |
| 2051 | /** |
| 2052 | * audit_log_format - format a message into the audit buffer. |
| 2053 | * @ab: audit_buffer |
| 2054 | * @fmt: format string |
| 2055 | * @...: optional parameters matching @fmt string |
| 2056 | * |
| 2057 | * All the work is done in audit_log_vformat. |
| 2058 | */ |
| 2059 | void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) |
| 2060 | { |
| 2061 | va_list args; |
| 2062 | |
| 2063 | if (!ab) |
| 2064 | return; |
| 2065 | va_start(args, fmt); |
| 2066 | audit_log_vformat(ab, fmt, args); |
| 2067 | va_end(args); |
| 2068 | } |
| 2069 | |
| 2070 | /** |
| 2071 | * audit_log_n_hex - convert a buffer to hex and append it to the audit skb |
| 2072 | * @ab: the audit_buffer |
| 2073 | * @buf: buffer to convert to hex |
| 2074 | * @len: length of @buf to be converted |
| 2075 | * |
| 2076 | * No return value; failure to expand is silently ignored. |
| 2077 | * |
| 2078 | * This function will take the passed buf and convert it into a string of |
| 2079 | * ascii hex digits. The new string is placed onto the skb. |
| 2080 | */ |
| 2081 | void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf, |
| 2082 | size_t len) |
| 2083 | { |
| 2084 | int avail; |
| 2085 | size_t i, new_len; |
| 2086 | unsigned char *ptr; |
| 2087 | struct sk_buff *skb; |
| 2088 | |
| 2089 | if (!ab) |
| 2090 | return; |
| 2091 | |
| 2092 | BUG_ON(!ab->skb); |
| 2093 | skb = ab->skb; |
| 2094 | avail = skb_tailroom(skb); |
| 2095 | |
| 2096 | if (check_shl_overflow(len, 1, &new_len)) { |
| 2097 | audit_log_format(ab, "?"); |
| 2098 | return; |
| 2099 | } |
| 2100 | |
| 2101 | if (new_len >= avail) { |
| 2102 | /* Round the buffer request up to the next multiple */ |
| 2103 | new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1); |
| 2104 | avail = audit_expand(ab, new_len); |
| 2105 | if (!avail) |
| 2106 | return; |
| 2107 | } |
| 2108 | |
| 2109 | ptr = skb_tail_pointer(skb); |
| 2110 | for (i = 0; i < len; i++) |
| 2111 | ptr = hex_byte_pack_upper(ptr, buf[i]); |
| 2112 | *ptr = 0; |
| 2113 | skb_put(skb, len << 1); /* new string is twice the old string */ |
| 2114 | } |
| 2115 | |
| 2116 | /* |
| 2117 | * Format a string of no more than slen characters into the audit buffer, |
| 2118 | * enclosed in quote marks. |
| 2119 | */ |
| 2120 | void audit_log_n_string(struct audit_buffer *ab, const char *string, |
| 2121 | size_t slen) |
| 2122 | { |
| 2123 | int avail; |
| 2124 | size_t new_len; |
| 2125 | unsigned char *ptr; |
| 2126 | struct sk_buff *skb; |
| 2127 | |
| 2128 | if (!ab) |
| 2129 | return; |
| 2130 | |
| 2131 | BUG_ON(!ab->skb); |
| 2132 | skb = ab->skb; |
| 2133 | avail = skb_tailroom(skb); |
| 2134 | |
| 2135 | /* enclosing quotes + null terminator */ |
| 2136 | if (check_add_overflow(slen, 3, &new_len)) { |
| 2137 | audit_log_format(ab, "?"); |
| 2138 | return; |
| 2139 | } |
| 2140 | |
| 2141 | if (new_len > avail) { |
| 2142 | avail = audit_expand(ab, new_len); |
| 2143 | if (!avail) |
| 2144 | return; |
| 2145 | } |
| 2146 | ptr = skb_tail_pointer(skb); |
| 2147 | *ptr++ = '"'; |
| 2148 | memcpy(ptr, string, slen); |
| 2149 | ptr += slen; |
| 2150 | *ptr++ = '"'; |
| 2151 | *ptr = 0; |
| 2152 | skb_put(skb, slen + 2); /* don't include null terminator */ |
| 2153 | } |
| 2154 | |
| 2155 | /** |
| 2156 | * audit_string_contains_control - does a string need to be logged in hex |
| 2157 | * @string: string to be checked |
| 2158 | * @len: max length of the string to check |
| 2159 | */ |
| 2160 | bool audit_string_contains_control(const char *string, size_t len) |
| 2161 | { |
| 2162 | const unsigned char *p; |
| 2163 | for (p = string; p < (const unsigned char *)string + len; p++) { |
| 2164 | if (*p == '"' || *p < 0x21 || *p > 0x7e) |
| 2165 | return true; |
| 2166 | } |
| 2167 | return false; |
| 2168 | } |
| 2169 | |
| 2170 | /** |
| 2171 | * audit_log_n_untrustedstring - log a string that may contain random characters |
| 2172 | * @ab: audit_buffer |
| 2173 | * @string: string to be logged |
| 2174 | * @len: length of string (not including trailing null) |
| 2175 | * |
| 2176 | * This code will escape a string that is passed to it if the string |
| 2177 | * contains a control character, unprintable character, double quote mark, |
| 2178 | * or a space. Unescaped strings will start and end with a double quote mark. |
| 2179 | * Strings that are escaped are printed in hex (2 digits per char). |
| 2180 | * |
| 2181 | * The caller specifies the number of characters in the string to log, which may |
| 2182 | * or may not be the entire string. |
| 2183 | */ |
| 2184 | void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string, |
| 2185 | size_t len) |
| 2186 | { |
| 2187 | if (audit_string_contains_control(string, len)) |
| 2188 | audit_log_n_hex(ab, string, len); |
| 2189 | else |
| 2190 | audit_log_n_string(ab, string, len); |
| 2191 | } |
| 2192 | |
| 2193 | /** |
| 2194 | * audit_log_untrustedstring - log a string that may contain random characters |
| 2195 | * @ab: audit_buffer |
| 2196 | * @string: string to be logged |
| 2197 | * |
| 2198 | * Same as audit_log_n_untrustedstring(), except that strlen is used to |
| 2199 | * determine string length. |
| 2200 | */ |
| 2201 | void audit_log_untrustedstring(struct audit_buffer *ab, const char *string) |
| 2202 | { |
| 2203 | audit_log_n_untrustedstring(ab, string, strlen(string)); |
| 2204 | } |
| 2205 | |
| 2206 | /* This is a helper-function to print the escaped d_path */ |
| 2207 | void audit_log_d_path(struct audit_buffer *ab, const char *prefix, |
| 2208 | const struct path *path) |
| 2209 | { |
| 2210 | char *p, *pathname; |
| 2211 | |
| 2212 | if (prefix) |
| 2213 | audit_log_format(ab, "%s", prefix); |
| 2214 | |
| 2215 | /* We will allow 11 spaces for ' (deleted)' to be appended */ |
| 2216 | pathname = kmalloc(PATH_MAX+11, ab->gfp_mask); |
| 2217 | if (!pathname) { |
| 2218 | audit_log_format(ab, "\"<no_memory>\""); |
| 2219 | return; |
| 2220 | } |
| 2221 | p = d_path(path, pathname, PATH_MAX+11); |
| 2222 | if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */ |
| 2223 | /* FIXME: can we save some information here? */ |
| 2224 | audit_log_format(ab, "\"<too_long>\""); |
| 2225 | } else |
| 2226 | audit_log_untrustedstring(ab, p); |
| 2227 | kfree(pathname); |
| 2228 | } |
| 2229 | |
| 2230 | void audit_log_session_info(struct audit_buffer *ab) |
| 2231 | { |
| 2232 | unsigned int sessionid = audit_get_sessionid(current); |
| 2233 | uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current)); |
| 2234 | |
| 2235 | audit_log_format(ab, "auid=%u ses=%u", auid, sessionid); |
| 2236 | } |
| 2237 | |
| 2238 | void audit_log_key(struct audit_buffer *ab, char *key) |
| 2239 | { |
| 2240 | audit_log_format(ab, " key="); |
| 2241 | if (key) |
| 2242 | audit_log_untrustedstring(ab, key); |
| 2243 | else |
| 2244 | audit_log_format(ab, "(null)"); |
| 2245 | } |
| 2246 | |
| 2247 | /** |
| 2248 | * audit_buffer_aux_new - Add an aux record buffer to the skb list |
| 2249 | * @ab: audit_buffer |
| 2250 | * @type: message type |
| 2251 | * |
| 2252 | * Aux records are allocated and added to the skb list of |
| 2253 | * the "main" record. The ab->skb is reset to point to the |
| 2254 | * aux record on its creation. When the aux record in complete |
| 2255 | * ab->skb has to be reset to point to the "main" record. |
| 2256 | * This allows the audit_log_ functions to be ignorant of |
| 2257 | * which kind of record it is logging to. It also avoids adding |
| 2258 | * special data for aux records. |
| 2259 | * |
| 2260 | * On success ab->skb will point to the new aux record. |
| 2261 | * Returns 0 on success, -ENOMEM should allocation fail. |
| 2262 | */ |
| 2263 | static int audit_buffer_aux_new(struct audit_buffer *ab, int type) |
| 2264 | { |
| 2265 | WARN_ON(ab->skb != skb_peek(&ab->skb_list)); |
| 2266 | |
| 2267 | ab->skb = nlmsg_new(AUDIT_BUFSIZ, ab->gfp_mask); |
| 2268 | if (!ab->skb) |
| 2269 | goto err; |
| 2270 | if (!nlmsg_put(ab->skb, 0, 0, type, 0, 0)) |
| 2271 | goto err; |
| 2272 | skb_queue_tail(&ab->skb_list, ab->skb); |
| 2273 | |
| 2274 | audit_log_format(ab, "audit(%llu.%03lu:%u): ", |
| 2275 | (unsigned long long)ab->stamp.ctime.tv_sec, |
| 2276 | ab->stamp.ctime.tv_nsec/1000000, |
| 2277 | ab->stamp.serial); |
| 2278 | |
| 2279 | return 0; |
| 2280 | |
| 2281 | err: |
| 2282 | kfree_skb(ab->skb); |
| 2283 | ab->skb = skb_peek(&ab->skb_list); |
| 2284 | return -ENOMEM; |
| 2285 | } |
| 2286 | |
| 2287 | /** |
| 2288 | * audit_buffer_aux_end - Switch back to the "main" record from an aux record |
| 2289 | * @ab: audit_buffer |
| 2290 | * |
| 2291 | * Restores the "main" audit record to ab->skb. |
| 2292 | */ |
| 2293 | static void audit_buffer_aux_end(struct audit_buffer *ab) |
| 2294 | { |
| 2295 | ab->skb = skb_peek(&ab->skb_list); |
| 2296 | } |
| 2297 | |
| 2298 | /** |
| 2299 | * audit_log_subj_ctx - Add LSM subject information |
| 2300 | * @ab: audit_buffer |
| 2301 | * @prop: LSM subject properties. |
| 2302 | * |
| 2303 | * Add a subj= field and, if necessary, a AUDIT_MAC_TASK_CONTEXTS record. |
| 2304 | */ |
| 2305 | int audit_log_subj_ctx(struct audit_buffer *ab, struct lsm_prop *prop) |
| 2306 | { |
| 2307 | struct lsm_context ctx; |
| 2308 | char *space = ""; |
| 2309 | int error; |
| 2310 | int i; |
| 2311 | |
| 2312 | security_current_getlsmprop_subj(prop); |
| 2313 | if (!lsmprop_is_set(prop)) |
| 2314 | return 0; |
| 2315 | |
| 2316 | if (audit_subj_secctx_cnt < 2) { |
| 2317 | error = security_lsmprop_to_secctx(prop, &ctx, LSM_ID_UNDEF); |
| 2318 | if (error < 0) { |
| 2319 | if (error != -EINVAL) |
| 2320 | goto error_path; |
| 2321 | return 0; |
| 2322 | } |
| 2323 | audit_log_format(ab, " subj=%s", ctx.context); |
| 2324 | security_release_secctx(&ctx); |
| 2325 | return 0; |
| 2326 | } |
| 2327 | /* Multiple LSMs provide contexts. Include an aux record. */ |
| 2328 | audit_log_format(ab, " subj=?"); |
| 2329 | error = audit_buffer_aux_new(ab, AUDIT_MAC_TASK_CONTEXTS); |
| 2330 | if (error) |
| 2331 | goto error_path; |
| 2332 | |
| 2333 | for (i = 0; i < audit_subj_secctx_cnt; i++) { |
| 2334 | error = security_lsmprop_to_secctx(prop, &ctx, |
| 2335 | audit_subj_lsms[i]->id); |
| 2336 | if (error < 0) { |
| 2337 | /* |
| 2338 | * Don't print anything. An LSM like BPF could |
| 2339 | * claim to support contexts, but only do so under |
| 2340 | * certain conditions. |
| 2341 | */ |
| 2342 | if (error == -EOPNOTSUPP) |
| 2343 | continue; |
| 2344 | if (error != -EINVAL) |
| 2345 | audit_panic("error in audit_log_subj_ctx"); |
| 2346 | } else { |
| 2347 | audit_log_format(ab, "%ssubj_%s=%s", space, |
| 2348 | audit_subj_lsms[i]->name, ctx.context); |
| 2349 | space = " "; |
| 2350 | security_release_secctx(&ctx); |
| 2351 | } |
| 2352 | } |
| 2353 | audit_buffer_aux_end(ab); |
| 2354 | return 0; |
| 2355 | |
| 2356 | error_path: |
| 2357 | audit_panic("error in audit_log_subj_ctx"); |
| 2358 | return error; |
| 2359 | } |
| 2360 | EXPORT_SYMBOL(audit_log_subj_ctx); |
| 2361 | |
| 2362 | int audit_log_task_context(struct audit_buffer *ab) |
| 2363 | { |
| 2364 | struct lsm_prop prop; |
| 2365 | |
| 2366 | security_current_getlsmprop_subj(&prop); |
| 2367 | return audit_log_subj_ctx(ab, &prop); |
| 2368 | } |
| 2369 | EXPORT_SYMBOL(audit_log_task_context); |
| 2370 | |
| 2371 | int audit_log_obj_ctx(struct audit_buffer *ab, struct lsm_prop *prop) |
| 2372 | { |
| 2373 | int i; |
| 2374 | int rc; |
| 2375 | int error = 0; |
| 2376 | char *space = ""; |
| 2377 | struct lsm_context ctx; |
| 2378 | |
| 2379 | if (audit_obj_secctx_cnt < 2) { |
| 2380 | error = security_lsmprop_to_secctx(prop, &ctx, LSM_ID_UNDEF); |
| 2381 | if (error < 0) { |
| 2382 | if (error != -EINVAL) |
| 2383 | goto error_path; |
| 2384 | return error; |
| 2385 | } |
| 2386 | audit_log_format(ab, " obj=%s", ctx.context); |
| 2387 | security_release_secctx(&ctx); |
| 2388 | return 0; |
| 2389 | } |
| 2390 | audit_log_format(ab, " obj=?"); |
| 2391 | error = audit_buffer_aux_new(ab, AUDIT_MAC_OBJ_CONTEXTS); |
| 2392 | if (error) |
| 2393 | goto error_path; |
| 2394 | |
| 2395 | for (i = 0; i < audit_obj_secctx_cnt; i++) { |
| 2396 | rc = security_lsmprop_to_secctx(prop, &ctx, |
| 2397 | audit_obj_lsms[i]->id); |
| 2398 | if (rc < 0) { |
| 2399 | audit_log_format(ab, "%sobj_%s=?", space, |
| 2400 | audit_obj_lsms[i]->name); |
| 2401 | if (rc != -EINVAL) |
| 2402 | audit_panic("error in audit_log_obj_ctx"); |
| 2403 | error = rc; |
| 2404 | } else { |
| 2405 | audit_log_format(ab, "%sobj_%s=%s", space, |
| 2406 | audit_obj_lsms[i]->name, ctx.context); |
| 2407 | security_release_secctx(&ctx); |
| 2408 | } |
| 2409 | space = " "; |
| 2410 | } |
| 2411 | |
| 2412 | audit_buffer_aux_end(ab); |
| 2413 | return error; |
| 2414 | |
| 2415 | error_path: |
| 2416 | audit_panic("error in audit_log_obj_ctx"); |
| 2417 | return error; |
| 2418 | } |
| 2419 | |
| 2420 | void audit_log_d_path_exe(struct audit_buffer *ab, |
| 2421 | struct mm_struct *mm) |
| 2422 | { |
| 2423 | struct file *exe_file; |
| 2424 | |
| 2425 | if (!mm) |
| 2426 | goto out_null; |
| 2427 | |
| 2428 | exe_file = get_mm_exe_file(mm); |
| 2429 | if (!exe_file) |
| 2430 | goto out_null; |
| 2431 | |
| 2432 | audit_log_d_path(ab, " exe=", &exe_file->f_path); |
| 2433 | fput(exe_file); |
| 2434 | return; |
| 2435 | out_null: |
| 2436 | audit_log_format(ab, " exe=(null)"); |
| 2437 | } |
| 2438 | |
| 2439 | struct tty_struct *audit_get_tty(void) |
| 2440 | { |
| 2441 | struct tty_struct *tty = NULL; |
| 2442 | unsigned long flags; |
| 2443 | |
| 2444 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 2445 | if (current->signal) |
| 2446 | tty = tty_kref_get(current->signal->tty); |
| 2447 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 2448 | return tty; |
| 2449 | } |
| 2450 | |
| 2451 | void audit_put_tty(struct tty_struct *tty) |
| 2452 | { |
| 2453 | tty_kref_put(tty); |
| 2454 | } |
| 2455 | |
| 2456 | void audit_log_task_info(struct audit_buffer *ab) |
| 2457 | { |
| 2458 | const struct cred *cred; |
| 2459 | char comm[sizeof(current->comm)]; |
| 2460 | struct tty_struct *tty; |
| 2461 | |
| 2462 | if (!ab) |
| 2463 | return; |
| 2464 | |
| 2465 | cred = current_cred(); |
| 2466 | tty = audit_get_tty(); |
| 2467 | audit_log_format(ab, |
| 2468 | " ppid=%d pid=%d auid=%u uid=%u gid=%u" |
| 2469 | " euid=%u suid=%u fsuid=%u" |
| 2470 | " egid=%u sgid=%u fsgid=%u tty=%s ses=%u", |
| 2471 | task_ppid_nr(current), |
| 2472 | task_tgid_nr(current), |
| 2473 | from_kuid(&init_user_ns, audit_get_loginuid(current)), |
| 2474 | from_kuid(&init_user_ns, cred->uid), |
| 2475 | from_kgid(&init_user_ns, cred->gid), |
| 2476 | from_kuid(&init_user_ns, cred->euid), |
| 2477 | from_kuid(&init_user_ns, cred->suid), |
| 2478 | from_kuid(&init_user_ns, cred->fsuid), |
| 2479 | from_kgid(&init_user_ns, cred->egid), |
| 2480 | from_kgid(&init_user_ns, cred->sgid), |
| 2481 | from_kgid(&init_user_ns, cred->fsgid), |
| 2482 | tty ? tty_name(tty) : "(none)", |
| 2483 | audit_get_sessionid(current)); |
| 2484 | audit_put_tty(tty); |
| 2485 | audit_log_format(ab, " comm="); |
| 2486 | audit_log_untrustedstring(ab, get_task_comm(comm, current)); |
| 2487 | audit_log_d_path_exe(ab, current->mm); |
| 2488 | audit_log_task_context(ab); |
| 2489 | } |
| 2490 | EXPORT_SYMBOL(audit_log_task_info); |
| 2491 | |
| 2492 | /** |
| 2493 | * audit_log_path_denied - report a path restriction denial |
| 2494 | * @type: audit message type (AUDIT_ANOM_LINK, AUDIT_ANOM_CREAT, etc) |
| 2495 | * @operation: specific operation name |
| 2496 | */ |
| 2497 | void audit_log_path_denied(int type, const char *operation) |
| 2498 | { |
| 2499 | struct audit_buffer *ab; |
| 2500 | |
| 2501 | if (!audit_enabled) |
| 2502 | return; |
| 2503 | |
| 2504 | /* Generate log with subject, operation, outcome. */ |
| 2505 | ab = audit_log_start(audit_context(), GFP_KERNEL, type); |
| 2506 | if (!ab) |
| 2507 | return; |
| 2508 | audit_log_format(ab, "op=%s", operation); |
| 2509 | audit_log_task_info(ab); |
| 2510 | audit_log_format(ab, " res=0"); |
| 2511 | audit_log_end(ab); |
| 2512 | } |
| 2513 | |
| 2514 | int audit_log_nf_skb(struct audit_buffer *ab, |
| 2515 | const struct sk_buff *skb, u8 nfproto) |
| 2516 | { |
| 2517 | /* find the IP protocol in the case of NFPROTO_BRIDGE */ |
| 2518 | if (nfproto == NFPROTO_BRIDGE) { |
| 2519 | switch (eth_hdr(skb)->h_proto) { |
| 2520 | case htons(ETH_P_IP): |
| 2521 | nfproto = NFPROTO_IPV4; |
| 2522 | break; |
| 2523 | case htons(ETH_P_IPV6): |
| 2524 | nfproto = NFPROTO_IPV6; |
| 2525 | break; |
| 2526 | default: |
| 2527 | goto unknown_proto; |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | switch (nfproto) { |
| 2532 | case NFPROTO_IPV4: { |
| 2533 | struct iphdr iph; |
| 2534 | const struct iphdr *ih; |
| 2535 | |
| 2536 | ih = skb_header_pointer(skb, skb_network_offset(skb), |
| 2537 | sizeof(iph), &iph); |
| 2538 | if (!ih) |
| 2539 | return -ENOMEM; |
| 2540 | |
| 2541 | switch (ih->protocol) { |
| 2542 | case IPPROTO_TCP: { |
| 2543 | struct tcphdr _tcph; |
| 2544 | const struct tcphdr *th; |
| 2545 | |
| 2546 | th = skb_header_pointer(skb, skb_transport_offset(skb), |
| 2547 | sizeof(_tcph), &_tcph); |
| 2548 | if (!th) |
| 2549 | return -ENOMEM; |
| 2550 | |
| 2551 | audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu sport=%hu dport=%hu", |
| 2552 | &ih->saddr, &ih->daddr, ih->protocol, |
| 2553 | ntohs(th->source), ntohs(th->dest)); |
| 2554 | break; |
| 2555 | } |
| 2556 | case IPPROTO_UDP: |
| 2557 | case IPPROTO_UDPLITE: { |
| 2558 | struct udphdr _udph; |
| 2559 | const struct udphdr *uh; |
| 2560 | |
| 2561 | uh = skb_header_pointer(skb, skb_transport_offset(skb), |
| 2562 | sizeof(_udph), &_udph); |
| 2563 | if (!uh) |
| 2564 | return -ENOMEM; |
| 2565 | |
| 2566 | audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu sport=%hu dport=%hu", |
| 2567 | &ih->saddr, &ih->daddr, ih->protocol, |
| 2568 | ntohs(uh->source), ntohs(uh->dest)); |
| 2569 | break; |
| 2570 | } |
| 2571 | case IPPROTO_SCTP: { |
| 2572 | struct sctphdr _sctph; |
| 2573 | const struct sctphdr *sh; |
| 2574 | |
| 2575 | sh = skb_header_pointer(skb, skb_transport_offset(skb), |
| 2576 | sizeof(_sctph), &_sctph); |
| 2577 | if (!sh) |
| 2578 | return -ENOMEM; |
| 2579 | |
| 2580 | audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu sport=%hu dport=%hu", |
| 2581 | &ih->saddr, &ih->daddr, ih->protocol, |
| 2582 | ntohs(sh->source), ntohs(sh->dest)); |
| 2583 | break; |
| 2584 | } |
| 2585 | default: |
| 2586 | audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu", |
| 2587 | &ih->saddr, &ih->daddr, ih->protocol); |
| 2588 | } |
| 2589 | |
| 2590 | break; |
| 2591 | } |
| 2592 | case NFPROTO_IPV6: { |
| 2593 | struct ipv6hdr iph; |
| 2594 | const struct ipv6hdr *ih; |
| 2595 | u8 nexthdr; |
| 2596 | __be16 frag_off; |
| 2597 | |
| 2598 | ih = skb_header_pointer(skb, skb_network_offset(skb), |
| 2599 | sizeof(iph), &iph); |
| 2600 | if (!ih) |
| 2601 | return -ENOMEM; |
| 2602 | |
| 2603 | nexthdr = ih->nexthdr; |
| 2604 | ipv6_skip_exthdr(skb, skb_network_offset(skb) + sizeof(iph), |
| 2605 | &nexthdr, &frag_off); |
| 2606 | |
| 2607 | switch (nexthdr) { |
| 2608 | case IPPROTO_TCP: { |
| 2609 | struct tcphdr _tcph; |
| 2610 | const struct tcphdr *th; |
| 2611 | |
| 2612 | th = skb_header_pointer(skb, skb_transport_offset(skb), |
| 2613 | sizeof(_tcph), &_tcph); |
| 2614 | if (!th) |
| 2615 | return -ENOMEM; |
| 2616 | |
| 2617 | audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu sport=%hu dport=%hu", |
| 2618 | &ih->saddr, &ih->daddr, nexthdr, |
| 2619 | ntohs(th->source), ntohs(th->dest)); |
| 2620 | break; |
| 2621 | } |
| 2622 | case IPPROTO_UDP: |
| 2623 | case IPPROTO_UDPLITE: { |
| 2624 | struct udphdr _udph; |
| 2625 | const struct udphdr *uh; |
| 2626 | |
| 2627 | uh = skb_header_pointer(skb, skb_transport_offset(skb), |
| 2628 | sizeof(_udph), &_udph); |
| 2629 | if (!uh) |
| 2630 | return -ENOMEM; |
| 2631 | |
| 2632 | audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu sport=%hu dport=%hu", |
| 2633 | &ih->saddr, &ih->daddr, nexthdr, |
| 2634 | ntohs(uh->source), ntohs(uh->dest)); |
| 2635 | break; |
| 2636 | } |
| 2637 | case IPPROTO_SCTP: { |
| 2638 | struct sctphdr _sctph; |
| 2639 | const struct sctphdr *sh; |
| 2640 | |
| 2641 | sh = skb_header_pointer(skb, skb_transport_offset(skb), |
| 2642 | sizeof(_sctph), &_sctph); |
| 2643 | if (!sh) |
| 2644 | return -ENOMEM; |
| 2645 | |
| 2646 | audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu sport=%hu dport=%hu", |
| 2647 | &ih->saddr, &ih->daddr, nexthdr, |
| 2648 | ntohs(sh->source), ntohs(sh->dest)); |
| 2649 | break; |
| 2650 | } |
| 2651 | default: |
| 2652 | audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu", |
| 2653 | &ih->saddr, &ih->daddr, nexthdr); |
| 2654 | } |
| 2655 | |
| 2656 | break; |
| 2657 | } |
| 2658 | default: |
| 2659 | goto unknown_proto; |
| 2660 | } |
| 2661 | |
| 2662 | return 0; |
| 2663 | |
| 2664 | unknown_proto: |
| 2665 | audit_log_format(ab, " saddr=? daddr=? proto=?"); |
| 2666 | return -EPFNOSUPPORT; |
| 2667 | } |
| 2668 | EXPORT_SYMBOL(audit_log_nf_skb); |
| 2669 | |
| 2670 | /* global counter which is incremented every time something logs in */ |
| 2671 | static atomic_t session_id = ATOMIC_INIT(0); |
| 2672 | |
| 2673 | static int audit_set_loginuid_perm(kuid_t loginuid) |
| 2674 | { |
| 2675 | /* if we are unset, we don't need privs */ |
| 2676 | if (!audit_loginuid_set(current)) |
| 2677 | return 0; |
| 2678 | /* if AUDIT_FEATURE_LOGINUID_IMMUTABLE means never ever allow a change*/ |
| 2679 | if (is_audit_feature_set(AUDIT_FEATURE_LOGINUID_IMMUTABLE)) |
| 2680 | return -EPERM; |
| 2681 | /* it is set, you need permission */ |
| 2682 | if (!capable(CAP_AUDIT_CONTROL)) |
| 2683 | return -EPERM; |
| 2684 | /* reject if this is not an unset and we don't allow that */ |
| 2685 | if (is_audit_feature_set(AUDIT_FEATURE_ONLY_UNSET_LOGINUID) |
| 2686 | && uid_valid(loginuid)) |
| 2687 | return -EPERM; |
| 2688 | return 0; |
| 2689 | } |
| 2690 | |
| 2691 | static void audit_log_set_loginuid(kuid_t koldloginuid, kuid_t kloginuid, |
| 2692 | unsigned int oldsessionid, |
| 2693 | unsigned int sessionid, int rc) |
| 2694 | { |
| 2695 | struct audit_buffer *ab; |
| 2696 | uid_t uid, oldloginuid, loginuid; |
| 2697 | struct tty_struct *tty; |
| 2698 | |
| 2699 | if (!audit_enabled) |
| 2700 | return; |
| 2701 | |
| 2702 | ab = audit_log_start(audit_context(), GFP_KERNEL, AUDIT_LOGIN); |
| 2703 | if (!ab) |
| 2704 | return; |
| 2705 | |
| 2706 | uid = from_kuid(&init_user_ns, task_uid(current)); |
| 2707 | oldloginuid = from_kuid(&init_user_ns, koldloginuid); |
| 2708 | loginuid = from_kuid(&init_user_ns, kloginuid); |
| 2709 | tty = audit_get_tty(); |
| 2710 | |
| 2711 | audit_log_format(ab, "pid=%d uid=%u", task_tgid_nr(current), uid); |
| 2712 | audit_log_task_context(ab); |
| 2713 | audit_log_format(ab, " old-auid=%u auid=%u tty=%s old-ses=%u ses=%u res=%d", |
| 2714 | oldloginuid, loginuid, tty ? tty_name(tty) : "(none)", |
| 2715 | oldsessionid, sessionid, !rc); |
| 2716 | audit_put_tty(tty); |
| 2717 | audit_log_end(ab); |
| 2718 | } |
| 2719 | |
| 2720 | /** |
| 2721 | * audit_set_loginuid - set current task's loginuid |
| 2722 | * @loginuid: loginuid value |
| 2723 | * |
| 2724 | * Returns 0. |
| 2725 | * |
| 2726 | * Called (set) from fs/proc/base.c::proc_loginuid_write(). |
| 2727 | */ |
| 2728 | int audit_set_loginuid(kuid_t loginuid) |
| 2729 | { |
| 2730 | unsigned int oldsessionid, sessionid = AUDIT_SID_UNSET; |
| 2731 | kuid_t oldloginuid; |
| 2732 | int rc; |
| 2733 | |
| 2734 | oldloginuid = audit_get_loginuid(current); |
| 2735 | oldsessionid = audit_get_sessionid(current); |
| 2736 | |
| 2737 | rc = audit_set_loginuid_perm(loginuid); |
| 2738 | if (rc) |
| 2739 | goto out; |
| 2740 | |
| 2741 | /* are we setting or clearing? */ |
| 2742 | if (uid_valid(loginuid)) { |
| 2743 | sessionid = (unsigned int)atomic_inc_return(&session_id); |
| 2744 | if (unlikely(sessionid == AUDIT_SID_UNSET)) |
| 2745 | sessionid = (unsigned int)atomic_inc_return(&session_id); |
| 2746 | } |
| 2747 | |
| 2748 | current->sessionid = sessionid; |
| 2749 | current->loginuid = loginuid; |
| 2750 | out: |
| 2751 | audit_log_set_loginuid(oldloginuid, loginuid, oldsessionid, sessionid, rc); |
| 2752 | return rc; |
| 2753 | } |
| 2754 | |
| 2755 | /** |
| 2756 | * audit_signal_info - record signal info for shutting down audit subsystem |
| 2757 | * @sig: signal value |
| 2758 | * @t: task being signaled |
| 2759 | * |
| 2760 | * If the audit subsystem is being terminated, record the task (pid) |
| 2761 | * and uid that is doing that. |
| 2762 | */ |
| 2763 | int audit_signal_info(int sig, struct task_struct *t) |
| 2764 | { |
| 2765 | kuid_t uid = current_uid(), auid; |
| 2766 | |
| 2767 | if (auditd_test_task(t) && |
| 2768 | (sig == SIGTERM || sig == SIGHUP || |
| 2769 | sig == SIGUSR1 || sig == SIGUSR2)) { |
| 2770 | audit_sig_pid = task_tgid_nr(current); |
| 2771 | auid = audit_get_loginuid(current); |
| 2772 | if (uid_valid(auid)) |
| 2773 | audit_sig_uid = auid; |
| 2774 | else |
| 2775 | audit_sig_uid = uid; |
| 2776 | security_current_getlsmprop_subj(&audit_sig_lsm); |
| 2777 | } |
| 2778 | |
| 2779 | return audit_signal_info_syscall(t); |
| 2780 | } |
| 2781 | |
| 2782 | /** |
| 2783 | * __audit_log_end - enqueue one audit record |
| 2784 | * @skb: the buffer to send |
| 2785 | */ |
| 2786 | static void __audit_log_end(struct sk_buff *skb) |
| 2787 | { |
| 2788 | struct nlmsghdr *nlh; |
| 2789 | |
| 2790 | if (audit_rate_check()) { |
| 2791 | /* setup the netlink header, see the comments in |
| 2792 | * kauditd_send_multicast_skb() for length quirks */ |
| 2793 | nlh = nlmsg_hdr(skb); |
| 2794 | nlh->nlmsg_len = skb->len - NLMSG_HDRLEN; |
| 2795 | |
| 2796 | /* queue the netlink packet */ |
| 2797 | skb_queue_tail(&audit_queue, skb); |
| 2798 | } else { |
| 2799 | audit_log_lost("rate limit exceeded"); |
| 2800 | kfree_skb(skb); |
| 2801 | } |
| 2802 | } |
| 2803 | |
| 2804 | /** |
| 2805 | * audit_log_end - end one audit record |
| 2806 | * @ab: the audit_buffer |
| 2807 | * |
| 2808 | * We can not do a netlink send inside an irq context because it blocks (last |
| 2809 | * arg, flags, is not set to MSG_DONTWAIT), so the audit buffer is placed on a |
| 2810 | * queue and a kthread is scheduled to remove them from the queue outside the |
| 2811 | * irq context. May be called in any context. |
| 2812 | */ |
| 2813 | void audit_log_end(struct audit_buffer *ab) |
| 2814 | { |
| 2815 | struct sk_buff *skb; |
| 2816 | |
| 2817 | if (!ab) |
| 2818 | return; |
| 2819 | |
| 2820 | while ((skb = skb_dequeue(&ab->skb_list))) |
| 2821 | __audit_log_end(skb); |
| 2822 | |
| 2823 | /* poke the kauditd thread */ |
| 2824 | wake_up_interruptible(&kauditd_wait); |
| 2825 | |
| 2826 | audit_buffer_free(ab); |
| 2827 | } |
| 2828 | |
| 2829 | /** |
| 2830 | * audit_log - Log an audit record |
| 2831 | * @ctx: audit context |
| 2832 | * @gfp_mask: type of allocation |
| 2833 | * @type: audit message type |
| 2834 | * @fmt: format string to use |
| 2835 | * @...: variable parameters matching the format string |
| 2836 | * |
| 2837 | * This is a convenience function that calls audit_log_start, |
| 2838 | * audit_log_vformat, and audit_log_end. It may be called |
| 2839 | * in any context. |
| 2840 | */ |
| 2841 | void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type, |
| 2842 | const char *fmt, ...) |
| 2843 | { |
| 2844 | struct audit_buffer *ab; |
| 2845 | va_list args; |
| 2846 | |
| 2847 | ab = audit_log_start(ctx, gfp_mask, type); |
| 2848 | if (ab) { |
| 2849 | va_start(args, fmt); |
| 2850 | audit_log_vformat(ab, fmt, args); |
| 2851 | va_end(args); |
| 2852 | audit_log_end(ab); |
| 2853 | } |
| 2854 | } |
| 2855 | |
| 2856 | EXPORT_SYMBOL(audit_log_start); |
| 2857 | EXPORT_SYMBOL(audit_log_end); |
| 2858 | EXPORT_SYMBOL(audit_log_format); |
| 2859 | EXPORT_SYMBOL(audit_log); |