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