| 85c8721 | 2005-04-29 16:23:29 +0100 | [diff] [blame] | 1 | /* auditsc.c -- System-call auditing support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Handles all system-call specific auditing features. |
| 3 | * |
| 4 | * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina. |
| 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | * Written by Rickard E. (Rik) Faith <faith@redhat.com> |
| 22 | * |
| 23 | * Many of the ideas implemented here are from Stephen C. Tweedie, |
| 24 | * especially the idea of avoiding a copy by using getname. |
| 25 | * |
| 26 | * The method for actual interception of syscall entry and exit (not in |
| 27 | * this file -- see entry.S) is based on a GPL'd patch written by |
| 28 | * okir@suse.de and Copyright 2003 SuSE Linux AG. |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #include <linux/init.h> |
| 33 | #include <asm/atomic.h> |
| 34 | #include <asm/types.h> |
| 35 | #include <linux/mm.h> |
| 36 | #include <linux/module.h> |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 37 | #include <linux/mount.h> |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 38 | #include <linux/socket.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/audit.h> |
| 40 | #include <linux/personality.h> |
| 41 | #include <linux/time.h> |
David Woodhouse | f6a789d | 2005-06-21 16:22:01 +0100 | [diff] [blame] | 42 | #include <linux/kthread.h> |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame^] | 43 | #include <linux/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <asm/unistd.h> |
| 45 | |
| 46 | /* 0 = no checking |
| 47 | 1 = put_count checking |
| 48 | 2 = verbose put_count checking |
| 49 | */ |
| 50 | #define AUDIT_DEBUG 0 |
| 51 | |
| 52 | /* No syscall auditing will take place unless audit_enabled != 0. */ |
| 53 | extern int audit_enabled; |
| 54 | |
| 55 | /* AUDIT_NAMES is the number of slots we reserve in the audit_context |
| 56 | * for saving names from getname(). */ |
| 57 | #define AUDIT_NAMES 20 |
| 58 | |
| 59 | /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the |
| 60 | * audit_context from being used for nameless inodes from |
| 61 | * path_lookup. */ |
| 62 | #define AUDIT_NAMES_RESERVED 7 |
| 63 | |
| 64 | /* At task start time, the audit_state is set in the audit_context using |
| 65 | a per-task filter. At syscall entry, the audit_state is augmented by |
| 66 | the syscall filter. */ |
| 67 | enum audit_state { |
| 68 | AUDIT_DISABLED, /* Do not create per-task audit_context. |
| 69 | * No syscall-specific audit records can |
| 70 | * be generated. */ |
| 71 | AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context, |
| 72 | * but don't necessarily fill it in at |
| 73 | * syscall entry time (i.e., filter |
| 74 | * instead). */ |
| 75 | AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context, |
| 76 | * and always fill it in at syscall |
| 77 | * entry time. This makes a full |
| 78 | * syscall record available if some |
| 79 | * other part of the kernel decides it |
| 80 | * should be recorded. */ |
| 81 | AUDIT_RECORD_CONTEXT /* Create the per-task audit_context, |
| 82 | * always fill it in at syscall entry |
| 83 | * time, and always write out the audit |
| 84 | * record at syscall exit time. */ |
| 85 | }; |
| 86 | |
| 87 | /* When fs/namei.c:getname() is called, we store the pointer in name and |
| 88 | * we don't let putname() free it (instead we free all of the saved |
| 89 | * pointers at syscall exit time). |
| 90 | * |
| 91 | * Further, in fs/namei.c:path_lookup() we store the inode and device. */ |
| 92 | struct audit_names { |
| 93 | const char *name; |
| 94 | unsigned long ino; |
| 95 | dev_t dev; |
| 96 | umode_t mode; |
| 97 | uid_t uid; |
| 98 | gid_t gid; |
| 99 | dev_t rdev; |
David Woodhouse | ae7b961b | 2005-06-20 16:11:05 +0100 | [diff] [blame] | 100 | unsigned flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | struct audit_aux_data { |
| 104 | struct audit_aux_data *next; |
| 105 | int type; |
| 106 | }; |
| 107 | |
| 108 | #define AUDIT_AUX_IPCPERM 0 |
| 109 | |
| 110 | struct audit_aux_data_ipcctl { |
| 111 | struct audit_aux_data d; |
| 112 | struct ipc_perm p; |
| 113 | unsigned long qbytes; |
| 114 | uid_t uid; |
| 115 | gid_t gid; |
| 116 | mode_t mode; |
| 117 | }; |
| 118 | |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 119 | struct audit_aux_data_socketcall { |
| 120 | struct audit_aux_data d; |
| 121 | int nargs; |
| 122 | unsigned long args[0]; |
| 123 | }; |
| 124 | |
| 125 | struct audit_aux_data_sockaddr { |
| 126 | struct audit_aux_data d; |
| 127 | int len; |
| 128 | char a[0]; |
| 129 | }; |
| 130 | |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 131 | struct audit_aux_data_path { |
| 132 | struct audit_aux_data d; |
| 133 | struct dentry *dentry; |
| 134 | struct vfsmount *mnt; |
| 135 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | /* The per-task audit context. */ |
| 138 | struct audit_context { |
| 139 | int in_syscall; /* 1 if task is in a syscall */ |
| 140 | enum audit_state state; |
| 141 | unsigned int serial; /* serial number for record */ |
| 142 | struct timespec ctime; /* time of syscall entry */ |
| 143 | uid_t loginuid; /* login uid (identity) */ |
| 144 | int major; /* syscall number */ |
| 145 | unsigned long argv[4]; /* syscall arguments */ |
| 146 | int return_valid; /* return code is valid */ |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 147 | long return_code;/* syscall return code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | int auditable; /* 1 if record should be written */ |
| 149 | int name_count; |
| 150 | struct audit_names names[AUDIT_NAMES]; |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 151 | struct dentry * pwd; |
| 152 | struct vfsmount * pwdmnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | struct audit_context *previous; /* For nested syscalls */ |
| 154 | struct audit_aux_data *aux; |
| 155 | |
| 156 | /* Save things to print about task_struct */ |
| 157 | pid_t pid; |
| 158 | uid_t uid, euid, suid, fsuid; |
| 159 | gid_t gid, egid, sgid, fsgid; |
| 160 | unsigned long personality; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 161 | int arch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | #if AUDIT_DEBUG |
| 164 | int put_count; |
| 165 | int ino_count; |
| 166 | #endif |
| 167 | }; |
| 168 | |
| 169 | /* Public API */ |
| 170 | /* There are three lists of rules -- one to search at task creation |
| 171 | * time, one to search at syscall entry time, and another to search at |
| 172 | * syscall exit time. */ |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 173 | static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = { |
| 174 | LIST_HEAD_INIT(audit_filter_list[0]), |
| 175 | LIST_HEAD_INIT(audit_filter_list[1]), |
| 176 | LIST_HEAD_INIT(audit_filter_list[2]), |
| 177 | LIST_HEAD_INIT(audit_filter_list[3]), |
| 178 | LIST_HEAD_INIT(audit_filter_list[4]), |
| 179 | #if AUDIT_NR_FILTERS != 5 |
| 180 | #error Fix audit_filter_list initialiser |
| 181 | #endif |
| 182 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
| 184 | struct audit_entry { |
| 185 | struct list_head list; |
| 186 | struct rcu_head rcu; |
| 187 | struct audit_rule rule; |
| 188 | }; |
| 189 | |
David Woodhouse | 7ca0026 | 2005-05-19 11:23:13 +0100 | [diff] [blame] | 190 | extern int audit_pid; |
| 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | /* Check to see if two rules are identical. It is called from |
| 193 | * audit_del_rule during AUDIT_DEL. */ |
| 194 | static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b) |
| 195 | { |
| 196 | int i; |
| 197 | |
| 198 | if (a->flags != b->flags) |
| 199 | return 1; |
| 200 | |
| 201 | if (a->action != b->action) |
| 202 | return 1; |
| 203 | |
| 204 | if (a->field_count != b->field_count) |
| 205 | return 1; |
| 206 | |
| 207 | for (i = 0; i < a->field_count; i++) { |
| 208 | if (a->fields[i] != b->fields[i] |
| 209 | || a->values[i] != b->values[i]) |
| 210 | return 1; |
| 211 | } |
| 212 | |
| 213 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) |
| 214 | if (a->mask[i] != b->mask[i]) |
| 215 | return 1; |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /* Note that audit_add_rule and audit_del_rule are called via |
| 221 | * audit_receive() in audit.c, and are protected by |
| 222 | * audit_netlink_sem. */ |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 223 | static inline void audit_add_rule(struct audit_entry *entry, |
| 224 | struct list_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 226 | if (entry->rule.flags & AUDIT_FILTER_PREPEND) { |
| 227 | entry->rule.flags &= ~AUDIT_FILTER_PREPEND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | list_add_rcu(&entry->list, list); |
| 229 | } else { |
| 230 | list_add_tail_rcu(&entry->list, list); |
| 231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static void audit_free_rule(struct rcu_head *head) |
| 235 | { |
| 236 | struct audit_entry *e = container_of(head, struct audit_entry, rcu); |
| 237 | kfree(e); |
| 238 | } |
| 239 | |
| 240 | /* Note that audit_add_rule and audit_del_rule are called via |
| 241 | * audit_receive() in audit.c, and are protected by |
| 242 | * audit_netlink_sem. */ |
| 243 | static inline int audit_del_rule(struct audit_rule *rule, |
| 244 | struct list_head *list) |
| 245 | { |
| 246 | struct audit_entry *e; |
| 247 | |
| 248 | /* Do not use the _rcu iterator here, since this is the only |
| 249 | * deletion routine. */ |
| 250 | list_for_each_entry(e, list, list) { |
| 251 | if (!audit_compare_rule(rule, &e->rule)) { |
| 252 | list_del_rcu(&e->list); |
| 253 | call_rcu(&e->rcu, audit_free_rule); |
| 254 | return 0; |
| 255 | } |
| 256 | } |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 257 | return -ENOENT; /* No matching rule */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | /* Copy rule from user-space to kernel-space. Called during |
| 261 | * AUDIT_ADD. */ |
| 262 | static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s) |
| 263 | { |
| 264 | int i; |
| 265 | |
| 266 | if (s->action != AUDIT_NEVER |
| 267 | && s->action != AUDIT_POSSIBLE |
| 268 | && s->action != AUDIT_ALWAYS) |
| 269 | return -1; |
| 270 | if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS) |
| 271 | return -1; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 272 | if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS) |
| 273 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | d->flags = s->flags; |
| 276 | d->action = s->action; |
| 277 | d->field_count = s->field_count; |
| 278 | for (i = 0; i < d->field_count; i++) { |
| 279 | d->fields[i] = s->fields[i]; |
| 280 | d->values[i] = s->values[i]; |
| 281 | } |
| 282 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i]; |
| 283 | return 0; |
| 284 | } |
| 285 | |
David Woodhouse | f6a789d | 2005-06-21 16:22:01 +0100 | [diff] [blame] | 286 | static int audit_list_rules(void *_dest) |
| 287 | { |
| 288 | int pid, seq; |
| 289 | int *dest = _dest; |
| 290 | struct audit_entry *entry; |
| 291 | int i; |
| 292 | |
| 293 | pid = dest[0]; |
| 294 | seq = dest[1]; |
| 295 | kfree(dest); |
| 296 | |
| 297 | down(&audit_netlink_sem); |
| 298 | |
| 299 | /* The *_rcu iterators not needed here because we are |
| 300 | always called with audit_netlink_sem held. */ |
| 301 | for (i=0; i<AUDIT_NR_FILTERS; i++) { |
| 302 | list_for_each_entry(entry, &audit_filter_list[i], list) |
| 303 | audit_send_reply(pid, seq, AUDIT_LIST, 0, 1, |
| 304 | &entry->rule, sizeof(entry->rule)); |
| 305 | } |
| 306 | audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0); |
| 307 | |
| 308 | up(&audit_netlink_sem); |
| 309 | return 0; |
| 310 | } |
| 311 | |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 312 | int audit_receive_filter(int type, int pid, int uid, int seq, void *data, |
| 313 | uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | struct audit_entry *entry; |
David Woodhouse | f6a789d | 2005-06-21 16:22:01 +0100 | [diff] [blame] | 316 | struct task_struct *tsk; |
| 317 | int *dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | int err = 0; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 319 | unsigned listnr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
| 321 | switch (type) { |
| 322 | case AUDIT_LIST: |
David Woodhouse | f6a789d | 2005-06-21 16:22:01 +0100 | [diff] [blame] | 323 | /* We can't just spew out the rules here because we might fill |
| 324 | * the available socket buffer space and deadlock waiting for |
| 325 | * auditctl to read from it... which isn't ever going to |
| 326 | * happen if we're actually running in the context of auditctl |
| 327 | * trying to _send_ the stuff */ |
| 328 | |
| 329 | dest = kmalloc(2 * sizeof(int), GFP_KERNEL); |
| 330 | if (!dest) |
| 331 | return -ENOMEM; |
| 332 | dest[0] = pid; |
| 333 | dest[1] = seq; |
| 334 | |
| 335 | tsk = kthread_run(audit_list_rules, dest, "audit_list_rules"); |
| 336 | if (IS_ERR(tsk)) { |
| 337 | kfree(dest); |
| 338 | err = PTR_ERR(tsk); |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 339 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | break; |
| 341 | case AUDIT_ADD: |
| 342 | if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL))) |
| 343 | return -ENOMEM; |
| 344 | if (audit_copy_rule(&entry->rule, data)) { |
| 345 | kfree(entry); |
| 346 | return -EINVAL; |
| 347 | } |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 348 | listnr = entry->rule.flags & ~AUDIT_FILTER_PREPEND; |
| 349 | audit_add_rule(entry, &audit_filter_list[listnr]); |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 350 | audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, |
David Woodhouse | bccf6ae | 2005-05-23 21:35:28 +0100 | [diff] [blame] | 351 | "auid=%u added an audit rule\n", loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | break; |
| 353 | case AUDIT_DEL: |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 354 | listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND; |
| 355 | if (listnr >= AUDIT_NR_FILTERS) |
| 356 | return -EINVAL; |
| 357 | |
| 358 | err = audit_del_rule(data, &audit_filter_list[listnr]); |
| 359 | if (!err) |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 360 | audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 361 | "auid=%u removed an audit rule\n", loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | break; |
| 363 | default: |
| 364 | return -EINVAL; |
| 365 | } |
| 366 | |
| 367 | return err; |
| 368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
| 370 | /* Compare a task_struct with an audit_rule. Return 1 on match, 0 |
| 371 | * otherwise. */ |
| 372 | static int audit_filter_rules(struct task_struct *tsk, |
| 373 | struct audit_rule *rule, |
| 374 | struct audit_context *ctx, |
| 375 | enum audit_state *state) |
| 376 | { |
| 377 | int i, j; |
| 378 | |
| 379 | for (i = 0; i < rule->field_count; i++) { |
| 380 | u32 field = rule->fields[i] & ~AUDIT_NEGATE; |
| 381 | u32 value = rule->values[i]; |
| 382 | int result = 0; |
| 383 | |
| 384 | switch (field) { |
| 385 | case AUDIT_PID: |
| 386 | result = (tsk->pid == value); |
| 387 | break; |
| 388 | case AUDIT_UID: |
| 389 | result = (tsk->uid == value); |
| 390 | break; |
| 391 | case AUDIT_EUID: |
| 392 | result = (tsk->euid == value); |
| 393 | break; |
| 394 | case AUDIT_SUID: |
| 395 | result = (tsk->suid == value); |
| 396 | break; |
| 397 | case AUDIT_FSUID: |
| 398 | result = (tsk->fsuid == value); |
| 399 | break; |
| 400 | case AUDIT_GID: |
| 401 | result = (tsk->gid == value); |
| 402 | break; |
| 403 | case AUDIT_EGID: |
| 404 | result = (tsk->egid == value); |
| 405 | break; |
| 406 | case AUDIT_SGID: |
| 407 | result = (tsk->sgid == value); |
| 408 | break; |
| 409 | case AUDIT_FSGID: |
| 410 | result = (tsk->fsgid == value); |
| 411 | break; |
| 412 | case AUDIT_PERS: |
| 413 | result = (tsk->personality == value); |
| 414 | break; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 415 | case AUDIT_ARCH: |
| 416 | if (ctx) |
| 417 | result = (ctx->arch == value); |
| 418 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | case AUDIT_EXIT: |
| 421 | if (ctx && ctx->return_valid) |
| 422 | result = (ctx->return_code == value); |
| 423 | break; |
| 424 | case AUDIT_SUCCESS: |
| 425 | if (ctx && ctx->return_valid) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 426 | result = (ctx->return_valid == AUDITSC_SUCCESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | break; |
| 428 | case AUDIT_DEVMAJOR: |
| 429 | if (ctx) { |
| 430 | for (j = 0; j < ctx->name_count; j++) { |
| 431 | if (MAJOR(ctx->names[j].dev)==value) { |
| 432 | ++result; |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | break; |
| 438 | case AUDIT_DEVMINOR: |
| 439 | if (ctx) { |
| 440 | for (j = 0; j < ctx->name_count; j++) { |
| 441 | if (MINOR(ctx->names[j].dev)==value) { |
| 442 | ++result; |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | break; |
| 448 | case AUDIT_INODE: |
| 449 | if (ctx) { |
| 450 | for (j = 0; j < ctx->name_count; j++) { |
| 451 | if (ctx->names[j].ino == value) { |
| 452 | ++result; |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | break; |
| 458 | case AUDIT_LOGINUID: |
| 459 | result = 0; |
| 460 | if (ctx) |
| 461 | result = (ctx->loginuid == value); |
| 462 | break; |
| 463 | case AUDIT_ARG0: |
| 464 | case AUDIT_ARG1: |
| 465 | case AUDIT_ARG2: |
| 466 | case AUDIT_ARG3: |
| 467 | if (ctx) |
| 468 | result = (ctx->argv[field-AUDIT_ARG0]==value); |
| 469 | break; |
| 470 | } |
| 471 | |
| 472 | if (rule->fields[i] & AUDIT_NEGATE) |
| 473 | result = !result; |
| 474 | if (!result) |
| 475 | return 0; |
| 476 | } |
| 477 | switch (rule->action) { |
| 478 | case AUDIT_NEVER: *state = AUDIT_DISABLED; break; |
| 479 | case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break; |
| 480 | case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break; |
| 481 | } |
| 482 | return 1; |
| 483 | } |
| 484 | |
| 485 | /* At process creation time, we can determine if system-call auditing is |
| 486 | * completely disabled for this task. Since we only have the task |
| 487 | * structure at this point, we can only check uid and gid. |
| 488 | */ |
| 489 | static enum audit_state audit_filter_task(struct task_struct *tsk) |
| 490 | { |
| 491 | struct audit_entry *e; |
| 492 | enum audit_state state; |
| 493 | |
| 494 | rcu_read_lock(); |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 495 | list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | if (audit_filter_rules(tsk, &e->rule, NULL, &state)) { |
| 497 | rcu_read_unlock(); |
| 498 | return state; |
| 499 | } |
| 500 | } |
| 501 | rcu_read_unlock(); |
| 502 | return AUDIT_BUILD_CONTEXT; |
| 503 | } |
| 504 | |
| 505 | /* At syscall entry and exit time, this filter is called if the |
| 506 | * audit_state is not low enough that auditing cannot take place, but is |
Steve Grubb | 23f32d1 | 2005-05-13 18:35:15 +0100 | [diff] [blame] | 507 | * also not high enough that we already know we have to write an audit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT). |
| 509 | */ |
| 510 | static enum audit_state audit_filter_syscall(struct task_struct *tsk, |
| 511 | struct audit_context *ctx, |
| 512 | struct list_head *list) |
| 513 | { |
| 514 | struct audit_entry *e; |
| 515 | enum audit_state state; |
| 516 | int word = AUDIT_WORD(ctx->major); |
| 517 | int bit = AUDIT_BIT(ctx->major); |
| 518 | |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 519 | if (audit_pid && ctx->pid == audit_pid) |
| 520 | return AUDIT_DISABLED; |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | rcu_read_lock(); |
| 523 | list_for_each_entry_rcu(e, list, list) { |
| 524 | if ((e->rule.mask[word] & bit) == bit |
| 525 | && audit_filter_rules(tsk, &e->rule, ctx, &state)) { |
| 526 | rcu_read_unlock(); |
| 527 | return state; |
| 528 | } |
| 529 | } |
| 530 | rcu_read_unlock(); |
| 531 | return AUDIT_BUILD_CONTEXT; |
| 532 | } |
| 533 | |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame^] | 534 | static int audit_filter_user_rules(struct netlink_skb_parms *cb, |
| 535 | struct audit_rule *rule, |
| 536 | enum audit_state *state) |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 537 | { |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame^] | 538 | int i; |
| 539 | |
| 540 | for (i = 0; i < rule->field_count; i++) { |
| 541 | u32 field = rule->fields[i] & ~AUDIT_NEGATE; |
| 542 | u32 value = rule->values[i]; |
| 543 | int result = 0; |
| 544 | |
| 545 | switch (field) { |
| 546 | case AUDIT_PID: |
| 547 | result = (cb->creds.pid == value); |
| 548 | break; |
| 549 | case AUDIT_UID: |
| 550 | result = (cb->creds.uid == value); |
| 551 | break; |
| 552 | case AUDIT_GID: |
| 553 | result = (cb->creds.gid == value); |
| 554 | break; |
| 555 | case AUDIT_LOGINUID: |
| 556 | result = (cb->loginuid == value); |
| 557 | break; |
| 558 | } |
| 559 | |
| 560 | if (rule->fields[i] & AUDIT_NEGATE) |
| 561 | result = !result; |
| 562 | if (!result) |
| 563 | return 0; |
| 564 | } |
| 565 | switch (rule->action) { |
| 566 | case AUDIT_NEVER: *state = AUDIT_DISABLED; break; |
| 567 | case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break; |
| 568 | case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break; |
| 569 | } |
| 570 | return 1; |
| 571 | } |
| 572 | |
| 573 | int audit_filter_user(struct netlink_skb_parms *cb, int type) |
| 574 | { |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 575 | struct audit_entry *e; |
| 576 | enum audit_state state; |
David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 577 | int ret = 1; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 578 | |
| 579 | rcu_read_lock(); |
| 580 | list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) { |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame^] | 581 | if (audit_filter_user_rules(cb, &e->rule, &state)) { |
David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 582 | if (state == AUDIT_DISABLED) |
| 583 | ret = 0; |
| 584 | break; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | rcu_read_unlock(); |
David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 588 | |
David Woodhouse | 993e2d41 | 2005-06-24 08:21:49 +0100 | [diff] [blame] | 589 | return ret; /* Audit by default */ |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | /* This should be called with task_lock() held. */ |
| 593 | static inline struct audit_context *audit_get_context(struct task_struct *tsk, |
| 594 | int return_valid, |
| 595 | int return_code) |
| 596 | { |
| 597 | struct audit_context *context = tsk->audit_context; |
| 598 | |
| 599 | if (likely(!context)) |
| 600 | return NULL; |
| 601 | context->return_valid = return_valid; |
| 602 | context->return_code = return_code; |
| 603 | |
David Woodhouse | 9e94e66 | 2005-06-23 18:33:54 +0100 | [diff] [blame] | 604 | if (context->in_syscall && !context->auditable && tsk->pid != audit_pid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | enum audit_state state; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 606 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | if (state == AUDIT_RECORD_CONTEXT) |
| 608 | context->auditable = 1; |
| 609 | } |
| 610 | |
| 611 | context->pid = tsk->pid; |
| 612 | context->uid = tsk->uid; |
| 613 | context->gid = tsk->gid; |
| 614 | context->euid = tsk->euid; |
| 615 | context->suid = tsk->suid; |
| 616 | context->fsuid = tsk->fsuid; |
| 617 | context->egid = tsk->egid; |
| 618 | context->sgid = tsk->sgid; |
| 619 | context->fsgid = tsk->fsgid; |
| 620 | context->personality = tsk->personality; |
| 621 | tsk->audit_context = NULL; |
| 622 | return context; |
| 623 | } |
| 624 | |
| 625 | static inline void audit_free_names(struct audit_context *context) |
| 626 | { |
| 627 | int i; |
| 628 | |
| 629 | #if AUDIT_DEBUG == 2 |
| 630 | if (context->auditable |
| 631 | ||context->put_count + context->ino_count != context->name_count) { |
| 632 | printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d" |
| 633 | " name_count=%d put_count=%d" |
| 634 | " ino_count=%d [NOT freeing]\n", |
| 635 | __LINE__, |
| 636 | context->serial, context->major, context->in_syscall, |
| 637 | context->name_count, context->put_count, |
| 638 | context->ino_count); |
| 639 | for (i = 0; i < context->name_count; i++) |
| 640 | printk(KERN_ERR "names[%d] = %p = %s\n", i, |
| 641 | context->names[i].name, |
| 642 | context->names[i].name); |
| 643 | dump_stack(); |
| 644 | return; |
| 645 | } |
| 646 | #endif |
| 647 | #if AUDIT_DEBUG |
| 648 | context->put_count = 0; |
| 649 | context->ino_count = 0; |
| 650 | #endif |
| 651 | |
| 652 | for (i = 0; i < context->name_count; i++) |
| 653 | if (context->names[i].name) |
| 654 | __putname(context->names[i].name); |
| 655 | context->name_count = 0; |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 656 | if (context->pwd) |
| 657 | dput(context->pwd); |
| 658 | if (context->pwdmnt) |
| 659 | mntput(context->pwdmnt); |
| 660 | context->pwd = NULL; |
| 661 | context->pwdmnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | static inline void audit_free_aux(struct audit_context *context) |
| 665 | { |
| 666 | struct audit_aux_data *aux; |
| 667 | |
| 668 | while ((aux = context->aux)) { |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 669 | if (aux->type == AUDIT_AVC_PATH) { |
| 670 | struct audit_aux_data_path *axi = (void *)aux; |
| 671 | dput(axi->dentry); |
| 672 | mntput(axi->mnt); |
| 673 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | context->aux = aux->next; |
| 675 | kfree(aux); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | static inline void audit_zero_context(struct audit_context *context, |
| 680 | enum audit_state state) |
| 681 | { |
| 682 | uid_t loginuid = context->loginuid; |
| 683 | |
| 684 | memset(context, 0, sizeof(*context)); |
| 685 | context->state = state; |
| 686 | context->loginuid = loginuid; |
| 687 | } |
| 688 | |
| 689 | static inline struct audit_context *audit_alloc_context(enum audit_state state) |
| 690 | { |
| 691 | struct audit_context *context; |
| 692 | |
| 693 | if (!(context = kmalloc(sizeof(*context), GFP_KERNEL))) |
| 694 | return NULL; |
| 695 | audit_zero_context(context, state); |
| 696 | return context; |
| 697 | } |
| 698 | |
| 699 | /* Filter on the task information and allocate a per-task audit context |
| 700 | * if necessary. Doing so turns on system call auditing for the |
| 701 | * specified task. This is called from copy_process, so no lock is |
| 702 | * needed. */ |
| 703 | int audit_alloc(struct task_struct *tsk) |
| 704 | { |
| 705 | struct audit_context *context; |
| 706 | enum audit_state state; |
| 707 | |
| 708 | if (likely(!audit_enabled)) |
| 709 | return 0; /* Return if not auditing. */ |
| 710 | |
| 711 | state = audit_filter_task(tsk); |
| 712 | if (likely(state == AUDIT_DISABLED)) |
| 713 | return 0; |
| 714 | |
| 715 | if (!(context = audit_alloc_context(state))) { |
| 716 | audit_log_lost("out of memory in audit_alloc"); |
| 717 | return -ENOMEM; |
| 718 | } |
| 719 | |
| 720 | /* Preserve login uid */ |
| 721 | context->loginuid = -1; |
| 722 | if (current->audit_context) |
| 723 | context->loginuid = current->audit_context->loginuid; |
| 724 | |
| 725 | tsk->audit_context = context; |
| 726 | set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT); |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | static inline void audit_free_context(struct audit_context *context) |
| 731 | { |
| 732 | struct audit_context *previous; |
| 733 | int count = 0; |
| 734 | |
| 735 | do { |
| 736 | previous = context->previous; |
| 737 | if (previous || (count && count < 10)) { |
| 738 | ++count; |
| 739 | printk(KERN_ERR "audit(:%d): major=%d name_count=%d:" |
| 740 | " freeing multiple contexts (%d)\n", |
| 741 | context->serial, context->major, |
| 742 | context->name_count, count); |
| 743 | } |
| 744 | audit_free_names(context); |
| 745 | audit_free_aux(context); |
| 746 | kfree(context); |
| 747 | context = previous; |
| 748 | } while (context); |
| 749 | if (count >= 10) |
| 750 | printk(KERN_ERR "audit: freed %d contexts\n", count); |
| 751 | } |
| 752 | |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 753 | static void audit_log_task_info(struct audit_buffer *ab) |
| 754 | { |
| 755 | char name[sizeof(current->comm)]; |
| 756 | struct mm_struct *mm = current->mm; |
| 757 | struct vm_area_struct *vma; |
| 758 | |
| 759 | get_task_comm(name, current); |
David Woodhouse | 99e45ee | 2005-05-23 21:57:41 +0100 | [diff] [blame] | 760 | audit_log_format(ab, " comm="); |
| 761 | audit_log_untrustedstring(ab, name); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 762 | |
| 763 | if (!mm) |
| 764 | return; |
| 765 | |
| 766 | down_read(&mm->mmap_sem); |
| 767 | vma = mm->mmap; |
| 768 | while (vma) { |
| 769 | if ((vma->vm_flags & VM_EXECUTABLE) && |
| 770 | vma->vm_file) { |
| 771 | audit_log_d_path(ab, "exe=", |
| 772 | vma->vm_file->f_dentry, |
| 773 | vma->vm_file->f_vfsmnt); |
| 774 | break; |
| 775 | } |
| 776 | vma = vma->vm_next; |
| 777 | } |
| 778 | up_read(&mm->mmap_sem); |
| 779 | } |
| 780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | static void audit_log_exit(struct audit_context *context) |
| 782 | { |
| 783 | int i; |
| 784 | struct audit_buffer *ab; |
David Woodhouse | 7551ced | 2005-05-26 12:04:57 +0100 | [diff] [blame] | 785 | struct audit_aux_data *aux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 787 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | if (!ab) |
| 789 | return; /* audit_panic has been called */ |
David Woodhouse | bccf6ae | 2005-05-23 21:35:28 +0100 | [diff] [blame] | 790 | audit_log_format(ab, "arch=%x syscall=%d", |
| 791 | context->arch, context->major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | if (context->personality != PER_LINUX) |
| 793 | audit_log_format(ab, " per=%lx", context->personality); |
| 794 | if (context->return_valid) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 795 | audit_log_format(ab, " success=%s exit=%ld", |
| 796 | (context->return_valid==AUDITSC_SUCCESS)?"yes":"no", |
| 797 | context->return_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | audit_log_format(ab, |
| 799 | " a0=%lx a1=%lx a2=%lx a3=%lx items=%d" |
Steve Grubb | 326e9c8 | 2005-05-21 00:22:31 +0100 | [diff] [blame] | 800 | " pid=%d auid=%u uid=%u gid=%u" |
| 801 | " euid=%u suid=%u fsuid=%u" |
| 802 | " egid=%u sgid=%u fsgid=%u", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | context->argv[0], |
| 804 | context->argv[1], |
| 805 | context->argv[2], |
| 806 | context->argv[3], |
| 807 | context->name_count, |
| 808 | context->pid, |
| 809 | context->loginuid, |
| 810 | context->uid, |
| 811 | context->gid, |
| 812 | context->euid, context->suid, context->fsuid, |
| 813 | context->egid, context->sgid, context->fsgid); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 814 | audit_log_task_info(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | audit_log_end(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | |
David Woodhouse | 7551ced | 2005-05-26 12:04:57 +0100 | [diff] [blame] | 817 | for (aux = context->aux; aux; aux = aux->next) { |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 818 | |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 819 | ab = audit_log_start(context, GFP_KERNEL, aux->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | if (!ab) |
| 821 | continue; /* audit_panic has been called */ |
| 822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | switch (aux->type) { |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 824 | case AUDIT_IPC: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | struct audit_aux_data_ipcctl *axi = (void *)aux; |
| 826 | audit_log_format(ab, |
Steve Grubb | 326e9c8 | 2005-05-21 00:22:31 +0100 | [diff] [blame] | 827 | " qbytes=%lx iuid=%u igid=%u mode=%x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | axi->qbytes, axi->uid, axi->gid, axi->mode); |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 829 | break; } |
| 830 | |
| 831 | case AUDIT_SOCKETCALL: { |
| 832 | int i; |
| 833 | struct audit_aux_data_socketcall *axs = (void *)aux; |
| 834 | audit_log_format(ab, "nargs=%d", axs->nargs); |
| 835 | for (i=0; i<axs->nargs; i++) |
| 836 | audit_log_format(ab, " a%d=%lx", i, axs->args[i]); |
| 837 | break; } |
| 838 | |
| 839 | case AUDIT_SOCKADDR: { |
| 840 | struct audit_aux_data_sockaddr *axs = (void *)aux; |
| 841 | |
| 842 | audit_log_format(ab, "saddr="); |
| 843 | audit_log_hex(ab, axs->a, axs->len); |
| 844 | break; } |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 845 | |
| 846 | case AUDIT_AVC_PATH: { |
| 847 | struct audit_aux_data_path *axi = (void *)aux; |
| 848 | audit_log_d_path(ab, "path=", axi->dentry, axi->mnt); |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 849 | break; } |
| 850 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | } |
| 852 | audit_log_end(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | } |
| 854 | |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 855 | if (context->pwd && context->pwdmnt) { |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 856 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD); |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 857 | if (ab) { |
| 858 | audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt); |
| 859 | audit_log_end(ab); |
| 860 | } |
| 861 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | for (i = 0; i < context->name_count; i++) { |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 863 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | if (!ab) |
| 865 | continue; /* audit_panic has been called */ |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | audit_log_format(ab, "item=%d", i); |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 868 | if (context->names[i].name) { |
| 869 | audit_log_format(ab, " name="); |
| 870 | audit_log_untrustedstring(ab, context->names[i].name); |
| 871 | } |
David Woodhouse | ae7b961b | 2005-06-20 16:11:05 +0100 | [diff] [blame] | 872 | audit_log_format(ab, " flags=%x\n", context->names[i].flags); |
| 873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | if (context->names[i].ino != (unsigned long)-1) |
| 875 | audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o" |
Steve Grubb | 326e9c8 | 2005-05-21 00:22:31 +0100 | [diff] [blame] | 876 | " ouid=%u ogid=%u rdev=%02x:%02x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | context->names[i].ino, |
| 878 | MAJOR(context->names[i].dev), |
| 879 | MINOR(context->names[i].dev), |
| 880 | context->names[i].mode, |
| 881 | context->names[i].uid, |
| 882 | context->names[i].gid, |
| 883 | MAJOR(context->names[i].rdev), |
| 884 | MINOR(context->names[i].rdev)); |
| 885 | audit_log_end(ab); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | /* Free a per-task audit context. Called from copy_process and |
| 890 | * __put_task_struct. */ |
| 891 | void audit_free(struct task_struct *tsk) |
| 892 | { |
| 893 | struct audit_context *context; |
| 894 | |
| 895 | task_lock(tsk); |
| 896 | context = audit_get_context(tsk, 0, 0); |
| 897 | task_unlock(tsk); |
| 898 | |
| 899 | if (likely(!context)) |
| 900 | return; |
| 901 | |
| 902 | /* Check for system calls that do not go through the exit |
| 903 | * function (e.g., exit_group), then free context block. */ |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 904 | if (context->in_syscall && context->auditable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | audit_log_exit(context); |
| 906 | |
| 907 | audit_free_context(context); |
| 908 | } |
| 909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | /* Fill in audit context at syscall entry. This only happens if the |
| 911 | * audit context was created when the task was created and the state or |
| 912 | * filters demand the audit context be built. If the state from the |
| 913 | * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT, |
| 914 | * then the record will be written at syscall exit time (otherwise, it |
| 915 | * will only be written if another part of the kernel requests that it |
| 916 | * be written). */ |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 917 | void audit_syscall_entry(struct task_struct *tsk, int arch, int major, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | unsigned long a1, unsigned long a2, |
| 919 | unsigned long a3, unsigned long a4) |
| 920 | { |
| 921 | struct audit_context *context = tsk->audit_context; |
| 922 | enum audit_state state; |
| 923 | |
| 924 | BUG_ON(!context); |
| 925 | |
| 926 | /* This happens only on certain architectures that make system |
| 927 | * calls in kernel_thread via the entry.S interface, instead of |
| 928 | * with direct calls. (If you are porting to a new |
| 929 | * architecture, hitting this condition can indicate that you |
| 930 | * got the _exit/_leave calls backward in entry.S.) |
| 931 | * |
| 932 | * i386 no |
| 933 | * x86_64 no |
| 934 | * ppc64 yes (see arch/ppc64/kernel/misc.S) |
| 935 | * |
| 936 | * This also happens with vm86 emulation in a non-nested manner |
| 937 | * (entries without exits), so this case must be caught. |
| 938 | */ |
| 939 | if (context->in_syscall) { |
| 940 | struct audit_context *newctx; |
| 941 | |
| 942 | #if defined(__NR_vm86) && defined(__NR_vm86old) |
| 943 | /* vm86 mode should only be entered once */ |
| 944 | if (major == __NR_vm86 || major == __NR_vm86old) |
| 945 | return; |
| 946 | #endif |
| 947 | #if AUDIT_DEBUG |
| 948 | printk(KERN_ERR |
| 949 | "audit(:%d) pid=%d in syscall=%d;" |
| 950 | " entering syscall=%d\n", |
| 951 | context->serial, tsk->pid, context->major, major); |
| 952 | #endif |
| 953 | newctx = audit_alloc_context(context->state); |
| 954 | if (newctx) { |
| 955 | newctx->previous = context; |
| 956 | context = newctx; |
| 957 | tsk->audit_context = newctx; |
| 958 | } else { |
| 959 | /* If we can't alloc a new context, the best we |
| 960 | * can do is to leak memory (any pending putname |
| 961 | * will be lost). The only other alternative is |
| 962 | * to abandon auditing. */ |
| 963 | audit_zero_context(context, context->state); |
| 964 | } |
| 965 | } |
| 966 | BUG_ON(context->in_syscall || context->name_count); |
| 967 | |
| 968 | if (!audit_enabled) |
| 969 | return; |
| 970 | |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 971 | context->arch = arch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | context->major = major; |
| 973 | context->argv[0] = a1; |
| 974 | context->argv[1] = a2; |
| 975 | context->argv[2] = a3; |
| 976 | context->argv[3] = a4; |
| 977 | |
| 978 | state = context->state; |
| 979 | if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT) |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 980 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | if (likely(state == AUDIT_DISABLED)) |
| 982 | return; |
| 983 | |
| 984 | context->serial = audit_serial(); |
| 985 | context->ctime = CURRENT_TIME; |
| 986 | context->in_syscall = 1; |
| 987 | context->auditable = !!(state == AUDIT_RECORD_CONTEXT); |
| 988 | } |
| 989 | |
| 990 | /* Tear down after system call. If the audit context has been marked as |
| 991 | * auditable (either because of the AUDIT_RECORD_CONTEXT state from |
| 992 | * filtering, or because some other part of the kernel write an audit |
| 993 | * message), then write out the syscall information. In call cases, |
| 994 | * free the names stored from getname(). */ |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 995 | void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | { |
| 997 | struct audit_context *context; |
| 998 | |
| 999 | get_task_struct(tsk); |
| 1000 | task_lock(tsk); |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1001 | context = audit_get_context(tsk, valid, return_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | task_unlock(tsk); |
| 1003 | |
| 1004 | /* Not having a context here is ok, since the parent may have |
| 1005 | * called __put_task_struct. */ |
| 1006 | if (likely(!context)) |
| 1007 | return; |
| 1008 | |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 1009 | if (context->in_syscall && context->auditable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | audit_log_exit(context); |
| 1011 | |
| 1012 | context->in_syscall = 0; |
| 1013 | context->auditable = 0; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | if (context->previous) { |
| 1016 | struct audit_context *new_context = context->previous; |
| 1017 | context->previous = NULL; |
| 1018 | audit_free_context(context); |
| 1019 | tsk->audit_context = new_context; |
| 1020 | } else { |
| 1021 | audit_free_names(context); |
| 1022 | audit_free_aux(context); |
| 1023 | audit_zero_context(context, context->state); |
| 1024 | tsk->audit_context = context; |
| 1025 | } |
| 1026 | put_task_struct(tsk); |
| 1027 | } |
| 1028 | |
| 1029 | /* Add a name to the list. Called from fs/namei.c:getname(). */ |
| 1030 | void audit_getname(const char *name) |
| 1031 | { |
| 1032 | struct audit_context *context = current->audit_context; |
| 1033 | |
| 1034 | if (!context || IS_ERR(name) || !name) |
| 1035 | return; |
| 1036 | |
| 1037 | if (!context->in_syscall) { |
| 1038 | #if AUDIT_DEBUG == 2 |
| 1039 | printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n", |
| 1040 | __FILE__, __LINE__, context->serial, name); |
| 1041 | dump_stack(); |
| 1042 | #endif |
| 1043 | return; |
| 1044 | } |
| 1045 | BUG_ON(context->name_count >= AUDIT_NAMES); |
| 1046 | context->names[context->name_count].name = name; |
| 1047 | context->names[context->name_count].ino = (unsigned long)-1; |
| 1048 | ++context->name_count; |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1049 | if (!context->pwd) { |
| 1050 | read_lock(¤t->fs->lock); |
| 1051 | context->pwd = dget(current->fs->pwd); |
| 1052 | context->pwdmnt = mntget(current->fs->pwdmnt); |
| 1053 | read_unlock(¤t->fs->lock); |
| 1054 | } |
| 1055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | /* Intercept a putname request. Called from |
| 1059 | * include/linux/fs.h:putname(). If we have stored the name from |
| 1060 | * getname in the audit context, then we delay the putname until syscall |
| 1061 | * exit. */ |
| 1062 | void audit_putname(const char *name) |
| 1063 | { |
| 1064 | struct audit_context *context = current->audit_context; |
| 1065 | |
| 1066 | BUG_ON(!context); |
| 1067 | if (!context->in_syscall) { |
| 1068 | #if AUDIT_DEBUG == 2 |
| 1069 | printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n", |
| 1070 | __FILE__, __LINE__, context->serial, name); |
| 1071 | if (context->name_count) { |
| 1072 | int i; |
| 1073 | for (i = 0; i < context->name_count; i++) |
| 1074 | printk(KERN_ERR "name[%d] = %p = %s\n", i, |
| 1075 | context->names[i].name, |
| 1076 | context->names[i].name); |
| 1077 | } |
| 1078 | #endif |
| 1079 | __putname(name); |
| 1080 | } |
| 1081 | #if AUDIT_DEBUG |
| 1082 | else { |
| 1083 | ++context->put_count; |
| 1084 | if (context->put_count > context->name_count) { |
| 1085 | printk(KERN_ERR "%s:%d(:%d): major=%d" |
| 1086 | " in_syscall=%d putname(%p) name_count=%d" |
| 1087 | " put_count=%d\n", |
| 1088 | __FILE__, __LINE__, |
| 1089 | context->serial, context->major, |
| 1090 | context->in_syscall, name, context->name_count, |
| 1091 | context->put_count); |
| 1092 | dump_stack(); |
| 1093 | } |
| 1094 | } |
| 1095 | #endif |
| 1096 | } |
| 1097 | |
| 1098 | /* Store the inode and device from a lookup. Called from |
| 1099 | * fs/namei.c:path_lookup(). */ |
David Woodhouse | ae7b961b | 2005-06-20 16:11:05 +0100 | [diff] [blame] | 1100 | void audit_inode(const char *name, const struct inode *inode, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | { |
| 1102 | int idx; |
| 1103 | struct audit_context *context = current->audit_context; |
| 1104 | |
| 1105 | if (!context->in_syscall) |
| 1106 | return; |
| 1107 | if (context->name_count |
| 1108 | && context->names[context->name_count-1].name |
| 1109 | && context->names[context->name_count-1].name == name) |
| 1110 | idx = context->name_count - 1; |
| 1111 | else if (context->name_count > 1 |
| 1112 | && context->names[context->name_count-2].name |
| 1113 | && context->names[context->name_count-2].name == name) |
| 1114 | idx = context->name_count - 2; |
| 1115 | else { |
| 1116 | /* FIXME: how much do we care about inodes that have no |
| 1117 | * associated name? */ |
| 1118 | if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED) |
| 1119 | return; |
| 1120 | idx = context->name_count++; |
| 1121 | context->names[idx].name = NULL; |
| 1122 | #if AUDIT_DEBUG |
| 1123 | ++context->ino_count; |
| 1124 | #endif |
| 1125 | } |
David Woodhouse | ae7b961b | 2005-06-20 16:11:05 +0100 | [diff] [blame] | 1126 | context->names[idx].flags = flags; |
| 1127 | context->names[idx].ino = inode->i_ino; |
| 1128 | context->names[idx].dev = inode->i_sb->s_dev; |
| 1129 | context->names[idx].mode = inode->i_mode; |
| 1130 | context->names[idx].uid = inode->i_uid; |
| 1131 | context->names[idx].gid = inode->i_gid; |
| 1132 | context->names[idx].rdev = inode->i_rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1135 | void auditsc_get_stamp(struct audit_context *ctx, |
| 1136 | struct timespec *t, unsigned int *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | { |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1138 | t->tv_sec = ctx->ctime.tv_sec; |
| 1139 | t->tv_nsec = ctx->ctime.tv_nsec; |
| 1140 | *serial = ctx->serial; |
| 1141 | ctx->auditable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
Steve Grubb | 456be6c | 2005-04-29 17:30:07 +0100 | [diff] [blame] | 1144 | int audit_set_loginuid(struct task_struct *task, uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | { |
Steve Grubb | 456be6c | 2005-04-29 17:30:07 +0100 | [diff] [blame] | 1146 | if (task->audit_context) { |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1147 | struct audit_buffer *ab; |
| 1148 | |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1149 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN); |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1150 | if (ab) { |
| 1151 | audit_log_format(ab, "login pid=%d uid=%u " |
Steve Grubb | 326e9c8 | 2005-05-21 00:22:31 +0100 | [diff] [blame] | 1152 | "old auid=%u new auid=%u", |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1153 | task->pid, task->uid, |
| 1154 | task->audit_context->loginuid, loginuid); |
| 1155 | audit_log_end(ab); |
| 1156 | } |
Steve Grubb | 456be6c | 2005-04-29 17:30:07 +0100 | [diff] [blame] | 1157 | task->audit_context->loginuid = loginuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | } |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | uid_t audit_get_loginuid(struct audit_context *ctx) |
| 1163 | { |
| 1164 | return ctx ? ctx->loginuid : -1; |
| 1165 | } |
| 1166 | |
| 1167 | int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode) |
| 1168 | { |
| 1169 | struct audit_aux_data_ipcctl *ax; |
| 1170 | struct audit_context *context = current->audit_context; |
| 1171 | |
| 1172 | if (likely(!context)) |
| 1173 | return 0; |
| 1174 | |
| 1175 | ax = kmalloc(sizeof(*ax), GFP_KERNEL); |
| 1176 | if (!ax) |
| 1177 | return -ENOMEM; |
| 1178 | |
| 1179 | ax->qbytes = qbytes; |
| 1180 | ax->uid = uid; |
| 1181 | ax->gid = gid; |
| 1182 | ax->mode = mode; |
| 1183 | |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1184 | ax->d.type = AUDIT_IPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | ax->d.next = context->aux; |
| 1186 | context->aux = (void *)ax; |
| 1187 | return 0; |
| 1188 | } |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1189 | |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1190 | int audit_socketcall(int nargs, unsigned long *args) |
| 1191 | { |
| 1192 | struct audit_aux_data_socketcall *ax; |
| 1193 | struct audit_context *context = current->audit_context; |
| 1194 | |
| 1195 | if (likely(!context)) |
| 1196 | return 0; |
| 1197 | |
| 1198 | ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL); |
| 1199 | if (!ax) |
| 1200 | return -ENOMEM; |
| 1201 | |
| 1202 | ax->nargs = nargs; |
| 1203 | memcpy(ax->args, args, nargs * sizeof(unsigned long)); |
| 1204 | |
| 1205 | ax->d.type = AUDIT_SOCKETCALL; |
| 1206 | ax->d.next = context->aux; |
| 1207 | context->aux = (void *)ax; |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
| 1211 | int audit_sockaddr(int len, void *a) |
| 1212 | { |
| 1213 | struct audit_aux_data_sockaddr *ax; |
| 1214 | struct audit_context *context = current->audit_context; |
| 1215 | |
| 1216 | if (likely(!context)) |
| 1217 | return 0; |
| 1218 | |
| 1219 | ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL); |
| 1220 | if (!ax) |
| 1221 | return -ENOMEM; |
| 1222 | |
| 1223 | ax->len = len; |
| 1224 | memcpy(ax->a, a, len); |
| 1225 | |
| 1226 | ax->d.type = AUDIT_SOCKADDR; |
| 1227 | ax->d.next = context->aux; |
| 1228 | context->aux = (void *)ax; |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 1232 | int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt) |
| 1233 | { |
| 1234 | struct audit_aux_data_path *ax; |
| 1235 | struct audit_context *context = current->audit_context; |
| 1236 | |
| 1237 | if (likely(!context)) |
| 1238 | return 0; |
| 1239 | |
| 1240 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 1241 | if (!ax) |
| 1242 | return -ENOMEM; |
| 1243 | |
| 1244 | ax->dentry = dget(dentry); |
| 1245 | ax->mnt = mntget(mnt); |
| 1246 | |
| 1247 | ax->d.type = AUDIT_AVC_PATH; |
| 1248 | ax->d.next = context->aux; |
| 1249 | context->aux = (void *)ax; |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1253 | void audit_signal_info(int sig, struct task_struct *t) |
| 1254 | { |
| 1255 | extern pid_t audit_sig_pid; |
| 1256 | extern uid_t audit_sig_uid; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1257 | |
| 1258 | if (unlikely(audit_pid && t->pid == audit_pid)) { |
| 1259 | if (sig == SIGTERM || sig == SIGHUP) { |
| 1260 | struct audit_context *ctx = current->audit_context; |
| 1261 | audit_sig_pid = current->pid; |
| 1262 | if (ctx) |
| 1263 | audit_sig_uid = ctx->loginuid; |
| 1264 | else |
| 1265 | audit_sig_uid = current->uid; |
| 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | |