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