| 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. |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 6 | * Copyright (C) 2005, 2006 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 | * |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 32 | * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>, |
| 33 | * 2006. |
| 34 | * |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 35 | * The support of additional filter rules compares (>, <, >=, <=) was |
| 36 | * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005. |
| 37 | * |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 38 | * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional |
| 39 | * filesystem information. |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 40 | * |
| 41 | * Subject and object context labeling support added by <danjones@us.ibm.com> |
| 42 | * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | */ |
| 44 | |
| 45 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <asm/types.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 47 | #include <linux/atomic.h> |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 48 | #include <linux/fs.h> |
| 49 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/mm.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 51 | #include <linux/export.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 52 | #include <linux/slab.h> |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 53 | #include <linux/mount.h> |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 54 | #include <linux/socket.h> |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 55 | #include <linux/mqueue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <linux/audit.h> |
| 57 | #include <linux/personality.h> |
| 58 | #include <linux/time.h> |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame] | 59 | #include <linux/netlink.h> |
David Woodhouse | f556196 | 2005-07-13 22:47:07 +0100 | [diff] [blame] | 60 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <asm/unistd.h> |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 62 | #include <linux/security.h> |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 63 | #include <linux/list.h> |
Steve Grubb | a6c043a | 2006-01-01 14:07:00 -0500 | [diff] [blame] | 64 | #include <linux/tty.h> |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 65 | #include <linux/binfmts.h> |
Al Viro | a1f8e7f7 | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 66 | #include <linux/highmem.h> |
Al Viro | f46038f | 2006-05-06 08:22:52 -0400 | [diff] [blame] | 67 | #include <linux/syscalls.h> |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 68 | #include <linux/capability.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 69 | #include <linux/fs_struct.h> |
Kees Cook | 3dc1c1b | 2012-04-12 16:47:58 -0500 | [diff] [blame] | 70 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 72 | #include "audit.h" |
| 73 | |
Eric Paris | d7e7528 | 2012-01-03 14:23:06 -0500 | [diff] [blame] | 74 | /* flags stating the success for a syscall */ |
| 75 | #define AUDITSC_INVALID 0 |
| 76 | #define AUDITSC_SUCCESS 1 |
| 77 | #define AUDITSC_FAILURE 2 |
| 78 | |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 79 | /* no execve audit message should be longer than this (userspace limits) */ |
| 80 | #define MAX_EXECVE_AUDIT_LEN 7500 |
| 81 | |
Al Viro | 471a5c7 | 2006-07-10 08:29:24 -0400 | [diff] [blame] | 82 | /* number of audit rules */ |
| 83 | int audit_n_rules; |
| 84 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 85 | /* determines whether we collect data for signals sent */ |
| 86 | int audit_signals; |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | struct audit_aux_data { |
| 89 | struct audit_aux_data *next; |
| 90 | int type; |
| 91 | }; |
| 92 | |
| 93 | #define AUDIT_AUX_IPCPERM 0 |
| 94 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 95 | /* Number of target pids per aux struct. */ |
| 96 | #define AUDIT_AUX_PIDS 16 |
| 97 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 98 | struct audit_aux_data_pids { |
| 99 | struct audit_aux_data d; |
| 100 | pid_t target_pid[AUDIT_AUX_PIDS]; |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 101 | kuid_t target_auid[AUDIT_AUX_PIDS]; |
Eric W. Biederman | cca080d | 2012-02-07 16:53:48 -0800 | [diff] [blame] | 102 | kuid_t target_uid[AUDIT_AUX_PIDS]; |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 103 | unsigned int target_sessionid[AUDIT_AUX_PIDS]; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 104 | u32 target_sid[AUDIT_AUX_PIDS]; |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 105 | char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN]; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 106 | int pid_count; |
| 107 | }; |
| 108 | |
Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame] | 109 | struct audit_aux_data_bprm_fcaps { |
| 110 | struct audit_aux_data d; |
| 111 | struct audit_cap_data fcap; |
| 112 | unsigned int fcap_ver; |
| 113 | struct audit_cap_data old_pcap; |
| 114 | struct audit_cap_data new_pcap; |
| 115 | }; |
| 116 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 117 | struct audit_tree_refs { |
| 118 | struct audit_tree_refs *next; |
| 119 | struct audit_chunk *c[31]; |
| 120 | }; |
| 121 | |
Al Viro | 55669bf | 2006-08-31 19:26:40 -0400 | [diff] [blame] | 122 | static inline int open_arg(int flags, int mask) |
| 123 | { |
| 124 | int n = ACC_MODE(flags); |
| 125 | if (flags & (O_TRUNC | O_CREAT)) |
| 126 | n |= AUDIT_PERM_WRITE; |
| 127 | return n & mask; |
| 128 | } |
| 129 | |
| 130 | static int audit_match_perm(struct audit_context *ctx, int mask) |
| 131 | { |
Cordelia | c4bacef | 2008-08-18 09:45:51 -0700 | [diff] [blame] | 132 | unsigned n; |
zhangxiliang | 1a61c88 | 2008-08-02 10:56:37 +0800 | [diff] [blame] | 133 | if (unlikely(!ctx)) |
| 134 | return 0; |
Cordelia | c4bacef | 2008-08-18 09:45:51 -0700 | [diff] [blame] | 135 | n = ctx->major; |
Alan Cox | dbda4c0 | 2008-10-13 10:40:53 +0100 | [diff] [blame] | 136 | |
Al Viro | 55669bf | 2006-08-31 19:26:40 -0400 | [diff] [blame] | 137 | switch (audit_classify_syscall(ctx->arch, n)) { |
| 138 | case 0: /* native */ |
| 139 | if ((mask & AUDIT_PERM_WRITE) && |
| 140 | audit_match_class(AUDIT_CLASS_WRITE, n)) |
| 141 | return 1; |
| 142 | if ((mask & AUDIT_PERM_READ) && |
| 143 | audit_match_class(AUDIT_CLASS_READ, n)) |
| 144 | return 1; |
| 145 | if ((mask & AUDIT_PERM_ATTR) && |
| 146 | audit_match_class(AUDIT_CLASS_CHATTR, n)) |
| 147 | return 1; |
| 148 | return 0; |
| 149 | case 1: /* 32bit on biarch */ |
| 150 | if ((mask & AUDIT_PERM_WRITE) && |
| 151 | audit_match_class(AUDIT_CLASS_WRITE_32, n)) |
| 152 | return 1; |
| 153 | if ((mask & AUDIT_PERM_READ) && |
| 154 | audit_match_class(AUDIT_CLASS_READ_32, n)) |
| 155 | return 1; |
| 156 | if ((mask & AUDIT_PERM_ATTR) && |
| 157 | audit_match_class(AUDIT_CLASS_CHATTR_32, n)) |
| 158 | return 1; |
| 159 | return 0; |
| 160 | case 2: /* open */ |
| 161 | return mask & ACC_MODE(ctx->argv[1]); |
| 162 | case 3: /* openat */ |
| 163 | return mask & ACC_MODE(ctx->argv[2]); |
| 164 | case 4: /* socketcall */ |
| 165 | return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND); |
| 166 | case 5: /* execve */ |
| 167 | return mask & AUDIT_PERM_EXEC; |
| 168 | default: |
| 169 | return 0; |
| 170 | } |
| 171 | } |
| 172 | |
Eric Paris | 5ef30ee | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 173 | static int audit_match_filetype(struct audit_context *ctx, int val) |
Al Viro | 8b67dca | 2008-04-28 04:15:49 -0400 | [diff] [blame] | 174 | { |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 175 | struct audit_names *n; |
Eric Paris | 5ef30ee | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 176 | umode_t mode = (umode_t)val; |
zhangxiliang | 1a61c88 | 2008-08-02 10:56:37 +0800 | [diff] [blame] | 177 | |
| 178 | if (unlikely(!ctx)) |
| 179 | return 0; |
| 180 | |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 181 | list_for_each_entry(n, &ctx->names_list, list) { |
| 182 | if ((n->ino != -1) && |
| 183 | ((n->mode & S_IFMT) == mode)) |
Eric Paris | 5ef30ee | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 184 | return 1; |
| 185 | } |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 186 | |
Eric Paris | 5ef30ee | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 187 | return 0; |
Al Viro | 8b67dca | 2008-04-28 04:15:49 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 190 | /* |
| 191 | * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *; |
| 192 | * ->first_trees points to its beginning, ->trees - to the current end of data. |
| 193 | * ->tree_count is the number of free entries in array pointed to by ->trees. |
| 194 | * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL, |
| 195 | * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously, |
| 196 | * it's going to remain 1-element for almost any setup) until we free context itself. |
| 197 | * References in it _are_ dropped - at the same time we free/drop aux stuff. |
| 198 | */ |
| 199 | |
| 200 | #ifdef CONFIG_AUDIT_TREE |
Eric Paris | 679173b | 2009-01-26 18:09:45 -0500 | [diff] [blame] | 201 | static void audit_set_auditable(struct audit_context *ctx) |
| 202 | { |
| 203 | if (!ctx->prio) { |
| 204 | ctx->prio = 1; |
| 205 | ctx->current_state = AUDIT_RECORD_CONTEXT; |
| 206 | } |
| 207 | } |
| 208 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 209 | static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk) |
| 210 | { |
| 211 | struct audit_tree_refs *p = ctx->trees; |
| 212 | int left = ctx->tree_count; |
| 213 | if (likely(left)) { |
| 214 | p->c[--left] = chunk; |
| 215 | ctx->tree_count = left; |
| 216 | return 1; |
| 217 | } |
| 218 | if (!p) |
| 219 | return 0; |
| 220 | p = p->next; |
| 221 | if (p) { |
| 222 | p->c[30] = chunk; |
| 223 | ctx->trees = p; |
| 224 | ctx->tree_count = 30; |
| 225 | return 1; |
| 226 | } |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int grow_tree_refs(struct audit_context *ctx) |
| 231 | { |
| 232 | struct audit_tree_refs *p = ctx->trees; |
| 233 | ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL); |
| 234 | if (!ctx->trees) { |
| 235 | ctx->trees = p; |
| 236 | return 0; |
| 237 | } |
| 238 | if (p) |
| 239 | p->next = ctx->trees; |
| 240 | else |
| 241 | ctx->first_trees = ctx->trees; |
| 242 | ctx->tree_count = 31; |
| 243 | return 1; |
| 244 | } |
| 245 | #endif |
| 246 | |
| 247 | static void unroll_tree_refs(struct audit_context *ctx, |
| 248 | struct audit_tree_refs *p, int count) |
| 249 | { |
| 250 | #ifdef CONFIG_AUDIT_TREE |
| 251 | struct audit_tree_refs *q; |
| 252 | int n; |
| 253 | if (!p) { |
| 254 | /* we started with empty chain */ |
| 255 | p = ctx->first_trees; |
| 256 | count = 31; |
| 257 | /* if the very first allocation has failed, nothing to do */ |
| 258 | if (!p) |
| 259 | return; |
| 260 | } |
| 261 | n = count; |
| 262 | for (q = p; q != ctx->trees; q = q->next, n = 31) { |
| 263 | while (n--) { |
| 264 | audit_put_chunk(q->c[n]); |
| 265 | q->c[n] = NULL; |
| 266 | } |
| 267 | } |
| 268 | while (n-- > ctx->tree_count) { |
| 269 | audit_put_chunk(q->c[n]); |
| 270 | q->c[n] = NULL; |
| 271 | } |
| 272 | ctx->trees = p; |
| 273 | ctx->tree_count = count; |
| 274 | #endif |
| 275 | } |
| 276 | |
| 277 | static void free_tree_refs(struct audit_context *ctx) |
| 278 | { |
| 279 | struct audit_tree_refs *p, *q; |
| 280 | for (p = ctx->first_trees; p; p = q) { |
| 281 | q = p->next; |
| 282 | kfree(p); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree) |
| 287 | { |
| 288 | #ifdef CONFIG_AUDIT_TREE |
| 289 | struct audit_tree_refs *p; |
| 290 | int n; |
| 291 | if (!tree) |
| 292 | return 0; |
| 293 | /* full ones */ |
| 294 | for (p = ctx->first_trees; p != ctx->trees; p = p->next) { |
| 295 | for (n = 0; n < 31; n++) |
| 296 | if (audit_tree_match(p->c[n], tree)) |
| 297 | return 1; |
| 298 | } |
| 299 | /* partial */ |
| 300 | if (p) { |
| 301 | for (n = ctx->tree_count; n < 31; n++) |
| 302 | if (audit_tree_match(p->c[n], tree)) |
| 303 | return 1; |
| 304 | } |
| 305 | #endif |
| 306 | return 0; |
| 307 | } |
| 308 | |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 309 | static int audit_compare_uid(kuid_t uid, |
| 310 | struct audit_names *name, |
| 311 | struct audit_field *f, |
| 312 | struct audit_context *ctx) |
Eric Paris | b34b039 | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 313 | { |
| 314 | struct audit_names *n; |
Eric Paris | b34b039 | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 315 | int rc; |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 316 | |
Eric Paris | b34b039 | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 317 | if (name) { |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 318 | rc = audit_uid_comparator(uid, f->op, name->uid); |
Eric Paris | b34b039 | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 319 | if (rc) |
| 320 | return rc; |
| 321 | } |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 322 | |
Eric Paris | b34b039 | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 323 | if (ctx) { |
| 324 | list_for_each_entry(n, &ctx->names_list, list) { |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 325 | rc = audit_uid_comparator(uid, f->op, n->uid); |
| 326 | if (rc) |
| 327 | return rc; |
| 328 | } |
| 329 | } |
| 330 | return 0; |
| 331 | } |
Eric Paris | b34b039 | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 332 | |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 333 | static int audit_compare_gid(kgid_t gid, |
| 334 | struct audit_names *name, |
| 335 | struct audit_field *f, |
| 336 | struct audit_context *ctx) |
| 337 | { |
| 338 | struct audit_names *n; |
| 339 | int rc; |
| 340 | |
| 341 | if (name) { |
| 342 | rc = audit_gid_comparator(gid, f->op, name->gid); |
| 343 | if (rc) |
| 344 | return rc; |
| 345 | } |
| 346 | |
| 347 | if (ctx) { |
| 348 | list_for_each_entry(n, &ctx->names_list, list) { |
| 349 | rc = audit_gid_comparator(gid, f->op, n->gid); |
Eric Paris | b34b039 | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 350 | if (rc) |
| 351 | return rc; |
| 352 | } |
| 353 | } |
| 354 | return 0; |
| 355 | } |
| 356 | |
Eric Paris | 02d86a5 | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 357 | static int audit_field_compare(struct task_struct *tsk, |
| 358 | const struct cred *cred, |
| 359 | struct audit_field *f, |
| 360 | struct audit_context *ctx, |
| 361 | struct audit_names *name) |
| 362 | { |
Eric Paris | 02d86a5 | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 363 | switch (f->val) { |
Peter Moody | 4a6633e | 2011-12-13 16:17:51 -0800 | [diff] [blame] | 364 | /* process to file object comparisons */ |
Eric Paris | 02d86a5 | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 365 | case AUDIT_COMPARE_UID_TO_OBJ_UID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 366 | return audit_compare_uid(cred->uid, name, f, ctx); |
Eric Paris | c9fe685 | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 367 | case AUDIT_COMPARE_GID_TO_OBJ_GID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 368 | return audit_compare_gid(cred->gid, name, f, ctx); |
Peter Moody | 4a6633e | 2011-12-13 16:17:51 -0800 | [diff] [blame] | 369 | case AUDIT_COMPARE_EUID_TO_OBJ_UID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 370 | return audit_compare_uid(cred->euid, name, f, ctx); |
Peter Moody | 4a6633e | 2011-12-13 16:17:51 -0800 | [diff] [blame] | 371 | case AUDIT_COMPARE_EGID_TO_OBJ_GID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 372 | return audit_compare_gid(cred->egid, name, f, ctx); |
Peter Moody | 4a6633e | 2011-12-13 16:17:51 -0800 | [diff] [blame] | 373 | case AUDIT_COMPARE_AUID_TO_OBJ_UID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 374 | return audit_compare_uid(tsk->loginuid, name, f, ctx); |
Peter Moody | 4a6633e | 2011-12-13 16:17:51 -0800 | [diff] [blame] | 375 | case AUDIT_COMPARE_SUID_TO_OBJ_UID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 376 | return audit_compare_uid(cred->suid, name, f, ctx); |
Peter Moody | 4a6633e | 2011-12-13 16:17:51 -0800 | [diff] [blame] | 377 | case AUDIT_COMPARE_SGID_TO_OBJ_GID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 378 | return audit_compare_gid(cred->sgid, name, f, ctx); |
Peter Moody | 4a6633e | 2011-12-13 16:17:51 -0800 | [diff] [blame] | 379 | case AUDIT_COMPARE_FSUID_TO_OBJ_UID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 380 | return audit_compare_uid(cred->fsuid, name, f, ctx); |
Peter Moody | 4a6633e | 2011-12-13 16:17:51 -0800 | [diff] [blame] | 381 | case AUDIT_COMPARE_FSGID_TO_OBJ_GID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 382 | return audit_compare_gid(cred->fsgid, name, f, ctx); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 383 | /* uid comparisons */ |
| 384 | case AUDIT_COMPARE_UID_TO_AUID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 385 | return audit_uid_comparator(cred->uid, f->op, tsk->loginuid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 386 | case AUDIT_COMPARE_UID_TO_EUID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 387 | return audit_uid_comparator(cred->uid, f->op, cred->euid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 388 | case AUDIT_COMPARE_UID_TO_SUID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 389 | return audit_uid_comparator(cred->uid, f->op, cred->suid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 390 | case AUDIT_COMPARE_UID_TO_FSUID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 391 | return audit_uid_comparator(cred->uid, f->op, cred->fsuid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 392 | /* auid comparisons */ |
| 393 | case AUDIT_COMPARE_AUID_TO_EUID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 394 | return audit_uid_comparator(tsk->loginuid, f->op, cred->euid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 395 | case AUDIT_COMPARE_AUID_TO_SUID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 396 | return audit_uid_comparator(tsk->loginuid, f->op, cred->suid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 397 | case AUDIT_COMPARE_AUID_TO_FSUID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 398 | return audit_uid_comparator(tsk->loginuid, f->op, cred->fsuid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 399 | /* euid comparisons */ |
| 400 | case AUDIT_COMPARE_EUID_TO_SUID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 401 | return audit_uid_comparator(cred->euid, f->op, cred->suid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 402 | case AUDIT_COMPARE_EUID_TO_FSUID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 403 | return audit_uid_comparator(cred->euid, f->op, cred->fsuid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 404 | /* suid comparisons */ |
| 405 | case AUDIT_COMPARE_SUID_TO_FSUID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 406 | return audit_uid_comparator(cred->suid, f->op, cred->fsuid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 407 | /* gid comparisons */ |
| 408 | case AUDIT_COMPARE_GID_TO_EGID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 409 | return audit_gid_comparator(cred->gid, f->op, cred->egid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 410 | case AUDIT_COMPARE_GID_TO_SGID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 411 | return audit_gid_comparator(cred->gid, f->op, cred->sgid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 412 | case AUDIT_COMPARE_GID_TO_FSGID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 413 | return audit_gid_comparator(cred->gid, f->op, cred->fsgid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 414 | /* egid comparisons */ |
| 415 | case AUDIT_COMPARE_EGID_TO_SGID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 416 | return audit_gid_comparator(cred->egid, f->op, cred->sgid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 417 | case AUDIT_COMPARE_EGID_TO_FSGID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 418 | return audit_gid_comparator(cred->egid, f->op, cred->fsgid); |
Peter Moody | 10d6836 | 2012-01-04 15:24:31 -0500 | [diff] [blame] | 419 | /* sgid comparison */ |
| 420 | case AUDIT_COMPARE_SGID_TO_FSGID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 421 | return audit_gid_comparator(cred->sgid, f->op, cred->fsgid); |
Eric Paris | 02d86a5 | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 422 | default: |
| 423 | WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n"); |
| 424 | return 0; |
| 425 | } |
| 426 | return 0; |
| 427 | } |
| 428 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 429 | /* Determine if any context name data matches a rule's watch data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | /* Compare a task_struct with an audit_rule. Return 1 on match, 0 |
Tony Jones | f562988 | 2011-04-27 15:10:49 +0200 | [diff] [blame] | 431 | * otherwise. |
| 432 | * |
| 433 | * If task_creation is true, this is an explicit indication that we are |
| 434 | * filtering a task rule at task creation time. This and tsk == current are |
| 435 | * the only situations where tsk->cred may be accessed without an rcu read lock. |
| 436 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | static int audit_filter_rules(struct task_struct *tsk, |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 438 | struct audit_krule *rule, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | struct audit_context *ctx, |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 440 | struct audit_names *name, |
Tony Jones | f562988 | 2011-04-27 15:10:49 +0200 | [diff] [blame] | 441 | enum audit_state *state, |
| 442 | bool task_creation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
Tony Jones | f562988 | 2011-04-27 15:10:49 +0200 | [diff] [blame] | 444 | const struct cred *cred; |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 445 | int i, need_sid = 1; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 446 | u32 sid; |
| 447 | |
Tony Jones | f562988 | 2011-04-27 15:10:49 +0200 | [diff] [blame] | 448 | cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation); |
| 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | for (i = 0; i < rule->field_count; i++) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 451 | struct audit_field *f = &rule->fields[i]; |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 452 | struct audit_names *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | int result = 0; |
| 454 | |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 455 | switch (f->type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | case AUDIT_PID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 457 | result = audit_comparator(tsk->pid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | break; |
Al Viro | 3c66251 | 2006-05-06 08:26:27 -0400 | [diff] [blame] | 459 | case AUDIT_PPID: |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 460 | if (ctx) { |
| 461 | if (!ctx->ppid) |
| 462 | ctx->ppid = sys_getppid(); |
Al Viro | 3c66251 | 2006-05-06 08:26:27 -0400 | [diff] [blame] | 463 | result = audit_comparator(ctx->ppid, f->op, f->val); |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 464 | } |
Al Viro | 3c66251 | 2006-05-06 08:26:27 -0400 | [diff] [blame] | 465 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | case AUDIT_UID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 467 | result = audit_uid_comparator(cred->uid, f->op, f->uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | break; |
| 469 | case AUDIT_EUID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 470 | result = audit_uid_comparator(cred->euid, f->op, f->uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | break; |
| 472 | case AUDIT_SUID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 473 | result = audit_uid_comparator(cred->suid, f->op, f->uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | break; |
| 475 | case AUDIT_FSUID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 476 | result = audit_uid_comparator(cred->fsuid, f->op, f->uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | break; |
| 478 | case AUDIT_GID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 479 | result = audit_gid_comparator(cred->gid, f->op, f->gid); |
Matvejchikov Ilya | 37eebe3 | 2011-12-13 23:09:08 +0300 | [diff] [blame] | 480 | if (f->op == Audit_equal) { |
| 481 | if (!result) |
| 482 | result = in_group_p(f->gid); |
| 483 | } else if (f->op == Audit_not_equal) { |
| 484 | if (result) |
| 485 | result = !in_group_p(f->gid); |
| 486 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | break; |
| 488 | case AUDIT_EGID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 489 | result = audit_gid_comparator(cred->egid, f->op, f->gid); |
Matvejchikov Ilya | 37eebe3 | 2011-12-13 23:09:08 +0300 | [diff] [blame] | 490 | if (f->op == Audit_equal) { |
| 491 | if (!result) |
| 492 | result = in_egroup_p(f->gid); |
| 493 | } else if (f->op == Audit_not_equal) { |
| 494 | if (result) |
| 495 | result = !in_egroup_p(f->gid); |
| 496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | break; |
| 498 | case AUDIT_SGID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 499 | result = audit_gid_comparator(cred->sgid, f->op, f->gid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | break; |
| 501 | case AUDIT_FSGID: |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 502 | result = audit_gid_comparator(cred->fsgid, f->op, f->gid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | break; |
| 504 | case AUDIT_PERS: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 505 | result = audit_comparator(tsk->personality, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | break; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 507 | case AUDIT_ARCH: |
Daniel Walker | 9f8dbe9 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 508 | if (ctx) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 509 | result = audit_comparator(ctx->arch, f->op, f->val); |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 510 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
| 512 | case AUDIT_EXIT: |
| 513 | if (ctx && ctx->return_valid) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 514 | result = audit_comparator(ctx->return_code, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | break; |
| 516 | case AUDIT_SUCCESS: |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 517 | if (ctx && ctx->return_valid) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 518 | if (f->val) |
| 519 | result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS); |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 520 | else |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 521 | result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE); |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 522 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | break; |
| 524 | case AUDIT_DEVMAJOR: |
Eric Paris | 16c174b | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 525 | if (name) { |
| 526 | if (audit_comparator(MAJOR(name->dev), f->op, f->val) || |
| 527 | audit_comparator(MAJOR(name->rdev), f->op, f->val)) |
| 528 | ++result; |
| 529 | } else if (ctx) { |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 530 | list_for_each_entry(n, &ctx->names_list, list) { |
Eric Paris | 16c174b | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 531 | if (audit_comparator(MAJOR(n->dev), f->op, f->val) || |
| 532 | audit_comparator(MAJOR(n->rdev), f->op, f->val)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | ++result; |
| 534 | break; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | break; |
| 539 | case AUDIT_DEVMINOR: |
Eric Paris | 16c174b | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 540 | if (name) { |
| 541 | if (audit_comparator(MINOR(name->dev), f->op, f->val) || |
| 542 | audit_comparator(MINOR(name->rdev), f->op, f->val)) |
| 543 | ++result; |
| 544 | } else if (ctx) { |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 545 | list_for_each_entry(n, &ctx->names_list, list) { |
Eric Paris | 16c174b | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 546 | if (audit_comparator(MINOR(n->dev), f->op, f->val) || |
| 547 | audit_comparator(MINOR(n->rdev), f->op, f->val)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | ++result; |
| 549 | break; |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | break; |
| 554 | case AUDIT_INODE: |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 555 | if (name) |
Richard Guy Briggs | db510fc | 2013-07-04 12:56:11 -0400 | [diff] [blame] | 556 | result = audit_comparator(name->ino, f->op, f->val); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 557 | else if (ctx) { |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 558 | list_for_each_entry(n, &ctx->names_list, list) { |
| 559 | if (audit_comparator(n->ino, f->op, f->val)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | ++result; |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | break; |
Eric Paris | efaffd6 | 2012-01-03 14:23:07 -0500 | [diff] [blame] | 566 | case AUDIT_OBJ_UID: |
| 567 | if (name) { |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 568 | result = audit_uid_comparator(name->uid, f->op, f->uid); |
Eric Paris | efaffd6 | 2012-01-03 14:23:07 -0500 | [diff] [blame] | 569 | } else if (ctx) { |
| 570 | list_for_each_entry(n, &ctx->names_list, list) { |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 571 | if (audit_uid_comparator(n->uid, f->op, f->uid)) { |
Eric Paris | efaffd6 | 2012-01-03 14:23:07 -0500 | [diff] [blame] | 572 | ++result; |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | break; |
Eric Paris | 54d3218 | 2012-01-03 14:23:07 -0500 | [diff] [blame] | 578 | case AUDIT_OBJ_GID: |
| 579 | if (name) { |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 580 | result = audit_gid_comparator(name->gid, f->op, f->gid); |
Eric Paris | 54d3218 | 2012-01-03 14:23:07 -0500 | [diff] [blame] | 581 | } else if (ctx) { |
| 582 | list_for_each_entry(n, &ctx->names_list, list) { |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 583 | if (audit_gid_comparator(n->gid, f->op, f->gid)) { |
Eric Paris | 54d3218 | 2012-01-03 14:23:07 -0500 | [diff] [blame] | 584 | ++result; |
| 585 | break; |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | break; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 590 | case AUDIT_WATCH: |
Eric Paris | ae7b8f4 | 2009-12-17 20:12:04 -0500 | [diff] [blame] | 591 | if (name) |
| 592 | result = audit_watch_compare(rule->watch, name->ino, name->dev); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 593 | break; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 594 | case AUDIT_DIR: |
| 595 | if (ctx) |
| 596 | result = match_tree_refs(ctx, rule->tree); |
| 597 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | case AUDIT_LOGINUID: |
| 599 | result = 0; |
| 600 | if (ctx) |
Eric W. Biederman | ca57ec0 | 2012-09-11 02:18:08 -0700 | [diff] [blame] | 601 | result = audit_uid_comparator(tsk->loginuid, f->op, f->uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | break; |
Eric W. Biederman | 780a765 | 2013-04-09 02:22:10 -0700 | [diff] [blame] | 603 | case AUDIT_LOGINUID_SET: |
| 604 | result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val); |
| 605 | break; |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 606 | case AUDIT_SUBJ_USER: |
| 607 | case AUDIT_SUBJ_ROLE: |
| 608 | case AUDIT_SUBJ_TYPE: |
| 609 | case AUDIT_SUBJ_SEN: |
| 610 | case AUDIT_SUBJ_CLR: |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 611 | /* NOTE: this may return negative values indicating |
| 612 | a temporary error. We simply treat this as a |
| 613 | match for now to avoid losing information that |
| 614 | may be wanted. An error message will also be |
| 615 | logged upon error */ |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 616 | if (f->lsm_rule) { |
Steve Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 617 | if (need_sid) { |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 618 | security_task_getsecid(tsk, &sid); |
Steve Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 619 | need_sid = 0; |
| 620 | } |
Ahmed S. Darwish | d7a96f3 | 2008-03-01 22:01:11 +0200 | [diff] [blame] | 621 | result = security_audit_rule_match(sid, f->type, |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 622 | f->op, |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 623 | f->lsm_rule, |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 624 | ctx); |
Steve Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 625 | } |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 626 | break; |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 627 | case AUDIT_OBJ_USER: |
| 628 | case AUDIT_OBJ_ROLE: |
| 629 | case AUDIT_OBJ_TYPE: |
| 630 | case AUDIT_OBJ_LEV_LOW: |
| 631 | case AUDIT_OBJ_LEV_HIGH: |
| 632 | /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR |
| 633 | also applies here */ |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 634 | if (f->lsm_rule) { |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 635 | /* Find files that match */ |
| 636 | if (name) { |
Ahmed S. Darwish | d7a96f3 | 2008-03-01 22:01:11 +0200 | [diff] [blame] | 637 | result = security_audit_rule_match( |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 638 | name->osid, f->type, f->op, |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 639 | f->lsm_rule, ctx); |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 640 | } else if (ctx) { |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 641 | list_for_each_entry(n, &ctx->names_list, list) { |
| 642 | if (security_audit_rule_match(n->osid, f->type, |
| 643 | f->op, f->lsm_rule, |
| 644 | ctx)) { |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 645 | ++result; |
| 646 | break; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | /* Find ipc objects that match */ |
Al Viro | a33e675 | 2008-12-10 03:40:06 -0500 | [diff] [blame] | 651 | if (!ctx || ctx->type != AUDIT_IPC) |
| 652 | break; |
| 653 | if (security_audit_rule_match(ctx->ipc.osid, |
| 654 | f->type, f->op, |
| 655 | f->lsm_rule, ctx)) |
| 656 | ++result; |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 657 | } |
| 658 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | case AUDIT_ARG0: |
| 660 | case AUDIT_ARG1: |
| 661 | case AUDIT_ARG2: |
| 662 | case AUDIT_ARG3: |
| 663 | if (ctx) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 664 | result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | break; |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 666 | case AUDIT_FILTERKEY: |
| 667 | /* ignore this field for filtering */ |
| 668 | result = 1; |
| 669 | break; |
Al Viro | 55669bf | 2006-08-31 19:26:40 -0400 | [diff] [blame] | 670 | case AUDIT_PERM: |
| 671 | result = audit_match_perm(ctx, f->val); |
| 672 | break; |
Al Viro | 8b67dca | 2008-04-28 04:15:49 -0400 | [diff] [blame] | 673 | case AUDIT_FILETYPE: |
| 674 | result = audit_match_filetype(ctx, f->val); |
| 675 | break; |
Eric Paris | 02d86a5 | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 676 | case AUDIT_FIELD_COMPARE: |
| 677 | result = audit_field_compare(tsk, cred, f, ctx, name); |
| 678 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
Tony Jones | f562988 | 2011-04-27 15:10:49 +0200 | [diff] [blame] | 680 | if (!result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | return 0; |
| 682 | } |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 683 | |
| 684 | if (ctx) { |
| 685 | if (rule->prio <= ctx->prio) |
| 686 | return 0; |
| 687 | if (rule->filterkey) { |
| 688 | kfree(ctx->filterkey); |
| 689 | ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC); |
| 690 | } |
| 691 | ctx->prio = rule->prio; |
| 692 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | switch (rule->action) { |
| 694 | case AUDIT_NEVER: *state = AUDIT_DISABLED; break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break; |
| 696 | } |
| 697 | return 1; |
| 698 | } |
| 699 | |
| 700 | /* At process creation time, we can determine if system-call auditing is |
| 701 | * completely disabled for this task. Since we only have the task |
| 702 | * structure at this point, we can only check uid and gid. |
| 703 | */ |
Al Viro | e048e02 | 2008-12-16 03:51:22 -0500 | [diff] [blame] | 704 | static enum audit_state audit_filter_task(struct task_struct *tsk, char **key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | { |
| 706 | struct audit_entry *e; |
| 707 | enum audit_state state; |
| 708 | |
| 709 | rcu_read_lock(); |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 710 | list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) { |
Tony Jones | f562988 | 2011-04-27 15:10:49 +0200 | [diff] [blame] | 711 | if (audit_filter_rules(tsk, &e->rule, NULL, NULL, |
| 712 | &state, true)) { |
Al Viro | e048e02 | 2008-12-16 03:51:22 -0500 | [diff] [blame] | 713 | if (state == AUDIT_RECORD_CONTEXT) |
| 714 | *key = kstrdup(e->rule.filterkey, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | rcu_read_unlock(); |
| 716 | return state; |
| 717 | } |
| 718 | } |
| 719 | rcu_read_unlock(); |
| 720 | return AUDIT_BUILD_CONTEXT; |
| 721 | } |
| 722 | |
| 723 | /* At syscall entry and exit time, this filter is called if the |
| 724 | * 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] | 725 | * 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] | 726 | * 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] | 727 | */ |
| 728 | static enum audit_state audit_filter_syscall(struct task_struct *tsk, |
| 729 | struct audit_context *ctx, |
| 730 | struct list_head *list) |
| 731 | { |
| 732 | struct audit_entry *e; |
David Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 733 | enum audit_state state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
David Woodhouse | 351bb72 | 2005-07-14 14:40:06 +0100 | [diff] [blame] | 735 | if (audit_pid && tsk->tgid == audit_pid) |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 736 | return AUDIT_DISABLED; |
| 737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | rcu_read_lock(); |
David Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 739 | if (!list_empty(list)) { |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 740 | int word = AUDIT_WORD(ctx->major); |
| 741 | int bit = AUDIT_BIT(ctx->major); |
David Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 742 | |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 743 | list_for_each_entry_rcu(e, list, list) { |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 744 | if ((e->rule.mask[word] & bit) == bit && |
| 745 | audit_filter_rules(tsk, &e->rule, ctx, NULL, |
Tony Jones | f562988 | 2011-04-27 15:10:49 +0200 | [diff] [blame] | 746 | &state, false)) { |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 747 | rcu_read_unlock(); |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 748 | ctx->current_state = state; |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 749 | return state; |
| 750 | } |
| 751 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | } |
| 753 | rcu_read_unlock(); |
| 754 | return AUDIT_BUILD_CONTEXT; |
| 755 | } |
| 756 | |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 757 | /* |
| 758 | * Given an audit_name check the inode hash table to see if they match. |
| 759 | * Called holding the rcu read lock to protect the use of audit_inode_hash |
| 760 | */ |
| 761 | static int audit_filter_inode_name(struct task_struct *tsk, |
| 762 | struct audit_names *n, |
| 763 | struct audit_context *ctx) { |
| 764 | int word, bit; |
| 765 | int h = audit_hash_ino((u32)n->ino); |
| 766 | struct list_head *list = &audit_inode_hash[h]; |
| 767 | struct audit_entry *e; |
| 768 | enum audit_state state; |
| 769 | |
| 770 | word = AUDIT_WORD(ctx->major); |
| 771 | bit = AUDIT_BIT(ctx->major); |
| 772 | |
| 773 | if (list_empty(list)) |
| 774 | return 0; |
| 775 | |
| 776 | list_for_each_entry_rcu(e, list, list) { |
| 777 | if ((e->rule.mask[word] & bit) == bit && |
| 778 | audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) { |
| 779 | ctx->current_state = state; |
| 780 | return 1; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | /* At syscall exit time, this filter is called if any audit_names have been |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 788 | * collected during syscall processing. We only check rules in sublists at hash |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 789 | * buckets applicable to the inode numbers in audit_names. |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 790 | * Regarding audit_state, same rules apply as for audit_filter_syscall(). |
| 791 | */ |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 792 | void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx) |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 793 | { |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 794 | struct audit_names *n; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 795 | |
| 796 | if (audit_pid && tsk->tgid == audit_pid) |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 797 | return; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 798 | |
| 799 | rcu_read_lock(); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 800 | |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 801 | list_for_each_entry(n, &ctx->names_list, list) { |
| 802 | if (audit_filter_inode_name(tsk, n, ctx)) |
| 803 | break; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 804 | } |
| 805 | rcu_read_unlock(); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 806 | } |
| 807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | static inline struct audit_context *audit_get_context(struct task_struct *tsk, |
| 809 | int return_valid, |
Paul Moore | 6d208da | 2009-04-01 15:47:27 -0400 | [diff] [blame] | 810 | long return_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | { |
| 812 | struct audit_context *context = tsk->audit_context; |
| 813 | |
Eric Paris | 56179a6 | 2012-01-03 14:23:06 -0500 | [diff] [blame] | 814 | if (!context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | return NULL; |
| 816 | context->return_valid = return_valid; |
Eric Paris | f701b75 | 2008-01-07 13:34:51 -0500 | [diff] [blame] | 817 | |
| 818 | /* |
| 819 | * we need to fix up the return code in the audit logs if the actual |
| 820 | * return codes are later going to be fixed up by the arch specific |
| 821 | * signal handlers |
| 822 | * |
| 823 | * This is actually a test for: |
| 824 | * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) || |
| 825 | * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK) |
| 826 | * |
| 827 | * but is faster than a bunch of || |
| 828 | */ |
| 829 | if (unlikely(return_code <= -ERESTARTSYS) && |
| 830 | (return_code >= -ERESTART_RESTARTBLOCK) && |
| 831 | (return_code != -ENOIOCTLCMD)) |
| 832 | context->return_code = -EINTR; |
| 833 | else |
| 834 | context->return_code = return_code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 836 | if (context->in_syscall && !context->dummy) { |
| 837 | audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]); |
| 838 | audit_filter_inodes(tsk, context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | tsk->audit_context = NULL; |
| 842 | return context; |
| 843 | } |
| 844 | |
| 845 | static inline void audit_free_names(struct audit_context *context) |
| 846 | { |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 847 | struct audit_names *n, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
| 849 | #if AUDIT_DEBUG == 2 |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 850 | if (context->put_count + context->ino_count != context->name_count) { |
Eric Paris | 34c474d | 2013-04-16 10:17:02 -0400 | [diff] [blame] | 851 | int i = 0; |
| 852 | |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 853 | printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | " name_count=%d put_count=%d" |
| 855 | " ino_count=%d [NOT freeing]\n", |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 856 | __FILE__, __LINE__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | context->serial, context->major, context->in_syscall, |
| 858 | context->name_count, context->put_count, |
| 859 | context->ino_count); |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 860 | list_for_each_entry(n, &context->names_list, list) { |
Eric Paris | 34c474d | 2013-04-16 10:17:02 -0400 | [diff] [blame] | 861 | printk(KERN_ERR "names[%d] = %p = %s\n", i++, |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 862 | n->name, n->name->name ?: "(null)"); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 863 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | dump_stack(); |
| 865 | return; |
| 866 | } |
| 867 | #endif |
| 868 | #if AUDIT_DEBUG |
| 869 | context->put_count = 0; |
| 870 | context->ino_count = 0; |
| 871 | #endif |
| 872 | |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 873 | list_for_each_entry_safe(n, next, &context->names_list, list) { |
| 874 | list_del(&n->list); |
| 875 | if (n->name && n->name_put) |
Dmitry Monakhov | 65ada7b | 2013-04-01 11:00:00 +0400 | [diff] [blame] | 876 | final_putname(n->name); |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 877 | if (n->should_free) |
| 878 | kfree(n); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 879 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | context->name_count = 0; |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 881 | path_put(&context->pwd); |
| 882 | context->pwd.dentry = NULL; |
| 883 | context->pwd.mnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | static inline void audit_free_aux(struct audit_context *context) |
| 887 | { |
| 888 | struct audit_aux_data *aux; |
| 889 | |
| 890 | while ((aux = context->aux)) { |
| 891 | context->aux = aux->next; |
| 892 | kfree(aux); |
| 893 | } |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 894 | while ((aux = context->aux_pids)) { |
| 895 | context->aux_pids = aux->next; |
| 896 | kfree(aux); |
| 897 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | static inline struct audit_context *audit_alloc_context(enum audit_state state) |
| 901 | { |
| 902 | struct audit_context *context; |
| 903 | |
Rakib Mullick | 17c6ee7 | 2013-04-07 16:14:18 +0600 | [diff] [blame] | 904 | context = kzalloc(sizeof(*context), GFP_KERNEL); |
| 905 | if (!context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | return NULL; |
Andrew Morton | e2c5adc | 2013-04-08 14:43:41 -0700 | [diff] [blame] | 907 | context->state = state; |
| 908 | context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0; |
Al Viro | 916d757 | 2009-06-24 00:02:38 -0400 | [diff] [blame] | 909 | INIT_LIST_HEAD(&context->killed_trees); |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 910 | INIT_LIST_HEAD(&context->names_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | return context; |
| 912 | } |
| 913 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 914 | /** |
| 915 | * audit_alloc - allocate an audit context block for a task |
| 916 | * @tsk: task |
| 917 | * |
| 918 | * Filter on the task information and allocate a per-task audit context |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | * if necessary. Doing so turns on system call auditing for the |
| 920 | * specified task. This is called from copy_process, so no lock is |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 921 | * needed. |
| 922 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | int audit_alloc(struct task_struct *tsk) |
| 924 | { |
| 925 | struct audit_context *context; |
| 926 | enum audit_state state; |
Al Viro | e048e02 | 2008-12-16 03:51:22 -0500 | [diff] [blame] | 927 | char *key = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | |
Eric Paris | b593d38 | 2008-01-08 17:38:31 -0500 | [diff] [blame] | 929 | if (likely(!audit_ever_enabled)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | return 0; /* Return if not auditing. */ |
| 931 | |
Al Viro | e048e02 | 2008-12-16 03:51:22 -0500 | [diff] [blame] | 932 | state = audit_filter_task(tsk, &key); |
Oleg Nesterov | d48d805 | 2013-09-15 19:11:09 +0200 | [diff] [blame] | 933 | if (state == AUDIT_DISABLED) { |
| 934 | clear_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | return 0; |
Oleg Nesterov | d48d805 | 2013-09-15 19:11:09 +0200 | [diff] [blame] | 936 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
| 938 | if (!(context = audit_alloc_context(state))) { |
Al Viro | e048e02 | 2008-12-16 03:51:22 -0500 | [diff] [blame] | 939 | kfree(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | audit_log_lost("out of memory in audit_alloc"); |
| 941 | return -ENOMEM; |
| 942 | } |
Al Viro | e048e02 | 2008-12-16 03:51:22 -0500 | [diff] [blame] | 943 | context->filterkey = key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | tsk->audit_context = context; |
| 946 | set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT); |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | static inline void audit_free_context(struct audit_context *context) |
| 951 | { |
Al Viro | c62d773 | 2012-10-20 15:07:18 -0400 | [diff] [blame] | 952 | audit_free_names(context); |
| 953 | unroll_tree_refs(context, NULL, 0); |
| 954 | free_tree_refs(context); |
| 955 | audit_free_aux(context); |
| 956 | kfree(context->filterkey); |
| 957 | kfree(context->sockaddr); |
| 958 | kfree(context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 961 | static int audit_log_pid_context(struct audit_context *context, pid_t pid, |
Eric W. Biederman | cca080d | 2012-02-07 16:53:48 -0800 | [diff] [blame] | 962 | kuid_t auid, kuid_t uid, unsigned int sessionid, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 963 | u32 sid, char *comm) |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 964 | { |
| 965 | struct audit_buffer *ab; |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 966 | char *ctx = NULL; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 967 | u32 len; |
| 968 | int rc = 0; |
| 969 | |
| 970 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID); |
| 971 | if (!ab) |
Eric Paris | 6246cca | 2008-01-07 14:01:18 -0500 | [diff] [blame] | 972 | return rc; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 973 | |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 974 | audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, |
| 975 | from_kuid(&init_user_ns, auid), |
Eric W. Biederman | cca080d | 2012-02-07 16:53:48 -0800 | [diff] [blame] | 976 | from_kuid(&init_user_ns, uid), sessionid); |
Eric Paris | ad395ab | 2012-10-23 08:58:35 -0400 | [diff] [blame] | 977 | if (sid) { |
| 978 | if (security_secid_to_secctx(sid, &ctx, &len)) { |
| 979 | audit_log_format(ab, " obj=(none)"); |
| 980 | rc = 1; |
| 981 | } else { |
| 982 | audit_log_format(ab, " obj=%s", ctx); |
| 983 | security_release_secctx(ctx, len); |
| 984 | } |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 985 | } |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 986 | audit_log_format(ab, " ocomm="); |
| 987 | audit_log_untrustedstring(ab, comm); |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 988 | audit_log_end(ab); |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 989 | |
| 990 | return rc; |
| 991 | } |
| 992 | |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 993 | /* |
| 994 | * to_send and len_sent accounting are very loose estimates. We aren't |
| 995 | * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 996 | * within about 500 bytes (next page boundary) |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 997 | * |
| 998 | * why snprintf? an int is up to 12 digits long. if we just assumed when |
| 999 | * logging that a[%d]= was going to be 16 characters long we would be wasting |
| 1000 | * space in every audit message. In one 7500 byte message we can log up to |
| 1001 | * about 1000 min size arguments. That comes down to about 50% waste of space |
| 1002 | * if we didn't do the snprintf to find out how long arg_num_len was. |
| 1003 | */ |
| 1004 | static int audit_log_single_execve_arg(struct audit_context *context, |
| 1005 | struct audit_buffer **ab, |
| 1006 | int arg_num, |
| 1007 | size_t *len_sent, |
| 1008 | const char __user *p, |
| 1009 | char *buf) |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1010 | { |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1011 | char arg_num_len_buf[12]; |
| 1012 | const char __user *tmp_p = p; |
Eric Paris | b87ce6e | 2009-06-11 14:31:34 -0400 | [diff] [blame] | 1013 | /* how many digits are in arg_num? 5 is the length of ' a=""' */ |
| 1014 | size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5; |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1015 | size_t len, len_left, to_send; |
| 1016 | size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN; |
| 1017 | unsigned int i, has_cntl = 0, too_long = 0; |
| 1018 | int ret; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1019 | |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1020 | /* strnlen_user includes the null we don't want to send */ |
| 1021 | len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1022 | |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1023 | /* |
| 1024 | * We just created this mm, if we can't find the strings |
| 1025 | * we just copied into it something is _very_ wrong. Similar |
| 1026 | * for strings that are too long, we should not have created |
| 1027 | * any. |
| 1028 | */ |
Eric Paris | b0abcfc | 2008-02-18 18:23:16 -0500 | [diff] [blame] | 1029 | if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) { |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1030 | WARN_ON(1); |
| 1031 | send_sig(SIGKILL, current, 0); |
Eric Paris | b0abcfc | 2008-02-18 18:23:16 -0500 | [diff] [blame] | 1032 | return -1; |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1033 | } |
Peter Zijlstra | 040b3a2 | 2007-07-28 00:55:18 +0200 | [diff] [blame] | 1034 | |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1035 | /* walk the whole argument looking for non-ascii chars */ |
| 1036 | do { |
| 1037 | if (len_left > MAX_EXECVE_AUDIT_LEN) |
| 1038 | to_send = MAX_EXECVE_AUDIT_LEN; |
| 1039 | else |
| 1040 | to_send = len_left; |
| 1041 | ret = copy_from_user(buf, tmp_p, to_send); |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1042 | /* |
| 1043 | * There is no reason for this copy to be short. We just |
| 1044 | * copied them here, and the mm hasn't been exposed to user- |
| 1045 | * space yet. |
| 1046 | */ |
Peter Zijlstra | 040b3a2 | 2007-07-28 00:55:18 +0200 | [diff] [blame] | 1047 | if (ret) { |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1048 | WARN_ON(1); |
| 1049 | send_sig(SIGKILL, current, 0); |
Eric Paris | b0abcfc | 2008-02-18 18:23:16 -0500 | [diff] [blame] | 1050 | return -1; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1051 | } |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1052 | buf[to_send] = '\0'; |
| 1053 | has_cntl = audit_string_contains_control(buf, to_send); |
| 1054 | if (has_cntl) { |
| 1055 | /* |
| 1056 | * hex messages get logged as 2 bytes, so we can only |
| 1057 | * send half as much in each message |
| 1058 | */ |
| 1059 | max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2; |
| 1060 | break; |
| 1061 | } |
| 1062 | len_left -= to_send; |
| 1063 | tmp_p += to_send; |
| 1064 | } while (len_left > 0); |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1065 | |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1066 | len_left = len; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1067 | |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1068 | if (len > max_execve_audit_len) |
| 1069 | too_long = 1; |
| 1070 | |
| 1071 | /* rewalk the argument actually logging the message */ |
| 1072 | for (i = 0; len_left > 0; i++) { |
| 1073 | int room_left; |
| 1074 | |
| 1075 | if (len_left > max_execve_audit_len) |
| 1076 | to_send = max_execve_audit_len; |
| 1077 | else |
| 1078 | to_send = len_left; |
| 1079 | |
| 1080 | /* do we have space left to send this argument in this ab? */ |
| 1081 | room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent; |
| 1082 | if (has_cntl) |
| 1083 | room_left -= (to_send * 2); |
| 1084 | else |
| 1085 | room_left -= to_send; |
| 1086 | if (room_left < 0) { |
| 1087 | *len_sent = 0; |
| 1088 | audit_log_end(*ab); |
| 1089 | *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE); |
| 1090 | if (!*ab) |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | /* |
| 1095 | * first record needs to say how long the original string was |
| 1096 | * so we can be sure nothing was lost. |
| 1097 | */ |
| 1098 | if ((i == 0) && (too_long)) |
Jiri Pirko | ca96a89 | 2009-01-09 16:44:16 +0100 | [diff] [blame] | 1099 | audit_log_format(*ab, " a%d_len=%zu", arg_num, |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1100 | has_cntl ? 2*len : len); |
| 1101 | |
| 1102 | /* |
| 1103 | * normally arguments are small enough to fit and we already |
| 1104 | * filled buf above when we checked for control characters |
| 1105 | * so don't bother with another copy_from_user |
| 1106 | */ |
| 1107 | if (len >= max_execve_audit_len) |
| 1108 | ret = copy_from_user(buf, p, to_send); |
| 1109 | else |
| 1110 | ret = 0; |
| 1111 | if (ret) { |
| 1112 | WARN_ON(1); |
| 1113 | send_sig(SIGKILL, current, 0); |
Eric Paris | b0abcfc | 2008-02-18 18:23:16 -0500 | [diff] [blame] | 1114 | return -1; |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1115 | } |
| 1116 | buf[to_send] = '\0'; |
| 1117 | |
| 1118 | /* actually log it */ |
Jiri Pirko | ca96a89 | 2009-01-09 16:44:16 +0100 | [diff] [blame] | 1119 | audit_log_format(*ab, " a%d", arg_num); |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1120 | if (too_long) |
| 1121 | audit_log_format(*ab, "[%d]", i); |
| 1122 | audit_log_format(*ab, "="); |
| 1123 | if (has_cntl) |
Eric Paris | b556f8a | 2008-04-18 10:12:59 -0400 | [diff] [blame] | 1124 | audit_log_n_hex(*ab, buf, to_send); |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1125 | else |
Eric Paris | 9d96098 | 2009-06-11 14:31:37 -0400 | [diff] [blame] | 1126 | audit_log_string(*ab, buf); |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1127 | |
| 1128 | p += to_send; |
| 1129 | len_left -= to_send; |
| 1130 | *len_sent += arg_num_len; |
| 1131 | if (has_cntl) |
| 1132 | *len_sent += to_send * 2; |
| 1133 | else |
| 1134 | *len_sent += to_send; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1135 | } |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1136 | /* include the null we didn't log */ |
| 1137 | return len + 1; |
| 1138 | } |
| 1139 | |
| 1140 | static void audit_log_execve_info(struct audit_context *context, |
Richard Guy Briggs | d9cfea9 | 2013-10-30 17:56:13 -0400 | [diff] [blame] | 1141 | struct audit_buffer **ab) |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1142 | { |
Xi Wang | 5afb8a3 | 2011-12-20 18:39:41 -0500 | [diff] [blame] | 1143 | int i, len; |
| 1144 | size_t len_sent = 0; |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1145 | const char __user *p; |
| 1146 | char *buf; |
| 1147 | |
Richard Guy Briggs | d9cfea9 | 2013-10-30 17:56:13 -0400 | [diff] [blame] | 1148 | p = (const char __user *)current->mm->arg_start; |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1149 | |
Richard Guy Briggs | d9cfea9 | 2013-10-30 17:56:13 -0400 | [diff] [blame] | 1150 | audit_log_format(*ab, "argc=%d", context->execve.argc); |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1151 | |
| 1152 | /* |
| 1153 | * we need some kernel buffer to hold the userspace args. Just |
| 1154 | * allocate one big one rather than allocating one of the right size |
| 1155 | * for every single argument inside audit_log_single_execve_arg() |
| 1156 | * should be <8k allocation so should be pretty safe. |
| 1157 | */ |
| 1158 | buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL); |
| 1159 | if (!buf) { |
| 1160 | audit_panic("out of memory for argv string\n"); |
| 1161 | return; |
| 1162 | } |
| 1163 | |
Richard Guy Briggs | d9cfea9 | 2013-10-30 17:56:13 -0400 | [diff] [blame] | 1164 | for (i = 0; i < context->execve.argc; i++) { |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1165 | len = audit_log_single_execve_arg(context, ab, i, |
| 1166 | &len_sent, p, buf); |
| 1167 | if (len <= 0) |
| 1168 | break; |
| 1169 | p += len; |
| 1170 | } |
| 1171 | kfree(buf); |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
Al Viro | a33e675 | 2008-12-10 03:40:06 -0500 | [diff] [blame] | 1174 | static void show_special(struct audit_context *context, int *call_panic) |
Al Viro | f3298dc | 2008-12-10 03:16:51 -0500 | [diff] [blame] | 1175 | { |
| 1176 | struct audit_buffer *ab; |
| 1177 | int i; |
| 1178 | |
| 1179 | ab = audit_log_start(context, GFP_KERNEL, context->type); |
| 1180 | if (!ab) |
| 1181 | return; |
| 1182 | |
| 1183 | switch (context->type) { |
| 1184 | case AUDIT_SOCKETCALL: { |
| 1185 | int nargs = context->socketcall.nargs; |
| 1186 | audit_log_format(ab, "nargs=%d", nargs); |
| 1187 | for (i = 0; i < nargs; i++) |
| 1188 | audit_log_format(ab, " a%d=%lx", i, |
| 1189 | context->socketcall.args[i]); |
| 1190 | break; } |
Al Viro | a33e675 | 2008-12-10 03:40:06 -0500 | [diff] [blame] | 1191 | case AUDIT_IPC: { |
| 1192 | u32 osid = context->ipc.osid; |
| 1193 | |
Al Viro | 2570ebb | 2011-07-27 14:03:22 -0400 | [diff] [blame] | 1194 | audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho", |
Eric W. Biederman | cca080d | 2012-02-07 16:53:48 -0800 | [diff] [blame] | 1195 | from_kuid(&init_user_ns, context->ipc.uid), |
| 1196 | from_kgid(&init_user_ns, context->ipc.gid), |
| 1197 | context->ipc.mode); |
Al Viro | a33e675 | 2008-12-10 03:40:06 -0500 | [diff] [blame] | 1198 | if (osid) { |
| 1199 | char *ctx = NULL; |
| 1200 | u32 len; |
| 1201 | if (security_secid_to_secctx(osid, &ctx, &len)) { |
| 1202 | audit_log_format(ab, " osid=%u", osid); |
| 1203 | *call_panic = 1; |
| 1204 | } else { |
| 1205 | audit_log_format(ab, " obj=%s", ctx); |
| 1206 | security_release_secctx(ctx, len); |
| 1207 | } |
| 1208 | } |
Al Viro | e816f37 | 2008-12-10 03:47:15 -0500 | [diff] [blame] | 1209 | if (context->ipc.has_perm) { |
| 1210 | audit_log_end(ab); |
| 1211 | ab = audit_log_start(context, GFP_KERNEL, |
| 1212 | AUDIT_IPC_SET_PERM); |
Kees Cook | 0644ec0 | 2013-01-11 14:32:07 -0800 | [diff] [blame] | 1213 | if (unlikely(!ab)) |
| 1214 | return; |
Al Viro | e816f37 | 2008-12-10 03:47:15 -0500 | [diff] [blame] | 1215 | audit_log_format(ab, |
Al Viro | 2570ebb | 2011-07-27 14:03:22 -0400 | [diff] [blame] | 1216 | "qbytes=%lx ouid=%u ogid=%u mode=%#ho", |
Al Viro | e816f37 | 2008-12-10 03:47:15 -0500 | [diff] [blame] | 1217 | context->ipc.qbytes, |
| 1218 | context->ipc.perm_uid, |
| 1219 | context->ipc.perm_gid, |
| 1220 | context->ipc.perm_mode); |
Al Viro | e816f37 | 2008-12-10 03:47:15 -0500 | [diff] [blame] | 1221 | } |
Al Viro | a33e675 | 2008-12-10 03:40:06 -0500 | [diff] [blame] | 1222 | break; } |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 1223 | case AUDIT_MQ_OPEN: { |
| 1224 | audit_log_format(ab, |
Al Viro | df0a428 | 2011-07-26 05:26:10 -0400 | [diff] [blame] | 1225 | "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld " |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 1226 | "mq_msgsize=%ld mq_curmsgs=%ld", |
| 1227 | context->mq_open.oflag, context->mq_open.mode, |
| 1228 | context->mq_open.attr.mq_flags, |
| 1229 | context->mq_open.attr.mq_maxmsg, |
| 1230 | context->mq_open.attr.mq_msgsize, |
| 1231 | context->mq_open.attr.mq_curmsgs); |
| 1232 | break; } |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 1233 | case AUDIT_MQ_SENDRECV: { |
| 1234 | audit_log_format(ab, |
| 1235 | "mqdes=%d msg_len=%zd msg_prio=%u " |
| 1236 | "abs_timeout_sec=%ld abs_timeout_nsec=%ld", |
| 1237 | context->mq_sendrecv.mqdes, |
| 1238 | context->mq_sendrecv.msg_len, |
| 1239 | context->mq_sendrecv.msg_prio, |
| 1240 | context->mq_sendrecv.abs_timeout.tv_sec, |
| 1241 | context->mq_sendrecv.abs_timeout.tv_nsec); |
| 1242 | break; } |
Al Viro | 20114f7 | 2008-12-10 07:16:12 -0500 | [diff] [blame] | 1243 | case AUDIT_MQ_NOTIFY: { |
| 1244 | audit_log_format(ab, "mqdes=%d sigev_signo=%d", |
| 1245 | context->mq_notify.mqdes, |
| 1246 | context->mq_notify.sigev_signo); |
| 1247 | break; } |
Al Viro | 7392906 | 2008-12-10 06:58:59 -0500 | [diff] [blame] | 1248 | case AUDIT_MQ_GETSETATTR: { |
| 1249 | struct mq_attr *attr = &context->mq_getsetattr.mqstat; |
| 1250 | audit_log_format(ab, |
| 1251 | "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld " |
| 1252 | "mq_curmsgs=%ld ", |
| 1253 | context->mq_getsetattr.mqdes, |
| 1254 | attr->mq_flags, attr->mq_maxmsg, |
| 1255 | attr->mq_msgsize, attr->mq_curmsgs); |
| 1256 | break; } |
Al Viro | 57f71a0 | 2009-01-04 14:52:57 -0500 | [diff] [blame] | 1257 | case AUDIT_CAPSET: { |
| 1258 | audit_log_format(ab, "pid=%d", context->capset.pid); |
| 1259 | audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable); |
| 1260 | audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted); |
| 1261 | audit_log_cap(ab, "cap_pe", &context->capset.cap.effective); |
| 1262 | break; } |
Al Viro | 120a795 | 2010-10-30 02:54:44 -0400 | [diff] [blame] | 1263 | case AUDIT_MMAP: { |
| 1264 | audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd, |
| 1265 | context->mmap.flags); |
| 1266 | break; } |
Richard Guy Briggs | d9cfea9 | 2013-10-30 17:56:13 -0400 | [diff] [blame] | 1267 | case AUDIT_EXECVE: { |
| 1268 | audit_log_execve_info(context, &ab); |
| 1269 | break; } |
Al Viro | f3298dc | 2008-12-10 03:16:51 -0500 | [diff] [blame] | 1270 | } |
| 1271 | audit_log_end(ab); |
| 1272 | } |
| 1273 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1274 | static void audit_log_exit(struct audit_context *context, struct task_struct *tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | { |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1276 | int i, call_panic = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | struct audit_buffer *ab; |
David Woodhouse | 7551ced | 2005-05-26 12:04:57 +0100 | [diff] [blame] | 1278 | struct audit_aux_data *aux; |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1279 | struct audit_names *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1281 | /* tsk == current */ |
Al Viro | 3f2792f | 2006-07-16 06:43:48 -0400 | [diff] [blame] | 1282 | context->personality = tsk->personality; |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1283 | |
| 1284 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | if (!ab) |
| 1286 | return; /* audit_panic has been called */ |
David Woodhouse | bccf6ae | 2005-05-23 21:35:28 +0100 | [diff] [blame] | 1287 | audit_log_format(ab, "arch=%x syscall=%d", |
| 1288 | context->arch, context->major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | if (context->personality != PER_LINUX) |
| 1290 | audit_log_format(ab, " per=%lx", context->personality); |
| 1291 | if (context->return_valid) |
Daniel Walker | 9f8dbe9 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 1292 | audit_log_format(ab, " success=%s exit=%ld", |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1293 | (context->return_valid==AUDITSC_SUCCESS)?"yes":"no", |
| 1294 | context->return_code); |
Alan Cox | eb84a20 | 2006-09-29 02:01:41 -0700 | [diff] [blame] | 1295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | audit_log_format(ab, |
Peter Moody | e23eb92 | 2012-06-14 10:04:35 -0700 | [diff] [blame] | 1297 | " a0=%lx a1=%lx a2=%lx a3=%lx items=%d", |
| 1298 | context->argv[0], |
| 1299 | context->argv[1], |
| 1300 | context->argv[2], |
| 1301 | context->argv[3], |
| 1302 | context->name_count); |
Alan Cox | eb84a20 | 2006-09-29 02:01:41 -0700 | [diff] [blame] | 1303 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1304 | audit_log_task_info(ab, tsk); |
Eric Paris | 9d96098 | 2009-06-11 14:31:37 -0400 | [diff] [blame] | 1305 | audit_log_key(ab, context->filterkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | audit_log_end(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | |
David Woodhouse | 7551ced | 2005-05-26 12:04:57 +0100 | [diff] [blame] | 1308 | for (aux = context->aux; aux; aux = aux->next) { |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1309 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1310 | ab = audit_log_start(context, GFP_KERNEL, aux->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | if (!ab) |
| 1312 | continue; /* audit_panic has been called */ |
| 1313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | switch (aux->type) { |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 1315 | |
Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame] | 1316 | case AUDIT_BPRM_FCAPS: { |
| 1317 | struct audit_aux_data_bprm_fcaps *axs = (void *)aux; |
| 1318 | audit_log_format(ab, "fver=%x", axs->fcap_ver); |
| 1319 | audit_log_cap(ab, "fp", &axs->fcap.permitted); |
| 1320 | audit_log_cap(ab, "fi", &axs->fcap.inheritable); |
| 1321 | audit_log_format(ab, " fe=%d", axs->fcap.fE); |
| 1322 | audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted); |
| 1323 | audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable); |
| 1324 | audit_log_cap(ab, "old_pe", &axs->old_pcap.effective); |
| 1325 | audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted); |
| 1326 | audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable); |
| 1327 | audit_log_cap(ab, "new_pe", &axs->new_pcap.effective); |
| 1328 | break; } |
| 1329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | } |
| 1331 | audit_log_end(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
Al Viro | f3298dc | 2008-12-10 03:16:51 -0500 | [diff] [blame] | 1334 | if (context->type) |
Al Viro | a33e675 | 2008-12-10 03:40:06 -0500 | [diff] [blame] | 1335 | show_special(context, &call_panic); |
Al Viro | f3298dc | 2008-12-10 03:16:51 -0500 | [diff] [blame] | 1336 | |
Al Viro | 157cf64 | 2008-12-14 04:57:47 -0500 | [diff] [blame] | 1337 | if (context->fds[0] >= 0) { |
| 1338 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR); |
| 1339 | if (ab) { |
| 1340 | audit_log_format(ab, "fd0=%d fd1=%d", |
| 1341 | context->fds[0], context->fds[1]); |
| 1342 | audit_log_end(ab); |
| 1343 | } |
| 1344 | } |
| 1345 | |
Al Viro | 4f6b434 | 2008-12-09 19:50:34 -0500 | [diff] [blame] | 1346 | if (context->sockaddr_len) { |
| 1347 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR); |
| 1348 | if (ab) { |
| 1349 | audit_log_format(ab, "saddr="); |
| 1350 | audit_log_n_hex(ab, (void *)context->sockaddr, |
| 1351 | context->sockaddr_len); |
| 1352 | audit_log_end(ab); |
| 1353 | } |
| 1354 | } |
| 1355 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1356 | for (aux = context->aux_pids; aux; aux = aux->next) { |
| 1357 | struct audit_aux_data_pids *axs = (void *)aux; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1358 | |
| 1359 | for (i = 0; i < axs->pid_count; i++) |
| 1360 | if (audit_log_pid_context(context, axs->target_pid[i], |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 1361 | axs->target_auid[i], |
| 1362 | axs->target_uid[i], |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 1363 | axs->target_sessionid[i], |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 1364 | axs->target_sid[i], |
| 1365 | axs->target_comm[i])) |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1366 | call_panic = 1; |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 1367 | } |
| 1368 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1369 | if (context->target_pid && |
| 1370 | audit_log_pid_context(context, context->target_pid, |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 1371 | context->target_auid, context->target_uid, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 1372 | context->target_sessionid, |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 1373 | context->target_sid, context->target_comm)) |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1374 | call_panic = 1; |
| 1375 | |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 1376 | if (context->pwd.dentry && context->pwd.mnt) { |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1377 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD); |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1378 | if (ab) { |
Kees Cook | c158a35 | 2012-01-06 14:07:10 -0800 | [diff] [blame] | 1379 | audit_log_d_path(ab, " cwd=", &context->pwd); |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1380 | audit_log_end(ab); |
| 1381 | } |
| 1382 | } |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1383 | |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1384 | i = 0; |
Jeff Layton | 79f6530 | 2013-07-08 15:59:36 -0700 | [diff] [blame] | 1385 | list_for_each_entry(n, &context->names_list, list) { |
| 1386 | if (n->hidden) |
| 1387 | continue; |
Eric Paris | b24a30a | 2013-04-30 15:30:32 -0400 | [diff] [blame] | 1388 | audit_log_name(context, n, NULL, i++, &call_panic); |
Jeff Layton | 79f6530 | 2013-07-08 15:59:36 -0700 | [diff] [blame] | 1389 | } |
Eric Paris | c0641f2 | 2008-01-07 13:49:15 -0500 | [diff] [blame] | 1390 | |
| 1391 | /* Send end of event record to help user space know we are finished */ |
| 1392 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE); |
| 1393 | if (ab) |
| 1394 | audit_log_end(ab); |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1395 | if (call_panic) |
| 1396 | audit_panic("error converting sid to string"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1399 | /** |
| 1400 | * audit_free - free a per-task audit context |
| 1401 | * @tsk: task whose audit context block to free |
| 1402 | * |
Al Viro | fa84cb9 | 2006-03-29 20:30:19 -0500 | [diff] [blame] | 1403 | * Called from copy_process and do_exit |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1404 | */ |
Eric Paris | a4ff8db | 2012-01-03 14:23:07 -0500 | [diff] [blame] | 1405 | void __audit_free(struct task_struct *tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | { |
| 1407 | struct audit_context *context; |
| 1408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | context = audit_get_context(tsk, 0, 0); |
Eric Paris | 56179a6 | 2012-01-03 14:23:06 -0500 | [diff] [blame] | 1410 | if (!context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | return; |
| 1412 | |
| 1413 | /* Check for system calls that do not go through the exit |
Daniel Walker | 9f8dbe9 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 1414 | * function (e.g., exit_group), then free context block. |
| 1415 | * We use GFP_ATOMIC here because we might be doing this |
David Woodhouse | f556196 | 2005-07-13 22:47:07 +0100 | [diff] [blame] | 1416 | * in the context of the idle thread */ |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1417 | /* that can happen only if we are called from do_exit() */ |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 1418 | if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT) |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1419 | audit_log_exit(context, tsk); |
Al Viro | 916d757 | 2009-06-24 00:02:38 -0400 | [diff] [blame] | 1420 | if (!list_empty(&context->killed_trees)) |
| 1421 | audit_kill_trees(&context->killed_trees); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | |
| 1423 | audit_free_context(context); |
| 1424 | } |
| 1425 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1426 | /** |
| 1427 | * audit_syscall_entry - fill in an audit record at syscall entry |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1428 | * @arch: architecture type |
| 1429 | * @major: major syscall type (function) |
| 1430 | * @a1: additional syscall register 1 |
| 1431 | * @a2: additional syscall register 2 |
| 1432 | * @a3: additional syscall register 3 |
| 1433 | * @a4: additional syscall register 4 |
| 1434 | * |
| 1435 | * Fill in audit context at syscall entry. This only happens if the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | * audit context was created when the task was created and the state or |
| 1437 | * filters demand the audit context be built. If the state from the |
| 1438 | * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT, |
| 1439 | * then the record will be written at syscall exit time (otherwise, it |
| 1440 | * 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] | 1441 | * be written). |
| 1442 | */ |
Eric Paris | b05d844 | 2012-01-03 14:23:06 -0500 | [diff] [blame] | 1443 | void __audit_syscall_entry(int arch, int major, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | unsigned long a1, unsigned long a2, |
| 1445 | unsigned long a3, unsigned long a4) |
| 1446 | { |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 1447 | struct task_struct *tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | struct audit_context *context = tsk->audit_context; |
| 1449 | enum audit_state state; |
| 1450 | |
Eric Paris | 56179a6 | 2012-01-03 14:23:06 -0500 | [diff] [blame] | 1451 | if (!context) |
Roland McGrath | 86a1c34 | 2008-06-23 15:37:04 -0700 | [diff] [blame] | 1452 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | BUG_ON(context->in_syscall || context->name_count); |
| 1455 | |
| 1456 | if (!audit_enabled) |
| 1457 | return; |
| 1458 | |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1459 | context->arch = arch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | context->major = major; |
| 1461 | context->argv[0] = a1; |
| 1462 | context->argv[1] = a2; |
| 1463 | context->argv[2] = a3; |
| 1464 | context->argv[3] = a4; |
| 1465 | |
| 1466 | state = context->state; |
Al Viro | d51374a | 2006-08-03 10:59:26 -0400 | [diff] [blame] | 1467 | context->dummy = !audit_n_rules; |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 1468 | if (!context->dummy && state == AUDIT_BUILD_CONTEXT) { |
| 1469 | context->prio = 0; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 1470 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]); |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 1471 | } |
Eric Paris | 56179a6 | 2012-01-03 14:23:06 -0500 | [diff] [blame] | 1472 | if (state == AUDIT_DISABLED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | return; |
| 1474 | |
David Woodhouse | ce625a8 | 2005-07-18 14:24:46 -0400 | [diff] [blame] | 1475 | context->serial = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | context->ctime = CURRENT_TIME; |
| 1477 | context->in_syscall = 1; |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 1478 | context->current_state = state; |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 1479 | context->ppid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1482 | /** |
| 1483 | * audit_syscall_exit - deallocate audit context after a system call |
Randy Dunlap | 42ae610c | 2012-01-21 11:02:24 -0800 | [diff] [blame] | 1484 | * @success: success value of the syscall |
| 1485 | * @return_code: return value of the syscall |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1486 | * |
| 1487 | * 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] | 1488 | * auditable (either because of the AUDIT_RECORD_CONTEXT state from |
Randy Dunlap | 42ae610c | 2012-01-21 11:02:24 -0800 | [diff] [blame] | 1489 | * filtering, or because some other part of the kernel wrote an audit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | * message), then write out the syscall information. In call cases, |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1491 | * free the names stored from getname(). |
| 1492 | */ |
Eric Paris | d7e7528 | 2012-01-03 14:23:06 -0500 | [diff] [blame] | 1493 | void __audit_syscall_exit(int success, long return_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | { |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 1495 | struct task_struct *tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | struct audit_context *context; |
| 1497 | |
Eric Paris | d7e7528 | 2012-01-03 14:23:06 -0500 | [diff] [blame] | 1498 | if (success) |
| 1499 | success = AUDITSC_SUCCESS; |
| 1500 | else |
| 1501 | success = AUDITSC_FAILURE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | |
Eric Paris | d7e7528 | 2012-01-03 14:23:06 -0500 | [diff] [blame] | 1503 | context = audit_get_context(tsk, success, return_code); |
Eric Paris | 56179a6 | 2012-01-03 14:23:06 -0500 | [diff] [blame] | 1504 | if (!context) |
Al Viro | 97e94c4 | 2006-03-29 20:26:24 -0500 | [diff] [blame] | 1505 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 1507 | if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT) |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1508 | audit_log_exit(context, tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | |
| 1510 | context->in_syscall = 0; |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 1511 | context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1512 | |
Al Viro | 916d757 | 2009-06-24 00:02:38 -0400 | [diff] [blame] | 1513 | if (!list_empty(&context->killed_trees)) |
| 1514 | audit_kill_trees(&context->killed_trees); |
| 1515 | |
Al Viro | c62d773 | 2012-10-20 15:07:18 -0400 | [diff] [blame] | 1516 | audit_free_names(context); |
| 1517 | unroll_tree_refs(context, NULL, 0); |
| 1518 | audit_free_aux(context); |
| 1519 | context->aux = NULL; |
| 1520 | context->aux_pids = NULL; |
| 1521 | context->target_pid = 0; |
| 1522 | context->target_sid = 0; |
| 1523 | context->sockaddr_len = 0; |
| 1524 | context->type = 0; |
| 1525 | context->fds[0] = -1; |
| 1526 | if (context->state != AUDIT_RECORD_CONTEXT) { |
| 1527 | kfree(context->filterkey); |
| 1528 | context->filterkey = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | } |
Al Viro | c62d773 | 2012-10-20 15:07:18 -0400 | [diff] [blame] | 1530 | tsk->audit_context = context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1533 | static inline void handle_one(const struct inode *inode) |
| 1534 | { |
| 1535 | #ifdef CONFIG_AUDIT_TREE |
| 1536 | struct audit_context *context; |
| 1537 | struct audit_tree_refs *p; |
| 1538 | struct audit_chunk *chunk; |
| 1539 | int count; |
Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 1540 | if (likely(hlist_empty(&inode->i_fsnotify_marks))) |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1541 | return; |
| 1542 | context = current->audit_context; |
| 1543 | p = context->trees; |
| 1544 | count = context->tree_count; |
| 1545 | rcu_read_lock(); |
| 1546 | chunk = audit_tree_lookup(inode); |
| 1547 | rcu_read_unlock(); |
| 1548 | if (!chunk) |
| 1549 | return; |
| 1550 | if (likely(put_tree_ref(context, chunk))) |
| 1551 | return; |
| 1552 | if (unlikely(!grow_tree_refs(context))) { |
Eric Paris | 436c405 | 2008-04-18 10:01:04 -0400 | [diff] [blame] | 1553 | printk(KERN_WARNING "out of memory, audit has lost a tree reference\n"); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1554 | audit_set_auditable(context); |
| 1555 | audit_put_chunk(chunk); |
| 1556 | unroll_tree_refs(context, p, count); |
| 1557 | return; |
| 1558 | } |
| 1559 | put_tree_ref(context, chunk); |
| 1560 | #endif |
| 1561 | } |
| 1562 | |
| 1563 | static void handle_path(const struct dentry *dentry) |
| 1564 | { |
| 1565 | #ifdef CONFIG_AUDIT_TREE |
| 1566 | struct audit_context *context; |
| 1567 | struct audit_tree_refs *p; |
| 1568 | const struct dentry *d, *parent; |
| 1569 | struct audit_chunk *drop; |
| 1570 | unsigned long seq; |
| 1571 | int count; |
| 1572 | |
| 1573 | context = current->audit_context; |
| 1574 | p = context->trees; |
| 1575 | count = context->tree_count; |
| 1576 | retry: |
| 1577 | drop = NULL; |
| 1578 | d = dentry; |
| 1579 | rcu_read_lock(); |
| 1580 | seq = read_seqbegin(&rename_lock); |
| 1581 | for(;;) { |
| 1582 | struct inode *inode = d->d_inode; |
Eric Paris | e61ce86 | 2009-12-17 21:24:24 -0500 | [diff] [blame] | 1583 | if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) { |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1584 | struct audit_chunk *chunk; |
| 1585 | chunk = audit_tree_lookup(inode); |
| 1586 | if (chunk) { |
| 1587 | if (unlikely(!put_tree_ref(context, chunk))) { |
| 1588 | drop = chunk; |
| 1589 | break; |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | parent = d->d_parent; |
| 1594 | if (parent == d) |
| 1595 | break; |
| 1596 | d = parent; |
| 1597 | } |
| 1598 | if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */ |
| 1599 | rcu_read_unlock(); |
| 1600 | if (!drop) { |
| 1601 | /* just a race with rename */ |
| 1602 | unroll_tree_refs(context, p, count); |
| 1603 | goto retry; |
| 1604 | } |
| 1605 | audit_put_chunk(drop); |
| 1606 | if (grow_tree_refs(context)) { |
| 1607 | /* OK, got more space */ |
| 1608 | unroll_tree_refs(context, p, count); |
| 1609 | goto retry; |
| 1610 | } |
| 1611 | /* too bad */ |
| 1612 | printk(KERN_WARNING |
Eric Paris | 436c405 | 2008-04-18 10:01:04 -0400 | [diff] [blame] | 1613 | "out of memory, audit has lost a tree reference\n"); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1614 | unroll_tree_refs(context, p, count); |
| 1615 | audit_set_auditable(context); |
| 1616 | return; |
| 1617 | } |
| 1618 | rcu_read_unlock(); |
| 1619 | #endif |
| 1620 | } |
| 1621 | |
Jeff Layton | 78e2e80 | 2012-10-10 15:25:22 -0400 | [diff] [blame] | 1622 | static struct audit_names *audit_alloc_name(struct audit_context *context, |
| 1623 | unsigned char type) |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1624 | { |
| 1625 | struct audit_names *aname; |
| 1626 | |
| 1627 | if (context->name_count < AUDIT_NAMES) { |
| 1628 | aname = &context->preallocated_names[context->name_count]; |
| 1629 | memset(aname, 0, sizeof(*aname)); |
| 1630 | } else { |
| 1631 | aname = kzalloc(sizeof(*aname), GFP_NOFS); |
| 1632 | if (!aname) |
| 1633 | return NULL; |
| 1634 | aname->should_free = true; |
| 1635 | } |
| 1636 | |
| 1637 | aname->ino = (unsigned long)-1; |
Jeff Layton | 78e2e80 | 2012-10-10 15:25:22 -0400 | [diff] [blame] | 1638 | aname->type = type; |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1639 | list_add_tail(&aname->list, &context->names_list); |
| 1640 | |
| 1641 | context->name_count++; |
| 1642 | #if AUDIT_DEBUG |
| 1643 | context->ino_count++; |
| 1644 | #endif |
| 1645 | return aname; |
| 1646 | } |
| 1647 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1648 | /** |
Jeff Layton | 7ac8626 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 1649 | * audit_reusename - fill out filename with info from existing entry |
| 1650 | * @uptr: userland ptr to pathname |
| 1651 | * |
| 1652 | * Search the audit_names list for the current audit context. If there is an |
| 1653 | * existing entry with a matching "uptr" then return the filename |
| 1654 | * associated with that audit_name. If not, return NULL. |
| 1655 | */ |
| 1656 | struct filename * |
| 1657 | __audit_reusename(const __user char *uptr) |
| 1658 | { |
| 1659 | struct audit_context *context = current->audit_context; |
| 1660 | struct audit_names *n; |
| 1661 | |
| 1662 | list_for_each_entry(n, &context->names_list, list) { |
| 1663 | if (!n->name) |
| 1664 | continue; |
| 1665 | if (n->name->uptr == uptr) |
| 1666 | return n->name; |
| 1667 | } |
| 1668 | return NULL; |
| 1669 | } |
| 1670 | |
| 1671 | /** |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1672 | * audit_getname - add a name to the list |
| 1673 | * @name: name to add |
| 1674 | * |
| 1675 | * Add a name to the list of audit names for this context. |
| 1676 | * Called from fs/namei.c:getname(). |
| 1677 | */ |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 1678 | void __audit_getname(struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | { |
| 1680 | struct audit_context *context = current->audit_context; |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1681 | struct audit_names *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | if (!context->in_syscall) { |
| 1684 | #if AUDIT_DEBUG == 2 |
| 1685 | printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n", |
| 1686 | __FILE__, __LINE__, context->serial, name); |
| 1687 | dump_stack(); |
| 1688 | #endif |
| 1689 | return; |
| 1690 | } |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1691 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 1692 | #if AUDIT_DEBUG |
| 1693 | /* The filename _must_ have a populated ->name */ |
| 1694 | BUG_ON(!name->name); |
| 1695 | #endif |
| 1696 | |
Jeff Layton | 78e2e80 | 2012-10-10 15:25:22 -0400 | [diff] [blame] | 1697 | n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN); |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1698 | if (!n) |
| 1699 | return; |
| 1700 | |
| 1701 | n->name = name; |
| 1702 | n->name_len = AUDIT_NAME_FULL; |
| 1703 | n->name_put = true; |
Jeff Layton | adb5c24 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 1704 | name->aname = n; |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1705 | |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 1706 | if (!context->pwd.dentry) |
| 1707 | get_fs_pwd(current->fs, &context->pwd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | } |
| 1709 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1710 | /* audit_putname - intercept a putname request |
| 1711 | * @name: name to intercept and delay for putname |
| 1712 | * |
| 1713 | * If we have stored the name from getname in the audit context, |
| 1714 | * then we delay the putname until syscall exit. |
| 1715 | * Called from include/linux/fs.h:putname(). |
| 1716 | */ |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 1717 | void audit_putname(struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | { |
| 1719 | struct audit_context *context = current->audit_context; |
| 1720 | |
| 1721 | BUG_ON(!context); |
| 1722 | if (!context->in_syscall) { |
| 1723 | #if AUDIT_DEBUG == 2 |
Dmitry Monakhov | 65ada7b | 2013-04-01 11:00:00 +0400 | [diff] [blame] | 1724 | printk(KERN_ERR "%s:%d(:%d): final_putname(%p)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | __FILE__, __LINE__, context->serial, name); |
| 1726 | if (context->name_count) { |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1727 | struct audit_names *n; |
Eric Paris | 34c474d | 2013-04-16 10:17:02 -0400 | [diff] [blame] | 1728 | int i = 0; |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1729 | |
| 1730 | list_for_each_entry(n, &context->names_list, list) |
Eric Paris | 34c474d | 2013-04-16 10:17:02 -0400 | [diff] [blame] | 1731 | printk(KERN_ERR "name[%d] = %p = %s\n", i++, |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 1732 | n->name, n->name->name ?: "(null)"); |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1733 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | #endif |
Dmitry Monakhov | 65ada7b | 2013-04-01 11:00:00 +0400 | [diff] [blame] | 1735 | final_putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | } |
| 1737 | #if AUDIT_DEBUG |
| 1738 | else { |
| 1739 | ++context->put_count; |
| 1740 | if (context->put_count > context->name_count) { |
| 1741 | printk(KERN_ERR "%s:%d(:%d): major=%d" |
| 1742 | " in_syscall=%d putname(%p) name_count=%d" |
| 1743 | " put_count=%d\n", |
| 1744 | __FILE__, __LINE__, |
| 1745 | context->serial, context->major, |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 1746 | context->in_syscall, name->name, |
| 1747 | context->name_count, context->put_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | dump_stack(); |
| 1749 | } |
| 1750 | } |
| 1751 | #endif |
| 1752 | } |
| 1753 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1754 | /** |
Jeff Layton | bfcec70 | 2012-10-10 15:25:23 -0400 | [diff] [blame] | 1755 | * __audit_inode - store the inode and device from a lookup |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1756 | * @name: name being audited |
Randy Dunlap | 481968f | 2007-10-21 20:59:53 -0700 | [diff] [blame] | 1757 | * @dentry: dentry being audited |
Jeff Layton | 79f6530 | 2013-07-08 15:59:36 -0700 | [diff] [blame] | 1758 | * @flags: attributes for this particular entry |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1759 | */ |
Jeff Layton | adb5c24 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 1760 | void __audit_inode(struct filename *name, const struct dentry *dentry, |
Jeff Layton | 79f6530 | 2013-07-08 15:59:36 -0700 | [diff] [blame] | 1761 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | struct audit_context *context = current->audit_context; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1764 | const struct inode *inode = dentry->d_inode; |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1765 | struct audit_names *n; |
Jeff Layton | 79f6530 | 2013-07-08 15:59:36 -0700 | [diff] [blame] | 1766 | bool parent = flags & AUDIT_INODE_PARENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | |
| 1768 | if (!context->in_syscall) |
| 1769 | return; |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1770 | |
Jeff Layton | 9cec9d6 | 2012-10-10 15:25:21 -0400 | [diff] [blame] | 1771 | if (!name) |
| 1772 | goto out_alloc; |
| 1773 | |
Jeff Layton | adb5c24 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 1774 | #if AUDIT_DEBUG |
| 1775 | /* The struct filename _must_ have a populated ->name */ |
| 1776 | BUG_ON(!name->name); |
| 1777 | #endif |
| 1778 | /* |
| 1779 | * If we have a pointer to an audit_names entry already, then we can |
| 1780 | * just use it directly if the type is correct. |
| 1781 | */ |
| 1782 | n = name->aname; |
| 1783 | if (n) { |
| 1784 | if (parent) { |
| 1785 | if (n->type == AUDIT_TYPE_PARENT || |
| 1786 | n->type == AUDIT_TYPE_UNKNOWN) |
| 1787 | goto out; |
| 1788 | } else { |
| 1789 | if (n->type != AUDIT_TYPE_PARENT) |
| 1790 | goto out; |
| 1791 | } |
| 1792 | } |
| 1793 | |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1794 | list_for_each_entry_reverse(n, &context->names_list, list) { |
Jeff Layton | bfcec70 | 2012-10-10 15:25:23 -0400 | [diff] [blame] | 1795 | /* does the name pointer match? */ |
Jeff Layton | adb5c24 | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 1796 | if (!n->name || n->name->name != name->name) |
Jeff Layton | bfcec70 | 2012-10-10 15:25:23 -0400 | [diff] [blame] | 1797 | continue; |
| 1798 | |
| 1799 | /* match the correct record type */ |
| 1800 | if (parent) { |
| 1801 | if (n->type == AUDIT_TYPE_PARENT || |
| 1802 | n->type == AUDIT_TYPE_UNKNOWN) |
| 1803 | goto out; |
| 1804 | } else { |
| 1805 | if (n->type != AUDIT_TYPE_PARENT) |
| 1806 | goto out; |
| 1807 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | } |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1809 | |
Jeff Layton | 9cec9d6 | 2012-10-10 15:25:21 -0400 | [diff] [blame] | 1810 | out_alloc: |
Jeff Layton | bfcec70 | 2012-10-10 15:25:23 -0400 | [diff] [blame] | 1811 | /* unable to find the name from a previous getname(). Allocate a new |
| 1812 | * anonymous entry. |
| 1813 | */ |
Jeff Layton | 78e2e80 | 2012-10-10 15:25:22 -0400 | [diff] [blame] | 1814 | n = audit_alloc_name(context, AUDIT_TYPE_NORMAL); |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1815 | if (!n) |
| 1816 | return; |
| 1817 | out: |
Jeff Layton | bfcec70 | 2012-10-10 15:25:23 -0400 | [diff] [blame] | 1818 | if (parent) { |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 1819 | n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL; |
Jeff Layton | bfcec70 | 2012-10-10 15:25:23 -0400 | [diff] [blame] | 1820 | n->type = AUDIT_TYPE_PARENT; |
Jeff Layton | 79f6530 | 2013-07-08 15:59:36 -0700 | [diff] [blame] | 1821 | if (flags & AUDIT_INODE_HIDDEN) |
| 1822 | n->hidden = true; |
Jeff Layton | bfcec70 | 2012-10-10 15:25:23 -0400 | [diff] [blame] | 1823 | } else { |
| 1824 | n->name_len = AUDIT_NAME_FULL; |
| 1825 | n->type = AUDIT_TYPE_NORMAL; |
| 1826 | } |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1827 | handle_path(dentry); |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1828 | audit_copy_inode(n, dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | } |
| 1830 | |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1831 | /** |
Jeff Layton | c43a25a | 2012-10-10 15:25:21 -0400 | [diff] [blame] | 1832 | * __audit_inode_child - collect inode info for created/removed objects |
Amy Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 1833 | * @parent: inode of dentry parent |
Jeff Layton | c43a25a | 2012-10-10 15:25:21 -0400 | [diff] [blame] | 1834 | * @dentry: dentry being audited |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1835 | * @type: AUDIT_TYPE_* value that we're looking for |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1836 | * |
| 1837 | * For syscalls that create or remove filesystem objects, audit_inode |
| 1838 | * can only collect information for the filesystem object's parent. |
| 1839 | * This call updates the audit context with the child's information. |
| 1840 | * Syscalls that create a new filesystem object must be hooked after |
| 1841 | * the object is created. Syscalls that remove a filesystem object |
| 1842 | * must be hooked prior, in order to capture the target inode during |
| 1843 | * unsuccessful attempts. |
| 1844 | */ |
Jeff Layton | c43a25a | 2012-10-10 15:25:21 -0400 | [diff] [blame] | 1845 | void __audit_inode_child(const struct inode *parent, |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1846 | const struct dentry *dentry, |
| 1847 | const unsigned char type) |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1848 | { |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1849 | struct audit_context *context = current->audit_context; |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 1850 | const struct inode *inode = dentry->d_inode; |
Al Viro | cccc6bb | 2009-12-25 05:07:33 -0500 | [diff] [blame] | 1851 | const char *dname = dentry->d_name.name; |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1852 | struct audit_names *n, *found_parent = NULL, *found_child = NULL; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1853 | |
| 1854 | if (!context->in_syscall) |
| 1855 | return; |
| 1856 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1857 | if (inode) |
| 1858 | handle_one(inode); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1859 | |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1860 | /* look for a parent entry first */ |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1861 | list_for_each_entry(n, &context->names_list, list) { |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1862 | if (!n->name || n->type != AUDIT_TYPE_PARENT) |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1863 | continue; |
| 1864 | |
| 1865 | if (n->ino == parent->i_ino && |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 1866 | !audit_compare_dname_path(dname, n->name->name, n->name_len)) { |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1867 | found_parent = n; |
| 1868 | break; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1869 | } |
Steve Grubb | ac9910c | 2006-09-28 14:31:32 -0400 | [diff] [blame] | 1870 | } |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1871 | |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1872 | /* is there a matching child entry? */ |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1873 | list_for_each_entry(n, &context->names_list, list) { |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1874 | /* can only match entries that have a name */ |
| 1875 | if (!n->name || n->type != type) |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1876 | continue; |
| 1877 | |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1878 | /* if we found a parent, make sure this one is a child of it */ |
| 1879 | if (found_parent && (n->name != found_parent->name)) |
| 1880 | continue; |
| 1881 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 1882 | if (!strcmp(dname, n->name->name) || |
| 1883 | !audit_compare_dname_path(dname, n->name->name, |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1884 | found_parent ? |
| 1885 | found_parent->name_len : |
Jeff Layton | e3d6b07 | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1886 | AUDIT_NAME_FULL)) { |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1887 | found_child = n; |
| 1888 | break; |
Steve Grubb | ac9910c | 2006-09-28 14:31:32 -0400 | [diff] [blame] | 1889 | } |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1890 | } |
| 1891 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1892 | if (!found_parent) { |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1893 | /* create a new, "anonymous" parent record */ |
| 1894 | n = audit_alloc_name(context, AUDIT_TYPE_PARENT); |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1895 | if (!n) |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1896 | return; |
Eric Paris | 5195d8e | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 1897 | audit_copy_inode(n, NULL, parent); |
Amy Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 1898 | } |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1899 | |
| 1900 | if (!found_child) { |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1901 | found_child = audit_alloc_name(context, type); |
| 1902 | if (!found_child) |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1903 | return; |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1904 | |
| 1905 | /* Re-use the name belonging to the slot for a matching parent |
| 1906 | * directory. All names for this context are relinquished in |
| 1907 | * audit_free_names() */ |
| 1908 | if (found_parent) { |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1909 | found_child->name = found_parent->name; |
| 1910 | found_child->name_len = AUDIT_NAME_FULL; |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1911 | /* don't call __putname() */ |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1912 | found_child->name_put = false; |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1913 | } |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1914 | } |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 1915 | if (inode) |
| 1916 | audit_copy_inode(found_child, dentry, inode); |
| 1917 | else |
| 1918 | found_child->ino = (unsigned long)-1; |
Amy Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 1919 | } |
Trond Myklebust | 50e437d | 2007-06-07 22:44:34 -0400 | [diff] [blame] | 1920 | EXPORT_SYMBOL_GPL(__audit_inode_child); |
Amy Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 1921 | |
| 1922 | /** |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1923 | * auditsc_get_stamp - get local copies of audit_context values |
| 1924 | * @ctx: audit_context for the task |
| 1925 | * @t: timespec to store time recorded in the audit_context |
| 1926 | * @serial: serial value that is recorded in the audit_context |
| 1927 | * |
| 1928 | * Also sets the context as auditable. |
| 1929 | */ |
Al Viro | 48887e6 | 2008-12-06 01:05:50 -0500 | [diff] [blame] | 1930 | int auditsc_get_stamp(struct audit_context *ctx, |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1931 | struct timespec *t, unsigned int *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | { |
Al Viro | 48887e6 | 2008-12-06 01:05:50 -0500 | [diff] [blame] | 1933 | if (!ctx->in_syscall) |
| 1934 | return 0; |
David Woodhouse | ce625a8 | 2005-07-18 14:24:46 -0400 | [diff] [blame] | 1935 | if (!ctx->serial) |
| 1936 | ctx->serial = audit_serial(); |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1937 | t->tv_sec = ctx->ctime.tv_sec; |
| 1938 | t->tv_nsec = ctx->ctime.tv_nsec; |
| 1939 | *serial = ctx->serial; |
Al Viro | 0590b93 | 2008-12-14 23:45:27 -0500 | [diff] [blame] | 1940 | if (!ctx->prio) { |
| 1941 | ctx->prio = 1; |
| 1942 | ctx->current_state = AUDIT_RECORD_CONTEXT; |
| 1943 | } |
Al Viro | 48887e6 | 2008-12-06 01:05:50 -0500 | [diff] [blame] | 1944 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | } |
| 1946 | |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 1947 | /* global counter which is incremented every time something logs in */ |
| 1948 | static atomic_t session_id = ATOMIC_INIT(0); |
| 1949 | |
Eric Paris | da0a610 | 2013-05-24 08:58:31 -0400 | [diff] [blame] | 1950 | static int audit_set_loginuid_perm(kuid_t loginuid) |
| 1951 | { |
Eric Paris | da0a610 | 2013-05-24 08:58:31 -0400 | [diff] [blame] | 1952 | /* if we are unset, we don't need privs */ |
| 1953 | if (!audit_loginuid_set(current)) |
| 1954 | return 0; |
Eric Paris | 21b85c3 | 2013-05-23 14:26:00 -0400 | [diff] [blame] | 1955 | /* if AUDIT_FEATURE_LOGINUID_IMMUTABLE means never ever allow a change*/ |
| 1956 | if (is_audit_feature_set(AUDIT_FEATURE_LOGINUID_IMMUTABLE)) |
| 1957 | return -EPERM; |
Eric Paris | 83fa6bb | 2013-05-24 09:39:29 -0400 | [diff] [blame] | 1958 | /* it is set, you need permission */ |
| 1959 | if (!capable(CAP_AUDIT_CONTROL)) |
| 1960 | return -EPERM; |
Eric Paris | d040e5a | 2013-05-24 09:18:04 -0400 | [diff] [blame] | 1961 | /* reject if this is not an unset and we don't allow that */ |
| 1962 | if (is_audit_feature_set(AUDIT_FEATURE_ONLY_UNSET_LOGINUID) && uid_valid(loginuid)) |
| 1963 | return -EPERM; |
Eric Paris | 83fa6bb | 2013-05-24 09:39:29 -0400 | [diff] [blame] | 1964 | return 0; |
Eric Paris | da0a610 | 2013-05-24 08:58:31 -0400 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | static void audit_log_set_loginuid(kuid_t koldloginuid, kuid_t kloginuid, |
| 1968 | unsigned int oldsessionid, unsigned int sessionid, |
| 1969 | int rc) |
| 1970 | { |
| 1971 | struct audit_buffer *ab; |
| 1972 | uid_t uid, ologinuid, nloginuid; |
| 1973 | |
| 1974 | uid = from_kuid(&init_user_ns, task_uid(current)); |
| 1975 | ologinuid = from_kuid(&init_user_ns, koldloginuid); |
| 1976 | nloginuid = from_kuid(&init_user_ns, kloginuid), |
| 1977 | |
| 1978 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN); |
| 1979 | if (!ab) |
| 1980 | return; |
| 1981 | audit_log_format(ab, "pid=%d uid=%u old auid=%u new auid=%u old " |
| 1982 | "ses=%u new ses=%u res=%d", current->pid, uid, ologinuid, |
| 1983 | nloginuid, oldsessionid, sessionid, !rc); |
| 1984 | audit_log_end(ab); |
| 1985 | } |
| 1986 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1987 | /** |
Eric Paris | 0a300be | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 1988 | * audit_set_loginuid - set current task's audit_context loginuid |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1989 | * @loginuid: loginuid value |
| 1990 | * |
| 1991 | * Returns 0. |
| 1992 | * |
| 1993 | * Called (set) from fs/proc/base.c::proc_loginuid_write(). |
| 1994 | */ |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 1995 | int audit_set_loginuid(kuid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | { |
Eric Paris | 0a300be | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 1997 | struct task_struct *task = current; |
Eric Paris | 9175c9d | 2013-11-06 10:47:17 -0500 | [diff] [blame] | 1998 | unsigned int oldsessionid, sessionid = (unsigned int)-1; |
| 1999 | kuid_t oldloginuid; |
Eric Paris | da0a610 | 2013-05-24 08:58:31 -0400 | [diff] [blame] | 2000 | int rc; |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 2001 | |
Eric Paris | da0a610 | 2013-05-24 08:58:31 -0400 | [diff] [blame] | 2002 | oldloginuid = audit_get_loginuid(current); |
| 2003 | oldsessionid = audit_get_sessionid(current); |
| 2004 | |
| 2005 | rc = audit_set_loginuid_perm(loginuid); |
| 2006 | if (rc) |
| 2007 | goto out; |
Eric Paris | 633b454 | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 2008 | |
Eric Paris | 81407c8 | 2013-05-24 09:49:14 -0400 | [diff] [blame] | 2009 | /* are we setting or clearing? */ |
| 2010 | if (uid_valid(loginuid)) |
| 2011 | sessionid = atomic_inc_return(&session_id); |
Steve Grubb | 4175710 | 2006-06-12 07:48:28 -0400 | [diff] [blame] | 2012 | |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2013 | task->sessionid = sessionid; |
Al Viro | bfef93a | 2008-01-10 04:53:18 -0500 | [diff] [blame] | 2014 | task->loginuid = loginuid; |
Eric Paris | da0a610 | 2013-05-24 08:58:31 -0400 | [diff] [blame] | 2015 | out: |
| 2016 | audit_log_set_loginuid(oldloginuid, loginuid, oldsessionid, sessionid, rc); |
| 2017 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | } |
| 2019 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2020 | /** |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2021 | * __audit_mq_open - record audit data for a POSIX MQ open |
| 2022 | * @oflag: open flag |
| 2023 | * @mode: mode bits |
Randy Dunlap | 6b96255 | 2009-01-05 13:41:13 -0800 | [diff] [blame] | 2024 | * @attr: queue attributes |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2025 | * |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2026 | */ |
Al Viro | df0a428 | 2011-07-26 05:26:10 -0400 | [diff] [blame] | 2027 | void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr) |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2028 | { |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2029 | struct audit_context *context = current->audit_context; |
| 2030 | |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 2031 | if (attr) |
| 2032 | memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr)); |
| 2033 | else |
| 2034 | memset(&context->mq_open.attr, 0, sizeof(struct mq_attr)); |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2035 | |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 2036 | context->mq_open.oflag = oflag; |
| 2037 | context->mq_open.mode = mode; |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2038 | |
Al Viro | 564f699 | 2008-12-14 04:02:26 -0500 | [diff] [blame] | 2039 | context->type = AUDIT_MQ_OPEN; |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | /** |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 2043 | * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2044 | * @mqdes: MQ descriptor |
| 2045 | * @msg_len: Message length |
| 2046 | * @msg_prio: Message priority |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 2047 | * @abs_timeout: Message timeout in absolute time |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2048 | * |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2049 | */ |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 2050 | void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, |
| 2051 | const struct timespec *abs_timeout) |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2052 | { |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2053 | struct audit_context *context = current->audit_context; |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 2054 | struct timespec *p = &context->mq_sendrecv.abs_timeout; |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2055 | |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 2056 | if (abs_timeout) |
| 2057 | memcpy(p, abs_timeout, sizeof(struct timespec)); |
| 2058 | else |
| 2059 | memset(p, 0, sizeof(struct timespec)); |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2060 | |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 2061 | context->mq_sendrecv.mqdes = mqdes; |
| 2062 | context->mq_sendrecv.msg_len = msg_len; |
| 2063 | context->mq_sendrecv.msg_prio = msg_prio; |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2064 | |
Al Viro | c32c8af | 2008-12-14 03:46:48 -0500 | [diff] [blame] | 2065 | context->type = AUDIT_MQ_SENDRECV; |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | /** |
| 2069 | * __audit_mq_notify - record audit data for a POSIX MQ notify |
| 2070 | * @mqdes: MQ descriptor |
Randy Dunlap | 6b96255 | 2009-01-05 13:41:13 -0800 | [diff] [blame] | 2071 | * @notification: Notification event |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2072 | * |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2073 | */ |
| 2074 | |
Al Viro | 20114f7 | 2008-12-10 07:16:12 -0500 | [diff] [blame] | 2075 | void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification) |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2076 | { |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2077 | struct audit_context *context = current->audit_context; |
| 2078 | |
Al Viro | 20114f7 | 2008-12-10 07:16:12 -0500 | [diff] [blame] | 2079 | if (notification) |
| 2080 | context->mq_notify.sigev_signo = notification->sigev_signo; |
| 2081 | else |
| 2082 | context->mq_notify.sigev_signo = 0; |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2083 | |
Al Viro | 20114f7 | 2008-12-10 07:16:12 -0500 | [diff] [blame] | 2084 | context->mq_notify.mqdes = mqdes; |
| 2085 | context->type = AUDIT_MQ_NOTIFY; |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | /** |
| 2089 | * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute |
| 2090 | * @mqdes: MQ descriptor |
| 2091 | * @mqstat: MQ flags |
| 2092 | * |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2093 | */ |
Al Viro | 7392906 | 2008-12-10 06:58:59 -0500 | [diff] [blame] | 2094 | void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat) |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2095 | { |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2096 | struct audit_context *context = current->audit_context; |
Al Viro | 7392906 | 2008-12-10 06:58:59 -0500 | [diff] [blame] | 2097 | context->mq_getsetattr.mqdes = mqdes; |
| 2098 | context->mq_getsetattr.mqstat = *mqstat; |
| 2099 | context->type = AUDIT_MQ_GETSETATTR; |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | /** |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 2103 | * audit_ipc_obj - record audit data for ipc object |
| 2104 | * @ipcp: ipc permissions |
| 2105 | * |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 2106 | */ |
Al Viro | a33e675 | 2008-12-10 03:40:06 -0500 | [diff] [blame] | 2107 | void __audit_ipc_obj(struct kern_ipc_perm *ipcp) |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 2108 | { |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 2109 | struct audit_context *context = current->audit_context; |
Al Viro | a33e675 | 2008-12-10 03:40:06 -0500 | [diff] [blame] | 2110 | context->ipc.uid = ipcp->uid; |
| 2111 | context->ipc.gid = ipcp->gid; |
| 2112 | context->ipc.mode = ipcp->mode; |
Al Viro | e816f37 | 2008-12-10 03:47:15 -0500 | [diff] [blame] | 2113 | context->ipc.has_perm = 0; |
Al Viro | a33e675 | 2008-12-10 03:40:06 -0500 | [diff] [blame] | 2114 | security_ipc_getsecid(ipcp, &context->ipc.osid); |
| 2115 | context->type = AUDIT_IPC; |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | /** |
| 2119 | * audit_ipc_set_perm - record audit data for new ipc permissions |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2120 | * @qbytes: msgq bytes |
| 2121 | * @uid: msgq user id |
| 2122 | * @gid: msgq group id |
| 2123 | * @mode: msgq mode (permissions) |
| 2124 | * |
Al Viro | e816f37 | 2008-12-10 03:47:15 -0500 | [diff] [blame] | 2125 | * Called only after audit_ipc_obj(). |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2126 | */ |
Al Viro | 2570ebb | 2011-07-27 14:03:22 -0400 | [diff] [blame] | 2127 | void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | struct audit_context *context = current->audit_context; |
| 2130 | |
Al Viro | e816f37 | 2008-12-10 03:47:15 -0500 | [diff] [blame] | 2131 | context->ipc.qbytes = qbytes; |
| 2132 | context->ipc.perm_uid = uid; |
| 2133 | context->ipc.perm_gid = gid; |
| 2134 | context->ipc.perm_mode = mode; |
| 2135 | context->ipc.has_perm = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | } |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2137 | |
Richard Guy Briggs | d9cfea9 | 2013-10-30 17:56:13 -0400 | [diff] [blame] | 2138 | void __audit_bprm(struct linux_binprm *bprm) |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 2139 | { |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 2140 | struct audit_context *context = current->audit_context; |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 2141 | |
Richard Guy Briggs | d9cfea9 | 2013-10-30 17:56:13 -0400 | [diff] [blame] | 2142 | context->type = AUDIT_EXECVE; |
| 2143 | context->execve.argc = bprm->argc; |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2147 | /** |
| 2148 | * audit_socketcall - record audit data for sys_socketcall |
Chen Gang | 2950fa9 | 2013-04-07 16:55:23 +0800 | [diff] [blame] | 2149 | * @nargs: number of args, which should not be more than AUDITSC_ARGS. |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2150 | * @args: args array |
| 2151 | * |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2152 | */ |
Chen Gang | 2950fa9 | 2013-04-07 16:55:23 +0800 | [diff] [blame] | 2153 | int __audit_socketcall(int nargs, unsigned long *args) |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 2154 | { |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 2155 | struct audit_context *context = current->audit_context; |
| 2156 | |
Chen Gang | 2950fa9 | 2013-04-07 16:55:23 +0800 | [diff] [blame] | 2157 | if (nargs <= 0 || nargs > AUDITSC_ARGS || !args) |
| 2158 | return -EINVAL; |
Al Viro | f3298dc | 2008-12-10 03:16:51 -0500 | [diff] [blame] | 2159 | context->type = AUDIT_SOCKETCALL; |
| 2160 | context->socketcall.nargs = nargs; |
| 2161 | memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long)); |
Chen Gang | 2950fa9 | 2013-04-07 16:55:23 +0800 | [diff] [blame] | 2162 | return 0; |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 2163 | } |
| 2164 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2165 | /** |
Al Viro | db34950 | 2007-02-07 01:48:00 -0500 | [diff] [blame] | 2166 | * __audit_fd_pair - record audit data for pipe and socketpair |
| 2167 | * @fd1: the first file descriptor |
| 2168 | * @fd2: the second file descriptor |
| 2169 | * |
Al Viro | db34950 | 2007-02-07 01:48:00 -0500 | [diff] [blame] | 2170 | */ |
Al Viro | 157cf64 | 2008-12-14 04:57:47 -0500 | [diff] [blame] | 2171 | void __audit_fd_pair(int fd1, int fd2) |
Al Viro | db34950 | 2007-02-07 01:48:00 -0500 | [diff] [blame] | 2172 | { |
| 2173 | struct audit_context *context = current->audit_context; |
Al Viro | 157cf64 | 2008-12-14 04:57:47 -0500 | [diff] [blame] | 2174 | context->fds[0] = fd1; |
| 2175 | context->fds[1] = fd2; |
Al Viro | db34950 | 2007-02-07 01:48:00 -0500 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | /** |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2179 | * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto |
| 2180 | * @len: data length in user space |
| 2181 | * @a: data address in kernel space |
| 2182 | * |
| 2183 | * Returns 0 for success or NULL context or < 0 on error. |
| 2184 | */ |
Eric Paris | 07c4941 | 2012-01-03 14:23:07 -0500 | [diff] [blame] | 2185 | int __audit_sockaddr(int len, void *a) |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 2186 | { |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 2187 | struct audit_context *context = current->audit_context; |
| 2188 | |
Al Viro | 4f6b434 | 2008-12-09 19:50:34 -0500 | [diff] [blame] | 2189 | if (!context->sockaddr) { |
| 2190 | void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL); |
| 2191 | if (!p) |
| 2192 | return -ENOMEM; |
| 2193 | context->sockaddr = p; |
| 2194 | } |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 2195 | |
Al Viro | 4f6b434 | 2008-12-09 19:50:34 -0500 | [diff] [blame] | 2196 | context->sockaddr_len = len; |
| 2197 | memcpy(context->sockaddr, a, len); |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 2198 | return 0; |
| 2199 | } |
| 2200 | |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 2201 | void __audit_ptrace(struct task_struct *t) |
| 2202 | { |
| 2203 | struct audit_context *context = current->audit_context; |
| 2204 | |
| 2205 | context->target_pid = t->pid; |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 2206 | context->target_auid = audit_get_loginuid(t); |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2207 | context->target_uid = task_uid(t); |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2208 | context->target_sessionid = audit_get_sessionid(t); |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 2209 | security_task_getsecid(t, &context->target_sid); |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 2210 | memcpy(context->target_comm, t->comm, TASK_COMM_LEN); |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 2211 | } |
| 2212 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2213 | /** |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2214 | * audit_signal_info - record signal info for shutting down audit subsystem |
| 2215 | * @sig: signal value |
| 2216 | * @t: task being signaled |
| 2217 | * |
| 2218 | * If the audit subsystem is being terminated, record the task (pid) |
| 2219 | * and uid that is doing that. |
| 2220 | */ |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2221 | int __audit_signal_info(int sig, struct task_struct *t) |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2222 | { |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2223 | struct audit_aux_data_pids *axp; |
| 2224 | struct task_struct *tsk = current; |
| 2225 | struct audit_context *ctx = tsk->audit_context; |
Eric W. Biederman | cca080d | 2012-02-07 16:53:48 -0800 | [diff] [blame] | 2226 | kuid_t uid = current_uid(), t_uid = task_uid(t); |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2227 | |
Al Viro | 175fc48 | 2007-08-08 00:01:46 +0100 | [diff] [blame] | 2228 | if (audit_pid && t->tgid == audit_pid) { |
Eric Paris | ee1d315 | 2008-07-07 10:49:45 -0400 | [diff] [blame] | 2229 | if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) { |
Al Viro | 175fc48 | 2007-08-08 00:01:46 +0100 | [diff] [blame] | 2230 | audit_sig_pid = tsk->pid; |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 2231 | if (uid_valid(tsk->loginuid)) |
Al Viro | bfef93a | 2008-01-10 04:53:18 -0500 | [diff] [blame] | 2232 | audit_sig_uid = tsk->loginuid; |
Al Viro | 175fc48 | 2007-08-08 00:01:46 +0100 | [diff] [blame] | 2233 | else |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2234 | audit_sig_uid = uid; |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 2235 | security_task_getsecid(tsk, &audit_sig_sid); |
Al Viro | 175fc48 | 2007-08-08 00:01:46 +0100 | [diff] [blame] | 2236 | } |
| 2237 | if (!audit_signals || audit_dummy_context()) |
| 2238 | return 0; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2239 | } |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2240 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2241 | /* optimize the common case by putting first signal recipient directly |
| 2242 | * in audit_context */ |
| 2243 | if (!ctx->target_pid) { |
| 2244 | ctx->target_pid = t->tgid; |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 2245 | ctx->target_auid = audit_get_loginuid(t); |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2246 | ctx->target_uid = t_uid; |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2247 | ctx->target_sessionid = audit_get_sessionid(t); |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 2248 | security_task_getsecid(t, &ctx->target_sid); |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 2249 | memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN); |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2250 | return 0; |
| 2251 | } |
| 2252 | |
| 2253 | axp = (void *)ctx->aux_pids; |
| 2254 | if (!axp || axp->pid_count == AUDIT_AUX_PIDS) { |
| 2255 | axp = kzalloc(sizeof(*axp), GFP_ATOMIC); |
| 2256 | if (!axp) |
| 2257 | return -ENOMEM; |
| 2258 | |
| 2259 | axp->d.type = AUDIT_OBJ_PID; |
| 2260 | axp->d.next = ctx->aux_pids; |
| 2261 | ctx->aux_pids = (void *)axp; |
| 2262 | } |
Adrian Bunk | 88ae704 | 2007-08-22 14:01:05 -0700 | [diff] [blame] | 2263 | BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS); |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2264 | |
| 2265 | axp->target_pid[axp->pid_count] = t->tgid; |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 2266 | axp->target_auid[axp->pid_count] = audit_get_loginuid(t); |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 2267 | axp->target_uid[axp->pid_count] = t_uid; |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2268 | axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t); |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 2269 | security_task_getsecid(t, &axp->target_sid[axp->pid_count]); |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 2270 | memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN); |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2271 | axp->pid_count++; |
| 2272 | |
| 2273 | return 0; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2274 | } |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2275 | |
| 2276 | /** |
Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame] | 2277 | * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 2278 | * @bprm: pointer to the bprm being processed |
| 2279 | * @new: the proposed new credentials |
| 2280 | * @old: the old credentials |
Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame] | 2281 | * |
| 2282 | * Simply check if the proc already has the caps given by the file and if not |
| 2283 | * store the priv escalation info for later auditing at the end of the syscall |
| 2284 | * |
Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame] | 2285 | * -Eric |
| 2286 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 2287 | int __audit_log_bprm_fcaps(struct linux_binprm *bprm, |
| 2288 | const struct cred *new, const struct cred *old) |
Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame] | 2289 | { |
| 2290 | struct audit_aux_data_bprm_fcaps *ax; |
| 2291 | struct audit_context *context = current->audit_context; |
| 2292 | struct cpu_vfs_cap_data vcaps; |
| 2293 | struct dentry *dentry; |
| 2294 | |
| 2295 | ax = kmalloc(sizeof(*ax), GFP_KERNEL); |
| 2296 | if (!ax) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 2297 | return -ENOMEM; |
Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame] | 2298 | |
| 2299 | ax->d.type = AUDIT_BPRM_FCAPS; |
| 2300 | ax->d.next = context->aux; |
| 2301 | context->aux = (void *)ax; |
| 2302 | |
| 2303 | dentry = dget(bprm->file->f_dentry); |
| 2304 | get_vfs_caps_from_disk(dentry, &vcaps); |
| 2305 | dput(dentry); |
| 2306 | |
| 2307 | ax->fcap.permitted = vcaps.permitted; |
| 2308 | ax->fcap.inheritable = vcaps.inheritable; |
| 2309 | ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE); |
| 2310 | ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT; |
| 2311 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 2312 | ax->old_pcap.permitted = old->cap_permitted; |
| 2313 | ax->old_pcap.inheritable = old->cap_inheritable; |
| 2314 | ax->old_pcap.effective = old->cap_effective; |
Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame] | 2315 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 2316 | ax->new_pcap.permitted = new->cap_permitted; |
| 2317 | ax->new_pcap.inheritable = new->cap_inheritable; |
| 2318 | ax->new_pcap.effective = new->cap_effective; |
| 2319 | return 0; |
Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | /** |
Eric Paris | e68b75a0 | 2008-11-11 21:48:22 +1100 | [diff] [blame] | 2323 | * __audit_log_capset - store information about the arguments to the capset syscall |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 2324 | * @new: the new credentials |
| 2325 | * @old: the old (current) credentials |
Eric Paris | e68b75a0 | 2008-11-11 21:48:22 +1100 | [diff] [blame] | 2326 | * |
| 2327 | * Record the aguments userspace sent to sys_capset for later printing by the |
| 2328 | * audit system if applicable |
| 2329 | */ |
Eric W. Biederman | ca24a23 | 2013-03-19 00:02:25 -0700 | [diff] [blame^] | 2330 | void __audit_log_capset(const struct cred *new, const struct cred *old) |
Eric Paris | e68b75a0 | 2008-11-11 21:48:22 +1100 | [diff] [blame] | 2331 | { |
Eric Paris | e68b75a0 | 2008-11-11 21:48:22 +1100 | [diff] [blame] | 2332 | struct audit_context *context = current->audit_context; |
Eric W. Biederman | ca24a23 | 2013-03-19 00:02:25 -0700 | [diff] [blame^] | 2333 | context->capset.pid = task_pid_nr(current); |
Al Viro | 57f71a0 | 2009-01-04 14:52:57 -0500 | [diff] [blame] | 2334 | context->capset.cap.effective = new->cap_effective; |
| 2335 | context->capset.cap.inheritable = new->cap_effective; |
| 2336 | context->capset.cap.permitted = new->cap_permitted; |
| 2337 | context->type = AUDIT_CAPSET; |
Eric Paris | e68b75a0 | 2008-11-11 21:48:22 +1100 | [diff] [blame] | 2338 | } |
| 2339 | |
Al Viro | 120a795 | 2010-10-30 02:54:44 -0400 | [diff] [blame] | 2340 | void __audit_mmap_fd(int fd, int flags) |
| 2341 | { |
| 2342 | struct audit_context *context = current->audit_context; |
| 2343 | context->mmap.fd = fd; |
| 2344 | context->mmap.flags = flags; |
| 2345 | context->type = AUDIT_MMAP; |
| 2346 | } |
| 2347 | |
Kees Cook | 7b9205b | 2013-01-11 14:32:05 -0800 | [diff] [blame] | 2348 | static void audit_log_task(struct audit_buffer *ab) |
Eric Paris | 85e7bac3 | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 2349 | { |
Eric W. Biederman | cca080d | 2012-02-07 16:53:48 -0800 | [diff] [blame] | 2350 | kuid_t auid, uid; |
| 2351 | kgid_t gid; |
Eric Paris | 85e7bac3 | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 2352 | unsigned int sessionid; |
| 2353 | |
| 2354 | auid = audit_get_loginuid(current); |
| 2355 | sessionid = audit_get_sessionid(current); |
| 2356 | current_uid_gid(&uid, &gid); |
| 2357 | |
| 2358 | audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u", |
Eric W. Biederman | cca080d | 2012-02-07 16:53:48 -0800 | [diff] [blame] | 2359 | from_kuid(&init_user_ns, auid), |
| 2360 | from_kuid(&init_user_ns, uid), |
| 2361 | from_kgid(&init_user_ns, gid), |
| 2362 | sessionid); |
Eric Paris | 85e7bac3 | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 2363 | audit_log_task_context(ab); |
| 2364 | audit_log_format(ab, " pid=%d comm=", current->pid); |
| 2365 | audit_log_untrustedstring(ab, current->comm); |
Kees Cook | 7b9205b | 2013-01-11 14:32:05 -0800 | [diff] [blame] | 2366 | } |
| 2367 | |
| 2368 | static void audit_log_abend(struct audit_buffer *ab, char *reason, long signr) |
| 2369 | { |
| 2370 | audit_log_task(ab); |
Eric Paris | 85e7bac3 | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 2371 | audit_log_format(ab, " reason="); |
| 2372 | audit_log_string(ab, reason); |
| 2373 | audit_log_format(ab, " sig=%ld", signr); |
| 2374 | } |
Eric Paris | e68b75a0 | 2008-11-11 21:48:22 +1100 | [diff] [blame] | 2375 | /** |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2376 | * audit_core_dumps - record information about processes that end abnormally |
Henrik Kretzschmar | 6d9525b | 2007-07-15 23:41:10 -0700 | [diff] [blame] | 2377 | * @signr: signal value |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2378 | * |
| 2379 | * If a process ends with a core dump, something fishy is going on and we |
| 2380 | * should record the event for investigation. |
| 2381 | */ |
| 2382 | void audit_core_dumps(long signr) |
| 2383 | { |
| 2384 | struct audit_buffer *ab; |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2385 | |
| 2386 | if (!audit_enabled) |
| 2387 | return; |
| 2388 | |
| 2389 | if (signr == SIGQUIT) /* don't care for those */ |
| 2390 | return; |
| 2391 | |
| 2392 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND); |
Kees Cook | 0644ec0 | 2013-01-11 14:32:07 -0800 | [diff] [blame] | 2393 | if (unlikely(!ab)) |
| 2394 | return; |
Eric Paris | 85e7bac3 | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 2395 | audit_log_abend(ab, "memory violation", signr); |
| 2396 | audit_log_end(ab); |
| 2397 | } |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2398 | |
Kees Cook | 3dc1c1b | 2012-04-12 16:47:58 -0500 | [diff] [blame] | 2399 | void __audit_seccomp(unsigned long syscall, long signr, int code) |
Eric Paris | 85e7bac3 | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 2400 | { |
| 2401 | struct audit_buffer *ab; |
| 2402 | |
Kees Cook | 7b9205b | 2013-01-11 14:32:05 -0800 | [diff] [blame] | 2403 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_SECCOMP); |
| 2404 | if (unlikely(!ab)) |
| 2405 | return; |
| 2406 | audit_log_task(ab); |
| 2407 | audit_log_format(ab, " sig=%ld", signr); |
Eric Paris | 85e7bac3 | 2012-01-03 14:23:05 -0500 | [diff] [blame] | 2408 | audit_log_format(ab, " syscall=%ld", syscall); |
Kees Cook | 3dc1c1b | 2012-04-12 16:47:58 -0500 | [diff] [blame] | 2409 | audit_log_format(ab, " compat=%d", is_compat_task()); |
| 2410 | audit_log_format(ab, " ip=0x%lx", KSTK_EIP(current)); |
| 2411 | audit_log_format(ab, " code=0x%x", code); |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2412 | audit_log_end(ab); |
| 2413 | } |
Al Viro | 916d757 | 2009-06-24 00:02:38 -0400 | [diff] [blame] | 2414 | |
| 2415 | struct list_head *audit_killed_trees(void) |
| 2416 | { |
| 2417 | struct audit_context *ctx = current->audit_context; |
| 2418 | if (likely(!ctx || !ctx->in_syscall)) |
| 2419 | return NULL; |
| 2420 | return &ctx->killed_trees; |
| 2421 | } |