PageRenderTime 38ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/security/selinux/ss/services.c

https://github.com/mstsirkin/kvm
C | 2049 lines | 1477 code | 248 blank | 324 comment | 273 complexity | 7dfd6db004fbb451653190cc733620f3 MD5 | raw file
  1. /*
  2. * Implementation of the security services.
  3. *
  4. * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
  5. * James Morris <jmorris@redhat.com>
  6. *
  7. * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
  8. *
  9. * Support for enhanced MLS infrastructure.
  10. * Support for context based audit filters.
  11. *
  12. * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
  13. *
  14. * Added conditional policy language extensions
  15. *
  16. * Updated: Hewlett-Packard <paul@paul-moore.com>
  17. *
  18. * Added support for NetLabel
  19. * Added support for the policy capability bitmap
  20. *
  21. * Updated: Chad Sellers <csellers@tresys.com>
  22. *
  23. * Added validation of kernel classes and permissions
  24. *
  25. * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com>
  26. *
  27. * Added support for bounds domain and audit messaged on masked permissions
  28. *
  29. * Updated: Guido Trentalancia <guido@trentalancia.com>
  30. *
  31. * Added support for runtime switching of the policy type
  32. *
  33. * Copyright (C) 2008, 2009 NEC Corporation
  34. * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
  35. * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
  36. * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
  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
  39. * it under the terms of the GNU General Public License as published by
  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>
  46. #include <linux/rcupdate.h>
  47. #include <linux/errno.h>
  48. #include <linux/in.h>
  49. #include <linux/sched.h>
  50. #include <linux/audit.h>
  51. #include <linux/mutex.h>
  52. #include <linux/selinux.h>
  53. #include <linux/flex_array.h>
  54. #include <linux/vmalloc.h>
  55. #include <net/netlabel.h>
  56. #include "flask.h"
  57. #include "avc.h"
  58. #include "avc_ss.h"
  59. #include "security.h"
  60. #include "context.h"
  61. #include "policydb.h"
  62. #include "sidtab.h"
  63. #include "services.h"
  64. #include "conditional.h"
  65. #include "mls.h"
  66. #include "objsec.h"
  67. #include "netlabel.h"
  68. #include "xfrm.h"
  69. #include "ebitmap.h"
  70. #include "audit.h"
  71. extern void selnl_notify_policyload(u32 seqno);
  72. int selinux_policycap_netpeer;
  73. int selinux_policycap_openperm;
  74. static DEFINE_RWLOCK(policy_rwlock);
  75. static struct sidtab sidtab;
  76. struct policydb policydb;
  77. int ss_initialized;
  78. /*
  79. * The largest sequence number that has been used when
  80. * providing an access decision to the access vector cache.
  81. * The sequence number only changes when a policy change
  82. * occurs.
  83. */
  84. static u32 latest_granting;
  85. /* Forward declaration. */
  86. static int context_struct_to_string(struct context *context, char **scontext,
  87. u32 *scontext_len);
  88. static void context_struct_compute_av(struct context *scontext,
  89. struct context *tcontext,
  90. u16 tclass,
  91. struct av_decision *avd);
  92. struct selinux_mapping {
  93. u16 value; /* policy value */
  94. unsigned num_perms;
  95. u32 perms[sizeof(u32) * 8];
  96. };
  97. static struct selinux_mapping *current_mapping;
  98. static u16 current_mapping_size;
  99. static int selinux_set_mapping(struct policydb *pol,
  100. struct security_class_mapping *map,
  101. struct selinux_mapping **out_map_p,
  102. u16 *out_map_size)
  103. {
  104. struct selinux_mapping *out_map = NULL;
  105. size_t size = sizeof(struct selinux_mapping);
  106. u16 i, j;
  107. unsigned k;
  108. bool print_unknown_handle = false;
  109. /* Find number of classes in the input mapping */
  110. if (!map)
  111. return -EINVAL;
  112. i = 0;
  113. while (map[i].name)
  114. i++;
  115. /* Allocate space for the class records, plus one for class zero */
  116. out_map = kcalloc(++i, size, GFP_ATOMIC);
  117. if (!out_map)
  118. return -ENOMEM;
  119. /* Store the raw class and permission values */
  120. j = 0;
  121. while (map[j].name) {
  122. struct security_class_mapping *p_in = map + (j++);
  123. struct selinux_mapping *p_out = out_map + j;
  124. /* An empty class string skips ahead */
  125. if (!strcmp(p_in->name, "")) {
  126. p_out->num_perms = 0;
  127. continue;
  128. }
  129. p_out->value = string_to_security_class(pol, p_in->name);
  130. if (!p_out->value) {
  131. printk(KERN_INFO
  132. "SELinux: Class %s not defined in policy.\n",
  133. p_in->name);
  134. if (pol->reject_unknown)
  135. goto err;
  136. p_out->num_perms = 0;
  137. print_unknown_handle = true;
  138. continue;
  139. }
  140. k = 0;
  141. while (p_in->perms && p_in->perms[k]) {
  142. /* An empty permission string skips ahead */
  143. if (!*p_in->perms[k]) {
  144. k++;
  145. continue;
  146. }
  147. p_out->perms[k] = string_to_av_perm(pol, p_out->value,
  148. p_in->perms[k]);
  149. if (!p_out->perms[k]) {
  150. printk(KERN_INFO
  151. "SELinux: Permission %s in class %s not defined in policy.\n",
  152. p_in->perms[k], p_in->name);
  153. if (pol->reject_unknown)
  154. goto err;
  155. print_unknown_handle = true;
  156. }
  157. k++;
  158. }
  159. p_out->num_perms = k;
  160. }
  161. if (print_unknown_handle)
  162. printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
  163. pol->allow_unknown ? "allowed" : "denied");
  164. *out_map_p = out_map;
  165. *out_map_size = i;
  166. return 0;
  167. err:
  168. kfree(out_map);
  169. return -EINVAL;
  170. }
  171. /*
  172. * Get real, policy values from mapped values
  173. */
  174. static u16 unmap_class(u16 tclass)
  175. {
  176. if (tclass < current_mapping_size)
  177. return current_mapping[tclass].value;
  178. return tclass;
  179. }
  180. /*
  181. * Get kernel value for class from its policy value
  182. */
  183. static u16 map_class(u16 pol_value)
  184. {
  185. u16 i;
  186. for (i = 1; i < current_mapping_size; i++) {
  187. if (current_mapping[i].value == pol_value)
  188. return i;
  189. }
  190. return SECCLASS_NULL;
  191. }
  192. static void map_decision(u16 tclass, struct av_decision *avd,
  193. int allow_unknown)
  194. {
  195. if (tclass < current_mapping_size) {
  196. unsigned i, n = current_mapping[tclass].num_perms;
  197. u32 result;
  198. for (i = 0, result = 0; i < n; i++) {
  199. if (avd->allowed & current_mapping[tclass].perms[i])
  200. result |= 1<<i;
  201. if (allow_unknown && !current_mapping[tclass].perms[i])
  202. result |= 1<<i;
  203. }
  204. avd->allowed = result;
  205. for (i = 0, result = 0; i < n; i++)
  206. if (avd->auditallow & current_mapping[tclass].perms[i])
  207. result |= 1<<i;
  208. avd->auditallow = result;
  209. for (i = 0, result = 0; i < n; i++) {
  210. if (avd->auditdeny & current_mapping[tclass].perms[i])
  211. result |= 1<<i;
  212. if (!allow_unknown && !current_mapping[tclass].perms[i])
  213. result |= 1<<i;
  214. }
  215. /*
  216. * In case the kernel has a bug and requests a permission
  217. * between num_perms and the maximum permission number, we
  218. * should audit that denial
  219. */
  220. for (; i < (sizeof(u32)*8); i++)
  221. result |= 1<<i;
  222. avd->auditdeny = result;
  223. }
  224. }
  225. int security_mls_enabled(void)
  226. {
  227. return policydb.mls_enabled;
  228. }
  229. /*
  230. * Return the boolean value of a constraint expression
  231. * when it is applied to the specified source and target
  232. * security contexts.
  233. *
  234. * xcontext is a special beast... It is used by the validatetrans rules
  235. * only. For these rules, scontext is the context before the transition,
  236. * tcontext is the context after the transition, and xcontext is the context
  237. * of the process performing the transition. All other callers of
  238. * constraint_expr_eval should pass in NULL for xcontext.
  239. */
  240. static int constraint_expr_eval(struct context *scontext,
  241. struct context *tcontext,
  242. struct context *xcontext,
  243. struct constraint_expr *cexpr)
  244. {
  245. u32 val1, val2;
  246. struct context *c;
  247. struct role_datum *r1, *r2;
  248. struct mls_level *l1, *l2;
  249. struct constraint_expr *e;
  250. int s[CEXPR_MAXDEPTH];
  251. int sp = -1;
  252. for (e = cexpr; e; e = e->next) {
  253. switch (e->expr_type) {
  254. case CEXPR_NOT:
  255. BUG_ON(sp < 0);
  256. s[sp] = !s[sp];
  257. break;
  258. case CEXPR_AND:
  259. BUG_ON(sp < 1);
  260. sp--;
  261. s[sp] &= s[sp + 1];
  262. break;
  263. case CEXPR_OR:
  264. BUG_ON(sp < 1);
  265. sp--;
  266. s[sp] |= s[sp + 1];
  267. break;
  268. case CEXPR_ATTR:
  269. if (sp == (CEXPR_MAXDEPTH - 1))
  270. return 0;
  271. switch (e->attr) {
  272. case CEXPR_USER:
  273. val1 = scontext->user;
  274. val2 = tcontext->user;
  275. break;
  276. case CEXPR_TYPE:
  277. val1 = scontext->type;
  278. val2 = tcontext->type;
  279. break;
  280. case CEXPR_ROLE:
  281. val1 = scontext->role;
  282. val2 = tcontext->role;
  283. r1 = policydb.role_val_to_struct[val1 - 1];
  284. r2 = policydb.role_val_to_struct[val2 - 1];
  285. switch (e->op) {
  286. case CEXPR_DOM:
  287. s[++sp] = ebitmap_get_bit(&r1->dominates,
  288. val2 - 1);
  289. continue;
  290. case CEXPR_DOMBY:
  291. s[++sp] = ebitmap_get_bit(&r2->dominates,
  292. val1 - 1);
  293. continue;
  294. case CEXPR_INCOMP:
  295. s[++sp] = (!ebitmap_get_bit(&r1->dominates,
  296. val2 - 1) &&
  297. !ebitmap_get_bit(&r2->dominates,
  298. val1 - 1));
  299. continue;
  300. default:
  301. break;
  302. }
  303. break;
  304. case CEXPR_L1L2:
  305. l1 = &(scontext->range.level[0]);
  306. l2 = &(tcontext->range.level[0]);
  307. goto mls_ops;
  308. case CEXPR_L1H2:
  309. l1 = &(scontext->range.level[0]);
  310. l2 = &(tcontext->range.level[1]);
  311. goto mls_ops;
  312. case CEXPR_H1L2:
  313. l1 = &(scontext->range.level[1]);
  314. l2 = &(tcontext->range.level[0]);
  315. goto mls_ops;
  316. case CEXPR_H1H2:
  317. l1 = &(scontext->range.level[1]);
  318. l2 = &(tcontext->range.level[1]);
  319. goto mls_ops;
  320. case CEXPR_L1H1:
  321. l1 = &(scontext->range.level[0]);
  322. l2 = &(scontext->range.level[1]);
  323. goto mls_ops;
  324. case CEXPR_L2H2:
  325. l1 = &(tcontext->range.level[0]);
  326. l2 = &(tcontext->range.level[1]);
  327. goto mls_ops;
  328. mls_ops:
  329. switch (e->op) {
  330. case CEXPR_EQ:
  331. s[++sp] = mls_level_eq(l1, l2);
  332. continue;
  333. case CEXPR_NEQ:
  334. s[++sp] = !mls_level_eq(l1, l2);
  335. continue;
  336. case CEXPR_DOM:
  337. s[++sp] = mls_level_dom(l1, l2);
  338. continue;
  339. case CEXPR_DOMBY:
  340. s[++sp] = mls_level_dom(l2, l1);
  341. continue;
  342. case CEXPR_INCOMP:
  343. s[++sp] = mls_level_incomp(l2, l1);
  344. continue;
  345. default:
  346. BUG();
  347. return 0;
  348. }
  349. break;
  350. default:
  351. BUG();
  352. return 0;
  353. }
  354. switch (e->op) {
  355. case CEXPR_EQ:
  356. s[++sp] = (val1 == val2);
  357. break;
  358. case CEXPR_NEQ:
  359. s[++sp] = (val1 != val2);
  360. break;
  361. default:
  362. BUG();
  363. return 0;
  364. }
  365. break;
  366. case CEXPR_NAMES:
  367. if (sp == (CEXPR_MAXDEPTH-1))
  368. return 0;
  369. c = scontext;
  370. if (e->attr & CEXPR_TARGET)
  371. c = tcontext;
  372. else if (e->attr & CEXPR_XTARGET) {
  373. c = xcontext;
  374. if (!c) {
  375. BUG();
  376. return 0;
  377. }
  378. }
  379. if (e->attr & CEXPR_USER)
  380. val1 = c->user;
  381. else if (e->attr & CEXPR_ROLE)
  382. val1 = c->role;
  383. else if (e->attr & CEXPR_TYPE)
  384. val1 = c->type;
  385. else {
  386. BUG();
  387. return 0;
  388. }
  389. switch (e->op) {
  390. case CEXPR_EQ:
  391. s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
  392. break;
  393. case CEXPR_NEQ:
  394. s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
  395. break;
  396. default:
  397. BUG();
  398. return 0;
  399. }
  400. break;
  401. default:
  402. BUG();
  403. return 0;
  404. }
  405. }
  406. BUG_ON(sp != 0);
  407. return s[0];
  408. }
  409. /*
  410. * security_dump_masked_av - dumps masked permissions during
  411. * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
  412. */
  413. static int dump_masked_av_helper(void *k, void *d, void *args)
  414. {
  415. struct perm_datum *pdatum = d;
  416. char **permission_names = args;
  417. BUG_ON(pdatum->value < 1 || pdatum->value > 32);
  418. permission_names[pdatum->value - 1] = (char *)k;
  419. return 0;
  420. }
  421. static void security_dump_masked_av(struct context *scontext,
  422. struct context *tcontext,
  423. u16 tclass,
  424. u32 permissions,
  425. const char *reason)
  426. {
  427. struct common_datum *common_dat;
  428. struct class_datum *tclass_dat;
  429. struct audit_buffer *ab;
  430. char *tclass_name;
  431. char *scontext_name = NULL;
  432. char *tcontext_name = NULL;
  433. char *permission_names[32];
  434. int index;
  435. u32 length;
  436. bool need_comma = false;
  437. if (!permissions)
  438. return;
  439. tclass_name = sym_name(&policydb, SYM_CLASSES, tclass - 1);
  440. tclass_dat = policydb.class_val_to_struct[tclass - 1];
  441. common_dat = tclass_dat->comdatum;
  442. /* init permission_names */
  443. if (common_dat &&
  444. hashtab_map(common_dat->permissions.table,
  445. dump_masked_av_helper, permission_names) < 0)
  446. goto out;
  447. if (hashtab_map(tclass_dat->permissions.table,
  448. dump_masked_av_helper, permission_names) < 0)
  449. goto out;
  450. /* get scontext/tcontext in text form */
  451. if (context_struct_to_string(scontext,
  452. &scontext_name, &length) < 0)
  453. goto out;
  454. if (context_struct_to_string(tcontext,
  455. &tcontext_name, &length) < 0)
  456. goto out;
  457. /* audit a message */
  458. ab = audit_log_start(current->audit_context,
  459. GFP_ATOMIC, AUDIT_SELINUX_ERR);
  460. if (!ab)
  461. goto out;
  462. audit_log_format(ab, "op=security_compute_av reason=%s "
  463. "scontext=%s tcontext=%s tclass=%s perms=",
  464. reason, scontext_name, tcontext_name, tclass_name);
  465. for (index = 0; index < 32; index++) {
  466. u32 mask = (1 << index);
  467. if ((mask & permissions) == 0)
  468. continue;
  469. audit_log_format(ab, "%s%s",
  470. need_comma ? "," : "",
  471. permission_names[index]
  472. ? permission_names[index] : "????");
  473. need_comma = true;
  474. }
  475. audit_log_end(ab);
  476. out:
  477. /* release scontext/tcontext */
  478. kfree(tcontext_name);
  479. kfree(scontext_name);
  480. return;
  481. }
  482. /*
  483. * security_boundary_permission - drops violated permissions
  484. * on boundary constraint.
  485. */
  486. static void type_attribute_bounds_av(struct context *scontext,
  487. struct context *tcontext,
  488. u16 tclass,
  489. struct av_decision *avd)
  490. {
  491. struct context lo_scontext;
  492. struct context lo_tcontext;
  493. struct av_decision lo_avd;
  494. struct type_datum *source;
  495. struct type_datum *target;
  496. u32 masked = 0;
  497. source = flex_array_get_ptr(policydb.type_val_to_struct_array,
  498. scontext->type - 1);
  499. BUG_ON(!source);
  500. target = flex_array_get_ptr(policydb.type_val_to_struct_array,
  501. tcontext->type - 1);
  502. BUG_ON(!target);
  503. if (source->bounds) {
  504. memset(&lo_avd, 0, sizeof(lo_avd));
  505. memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
  506. lo_scontext.type = source->bounds;
  507. context_struct_compute_av(&lo_scontext,
  508. tcontext,
  509. tclass,
  510. &lo_avd);
  511. if ((lo_avd.allowed & avd->allowed) == avd->allowed)
  512. return; /* no masked permission */
  513. masked = ~lo_avd.allowed & avd->allowed;
  514. }
  515. if (target->bounds) {
  516. memset(&lo_avd, 0, sizeof(lo_avd));
  517. memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
  518. lo_tcontext.type = target->bounds;
  519. context_struct_compute_av(scontext,
  520. &lo_tcontext,
  521. tclass,
  522. &lo_avd);
  523. if ((lo_avd.allowed & avd->allowed) == avd->allowed)
  524. return; /* no masked permission */
  525. masked = ~lo_avd.allowed & avd->allowed;
  526. }
  527. if (source->bounds && target->bounds) {
  528. memset(&lo_avd, 0, sizeof(lo_avd));
  529. /*
  530. * lo_scontext and lo_tcontext are already
  531. * set up.
  532. */
  533. context_struct_compute_av(&lo_scontext,
  534. &lo_tcontext,
  535. tclass,
  536. &lo_avd);
  537. if ((lo_avd.allowed & avd->allowed) == avd->allowed)
  538. return; /* no masked permission */
  539. masked = ~lo_avd.allowed & avd->allowed;
  540. }
  541. if (masked) {
  542. /* mask violated permissions */
  543. avd->allowed &= ~masked;
  544. /* audit masked permissions */
  545. security_dump_masked_av(scontext, tcontext,
  546. tclass, masked, "bounds");
  547. }
  548. }
  549. /*
  550. * Compute access vectors based on a context structure pair for
  551. * the permissions in a particular class.
  552. */
  553. static void context_struct_compute_av(struct context *scontext,
  554. struct context *tcontext,
  555. u16 tclass,
  556. struct av_decision *avd)
  557. {
  558. struct constraint_node *constraint;
  559. struct role_allow *ra;
  560. struct avtab_key avkey;
  561. struct avtab_node *node;
  562. struct class_datum *tclass_datum;
  563. struct ebitmap *sattr, *tattr;
  564. struct ebitmap_node *snode, *tnode;
  565. unsigned int i, j;
  566. avd->allowed = 0;
  567. avd->auditallow = 0;
  568. avd->auditdeny = 0xffffffff;
  569. if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
  570. if (printk_ratelimit())
  571. printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass);
  572. return;
  573. }
  574. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  575. /*
  576. * If a specific type enforcement rule was defined for
  577. * this permission check, then use it.
  578. */
  579. avkey.target_class = tclass;
  580. avkey.specified = AVTAB_AV;
  581. sattr = flex_array_get(policydb.type_attr_map_array, scontext->type - 1);
  582. BUG_ON(!sattr);
  583. tattr = flex_array_get(policydb.type_attr_map_array, tcontext->type - 1);
  584. BUG_ON(!tattr);
  585. ebitmap_for_each_positive_bit(sattr, snode, i) {
  586. ebitmap_for_each_positive_bit(tattr, tnode, j) {
  587. avkey.source_type = i + 1;
  588. avkey.target_type = j + 1;
  589. for (node = avtab_search_node(&policydb.te_avtab, &avkey);
  590. node;
  591. node = avtab_search_node_next(node, avkey.specified)) {
  592. if (node->key.specified == AVTAB_ALLOWED)
  593. avd->allowed |= node->datum.data;
  594. else if (node->key.specified == AVTAB_AUDITALLOW)
  595. avd->auditallow |= node->datum.data;
  596. else if (node->key.specified == AVTAB_AUDITDENY)
  597. avd->auditdeny &= node->datum.data;
  598. }
  599. /* Check conditional av table for additional permissions */
  600. cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
  601. }
  602. }
  603. /*
  604. * Remove any permissions prohibited by a constraint (this includes
  605. * the MLS policy).
  606. */
  607. constraint = tclass_datum->constraints;
  608. while (constraint) {
  609. if ((constraint->permissions & (avd->allowed)) &&
  610. !constraint_expr_eval(scontext, tcontext, NULL,
  611. constraint->expr)) {
  612. avd->allowed &= ~(constraint->permissions);
  613. }
  614. constraint = constraint->next;
  615. }
  616. /*
  617. * If checking process transition permission and the
  618. * role is changing, then check the (current_role, new_role)
  619. * pair.
  620. */
  621. if (tclass == policydb.process_class &&
  622. (avd->allowed & policydb.process_trans_perms) &&
  623. scontext->role != tcontext->role) {
  624. for (ra = policydb.role_allow; ra; ra = ra->next) {
  625. if (scontext->role == ra->role &&
  626. tcontext->role == ra->new_role)
  627. break;
  628. }
  629. if (!ra)
  630. avd->allowed &= ~policydb.process_trans_perms;
  631. }
  632. /*
  633. * If the given source and target types have boundary
  634. * constraint, lazy checks have to mask any violated
  635. * permission and notice it to userspace via audit.
  636. */
  637. type_attribute_bounds_av(scontext, tcontext,
  638. tclass, avd);
  639. }
  640. static int security_validtrans_handle_fail(struct context *ocontext,
  641. struct context *ncontext,
  642. struct context *tcontext,
  643. u16 tclass)
  644. {
  645. char *o = NULL, *n = NULL, *t = NULL;
  646. u32 olen, nlen, tlen;
  647. if (context_struct_to_string(ocontext, &o, &olen))
  648. goto out;
  649. if (context_struct_to_string(ncontext, &n, &nlen))
  650. goto out;
  651. if (context_struct_to_string(tcontext, &t, &tlen))
  652. goto out;
  653. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  654. "security_validate_transition: denied for"
  655. " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
  656. o, n, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
  657. out:
  658. kfree(o);
  659. kfree(n);
  660. kfree(t);
  661. if (!selinux_enforcing)
  662. return 0;
  663. return -EPERM;
  664. }
  665. int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
  666. u16 orig_tclass)
  667. {
  668. struct context *ocontext;
  669. struct context *ncontext;
  670. struct context *tcontext;
  671. struct class_datum *tclass_datum;
  672. struct constraint_node *constraint;
  673. u16 tclass;
  674. int rc = 0;
  675. if (!ss_initialized)
  676. return 0;
  677. read_lock(&policy_rwlock);
  678. tclass = unmap_class(orig_tclass);
  679. if (!tclass || tclass > policydb.p_classes.nprim) {
  680. printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
  681. __func__, tclass);
  682. rc = -EINVAL;
  683. goto out;
  684. }
  685. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  686. ocontext = sidtab_search(&sidtab, oldsid);
  687. if (!ocontext) {
  688. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  689. __func__, oldsid);
  690. rc = -EINVAL;
  691. goto out;
  692. }
  693. ncontext = sidtab_search(&sidtab, newsid);
  694. if (!ncontext) {
  695. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  696. __func__, newsid);
  697. rc = -EINVAL;
  698. goto out;
  699. }
  700. tcontext = sidtab_search(&sidtab, tasksid);
  701. if (!tcontext) {
  702. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  703. __func__, tasksid);
  704. rc = -EINVAL;
  705. goto out;
  706. }
  707. constraint = tclass_datum->validatetrans;
  708. while (constraint) {
  709. if (!constraint_expr_eval(ocontext, ncontext, tcontext,
  710. constraint->expr)) {
  711. rc = security_validtrans_handle_fail(ocontext, ncontext,
  712. tcontext, tclass);
  713. goto out;
  714. }
  715. constraint = constraint->next;
  716. }
  717. out:
  718. read_unlock(&policy_rwlock);
  719. return rc;
  720. }
  721. /*
  722. * security_bounded_transition - check whether the given
  723. * transition is directed to bounded, or not.
  724. * It returns 0, if @newsid is bounded by @oldsid.
  725. * Otherwise, it returns error code.
  726. *
  727. * @oldsid : current security identifier
  728. * @newsid : destinated security identifier
  729. */
  730. int security_bounded_transition(u32 old_sid, u32 new_sid)
  731. {
  732. struct context *old_context, *new_context;
  733. struct type_datum *type;
  734. int index;
  735. int rc;
  736. read_lock(&policy_rwlock);
  737. rc = -EINVAL;
  738. old_context = sidtab_search(&sidtab, old_sid);
  739. if (!old_context) {
  740. printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
  741. __func__, old_sid);
  742. goto out;
  743. }
  744. rc = -EINVAL;
  745. new_context = sidtab_search(&sidtab, new_sid);
  746. if (!new_context) {
  747. printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
  748. __func__, new_sid);
  749. goto out;
  750. }
  751. rc = 0;
  752. /* type/domain unchanged */
  753. if (old_context->type == new_context->type)
  754. goto out;
  755. index = new_context->type;
  756. while (true) {
  757. type = flex_array_get_ptr(policydb.type_val_to_struct_array,
  758. index - 1);
  759. BUG_ON(!type);
  760. /* not bounded anymore */
  761. rc = -EPERM;
  762. if (!type->bounds)
  763. break;
  764. /* @newsid is bounded by @oldsid */
  765. rc = 0;
  766. if (type->bounds == old_context->type)
  767. break;
  768. index = type->bounds;
  769. }
  770. if (rc) {
  771. char *old_name = NULL;
  772. char *new_name = NULL;
  773. u32 length;
  774. if (!context_struct_to_string(old_context,
  775. &old_name, &length) &&
  776. !context_struct_to_string(new_context,
  777. &new_name, &length)) {
  778. audit_log(current->audit_context,
  779. GFP_ATOMIC, AUDIT_SELINUX_ERR,
  780. "op=security_bounded_transition "
  781. "result=denied "
  782. "oldcontext=%s newcontext=%s",
  783. old_name, new_name);
  784. }
  785. kfree(new_name);
  786. kfree(old_name);
  787. }
  788. out:
  789. read_unlock(&policy_rwlock);
  790. return rc;
  791. }
  792. static void avd_init(struct av_decision *avd)
  793. {
  794. avd->allowed = 0;
  795. avd->auditallow = 0;
  796. avd->auditdeny = 0xffffffff;
  797. avd->seqno = latest_granting;
  798. avd->flags = 0;
  799. }
  800. /**
  801. * security_compute_av - Compute access vector decisions.
  802. * @ssid: source security identifier
  803. * @tsid: target security identifier
  804. * @tclass: target security class
  805. * @avd: access vector decisions
  806. *
  807. * Compute a set of access vector decisions based on the
  808. * SID pair (@ssid, @tsid) for the permissions in @tclass.
  809. */
  810. void security_compute_av(u32 ssid,
  811. u32 tsid,
  812. u16 orig_tclass,
  813. struct av_decision *avd)
  814. {
  815. u16 tclass;
  816. struct context *scontext = NULL, *tcontext = NULL;
  817. read_lock(&policy_rwlock);
  818. avd_init(avd);
  819. if (!ss_initialized)
  820. goto allow;
  821. scontext = sidtab_search(&sidtab, ssid);
  822. if (!scontext) {
  823. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  824. __func__, ssid);
  825. goto out;
  826. }
  827. /* permissive domain? */
  828. if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
  829. avd->flags |= AVD_FLAGS_PERMISSIVE;
  830. tcontext = sidtab_search(&sidtab, tsid);
  831. if (!tcontext) {
  832. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  833. __func__, tsid);
  834. goto out;
  835. }
  836. tclass = unmap_class(orig_tclass);
  837. if (unlikely(orig_tclass && !tclass)) {
  838. if (policydb.allow_unknown)
  839. goto allow;
  840. goto out;
  841. }
  842. context_struct_compute_av(scontext, tcontext, tclass, avd);
  843. map_decision(orig_tclass, avd, policydb.allow_unknown);
  844. out:
  845. read_unlock(&policy_rwlock);
  846. return;
  847. allow:
  848. avd->allowed = 0xffffffff;
  849. goto out;
  850. }
  851. void security_compute_av_user(u32 ssid,
  852. u32 tsid,
  853. u16 tclass,
  854. struct av_decision *avd)
  855. {
  856. struct context *scontext = NULL, *tcontext = NULL;
  857. read_lock(&policy_rwlock);
  858. avd_init(avd);
  859. if (!ss_initialized)
  860. goto allow;
  861. scontext = sidtab_search(&sidtab, ssid);
  862. if (!scontext) {
  863. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  864. __func__, ssid);
  865. goto out;
  866. }
  867. /* permissive domain? */
  868. if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
  869. avd->flags |= AVD_FLAGS_PERMISSIVE;
  870. tcontext = sidtab_search(&sidtab, tsid);
  871. if (!tcontext) {
  872. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  873. __func__, tsid);
  874. goto out;
  875. }
  876. if (unlikely(!tclass)) {
  877. if (policydb.allow_unknown)
  878. goto allow;
  879. goto out;
  880. }
  881. context_struct_compute_av(scontext, tcontext, tclass, avd);
  882. out:
  883. read_unlock(&policy_rwlock);
  884. return;
  885. allow:
  886. avd->allowed = 0xffffffff;
  887. goto out;
  888. }
  889. /*
  890. * Write the security context string representation of
  891. * the context structure `context' into a dynamically
  892. * allocated string of the correct size. Set `*scontext'
  893. * to point to this string and set `*scontext_len' to
  894. * the length of the string.
  895. */
  896. static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
  897. {
  898. char *scontextp;
  899. if (scontext)
  900. *scontext = NULL;
  901. *scontext_len = 0;
  902. if (context->len) {
  903. *scontext_len = context->len;
  904. *scontext = kstrdup(context->str, GFP_ATOMIC);
  905. if (!(*scontext))
  906. return -ENOMEM;
  907. return 0;
  908. }
  909. /* Compute the size of the context. */
  910. *scontext_len += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) + 1;
  911. *scontext_len += strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) + 1;
  912. *scontext_len += strlen(sym_name(&policydb, SYM_TYPES, context->type - 1)) + 1;
  913. *scontext_len += mls_compute_context_len(context);
  914. if (!scontext)
  915. return 0;
  916. /* Allocate space for the context; caller must free this space. */
  917. scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
  918. if (!scontextp)
  919. return -ENOMEM;
  920. *scontext = scontextp;
  921. /*
  922. * Copy the user name, role name and type name into the context.
  923. */
  924. sprintf(scontextp, "%s:%s:%s",
  925. sym_name(&policydb, SYM_USERS, context->user - 1),
  926. sym_name(&policydb, SYM_ROLES, context->role - 1),
  927. sym_name(&policydb, SYM_TYPES, context->type - 1));
  928. scontextp += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) +
  929. 1 + strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) +
  930. 1 + strlen(sym_name(&policydb, SYM_TYPES, context->type - 1));
  931. mls_sid_to_context(context, &scontextp);
  932. *scontextp = 0;
  933. return 0;
  934. }
  935. #include "initial_sid_to_string.h"
  936. const char *security_get_initial_sid_context(u32 sid)
  937. {
  938. if (unlikely(sid > SECINITSID_NUM))
  939. return NULL;
  940. return initial_sid_to_string[sid];
  941. }
  942. static int security_sid_to_context_core(u32 sid, char **scontext,
  943. u32 *scontext_len, int force)
  944. {
  945. struct context *context;
  946. int rc = 0;
  947. if (scontext)
  948. *scontext = NULL;
  949. *scontext_len = 0;
  950. if (!ss_initialized) {
  951. if (sid <= SECINITSID_NUM) {
  952. char *scontextp;
  953. *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
  954. if (!scontext)
  955. goto out;
  956. scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
  957. if (!scontextp) {
  958. rc = -ENOMEM;
  959. goto out;
  960. }
  961. strcpy(scontextp, initial_sid_to_string[sid]);
  962. *scontext = scontextp;
  963. goto out;
  964. }
  965. printk(KERN_ERR "SELinux: %s: called before initial "
  966. "load_policy on unknown SID %d\n", __func__, sid);
  967. rc = -EINVAL;
  968. goto out;
  969. }
  970. read_lock(&policy_rwlock);
  971. if (force)
  972. context = sidtab_search_force(&sidtab, sid);
  973. else
  974. context = sidtab_search(&sidtab, sid);
  975. if (!context) {
  976. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  977. __func__, sid);
  978. rc = -EINVAL;
  979. goto out_unlock;
  980. }
  981. rc = context_struct_to_string(context, scontext, scontext_len);
  982. out_unlock:
  983. read_unlock(&policy_rwlock);
  984. out:
  985. return rc;
  986. }
  987. /**
  988. * security_sid_to_context - Obtain a context for a given SID.
  989. * @sid: security identifier, SID
  990. * @scontext: security context
  991. * @scontext_len: length in bytes
  992. *
  993. * Write the string representation of the context associated with @sid
  994. * into a dynamically allocated string of the correct size. Set @scontext
  995. * to point to this string and set @scontext_len to the length of the string.
  996. */
  997. int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
  998. {
  999. return security_sid_to_context_core(sid, scontext, scontext_len, 0);
  1000. }
  1001. int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
  1002. {
  1003. return security_sid_to_context_core(sid, scontext, scontext_len, 1);
  1004. }
  1005. /*
  1006. * Caveat: Mutates scontext.
  1007. */
  1008. static int string_to_context_struct(struct policydb *pol,
  1009. struct sidtab *sidtabp,
  1010. char *scontext,
  1011. u32 scontext_len,
  1012. struct context *ctx,
  1013. u32 def_sid)
  1014. {
  1015. struct role_datum *role;
  1016. struct type_datum *typdatum;
  1017. struct user_datum *usrdatum;
  1018. char *scontextp, *p, oldc;
  1019. int rc = 0;
  1020. context_init(ctx);
  1021. /* Parse the security context. */
  1022. rc = -EINVAL;
  1023. scontextp = (char *) scontext;
  1024. /* Extract the user. */
  1025. p = scontextp;
  1026. while (*p && *p != ':')
  1027. p++;
  1028. if (*p == 0)
  1029. goto out;
  1030. *p++ = 0;
  1031. usrdatum = hashtab_search(pol->p_users.table, scontextp);
  1032. if (!usrdatum)
  1033. goto out;
  1034. ctx->user = usrdatum->value;
  1035. /* Extract role. */
  1036. scontextp = p;
  1037. while (*p && *p != ':')
  1038. p++;
  1039. if (*p == 0)
  1040. goto out;
  1041. *p++ = 0;
  1042. role = hashtab_search(pol->p_roles.table, scontextp);
  1043. if (!role)
  1044. goto out;
  1045. ctx->role = role->value;
  1046. /* Extract type. */
  1047. scontextp = p;
  1048. while (*p && *p != ':')
  1049. p++;
  1050. oldc = *p;
  1051. *p++ = 0;
  1052. typdatum = hashtab_search(pol->p_types.table, scontextp);
  1053. if (!typdatum || typdatum->attribute)
  1054. goto out;
  1055. ctx->type = typdatum->value;
  1056. rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
  1057. if (rc)
  1058. goto out;
  1059. rc = -EINVAL;
  1060. if ((p - scontext) < scontext_len)
  1061. goto out;
  1062. /* Check the validity of the new context. */
  1063. if (!policydb_context_isvalid(pol, ctx))
  1064. goto out;
  1065. rc = 0;
  1066. out:
  1067. if (rc)
  1068. context_destroy(ctx);
  1069. return rc;
  1070. }
  1071. static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
  1072. u32 *sid, u32 def_sid, gfp_t gfp_flags,
  1073. int force)
  1074. {
  1075. char *scontext2, *str = NULL;
  1076. struct context context;
  1077. int rc = 0;
  1078. if (!ss_initialized) {
  1079. int i;
  1080. for (i = 1; i < SECINITSID_NUM; i++) {
  1081. if (!strcmp(initial_sid_to_string[i], scontext)) {
  1082. *sid = i;
  1083. return 0;
  1084. }
  1085. }
  1086. *sid = SECINITSID_KERNEL;
  1087. return 0;
  1088. }
  1089. *sid = SECSID_NULL;
  1090. /* Copy the string so that we can modify the copy as we parse it. */
  1091. scontext2 = kmalloc(scontext_len + 1, gfp_flags);
  1092. if (!scontext2)
  1093. return -ENOMEM;
  1094. memcpy(scontext2, scontext, scontext_len);
  1095. scontext2[scontext_len] = 0;
  1096. if (force) {
  1097. /* Save another copy for storing in uninterpreted form */
  1098. rc = -ENOMEM;
  1099. str = kstrdup(scontext2, gfp_flags);
  1100. if (!str)
  1101. goto out;
  1102. }
  1103. read_lock(&policy_rwlock);
  1104. rc = string_to_context_struct(&policydb, &sidtab, scontext2,
  1105. scontext_len, &context, def_sid);
  1106. if (rc == -EINVAL && force) {
  1107. context.str = str;
  1108. context.len = scontext_len;
  1109. str = NULL;
  1110. } else if (rc)
  1111. goto out_unlock;
  1112. rc = sidtab_context_to_sid(&sidtab, &context, sid);
  1113. context_destroy(&context);
  1114. out_unlock:
  1115. read_unlock(&policy_rwlock);
  1116. out:
  1117. kfree(scontext2);
  1118. kfree(str);
  1119. return rc;
  1120. }
  1121. /**
  1122. * security_context_to_sid - Obtain a SID for a given security context.
  1123. * @scontext: security context
  1124. * @scontext_len: length in bytes
  1125. * @sid: security identifier, SID
  1126. *
  1127. * Obtains a SID associated with the security context that
  1128. * has the string representation specified by @scontext.
  1129. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  1130. * memory is available, or 0 on success.
  1131. */
  1132. int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
  1133. {
  1134. return security_context_to_sid_core(scontext, scontext_len,
  1135. sid, SECSID_NULL, GFP_KERNEL, 0);
  1136. }
  1137. /**
  1138. * security_context_to_sid_default - Obtain a SID for a given security context,
  1139. * falling back to specified default if needed.
  1140. *
  1141. * @scontext: security context
  1142. * @scontext_len: length in bytes
  1143. * @sid: security identifier, SID
  1144. * @def_sid: default SID to assign on error
  1145. *
  1146. * Obtains a SID associated with the security context that
  1147. * has the string representation specified by @scontext.
  1148. * The default SID is passed to the MLS layer to be used to allow
  1149. * kernel labeling of the MLS field if the MLS field is not present
  1150. * (for upgrading to MLS without full relabel).
  1151. * Implicitly forces adding of the context even if it cannot be mapped yet.
  1152. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  1153. * memory is available, or 0 on success.
  1154. */
  1155. int security_context_to_sid_default(const char *scontext, u32 scontext_len,
  1156. u32 *sid, u32 def_sid, gfp_t gfp_flags)
  1157. {
  1158. return security_context_to_sid_core(scontext, scontext_len,
  1159. sid, def_sid, gfp_flags, 1);
  1160. }
  1161. int security_context_to_sid_force(const char *scontext, u32 scontext_len,
  1162. u32 *sid)
  1163. {
  1164. return security_context_to_sid_core(scontext, scontext_len,
  1165. sid, SECSID_NULL, GFP_KERNEL, 1);
  1166. }
  1167. static int compute_sid_handle_invalid_context(
  1168. struct context *scontext,
  1169. struct context *tcontext,
  1170. u16 tclass,
  1171. struct context *newcontext)
  1172. {
  1173. char *s = NULL, *t = NULL, *n = NULL;
  1174. u32 slen, tlen, nlen;
  1175. if (context_struct_to_string(scontext, &s, &slen))
  1176. goto out;
  1177. if (context_struct_to_string(tcontext, &t, &tlen))
  1178. goto out;
  1179. if (context_struct_to_string(newcontext, &n, &nlen))
  1180. goto out;
  1181. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1182. "security_compute_sid: invalid context %s"
  1183. " for scontext=%s"
  1184. " tcontext=%s"
  1185. " tclass=%s",
  1186. n, s, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
  1187. out:
  1188. kfree(s);
  1189. kfree(t);
  1190. kfree(n);
  1191. if (!selinux_enforcing)
  1192. return 0;
  1193. return -EACCES;
  1194. }
  1195. static void filename_compute_type(struct policydb *p, struct context *newcontext,
  1196. u32 stype, u32 ttype, u16 tclass,
  1197. const char *objname)
  1198. {
  1199. struct filename_trans ft;
  1200. struct filename_trans_datum *otype;
  1201. /*
  1202. * Most filename trans rules are going to live in specific directories
  1203. * like /dev or /var/run. This bitmap will quickly skip rule searches
  1204. * if the ttype does not contain any rules.
  1205. */
  1206. if (!ebitmap_get_bit(&p->filename_trans_ttypes, ttype))
  1207. return;
  1208. ft.stype = stype;
  1209. ft.ttype = ttype;
  1210. ft.tclass = tclass;
  1211. ft.name = objname;
  1212. otype = hashtab_search(p->filename_trans, &ft);
  1213. if (otype)
  1214. newcontext->type = otype->otype;
  1215. }
  1216. static int security_compute_sid(u32 ssid,
  1217. u32 tsid,
  1218. u16 orig_tclass,
  1219. u32 specified,
  1220. const char *objname,
  1221. u32 *out_sid,
  1222. bool kern)
  1223. {
  1224. struct context *scontext = NULL, *tcontext = NULL, newcontext;
  1225. struct role_trans *roletr = NULL;
  1226. struct avtab_key avkey;
  1227. struct avtab_datum *avdatum;
  1228. struct avtab_node *node;
  1229. u16 tclass;
  1230. int rc = 0;
  1231. bool sock;
  1232. if (!ss_initialized) {
  1233. switch (orig_tclass) {
  1234. case SECCLASS_PROCESS: /* kernel value */
  1235. *out_sid = ssid;
  1236. break;
  1237. default:
  1238. *out_sid = tsid;
  1239. break;
  1240. }
  1241. goto out;
  1242. }
  1243. context_init(&newcontext);
  1244. read_lock(&policy_rwlock);
  1245. if (kern) {
  1246. tclass = unmap_class(orig_tclass);
  1247. sock = security_is_socket_class(orig_tclass);
  1248. } else {
  1249. tclass = orig_tclass;
  1250. sock = security_is_socket_class(map_class(tclass));
  1251. }
  1252. scontext = sidtab_search(&sidtab, ssid);
  1253. if (!scontext) {
  1254. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1255. __func__, ssid);
  1256. rc = -EINVAL;
  1257. goto out_unlock;
  1258. }
  1259. tcontext = sidtab_search(&sidtab, tsid);
  1260. if (!tcontext) {
  1261. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1262. __func__, tsid);
  1263. rc = -EINVAL;
  1264. goto out_unlock;
  1265. }
  1266. /* Set the user identity. */
  1267. switch (specified) {
  1268. case AVTAB_TRANSITION:
  1269. case AVTAB_CHANGE:
  1270. /* Use the process user identity. */
  1271. newcontext.user = scontext->user;
  1272. break;
  1273. case AVTAB_MEMBER:
  1274. /* Use the related object owner. */
  1275. newcontext.user = tcontext->user;
  1276. break;
  1277. }
  1278. /* Set the role and type to default values. */
  1279. if ((tclass == policydb.process_class) || (sock == true)) {
  1280. /* Use the current role and type of process. */
  1281. newcontext.role = scontext->role;
  1282. newcontext.type = scontext->type;
  1283. } else {
  1284. /* Use the well-defined object role. */
  1285. newcontext.role = OBJECT_R_VAL;
  1286. /* Use the type of the related object. */
  1287. newcontext.type = tcontext->type;
  1288. }
  1289. /* Look for a type transition/member/change rule. */
  1290. avkey.source_type = scontext->type;
  1291. avkey.target_type = tcontext->type;
  1292. avkey.target_class = tclass;
  1293. avkey.specified = specified;
  1294. avdatum = avtab_search(&policydb.te_avtab, &avkey);
  1295. /* If no permanent rule, also check for enabled conditional rules */
  1296. if (!avdatum) {
  1297. node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
  1298. for (; node; node = avtab_search_node_next(node, specified)) {
  1299. if (node->key.specified & AVTAB_ENABLED) {
  1300. avdatum = &node->datum;
  1301. break;
  1302. }
  1303. }
  1304. }
  1305. if (avdatum) {
  1306. /* Use the type from the type transition/member/change rule. */
  1307. newcontext.type = avdatum->data;
  1308. }
  1309. /* if we have a objname this is a file trans check so check those rules */
  1310. if (objname)
  1311. filename_compute_type(&policydb, &newcontext, scontext->type,
  1312. tcontext->type, tclass, objname);
  1313. /* Check for class-specific changes. */
  1314. if (specified & AVTAB_TRANSITION) {
  1315. /* Look for a role transition rule. */
  1316. for (roletr = policydb.role_tr; roletr; roletr = roletr->next) {
  1317. if ((roletr->role == scontext->role) &&
  1318. (roletr->type == tcontext->type) &&
  1319. (roletr->tclass == tclass)) {
  1320. /* Use the role transition rule. */
  1321. newcontext.role = roletr->new_role;
  1322. break;
  1323. }
  1324. }
  1325. }
  1326. /* Set the MLS attributes.
  1327. This is done last because it may allocate memory. */
  1328. rc = mls_compute_sid(scontext, tcontext, tclass, specified,
  1329. &newcontext, sock);
  1330. if (rc)
  1331. goto out_unlock;
  1332. /* Check the validity of the context. */
  1333. if (!policydb_context_isvalid(&policydb, &newcontext)) {
  1334. rc = compute_sid_handle_invalid_context(scontext,
  1335. tcontext,
  1336. tclass,
  1337. &newcontext);
  1338. if (rc)
  1339. goto out_unlock;
  1340. }
  1341. /* Obtain the sid for the context. */
  1342. rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
  1343. out_unlock:
  1344. read_unlock(&policy_rwlock);
  1345. context_destroy(&newcontext);
  1346. out:
  1347. return rc;
  1348. }
  1349. /**
  1350. * security_transition_sid - Compute the SID for a new subject/object.
  1351. * @ssid: source security identifier
  1352. * @tsid: target security identifier
  1353. * @tclass: target security class
  1354. * @out_sid: security identifier for new subject/object
  1355. *
  1356. * Compute a SID to use for labeling a new subject or object in the
  1357. * class @tclass based on a SID pair (@ssid, @tsid).
  1358. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1359. * if insufficient memory is available, or %0 if the new SID was
  1360. * computed successfully.
  1361. */
  1362. int security_transition_sid(u32 ssid, u32 tsid, u16 tclass,
  1363. const struct qstr *qstr, u32 *out_sid)
  1364. {
  1365. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
  1366. qstr ? qstr->name : NULL, out_sid, true);
  1367. }
  1368. int security_transition_sid_user(u32 ssid, u32 tsid, u16 tclass,
  1369. const char *objname, u32 *out_sid)
  1370. {
  1371. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
  1372. objname, out_sid, false);
  1373. }
  1374. /**
  1375. * security_member_sid - Compute the SID for member selection.
  1376. * @ssid: source security identifier
  1377. * @tsid: target security identifier
  1378. * @tclass: target security class
  1379. * @out_sid: security identifier for selected member
  1380. *
  1381. * Compute a SID to use when selecting a member of a polyinstantiated
  1382. * object of class @tclass based on a SID pair (@ssid, @tsid).
  1383. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1384. * if insufficient memory is available, or %0 if the SID was
  1385. * computed successfully.
  1386. */
  1387. int security_member_sid(u32 ssid,
  1388. u32 tsid,
  1389. u16 tclass,
  1390. u32 *out_sid)
  1391. {
  1392. return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, NULL,
  1393. out_sid, false);
  1394. }
  1395. /**
  1396. * security_change_sid - Compute the SID for object relabeling.
  1397. * @ssid: source security identifier
  1398. * @tsid: target security identifier
  1399. * @tclass: target security class
  1400. * @out_sid: security identifier for selected member
  1401. *
  1402. * Compute a SID to use for relabeling an object of class @tclass
  1403. * based on a SID pair (@ssid, @tsid).
  1404. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1405. * if insufficient memory is available, or %0 if the SID was
  1406. * computed successfully.
  1407. */
  1408. int security_change_sid(u32 ssid,
  1409. u32 tsid,
  1410. u16 tclass,
  1411. u32 *out_sid)
  1412. {
  1413. return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, NULL,
  1414. out_sid, false);
  1415. }
  1416. /* Clone the SID into the new SID table. */
  1417. static int clone_sid(u32 sid,
  1418. struct context *context,
  1419. void *arg)
  1420. {
  1421. struct sidtab *s = arg;
  1422. if (sid > SECINITSID_NUM)
  1423. return sidtab_insert(s, sid, context);
  1424. else
  1425. return 0;
  1426. }
  1427. static inline int convert_context_handle_invalid_context(struct context *context)
  1428. {
  1429. char *s;
  1430. u32 len;
  1431. if (selinux_enforcing)
  1432. return -EINVAL;
  1433. if (!context_struct_to_string(context, &s, &len)) {
  1434. printk(KERN_WARNING "SELinux: Context %s would be invalid if enforcing\n", s);
  1435. kfree(s);
  1436. }
  1437. return 0;
  1438. }
  1439. struct convert_context_args {
  1440. struct policydb *oldp;
  1441. struct policydb *newp;
  1442. };
  1443. /*
  1444. * Convert the values in the security context
  1445. * structure `c' from the values specified
  1446. * in the policy `p->oldp' to the values specified
  1447. * in the policy `p->newp'. Verify that the
  1448. * context is valid under the new policy.
  1449. */
  1450. static int convert_context(u32 key,
  1451. struct context *c,
  1452. void *p)
  1453. {
  1454. struct convert_context_args *args;
  1455. struct context oldc;
  1456. struct ocontext *oc;
  1457. struct mls_range *range;
  1458. struct role_datum *role;
  1459. struct type_datum *typdatum;
  1460. struct user_datum *usrdatum;
  1461. char *s;
  1462. u32 len;
  1463. int rc = 0;
  1464. if (key <= SECINITSID_NUM)
  1465. goto out;
  1466. args = p;
  1467. if (c->str) {
  1468. struct context ctx;
  1469. rc = -ENOMEM;
  1470. s = kstrdup(c->str, GFP_KERNEL);
  1471. if (!s)
  1472. goto out;
  1473. rc = string_to_context_struct(args->newp, NULL, s,
  1474. c->len, &ctx, SECSID_NULL);
  1475. kfree(s);
  1476. if (!rc) {
  1477. printk(KERN_INFO "SELinux: Context %s became valid (mapped).\n",
  1478. c->str);
  1479. /* Replace string with mapped representation. */
  1480. kfree(c->str);
  1481. memcpy(c, &ctx, sizeof(*c));
  1482. goto out;
  1483. } else if (rc == -EINVAL) {
  1484. /* Retain string representation for later mapping. */
  1485. rc = 0;
  1486. goto out;
  1487. } else {
  1488. /* Other error condition, e.g. ENOMEM. */
  1489. printk(KERN_ERR "SELinux: Unable to map context %s, rc = %d.\n",
  1490. c->str, -rc);
  1491. goto out;
  1492. }
  1493. }
  1494. rc = context_cpy(&oldc, c);
  1495. if (rc)
  1496. goto out;
  1497. /* Convert the user. */
  1498. rc = -EINVAL;
  1499. usrdatum = hashtab_search(args->newp->p_users.table,
  1500. sym_name(args->oldp, SYM_USERS, c->user - 1));
  1501. if (!usrdatum)
  1502. goto bad;
  1503. c->user = usrdatum->value;
  1504. /* Convert the role. */
  1505. rc = -EINVAL;
  1506. role = hashtab_search(args->newp->p_roles.table,
  1507. sym_name(args->oldp, SYM_ROLES, c->role - 1));
  1508. if (!role)
  1509. goto bad;
  1510. c->role = role->value;
  1511. /* Convert the type. */
  1512. rc = -EINVAL;
  1513. typdatum = hashtab_search(args->newp->p_types.table,
  1514. sym_name(args->oldp, SYM_TYPES, c->type - 1));
  1515. if (!typdatum)
  1516. goto bad;
  1517. c->type = typdatum->value;
  1518. /* Convert the MLS fields if dealing with MLS policies */
  1519. if (args->oldp->mls_enabled && args->newp->mls_enabled) {
  1520. rc = mls_convert_context(args->oldp, args->newp, c);
  1521. if (rc)
  1522. goto bad;
  1523. } else if (args->oldp->mls_enabled && !args->newp->mls_enabled) {
  1524. /*
  1525. * Switching between MLS and non-MLS policy:
  1526. * free any storage used by the MLS fields in the
  1527. * context for all existing entries in the sidtab.
  1528. */
  1529. mls_context_destroy(c);
  1530. } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
  1531. /*
  1532. * Switching between non-MLS and MLS policy:
  1533. * ensure that the MLS fields of the context for all
  1534. * existing entries in the sidtab are filled in with a
  1535. * suitable default value, likely taken from one of the
  1536. * initial SIDs.
  1537. */
  1538. oc = args->newp->ocontexts[OCON_ISID];
  1539. while (oc && oc->sid[0] != SECINITSID_UNLABELED)
  1540. oc = oc->next;
  1541. rc = -EINVAL;
  1542. if (!oc) {
  1543. printk(KERN_ERR "SELinux: unable to look up"
  1544. " the initial SIDs list\n");
  1545. goto bad;
  1546. }
  1547. range = &oc->context[0].range;
  1548. rc = mls_range_set(c, range);
  1549. if (rc)
  1550. goto bad;
  1551. }
  1552. /* Check the validity of the new context. */
  1553. if (!policydb_context_isvalid(args->newp, c)) {
  1554. rc = convert_context_handle_invalid_context(&oldc);
  1555. if (rc)
  1556. goto bad;
  1557. }
  1558. context_destroy(&oldc);
  1559. rc = 0;
  1560. out:
  1561. return rc;
  1562. bad:
  1563. /* Map old representation to string and save it. */
  1564. rc = context_struct_to_string(&oldc, &s, &len);
  1565. if (rc)
  1566. return rc;
  1567. context_destroy(&oldc);
  1568. context_destroy(c);
  1569. c->str = s;
  1570. c->len = len;
  1571. printk(KERN_INFO "SELinux: Context %s became invalid (unmapped).\n",
  1572. c->str);
  1573. rc = 0;
  1574. goto out;
  1575. }
  1576. static void security_load_policycaps(void)
  1577. {
  1578. selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
  1579. POLICYDB_CAPABILITY_NETPEER);
  1580. selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
  1581. POLICYDB_CAPABILITY_OPENPERM);
  1582. }
  1583. extern void selinux_complete_init(void);
  1584. static int security_preserve_bools(struct policydb *p);
  1585. /**
  1586. * security_load_policy - Load a security policy configuration.
  1587. * @data: binary policy data
  1588. * @len: length of data in bytes
  1589. *
  1590. * Load a new set of security policy configuration data,
  1591. * validate it and convert the SID table as necessary.
  1592. * This function will flush the access vector cache after
  1593. * loading the new policy.
  1594. */
  1595. int security_load_policy(void *data, size_t len)
  1596. {
  1597. struct policydb oldpolicydb, newpolicydb;
  1598. struct sidtab oldsidtab, newsidtab;
  1599. struct selinux_mapping *oldmap, *map = NULL;
  1600. struct convert_context_args args;
  1601. u32 seqno;
  1602. u16 map_size;
  1603. int rc = 0;
  1604. struct policy_file file = { data, len }, *fp = &file;
  1605. if (!ss_initialized) {
  1606. avtab_cache_init();
  1607. rc = policydb_read(&policydb, fp);
  1608. if (rc) {
  1609. avtab_cache_destroy();
  1610. return rc;
  1611. }
  1612. policydb.len = len;
  1613. rc = selinux_set_mapping(&policydb, secclass_map,
  1614. &current_mapping,
  1615. &current_mapping_size);
  1616. if (rc) {
  1617. policydb_destroy(&policydb);
  1618. avtab_cache_destroy();
  1619. return rc;
  1620. }
  1621. rc = policydb_load_isids(&policydb, &sidtab);
  1622. if (rc) {
  1623. policydb_destroy(&policydb);
  1624. avtab_cache_destroy();
  1625. return rc;
  1626. }
  1627. security_load_policycaps();
  1628. ss_initialized = 1;
  1629. seqno = ++latest_granting;
  1630. selinux_complete_init();
  1631. avc_ss_reset(seqno);
  1632. selnl_notify_policyload(seqno);
  1633. selinux_status_update_policyload(seqno);
  1634. selinux_netlbl_cache_invalidate();
  1635. selinux_xfrm_notify_policyload();
  1636. return 0;
  1637. }
  1638. #if 0
  1639. sidtab_hash_eval(&sidtab, "sids");
  1640. #endif
  1641. rc = policydb_read(&newpolicydb, fp);
  1642. if (rc)
  1643. return rc;
  1644. newpolicydb.len = len;
  1645. /* If switching between different policy types, log MLS status */
  1646. if (policydb.mls_enabled && !newpolicydb.mls_enabled)
  1647. printk(KERN_INFO "SELinux: Disabling MLS support...\n");
  1648. else if (!policydb.mls_enabled && newpolicydb.mls_enabled)
  1649. printk(KERN_INFO "SELinux: Enabling MLS support...\n");
  1650. rc = policydb_load_isids(&newpolicydb, &newsidtab);
  1651. if (rc) {
  1652. printk(KERN_ERR "SELinux: unable to load the initial SIDs\n");
  1653. policydb_destroy(&newpolicydb);
  1654. return rc;
  1655. }
  1656. rc = selinux_set_mapping(&newpolicydb, secclass_map, &map, &map_size);
  1657. if (rc)
  1658. goto err;
  1659. rc = security_preserve_bools(&newpolicydb);
  1660. if (rc) {
  1661. printk(KERN_ERR "SELinux: unable to preserve booleans\n");
  1662. goto err;
  1663. }
  1664. /* Clone the SID table. */
  1665. sidtab_shutdown(&sidtab);
  1666. rc = sidtab_map(&sidtab, clone_sid, &newsidtab);
  1667. if (rc)
  1668. goto err;
  1669. /*
  1670. * Convert the internal representations of contexts
  1671. * in the new SID table.
  1672. */
  1673. args.oldp = &policydb;
  1674. args.newp = &newpolicydb;
  1675. rc = sidtab_map(&newsidtab, convert_context, &args);
  1676. if (rc) {
  1677. printk(KERN_ERR "SELinux: unable to convert the internal"
  1678. " representation of contexts in the new SID"
  1679. " table\n");
  1680. goto err;
  1681. }
  1682. /* Save the old policydb and SID table to free later. */
  1683. memcpy(&oldpolicydb, &policydb, sizeof policydb);
  1684. sidtab_set(&oldsidtab, &sidtab);
  1685. /* Install the new policydb and SID table. */
  1686. write_lock_irq(&policy_rwlock);
  1687. memcpy(&policydb, &newpolicydb, sizeof policydb);
  1688. sidtab_set(&sidtab, &newsidtab);
  1689. security_load_policycaps();
  1690. oldmap = current_mapping;
  1691. current_mapping = map;
  1692. current_mapping_size = map_size;
  1693. seqno = ++latest_granting;
  1694. write_unlock_irq(&policy_rwlock);
  1695. /* Free the old policydb and SID table. */
  1696. policydb_destroy(&oldpolicydb);
  1697. sidtab_destroy(&oldsidtab);
  1698. kfree(oldmap);
  1699. avc_ss_reset(seqno);
  1700. selnl_notify_policyload(seqno);
  1701. selinux_status_update_policyload(seqno);
  1702. selinux_netlbl_cache_invalidate();
  1703. selinux_xfrm_notify_policyload();
  1704. return 0;
  1705. err:
  1706. kfree(map);
  1707. sidtab_destroy(&newsidtab);
  1708. policydb_destroy(&newpolicydb);
  1709. return rc;
  1710. }
  1711. size_t security_policydb_len(void)
  1712. {
  1713. size_t len;
  1714. read_lock(&policy_rwlock);
  1715. len = policydb.len;
  1716. read_unlock(&policy_rwlock);
  1717. return len;
  1718. }
  1719. /**
  1720. * security_port_sid - Obtain the SID for a port.
  1721. * @protocol: protocol number
  1722. * @port: port number
  1723. * @out_sid: security identifier
  1724. */
  1725. int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
  1726. {
  1727. struct ocontext *c;
  1728. int rc = 0;
  1729. read_lock(&policy_rwlock);
  1730. c = policydb.ocontexts[OCON_PORT];
  1731. while (c) {
  1732. if (c->u.port.protocol == protocol &&
  1733. c->u.port.low_port <= port &&
  1734. c->u.port.high_port >= port)
  1735. break;
  1736. c = c->next;
  1737. }
  1738. if (c) {
  1739. if (!c->sid[0]) {
  1740. rc = sidtab_context_to_sid(&sidtab,
  1741. &c->context[0],
  1742. &c->sid[0]);
  1743. if (rc)
  1744. goto out;
  1745. }
  1746. *out_sid = c->sid[0];
  1747. } else {
  1748. *out_sid = SECINITSID_PORT;
  1749. }
  1750. out:
  1751. read_unlock(&policy_rwlock);
  1752. return rc;
  1753. }
  1754. /**
  1755. * security_netif_sid - Obtain the SID for a network interface.
  1756. * @name: interface name
  1757. * @if_sid: interface SID
  1758. */
  1759. int security_netif_sid(char *name, u32 *if_sid)
  1760. {
  1761. int rc = 0;
  1762. struct ocontext *c;
  1763. read_lock(&policy_rwlock);
  1764. c = policydb.ocontexts[OCON_NETIF];
  1765. while (c) {
  1766. if (strcmp(name, c->u.name) == 0)
  1767. break;
  1768. c = c->next;
  1769. }
  1770. if (c) {
  1771. if (!c->sid[0] || !c->sid[1]) {
  1772. rc = sidtab_context_to_sid(&sidtab,
  1773. &c->context[0],
  1774. &c->sid[0]);
  1775. if (rc)
  1776. goto out;
  1777. rc = sidtab_context_to_sid(&sidtab,
  1778. &c->context[1],
  1779. &c->sid[1]);
  1780. if (rc)
  1781. goto out;
  1782. }
  1783. *if_sid = c->sid[0];
  1784. } else
  1785. *if_sid = SECINITSID_NETIF;
  1786. out:
  1787. read_unlock(&policy_rwlock);
  1788. return rc;
  1789. }
  1790. static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
  1791. {
  1792. int i, fail = 0;
  1793. for (i = 0; i < 4; i++)
  1794. if (addr[i] != (input[i] & mask[i])) {
  1795. fail = 1;
  1796. break;
  1797. }
  1798. return !fail;
  1799. }
  1800. /**
  1801. * security_node_sid -