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