| 85c8721 | 2005-04-29 16:23:29 +0100 | [diff] [blame] | 1 | /* audit.c -- Auditing support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Gateway between the kernel (e.g., selinux) and the user-space audit daemon. |
| 3 | * System-call specific features have moved to auditsc.c |
| 4 | * |
| 5 | * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina. |
| 6 | * All Rights Reserved. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | * Written by Rickard E. (Rik) Faith <faith@redhat.com> |
| 23 | * |
| 24 | * Goals: 1) Integrate fully with SELinux. |
| 25 | * 2) Minimal run-time overhead: |
| 26 | * a) Minimal when syscall auditing is disabled (audit_enable=0). |
| 27 | * b) Small when syscall auditing is enabled and no audit record |
| 28 | * is generated (defer as much work as possible to record |
| 29 | * generation time): |
| 30 | * i) context is allocated, |
| 31 | * ii) names from getname are stored without a copy, and |
| 32 | * iii) inode information stored from path_lookup. |
| 33 | * 3) Ability to disable syscall auditing at boot time (audit=0). |
| 34 | * 4) Usable by other parts of the kernel (if audit_log* is called, |
| 35 | * then a syscall record will be generated automatically for the |
| 36 | * current syscall). |
| 37 | * 5) Netlink interface to user-space. |
| 38 | * 6) Support low-overhead kernel-based filtering to minimize the |
| 39 | * information that must be passed to user-space. |
| 40 | * |
| 85c8721 | 2005-04-29 16:23:29 +0100 | [diff] [blame] | 41 | * Example user-space utilities: http://people.redhat.com/sgrubb/audit/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | */ |
| 43 | |
| 44 | #include <linux/init.h> |
| 45 | #include <asm/atomic.h> |
| 46 | #include <asm/types.h> |
| 47 | #include <linux/mm.h> |
| 48 | #include <linux/module.h> |
| 49 | |
| 50 | #include <linux/audit.h> |
| 51 | |
| 52 | #include <net/sock.h> |
| 53 | #include <linux/skbuff.h> |
| 54 | #include <linux/netlink.h> |
| 55 | |
| 56 | /* No auditing will take place until audit_initialized != 0. |
| 57 | * (Initialization happens after skb_init is called.) */ |
| 58 | static int audit_initialized; |
| 59 | |
| 60 | /* No syscall auditing will take place unless audit_enabled != 0. */ |
| 61 | int audit_enabled; |
| 62 | |
| 63 | /* Default state when kernel boots without any parameters. */ |
| 64 | static int audit_default; |
| 65 | |
| 66 | /* If auditing cannot proceed, audit_failure selects what happens. */ |
| 67 | static int audit_failure = AUDIT_FAIL_PRINTK; |
| 68 | |
| 69 | /* If audit records are to be written to the netlink socket, audit_pid |
| 70 | * contains the (non-zero) pid. */ |
| 71 | static int audit_pid; |
| 72 | |
| 73 | /* If audit_limit is non-zero, limit the rate of sending audit records |
| 74 | * to that number per second. This prevents DoS attacks, but results in |
| 75 | * audit records being dropped. */ |
| 76 | static int audit_rate_limit; |
| 77 | |
| 78 | /* Number of outstanding audit_buffers allowed. */ |
| 79 | static int audit_backlog_limit = 64; |
| 80 | static atomic_t audit_backlog = ATOMIC_INIT(0); |
| 81 | |
| 82 | /* Records can be lost in several ways: |
| 83 | 0) [suppressed in audit_alloc] |
| 84 | 1) out of memory in audit_log_start [kmalloc of struct audit_buffer] |
| 85 | 2) out of memory in audit_log_move [alloc_skb] |
| 86 | 3) suppressed due to audit_rate_limit |
| 87 | 4) suppressed due to audit_backlog_limit |
| 88 | */ |
| 89 | static atomic_t audit_lost = ATOMIC_INIT(0); |
| 90 | |
| 91 | /* The netlink socket. */ |
| 92 | static struct sock *audit_sock; |
| 93 | |
| 94 | /* There are two lists of audit buffers. The txlist contains audit |
| 95 | * buffers that cannot be sent immediately to the netlink device because |
| 96 | * we are in an irq context (these are sent later in a tasklet). |
| 97 | * |
| 98 | * The second list is a list of pre-allocated audit buffers (if more |
| 99 | * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of |
| 100 | * being placed on the freelist). */ |
| 101 | static DEFINE_SPINLOCK(audit_txlist_lock); |
| 102 | static DEFINE_SPINLOCK(audit_freelist_lock); |
| 103 | static int audit_freelist_count = 0; |
| 104 | static LIST_HEAD(audit_txlist); |
| 105 | static LIST_HEAD(audit_freelist); |
| 106 | |
| 107 | /* There are three lists of rules -- one to search at task creation |
| 108 | * time, one to search at syscall entry time, and another to search at |
| 109 | * syscall exit time. */ |
| 110 | static LIST_HEAD(audit_tsklist); |
| 111 | static LIST_HEAD(audit_entlist); |
| 112 | static LIST_HEAD(audit_extlist); |
| 113 | |
| 114 | /* The netlink socket is only to be read by 1 CPU, which lets us assume |
| 115 | * that list additions and deletions never happen simultaneiously in |
| 116 | * auditsc.c */ |
| 117 | static DECLARE_MUTEX(audit_netlink_sem); |
| 118 | |
| 119 | /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting |
| 120 | * audit records. Since printk uses a 1024 byte buffer, this buffer |
| 121 | * should be at least that large. */ |
| 122 | #define AUDIT_BUFSIZ 1024 |
| 123 | |
| 124 | /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the |
| 125 | * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */ |
| 126 | #define AUDIT_MAXFREE (2*NR_CPUS) |
| 127 | |
| 128 | /* The audit_buffer is used when formatting an audit record. The caller |
| 129 | * locks briefly to get the record off the freelist or to allocate the |
| 130 | * buffer, and locks briefly to send the buffer to the netlink layer or |
| 131 | * to place it on a transmit queue. Multiple audit_buffers can be in |
| 132 | * use simultaneously. */ |
| 133 | struct audit_buffer { |
| 134 | struct list_head list; |
| 135 | struct sk_buff_head sklist; /* formatted skbs ready to send */ |
| 136 | struct audit_context *ctx; /* NULL or associated context */ |
| 137 | int len; /* used area of tmp */ |
| 138 | char tmp[AUDIT_BUFSIZ]; |
| 139 | |
| 140 | /* Pointer to header and contents */ |
| 141 | struct nlmsghdr *nlh; |
| 142 | int total; |
| 143 | int type; |
| 144 | int pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | void audit_set_type(struct audit_buffer *ab, int type) |
| 148 | { |
| 149 | ab->type = type; |
| 150 | } |
| 151 | |
| 152 | struct audit_entry { |
| 153 | struct list_head list; |
| 154 | struct audit_rule rule; |
| 155 | }; |
| 156 | |
| 157 | static void audit_log_end_irq(struct audit_buffer *ab); |
| 158 | static void audit_log_end_fast(struct audit_buffer *ab); |
| 159 | |
| 160 | static void audit_panic(const char *message) |
| 161 | { |
| 162 | switch (audit_failure) |
| 163 | { |
| 164 | case AUDIT_FAIL_SILENT: |
| 165 | break; |
| 166 | case AUDIT_FAIL_PRINTK: |
| 167 | printk(KERN_ERR "audit: %s\n", message); |
| 168 | break; |
| 169 | case AUDIT_FAIL_PANIC: |
| 170 | panic("audit: %s\n", message); |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static inline int audit_rate_check(void) |
| 176 | { |
| 177 | static unsigned long last_check = 0; |
| 178 | static int messages = 0; |
| 179 | static DEFINE_SPINLOCK(lock); |
| 180 | unsigned long flags; |
| 181 | unsigned long now; |
| 182 | unsigned long elapsed; |
| 183 | int retval = 0; |
| 184 | |
| 185 | if (!audit_rate_limit) return 1; |
| 186 | |
| 187 | spin_lock_irqsave(&lock, flags); |
| 188 | if (++messages < audit_rate_limit) { |
| 189 | retval = 1; |
| 190 | } else { |
| 191 | now = jiffies; |
| 192 | elapsed = now - last_check; |
| 193 | if (elapsed > HZ) { |
| 194 | last_check = now; |
| 195 | messages = 0; |
| 196 | retval = 1; |
| 197 | } |
| 198 | } |
| 199 | spin_unlock_irqrestore(&lock, flags); |
| 200 | |
| 201 | return retval; |
| 202 | } |
| 203 | |
| 204 | /* Emit at least 1 message per second, even if audit_rate_check is |
| 205 | * throttling. */ |
| 206 | void audit_log_lost(const char *message) |
| 207 | { |
| 208 | static unsigned long last_msg = 0; |
| 209 | static DEFINE_SPINLOCK(lock); |
| 210 | unsigned long flags; |
| 211 | unsigned long now; |
| 212 | int print; |
| 213 | |
| 214 | atomic_inc(&audit_lost); |
| 215 | |
| 216 | print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit); |
| 217 | |
| 218 | if (!print) { |
| 219 | spin_lock_irqsave(&lock, flags); |
| 220 | now = jiffies; |
| 221 | if (now - last_msg > HZ) { |
| 222 | print = 1; |
| 223 | last_msg = now; |
| 224 | } |
| 225 | spin_unlock_irqrestore(&lock, flags); |
| 226 | } |
| 227 | |
| 228 | if (print) { |
| 229 | printk(KERN_WARNING |
| 230 | "audit: audit_lost=%d audit_backlog=%d" |
| 231 | " audit_rate_limit=%d audit_backlog_limit=%d\n", |
| 232 | atomic_read(&audit_lost), |
| 233 | atomic_read(&audit_backlog), |
| 234 | audit_rate_limit, |
| 235 | audit_backlog_limit); |
| 236 | audit_panic(message); |
| 237 | } |
| 238 | |
| 239 | } |
| 240 | |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 241 | static int audit_set_rate_limit(int limit, uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
| 243 | int old = audit_rate_limit; |
| 244 | audit_rate_limit = limit; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 245 | audit_log(NULL, "audit_rate_limit=%d old=%d by auid %u", |
| 246 | audit_rate_limit, old, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | return old; |
| 248 | } |
| 249 | |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 250 | static int audit_set_backlog_limit(int limit, uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
| 252 | int old = audit_backlog_limit; |
| 253 | audit_backlog_limit = limit; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 254 | audit_log(NULL, "audit_backlog_limit=%d old=%d by auid %u", |
| 255 | audit_backlog_limit, old, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | return old; |
| 257 | } |
| 258 | |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 259 | static int audit_set_enabled(int state, uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
| 261 | int old = audit_enabled; |
| 262 | if (state != 0 && state != 1) |
| 263 | return -EINVAL; |
| 264 | audit_enabled = state; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 265 | audit_log(NULL, "audit_enabled=%d old=%d by auid %u", |
| 266 | audit_enabled, old, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | return old; |
| 268 | } |
| 269 | |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 270 | static int audit_set_failure(int state, uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
| 272 | int old = audit_failure; |
| 273 | if (state != AUDIT_FAIL_SILENT |
| 274 | && state != AUDIT_FAIL_PRINTK |
| 275 | && state != AUDIT_FAIL_PANIC) |
| 276 | return -EINVAL; |
| 277 | audit_failure = state; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 278 | audit_log(NULL, "audit_failure=%d old=%d by auid %u", |
| 279 | audit_failure, old, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | return old; |
| 281 | } |
| 282 | |
| 283 | #ifdef CONFIG_NET |
| 284 | void audit_send_reply(int pid, int seq, int type, int done, int multi, |
| 285 | void *payload, int size) |
| 286 | { |
| 287 | struct sk_buff *skb; |
| 288 | struct nlmsghdr *nlh; |
| 289 | int len = NLMSG_SPACE(size); |
| 290 | void *data; |
| 291 | int flags = multi ? NLM_F_MULTI : 0; |
| 292 | int t = done ? NLMSG_DONE : type; |
| 293 | |
| 294 | skb = alloc_skb(len, GFP_KERNEL); |
| 295 | if (!skb) |
| 296 | goto nlmsg_failure; |
| 297 | |
| 298 | nlh = NLMSG_PUT(skb, pid, seq, t, len - sizeof(*nlh)); |
| 299 | nlh->nlmsg_flags = flags; |
| 300 | data = NLMSG_DATA(nlh); |
| 301 | memcpy(data, payload, size); |
| 302 | netlink_unicast(audit_sock, skb, pid, MSG_DONTWAIT); |
| 303 | return; |
| 304 | |
| 305 | nlmsg_failure: /* Used by NLMSG_PUT */ |
| 306 | if (skb) |
| 307 | kfree_skb(skb); |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Check for appropriate CAP_AUDIT_ capabilities on incoming audit |
| 312 | * control messages. |
| 313 | */ |
| 314 | static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type) |
| 315 | { |
| 316 | int err = 0; |
| 317 | |
| 318 | switch (msg_type) { |
| 319 | case AUDIT_GET: |
| 320 | case AUDIT_LIST: |
| 321 | case AUDIT_SET: |
| 322 | case AUDIT_ADD: |
| 323 | case AUDIT_DEL: |
| 324 | if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL)) |
| 325 | err = -EPERM; |
| 326 | break; |
| 327 | case AUDIT_USER: |
| 328 | if (!cap_raised(eff_cap, CAP_AUDIT_WRITE)) |
| 329 | err = -EPERM; |
| 330 | break; |
| 331 | default: /* bad msg */ |
| 332 | err = -EINVAL; |
| 333 | } |
| 334 | |
| 335 | return err; |
| 336 | } |
| 337 | |
| 338 | static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 339 | { |
| 340 | u32 uid, pid, seq; |
| 341 | void *data; |
| 342 | struct audit_status *status_get, status_set; |
| 343 | int err; |
| 344 | struct audit_buffer *ab; |
| 345 | u16 msg_type = nlh->nlmsg_type; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 346 | uid_t loginuid; /* loginuid of sender */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type); |
| 349 | if (err) |
| 350 | return err; |
| 351 | |
| 352 | pid = NETLINK_CREDS(skb)->pid; |
| 353 | uid = NETLINK_CREDS(skb)->uid; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 354 | loginuid = NETLINK_CB(skb).loginuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | seq = nlh->nlmsg_seq; |
| 356 | data = NLMSG_DATA(nlh); |
| 357 | |
| 358 | switch (msg_type) { |
| 359 | case AUDIT_GET: |
| 360 | status_set.enabled = audit_enabled; |
| 361 | status_set.failure = audit_failure; |
| 362 | status_set.pid = audit_pid; |
| 363 | status_set.rate_limit = audit_rate_limit; |
| 364 | status_set.backlog_limit = audit_backlog_limit; |
| 365 | status_set.lost = atomic_read(&audit_lost); |
| 366 | status_set.backlog = atomic_read(&audit_backlog); |
| 367 | audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0, |
| 368 | &status_set, sizeof(status_set)); |
| 369 | break; |
| 370 | case AUDIT_SET: |
| 371 | if (nlh->nlmsg_len < sizeof(struct audit_status)) |
| 372 | return -EINVAL; |
| 373 | status_get = (struct audit_status *)data; |
| 374 | if (status_get->mask & AUDIT_STATUS_ENABLED) { |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 375 | err = audit_set_enabled(status_get->enabled, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | if (err < 0) return err; |
| 377 | } |
| 378 | if (status_get->mask & AUDIT_STATUS_FAILURE) { |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 379 | err = audit_set_failure(status_get->failure, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if (err < 0) return err; |
| 381 | } |
| 382 | if (status_get->mask & AUDIT_STATUS_PID) { |
| 383 | int old = audit_pid; |
| 384 | audit_pid = status_get->pid; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 385 | audit_log(NULL, "audit_pid=%d old=%d by auid %u", |
| 386 | audit_pid, old, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 389 | audit_set_rate_limit(status_get->rate_limit, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT) |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 391 | audit_set_backlog_limit(status_get->backlog_limit, |
| 392 | loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | break; |
| 394 | case AUDIT_USER: |
| 395 | ab = audit_log_start(NULL); |
| 396 | if (!ab) |
| 397 | break; /* audit_panic has been called */ |
| 398 | audit_log_format(ab, |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 399 | "user pid=%d uid=%d length=%d loginuid=%u" |
| 400 | " msg='%.1024s'", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | pid, uid, |
| 402 | (int)(nlh->nlmsg_len |
| 403 | - ((char *)data - (char *)nlh)), |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 404 | loginuid, (char *)data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | ab->type = AUDIT_USER; |
| 406 | ab->pid = pid; |
| 407 | audit_log_end(ab); |
| 408 | break; |
| 409 | case AUDIT_ADD: |
| 410 | case AUDIT_DEL: |
| 411 | if (nlh->nlmsg_len < sizeof(struct audit_rule)) |
| 412 | return -EINVAL; |
| 413 | /* fallthrough */ |
| 414 | case AUDIT_LIST: |
| 415 | #ifdef CONFIG_AUDITSYSCALL |
| 416 | err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid, |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 417 | uid, seq, data, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | #else |
| 419 | err = -EOPNOTSUPP; |
| 420 | #endif |
| 421 | break; |
| 422 | default: |
| 423 | err = -EINVAL; |
| 424 | break; |
| 425 | } |
| 426 | |
| 427 | return err < 0 ? err : 0; |
| 428 | } |
| 429 | |
| 430 | /* Get message from skb (based on rtnetlink_rcv_skb). Each message is |
| 431 | * processed by audit_receive_msg. Malformed skbs with wrong length are |
| 432 | * discarded silently. */ |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 433 | static void audit_receive_skb(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
| 435 | int err; |
| 436 | struct nlmsghdr *nlh; |
| 437 | u32 rlen; |
| 438 | |
| 439 | while (skb->len >= NLMSG_SPACE(0)) { |
| 440 | nlh = (struct nlmsghdr *)skb->data; |
| 441 | if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len) |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 442 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 444 | if (rlen > skb->len) |
| 445 | rlen = skb->len; |
| 446 | if ((err = audit_receive_msg(skb, nlh))) { |
| 447 | netlink_ack(skb, nlh, err); |
| 448 | } else if (nlh->nlmsg_flags & NLM_F_ACK) |
| 449 | netlink_ack(skb, nlh, 0); |
| 450 | skb_pull(skb, rlen); |
| 451 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* Receive messages from netlink socket. */ |
| 455 | static void audit_receive(struct sock *sk, int length) |
| 456 | { |
| 457 | struct sk_buff *skb; |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 458 | unsigned int qlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 460 | down(&audit_netlink_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 462 | for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) { |
| 463 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 464 | audit_receive_skb(skb); |
| 465 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | up(&audit_netlink_sem); |
| 468 | } |
| 469 | |
| 470 | /* Move data from tmp buffer into an skb. This is an extra copy, and |
| 471 | * that is unfortunate. However, the copy will only occur when a record |
| 472 | * is being written to user space, which is already a high-overhead |
| 473 | * operation. (Elimination of the copy is possible, for example, by |
| 474 | * writing directly into a pre-allocated skb, at the cost of wasting |
| 475 | * memory. */ |
| 476 | static void audit_log_move(struct audit_buffer *ab) |
| 477 | { |
| 478 | struct sk_buff *skb; |
| 479 | char *start; |
| 480 | int extra = ab->nlh ? 0 : NLMSG_SPACE(0); |
| 481 | |
| 482 | /* possible resubmission */ |
| 483 | if (ab->len == 0) |
| 484 | return; |
| 485 | |
Chris Wright | 0dd8e06 | 2005-05-03 14:01:15 +0100 | [diff] [blame] | 486 | skb = skb_peek_tail(&ab->sklist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | if (!skb || skb_tailroom(skb) <= ab->len + extra) { |
| 488 | skb = alloc_skb(2 * ab->len + extra, GFP_ATOMIC); |
| 489 | if (!skb) { |
| 490 | ab->len = 0; /* Lose information in ab->tmp */ |
| 491 | audit_log_lost("out of memory in audit_log_move"); |
| 492 | return; |
| 493 | } |
| 494 | __skb_queue_tail(&ab->sklist, skb); |
| 495 | if (!ab->nlh) |
| 496 | ab->nlh = (struct nlmsghdr *)skb_put(skb, |
| 497 | NLMSG_SPACE(0)); |
| 498 | } |
| 499 | start = skb_put(skb, ab->len); |
| 500 | memcpy(start, ab->tmp, ab->len); |
| 501 | ab->len = 0; |
| 502 | } |
| 503 | |
| 504 | /* Iterate over the skbuff in the audit_buffer, sending their contents |
| 505 | * to user space. */ |
| 506 | static inline int audit_log_drain(struct audit_buffer *ab) |
| 507 | { |
| 508 | struct sk_buff *skb; |
| 509 | |
| 510 | while ((skb = skb_dequeue(&ab->sklist))) { |
| 511 | int retval = 0; |
| 512 | |
| 513 | if (audit_pid) { |
| 514 | if (ab->nlh) { |
| 515 | ab->nlh->nlmsg_len = ab->total; |
| 516 | ab->nlh->nlmsg_type = ab->type; |
| 517 | ab->nlh->nlmsg_flags = 0; |
| 518 | ab->nlh->nlmsg_seq = 0; |
| 519 | ab->nlh->nlmsg_pid = ab->pid; |
| 520 | } |
| 521 | skb_get(skb); /* because netlink_* frees */ |
| 522 | retval = netlink_unicast(audit_sock, skb, audit_pid, |
| 523 | MSG_DONTWAIT); |
| 524 | } |
Chris Wright | 37509e7 | 2005-04-29 17:19:14 +0100 | [diff] [blame] | 525 | if (retval == -EAGAIN && |
| 526 | (atomic_read(&audit_backlog)) < audit_backlog_limit) { |
| 527 | skb_queue_head(&ab->sklist, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | audit_log_end_irq(ab); |
| 529 | return 1; |
| 530 | } |
| 531 | if (retval < 0) { |
| 532 | if (retval == -ECONNREFUSED) { |
| 533 | printk(KERN_ERR |
| 534 | "audit: *NO* daemon at audit_pid=%d\n", |
| 535 | audit_pid); |
| 536 | audit_pid = 0; |
| 537 | } else |
| 538 | audit_log_lost("netlink socket too busy"); |
| 539 | } |
| 540 | if (!audit_pid) { /* No daemon */ |
| 541 | int offset = ab->nlh ? NLMSG_SPACE(0) : 0; |
| 542 | int len = skb->len - offset; |
Peter Martuccelli | c7fcb0e | 2005-04-29 16:10:24 +0100 | [diff] [blame] | 543 | skb->data[offset + len] = '\0'; |
| 544 | printk(KERN_ERR "%s\n", skb->data + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } |
| 546 | kfree_skb(skb); |
| 547 | ab->nlh = NULL; |
| 548 | } |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | /* Initialize audit support at boot time. */ |
| 553 | static int __init audit_init(void) |
| 554 | { |
| 555 | printk(KERN_INFO "audit: initializing netlink socket (%s)\n", |
| 556 | audit_default ? "enabled" : "disabled"); |
| 557 | audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive); |
| 558 | if (!audit_sock) |
| 559 | audit_panic("cannot initialize netlink socket"); |
| 560 | |
| 561 | audit_initialized = 1; |
| 562 | audit_enabled = audit_default; |
| 563 | audit_log(NULL, "initialized"); |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | #else |
| 568 | /* Without CONFIG_NET, we have no skbuffs. For now, print what we have |
| 569 | * in the buffer. */ |
| 570 | static void audit_log_move(struct audit_buffer *ab) |
| 571 | { |
| 572 | printk(KERN_ERR "%*.*s\n", ab->len, ab->len, ab->tmp); |
| 573 | ab->len = 0; |
| 574 | } |
| 575 | |
| 576 | static inline int audit_log_drain(struct audit_buffer *ab) |
| 577 | { |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | /* Initialize audit support at boot time. */ |
| 582 | int __init audit_init(void) |
| 583 | { |
| 584 | printk(KERN_INFO "audit: initializing WITHOUT netlink support\n"); |
| 585 | audit_sock = NULL; |
| 586 | audit_pid = 0; |
| 587 | |
| 588 | audit_initialized = 1; |
| 589 | audit_enabled = audit_default; |
| 590 | audit_log(NULL, "initialized"); |
| 591 | return 0; |
| 592 | } |
| 593 | #endif |
| 594 | |
| 595 | __initcall(audit_init); |
| 596 | |
| 597 | /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */ |
| 598 | static int __init audit_enable(char *str) |
| 599 | { |
| 600 | audit_default = !!simple_strtol(str, NULL, 0); |
| 601 | printk(KERN_INFO "audit: %s%s\n", |
| 602 | audit_default ? "enabled" : "disabled", |
| 603 | audit_initialized ? "" : " (after initialization)"); |
| 604 | if (audit_initialized) |
| 605 | audit_enabled = audit_default; |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | __setup("audit=", audit_enable); |
| 610 | |
| 611 | |
| 612 | /* Obtain an audit buffer. This routine does locking to obtain the |
| 613 | * audit buffer, but then no locking is required for calls to |
| 614 | * audit_log_*format. If the tsk is a task that is currently in a |
| 615 | * syscall, then the syscall is marked as auditable and an audit record |
| 616 | * will be written at syscall exit. If there is no associated task, tsk |
| 617 | * should be NULL. */ |
| 618 | struct audit_buffer *audit_log_start(struct audit_context *ctx) |
| 619 | { |
| 620 | struct audit_buffer *ab = NULL; |
| 621 | unsigned long flags; |
| 622 | struct timespec t; |
Steve Grubb | d812ddb | 2005-04-29 16:09:52 +0100 | [diff] [blame] | 623 | unsigned int serial; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
| 625 | if (!audit_initialized) |
| 626 | return NULL; |
| 627 | |
| 628 | if (audit_backlog_limit |
| 629 | && atomic_read(&audit_backlog) > audit_backlog_limit) { |
| 630 | if (audit_rate_check()) |
| 631 | printk(KERN_WARNING |
| 632 | "audit: audit_backlog=%d > " |
| 633 | "audit_backlog_limit=%d\n", |
| 634 | atomic_read(&audit_backlog), |
| 635 | audit_backlog_limit); |
| 636 | audit_log_lost("backlog limit exceeded"); |
| 637 | return NULL; |
| 638 | } |
| 639 | |
| 640 | spin_lock_irqsave(&audit_freelist_lock, flags); |
| 641 | if (!list_empty(&audit_freelist)) { |
| 642 | ab = list_entry(audit_freelist.next, |
| 643 | struct audit_buffer, list); |
| 644 | list_del(&ab->list); |
| 645 | --audit_freelist_count; |
| 646 | } |
| 647 | spin_unlock_irqrestore(&audit_freelist_lock, flags); |
| 648 | |
| 649 | if (!ab) |
| 650 | ab = kmalloc(sizeof(*ab), GFP_ATOMIC); |
| 651 | if (!ab) { |
| 652 | audit_log_lost("out of memory in audit_log_start"); |
| 653 | return NULL; |
| 654 | } |
| 655 | |
| 656 | atomic_inc(&audit_backlog); |
| 657 | skb_queue_head_init(&ab->sklist); |
| 658 | |
| 659 | ab->ctx = ctx; |
| 660 | ab->len = 0; |
| 661 | ab->nlh = NULL; |
| 662 | ab->total = 0; |
| 663 | ab->type = AUDIT_KERNEL; |
| 664 | ab->pid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
| 666 | #ifdef CONFIG_AUDITSYSCALL |
| 667 | if (ab->ctx) |
| 668 | audit_get_stamp(ab->ctx, &t, &serial); |
| 669 | else |
| 670 | #endif |
Steve Grubb | d812ddb | 2005-04-29 16:09:52 +0100 | [diff] [blame] | 671 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | t = CURRENT_TIME; |
Steve Grubb | d812ddb | 2005-04-29 16:09:52 +0100 | [diff] [blame] | 673 | serial = 0; |
| 674 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | audit_log_format(ab, "audit(%lu.%03lu:%u): ", |
| 676 | t.tv_sec, t.tv_nsec/1000000, serial); |
| 677 | return ab; |
| 678 | } |
| 679 | |
| 680 | |
| 681 | /* Format an audit message into the audit buffer. If there isn't enough |
| 682 | * room in the audit buffer, more room will be allocated and vsnprint |
| 683 | * will be called a second time. Currently, we assume that a printk |
| 684 | * can't format message larger than 1024 bytes, so we don't either. */ |
| 685 | static void audit_log_vformat(struct audit_buffer *ab, const char *fmt, |
| 686 | va_list args) |
| 687 | { |
| 688 | int len, avail; |
| 689 | |
| 690 | if (!ab) |
| 691 | return; |
| 692 | |
| 693 | avail = sizeof(ab->tmp) - ab->len; |
| 694 | if (avail <= 0) { |
| 695 | audit_log_move(ab); |
| 696 | avail = sizeof(ab->tmp) - ab->len; |
| 697 | } |
| 698 | len = vsnprintf(ab->tmp + ab->len, avail, fmt, args); |
| 699 | if (len >= avail) { |
| 700 | /* The printk buffer is 1024 bytes long, so if we get |
| 701 | * here and AUDIT_BUFSIZ is at least 1024, then we can |
| 702 | * log everything that printk could have logged. */ |
| 703 | audit_log_move(ab); |
| 704 | avail = sizeof(ab->tmp) - ab->len; |
| 705 | len = vsnprintf(ab->tmp + ab->len, avail, fmt, args); |
| 706 | } |
| 707 | ab->len += (len < avail) ? len : avail; |
| 708 | ab->total += (len < avail) ? len : avail; |
| 709 | } |
| 710 | |
| 711 | /* Format a message into the audit buffer. All the work is done in |
| 712 | * audit_log_vformat. */ |
| 713 | void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) |
| 714 | { |
| 715 | va_list args; |
| 716 | |
| 717 | if (!ab) |
| 718 | return; |
| 719 | va_start(args, fmt); |
| 720 | audit_log_vformat(ab, fmt, args); |
| 721 | va_end(args); |
| 722 | } |
| 723 | |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 724 | void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, size_t len) |
| 725 | { |
| 726 | int i; |
| 727 | |
| 728 | for (i=0; i<len; i++) |
| 729 | audit_log_format(ab, "%02x", buf[i]); |
| 730 | } |
| 731 | |
| 732 | void audit_log_untrustedstring(struct audit_buffer *ab, const char *string) |
| 733 | { |
Andrew Morton | 81b7854 | 2005-04-29 15:59:11 +0100 | [diff] [blame] | 734 | const unsigned char *p = string; |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 735 | |
| 736 | while (*p) { |
| 737 | if (*p == '"' || *p == ' ' || *p < 0x20 || *p > 0x7f) { |
| 738 | audit_log_hex(ab, string, strlen(string)); |
| 739 | return; |
| 740 | } |
| 741 | p++; |
| 742 | } |
| 743 | audit_log_format(ab, "\"%s\"", string); |
| 744 | } |
| 745 | |
| 746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | /* This is a helper-function to print the d_path without using a static |
| 748 | * buffer or allocating another buffer in addition to the one in |
| 749 | * audit_buffer. */ |
| 750 | void audit_log_d_path(struct audit_buffer *ab, const char *prefix, |
| 751 | struct dentry *dentry, struct vfsmount *vfsmnt) |
| 752 | { |
| 753 | char *p; |
| 754 | int len, avail; |
| 755 | |
| 756 | if (prefix) audit_log_format(ab, " %s", prefix); |
| 757 | |
| 758 | if (ab->len > 128) |
| 759 | audit_log_move(ab); |
| 760 | avail = sizeof(ab->tmp) - ab->len; |
| 761 | p = d_path(dentry, vfsmnt, ab->tmp + ab->len, avail); |
| 762 | if (IS_ERR(p)) { |
| 763 | /* FIXME: can we save some information here? */ |
| 764 | audit_log_format(ab, "<toolong>"); |
| 765 | } else { |
| 766 | /* path isn't at start of buffer */ |
| 767 | len = (ab->tmp + sizeof(ab->tmp) - 1) - p; |
| 768 | memmove(ab->tmp + ab->len, p, len); |
| 769 | ab->len += len; |
| 770 | ab->total += len; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | /* Remove queued messages from the audit_txlist and send them to userspace. */ |
| 775 | static void audit_tasklet_handler(unsigned long arg) |
| 776 | { |
| 777 | LIST_HEAD(list); |
| 778 | struct audit_buffer *ab; |
| 779 | unsigned long flags; |
| 780 | |
| 781 | spin_lock_irqsave(&audit_txlist_lock, flags); |
| 782 | list_splice_init(&audit_txlist, &list); |
| 783 | spin_unlock_irqrestore(&audit_txlist_lock, flags); |
| 784 | |
| 785 | while (!list_empty(&list)) { |
| 786 | ab = list_entry(list.next, struct audit_buffer, list); |
| 787 | list_del(&ab->list); |
| 788 | audit_log_end_fast(ab); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0); |
| 793 | |
| 794 | /* The netlink_* functions cannot be called inside an irq context, so |
| 795 | * the audit buffer is places on a queue and a tasklet is scheduled to |
| 796 | * remove them from the queue outside the irq context. May be called in |
| 797 | * any context. */ |
| 798 | static void audit_log_end_irq(struct audit_buffer *ab) |
| 799 | { |
| 800 | unsigned long flags; |
| 801 | |
| 802 | if (!ab) |
| 803 | return; |
| 804 | spin_lock_irqsave(&audit_txlist_lock, flags); |
| 805 | list_add_tail(&ab->list, &audit_txlist); |
| 806 | spin_unlock_irqrestore(&audit_txlist_lock, flags); |
| 807 | |
| 808 | tasklet_schedule(&audit_tasklet); |
| 809 | } |
| 810 | |
| 811 | /* Send the message in the audit buffer directly to user space. May not |
| 812 | * be called in an irq context. */ |
| 813 | static void audit_log_end_fast(struct audit_buffer *ab) |
| 814 | { |
| 815 | unsigned long flags; |
| 816 | |
| 817 | BUG_ON(in_irq()); |
| 818 | if (!ab) |
| 819 | return; |
| 820 | if (!audit_rate_check()) { |
| 821 | audit_log_lost("rate limit exceeded"); |
| 822 | } else { |
| 823 | audit_log_move(ab); |
| 824 | if (audit_log_drain(ab)) |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | atomic_dec(&audit_backlog); |
| 829 | spin_lock_irqsave(&audit_freelist_lock, flags); |
| 830 | if (++audit_freelist_count > AUDIT_MAXFREE) |
| 831 | kfree(ab); |
| 832 | else |
| 833 | list_add(&ab->list, &audit_freelist); |
| 834 | spin_unlock_irqrestore(&audit_freelist_lock, flags); |
| 835 | } |
| 836 | |
| 837 | /* Send or queue the message in the audit buffer, depending on the |
| 838 | * current context. (A convenience function that may be called in any |
| 839 | * context.) */ |
| 840 | void audit_log_end(struct audit_buffer *ab) |
| 841 | { |
| 842 | if (in_irq()) |
| 843 | audit_log_end_irq(ab); |
| 844 | else |
| 845 | audit_log_end_fast(ab); |
| 846 | } |
| 847 | |
| 848 | /* Log an audit record. This is a convenience function that calls |
| 849 | * audit_log_start, audit_log_vformat, and audit_log_end. It may be |
| 850 | * called in any context. */ |
| 851 | void audit_log(struct audit_context *ctx, const char *fmt, ...) |
| 852 | { |
| 853 | struct audit_buffer *ab; |
| 854 | va_list args; |
| 855 | |
| 856 | ab = audit_log_start(ctx); |
| 857 | if (ab) { |
| 858 | va_start(args, fmt); |
| 859 | audit_log_vformat(ab, fmt, args); |
| 860 | va_end(args); |
| 861 | audit_log_end(ab); |
| 862 | } |
| 863 | } |