Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Implementation of the security services. |
| 3 | * |
| 4 | * Authors : Stephen Smalley, <sds@epoch.ncsc.mil> |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 5 | * James Morris <jmorris@redhat.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com> |
| 8 | * |
| 9 | * Support for enhanced MLS infrastructure. |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 10 | * Support for context based audit filters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com> |
| 13 | * |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 14 | * Added conditional policy language extensions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 16 | * Updated: Hewlett-Packard <paul.moore@hp.com> |
| 17 | * |
| 18 | * Added support for NetLabel |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 19 | * Added support for the policy capability bitmap |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 20 | * |
Chad Sellers | b94c7e6 | 2006-11-06 12:38:18 -0500 | [diff] [blame] | 21 | * Updated: Chad Sellers <csellers@tresys.com> |
| 22 | * |
| 23 | * Added validation of kernel classes and permissions |
| 24 | * |
KaiGai Kohei | 44c2d9b | 2009-06-18 17:26:13 +0900 | [diff] [blame] | 25 | * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com> |
| 26 | * |
| 27 | * Added support for bounds domain and audit messaged on masked permissions |
| 28 | * |
Guido Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 29 | * Updated: Guido Trentalancia <guido@trentalancia.com> |
| 30 | * |
| 31 | * Added support for runtime switching of the policy type |
| 32 | * |
KaiGai Kohei | 44c2d9b | 2009-06-18 17:26:13 +0900 | [diff] [blame] | 33 | * Copyright (C) 2008, 2009 NEC Corporation |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 34 | * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P. |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 35 | * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc. |
Chad Sellers | b94c7e6 | 2006-11-06 12:38:18 -0500 | [diff] [blame] | 36 | * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com> |
| 38 | * This program is free software; you can redistribute it and/or modify |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 39 | * it under the terms of the GNU General Public License as published by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | * the Free Software Foundation, version 2. |
| 41 | */ |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/slab.h> |
| 44 | #include <linux/string.h> |
| 45 | #include <linux/spinlock.h> |
Paul Moore | 9f2ad66 | 2006-11-17 17:38:53 -0500 | [diff] [blame] | 46 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/errno.h> |
| 48 | #include <linux/in.h> |
| 49 | #include <linux/sched.h> |
| 50 | #include <linux/audit.h> |
Ingo Molnar | bb00307 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 51 | #include <linux/mutex.h> |
Adrian Bunk | 0e55a00 | 2008-03-31 01:54:02 +0300 | [diff] [blame] | 52 | #include <linux/selinux.h> |
Eric Paris | 6371dcd | 2010-07-29 23:02:34 -0400 | [diff] [blame] | 53 | #include <linux/flex_array.h> |
Stephen Rothwell | f0d3d98 | 2010-10-20 16:08:00 +1100 | [diff] [blame^] | 54 | #include <linux/vmalloc.h> |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 55 | #include <net/netlabel.h> |
Ingo Molnar | bb00307 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #include "flask.h" |
| 58 | #include "avc.h" |
| 59 | #include "avc_ss.h" |
| 60 | #include "security.h" |
| 61 | #include "context.h" |
| 62 | #include "policydb.h" |
| 63 | #include "sidtab.h" |
| 64 | #include "services.h" |
| 65 | #include "conditional.h" |
| 66 | #include "mls.h" |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 67 | #include "objsec.h" |
Paul Moore | c60475b | 2007-02-28 15:14:23 -0500 | [diff] [blame] | 68 | #include "netlabel.h" |
Paul Moore | 3de4bab | 2006-11-17 17:38:54 -0500 | [diff] [blame] | 69 | #include "xfrm.h" |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 70 | #include "ebitmap.h" |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 71 | #include "audit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | extern void selnl_notify_policyload(u32 seqno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 75 | int selinux_policycap_netpeer; |
Eric Paris | b0c636b | 2008-02-28 12:58:40 -0500 | [diff] [blame] | 76 | int selinux_policycap_openperm; |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | static DEFINE_RWLOCK(policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | static struct sidtab sidtab; |
| 81 | struct policydb policydb; |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 82 | int ss_initialized; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * The largest sequence number that has been used when |
| 86 | * providing an access decision to the access vector cache. |
| 87 | * The sequence number only changes when a policy change |
| 88 | * occurs. |
| 89 | */ |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 90 | static u32 latest_granting; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | /* Forward declaration. */ |
| 93 | static int context_struct_to_string(struct context *context, char **scontext, |
| 94 | u32 *scontext_len); |
| 95 | |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 96 | static void context_struct_compute_av(struct context *scontext, |
| 97 | struct context *tcontext, |
| 98 | u16 tclass, |
| 99 | struct av_decision *avd); |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 100 | |
| 101 | struct selinux_mapping { |
| 102 | u16 value; /* policy value */ |
| 103 | unsigned num_perms; |
| 104 | u32 perms[sizeof(u32) * 8]; |
| 105 | }; |
| 106 | |
| 107 | static struct selinux_mapping *current_mapping; |
| 108 | static u16 current_mapping_size; |
| 109 | |
| 110 | static int selinux_set_mapping(struct policydb *pol, |
| 111 | struct security_class_mapping *map, |
| 112 | struct selinux_mapping **out_map_p, |
| 113 | u16 *out_map_size) |
| 114 | { |
| 115 | struct selinux_mapping *out_map = NULL; |
| 116 | size_t size = sizeof(struct selinux_mapping); |
| 117 | u16 i, j; |
| 118 | unsigned k; |
| 119 | bool print_unknown_handle = false; |
| 120 | |
| 121 | /* Find number of classes in the input mapping */ |
| 122 | if (!map) |
| 123 | return -EINVAL; |
| 124 | i = 0; |
| 125 | while (map[i].name) |
| 126 | i++; |
| 127 | |
| 128 | /* Allocate space for the class records, plus one for class zero */ |
| 129 | out_map = kcalloc(++i, size, GFP_ATOMIC); |
| 130 | if (!out_map) |
| 131 | return -ENOMEM; |
| 132 | |
| 133 | /* Store the raw class and permission values */ |
| 134 | j = 0; |
| 135 | while (map[j].name) { |
| 136 | struct security_class_mapping *p_in = map + (j++); |
| 137 | struct selinux_mapping *p_out = out_map + j; |
| 138 | |
| 139 | /* An empty class string skips ahead */ |
| 140 | if (!strcmp(p_in->name, "")) { |
| 141 | p_out->num_perms = 0; |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | p_out->value = string_to_security_class(pol, p_in->name); |
| 146 | if (!p_out->value) { |
| 147 | printk(KERN_INFO |
| 148 | "SELinux: Class %s not defined in policy.\n", |
| 149 | p_in->name); |
| 150 | if (pol->reject_unknown) |
| 151 | goto err; |
| 152 | p_out->num_perms = 0; |
| 153 | print_unknown_handle = true; |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | k = 0; |
| 158 | while (p_in->perms && p_in->perms[k]) { |
| 159 | /* An empty permission string skips ahead */ |
| 160 | if (!*p_in->perms[k]) { |
| 161 | k++; |
| 162 | continue; |
| 163 | } |
| 164 | p_out->perms[k] = string_to_av_perm(pol, p_out->value, |
| 165 | p_in->perms[k]); |
| 166 | if (!p_out->perms[k]) { |
| 167 | printk(KERN_INFO |
| 168 | "SELinux: Permission %s in class %s not defined in policy.\n", |
| 169 | p_in->perms[k], p_in->name); |
| 170 | if (pol->reject_unknown) |
| 171 | goto err; |
| 172 | print_unknown_handle = true; |
| 173 | } |
| 174 | |
| 175 | k++; |
| 176 | } |
| 177 | p_out->num_perms = k; |
| 178 | } |
| 179 | |
| 180 | if (print_unknown_handle) |
| 181 | printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n", |
| 182 | pol->allow_unknown ? "allowed" : "denied"); |
| 183 | |
| 184 | *out_map_p = out_map; |
| 185 | *out_map_size = i; |
| 186 | return 0; |
| 187 | err: |
| 188 | kfree(out_map); |
| 189 | return -EINVAL; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Get real, policy values from mapped values |
| 194 | */ |
| 195 | |
| 196 | static u16 unmap_class(u16 tclass) |
| 197 | { |
| 198 | if (tclass < current_mapping_size) |
| 199 | return current_mapping[tclass].value; |
| 200 | |
| 201 | return tclass; |
| 202 | } |
| 203 | |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 204 | static void map_decision(u16 tclass, struct av_decision *avd, |
| 205 | int allow_unknown) |
| 206 | { |
| 207 | if (tclass < current_mapping_size) { |
| 208 | unsigned i, n = current_mapping[tclass].num_perms; |
| 209 | u32 result; |
| 210 | |
| 211 | for (i = 0, result = 0; i < n; i++) { |
| 212 | if (avd->allowed & current_mapping[tclass].perms[i]) |
| 213 | result |= 1<<i; |
| 214 | if (allow_unknown && !current_mapping[tclass].perms[i]) |
| 215 | result |= 1<<i; |
| 216 | } |
| 217 | avd->allowed = result; |
| 218 | |
| 219 | for (i = 0, result = 0; i < n; i++) |
| 220 | if (avd->auditallow & current_mapping[tclass].perms[i]) |
| 221 | result |= 1<<i; |
| 222 | avd->auditallow = result; |
| 223 | |
| 224 | for (i = 0, result = 0; i < n; i++) { |
| 225 | if (avd->auditdeny & current_mapping[tclass].perms[i]) |
| 226 | result |= 1<<i; |
| 227 | if (!allow_unknown && !current_mapping[tclass].perms[i]) |
| 228 | result |= 1<<i; |
| 229 | } |
Eric Paris | 0bce952 | 2009-11-23 16:47:23 -0500 | [diff] [blame] | 230 | /* |
| 231 | * In case the kernel has a bug and requests a permission |
| 232 | * between num_perms and the maximum permission number, we |
| 233 | * should audit that denial |
| 234 | */ |
| 235 | for (; i < (sizeof(u32)*8); i++) |
| 236 | result |= 1<<i; |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 237 | avd->auditdeny = result; |
| 238 | } |
| 239 | } |
| 240 | |
Guido Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 241 | int security_mls_enabled(void) |
| 242 | { |
| 243 | return policydb.mls_enabled; |
| 244 | } |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | /* |
| 247 | * Return the boolean value of a constraint expression |
| 248 | * when it is applied to the specified source and target |
| 249 | * security contexts. |
| 250 | * |
| 251 | * xcontext is a special beast... It is used by the validatetrans rules |
| 252 | * only. For these rules, scontext is the context before the transition, |
| 253 | * tcontext is the context after the transition, and xcontext is the context |
| 254 | * of the process performing the transition. All other callers of |
| 255 | * constraint_expr_eval should pass in NULL for xcontext. |
| 256 | */ |
| 257 | static int constraint_expr_eval(struct context *scontext, |
| 258 | struct context *tcontext, |
| 259 | struct context *xcontext, |
| 260 | struct constraint_expr *cexpr) |
| 261 | { |
| 262 | u32 val1, val2; |
| 263 | struct context *c; |
| 264 | struct role_datum *r1, *r2; |
| 265 | struct mls_level *l1, *l2; |
| 266 | struct constraint_expr *e; |
| 267 | int s[CEXPR_MAXDEPTH]; |
| 268 | int sp = -1; |
| 269 | |
| 270 | for (e = cexpr; e; e = e->next) { |
| 271 | switch (e->expr_type) { |
| 272 | case CEXPR_NOT: |
| 273 | BUG_ON(sp < 0); |
| 274 | s[sp] = !s[sp]; |
| 275 | break; |
| 276 | case CEXPR_AND: |
| 277 | BUG_ON(sp < 1); |
| 278 | sp--; |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 279 | s[sp] &= s[sp + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | break; |
| 281 | case CEXPR_OR: |
| 282 | BUG_ON(sp < 1); |
| 283 | sp--; |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 284 | s[sp] |= s[sp + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | break; |
| 286 | case CEXPR_ATTR: |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 287 | if (sp == (CEXPR_MAXDEPTH - 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | return 0; |
| 289 | switch (e->attr) { |
| 290 | case CEXPR_USER: |
| 291 | val1 = scontext->user; |
| 292 | val2 = tcontext->user; |
| 293 | break; |
| 294 | case CEXPR_TYPE: |
| 295 | val1 = scontext->type; |
| 296 | val2 = tcontext->type; |
| 297 | break; |
| 298 | case CEXPR_ROLE: |
| 299 | val1 = scontext->role; |
| 300 | val2 = tcontext->role; |
| 301 | r1 = policydb.role_val_to_struct[val1 - 1]; |
| 302 | r2 = policydb.role_val_to_struct[val2 - 1]; |
| 303 | switch (e->op) { |
| 304 | case CEXPR_DOM: |
| 305 | s[++sp] = ebitmap_get_bit(&r1->dominates, |
| 306 | val2 - 1); |
| 307 | continue; |
| 308 | case CEXPR_DOMBY: |
| 309 | s[++sp] = ebitmap_get_bit(&r2->dominates, |
| 310 | val1 - 1); |
| 311 | continue; |
| 312 | case CEXPR_INCOMP: |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 313 | s[++sp] = (!ebitmap_get_bit(&r1->dominates, |
| 314 | val2 - 1) && |
| 315 | !ebitmap_get_bit(&r2->dominates, |
| 316 | val1 - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | continue; |
| 318 | default: |
| 319 | break; |
| 320 | } |
| 321 | break; |
| 322 | case CEXPR_L1L2: |
| 323 | l1 = &(scontext->range.level[0]); |
| 324 | l2 = &(tcontext->range.level[0]); |
| 325 | goto mls_ops; |
| 326 | case CEXPR_L1H2: |
| 327 | l1 = &(scontext->range.level[0]); |
| 328 | l2 = &(tcontext->range.level[1]); |
| 329 | goto mls_ops; |
| 330 | case CEXPR_H1L2: |
| 331 | l1 = &(scontext->range.level[1]); |
| 332 | l2 = &(tcontext->range.level[0]); |
| 333 | goto mls_ops; |
| 334 | case CEXPR_H1H2: |
| 335 | l1 = &(scontext->range.level[1]); |
| 336 | l2 = &(tcontext->range.level[1]); |
| 337 | goto mls_ops; |
| 338 | case CEXPR_L1H1: |
| 339 | l1 = &(scontext->range.level[0]); |
| 340 | l2 = &(scontext->range.level[1]); |
| 341 | goto mls_ops; |
| 342 | case CEXPR_L2H2: |
| 343 | l1 = &(tcontext->range.level[0]); |
| 344 | l2 = &(tcontext->range.level[1]); |
| 345 | goto mls_ops; |
| 346 | mls_ops: |
| 347 | switch (e->op) { |
| 348 | case CEXPR_EQ: |
| 349 | s[++sp] = mls_level_eq(l1, l2); |
| 350 | continue; |
| 351 | case CEXPR_NEQ: |
| 352 | s[++sp] = !mls_level_eq(l1, l2); |
| 353 | continue; |
| 354 | case CEXPR_DOM: |
| 355 | s[++sp] = mls_level_dom(l1, l2); |
| 356 | continue; |
| 357 | case CEXPR_DOMBY: |
| 358 | s[++sp] = mls_level_dom(l2, l1); |
| 359 | continue; |
| 360 | case CEXPR_INCOMP: |
| 361 | s[++sp] = mls_level_incomp(l2, l1); |
| 362 | continue; |
| 363 | default: |
| 364 | BUG(); |
| 365 | return 0; |
| 366 | } |
| 367 | break; |
| 368 | default: |
| 369 | BUG(); |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | switch (e->op) { |
| 374 | case CEXPR_EQ: |
| 375 | s[++sp] = (val1 == val2); |
| 376 | break; |
| 377 | case CEXPR_NEQ: |
| 378 | s[++sp] = (val1 != val2); |
| 379 | break; |
| 380 | default: |
| 381 | BUG(); |
| 382 | return 0; |
| 383 | } |
| 384 | break; |
| 385 | case CEXPR_NAMES: |
| 386 | if (sp == (CEXPR_MAXDEPTH-1)) |
| 387 | return 0; |
| 388 | c = scontext; |
| 389 | if (e->attr & CEXPR_TARGET) |
| 390 | c = tcontext; |
| 391 | else if (e->attr & CEXPR_XTARGET) { |
| 392 | c = xcontext; |
| 393 | if (!c) { |
| 394 | BUG(); |
| 395 | return 0; |
| 396 | } |
| 397 | } |
| 398 | if (e->attr & CEXPR_USER) |
| 399 | val1 = c->user; |
| 400 | else if (e->attr & CEXPR_ROLE) |
| 401 | val1 = c->role; |
| 402 | else if (e->attr & CEXPR_TYPE) |
| 403 | val1 = c->type; |
| 404 | else { |
| 405 | BUG(); |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | switch (e->op) { |
| 410 | case CEXPR_EQ: |
| 411 | s[++sp] = ebitmap_get_bit(&e->names, val1 - 1); |
| 412 | break; |
| 413 | case CEXPR_NEQ: |
| 414 | s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1); |
| 415 | break; |
| 416 | default: |
| 417 | BUG(); |
| 418 | return 0; |
| 419 | } |
| 420 | break; |
| 421 | default: |
| 422 | BUG(); |
| 423 | return 0; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | BUG_ON(sp != 0); |
| 428 | return s[0]; |
| 429 | } |
| 430 | |
| 431 | /* |
KaiGai Kohei | 44c2d9b | 2009-06-18 17:26:13 +0900 | [diff] [blame] | 432 | * security_dump_masked_av - dumps masked permissions during |
| 433 | * security_compute_av due to RBAC, MLS/Constraint and Type bounds. |
| 434 | */ |
| 435 | static int dump_masked_av_helper(void *k, void *d, void *args) |
| 436 | { |
| 437 | struct perm_datum *pdatum = d; |
| 438 | char **permission_names = args; |
| 439 | |
| 440 | BUG_ON(pdatum->value < 1 || pdatum->value > 32); |
| 441 | |
| 442 | permission_names[pdatum->value - 1] = (char *)k; |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static void security_dump_masked_av(struct context *scontext, |
| 448 | struct context *tcontext, |
| 449 | u16 tclass, |
| 450 | u32 permissions, |
| 451 | const char *reason) |
| 452 | { |
| 453 | struct common_datum *common_dat; |
| 454 | struct class_datum *tclass_dat; |
| 455 | struct audit_buffer *ab; |
| 456 | char *tclass_name; |
| 457 | char *scontext_name = NULL; |
| 458 | char *tcontext_name = NULL; |
| 459 | char *permission_names[32]; |
James Morris | 2da5d31 | 2010-02-16 17:29:06 +1100 | [diff] [blame] | 460 | int index; |
| 461 | u32 length; |
KaiGai Kohei | 44c2d9b | 2009-06-18 17:26:13 +0900 | [diff] [blame] | 462 | bool need_comma = false; |
| 463 | |
| 464 | if (!permissions) |
| 465 | return; |
| 466 | |
| 467 | tclass_name = policydb.p_class_val_to_name[tclass - 1]; |
| 468 | tclass_dat = policydb.class_val_to_struct[tclass - 1]; |
| 469 | common_dat = tclass_dat->comdatum; |
| 470 | |
| 471 | /* init permission_names */ |
| 472 | if (common_dat && |
| 473 | hashtab_map(common_dat->permissions.table, |
| 474 | dump_masked_av_helper, permission_names) < 0) |
| 475 | goto out; |
| 476 | |
| 477 | if (hashtab_map(tclass_dat->permissions.table, |
| 478 | dump_masked_av_helper, permission_names) < 0) |
| 479 | goto out; |
| 480 | |
| 481 | /* get scontext/tcontext in text form */ |
| 482 | if (context_struct_to_string(scontext, |
| 483 | &scontext_name, &length) < 0) |
| 484 | goto out; |
| 485 | |
| 486 | if (context_struct_to_string(tcontext, |
| 487 | &tcontext_name, &length) < 0) |
| 488 | goto out; |
| 489 | |
| 490 | /* audit a message */ |
| 491 | ab = audit_log_start(current->audit_context, |
| 492 | GFP_ATOMIC, AUDIT_SELINUX_ERR); |
| 493 | if (!ab) |
| 494 | goto out; |
| 495 | |
| 496 | audit_log_format(ab, "op=security_compute_av reason=%s " |
| 497 | "scontext=%s tcontext=%s tclass=%s perms=", |
| 498 | reason, scontext_name, tcontext_name, tclass_name); |
| 499 | |
| 500 | for (index = 0; index < 32; index++) { |
| 501 | u32 mask = (1 << index); |
| 502 | |
| 503 | if ((mask & permissions) == 0) |
| 504 | continue; |
| 505 | |
| 506 | audit_log_format(ab, "%s%s", |
| 507 | need_comma ? "," : "", |
| 508 | permission_names[index] |
| 509 | ? permission_names[index] : "????"); |
| 510 | need_comma = true; |
| 511 | } |
| 512 | audit_log_end(ab); |
| 513 | out: |
| 514 | /* release scontext/tcontext */ |
| 515 | kfree(tcontext_name); |
| 516 | kfree(scontext_name); |
| 517 | |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | /* |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 522 | * security_boundary_permission - drops violated permissions |
| 523 | * on boundary constraint. |
| 524 | */ |
| 525 | static void type_attribute_bounds_av(struct context *scontext, |
| 526 | struct context *tcontext, |
| 527 | u16 tclass, |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 528 | struct av_decision *avd) |
| 529 | { |
KaiGai Kohei | 2ae3ba3 | 2010-02-17 08:49:41 +0900 | [diff] [blame] | 530 | struct context lo_scontext; |
| 531 | struct context lo_tcontext; |
| 532 | struct av_decision lo_avd; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 533 | struct type_datum *source |
| 534 | = policydb.type_val_to_struct[scontext->type - 1]; |
KaiGai Kohei | 2ae3ba3 | 2010-02-17 08:49:41 +0900 | [diff] [blame] | 535 | struct type_datum *target |
| 536 | = policydb.type_val_to_struct[tcontext->type - 1]; |
| 537 | u32 masked = 0; |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 538 | |
| 539 | if (source->bounds) { |
| 540 | memset(&lo_avd, 0, sizeof(lo_avd)); |
| 541 | |
| 542 | memcpy(&lo_scontext, scontext, sizeof(lo_scontext)); |
| 543 | lo_scontext.type = source->bounds; |
| 544 | |
| 545 | context_struct_compute_av(&lo_scontext, |
| 546 | tcontext, |
| 547 | tclass, |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 548 | &lo_avd); |
| 549 | if ((lo_avd.allowed & avd->allowed) == avd->allowed) |
| 550 | return; /* no masked permission */ |
| 551 | masked = ~lo_avd.allowed & avd->allowed; |
KaiGai Kohei | 2ae3ba3 | 2010-02-17 08:49:41 +0900 | [diff] [blame] | 552 | } |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 553 | |
KaiGai Kohei | 2ae3ba3 | 2010-02-17 08:49:41 +0900 | [diff] [blame] | 554 | if (target->bounds) { |
| 555 | memset(&lo_avd, 0, sizeof(lo_avd)); |
| 556 | |
| 557 | memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext)); |
| 558 | lo_tcontext.type = target->bounds; |
| 559 | |
| 560 | context_struct_compute_av(scontext, |
| 561 | &lo_tcontext, |
| 562 | tclass, |
| 563 | &lo_avd); |
| 564 | if ((lo_avd.allowed & avd->allowed) == avd->allowed) |
| 565 | return; /* no masked permission */ |
| 566 | masked = ~lo_avd.allowed & avd->allowed; |
| 567 | } |
| 568 | |
| 569 | if (source->bounds && target->bounds) { |
| 570 | memset(&lo_avd, 0, sizeof(lo_avd)); |
| 571 | /* |
| 572 | * lo_scontext and lo_tcontext are already |
| 573 | * set up. |
| 574 | */ |
| 575 | |
| 576 | context_struct_compute_av(&lo_scontext, |
| 577 | &lo_tcontext, |
| 578 | tclass, |
| 579 | &lo_avd); |
| 580 | if ((lo_avd.allowed & avd->allowed) == avd->allowed) |
| 581 | return; /* no masked permission */ |
| 582 | masked = ~lo_avd.allowed & avd->allowed; |
| 583 | } |
| 584 | |
| 585 | if (masked) { |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 586 | /* mask violated permissions */ |
| 587 | avd->allowed &= ~masked; |
| 588 | |
KaiGai Kohei | 44c2d9b | 2009-06-18 17:26:13 +0900 | [diff] [blame] | 589 | /* audit masked permissions */ |
| 590 | security_dump_masked_av(scontext, tcontext, |
| 591 | tclass, masked, "bounds"); |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
| 595 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | * Compute access vectors based on a context structure pair for |
| 597 | * the permissions in a particular class. |
| 598 | */ |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 599 | static void context_struct_compute_av(struct context *scontext, |
| 600 | struct context *tcontext, |
| 601 | u16 tclass, |
| 602 | struct av_decision *avd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | { |
| 604 | struct constraint_node *constraint; |
| 605 | struct role_allow *ra; |
| 606 | struct avtab_key avkey; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 607 | struct avtab_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | struct class_datum *tclass_datum; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 609 | struct ebitmap *sattr, *tattr; |
| 610 | struct ebitmap_node *snode, *tnode; |
| 611 | unsigned int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | avd->allowed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | avd->auditallow = 0; |
| 615 | avd->auditdeny = 0xffffffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 617 | if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) { |
| 618 | if (printk_ratelimit()) |
| 619 | printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass); |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 620 | return; |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 621 | } |
Eric Paris | 3f12070 | 2007-09-21 14:37:10 -0400 | [diff] [blame] | 622 | |
| 623 | tclass_datum = policydb.class_val_to_struct[tclass - 1]; |
| 624 | |
| 625 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | * If a specific type enforcement rule was defined for |
| 627 | * this permission check, then use it. |
| 628 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | avkey.target_class = tclass; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 630 | avkey.specified = AVTAB_AV; |
Eric Paris | 6371dcd | 2010-07-29 23:02:34 -0400 | [diff] [blame] | 631 | sattr = flex_array_get(policydb.type_attr_map_array, scontext->type - 1); |
| 632 | BUG_ON(!sattr); |
| 633 | tattr = flex_array_get(policydb.type_attr_map_array, tcontext->type - 1); |
| 634 | BUG_ON(!tattr); |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 635 | ebitmap_for_each_positive_bit(sattr, snode, i) { |
| 636 | ebitmap_for_each_positive_bit(tattr, tnode, j) { |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 637 | avkey.source_type = i + 1; |
| 638 | avkey.target_type = j + 1; |
| 639 | for (node = avtab_search_node(&policydb.te_avtab, &avkey); |
Vesa-Matti Kari | dbc74c6 | 2008-08-07 03:18:20 +0300 | [diff] [blame] | 640 | node; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 641 | node = avtab_search_node_next(node, avkey.specified)) { |
| 642 | if (node->key.specified == AVTAB_ALLOWED) |
| 643 | avd->allowed |= node->datum.data; |
| 644 | else if (node->key.specified == AVTAB_AUDITALLOW) |
| 645 | avd->auditallow |= node->datum.data; |
| 646 | else if (node->key.specified == AVTAB_AUDITDENY) |
| 647 | avd->auditdeny &= node->datum.data; |
| 648 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 650 | /* Check conditional av table for additional permissions */ |
| 651 | cond_compute_av(&policydb.te_cond_avtab, &avkey, avd); |
| 652 | |
| 653 | } |
| 654 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
| 656 | /* |
| 657 | * Remove any permissions prohibited by a constraint (this includes |
| 658 | * the MLS policy). |
| 659 | */ |
| 660 | constraint = tclass_datum->constraints; |
| 661 | while (constraint) { |
| 662 | if ((constraint->permissions & (avd->allowed)) && |
| 663 | !constraint_expr_eval(scontext, tcontext, NULL, |
| 664 | constraint->expr)) { |
KaiGai Kohei | caabbdc | 2009-06-18 17:30:07 +0900 | [diff] [blame] | 665 | avd->allowed &= ~(constraint->permissions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } |
| 667 | constraint = constraint->next; |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | * If checking process transition permission and the |
| 672 | * role is changing, then check the (current_role, new_role) |
| 673 | * pair. |
| 674 | */ |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 675 | if (tclass == policydb.process_class && |
| 676 | (avd->allowed & policydb.process_trans_perms) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | scontext->role != tcontext->role) { |
| 678 | for (ra = policydb.role_allow; ra; ra = ra->next) { |
| 679 | if (scontext->role == ra->role && |
| 680 | tcontext->role == ra->new_role) |
| 681 | break; |
| 682 | } |
| 683 | if (!ra) |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 684 | avd->allowed &= ~policydb.process_trans_perms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | } |
| 686 | |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 687 | /* |
| 688 | * If the given source and target types have boundary |
| 689 | * constraint, lazy checks have to mask any violated |
| 690 | * permission and notice it to userspace via audit. |
| 691 | */ |
| 692 | type_attribute_bounds_av(scontext, tcontext, |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 693 | tclass, avd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | static int security_validtrans_handle_fail(struct context *ocontext, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 697 | struct context *ncontext, |
| 698 | struct context *tcontext, |
| 699 | u16 tclass) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | { |
| 701 | char *o = NULL, *n = NULL, *t = NULL; |
| 702 | u32 olen, nlen, tlen; |
| 703 | |
| 704 | if (context_struct_to_string(ocontext, &o, &olen) < 0) |
| 705 | goto out; |
| 706 | if (context_struct_to_string(ncontext, &n, &nlen) < 0) |
| 707 | goto out; |
| 708 | if (context_struct_to_string(tcontext, &t, &tlen) < 0) |
| 709 | goto out; |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 710 | audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 711 | "security_validate_transition: denied for" |
| 712 | " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s", |
| 713 | o, n, t, policydb.p_class_val_to_name[tclass-1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | out: |
| 715 | kfree(o); |
| 716 | kfree(n); |
| 717 | kfree(t); |
| 718 | |
| 719 | if (!selinux_enforcing) |
| 720 | return 0; |
| 721 | return -EPERM; |
| 722 | } |
| 723 | |
| 724 | int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid, |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 725 | u16 orig_tclass) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | { |
| 727 | struct context *ocontext; |
| 728 | struct context *ncontext; |
| 729 | struct context *tcontext; |
| 730 | struct class_datum *tclass_datum; |
| 731 | struct constraint_node *constraint; |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 732 | u16 tclass; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | int rc = 0; |
| 734 | |
| 735 | if (!ss_initialized) |
| 736 | return 0; |
| 737 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 738 | read_lock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 740 | tclass = unmap_class(orig_tclass); |
| 741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | if (!tclass || tclass > policydb.p_classes.nprim) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 743 | printk(KERN_ERR "SELinux: %s: unrecognized class %d\n", |
| 744 | __func__, tclass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | rc = -EINVAL; |
| 746 | goto out; |
| 747 | } |
| 748 | tclass_datum = policydb.class_val_to_struct[tclass - 1]; |
| 749 | |
| 750 | ocontext = sidtab_search(&sidtab, oldsid); |
| 751 | if (!ocontext) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 752 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
| 753 | __func__, oldsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | rc = -EINVAL; |
| 755 | goto out; |
| 756 | } |
| 757 | |
| 758 | ncontext = sidtab_search(&sidtab, newsid); |
| 759 | if (!ncontext) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 760 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
| 761 | __func__, newsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | rc = -EINVAL; |
| 763 | goto out; |
| 764 | } |
| 765 | |
| 766 | tcontext = sidtab_search(&sidtab, tasksid); |
| 767 | if (!tcontext) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 768 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
| 769 | __func__, tasksid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | rc = -EINVAL; |
| 771 | goto out; |
| 772 | } |
| 773 | |
| 774 | constraint = tclass_datum->validatetrans; |
| 775 | while (constraint) { |
| 776 | if (!constraint_expr_eval(ocontext, ncontext, tcontext, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 777 | constraint->expr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | rc = security_validtrans_handle_fail(ocontext, ncontext, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 779 | tcontext, tclass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | goto out; |
| 781 | } |
| 782 | constraint = constraint->next; |
| 783 | } |
| 784 | |
| 785 | out: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 786 | read_unlock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | return rc; |
| 788 | } |
| 789 | |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 790 | /* |
| 791 | * security_bounded_transition - check whether the given |
| 792 | * transition is directed to bounded, or not. |
| 793 | * It returns 0, if @newsid is bounded by @oldsid. |
| 794 | * Otherwise, it returns error code. |
| 795 | * |
| 796 | * @oldsid : current security identifier |
| 797 | * @newsid : destinated security identifier |
| 798 | */ |
| 799 | int security_bounded_transition(u32 old_sid, u32 new_sid) |
| 800 | { |
| 801 | struct context *old_context, *new_context; |
| 802 | struct type_datum *type; |
| 803 | int index; |
| 804 | int rc = -EINVAL; |
| 805 | |
| 806 | read_lock(&policy_rwlock); |
| 807 | |
| 808 | old_context = sidtab_search(&sidtab, old_sid); |
| 809 | if (!old_context) { |
| 810 | printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n", |
| 811 | __func__, old_sid); |
| 812 | goto out; |
| 813 | } |
| 814 | |
| 815 | new_context = sidtab_search(&sidtab, new_sid); |
| 816 | if (!new_context) { |
| 817 | printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n", |
| 818 | __func__, new_sid); |
| 819 | goto out; |
| 820 | } |
| 821 | |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 822 | /* type/domain unchanged */ |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 823 | if (old_context->type == new_context->type) { |
| 824 | rc = 0; |
| 825 | goto out; |
| 826 | } |
| 827 | |
| 828 | index = new_context->type; |
| 829 | while (true) { |
| 830 | type = policydb.type_val_to_struct[index - 1]; |
| 831 | BUG_ON(!type); |
| 832 | |
| 833 | /* not bounded anymore */ |
| 834 | if (!type->bounds) { |
| 835 | rc = -EPERM; |
| 836 | break; |
| 837 | } |
| 838 | |
| 839 | /* @newsid is bounded by @oldsid */ |
| 840 | if (type->bounds == old_context->type) { |
| 841 | rc = 0; |
| 842 | break; |
| 843 | } |
| 844 | index = type->bounds; |
| 845 | } |
KaiGai Kohei | 44c2d9b | 2009-06-18 17:26:13 +0900 | [diff] [blame] | 846 | |
| 847 | if (rc) { |
| 848 | char *old_name = NULL; |
| 849 | char *new_name = NULL; |
James Morris | 2da5d31 | 2010-02-16 17:29:06 +1100 | [diff] [blame] | 850 | u32 length; |
KaiGai Kohei | 44c2d9b | 2009-06-18 17:26:13 +0900 | [diff] [blame] | 851 | |
| 852 | if (!context_struct_to_string(old_context, |
| 853 | &old_name, &length) && |
| 854 | !context_struct_to_string(new_context, |
| 855 | &new_name, &length)) { |
| 856 | audit_log(current->audit_context, |
| 857 | GFP_ATOMIC, AUDIT_SELINUX_ERR, |
| 858 | "op=security_bounded_transition " |
| 859 | "result=denied " |
| 860 | "oldcontext=%s newcontext=%s", |
| 861 | old_name, new_name); |
| 862 | } |
| 863 | kfree(new_name); |
| 864 | kfree(old_name); |
| 865 | } |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 866 | out: |
| 867 | read_unlock(&policy_rwlock); |
| 868 | |
| 869 | return rc; |
| 870 | } |
| 871 | |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 872 | static void avd_init(struct av_decision *avd) |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 873 | { |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 874 | avd->allowed = 0; |
| 875 | avd->auditallow = 0; |
| 876 | avd->auditdeny = 0xffffffff; |
| 877 | avd->seqno = latest_granting; |
| 878 | avd->flags = 0; |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 879 | } |
| 880 | |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | /** |
| 883 | * security_compute_av - Compute access vector decisions. |
| 884 | * @ssid: source security identifier |
| 885 | * @tsid: target security identifier |
| 886 | * @tclass: target security class |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | * @avd: access vector decisions |
| 888 | * |
| 889 | * Compute a set of access vector decisions based on the |
| 890 | * SID pair (@ssid, @tsid) for the permissions in @tclass. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | */ |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 892 | void security_compute_av(u32 ssid, |
| 893 | u32 tsid, |
| 894 | u16 orig_tclass, |
| 895 | struct av_decision *avd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | { |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 897 | u16 tclass; |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 898 | struct context *scontext = NULL, *tcontext = NULL; |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 899 | |
Stephen Smalley | b7f3008 | 2009-10-19 10:08:50 -0400 | [diff] [blame] | 900 | read_lock(&policy_rwlock); |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 901 | avd_init(avd); |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 902 | if (!ss_initialized) |
| 903 | goto allow; |
| 904 | |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 905 | scontext = sidtab_search(&sidtab, ssid); |
| 906 | if (!scontext) { |
| 907 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
| 908 | __func__, ssid); |
| 909 | goto out; |
| 910 | } |
| 911 | |
| 912 | /* permissive domain? */ |
| 913 | if (ebitmap_get_bit(&policydb.permissive_map, scontext->type)) |
| 914 | avd->flags |= AVD_FLAGS_PERMISSIVE; |
| 915 | |
| 916 | tcontext = sidtab_search(&sidtab, tsid); |
| 917 | if (!tcontext) { |
| 918 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
| 919 | __func__, tsid); |
| 920 | goto out; |
| 921 | } |
| 922 | |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 923 | tclass = unmap_class(orig_tclass); |
| 924 | if (unlikely(orig_tclass && !tclass)) { |
| 925 | if (policydb.allow_unknown) |
| 926 | goto allow; |
Stephen Smalley | b7f3008 | 2009-10-19 10:08:50 -0400 | [diff] [blame] | 927 | goto out; |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 928 | } |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 929 | context_struct_compute_av(scontext, tcontext, tclass, avd); |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 930 | map_decision(orig_tclass, avd, policydb.allow_unknown); |
Stephen Smalley | b7f3008 | 2009-10-19 10:08:50 -0400 | [diff] [blame] | 931 | out: |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 932 | read_unlock(&policy_rwlock); |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 933 | return; |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 934 | allow: |
| 935 | avd->allowed = 0xffffffff; |
Stephen Smalley | b7f3008 | 2009-10-19 10:08:50 -0400 | [diff] [blame] | 936 | goto out; |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 937 | } |
| 938 | |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 939 | void security_compute_av_user(u32 ssid, |
| 940 | u32 tsid, |
| 941 | u16 tclass, |
| 942 | struct av_decision *avd) |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 943 | { |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 944 | struct context *scontext = NULL, *tcontext = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 946 | read_lock(&policy_rwlock); |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 947 | avd_init(avd); |
| 948 | if (!ss_initialized) |
| 949 | goto allow; |
| 950 | |
| 951 | scontext = sidtab_search(&sidtab, ssid); |
| 952 | if (!scontext) { |
| 953 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
| 954 | __func__, ssid); |
| 955 | goto out; |
| 956 | } |
| 957 | |
| 958 | /* permissive domain? */ |
| 959 | if (ebitmap_get_bit(&policydb.permissive_map, scontext->type)) |
| 960 | avd->flags |= AVD_FLAGS_PERMISSIVE; |
| 961 | |
| 962 | tcontext = sidtab_search(&sidtab, tsid); |
| 963 | if (!tcontext) { |
| 964 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
| 965 | __func__, tsid); |
| 966 | goto out; |
| 967 | } |
| 968 | |
| 969 | if (unlikely(!tclass)) { |
| 970 | if (policydb.allow_unknown) |
| 971 | goto allow; |
| 972 | goto out; |
| 973 | } |
| 974 | |
| 975 | context_struct_compute_av(scontext, tcontext, tclass, avd); |
| 976 | out: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 977 | read_unlock(&policy_rwlock); |
Stephen Smalley | 19439d0 | 2010-01-14 17:28:10 -0500 | [diff] [blame] | 978 | return; |
| 979 | allow: |
| 980 | avd->allowed = 0xffffffff; |
| 981 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /* |
| 985 | * Write the security context string representation of |
| 986 | * the context structure `context' into a dynamically |
| 987 | * allocated string of the correct size. Set `*scontext' |
| 988 | * to point to this string and set `*scontext_len' to |
| 989 | * the length of the string. |
| 990 | */ |
| 991 | static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len) |
| 992 | { |
| 993 | char *scontextp; |
| 994 | |
Eric Paris | d5630b9 | 2010-10-13 16:24:48 -0400 | [diff] [blame] | 995 | if (scontext) |
| 996 | *scontext = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | *scontext_len = 0; |
| 998 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 999 | if (context->len) { |
| 1000 | *scontext_len = context->len; |
| 1001 | *scontext = kstrdup(context->str, GFP_ATOMIC); |
| 1002 | if (!(*scontext)) |
| 1003 | return -ENOMEM; |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | /* Compute the size of the context. */ |
| 1008 | *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1; |
| 1009 | *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1; |
| 1010 | *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1; |
| 1011 | *scontext_len += mls_compute_context_len(context); |
| 1012 | |
Eric Paris | d5630b9 | 2010-10-13 16:24:48 -0400 | [diff] [blame] | 1013 | if (!scontext) |
| 1014 | return 0; |
| 1015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | /* Allocate space for the context; caller must free this space. */ |
| 1017 | scontextp = kmalloc(*scontext_len, GFP_ATOMIC); |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 1018 | if (!scontextp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | *scontext = scontextp; |
| 1021 | |
| 1022 | /* |
| 1023 | * Copy the user name, role name and type name into the context. |
| 1024 | */ |
| 1025 | sprintf(scontextp, "%s:%s:%s", |
| 1026 | policydb.p_user_val_to_name[context->user - 1], |
| 1027 | policydb.p_role_val_to_name[context->role - 1], |
| 1028 | policydb.p_type_val_to_name[context->type - 1]); |
| 1029 | scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) + |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 1030 | 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) + |
| 1031 | 1 + strlen(policydb.p_type_val_to_name[context->type - 1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | |
| 1033 | mls_sid_to_context(context, &scontextp); |
| 1034 | |
| 1035 | *scontextp = 0; |
| 1036 | |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
| 1040 | #include "initial_sid_to_string.h" |
| 1041 | |
James Carter | f0ee2e4 | 2007-04-04 10:11:29 -0400 | [diff] [blame] | 1042 | const char *security_get_initial_sid_context(u32 sid) |
| 1043 | { |
| 1044 | if (unlikely(sid > SECINITSID_NUM)) |
| 1045 | return NULL; |
| 1046 | return initial_sid_to_string[sid]; |
| 1047 | } |
| 1048 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1049 | static int security_sid_to_context_core(u32 sid, char **scontext, |
| 1050 | u32 *scontext_len, int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | { |
| 1052 | struct context *context; |
| 1053 | int rc = 0; |
| 1054 | |
Eric Paris | d5630b9 | 2010-10-13 16:24:48 -0400 | [diff] [blame] | 1055 | if (scontext) |
| 1056 | *scontext = NULL; |
Stephen Smalley | 4f4acf3 | 2007-02-26 12:02:34 -0500 | [diff] [blame] | 1057 | *scontext_len = 0; |
| 1058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | if (!ss_initialized) { |
| 1060 | if (sid <= SECINITSID_NUM) { |
| 1061 | char *scontextp; |
| 1062 | |
| 1063 | *scontext_len = strlen(initial_sid_to_string[sid]) + 1; |
Eric Paris | d5630b9 | 2010-10-13 16:24:48 -0400 | [diff] [blame] | 1064 | if (!scontext) |
| 1065 | goto out; |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 1066 | scontextp = kmalloc(*scontext_len, GFP_ATOMIC); |
Serge E. Hallyn | 0cccca0 | 2006-05-15 09:43:48 -0700 | [diff] [blame] | 1067 | if (!scontextp) { |
| 1068 | rc = -ENOMEM; |
| 1069 | goto out; |
| 1070 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | strcpy(scontextp, initial_sid_to_string[sid]); |
| 1072 | *scontext = scontextp; |
| 1073 | goto out; |
| 1074 | } |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1075 | printk(KERN_ERR "SELinux: %s: called before initial " |
| 1076 | "load_policy on unknown SID %d\n", __func__, sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | rc = -EINVAL; |
| 1078 | goto out; |
| 1079 | } |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 1080 | read_lock(&policy_rwlock); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1081 | if (force) |
| 1082 | context = sidtab_search_force(&sidtab, sid); |
| 1083 | else |
| 1084 | context = sidtab_search(&sidtab, sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | if (!context) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1086 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
| 1087 | __func__, sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | rc = -EINVAL; |
| 1089 | goto out_unlock; |
| 1090 | } |
| 1091 | rc = context_struct_to_string(context, scontext, scontext_len); |
| 1092 | out_unlock: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 1093 | read_unlock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | out: |
| 1095 | return rc; |
| 1096 | |
| 1097 | } |
| 1098 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1099 | /** |
| 1100 | * security_sid_to_context - Obtain a context for a given SID. |
| 1101 | * @sid: security identifier, SID |
| 1102 | * @scontext: security context |
| 1103 | * @scontext_len: length in bytes |
| 1104 | * |
| 1105 | * Write the string representation of the context associated with @sid |
| 1106 | * into a dynamically allocated string of the correct size. Set @scontext |
| 1107 | * to point to this string and set @scontext_len to the length of the string. |
| 1108 | */ |
| 1109 | int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | { |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1111 | return security_sid_to_context_core(sid, scontext, scontext_len, 0); |
| 1112 | } |
| 1113 | |
| 1114 | int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len) |
| 1115 | { |
| 1116 | return security_sid_to_context_core(sid, scontext, scontext_len, 1); |
| 1117 | } |
| 1118 | |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1119 | /* |
| 1120 | * Caveat: Mutates scontext. |
| 1121 | */ |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1122 | static int string_to_context_struct(struct policydb *pol, |
| 1123 | struct sidtab *sidtabp, |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1124 | char *scontext, |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1125 | u32 scontext_len, |
| 1126 | struct context *ctx, |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1127 | u32 def_sid) |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1128 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | struct role_datum *role; |
| 1130 | struct type_datum *typdatum; |
| 1131 | struct user_datum *usrdatum; |
| 1132 | char *scontextp, *p, oldc; |
| 1133 | int rc = 0; |
| 1134 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1135 | context_init(ctx); |
| 1136 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1137 | /* Parse the security context. */ |
| 1138 | |
| 1139 | rc = -EINVAL; |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1140 | scontextp = (char *) scontext; |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1141 | |
| 1142 | /* Extract the user. */ |
| 1143 | p = scontextp; |
| 1144 | while (*p && *p != ':') |
| 1145 | p++; |
| 1146 | |
| 1147 | if (*p == 0) |
| 1148 | goto out; |
| 1149 | |
| 1150 | *p++ = 0; |
| 1151 | |
| 1152 | usrdatum = hashtab_search(pol->p_users.table, scontextp); |
| 1153 | if (!usrdatum) |
| 1154 | goto out; |
| 1155 | |
| 1156 | ctx->user = usrdatum->value; |
| 1157 | |
| 1158 | /* Extract role. */ |
| 1159 | scontextp = p; |
| 1160 | while (*p && *p != ':') |
| 1161 | p++; |
| 1162 | |
| 1163 | if (*p == 0) |
| 1164 | goto out; |
| 1165 | |
| 1166 | *p++ = 0; |
| 1167 | |
| 1168 | role = hashtab_search(pol->p_roles.table, scontextp); |
| 1169 | if (!role) |
| 1170 | goto out; |
| 1171 | ctx->role = role->value; |
| 1172 | |
| 1173 | /* Extract type. */ |
| 1174 | scontextp = p; |
| 1175 | while (*p && *p != ':') |
| 1176 | p++; |
| 1177 | oldc = *p; |
| 1178 | *p++ = 0; |
| 1179 | |
| 1180 | typdatum = hashtab_search(pol->p_types.table, scontextp); |
KaiGai Kohei | d9250de | 2008-08-28 16:35:57 +0900 | [diff] [blame] | 1181 | if (!typdatum || typdatum->attribute) |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1182 | goto out; |
| 1183 | |
| 1184 | ctx->type = typdatum->value; |
| 1185 | |
| 1186 | rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid); |
| 1187 | if (rc) |
| 1188 | goto out; |
| 1189 | |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1190 | if ((p - scontext) < scontext_len) { |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1191 | rc = -EINVAL; |
| 1192 | goto out; |
| 1193 | } |
| 1194 | |
| 1195 | /* Check the validity of the new context. */ |
| 1196 | if (!policydb_context_isvalid(pol, ctx)) { |
| 1197 | rc = -EINVAL; |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1198 | goto out; |
| 1199 | } |
| 1200 | rc = 0; |
| 1201 | out: |
Eric Paris | 8e531af | 2008-09-03 11:49:47 -0400 | [diff] [blame] | 1202 | if (rc) |
| 1203 | context_destroy(ctx); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1204 | return rc; |
| 1205 | } |
| 1206 | |
| 1207 | static int security_context_to_sid_core(const char *scontext, u32 scontext_len, |
| 1208 | u32 *sid, u32 def_sid, gfp_t gfp_flags, |
| 1209 | int force) |
| 1210 | { |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1211 | char *scontext2, *str = NULL; |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1212 | struct context context; |
| 1213 | int rc = 0; |
| 1214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | if (!ss_initialized) { |
| 1216 | int i; |
| 1217 | |
| 1218 | for (i = 1; i < SECINITSID_NUM; i++) { |
| 1219 | if (!strcmp(initial_sid_to_string[i], scontext)) { |
| 1220 | *sid = i; |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1221 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | *sid = SECINITSID_KERNEL; |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1225 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | } |
| 1227 | *sid = SECSID_NULL; |
| 1228 | |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1229 | /* Copy the string so that we can modify the copy as we parse it. */ |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 1230 | scontext2 = kmalloc(scontext_len + 1, gfp_flags); |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1231 | if (!scontext2) |
| 1232 | return -ENOMEM; |
| 1233 | memcpy(scontext2, scontext, scontext_len); |
| 1234 | scontext2[scontext_len] = 0; |
| 1235 | |
| 1236 | if (force) { |
| 1237 | /* Save another copy for storing in uninterpreted form */ |
| 1238 | str = kstrdup(scontext2, gfp_flags); |
| 1239 | if (!str) { |
| 1240 | kfree(scontext2); |
| 1241 | return -ENOMEM; |
| 1242 | } |
| 1243 | } |
| 1244 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 1245 | read_lock(&policy_rwlock); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1246 | rc = string_to_context_struct(&policydb, &sidtab, |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1247 | scontext2, scontext_len, |
| 1248 | &context, def_sid); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1249 | if (rc == -EINVAL && force) { |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1250 | context.str = str; |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1251 | context.len = scontext_len; |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1252 | str = NULL; |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1253 | } else if (rc) |
| 1254 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | rc = sidtab_context_to_sid(&sidtab, &context, sid); |
Eric Paris | 8e531af | 2008-09-03 11:49:47 -0400 | [diff] [blame] | 1256 | context_destroy(&context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | out: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 1258 | read_unlock(&policy_rwlock); |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1259 | kfree(scontext2); |
| 1260 | kfree(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | return rc; |
| 1262 | } |
| 1263 | |
James Morris | f5c1d5b | 2005-07-28 01:07:37 -0700 | [diff] [blame] | 1264 | /** |
| 1265 | * security_context_to_sid - Obtain a SID for a given security context. |
| 1266 | * @scontext: security context |
| 1267 | * @scontext_len: length in bytes |
| 1268 | * @sid: security identifier, SID |
| 1269 | * |
| 1270 | * Obtains a SID associated with the security context that |
| 1271 | * has the string representation specified by @scontext. |
| 1272 | * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient |
| 1273 | * memory is available, or 0 on success. |
| 1274 | */ |
David Howells | 8f0cfa5 | 2008-04-29 00:59:41 -0700 | [diff] [blame] | 1275 | int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid) |
James Morris | f5c1d5b | 2005-07-28 01:07:37 -0700 | [diff] [blame] | 1276 | { |
| 1277 | return security_context_to_sid_core(scontext, scontext_len, |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1278 | sid, SECSID_NULL, GFP_KERNEL, 0); |
James Morris | f5c1d5b | 2005-07-28 01:07:37 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | /** |
| 1282 | * security_context_to_sid_default - Obtain a SID for a given security context, |
| 1283 | * falling back to specified default if needed. |
| 1284 | * |
| 1285 | * @scontext: security context |
| 1286 | * @scontext_len: length in bytes |
| 1287 | * @sid: security identifier, SID |
Gabriel Craciunescu | d133a96 | 2007-07-31 00:39:19 -0700 | [diff] [blame] | 1288 | * @def_sid: default SID to assign on error |
James Morris | f5c1d5b | 2005-07-28 01:07:37 -0700 | [diff] [blame] | 1289 | * |
| 1290 | * Obtains a SID associated with the security context that |
| 1291 | * has the string representation specified by @scontext. |
| 1292 | * The default SID is passed to the MLS layer to be used to allow |
| 1293 | * kernel labeling of the MLS field if the MLS field is not present |
| 1294 | * (for upgrading to MLS without full relabel). |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1295 | * Implicitly forces adding of the context even if it cannot be mapped yet. |
James Morris | f5c1d5b | 2005-07-28 01:07:37 -0700 | [diff] [blame] | 1296 | * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient |
| 1297 | * memory is available, or 0 on success. |
| 1298 | */ |
David Howells | 7bf570d | 2008-04-29 20:52:51 +0100 | [diff] [blame] | 1299 | int security_context_to_sid_default(const char *scontext, u32 scontext_len, |
| 1300 | u32 *sid, u32 def_sid, gfp_t gfp_flags) |
James Morris | f5c1d5b | 2005-07-28 01:07:37 -0700 | [diff] [blame] | 1301 | { |
| 1302 | return security_context_to_sid_core(scontext, scontext_len, |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1303 | sid, def_sid, gfp_flags, 1); |
| 1304 | } |
| 1305 | |
| 1306 | int security_context_to_sid_force(const char *scontext, u32 scontext_len, |
| 1307 | u32 *sid) |
| 1308 | { |
| 1309 | return security_context_to_sid_core(scontext, scontext_len, |
| 1310 | sid, SECSID_NULL, GFP_KERNEL, 1); |
James Morris | f5c1d5b | 2005-07-28 01:07:37 -0700 | [diff] [blame] | 1311 | } |
| 1312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | static int compute_sid_handle_invalid_context( |
| 1314 | struct context *scontext, |
| 1315 | struct context *tcontext, |
| 1316 | u16 tclass, |
| 1317 | struct context *newcontext) |
| 1318 | { |
| 1319 | char *s = NULL, *t = NULL, *n = NULL; |
| 1320 | u32 slen, tlen, nlen; |
| 1321 | |
| 1322 | if (context_struct_to_string(scontext, &s, &slen) < 0) |
| 1323 | goto out; |
| 1324 | if (context_struct_to_string(tcontext, &t, &tlen) < 0) |
| 1325 | goto out; |
| 1326 | if (context_struct_to_string(newcontext, &n, &nlen) < 0) |
| 1327 | goto out; |
David Woodhouse | 9ad9ad3 | 2005-06-22 15:04:33 +0100 | [diff] [blame] | 1328 | audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | "security_compute_sid: invalid context %s" |
| 1330 | " for scontext=%s" |
| 1331 | " tcontext=%s" |
| 1332 | " tclass=%s", |
| 1333 | n, s, t, policydb.p_class_val_to_name[tclass-1]); |
| 1334 | out: |
| 1335 | kfree(s); |
| 1336 | kfree(t); |
| 1337 | kfree(n); |
| 1338 | if (!selinux_enforcing) |
| 1339 | return 0; |
| 1340 | return -EACCES; |
| 1341 | } |
| 1342 | |
| 1343 | static int security_compute_sid(u32 ssid, |
| 1344 | u32 tsid, |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1345 | u16 orig_tclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | u32 specified, |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1347 | u32 *out_sid, |
| 1348 | bool kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | { |
| 1350 | struct context *scontext = NULL, *tcontext = NULL, newcontext; |
| 1351 | struct role_trans *roletr = NULL; |
| 1352 | struct avtab_key avkey; |
| 1353 | struct avtab_datum *avdatum; |
| 1354 | struct avtab_node *node; |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1355 | u16 tclass; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | int rc = 0; |
| 1357 | |
| 1358 | if (!ss_initialized) { |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1359 | switch (orig_tclass) { |
| 1360 | case SECCLASS_PROCESS: /* kernel value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | *out_sid = ssid; |
| 1362 | break; |
| 1363 | default: |
| 1364 | *out_sid = tsid; |
| 1365 | break; |
| 1366 | } |
| 1367 | goto out; |
| 1368 | } |
| 1369 | |
Venkat Yekkirala | 851f8a6 | 2006-07-30 03:03:18 -0700 | [diff] [blame] | 1370 | context_init(&newcontext); |
| 1371 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 1372 | read_lock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1374 | if (kern) |
| 1375 | tclass = unmap_class(orig_tclass); |
| 1376 | else |
| 1377 | tclass = orig_tclass; |
| 1378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | scontext = sidtab_search(&sidtab, ssid); |
| 1380 | if (!scontext) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1381 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
| 1382 | __func__, ssid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | rc = -EINVAL; |
| 1384 | goto out_unlock; |
| 1385 | } |
| 1386 | tcontext = sidtab_search(&sidtab, tsid); |
| 1387 | if (!tcontext) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 1388 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
| 1389 | __func__, tsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | rc = -EINVAL; |
| 1391 | goto out_unlock; |
| 1392 | } |
| 1393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | /* Set the user identity. */ |
| 1395 | switch (specified) { |
| 1396 | case AVTAB_TRANSITION: |
| 1397 | case AVTAB_CHANGE: |
| 1398 | /* Use the process user identity. */ |
| 1399 | newcontext.user = scontext->user; |
| 1400 | break; |
| 1401 | case AVTAB_MEMBER: |
| 1402 | /* Use the related object owner. */ |
| 1403 | newcontext.user = tcontext->user; |
| 1404 | break; |
| 1405 | } |
| 1406 | |
| 1407 | /* Set the role and type to default values. */ |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1408 | if (tclass == policydb.process_class) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | /* Use the current role and type of process. */ |
| 1410 | newcontext.role = scontext->role; |
| 1411 | newcontext.type = scontext->type; |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1412 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | /* Use the well-defined object role. */ |
| 1414 | newcontext.role = OBJECT_R_VAL; |
| 1415 | /* Use the type of the related object. */ |
| 1416 | newcontext.type = tcontext->type; |
| 1417 | } |
| 1418 | |
| 1419 | /* Look for a type transition/member/change rule. */ |
| 1420 | avkey.source_type = scontext->type; |
| 1421 | avkey.target_type = tcontext->type; |
| 1422 | avkey.target_class = tclass; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 1423 | avkey.specified = specified; |
| 1424 | avdatum = avtab_search(&policydb.te_avtab, &avkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | |
| 1426 | /* If no permanent rule, also check for enabled conditional rules */ |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 1427 | if (!avdatum) { |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 1428 | node = avtab_search_node(&policydb.te_cond_avtab, &avkey); |
Vesa-Matti Kari | dbc74c6 | 2008-08-07 03:18:20 +0300 | [diff] [blame] | 1429 | for (; node; node = avtab_search_node_next(node, specified)) { |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 1430 | if (node->key.specified & AVTAB_ENABLED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | avdatum = &node->datum; |
| 1432 | break; |
| 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 1437 | if (avdatum) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | /* Use the type from the type transition/member/change rule. */ |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 1439 | newcontext.type = avdatum->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | /* Check for class-specific changes. */ |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1443 | if (tclass == policydb.process_class) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | if (specified & AVTAB_TRANSITION) { |
| 1445 | /* Look for a role transition rule. */ |
| 1446 | for (roletr = policydb.role_tr; roletr; |
| 1447 | roletr = roletr->next) { |
| 1448 | if (roletr->role == scontext->role && |
| 1449 | roletr->type == tcontext->type) { |
| 1450 | /* Use the role transition rule. */ |
| 1451 | newcontext.role = roletr->new_role; |
| 1452 | break; |
| 1453 | } |
| 1454 | } |
| 1455 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | /* Set the MLS attributes. |
| 1459 | This is done last because it may allocate memory. */ |
| 1460 | rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext); |
| 1461 | if (rc) |
| 1462 | goto out_unlock; |
| 1463 | |
| 1464 | /* Check the validity of the context. */ |
| 1465 | if (!policydb_context_isvalid(&policydb, &newcontext)) { |
| 1466 | rc = compute_sid_handle_invalid_context(scontext, |
| 1467 | tcontext, |
| 1468 | tclass, |
| 1469 | &newcontext); |
| 1470 | if (rc) |
| 1471 | goto out_unlock; |
| 1472 | } |
| 1473 | /* Obtain the sid for the context. */ |
| 1474 | rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid); |
| 1475 | out_unlock: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 1476 | read_unlock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | context_destroy(&newcontext); |
| 1478 | out: |
| 1479 | return rc; |
| 1480 | } |
| 1481 | |
| 1482 | /** |
| 1483 | * security_transition_sid - Compute the SID for a new subject/object. |
| 1484 | * @ssid: source security identifier |
| 1485 | * @tsid: target security identifier |
| 1486 | * @tclass: target security class |
| 1487 | * @out_sid: security identifier for new subject/object |
| 1488 | * |
| 1489 | * Compute a SID to use for labeling a new subject or object in the |
| 1490 | * class @tclass based on a SID pair (@ssid, @tsid). |
| 1491 | * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM |
| 1492 | * if insufficient memory is available, or %0 if the new SID was |
| 1493 | * computed successfully. |
| 1494 | */ |
| 1495 | int security_transition_sid(u32 ssid, |
| 1496 | u32 tsid, |
| 1497 | u16 tclass, |
| 1498 | u32 *out_sid) |
| 1499 | { |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1500 | return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, |
| 1501 | out_sid, true); |
| 1502 | } |
| 1503 | |
| 1504 | int security_transition_sid_user(u32 ssid, |
| 1505 | u32 tsid, |
| 1506 | u16 tclass, |
| 1507 | u32 *out_sid) |
| 1508 | { |
| 1509 | return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, |
| 1510 | out_sid, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | /** |
| 1514 | * security_member_sid - Compute the SID for member selection. |
| 1515 | * @ssid: source security identifier |
| 1516 | * @tsid: target security identifier |
| 1517 | * @tclass: target security class |
| 1518 | * @out_sid: security identifier for selected member |
| 1519 | * |
| 1520 | * Compute a SID to use when selecting a member of a polyinstantiated |
| 1521 | * object of class @tclass based on a SID pair (@ssid, @tsid). |
| 1522 | * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM |
| 1523 | * if insufficient memory is available, or %0 if the SID was |
| 1524 | * computed successfully. |
| 1525 | */ |
| 1526 | int security_member_sid(u32 ssid, |
| 1527 | u32 tsid, |
| 1528 | u16 tclass, |
| 1529 | u32 *out_sid) |
| 1530 | { |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1531 | return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid, |
| 1532 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | /** |
| 1536 | * security_change_sid - Compute the SID for object relabeling. |
| 1537 | * @ssid: source security identifier |
| 1538 | * @tsid: target security identifier |
| 1539 | * @tclass: target security class |
| 1540 | * @out_sid: security identifier for selected member |
| 1541 | * |
| 1542 | * Compute a SID to use for relabeling an object of class @tclass |
| 1543 | * based on a SID pair (@ssid, @tsid). |
| 1544 | * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM |
| 1545 | * if insufficient memory is available, or %0 if the SID was |
| 1546 | * computed successfully. |
| 1547 | */ |
| 1548 | int security_change_sid(u32 ssid, |
| 1549 | u32 tsid, |
| 1550 | u16 tclass, |
| 1551 | u32 *out_sid) |
| 1552 | { |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1553 | return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid, |
| 1554 | false); |
Chad Sellers | b94c7e6 | 2006-11-06 12:38:18 -0500 | [diff] [blame] | 1555 | } |
| 1556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | /* Clone the SID into the new SID table. */ |
| 1558 | static int clone_sid(u32 sid, |
| 1559 | struct context *context, |
| 1560 | void *arg) |
| 1561 | { |
| 1562 | struct sidtab *s = arg; |
| 1563 | |
Guido Trentalancia | 42596ea | 2010-02-03 17:06:01 +0100 | [diff] [blame] | 1564 | if (sid > SECINITSID_NUM) |
| 1565 | return sidtab_insert(s, sid, context); |
| 1566 | else |
| 1567 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | static inline int convert_context_handle_invalid_context(struct context *context) |
| 1571 | { |
| 1572 | int rc = 0; |
| 1573 | |
| 1574 | if (selinux_enforcing) { |
| 1575 | rc = -EINVAL; |
| 1576 | } else { |
| 1577 | char *s; |
| 1578 | u32 len; |
| 1579 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1580 | if (!context_struct_to_string(context, &s, &len)) { |
| 1581 | printk(KERN_WARNING |
| 1582 | "SELinux: Context %s would be invalid if enforcing\n", |
| 1583 | s); |
| 1584 | kfree(s); |
| 1585 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | } |
| 1587 | return rc; |
| 1588 | } |
| 1589 | |
| 1590 | struct convert_context_args { |
| 1591 | struct policydb *oldp; |
| 1592 | struct policydb *newp; |
| 1593 | }; |
| 1594 | |
| 1595 | /* |
| 1596 | * Convert the values in the security context |
| 1597 | * structure `c' from the values specified |
| 1598 | * in the policy `p->oldp' to the values specified |
| 1599 | * in the policy `p->newp'. Verify that the |
| 1600 | * context is valid under the new policy. |
| 1601 | */ |
| 1602 | static int convert_context(u32 key, |
| 1603 | struct context *c, |
| 1604 | void *p) |
| 1605 | { |
| 1606 | struct convert_context_args *args; |
| 1607 | struct context oldc; |
Guido Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 1608 | struct ocontext *oc; |
| 1609 | struct mls_range *range; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | struct role_datum *role; |
| 1611 | struct type_datum *typdatum; |
| 1612 | struct user_datum *usrdatum; |
| 1613 | char *s; |
| 1614 | u32 len; |
Guido Trentalancia | 42596ea | 2010-02-03 17:06:01 +0100 | [diff] [blame] | 1615 | int rc = 0; |
| 1616 | |
| 1617 | if (key <= SECINITSID_NUM) |
| 1618 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | |
| 1620 | args = p; |
| 1621 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1622 | if (c->str) { |
| 1623 | struct context ctx; |
Stephen Smalley | 9a59daa | 2008-05-14 10:33:55 -0400 | [diff] [blame] | 1624 | s = kstrdup(c->str, GFP_KERNEL); |
| 1625 | if (!s) { |
| 1626 | rc = -ENOMEM; |
| 1627 | goto out; |
| 1628 | } |
| 1629 | rc = string_to_context_struct(args->newp, NULL, s, |
| 1630 | c->len, &ctx, SECSID_NULL); |
| 1631 | kfree(s); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1632 | if (!rc) { |
| 1633 | printk(KERN_INFO |
| 1634 | "SELinux: Context %s became valid (mapped).\n", |
| 1635 | c->str); |
| 1636 | /* Replace string with mapped representation. */ |
| 1637 | kfree(c->str); |
| 1638 | memcpy(c, &ctx, sizeof(*c)); |
| 1639 | goto out; |
| 1640 | } else if (rc == -EINVAL) { |
| 1641 | /* Retain string representation for later mapping. */ |
| 1642 | rc = 0; |
| 1643 | goto out; |
| 1644 | } else { |
| 1645 | /* Other error condition, e.g. ENOMEM. */ |
| 1646 | printk(KERN_ERR |
| 1647 | "SELinux: Unable to map context %s, rc = %d.\n", |
| 1648 | c->str, -rc); |
| 1649 | goto out; |
| 1650 | } |
| 1651 | } |
| 1652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | rc = context_cpy(&oldc, c); |
| 1654 | if (rc) |
| 1655 | goto out; |
| 1656 | |
| 1657 | rc = -EINVAL; |
| 1658 | |
| 1659 | /* Convert the user. */ |
| 1660 | usrdatum = hashtab_search(args->newp->p_users.table, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 1661 | args->oldp->p_user_val_to_name[c->user - 1]); |
| 1662 | if (!usrdatum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | c->user = usrdatum->value; |
| 1665 | |
| 1666 | /* Convert the role. */ |
| 1667 | role = hashtab_search(args->newp->p_roles.table, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 1668 | args->oldp->p_role_val_to_name[c->role - 1]); |
| 1669 | if (!role) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | c->role = role->value; |
| 1672 | |
| 1673 | /* Convert the type. */ |
| 1674 | typdatum = hashtab_search(args->newp->p_types.table, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 1675 | args->oldp->p_type_val_to_name[c->type - 1]); |
| 1676 | if (!typdatum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | c->type = typdatum->value; |
| 1679 | |
Guido Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 1680 | /* Convert the MLS fields if dealing with MLS policies */ |
| 1681 | if (args->oldp->mls_enabled && args->newp->mls_enabled) { |
| 1682 | rc = mls_convert_context(args->oldp, args->newp, c); |
| 1683 | if (rc) |
| 1684 | goto bad; |
| 1685 | } else if (args->oldp->mls_enabled && !args->newp->mls_enabled) { |
| 1686 | /* |
| 1687 | * Switching between MLS and non-MLS policy: |
| 1688 | * free any storage used by the MLS fields in the |
| 1689 | * context for all existing entries in the sidtab. |
| 1690 | */ |
| 1691 | mls_context_destroy(c); |
| 1692 | } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) { |
| 1693 | /* |
| 1694 | * Switching between non-MLS and MLS policy: |
| 1695 | * ensure that the MLS fields of the context for all |
| 1696 | * existing entries in the sidtab are filled in with a |
| 1697 | * suitable default value, likely taken from one of the |
| 1698 | * initial SIDs. |
| 1699 | */ |
| 1700 | oc = args->newp->ocontexts[OCON_ISID]; |
| 1701 | while (oc && oc->sid[0] != SECINITSID_UNLABELED) |
| 1702 | oc = oc->next; |
| 1703 | if (!oc) { |
| 1704 | printk(KERN_ERR "SELinux: unable to look up" |
| 1705 | " the initial SIDs list\n"); |
| 1706 | goto bad; |
| 1707 | } |
| 1708 | range = &oc->context[0].range; |
| 1709 | rc = mls_range_set(c, range); |
| 1710 | if (rc) |
| 1711 | goto bad; |
| 1712 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | |
| 1714 | /* Check the validity of the new context. */ |
| 1715 | if (!policydb_context_isvalid(args->newp, c)) { |
| 1716 | rc = convert_context_handle_invalid_context(&oldc); |
| 1717 | if (rc) |
| 1718 | goto bad; |
| 1719 | } |
| 1720 | |
| 1721 | context_destroy(&oldc); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1722 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | out: |
| 1724 | return rc; |
| 1725 | bad: |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1726 | /* Map old representation to string and save it. */ |
| 1727 | if (context_struct_to_string(&oldc, &s, &len)) |
| 1728 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | context_destroy(&oldc); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1730 | context_destroy(c); |
| 1731 | c->str = s; |
| 1732 | c->len = len; |
| 1733 | printk(KERN_INFO |
| 1734 | "SELinux: Context %s became invalid (unmapped).\n", |
| 1735 | c->str); |
| 1736 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | goto out; |
| 1738 | } |
| 1739 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 1740 | static void security_load_policycaps(void) |
| 1741 | { |
| 1742 | selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps, |
| 1743 | POLICYDB_CAPABILITY_NETPEER); |
Eric Paris | b0c636b | 2008-02-28 12:58:40 -0500 | [diff] [blame] | 1744 | selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps, |
| 1745 | POLICYDB_CAPABILITY_OPENPERM); |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 1746 | } |
| 1747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | extern void selinux_complete_init(void); |
Stephen Smalley | e900a7d | 2007-04-19 14:16:19 -0400 | [diff] [blame] | 1749 | static int security_preserve_bools(struct policydb *p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | |
| 1751 | /** |
| 1752 | * security_load_policy - Load a security policy configuration. |
| 1753 | * @data: binary policy data |
| 1754 | * @len: length of data in bytes |
| 1755 | * |
| 1756 | * Load a new set of security policy configuration data, |
| 1757 | * validate it and convert the SID table as necessary. |
| 1758 | * This function will flush the access vector cache after |
| 1759 | * loading the new policy. |
| 1760 | */ |
| 1761 | int security_load_policy(void *data, size_t len) |
| 1762 | { |
| 1763 | struct policydb oldpolicydb, newpolicydb; |
| 1764 | struct sidtab oldsidtab, newsidtab; |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1765 | struct selinux_mapping *oldmap, *map = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | struct convert_context_args args; |
| 1767 | u32 seqno; |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1768 | u16 map_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | int rc = 0; |
| 1770 | struct policy_file file = { data, len }, *fp = &file; |
| 1771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | if (!ss_initialized) { |
| 1773 | avtab_cache_init(); |
Eric Paris | a200005 | 2010-04-20 10:29:42 -0400 | [diff] [blame] | 1774 | rc = policydb_read(&policydb, fp); |
| 1775 | if (rc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | avtab_cache_destroy(); |
Eric Paris | a200005 | 2010-04-20 10:29:42 -0400 | [diff] [blame] | 1777 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | } |
Eric Paris | a200005 | 2010-04-20 10:29:42 -0400 | [diff] [blame] | 1779 | |
Eric Paris | cee74f4 | 2010-10-13 17:50:25 -0400 | [diff] [blame] | 1780 | policydb.len = len; |
Eric Paris | a200005 | 2010-04-20 10:29:42 -0400 | [diff] [blame] | 1781 | rc = selinux_set_mapping(&policydb, secclass_map, |
| 1782 | ¤t_mapping, |
| 1783 | ¤t_mapping_size); |
| 1784 | if (rc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | policydb_destroy(&policydb); |
| 1786 | avtab_cache_destroy(); |
Eric Paris | a200005 | 2010-04-20 10:29:42 -0400 | [diff] [blame] | 1787 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | } |
Eric Paris | a200005 | 2010-04-20 10:29:42 -0400 | [diff] [blame] | 1789 | |
| 1790 | rc = policydb_load_isids(&policydb, &sidtab); |
| 1791 | if (rc) { |
Chad Sellers | b94c7e6 | 2006-11-06 12:38:18 -0500 | [diff] [blame] | 1792 | policydb_destroy(&policydb); |
| 1793 | avtab_cache_destroy(); |
Eric Paris | a200005 | 2010-04-20 10:29:42 -0400 | [diff] [blame] | 1794 | return rc; |
Chad Sellers | b94c7e6 | 2006-11-06 12:38:18 -0500 | [diff] [blame] | 1795 | } |
Eric Paris | a200005 | 2010-04-20 10:29:42 -0400 | [diff] [blame] | 1796 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 1797 | security_load_policycaps(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | ss_initialized = 1; |
Stephen Smalley | 4c443d1 | 2005-05-16 21:53:52 -0700 | [diff] [blame] | 1799 | seqno = ++latest_granting; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | selinux_complete_init(); |
Stephen Smalley | 4c443d1 | 2005-05-16 21:53:52 -0700 | [diff] [blame] | 1801 | avc_ss_reset(seqno); |
| 1802 | selnl_notify_policyload(seqno); |
KaiGai Kohei | 1190416 | 2010-09-14 18:28:39 +0900 | [diff] [blame] | 1803 | selinux_status_update_policyload(seqno); |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 1804 | selinux_netlbl_cache_invalidate(); |
Venkat Yekkirala | 342a0cf | 2007-01-26 19:03:48 -0800 | [diff] [blame] | 1805 | selinux_xfrm_notify_policyload(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | return 0; |
| 1807 | } |
| 1808 | |
| 1809 | #if 0 |
| 1810 | sidtab_hash_eval(&sidtab, "sids"); |
| 1811 | #endif |
| 1812 | |
Eric Paris | a200005 | 2010-04-20 10:29:42 -0400 | [diff] [blame] | 1813 | rc = policydb_read(&newpolicydb, fp); |
| 1814 | if (rc) |
| 1815 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | |
Eric Paris | cee74f4 | 2010-10-13 17:50:25 -0400 | [diff] [blame] | 1817 | newpolicydb.len = len; |
Guido Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 1818 | /* If switching between different policy types, log MLS status */ |
| 1819 | if (policydb.mls_enabled && !newpolicydb.mls_enabled) |
| 1820 | printk(KERN_INFO "SELinux: Disabling MLS support...\n"); |
| 1821 | else if (!policydb.mls_enabled && newpolicydb.mls_enabled) |
| 1822 | printk(KERN_INFO "SELinux: Enabling MLS support...\n"); |
| 1823 | |
Guido Trentalancia | 42596ea | 2010-02-03 17:06:01 +0100 | [diff] [blame] | 1824 | rc = policydb_load_isids(&newpolicydb, &newsidtab); |
| 1825 | if (rc) { |
| 1826 | printk(KERN_ERR "SELinux: unable to load the initial SIDs\n"); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1827 | policydb_destroy(&newpolicydb); |
Guido Trentalancia | 42596ea | 2010-02-03 17:06:01 +0100 | [diff] [blame] | 1828 | return rc; |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | |
Eric Paris | a200005 | 2010-04-20 10:29:42 -0400 | [diff] [blame] | 1831 | rc = selinux_set_mapping(&newpolicydb, secclass_map, &map, &map_size); |
| 1832 | if (rc) |
Chad Sellers | b94c7e6 | 2006-11-06 12:38:18 -0500 | [diff] [blame] | 1833 | goto err; |
Chad Sellers | b94c7e6 | 2006-11-06 12:38:18 -0500 | [diff] [blame] | 1834 | |
Stephen Smalley | e900a7d | 2007-04-19 14:16:19 -0400 | [diff] [blame] | 1835 | rc = security_preserve_bools(&newpolicydb); |
| 1836 | if (rc) { |
James Morris | 454d972 | 2008-02-26 20:42:02 +1100 | [diff] [blame] | 1837 | printk(KERN_ERR "SELinux: unable to preserve booleans\n"); |
Stephen Smalley | e900a7d | 2007-04-19 14:16:19 -0400 | [diff] [blame] | 1838 | goto err; |
| 1839 | } |
| 1840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | /* Clone the SID table. */ |
| 1842 | sidtab_shutdown(&sidtab); |
Eric Paris | a200005 | 2010-04-20 10:29:42 -0400 | [diff] [blame] | 1843 | |
| 1844 | rc = sidtab_map(&sidtab, clone_sid, &newsidtab); |
| 1845 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1848 | /* |
| 1849 | * Convert the internal representations of contexts |
| 1850 | * in the new SID table. |
| 1851 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | args.oldp = &policydb; |
| 1853 | args.newp = &newpolicydb; |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1854 | rc = sidtab_map(&newsidtab, convert_context, &args); |
Guido Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 1855 | if (rc) { |
| 1856 | printk(KERN_ERR "SELinux: unable to convert the internal" |
| 1857 | " representation of contexts in the new SID" |
| 1858 | " table\n"); |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 1859 | goto err; |
Guido Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 1860 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | |
| 1862 | /* Save the old policydb and SID table to free later. */ |
| 1863 | memcpy(&oldpolicydb, &policydb, sizeof policydb); |
| 1864 | sidtab_set(&oldsidtab, &sidtab); |
| 1865 | |
| 1866 | /* Install the new policydb and SID table. */ |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 1867 | write_lock_irq(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | memcpy(&policydb, &newpolicydb, sizeof policydb); |
| 1869 | sidtab_set(&sidtab, &newsidtab); |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 1870 | security_load_policycaps(); |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1871 | oldmap = current_mapping; |
| 1872 | current_mapping = map; |
| 1873 | current_mapping_size = map_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | seqno = ++latest_granting; |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 1875 | write_unlock_irq(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | |
| 1877 | /* Free the old policydb and SID table. */ |
| 1878 | policydb_destroy(&oldpolicydb); |
| 1879 | sidtab_destroy(&oldsidtab); |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1880 | kfree(oldmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | |
| 1882 | avc_ss_reset(seqno); |
| 1883 | selnl_notify_policyload(seqno); |
KaiGai Kohei | 1190416 | 2010-09-14 18:28:39 +0900 | [diff] [blame] | 1884 | selinux_status_update_policyload(seqno); |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 1885 | selinux_netlbl_cache_invalidate(); |
Venkat Yekkirala | 342a0cf | 2007-01-26 19:03:48 -0800 | [diff] [blame] | 1886 | selinux_xfrm_notify_policyload(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | |
| 1888 | return 0; |
| 1889 | |
| 1890 | err: |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 1891 | kfree(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | sidtab_destroy(&newsidtab); |
| 1893 | policydb_destroy(&newpolicydb); |
| 1894 | return rc; |
| 1895 | |
| 1896 | } |
| 1897 | |
Eric Paris | cee74f4 | 2010-10-13 17:50:25 -0400 | [diff] [blame] | 1898 | size_t security_policydb_len(void) |
| 1899 | { |
| 1900 | size_t len; |
| 1901 | |
| 1902 | read_lock(&policy_rwlock); |
| 1903 | len = policydb.len; |
| 1904 | read_unlock(&policy_rwlock); |
| 1905 | |
| 1906 | return len; |
| 1907 | } |
| 1908 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | /** |
| 1910 | * security_port_sid - Obtain the SID for a port. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | * @protocol: protocol number |
| 1912 | * @port: port number |
| 1913 | * @out_sid: security identifier |
| 1914 | */ |
Paul Moore | 3e11217 | 2008-04-10 10:48:14 -0400 | [diff] [blame] | 1915 | int security_port_sid(u8 protocol, u16 port, u32 *out_sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | { |
| 1917 | struct ocontext *c; |
| 1918 | int rc = 0; |
| 1919 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 1920 | read_lock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | |
| 1922 | c = policydb.ocontexts[OCON_PORT]; |
| 1923 | while (c) { |
| 1924 | if (c->u.port.protocol == protocol && |
| 1925 | c->u.port.low_port <= port && |
| 1926 | c->u.port.high_port >= port) |
| 1927 | break; |
| 1928 | c = c->next; |
| 1929 | } |
| 1930 | |
| 1931 | if (c) { |
| 1932 | if (!c->sid[0]) { |
| 1933 | rc = sidtab_context_to_sid(&sidtab, |
| 1934 | &c->context[0], |
| 1935 | &c->sid[0]); |
| 1936 | if (rc) |
| 1937 | goto out; |
| 1938 | } |
| 1939 | *out_sid = c->sid[0]; |
| 1940 | } else { |
| 1941 | *out_sid = SECINITSID_PORT; |
| 1942 | } |
| 1943 | |
| 1944 | out: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 1945 | read_unlock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | return rc; |
| 1947 | } |
| 1948 | |
| 1949 | /** |
| 1950 | * security_netif_sid - Obtain the SID for a network interface. |
| 1951 | * @name: interface name |
| 1952 | * @if_sid: interface SID |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | */ |
Paul Moore | e8bfdb9 | 2008-01-29 08:38:08 -0500 | [diff] [blame] | 1954 | int security_netif_sid(char *name, u32 *if_sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | { |
| 1956 | int rc = 0; |
| 1957 | struct ocontext *c; |
| 1958 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 1959 | read_lock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | |
| 1961 | c = policydb.ocontexts[OCON_NETIF]; |
| 1962 | while (c) { |
| 1963 | if (strcmp(name, c->u.name) == 0) |
| 1964 | break; |
| 1965 | c = c->next; |
| 1966 | } |
| 1967 | |
| 1968 | if (c) { |
| 1969 | if (!c->sid[0] || !c->sid[1]) { |
| 1970 | rc = sidtab_context_to_sid(&sidtab, |
| 1971 | &c->context[0], |
| 1972 | &c->sid[0]); |
| 1973 | if (rc) |
| 1974 | goto out; |
| 1975 | rc = sidtab_context_to_sid(&sidtab, |
| 1976 | &c->context[1], |
| 1977 | &c->sid[1]); |
| 1978 | if (rc) |
| 1979 | goto out; |
| 1980 | } |
| 1981 | *if_sid = c->sid[0]; |
Paul Moore | e8bfdb9 | 2008-01-29 08:38:08 -0500 | [diff] [blame] | 1982 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | *if_sid = SECINITSID_NETIF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | |
| 1985 | out: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 1986 | read_unlock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | return rc; |
| 1988 | } |
| 1989 | |
| 1990 | static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask) |
| 1991 | { |
| 1992 | int i, fail = 0; |
| 1993 | |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 1994 | for (i = 0; i < 4; i++) |
| 1995 | if (addr[i] != (input[i] & mask[i])) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | fail = 1; |
| 1997 | break; |
| 1998 | } |
| 1999 | |
| 2000 | return !fail; |
| 2001 | } |
| 2002 | |
| 2003 | /** |
| 2004 | * security_node_sid - Obtain the SID for a node (host). |
| 2005 | * @domain: communication domain aka address family |
| 2006 | * @addrp: address |
| 2007 | * @addrlen: address length in bytes |
| 2008 | * @out_sid: security identifier |
| 2009 | */ |
| 2010 | int security_node_sid(u16 domain, |
| 2011 | void *addrp, |
| 2012 | u32 addrlen, |
| 2013 | u32 *out_sid) |
| 2014 | { |
| 2015 | int rc = 0; |
| 2016 | struct ocontext *c; |
| 2017 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2018 | read_lock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | |
| 2020 | switch (domain) { |
| 2021 | case AF_INET: { |
| 2022 | u32 addr; |
| 2023 | |
| 2024 | if (addrlen != sizeof(u32)) { |
| 2025 | rc = -EINVAL; |
| 2026 | goto out; |
| 2027 | } |
| 2028 | |
| 2029 | addr = *((u32 *)addrp); |
| 2030 | |
| 2031 | c = policydb.ocontexts[OCON_NODE]; |
| 2032 | while (c) { |
| 2033 | if (c->u.node.addr == (addr & c->u.node.mask)) |
| 2034 | break; |
| 2035 | c = c->next; |
| 2036 | } |
| 2037 | break; |
| 2038 | } |
| 2039 | |
| 2040 | case AF_INET6: |
| 2041 | if (addrlen != sizeof(u64) * 2) { |
| 2042 | rc = -EINVAL; |
| 2043 | goto out; |
| 2044 | } |
| 2045 | c = policydb.ocontexts[OCON_NODE6]; |
| 2046 | while (c) { |
| 2047 | if (match_ipv6_addrmask(addrp, c->u.node6.addr, |
| 2048 | c->u.node6.mask)) |
| 2049 | break; |
| 2050 | c = c->next; |
| 2051 | } |
| 2052 | break; |
| 2053 | |
| 2054 | default: |
| 2055 | *out_sid = SECINITSID_NODE; |
| 2056 | goto out; |
| 2057 | } |
| 2058 | |
| 2059 | if (c) { |
| 2060 | if (!c->sid[0]) { |
| 2061 | rc = sidtab_context_to_sid(&sidtab, |
| 2062 | &c->context[0], |
| 2063 | &c->sid[0]); |
| 2064 | if (rc) |
| 2065 | goto out; |
| 2066 | } |
| 2067 | *out_sid = c->sid[0]; |
| 2068 | } else { |
| 2069 | *out_sid = SECINITSID_NODE; |
| 2070 | } |
| 2071 | |
| 2072 | out: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2073 | read_unlock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | return rc; |
| 2075 | } |
| 2076 | |
| 2077 | #define SIDS_NEL 25 |
| 2078 | |
| 2079 | /** |
| 2080 | * security_get_user_sids - Obtain reachable SIDs for a user. |
| 2081 | * @fromsid: starting SID |
| 2082 | * @username: username |
| 2083 | * @sids: array of reachable SIDs for user |
| 2084 | * @nel: number of elements in @sids |
| 2085 | * |
| 2086 | * Generate the set of SIDs for legal security contexts |
| 2087 | * for a given user that can be reached by @fromsid. |
| 2088 | * Set *@sids to point to a dynamically allocated |
| 2089 | * array containing the set of SIDs. Set *@nel to the |
| 2090 | * number of elements in the array. |
| 2091 | */ |
| 2092 | |
| 2093 | int security_get_user_sids(u32 fromsid, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2094 | char *username, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | u32 **sids, |
| 2096 | u32 *nel) |
| 2097 | { |
| 2098 | struct context *fromcon, usercon; |
Stephen Smalley | 2c3c05d | 2007-06-07 15:34:10 -0400 | [diff] [blame] | 2099 | u32 *mysids = NULL, *mysids2, sid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | u32 mynel = 0, maxnel = SIDS_NEL; |
| 2101 | struct user_datum *user; |
| 2102 | struct role_datum *role; |
Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 2103 | struct ebitmap_node *rnode, *tnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | int rc = 0, i, j; |
| 2105 | |
Stephen Smalley | 2c3c05d | 2007-06-07 15:34:10 -0400 | [diff] [blame] | 2106 | *sids = NULL; |
| 2107 | *nel = 0; |
| 2108 | |
| 2109 | if (!ss_initialized) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2112 | read_lock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | |
Stephen Smalley | 12b29f3 | 2008-05-07 13:03:20 -0400 | [diff] [blame] | 2114 | context_init(&usercon); |
| 2115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | fromcon = sidtab_search(&sidtab, fromsid); |
| 2117 | if (!fromcon) { |
| 2118 | rc = -EINVAL; |
| 2119 | goto out_unlock; |
| 2120 | } |
| 2121 | |
| 2122 | user = hashtab_search(policydb.p_users.table, username); |
| 2123 | if (!user) { |
| 2124 | rc = -EINVAL; |
| 2125 | goto out_unlock; |
| 2126 | } |
| 2127 | usercon.user = user->value; |
| 2128 | |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 2129 | mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | if (!mysids) { |
| 2131 | rc = -ENOMEM; |
| 2132 | goto out_unlock; |
| 2133 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 2135 | ebitmap_for_each_positive_bit(&user->roles, rnode, i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | role = policydb.role_val_to_struct[i]; |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 2137 | usercon.role = i + 1; |
KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 2138 | ebitmap_for_each_positive_bit(&role->types, tnode, j) { |
wzt.wzt@gmail.com | c1a7368 | 2010-04-09 19:30:29 +0800 | [diff] [blame] | 2139 | usercon.type = j + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | |
| 2141 | if (mls_setup_user_range(fromcon, user, &usercon)) |
| 2142 | continue; |
| 2143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | rc = sidtab_context_to_sid(&sidtab, &usercon, &sid); |
Stephen Smalley | 2c3c05d | 2007-06-07 15:34:10 -0400 | [diff] [blame] | 2145 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | if (mynel < maxnel) { |
| 2148 | mysids[mynel++] = sid; |
| 2149 | } else { |
| 2150 | maxnel += SIDS_NEL; |
James Morris | 89d155e | 2005-10-30 14:59:21 -0800 | [diff] [blame] | 2151 | mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | if (!mysids2) { |
| 2153 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | goto out_unlock; |
| 2155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | memcpy(mysids2, mysids, mynel * sizeof(*mysids2)); |
| 2157 | kfree(mysids); |
| 2158 | mysids = mysids2; |
| 2159 | mysids[mynel++] = sid; |
| 2160 | } |
| 2161 | } |
| 2162 | } |
| 2163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | out_unlock: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2165 | read_unlock(&policy_rwlock); |
Stephen Smalley | 2c3c05d | 2007-06-07 15:34:10 -0400 | [diff] [blame] | 2166 | if (rc || !mynel) { |
| 2167 | kfree(mysids); |
| 2168 | goto out; |
| 2169 | } |
| 2170 | |
| 2171 | mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL); |
| 2172 | if (!mysids2) { |
| 2173 | rc = -ENOMEM; |
| 2174 | kfree(mysids); |
| 2175 | goto out; |
| 2176 | } |
| 2177 | for (i = 0, j = 0; i < mynel; i++) { |
| 2178 | rc = avc_has_perm_noaudit(fromsid, mysids[i], |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 2179 | SECCLASS_PROCESS, /* kernel value */ |
Stephen Smalley | 2c3c05d | 2007-06-07 15:34:10 -0400 | [diff] [blame] | 2180 | PROCESS__TRANSITION, AVC_STRICT, |
| 2181 | NULL); |
| 2182 | if (!rc) |
| 2183 | mysids2[j++] = mysids[i]; |
| 2184 | cond_resched(); |
| 2185 | } |
| 2186 | rc = 0; |
| 2187 | kfree(mysids); |
| 2188 | *sids = mysids2; |
| 2189 | *nel = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | out: |
| 2191 | return rc; |
| 2192 | } |
| 2193 | |
| 2194 | /** |
| 2195 | * security_genfs_sid - Obtain a SID for a file in a filesystem |
| 2196 | * @fstype: filesystem type |
| 2197 | * @path: path from root of mount |
| 2198 | * @sclass: file security class |
| 2199 | * @sid: SID for path |
| 2200 | * |
| 2201 | * Obtain a SID to use for a file in a filesystem that |
| 2202 | * cannot support xattr or use a fixed labeling behavior like |
| 2203 | * transition SIDs or task SIDs. |
| 2204 | */ |
| 2205 | int security_genfs_sid(const char *fstype, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2206 | char *path, |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 2207 | u16 orig_sclass, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | u32 *sid) |
| 2209 | { |
| 2210 | int len; |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 2211 | u16 sclass; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | struct genfs *genfs; |
| 2213 | struct ocontext *c; |
| 2214 | int rc = 0, cmp = 0; |
| 2215 | |
Stephen Smalley | b1aa530 | 2008-01-25 13:03:42 -0500 | [diff] [blame] | 2216 | while (path[0] == '/' && path[1] == '/') |
| 2217 | path++; |
| 2218 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2219 | read_lock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | |
Stephen Smalley | c6d3aaa | 2009-09-30 13:37:50 -0400 | [diff] [blame] | 2221 | sclass = unmap_class(orig_sclass); |
| 2222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | for (genfs = policydb.genfs; genfs; genfs = genfs->next) { |
| 2224 | cmp = strcmp(fstype, genfs->fstype); |
| 2225 | if (cmp <= 0) |
| 2226 | break; |
| 2227 | } |
| 2228 | |
| 2229 | if (!genfs || cmp) { |
| 2230 | *sid = SECINITSID_UNLABELED; |
| 2231 | rc = -ENOENT; |
| 2232 | goto out; |
| 2233 | } |
| 2234 | |
| 2235 | for (c = genfs->head; c; c = c->next) { |
| 2236 | len = strlen(c->u.name); |
| 2237 | if ((!c->v.sclass || sclass == c->v.sclass) && |
| 2238 | (strncmp(c->u.name, path, len) == 0)) |
| 2239 | break; |
| 2240 | } |
| 2241 | |
| 2242 | if (!c) { |
| 2243 | *sid = SECINITSID_UNLABELED; |
| 2244 | rc = -ENOENT; |
| 2245 | goto out; |
| 2246 | } |
| 2247 | |
| 2248 | if (!c->sid[0]) { |
| 2249 | rc = sidtab_context_to_sid(&sidtab, |
| 2250 | &c->context[0], |
| 2251 | &c->sid[0]); |
| 2252 | if (rc) |
| 2253 | goto out; |
| 2254 | } |
| 2255 | |
| 2256 | *sid = c->sid[0]; |
| 2257 | out: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2258 | read_unlock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | return rc; |
| 2260 | } |
| 2261 | |
| 2262 | /** |
| 2263 | * security_fs_use - Determine how to handle labeling for a filesystem. |
| 2264 | * @fstype: filesystem type |
| 2265 | * @behavior: labeling behavior |
| 2266 | * @sid: SID for filesystem (superblock) |
| 2267 | */ |
| 2268 | int security_fs_use( |
| 2269 | const char *fstype, |
| 2270 | unsigned int *behavior, |
James Morris | 089be43 | 2008-07-15 18:32:49 +1000 | [diff] [blame] | 2271 | u32 *sid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | { |
| 2273 | int rc = 0; |
| 2274 | struct ocontext *c; |
| 2275 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2276 | read_lock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | |
| 2278 | c = policydb.ocontexts[OCON_FSUSE]; |
| 2279 | while (c) { |
| 2280 | if (strcmp(fstype, c->u.name) == 0) |
| 2281 | break; |
| 2282 | c = c->next; |
| 2283 | } |
| 2284 | |
| 2285 | if (c) { |
| 2286 | *behavior = c->v.behavior; |
| 2287 | if (!c->sid[0]) { |
| 2288 | rc = sidtab_context_to_sid(&sidtab, |
| 2289 | &c->context[0], |
| 2290 | &c->sid[0]); |
| 2291 | if (rc) |
| 2292 | goto out; |
| 2293 | } |
| 2294 | *sid = c->sid[0]; |
| 2295 | } else { |
James Morris | 089be43 | 2008-07-15 18:32:49 +1000 | [diff] [blame] | 2296 | rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid); |
| 2297 | if (rc) { |
| 2298 | *behavior = SECURITY_FS_USE_NONE; |
| 2299 | rc = 0; |
| 2300 | } else { |
| 2301 | *behavior = SECURITY_FS_USE_GENFS; |
| 2302 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | out: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2306 | read_unlock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | return rc; |
| 2308 | } |
| 2309 | |
| 2310 | int security_get_bools(int *len, char ***names, int **values) |
| 2311 | { |
| 2312 | int i, rc = -ENOMEM; |
| 2313 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2314 | read_lock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | *names = NULL; |
| 2316 | *values = NULL; |
| 2317 | |
| 2318 | *len = policydb.p_bools.nprim; |
| 2319 | if (!*len) { |
| 2320 | rc = 0; |
| 2321 | goto out; |
| 2322 | } |
| 2323 | |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2324 | *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | if (!*names) |
| 2326 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | |
Jesper Juhl | e0795cf | 2006-01-09 20:54:46 -0800 | [diff] [blame] | 2328 | *values = kcalloc(*len, sizeof(int), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | if (!*values) |
| 2330 | goto err; |
| 2331 | |
| 2332 | for (i = 0; i < *len; i++) { |
| 2333 | size_t name_len; |
| 2334 | (*values)[i] = policydb.bool_val_to_struct[i]->state; |
| 2335 | name_len = strlen(policydb.p_bool_val_to_name[i]) + 1; |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2336 | (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | if (!(*names)[i]) |
| 2338 | goto err; |
| 2339 | strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len); |
| 2340 | (*names)[i][name_len - 1] = 0; |
| 2341 | } |
| 2342 | rc = 0; |
| 2343 | out: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2344 | read_unlock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | return rc; |
| 2346 | err: |
| 2347 | if (*names) { |
| 2348 | for (i = 0; i < *len; i++) |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 2349 | kfree((*names)[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | } |
Jesper Juhl | 9a5f04b | 2005-06-25 14:58:51 -0700 | [diff] [blame] | 2351 | kfree(*values); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | goto out; |
| 2353 | } |
| 2354 | |
| 2355 | |
| 2356 | int security_set_bools(int len, int *values) |
| 2357 | { |
| 2358 | int i, rc = 0; |
| 2359 | int lenp, seqno = 0; |
| 2360 | struct cond_node *cur; |
| 2361 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2362 | write_lock_irq(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | |
| 2364 | lenp = policydb.p_bools.nprim; |
| 2365 | if (len != lenp) { |
| 2366 | rc = -EFAULT; |
| 2367 | goto out; |
| 2368 | } |
| 2369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | for (i = 0; i < len; i++) { |
Steve Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 2371 | if (!!values[i] != policydb.bool_val_to_struct[i]->state) { |
| 2372 | audit_log(current->audit_context, GFP_ATOMIC, |
| 2373 | AUDIT_MAC_CONFIG_CHANGE, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2374 | "bool=%s val=%d old_val=%d auid=%u ses=%u", |
Steve Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 2375 | policydb.p_bool_val_to_name[i], |
| 2376 | !!values[i], |
| 2377 | policydb.bool_val_to_struct[i]->state, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2378 | audit_get_loginuid(current), |
| 2379 | audit_get_sessionid(current)); |
Steve Grubb | af601e4 | 2006-01-04 14:08:39 +0000 | [diff] [blame] | 2380 | } |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2381 | if (values[i]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | policydb.bool_val_to_struct[i]->state = 1; |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2383 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | policydb.bool_val_to_struct[i]->state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | |
Vesa-Matti Kari | dbc74c6 | 2008-08-07 03:18:20 +0300 | [diff] [blame] | 2387 | for (cur = policydb.cond_list; cur; cur = cur->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | rc = evaluate_cond_node(&policydb, cur); |
| 2389 | if (rc) |
| 2390 | goto out; |
| 2391 | } |
| 2392 | |
| 2393 | seqno = ++latest_granting; |
| 2394 | |
| 2395 | out: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2396 | write_unlock_irq(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | if (!rc) { |
| 2398 | avc_ss_reset(seqno); |
| 2399 | selnl_notify_policyload(seqno); |
KaiGai Kohei | 1190416 | 2010-09-14 18:28:39 +0900 | [diff] [blame] | 2400 | selinux_status_update_policyload(seqno); |
Venkat Yekkirala | 342a0cf | 2007-01-26 19:03:48 -0800 | [diff] [blame] | 2401 | selinux_xfrm_notify_policyload(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | } |
| 2403 | return rc; |
| 2404 | } |
| 2405 | |
| 2406 | int security_get_bool_value(int bool) |
| 2407 | { |
| 2408 | int rc = 0; |
| 2409 | int len; |
| 2410 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2411 | read_lock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | |
| 2413 | len = policydb.p_bools.nprim; |
| 2414 | if (bool >= len) { |
| 2415 | rc = -EFAULT; |
| 2416 | goto out; |
| 2417 | } |
| 2418 | |
| 2419 | rc = policydb.bool_val_to_struct[bool]->state; |
| 2420 | out: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2421 | read_unlock(&policy_rwlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | return rc; |
| 2423 | } |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2424 | |
Stephen Smalley | e900a7d | 2007-04-19 14:16:19 -0400 | [diff] [blame] | 2425 | static int security_preserve_bools(struct policydb *p) |
| 2426 | { |
| 2427 | int rc, nbools = 0, *bvalues = NULL, i; |
| 2428 | char **bnames = NULL; |
| 2429 | struct cond_bool_datum *booldatum; |
| 2430 | struct cond_node *cur; |
| 2431 | |
| 2432 | rc = security_get_bools(&nbools, &bnames, &bvalues); |
| 2433 | if (rc) |
| 2434 | goto out; |
| 2435 | for (i = 0; i < nbools; i++) { |
| 2436 | booldatum = hashtab_search(p->p_bools.table, bnames[i]); |
| 2437 | if (booldatum) |
| 2438 | booldatum->state = bvalues[i]; |
| 2439 | } |
Vesa-Matti Kari | dbc74c6 | 2008-08-07 03:18:20 +0300 | [diff] [blame] | 2440 | for (cur = p->cond_list; cur; cur = cur->next) { |
Stephen Smalley | e900a7d | 2007-04-19 14:16:19 -0400 | [diff] [blame] | 2441 | rc = evaluate_cond_node(p, cur); |
| 2442 | if (rc) |
| 2443 | goto out; |
| 2444 | } |
| 2445 | |
| 2446 | out: |
| 2447 | if (bnames) { |
| 2448 | for (i = 0; i < nbools; i++) |
| 2449 | kfree(bnames[i]); |
| 2450 | } |
| 2451 | kfree(bnames); |
| 2452 | kfree(bvalues); |
| 2453 | return rc; |
| 2454 | } |
| 2455 | |
Venkat Yekkirala | 08554d6 | 2006-07-24 23:27:16 -0700 | [diff] [blame] | 2456 | /* |
| 2457 | * security_sid_mls_copy() - computes a new sid based on the given |
| 2458 | * sid and the mls portion of mls_sid. |
| 2459 | */ |
| 2460 | int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid) |
| 2461 | { |
| 2462 | struct context *context1; |
| 2463 | struct context *context2; |
| 2464 | struct context newcon; |
| 2465 | char *s; |
| 2466 | u32 len; |
| 2467 | int rc = 0; |
| 2468 | |
Guido Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 2469 | if (!ss_initialized || !policydb.mls_enabled) { |
Venkat Yekkirala | 08554d6 | 2006-07-24 23:27:16 -0700 | [diff] [blame] | 2470 | *new_sid = sid; |
| 2471 | goto out; |
| 2472 | } |
| 2473 | |
| 2474 | context_init(&newcon); |
| 2475 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2476 | read_lock(&policy_rwlock); |
Venkat Yekkirala | 08554d6 | 2006-07-24 23:27:16 -0700 | [diff] [blame] | 2477 | context1 = sidtab_search(&sidtab, sid); |
| 2478 | if (!context1) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 2479 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
| 2480 | __func__, sid); |
Venkat Yekkirala | 08554d6 | 2006-07-24 23:27:16 -0700 | [diff] [blame] | 2481 | rc = -EINVAL; |
| 2482 | goto out_unlock; |
| 2483 | } |
| 2484 | |
| 2485 | context2 = sidtab_search(&sidtab, mls_sid); |
| 2486 | if (!context2) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 2487 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
| 2488 | __func__, mls_sid); |
Venkat Yekkirala | 08554d6 | 2006-07-24 23:27:16 -0700 | [diff] [blame] | 2489 | rc = -EINVAL; |
| 2490 | goto out_unlock; |
| 2491 | } |
| 2492 | |
| 2493 | newcon.user = context1->user; |
| 2494 | newcon.role = context1->role; |
| 2495 | newcon.type = context1->type; |
Venkat Yekkirala | 0efc61e | 2006-12-12 13:02:41 -0600 | [diff] [blame] | 2496 | rc = mls_context_cpy(&newcon, context2); |
Venkat Yekkirala | 08554d6 | 2006-07-24 23:27:16 -0700 | [diff] [blame] | 2497 | if (rc) |
| 2498 | goto out_unlock; |
| 2499 | |
Venkat Yekkirala | 08554d6 | 2006-07-24 23:27:16 -0700 | [diff] [blame] | 2500 | /* Check the validity of the new context. */ |
| 2501 | if (!policydb_context_isvalid(&policydb, &newcon)) { |
| 2502 | rc = convert_context_handle_invalid_context(&newcon); |
| 2503 | if (rc) |
| 2504 | goto bad; |
| 2505 | } |
| 2506 | |
| 2507 | rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid); |
| 2508 | goto out_unlock; |
| 2509 | |
| 2510 | bad: |
| 2511 | if (!context_struct_to_string(&newcon, &s, &len)) { |
| 2512 | audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR, |
| 2513 | "security_sid_mls_copy: invalid context %s", s); |
| 2514 | kfree(s); |
| 2515 | } |
| 2516 | |
| 2517 | out_unlock: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2518 | read_unlock(&policy_rwlock); |
Venkat Yekkirala | 08554d6 | 2006-07-24 23:27:16 -0700 | [diff] [blame] | 2519 | context_destroy(&newcon); |
| 2520 | out: |
| 2521 | return rc; |
| 2522 | } |
| 2523 | |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 2524 | /** |
| 2525 | * security_net_peersid_resolve - Compare and resolve two network peer SIDs |
| 2526 | * @nlbl_sid: NetLabel SID |
| 2527 | * @nlbl_type: NetLabel labeling protocol type |
| 2528 | * @xfrm_sid: XFRM SID |
| 2529 | * |
| 2530 | * Description: |
| 2531 | * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be |
| 2532 | * resolved into a single SID it is returned via @peer_sid and the function |
| 2533 | * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function |
| 2534 | * returns a negative value. A table summarizing the behavior is below: |
| 2535 | * |
| 2536 | * | function return | @sid |
| 2537 | * ------------------------------+-----------------+----------------- |
| 2538 | * no peer labels | 0 | SECSID_NULL |
| 2539 | * single peer label | 0 | <peer_label> |
| 2540 | * multiple, consistent labels | 0 | <peer_label> |
| 2541 | * multiple, inconsistent labels | -<errno> | SECSID_NULL |
| 2542 | * |
| 2543 | */ |
| 2544 | int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type, |
| 2545 | u32 xfrm_sid, |
| 2546 | u32 *peer_sid) |
| 2547 | { |
| 2548 | int rc; |
| 2549 | struct context *nlbl_ctx; |
| 2550 | struct context *xfrm_ctx; |
| 2551 | |
| 2552 | /* handle the common (which also happens to be the set of easy) cases |
| 2553 | * right away, these two if statements catch everything involving a |
| 2554 | * single or absent peer SID/label */ |
| 2555 | if (xfrm_sid == SECSID_NULL) { |
| 2556 | *peer_sid = nlbl_sid; |
| 2557 | return 0; |
| 2558 | } |
| 2559 | /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label |
| 2560 | * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label |
| 2561 | * is present */ |
| 2562 | if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) { |
| 2563 | *peer_sid = xfrm_sid; |
| 2564 | return 0; |
| 2565 | } |
| 2566 | |
| 2567 | /* we don't need to check ss_initialized here since the only way both |
| 2568 | * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the |
| 2569 | * security server was initialized and ss_initialized was true */ |
Guido Trentalancia | 0719aaf | 2010-02-03 16:40:20 +0100 | [diff] [blame] | 2570 | if (!policydb.mls_enabled) { |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 2571 | *peer_sid = SECSID_NULL; |
| 2572 | return 0; |
| 2573 | } |
| 2574 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2575 | read_lock(&policy_rwlock); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 2576 | |
| 2577 | nlbl_ctx = sidtab_search(&sidtab, nlbl_sid); |
| 2578 | if (!nlbl_ctx) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 2579 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
| 2580 | __func__, nlbl_sid); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 2581 | rc = -EINVAL; |
| 2582 | goto out_slowpath; |
| 2583 | } |
| 2584 | xfrm_ctx = sidtab_search(&sidtab, xfrm_sid); |
| 2585 | if (!xfrm_ctx) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 2586 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
| 2587 | __func__, xfrm_sid); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 2588 | rc = -EINVAL; |
| 2589 | goto out_slowpath; |
| 2590 | } |
| 2591 | rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES); |
| 2592 | |
| 2593 | out_slowpath: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2594 | read_unlock(&policy_rwlock); |
Paul Moore | 220deb9 | 2008-01-29 08:38:23 -0500 | [diff] [blame] | 2595 | if (rc == 0) |
| 2596 | /* at present NetLabel SIDs/labels really only carry MLS |
| 2597 | * information so if the MLS portion of the NetLabel SID |
| 2598 | * matches the MLS portion of the labeled XFRM SID/label |
| 2599 | * then pass along the XFRM SID as it is the most |
| 2600 | * expressive */ |
| 2601 | *peer_sid = xfrm_sid; |
| 2602 | else |
| 2603 | *peer_sid = SECSID_NULL; |
| 2604 | return rc; |
| 2605 | } |
| 2606 | |
Christopher J. PeBenito | 55fcf09 | 2007-05-23 09:12:06 -0400 | [diff] [blame] | 2607 | static int get_classes_callback(void *k, void *d, void *args) |
| 2608 | { |
| 2609 | struct class_datum *datum = d; |
| 2610 | char *name = k, **classes = args; |
| 2611 | int value = datum->value - 1; |
| 2612 | |
| 2613 | classes[value] = kstrdup(name, GFP_ATOMIC); |
| 2614 | if (!classes[value]) |
| 2615 | return -ENOMEM; |
| 2616 | |
| 2617 | return 0; |
| 2618 | } |
| 2619 | |
| 2620 | int security_get_classes(char ***classes, int *nclasses) |
| 2621 | { |
| 2622 | int rc = -ENOMEM; |
| 2623 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2624 | read_lock(&policy_rwlock); |
Christopher J. PeBenito | 55fcf09 | 2007-05-23 09:12:06 -0400 | [diff] [blame] | 2625 | |
| 2626 | *nclasses = policydb.p_classes.nprim; |
Julia Lawall | 9f59f90 | 2009-12-06 10:16:51 +0100 | [diff] [blame] | 2627 | *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC); |
Christopher J. PeBenito | 55fcf09 | 2007-05-23 09:12:06 -0400 | [diff] [blame] | 2628 | if (!*classes) |
| 2629 | goto out; |
| 2630 | |
| 2631 | rc = hashtab_map(policydb.p_classes.table, get_classes_callback, |
| 2632 | *classes); |
| 2633 | if (rc < 0) { |
| 2634 | int i; |
| 2635 | for (i = 0; i < *nclasses; i++) |
| 2636 | kfree((*classes)[i]); |
| 2637 | kfree(*classes); |
| 2638 | } |
| 2639 | |
| 2640 | out: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2641 | read_unlock(&policy_rwlock); |
Christopher J. PeBenito | 55fcf09 | 2007-05-23 09:12:06 -0400 | [diff] [blame] | 2642 | return rc; |
| 2643 | } |
| 2644 | |
| 2645 | static int get_permissions_callback(void *k, void *d, void *args) |
| 2646 | { |
| 2647 | struct perm_datum *datum = d; |
| 2648 | char *name = k, **perms = args; |
| 2649 | int value = datum->value - 1; |
| 2650 | |
| 2651 | perms[value] = kstrdup(name, GFP_ATOMIC); |
| 2652 | if (!perms[value]) |
| 2653 | return -ENOMEM; |
| 2654 | |
| 2655 | return 0; |
| 2656 | } |
| 2657 | |
| 2658 | int security_get_permissions(char *class, char ***perms, int *nperms) |
| 2659 | { |
| 2660 | int rc = -ENOMEM, i; |
| 2661 | struct class_datum *match; |
| 2662 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2663 | read_lock(&policy_rwlock); |
Christopher J. PeBenito | 55fcf09 | 2007-05-23 09:12:06 -0400 | [diff] [blame] | 2664 | |
| 2665 | match = hashtab_search(policydb.p_classes.table, class); |
| 2666 | if (!match) { |
Eric Paris | 744ba35 | 2008-04-17 11:52:44 -0400 | [diff] [blame] | 2667 | printk(KERN_ERR "SELinux: %s: unrecognized class %s\n", |
Harvey Harrison | dd6f953 | 2008-03-06 10:03:59 +1100 | [diff] [blame] | 2668 | __func__, class); |
Christopher J. PeBenito | 55fcf09 | 2007-05-23 09:12:06 -0400 | [diff] [blame] | 2669 | rc = -EINVAL; |
| 2670 | goto out; |
| 2671 | } |
| 2672 | |
| 2673 | *nperms = match->permissions.nprim; |
Julia Lawall | 9f59f90 | 2009-12-06 10:16:51 +0100 | [diff] [blame] | 2674 | *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC); |
Christopher J. PeBenito | 55fcf09 | 2007-05-23 09:12:06 -0400 | [diff] [blame] | 2675 | if (!*perms) |
| 2676 | goto out; |
| 2677 | |
| 2678 | if (match->comdatum) { |
| 2679 | rc = hashtab_map(match->comdatum->permissions.table, |
| 2680 | get_permissions_callback, *perms); |
| 2681 | if (rc < 0) |
| 2682 | goto err; |
| 2683 | } |
| 2684 | |
| 2685 | rc = hashtab_map(match->permissions.table, get_permissions_callback, |
| 2686 | *perms); |
| 2687 | if (rc < 0) |
| 2688 | goto err; |
| 2689 | |
| 2690 | out: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2691 | read_unlock(&policy_rwlock); |
Christopher J. PeBenito | 55fcf09 | 2007-05-23 09:12:06 -0400 | [diff] [blame] | 2692 | return rc; |
| 2693 | |
| 2694 | err: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2695 | read_unlock(&policy_rwlock); |
Christopher J. PeBenito | 55fcf09 | 2007-05-23 09:12:06 -0400 | [diff] [blame] | 2696 | for (i = 0; i < *nperms; i++) |
| 2697 | kfree((*perms)[i]); |
| 2698 | kfree(*perms); |
| 2699 | return rc; |
| 2700 | } |
| 2701 | |
Eric Paris | 3f12070 | 2007-09-21 14:37:10 -0400 | [diff] [blame] | 2702 | int security_get_reject_unknown(void) |
| 2703 | { |
| 2704 | return policydb.reject_unknown; |
| 2705 | } |
| 2706 | |
| 2707 | int security_get_allow_unknown(void) |
| 2708 | { |
| 2709 | return policydb.allow_unknown; |
| 2710 | } |
| 2711 | |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 2712 | /** |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 2713 | * security_policycap_supported - Check for a specific policy capability |
| 2714 | * @req_cap: capability |
| 2715 | * |
| 2716 | * Description: |
| 2717 | * This function queries the currently loaded policy to see if it supports the |
| 2718 | * capability specified by @req_cap. Returns true (1) if the capability is |
| 2719 | * supported, false (0) if it isn't supported. |
| 2720 | * |
| 2721 | */ |
| 2722 | int security_policycap_supported(unsigned int req_cap) |
| 2723 | { |
| 2724 | int rc; |
| 2725 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2726 | read_lock(&policy_rwlock); |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 2727 | rc = ebitmap_get_bit(&policydb.policycaps, req_cap); |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2728 | read_unlock(&policy_rwlock); |
Paul Moore | 3bb56b2 | 2008-01-29 08:38:19 -0500 | [diff] [blame] | 2729 | |
| 2730 | return rc; |
| 2731 | } |
| 2732 | |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2733 | struct selinux_audit_rule { |
| 2734 | u32 au_seqno; |
| 2735 | struct context au_ctxt; |
| 2736 | }; |
| 2737 | |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 2738 | void selinux_audit_rule_free(void *vrule) |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2739 | { |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 2740 | struct selinux_audit_rule *rule = vrule; |
| 2741 | |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2742 | if (rule) { |
| 2743 | context_destroy(&rule->au_ctxt); |
| 2744 | kfree(rule); |
| 2745 | } |
| 2746 | } |
| 2747 | |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 2748 | int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2749 | { |
| 2750 | struct selinux_audit_rule *tmprule; |
| 2751 | struct role_datum *roledatum; |
| 2752 | struct type_datum *typedatum; |
| 2753 | struct user_datum *userdatum; |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 2754 | struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule; |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2755 | int rc = 0; |
| 2756 | |
| 2757 | *rule = NULL; |
| 2758 | |
| 2759 | if (!ss_initialized) |
Steve G | 3ad40d6 | 2007-08-14 12:50:46 -0700 | [diff] [blame] | 2760 | return -EOPNOTSUPP; |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2761 | |
| 2762 | switch (field) { |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 2763 | case AUDIT_SUBJ_USER: |
| 2764 | case AUDIT_SUBJ_ROLE: |
| 2765 | case AUDIT_SUBJ_TYPE: |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 2766 | case AUDIT_OBJ_USER: |
| 2767 | case AUDIT_OBJ_ROLE: |
| 2768 | case AUDIT_OBJ_TYPE: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2769 | /* only 'equals' and 'not equals' fit user, role, and type */ |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 2770 | if (op != Audit_equal && op != Audit_not_equal) |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2771 | return -EINVAL; |
| 2772 | break; |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 2773 | case AUDIT_SUBJ_SEN: |
| 2774 | case AUDIT_SUBJ_CLR: |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 2775 | case AUDIT_OBJ_LEV_LOW: |
| 2776 | case AUDIT_OBJ_LEV_HIGH: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2777 | /* we do not allow a range, indicated by the presense of '-' */ |
| 2778 | if (strchr(rulestr, '-')) |
| 2779 | return -EINVAL; |
| 2780 | break; |
| 2781 | default: |
| 2782 | /* only the above fields are valid */ |
| 2783 | return -EINVAL; |
| 2784 | } |
| 2785 | |
| 2786 | tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL); |
| 2787 | if (!tmprule) |
| 2788 | return -ENOMEM; |
| 2789 | |
| 2790 | context_init(&tmprule->au_ctxt); |
| 2791 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2792 | read_lock(&policy_rwlock); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2793 | |
| 2794 | tmprule->au_seqno = latest_granting; |
| 2795 | |
| 2796 | switch (field) { |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 2797 | case AUDIT_SUBJ_USER: |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 2798 | case AUDIT_OBJ_USER: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2799 | userdatum = hashtab_search(policydb.p_users.table, rulestr); |
| 2800 | if (!userdatum) |
| 2801 | rc = -EINVAL; |
| 2802 | else |
| 2803 | tmprule->au_ctxt.user = userdatum->value; |
| 2804 | break; |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 2805 | case AUDIT_SUBJ_ROLE: |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 2806 | case AUDIT_OBJ_ROLE: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2807 | roledatum = hashtab_search(policydb.p_roles.table, rulestr); |
| 2808 | if (!roledatum) |
| 2809 | rc = -EINVAL; |
| 2810 | else |
| 2811 | tmprule->au_ctxt.role = roledatum->value; |
| 2812 | break; |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 2813 | case AUDIT_SUBJ_TYPE: |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 2814 | case AUDIT_OBJ_TYPE: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2815 | typedatum = hashtab_search(policydb.p_types.table, rulestr); |
| 2816 | if (!typedatum) |
| 2817 | rc = -EINVAL; |
| 2818 | else |
| 2819 | tmprule->au_ctxt.type = typedatum->value; |
| 2820 | break; |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 2821 | case AUDIT_SUBJ_SEN: |
| 2822 | case AUDIT_SUBJ_CLR: |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 2823 | case AUDIT_OBJ_LEV_LOW: |
| 2824 | case AUDIT_OBJ_LEV_HIGH: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2825 | rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC); |
| 2826 | break; |
| 2827 | } |
| 2828 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2829 | read_unlock(&policy_rwlock); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2830 | |
| 2831 | if (rc) { |
| 2832 | selinux_audit_rule_free(tmprule); |
| 2833 | tmprule = NULL; |
| 2834 | } |
| 2835 | |
| 2836 | *rule = tmprule; |
| 2837 | |
| 2838 | return rc; |
| 2839 | } |
| 2840 | |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 2841 | /* Check to see if the rule contains any selinux fields */ |
| 2842 | int selinux_audit_rule_known(struct audit_krule *rule) |
| 2843 | { |
| 2844 | int i; |
| 2845 | |
| 2846 | for (i = 0; i < rule->field_count; i++) { |
| 2847 | struct audit_field *f = &rule->fields[i]; |
| 2848 | switch (f->type) { |
| 2849 | case AUDIT_SUBJ_USER: |
| 2850 | case AUDIT_SUBJ_ROLE: |
| 2851 | case AUDIT_SUBJ_TYPE: |
| 2852 | case AUDIT_SUBJ_SEN: |
| 2853 | case AUDIT_SUBJ_CLR: |
| 2854 | case AUDIT_OBJ_USER: |
| 2855 | case AUDIT_OBJ_ROLE: |
| 2856 | case AUDIT_OBJ_TYPE: |
| 2857 | case AUDIT_OBJ_LEV_LOW: |
| 2858 | case AUDIT_OBJ_LEV_HIGH: |
| 2859 | return 1; |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | return 0; |
| 2864 | } |
| 2865 | |
| 2866 | int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule, |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 2867 | struct audit_context *actx) |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2868 | { |
| 2869 | struct context *ctxt; |
| 2870 | struct mls_level *level; |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 2871 | struct selinux_audit_rule *rule = vrule; |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2872 | int match = 0; |
| 2873 | |
| 2874 | if (!rule) { |
| 2875 | audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2876 | "selinux_audit_rule_match: missing rule\n"); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2877 | return -ENOENT; |
| 2878 | } |
| 2879 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2880 | read_lock(&policy_rwlock); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2881 | |
| 2882 | if (rule->au_seqno < latest_granting) { |
| 2883 | audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2884 | "selinux_audit_rule_match: stale rule\n"); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2885 | match = -ESTALE; |
| 2886 | goto out; |
| 2887 | } |
| 2888 | |
Stephen Smalley | 9a2f44f | 2006-09-25 23:31:58 -0700 | [diff] [blame] | 2889 | ctxt = sidtab_search(&sidtab, sid); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2890 | if (!ctxt) { |
| 2891 | audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2892 | "selinux_audit_rule_match: unrecognized SID %d\n", |
| 2893 | sid); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2894 | match = -ENOENT; |
| 2895 | goto out; |
| 2896 | } |
| 2897 | |
| 2898 | /* a field/op pair that is not caught here will simply fall through |
| 2899 | without a match */ |
| 2900 | switch (field) { |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 2901 | case AUDIT_SUBJ_USER: |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 2902 | case AUDIT_OBJ_USER: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2903 | switch (op) { |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 2904 | case Audit_equal: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2905 | match = (ctxt->user == rule->au_ctxt.user); |
| 2906 | break; |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 2907 | case Audit_not_equal: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2908 | match = (ctxt->user != rule->au_ctxt.user); |
| 2909 | break; |
| 2910 | } |
| 2911 | break; |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 2912 | case AUDIT_SUBJ_ROLE: |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 2913 | case AUDIT_OBJ_ROLE: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2914 | switch (op) { |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 2915 | case Audit_equal: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2916 | match = (ctxt->role == rule->au_ctxt.role); |
| 2917 | break; |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 2918 | case Audit_not_equal: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2919 | match = (ctxt->role != rule->au_ctxt.role); |
| 2920 | break; |
| 2921 | } |
| 2922 | break; |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 2923 | case AUDIT_SUBJ_TYPE: |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 2924 | case AUDIT_OBJ_TYPE: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2925 | switch (op) { |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 2926 | case Audit_equal: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2927 | match = (ctxt->type == rule->au_ctxt.type); |
| 2928 | break; |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 2929 | case Audit_not_equal: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2930 | match = (ctxt->type != rule->au_ctxt.type); |
| 2931 | break; |
| 2932 | } |
| 2933 | break; |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 2934 | case AUDIT_SUBJ_SEN: |
| 2935 | case AUDIT_SUBJ_CLR: |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 2936 | case AUDIT_OBJ_LEV_LOW: |
| 2937 | case AUDIT_OBJ_LEV_HIGH: |
| 2938 | level = ((field == AUDIT_SUBJ_SEN || |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2939 | field == AUDIT_OBJ_LEV_LOW) ? |
| 2940 | &ctxt->range.level[0] : &ctxt->range.level[1]); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2941 | switch (op) { |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 2942 | case Audit_equal: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2943 | match = mls_level_eq(&rule->au_ctxt.range.level[0], |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2944 | level); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2945 | break; |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 2946 | case Audit_not_equal: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2947 | match = !mls_level_eq(&rule->au_ctxt.range.level[0], |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2948 | level); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2949 | break; |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 2950 | case Audit_lt: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2951 | match = (mls_level_dom(&rule->au_ctxt.range.level[0], |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2952 | level) && |
| 2953 | !mls_level_eq(&rule->au_ctxt.range.level[0], |
| 2954 | level)); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2955 | break; |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 2956 | case Audit_le: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2957 | match = mls_level_dom(&rule->au_ctxt.range.level[0], |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2958 | level); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2959 | break; |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 2960 | case Audit_gt: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2961 | match = (mls_level_dom(level, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2962 | &rule->au_ctxt.range.level[0]) && |
| 2963 | !mls_level_eq(level, |
| 2964 | &rule->au_ctxt.range.level[0])); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2965 | break; |
Al Viro | 5af75d8 | 2008-12-16 05:59:26 -0500 | [diff] [blame] | 2966 | case Audit_ge: |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2967 | match = mls_level_dom(level, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2968 | &rule->au_ctxt.range.level[0]); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2969 | break; |
| 2970 | } |
| 2971 | } |
| 2972 | |
| 2973 | out: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 2974 | read_unlock(&policy_rwlock); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2975 | return match; |
| 2976 | } |
| 2977 | |
Ahmed S. Darwish | 9d57a7f | 2008-03-01 22:03:14 +0200 | [diff] [blame] | 2978 | static int (*aurule_callback)(void) = audit_update_lsm_rules; |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2979 | |
| 2980 | static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid, |
Eric Paris | f526971 | 2008-05-14 11:27:45 -0400 | [diff] [blame] | 2981 | u16 class, u32 perms, u32 *retained) |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2982 | { |
| 2983 | int err = 0; |
| 2984 | |
| 2985 | if (event == AVC_CALLBACK_RESET && aurule_callback) |
| 2986 | err = aurule_callback(); |
| 2987 | return err; |
| 2988 | } |
| 2989 | |
| 2990 | static int __init aurule_init(void) |
| 2991 | { |
| 2992 | int err; |
| 2993 | |
| 2994 | err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET, |
Eric Paris | 5d55a34 | 2008-04-18 17:38:33 -0400 | [diff] [blame] | 2995 | SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0); |
Darrel Goeddel | 376bd9c | 2006-02-24 15:44:05 -0600 | [diff] [blame] | 2996 | if (err) |
| 2997 | panic("avc_add_callback() failed, error %d\n", err); |
| 2998 | |
| 2999 | return err; |
| 3000 | } |
| 3001 | __initcall(aurule_init); |
| 3002 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3003 | #ifdef CONFIG_NETLABEL |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3004 | /** |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3005 | * security_netlbl_cache_add - Add an entry to the NetLabel cache |
| 3006 | * @secattr: the NetLabel packet security attributes |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 3007 | * @sid: the SELinux SID |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3008 | * |
| 3009 | * Description: |
| 3010 | * Attempt to cache the context in @ctx, which was derived from the packet in |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3011 | * @skb, in the NetLabel subsystem cache. This function assumes @secattr has |
| 3012 | * already been initialized. |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3013 | * |
| 3014 | */ |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3015 | static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr, |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 3016 | u32 sid) |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3017 | { |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 3018 | u32 *sid_cache; |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3019 | |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 3020 | sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC); |
| 3021 | if (sid_cache == NULL) |
| 3022 | return; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3023 | secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC); |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 3024 | if (secattr->cache == NULL) { |
| 3025 | kfree(sid_cache); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3026 | return; |
Jesper Juhl | 0ec8abd | 2007-07-21 00:12:44 +0200 | [diff] [blame] | 3027 | } |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3028 | |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 3029 | *sid_cache = sid; |
| 3030 | secattr->cache->free = kfree; |
| 3031 | secattr->cache->data = sid_cache; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3032 | secattr->flags |= NETLBL_SECATTR_CACHE; |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | /** |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3036 | * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3037 | * @secattr: the NetLabel packet security attributes |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3038 | * @sid: the SELinux SID |
| 3039 | * |
| 3040 | * Description: |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3041 | * Convert the given NetLabel security attributes in @secattr into a |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3042 | * SELinux SID. If the @secattr field does not contain a full SELinux |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 3043 | * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the |
| 3044 | * 'cache' field of @secattr is set and the CACHE flag is set; this is to |
| 3045 | * allow the @secattr to be used by NetLabel to cache the secattr to SID |
| 3046 | * conversion for future lookups. Returns zero on success, negative values on |
| 3047 | * failure. |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3048 | * |
| 3049 | */ |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3050 | int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr, |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3051 | u32 *sid) |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3052 | { |
| 3053 | int rc = -EIDRM; |
| 3054 | struct context *ctx; |
| 3055 | struct context ctx_new; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3056 | |
| 3057 | if (!ss_initialized) { |
| 3058 | *sid = SECSID_NULL; |
| 3059 | return 0; |
| 3060 | } |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3061 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 3062 | read_lock(&policy_rwlock); |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3063 | |
Paul Moore | 701a90b | 2006-11-17 17:38:46 -0500 | [diff] [blame] | 3064 | if (secattr->flags & NETLBL_SECATTR_CACHE) { |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 3065 | *sid = *(u32 *)secattr->cache->data; |
| 3066 | rc = 0; |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 3067 | } else if (secattr->flags & NETLBL_SECATTR_SECID) { |
| 3068 | *sid = secattr->attr.secid; |
| 3069 | rc = 0; |
Paul Moore | 701a90b | 2006-11-17 17:38:46 -0500 | [diff] [blame] | 3070 | } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) { |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 3071 | ctx = sidtab_search(&sidtab, SECINITSID_NETMSG); |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3072 | if (ctx == NULL) |
| 3073 | goto netlbl_secattr_to_sid_return; |
| 3074 | |
Paul Moore | 81990fb | 2008-10-03 10:51:15 -0400 | [diff] [blame] | 3075 | context_init(&ctx_new); |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3076 | ctx_new.user = ctx->user; |
| 3077 | ctx_new.role = ctx->role; |
| 3078 | ctx_new.type = ctx->type; |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 3079 | mls_import_netlbl_lvl(&ctx_new, secattr); |
Paul Moore | 701a90b | 2006-11-17 17:38:46 -0500 | [diff] [blame] | 3080 | if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 3081 | if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat, |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 3082 | secattr->attr.mls.cat) != 0) |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3083 | goto netlbl_secattr_to_sid_return; |
Paul Moore | 81990fb | 2008-10-03 10:51:15 -0400 | [diff] [blame] | 3084 | memcpy(&ctx_new.range.level[1].cat, |
| 3085 | &ctx_new.range.level[0].cat, |
| 3086 | sizeof(ctx_new.range.level[0].cat)); |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3087 | } |
| 3088 | if (mls_context_isvalid(&policydb, &ctx_new) != 1) |
| 3089 | goto netlbl_secattr_to_sid_return_cleanup; |
| 3090 | |
| 3091 | rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid); |
| 3092 | if (rc != 0) |
| 3093 | goto netlbl_secattr_to_sid_return_cleanup; |
| 3094 | |
Paul Moore | 5dbe1eb | 2008-01-29 08:44:18 -0500 | [diff] [blame] | 3095 | security_netlbl_cache_add(secattr, *sid); |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3096 | |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3097 | ebitmap_destroy(&ctx_new.range.level[0].cat); |
| 3098 | } else { |
paul.moore@hp.com | 388b240 | 2006-10-05 18:28:24 -0400 | [diff] [blame] | 3099 | *sid = SECSID_NULL; |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3100 | rc = 0; |
| 3101 | } |
| 3102 | |
| 3103 | netlbl_secattr_to_sid_return: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 3104 | read_unlock(&policy_rwlock); |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3105 | return rc; |
| 3106 | netlbl_secattr_to_sid_return_cleanup: |
| 3107 | ebitmap_destroy(&ctx_new.range.level[0].cat); |
| 3108 | goto netlbl_secattr_to_sid_return; |
| 3109 | } |
| 3110 | |
| 3111 | /** |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3112 | * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr |
| 3113 | * @sid: the SELinux SID |
| 3114 | * @secattr: the NetLabel packet security attributes |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3115 | * |
| 3116 | * Description: |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3117 | * Convert the given SELinux SID in @sid into a NetLabel security attribute. |
| 3118 | * Returns zero on success, negative values on failure. |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3119 | * |
| 3120 | */ |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3121 | int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr) |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3122 | { |
Paul Moore | 99d854d | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 3123 | int rc; |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3124 | struct context *ctx; |
| 3125 | |
| 3126 | if (!ss_initialized) |
| 3127 | return 0; |
| 3128 | |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 3129 | read_lock(&policy_rwlock); |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3130 | ctx = sidtab_search(&sidtab, sid); |
Paul Moore | 99d854d | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 3131 | if (ctx == NULL) { |
| 3132 | rc = -ENOENT; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3133 | goto netlbl_sid_to_secattr_failure; |
Paul Moore | 99d854d | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 3134 | } |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3135 | secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1], |
| 3136 | GFP_ATOMIC); |
Paul Moore | 99d854d | 2008-10-10 10:16:30 -0400 | [diff] [blame] | 3137 | if (secattr->domain == NULL) { |
| 3138 | rc = -ENOMEM; |
| 3139 | goto netlbl_sid_to_secattr_failure; |
| 3140 | } |
Paul Moore | 8d75899 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 3141 | secattr->attr.secid = sid; |
| 3142 | secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID; |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3143 | mls_export_netlbl_lvl(ctx, secattr); |
| 3144 | rc = mls_export_netlbl_cat(ctx, secattr); |
Paul Moore | bf0edf3 | 2006-10-11 19:10:48 -0400 | [diff] [blame] | 3145 | if (rc != 0) |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3146 | goto netlbl_sid_to_secattr_failure; |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 3147 | read_unlock(&policy_rwlock); |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3148 | |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3149 | return 0; |
Paul Moore | 9f2ad66 | 2006-11-17 17:38:53 -0500 | [diff] [blame] | 3150 | |
Paul Moore | 5778eab | 2007-02-28 15:14:22 -0500 | [diff] [blame] | 3151 | netlbl_sid_to_secattr_failure: |
James Morris | 0804d11 | 2008-06-06 18:40:29 +1000 | [diff] [blame] | 3152 | read_unlock(&policy_rwlock); |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 3153 | return rc; |
| 3154 | } |
Venkat Yekkirala | 7420ed2 | 2006-08-04 23:17:57 -0700 | [diff] [blame] | 3155 | #endif /* CONFIG_NETLABEL */ |
Eric Paris | cee74f4 | 2010-10-13 17:50:25 -0400 | [diff] [blame] | 3156 | |
| 3157 | /** |
| 3158 | * security_read_policy - read the policy. |
| 3159 | * @data: binary policy data |
| 3160 | * @len: length of data in bytes |
| 3161 | * |
| 3162 | */ |
| 3163 | int security_read_policy(void **data, ssize_t *len) |
| 3164 | { |
| 3165 | int rc; |
| 3166 | struct policy_file fp; |
| 3167 | |
| 3168 | if (!ss_initialized) |
| 3169 | return -EINVAL; |
| 3170 | |
| 3171 | *len = security_policydb_len(); |
| 3172 | |
Eric Paris | 845ca30 | 2010-10-13 17:50:31 -0400 | [diff] [blame] | 3173 | *data = vmalloc_user(*len); |
Eric Paris | cee74f4 | 2010-10-13 17:50:25 -0400 | [diff] [blame] | 3174 | if (!*data) |
| 3175 | return -ENOMEM; |
| 3176 | |
| 3177 | fp.data = *data; |
| 3178 | fp.len = *len; |
| 3179 | |
| 3180 | read_lock(&policy_rwlock); |
| 3181 | rc = policydb_write(&policydb, &fp); |
| 3182 | read_unlock(&policy_rwlock); |
| 3183 | |
| 3184 | if (rc) |
| 3185 | return rc; |
| 3186 | |
| 3187 | *len = (unsigned long)fp.data - (unsigned long)*data; |
| 3188 | return 0; |
| 3189 | |
| 3190 | } |