| 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 |
| 119 | * that list additions and deletions never happen simultaneiously in |
| 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 | |
| 143 | void audit_set_type(struct audit_buffer *ab, int type) |
| 144 | { |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 145 | struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data; |
| 146 | nlh->nlmsg_type = type; |
| 147 | } |
| 148 | |
| 149 | static void audit_set_pid(struct audit_buffer *ab, pid_t pid) |
| 150 | { |
| 151 | struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data; |
| 152 | nlh->nlmsg_pid = pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | struct audit_entry { |
| 156 | struct list_head list; |
| 157 | struct audit_rule rule; |
| 158 | }; |
| 159 | |
| 160 | static void audit_log_end_irq(struct audit_buffer *ab); |
| 161 | static void audit_log_end_fast(struct audit_buffer *ab); |
| 162 | |
| 163 | static void audit_panic(const char *message) |
| 164 | { |
| 165 | switch (audit_failure) |
| 166 | { |
| 167 | case AUDIT_FAIL_SILENT: |
| 168 | break; |
| 169 | case AUDIT_FAIL_PRINTK: |
| 170 | printk(KERN_ERR "audit: %s\n", message); |
| 171 | break; |
| 172 | case AUDIT_FAIL_PANIC: |
| 173 | panic("audit: %s\n", message); |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | static inline int audit_rate_check(void) |
| 179 | { |
| 180 | static unsigned long last_check = 0; |
| 181 | static int messages = 0; |
| 182 | static DEFINE_SPINLOCK(lock); |
| 183 | unsigned long flags; |
| 184 | unsigned long now; |
| 185 | unsigned long elapsed; |
| 186 | int retval = 0; |
| 187 | |
| 188 | if (!audit_rate_limit) return 1; |
| 189 | |
| 190 | spin_lock_irqsave(&lock, flags); |
| 191 | if (++messages < audit_rate_limit) { |
| 192 | retval = 1; |
| 193 | } else { |
| 194 | now = jiffies; |
| 195 | elapsed = now - last_check; |
| 196 | if (elapsed > HZ) { |
| 197 | last_check = now; |
| 198 | messages = 0; |
| 199 | retval = 1; |
| 200 | } |
| 201 | } |
| 202 | spin_unlock_irqrestore(&lock, flags); |
| 203 | |
| 204 | return retval; |
| 205 | } |
| 206 | |
| 207 | /* Emit at least 1 message per second, even if audit_rate_check is |
| 208 | * throttling. */ |
| 209 | void audit_log_lost(const char *message) |
| 210 | { |
| 211 | static unsigned long last_msg = 0; |
| 212 | static DEFINE_SPINLOCK(lock); |
| 213 | unsigned long flags; |
| 214 | unsigned long now; |
| 215 | int print; |
| 216 | |
| 217 | atomic_inc(&audit_lost); |
| 218 | |
| 219 | print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit); |
| 220 | |
| 221 | if (!print) { |
| 222 | spin_lock_irqsave(&lock, flags); |
| 223 | now = jiffies; |
| 224 | if (now - last_msg > HZ) { |
| 225 | print = 1; |
| 226 | last_msg = now; |
| 227 | } |
| 228 | spin_unlock_irqrestore(&lock, flags); |
| 229 | } |
| 230 | |
| 231 | if (print) { |
| 232 | printk(KERN_WARNING |
| 233 | "audit: audit_lost=%d audit_backlog=%d" |
| 234 | " audit_rate_limit=%d audit_backlog_limit=%d\n", |
| 235 | atomic_read(&audit_lost), |
| 236 | atomic_read(&audit_backlog), |
| 237 | audit_rate_limit, |
| 238 | audit_backlog_limit); |
| 239 | audit_panic(message); |
| 240 | } |
| 241 | |
| 242 | } |
| 243 | |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 244 | static int audit_set_rate_limit(int limit, uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
| 246 | int old = audit_rate_limit; |
| 247 | audit_rate_limit = limit; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 248 | audit_log(NULL, "audit_rate_limit=%d old=%d by auid %u", |
| 249 | audit_rate_limit, old, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | return old; |
| 251 | } |
| 252 | |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 253 | static int audit_set_backlog_limit(int limit, uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
| 255 | int old = audit_backlog_limit; |
| 256 | audit_backlog_limit = limit; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 257 | audit_log(NULL, "audit_backlog_limit=%d old=%d by auid %u", |
| 258 | audit_backlog_limit, old, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return old; |
| 260 | } |
| 261 | |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 262 | static int audit_set_enabled(int state, uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
| 264 | int old = audit_enabled; |
| 265 | if (state != 0 && state != 1) |
| 266 | return -EINVAL; |
| 267 | audit_enabled = state; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 268 | audit_log(NULL, "audit_enabled=%d old=%d by auid %u", |
| 269 | audit_enabled, old, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | return old; |
| 271 | } |
| 272 | |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 273 | static int audit_set_failure(int state, uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
| 275 | int old = audit_failure; |
| 276 | if (state != AUDIT_FAIL_SILENT |
| 277 | && state != AUDIT_FAIL_PRINTK |
| 278 | && state != AUDIT_FAIL_PANIC) |
| 279 | return -EINVAL; |
| 280 | audit_failure = state; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 281 | audit_log(NULL, "audit_failure=%d old=%d by auid %u", |
| 282 | audit_failure, old, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | return old; |
| 284 | } |
| 285 | |
| 286 | #ifdef CONFIG_NET |
| 287 | void audit_send_reply(int pid, int seq, int type, int done, int multi, |
| 288 | void *payload, int size) |
| 289 | { |
| 290 | struct sk_buff *skb; |
| 291 | struct nlmsghdr *nlh; |
| 292 | int len = NLMSG_SPACE(size); |
| 293 | void *data; |
| 294 | int flags = multi ? NLM_F_MULTI : 0; |
| 295 | int t = done ? NLMSG_DONE : type; |
| 296 | |
| 297 | skb = alloc_skb(len, GFP_KERNEL); |
| 298 | if (!skb) |
| 299 | goto nlmsg_failure; |
| 300 | |
| 301 | nlh = NLMSG_PUT(skb, pid, seq, t, len - sizeof(*nlh)); |
| 302 | nlh->nlmsg_flags = flags; |
| 303 | data = NLMSG_DATA(nlh); |
| 304 | memcpy(data, payload, size); |
| 305 | netlink_unicast(audit_sock, skb, pid, MSG_DONTWAIT); |
| 306 | return; |
| 307 | |
| 308 | nlmsg_failure: /* Used by NLMSG_PUT */ |
| 309 | if (skb) |
| 310 | kfree_skb(skb); |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Check for appropriate CAP_AUDIT_ capabilities on incoming audit |
| 315 | * control messages. |
| 316 | */ |
| 317 | static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type) |
| 318 | { |
| 319 | int err = 0; |
| 320 | |
| 321 | switch (msg_type) { |
| 322 | case AUDIT_GET: |
| 323 | case AUDIT_LIST: |
| 324 | case AUDIT_SET: |
| 325 | case AUDIT_ADD: |
| 326 | case AUDIT_DEL: |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 327 | case AUDIT_SIGNAL_INFO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL)) |
| 329 | err = -EPERM; |
| 330 | break; |
| 331 | case AUDIT_USER: |
| 332 | if (!cap_raised(eff_cap, CAP_AUDIT_WRITE)) |
| 333 | err = -EPERM; |
| 334 | break; |
| 335 | default: /* bad msg */ |
| 336 | err = -EINVAL; |
| 337 | } |
| 338 | |
| 339 | return err; |
| 340 | } |
| 341 | |
| 342 | static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 343 | { |
| 344 | u32 uid, pid, seq; |
| 345 | void *data; |
| 346 | struct audit_status *status_get, status_set; |
| 347 | int err; |
| 348 | struct audit_buffer *ab; |
| 349 | u16 msg_type = nlh->nlmsg_type; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 350 | uid_t loginuid; /* loginuid of sender */ |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 351 | struct audit_sig_info sig_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
| 353 | err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type); |
| 354 | if (err) |
| 355 | return err; |
| 356 | |
| 357 | pid = NETLINK_CREDS(skb)->pid; |
| 358 | uid = NETLINK_CREDS(skb)->uid; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 359 | loginuid = NETLINK_CB(skb).loginuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | seq = nlh->nlmsg_seq; |
| 361 | data = NLMSG_DATA(nlh); |
| 362 | |
| 363 | switch (msg_type) { |
| 364 | case AUDIT_GET: |
| 365 | status_set.enabled = audit_enabled; |
| 366 | status_set.failure = audit_failure; |
| 367 | status_set.pid = audit_pid; |
| 368 | status_set.rate_limit = audit_rate_limit; |
| 369 | status_set.backlog_limit = audit_backlog_limit; |
| 370 | status_set.lost = atomic_read(&audit_lost); |
| 371 | status_set.backlog = atomic_read(&audit_backlog); |
| 372 | audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0, |
| 373 | &status_set, sizeof(status_set)); |
| 374 | break; |
| 375 | case AUDIT_SET: |
| 376 | if (nlh->nlmsg_len < sizeof(struct audit_status)) |
| 377 | return -EINVAL; |
| 378 | status_get = (struct audit_status *)data; |
| 379 | if (status_get->mask & AUDIT_STATUS_ENABLED) { |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 380 | err = audit_set_enabled(status_get->enabled, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | if (err < 0) return err; |
| 382 | } |
| 383 | if (status_get->mask & AUDIT_STATUS_FAILURE) { |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 384 | err = audit_set_failure(status_get->failure, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | if (err < 0) return err; |
| 386 | } |
| 387 | if (status_get->mask & AUDIT_STATUS_PID) { |
| 388 | int old = audit_pid; |
| 389 | audit_pid = status_get->pid; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 390 | audit_log(NULL, "audit_pid=%d old=%d by auid %u", |
| 391 | audit_pid, old, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 394 | audit_set_rate_limit(status_get->rate_limit, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT) |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 396 | audit_set_backlog_limit(status_get->backlog_limit, |
| 397 | loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | break; |
| 399 | case AUDIT_USER: |
| 400 | ab = audit_log_start(NULL); |
| 401 | if (!ab) |
| 402 | break; /* audit_panic has been called */ |
| 403 | audit_log_format(ab, |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 404 | "user pid=%d uid=%d length=%d loginuid=%u" |
| 405 | " msg='%.1024s'", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | pid, uid, |
| 407 | (int)(nlh->nlmsg_len |
| 408 | - ((char *)data - (char *)nlh)), |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 409 | loginuid, (char *)data); |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 410 | audit_set_type(ab, AUDIT_USER); |
| 411 | audit_set_pid(ab, pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | audit_log_end(ab); |
| 413 | break; |
| 414 | case AUDIT_ADD: |
| 415 | case AUDIT_DEL: |
| 416 | if (nlh->nlmsg_len < sizeof(struct audit_rule)) |
| 417 | return -EINVAL; |
| 418 | /* fallthrough */ |
| 419 | case AUDIT_LIST: |
| 420 | #ifdef CONFIG_AUDITSYSCALL |
| 421 | err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid, |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 422 | uid, seq, data, loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | #else |
| 424 | err = -EOPNOTSUPP; |
| 425 | #endif |
| 426 | break; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 427 | case AUDIT_SIGNAL_INFO: |
| 428 | sig_data.uid = audit_sig_uid; |
| 429 | sig_data.pid = audit_sig_pid; |
| 430 | audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO, |
| 431 | 0, 0, &sig_data, sizeof(sig_data)); |
| 432 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | default: |
| 434 | err = -EINVAL; |
| 435 | break; |
| 436 | } |
| 437 | |
| 438 | return err < 0 ? err : 0; |
| 439 | } |
| 440 | |
| 441 | /* Get message from skb (based on rtnetlink_rcv_skb). Each message is |
| 442 | * processed by audit_receive_msg. Malformed skbs with wrong length are |
| 443 | * discarded silently. */ |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 444 | static void audit_receive_skb(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | { |
| 446 | int err; |
| 447 | struct nlmsghdr *nlh; |
| 448 | u32 rlen; |
| 449 | |
| 450 | while (skb->len >= NLMSG_SPACE(0)) { |
| 451 | nlh = (struct nlmsghdr *)skb->data; |
| 452 | if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len) |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 453 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 455 | if (rlen > skb->len) |
| 456 | rlen = skb->len; |
| 457 | if ((err = audit_receive_msg(skb, nlh))) { |
| 458 | netlink_ack(skb, nlh, err); |
| 459 | } else if (nlh->nlmsg_flags & NLM_F_ACK) |
| 460 | netlink_ack(skb, nlh, 0); |
| 461 | skb_pull(skb, rlen); |
| 462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* Receive messages from netlink socket. */ |
| 466 | static void audit_receive(struct sock *sk, int length) |
| 467 | { |
| 468 | struct sk_buff *skb; |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 469 | unsigned int qlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 471 | down(&audit_netlink_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 473 | for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) { |
| 474 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 475 | audit_receive_skb(skb); |
| 476 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | up(&audit_netlink_sem); |
| 479 | } |
| 480 | |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 481 | /* Grab skbuff from the audit_buffer and send to user space. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | static inline int audit_log_drain(struct audit_buffer *ab) |
| 483 | { |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 484 | struct sk_buff *skb = ab->skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 486 | if (skb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | int retval = 0; |
| 488 | |
| 489 | if (audit_pid) { |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 490 | struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data; |
Steve Grubb | 8c5aa40 | 2005-05-10 18:53:07 +0100 | [diff] [blame] | 491 | nlh->nlmsg_len = skb->len - sizeof(*nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | skb_get(skb); /* because netlink_* frees */ |
| 493 | retval = netlink_unicast(audit_sock, skb, audit_pid, |
| 494 | MSG_DONTWAIT); |
| 495 | } |
Chris Wright | 37509e7 | 2005-04-29 17:19:14 +0100 | [diff] [blame] | 496 | if (retval == -EAGAIN && |
| 497 | (atomic_read(&audit_backlog)) < audit_backlog_limit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | audit_log_end_irq(ab); |
| 499 | return 1; |
| 500 | } |
| 501 | if (retval < 0) { |
| 502 | if (retval == -ECONNREFUSED) { |
| 503 | printk(KERN_ERR |
| 504 | "audit: *NO* daemon at audit_pid=%d\n", |
| 505 | audit_pid); |
| 506 | audit_pid = 0; |
| 507 | } else |
| 508 | audit_log_lost("netlink socket too busy"); |
| 509 | } |
| 510 | if (!audit_pid) { /* No daemon */ |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 511 | int offset = NLMSG_SPACE(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | int len = skb->len - offset; |
Peter Martuccelli | c7fcb0e | 2005-04-29 16:10:24 +0100 | [diff] [blame] | 513 | skb->data[offset + len] = '\0'; |
| 514 | printk(KERN_ERR "%s\n", skb->data + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | /* Initialize audit support at boot time. */ |
| 521 | static int __init audit_init(void) |
| 522 | { |
| 523 | printk(KERN_INFO "audit: initializing netlink socket (%s)\n", |
| 524 | audit_default ? "enabled" : "disabled"); |
| 525 | audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive); |
| 526 | if (!audit_sock) |
| 527 | audit_panic("cannot initialize netlink socket"); |
| 528 | |
| 529 | audit_initialized = 1; |
| 530 | audit_enabled = audit_default; |
| 531 | audit_log(NULL, "initialized"); |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | #else |
| 536 | /* Without CONFIG_NET, we have no skbuffs. For now, print what we have |
| 537 | * in the buffer. */ |
| 538 | static void audit_log_move(struct audit_buffer *ab) |
| 539 | { |
| 540 | printk(KERN_ERR "%*.*s\n", ab->len, ab->len, ab->tmp); |
| 541 | ab->len = 0; |
| 542 | } |
| 543 | |
| 544 | static inline int audit_log_drain(struct audit_buffer *ab) |
| 545 | { |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | /* Initialize audit support at boot time. */ |
| 550 | int __init audit_init(void) |
| 551 | { |
| 552 | printk(KERN_INFO "audit: initializing WITHOUT netlink support\n"); |
| 553 | audit_sock = NULL; |
| 554 | audit_pid = 0; |
| 555 | |
| 556 | audit_initialized = 1; |
| 557 | audit_enabled = audit_default; |
| 558 | audit_log(NULL, "initialized"); |
| 559 | return 0; |
| 560 | } |
| 561 | #endif |
| 562 | |
| 563 | __initcall(audit_init); |
| 564 | |
| 565 | /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */ |
| 566 | static int __init audit_enable(char *str) |
| 567 | { |
| 568 | audit_default = !!simple_strtol(str, NULL, 0); |
| 569 | printk(KERN_INFO "audit: %s%s\n", |
| 570 | audit_default ? "enabled" : "disabled", |
| 571 | audit_initialized ? "" : " (after initialization)"); |
| 572 | if (audit_initialized) |
| 573 | audit_enabled = audit_default; |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | __setup("audit=", audit_enable); |
| 578 | |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 579 | static void audit_buffer_free(struct audit_buffer *ab) |
| 580 | { |
| 581 | unsigned long flags; |
| 582 | |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 583 | if (!ab) |
| 584 | return; |
| 585 | |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 586 | if (ab->skb) |
| 587 | kfree_skb(ab->skb); |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 588 | atomic_dec(&audit_backlog); |
| 589 | spin_lock_irqsave(&audit_freelist_lock, flags); |
| 590 | if (++audit_freelist_count > AUDIT_MAXFREE) |
| 591 | kfree(ab); |
| 592 | else |
| 593 | list_add(&ab->list, &audit_freelist); |
| 594 | spin_unlock_irqrestore(&audit_freelist_lock, flags); |
| 595 | } |
| 596 | |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 597 | static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx, |
| 598 | int gfp_mask) |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 599 | { |
| 600 | unsigned long flags; |
| 601 | struct audit_buffer *ab = NULL; |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 602 | struct nlmsghdr *nlh; |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 603 | |
| 604 | spin_lock_irqsave(&audit_freelist_lock, flags); |
| 605 | if (!list_empty(&audit_freelist)) { |
| 606 | ab = list_entry(audit_freelist.next, |
| 607 | struct audit_buffer, list); |
| 608 | list_del(&ab->list); |
| 609 | --audit_freelist_count; |
| 610 | } |
| 611 | spin_unlock_irqrestore(&audit_freelist_lock, flags); |
| 612 | |
| 613 | if (!ab) { |
David Woodhouse | 4332bdd | 2005-05-06 15:59:57 +0100 | [diff] [blame] | 614 | ab = kmalloc(sizeof(*ab), gfp_mask); |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 615 | if (!ab) |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 616 | goto err; |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 617 | } |
| 618 | atomic_inc(&audit_backlog); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 619 | |
David Woodhouse | 4332bdd | 2005-05-06 15:59:57 +0100 | [diff] [blame] | 620 | ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask); |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 621 | if (!ab->skb) |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 622 | goto err; |
| 623 | |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 624 | ab->ctx = ctx; |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 625 | nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0)); |
| 626 | nlh->nlmsg_type = AUDIT_KERNEL; |
| 627 | nlh->nlmsg_flags = 0; |
| 628 | nlh->nlmsg_pid = 0; |
| 629 | nlh->nlmsg_seq = 0; |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 630 | return ab; |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 631 | err: |
| 632 | audit_buffer_free(ab); |
| 633 | return NULL; |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 634 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
| 636 | /* Obtain an audit buffer. This routine does locking to obtain the |
| 637 | * audit buffer, but then no locking is required for calls to |
| 638 | * audit_log_*format. If the tsk is a task that is currently in a |
| 639 | * syscall, then the syscall is marked as auditable and an audit record |
| 640 | * will be written at syscall exit. If there is no associated task, tsk |
| 641 | * should be NULL. */ |
| 642 | struct audit_buffer *audit_log_start(struct audit_context *ctx) |
| 643 | { |
| 644 | struct audit_buffer *ab = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | struct timespec t; |
Steve Grubb | d812ddb | 2005-04-29 16:09:52 +0100 | [diff] [blame] | 646 | unsigned int serial; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
| 648 | if (!audit_initialized) |
| 649 | return NULL; |
| 650 | |
| 651 | if (audit_backlog_limit |
| 652 | && atomic_read(&audit_backlog) > audit_backlog_limit) { |
| 653 | if (audit_rate_check()) |
| 654 | printk(KERN_WARNING |
| 655 | "audit: audit_backlog=%d > " |
| 656 | "audit_backlog_limit=%d\n", |
| 657 | atomic_read(&audit_backlog), |
| 658 | audit_backlog_limit); |
| 659 | audit_log_lost("backlog limit exceeded"); |
| 660 | return NULL; |
| 661 | } |
| 662 | |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 663 | ab = audit_buffer_alloc(ctx, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | if (!ab) { |
| 665 | audit_log_lost("out of memory in audit_log_start"); |
| 666 | return NULL; |
| 667 | } |
| 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | #ifdef CONFIG_AUDITSYSCALL |
| 670 | if (ab->ctx) |
| 671 | audit_get_stamp(ab->ctx, &t, &serial); |
| 672 | else |
| 673 | #endif |
Steve Grubb | d812ddb | 2005-04-29 16:09:52 +0100 | [diff] [blame] | 674 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | t = CURRENT_TIME; |
Steve Grubb | d812ddb | 2005-04-29 16:09:52 +0100 | [diff] [blame] | 676 | serial = 0; |
| 677 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | audit_log_format(ab, "audit(%lu.%03lu:%u): ", |
| 679 | t.tv_sec, t.tv_nsec/1000000, serial); |
| 680 | return ab; |
| 681 | } |
| 682 | |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 683 | /** |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 684 | * audit_expand - expand skb in the audit buffer |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 685 | * @ab: audit_buffer |
| 686 | * |
| 687 | * Returns 0 (no space) on failed expansion, or available space if |
| 688 | * successful. |
| 689 | */ |
David Woodhouse | e3b926b | 2005-05-10 18:56:08 +0100 | [diff] [blame] | 690 | static inline int audit_expand(struct audit_buffer *ab, int extra) |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 691 | { |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 692 | struct sk_buff *skb = ab->skb; |
David Woodhouse | e3b926b | 2005-05-10 18:56:08 +0100 | [diff] [blame] | 693 | int ret = pskb_expand_head(skb, skb_headroom(skb), extra, |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 694 | GFP_ATOMIC); |
| 695 | if (ret < 0) { |
| 696 | audit_log_lost("out of memory in audit_expand"); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 697 | return 0; |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 698 | } |
| 699 | return skb_tailroom(skb); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 700 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
| 702 | /* Format an audit message into the audit buffer. If there isn't enough |
| 703 | * room in the audit buffer, more room will be allocated and vsnprint |
| 704 | * will be called a second time. Currently, we assume that a printk |
| 705 | * can't format message larger than 1024 bytes, so we don't either. */ |
| 706 | static void audit_log_vformat(struct audit_buffer *ab, const char *fmt, |
| 707 | va_list args) |
| 708 | { |
| 709 | int len, avail; |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 710 | struct sk_buff *skb; |
David Woodhouse | eecb0a7 | 2005-05-10 18:58:51 +0100 | [diff] [blame^] | 711 | va_list args2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
| 713 | if (!ab) |
| 714 | return; |
| 715 | |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 716 | BUG_ON(!ab->skb); |
| 717 | skb = ab->skb; |
| 718 | avail = skb_tailroom(skb); |
| 719 | if (avail == 0) { |
David Woodhouse | e3b926b | 2005-05-10 18:56:08 +0100 | [diff] [blame] | 720 | avail = audit_expand(ab, AUDIT_BUFSIZ); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 721 | if (!avail) |
| 722 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | } |
David Woodhouse | eecb0a7 | 2005-05-10 18:58:51 +0100 | [diff] [blame^] | 724 | va_copy(args2, args); |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 725 | len = vsnprintf(skb->tail, avail, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | if (len >= avail) { |
| 727 | /* The printk buffer is 1024 bytes long, so if we get |
| 728 | * here and AUDIT_BUFSIZ is at least 1024, then we can |
| 729 | * log everything that printk could have logged. */ |
David Woodhouse | e3b926b | 2005-05-10 18:56:08 +0100 | [diff] [blame] | 730 | avail = audit_expand(ab, 1+len-avail); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 731 | if (!avail) |
| 732 | goto out; |
David Woodhouse | eecb0a7 | 2005-05-10 18:58:51 +0100 | [diff] [blame^] | 733 | len = vsnprintf(skb->tail, avail, fmt, args2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | } |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 735 | skb_put(skb, (len < avail) ? len : avail); |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 736 | out: |
| 737 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /* Format a message into the audit buffer. All the work is done in |
| 741 | * audit_log_vformat. */ |
| 742 | void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) |
| 743 | { |
| 744 | va_list args; |
| 745 | |
| 746 | if (!ab) |
| 747 | return; |
| 748 | va_start(args, fmt); |
| 749 | audit_log_vformat(ab, fmt, args); |
| 750 | va_end(args); |
| 751 | } |
| 752 | |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 753 | void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, size_t len) |
| 754 | { |
| 755 | int i; |
| 756 | |
| 757 | for (i=0; i<len; i++) |
| 758 | audit_log_format(ab, "%02x", buf[i]); |
| 759 | } |
| 760 | |
| 761 | void audit_log_untrustedstring(struct audit_buffer *ab, const char *string) |
| 762 | { |
Andrew Morton | 81b7854 | 2005-04-29 15:59:11 +0100 | [diff] [blame] | 763 | const unsigned char *p = string; |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 764 | |
| 765 | while (*p) { |
| 766 | if (*p == '"' || *p == ' ' || *p < 0x20 || *p > 0x7f) { |
| 767 | audit_log_hex(ab, string, strlen(string)); |
| 768 | return; |
| 769 | } |
| 770 | p++; |
| 771 | } |
| 772 | audit_log_format(ab, "\"%s\"", string); |
| 773 | } |
| 774 | |
| 775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | /* This is a helper-function to print the d_path without using a static |
| 777 | * buffer or allocating another buffer in addition to the one in |
| 778 | * audit_buffer. */ |
| 779 | void audit_log_d_path(struct audit_buffer *ab, const char *prefix, |
| 780 | struct dentry *dentry, struct vfsmount *vfsmnt) |
| 781 | { |
| 782 | char *p; |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 783 | struct sk_buff *skb = ab->skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | int len, avail; |
| 785 | |
Chris Wright | 8fc6115 | 2005-05-06 15:54:17 +0100 | [diff] [blame] | 786 | if (prefix) |
| 787 | audit_log_format(ab, " %s", prefix); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 789 | avail = skb_tailroom(skb); |
| 790 | p = d_path(dentry, vfsmnt, skb->tail, avail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | if (IS_ERR(p)) { |
| 792 | /* FIXME: can we save some information here? */ |
| 793 | audit_log_format(ab, "<toolong>"); |
| 794 | } else { |
Chris Wright | 5ac52f3 | 2005-05-06 15:54:53 +0100 | [diff] [blame] | 795 | /* path isn't at start of buffer */ |
| 796 | len = ((char *)skb->tail + avail - 1) - p; |
| 797 | memmove(skb->tail, p, len); |
| 798 | skb_put(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
| 802 | /* Remove queued messages from the audit_txlist and send them to userspace. */ |
| 803 | static void audit_tasklet_handler(unsigned long arg) |
| 804 | { |
| 805 | LIST_HEAD(list); |
| 806 | struct audit_buffer *ab; |
| 807 | unsigned long flags; |
| 808 | |
| 809 | spin_lock_irqsave(&audit_txlist_lock, flags); |
| 810 | list_splice_init(&audit_txlist, &list); |
| 811 | spin_unlock_irqrestore(&audit_txlist_lock, flags); |
| 812 | |
| 813 | while (!list_empty(&list)) { |
| 814 | ab = list_entry(list.next, struct audit_buffer, list); |
| 815 | list_del(&ab->list); |
| 816 | audit_log_end_fast(ab); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0); |
| 821 | |
| 822 | /* The netlink_* functions cannot be called inside an irq context, so |
| 823 | * the audit buffer is places on a queue and a tasklet is scheduled to |
| 824 | * remove them from the queue outside the irq context. May be called in |
| 825 | * any context. */ |
| 826 | static void audit_log_end_irq(struct audit_buffer *ab) |
| 827 | { |
| 828 | unsigned long flags; |
| 829 | |
| 830 | if (!ab) |
| 831 | return; |
| 832 | spin_lock_irqsave(&audit_txlist_lock, flags); |
| 833 | list_add_tail(&ab->list, &audit_txlist); |
| 834 | spin_unlock_irqrestore(&audit_txlist_lock, flags); |
| 835 | |
| 836 | tasklet_schedule(&audit_tasklet); |
| 837 | } |
| 838 | |
| 839 | /* Send the message in the audit buffer directly to user space. May not |
| 840 | * be called in an irq context. */ |
| 841 | static void audit_log_end_fast(struct audit_buffer *ab) |
| 842 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | BUG_ON(in_irq()); |
| 844 | if (!ab) |
| 845 | return; |
| 846 | if (!audit_rate_check()) { |
| 847 | audit_log_lost("rate limit exceeded"); |
| 848 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | if (audit_log_drain(ab)) |
| 850 | return; |
| 851 | } |
Chris Wright | 16e1904 | 2005-05-06 15:53:34 +0100 | [diff] [blame] | 852 | audit_buffer_free(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /* Send or queue the message in the audit buffer, depending on the |
| 856 | * current context. (A convenience function that may be called in any |
| 857 | * context.) */ |
| 858 | void audit_log_end(struct audit_buffer *ab) |
| 859 | { |
| 860 | if (in_irq()) |
| 861 | audit_log_end_irq(ab); |
| 862 | else |
| 863 | audit_log_end_fast(ab); |
| 864 | } |
| 865 | |
| 866 | /* Log an audit record. This is a convenience function that calls |
| 867 | * audit_log_start, audit_log_vformat, and audit_log_end. It may be |
| 868 | * called in any context. */ |
| 869 | void audit_log(struct audit_context *ctx, const char *fmt, ...) |
| 870 | { |
| 871 | struct audit_buffer *ab; |
| 872 | va_list args; |
| 873 | |
| 874 | ab = audit_log_start(ctx); |
| 875 | if (ab) { |
| 876 | va_start(args, fmt); |
| 877 | audit_log_vformat(ab, fmt, args); |
| 878 | va_end(args); |
| 879 | audit_log_end(ab); |
| 880 | } |
| 881 | } |