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