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