services.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  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. *
  11. * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
  12. *
  13. * Added conditional policy language extensions
  14. *
  15. * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
  16. * Copyright (C) 2003 - 2004 Tresys Technology, LLC
  17. * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation, version 2.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/string.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/errno.h>
  27. #include <linux/in.h>
  28. #include <linux/sched.h>
  29. #include <linux/audit.h>
  30. #include <asm/semaphore.h>
  31. #include "flask.h"
  32. #include "avc.h"
  33. #include "avc_ss.h"
  34. #include "security.h"
  35. #include "context.h"
  36. #include "policydb.h"
  37. #include "sidtab.h"
  38. #include "services.h"
  39. #include "conditional.h"
  40. #include "mls.h"
  41. extern void selnl_notify_policyload(u32 seqno);
  42. unsigned int policydb_loaded_version;
  43. static DEFINE_RWLOCK(policy_rwlock);
  44. #define POLICY_RDLOCK read_lock(&policy_rwlock)
  45. #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
  46. #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
  47. #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
  48. static DECLARE_MUTEX(load_sem);
  49. #define LOAD_LOCK down(&load_sem)
  50. #define LOAD_UNLOCK up(&load_sem)
  51. static struct sidtab sidtab;
  52. struct policydb policydb;
  53. int ss_initialized = 0;
  54. /*
  55. * The largest sequence number that has been used when
  56. * providing an access decision to the access vector cache.
  57. * The sequence number only changes when a policy change
  58. * occurs.
  59. */
  60. static u32 latest_granting = 0;
  61. /* Forward declaration. */
  62. static int context_struct_to_string(struct context *context, char **scontext,
  63. u32 *scontext_len);
  64. /*
  65. * Return the boolean value of a constraint expression
  66. * when it is applied to the specified source and target
  67. * security contexts.
  68. *
  69. * xcontext is a special beast... It is used by the validatetrans rules
  70. * only. For these rules, scontext is the context before the transition,
  71. * tcontext is the context after the transition, and xcontext is the context
  72. * of the process performing the transition. All other callers of
  73. * constraint_expr_eval should pass in NULL for xcontext.
  74. */
  75. static int constraint_expr_eval(struct context *scontext,
  76. struct context *tcontext,
  77. struct context *xcontext,
  78. struct constraint_expr *cexpr)
  79. {
  80. u32 val1, val2;
  81. struct context *c;
  82. struct role_datum *r1, *r2;
  83. struct mls_level *l1, *l2;
  84. struct constraint_expr *e;
  85. int s[CEXPR_MAXDEPTH];
  86. int sp = -1;
  87. for (e = cexpr; e; e = e->next) {
  88. switch (e->expr_type) {
  89. case CEXPR_NOT:
  90. BUG_ON(sp < 0);
  91. s[sp] = !s[sp];
  92. break;
  93. case CEXPR_AND:
  94. BUG_ON(sp < 1);
  95. sp--;
  96. s[sp] &= s[sp+1];
  97. break;
  98. case CEXPR_OR:
  99. BUG_ON(sp < 1);
  100. sp--;
  101. s[sp] |= s[sp+1];
  102. break;
  103. case CEXPR_ATTR:
  104. if (sp == (CEXPR_MAXDEPTH-1))
  105. return 0;
  106. switch (e->attr) {
  107. case CEXPR_USER:
  108. val1 = scontext->user;
  109. val2 = tcontext->user;
  110. break;
  111. case CEXPR_TYPE:
  112. val1 = scontext->type;
  113. val2 = tcontext->type;
  114. break;
  115. case CEXPR_ROLE:
  116. val1 = scontext->role;
  117. val2 = tcontext->role;
  118. r1 = policydb.role_val_to_struct[val1 - 1];
  119. r2 = policydb.role_val_to_struct[val2 - 1];
  120. switch (e->op) {
  121. case CEXPR_DOM:
  122. s[++sp] = ebitmap_get_bit(&r1->dominates,
  123. val2 - 1);
  124. continue;
  125. case CEXPR_DOMBY:
  126. s[++sp] = ebitmap_get_bit(&r2->dominates,
  127. val1 - 1);
  128. continue;
  129. case CEXPR_INCOMP:
  130. s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
  131. val2 - 1) &&
  132. !ebitmap_get_bit(&r2->dominates,
  133. val1 - 1) );
  134. continue;
  135. default:
  136. break;
  137. }
  138. break;
  139. case CEXPR_L1L2:
  140. l1 = &(scontext->range.level[0]);
  141. l2 = &(tcontext->range.level[0]);
  142. goto mls_ops;
  143. case CEXPR_L1H2:
  144. l1 = &(scontext->range.level[0]);
  145. l2 = &(tcontext->range.level[1]);
  146. goto mls_ops;
  147. case CEXPR_H1L2:
  148. l1 = &(scontext->range.level[1]);
  149. l2 = &(tcontext->range.level[0]);
  150. goto mls_ops;
  151. case CEXPR_H1H2:
  152. l1 = &(scontext->range.level[1]);
  153. l2 = &(tcontext->range.level[1]);
  154. goto mls_ops;
  155. case CEXPR_L1H1:
  156. l1 = &(scontext->range.level[0]);
  157. l2 = &(scontext->range.level[1]);
  158. goto mls_ops;
  159. case CEXPR_L2H2:
  160. l1 = &(tcontext->range.level[0]);
  161. l2 = &(tcontext->range.level[1]);
  162. goto mls_ops;
  163. mls_ops:
  164. switch (e->op) {
  165. case CEXPR_EQ:
  166. s[++sp] = mls_level_eq(l1, l2);
  167. continue;
  168. case CEXPR_NEQ:
  169. s[++sp] = !mls_level_eq(l1, l2);
  170. continue;
  171. case CEXPR_DOM:
  172. s[++sp] = mls_level_dom(l1, l2);
  173. continue;
  174. case CEXPR_DOMBY:
  175. s[++sp] = mls_level_dom(l2, l1);
  176. continue;
  177. case CEXPR_INCOMP:
  178. s[++sp] = mls_level_incomp(l2, l1);
  179. continue;
  180. default:
  181. BUG();
  182. return 0;
  183. }
  184. break;
  185. default:
  186. BUG();
  187. return 0;
  188. }
  189. switch (e->op) {
  190. case CEXPR_EQ:
  191. s[++sp] = (val1 == val2);
  192. break;
  193. case CEXPR_NEQ:
  194. s[++sp] = (val1 != val2);
  195. break;
  196. default:
  197. BUG();
  198. return 0;
  199. }
  200. break;
  201. case CEXPR_NAMES:
  202. if (sp == (CEXPR_MAXDEPTH-1))
  203. return 0;
  204. c = scontext;
  205. if (e->attr & CEXPR_TARGET)
  206. c = tcontext;
  207. else if (e->attr & CEXPR_XTARGET) {
  208. c = xcontext;
  209. if (!c) {
  210. BUG();
  211. return 0;
  212. }
  213. }
  214. if (e->attr & CEXPR_USER)
  215. val1 = c->user;
  216. else if (e->attr & CEXPR_ROLE)
  217. val1 = c->role;
  218. else if (e->attr & CEXPR_TYPE)
  219. val1 = c->type;
  220. else {
  221. BUG();
  222. return 0;
  223. }
  224. switch (e->op) {
  225. case CEXPR_EQ:
  226. s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
  227. break;
  228. case CEXPR_NEQ:
  229. s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
  230. break;
  231. default:
  232. BUG();
  233. return 0;
  234. }
  235. break;
  236. default:
  237. BUG();
  238. return 0;
  239. }
  240. }
  241. BUG_ON(sp != 0);
  242. return s[0];
  243. }
  244. /*
  245. * Compute access vectors based on a context structure pair for
  246. * the permissions in a particular class.
  247. */
  248. static int context_struct_compute_av(struct context *scontext,
  249. struct context *tcontext,
  250. u16 tclass,
  251. u32 requested,
  252. struct av_decision *avd)
  253. {
  254. struct constraint_node *constraint;
  255. struct role_allow *ra;
  256. struct avtab_key avkey;
  257. struct avtab_node *node;
  258. struct class_datum *tclass_datum;
  259. struct ebitmap *sattr, *tattr;
  260. struct ebitmap_node *snode, *tnode;
  261. unsigned int i, j;
  262. /*
  263. * Remap extended Netlink classes for old policy versions.
  264. * Do this here rather than socket_type_to_security_class()
  265. * in case a newer policy version is loaded, allowing sockets
  266. * to remain in the correct class.
  267. */
  268. if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
  269. if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
  270. tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
  271. tclass = SECCLASS_NETLINK_SOCKET;
  272. if (!tclass || tclass > policydb.p_classes.nprim) {
  273. printk(KERN_ERR "security_compute_av: unrecognized class %d\n",
  274. tclass);
  275. return -EINVAL;
  276. }
  277. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  278. /*
  279. * Initialize the access vectors to the default values.
  280. */
  281. avd->allowed = 0;
  282. avd->decided = 0xffffffff;
  283. avd->auditallow = 0;
  284. avd->auditdeny = 0xffffffff;
  285. avd->seqno = latest_granting;
  286. /*
  287. * If a specific type enforcement rule was defined for
  288. * this permission check, then use it.
  289. */
  290. avkey.target_class = tclass;
  291. avkey.specified = AVTAB_AV;
  292. sattr = &policydb.type_attr_map[scontext->type - 1];
  293. tattr = &policydb.type_attr_map[tcontext->type - 1];
  294. ebitmap_for_each_bit(sattr, snode, i) {
  295. if (!ebitmap_node_get_bit(snode, i))
  296. continue;
  297. ebitmap_for_each_bit(tattr, tnode, j) {
  298. if (!ebitmap_node_get_bit(tnode, j))
  299. continue;
  300. avkey.source_type = i + 1;
  301. avkey.target_type = j + 1;
  302. for (node = avtab_search_node(&policydb.te_avtab, &avkey);
  303. node != NULL;
  304. node = avtab_search_node_next(node, avkey.specified)) {
  305. if (node->key.specified == AVTAB_ALLOWED)
  306. avd->allowed |= node->datum.data;
  307. else if (node->key.specified == AVTAB_AUDITALLOW)
  308. avd->auditallow |= node->datum.data;
  309. else if (node->key.specified == AVTAB_AUDITDENY)
  310. avd->auditdeny &= node->datum.data;
  311. }
  312. /* Check conditional av table for additional permissions */
  313. cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
  314. }
  315. }
  316. /*
  317. * Remove any permissions prohibited by a constraint (this includes
  318. * the MLS policy).
  319. */
  320. constraint = tclass_datum->constraints;
  321. while (constraint) {
  322. if ((constraint->permissions & (avd->allowed)) &&
  323. !constraint_expr_eval(scontext, tcontext, NULL,
  324. constraint->expr)) {
  325. avd->allowed = (avd->allowed) & ~(constraint->permissions);
  326. }
  327. constraint = constraint->next;
  328. }
  329. /*
  330. * If checking process transition permission and the
  331. * role is changing, then check the (current_role, new_role)
  332. * pair.
  333. */
  334. if (tclass == SECCLASS_PROCESS &&
  335. (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
  336. scontext->role != tcontext->role) {
  337. for (ra = policydb.role_allow; ra; ra = ra->next) {
  338. if (scontext->role == ra->role &&
  339. tcontext->role == ra->new_role)
  340. break;
  341. }
  342. if (!ra)
  343. avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
  344. PROCESS__DYNTRANSITION);
  345. }
  346. return 0;
  347. }
  348. static int security_validtrans_handle_fail(struct context *ocontext,
  349. struct context *ncontext,
  350. struct context *tcontext,
  351. u16 tclass)
  352. {
  353. char *o = NULL, *n = NULL, *t = NULL;
  354. u32 olen, nlen, tlen;
  355. if (context_struct_to_string(ocontext, &o, &olen) < 0)
  356. goto out;
  357. if (context_struct_to_string(ncontext, &n, &nlen) < 0)
  358. goto out;
  359. if (context_struct_to_string(tcontext, &t, &tlen) < 0)
  360. goto out;
  361. audit_log(current->audit_context, AUDIT_SELINUX_ERR,
  362. "security_validate_transition: denied for"
  363. " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
  364. o, n, t, policydb.p_class_val_to_name[tclass-1]);
  365. out:
  366. kfree(o);
  367. kfree(n);
  368. kfree(t);
  369. if (!selinux_enforcing)
  370. return 0;
  371. return -EPERM;
  372. }
  373. int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
  374. u16 tclass)
  375. {
  376. struct context *ocontext;
  377. struct context *ncontext;
  378. struct context *tcontext;
  379. struct class_datum *tclass_datum;
  380. struct constraint_node *constraint;
  381. int rc = 0;
  382. if (!ss_initialized)
  383. return 0;
  384. POLICY_RDLOCK;
  385. /*
  386. * Remap extended Netlink classes for old policy versions.
  387. * Do this here rather than socket_type_to_security_class()
  388. * in case a newer policy version is loaded, allowing sockets
  389. * to remain in the correct class.
  390. */
  391. if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
  392. if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
  393. tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
  394. tclass = SECCLASS_NETLINK_SOCKET;
  395. if (!tclass || tclass > policydb.p_classes.nprim) {
  396. printk(KERN_ERR "security_validate_transition: "
  397. "unrecognized class %d\n", tclass);
  398. rc = -EINVAL;
  399. goto out;
  400. }
  401. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  402. ocontext = sidtab_search(&sidtab, oldsid);
  403. if (!ocontext) {
  404. printk(KERN_ERR "security_validate_transition: "
  405. " unrecognized SID %d\n", oldsid);
  406. rc = -EINVAL;
  407. goto out;
  408. }
  409. ncontext = sidtab_search(&sidtab, newsid);
  410. if (!ncontext) {
  411. printk(KERN_ERR "security_validate_transition: "
  412. " unrecognized SID %d\n", newsid);
  413. rc = -EINVAL;
  414. goto out;
  415. }
  416. tcontext = sidtab_search(&sidtab, tasksid);
  417. if (!tcontext) {
  418. printk(KERN_ERR "security_validate_transition: "
  419. " unrecognized SID %d\n", tasksid);
  420. rc = -EINVAL;
  421. goto out;
  422. }
  423. constraint = tclass_datum->validatetrans;
  424. while (constraint) {
  425. if (!constraint_expr_eval(ocontext, ncontext, tcontext,
  426. constraint->expr)) {
  427. rc = security_validtrans_handle_fail(ocontext, ncontext,
  428. tcontext, tclass);
  429. goto out;
  430. }
  431. constraint = constraint->next;
  432. }
  433. out:
  434. POLICY_RDUNLOCK;
  435. return rc;
  436. }
  437. /**
  438. * security_compute_av - Compute access vector decisions.
  439. * @ssid: source security identifier
  440. * @tsid: target security identifier
  441. * @tclass: target security class
  442. * @requested: requested permissions
  443. * @avd: access vector decisions
  444. *
  445. * Compute a set of access vector decisions based on the
  446. * SID pair (@ssid, @tsid) for the permissions in @tclass.
  447. * Return -%EINVAL if any of the parameters are invalid or %0
  448. * if the access vector decisions were computed successfully.
  449. */
  450. int security_compute_av(u32 ssid,
  451. u32 tsid,
  452. u16 tclass,
  453. u32 requested,
  454. struct av_decision *avd)
  455. {
  456. struct context *scontext = NULL, *tcontext = NULL;
  457. int rc = 0;
  458. if (!ss_initialized) {
  459. avd->allowed = 0xffffffff;
  460. avd->decided = 0xffffffff;
  461. avd->auditallow = 0;
  462. avd->auditdeny = 0xffffffff;
  463. avd->seqno = latest_granting;
  464. return 0;
  465. }
  466. POLICY_RDLOCK;
  467. scontext = sidtab_search(&sidtab, ssid);
  468. if (!scontext) {
  469. printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
  470. ssid);
  471. rc = -EINVAL;
  472. goto out;
  473. }
  474. tcontext = sidtab_search(&sidtab, tsid);
  475. if (!tcontext) {
  476. printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
  477. tsid);
  478. rc = -EINVAL;
  479. goto out;
  480. }
  481. rc = context_struct_compute_av(scontext, tcontext, tclass,
  482. requested, avd);
  483. out:
  484. POLICY_RDUNLOCK;
  485. return rc;
  486. }
  487. /*
  488. * Write the security context string representation of
  489. * the context structure `context' into a dynamically
  490. * allocated string of the correct size. Set `*scontext'
  491. * to point to this string and set `*scontext_len' to
  492. * the length of the string.
  493. */
  494. static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
  495. {
  496. char *scontextp;
  497. *scontext = NULL;
  498. *scontext_len = 0;
  499. /* Compute the size of the context. */
  500. *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
  501. *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
  502. *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
  503. *scontext_len += mls_compute_context_len(context);
  504. /* Allocate space for the context; caller must free this space. */
  505. scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
  506. if (!scontextp) {
  507. return -ENOMEM;
  508. }
  509. *scontext = scontextp;
  510. /*
  511. * Copy the user name, role name and type name into the context.
  512. */
  513. sprintf(scontextp, "%s:%s:%s",
  514. policydb.p_user_val_to_name[context->user - 1],
  515. policydb.p_role_val_to_name[context->role - 1],
  516. policydb.p_type_val_to_name[context->type - 1]);
  517. scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
  518. 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
  519. 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
  520. mls_sid_to_context(context, &scontextp);
  521. *scontextp = 0;
  522. return 0;
  523. }
  524. #include "initial_sid_to_string.h"
  525. /**
  526. * security_sid_to_context - Obtain a context for a given SID.
  527. * @sid: security identifier, SID
  528. * @scontext: security context
  529. * @scontext_len: length in bytes
  530. *
  531. * Write the string representation of the context associated with @sid
  532. * into a dynamically allocated string of the correct size. Set @scontext
  533. * to point to this string and set @scontext_len to the length of the string.
  534. */
  535. int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
  536. {
  537. struct context *context;
  538. int rc = 0;
  539. if (!ss_initialized) {
  540. if (sid <= SECINITSID_NUM) {
  541. char *scontextp;
  542. *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
  543. scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
  544. strcpy(scontextp, initial_sid_to_string[sid]);
  545. *scontext = scontextp;
  546. goto out;
  547. }
  548. printk(KERN_ERR "security_sid_to_context: called before initial "
  549. "load_policy on unknown SID %d\n", sid);
  550. rc = -EINVAL;
  551. goto out;
  552. }
  553. POLICY_RDLOCK;
  554. context = sidtab_search(&sidtab, sid);
  555. if (!context) {
  556. printk(KERN_ERR "security_sid_to_context: unrecognized SID "
  557. "%d\n", sid);
  558. rc = -EINVAL;
  559. goto out_unlock;
  560. }
  561. rc = context_struct_to_string(context, scontext, scontext_len);
  562. out_unlock:
  563. POLICY_RDUNLOCK;
  564. out:
  565. return rc;
  566. }
  567. static int security_context_to_sid_core(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
  568. {
  569. char *scontext2;
  570. struct context context;
  571. struct role_datum *role;
  572. struct type_datum *typdatum;
  573. struct user_datum *usrdatum;
  574. char *scontextp, *p, oldc;
  575. int rc = 0;
  576. if (!ss_initialized) {
  577. int i;
  578. for (i = 1; i < SECINITSID_NUM; i++) {
  579. if (!strcmp(initial_sid_to_string[i], scontext)) {
  580. *sid = i;
  581. goto out;
  582. }
  583. }
  584. *sid = SECINITSID_KERNEL;
  585. goto out;
  586. }
  587. *sid = SECSID_NULL;
  588. /* Copy the string so that we can modify the copy as we parse it.
  589. The string should already by null terminated, but we append a
  590. null suffix to the copy to avoid problems with the existing
  591. attr package, which doesn't view the null terminator as part
  592. of the attribute value. */
  593. scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
  594. if (!scontext2) {
  595. rc = -ENOMEM;
  596. goto out;
  597. }
  598. memcpy(scontext2, scontext, scontext_len);
  599. scontext2[scontext_len] = 0;
  600. context_init(&context);
  601. *sid = SECSID_NULL;
  602. POLICY_RDLOCK;
  603. /* Parse the security context. */
  604. rc = -EINVAL;
  605. scontextp = (char *) scontext2;
  606. /* Extract the user. */
  607. p = scontextp;
  608. while (*p && *p != ':')
  609. p++;
  610. if (*p == 0)
  611. goto out_unlock;
  612. *p++ = 0;
  613. usrdatum = hashtab_search(policydb.p_users.table, scontextp);
  614. if (!usrdatum)
  615. goto out_unlock;
  616. context.user = usrdatum->value;
  617. /* Extract role. */
  618. scontextp = p;
  619. while (*p && *p != ':')
  620. p++;
  621. if (*p == 0)
  622. goto out_unlock;
  623. *p++ = 0;
  624. role = hashtab_search(policydb.p_roles.table, scontextp);
  625. if (!role)
  626. goto out_unlock;
  627. context.role = role->value;
  628. /* Extract type. */
  629. scontextp = p;
  630. while (*p && *p != ':')
  631. p++;
  632. oldc = *p;
  633. *p++ = 0;
  634. typdatum = hashtab_search(policydb.p_types.table, scontextp);
  635. if (!typdatum)
  636. goto out_unlock;
  637. context.type = typdatum->value;
  638. rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
  639. if (rc)
  640. goto out_unlock;
  641. if ((p - scontext2) < scontext_len) {
  642. rc = -EINVAL;
  643. goto out_unlock;
  644. }
  645. /* Check the validity of the new context. */
  646. if (!policydb_context_isvalid(&policydb, &context)) {
  647. rc = -EINVAL;
  648. goto out_unlock;
  649. }
  650. /* Obtain the new sid. */
  651. rc = sidtab_context_to_sid(&sidtab, &context, sid);
  652. out_unlock:
  653. POLICY_RDUNLOCK;
  654. context_destroy(&context);
  655. kfree(scontext2);
  656. out:
  657. return rc;
  658. }
  659. /**
  660. * security_context_to_sid - Obtain a SID for a given security context.
  661. * @scontext: security context
  662. * @scontext_len: length in bytes
  663. * @sid: security identifier, SID
  664. *
  665. * Obtains a SID associated with the security context that
  666. * has the string representation specified by @scontext.
  667. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  668. * memory is available, or 0 on success.
  669. */
  670. int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
  671. {
  672. return security_context_to_sid_core(scontext, scontext_len,
  673. sid, SECSID_NULL);
  674. }
  675. /**
  676. * security_context_to_sid_default - Obtain a SID for a given security context,
  677. * falling back to specified default if needed.
  678. *
  679. * @scontext: security context
  680. * @scontext_len: length in bytes
  681. * @sid: security identifier, SID
  682. * @def_sid: default SID to assign on errror
  683. *
  684. * Obtains a SID associated with the security context that
  685. * has the string representation specified by @scontext.
  686. * The default SID is passed to the MLS layer to be used to allow
  687. * kernel labeling of the MLS field if the MLS field is not present
  688. * (for upgrading to MLS without full relabel).
  689. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  690. * memory is available, or 0 on success.
  691. */
  692. int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
  693. {
  694. return security_context_to_sid_core(scontext, scontext_len,
  695. sid, def_sid);
  696. }
  697. static int compute_sid_handle_invalid_context(
  698. struct context *scontext,
  699. struct context *tcontext,
  700. u16 tclass,
  701. struct context *newcontext)
  702. {
  703. char *s = NULL, *t = NULL, *n = NULL;
  704. u32 slen, tlen, nlen;
  705. if (context_struct_to_string(scontext, &s, &slen) < 0)
  706. goto out;
  707. if (context_struct_to_string(tcontext, &t, &tlen) < 0)
  708. goto out;
  709. if (context_struct_to_string(newcontext, &n, &nlen) < 0)
  710. goto out;
  711. audit_log(current->audit_context, AUDIT_SELINUX_ERR,
  712. "security_compute_sid: invalid context %s"
  713. " for scontext=%s"
  714. " tcontext=%s"
  715. " tclass=%s",
  716. n, s, t, policydb.p_class_val_to_name[tclass-1]);
  717. out:
  718. kfree(s);
  719. kfree(t);
  720. kfree(n);
  721. if (!selinux_enforcing)
  722. return 0;
  723. return -EACCES;
  724. }
  725. static int security_compute_sid(u32 ssid,
  726. u32 tsid,
  727. u16 tclass,
  728. u32 specified,
  729. u32 *out_sid)
  730. {
  731. struct context *scontext = NULL, *tcontext = NULL, newcontext;
  732. struct role_trans *roletr = NULL;
  733. struct avtab_key avkey;
  734. struct avtab_datum *avdatum;
  735. struct avtab_node *node;
  736. int rc = 0;
  737. if (!ss_initialized) {
  738. switch (tclass) {
  739. case SECCLASS_PROCESS:
  740. *out_sid = ssid;
  741. break;
  742. default:
  743. *out_sid = tsid;
  744. break;
  745. }
  746. goto out;
  747. }
  748. POLICY_RDLOCK;
  749. scontext = sidtab_search(&sidtab, ssid);
  750. if (!scontext) {
  751. printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
  752. ssid);
  753. rc = -EINVAL;
  754. goto out_unlock;
  755. }
  756. tcontext = sidtab_search(&sidtab, tsid);
  757. if (!tcontext) {
  758. printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
  759. tsid);
  760. rc = -EINVAL;
  761. goto out_unlock;
  762. }
  763. context_init(&newcontext);
  764. /* Set the user identity. */
  765. switch (specified) {
  766. case AVTAB_TRANSITION:
  767. case AVTAB_CHANGE:
  768. /* Use the process user identity. */
  769. newcontext.user = scontext->user;
  770. break;
  771. case AVTAB_MEMBER:
  772. /* Use the related object owner. */
  773. newcontext.user = tcontext->user;
  774. break;
  775. }
  776. /* Set the role and type to default values. */
  777. switch (tclass) {
  778. case SECCLASS_PROCESS:
  779. /* Use the current role and type of process. */
  780. newcontext.role = scontext->role;
  781. newcontext.type = scontext->type;
  782. break;
  783. default:
  784. /* Use the well-defined object role. */
  785. newcontext.role = OBJECT_R_VAL;
  786. /* Use the type of the related object. */
  787. newcontext.type = tcontext->type;
  788. }
  789. /* Look for a type transition/member/change rule. */
  790. avkey.source_type = scontext->type;
  791. avkey.target_type = tcontext->type;
  792. avkey.target_class = tclass;
  793. avkey.specified = specified;
  794. avdatum = avtab_search(&policydb.te_avtab, &avkey);
  795. /* If no permanent rule, also check for enabled conditional rules */
  796. if(!avdatum) {
  797. node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
  798. for (; node != NULL; node = avtab_search_node_next(node, specified)) {
  799. if (node->key.specified & AVTAB_ENABLED) {
  800. avdatum = &node->datum;
  801. break;
  802. }
  803. }
  804. }
  805. if (avdatum) {
  806. /* Use the type from the type transition/member/change rule. */
  807. newcontext.type = avdatum->data;
  808. }
  809. /* Check for class-specific changes. */
  810. switch (tclass) {
  811. case SECCLASS_PROCESS:
  812. if (specified & AVTAB_TRANSITION) {
  813. /* Look for a role transition rule. */
  814. for (roletr = policydb.role_tr; roletr;
  815. roletr = roletr->next) {
  816. if (roletr->role == scontext->role &&
  817. roletr->type == tcontext->type) {
  818. /* Use the role transition rule. */
  819. newcontext.role = roletr->new_role;
  820. break;
  821. }
  822. }
  823. }
  824. break;
  825. default:
  826. break;
  827. }
  828. /* Set the MLS attributes.
  829. This is done last because it may allocate memory. */
  830. rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
  831. if (rc)
  832. goto out_unlock;
  833. /* Check the validity of the context. */
  834. if (!policydb_context_isvalid(&policydb, &newcontext)) {
  835. rc = compute_sid_handle_invalid_context(scontext,
  836. tcontext,
  837. tclass,
  838. &newcontext);
  839. if (rc)
  840. goto out_unlock;
  841. }
  842. /* Obtain the sid for the context. */
  843. rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
  844. out_unlock:
  845. POLICY_RDUNLOCK;
  846. context_destroy(&newcontext);
  847. out:
  848. return rc;
  849. }
  850. /**
  851. * security_transition_sid - Compute the SID for a new subject/object.
  852. * @ssid: source security identifier
  853. * @tsid: target security identifier
  854. * @tclass: target security class
  855. * @out_sid: security identifier for new subject/object
  856. *
  857. * Compute a SID to use for labeling a new subject or object in the
  858. * class @tclass based on a SID pair (@ssid, @tsid).
  859. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  860. * if insufficient memory is available, or %0 if the new SID was
  861. * computed successfully.
  862. */
  863. int security_transition_sid(u32 ssid,
  864. u32 tsid,
  865. u16 tclass,
  866. u32 *out_sid)
  867. {
  868. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
  869. }
  870. /**
  871. * security_member_sid - Compute the SID for member selection.
  872. * @ssid: source security identifier
  873. * @tsid: target security identifier
  874. * @tclass: target security class
  875. * @out_sid: security identifier for selected member
  876. *
  877. * Compute a SID to use when selecting a member of a polyinstantiated
  878. * object of class @tclass based on a SID pair (@ssid, @tsid).
  879. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  880. * if insufficient memory is available, or %0 if the SID was
  881. * computed successfully.
  882. */
  883. int security_member_sid(u32 ssid,
  884. u32 tsid,
  885. u16 tclass,
  886. u32 *out_sid)
  887. {
  888. return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
  889. }
  890. /**
  891. * security_change_sid - Compute the SID for object relabeling.
  892. * @ssid: source security identifier
  893. * @tsid: target security identifier
  894. * @tclass: target security class
  895. * @out_sid: security identifier for selected member
  896. *
  897. * Compute a SID to use for relabeling an object of class @tclass
  898. * based on a SID pair (@ssid, @tsid).
  899. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  900. * if insufficient memory is available, or %0 if the SID was
  901. * computed successfully.
  902. */
  903. int security_change_sid(u32 ssid,
  904. u32 tsid,
  905. u16 tclass,
  906. u32 *out_sid)
  907. {
  908. return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
  909. }
  910. /*
  911. * Verify that each permission that is defined under the
  912. * existing policy is still defined with the same value
  913. * in the new policy.
  914. */
  915. static int validate_perm(void *key, void *datum, void *p)
  916. {
  917. struct hashtab *h;
  918. struct perm_datum *perdatum, *perdatum2;
  919. int rc = 0;
  920. h = p;
  921. perdatum = datum;
  922. perdatum2 = hashtab_search(h, key);
  923. if (!perdatum2) {
  924. printk(KERN_ERR "security: permission %s disappeared",
  925. (char *)key);
  926. rc = -ENOENT;
  927. goto out;
  928. }
  929. if (perdatum->value != perdatum2->value) {
  930. printk(KERN_ERR "security: the value of permission %s changed",
  931. (char *)key);
  932. rc = -EINVAL;
  933. }
  934. out:
  935. return rc;
  936. }
  937. /*
  938. * Verify that each class that is defined under the
  939. * existing policy is still defined with the same
  940. * attributes in the new policy.
  941. */
  942. static int validate_class(void *key, void *datum, void *p)
  943. {
  944. struct policydb *newp;
  945. struct class_datum *cladatum, *cladatum2;
  946. int rc;
  947. newp = p;
  948. cladatum = datum;
  949. cladatum2 = hashtab_search(newp->p_classes.table, key);
  950. if (!cladatum2) {
  951. printk(KERN_ERR "security: class %s disappeared\n",
  952. (char *)key);
  953. rc = -ENOENT;
  954. goto out;
  955. }
  956. if (cladatum->value != cladatum2->value) {
  957. printk(KERN_ERR "security: the value of class %s changed\n",
  958. (char *)key);
  959. rc = -EINVAL;
  960. goto out;
  961. }
  962. if ((cladatum->comdatum && !cladatum2->comdatum) ||
  963. (!cladatum->comdatum && cladatum2->comdatum)) {
  964. printk(KERN_ERR "security: the inherits clause for the access "
  965. "vector definition for class %s changed\n", (char *)key);
  966. rc = -EINVAL;
  967. goto out;
  968. }
  969. if (cladatum->comdatum) {
  970. rc = hashtab_map(cladatum->comdatum->permissions.table, validate_perm,
  971. cladatum2->comdatum->permissions.table);
  972. if (rc) {
  973. printk(" in the access vector definition for class "
  974. "%s\n", (char *)key);
  975. goto out;
  976. }
  977. }
  978. rc = hashtab_map(cladatum->permissions.table, validate_perm,
  979. cladatum2->permissions.table);
  980. if (rc)
  981. printk(" in access vector definition for class %s\n",
  982. (char *)key);
  983. out:
  984. return rc;
  985. }
  986. /* Clone the SID into the new SID table. */
  987. static int clone_sid(u32 sid,
  988. struct context *context,
  989. void *arg)
  990. {
  991. struct sidtab *s = arg;
  992. return sidtab_insert(s, sid, context);
  993. }
  994. static inline int convert_context_handle_invalid_context(struct context *context)
  995. {
  996. int rc = 0;
  997. if (selinux_enforcing) {
  998. rc = -EINVAL;
  999. } else {
  1000. char *s;
  1001. u32 len;
  1002. context_struct_to_string(context, &s, &len);
  1003. printk(KERN_ERR "security: context %s is invalid\n", s);
  1004. kfree(s);
  1005. }
  1006. return rc;
  1007. }
  1008. struct convert_context_args {
  1009. struct policydb *oldp;
  1010. struct policydb *newp;
  1011. };
  1012. /*
  1013. * Convert the values in the security context
  1014. * structure `c' from the values specified
  1015. * in the policy `p->oldp' to the values specified
  1016. * in the policy `p->newp'. Verify that the
  1017. * context is valid under the new policy.
  1018. */
  1019. static int convert_context(u32 key,
  1020. struct context *c,
  1021. void *p)
  1022. {
  1023. struct convert_context_args *args;
  1024. struct context oldc;
  1025. struct role_datum *role;
  1026. struct type_datum *typdatum;
  1027. struct user_datum *usrdatum;
  1028. char *s;
  1029. u32 len;
  1030. int rc;
  1031. args = p;
  1032. rc = context_cpy(&oldc, c);
  1033. if (rc)
  1034. goto out;
  1035. rc = -EINVAL;
  1036. /* Convert the user. */
  1037. usrdatum = hashtab_search(args->newp->p_users.table,
  1038. args->oldp->p_user_val_to_name[c->user - 1]);
  1039. if (!usrdatum) {
  1040. goto bad;
  1041. }
  1042. c->user = usrdatum->value;
  1043. /* Convert the role. */
  1044. role = hashtab_search(args->newp->p_roles.table,
  1045. args->oldp->p_role_val_to_name[c->role - 1]);
  1046. if (!role) {
  1047. goto bad;
  1048. }
  1049. c->role = role->value;
  1050. /* Convert the type. */
  1051. typdatum = hashtab_search(args->newp->p_types.table,
  1052. args->oldp->p_type_val_to_name[c->type - 1]);
  1053. if (!typdatum) {
  1054. goto bad;
  1055. }
  1056. c->type = typdatum->value;
  1057. rc = mls_convert_context(args->oldp, args->newp, c);
  1058. if (rc)
  1059. goto bad;
  1060. /* Check the validity of the new context. */
  1061. if (!policydb_context_isvalid(args->newp, c)) {
  1062. rc = convert_context_handle_invalid_context(&oldc);
  1063. if (rc)
  1064. goto bad;
  1065. }
  1066. context_destroy(&oldc);
  1067. out:
  1068. return rc;
  1069. bad:
  1070. context_struct_to_string(&oldc, &s, &len);
  1071. context_destroy(&oldc);
  1072. printk(KERN_ERR "security: invalidating context %s\n", s);
  1073. kfree(s);
  1074. goto out;
  1075. }
  1076. extern void selinux_complete_init(void);
  1077. /**
  1078. * security_load_policy - Load a security policy configuration.
  1079. * @data: binary policy data
  1080. * @len: length of data in bytes
  1081. *
  1082. * Load a new set of security policy configuration data,
  1083. * validate it and convert the SID table as necessary.
  1084. * This function will flush the access vector cache after
  1085. * loading the new policy.
  1086. */
  1087. int security_load_policy(void *data, size_t len)
  1088. {
  1089. struct policydb oldpolicydb, newpolicydb;
  1090. struct sidtab oldsidtab, newsidtab;
  1091. struct convert_context_args args;
  1092. u32 seqno;
  1093. int rc = 0;
  1094. struct policy_file file = { data, len }, *fp = &file;
  1095. LOAD_LOCK;
  1096. if (!ss_initialized) {
  1097. avtab_cache_init();
  1098. if (policydb_read(&policydb, fp)) {
  1099. LOAD_UNLOCK;
  1100. avtab_cache_destroy();
  1101. return -EINVAL;
  1102. }
  1103. if (policydb_load_isids(&policydb, &sidtab)) {
  1104. LOAD_UNLOCK;
  1105. policydb_destroy(&policydb);
  1106. avtab_cache_destroy();
  1107. return -EINVAL;
  1108. }
  1109. policydb_loaded_version = policydb.policyvers;
  1110. ss_initialized = 1;
  1111. seqno = ++latest_granting;
  1112. LOAD_UNLOCK;
  1113. selinux_complete_init();
  1114. avc_ss_reset(seqno);
  1115. selnl_notify_policyload(seqno);
  1116. return 0;
  1117. }
  1118. #if 0
  1119. sidtab_hash_eval(&sidtab, "sids");
  1120. #endif
  1121. if (policydb_read(&newpolicydb, fp)) {
  1122. LOAD_UNLOCK;
  1123. return -EINVAL;
  1124. }
  1125. sidtab_init(&newsidtab);
  1126. /* Verify that the existing classes did not change. */
  1127. if (hashtab_map(policydb.p_classes.table, validate_class, &newpolicydb)) {
  1128. printk(KERN_ERR "security: the definition of an existing "
  1129. "class changed\n");
  1130. rc = -EINVAL;
  1131. goto err;
  1132. }
  1133. /* Clone the SID table. */
  1134. sidtab_shutdown(&sidtab);
  1135. if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
  1136. rc = -ENOMEM;
  1137. goto err;
  1138. }
  1139. /* Convert the internal representations of contexts
  1140. in the new SID table and remove invalid SIDs. */
  1141. args.oldp = &policydb;
  1142. args.newp = &newpolicydb;
  1143. sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
  1144. /* Save the old policydb and SID table to free later. */
  1145. memcpy(&oldpolicydb, &policydb, sizeof policydb);
  1146. sidtab_set(&oldsidtab, &sidtab);
  1147. /* Install the new policydb and SID table. */
  1148. POLICY_WRLOCK;
  1149. memcpy(&policydb, &newpolicydb, sizeof policydb);
  1150. sidtab_set(&sidtab, &newsidtab);
  1151. seqno = ++latest_granting;
  1152. policydb_loaded_version = policydb.policyvers;
  1153. POLICY_WRUNLOCK;
  1154. LOAD_UNLOCK;
  1155. /* Free the old policydb and SID table. */
  1156. policydb_destroy(&oldpolicydb);
  1157. sidtab_destroy(&oldsidtab);
  1158. avc_ss_reset(seqno);
  1159. selnl_notify_policyload(seqno);
  1160. return 0;
  1161. err:
  1162. LOAD_UNLOCK;
  1163. sidtab_destroy(&newsidtab);
  1164. policydb_destroy(&newpolicydb);
  1165. return rc;
  1166. }
  1167. /**
  1168. * security_port_sid - Obtain the SID for a port.
  1169. * @domain: communication domain aka address family
  1170. * @type: socket type
  1171. * @protocol: protocol number
  1172. * @port: port number
  1173. * @out_sid: security identifier
  1174. */
  1175. int security_port_sid(u16 domain,
  1176. u16 type,
  1177. u8 protocol,
  1178. u16 port,
  1179. u32 *out_sid)
  1180. {
  1181. struct ocontext *c;
  1182. int rc = 0;
  1183. POLICY_RDLOCK;
  1184. c = policydb.ocontexts[OCON_PORT];
  1185. while (c) {
  1186. if (c->u.port.protocol == protocol &&
  1187. c->u.port.low_port <= port &&
  1188. c->u.port.high_port >= port)
  1189. break;
  1190. c = c->next;
  1191. }
  1192. if (c) {
  1193. if (!c->sid[0]) {
  1194. rc = sidtab_context_to_sid(&sidtab,
  1195. &c->context[0],
  1196. &c->sid[0]);
  1197. if (rc)
  1198. goto out;
  1199. }
  1200. *out_sid = c->sid[0];
  1201. } else {
  1202. *out_sid = SECINITSID_PORT;
  1203. }
  1204. out:
  1205. POLICY_RDUNLOCK;
  1206. return rc;
  1207. }
  1208. /**
  1209. * security_netif_sid - Obtain the SID for a network interface.
  1210. * @name: interface name
  1211. * @if_sid: interface SID
  1212. * @msg_sid: default SID for received packets
  1213. */
  1214. int security_netif_sid(char *name,
  1215. u32 *if_sid,
  1216. u32 *msg_sid)
  1217. {
  1218. int rc = 0;
  1219. struct ocontext *c;
  1220. POLICY_RDLOCK;
  1221. c = policydb.ocontexts[OCON_NETIF];
  1222. while (c) {
  1223. if (strcmp(name, c->u.name) == 0)
  1224. break;
  1225. c = c->next;
  1226. }
  1227. if (c) {
  1228. if (!c->sid[0] || !c->sid[1]) {
  1229. rc = sidtab_context_to_sid(&sidtab,
  1230. &c->context[0],
  1231. &c->sid[0]);
  1232. if (rc)
  1233. goto out;
  1234. rc = sidtab_context_to_sid(&sidtab,
  1235. &c->context[1],
  1236. &c->sid[1]);
  1237. if (rc)
  1238. goto out;
  1239. }
  1240. *if_sid = c->sid[0];
  1241. *msg_sid = c->sid[1];
  1242. } else {
  1243. *if_sid = SECINITSID_NETIF;
  1244. *msg_sid = SECINITSID_NETMSG;
  1245. }
  1246. out:
  1247. POLICY_RDUNLOCK;
  1248. return rc;
  1249. }
  1250. static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
  1251. {
  1252. int i, fail = 0;
  1253. for(i = 0; i < 4; i++)
  1254. if(addr[i] != (input[i] & mask[i])) {
  1255. fail = 1;
  1256. break;
  1257. }
  1258. return !fail;
  1259. }
  1260. /**
  1261. * security_node_sid - Obtain the SID for a node (host).
  1262. * @domain: communication domain aka address family
  1263. * @addrp: address
  1264. * @addrlen: address length in bytes
  1265. * @out_sid: security identifier
  1266. */
  1267. int security_node_sid(u16 domain,
  1268. void *addrp,
  1269. u32 addrlen,
  1270. u32 *out_sid)
  1271. {
  1272. int rc = 0;
  1273. struct ocontext *c;
  1274. POLICY_RDLOCK;
  1275. switch (domain) {
  1276. case AF_INET: {
  1277. u32 addr;
  1278. if (addrlen != sizeof(u32)) {
  1279. rc = -EINVAL;
  1280. goto out;
  1281. }
  1282. addr = *((u32 *)addrp);
  1283. c = policydb.ocontexts[OCON_NODE];
  1284. while (c) {
  1285. if (c->u.node.addr == (addr & c->u.node.mask))
  1286. break;
  1287. c = c->next;
  1288. }
  1289. break;
  1290. }
  1291. case AF_INET6:
  1292. if (addrlen != sizeof(u64) * 2) {
  1293. rc = -EINVAL;
  1294. goto out;
  1295. }
  1296. c = policydb.ocontexts[OCON_NODE6];
  1297. while (c) {
  1298. if (match_ipv6_addrmask(addrp, c->u.node6.addr,
  1299. c->u.node6.mask))
  1300. break;
  1301. c = c->next;
  1302. }
  1303. break;
  1304. default:
  1305. *out_sid = SECINITSID_NODE;
  1306. goto out;
  1307. }
  1308. if (c) {
  1309. if (!c->sid[0]) {
  1310. rc = sidtab_context_to_sid(&sidtab,
  1311. &c->context[0],
  1312. &c->sid[0]);
  1313. if (rc)
  1314. goto out;
  1315. }
  1316. *out_sid = c->sid[0];
  1317. } else {
  1318. *out_sid = SECINITSID_NODE;
  1319. }
  1320. out:
  1321. POLICY_RDUNLOCK;
  1322. return rc;
  1323. }
  1324. #define SIDS_NEL 25
  1325. /**
  1326. * security_get_user_sids - Obtain reachable SIDs for a user.
  1327. * @fromsid: starting SID
  1328. * @username: username
  1329. * @sids: array of reachable SIDs for user
  1330. * @nel: number of elements in @sids
  1331. *
  1332. * Generate the set of SIDs for legal security contexts
  1333. * for a given user that can be reached by @fromsid.
  1334. * Set *@sids to point to a dynamically allocated
  1335. * array containing the set of SIDs. Set *@nel to the
  1336. * number of elements in the array.
  1337. */
  1338. int security_get_user_sids(u32 fromsid,
  1339. char *username,
  1340. u32 **sids,
  1341. u32 *nel)
  1342. {
  1343. struct context *fromcon, usercon;
  1344. u32 *mysids, *mysids2, sid;
  1345. u32 mynel = 0, maxnel = SIDS_NEL;
  1346. struct user_datum *user;
  1347. struct role_datum *role;
  1348. struct av_decision avd;
  1349. struct ebitmap_node *rnode, *tnode;
  1350. int rc = 0, i, j;
  1351. if (!ss_initialized) {
  1352. *sids = NULL;
  1353. *nel = 0;
  1354. goto out;
  1355. }
  1356. POLICY_RDLOCK;
  1357. fromcon = sidtab_search(&sidtab, fromsid);
  1358. if (!fromcon) {
  1359. rc = -EINVAL;
  1360. goto out_unlock;
  1361. }
  1362. user = hashtab_search(policydb.p_users.table, username);
  1363. if (!user) {
  1364. rc = -EINVAL;
  1365. goto out_unlock;
  1366. }
  1367. usercon.user = user->value;
  1368. mysids = kmalloc(maxnel*sizeof(*mysids), GFP_ATOMIC);
  1369. if (!mysids) {
  1370. rc = -ENOMEM;
  1371. goto out_unlock;
  1372. }
  1373. memset(mysids, 0, maxnel*sizeof(*mysids));
  1374. ebitmap_for_each_bit(&user->roles, rnode, i) {
  1375. if (!ebitmap_node_get_bit(rnode, i))
  1376. continue;
  1377. role = policydb.role_val_to_struct[i];
  1378. usercon.role = i+1;
  1379. ebitmap_for_each_bit(&role->types, tnode, j) {
  1380. if (!ebitmap_node_get_bit(tnode, j))
  1381. continue;
  1382. usercon.type = j+1;
  1383. if (mls_setup_user_range(fromcon, user, &usercon))
  1384. continue;
  1385. rc = context_struct_compute_av(fromcon, &usercon,
  1386. SECCLASS_PROCESS,
  1387. PROCESS__TRANSITION,
  1388. &avd);
  1389. if (rc || !(avd.allowed & PROCESS__TRANSITION))
  1390. continue;
  1391. rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
  1392. if (rc) {
  1393. kfree(mysids);
  1394. goto out_unlock;
  1395. }
  1396. if (mynel < maxnel) {
  1397. mysids[mynel++] = sid;
  1398. } else {
  1399. maxnel += SIDS_NEL;
  1400. mysids2 = kmalloc(maxnel*sizeof(*mysids2), GFP_ATOMIC);
  1401. if (!mysids2) {
  1402. rc = -ENOMEM;
  1403. kfree(mysids);
  1404. goto out_unlock;
  1405. }
  1406. memset(mysids2, 0, maxnel*sizeof(*mysids2));
  1407. memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
  1408. kfree(mysids);
  1409. mysids = mysids2;
  1410. mysids[mynel++] = sid;
  1411. }
  1412. }
  1413. }
  1414. *sids = mysids;
  1415. *nel = mynel;
  1416. out_unlock:
  1417. POLICY_RDUNLOCK;
  1418. out:
  1419. return rc;
  1420. }
  1421. /**
  1422. * security_genfs_sid - Obtain a SID for a file in a filesystem
  1423. * @fstype: filesystem type
  1424. * @path: path from root of mount
  1425. * @sclass: file security class
  1426. * @sid: SID for path
  1427. *
  1428. * Obtain a SID to use for a file in a filesystem that
  1429. * cannot support xattr or use a fixed labeling behavior like
  1430. * transition SIDs or task SIDs.
  1431. */
  1432. int security_genfs_sid(const char *fstype,
  1433. char *path,
  1434. u16 sclass,
  1435. u32 *sid)
  1436. {
  1437. int len;
  1438. struct genfs *genfs;
  1439. struct ocontext *c;
  1440. int rc = 0, cmp = 0;
  1441. POLICY_RDLOCK;
  1442. for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
  1443. cmp = strcmp(fstype, genfs->fstype);
  1444. if (cmp <= 0)
  1445. break;
  1446. }
  1447. if (!genfs || cmp) {
  1448. *sid = SECINITSID_UNLABELED;
  1449. rc = -ENOENT;
  1450. goto out;
  1451. }
  1452. for (c = genfs->head; c; c = c->next) {
  1453. len = strlen(c->u.name);
  1454. if ((!c->v.sclass || sclass == c->v.sclass) &&
  1455. (strncmp(c->u.name, path, len) == 0))
  1456. break;
  1457. }
  1458. if (!c) {
  1459. *sid = SECINITSID_UNLABELED;
  1460. rc = -ENOENT;
  1461. goto out;
  1462. }
  1463. if (!c->sid[0]) {
  1464. rc = sidtab_context_to_sid(&sidtab,
  1465. &c->context[0],
  1466. &c->sid[0]);
  1467. if (rc)
  1468. goto out;
  1469. }
  1470. *sid = c->sid[0];
  1471. out:
  1472. POLICY_RDUNLOCK;
  1473. return rc;
  1474. }
  1475. /**
  1476. * security_fs_use - Determine how to handle labeling for a filesystem.
  1477. * @fstype: filesystem type
  1478. * @behavior: labeling behavior
  1479. * @sid: SID for filesystem (superblock)
  1480. */
  1481. int security_fs_use(
  1482. const char *fstype,
  1483. unsigned int *behavior,
  1484. u32 *sid)
  1485. {
  1486. int rc = 0;
  1487. struct ocontext *c;
  1488. POLICY_RDLOCK;
  1489. c = policydb.ocontexts[OCON_FSUSE];
  1490. while (c) {
  1491. if (strcmp(fstype, c->u.name) == 0)
  1492. break;
  1493. c = c->next;
  1494. }
  1495. if (c) {
  1496. *behavior = c->v.behavior;
  1497. if (!c->sid[0]) {
  1498. rc = sidtab_context_to_sid(&sidtab,
  1499. &c->context[0],
  1500. &c->sid[0]);
  1501. if (rc)
  1502. goto out;
  1503. }
  1504. *sid = c->sid[0];
  1505. } else {
  1506. rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
  1507. if (rc) {
  1508. *behavior = SECURITY_FS_USE_NONE;
  1509. rc = 0;
  1510. } else {
  1511. *behavior = SECURITY_FS_USE_GENFS;
  1512. }
  1513. }
  1514. out:
  1515. POLICY_RDUNLOCK;
  1516. return rc;
  1517. }
  1518. int security_get_bools(int *len, char ***names, int **values)
  1519. {
  1520. int i, rc = -ENOMEM;
  1521. POLICY_RDLOCK;
  1522. *names = NULL;
  1523. *values = NULL;
  1524. *len = policydb.p_bools.nprim;
  1525. if (!*len) {
  1526. rc = 0;
  1527. goto out;
  1528. }
  1529. *names = (char**)kmalloc(sizeof(char*) * *len, GFP_ATOMIC);
  1530. if (!*names)
  1531. goto err;
  1532. memset(*names, 0, sizeof(char*) * *len);
  1533. *values = (int*)kmalloc(sizeof(int) * *len, GFP_ATOMIC);
  1534. if (!*values)
  1535. goto err;
  1536. for (i = 0; i < *len; i++) {
  1537. size_t name_len;
  1538. (*values)[i] = policydb.bool_val_to_struct[i]->state;
  1539. name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
  1540. (*names)[i] = (char*)kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
  1541. if (!(*names)[i])
  1542. goto err;
  1543. strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
  1544. (*names)[i][name_len - 1] = 0;
  1545. }
  1546. rc = 0;
  1547. out:
  1548. POLICY_RDUNLOCK;
  1549. return rc;
  1550. err:
  1551. if (*names) {
  1552. for (i = 0; i < *len; i++)
  1553. kfree((*names)[i]);
  1554. }
  1555. kfree(*values);
  1556. goto out;
  1557. }
  1558. int security_set_bools(int len, int *values)
  1559. {
  1560. int i, rc = 0;
  1561. int lenp, seqno = 0;
  1562. struct cond_node *cur;
  1563. POLICY_WRLOCK;
  1564. lenp = policydb.p_bools.nprim;
  1565. if (len != lenp) {
  1566. rc = -EFAULT;
  1567. goto out;
  1568. }
  1569. printk(KERN_INFO "security: committed booleans { ");
  1570. for (i = 0; i < len; i++) {
  1571. if (values[i]) {
  1572. policydb.bool_val_to_struct[i]->state = 1;
  1573. } else {
  1574. policydb.bool_val_to_struct[i]->state = 0;
  1575. }
  1576. if (i != 0)
  1577. printk(", ");
  1578. printk("%s:%d", policydb.p_bool_val_to_name[i],
  1579. policydb.bool_val_to_struct[i]->state);
  1580. }
  1581. printk(" }\n");
  1582. for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
  1583. rc = evaluate_cond_node(&policydb, cur);
  1584. if (rc)
  1585. goto out;
  1586. }
  1587. seqno = ++latest_granting;
  1588. out:
  1589. POLICY_WRUNLOCK;
  1590. if (!rc) {
  1591. avc_ss_reset(seqno);
  1592. selnl_notify_policyload(seqno);
  1593. }
  1594. return rc;
  1595. }
  1596. int security_get_bool_value(int bool)
  1597. {
  1598. int rc = 0;
  1599. int len;
  1600. POLICY_RDLOCK;
  1601. len = policydb.p_bools.nprim;
  1602. if (bool >= len) {
  1603. rc = -EFAULT;
  1604. goto out;
  1605. }
  1606. rc = policydb.bool_val_to_struct[bool]->state;
  1607. out:
  1608. POLICY_RDUNLOCK;
  1609. return rc;
  1610. }