services.c 42 KB

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