services.c 47 KB

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