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