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