| 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <asm/types.h> |
Alan Cox | 715b49e | 2006-01-18 17:44:07 -0800 | [diff] [blame] | 46 | #include <asm/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/mm.h> |
| 48 | #include <linux/module.h> |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 49 | #include <linux/err.h> |
| 50 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | #include <linux/audit.h> |
| 53 | |
| 54 | #include <net/sock.h> |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 55 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <linux/skbuff.h> |
| 57 | #include <linux/netlink.h> |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 58 | #include <linux/selinux.h> |
| 59 | |
| 60 | #include "audit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | /* No auditing will take place until audit_initialized != 0. |
| 63 | * (Initialization happens after skb_init is called.) */ |
| 64 | static int audit_initialized; |
| 65 | |
| 66 | /* No syscall auditing will take place unless audit_enabled != 0. */ |
| 67 | int audit_enabled; |
| 68 | |
| 69 | /* Default state when kernel boots without any parameters. */ |
| 70 | static int audit_default; |
| 71 | |
| 72 | /* If auditing cannot proceed, audit_failure selects what happens. */ |
| 73 | static int audit_failure = AUDIT_FAIL_PRINTK; |
| 74 | |
| 75 | /* If audit records are to be written to the netlink socket, audit_pid |
| 76 | * contains the (non-zero) pid. */ |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 77 | int audit_pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 79 | /* If audit_rate_limit is non-zero, limit the rate of sending audit records |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | * to that number per second. This prevents DoS attacks, but results in |
| 81 | * audit records being dropped. */ |
| 82 | static int audit_rate_limit; |
| 83 | |
| 84 | /* Number of outstanding audit_buffers allowed. */ |
| 85 | static int audit_backlog_limit = 64; |
David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 86 | static int audit_backlog_wait_time = 60 * HZ; |
| 87 | static int audit_backlog_wait_overflow = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 89 | /* The identity of the user shutting down the audit system. */ |
| 90 | uid_t audit_sig_uid = -1; |
| 91 | pid_t audit_sig_pid = -1; |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | /* Records can be lost in several ways: |
| 94 | 0) [suppressed in audit_alloc] |
| 95 | 1) out of memory in audit_log_start [kmalloc of struct audit_buffer] |
| 96 | 2) out of memory in audit_log_move [alloc_skb] |
| 97 | 3) suppressed due to audit_rate_limit |
| 98 | 4) suppressed due to audit_backlog_limit |
| 99 | */ |
| 100 | static atomic_t audit_lost = ATOMIC_INIT(0); |
| 101 | |
| 102 | /* The netlink socket. */ |
| 103 | static struct sock *audit_sock; |
| 104 | |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 105 | /* The audit_freelist is a list of pre-allocated audit buffers (if more |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of |
| 107 | * being placed on the freelist). */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | static DEFINE_SPINLOCK(audit_freelist_lock); |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 109 | static int audit_freelist_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | static LIST_HEAD(audit_freelist); |
| 111 | |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 112 | static struct sk_buff_head audit_skb_queue; |
| 113 | static struct task_struct *kauditd_task; |
| 114 | static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait); |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 115 | static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | /* The netlink socket is only to be read by 1 CPU, which lets us assume |
Steve Grubb | 23f32d1 | 2005-05-13 18:35:15 +0100 | [diff] [blame] | 118 | * that list additions and deletions never happen simultaneously in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | * auditsc.c */ |
Ingo Molnar | 5a0bbce | 2006-03-07 23:51:38 -0800 | [diff] [blame] | 120 | DEFINE_MUTEX(audit_netlink_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting |
| 123 | * audit records. Since printk uses a 1024 byte buffer, this buffer |
| 124 | * should be at least that large. */ |
| 125 | #define AUDIT_BUFSIZ 1024 |
| 126 | |
| 127 | /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the |
| 128 | * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */ |
| 129 | #define AUDIT_MAXFREE (2*NR_CPUS) |
| 130 | |
| 131 | /* The audit_buffer is used when formatting an audit record. The caller |
| 132 | * locks briefly to get the record off the freelist or to allocate the |
| 133 | * buffer, and locks briefly to send the buffer to the netlink layer or |
| 134 | * to place it on a transmit queue. Multiple audit_buffers can be in |
| 135 | * use simultaneously. */ |
| 136 | struct audit_buffer { |
| 137 | struct list_head list; |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 138 | struct sk_buff *skb; /* formatted skb ready to send */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | struct audit_context *ctx; /* NULL or associated context */ |
Al Viro | 9796fdd | 2005-10-21 03:22:03 -0400 | [diff] [blame] | 140 | gfp_t gfp_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 143 | static void audit_set_pid(struct audit_buffer *ab, pid_t pid) |
| 144 | { |
| 145 | struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data; |
| 146 | nlh->nlmsg_pid = pid; |
| 147 | } |
| 148 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 149 | void audit_panic(const char *message) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | switch (audit_failure) |
| 152 | { |
| 153 | case AUDIT_FAIL_SILENT: |
| 154 | break; |
| 155 | case AUDIT_FAIL_PRINTK: |
| 156 | printk(KERN_ERR "audit: %s\n", message); |
| 157 | break; |
| 158 | case AUDIT_FAIL_PANIC: |
| 159 | panic("audit: %s\n", message); |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | static inline int audit_rate_check(void) |
| 165 | { |
| 166 | static unsigned long last_check = 0; |
| 167 | static int messages = 0; |
| 168 | static DEFINE_SPINLOCK(lock); |
| 169 | unsigned long flags; |
| 170 | unsigned long now; |
| 171 | unsigned long elapsed; |
| 172 | int retval = 0; |
| 173 | |
| 174 | if (!audit_rate_limit) return 1; |
| 175 | |
| 176 | spin_lock_irqsave(&lock, flags); |
| 177 | if (++messages < audit_rate_limit) { |
| 178 | retval = 1; |
| 179 | } else { |
| 180 | now = jiffies; |
| 181 | elapsed = now - last_check; |
| 182 | if (elapsed > HZ) { |
| 183 | last_check = now; |
| 184 | messages = 0; |
| 185 | retval = 1; |
| 186 | } |
| 187 | } |
| 188 | spin_unlock_irqrestore(&lock, flags); |
| 189 | |
| 190 | return retval; |
| 191 | } |
| 192 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 193 | /** |
| 194 | * audit_log_lost - conditionally log lost audit message event |
| 195 | * @message: the message stating reason for lost audit message |
| 196 | * |
| 197 | * Emit at least 1 message per second, even if audit_rate_check is |
| 198 | * throttling. |
| 199 | * Always increment the lost messages counter. |
| 200 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | void audit_log_lost(const char *message) |
| 202 | { |
| 203 | static unsigned long last_msg = 0; |
| 204 | static DEFINE_SPINLOCK(lock); |
| 205 | unsigned long flags; |
| 206 | unsigned long now; |
| 207 | int print; |
| 208 | |
| 209 | atomic_inc(&audit_lost); |
| 210 | |
| 211 | print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit); |
| 212 | |
| 213 | if (!print) { |
| 214 | spin_lock_irqsave(&lock, flags); |
| 215 | now = jiffies; |
| 216 | if (now - last_msg > HZ) { |
| 217 | print = 1; |
| 218 | last_msg = now; |
| 219 | } |
| 220 | spin_unlock_irqrestore(&lock, flags); |
| 221 | } |
| 222 | |
| 223 | if (print) { |
| 224 | printk(KERN_WARNING |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 225 | "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | atomic_read(&audit_lost), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | audit_rate_limit, |
| 228 | audit_backlog_limit); |
| 229 | audit_panic(message); |
| 230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 233 | static int audit_set_rate_limit(int limit, uid_t loginuid, u32 sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 235 | int old = audit_rate_limit; |
| 236 | |
| 237 | if (sid) { |
| 238 | char *ctx = NULL; |
| 239 | u32 len; |
| 240 | int rc; |
| 241 | if ((rc = selinux_ctxid_to_string(sid, &ctx, &len))) |
| 242 | return rc; |
| 243 | else |
| 244 | audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, |
| 245 | "audit_rate_limit=%d old=%d by auid=%u subj=%s", |
| 246 | limit, old, loginuid, ctx); |
| 247 | kfree(ctx); |
| 248 | } else |
| 249 | audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, |
David Woodhouse | bccf6ae | 2005-05-23 21:35:28 +0100 | [diff] [blame] | 250 | "audit_rate_limit=%d old=%d by auid=%u", |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 251 | limit, old, loginuid); |
| 252 | audit_rate_limit = limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | return old; |
| 254 | } |
| 255 | |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 256 | static int audit_set_backlog_limit(int limit, uid_t loginuid, u32 sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 258 | int old = audit_backlog_limit; |
| 259 | |
| 260 | if (sid) { |
| 261 | char *ctx = NULL; |
| 262 | u32 len; |
| 263 | int rc; |
| 264 | if ((rc = selinux_ctxid_to_string(sid, &ctx, &len))) |
| 265 | return rc; |
| 266 | else |
| 267 | audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, |
| 268 | "audit_backlog_limit=%d old=%d by auid=%u subj=%s", |
| 269 | limit, old, loginuid, ctx); |
| 270 | kfree(ctx); |
| 271 | } else |
| 272 | audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, |
David Woodhouse | bccf6ae | 2005-05-23 21:35:28 +0100 | [diff] [blame] | 273 | "audit_backlog_limit=%d old=%d by auid=%u", |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 274 | limit, old, loginuid); |
| 275 | audit_backlog_limit = limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | return old; |
| 277 | } |
| 278 | |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 279 | static int audit_set_enabled(int state, uid_t loginuid, u32 sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 281 | int old = audit_enabled; |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | if (state != 0 && state != 1) |
| 284 | return -EINVAL; |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 285 | |
| 286 | if (sid) { |
| 287 | char *ctx = NULL; |
| 288 | u32 len; |
| 289 | int rc; |
| 290 | if ((rc = selinux_ctxid_to_string(sid, &ctx, &len))) |
| 291 | return rc; |
| 292 | else |
| 293 | audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, |
| 294 | "audit_enabled=%d old=%d by auid=%u subj=%s", |
| 295 | state, old, loginuid, ctx); |
| 296 | kfree(ctx); |
| 297 | } else |
| 298 | audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, |
David Woodhouse | bccf6ae | 2005-05-23 21:35:28 +0100 | [diff] [blame] | 299 | "audit_enabled=%d old=%d by auid=%u", |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 300 | state, old, loginuid); |
| 301 | audit_enabled = state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | return old; |
| 303 | } |
| 304 | |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 305 | static int audit_set_failure(int state, uid_t loginuid, u32 sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 307 | int old = audit_failure; |
| 308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | if (state != AUDIT_FAIL_SILENT |
| 310 | && state != AUDIT_FAIL_PRINTK |
| 311 | && state != AUDIT_FAIL_PANIC) |
| 312 | return -EINVAL; |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 313 | |
| 314 | if (sid) { |
| 315 | char *ctx = NULL; |
| 316 | u32 len; |
| 317 | int rc; |
| 318 | if ((rc = selinux_ctxid_to_string(sid, &ctx, &len))) |
| 319 | return rc; |
| 320 | else |
| 321 | audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, |
| 322 | "audit_failure=%d old=%d by auid=%u subj=%s", |
| 323 | state, old, loginuid, ctx); |
| 324 | kfree(ctx); |
| 325 | } else |
| 326 | audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, |
David Woodhouse | bccf6ae | 2005-05-23 21:35:28 +0100 | [diff] [blame] | 327 | "audit_failure=%d old=%d by auid=%u", |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 328 | state, old, loginuid); |
| 329 | audit_failure = state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | return old; |
| 331 | } |
| 332 | |
Adrian Bunk | 97a41e2 | 2006-01-08 01:02:17 -0800 | [diff] [blame] | 333 | static int kauditd_thread(void *dummy) |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 334 | { |
| 335 | struct sk_buff *skb; |
| 336 | |
| 337 | while (1) { |
| 338 | skb = skb_dequeue(&audit_skb_queue); |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 339 | wake_up(&audit_backlog_wait); |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 340 | if (skb) { |
| 341 | if (audit_pid) { |
| 342 | int err = netlink_unicast(audit_sock, skb, audit_pid, 0); |
| 343 | if (err < 0) { |
| 344 | BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */ |
| 345 | printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid); |
| 346 | audit_pid = 0; |
| 347 | } |
| 348 | } else { |
David Woodhouse | e1b09eb | 2005-06-24 17:24:11 +0100 | [diff] [blame] | 349 | printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0)); |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 350 | kfree_skb(skb); |
| 351 | } |
| 352 | } else { |
| 353 | DECLARE_WAITQUEUE(wait, current); |
| 354 | set_current_state(TASK_INTERRUPTIBLE); |
| 355 | add_wait_queue(&kauditd_wait, &wait); |
| 356 | |
Pierre Ossman | 7a4ae74 | 2005-12-12 00:37:22 -0800 | [diff] [blame] | 357 | if (!skb_queue_len(&audit_skb_queue)) { |
| 358 | try_to_freeze(); |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 359 | schedule(); |
Pierre Ossman | 7a4ae74 | 2005-12-12 00:37:22 -0800 | [diff] [blame] | 360 | } |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 361 | |
| 362 | __set_current_state(TASK_RUNNING); |
| 363 | remove_wait_queue(&kauditd_wait, &wait); |
| 364 | } |
| 365 | } |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 366 | return 0; |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 367 | } |
| 368 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 369 | /** |
| 370 | * audit_send_reply - send an audit reply message via netlink |
| 371 | * @pid: process id to send reply to |
| 372 | * @seq: sequence number |
| 373 | * @type: audit message type |
| 374 | * @done: done (last) flag |
| 375 | * @multi: multi-part message flag |
| 376 | * @payload: payload data |
| 377 | * @size: payload size |
| 378 | * |
| 379 | * Allocates an skb, builds the netlink message, and sends it to the pid. |
| 380 | * No failure notifications. |
| 381 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | void audit_send_reply(int pid, int seq, int type, int done, int multi, |
| 383 | void *payload, int size) |
| 384 | { |
| 385 | struct sk_buff *skb; |
| 386 | struct nlmsghdr *nlh; |
| 387 | int len = NLMSG_SPACE(size); |
| 388 | void *data; |
| 389 | int flags = multi ? NLM_F_MULTI : 0; |
| 390 | int t = done ? NLMSG_DONE : type; |
| 391 | |
| 392 | skb = alloc_skb(len, GFP_KERNEL); |
| 393 | if (!skb) |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 394 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 396 | nlh = NLMSG_PUT(skb, pid, seq, t, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | nlh->nlmsg_flags = flags; |
| 398 | data = NLMSG_DATA(nlh); |
| 399 | memcpy(data, payload, size); |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 400 | |
| 401 | /* Ignore failure. It'll only happen if the sender goes away, |
| 402 | because our timeout is set to infinite. */ |
| 403 | netlink_unicast(audit_sock, skb, pid, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | return; |
| 405 | |
| 406 | nlmsg_failure: /* Used by NLMSG_PUT */ |
| 407 | if (skb) |
| 408 | kfree_skb(skb); |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Check for appropriate CAP_AUDIT_ capabilities on incoming audit |
| 413 | * control messages. |
| 414 | */ |
| 415 | static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type) |
| 416 | { |
| 417 | int err = 0; |
| 418 | |
| 419 | switch (msg_type) { |
| 420 | case AUDIT_GET: |
| 421 | case AUDIT_LIST: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 422 | case AUDIT_LIST_RULES: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | case AUDIT_SET: |
| 424 | case AUDIT_ADD: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 425 | case AUDIT_ADD_RULE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | case AUDIT_DEL: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 427 | case AUDIT_DEL_RULE: |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 428 | case AUDIT_SIGNAL_INFO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL)) |
| 430 | err = -EPERM; |
| 431 | break; |
Steve Grubb | 0547410 | 2005-05-21 00:18:37 +0100 | [diff] [blame] | 432 | case AUDIT_USER: |
David Woodhouse | 209aba0 | 2005-05-18 10:21:07 +0100 | [diff] [blame] | 433 | case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG: |
Steve Grubb | 90d526c | 2005-11-03 15:48:08 +0000 | [diff] [blame] | 434 | case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | if (!cap_raised(eff_cap, CAP_AUDIT_WRITE)) |
| 436 | err = -EPERM; |
| 437 | break; |
| 438 | default: /* bad msg */ |
| 439 | err = -EINVAL; |
| 440 | } |
| 441 | |
| 442 | return err; |
| 443 | } |
| 444 | |
| 445 | static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 446 | { |
Steve Grubb | e7c3497 | 2006-04-03 09:08:13 -0400 | [diff] [blame] | 447 | u32 uid, pid, seq, sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | void *data; |
| 449 | struct audit_status *status_get, status_set; |
| 450 | int err; |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 451 | struct audit_buffer *ab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | u16 msg_type = nlh->nlmsg_type; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 453 | uid_t loginuid; /* loginuid of sender */ |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 454 | struct audit_sig_info sig_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type); |
| 457 | if (err) |
| 458 | return err; |
| 459 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 460 | /* As soon as there's any sign of userspace auditd, |
| 461 | * start kauditd to talk to it */ |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 462 | if (!kauditd_task) |
| 463 | kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd"); |
| 464 | if (IS_ERR(kauditd_task)) { |
| 465 | err = PTR_ERR(kauditd_task); |
| 466 | kauditd_task = NULL; |
| 467 | return err; |
| 468 | } |
| 469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | pid = NETLINK_CREDS(skb)->pid; |
| 471 | uid = NETLINK_CREDS(skb)->uid; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 472 | loginuid = NETLINK_CB(skb).loginuid; |
Steve Grubb | e7c3497 | 2006-04-03 09:08:13 -0400 | [diff] [blame] | 473 | sid = NETLINK_CB(skb).sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | seq = nlh->nlmsg_seq; |
| 475 | data = NLMSG_DATA(nlh); |
| 476 | |
| 477 | switch (msg_type) { |
| 478 | case AUDIT_GET: |
| 479 | status_set.enabled = audit_enabled; |
| 480 | status_set.failure = audit_failure; |
| 481 | status_set.pid = audit_pid; |
| 482 | status_set.rate_limit = audit_rate_limit; |
| 483 | status_set.backlog_limit = audit_backlog_limit; |
| 484 | status_set.lost = atomic_read(&audit_lost); |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 485 | status_set.backlog = skb_queue_len(&audit_skb_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0, |
| 487 | &status_set, sizeof(status_set)); |
| 488 | break; |
| 489 | case AUDIT_SET: |
| 490 | if (nlh->nlmsg_len < sizeof(struct audit_status)) |
| 491 | return -EINVAL; |
| 492 | status_get = (struct audit_status *)data; |
| 493 | if (status_get->mask & AUDIT_STATUS_ENABLED) { |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 494 | err = audit_set_enabled(status_get->enabled, |
| 495 | loginuid, sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | if (err < 0) return err; |
| 497 | } |
| 498 | if (status_get->mask & AUDIT_STATUS_FAILURE) { |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 499 | err = audit_set_failure(status_get->failure, |
| 500 | loginuid, sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | if (err < 0) return err; |
| 502 | } |
| 503 | if (status_get->mask & AUDIT_STATUS_PID) { |
| 504 | int old = audit_pid; |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 505 | if (sid) { |
| 506 | char *ctx = NULL; |
| 507 | u32 len; |
| 508 | int rc; |
| 509 | if ((rc = selinux_ctxid_to_string( |
| 510 | sid, &ctx, &len))) |
| 511 | return rc; |
| 512 | else |
| 513 | audit_log(NULL, GFP_KERNEL, |
| 514 | AUDIT_CONFIG_CHANGE, |
| 515 | "audit_pid=%d old=%d by auid=%u subj=%s", |
| 516 | status_get->pid, old, |
| 517 | loginuid, ctx); |
| 518 | kfree(ctx); |
| 519 | } else |
| 520 | audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, |
| 521 | "audit_pid=%d old=%d by auid=%u", |
| 522 | status_get->pid, old, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | audit_pid = status_get->pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | } |
| 525 | if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 526 | audit_set_rate_limit(status_get->rate_limit, |
| 527 | loginuid, sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT) |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 529 | audit_set_backlog_limit(status_get->backlog_limit, |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 530 | loginuid, sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | break; |
Steve Grubb | 0547410 | 2005-05-21 00:18:37 +0100 | [diff] [blame] | 532 | case AUDIT_USER: |
David Woodhouse | 209aba0 | 2005-05-18 10:21:07 +0100 | [diff] [blame] | 533 | case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG: |
Steve Grubb | 90d526c | 2005-11-03 15:48:08 +0000 | [diff] [blame] | 534 | case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2: |
David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 535 | if (!audit_enabled && msg_type != AUDIT_USER_AVC) |
| 536 | return 0; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 537 | |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame] | 538 | err = audit_filter_user(&NETLINK_CB(skb), msg_type); |
David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 539 | if (err == 1) { |
| 540 | err = 0; |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 541 | ab = audit_log_start(NULL, GFP_KERNEL, msg_type); |
David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 542 | if (ab) { |
| 543 | audit_log_format(ab, |
Steve Grubb | e7c3497 | 2006-04-03 09:08:13 -0400 | [diff] [blame] | 544 | "user pid=%d uid=%u auid=%u", |
| 545 | pid, uid, loginuid); |
| 546 | if (sid) { |
| 547 | char *ctx = NULL; |
| 548 | u32 len; |
| 549 | if (selinux_ctxid_to_string( |
| 550 | sid, &ctx, &len)) { |
| 551 | audit_log_format(ab, |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 552 | " ssid=%u", sid); |
Steve Grubb | e7c3497 | 2006-04-03 09:08:13 -0400 | [diff] [blame] | 553 | /* Maybe call audit_panic? */ |
| 554 | } else |
| 555 | audit_log_format(ab, |
| 556 | " subj=%s", ctx); |
| 557 | kfree(ctx); |
| 558 | } |
| 559 | audit_log_format(ab, " msg='%.1024s'", |
| 560 | (char *)data); |
David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 561 | audit_set_pid(ab, pid); |
| 562 | audit_log_end(ab); |
| 563 | } |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 564 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | break; |
| 566 | case AUDIT_ADD: |
| 567 | case AUDIT_DEL: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 568 | if (nlmsg_len(nlh) < sizeof(struct audit_rule)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | return -EINVAL; |
| 570 | /* fallthrough */ |
| 571 | case AUDIT_LIST: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid, |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 573 | uid, seq, data, nlmsg_len(nlh), |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 574 | loginuid, sid); |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 575 | break; |
| 576 | case AUDIT_ADD_RULE: |
| 577 | case AUDIT_DEL_RULE: |
| 578 | if (nlmsg_len(nlh) < sizeof(struct audit_rule_data)) |
| 579 | return -EINVAL; |
| 580 | /* fallthrough */ |
| 581 | case AUDIT_LIST_RULES: |
| 582 | err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid, |
| 583 | uid, seq, data, nlmsg_len(nlh), |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 584 | loginuid, sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | break; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 586 | case AUDIT_SIGNAL_INFO: |
| 587 | sig_data.uid = audit_sig_uid; |
| 588 | sig_data.pid = audit_sig_pid; |
| 589 | audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO, |
| 590 | 0, 0, &sig_data, sizeof(sig_data)); |
| 591 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | default: |
| 593 | err = -EINVAL; |
| 594 | break; |
| 595 | } |
| 596 | |
| 597 | return err < 0 ? err : 0; |
| 598 | } |
| 599 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 600 | /* |
| 601 | * Get message from skb (based on rtnetlink_rcv_skb). Each message is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | * processed by audit_receive_msg. Malformed skbs with wrong length are |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 603 | * discarded silently. |
| 604 | */ |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 605 | static void audit_receive_skb(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | { |
| 607 | int err; |
| 608 | struct nlmsghdr *nlh; |
| 609 | u32 rlen; |
| 610 | |
| 611 | while (skb->len >= NLMSG_SPACE(0)) { |
| 612 | nlh = (struct nlmsghdr *)skb->data; |
| 613 | if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len) |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 614 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 616 | if (rlen > skb->len) |
| 617 | rlen = skb->len; |
| 618 | if ((err = audit_receive_msg(skb, nlh))) { |
| 619 | netlink_ack(skb, nlh, err); |
| 620 | } else if (nlh->nlmsg_flags & NLM_F_ACK) |
| 621 | netlink_ack(skb, nlh, 0); |
| 622 | skb_pull(skb, rlen); |
| 623 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | /* Receive messages from netlink socket. */ |
| 627 | static void audit_receive(struct sock *sk, int length) |
| 628 | { |
| 629 | struct sk_buff *skb; |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 630 | unsigned int qlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
Ingo Molnar | 5a0bbce | 2006-03-07 23:51:38 -0800 | [diff] [blame] | 632 | mutex_lock(&audit_netlink_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 634 | for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) { |
| 635 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 636 | audit_receive_skb(skb); |
| 637 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | } |
Ingo Molnar | 5a0bbce | 2006-03-07 23:51:38 -0800 | [diff] [blame] | 639 | mutex_unlock(&audit_netlink_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
| 643 | /* Initialize audit support at boot time. */ |
| 644 | static int __init audit_init(void) |
| 645 | { |
| 646 | printk(KERN_INFO "audit: initializing netlink socket (%s)\n", |
| 647 | audit_default ? "enabled" : "disabled"); |
Patrick McHardy | 0662860 | 2005-08-15 12:33:26 -0700 | [diff] [blame] | 648 | audit_sock = netlink_kernel_create(NETLINK_AUDIT, 0, audit_receive, |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 649 | THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | if (!audit_sock) |
| 651 | audit_panic("cannot initialize netlink socket"); |
Amy Griffis | 71e1c78 | 2006-03-06 22:40:05 -0500 | [diff] [blame] | 652 | else |
| 653 | audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 655 | skb_queue_head_init(&audit_skb_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | audit_initialized = 1; |
| 657 | audit_enabled = audit_default; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 658 | |
| 659 | /* Register the callback with selinux. This callback will be invoked |
| 660 | * when a new policy is loaded. */ |
| 661 | selinux_audit_set_callback(&selinux_audit_rule_update); |
| 662 | |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 663 | audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | return 0; |
| 665 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | __initcall(audit_init); |
| 667 | |
| 668 | /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */ |
| 669 | static int __init audit_enable(char *str) |
| 670 | { |
| 671 | audit_default = !!simple_strtol(str, NULL, 0); |
| 672 | printk(KERN_INFO "audit: %s%s\n", |
| 673 | audit_default ? "enabled" : "disabled", |
| 674 | audit_initialized ? "" : " (after initialization)"); |
| 675 | if (audit_initialized) |
| 676 | audit_enabled = audit_default; |
OGAWA Hirofumi | 9b41046 | 2006-03-31 02:30:33 -0800 | [diff] [blame] | 677 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | __setup("audit=", audit_enable); |
| 681 | |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 682 | static void audit_buffer_free(struct audit_buffer *ab) |
| 683 | { |
| 684 | unsigned long flags; |
| 685 | |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 686 | if (!ab) |
| 687 | return; |
| 688 | |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 689 | if (ab->skb) |
| 690 | kfree_skb(ab->skb); |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 691 | |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 692 | spin_lock_irqsave(&audit_freelist_lock, flags); |
| 693 | if (++audit_freelist_count > AUDIT_MAXFREE) |
| 694 | kfree(ab); |
| 695 | else |
| 696 | list_add(&ab->list, &audit_freelist); |
| 697 | spin_unlock_irqrestore(&audit_freelist_lock, flags); |
| 698 | } |
| 699 | |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 700 | static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 701 | gfp_t gfp_mask, int type) |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 702 | { |
| 703 | unsigned long flags; |
| 704 | struct audit_buffer *ab = NULL; |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 705 | struct nlmsghdr *nlh; |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 706 | |
| 707 | spin_lock_irqsave(&audit_freelist_lock, flags); |
| 708 | if (!list_empty(&audit_freelist)) { |
| 709 | ab = list_entry(audit_freelist.next, |
| 710 | struct audit_buffer, list); |
| 711 | list_del(&ab->list); |
| 712 | --audit_freelist_count; |
| 713 | } |
| 714 | spin_unlock_irqrestore(&audit_freelist_lock, flags); |
| 715 | |
| 716 | if (!ab) { |
David Woodhouse | 4332bdd | 2005-05-06 15:59:57 +0100 | [diff] [blame] | 717 | ab = kmalloc(sizeof(*ab), gfp_mask); |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 718 | if (!ab) |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 719 | goto err; |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 720 | } |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 721 | |
David Woodhouse | 4332bdd | 2005-05-06 15:59:57 +0100 | [diff] [blame] | 722 | ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask); |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 723 | if (!ab->skb) |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 724 | goto err; |
| 725 | |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 726 | ab->ctx = ctx; |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 727 | ab->gfp_mask = gfp_mask; |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 728 | nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0)); |
| 729 | nlh->nlmsg_type = type; |
| 730 | nlh->nlmsg_flags = 0; |
| 731 | nlh->nlmsg_pid = 0; |
| 732 | nlh->nlmsg_seq = 0; |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 733 | return ab; |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 734 | err: |
| 735 | audit_buffer_free(ab); |
| 736 | return NULL; |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 737 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 739 | /** |
| 740 | * audit_serial - compute a serial number for the audit record |
| 741 | * |
| 742 | * Compute a serial number for the audit record. Audit records are |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 743 | * written to user-space as soon as they are generated, so a complete |
| 744 | * audit record may be written in several pieces. The timestamp of the |
| 745 | * record and this serial number are used by the user-space tools to |
| 746 | * determine which pieces belong to the same audit record. The |
| 747 | * (timestamp,serial) tuple is unique for each syscall and is live from |
| 748 | * syscall entry to syscall exit. |
| 749 | * |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 750 | * NOTE: Another possibility is to store the formatted records off the |
| 751 | * audit context (for those records that have a context), and emit them |
| 752 | * all at syscall exit. However, this could delay the reporting of |
| 753 | * significant errors until syscall exit (or never, if the system |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 754 | * halts). |
| 755 | */ |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 756 | unsigned int audit_serial(void) |
| 757 | { |
David Woodhouse | d5b454f | 2005-07-15 12:56:03 +0100 | [diff] [blame] | 758 | static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED; |
| 759 | static unsigned int serial = 0; |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 760 | |
David Woodhouse | d5b454f | 2005-07-15 12:56:03 +0100 | [diff] [blame] | 761 | unsigned long flags; |
| 762 | unsigned int ret; |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 763 | |
David Woodhouse | d5b454f | 2005-07-15 12:56:03 +0100 | [diff] [blame] | 764 | spin_lock_irqsave(&serial_lock, flags); |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 765 | do { |
David Woodhouse | ce625a8 | 2005-07-18 14:24:46 -0400 | [diff] [blame] | 766 | ret = ++serial; |
| 767 | } while (unlikely(!ret)); |
David Woodhouse | d5b454f | 2005-07-15 12:56:03 +0100 | [diff] [blame] | 768 | spin_unlock_irqrestore(&serial_lock, flags); |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 769 | |
David Woodhouse | d5b454f | 2005-07-15 12:56:03 +0100 | [diff] [blame] | 770 | return ret; |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | static inline void audit_get_stamp(struct audit_context *ctx, |
| 774 | struct timespec *t, unsigned int *serial) |
| 775 | { |
| 776 | if (ctx) |
| 777 | auditsc_get_stamp(ctx, t, serial); |
| 778 | else { |
| 779 | *t = CURRENT_TIME; |
| 780 | *serial = audit_serial(); |
| 781 | } |
| 782 | } |
| 783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | /* Obtain an audit buffer. This routine does locking to obtain the |
| 785 | * audit buffer, but then no locking is required for calls to |
| 786 | * audit_log_*format. If the tsk is a task that is currently in a |
| 787 | * syscall, then the syscall is marked as auditable and an audit record |
| 788 | * will be written at syscall exit. If there is no associated task, tsk |
| 789 | * should be NULL. */ |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 790 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 791 | /** |
| 792 | * audit_log_start - obtain an audit buffer |
| 793 | * @ctx: audit_context (may be NULL) |
| 794 | * @gfp_mask: type of allocation |
| 795 | * @type: audit message type |
| 796 | * |
| 797 | * Returns audit_buffer pointer on success or NULL on error. |
| 798 | * |
| 799 | * Obtain an audit buffer. This routine does locking to obtain the |
| 800 | * audit buffer, but then no locking is required for calls to |
| 801 | * audit_log_*format. If the task (ctx) is a task that is currently in a |
| 802 | * syscall, then the syscall is marked as auditable and an audit record |
| 803 | * will be written at syscall exit. If there is no associated task, then |
| 804 | * task context (ctx) should be NULL. |
| 805 | */ |
Al Viro | 9796fdd | 2005-10-21 03:22:03 -0400 | [diff] [blame] | 806 | struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 807 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | { |
| 809 | struct audit_buffer *ab = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | struct timespec t; |
Steve Grubb | d812ddb | 2005-04-29 16:09:52 +0100 | [diff] [blame] | 811 | unsigned int serial; |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 812 | int reserve; |
David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 813 | unsigned long timeout_start = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
| 815 | if (!audit_initialized) |
| 816 | return NULL; |
| 817 | |
Dustin Kirkland | c8edc80 | 2005-11-03 16:12:36 +0000 | [diff] [blame] | 818 | if (unlikely(audit_filter_type(type))) |
| 819 | return NULL; |
| 820 | |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 821 | if (gfp_mask & __GFP_WAIT) |
| 822 | reserve = 0; |
| 823 | else |
| 824 | reserve = 5; /* Allow atomic callers to go up to five |
| 825 | entries over the normal backlog limit */ |
| 826 | |
| 827 | while (audit_backlog_limit |
| 828 | && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) { |
David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 829 | if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time |
| 830 | && time_before(jiffies, timeout_start + audit_backlog_wait_time)) { |
| 831 | |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 832 | /* Wait for auditd to drain the queue a little */ |
| 833 | DECLARE_WAITQUEUE(wait, current); |
| 834 | set_current_state(TASK_INTERRUPTIBLE); |
| 835 | add_wait_queue(&audit_backlog_wait, &wait); |
| 836 | |
| 837 | if (audit_backlog_limit && |
| 838 | skb_queue_len(&audit_skb_queue) > audit_backlog_limit) |
David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 839 | schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies); |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 840 | |
| 841 | __set_current_state(TASK_RUNNING); |
| 842 | remove_wait_queue(&audit_backlog_wait, &wait); |
David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 843 | continue; |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 844 | } |
David Woodhouse | fb19b4c | 2005-05-19 14:55:56 +0100 | [diff] [blame] | 845 | if (audit_rate_check()) |
| 846 | printk(KERN_WARNING |
| 847 | "audit: audit_backlog=%d > " |
| 848 | "audit_backlog_limit=%d\n", |
| 849 | skb_queue_len(&audit_skb_queue), |
| 850 | audit_backlog_limit); |
| 851 | audit_log_lost("backlog limit exceeded"); |
David Woodhouse | ac4cec4 | 2005-07-02 14:08:48 +0100 | [diff] [blame] | 852 | audit_backlog_wait_time = audit_backlog_wait_overflow; |
| 853 | wake_up(&audit_backlog_wait); |
David Woodhouse | fb19b4c | 2005-05-19 14:55:56 +0100 | [diff] [blame] | 854 | return NULL; |
| 855 | } |
| 856 | |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 857 | ab = audit_buffer_alloc(ctx, gfp_mask, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | if (!ab) { |
| 859 | audit_log_lost("out of memory in audit_log_start"); |
| 860 | return NULL; |
| 861 | } |
| 862 | |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 863 | audit_get_stamp(ab->ctx, &t, &serial); |
Chris Wright | 197c69c | 2005-05-11 10:54:05 +0100 | [diff] [blame] | 864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | audit_log_format(ab, "audit(%lu.%03lu:%u): ", |
| 866 | t.tv_sec, t.tv_nsec/1000000, serial); |
| 867 | return ab; |
| 868 | } |
| 869 | |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 870 | /** |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 871 | * audit_expand - expand skb in the audit buffer |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 872 | * @ab: audit_buffer |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 873 | * @extra: space to add at tail of the skb |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 874 | * |
| 875 | * Returns 0 (no space) on failed expansion, or available space if |
| 876 | * successful. |
| 877 | */ |
David Woodhouse | e3b926b | 2005-05-10 18:56:08 +0100 | [diff] [blame] | 878 | static inline int audit_expand(struct audit_buffer *ab, int extra) |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 879 | { |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 880 | struct sk_buff *skb = ab->skb; |
David Woodhouse | e3b926b | 2005-05-10 18:56:08 +0100 | [diff] [blame] | 881 | int ret = pskb_expand_head(skb, skb_headroom(skb), extra, |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 882 | ab->gfp_mask); |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 883 | if (ret < 0) { |
| 884 | audit_log_lost("out of memory in audit_expand"); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 885 | return 0; |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 886 | } |
| 887 | return skb_tailroom(skb); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 888 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 890 | /* |
| 891 | * Format an audit message into the audit buffer. If there isn't enough |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | * room in the audit buffer, more room will be allocated and vsnprint |
| 893 | * will be called a second time. Currently, we assume that a printk |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 894 | * can't format message larger than 1024 bytes, so we don't either. |
| 895 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | static void audit_log_vformat(struct audit_buffer *ab, const char *fmt, |
| 897 | va_list args) |
| 898 | { |
| 899 | int len, avail; |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 900 | struct sk_buff *skb; |
David Woodhouse | eecb0a7 | 2005-05-10 18:58:51 +0100 | [diff] [blame] | 901 | va_list args2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
| 903 | if (!ab) |
| 904 | return; |
| 905 | |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 906 | BUG_ON(!ab->skb); |
| 907 | skb = ab->skb; |
| 908 | avail = skb_tailroom(skb); |
| 909 | if (avail == 0) { |
David Woodhouse | e3b926b | 2005-05-10 18:56:08 +0100 | [diff] [blame] | 910 | avail = audit_expand(ab, AUDIT_BUFSIZ); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 911 | if (!avail) |
| 912 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | } |
David Woodhouse | eecb0a7 | 2005-05-10 18:58:51 +0100 | [diff] [blame] | 914 | va_copy(args2, args); |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 915 | len = vsnprintf(skb->tail, avail, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | if (len >= avail) { |
| 917 | /* The printk buffer is 1024 bytes long, so if we get |
| 918 | * here and AUDIT_BUFSIZ is at least 1024, then we can |
| 919 | * log everything that printk could have logged. */ |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 920 | avail = audit_expand(ab, |
| 921 | max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail)); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 922 | if (!avail) |
| 923 | goto out; |
David Woodhouse | eecb0a7 | 2005-05-10 18:58:51 +0100 | [diff] [blame] | 924 | len = vsnprintf(skb->tail, avail, fmt, args2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | } |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 926 | if (len > 0) |
| 927 | skb_put(skb, len); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 928 | out: |
| 929 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 932 | /** |
| 933 | * audit_log_format - format a message into the audit buffer. |
| 934 | * @ab: audit_buffer |
| 935 | * @fmt: format string |
| 936 | * @...: optional parameters matching @fmt string |
| 937 | * |
| 938 | * All the work is done in audit_log_vformat. |
| 939 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) |
| 941 | { |
| 942 | va_list args; |
| 943 | |
| 944 | if (!ab) |
| 945 | return; |
| 946 | va_start(args, fmt); |
| 947 | audit_log_vformat(ab, fmt, args); |
| 948 | va_end(args); |
| 949 | } |
| 950 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 951 | /** |
| 952 | * audit_log_hex - convert a buffer to hex and append it to the audit skb |
| 953 | * @ab: the audit_buffer |
| 954 | * @buf: buffer to convert to hex |
| 955 | * @len: length of @buf to be converted |
| 956 | * |
| 957 | * No return value; failure to expand is silently ignored. |
| 958 | * |
| 959 | * This function will take the passed buf and convert it into a string of |
| 960 | * ascii hex digits. The new string is placed onto the skb. |
| 961 | */ |
| 962 | void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 963 | size_t len) |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 964 | { |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 965 | int i, avail, new_len; |
| 966 | unsigned char *ptr; |
| 967 | struct sk_buff *skb; |
| 968 | static const unsigned char *hex = "0123456789ABCDEF"; |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 969 | |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 970 | BUG_ON(!ab->skb); |
| 971 | skb = ab->skb; |
| 972 | avail = skb_tailroom(skb); |
| 973 | new_len = len<<1; |
| 974 | if (new_len >= avail) { |
| 975 | /* Round the buffer request up to the next multiple */ |
| 976 | new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1); |
| 977 | avail = audit_expand(ab, new_len); |
| 978 | if (!avail) |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | ptr = skb->tail; |
| 983 | for (i=0; i<len; i++) { |
| 984 | *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */ |
| 985 | *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */ |
| 986 | } |
| 987 | *ptr = 0; |
| 988 | skb_put(skb, len << 1); /* new string is twice the old string */ |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 989 | } |
| 990 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 991 | /** |
| 992 | * audit_log_unstrustedstring - log a string that may contain random characters |
| 993 | * @ab: audit_buffer |
| 994 | * @string: string to be logged |
| 995 | * |
| 996 | * This code will escape a string that is passed to it if the string |
| 997 | * contains a control character, unprintable character, double quote mark, |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 998 | * or a space. Unescaped strings will start and end with a double quote mark. |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 999 | * Strings that are escaped are printed in hex (2 digits per char). |
| 1000 | */ |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1001 | void audit_log_untrustedstring(struct audit_buffer *ab, const char *string) |
| 1002 | { |
Andrew Morton | 81b7854 | 2005-04-29 15:59:11 +0100 | [diff] [blame] | 1003 | const unsigned char *p = string; |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1004 | |
| 1005 | while (*p) { |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1006 | if (*p == '"' || *p < 0x21 || *p > 0x7f) { |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1007 | audit_log_hex(ab, string, strlen(string)); |
| 1008 | return; |
| 1009 | } |
| 1010 | p++; |
| 1011 | } |
| 1012 | audit_log_format(ab, "\"%s\"", string); |
| 1013 | } |
| 1014 | |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1015 | /* This is a helper-function to print the escaped d_path */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | void audit_log_d_path(struct audit_buffer *ab, const char *prefix, |
| 1017 | struct dentry *dentry, struct vfsmount *vfsmnt) |
| 1018 | { |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1019 | char *p, *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 1021 | if (prefix) |
| 1022 | audit_log_format(ab, " %s", prefix); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1024 | /* We will allow 11 spaces for ' (deleted)' to be appended */ |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1025 | path = kmalloc(PATH_MAX+11, ab->gfp_mask); |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1026 | if (!path) { |
| 1027 | audit_log_format(ab, "<no memory>"); |
| 1028 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | } |
Steve Grubb | 168b717 | 2005-05-19 10:24:22 +0100 | [diff] [blame] | 1030 | p = d_path(dentry, vfsmnt, path, PATH_MAX+11); |
| 1031 | if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */ |
| 1032 | /* FIXME: can we save some information here? */ |
| 1033 | audit_log_format(ab, "<too long>"); |
| 1034 | } else |
| 1035 | audit_log_untrustedstring(ab, p); |
| 1036 | kfree(path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1039 | /** |
| 1040 | * audit_log_end - end one audit record |
| 1041 | * @ab: the audit_buffer |
| 1042 | * |
| 1043 | * The netlink_* functions cannot be called inside an irq context, so |
| 1044 | * the audit buffer is placed on a queue and a tasklet is scheduled to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | * remove them from the queue outside the irq context. May be called in |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1046 | * any context. |
| 1047 | */ |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 1048 | void audit_log_end(struct audit_buffer *ab) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | if (!ab) |
| 1051 | return; |
| 1052 | if (!audit_rate_check()) { |
| 1053 | audit_log_lost("rate limit exceeded"); |
| 1054 | } else { |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 1055 | if (audit_pid) { |
| 1056 | struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data; |
| 1057 | nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0); |
| 1058 | skb_queue_tail(&audit_skb_queue, ab->skb); |
| 1059 | ab->skb = NULL; |
| 1060 | wake_up_interruptible(&kauditd_wait); |
| 1061 | } else { |
David Woodhouse | e1b09eb | 2005-06-24 17:24:11 +0100 | [diff] [blame] | 1062 | printk(KERN_NOTICE "%s\n", ab->skb->data + NLMSG_SPACE(0)); |
David Woodhouse | b7d1125 | 2005-05-19 10:56:58 +0100 | [diff] [blame] | 1063 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | } |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 1065 | audit_buffer_free(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1068 | /** |
| 1069 | * audit_log - Log an audit record |
| 1070 | * @ctx: audit context |
| 1071 | * @gfp_mask: type of allocation |
| 1072 | * @type: audit message type |
| 1073 | * @fmt: format string to use |
| 1074 | * @...: variable parameters matching the format string |
| 1075 | * |
| 1076 | * This is a convenience function that calls audit_log_start, |
| 1077 | * audit_log_vformat, and audit_log_end. It may be called |
| 1078 | * in any context. |
| 1079 | */ |
Al Viro | 9796fdd | 2005-10-21 03:22:03 -0400 | [diff] [blame] | 1080 | void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type, |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1081 | const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | { |
| 1083 | struct audit_buffer *ab; |
| 1084 | va_list args; |
| 1085 | |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1086 | ab = audit_log_start(ctx, gfp_mask, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | if (ab) { |
| 1088 | va_start(args, fmt); |
| 1089 | audit_log_vformat(ab, fmt, args); |
| 1090 | va_end(args); |
| 1091 | audit_log_end(ab); |
| 1092 | } |
| 1093 | } |
lorenzo@gnu.org | bf45da9 | 2006-03-09 00:33:47 +0100 | [diff] [blame] | 1094 | |
| 1095 | EXPORT_SYMBOL(audit_log_start); |
| 1096 | EXPORT_SYMBOL(audit_log_end); |
| 1097 | EXPORT_SYMBOL(audit_log_format); |
| 1098 | EXPORT_SYMBOL(audit_log); |