services.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  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. /**
  553. * security_context_to_sid - Obtain a SID for a given security context.
  554. * @scontext: security context
  555. * @scontext_len: length in bytes
  556. * @sid: security identifier, SID
  557. *
  558. * Obtains a SID associated with the security context that
  559. * has the string representation specified by @scontext.
  560. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  561. * memory is available, or 0 on success.
  562. */
  563. int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
  564. {
  565. char *scontext2;
  566. struct context context;
  567. struct role_datum *role;
  568. struct type_datum *typdatum;
  569. struct user_datum *usrdatum;
  570. char *scontextp, *p, oldc;
  571. int rc = 0;
  572. if (!ss_initialized) {
  573. int i;
  574. for (i = 1; i < SECINITSID_NUM; i++) {
  575. if (!strcmp(initial_sid_to_string[i], scontext)) {
  576. *sid = i;
  577. goto out;
  578. }
  579. }
  580. *sid = SECINITSID_KERNEL;
  581. goto out;
  582. }
  583. *sid = SECSID_NULL;
  584. /* Copy the string so that we can modify the copy as we parse it.
  585. The string should already by null terminated, but we append a
  586. null suffix to the copy to avoid problems with the existing
  587. attr package, which doesn't view the null terminator as part
  588. of the attribute value. */
  589. scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
  590. if (!scontext2) {
  591. rc = -ENOMEM;
  592. goto out;
  593. }
  594. memcpy(scontext2, scontext, scontext_len);
  595. scontext2[scontext_len] = 0;
  596. context_init(&context);
  597. *sid = SECSID_NULL;
  598. POLICY_RDLOCK;
  599. /* Parse the security context. */
  600. rc = -EINVAL;
  601. scontextp = (char *) scontext2;
  602. /* Extract the user. */
  603. p = scontextp;
  604. while (*p && *p != ':')
  605. p++;
  606. if (*p == 0)
  607. goto out_unlock;
  608. *p++ = 0;
  609. usrdatum = hashtab_search(policydb.p_users.table, scontextp);
  610. if (!usrdatum)
  611. goto out_unlock;
  612. context.user = usrdatum->value;
  613. /* Extract role. */
  614. scontextp = p;
  615. while (*p && *p != ':')
  616. p++;
  617. if (*p == 0)
  618. goto out_unlock;
  619. *p++ = 0;
  620. role = hashtab_search(policydb.p_roles.table, scontextp);
  621. if (!role)
  622. goto out_unlock;
  623. context.role = role->value;
  624. /* Extract type. */
  625. scontextp = p;
  626. while (*p && *p != ':')
  627. p++;
  628. oldc = *p;
  629. *p++ = 0;
  630. typdatum = hashtab_search(policydb.p_types.table, scontextp);
  631. if (!typdatum)
  632. goto out_unlock;
  633. context.type = typdatum->value;
  634. rc = mls_context_to_sid(oldc, &p, &context);
  635. if (rc)
  636. goto out_unlock;
  637. if ((p - scontext2) < scontext_len) {
  638. rc = -EINVAL;
  639. goto out_unlock;
  640. }
  641. /* Check the validity of the new context. */
  642. if (!policydb_context_isvalid(&policydb, &context)) {
  643. rc = -EINVAL;
  644. goto out_unlock;
  645. }
  646. /* Obtain the new sid. */
  647. rc = sidtab_context_to_sid(&sidtab, &context, sid);
  648. out_unlock:
  649. POLICY_RDUNLOCK;
  650. context_destroy(&context);
  651. kfree(scontext2);
  652. out:
  653. return rc;
  654. }
  655. static int compute_sid_handle_invalid_context(
  656. struct context *scontext,
  657. struct context *tcontext,
  658. u16 tclass,
  659. struct context *newcontext)
  660. {
  661. char *s = NULL, *t = NULL, *n = NULL;
  662. u32 slen, tlen, nlen;
  663. if (context_struct_to_string(scontext, &s, &slen) < 0)
  664. goto out;
  665. if (context_struct_to_string(tcontext, &t, &tlen) < 0)
  666. goto out;
  667. if (context_struct_to_string(newcontext, &n, &nlen) < 0)
  668. goto out;
  669. audit_log(current->audit_context, AUDIT_SELINUX_ERR,
  670. "security_compute_sid: invalid context %s"
  671. " for scontext=%s"
  672. " tcontext=%s"
  673. " tclass=%s",
  674. n, s, t, policydb.p_class_val_to_name[tclass-1]);
  675. out:
  676. kfree(s);
  677. kfree(t);
  678. kfree(n);
  679. if (!selinux_enforcing)
  680. return 0;
  681. return -EACCES;
  682. }
  683. static int security_compute_sid(u32 ssid,
  684. u32 tsid,
  685. u16 tclass,
  686. u32 specified,
  687. u32 *out_sid)
  688. {
  689. struct context *scontext = NULL, *tcontext = NULL, newcontext;
  690. struct role_trans *roletr = NULL;
  691. struct avtab_key avkey;
  692. struct avtab_datum *avdatum;
  693. struct avtab_node *node;
  694. unsigned int type_change = 0;
  695. int rc = 0;
  696. if (!ss_initialized) {
  697. switch (tclass) {
  698. case SECCLASS_PROCESS:
  699. *out_sid = ssid;
  700. break;
  701. default:
  702. *out_sid = tsid;
  703. break;
  704. }
  705. goto out;
  706. }
  707. POLICY_RDLOCK;
  708. scontext = sidtab_search(&sidtab, ssid);
  709. if (!scontext) {
  710. printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
  711. ssid);
  712. rc = -EINVAL;
  713. goto out_unlock;
  714. }
  715. tcontext = sidtab_search(&sidtab, tsid);
  716. if (!tcontext) {
  717. printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
  718. tsid);
  719. rc = -EINVAL;
  720. goto out_unlock;
  721. }
  722. context_init(&newcontext);
  723. /* Set the user identity. */
  724. switch (specified) {
  725. case AVTAB_TRANSITION:
  726. case AVTAB_CHANGE:
  727. /* Use the process user identity. */
  728. newcontext.user = scontext->user;
  729. break;
  730. case AVTAB_MEMBER:
  731. /* Use the related object owner. */
  732. newcontext.user = tcontext->user;
  733. break;
  734. }
  735. /* Set the role and type to default values. */
  736. switch (tclass) {
  737. case SECCLASS_PROCESS:
  738. /* Use the current role and type of process. */
  739. newcontext.role = scontext->role;
  740. newcontext.type = scontext->type;
  741. break;
  742. default:
  743. /* Use the well-defined object role. */
  744. newcontext.role = OBJECT_R_VAL;
  745. /* Use the type of the related object. */
  746. newcontext.type = tcontext->type;
  747. }
  748. /* Look for a type transition/member/change rule. */
  749. avkey.source_type = scontext->type;
  750. avkey.target_type = tcontext->type;
  751. avkey.target_class = tclass;
  752. avdatum = avtab_search(&policydb.te_avtab, &avkey, AVTAB_TYPE);
  753. /* If no permanent rule, also check for enabled conditional rules */
  754. if(!avdatum) {
  755. node = avtab_search_node(&policydb.te_cond_avtab, &avkey, specified);
  756. for (; node != NULL; node = avtab_search_node_next(node, specified)) {
  757. if (node->datum.specified & AVTAB_ENABLED) {
  758. avdatum = &node->datum;
  759. break;
  760. }
  761. }
  762. }
  763. type_change = (avdatum && (avdatum->specified & specified));
  764. if (type_change) {
  765. /* Use the type from the type transition/member/change rule. */
  766. switch (specified) {
  767. case AVTAB_TRANSITION:
  768. newcontext.type = avtab_transition(avdatum);
  769. break;
  770. case AVTAB_MEMBER:
  771. newcontext.type = avtab_member(avdatum);
  772. break;
  773. case AVTAB_CHANGE:
  774. newcontext.type = avtab_change(avdatum);
  775. break;
  776. }
  777. }
  778. /* Check for class-specific changes. */
  779. switch (tclass) {
  780. case SECCLASS_PROCESS:
  781. if (specified & AVTAB_TRANSITION) {
  782. /* Look for a role transition rule. */
  783. for (roletr = policydb.role_tr; roletr;
  784. roletr = roletr->next) {
  785. if (roletr->role == scontext->role &&
  786. roletr->type == tcontext->type) {
  787. /* Use the role transition rule. */
  788. newcontext.role = roletr->new_role;
  789. break;
  790. }
  791. }
  792. }
  793. break;
  794. default:
  795. break;
  796. }
  797. /* Set the MLS attributes.
  798. This is done last because it may allocate memory. */
  799. rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
  800. if (rc)
  801. goto out_unlock;
  802. /* Check the validity of the context. */
  803. if (!policydb_context_isvalid(&policydb, &newcontext)) {
  804. rc = compute_sid_handle_invalid_context(scontext,
  805. tcontext,
  806. tclass,
  807. &newcontext);
  808. if (rc)
  809. goto out_unlock;
  810. }
  811. /* Obtain the sid for the context. */
  812. rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
  813. out_unlock:
  814. POLICY_RDUNLOCK;
  815. context_destroy(&newcontext);
  816. out:
  817. return rc;
  818. }
  819. /**
  820. * security_transition_sid - Compute the SID for a new subject/object.
  821. * @ssid: source security identifier
  822. * @tsid: target security identifier
  823. * @tclass: target security class
  824. * @out_sid: security identifier for new subject/object
  825. *
  826. * Compute a SID to use for labeling a new subject or object in the
  827. * class @tclass based on a SID pair (@ssid, @tsid).
  828. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  829. * if insufficient memory is available, or %0 if the new SID was
  830. * computed successfully.
  831. */
  832. int security_transition_sid(u32 ssid,
  833. u32 tsid,
  834. u16 tclass,
  835. u32 *out_sid)
  836. {
  837. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
  838. }
  839. /**
  840. * security_member_sid - Compute the SID for member selection.
  841. * @ssid: source security identifier
  842. * @tsid: target security identifier
  843. * @tclass: target security class
  844. * @out_sid: security identifier for selected member
  845. *
  846. * Compute a SID to use when selecting a member of a polyinstantiated
  847. * object of class @tclass based on a SID pair (@ssid, @tsid).
  848. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  849. * if insufficient memory is available, or %0 if the SID was
  850. * computed successfully.
  851. */
  852. int security_member_sid(u32 ssid,
  853. u32 tsid,
  854. u16 tclass,
  855. u32 *out_sid)
  856. {
  857. return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
  858. }
  859. /**
  860. * security_change_sid - Compute the SID for object relabeling.
  861. * @ssid: source security identifier
  862. * @tsid: target security identifier
  863. * @tclass: target security class
  864. * @out_sid: security identifier for selected member
  865. *
  866. * Compute a SID to use for relabeling an object of class @tclass
  867. * based on a SID pair (@ssid, @tsid).
  868. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  869. * if insufficient memory is available, or %0 if the SID was
  870. * computed successfully.
  871. */
  872. int security_change_sid(u32 ssid,
  873. u32 tsid,
  874. u16 tclass,
  875. u32 *out_sid)
  876. {
  877. return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
  878. }
  879. /*
  880. * Verify that each permission that is defined under the
  881. * existing policy is still defined with the same value
  882. * in the new policy.
  883. */
  884. static int validate_perm(void *key, void *datum, void *p)
  885. {
  886. struct hashtab *h;
  887. struct perm_datum *perdatum, *perdatum2;
  888. int rc = 0;
  889. h = p;
  890. perdatum = datum;
  891. perdatum2 = hashtab_search(h, key);
  892. if (!perdatum2) {
  893. printk(KERN_ERR "security: permission %s disappeared",
  894. (char *)key);
  895. rc = -ENOENT;
  896. goto out;
  897. }
  898. if (perdatum->value != perdatum2->value) {
  899. printk(KERN_ERR "security: the value of permission %s changed",
  900. (char *)key);
  901. rc = -EINVAL;
  902. }
  903. out:
  904. return rc;
  905. }
  906. /*
  907. * Verify that each class that is defined under the
  908. * existing policy is still defined with the same
  909. * attributes in the new policy.
  910. */
  911. static int validate_class(void *key, void *datum, void *p)
  912. {
  913. struct policydb *newp;
  914. struct class_datum *cladatum, *cladatum2;
  915. int rc;
  916. newp = p;
  917. cladatum = datum;
  918. cladatum2 = hashtab_search(newp->p_classes.table, key);
  919. if (!cladatum2) {
  920. printk(KERN_ERR "security: class %s disappeared\n",
  921. (char *)key);
  922. rc = -ENOENT;
  923. goto out;
  924. }
  925. if (cladatum->value != cladatum2->value) {
  926. printk(KERN_ERR "security: the value of class %s changed\n",
  927. (char *)key);
  928. rc = -EINVAL;
  929. goto out;
  930. }
  931. if ((cladatum->comdatum && !cladatum2->comdatum) ||
  932. (!cladatum->comdatum && cladatum2->comdatum)) {
  933. printk(KERN_ERR "security: the inherits clause for the access "
  934. "vector definition for class %s changed\n", (char *)key);
  935. rc = -EINVAL;
  936. goto out;
  937. }
  938. if (cladatum->comdatum) {
  939. rc = hashtab_map(cladatum->comdatum->permissions.table, validate_perm,
  940. cladatum2->comdatum->permissions.table);
  941. if (rc) {
  942. printk(" in the access vector definition for class "
  943. "%s\n", (char *)key);
  944. goto out;
  945. }
  946. }
  947. rc = hashtab_map(cladatum->permissions.table, validate_perm,
  948. cladatum2->permissions.table);
  949. if (rc)
  950. printk(" in access vector definition for class %s\n",
  951. (char *)key);
  952. out:
  953. return rc;
  954. }
  955. /* Clone the SID into the new SID table. */
  956. static int clone_sid(u32 sid,
  957. struct context *context,
  958. void *arg)
  959. {
  960. struct sidtab *s = arg;
  961. return sidtab_insert(s, sid, context);
  962. }
  963. static inline int convert_context_handle_invalid_context(struct context *context)
  964. {
  965. int rc = 0;
  966. if (selinux_enforcing) {
  967. rc = -EINVAL;
  968. } else {
  969. char *s;
  970. u32 len;
  971. context_struct_to_string(context, &s, &len);
  972. printk(KERN_ERR "security: context %s is invalid\n", s);
  973. kfree(s);
  974. }
  975. return rc;
  976. }
  977. struct convert_context_args {
  978. struct policydb *oldp;
  979. struct policydb *newp;
  980. };
  981. /*
  982. * Convert the values in the security context
  983. * structure `c' from the values specified
  984. * in the policy `p->oldp' to the values specified
  985. * in the policy `p->newp'. Verify that the
  986. * context is valid under the new policy.
  987. */
  988. static int convert_context(u32 key,
  989. struct context *c,
  990. void *p)
  991. {
  992. struct convert_context_args *args;
  993. struct context oldc;
  994. struct role_datum *role;
  995. struct type_datum *typdatum;
  996. struct user_datum *usrdatum;
  997. char *s;
  998. u32 len;
  999. int rc;
  1000. args = p;
  1001. rc = context_cpy(&oldc, c);
  1002. if (rc)
  1003. goto out;
  1004. rc = -EINVAL;
  1005. /* Convert the user. */
  1006. usrdatum = hashtab_search(args->newp->p_users.table,
  1007. args->oldp->p_user_val_to_name[c->user - 1]);
  1008. if (!usrdatum) {
  1009. goto bad;
  1010. }
  1011. c->user = usrdatum->value;
  1012. /* Convert the role. */
  1013. role = hashtab_search(args->newp->p_roles.table,
  1014. args->oldp->p_role_val_to_name[c->role - 1]);
  1015. if (!role) {
  1016. goto bad;
  1017. }
  1018. c->role = role->value;
  1019. /* Convert the type. */
  1020. typdatum = hashtab_search(args->newp->p_types.table,
  1021. args->oldp->p_type_val_to_name[c->type - 1]);
  1022. if (!typdatum) {
  1023. goto bad;
  1024. }
  1025. c->type = typdatum->value;
  1026. rc = mls_convert_context(args->oldp, args->newp, c);
  1027. if (rc)
  1028. goto bad;
  1029. /* Check the validity of the new context. */
  1030. if (!policydb_context_isvalid(args->newp, c)) {
  1031. rc = convert_context_handle_invalid_context(&oldc);
  1032. if (rc)
  1033. goto bad;
  1034. }
  1035. context_destroy(&oldc);
  1036. out:
  1037. return rc;
  1038. bad:
  1039. context_struct_to_string(&oldc, &s, &len);
  1040. context_destroy(&oldc);
  1041. printk(KERN_ERR "security: invalidating context %s\n", s);
  1042. kfree(s);
  1043. goto out;
  1044. }
  1045. extern void selinux_complete_init(void);
  1046. /**
  1047. * security_load_policy - Load a security policy configuration.
  1048. * @data: binary policy data
  1049. * @len: length of data in bytes
  1050. *
  1051. * Load a new set of security policy configuration data,
  1052. * validate it and convert the SID table as necessary.
  1053. * This function will flush the access vector cache after
  1054. * loading the new policy.
  1055. */
  1056. int security_load_policy(void *data, size_t len)
  1057. {
  1058. struct policydb oldpolicydb, newpolicydb;
  1059. struct sidtab oldsidtab, newsidtab;
  1060. struct convert_context_args args;
  1061. u32 seqno;
  1062. int rc = 0;
  1063. struct policy_file file = { data, len }, *fp = &file;
  1064. LOAD_LOCK;
  1065. if (!ss_initialized) {
  1066. avtab_cache_init();
  1067. if (policydb_read(&policydb, fp)) {
  1068. LOAD_UNLOCK;
  1069. avtab_cache_destroy();
  1070. return -EINVAL;
  1071. }
  1072. if (policydb_load_isids(&policydb, &sidtab)) {
  1073. LOAD_UNLOCK;
  1074. policydb_destroy(&policydb);
  1075. avtab_cache_destroy();
  1076. return -EINVAL;
  1077. }
  1078. policydb_loaded_version = policydb.policyvers;
  1079. ss_initialized = 1;
  1080. seqno = ++latest_granting;
  1081. LOAD_UNLOCK;
  1082. selinux_complete_init();
  1083. avc_ss_reset(seqno);
  1084. selnl_notify_policyload(seqno);
  1085. return 0;
  1086. }
  1087. #if 0
  1088. sidtab_hash_eval(&sidtab, "sids");
  1089. #endif
  1090. if (policydb_read(&newpolicydb, fp)) {
  1091. LOAD_UNLOCK;
  1092. return -EINVAL;
  1093. }
  1094. sidtab_init(&newsidtab);
  1095. /* Verify that the existing classes did not change. */
  1096. if (hashtab_map(policydb.p_classes.table, validate_class, &newpolicydb)) {
  1097. printk(KERN_ERR "security: the definition of an existing "
  1098. "class changed\n");
  1099. rc = -EINVAL;
  1100. goto err;
  1101. }
  1102. /* Clone the SID table. */
  1103. sidtab_shutdown(&sidtab);
  1104. if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
  1105. rc = -ENOMEM;
  1106. goto err;
  1107. }
  1108. /* Convert the internal representations of contexts
  1109. in the new SID table and remove invalid SIDs. */
  1110. args.oldp = &policydb;
  1111. args.newp = &newpolicydb;
  1112. sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
  1113. /* Save the old policydb and SID table to free later. */
  1114. memcpy(&oldpolicydb, &policydb, sizeof policydb);
  1115. sidtab_set(&oldsidtab, &sidtab);
  1116. /* Install the new policydb and SID table. */
  1117. POLICY_WRLOCK;
  1118. memcpy(&policydb, &newpolicydb, sizeof policydb);
  1119. sidtab_set(&sidtab, &newsidtab);
  1120. seqno = ++latest_granting;
  1121. policydb_loaded_version = policydb.policyvers;
  1122. POLICY_WRUNLOCK;
  1123. LOAD_UNLOCK;
  1124. /* Free the old policydb and SID table. */
  1125. policydb_destroy(&oldpolicydb);
  1126. sidtab_destroy(&oldsidtab);
  1127. avc_ss_reset(seqno);
  1128. selnl_notify_policyload(seqno);
  1129. return 0;
  1130. err:
  1131. LOAD_UNLOCK;
  1132. sidtab_destroy(&newsidtab);
  1133. policydb_destroy(&newpolicydb);
  1134. return rc;
  1135. }
  1136. /**
  1137. * security_port_sid - Obtain the SID for a port.
  1138. * @domain: communication domain aka address family
  1139. * @type: socket type
  1140. * @protocol: protocol number
  1141. * @port: port number
  1142. * @out_sid: security identifier
  1143. */
  1144. int security_port_sid(u16 domain,
  1145. u16 type,
  1146. u8 protocol,
  1147. u16 port,
  1148. u32 *out_sid)
  1149. {
  1150. struct ocontext *c;
  1151. int rc = 0;
  1152. POLICY_RDLOCK;
  1153. c = policydb.ocontexts[OCON_PORT];
  1154. while (c) {
  1155. if (c->u.port.protocol == protocol &&
  1156. c->u.port.low_port <= port &&
  1157. c->u.port.high_port >= port)
  1158. break;
  1159. c = c->next;
  1160. }
  1161. if (c) {
  1162. if (!c->sid[0]) {
  1163. rc = sidtab_context_to_sid(&sidtab,
  1164. &c->context[0],
  1165. &c->sid[0]);
  1166. if (rc)
  1167. goto out;
  1168. }
  1169. *out_sid = c->sid[0];
  1170. } else {
  1171. *out_sid = SECINITSID_PORT;
  1172. }
  1173. out:
  1174. POLICY_RDUNLOCK;
  1175. return rc;
  1176. }
  1177. /**
  1178. * security_netif_sid - Obtain the SID for a network interface.
  1179. * @name: interface name
  1180. * @if_sid: interface SID
  1181. * @msg_sid: default SID for received packets
  1182. */
  1183. int security_netif_sid(char *name,
  1184. u32 *if_sid,
  1185. u32 *msg_sid)
  1186. {
  1187. int rc = 0;
  1188. struct ocontext *c;
  1189. POLICY_RDLOCK;
  1190. c = policydb.ocontexts[OCON_NETIF];
  1191. while (c) {
  1192. if (strcmp(name, c->u.name) == 0)
  1193. break;
  1194. c = c->next;
  1195. }
  1196. if (c) {
  1197. if (!c->sid[0] || !c->sid[1]) {
  1198. rc = sidtab_context_to_sid(&sidtab,
  1199. &c->context[0],
  1200. &c->sid[0]);
  1201. if (rc)
  1202. goto out;
  1203. rc = sidtab_context_to_sid(&sidtab,
  1204. &c->context[1],
  1205. &c->sid[1]);
  1206. if (rc)
  1207. goto out;
  1208. }
  1209. *if_sid = c->sid[0];
  1210. *msg_sid = c->sid[1];
  1211. } else {
  1212. *if_sid = SECINITSID_NETIF;
  1213. *msg_sid = SECINITSID_NETMSG;
  1214. }
  1215. out:
  1216. POLICY_RDUNLOCK;
  1217. return rc;
  1218. }
  1219. static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
  1220. {
  1221. int i, fail = 0;
  1222. for(i = 0; i < 4; i++)
  1223. if(addr[i] != (input[i] & mask[i])) {
  1224. fail = 1;
  1225. break;
  1226. }
  1227. return !fail;
  1228. }
  1229. /**
  1230. * security_node_sid - Obtain the SID for a node (host).
  1231. * @domain: communication domain aka address family
  1232. * @addrp: address
  1233. * @addrlen: address length in bytes
  1234. * @out_sid: security identifier
  1235. */
  1236. int security_node_sid(u16 domain,
  1237. void *addrp,
  1238. u32 addrlen,
  1239. u32 *out_sid)
  1240. {
  1241. int rc = 0;
  1242. struct ocontext *c;
  1243. POLICY_RDLOCK;
  1244. switch (domain) {
  1245. case AF_INET: {
  1246. u32 addr;
  1247. if (addrlen != sizeof(u32)) {
  1248. rc = -EINVAL;
  1249. goto out;
  1250. }
  1251. addr = *((u32 *)addrp);
  1252. c = policydb.ocontexts[OCON_NODE];
  1253. while (c) {
  1254. if (c->u.node.addr == (addr & c->u.node.mask))
  1255. break;
  1256. c = c->next;
  1257. }
  1258. break;
  1259. }
  1260. case AF_INET6:
  1261. if (addrlen != sizeof(u64) * 2) {
  1262. rc = -EINVAL;
  1263. goto out;
  1264. }
  1265. c = policydb.ocontexts[OCON_NODE6];
  1266. while (c) {
  1267. if (match_ipv6_addrmask(addrp, c->u.node6.addr,
  1268. c->u.node6.mask))
  1269. break;
  1270. c = c->next;
  1271. }
  1272. break;
  1273. default:
  1274. *out_sid = SECINITSID_NODE;
  1275. goto out;
  1276. }
  1277. if (c) {
  1278. if (!c->sid[0]) {
  1279. rc = sidtab_context_to_sid(&sidtab,
  1280. &c->context[0],
  1281. &c->sid[0]);
  1282. if (rc)
  1283. goto out;
  1284. }
  1285. *out_sid = c->sid[0];
  1286. } else {
  1287. *out_sid = SECINITSID_NODE;
  1288. }
  1289. out:
  1290. POLICY_RDUNLOCK;
  1291. return rc;
  1292. }
  1293. #define SIDS_NEL 25
  1294. /**
  1295. * security_get_user_sids - Obtain reachable SIDs for a user.
  1296. * @fromsid: starting SID
  1297. * @username: username
  1298. * @sids: array of reachable SIDs for user
  1299. * @nel: number of elements in @sids
  1300. *
  1301. * Generate the set of SIDs for legal security contexts
  1302. * for a given user that can be reached by @fromsid.
  1303. * Set *@sids to point to a dynamically allocated
  1304. * array containing the set of SIDs. Set *@nel to the
  1305. * number of elements in the array.
  1306. */
  1307. int security_get_user_sids(u32 fromsid,
  1308. char *username,
  1309. u32 **sids,
  1310. u32 *nel)
  1311. {
  1312. struct context *fromcon, usercon;
  1313. u32 *mysids, *mysids2, sid;
  1314. u32 mynel = 0, maxnel = SIDS_NEL;
  1315. struct user_datum *user;
  1316. struct role_datum *role;
  1317. struct av_decision avd;
  1318. int rc = 0, i, j;
  1319. if (!ss_initialized) {
  1320. *sids = NULL;
  1321. *nel = 0;
  1322. goto out;
  1323. }
  1324. POLICY_RDLOCK;
  1325. fromcon = sidtab_search(&sidtab, fromsid);
  1326. if (!fromcon) {
  1327. rc = -EINVAL;
  1328. goto out_unlock;
  1329. }
  1330. user = hashtab_search(policydb.p_users.table, username);
  1331. if (!user) {
  1332. rc = -EINVAL;
  1333. goto out_unlock;
  1334. }
  1335. usercon.user = user->value;
  1336. mysids = kmalloc(maxnel*sizeof(*mysids), GFP_ATOMIC);
  1337. if (!mysids) {
  1338. rc = -ENOMEM;
  1339. goto out_unlock;
  1340. }
  1341. memset(mysids, 0, maxnel*sizeof(*mysids));
  1342. for (i = ebitmap_startbit(&user->roles); i < ebitmap_length(&user->roles); i++) {
  1343. if (!ebitmap_get_bit(&user->roles, i))
  1344. continue;
  1345. role = policydb.role_val_to_struct[i];
  1346. usercon.role = i+1;
  1347. for (j = ebitmap_startbit(&role->types); j < ebitmap_length(&role->types); j++) {
  1348. if (!ebitmap_get_bit(&role->types, j))
  1349. continue;
  1350. usercon.type = j+1;
  1351. if (mls_setup_user_range(fromcon, user, &usercon))
  1352. continue;
  1353. rc = context_struct_compute_av(fromcon, &usercon,
  1354. SECCLASS_PROCESS,
  1355. PROCESS__TRANSITION,
  1356. &avd);
  1357. if (rc || !(avd.allowed & PROCESS__TRANSITION))
  1358. continue;
  1359. rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
  1360. if (rc) {
  1361. kfree(mysids);
  1362. goto out_unlock;
  1363. }
  1364. if (mynel < maxnel) {
  1365. mysids[mynel++] = sid;
  1366. } else {
  1367. maxnel += SIDS_NEL;
  1368. mysids2 = kmalloc(maxnel*sizeof(*mysids2), GFP_ATOMIC);
  1369. if (!mysids2) {
  1370. rc = -ENOMEM;
  1371. kfree(mysids);
  1372. goto out_unlock;
  1373. }
  1374. memset(mysids2, 0, maxnel*sizeof(*mysids2));
  1375. memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
  1376. kfree(mysids);
  1377. mysids = mysids2;
  1378. mysids[mynel++] = sid;
  1379. }
  1380. }
  1381. }
  1382. *sids = mysids;
  1383. *nel = mynel;
  1384. out_unlock:
  1385. POLICY_RDUNLOCK;
  1386. out:
  1387. return rc;
  1388. }
  1389. /**
  1390. * security_genfs_sid - Obtain a SID for a file in a filesystem
  1391. * @fstype: filesystem type
  1392. * @path: path from root of mount
  1393. * @sclass: file security class
  1394. * @sid: SID for path
  1395. *
  1396. * Obtain a SID to use for a file in a filesystem that
  1397. * cannot support xattr or use a fixed labeling behavior like
  1398. * transition SIDs or task SIDs.
  1399. */
  1400. int security_genfs_sid(const char *fstype,
  1401. char *path,
  1402. u16 sclass,
  1403. u32 *sid)
  1404. {
  1405. int len;
  1406. struct genfs *genfs;
  1407. struct ocontext *c;
  1408. int rc = 0, cmp = 0;
  1409. POLICY_RDLOCK;
  1410. for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
  1411. cmp = strcmp(fstype, genfs->fstype);
  1412. if (cmp <= 0)
  1413. break;
  1414. }
  1415. if (!genfs || cmp) {
  1416. *sid = SECINITSID_UNLABELED;
  1417. rc = -ENOENT;
  1418. goto out;
  1419. }
  1420. for (c = genfs->head; c; c = c->next) {
  1421. len = strlen(c->u.name);
  1422. if ((!c->v.sclass || sclass == c->v.sclass) &&
  1423. (strncmp(c->u.name, path, len) == 0))
  1424. break;
  1425. }
  1426. if (!c) {
  1427. *sid = SECINITSID_UNLABELED;
  1428. rc = -ENOENT;
  1429. goto out;
  1430. }
  1431. if (!c->sid[0]) {
  1432. rc = sidtab_context_to_sid(&sidtab,
  1433. &c->context[0],
  1434. &c->sid[0]);
  1435. if (rc)
  1436. goto out;
  1437. }
  1438. *sid = c->sid[0];
  1439. out:
  1440. POLICY_RDUNLOCK;
  1441. return rc;
  1442. }
  1443. /**
  1444. * security_fs_use - Determine how to handle labeling for a filesystem.
  1445. * @fstype: filesystem type
  1446. * @behavior: labeling behavior
  1447. * @sid: SID for filesystem (superblock)
  1448. */
  1449. int security_fs_use(
  1450. const char *fstype,
  1451. unsigned int *behavior,
  1452. u32 *sid)
  1453. {
  1454. int rc = 0;
  1455. struct ocontext *c;
  1456. POLICY_RDLOCK;
  1457. c = policydb.ocontexts[OCON_FSUSE];
  1458. while (c) {
  1459. if (strcmp(fstype, c->u.name) == 0)
  1460. break;
  1461. c = c->next;
  1462. }
  1463. if (c) {
  1464. *behavior = c->v.behavior;
  1465. if (!c->sid[0]) {
  1466. rc = sidtab_context_to_sid(&sidtab,
  1467. &c->context[0],
  1468. &c->sid[0]);
  1469. if (rc)
  1470. goto out;
  1471. }
  1472. *sid = c->sid[0];
  1473. } else {
  1474. rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
  1475. if (rc) {
  1476. *behavior = SECURITY_FS_USE_NONE;
  1477. rc = 0;
  1478. } else {
  1479. *behavior = SECURITY_FS_USE_GENFS;
  1480. }
  1481. }
  1482. out:
  1483. POLICY_RDUNLOCK;
  1484. return rc;
  1485. }
  1486. int security_get_bools(int *len, char ***names, int **values)
  1487. {
  1488. int i, rc = -ENOMEM;
  1489. POLICY_RDLOCK;
  1490. *names = NULL;
  1491. *values = NULL;
  1492. *len = policydb.p_bools.nprim;
  1493. if (!*len) {
  1494. rc = 0;
  1495. goto out;
  1496. }
  1497. *names = (char**)kmalloc(sizeof(char*) * *len, GFP_ATOMIC);
  1498. if (!*names)
  1499. goto err;
  1500. memset(*names, 0, sizeof(char*) * *len);
  1501. *values = (int*)kmalloc(sizeof(int) * *len, GFP_ATOMIC);
  1502. if (!*values)
  1503. goto err;
  1504. for (i = 0; i < *len; i++) {
  1505. size_t name_len;
  1506. (*values)[i] = policydb.bool_val_to_struct[i]->state;
  1507. name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
  1508. (*names)[i] = (char*)kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
  1509. if (!(*names)[i])
  1510. goto err;
  1511. strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
  1512. (*names)[i][name_len - 1] = 0;
  1513. }
  1514. rc = 0;
  1515. out:
  1516. POLICY_RDUNLOCK;
  1517. return rc;
  1518. err:
  1519. if (*names) {
  1520. for (i = 0; i < *len; i++)
  1521. if ((*names)[i])
  1522. kfree((*names)[i]);
  1523. }
  1524. if (*values)
  1525. kfree(*values);
  1526. goto out;
  1527. }
  1528. int security_set_bools(int len, int *values)
  1529. {
  1530. int i, rc = 0;
  1531. int lenp, seqno = 0;
  1532. struct cond_node *cur;
  1533. POLICY_WRLOCK;
  1534. lenp = policydb.p_bools.nprim;
  1535. if (len != lenp) {
  1536. rc = -EFAULT;
  1537. goto out;
  1538. }
  1539. printk(KERN_INFO "security: committed booleans { ");
  1540. for (i = 0; i < len; i++) {
  1541. if (values[i]) {
  1542. policydb.bool_val_to_struct[i]->state = 1;
  1543. } else {
  1544. policydb.bool_val_to_struct[i]->state = 0;
  1545. }
  1546. if (i != 0)
  1547. printk(", ");
  1548. printk("%s:%d", policydb.p_bool_val_to_name[i],
  1549. policydb.bool_val_to_struct[i]->state);
  1550. }
  1551. printk(" }\n");
  1552. for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
  1553. rc = evaluate_cond_node(&policydb, cur);
  1554. if (rc)
  1555. goto out;
  1556. }
  1557. seqno = ++latest_granting;
  1558. out:
  1559. POLICY_WRUNLOCK;
  1560. if (!rc) {
  1561. avc_ss_reset(seqno);
  1562. selnl_notify_policyload(seqno);
  1563. }
  1564. return rc;
  1565. }
  1566. int security_get_bool_value(int bool)
  1567. {
  1568. int rc = 0;
  1569. int len;
  1570. POLICY_RDLOCK;
  1571. len = policydb.p_bools.nprim;
  1572. if (bool >= len) {
  1573. rc = -EFAULT;
  1574. goto out;
  1575. }
  1576. rc = policydb.bool_val_to_struct[bool]->state;
  1577. out:
  1578. POLICY_RDUNLOCK;
  1579. return rc;
  1580. }