services.c 47 KB

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