| 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. |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 5 | * Copyright 2005 Hewlett-Packard Development Company, L.P. |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 6 | * Copyright (C) 2005 IBM Corporation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * All Rights Reserved. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | * Written by Rickard E. (Rik) Faith <faith@redhat.com> |
| 24 | * |
| 25 | * Many of the ideas implemented here are from Stephen C. Tweedie, |
| 26 | * especially the idea of avoiding a copy by using getname. |
| 27 | * |
| 28 | * The method for actual interception of syscall entry and exit (not in |
| 29 | * this file -- see entry.S) is based on a GPL'd patch written by |
| 30 | * okir@suse.de and Copyright 2003 SuSE Linux AG. |
| 31 | * |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 32 | * The support of additional filter rules compares (>, <, >=, <=) was |
| 33 | * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005. |
| 34 | * |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 35 | * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional |
| 36 | * filesystem information. |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 37 | * |
| 38 | * Subject and object context labeling support added by <danjones@us.ibm.com> |
| 39 | * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | */ |
| 41 | |
| 42 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/types.h> |
Alan Cox | 715b49e | 2006-01-18 17:44:07 -0800 | [diff] [blame] | 44 | #include <asm/atomic.h> |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 45 | #include <asm/types.h> |
| 46 | #include <linux/fs.h> |
| 47 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/mm.h> |
| 49 | #include <linux/module.h> |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 50 | #include <linux/mount.h> |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 51 | #include <linux/socket.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/audit.h> |
| 53 | #include <linux/personality.h> |
| 54 | #include <linux/time.h> |
David Woodhouse | f6a789d | 2005-06-21 16:22:01 +0100 | [diff] [blame] | 55 | #include <linux/kthread.h> |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame] | 56 | #include <linux/netlink.h> |
David Woodhouse | f556196 | 2005-07-13 22:47:07 +0100 | [diff] [blame] | 57 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <asm/unistd.h> |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 59 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | /* 0 = no checking |
| 62 | 1 = put_count checking |
| 63 | 2 = verbose put_count checking |
| 64 | */ |
| 65 | #define AUDIT_DEBUG 0 |
| 66 | |
| 67 | /* No syscall auditing will take place unless audit_enabled != 0. */ |
| 68 | extern int audit_enabled; |
| 69 | |
| 70 | /* AUDIT_NAMES is the number of slots we reserve in the audit_context |
| 71 | * for saving names from getname(). */ |
| 72 | #define AUDIT_NAMES 20 |
| 73 | |
| 74 | /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the |
| 75 | * audit_context from being used for nameless inodes from |
| 76 | * path_lookup. */ |
| 77 | #define AUDIT_NAMES_RESERVED 7 |
| 78 | |
| 79 | /* At task start time, the audit_state is set in the audit_context using |
| 80 | a per-task filter. At syscall entry, the audit_state is augmented by |
| 81 | the syscall filter. */ |
| 82 | enum audit_state { |
| 83 | AUDIT_DISABLED, /* Do not create per-task audit_context. |
| 84 | * No syscall-specific audit records can |
| 85 | * be generated. */ |
| 86 | AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context, |
| 87 | * but don't necessarily fill it in at |
| 88 | * syscall entry time (i.e., filter |
| 89 | * instead). */ |
| 90 | AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context, |
| 91 | * and always fill it in at syscall |
| 92 | * entry time. This makes a full |
| 93 | * syscall record available if some |
| 94 | * other part of the kernel decides it |
| 95 | * should be recorded. */ |
| 96 | AUDIT_RECORD_CONTEXT /* Create the per-task audit_context, |
| 97 | * always fill it in at syscall entry |
| 98 | * time, and always write out the audit |
| 99 | * record at syscall exit time. */ |
| 100 | }; |
| 101 | |
| 102 | /* When fs/namei.c:getname() is called, we store the pointer in name and |
| 103 | * we don't let putname() free it (instead we free all of the saved |
| 104 | * pointers at syscall exit time). |
| 105 | * |
| 106 | * Further, in fs/namei.c:path_lookup() we store the inode and device. */ |
| 107 | struct audit_names { |
| 108 | const char *name; |
| 109 | unsigned long ino; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 110 | unsigned long pino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | dev_t dev; |
| 112 | umode_t mode; |
| 113 | uid_t uid; |
| 114 | gid_t gid; |
| 115 | dev_t rdev; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 116 | char *ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | struct audit_aux_data { |
| 120 | struct audit_aux_data *next; |
| 121 | int type; |
| 122 | }; |
| 123 | |
| 124 | #define AUDIT_AUX_IPCPERM 0 |
| 125 | |
| 126 | struct audit_aux_data_ipcctl { |
| 127 | struct audit_aux_data d; |
| 128 | struct ipc_perm p; |
| 129 | unsigned long qbytes; |
| 130 | uid_t uid; |
| 131 | gid_t gid; |
| 132 | mode_t mode; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 133 | char *ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 136 | struct audit_aux_data_socketcall { |
| 137 | struct audit_aux_data d; |
| 138 | int nargs; |
| 139 | unsigned long args[0]; |
| 140 | }; |
| 141 | |
| 142 | struct audit_aux_data_sockaddr { |
| 143 | struct audit_aux_data d; |
| 144 | int len; |
| 145 | char a[0]; |
| 146 | }; |
| 147 | |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 148 | struct audit_aux_data_path { |
| 149 | struct audit_aux_data d; |
| 150 | struct dentry *dentry; |
| 151 | struct vfsmount *mnt; |
| 152 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | /* The per-task audit context. */ |
| 155 | struct audit_context { |
| 156 | int in_syscall; /* 1 if task is in a syscall */ |
| 157 | enum audit_state state; |
| 158 | unsigned int serial; /* serial number for record */ |
| 159 | struct timespec ctime; /* time of syscall entry */ |
| 160 | uid_t loginuid; /* login uid (identity) */ |
| 161 | int major; /* syscall number */ |
| 162 | unsigned long argv[4]; /* syscall arguments */ |
| 163 | int return_valid; /* return code is valid */ |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 164 | long return_code;/* syscall return code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | int auditable; /* 1 if record should be written */ |
| 166 | int name_count; |
| 167 | struct audit_names names[AUDIT_NAMES]; |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 168 | struct dentry * pwd; |
| 169 | struct vfsmount * pwdmnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | struct audit_context *previous; /* For nested syscalls */ |
| 171 | struct audit_aux_data *aux; |
| 172 | |
| 173 | /* Save things to print about task_struct */ |
| 174 | pid_t pid; |
| 175 | uid_t uid, euid, suid, fsuid; |
| 176 | gid_t gid, egid, sgid, fsgid; |
| 177 | unsigned long personality; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 178 | int arch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | #if AUDIT_DEBUG |
| 181 | int put_count; |
| 182 | int ino_count; |
| 183 | #endif |
| 184 | }; |
| 185 | |
| 186 | /* Public API */ |
| 187 | /* There are three lists of rules -- one to search at task creation |
| 188 | * time, one to search at syscall entry time, and another to search at |
| 189 | * syscall exit time. */ |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 190 | static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = { |
| 191 | LIST_HEAD_INIT(audit_filter_list[0]), |
| 192 | LIST_HEAD_INIT(audit_filter_list[1]), |
| 193 | LIST_HEAD_INIT(audit_filter_list[2]), |
| 194 | LIST_HEAD_INIT(audit_filter_list[3]), |
| 195 | LIST_HEAD_INIT(audit_filter_list[4]), |
Dustin Kirkland | c8edc80 | 2005-11-03 16:12:36 +0000 | [diff] [blame] | 196 | LIST_HEAD_INIT(audit_filter_list[5]), |
| 197 | #if AUDIT_NR_FILTERS != 6 |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 198 | #error Fix audit_filter_list initialiser |
| 199 | #endif |
| 200 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | struct audit_entry { |
| 203 | struct list_head list; |
| 204 | struct rcu_head rcu; |
| 205 | struct audit_rule rule; |
| 206 | }; |
| 207 | |
David Woodhouse | 7ca0026 | 2005-05-19 11:23:13 +0100 | [diff] [blame] | 208 | extern int audit_pid; |
| 209 | |
Amy Griffis | 3c789a1 | 2005-08-17 16:05:35 +0100 | [diff] [blame] | 210 | /* Copy rule from user-space to kernel-space. Called from |
| 211 | * audit_add_rule during AUDIT_ADD. */ |
| 212 | static inline int audit_copy_rule(struct audit_rule *d, struct audit_rule *s) |
| 213 | { |
| 214 | int i; |
| 215 | |
| 216 | if (s->action != AUDIT_NEVER |
| 217 | && s->action != AUDIT_POSSIBLE |
| 218 | && s->action != AUDIT_ALWAYS) |
| 219 | return -1; |
| 220 | if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS) |
| 221 | return -1; |
| 222 | if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS) |
| 223 | return -1; |
| 224 | |
| 225 | d->flags = s->flags; |
| 226 | d->action = s->action; |
| 227 | d->field_count = s->field_count; |
| 228 | for (i = 0; i < d->field_count; i++) { |
| 229 | d->fields[i] = s->fields[i]; |
| 230 | d->values[i] = s->values[i]; |
| 231 | } |
| 232 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i]; |
| 233 | return 0; |
| 234 | } |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | /* Check to see if two rules are identical. It is called from |
Amy Griffis | 3c789a1 | 2005-08-17 16:05:35 +0100 | [diff] [blame] | 237 | * audit_add_rule during AUDIT_ADD and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | * audit_del_rule during AUDIT_DEL. */ |
Amy Griffis | 3c789a1 | 2005-08-17 16:05:35 +0100 | [diff] [blame] | 239 | static inline int audit_compare_rule(struct audit_rule *a, struct audit_rule *b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
| 241 | int i; |
| 242 | |
| 243 | if (a->flags != b->flags) |
| 244 | return 1; |
| 245 | |
| 246 | if (a->action != b->action) |
| 247 | return 1; |
| 248 | |
| 249 | if (a->field_count != b->field_count) |
| 250 | return 1; |
| 251 | |
| 252 | for (i = 0; i < a->field_count; i++) { |
| 253 | if (a->fields[i] != b->fields[i] |
| 254 | || a->values[i] != b->values[i]) |
| 255 | return 1; |
| 256 | } |
| 257 | |
| 258 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) |
| 259 | if (a->mask[i] != b->mask[i]) |
| 260 | return 1; |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /* Note that audit_add_rule and audit_del_rule are called via |
| 266 | * audit_receive() in audit.c, and are protected by |
| 267 | * audit_netlink_sem. */ |
Amy Griffis | 3c789a1 | 2005-08-17 16:05:35 +0100 | [diff] [blame] | 268 | static inline int audit_add_rule(struct audit_rule *rule, |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 269 | struct list_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
Amy Griffis | 3c789a1 | 2005-08-17 16:05:35 +0100 | [diff] [blame] | 271 | struct audit_entry *entry; |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 272 | int i; |
Amy Griffis | 3c789a1 | 2005-08-17 16:05:35 +0100 | [diff] [blame] | 273 | |
| 274 | /* Do not use the _rcu iterator here, since this is the only |
| 275 | * addition routine. */ |
| 276 | list_for_each_entry(entry, list, list) { |
| 277 | if (!audit_compare_rule(rule, &entry->rule)) { |
| 278 | return -EEXIST; |
| 279 | } |
| 280 | } |
| 281 | |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 282 | for (i = 0; i < rule->field_count; i++) { |
| 283 | if (rule->fields[i] & AUDIT_UNUSED_BITS) |
| 284 | return -EINVAL; |
| 285 | if ( rule->fields[i] & AUDIT_NEGATE ) |
| 286 | rule->fields[i] |= AUDIT_NOT_EQUAL; |
| 287 | else if ( (rule->fields[i] & AUDIT_OPERATORS) == 0 ) |
| 288 | rule->fields[i] |= AUDIT_EQUAL; |
| 289 | rule->fields[i] &= (~AUDIT_NEGATE); |
| 290 | } |
| 291 | |
Amy Griffis | 3c789a1 | 2005-08-17 16:05:35 +0100 | [diff] [blame] | 292 | if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL))) |
| 293 | return -ENOMEM; |
| 294 | if (audit_copy_rule(&entry->rule, rule)) { |
| 295 | kfree(entry); |
| 296 | return -EINVAL; |
| 297 | } |
| 298 | |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 299 | if (entry->rule.flags & AUDIT_FILTER_PREPEND) { |
| 300 | entry->rule.flags &= ~AUDIT_FILTER_PREPEND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | list_add_rcu(&entry->list, list); |
| 302 | } else { |
| 303 | list_add_tail_rcu(&entry->list, list); |
| 304 | } |
Amy Griffis | 3c789a1 | 2005-08-17 16:05:35 +0100 | [diff] [blame] | 305 | |
| 306 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Amy Griffis | 3c789a1 | 2005-08-17 16:05:35 +0100 | [diff] [blame] | 309 | static inline void audit_free_rule(struct rcu_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | { |
| 311 | struct audit_entry *e = container_of(head, struct audit_entry, rcu); |
| 312 | kfree(e); |
| 313 | } |
| 314 | |
| 315 | /* Note that audit_add_rule and audit_del_rule are called via |
| 316 | * audit_receive() in audit.c, and are protected by |
| 317 | * audit_netlink_sem. */ |
| 318 | static inline int audit_del_rule(struct audit_rule *rule, |
| 319 | struct list_head *list) |
| 320 | { |
| 321 | struct audit_entry *e; |
| 322 | |
| 323 | /* Do not use the _rcu iterator here, since this is the only |
| 324 | * deletion routine. */ |
| 325 | list_for_each_entry(e, list, list) { |
| 326 | if (!audit_compare_rule(rule, &e->rule)) { |
| 327 | list_del_rcu(&e->list); |
| 328 | call_rcu(&e->rcu, audit_free_rule); |
| 329 | return 0; |
| 330 | } |
| 331 | } |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 332 | return -ENOENT; /* No matching rule */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } |
| 334 | |
David Woodhouse | f6a789d | 2005-06-21 16:22:01 +0100 | [diff] [blame] | 335 | static int audit_list_rules(void *_dest) |
| 336 | { |
| 337 | int pid, seq; |
| 338 | int *dest = _dest; |
| 339 | struct audit_entry *entry; |
| 340 | int i; |
| 341 | |
| 342 | pid = dest[0]; |
| 343 | seq = dest[1]; |
| 344 | kfree(dest); |
| 345 | |
| 346 | down(&audit_netlink_sem); |
| 347 | |
| 348 | /* The *_rcu iterators not needed here because we are |
| 349 | always called with audit_netlink_sem held. */ |
| 350 | for (i=0; i<AUDIT_NR_FILTERS; i++) { |
| 351 | list_for_each_entry(entry, &audit_filter_list[i], list) |
| 352 | audit_send_reply(pid, seq, AUDIT_LIST, 0, 1, |
| 353 | &entry->rule, sizeof(entry->rule)); |
| 354 | } |
| 355 | audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0); |
| 356 | |
| 357 | up(&audit_netlink_sem); |
| 358 | return 0; |
| 359 | } |
| 360 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 361 | /** |
| 362 | * audit_receive_filter - apply all rules to the specified message type |
| 363 | * @type: audit message type |
| 364 | * @pid: target pid for netlink audit messages |
| 365 | * @uid: target uid for netlink audit messages |
| 366 | * @seq: netlink audit message sequence (serial) number |
| 367 | * @data: payload data |
| 368 | * @loginuid: loginuid of sender |
| 369 | */ |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 370 | int audit_receive_filter(int type, int pid, int uid, int seq, void *data, |
| 371 | uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
David Woodhouse | f6a789d | 2005-06-21 16:22:01 +0100 | [diff] [blame] | 373 | struct task_struct *tsk; |
| 374 | int *dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | int err = 0; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 376 | unsigned listnr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
| 378 | switch (type) { |
| 379 | case AUDIT_LIST: |
David Woodhouse | f6a789d | 2005-06-21 16:22:01 +0100 | [diff] [blame] | 380 | /* We can't just spew out the rules here because we might fill |
| 381 | * the available socket buffer space and deadlock waiting for |
| 382 | * auditctl to read from it... which isn't ever going to |
| 383 | * happen if we're actually running in the context of auditctl |
| 384 | * trying to _send_ the stuff */ |
| 385 | |
| 386 | dest = kmalloc(2 * sizeof(int), GFP_KERNEL); |
| 387 | if (!dest) |
| 388 | return -ENOMEM; |
| 389 | dest[0] = pid; |
| 390 | dest[1] = seq; |
| 391 | |
| 392 | tsk = kthread_run(audit_list_rules, dest, "audit_list_rules"); |
| 393 | if (IS_ERR(tsk)) { |
| 394 | kfree(dest); |
| 395 | err = PTR_ERR(tsk); |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | break; |
| 398 | case AUDIT_ADD: |
Amy Griffis | 3c789a1 | 2005-08-17 16:05:35 +0100 | [diff] [blame] | 399 | listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND; |
| 400 | if (listnr >= AUDIT_NR_FILTERS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | return -EINVAL; |
Amy Griffis | 3c789a1 | 2005-08-17 16:05:35 +0100 | [diff] [blame] | 402 | |
| 403 | err = audit_add_rule(data, &audit_filter_list[listnr]); |
| 404 | if (!err) |
| 405 | audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, |
| 406 | "auid=%u added an audit rule\n", loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | break; |
| 408 | case AUDIT_DEL: |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 409 | listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND; |
| 410 | if (listnr >= AUDIT_NR_FILTERS) |
| 411 | return -EINVAL; |
| 412 | |
| 413 | err = audit_del_rule(data, &audit_filter_list[listnr]); |
| 414 | if (!err) |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 415 | audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 416 | "auid=%u removed an audit rule\n", loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | break; |
| 418 | default: |
| 419 | return -EINVAL; |
| 420 | } |
| 421 | |
| 422 | return err; |
| 423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 425 | static int audit_comparator(const u32 left, const u32 op, const u32 right) |
| 426 | { |
| 427 | switch (op) { |
| 428 | case AUDIT_EQUAL: |
| 429 | return (left == right); |
| 430 | case AUDIT_NOT_EQUAL: |
| 431 | return (left != right); |
| 432 | case AUDIT_LESS_THAN: |
| 433 | return (left < right); |
| 434 | case AUDIT_LESS_THAN_OR_EQUAL: |
| 435 | return (left <= right); |
| 436 | case AUDIT_GREATER_THAN: |
| 437 | return (left > right); |
| 438 | case AUDIT_GREATER_THAN_OR_EQUAL: |
| 439 | return (left >= right); |
| 440 | default: |
| 441 | return -EINVAL; |
| 442 | } |
| 443 | } |
| 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | /* Compare a task_struct with an audit_rule. Return 1 on match, 0 |
| 446 | * otherwise. */ |
| 447 | static int audit_filter_rules(struct task_struct *tsk, |
| 448 | struct audit_rule *rule, |
| 449 | struct audit_context *ctx, |
| 450 | enum audit_state *state) |
| 451 | { |
| 452 | int i, j; |
| 453 | |
| 454 | for (i = 0; i < rule->field_count; i++) { |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 455 | u32 field = rule->fields[i] & ~AUDIT_OPERATORS; |
| 456 | u32 op = rule->fields[i] & AUDIT_OPERATORS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | u32 value = rule->values[i]; |
| 458 | int result = 0; |
| 459 | |
| 460 | switch (field) { |
| 461 | case AUDIT_PID: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 462 | result = audit_comparator(tsk->pid, op, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | break; |
| 464 | case AUDIT_UID: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 465 | result = audit_comparator(tsk->uid, op, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | break; |
| 467 | case AUDIT_EUID: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 468 | result = audit_comparator(tsk->euid, op, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | break; |
| 470 | case AUDIT_SUID: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 471 | result = audit_comparator(tsk->suid, op, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | break; |
| 473 | case AUDIT_FSUID: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 474 | result = audit_comparator(tsk->fsuid, op, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | break; |
| 476 | case AUDIT_GID: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 477 | result = audit_comparator(tsk->gid, op, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | break; |
| 479 | case AUDIT_EGID: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 480 | result = audit_comparator(tsk->egid, op, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | break; |
| 482 | case AUDIT_SGID: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 483 | result = audit_comparator(tsk->sgid, op, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | break; |
| 485 | case AUDIT_FSGID: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 486 | result = audit_comparator(tsk->fsgid, op, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | break; |
| 488 | case AUDIT_PERS: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 489 | result = audit_comparator(tsk->personality, op, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | break; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 491 | case AUDIT_ARCH: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 492 | if (ctx) |
| 493 | result = audit_comparator(ctx->arch, op, value); |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 494 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | case AUDIT_EXIT: |
| 497 | if (ctx && ctx->return_valid) |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 498 | result = audit_comparator(ctx->return_code, op, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | break; |
| 500 | case AUDIT_SUCCESS: |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 501 | if (ctx && ctx->return_valid) { |
| 502 | if (value) |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 503 | result = audit_comparator(ctx->return_valid, op, AUDITSC_SUCCESS); |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 504 | else |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 505 | result = audit_comparator(ctx->return_valid, op, AUDITSC_FAILURE); |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 506 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | break; |
| 508 | case AUDIT_DEVMAJOR: |
| 509 | if (ctx) { |
| 510 | for (j = 0; j < ctx->name_count; j++) { |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 511 | if (audit_comparator(MAJOR(ctx->names[j].dev), op, value)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | ++result; |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | break; |
| 518 | case AUDIT_DEVMINOR: |
| 519 | if (ctx) { |
| 520 | for (j = 0; j < ctx->name_count; j++) { |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 521 | if (audit_comparator(MINOR(ctx->names[j].dev), op, value)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | ++result; |
| 523 | break; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | break; |
| 528 | case AUDIT_INODE: |
| 529 | if (ctx) { |
| 530 | for (j = 0; j < ctx->name_count; j++) { |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 531 | if (audit_comparator(ctx->names[j].ino, op, value) || |
| 532 | audit_comparator(ctx->names[j].pino, op, value)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | ++result; |
| 534 | break; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | break; |
| 539 | case AUDIT_LOGINUID: |
| 540 | result = 0; |
| 541 | if (ctx) |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 542 | result = audit_comparator(ctx->loginuid, op, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | break; |
| 544 | case AUDIT_ARG0: |
| 545 | case AUDIT_ARG1: |
| 546 | case AUDIT_ARG2: |
| 547 | case AUDIT_ARG3: |
| 548 | if (ctx) |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 549 | result = audit_comparator(ctx->argv[field-AUDIT_ARG0], op, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | break; |
| 551 | } |
| 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | if (!result) |
| 554 | return 0; |
| 555 | } |
| 556 | switch (rule->action) { |
| 557 | case AUDIT_NEVER: *state = AUDIT_DISABLED; break; |
| 558 | case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break; |
| 559 | case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break; |
| 560 | } |
| 561 | return 1; |
| 562 | } |
| 563 | |
| 564 | /* At process creation time, we can determine if system-call auditing is |
| 565 | * completely disabled for this task. Since we only have the task |
| 566 | * structure at this point, we can only check uid and gid. |
| 567 | */ |
| 568 | static enum audit_state audit_filter_task(struct task_struct *tsk) |
| 569 | { |
| 570 | struct audit_entry *e; |
| 571 | enum audit_state state; |
| 572 | |
| 573 | rcu_read_lock(); |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 574 | 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] | 575 | if (audit_filter_rules(tsk, &e->rule, NULL, &state)) { |
| 576 | rcu_read_unlock(); |
| 577 | return state; |
| 578 | } |
| 579 | } |
| 580 | rcu_read_unlock(); |
| 581 | return AUDIT_BUILD_CONTEXT; |
| 582 | } |
| 583 | |
| 584 | /* At syscall entry and exit time, this filter is called if the |
| 585 | * 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] | 586 | * also not high enough that we already know we have to write an audit |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 587 | * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | */ |
| 589 | static enum audit_state audit_filter_syscall(struct task_struct *tsk, |
| 590 | struct audit_context *ctx, |
| 591 | struct list_head *list) |
| 592 | { |
| 593 | struct audit_entry *e; |
David Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 594 | enum audit_state state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
David Woodhouse | 351bb72 | 2005-07-14 14:40:06 +0100 | [diff] [blame] | 596 | if (audit_pid && tsk->tgid == audit_pid) |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 597 | return AUDIT_DISABLED; |
| 598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | rcu_read_lock(); |
David Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 600 | if (!list_empty(list)) { |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 601 | int word = AUDIT_WORD(ctx->major); |
| 602 | int bit = AUDIT_BIT(ctx->major); |
David Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 603 | |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 604 | list_for_each_entry_rcu(e, list, list) { |
| 605 | if ((e->rule.mask[word] & bit) == bit |
| 606 | && audit_filter_rules(tsk, &e->rule, ctx, &state)) { |
| 607 | rcu_read_unlock(); |
| 608 | return state; |
| 609 | } |
| 610 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
| 612 | rcu_read_unlock(); |
| 613 | return AUDIT_BUILD_CONTEXT; |
| 614 | } |
| 615 | |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame] | 616 | static int audit_filter_user_rules(struct netlink_skb_parms *cb, |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 617 | struct audit_rule *rule, |
| 618 | enum audit_state *state) |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 619 | { |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame] | 620 | int i; |
| 621 | |
| 622 | for (i = 0; i < rule->field_count; i++) { |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 623 | u32 field = rule->fields[i] & ~AUDIT_OPERATORS; |
| 624 | u32 op = rule->fields[i] & AUDIT_OPERATORS; |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame] | 625 | u32 value = rule->values[i]; |
| 626 | int result = 0; |
| 627 | |
| 628 | switch (field) { |
| 629 | case AUDIT_PID: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 630 | result = audit_comparator(cb->creds.pid, op, value); |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame] | 631 | break; |
| 632 | case AUDIT_UID: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 633 | result = audit_comparator(cb->creds.uid, op, value); |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame] | 634 | break; |
| 635 | case AUDIT_GID: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 636 | result = audit_comparator(cb->creds.gid, op, value); |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame] | 637 | break; |
| 638 | case AUDIT_LOGINUID: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 639 | result = audit_comparator(cb->loginuid, op, value); |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame] | 640 | break; |
| 641 | } |
| 642 | |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame] | 643 | if (!result) |
| 644 | return 0; |
| 645 | } |
| 646 | switch (rule->action) { |
| 647 | case AUDIT_NEVER: *state = AUDIT_DISABLED; break; |
| 648 | case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break; |
| 649 | case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break; |
| 650 | } |
| 651 | return 1; |
| 652 | } |
| 653 | |
| 654 | int audit_filter_user(struct netlink_skb_parms *cb, int type) |
| 655 | { |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 656 | struct audit_entry *e; |
| 657 | enum audit_state state; |
David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 658 | int ret = 1; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 659 | |
| 660 | rcu_read_lock(); |
| 661 | 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] | 662 | if (audit_filter_user_rules(cb, &e->rule, &state)) { |
David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 663 | if (state == AUDIT_DISABLED) |
| 664 | ret = 0; |
| 665 | break; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | rcu_read_unlock(); |
David Woodhouse | 4a4cd63 | 2005-06-22 14:56:47 +0100 | [diff] [blame] | 669 | |
David Woodhouse | 993e2d4 | 2005-06-24 08:21:49 +0100 | [diff] [blame] | 670 | return ret; /* Audit by default */ |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 671 | } |
| 672 | |
Dustin Kirkland | c8edc80 | 2005-11-03 16:12:36 +0000 | [diff] [blame] | 673 | int audit_filter_type(int type) |
| 674 | { |
| 675 | struct audit_entry *e; |
| 676 | int result = 0; |
| 677 | |
| 678 | rcu_read_lock(); |
| 679 | if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE])) |
| 680 | goto unlock_and_return; |
| 681 | |
| 682 | list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE], |
| 683 | list) { |
| 684 | struct audit_rule *rule = &e->rule; |
| 685 | int i; |
| 686 | for (i = 0; i < rule->field_count; i++) { |
| 687 | u32 field = rule->fields[i] & ~AUDIT_OPERATORS; |
| 688 | u32 op = rule->fields[i] & AUDIT_OPERATORS; |
| 689 | u32 value = rule->values[i]; |
| 690 | if ( field == AUDIT_MSGTYPE ) { |
| 691 | result = audit_comparator(type, op, value); |
| 692 | if (!result) |
| 693 | break; |
| 694 | } |
| 695 | } |
| 696 | if (result) |
| 697 | goto unlock_and_return; |
| 698 | } |
| 699 | unlock_and_return: |
| 700 | rcu_read_unlock(); |
| 701 | return result; |
| 702 | } |
| 703 | |
| 704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | /* This should be called with task_lock() held. */ |
| 706 | static inline struct audit_context *audit_get_context(struct task_struct *tsk, |
| 707 | int return_valid, |
| 708 | int return_code) |
| 709 | { |
| 710 | struct audit_context *context = tsk->audit_context; |
| 711 | |
| 712 | if (likely(!context)) |
| 713 | return NULL; |
| 714 | context->return_valid = return_valid; |
| 715 | context->return_code = return_code; |
| 716 | |
David Woodhouse | 21af6c4 | 2005-07-02 14:10:46 +0100 | [diff] [blame] | 717 | if (context->in_syscall && !context->auditable) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | enum audit_state state; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 719 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | if (state == AUDIT_RECORD_CONTEXT) |
| 721 | context->auditable = 1; |
| 722 | } |
| 723 | |
| 724 | context->pid = tsk->pid; |
| 725 | context->uid = tsk->uid; |
| 726 | context->gid = tsk->gid; |
| 727 | context->euid = tsk->euid; |
| 728 | context->suid = tsk->suid; |
| 729 | context->fsuid = tsk->fsuid; |
| 730 | context->egid = tsk->egid; |
| 731 | context->sgid = tsk->sgid; |
| 732 | context->fsgid = tsk->fsgid; |
| 733 | context->personality = tsk->personality; |
| 734 | tsk->audit_context = NULL; |
| 735 | return context; |
| 736 | } |
| 737 | |
| 738 | static inline void audit_free_names(struct audit_context *context) |
| 739 | { |
| 740 | int i; |
| 741 | |
| 742 | #if AUDIT_DEBUG == 2 |
| 743 | if (context->auditable |
| 744 | ||context->put_count + context->ino_count != context->name_count) { |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 745 | printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | " name_count=%d put_count=%d" |
| 747 | " ino_count=%d [NOT freeing]\n", |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 748 | __FILE__, __LINE__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | context->serial, context->major, context->in_syscall, |
| 750 | context->name_count, context->put_count, |
| 751 | context->ino_count); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 752 | for (i = 0; i < context->name_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | printk(KERN_ERR "names[%d] = %p = %s\n", i, |
| 754 | context->names[i].name, |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 755 | context->names[i].name ?: "(null)"); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 756 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | dump_stack(); |
| 758 | return; |
| 759 | } |
| 760 | #endif |
| 761 | #if AUDIT_DEBUG |
| 762 | context->put_count = 0; |
| 763 | context->ino_count = 0; |
| 764 | #endif |
| 765 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 766 | for (i = 0; i < context->name_count; i++) { |
| 767 | char *p = context->names[i].ctx; |
| 768 | context->names[i].ctx = NULL; |
| 769 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | if (context->names[i].name) |
| 771 | __putname(context->names[i].name); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | context->name_count = 0; |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 774 | if (context->pwd) |
| 775 | dput(context->pwd); |
| 776 | if (context->pwdmnt) |
| 777 | mntput(context->pwdmnt); |
| 778 | context->pwd = NULL; |
| 779 | context->pwdmnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | static inline void audit_free_aux(struct audit_context *context) |
| 783 | { |
| 784 | struct audit_aux_data *aux; |
| 785 | |
| 786 | while ((aux = context->aux)) { |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 787 | if (aux->type == AUDIT_AVC_PATH) { |
| 788 | struct audit_aux_data_path *axi = (void *)aux; |
| 789 | dput(axi->dentry); |
| 790 | mntput(axi->mnt); |
| 791 | } |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 792 | if ( aux->type == AUDIT_IPC ) { |
| 793 | struct audit_aux_data_ipcctl *axi = (void *)aux; |
| 794 | if (axi->ctx) |
| 795 | kfree(axi->ctx); |
| 796 | } |
| 797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | context->aux = aux->next; |
| 799 | kfree(aux); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | static inline void audit_zero_context(struct audit_context *context, |
| 804 | enum audit_state state) |
| 805 | { |
| 806 | uid_t loginuid = context->loginuid; |
| 807 | |
| 808 | memset(context, 0, sizeof(*context)); |
| 809 | context->state = state; |
| 810 | context->loginuid = loginuid; |
| 811 | } |
| 812 | |
| 813 | static inline struct audit_context *audit_alloc_context(enum audit_state state) |
| 814 | { |
| 815 | struct audit_context *context; |
| 816 | |
| 817 | if (!(context = kmalloc(sizeof(*context), GFP_KERNEL))) |
| 818 | return NULL; |
| 819 | audit_zero_context(context, state); |
| 820 | return context; |
| 821 | } |
| 822 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 823 | /** |
| 824 | * audit_alloc - allocate an audit context block for a task |
| 825 | * @tsk: task |
| 826 | * |
| 827 | * Filter on the task information and allocate a per-task audit context |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | * if necessary. Doing so turns on system call auditing for the |
| 829 | * specified task. This is called from copy_process, so no lock is |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 830 | * needed. |
| 831 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | int audit_alloc(struct task_struct *tsk) |
| 833 | { |
| 834 | struct audit_context *context; |
| 835 | enum audit_state state; |
| 836 | |
| 837 | if (likely(!audit_enabled)) |
| 838 | return 0; /* Return if not auditing. */ |
| 839 | |
| 840 | state = audit_filter_task(tsk); |
| 841 | if (likely(state == AUDIT_DISABLED)) |
| 842 | return 0; |
| 843 | |
| 844 | if (!(context = audit_alloc_context(state))) { |
| 845 | audit_log_lost("out of memory in audit_alloc"); |
| 846 | return -ENOMEM; |
| 847 | } |
| 848 | |
| 849 | /* Preserve login uid */ |
| 850 | context->loginuid = -1; |
| 851 | if (current->audit_context) |
| 852 | context->loginuid = current->audit_context->loginuid; |
| 853 | |
| 854 | tsk->audit_context = context; |
| 855 | set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT); |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | static inline void audit_free_context(struct audit_context *context) |
| 860 | { |
| 861 | struct audit_context *previous; |
| 862 | int count = 0; |
| 863 | |
| 864 | do { |
| 865 | previous = context->previous; |
| 866 | if (previous || (count && count < 10)) { |
| 867 | ++count; |
| 868 | printk(KERN_ERR "audit(:%d): major=%d name_count=%d:" |
| 869 | " freeing multiple contexts (%d)\n", |
| 870 | context->serial, context->major, |
| 871 | context->name_count, count); |
| 872 | } |
| 873 | audit_free_names(context); |
| 874 | audit_free_aux(context); |
| 875 | kfree(context); |
| 876 | context = previous; |
| 877 | } while (context); |
| 878 | if (count >= 10) |
| 879 | printk(KERN_ERR "audit: freed %d contexts\n", count); |
| 880 | } |
| 881 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 882 | static void audit_log_task_context(struct audit_buffer *ab, gfp_t gfp_mask) |
| 883 | { |
| 884 | char *ctx = NULL; |
| 885 | ssize_t len = 0; |
| 886 | |
| 887 | len = security_getprocattr(current, "current", NULL, 0); |
| 888 | if (len < 0) { |
| 889 | if (len != -EINVAL) |
| 890 | goto error_path; |
| 891 | return; |
| 892 | } |
| 893 | |
| 894 | ctx = kmalloc(len, gfp_mask); |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame^] | 895 | if (!ctx) |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 896 | goto error_path; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 897 | |
| 898 | len = security_getprocattr(current, "current", ctx, len); |
| 899 | if (len < 0 ) |
| 900 | goto error_path; |
| 901 | |
| 902 | audit_log_format(ab, " subj=%s", ctx); |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame^] | 903 | return; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 904 | |
| 905 | error_path: |
| 906 | if (ctx) |
| 907 | kfree(ctx); |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame^] | 908 | audit_panic("error in audit_log_task_context"); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 909 | return; |
| 910 | } |
| 911 | |
| 912 | static void audit_log_task_info(struct audit_buffer *ab, gfp_t gfp_mask) |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 913 | { |
| 914 | char name[sizeof(current->comm)]; |
| 915 | struct mm_struct *mm = current->mm; |
| 916 | struct vm_area_struct *vma; |
| 917 | |
| 918 | get_task_comm(name, current); |
David Woodhouse | 99e45ee | 2005-05-23 21:57:41 +0100 | [diff] [blame] | 919 | audit_log_format(ab, " comm="); |
| 920 | audit_log_untrustedstring(ab, name); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 921 | |
| 922 | if (!mm) |
| 923 | return; |
| 924 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 925 | /* |
| 926 | * this is brittle; all callers that pass GFP_ATOMIC will have |
| 927 | * NULL current->mm and we won't get here. |
| 928 | */ |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 929 | down_read(&mm->mmap_sem); |
| 930 | vma = mm->mmap; |
| 931 | while (vma) { |
| 932 | if ((vma->vm_flags & VM_EXECUTABLE) && |
| 933 | vma->vm_file) { |
| 934 | audit_log_d_path(ab, "exe=", |
| 935 | vma->vm_file->f_dentry, |
| 936 | vma->vm_file->f_vfsmnt); |
| 937 | break; |
| 938 | } |
| 939 | vma = vma->vm_next; |
| 940 | } |
| 941 | up_read(&mm->mmap_sem); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 942 | audit_log_task_context(ab, gfp_mask); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Al Viro | 9796fdd | 2005-10-21 03:22:03 -0400 | [diff] [blame] | 945 | static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | { |
| 947 | int i; |
| 948 | struct audit_buffer *ab; |
David Woodhouse | 7551ced | 2005-05-26 12:04:57 +0100 | [diff] [blame] | 949 | struct audit_aux_data *aux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
David Woodhouse | f556196 | 2005-07-13 22:47:07 +0100 | [diff] [blame] | 951 | ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | if (!ab) |
| 953 | return; /* audit_panic has been called */ |
David Woodhouse | bccf6ae | 2005-05-23 21:35:28 +0100 | [diff] [blame] | 954 | audit_log_format(ab, "arch=%x syscall=%d", |
| 955 | context->arch, context->major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | if (context->personality != PER_LINUX) |
| 957 | audit_log_format(ab, " per=%lx", context->personality); |
| 958 | if (context->return_valid) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 959 | audit_log_format(ab, " success=%s exit=%ld", |
| 960 | (context->return_valid==AUDITSC_SUCCESS)?"yes":"no", |
| 961 | context->return_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | audit_log_format(ab, |
| 963 | " a0=%lx a1=%lx a2=%lx a3=%lx items=%d" |
Steve Grubb | 326e9c8 | 2005-05-21 00:22:31 +0100 | [diff] [blame] | 964 | " pid=%d auid=%u uid=%u gid=%u" |
| 965 | " euid=%u suid=%u fsuid=%u" |
| 966 | " egid=%u sgid=%u fsgid=%u", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | context->argv[0], |
| 968 | context->argv[1], |
| 969 | context->argv[2], |
| 970 | context->argv[3], |
| 971 | context->name_count, |
| 972 | context->pid, |
| 973 | context->loginuid, |
| 974 | context->uid, |
| 975 | context->gid, |
| 976 | context->euid, context->suid, context->fsuid, |
| 977 | context->egid, context->sgid, context->fsgid); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 978 | audit_log_task_info(ab, gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | audit_log_end(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
David Woodhouse | 7551ced | 2005-05-26 12:04:57 +0100 | [diff] [blame] | 981 | for (aux = context->aux; aux; aux = aux->next) { |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 982 | |
Al Viro | ef20c8c | 2006-02-18 15:41:50 -0500 | [diff] [blame] | 983 | ab = audit_log_start(context, gfp_mask, aux->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | if (!ab) |
| 985 | continue; /* audit_panic has been called */ |
| 986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | switch (aux->type) { |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 988 | case AUDIT_IPC: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | struct audit_aux_data_ipcctl *axi = (void *)aux; |
| 990 | audit_log_format(ab, |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 991 | " qbytes=%lx iuid=%u igid=%u mode=%x obj=%s", |
| 992 | axi->qbytes, axi->uid, axi->gid, axi->mode, axi->ctx); |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 993 | break; } |
| 994 | |
| 995 | case AUDIT_SOCKETCALL: { |
| 996 | int i; |
| 997 | struct audit_aux_data_socketcall *axs = (void *)aux; |
| 998 | audit_log_format(ab, "nargs=%d", axs->nargs); |
| 999 | for (i=0; i<axs->nargs; i++) |
| 1000 | audit_log_format(ab, " a%d=%lx", i, axs->args[i]); |
| 1001 | break; } |
| 1002 | |
| 1003 | case AUDIT_SOCKADDR: { |
| 1004 | struct audit_aux_data_sockaddr *axs = (void *)aux; |
| 1005 | |
| 1006 | audit_log_format(ab, "saddr="); |
| 1007 | audit_log_hex(ab, axs->a, axs->len); |
| 1008 | break; } |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 1009 | |
| 1010 | case AUDIT_AVC_PATH: { |
| 1011 | struct audit_aux_data_path *axi = (void *)aux; |
| 1012 | audit_log_d_path(ab, "path=", axi->dentry, axi->mnt); |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 1013 | break; } |
| 1014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | } |
| 1016 | audit_log_end(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1019 | if (context->pwd && context->pwdmnt) { |
Al Viro | ef20c8c | 2006-02-18 15:41:50 -0500 | [diff] [blame] | 1020 | ab = audit_log_start(context, gfp_mask, AUDIT_CWD); |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1021 | if (ab) { |
| 1022 | audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt); |
| 1023 | audit_log_end(ab); |
| 1024 | } |
| 1025 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | for (i = 0; i < context->name_count; i++) { |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1027 | unsigned long ino = context->names[i].ino; |
| 1028 | unsigned long pino = context->names[i].pino; |
| 1029 | |
Al Viro | ef20c8c | 2006-02-18 15:41:50 -0500 | [diff] [blame] | 1030 | ab = audit_log_start(context, gfp_mask, AUDIT_PATH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | if (!ab) |
| 1032 | continue; /* audit_panic has been called */ |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | audit_log_format(ab, "item=%d", i); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1035 | |
| 1036 | audit_log_format(ab, " name="); |
| 1037 | if (context->names[i].name) |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 1038 | audit_log_untrustedstring(ab, context->names[i].name); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1039 | else |
| 1040 | audit_log_format(ab, "(null)"); |
| 1041 | |
| 1042 | if (pino != (unsigned long)-1) |
| 1043 | audit_log_format(ab, " parent=%lu", pino); |
| 1044 | if (ino != (unsigned long)-1) |
| 1045 | audit_log_format(ab, " inode=%lu", ino); |
| 1046 | if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1)) |
| 1047 | audit_log_format(ab, " dev=%02x:%02x mode=%#o" |
| 1048 | " ouid=%u ogid=%u rdev=%02x:%02x", |
| 1049 | MAJOR(context->names[i].dev), |
| 1050 | MINOR(context->names[i].dev), |
| 1051 | context->names[i].mode, |
| 1052 | context->names[i].uid, |
| 1053 | context->names[i].gid, |
| 1054 | MAJOR(context->names[i].rdev), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | MINOR(context->names[i].rdev)); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1056 | if (context->names[i].ctx) { |
| 1057 | audit_log_format(ab, " obj=%s", |
| 1058 | context->names[i].ctx); |
| 1059 | } |
| 1060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | audit_log_end(ab); |
| 1062 | } |
| 1063 | } |
| 1064 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1065 | /** |
| 1066 | * audit_free - free a per-task audit context |
| 1067 | * @tsk: task whose audit context block to free |
| 1068 | * |
| 1069 | * Called from copy_process and __put_task_struct. |
| 1070 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | void audit_free(struct task_struct *tsk) |
| 1072 | { |
| 1073 | struct audit_context *context; |
| 1074 | |
| 1075 | task_lock(tsk); |
| 1076 | context = audit_get_context(tsk, 0, 0); |
| 1077 | task_unlock(tsk); |
| 1078 | |
| 1079 | if (likely(!context)) |
| 1080 | return; |
| 1081 | |
| 1082 | /* Check for system calls that do not go through the exit |
David Woodhouse | f556196 | 2005-07-13 22:47:07 +0100 | [diff] [blame] | 1083 | * function (e.g., exit_group), then free context block. |
| 1084 | * We use GFP_ATOMIC here because we might be doing this |
| 1085 | * in the context of the idle thread */ |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 1086 | if (context->in_syscall && context->auditable) |
David Woodhouse | f556196 | 2005-07-13 22:47:07 +0100 | [diff] [blame] | 1087 | audit_log_exit(context, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | |
| 1089 | audit_free_context(context); |
| 1090 | } |
| 1091 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1092 | /** |
| 1093 | * audit_syscall_entry - fill in an audit record at syscall entry |
| 1094 | * @tsk: task being audited |
| 1095 | * @arch: architecture type |
| 1096 | * @major: major syscall type (function) |
| 1097 | * @a1: additional syscall register 1 |
| 1098 | * @a2: additional syscall register 2 |
| 1099 | * @a3: additional syscall register 3 |
| 1100 | * @a4: additional syscall register 4 |
| 1101 | * |
| 1102 | * Fill in audit context at syscall entry. This only happens if the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | * audit context was created when the task was created and the state or |
| 1104 | * filters demand the audit context be built. If the state from the |
| 1105 | * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT, |
| 1106 | * then the record will be written at syscall exit time (otherwise, it |
| 1107 | * will only be written if another part of the kernel requests that it |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1108 | * be written). |
| 1109 | */ |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1110 | void audit_syscall_entry(struct task_struct *tsk, int arch, int major, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | unsigned long a1, unsigned long a2, |
| 1112 | unsigned long a3, unsigned long a4) |
| 1113 | { |
| 1114 | struct audit_context *context = tsk->audit_context; |
| 1115 | enum audit_state state; |
| 1116 | |
| 1117 | BUG_ON(!context); |
| 1118 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1119 | /* |
| 1120 | * This happens only on certain architectures that make system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | * calls in kernel_thread via the entry.S interface, instead of |
| 1122 | * with direct calls. (If you are porting to a new |
| 1123 | * architecture, hitting this condition can indicate that you |
| 1124 | * got the _exit/_leave calls backward in entry.S.) |
| 1125 | * |
| 1126 | * i386 no |
| 1127 | * x86_64 no |
| 1128 | * ppc64 yes (see arch/ppc64/kernel/misc.S) |
| 1129 | * |
| 1130 | * This also happens with vm86 emulation in a non-nested manner |
| 1131 | * (entries without exits), so this case must be caught. |
| 1132 | */ |
| 1133 | if (context->in_syscall) { |
| 1134 | struct audit_context *newctx; |
| 1135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | #if AUDIT_DEBUG |
| 1137 | printk(KERN_ERR |
| 1138 | "audit(:%d) pid=%d in syscall=%d;" |
| 1139 | " entering syscall=%d\n", |
| 1140 | context->serial, tsk->pid, context->major, major); |
| 1141 | #endif |
| 1142 | newctx = audit_alloc_context(context->state); |
| 1143 | if (newctx) { |
| 1144 | newctx->previous = context; |
| 1145 | context = newctx; |
| 1146 | tsk->audit_context = newctx; |
| 1147 | } else { |
| 1148 | /* If we can't alloc a new context, the best we |
| 1149 | * can do is to leak memory (any pending putname |
| 1150 | * will be lost). The only other alternative is |
| 1151 | * to abandon auditing. */ |
| 1152 | audit_zero_context(context, context->state); |
| 1153 | } |
| 1154 | } |
| 1155 | BUG_ON(context->in_syscall || context->name_count); |
| 1156 | |
| 1157 | if (!audit_enabled) |
| 1158 | return; |
| 1159 | |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1160 | context->arch = arch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | context->major = major; |
| 1162 | context->argv[0] = a1; |
| 1163 | context->argv[1] = a2; |
| 1164 | context->argv[2] = a3; |
| 1165 | context->argv[3] = a4; |
| 1166 | |
| 1167 | state = context->state; |
| 1168 | if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT) |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 1169 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | if (likely(state == AUDIT_DISABLED)) |
| 1171 | return; |
| 1172 | |
David Woodhouse | ce625a8 | 2005-07-18 14:24:46 -0400 | [diff] [blame] | 1173 | context->serial = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | context->ctime = CURRENT_TIME; |
| 1175 | context->in_syscall = 1; |
| 1176 | context->auditable = !!(state == AUDIT_RECORD_CONTEXT); |
| 1177 | } |
| 1178 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1179 | /** |
| 1180 | * audit_syscall_exit - deallocate audit context after a system call |
| 1181 | * @tsk: task being audited |
| 1182 | * @valid: success/failure flag |
| 1183 | * @return_code: syscall return value |
| 1184 | * |
| 1185 | * Tear down after system call. If the audit context has been marked as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | * auditable (either because of the AUDIT_RECORD_CONTEXT state from |
| 1187 | * filtering, or because some other part of the kernel write an audit |
| 1188 | * message), then write out the syscall information. In call cases, |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1189 | * free the names stored from getname(). |
| 1190 | */ |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1191 | 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] | 1192 | { |
| 1193 | struct audit_context *context; |
| 1194 | |
| 1195 | get_task_struct(tsk); |
| 1196 | task_lock(tsk); |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1197 | context = audit_get_context(tsk, valid, return_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | task_unlock(tsk); |
| 1199 | |
| 1200 | /* Not having a context here is ok, since the parent may have |
| 1201 | * called __put_task_struct. */ |
| 1202 | if (likely(!context)) |
David Woodhouse | 413a1c7 | 2005-08-17 14:45:55 +0100 | [diff] [blame] | 1203 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 1205 | if (context->in_syscall && context->auditable) |
David Woodhouse | f556196 | 2005-07-13 22:47:07 +0100 | [diff] [blame] | 1206 | audit_log_exit(context, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | |
| 1208 | context->in_syscall = 0; |
| 1209 | context->auditable = 0; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | if (context->previous) { |
| 1212 | struct audit_context *new_context = context->previous; |
| 1213 | context->previous = NULL; |
| 1214 | audit_free_context(context); |
| 1215 | tsk->audit_context = new_context; |
| 1216 | } else { |
| 1217 | audit_free_names(context); |
| 1218 | audit_free_aux(context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | tsk->audit_context = context; |
| 1220 | } |
David Woodhouse | 413a1c7 | 2005-08-17 14:45:55 +0100 | [diff] [blame] | 1221 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | put_task_struct(tsk); |
| 1223 | } |
| 1224 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1225 | /** |
| 1226 | * audit_getname - add a name to the list |
| 1227 | * @name: name to add |
| 1228 | * |
| 1229 | * Add a name to the list of audit names for this context. |
| 1230 | * Called from fs/namei.c:getname(). |
| 1231 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | void audit_getname(const char *name) |
| 1233 | { |
| 1234 | struct audit_context *context = current->audit_context; |
| 1235 | |
| 1236 | if (!context || IS_ERR(name) || !name) |
| 1237 | return; |
| 1238 | |
| 1239 | if (!context->in_syscall) { |
| 1240 | #if AUDIT_DEBUG == 2 |
| 1241 | printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n", |
| 1242 | __FILE__, __LINE__, context->serial, name); |
| 1243 | dump_stack(); |
| 1244 | #endif |
| 1245 | return; |
| 1246 | } |
| 1247 | BUG_ON(context->name_count >= AUDIT_NAMES); |
| 1248 | context->names[context->name_count].name = name; |
| 1249 | context->names[context->name_count].ino = (unsigned long)-1; |
| 1250 | ++context->name_count; |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1251 | if (!context->pwd) { |
| 1252 | read_lock(¤t->fs->lock); |
| 1253 | context->pwd = dget(current->fs->pwd); |
| 1254 | context->pwdmnt = mntget(current->fs->pwdmnt); |
| 1255 | read_unlock(¤t->fs->lock); |
| 1256 | } |
| 1257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1260 | /* audit_putname - intercept a putname request |
| 1261 | * @name: name to intercept and delay for putname |
| 1262 | * |
| 1263 | * If we have stored the name from getname in the audit context, |
| 1264 | * then we delay the putname until syscall exit. |
| 1265 | * Called from include/linux/fs.h:putname(). |
| 1266 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | void audit_putname(const char *name) |
| 1268 | { |
| 1269 | struct audit_context *context = current->audit_context; |
| 1270 | |
| 1271 | BUG_ON(!context); |
| 1272 | if (!context->in_syscall) { |
| 1273 | #if AUDIT_DEBUG == 2 |
| 1274 | printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n", |
| 1275 | __FILE__, __LINE__, context->serial, name); |
| 1276 | if (context->name_count) { |
| 1277 | int i; |
| 1278 | for (i = 0; i < context->name_count; i++) |
| 1279 | printk(KERN_ERR "name[%d] = %p = %s\n", i, |
| 1280 | context->names[i].name, |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1281 | context->names[i].name ?: "(null)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | } |
| 1283 | #endif |
| 1284 | __putname(name); |
| 1285 | } |
| 1286 | #if AUDIT_DEBUG |
| 1287 | else { |
| 1288 | ++context->put_count; |
| 1289 | if (context->put_count > context->name_count) { |
| 1290 | printk(KERN_ERR "%s:%d(:%d): major=%d" |
| 1291 | " in_syscall=%d putname(%p) name_count=%d" |
| 1292 | " put_count=%d\n", |
| 1293 | __FILE__, __LINE__, |
| 1294 | context->serial, context->major, |
| 1295 | context->in_syscall, name, context->name_count, |
| 1296 | context->put_count); |
| 1297 | dump_stack(); |
| 1298 | } |
| 1299 | } |
| 1300 | #endif |
| 1301 | } |
| 1302 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1303 | void audit_inode_context(int idx, const struct inode *inode) |
| 1304 | { |
| 1305 | struct audit_context *context = current->audit_context; |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame^] | 1306 | const char *suffix = security_inode_xattr_getsuffix(); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1307 | char *ctx = NULL; |
| 1308 | int len = 0; |
| 1309 | |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame^] | 1310 | if (!suffix) |
| 1311 | goto ret; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1312 | |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame^] | 1313 | len = security_inode_getsecurity(inode, suffix, NULL, 0, 0); |
| 1314 | if (len == -EOPNOTSUPP) |
| 1315 | goto ret; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1316 | if (len < 0) |
| 1317 | goto error_path; |
| 1318 | |
| 1319 | ctx = kmalloc(len, GFP_KERNEL); |
| 1320 | if (!ctx) |
| 1321 | goto error_path; |
| 1322 | |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame^] | 1323 | len = security_inode_getsecurity(inode, suffix, ctx, len, 0); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1324 | if (len < 0) |
| 1325 | goto error_path; |
| 1326 | |
| 1327 | kfree(context->names[idx].ctx); |
| 1328 | context->names[idx].ctx = ctx; |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame^] | 1329 | goto ret; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1330 | |
| 1331 | error_path: |
| 1332 | if (ctx) |
| 1333 | kfree(ctx); |
| 1334 | audit_panic("error in audit_inode_context"); |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame^] | 1335 | ret: |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1336 | return; |
| 1337 | } |
| 1338 | |
| 1339 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1340 | /** |
| 1341 | * audit_inode - store the inode and device from a lookup |
| 1342 | * @name: name being audited |
| 1343 | * @inode: inode being audited |
| 1344 | * @flags: lookup flags (as used in path_lookup()) |
| 1345 | * |
| 1346 | * Called from fs/namei.c:path_lookup(). |
| 1347 | */ |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1348 | void __audit_inode(const char *name, const struct inode *inode, unsigned flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | { |
| 1350 | int idx; |
| 1351 | struct audit_context *context = current->audit_context; |
| 1352 | |
| 1353 | if (!context->in_syscall) |
| 1354 | return; |
| 1355 | if (context->name_count |
| 1356 | && context->names[context->name_count-1].name |
| 1357 | && context->names[context->name_count-1].name == name) |
| 1358 | idx = context->name_count - 1; |
| 1359 | else if (context->name_count > 1 |
| 1360 | && context->names[context->name_count-2].name |
| 1361 | && context->names[context->name_count-2].name == name) |
| 1362 | idx = context->name_count - 2; |
| 1363 | else { |
| 1364 | /* FIXME: how much do we care about inodes that have no |
| 1365 | * associated name? */ |
| 1366 | if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED) |
| 1367 | return; |
| 1368 | idx = context->name_count++; |
| 1369 | context->names[idx].name = NULL; |
| 1370 | #if AUDIT_DEBUG |
| 1371 | ++context->ino_count; |
| 1372 | #endif |
| 1373 | } |
David Woodhouse | ae7b961 | 2005-06-20 16:11:05 +0100 | [diff] [blame] | 1374 | context->names[idx].dev = inode->i_sb->s_dev; |
| 1375 | context->names[idx].mode = inode->i_mode; |
| 1376 | context->names[idx].uid = inode->i_uid; |
| 1377 | context->names[idx].gid = inode->i_gid; |
| 1378 | context->names[idx].rdev = inode->i_rdev; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1379 | audit_inode_context(idx, inode); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1380 | if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) && |
| 1381 | (strcmp(name, ".") != 0)) { |
| 1382 | context->names[idx].ino = (unsigned long)-1; |
| 1383 | context->names[idx].pino = inode->i_ino; |
| 1384 | } else { |
| 1385 | context->names[idx].ino = inode->i_ino; |
| 1386 | context->names[idx].pino = (unsigned long)-1; |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | /** |
| 1391 | * audit_inode_child - collect inode info for created/removed objects |
| 1392 | * @dname: inode's dentry name |
| 1393 | * @inode: inode being audited |
| 1394 | * @pino: inode number of dentry parent |
| 1395 | * |
| 1396 | * For syscalls that create or remove filesystem objects, audit_inode |
| 1397 | * can only collect information for the filesystem object's parent. |
| 1398 | * This call updates the audit context with the child's information. |
| 1399 | * Syscalls that create a new filesystem object must be hooked after |
| 1400 | * the object is created. Syscalls that remove a filesystem object |
| 1401 | * must be hooked prior, in order to capture the target inode during |
| 1402 | * unsuccessful attempts. |
| 1403 | */ |
| 1404 | void __audit_inode_child(const char *dname, const struct inode *inode, |
| 1405 | unsigned long pino) |
| 1406 | { |
| 1407 | int idx; |
| 1408 | struct audit_context *context = current->audit_context; |
| 1409 | |
| 1410 | if (!context->in_syscall) |
| 1411 | return; |
| 1412 | |
| 1413 | /* determine matching parent */ |
| 1414 | if (dname) |
| 1415 | for (idx = 0; idx < context->name_count; idx++) |
| 1416 | if (context->names[idx].pino == pino) { |
| 1417 | const char *n; |
| 1418 | const char *name = context->names[idx].name; |
| 1419 | int dlen = strlen(dname); |
| 1420 | int nlen = name ? strlen(name) : 0; |
| 1421 | |
| 1422 | if (nlen < dlen) |
| 1423 | continue; |
| 1424 | |
| 1425 | /* disregard trailing slashes */ |
| 1426 | n = name + nlen - 1; |
| 1427 | while ((*n == '/') && (n > name)) |
| 1428 | n--; |
| 1429 | |
| 1430 | /* find last path component */ |
| 1431 | n = n - dlen + 1; |
| 1432 | if (n < name) |
| 1433 | continue; |
| 1434 | else if (n > name) { |
| 1435 | if (*--n != '/') |
| 1436 | continue; |
| 1437 | else |
| 1438 | n++; |
| 1439 | } |
| 1440 | |
| 1441 | if (strncmp(n, dname, dlen) == 0) |
| 1442 | goto update_context; |
| 1443 | } |
| 1444 | |
| 1445 | /* catch-all in case match not found */ |
| 1446 | idx = context->name_count++; |
| 1447 | context->names[idx].name = NULL; |
| 1448 | context->names[idx].pino = pino; |
| 1449 | #if AUDIT_DEBUG |
| 1450 | context->ino_count++; |
| 1451 | #endif |
| 1452 | |
| 1453 | update_context: |
| 1454 | if (inode) { |
| 1455 | context->names[idx].ino = inode->i_ino; |
| 1456 | context->names[idx].dev = inode->i_sb->s_dev; |
| 1457 | context->names[idx].mode = inode->i_mode; |
| 1458 | context->names[idx].uid = inode->i_uid; |
| 1459 | context->names[idx].gid = inode->i_gid; |
| 1460 | context->names[idx].rdev = inode->i_rdev; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1461 | audit_inode_context(idx, inode); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1465 | /** |
| 1466 | * auditsc_get_stamp - get local copies of audit_context values |
| 1467 | * @ctx: audit_context for the task |
| 1468 | * @t: timespec to store time recorded in the audit_context |
| 1469 | * @serial: serial value that is recorded in the audit_context |
| 1470 | * |
| 1471 | * Also sets the context as auditable. |
| 1472 | */ |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1473 | void auditsc_get_stamp(struct audit_context *ctx, |
| 1474 | struct timespec *t, unsigned int *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | { |
David Woodhouse | ce625a8 | 2005-07-18 14:24:46 -0400 | [diff] [blame] | 1476 | if (!ctx->serial) |
| 1477 | ctx->serial = audit_serial(); |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1478 | t->tv_sec = ctx->ctime.tv_sec; |
| 1479 | t->tv_nsec = ctx->ctime.tv_nsec; |
| 1480 | *serial = ctx->serial; |
| 1481 | ctx->auditable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1484 | /** |
| 1485 | * audit_set_loginuid - set a task's audit_context loginuid |
| 1486 | * @task: task whose audit context is being modified |
| 1487 | * @loginuid: loginuid value |
| 1488 | * |
| 1489 | * Returns 0. |
| 1490 | * |
| 1491 | * Called (set) from fs/proc/base.c::proc_loginuid_write(). |
| 1492 | */ |
Steve Grubb | 456be6c | 2005-04-29 17:30:07 +0100 | [diff] [blame] | 1493 | int audit_set_loginuid(struct task_struct *task, uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | { |
Steve Grubb | 456be6c | 2005-04-29 17:30:07 +0100 | [diff] [blame] | 1495 | if (task->audit_context) { |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1496 | struct audit_buffer *ab; |
| 1497 | |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1498 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN); |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1499 | if (ab) { |
| 1500 | audit_log_format(ab, "login pid=%d uid=%u " |
Steve Grubb | 326e9c8 | 2005-05-21 00:22:31 +0100 | [diff] [blame] | 1501 | "old auid=%u new auid=%u", |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1502 | task->pid, task->uid, |
| 1503 | task->audit_context->loginuid, loginuid); |
| 1504 | audit_log_end(ab); |
| 1505 | } |
Steve Grubb | 456be6c | 2005-04-29 17:30:07 +0100 | [diff] [blame] | 1506 | task->audit_context->loginuid = loginuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | } |
| 1508 | return 0; |
| 1509 | } |
| 1510 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1511 | /** |
| 1512 | * audit_get_loginuid - get the loginuid for an audit_context |
| 1513 | * @ctx: the audit_context |
| 1514 | * |
| 1515 | * Returns the context's loginuid or -1 if @ctx is NULL. |
| 1516 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | uid_t audit_get_loginuid(struct audit_context *ctx) |
| 1518 | { |
| 1519 | return ctx ? ctx->loginuid : -1; |
| 1520 | } |
| 1521 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1522 | static char *audit_ipc_context(struct kern_ipc_perm *ipcp) |
| 1523 | { |
| 1524 | struct audit_context *context = current->audit_context; |
| 1525 | char *ctx = NULL; |
| 1526 | int len = 0; |
| 1527 | |
| 1528 | if (likely(!context)) |
| 1529 | return NULL; |
| 1530 | |
| 1531 | len = security_ipc_getsecurity(ipcp, NULL, 0); |
| 1532 | if (len == -EOPNOTSUPP) |
| 1533 | goto ret; |
| 1534 | if (len < 0) |
| 1535 | goto error_path; |
| 1536 | |
| 1537 | ctx = kmalloc(len, GFP_ATOMIC); |
| 1538 | if (!ctx) |
| 1539 | goto error_path; |
| 1540 | |
| 1541 | len = security_ipc_getsecurity(ipcp, ctx, len); |
| 1542 | if (len < 0) |
| 1543 | goto error_path; |
| 1544 | |
| 1545 | return ctx; |
| 1546 | |
| 1547 | error_path: |
| 1548 | kfree(ctx); |
| 1549 | audit_panic("error in audit_ipc_context"); |
| 1550 | ret: |
| 1551 | return NULL; |
| 1552 | } |
| 1553 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1554 | /** |
| 1555 | * audit_ipc_perms - record audit data for ipc |
| 1556 | * @qbytes: msgq bytes |
| 1557 | * @uid: msgq user id |
| 1558 | * @gid: msgq group id |
| 1559 | * @mode: msgq mode (permissions) |
| 1560 | * |
| 1561 | * Returns 0 for success or NULL context or < 0 on error. |
| 1562 | */ |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1563 | int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode, struct kern_ipc_perm *ipcp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | { |
| 1565 | struct audit_aux_data_ipcctl *ax; |
| 1566 | struct audit_context *context = current->audit_context; |
| 1567 | |
| 1568 | if (likely(!context)) |
| 1569 | return 0; |
| 1570 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1571 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | if (!ax) |
| 1573 | return -ENOMEM; |
| 1574 | |
| 1575 | ax->qbytes = qbytes; |
| 1576 | ax->uid = uid; |
| 1577 | ax->gid = gid; |
| 1578 | ax->mode = mode; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1579 | ax->ctx = audit_ipc_context(ipcp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1581 | ax->d.type = AUDIT_IPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | ax->d.next = context->aux; |
| 1583 | context->aux = (void *)ax; |
| 1584 | return 0; |
| 1585 | } |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1586 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1587 | /** |
| 1588 | * audit_socketcall - record audit data for sys_socketcall |
| 1589 | * @nargs: number of args |
| 1590 | * @args: args array |
| 1591 | * |
| 1592 | * Returns 0 for success or NULL context or < 0 on error. |
| 1593 | */ |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1594 | int audit_socketcall(int nargs, unsigned long *args) |
| 1595 | { |
| 1596 | struct audit_aux_data_socketcall *ax; |
| 1597 | struct audit_context *context = current->audit_context; |
| 1598 | |
| 1599 | if (likely(!context)) |
| 1600 | return 0; |
| 1601 | |
| 1602 | ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL); |
| 1603 | if (!ax) |
| 1604 | return -ENOMEM; |
| 1605 | |
| 1606 | ax->nargs = nargs; |
| 1607 | memcpy(ax->args, args, nargs * sizeof(unsigned long)); |
| 1608 | |
| 1609 | ax->d.type = AUDIT_SOCKETCALL; |
| 1610 | ax->d.next = context->aux; |
| 1611 | context->aux = (void *)ax; |
| 1612 | return 0; |
| 1613 | } |
| 1614 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1615 | /** |
| 1616 | * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto |
| 1617 | * @len: data length in user space |
| 1618 | * @a: data address in kernel space |
| 1619 | * |
| 1620 | * Returns 0 for success or NULL context or < 0 on error. |
| 1621 | */ |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1622 | int audit_sockaddr(int len, void *a) |
| 1623 | { |
| 1624 | struct audit_aux_data_sockaddr *ax; |
| 1625 | struct audit_context *context = current->audit_context; |
| 1626 | |
| 1627 | if (likely(!context)) |
| 1628 | return 0; |
| 1629 | |
| 1630 | ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL); |
| 1631 | if (!ax) |
| 1632 | return -ENOMEM; |
| 1633 | |
| 1634 | ax->len = len; |
| 1635 | memcpy(ax->a, a, len); |
| 1636 | |
| 1637 | ax->d.type = AUDIT_SOCKADDR; |
| 1638 | ax->d.next = context->aux; |
| 1639 | context->aux = (void *)ax; |
| 1640 | return 0; |
| 1641 | } |
| 1642 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1643 | /** |
| 1644 | * audit_avc_path - record the granting or denial of permissions |
| 1645 | * @dentry: dentry to record |
| 1646 | * @mnt: mnt to record |
| 1647 | * |
| 1648 | * Returns 0 for success or NULL context or < 0 on error. |
| 1649 | * |
| 1650 | * Called from security/selinux/avc.c::avc_audit() |
| 1651 | */ |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 1652 | int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt) |
| 1653 | { |
| 1654 | struct audit_aux_data_path *ax; |
| 1655 | struct audit_context *context = current->audit_context; |
| 1656 | |
| 1657 | if (likely(!context)) |
| 1658 | return 0; |
| 1659 | |
| 1660 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 1661 | if (!ax) |
| 1662 | return -ENOMEM; |
| 1663 | |
| 1664 | ax->dentry = dget(dentry); |
| 1665 | ax->mnt = mntget(mnt); |
| 1666 | |
| 1667 | ax->d.type = AUDIT_AVC_PATH; |
| 1668 | ax->d.next = context->aux; |
| 1669 | context->aux = (void *)ax; |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1673 | /** |
| 1674 | * audit_signal_info - record signal info for shutting down audit subsystem |
| 1675 | * @sig: signal value |
| 1676 | * @t: task being signaled |
| 1677 | * |
| 1678 | * If the audit subsystem is being terminated, record the task (pid) |
| 1679 | * and uid that is doing that. |
| 1680 | */ |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1681 | void audit_signal_info(int sig, struct task_struct *t) |
| 1682 | { |
| 1683 | extern pid_t audit_sig_pid; |
| 1684 | extern uid_t audit_sig_uid; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1685 | |
David Woodhouse | 582edda | 2005-07-13 22:39:34 +0100 | [diff] [blame] | 1686 | if (unlikely(audit_pid && t->tgid == audit_pid)) { |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1687 | if (sig == SIGTERM || sig == SIGHUP) { |
| 1688 | struct audit_context *ctx = current->audit_context; |
| 1689 | audit_sig_pid = current->pid; |
| 1690 | if (ctx) |
| 1691 | audit_sig_uid = ctx->loginuid; |
| 1692 | else |
| 1693 | audit_sig_uid = current->uid; |
| 1694 | } |
| 1695 | } |
| 1696 | } |