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