PageRenderTime 48ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/security/selinux/ss/services.c

https://gitlab.com/LiquidSmooth-Devices/android_kernel_htc_msm8974
C | 2877 lines | 2367 code | 469 blank | 41 comment | 442 complexity | 31ad24ff7ada71a99565e5cd23638c3d MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full 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. int selinux_policycap_netpeer;
  72. int selinux_policycap_openperm;
  73. static DEFINE_RWLOCK(policy_rwlock);
  74. static struct sidtab sidtab;
  75. struct policydb policydb;
  76. int ss_initialized;
  77. static u32 latest_granting;
  78. static int context_struct_to_string(struct context *context, char **scontext,
  79. u32 *scontext_len);
  80. static void context_struct_compute_av(struct context *scontext,
  81. struct context *tcontext,
  82. u16 tclass,
  83. struct av_decision *avd);
  84. struct selinux_mapping {
  85. u16 value;
  86. unsigned num_perms;
  87. u32 perms[sizeof(u32) * 8];
  88. };
  89. static struct selinux_mapping *current_mapping;
  90. static u16 current_mapping_size;
  91. static int selinux_set_mapping(struct policydb *pol,
  92. struct security_class_mapping *map,
  93. struct selinux_mapping **out_map_p,
  94. u16 *out_map_size)
  95. {
  96. struct selinux_mapping *out_map = NULL;
  97. size_t size = sizeof(struct selinux_mapping);
  98. u16 i, j;
  99. unsigned k;
  100. bool print_unknown_handle = false;
  101. if (!map)
  102. return -EINVAL;
  103. i = 0;
  104. while (map[i].name)
  105. i++;
  106. out_map = kcalloc(++i, size, GFP_ATOMIC);
  107. if (!out_map)
  108. return -ENOMEM;
  109. j = 0;
  110. while (map[j].name) {
  111. struct security_class_mapping *p_in = map + (j++);
  112. struct selinux_mapping *p_out = out_map + j;
  113. if (!strcmp(p_in->name, "")) {
  114. p_out->num_perms = 0;
  115. continue;
  116. }
  117. p_out->value = string_to_security_class(pol, p_in->name);
  118. if (!p_out->value) {
  119. printk(KERN_INFO
  120. "SELinux: Class %s not defined in policy.\n",
  121. p_in->name);
  122. if (pol->reject_unknown)
  123. goto err;
  124. p_out->num_perms = 0;
  125. print_unknown_handle = true;
  126. continue;
  127. }
  128. k = 0;
  129. while (p_in->perms && p_in->perms[k]) {
  130. if (!*p_in->perms[k]) {
  131. k++;
  132. continue;
  133. }
  134. p_out->perms[k] = string_to_av_perm(pol, p_out->value,
  135. p_in->perms[k]);
  136. if (!p_out->perms[k]) {
  137. printk(KERN_INFO
  138. "SELinux: Permission %s in class %s not defined in policy.\n",
  139. p_in->perms[k], p_in->name);
  140. if (pol->reject_unknown)
  141. goto err;
  142. print_unknown_handle = true;
  143. }
  144. k++;
  145. }
  146. p_out->num_perms = k;
  147. }
  148. if (print_unknown_handle)
  149. printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
  150. pol->allow_unknown ? "allowed" : "denied");
  151. *out_map_p = out_map;
  152. *out_map_size = i;
  153. return 0;
  154. err:
  155. kfree(out_map);
  156. return -EINVAL;
  157. }
  158. static u16 unmap_class(u16 tclass)
  159. {
  160. if (tclass < current_mapping_size)
  161. return current_mapping[tclass].value;
  162. return tclass;
  163. }
  164. static u16 map_class(u16 pol_value)
  165. {
  166. u16 i;
  167. for (i = 1; i < current_mapping_size; i++) {
  168. if (current_mapping[i].value == pol_value)
  169. return i;
  170. }
  171. return SECCLASS_NULL;
  172. }
  173. static void map_decision(u16 tclass, struct av_decision *avd,
  174. int allow_unknown)
  175. {
  176. if (tclass < current_mapping_size) {
  177. unsigned i, n = current_mapping[tclass].num_perms;
  178. u32 result;
  179. for (i = 0, result = 0; i < n; i++) {
  180. if (avd->allowed & current_mapping[tclass].perms[i])
  181. result |= 1<<i;
  182. if (allow_unknown && !current_mapping[tclass].perms[i])
  183. result |= 1<<i;
  184. }
  185. avd->allowed = result;
  186. for (i = 0, result = 0; i < n; i++)
  187. if (avd->auditallow & current_mapping[tclass].perms[i])
  188. result |= 1<<i;
  189. avd->auditallow = result;
  190. for (i = 0, result = 0; i < n; i++) {
  191. if (avd->auditdeny & current_mapping[tclass].perms[i])
  192. result |= 1<<i;
  193. if (!allow_unknown && !current_mapping[tclass].perms[i])
  194. result |= 1<<i;
  195. }
  196. for (; i < (sizeof(u32)*8); i++)
  197. result |= 1<<i;
  198. avd->auditdeny = result;
  199. }
  200. }
  201. int security_mls_enabled(void)
  202. {
  203. return policydb.mls_enabled;
  204. }
  205. static int constraint_expr_eval(struct context *scontext,
  206. struct context *tcontext,
  207. struct context *xcontext,
  208. struct constraint_expr *cexpr)
  209. {
  210. u32 val1, val2;
  211. struct context *c;
  212. struct role_datum *r1, *r2;
  213. struct mls_level *l1, *l2;
  214. struct constraint_expr *e;
  215. int s[CEXPR_MAXDEPTH] = {0};
  216. int sp = -1;
  217. for (e = cexpr; e; e = e->next) {
  218. switch (e->expr_type) {
  219. case CEXPR_NOT:
  220. BUG_ON(sp < 0);
  221. s[sp] = !s[sp];
  222. break;
  223. case CEXPR_AND:
  224. BUG_ON(sp < 1);
  225. sp--;
  226. s[sp] &= s[sp + 1];
  227. break;
  228. case CEXPR_OR:
  229. BUG_ON(sp < 1);
  230. sp--;
  231. s[sp] |= s[sp + 1];
  232. break;
  233. case CEXPR_ATTR:
  234. if (sp == (CEXPR_MAXDEPTH - 1))
  235. return 0;
  236. switch (e->attr) {
  237. case CEXPR_USER:
  238. val1 = scontext->user;
  239. val2 = tcontext->user;
  240. break;
  241. case CEXPR_TYPE:
  242. val1 = scontext->type;
  243. val2 = tcontext->type;
  244. break;
  245. case CEXPR_ROLE:
  246. val1 = scontext->role;
  247. val2 = tcontext->role;
  248. r1 = policydb.role_val_to_struct[val1 - 1];
  249. r2 = policydb.role_val_to_struct[val2 - 1];
  250. switch (e->op) {
  251. case CEXPR_DOM:
  252. s[++sp] = ebitmap_get_bit(&r1->dominates,
  253. val2 - 1);
  254. continue;
  255. case CEXPR_DOMBY:
  256. s[++sp] = ebitmap_get_bit(&r2->dominates,
  257. val1 - 1);
  258. continue;
  259. case CEXPR_INCOMP:
  260. s[++sp] = (!ebitmap_get_bit(&r1->dominates,
  261. val2 - 1) &&
  262. !ebitmap_get_bit(&r2->dominates,
  263. val1 - 1));
  264. continue;
  265. default:
  266. break;
  267. }
  268. break;
  269. case CEXPR_L1L2:
  270. l1 = &(scontext->range.level[0]);
  271. l2 = &(tcontext->range.level[0]);
  272. goto mls_ops;
  273. case CEXPR_L1H2:
  274. l1 = &(scontext->range.level[0]);
  275. l2 = &(tcontext->range.level[1]);
  276. goto mls_ops;
  277. case CEXPR_H1L2:
  278. l1 = &(scontext->range.level[1]);
  279. l2 = &(tcontext->range.level[0]);
  280. goto mls_ops;
  281. case CEXPR_H1H2:
  282. l1 = &(scontext->range.level[1]);
  283. l2 = &(tcontext->range.level[1]);
  284. goto mls_ops;
  285. case CEXPR_L1H1:
  286. l1 = &(scontext->range.level[0]);
  287. l2 = &(scontext->range.level[1]);
  288. goto mls_ops;
  289. case CEXPR_L2H2:
  290. l1 = &(tcontext->range.level[0]);
  291. l2 = &(tcontext->range.level[1]);
  292. goto mls_ops;
  293. mls_ops:
  294. switch (e->op) {
  295. case CEXPR_EQ:
  296. s[++sp] = mls_level_eq(l1, l2);
  297. continue;
  298. case CEXPR_NEQ:
  299. s[++sp] = !mls_level_eq(l1, l2);
  300. continue;
  301. case CEXPR_DOM:
  302. s[++sp] = mls_level_dom(l1, l2);
  303. continue;
  304. case CEXPR_DOMBY:
  305. s[++sp] = mls_level_dom(l2, l1);
  306. continue;
  307. case CEXPR_INCOMP:
  308. s[++sp] = mls_level_incomp(l2, l1);
  309. continue;
  310. default:
  311. BUG();
  312. return 0;
  313. }
  314. break;
  315. default:
  316. BUG();
  317. return 0;
  318. }
  319. switch (e->op) {
  320. case CEXPR_EQ:
  321. s[++sp] = (val1 == val2);
  322. break;
  323. case CEXPR_NEQ:
  324. s[++sp] = (val1 != val2);
  325. break;
  326. default:
  327. BUG();
  328. return 0;
  329. }
  330. break;
  331. case CEXPR_NAMES:
  332. if (sp == (CEXPR_MAXDEPTH-1))
  333. return 0;
  334. c = scontext;
  335. if (e->attr & CEXPR_TARGET)
  336. c = tcontext;
  337. else if (e->attr & CEXPR_XTARGET) {
  338. c = xcontext;
  339. if (!c) {
  340. BUG();
  341. return 0;
  342. }
  343. }
  344. if (e->attr & CEXPR_USER)
  345. val1 = c->user;
  346. else if (e->attr & CEXPR_ROLE)
  347. val1 = c->role;
  348. else if (e->attr & CEXPR_TYPE)
  349. val1 = c->type;
  350. else {
  351. BUG();
  352. return 0;
  353. }
  354. switch (e->op) {
  355. case CEXPR_EQ:
  356. s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
  357. break;
  358. case CEXPR_NEQ:
  359. s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
  360. break;
  361. default:
  362. BUG();
  363. return 0;
  364. }
  365. break;
  366. default:
  367. BUG();
  368. return 0;
  369. }
  370. }
  371. BUG_ON(sp != 0);
  372. return s[0];
  373. }
  374. static int dump_masked_av_helper(void *k, void *d, void *args)
  375. {
  376. struct perm_datum *pdatum = d;
  377. char **permission_names = args;
  378. BUG_ON(pdatum->value < 1 || pdatum->value > 32);
  379. permission_names[pdatum->value - 1] = (char *)k;
  380. return 0;
  381. }
  382. static void security_dump_masked_av(struct context *scontext,
  383. struct context *tcontext,
  384. u16 tclass,
  385. u32 permissions,
  386. const char *reason)
  387. {
  388. struct common_datum *common_dat;
  389. struct class_datum *tclass_dat;
  390. struct audit_buffer *ab;
  391. char *tclass_name;
  392. char *scontext_name = NULL;
  393. char *tcontext_name = NULL;
  394. char *permission_names[32];
  395. int index;
  396. u32 length;
  397. bool need_comma = false;
  398. if (!permissions)
  399. return;
  400. tclass_name = sym_name(&policydb, SYM_CLASSES, tclass - 1);
  401. tclass_dat = policydb.class_val_to_struct[tclass - 1];
  402. common_dat = tclass_dat->comdatum;
  403. if (common_dat &&
  404. hashtab_map(common_dat->permissions.table,
  405. dump_masked_av_helper, permission_names) < 0)
  406. goto out;
  407. if (hashtab_map(tclass_dat->permissions.table,
  408. dump_masked_av_helper, permission_names) < 0)
  409. goto out;
  410. if (context_struct_to_string(scontext,
  411. &scontext_name, &length) < 0)
  412. goto out;
  413. if (context_struct_to_string(tcontext,
  414. &tcontext_name, &length) < 0)
  415. goto out;
  416. ab = audit_log_start(current->audit_context,
  417. GFP_ATOMIC, AUDIT_SELINUX_ERR);
  418. if (!ab)
  419. goto out;
  420. audit_log_format(ab, "op=security_compute_av reason=%s "
  421. "scontext=%s tcontext=%s tclass=%s perms=",
  422. reason, scontext_name, tcontext_name, tclass_name);
  423. for (index = 0; index < 32; index++) {
  424. u32 mask = (1 << index);
  425. if ((mask & permissions) == 0)
  426. continue;
  427. audit_log_format(ab, "%s%s",
  428. need_comma ? "," : "",
  429. permission_names[index]
  430. ? permission_names[index] : "????");
  431. need_comma = true;
  432. }
  433. audit_log_end(ab);
  434. out:
  435. kfree(tcontext_name);
  436. kfree(scontext_name);
  437. return;
  438. }
  439. static void type_attribute_bounds_av(struct context *scontext,
  440. struct context *tcontext,
  441. u16 tclass,
  442. struct av_decision *avd)
  443. {
  444. struct context lo_scontext;
  445. struct context lo_tcontext;
  446. struct av_decision lo_avd;
  447. struct type_datum *source;
  448. struct type_datum *target;
  449. u32 masked = 0;
  450. source = flex_array_get_ptr(policydb.type_val_to_struct_array,
  451. scontext->type - 1);
  452. BUG_ON(!source);
  453. target = flex_array_get_ptr(policydb.type_val_to_struct_array,
  454. tcontext->type - 1);
  455. BUG_ON(!target);
  456. if (source->bounds) {
  457. memset(&lo_avd, 0, sizeof(lo_avd));
  458. memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
  459. lo_scontext.type = source->bounds;
  460. context_struct_compute_av(&lo_scontext,
  461. tcontext,
  462. tclass,
  463. &lo_avd);
  464. if ((lo_avd.allowed & avd->allowed) == avd->allowed)
  465. return;
  466. masked = ~lo_avd.allowed & avd->allowed;
  467. }
  468. if (target->bounds) {
  469. memset(&lo_avd, 0, sizeof(lo_avd));
  470. memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
  471. lo_tcontext.type = target->bounds;
  472. context_struct_compute_av(scontext,
  473. &lo_tcontext,
  474. tclass,
  475. &lo_avd);
  476. if ((lo_avd.allowed & avd->allowed) == avd->allowed)
  477. return;
  478. masked = ~lo_avd.allowed & avd->allowed;
  479. }
  480. if (source->bounds && target->bounds) {
  481. memset(&lo_avd, 0, sizeof(lo_avd));
  482. context_struct_compute_av(&lo_scontext,
  483. &lo_tcontext,
  484. tclass,
  485. &lo_avd);
  486. if ((lo_avd.allowed & avd->allowed) == avd->allowed)
  487. return;
  488. masked = ~lo_avd.allowed & avd->allowed;
  489. }
  490. if (masked) {
  491. avd->allowed &= ~masked;
  492. security_dump_masked_av(scontext, tcontext,
  493. tclass, masked, "bounds");
  494. }
  495. }
  496. static void context_struct_compute_av(struct context *scontext,
  497. struct context *tcontext,
  498. u16 tclass,
  499. struct av_decision *avd)
  500. {
  501. struct constraint_node *constraint;
  502. struct role_allow *ra;
  503. struct avtab_key avkey;
  504. struct avtab_node *node;
  505. struct class_datum *tclass_datum;
  506. struct ebitmap *sattr, *tattr;
  507. struct ebitmap_node *snode, *tnode;
  508. unsigned int i, j;
  509. avd->allowed = 0;
  510. avd->auditallow = 0;
  511. avd->auditdeny = 0xffffffff;
  512. if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
  513. if (printk_ratelimit())
  514. printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass);
  515. return;
  516. }
  517. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  518. avkey.target_class = tclass;
  519. avkey.specified = AVTAB_AV;
  520. sattr = flex_array_get(policydb.type_attr_map_array, scontext->type - 1);
  521. BUG_ON(!sattr);
  522. tattr = flex_array_get(policydb.type_attr_map_array, tcontext->type - 1);
  523. BUG_ON(!tattr);
  524. ebitmap_for_each_positive_bit(sattr, snode, i) {
  525. ebitmap_for_each_positive_bit(tattr, tnode, j) {
  526. avkey.source_type = i + 1;
  527. avkey.target_type = j + 1;
  528. for (node = avtab_search_node(&policydb.te_avtab, &avkey);
  529. node;
  530. node = avtab_search_node_next(node, avkey.specified)) {
  531. if (node->key.specified == AVTAB_ALLOWED)
  532. avd->allowed |= node->datum.data;
  533. else if (node->key.specified == AVTAB_AUDITALLOW)
  534. avd->auditallow |= node->datum.data;
  535. else if (node->key.specified == AVTAB_AUDITDENY)
  536. avd->auditdeny &= node->datum.data;
  537. }
  538. cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
  539. }
  540. }
  541. constraint = tclass_datum->constraints;
  542. while (constraint) {
  543. if ((constraint->permissions & (avd->allowed)) &&
  544. !constraint_expr_eval(scontext, tcontext, NULL,
  545. constraint->expr)) {
  546. avd->allowed &= ~(constraint->permissions);
  547. }
  548. constraint = constraint->next;
  549. }
  550. if (tclass == policydb.process_class &&
  551. (avd->allowed & policydb.process_trans_perms) &&
  552. scontext->role != tcontext->role) {
  553. for (ra = policydb.role_allow; ra; ra = ra->next) {
  554. if (scontext->role == ra->role &&
  555. tcontext->role == ra->new_role)
  556. break;
  557. }
  558. if (!ra)
  559. avd->allowed &= ~policydb.process_trans_perms;
  560. }
  561. type_attribute_bounds_av(scontext, tcontext,
  562. tclass, avd);
  563. }
  564. static int security_validtrans_handle_fail(struct context *ocontext,
  565. struct context *ncontext,
  566. struct context *tcontext,
  567. u16 tclass)
  568. {
  569. char *o = NULL, *n = NULL, *t = NULL;
  570. u32 olen, nlen, tlen;
  571. if (context_struct_to_string(ocontext, &o, &olen))
  572. goto out;
  573. if (context_struct_to_string(ncontext, &n, &nlen))
  574. goto out;
  575. if (context_struct_to_string(tcontext, &t, &tlen))
  576. goto out;
  577. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  578. "security_validate_transition: denied for"
  579. " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
  580. o, n, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
  581. out:
  582. kfree(o);
  583. kfree(n);
  584. kfree(t);
  585. if (!selinux_enforcing)
  586. return 0;
  587. return -EPERM;
  588. }
  589. int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
  590. u16 orig_tclass)
  591. {
  592. struct context *ocontext;
  593. struct context *ncontext;
  594. struct context *tcontext;
  595. struct class_datum *tclass_datum;
  596. struct constraint_node *constraint;
  597. u16 tclass;
  598. int rc = 0;
  599. if (!ss_initialized)
  600. return 0;
  601. read_lock(&policy_rwlock);
  602. tclass = unmap_class(orig_tclass);
  603. if (!tclass || tclass > policydb.p_classes.nprim) {
  604. printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
  605. __func__, tclass);
  606. rc = -EINVAL;
  607. goto out;
  608. }
  609. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  610. ocontext = sidtab_search(&sidtab, oldsid);
  611. if (!ocontext) {
  612. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  613. __func__, oldsid);
  614. rc = -EINVAL;
  615. goto out;
  616. }
  617. ncontext = sidtab_search(&sidtab, newsid);
  618. if (!ncontext) {
  619. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  620. __func__, newsid);
  621. rc = -EINVAL;
  622. goto out;
  623. }
  624. tcontext = sidtab_search(&sidtab, tasksid);
  625. if (!tcontext) {
  626. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  627. __func__, tasksid);
  628. rc = -EINVAL;
  629. goto out;
  630. }
  631. constraint = tclass_datum->validatetrans;
  632. while (constraint) {
  633. if (!constraint_expr_eval(ocontext, ncontext, tcontext,
  634. constraint->expr)) {
  635. rc = security_validtrans_handle_fail(ocontext, ncontext,
  636. tcontext, tclass);
  637. goto out;
  638. }
  639. constraint = constraint->next;
  640. }
  641. out:
  642. read_unlock(&policy_rwlock);
  643. return rc;
  644. }
  645. int security_bounded_transition(u32 old_sid, u32 new_sid)
  646. {
  647. struct context *old_context, *new_context;
  648. struct type_datum *type;
  649. int index;
  650. int rc;
  651. read_lock(&policy_rwlock);
  652. rc = -EINVAL;
  653. old_context = sidtab_search(&sidtab, old_sid);
  654. if (!old_context) {
  655. printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
  656. __func__, old_sid);
  657. goto out;
  658. }
  659. rc = -EINVAL;
  660. new_context = sidtab_search(&sidtab, new_sid);
  661. if (!new_context) {
  662. printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
  663. __func__, new_sid);
  664. goto out;
  665. }
  666. rc = 0;
  667. if (old_context->type == new_context->type)
  668. goto out;
  669. index = new_context->type;
  670. while (true) {
  671. type = flex_array_get_ptr(policydb.type_val_to_struct_array,
  672. index - 1);
  673. BUG_ON(!type);
  674. rc = -EPERM;
  675. if (!type->bounds)
  676. break;
  677. rc = 0;
  678. if (type->bounds == old_context->type)
  679. break;
  680. index = type->bounds;
  681. }
  682. if (rc) {
  683. char *old_name = NULL;
  684. char *new_name = NULL;
  685. u32 length;
  686. if (!context_struct_to_string(old_context,
  687. &old_name, &length) &&
  688. !context_struct_to_string(new_context,
  689. &new_name, &length)) {
  690. audit_log(current->audit_context,
  691. GFP_ATOMIC, AUDIT_SELINUX_ERR,
  692. "op=security_bounded_transition "
  693. "result=denied "
  694. "oldcontext=%s newcontext=%s",
  695. old_name, new_name);
  696. }
  697. kfree(new_name);
  698. kfree(old_name);
  699. }
  700. out:
  701. read_unlock(&policy_rwlock);
  702. return rc;
  703. }
  704. static void avd_init(struct av_decision *avd)
  705. {
  706. avd->allowed = 0;
  707. avd->auditallow = 0;
  708. avd->auditdeny = 0xffffffff;
  709. avd->seqno = latest_granting;
  710. avd->flags = 0;
  711. }
  712. void security_compute_av(u32 ssid,
  713. u32 tsid,
  714. u16 orig_tclass,
  715. struct av_decision *avd)
  716. {
  717. u16 tclass;
  718. struct context *scontext = NULL, *tcontext = NULL;
  719. read_lock(&policy_rwlock);
  720. avd_init(avd);
  721. if (!ss_initialized)
  722. goto allow;
  723. scontext = sidtab_search(&sidtab, ssid);
  724. if (!scontext) {
  725. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  726. __func__, ssid);
  727. goto out;
  728. }
  729. if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
  730. avd->flags |= AVD_FLAGS_PERMISSIVE;
  731. tcontext = sidtab_search(&sidtab, tsid);
  732. if (!tcontext) {
  733. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  734. __func__, tsid);
  735. goto out;
  736. }
  737. tclass = unmap_class(orig_tclass);
  738. if (unlikely(orig_tclass && !tclass)) {
  739. if (policydb.allow_unknown)
  740. goto allow;
  741. goto out;
  742. }
  743. context_struct_compute_av(scontext, tcontext, tclass, avd);
  744. map_decision(orig_tclass, avd, policydb.allow_unknown);
  745. out:
  746. read_unlock(&policy_rwlock);
  747. return;
  748. allow:
  749. avd->allowed = 0xffffffff;
  750. goto out;
  751. }
  752. void security_compute_av_user(u32 ssid,
  753. u32 tsid,
  754. u16 tclass,
  755. struct av_decision *avd)
  756. {
  757. struct context *scontext = NULL, *tcontext = NULL;
  758. read_lock(&policy_rwlock);
  759. avd_init(avd);
  760. if (!ss_initialized)
  761. goto allow;
  762. scontext = sidtab_search(&sidtab, ssid);
  763. if (!scontext) {
  764. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  765. __func__, ssid);
  766. goto out;
  767. }
  768. if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
  769. avd->flags |= AVD_FLAGS_PERMISSIVE;
  770. tcontext = sidtab_search(&sidtab, tsid);
  771. if (!tcontext) {
  772. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  773. __func__, tsid);
  774. goto out;
  775. }
  776. if (unlikely(!tclass)) {
  777. if (policydb.allow_unknown)
  778. goto allow;
  779. goto out;
  780. }
  781. context_struct_compute_av(scontext, tcontext, tclass, avd);
  782. out:
  783. read_unlock(&policy_rwlock);
  784. return;
  785. allow:
  786. avd->allowed = 0xffffffff;
  787. goto out;
  788. }
  789. static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
  790. {
  791. char *scontextp;
  792. if (scontext)
  793. *scontext = NULL;
  794. *scontext_len = 0;
  795. if (context->len) {
  796. *scontext_len = context->len;
  797. if (scontext) {
  798. *scontext = kstrdup(context->str, GFP_ATOMIC);
  799. if (!(*scontext))
  800. return -ENOMEM;
  801. }
  802. return 0;
  803. }
  804. *scontext_len += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) + 1;
  805. *scontext_len += strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) + 1;
  806. *scontext_len += strlen(sym_name(&policydb, SYM_TYPES, context->type - 1)) + 1;
  807. *scontext_len += mls_compute_context_len(context);
  808. if (!scontext)
  809. return 0;
  810. scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
  811. if (!scontextp)
  812. return -ENOMEM;
  813. *scontext = scontextp;
  814. snprintf(scontextp, *scontext_len, "%s:%s:%s",
  815. sym_name(&policydb, SYM_USERS, context->user - 1),
  816. sym_name(&policydb, SYM_ROLES, context->role - 1),
  817. sym_name(&policydb, SYM_TYPES, context->type - 1));
  818. scontextp += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) +
  819. 1 + strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) +
  820. 1 + strlen(sym_name(&policydb, SYM_TYPES, context->type - 1));
  821. mls_sid_to_context(context, &scontextp);
  822. *scontextp = 0;
  823. return 0;
  824. }
  825. #include "initial_sid_to_string.h"
  826. const char *security_get_initial_sid_context(u32 sid)
  827. {
  828. if (unlikely(sid > SECINITSID_NUM))
  829. return NULL;
  830. return initial_sid_to_string[sid];
  831. }
  832. static int security_sid_to_context_core(u32 sid, char **scontext,
  833. u32 *scontext_len, int force)
  834. {
  835. struct context *context;
  836. int rc = 0;
  837. if (scontext)
  838. *scontext = NULL;
  839. *scontext_len = 0;
  840. if (!ss_initialized) {
  841. if (sid <= SECINITSID_NUM) {
  842. char *scontextp;
  843. *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
  844. if (!scontext)
  845. goto out;
  846. scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
  847. if (!scontextp) {
  848. rc = -ENOMEM;
  849. goto out;
  850. }
  851. strcpy(scontextp, initial_sid_to_string[sid]);
  852. *scontext = scontextp;
  853. goto out;
  854. }
  855. printk(KERN_ERR "SELinux: %s: called before initial "
  856. "load_policy on unknown SID %d\n", __func__, sid);
  857. rc = -EINVAL;
  858. goto out;
  859. }
  860. read_lock(&policy_rwlock);
  861. if (force)
  862. context = sidtab_search_force(&sidtab, sid);
  863. else
  864. context = sidtab_search(&sidtab, sid);
  865. if (!context) {
  866. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  867. __func__, sid);
  868. rc = -EINVAL;
  869. goto out_unlock;
  870. }
  871. rc = context_struct_to_string(context, scontext, scontext_len);
  872. out_unlock:
  873. read_unlock(&policy_rwlock);
  874. out:
  875. return rc;
  876. }
  877. int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
  878. {
  879. return security_sid_to_context_core(sid, scontext, scontext_len, 0);
  880. }
  881. int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
  882. {
  883. return security_sid_to_context_core(sid, scontext, scontext_len, 1);
  884. }
  885. static int string_to_context_struct(struct policydb *pol,
  886. struct sidtab *sidtabp,
  887. char *scontext,
  888. u32 scontext_len,
  889. struct context *ctx,
  890. u32 def_sid)
  891. {
  892. struct role_datum *role;
  893. struct type_datum *typdatum;
  894. struct user_datum *usrdatum;
  895. char *scontextp, *p, oldc;
  896. int rc = 0;
  897. context_init(ctx);
  898. rc = -EINVAL;
  899. scontextp = (char *) scontext;
  900. p = scontextp;
  901. while (*p && *p != ':')
  902. p++;
  903. if (*p == 0)
  904. goto out;
  905. *p++ = 0;
  906. usrdatum = hashtab_search(pol->p_users.table, scontextp);
  907. if (!usrdatum)
  908. goto out;
  909. ctx->user = usrdatum->value;
  910. scontextp = p;
  911. while (*p && *p != ':')
  912. p++;
  913. if (*p == 0)
  914. goto out;
  915. *p++ = 0;
  916. role = hashtab_search(pol->p_roles.table, scontextp);
  917. if (!role)
  918. goto out;
  919. ctx->role = role->value;
  920. scontextp = p;
  921. while (*p && *p != ':')
  922. p++;
  923. oldc = *p;
  924. *p++ = 0;
  925. typdatum = hashtab_search(pol->p_types.table, scontextp);
  926. if (!typdatum || typdatum->attribute)
  927. goto out;
  928. ctx->type = typdatum->value;
  929. rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
  930. if (rc)
  931. goto out;
  932. rc = -EINVAL;
  933. if ((p - scontext) < scontext_len)
  934. goto out;
  935. if (!policydb_context_isvalid(pol, ctx))
  936. goto out;
  937. rc = 0;
  938. out:
  939. if (rc)
  940. context_destroy(ctx);
  941. return rc;
  942. }
  943. static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
  944. u32 *sid, u32 def_sid, gfp_t gfp_flags,
  945. int force)
  946. {
  947. char *scontext2, *str = NULL;
  948. struct context context;
  949. int rc = 0;
  950. if (!scontext_len)
  951. return -EINVAL;
  952. if (!ss_initialized) {
  953. int i;
  954. for (i = 1; i < SECINITSID_NUM; i++) {
  955. if (!strcmp(initial_sid_to_string[i], scontext)) {
  956. *sid = i;
  957. return 0;
  958. }
  959. }
  960. *sid = SECINITSID_KERNEL;
  961. return 0;
  962. }
  963. *sid = SECSID_NULL;
  964. scontext2 = kmalloc(scontext_len + 1, gfp_flags);
  965. if (!scontext2)
  966. return -ENOMEM;
  967. memcpy(scontext2, scontext, scontext_len);
  968. scontext2[scontext_len] = 0;
  969. if (force) {
  970. rc = -ENOMEM;
  971. str = kstrdup(scontext2, gfp_flags);
  972. if (!str)
  973. goto out;
  974. }
  975. read_lock(&policy_rwlock);
  976. rc = string_to_context_struct(&policydb, &sidtab, scontext2,
  977. scontext_len, &context, def_sid);
  978. if (rc == -EINVAL && force) {
  979. context.str = str;
  980. context.len = scontext_len;
  981. str = NULL;
  982. } else if (rc)
  983. goto out_unlock;
  984. rc = sidtab_context_to_sid(&sidtab, &context, sid);
  985. context_destroy(&context);
  986. out_unlock:
  987. read_unlock(&policy_rwlock);
  988. out:
  989. kfree(scontext2);
  990. kfree(str);
  991. return rc;
  992. }
  993. int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
  994. {
  995. return security_context_to_sid_core(scontext, scontext_len,
  996. sid, SECSID_NULL, GFP_KERNEL, 0);
  997. }
  998. int security_context_to_sid_default(const char *scontext, u32 scontext_len,
  999. u32 *sid, u32 def_sid, gfp_t gfp_flags)
  1000. {
  1001. return security_context_to_sid_core(scontext, scontext_len,
  1002. sid, def_sid, gfp_flags, 1);
  1003. }
  1004. int security_context_to_sid_force(const char *scontext, u32 scontext_len,
  1005. u32 *sid)
  1006. {
  1007. return security_context_to_sid_core(scontext, scontext_len,
  1008. sid, SECSID_NULL, GFP_KERNEL, 1);
  1009. }
  1010. static int compute_sid_handle_invalid_context(
  1011. struct context *scontext,
  1012. struct context *tcontext,
  1013. u16 tclass,
  1014. struct context *newcontext)
  1015. {
  1016. char *s = NULL, *t = NULL, *n = NULL;
  1017. u32 slen, tlen, nlen;
  1018. if (context_struct_to_string(scontext, &s, &slen))
  1019. goto out;
  1020. if (context_struct_to_string(tcontext, &t, &tlen))
  1021. goto out;
  1022. if (context_struct_to_string(newcontext, &n, &nlen))
  1023. goto out;
  1024. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1025. "security_compute_sid: invalid context %s"
  1026. " for scontext=%s"
  1027. " tcontext=%s"
  1028. " tclass=%s",
  1029. n, s, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
  1030. out:
  1031. kfree(s);
  1032. kfree(t);
  1033. kfree(n);
  1034. if (!selinux_enforcing)
  1035. return 0;
  1036. return -EACCES;
  1037. }
  1038. static void filename_compute_type(struct policydb *p, struct context *newcontext,
  1039. u32 stype, u32 ttype, u16 tclass,
  1040. const char *objname)
  1041. {
  1042. struct filename_trans ft;
  1043. struct filename_trans_datum *otype;
  1044. if (!ebitmap_get_bit(&p->filename_trans_ttypes, ttype))
  1045. return;
  1046. ft.stype = stype;
  1047. ft.ttype = ttype;
  1048. ft.tclass = tclass;
  1049. ft.name = objname;
  1050. otype = hashtab_search(p->filename_trans, &ft);
  1051. if (otype)
  1052. newcontext->type = otype->otype;
  1053. }
  1054. static int security_compute_sid(u32 ssid,
  1055. u32 tsid,
  1056. u16 orig_tclass,
  1057. u32 specified,
  1058. const char *objname,
  1059. u32 *out_sid,
  1060. bool kern)
  1061. {
  1062. struct context *scontext = NULL, *tcontext = NULL, newcontext;
  1063. struct role_trans *roletr = NULL;
  1064. struct avtab_key avkey;
  1065. struct avtab_datum *avdatum;
  1066. struct avtab_node *node;
  1067. u16 tclass;
  1068. int rc = 0;
  1069. bool sock;
  1070. if (!ss_initialized) {
  1071. switch (orig_tclass) {
  1072. case SECCLASS_PROCESS:
  1073. *out_sid = ssid;
  1074. break;
  1075. default:
  1076. *out_sid = tsid;
  1077. break;
  1078. }
  1079. goto out;
  1080. }
  1081. context_init(&newcontext);
  1082. read_lock(&policy_rwlock);
  1083. if (kern) {
  1084. tclass = unmap_class(orig_tclass);
  1085. sock = security_is_socket_class(orig_tclass);
  1086. } else {
  1087. tclass = orig_tclass;
  1088. sock = security_is_socket_class(map_class(tclass));
  1089. }
  1090. scontext = sidtab_search(&sidtab, ssid);
  1091. if (!scontext) {
  1092. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1093. __func__, ssid);
  1094. rc = -EINVAL;
  1095. goto out_unlock;
  1096. }
  1097. tcontext = sidtab_search(&sidtab, tsid);
  1098. if (!tcontext) {
  1099. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1100. __func__, tsid);
  1101. rc = -EINVAL;
  1102. goto out_unlock;
  1103. }
  1104. switch (specified) {
  1105. case AVTAB_TRANSITION:
  1106. case AVTAB_CHANGE:
  1107. newcontext.user = scontext->user;
  1108. break;
  1109. case AVTAB_MEMBER:
  1110. newcontext.user = tcontext->user;
  1111. break;
  1112. }
  1113. if ((tclass == policydb.process_class) || (sock == true)) {
  1114. newcontext.role = scontext->role;
  1115. newcontext.type = scontext->type;
  1116. } else {
  1117. newcontext.role = OBJECT_R_VAL;
  1118. newcontext.type = tcontext->type;
  1119. }
  1120. avkey.source_type = scontext->type;
  1121. avkey.target_type = tcontext->type;
  1122. avkey.target_class = tclass;
  1123. avkey.specified = specified;
  1124. avdatum = avtab_search(&policydb.te_avtab, &avkey);
  1125. if (!avdatum) {
  1126. node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
  1127. for (; node; node = avtab_search_node_next(node, specified)) {
  1128. if (node->key.specified & AVTAB_ENABLED) {
  1129. avdatum = &node->datum;
  1130. break;
  1131. }
  1132. }
  1133. }
  1134. if (avdatum) {
  1135. newcontext.type = avdatum->data;
  1136. }
  1137. if (objname)
  1138. filename_compute_type(&policydb, &newcontext, scontext->type,
  1139. tcontext->type, tclass, objname);
  1140. if (specified & AVTAB_TRANSITION) {
  1141. for (roletr = policydb.role_tr; roletr; roletr = roletr->next) {
  1142. if ((roletr->role == scontext->role) &&
  1143. (roletr->type == tcontext->type) &&
  1144. (roletr->tclass == tclass)) {
  1145. newcontext.role = roletr->new_role;
  1146. break;
  1147. }
  1148. }
  1149. }
  1150. rc = mls_compute_sid(scontext, tcontext, tclass, specified,
  1151. &newcontext, sock);
  1152. if (rc)
  1153. goto out_unlock;
  1154. if (!policydb_context_isvalid(&policydb, &newcontext)) {
  1155. rc = compute_sid_handle_invalid_context(scontext,
  1156. tcontext,
  1157. tclass,
  1158. &newcontext);
  1159. if (rc)
  1160. goto out_unlock;
  1161. }
  1162. rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
  1163. out_unlock:
  1164. read_unlock(&policy_rwlock);
  1165. context_destroy(&newcontext);
  1166. out:
  1167. return rc;
  1168. }
  1169. int security_transition_sid(u32 ssid, u32 tsid, u16 tclass,
  1170. const struct qstr *qstr, u32 *out_sid)
  1171. {
  1172. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
  1173. qstr ? qstr->name : NULL, out_sid, true);
  1174. }
  1175. int security_transition_sid_user(u32 ssid, u32 tsid, u16 tclass,
  1176. const char *objname, u32 *out_sid)
  1177. {
  1178. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
  1179. objname, out_sid, false);
  1180. }
  1181. int security_member_sid(u32 ssid,
  1182. u32 tsid,
  1183. u16 tclass,
  1184. u32 *out_sid)
  1185. {
  1186. return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, NULL,
  1187. out_sid, false);
  1188. }
  1189. int security_change_sid(u32 ssid,
  1190. u32 tsid,
  1191. u16 tclass,
  1192. u32 *out_sid)
  1193. {
  1194. return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, NULL,
  1195. out_sid, false);
  1196. }
  1197. static int clone_sid(u32 sid,
  1198. struct context *context,
  1199. void *arg)
  1200. {
  1201. struct sidtab *s = arg;
  1202. if (sid > SECINITSID_NUM)
  1203. return sidtab_insert(s, sid, context);
  1204. else
  1205. return 0;
  1206. }
  1207. static inline int convert_context_handle_invalid_context(struct context *context)
  1208. {
  1209. char *s;
  1210. u32 len;
  1211. if (selinux_enforcing)
  1212. return -EINVAL;
  1213. if (!context_struct_to_string(context, &s, &len)) {
  1214. printk(KERN_WARNING "SELinux: Context %s would be invalid if enforcing\n", s);
  1215. kfree(s);
  1216. }
  1217. return 0;
  1218. }
  1219. struct convert_context_args {
  1220. struct policydb *oldp;
  1221. struct policydb *newp;
  1222. };
  1223. static int convert_context(u32 key,
  1224. struct context *c,
  1225. void *p)
  1226. {
  1227. struct convert_context_args *args;
  1228. struct context oldc;
  1229. struct ocontext *oc;
  1230. struct mls_range *range;
  1231. struct role_datum *role;
  1232. struct type_datum *typdatum;
  1233. struct user_datum *usrdatum;
  1234. char *s;
  1235. u32 len;
  1236. int rc = 0;
  1237. if (key <= SECINITSID_NUM)
  1238. goto out;
  1239. args = p;
  1240. if (c->str) {
  1241. struct context ctx;
  1242. rc = -ENOMEM;
  1243. s = kstrdup(c->str, GFP_KERNEL);
  1244. if (!s)
  1245. goto out;
  1246. rc = string_to_context_struct(args->newp, NULL, s,
  1247. c->len, &ctx, SECSID_NULL);
  1248. kfree(s);
  1249. if (!rc) {
  1250. printk(KERN_INFO "SELinux: Context %s became valid (mapped).\n",
  1251. c->str);
  1252. kfree(c->str);
  1253. memcpy(c, &ctx, sizeof(*c));
  1254. goto out;
  1255. } else if (rc == -EINVAL) {
  1256. rc = 0;
  1257. goto out;
  1258. } else {
  1259. printk(KERN_ERR "SELinux: Unable to map context %s, rc = %d.\n",
  1260. c->str, -rc);
  1261. goto out;
  1262. }
  1263. }
  1264. rc = context_cpy(&oldc, c);
  1265. if (rc)
  1266. goto out;
  1267. rc = -EINVAL;
  1268. usrdatum = hashtab_search(args->newp->p_users.table,
  1269. sym_name(args->oldp, SYM_USERS, c->user - 1));
  1270. if (!usrdatum)
  1271. goto bad;
  1272. c->user = usrdatum->value;
  1273. rc = -EINVAL;
  1274. role = hashtab_search(args->newp->p_roles.table,
  1275. sym_name(args->oldp, SYM_ROLES, c->role - 1));
  1276. if (!role)
  1277. goto bad;
  1278. c->role = role->value;
  1279. rc = -EINVAL;
  1280. typdatum = hashtab_search(args->newp->p_types.table,
  1281. sym_name(args->oldp, SYM_TYPES, c->type - 1));
  1282. if (!typdatum)
  1283. goto bad;
  1284. c->type = typdatum->value;
  1285. if (args->oldp->mls_enabled && args->newp->mls_enabled) {
  1286. rc = mls_convert_context(args->oldp, args->newp, c);
  1287. if (rc)
  1288. goto bad;
  1289. } else if (args->oldp->mls_enabled && !args->newp->mls_enabled) {
  1290. mls_context_destroy(c);
  1291. } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
  1292. oc = args->newp->ocontexts[OCON_ISID];
  1293. while (oc && oc->sid[0] != SECINITSID_UNLABELED)
  1294. oc = oc->next;
  1295. rc = -EINVAL;
  1296. if (!oc) {
  1297. printk(KERN_ERR "SELinux: unable to look up"
  1298. " the initial SIDs list\n");
  1299. goto bad;
  1300. }
  1301. range = &oc->context[0].range;
  1302. rc = mls_range_set(c, range);
  1303. if (rc)
  1304. goto bad;
  1305. }
  1306. if (!policydb_context_isvalid(args->newp, c)) {
  1307. rc = convert_context_handle_invalid_context(&oldc);
  1308. if (rc)
  1309. goto bad;
  1310. }
  1311. context_destroy(&oldc);
  1312. rc = 0;
  1313. out:
  1314. return rc;
  1315. bad:
  1316. rc = context_struct_to_string(&oldc, &s, &len);
  1317. if (rc)
  1318. return rc;
  1319. context_destroy(&oldc);
  1320. context_destroy(c);
  1321. c->str = s;
  1322. c->len = len;
  1323. printk(KERN_INFO "SELinux: Context %s became invalid (unmapped).\n",
  1324. c->str);
  1325. rc = 0;
  1326. goto out;
  1327. }
  1328. static void security_load_policycaps(void)
  1329. {
  1330. selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
  1331. POLICYDB_CAPABILITY_NETPEER);
  1332. selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
  1333. POLICYDB_CAPABILITY_OPENPERM);
  1334. }
  1335. static int security_preserve_bools(struct policydb *p);
  1336. int security_load_policy(void *data, size_t len)
  1337. {
  1338. struct policydb oldpolicydb, newpolicydb;
  1339. struct sidtab oldsidtab, newsidtab;
  1340. struct selinux_mapping *oldmap, *map = NULL;
  1341. struct convert_context_args args;
  1342. u32 seqno;
  1343. u16 map_size;
  1344. int rc = 0;
  1345. struct policy_file file = { data, len }, *fp = &file;
  1346. if (!ss_initialized) {
  1347. avtab_cache_init();
  1348. rc = policydb_read(&policydb, fp);
  1349. if (rc) {
  1350. avtab_cache_destroy();
  1351. return rc;
  1352. }
  1353. policydb.len = len;
  1354. rc = selinux_set_mapping(&policydb, secclass_map,
  1355. &current_mapping,
  1356. &current_mapping_size);
  1357. if (rc) {
  1358. policydb_destroy(&policydb);
  1359. avtab_cache_destroy();
  1360. return rc;
  1361. }
  1362. rc = policydb_load_isids(&policydb, &sidtab);
  1363. if (rc) {
  1364. policydb_destroy(&policydb);
  1365. avtab_cache_destroy();
  1366. return rc;
  1367. }
  1368. security_load_policycaps();
  1369. ss_initialized = 1;
  1370. seqno = ++latest_granting;
  1371. selinux_complete_init();
  1372. avc_ss_reset(seqno);
  1373. selnl_notify_policyload(seqno);
  1374. selinux_status_update_policyload(seqno);
  1375. selinux_netlbl_cache_invalidate();
  1376. selinux_xfrm_notify_policyload();
  1377. return 0;
  1378. }
  1379. #if 0
  1380. sidtab_hash_eval(&sidtab, "sids");
  1381. #endif
  1382. rc = policydb_read(&newpolicydb, fp);
  1383. if (rc)
  1384. return rc;
  1385. newpolicydb.len = len;
  1386. if (policydb.mls_enabled && !newpolicydb.mls_enabled)
  1387. printk(KERN_INFO "SELinux: Disabling MLS support...\n");
  1388. else if (!policydb.mls_enabled && newpolicydb.mls_enabled)
  1389. printk(KERN_INFO "SELinux: Enabling MLS support...\n");
  1390. rc = policydb_load_isids(&newpolicydb, &newsidtab);
  1391. if (rc) {
  1392. printk(KERN_ERR "SELinux: unable to load the initial SIDs\n");
  1393. policydb_destroy(&newpolicydb);
  1394. return rc;
  1395. }
  1396. rc = selinux_set_mapping(&newpolicydb, secclass_map, &map, &map_size);
  1397. if (rc)
  1398. goto err;
  1399. rc = security_preserve_bools(&newpolicydb);
  1400. if (rc) {
  1401. printk(KERN_ERR "SELinux: unable to preserve booleans\n");
  1402. goto err;
  1403. }
  1404. sidtab_shutdown(&sidtab);
  1405. rc = sidtab_map(&sidtab, clone_sid, &newsidtab);
  1406. if (rc)
  1407. goto err;
  1408. args.oldp = &policydb;
  1409. args.newp = &newpolicydb;
  1410. rc = sidtab_map(&newsidtab, convert_context, &args);
  1411. if (rc) {
  1412. printk(KERN_ERR "SELinux: unable to convert the internal"
  1413. " representation of contexts in the new SID"
  1414. " table\n");
  1415. goto err;
  1416. }
  1417. memcpy(&oldpolicydb, &policydb, sizeof policydb);
  1418. sidtab_set(&oldsidtab, &sidtab);
  1419. write_lock_irq(&policy_rwlock);
  1420. memcpy(&policydb, &newpolicydb, sizeof policydb);
  1421. sidtab_set(&sidtab, &newsidtab);
  1422. security_load_policycaps();
  1423. oldmap = current_mapping;
  1424. current_mapping = map;
  1425. current_mapping_size = map_size;
  1426. seqno = ++latest_granting;
  1427. write_unlock_irq(&policy_rwlock);
  1428. policydb_destroy(&oldpolicydb);
  1429. sidtab_destroy(&oldsidtab);
  1430. kfree(oldmap);
  1431. avc_ss_reset(seqno);
  1432. selnl_notify_policyload(seqno);
  1433. selinux_status_update_policyload(seqno);
  1434. selinux_netlbl_cache_invalidate();
  1435. selinux_xfrm_notify_policyload();
  1436. return 0;
  1437. err:
  1438. kfree(map);
  1439. sidtab_destroy(&newsidtab);
  1440. policydb_destroy(&newpolicydb);
  1441. return rc;
  1442. }
  1443. size_t security_policydb_len(void)
  1444. {
  1445. size_t len;
  1446. read_lock(&policy_rwlock);
  1447. len = policydb.len;
  1448. read_unlock(&policy_rwlock);
  1449. return len;
  1450. }
  1451. int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
  1452. {
  1453. struct ocontext *c;
  1454. int rc = 0;
  1455. read_lock(&policy_rwlock);
  1456. c = policydb.ocontexts[OCON_PORT];
  1457. while (c) {
  1458. if (c->u.port.protocol == protocol &&
  1459. c->u.port.low_port <= port &&
  1460. c->u.port.high_port >= port)
  1461. break;
  1462. c = c->next;
  1463. }
  1464. if (c) {
  1465. if (!c->sid[0]) {
  1466. rc = sidtab_context_to_sid(&sidtab,
  1467. &c->context[0],
  1468. &c->sid[0]);
  1469. if (rc)
  1470. goto out;
  1471. }
  1472. *out_sid = c->sid[0];
  1473. } else {
  1474. *out_sid = SECINITSID_PORT;
  1475. }
  1476. out:
  1477. read_unlock(&policy_rwlock);
  1478. return rc;
  1479. }
  1480. int security_netif_sid(char *name, u32 *if_sid)
  1481. {
  1482. int rc = 0;
  1483. struct ocontext *c;
  1484. read_lock(&policy_rwlock);
  1485. c = policydb.ocontexts[OCON_NETIF];
  1486. while (c) {
  1487. if (strcmp(name, c->u.name) == 0)
  1488. break;
  1489. c = c->next;
  1490. }
  1491. if (c) {
  1492. if (!c->sid[0] || !c->sid[1]) {
  1493. rc = sidtab_context_to_sid(&sidtab,
  1494. &c->context[0],
  1495. &c->sid[0]);
  1496. if (rc)
  1497. goto out;
  1498. rc = sidtab_context_to_sid(&sidtab,
  1499. &c->context[1],
  1500. &c->sid[1]);
  1501. if (rc)
  1502. goto out;
  1503. }
  1504. *if_sid = c->sid[0];
  1505. } else
  1506. *if_sid = SECINITSID_NETIF;
  1507. out:
  1508. read_unlock(&policy_rwlock);
  1509. return rc;
  1510. }
  1511. static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
  1512. {
  1513. int i, fail = 0;
  1514. for (i = 0; i < 4; i++)
  1515. if (addr[i] != (input[i] & mask[i])) {
  1516. fail = 1;
  1517. break;
  1518. }
  1519. return !fail;
  1520. }
  1521. int security_node_sid(u16 domain,
  1522. void *addrp,
  1523. u32 addrlen,
  1524. u32 *out_sid)
  1525. {
  1526. int rc;
  1527. struct ocontext *c;
  1528. read_lock(&policy_rwlock);
  1529. switch (domain) {
  1530. case AF_INET: {
  1531. u32 addr;
  1532. rc = -EINVAL;
  1533. if (addrlen != sizeof(u32))
  1534. goto out;
  1535. addr = *((u32 *)addrp);
  1536. c = policydb.ocontexts[OCON_NODE];
  1537. while (c) {
  1538. if (c->u.node.addr == (addr & c->u.node.mask))
  1539. break;
  1540. c = c->next;
  1541. }
  1542. break;
  1543. }
  1544. case AF_INET6:
  1545. rc = -EINVAL;
  1546. if (addrlen != sizeof(u64) * 2)
  1547. goto out;
  1548. c = policydb.ocontexts[OCON_NODE6];
  1549. while (c) {
  1550. if (match_ipv6_addrmask(addrp, c->u.node6.addr,
  1551. c->u.node6.mask))
  1552. break;
  1553. c = c->next;
  1554. }
  1555. break;
  1556. default:
  1557. rc = 0;
  1558. *out_sid = SECINITSID_NODE;
  1559. goto out;
  1560. }
  1561. if (c) {
  1562. if (!c->sid[0]) {
  1563. rc = sidtab_context_to_sid(&sidtab,
  1564. &c->context[0],
  1565. &c->sid[0]);
  1566. if (rc)
  1567. goto out;
  1568. }
  1569. *out_sid = c->sid[0];
  1570. } else {
  1571. *out_sid = SECINITSID_NODE;
  1572. }
  1573. rc = 0;
  1574. out:
  1575. read_unlock(&policy_rwlock);
  1576. return rc;
  1577. }
  1578. #define SIDS_NEL 25
  1579. int security_get_user_sids(u32 fromsid,
  1580. char *username,
  1581. u32 **sids,
  1582. u32 *nel)
  1583. {
  1584. struct context *fromcon, usercon;
  1585. u32 *mysids = NULL, *mysids2, sid;
  1586. u32 mynel = 0, maxnel = SIDS_NEL;
  1587. struct user_datum *user;
  1588. struct role_datum *role;
  1589. struct ebitmap_node *rnode, *tnode;
  1590. int rc = 0, i, j;
  1591. *sids = NULL;
  1592. *nel = 0;
  1593. if (!ss_initialized)
  1594. goto out;
  1595. read_lock(&policy_rwlock);
  1596. context_init(&usercon);
  1597. rc = -EINVAL;
  1598. fromcon = sidtab_search(&sidtab, fromsid);
  1599. if (!fromcon)
  1600. goto out_unlock;
  1601. rc = -EINVAL;
  1602. user = hashtab_search(policydb.p_users.table, username);
  1603. if (!user)
  1604. goto out_unlock;
  1605. usercon.user = user->value;
  1606. rc = -ENOMEM;
  1607. mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
  1608. if (!mysids)
  1609. goto out_unlock;
  1610. ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
  1611. role = policydb.role_val_to_struct[i];
  1612. usercon.role = i + 1;
  1613. ebitmap_for_each_positive_bit(&role->types, tnode, j) {
  1614. usercon.type = j + 1;
  1615. if (mls_setup_user_range(fromcon, user, &usercon))
  1616. continue;
  1617. rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
  1618. if (rc)
  1619. goto out_unlock;
  1620. if (mynel < maxnel) {
  1621. mysids[mynel++] = sid;
  1622. } else {
  1623. rc = -ENOMEM;
  1624. maxnel += SIDS_NEL;
  1625. mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
  1626. if (!mysids2)
  1627. goto out_unlock;
  1628. memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
  1629. kfree(mysids);
  1630. mysids = mysids2;
  1631. mysids[mynel++] = sid;
  1632. }
  1633. }
  1634. }
  1635. rc = 0;
  1636. out_unlock:
  1637. read_unlock(&policy_rwlock);
  1638. if (rc || !mynel) {
  1639. kfree(mysids);
  1640. goto out;
  1641. }
  1642. rc = -ENOMEM;
  1643. mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
  1644. if (!mysids2) {
  1645. kfree(mysids);
  1646. goto out;
  1647. }
  1648. for (i = 0, j = 0; i < mynel; i++) {
  1649. struct av_decision dummy_avd;
  1650. rc = avc_has_perm_noaudit(fromsid, mysids[i],
  1651. SECCLASS_PROCESS,
  1652. PROCESS__TRANSITION, AVC_STRICT,
  1653. &dummy_avd);
  1654. if (!rc)
  1655. mysids2[j++] = mysids[i];
  1656. cond_resched();
  1657. }
  1658. rc = 0;
  1659. kfree(mysids);
  1660. *sids = mysids2;
  1661. *nel = j;
  1662. out:
  1663. return rc;
  1664. }
  1665. int security_genfs_sid(const char *fstype,
  1666. char *path,
  1667. u16 orig_sclass,
  1668. u32 *sid)
  1669. {
  1670. int len;
  1671. u16 sclass;
  1672. struct genfs *genfs;
  1673. struct ocontext *c;
  1674. int rc, cmp = 0;
  1675. while (path[0] == '/' && path[1] == '/')
  1676. path++;
  1677. read_lock(&policy_rwlock);
  1678. sclass = unmap_class(orig_sclass);
  1679. *sid = SECINITSID_UNLABELED;
  1680. for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
  1681. cmp = strcmp(fstype, genfs->fstype);
  1682. if (cmp <= 0)
  1683. break;
  1684. }
  1685. rc = -ENOENT;
  1686. if (!genfs || cmp)
  1687. goto out;
  1688. for (c = genfs->head; c; c = c->next) {
  1689. len = strlen(c->u.name);
  1690. if ((!c->v.sclass || sclass == c->v.sclass) &&
  1691. (strncmp(c->u.name, path, len) == 0))
  1692. break;
  1693. }
  1694. rc = -ENOENT;
  1695. if (!c)
  1696. goto out;
  1697. if (!c->sid[0]) {
  1698. rc = sidtab_context_to_sid(&sidtab, &c->context[0], &c->sid[0]);
  1699. if (rc)
  1700. goto out;
  1701. }
  1702. *sid = c->sid[0];
  1703. rc = 0;
  1704. out:
  1705. read_unlock(&policy_rwlock);
  1706. return rc;
  1707. }
  1708. int security_fs_use(
  1709. const char *fstype,
  1710. unsigned int *behavior,
  1711. u32 *sid)
  1712. {
  1713. int rc = 0;
  1714. struct ocontext *c;
  1715. read_lock(&policy_rwlock);
  1716. c = policydb.ocontexts[OCON_FSUSE];
  1717. while (c) {
  1718. if (strcmp(fstype, c->u.name) == 0)
  1719. break;
  1720. c = c->next;
  1721. }
  1722. if (c) {
  1723. *behavior = c->v.behavior;
  1724. if (!c->sid[0]) {
  1725. rc = sidtab_context_to_sid(&sidtab, &c->context[0],
  1726. &c->sid[0]);
  1727. if (rc)
  1728. goto out;
  1729. }
  1730. *sid = c->sid[0];
  1731. } else {
  1732. rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
  1733. if (rc) {
  1734. *behavior = SECURITY_FS_USE_NONE;
  1735. rc = 0;
  1736. } else {
  1737. *behavior = SECURITY_FS_USE_GENFS;
  1738. }
  1739. }
  1740. out:
  1741. read_unlock(&policy_rwlock);
  1742. return rc;
  1743. }
  1744. int security_get_bools(int *len, char ***names, int **values)
  1745. {
  1746. int i, rc;
  1747. read_lock(&policy_rwlock);
  1748. *names = NULL;
  1749. *values = NULL;
  1750. rc = 0;
  1751. *len = policydb.p_bools.nprim;
  1752. if (!*len)
  1753. goto out;
  1754. rc = -ENOMEM;
  1755. *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
  1756. if (!*names)
  1757. goto err;
  1758. rc = -ENOMEM;
  1759. *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
  1760. if (!*values)
  1761. goto err;
  1762. for (i = 0; i < *len; i++) {
  1763. size_t name_len;
  1764. (*values)[i] = policydb.bool_val_to_struct[i]->state;
  1765. name_len = strlen(sym_name(&policydb, SYM_BOOLS, i)) + 1;
  1766. rc = -ENOMEM;
  1767. (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
  1768. if (!(*names)[i])
  1769. goto err;
  1770. strncpy((*names)[i], sym_name(&policydb, SYM_BOOLS, i), name_len);
  1771. (*names)[i][name_len - 1] = 0;
  1772. }
  1773. rc = 0;
  1774. out:
  1775. read_unlock(&policy_rwlock);
  1776. return rc;
  1777. err:
  1778. if (*names) {
  1779. for (i = 0; i < *len; i++)
  1780. kfree((*names)[i]);
  1781. }
  1782. kfree(*values);
  1783. goto out;
  1784. }
  1785. int security_set_bools(int len, int *values)
  1786. {
  1787. int i, rc;
  1788. int lenp, seqno = 0;
  1789. struct cond_node *cur;
  1790. write_lock_irq(&policy_rwlock);
  1791. rc = -EFAULT;
  1792. lenp = policydb.p_bools.nprim;
  1793. if (len != lenp)
  1794. goto out;
  1795. for (i = 0; i < len; i++) {
  1796. if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
  1797. audit_log(current->audit_context, GFP_ATOMIC,
  1798. AUDIT_MAC_CONFIG_CHANGE,
  1799. "bool=%s val=%d old_val=%d auid=%u ses=%u",
  1800. sym_name(&policydb, SYM_BOOLS, i),
  1801. !!values[i],
  1802. policydb.bool_val_to_struct[i]->state,
  1803. audit_get_loginuid(current),
  1804. audit_get_sessionid(current));
  1805. }
  1806. if (values[i])
  1807. policydb.bool_val_to_struct[i]->state = 1;
  1808. else
  1809. policydb.bool_val_to_struct[i]->state = 0;
  1810. }
  1811. for (cur = policydb.cond_list; cur; cur = cur->next) {
  1812. rc = evaluate_cond_node(&policydb, cur);
  1813. if (rc)
  1814. goto out;
  1815. }
  1816. seqno = ++latest_granting;
  1817. rc = 0;
  1818. out:
  1819. write_unlock_irq(&policy_rwlock);
  1820. if (!rc) {
  1821. avc_ss_reset(seqno);
  1822. selnl_notify_policyload(seqno);
  1823. selinux_status_update_policyload(seqno);
  1824. selinux_xfrm_notify_policyload();
  1825. }
  1826. return rc;
  1827. }
  1828. int security_get_bool_value(int bool)
  1829. {
  1830. int rc;
  1831. int len;
  1832. read_lock(&policy_rwlock);
  1833. rc = -EFAULT;
  1834. len = policydb.p_bools.nprim;
  1835. if (bool >= len)
  1836. goto out;
  1837. rc = policydb.bool_val_to_struct[bool]->state;
  1838. out:
  1839. read_unlock(&policy_rwlock);
  1840. return rc;
  1841. }
  1842. static int security_preserve_bools(struct policydb *p)
  1843. {
  1844. int rc, nbools = 0, *bvalues = NULL, i;
  1845. char **bnames = NULL;
  1846. struct cond_bool_datum *booldatum;
  1847. struct cond_node *cur;
  1848. rc = security_get_bools(&nbools, &bnames, &bvalues);
  1849. if (rc)
  1850. goto out;
  1851. for (i = 0; i < nbools; i++) {
  1852. booldatum = hashtab_search(p->p_bools.table, bnames[i]);
  1853. if (booldatum)
  1854. booldatum->state = bvalues[i];
  1855. }
  1856. for (cur = p->cond_list; cur; cur = cur->next) {
  1857. rc = evaluate_cond_node(p, cur);
  1858. if (rc)
  1859. goto out;
  1860. }
  1861. out:
  1862. if (bnames) {
  1863. for (i = 0; i < nbools; i++)
  1864. kfree(bnames[i]);
  1865. }
  1866. kfree(bnames);
  1867. kfree(bvalues);
  1868. return rc;
  1869. }
  1870. int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
  1871. {
  1872. struct context *context1;
  1873. struct context *context2;
  1874. struct context newcon;
  1875. char *s;
  1876. u32 len;
  1877. int rc;
  1878. rc = 0;
  1879. if (!ss_initialized || !policydb.mls_enabled) {
  1880. *new_sid = sid;
  1881. goto out;
  1882. }
  1883. context_init(&newcon);
  1884. read_lock(&policy_rwlock);
  1885. rc = -EINVAL;
  1886. context1 = sidtab_search(&sidtab, sid);
  1887. if (!context1) {
  1888. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1889. __func__, sid);
  1890. goto out_unlock;
  1891. }
  1892. rc = -EINVAL;
  1893. context2 = sidtab_search(&sidtab, mls_sid);
  1894. if (!context2) {
  1895. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1896. __func__, mls_sid);
  1897. goto out_unlock;
  1898. }
  1899. newcon.user = context1->user;
  1900. newcon.role = context1->role;
  1901. newcon.type = context1->type;
  1902. rc = mls_context_cpy(&newcon, context2);
  1903. if (rc)
  1904. goto out_unlock;
  1905. if (!policydb_context_isvalid(&policydb, &newcon)) {
  1906. rc = convert_context_handle_invalid_context(&newcon);
  1907. if (rc) {
  1908. if (!context_struct_to_string(&newcon, &s, &len)) {
  1909. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1910. "security_sid_mls_copy: invalid context %s", s);
  1911. kfree(s);
  1912. }
  1913. goto out_unlock;
  1914. }
  1915. }
  1916. rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
  1917. out_unlock:
  1918. read_unlock(&policy_rwlock);
  1919. context_destroy(&newcon);
  1920. out:
  1921. return rc;
  1922. }
  1923. int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
  1924. u32 xfrm_sid,
  1925. u32 *peer_sid)
  1926. {
  1927. int rc;
  1928. struct context *nlbl_ctx;
  1929. struct context *xfrm_ctx;
  1930. *peer_sid = SECSID_NULL;
  1931. if (xfrm_sid == SECSID_NULL) {
  1932. *peer_sid = nlbl_sid;
  1933. return 0;
  1934. }
  1935. if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
  1936. *peer_sid = xfrm_sid;
  1937. return 0;
  1938. }
  1939. if (!policydb.mls_enabled)
  1940. return 0;
  1941. read_lock(&policy_rwlock);
  1942. rc = -EINVAL;
  1943. nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
  1944. if (!nlbl_ctx) {
  1945. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1946. __func__, nlbl_sid);
  1947. goto out;
  1948. }
  1949. rc = -EINVAL;
  1950. xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
  1951. if (!xfrm_ctx) {
  1952. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1953. __func__, xfrm_sid);
  1954. goto out;
  1955. }
  1956. rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
  1957. if (rc)
  1958. goto out;
  1959. *peer_sid = xfrm_sid;
  1960. out:
  1961. read_unlock(&policy_rwlock);
  1962. return rc;
  1963. }
  1964. static int get_classes_callback(void *k, void *d, void *args)
  1965. {
  1966. struct class_datum *datum = d;
  1967. char *name = k, **classes = args;
  1968. int value = datum->value - 1;
  1969. classes[value] = kstrdup(n

Large files files are truncated, but you can click here to view the full file