services.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585
  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. * Updated: Hewlett-Packard <paul.moore@hp.com>
  17. *
  18. * Added support for NetLabel
  19. *
  20. * Updated: Chad Sellers <csellers@tresys.com>
  21. *
  22. * Added validation of kernel classes and permissions
  23. *
  24. * Copyright (C) 2006 Hewlett-Packard Development Company, L.P.
  25. * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
  26. * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
  27. * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
  28. * This program is free software; you can redistribute it and/or modify
  29. * it under the terms of the GNU General Public License as published by
  30. * the Free Software Foundation, version 2.
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/slab.h>
  34. #include <linux/string.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/rcupdate.h>
  37. #include <linux/errno.h>
  38. #include <linux/in.h>
  39. #include <linux/sched.h>
  40. #include <linux/audit.h>
  41. #include <linux/mutex.h>
  42. #include <net/netlabel.h>
  43. #include "flask.h"
  44. #include "avc.h"
  45. #include "avc_ss.h"
  46. #include "security.h"
  47. #include "context.h"
  48. #include "policydb.h"
  49. #include "sidtab.h"
  50. #include "services.h"
  51. #include "conditional.h"
  52. #include "mls.h"
  53. #include "objsec.h"
  54. #include "netlabel.h"
  55. #include "xfrm.h"
  56. #include "ebitmap.h"
  57. extern void selnl_notify_policyload(u32 seqno);
  58. unsigned int policydb_loaded_version;
  59. /*
  60. * This is declared in avc.c
  61. */
  62. extern const struct selinux_class_perm selinux_class_perm;
  63. static DEFINE_RWLOCK(policy_rwlock);
  64. #define POLICY_RDLOCK read_lock(&policy_rwlock)
  65. #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
  66. #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
  67. #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
  68. static DEFINE_MUTEX(load_mutex);
  69. #define LOAD_LOCK mutex_lock(&load_mutex)
  70. #define LOAD_UNLOCK mutex_unlock(&load_mutex)
  71. static struct sidtab sidtab;
  72. struct policydb policydb;
  73. int ss_initialized = 0;
  74. /*
  75. * The largest sequence number that has been used when
  76. * providing an access decision to the access vector cache.
  77. * The sequence number only changes when a policy change
  78. * occurs.
  79. */
  80. static u32 latest_granting = 0;
  81. /* Forward declaration. */
  82. static int context_struct_to_string(struct context *context, char **scontext,
  83. u32 *scontext_len);
  84. /*
  85. * Return the boolean value of a constraint expression
  86. * when it is applied to the specified source and target
  87. * security contexts.
  88. *
  89. * xcontext is a special beast... It is used by the validatetrans rules
  90. * only. For these rules, scontext is the context before the transition,
  91. * tcontext is the context after the transition, and xcontext is the context
  92. * of the process performing the transition. All other callers of
  93. * constraint_expr_eval should pass in NULL for xcontext.
  94. */
  95. static int constraint_expr_eval(struct context *scontext,
  96. struct context *tcontext,
  97. struct context *xcontext,
  98. struct constraint_expr *cexpr)
  99. {
  100. u32 val1, val2;
  101. struct context *c;
  102. struct role_datum *r1, *r2;
  103. struct mls_level *l1, *l2;
  104. struct constraint_expr *e;
  105. int s[CEXPR_MAXDEPTH];
  106. int sp = -1;
  107. for (e = cexpr; e; e = e->next) {
  108. switch (e->expr_type) {
  109. case CEXPR_NOT:
  110. BUG_ON(sp < 0);
  111. s[sp] = !s[sp];
  112. break;
  113. case CEXPR_AND:
  114. BUG_ON(sp < 1);
  115. sp--;
  116. s[sp] &= s[sp+1];
  117. break;
  118. case CEXPR_OR:
  119. BUG_ON(sp < 1);
  120. sp--;
  121. s[sp] |= s[sp+1];
  122. break;
  123. case CEXPR_ATTR:
  124. if (sp == (CEXPR_MAXDEPTH-1))
  125. return 0;
  126. switch (e->attr) {
  127. case CEXPR_USER:
  128. val1 = scontext->user;
  129. val2 = tcontext->user;
  130. break;
  131. case CEXPR_TYPE:
  132. val1 = scontext->type;
  133. val2 = tcontext->type;
  134. break;
  135. case CEXPR_ROLE:
  136. val1 = scontext->role;
  137. val2 = tcontext->role;
  138. r1 = policydb.role_val_to_struct[val1 - 1];
  139. r2 = policydb.role_val_to_struct[val2 - 1];
  140. switch (e->op) {
  141. case CEXPR_DOM:
  142. s[++sp] = ebitmap_get_bit(&r1->dominates,
  143. val2 - 1);
  144. continue;
  145. case CEXPR_DOMBY:
  146. s[++sp] = ebitmap_get_bit(&r2->dominates,
  147. val1 - 1);
  148. continue;
  149. case CEXPR_INCOMP:
  150. s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
  151. val2 - 1) &&
  152. !ebitmap_get_bit(&r2->dominates,
  153. val1 - 1) );
  154. continue;
  155. default:
  156. break;
  157. }
  158. break;
  159. case CEXPR_L1L2:
  160. l1 = &(scontext->range.level[0]);
  161. l2 = &(tcontext->range.level[0]);
  162. goto mls_ops;
  163. case CEXPR_L1H2:
  164. l1 = &(scontext->range.level[0]);
  165. l2 = &(tcontext->range.level[1]);
  166. goto mls_ops;
  167. case CEXPR_H1L2:
  168. l1 = &(scontext->range.level[1]);
  169. l2 = &(tcontext->range.level[0]);
  170. goto mls_ops;
  171. case CEXPR_H1H2:
  172. l1 = &(scontext->range.level[1]);
  173. l2 = &(tcontext->range.level[1]);
  174. goto mls_ops;
  175. case CEXPR_L1H1:
  176. l1 = &(scontext->range.level[0]);
  177. l2 = &(scontext->range.level[1]);
  178. goto mls_ops;
  179. case CEXPR_L2H2:
  180. l1 = &(tcontext->range.level[0]);
  181. l2 = &(tcontext->range.level[1]);
  182. goto mls_ops;
  183. mls_ops:
  184. switch (e->op) {
  185. case CEXPR_EQ:
  186. s[++sp] = mls_level_eq(l1, l2);
  187. continue;
  188. case CEXPR_NEQ:
  189. s[++sp] = !mls_level_eq(l1, l2);
  190. continue;
  191. case CEXPR_DOM:
  192. s[++sp] = mls_level_dom(l1, l2);
  193. continue;
  194. case CEXPR_DOMBY:
  195. s[++sp] = mls_level_dom(l2, l1);
  196. continue;
  197. case CEXPR_INCOMP:
  198. s[++sp] = mls_level_incomp(l2, l1);
  199. continue;
  200. default:
  201. BUG();
  202. return 0;
  203. }
  204. break;
  205. default:
  206. BUG();
  207. return 0;
  208. }
  209. switch (e->op) {
  210. case CEXPR_EQ:
  211. s[++sp] = (val1 == val2);
  212. break;
  213. case CEXPR_NEQ:
  214. s[++sp] = (val1 != val2);
  215. break;
  216. default:
  217. BUG();
  218. return 0;
  219. }
  220. break;
  221. case CEXPR_NAMES:
  222. if (sp == (CEXPR_MAXDEPTH-1))
  223. return 0;
  224. c = scontext;
  225. if (e->attr & CEXPR_TARGET)
  226. c = tcontext;
  227. else if (e->attr & CEXPR_XTARGET) {
  228. c = xcontext;
  229. if (!c) {
  230. BUG();
  231. return 0;
  232. }
  233. }
  234. if (e->attr & CEXPR_USER)
  235. val1 = c->user;
  236. else if (e->attr & CEXPR_ROLE)
  237. val1 = c->role;
  238. else if (e->attr & CEXPR_TYPE)
  239. val1 = c->type;
  240. else {
  241. BUG();
  242. return 0;
  243. }
  244. switch (e->op) {
  245. case CEXPR_EQ:
  246. s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
  247. break;
  248. case CEXPR_NEQ:
  249. s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
  250. break;
  251. default:
  252. BUG();
  253. return 0;
  254. }
  255. break;
  256. default:
  257. BUG();
  258. return 0;
  259. }
  260. }
  261. BUG_ON(sp != 0);
  262. return s[0];
  263. }
  264. /*
  265. * Compute access vectors based on a context structure pair for
  266. * the permissions in a particular class.
  267. */
  268. static int context_struct_compute_av(struct context *scontext,
  269. struct context *tcontext,
  270. u16 tclass,
  271. u32 requested,
  272. struct av_decision *avd)
  273. {
  274. struct constraint_node *constraint;
  275. struct role_allow *ra;
  276. struct avtab_key avkey;
  277. struct avtab_node *node;
  278. struct class_datum *tclass_datum;
  279. struct ebitmap *sattr, *tattr;
  280. struct ebitmap_node *snode, *tnode;
  281. unsigned int i, j;
  282. /*
  283. * Remap extended Netlink classes for old policy versions.
  284. * Do this here rather than socket_type_to_security_class()
  285. * in case a newer policy version is loaded, allowing sockets
  286. * to remain in the correct class.
  287. */
  288. if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
  289. if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
  290. tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
  291. tclass = SECCLASS_NETLINK_SOCKET;
  292. if (!tclass || tclass > policydb.p_classes.nprim) {
  293. printk(KERN_ERR "security_compute_av: unrecognized class %d\n",
  294. tclass);
  295. return -EINVAL;
  296. }
  297. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  298. /*
  299. * Initialize the access vectors to the default values.
  300. */
  301. avd->allowed = 0;
  302. avd->decided = 0xffffffff;
  303. avd->auditallow = 0;
  304. avd->auditdeny = 0xffffffff;
  305. avd->seqno = latest_granting;
  306. /*
  307. * If a specific type enforcement rule was defined for
  308. * this permission check, then use it.
  309. */
  310. avkey.target_class = tclass;
  311. avkey.specified = AVTAB_AV;
  312. sattr = &policydb.type_attr_map[scontext->type - 1];
  313. tattr = &policydb.type_attr_map[tcontext->type - 1];
  314. ebitmap_for_each_bit(sattr, snode, i) {
  315. if (!ebitmap_node_get_bit(snode, i))
  316. continue;
  317. ebitmap_for_each_bit(tattr, tnode, j) {
  318. if (!ebitmap_node_get_bit(tnode, j))
  319. continue;
  320. avkey.source_type = i + 1;
  321. avkey.target_type = j + 1;
  322. for (node = avtab_search_node(&policydb.te_avtab, &avkey);
  323. node != NULL;
  324. node = avtab_search_node_next(node, avkey.specified)) {
  325. if (node->key.specified == AVTAB_ALLOWED)
  326. avd->allowed |= node->datum.data;
  327. else if (node->key.specified == AVTAB_AUDITALLOW)
  328. avd->auditallow |= node->datum.data;
  329. else if (node->key.specified == AVTAB_AUDITDENY)
  330. avd->auditdeny &= node->datum.data;
  331. }
  332. /* Check conditional av table for additional permissions */
  333. cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
  334. }
  335. }
  336. /*
  337. * Remove any permissions prohibited by a constraint (this includes
  338. * the MLS policy).
  339. */
  340. constraint = tclass_datum->constraints;
  341. while (constraint) {
  342. if ((constraint->permissions & (avd->allowed)) &&
  343. !constraint_expr_eval(scontext, tcontext, NULL,
  344. constraint->expr)) {
  345. avd->allowed = (avd->allowed) & ~(constraint->permissions);
  346. }
  347. constraint = constraint->next;
  348. }
  349. /*
  350. * If checking process transition permission and the
  351. * role is changing, then check the (current_role, new_role)
  352. * pair.
  353. */
  354. if (tclass == SECCLASS_PROCESS &&
  355. (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
  356. scontext->role != tcontext->role) {
  357. for (ra = policydb.role_allow; ra; ra = ra->next) {
  358. if (scontext->role == ra->role &&
  359. tcontext->role == ra->new_role)
  360. break;
  361. }
  362. if (!ra)
  363. avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
  364. PROCESS__DYNTRANSITION);
  365. }
  366. return 0;
  367. }
  368. static int security_validtrans_handle_fail(struct context *ocontext,
  369. struct context *ncontext,
  370. struct context *tcontext,
  371. u16 tclass)
  372. {
  373. char *o = NULL, *n = NULL, *t = NULL;
  374. u32 olen, nlen, tlen;
  375. if (context_struct_to_string(ocontext, &o, &olen) < 0)
  376. goto out;
  377. if (context_struct_to_string(ncontext, &n, &nlen) < 0)
  378. goto out;
  379. if (context_struct_to_string(tcontext, &t, &tlen) < 0)
  380. goto out;
  381. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  382. "security_validate_transition: denied for"
  383. " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
  384. o, n, t, policydb.p_class_val_to_name[tclass-1]);
  385. out:
  386. kfree(o);
  387. kfree(n);
  388. kfree(t);
  389. if (!selinux_enforcing)
  390. return 0;
  391. return -EPERM;
  392. }
  393. int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
  394. u16 tclass)
  395. {
  396. struct context *ocontext;
  397. struct context *ncontext;
  398. struct context *tcontext;
  399. struct class_datum *tclass_datum;
  400. struct constraint_node *constraint;
  401. int rc = 0;
  402. if (!ss_initialized)
  403. return 0;
  404. POLICY_RDLOCK;
  405. /*
  406. * Remap extended Netlink classes for old policy versions.
  407. * Do this here rather than socket_type_to_security_class()
  408. * in case a newer policy version is loaded, allowing sockets
  409. * to remain in the correct class.
  410. */
  411. if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
  412. if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
  413. tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
  414. tclass = SECCLASS_NETLINK_SOCKET;
  415. if (!tclass || tclass > policydb.p_classes.nprim) {
  416. printk(KERN_ERR "security_validate_transition: "
  417. "unrecognized class %d\n", tclass);
  418. rc = -EINVAL;
  419. goto out;
  420. }
  421. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  422. ocontext = sidtab_search(&sidtab, oldsid);
  423. if (!ocontext) {
  424. printk(KERN_ERR "security_validate_transition: "
  425. " unrecognized SID %d\n", oldsid);
  426. rc = -EINVAL;
  427. goto out;
  428. }
  429. ncontext = sidtab_search(&sidtab, newsid);
  430. if (!ncontext) {
  431. printk(KERN_ERR "security_validate_transition: "
  432. " unrecognized SID %d\n", newsid);
  433. rc = -EINVAL;
  434. goto out;
  435. }
  436. tcontext = sidtab_search(&sidtab, tasksid);
  437. if (!tcontext) {
  438. printk(KERN_ERR "security_validate_transition: "
  439. " unrecognized SID %d\n", tasksid);
  440. rc = -EINVAL;
  441. goto out;
  442. }
  443. constraint = tclass_datum->validatetrans;
  444. while (constraint) {
  445. if (!constraint_expr_eval(ocontext, ncontext, tcontext,
  446. constraint->expr)) {
  447. rc = security_validtrans_handle_fail(ocontext, ncontext,
  448. tcontext, tclass);
  449. goto out;
  450. }
  451. constraint = constraint->next;
  452. }
  453. out:
  454. POLICY_RDUNLOCK;
  455. return rc;
  456. }
  457. /**
  458. * security_compute_av - Compute access vector decisions.
  459. * @ssid: source security identifier
  460. * @tsid: target security identifier
  461. * @tclass: target security class
  462. * @requested: requested permissions
  463. * @avd: access vector decisions
  464. *
  465. * Compute a set of access vector decisions based on the
  466. * SID pair (@ssid, @tsid) for the permissions in @tclass.
  467. * Return -%EINVAL if any of the parameters are invalid or %0
  468. * if the access vector decisions were computed successfully.
  469. */
  470. int security_compute_av(u32 ssid,
  471. u32 tsid,
  472. u16 tclass,
  473. u32 requested,
  474. struct av_decision *avd)
  475. {
  476. struct context *scontext = NULL, *tcontext = NULL;
  477. int rc = 0;
  478. if (!ss_initialized) {
  479. avd->allowed = 0xffffffff;
  480. avd->decided = 0xffffffff;
  481. avd->auditallow = 0;
  482. avd->auditdeny = 0xffffffff;
  483. avd->seqno = latest_granting;
  484. return 0;
  485. }
  486. POLICY_RDLOCK;
  487. scontext = sidtab_search(&sidtab, ssid);
  488. if (!scontext) {
  489. printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
  490. ssid);
  491. rc = -EINVAL;
  492. goto out;
  493. }
  494. tcontext = sidtab_search(&sidtab, tsid);
  495. if (!tcontext) {
  496. printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
  497. tsid);
  498. rc = -EINVAL;
  499. goto out;
  500. }
  501. rc = context_struct_compute_av(scontext, tcontext, tclass,
  502. requested, avd);
  503. out:
  504. POLICY_RDUNLOCK;
  505. return rc;
  506. }
  507. /*
  508. * Write the security context string representation of
  509. * the context structure `context' into a dynamically
  510. * allocated string of the correct size. Set `*scontext'
  511. * to point to this string and set `*scontext_len' to
  512. * the length of the string.
  513. */
  514. static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
  515. {
  516. char *scontextp;
  517. *scontext = NULL;
  518. *scontext_len = 0;
  519. /* Compute the size of the context. */
  520. *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
  521. *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
  522. *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
  523. *scontext_len += mls_compute_context_len(context);
  524. /* Allocate space for the context; caller must free this space. */
  525. scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
  526. if (!scontextp) {
  527. return -ENOMEM;
  528. }
  529. *scontext = scontextp;
  530. /*
  531. * Copy the user name, role name and type name into the context.
  532. */
  533. sprintf(scontextp, "%s:%s:%s",
  534. policydb.p_user_val_to_name[context->user - 1],
  535. policydb.p_role_val_to_name[context->role - 1],
  536. policydb.p_type_val_to_name[context->type - 1]);
  537. scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
  538. 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
  539. 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
  540. mls_sid_to_context(context, &scontextp);
  541. *scontextp = 0;
  542. return 0;
  543. }
  544. #include "initial_sid_to_string.h"
  545. const char *security_get_initial_sid_context(u32 sid)
  546. {
  547. if (unlikely(sid > SECINITSID_NUM))
  548. return NULL;
  549. return initial_sid_to_string[sid];
  550. }
  551. /**
  552. * security_sid_to_context - Obtain a context for a given SID.
  553. * @sid: security identifier, SID
  554. * @scontext: security context
  555. * @scontext_len: length in bytes
  556. *
  557. * Write the string representation of the context associated with @sid
  558. * into a dynamically allocated string of the correct size. Set @scontext
  559. * to point to this string and set @scontext_len to the length of the string.
  560. */
  561. int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
  562. {
  563. struct context *context;
  564. int rc = 0;
  565. *scontext = NULL;
  566. *scontext_len = 0;
  567. if (!ss_initialized) {
  568. if (sid <= SECINITSID_NUM) {
  569. char *scontextp;
  570. *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
  571. scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
  572. if (!scontextp) {
  573. rc = -ENOMEM;
  574. goto out;
  575. }
  576. strcpy(scontextp, initial_sid_to_string[sid]);
  577. *scontext = scontextp;
  578. goto out;
  579. }
  580. printk(KERN_ERR "security_sid_to_context: called before initial "
  581. "load_policy on unknown SID %d\n", sid);
  582. rc = -EINVAL;
  583. goto out;
  584. }
  585. POLICY_RDLOCK;
  586. context = sidtab_search(&sidtab, sid);
  587. if (!context) {
  588. printk(KERN_ERR "security_sid_to_context: unrecognized SID "
  589. "%d\n", sid);
  590. rc = -EINVAL;
  591. goto out_unlock;
  592. }
  593. rc = context_struct_to_string(context, scontext, scontext_len);
  594. out_unlock:
  595. POLICY_RDUNLOCK;
  596. out:
  597. return rc;
  598. }
  599. static int security_context_to_sid_core(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
  600. {
  601. char *scontext2;
  602. struct context context;
  603. struct role_datum *role;
  604. struct type_datum *typdatum;
  605. struct user_datum *usrdatum;
  606. char *scontextp, *p, oldc;
  607. int rc = 0;
  608. if (!ss_initialized) {
  609. int i;
  610. for (i = 1; i < SECINITSID_NUM; i++) {
  611. if (!strcmp(initial_sid_to_string[i], scontext)) {
  612. *sid = i;
  613. goto out;
  614. }
  615. }
  616. *sid = SECINITSID_KERNEL;
  617. goto out;
  618. }
  619. *sid = SECSID_NULL;
  620. /* Copy the string so that we can modify the copy as we parse it.
  621. The string should already by null terminated, but we append a
  622. null suffix to the copy to avoid problems with the existing
  623. attr package, which doesn't view the null terminator as part
  624. of the attribute value. */
  625. scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
  626. if (!scontext2) {
  627. rc = -ENOMEM;
  628. goto out;
  629. }
  630. memcpy(scontext2, scontext, scontext_len);
  631. scontext2[scontext_len] = 0;
  632. context_init(&context);
  633. *sid = SECSID_NULL;
  634. POLICY_RDLOCK;
  635. /* Parse the security context. */
  636. rc = -EINVAL;
  637. scontextp = (char *) scontext2;
  638. /* Extract the user. */
  639. p = scontextp;
  640. while (*p && *p != ':')
  641. p++;
  642. if (*p == 0)
  643. goto out_unlock;
  644. *p++ = 0;
  645. usrdatum = hashtab_search(policydb.p_users.table, scontextp);
  646. if (!usrdatum)
  647. goto out_unlock;
  648. context.user = usrdatum->value;
  649. /* Extract role. */
  650. scontextp = p;
  651. while (*p && *p != ':')
  652. p++;
  653. if (*p == 0)
  654. goto out_unlock;
  655. *p++ = 0;
  656. role = hashtab_search(policydb.p_roles.table, scontextp);
  657. if (!role)
  658. goto out_unlock;
  659. context.role = role->value;
  660. /* Extract type. */
  661. scontextp = p;
  662. while (*p && *p != ':')
  663. p++;
  664. oldc = *p;
  665. *p++ = 0;
  666. typdatum = hashtab_search(policydb.p_types.table, scontextp);
  667. if (!typdatum)
  668. goto out_unlock;
  669. context.type = typdatum->value;
  670. rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
  671. if (rc)
  672. goto out_unlock;
  673. if ((p - scontext2) < scontext_len) {
  674. rc = -EINVAL;
  675. goto out_unlock;
  676. }
  677. /* Check the validity of the new context. */
  678. if (!policydb_context_isvalid(&policydb, &context)) {
  679. rc = -EINVAL;
  680. goto out_unlock;
  681. }
  682. /* Obtain the new sid. */
  683. rc = sidtab_context_to_sid(&sidtab, &context, sid);
  684. out_unlock:
  685. POLICY_RDUNLOCK;
  686. context_destroy(&context);
  687. kfree(scontext2);
  688. out:
  689. return rc;
  690. }
  691. /**
  692. * security_context_to_sid - Obtain a SID for a given security context.
  693. * @scontext: security context
  694. * @scontext_len: length in bytes
  695. * @sid: security identifier, SID
  696. *
  697. * Obtains a SID associated with the security context that
  698. * has the string representation specified by @scontext.
  699. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  700. * memory is available, or 0 on success.
  701. */
  702. int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
  703. {
  704. return security_context_to_sid_core(scontext, scontext_len,
  705. sid, SECSID_NULL);
  706. }
  707. /**
  708. * security_context_to_sid_default - Obtain a SID for a given security context,
  709. * falling back to specified default if needed.
  710. *
  711. * @scontext: security context
  712. * @scontext_len: length in bytes
  713. * @sid: security identifier, SID
  714. * @def_sid: default SID to assign on error
  715. *
  716. * Obtains a SID associated with the security context that
  717. * has the string representation specified by @scontext.
  718. * The default SID is passed to the MLS layer to be used to allow
  719. * kernel labeling of the MLS field if the MLS field is not present
  720. * (for upgrading to MLS without full relabel).
  721. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  722. * memory is available, or 0 on success.
  723. */
  724. int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
  725. {
  726. return security_context_to_sid_core(scontext, scontext_len,
  727. sid, def_sid);
  728. }
  729. static int compute_sid_handle_invalid_context(
  730. struct context *scontext,
  731. struct context *tcontext,
  732. u16 tclass,
  733. struct context *newcontext)
  734. {
  735. char *s = NULL, *t = NULL, *n = NULL;
  736. u32 slen, tlen, nlen;
  737. if (context_struct_to_string(scontext, &s, &slen) < 0)
  738. goto out;
  739. if (context_struct_to_string(tcontext, &t, &tlen) < 0)
  740. goto out;
  741. if (context_struct_to_string(newcontext, &n, &nlen) < 0)
  742. goto out;
  743. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  744. "security_compute_sid: invalid context %s"
  745. " for scontext=%s"
  746. " tcontext=%s"
  747. " tclass=%s",
  748. n, s, t, policydb.p_class_val_to_name[tclass-1]);
  749. out:
  750. kfree(s);
  751. kfree(t);
  752. kfree(n);
  753. if (!selinux_enforcing)
  754. return 0;
  755. return -EACCES;
  756. }
  757. static int security_compute_sid(u32 ssid,
  758. u32 tsid,
  759. u16 tclass,
  760. u32 specified,
  761. u32 *out_sid)
  762. {
  763. struct context *scontext = NULL, *tcontext = NULL, newcontext;
  764. struct role_trans *roletr = NULL;
  765. struct avtab_key avkey;
  766. struct avtab_datum *avdatum;
  767. struct avtab_node *node;
  768. int rc = 0;
  769. if (!ss_initialized) {
  770. switch (tclass) {
  771. case SECCLASS_PROCESS:
  772. *out_sid = ssid;
  773. break;
  774. default:
  775. *out_sid = tsid;
  776. break;
  777. }
  778. goto out;
  779. }
  780. context_init(&newcontext);
  781. POLICY_RDLOCK;
  782. scontext = sidtab_search(&sidtab, ssid);
  783. if (!scontext) {
  784. printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
  785. ssid);
  786. rc = -EINVAL;
  787. goto out_unlock;
  788. }
  789. tcontext = sidtab_search(&sidtab, tsid);
  790. if (!tcontext) {
  791. printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
  792. tsid);
  793. rc = -EINVAL;
  794. goto out_unlock;
  795. }
  796. /* Set the user identity. */
  797. switch (specified) {
  798. case AVTAB_TRANSITION:
  799. case AVTAB_CHANGE:
  800. /* Use the process user identity. */
  801. newcontext.user = scontext->user;
  802. break;
  803. case AVTAB_MEMBER:
  804. /* Use the related object owner. */
  805. newcontext.user = tcontext->user;
  806. break;
  807. }
  808. /* Set the role and type to default values. */
  809. switch (tclass) {
  810. case SECCLASS_PROCESS:
  811. /* Use the current role and type of process. */
  812. newcontext.role = scontext->role;
  813. newcontext.type = scontext->type;
  814. break;
  815. default:
  816. /* Use the well-defined object role. */
  817. newcontext.role = OBJECT_R_VAL;
  818. /* Use the type of the related object. */
  819. newcontext.type = tcontext->type;
  820. }
  821. /* Look for a type transition/member/change rule. */
  822. avkey.source_type = scontext->type;
  823. avkey.target_type = tcontext->type;
  824. avkey.target_class = tclass;
  825. avkey.specified = specified;
  826. avdatum = avtab_search(&policydb.te_avtab, &avkey);
  827. /* If no permanent rule, also check for enabled conditional rules */
  828. if(!avdatum) {
  829. node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
  830. for (; node != NULL; node = avtab_search_node_next(node, specified)) {
  831. if (node->key.specified & AVTAB_ENABLED) {
  832. avdatum = &node->datum;
  833. break;
  834. }
  835. }
  836. }
  837. if (avdatum) {
  838. /* Use the type from the type transition/member/change rule. */
  839. newcontext.type = avdatum->data;
  840. }
  841. /* Check for class-specific changes. */
  842. switch (tclass) {
  843. case SECCLASS_PROCESS:
  844. if (specified & AVTAB_TRANSITION) {
  845. /* Look for a role transition rule. */
  846. for (roletr = policydb.role_tr; roletr;
  847. roletr = roletr->next) {
  848. if (roletr->role == scontext->role &&
  849. roletr->type == tcontext->type) {
  850. /* Use the role transition rule. */
  851. newcontext.role = roletr->new_role;
  852. break;
  853. }
  854. }
  855. }
  856. break;
  857. default:
  858. break;
  859. }
  860. /* Set the MLS attributes.
  861. This is done last because it may allocate memory. */
  862. rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
  863. if (rc)
  864. goto out_unlock;
  865. /* Check the validity of the context. */
  866. if (!policydb_context_isvalid(&policydb, &newcontext)) {
  867. rc = compute_sid_handle_invalid_context(scontext,
  868. tcontext,
  869. tclass,
  870. &newcontext);
  871. if (rc)
  872. goto out_unlock;
  873. }
  874. /* Obtain the sid for the context. */
  875. rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
  876. out_unlock:
  877. POLICY_RDUNLOCK;
  878. context_destroy(&newcontext);
  879. out:
  880. return rc;
  881. }
  882. /**
  883. * security_transition_sid - Compute the SID for a new subject/object.
  884. * @ssid: source security identifier
  885. * @tsid: target security identifier
  886. * @tclass: target security class
  887. * @out_sid: security identifier for new subject/object
  888. *
  889. * Compute a SID to use for labeling a new subject or object in the
  890. * class @tclass based on a SID pair (@ssid, @tsid).
  891. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  892. * if insufficient memory is available, or %0 if the new SID was
  893. * computed successfully.
  894. */
  895. int security_transition_sid(u32 ssid,
  896. u32 tsid,
  897. u16 tclass,
  898. u32 *out_sid)
  899. {
  900. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
  901. }
  902. /**
  903. * security_member_sid - Compute the SID for member selection.
  904. * @ssid: source security identifier
  905. * @tsid: target security identifier
  906. * @tclass: target security class
  907. * @out_sid: security identifier for selected member
  908. *
  909. * Compute a SID to use when selecting a member of a polyinstantiated
  910. * object of class @tclass based on a SID pair (@ssid, @tsid).
  911. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  912. * if insufficient memory is available, or %0 if the SID was
  913. * computed successfully.
  914. */
  915. int security_member_sid(u32 ssid,
  916. u32 tsid,
  917. u16 tclass,
  918. u32 *out_sid)
  919. {
  920. return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
  921. }
  922. /**
  923. * security_change_sid - Compute the SID for object relabeling.
  924. * @ssid: source security identifier
  925. * @tsid: target security identifier
  926. * @tclass: target security class
  927. * @out_sid: security identifier for selected member
  928. *
  929. * Compute a SID to use for relabeling an object of class @tclass
  930. * based on a SID pair (@ssid, @tsid).
  931. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  932. * if insufficient memory is available, or %0 if the SID was
  933. * computed successfully.
  934. */
  935. int security_change_sid(u32 ssid,
  936. u32 tsid,
  937. u16 tclass,
  938. u32 *out_sid)
  939. {
  940. return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
  941. }
  942. /*
  943. * Verify that each kernel class that is defined in the
  944. * policy is correct
  945. */
  946. static int validate_classes(struct policydb *p)
  947. {
  948. int i, j;
  949. struct class_datum *cladatum;
  950. struct perm_datum *perdatum;
  951. u32 nprim, tmp, common_pts_len, perm_val, pol_val;
  952. u16 class_val;
  953. const struct selinux_class_perm *kdefs = &selinux_class_perm;
  954. const char *def_class, *def_perm, *pol_class;
  955. struct symtab *perms;
  956. for (i = 1; i < kdefs->cts_len; i++) {
  957. def_class = kdefs->class_to_string[i];
  958. if (!def_class)
  959. continue;
  960. if (i > p->p_classes.nprim) {
  961. printk(KERN_INFO
  962. "security: class %s not defined in policy\n",
  963. def_class);
  964. continue;
  965. }
  966. pol_class = p->p_class_val_to_name[i-1];
  967. if (strcmp(pol_class, def_class)) {
  968. printk(KERN_ERR
  969. "security: class %d is incorrect, found %s but should be %s\n",
  970. i, pol_class, def_class);
  971. return -EINVAL;
  972. }
  973. }
  974. for (i = 0; i < kdefs->av_pts_len; i++) {
  975. class_val = kdefs->av_perm_to_string[i].tclass;
  976. perm_val = kdefs->av_perm_to_string[i].value;
  977. def_perm = kdefs->av_perm_to_string[i].name;
  978. if (class_val > p->p_classes.nprim)
  979. continue;
  980. pol_class = p->p_class_val_to_name[class_val-1];
  981. cladatum = hashtab_search(p->p_classes.table, pol_class);
  982. BUG_ON(!cladatum);
  983. perms = &cladatum->permissions;
  984. nprim = 1 << (perms->nprim - 1);
  985. if (perm_val > nprim) {
  986. printk(KERN_INFO
  987. "security: permission %s in class %s not defined in policy\n",
  988. def_perm, pol_class);
  989. continue;
  990. }
  991. perdatum = hashtab_search(perms->table, def_perm);
  992. if (perdatum == NULL) {
  993. printk(KERN_ERR
  994. "security: permission %s in class %s not found in policy\n",
  995. def_perm, pol_class);
  996. return -EINVAL;
  997. }
  998. pol_val = 1 << (perdatum->value - 1);
  999. if (pol_val != perm_val) {
  1000. printk(KERN_ERR
  1001. "security: permission %s in class %s has incorrect value\n",
  1002. def_perm, pol_class);
  1003. return -EINVAL;
  1004. }
  1005. }
  1006. for (i = 0; i < kdefs->av_inherit_len; i++) {
  1007. class_val = kdefs->av_inherit[i].tclass;
  1008. if (class_val > p->p_classes.nprim)
  1009. continue;
  1010. pol_class = p->p_class_val_to_name[class_val-1];
  1011. cladatum = hashtab_search(p->p_classes.table, pol_class);
  1012. BUG_ON(!cladatum);
  1013. if (!cladatum->comdatum) {
  1014. printk(KERN_ERR
  1015. "security: class %s should have an inherits clause but does not\n",
  1016. pol_class);
  1017. return -EINVAL;
  1018. }
  1019. tmp = kdefs->av_inherit[i].common_base;
  1020. common_pts_len = 0;
  1021. while (!(tmp & 0x01)) {
  1022. common_pts_len++;
  1023. tmp >>= 1;
  1024. }
  1025. perms = &cladatum->comdatum->permissions;
  1026. for (j = 0; j < common_pts_len; j++) {
  1027. def_perm = kdefs->av_inherit[i].common_pts[j];
  1028. if (j >= perms->nprim) {
  1029. printk(KERN_INFO
  1030. "security: permission %s in class %s not defined in policy\n",
  1031. def_perm, pol_class);
  1032. continue;
  1033. }
  1034. perdatum = hashtab_search(perms->table, def_perm);
  1035. if (perdatum == NULL) {
  1036. printk(KERN_ERR
  1037. "security: permission %s in class %s not found in policy\n",
  1038. def_perm, pol_class);
  1039. return -EINVAL;
  1040. }
  1041. if (perdatum->value != j + 1) {
  1042. printk(KERN_ERR
  1043. "security: permission %s in class %s has incorrect value\n",
  1044. def_perm, pol_class);
  1045. return -EINVAL;
  1046. }
  1047. }
  1048. }
  1049. return 0;
  1050. }
  1051. /* Clone the SID into the new SID table. */
  1052. static int clone_sid(u32 sid,
  1053. struct context *context,
  1054. void *arg)
  1055. {
  1056. struct sidtab *s = arg;
  1057. return sidtab_insert(s, sid, context);
  1058. }
  1059. static inline int convert_context_handle_invalid_context(struct context *context)
  1060. {
  1061. int rc = 0;
  1062. if (selinux_enforcing) {
  1063. rc = -EINVAL;
  1064. } else {
  1065. char *s;
  1066. u32 len;
  1067. context_struct_to_string(context, &s, &len);
  1068. printk(KERN_ERR "security: context %s is invalid\n", s);
  1069. kfree(s);
  1070. }
  1071. return rc;
  1072. }
  1073. struct convert_context_args {
  1074. struct policydb *oldp;
  1075. struct policydb *newp;
  1076. };
  1077. /*
  1078. * Convert the values in the security context
  1079. * structure `c' from the values specified
  1080. * in the policy `p->oldp' to the values specified
  1081. * in the policy `p->newp'. Verify that the
  1082. * context is valid under the new policy.
  1083. */
  1084. static int convert_context(u32 key,
  1085. struct context *c,
  1086. void *p)
  1087. {
  1088. struct convert_context_args *args;
  1089. struct context oldc;
  1090. struct role_datum *role;
  1091. struct type_datum *typdatum;
  1092. struct user_datum *usrdatum;
  1093. char *s;
  1094. u32 len;
  1095. int rc;
  1096. args = p;
  1097. rc = context_cpy(&oldc, c);
  1098. if (rc)
  1099. goto out;
  1100. rc = -EINVAL;
  1101. /* Convert the user. */
  1102. usrdatum = hashtab_search(args->newp->p_users.table,
  1103. args->oldp->p_user_val_to_name[c->user - 1]);
  1104. if (!usrdatum) {
  1105. goto bad;
  1106. }
  1107. c->user = usrdatum->value;
  1108. /* Convert the role. */
  1109. role = hashtab_search(args->newp->p_roles.table,
  1110. args->oldp->p_role_val_to_name[c->role - 1]);
  1111. if (!role) {
  1112. goto bad;
  1113. }
  1114. c->role = role->value;
  1115. /* Convert the type. */
  1116. typdatum = hashtab_search(args->newp->p_types.table,
  1117. args->oldp->p_type_val_to_name[c->type - 1]);
  1118. if (!typdatum) {
  1119. goto bad;
  1120. }
  1121. c->type = typdatum->value;
  1122. rc = mls_convert_context(args->oldp, args->newp, c);
  1123. if (rc)
  1124. goto bad;
  1125. /* Check the validity of the new context. */
  1126. if (!policydb_context_isvalid(args->newp, c)) {
  1127. rc = convert_context_handle_invalid_context(&oldc);
  1128. if (rc)
  1129. goto bad;
  1130. }
  1131. context_destroy(&oldc);
  1132. out:
  1133. return rc;
  1134. bad:
  1135. context_struct_to_string(&oldc, &s, &len);
  1136. context_destroy(&oldc);
  1137. printk(KERN_ERR "security: invalidating context %s\n", s);
  1138. kfree(s);
  1139. goto out;
  1140. }
  1141. extern void selinux_complete_init(void);
  1142. static int security_preserve_bools(struct policydb *p);
  1143. /**
  1144. * security_load_policy - Load a security policy configuration.
  1145. * @data: binary policy data
  1146. * @len: length of data in bytes
  1147. *
  1148. * Load a new set of security policy configuration data,
  1149. * validate it and convert the SID table as necessary.
  1150. * This function will flush the access vector cache after
  1151. * loading the new policy.
  1152. */
  1153. int security_load_policy(void *data, size_t len)
  1154. {
  1155. struct policydb oldpolicydb, newpolicydb;
  1156. struct sidtab oldsidtab, newsidtab;
  1157. struct convert_context_args args;
  1158. u32 seqno;
  1159. int rc = 0;
  1160. struct policy_file file = { data, len }, *fp = &file;
  1161. LOAD_LOCK;
  1162. if (!ss_initialized) {
  1163. avtab_cache_init();
  1164. if (policydb_read(&policydb, fp)) {
  1165. LOAD_UNLOCK;
  1166. avtab_cache_destroy();
  1167. return -EINVAL;
  1168. }
  1169. if (policydb_load_isids(&policydb, &sidtab)) {
  1170. LOAD_UNLOCK;
  1171. policydb_destroy(&policydb);
  1172. avtab_cache_destroy();
  1173. return -EINVAL;
  1174. }
  1175. /* Verify that the kernel defined classes are correct. */
  1176. if (validate_classes(&policydb)) {
  1177. printk(KERN_ERR
  1178. "security: the definition of a class is incorrect\n");
  1179. LOAD_UNLOCK;
  1180. sidtab_destroy(&sidtab);
  1181. policydb_destroy(&policydb);
  1182. avtab_cache_destroy();
  1183. return -EINVAL;
  1184. }
  1185. policydb_loaded_version = policydb.policyvers;
  1186. ss_initialized = 1;
  1187. seqno = ++latest_granting;
  1188. LOAD_UNLOCK;
  1189. selinux_complete_init();
  1190. avc_ss_reset(seqno);
  1191. selnl_notify_policyload(seqno);
  1192. selinux_netlbl_cache_invalidate();
  1193. selinux_xfrm_notify_policyload();
  1194. return 0;
  1195. }
  1196. #if 0
  1197. sidtab_hash_eval(&sidtab, "sids");
  1198. #endif
  1199. if (policydb_read(&newpolicydb, fp)) {
  1200. LOAD_UNLOCK;
  1201. return -EINVAL;
  1202. }
  1203. sidtab_init(&newsidtab);
  1204. /* Verify that the kernel defined classes are correct. */
  1205. if (validate_classes(&newpolicydb)) {
  1206. printk(KERN_ERR
  1207. "security: the definition of a class is incorrect\n");
  1208. rc = -EINVAL;
  1209. goto err;
  1210. }
  1211. rc = security_preserve_bools(&newpolicydb);
  1212. if (rc) {
  1213. printk(KERN_ERR "security: unable to preserve booleans\n");
  1214. goto err;
  1215. }
  1216. /* Clone the SID table. */
  1217. sidtab_shutdown(&sidtab);
  1218. if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
  1219. rc = -ENOMEM;
  1220. goto err;
  1221. }
  1222. /* Convert the internal representations of contexts
  1223. in the new SID table and remove invalid SIDs. */
  1224. args.oldp = &policydb;
  1225. args.newp = &newpolicydb;
  1226. sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
  1227. /* Save the old policydb and SID table to free later. */
  1228. memcpy(&oldpolicydb, &policydb, sizeof policydb);
  1229. sidtab_set(&oldsidtab, &sidtab);
  1230. /* Install the new policydb and SID table. */
  1231. POLICY_WRLOCK;
  1232. memcpy(&policydb, &newpolicydb, sizeof policydb);
  1233. sidtab_set(&sidtab, &newsidtab);
  1234. seqno = ++latest_granting;
  1235. policydb_loaded_version = policydb.policyvers;
  1236. POLICY_WRUNLOCK;
  1237. LOAD_UNLOCK;
  1238. /* Free the old policydb and SID table. */
  1239. policydb_destroy(&oldpolicydb);
  1240. sidtab_destroy(&oldsidtab);
  1241. avc_ss_reset(seqno);
  1242. selnl_notify_policyload(seqno);
  1243. selinux_netlbl_cache_invalidate();
  1244. selinux_xfrm_notify_policyload();
  1245. return 0;
  1246. err:
  1247. LOAD_UNLOCK;
  1248. sidtab_destroy(&newsidtab);
  1249. policydb_destroy(&newpolicydb);
  1250. return rc;
  1251. }
  1252. /**
  1253. * security_port_sid - Obtain the SID for a port.
  1254. * @domain: communication domain aka address family
  1255. * @type: socket type
  1256. * @protocol: protocol number
  1257. * @port: port number
  1258. * @out_sid: security identifier
  1259. */
  1260. int security_port_sid(u16 domain,
  1261. u16 type,
  1262. u8 protocol,
  1263. u16 port,
  1264. u32 *out_sid)
  1265. {
  1266. struct ocontext *c;
  1267. int rc = 0;
  1268. POLICY_RDLOCK;
  1269. c = policydb.ocontexts[OCON_PORT];
  1270. while (c) {
  1271. if (c->u.port.protocol == protocol &&
  1272. c->u.port.low_port <= port &&
  1273. c->u.port.high_port >= port)
  1274. break;
  1275. c = c->next;
  1276. }
  1277. if (c) {
  1278. if (!c->sid[0]) {
  1279. rc = sidtab_context_to_sid(&sidtab,
  1280. &c->context[0],
  1281. &c->sid[0]);
  1282. if (rc)
  1283. goto out;
  1284. }
  1285. *out_sid = c->sid[0];
  1286. } else {
  1287. *out_sid = SECINITSID_PORT;
  1288. }
  1289. out:
  1290. POLICY_RDUNLOCK;
  1291. return rc;
  1292. }
  1293. /**
  1294. * security_netif_sid - Obtain the SID for a network interface.
  1295. * @name: interface name
  1296. * @if_sid: interface SID
  1297. * @msg_sid: default SID for received packets
  1298. */
  1299. int security_netif_sid(char *name,
  1300. u32 *if_sid,
  1301. u32 *msg_sid)
  1302. {
  1303. int rc = 0;
  1304. struct ocontext *c;
  1305. POLICY_RDLOCK;
  1306. c = policydb.ocontexts[OCON_NETIF];
  1307. while (c) {
  1308. if (strcmp(name, c->u.name) == 0)
  1309. break;
  1310. c = c->next;
  1311. }
  1312. if (c) {
  1313. if (!c->sid[0] || !c->sid[1]) {
  1314. rc = sidtab_context_to_sid(&sidtab,
  1315. &c->context[0],
  1316. &c->sid[0]);
  1317. if (rc)
  1318. goto out;
  1319. rc = sidtab_context_to_sid(&sidtab,
  1320. &c->context[1],
  1321. &c->sid[1]);
  1322. if (rc)
  1323. goto out;
  1324. }
  1325. *if_sid = c->sid[0];
  1326. *msg_sid = c->sid[1];
  1327. } else {
  1328. *if_sid = SECINITSID_NETIF;
  1329. *msg_sid = SECINITSID_NETMSG;
  1330. }
  1331. out:
  1332. POLICY_RDUNLOCK;
  1333. return rc;
  1334. }
  1335. static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
  1336. {
  1337. int i, fail = 0;
  1338. for(i = 0; i < 4; i++)
  1339. if(addr[i] != (input[i] & mask[i])) {
  1340. fail = 1;
  1341. break;
  1342. }
  1343. return !fail;
  1344. }
  1345. /**
  1346. * security_node_sid - Obtain the SID for a node (host).
  1347. * @domain: communication domain aka address family
  1348. * @addrp: address
  1349. * @addrlen: address length in bytes
  1350. * @out_sid: security identifier
  1351. */
  1352. int security_node_sid(u16 domain,
  1353. void *addrp,
  1354. u32 addrlen,
  1355. u32 *out_sid)
  1356. {
  1357. int rc = 0;
  1358. struct ocontext *c;
  1359. POLICY_RDLOCK;
  1360. switch (domain) {
  1361. case AF_INET: {
  1362. u32 addr;
  1363. if (addrlen != sizeof(u32)) {
  1364. rc = -EINVAL;
  1365. goto out;
  1366. }
  1367. addr = *((u32 *)addrp);
  1368. c = policydb.ocontexts[OCON_NODE];
  1369. while (c) {
  1370. if (c->u.node.addr == (addr & c->u.node.mask))
  1371. break;
  1372. c = c->next;
  1373. }
  1374. break;
  1375. }
  1376. case AF_INET6:
  1377. if (addrlen != sizeof(u64) * 2) {
  1378. rc = -EINVAL;
  1379. goto out;
  1380. }
  1381. c = policydb.ocontexts[OCON_NODE6];
  1382. while (c) {
  1383. if (match_ipv6_addrmask(addrp, c->u.node6.addr,
  1384. c->u.node6.mask))
  1385. break;
  1386. c = c->next;
  1387. }
  1388. break;
  1389. default:
  1390. *out_sid = SECINITSID_NODE;
  1391. goto out;
  1392. }
  1393. if (c) {
  1394. if (!c->sid[0]) {
  1395. rc = sidtab_context_to_sid(&sidtab,
  1396. &c->context[0],
  1397. &c->sid[0]);
  1398. if (rc)
  1399. goto out;
  1400. }
  1401. *out_sid = c->sid[0];
  1402. } else {
  1403. *out_sid = SECINITSID_NODE;
  1404. }
  1405. out:
  1406. POLICY_RDUNLOCK;
  1407. return rc;
  1408. }
  1409. #define SIDS_NEL 25
  1410. /**
  1411. * security_get_user_sids - Obtain reachable SIDs for a user.
  1412. * @fromsid: starting SID
  1413. * @username: username
  1414. * @sids: array of reachable SIDs for user
  1415. * @nel: number of elements in @sids
  1416. *
  1417. * Generate the set of SIDs for legal security contexts
  1418. * for a given user that can be reached by @fromsid.
  1419. * Set *@sids to point to a dynamically allocated
  1420. * array containing the set of SIDs. Set *@nel to the
  1421. * number of elements in the array.
  1422. */
  1423. int security_get_user_sids(u32 fromsid,
  1424. char *username,
  1425. u32 **sids,
  1426. u32 *nel)
  1427. {
  1428. struct context *fromcon, usercon;
  1429. u32 *mysids = NULL, *mysids2, sid;
  1430. u32 mynel = 0, maxnel = SIDS_NEL;
  1431. struct user_datum *user;
  1432. struct role_datum *role;
  1433. struct ebitmap_node *rnode, *tnode;
  1434. int rc = 0, i, j;
  1435. *sids = NULL;
  1436. *nel = 0;
  1437. if (!ss_initialized)
  1438. goto out;
  1439. POLICY_RDLOCK;
  1440. fromcon = sidtab_search(&sidtab, fromsid);
  1441. if (!fromcon) {
  1442. rc = -EINVAL;
  1443. goto out_unlock;
  1444. }
  1445. user = hashtab_search(policydb.p_users.table, username);
  1446. if (!user) {
  1447. rc = -EINVAL;
  1448. goto out_unlock;
  1449. }
  1450. usercon.user = user->value;
  1451. mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
  1452. if (!mysids) {
  1453. rc = -ENOMEM;
  1454. goto out_unlock;
  1455. }
  1456. ebitmap_for_each_bit(&user->roles, rnode, i) {
  1457. if (!ebitmap_node_get_bit(rnode, i))
  1458. continue;
  1459. role = policydb.role_val_to_struct[i];
  1460. usercon.role = i+1;
  1461. ebitmap_for_each_bit(&role->types, tnode, j) {
  1462. if (!ebitmap_node_get_bit(tnode, j))
  1463. continue;
  1464. usercon.type = j+1;
  1465. if (mls_setup_user_range(fromcon, user, &usercon))
  1466. continue;
  1467. rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
  1468. if (rc)
  1469. goto out_unlock;
  1470. if (mynel < maxnel) {
  1471. mysids[mynel++] = sid;
  1472. } else {
  1473. maxnel += SIDS_NEL;
  1474. mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
  1475. if (!mysids2) {
  1476. rc = -ENOMEM;
  1477. goto out_unlock;
  1478. }
  1479. memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
  1480. kfree(mysids);
  1481. mysids = mysids2;
  1482. mysids[mynel++] = sid;
  1483. }
  1484. }
  1485. }
  1486. out_unlock:
  1487. POLICY_RDUNLOCK;
  1488. if (rc || !mynel) {
  1489. kfree(mysids);
  1490. goto out;
  1491. }
  1492. mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
  1493. if (!mysids2) {
  1494. rc = -ENOMEM;
  1495. kfree(mysids);
  1496. goto out;
  1497. }
  1498. for (i = 0, j = 0; i < mynel; i++) {
  1499. rc = avc_has_perm_noaudit(fromsid, mysids[i],
  1500. SECCLASS_PROCESS,
  1501. PROCESS__TRANSITION, AVC_STRICT,
  1502. NULL);
  1503. if (!rc)
  1504. mysids2[j++] = mysids[i];
  1505. cond_resched();
  1506. }
  1507. rc = 0;
  1508. kfree(mysids);
  1509. *sids = mysids2;
  1510. *nel = j;
  1511. out:
  1512. return rc;
  1513. }
  1514. /**
  1515. * security_genfs_sid - Obtain a SID for a file in a filesystem
  1516. * @fstype: filesystem type
  1517. * @path: path from root of mount
  1518. * @sclass: file security class
  1519. * @sid: SID for path
  1520. *
  1521. * Obtain a SID to use for a file in a filesystem that
  1522. * cannot support xattr or use a fixed labeling behavior like
  1523. * transition SIDs or task SIDs.
  1524. */
  1525. int security_genfs_sid(const char *fstype,
  1526. char *path,
  1527. u16 sclass,
  1528. u32 *sid)
  1529. {
  1530. int len;
  1531. struct genfs *genfs;
  1532. struct ocontext *c;
  1533. int rc = 0, cmp = 0;
  1534. POLICY_RDLOCK;
  1535. for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
  1536. cmp = strcmp(fstype, genfs->fstype);
  1537. if (cmp <= 0)
  1538. break;
  1539. }
  1540. if (!genfs || cmp) {
  1541. *sid = SECINITSID_UNLABELED;
  1542. rc = -ENOENT;
  1543. goto out;
  1544. }
  1545. for (c = genfs->head; c; c = c->next) {
  1546. len = strlen(c->u.name);
  1547. if ((!c->v.sclass || sclass == c->v.sclass) &&
  1548. (strncmp(c->u.name, path, len) == 0))
  1549. break;
  1550. }
  1551. if (!c) {
  1552. *sid = SECINITSID_UNLABELED;
  1553. rc = -ENOENT;
  1554. goto out;
  1555. }
  1556. if (!c->sid[0]) {
  1557. rc = sidtab_context_to_sid(&sidtab,
  1558. &c->context[0],
  1559. &c->sid[0]);
  1560. if (rc)
  1561. goto out;
  1562. }
  1563. *sid = c->sid[0];
  1564. out:
  1565. POLICY_RDUNLOCK;
  1566. return rc;
  1567. }
  1568. /**
  1569. * security_fs_use - Determine how to handle labeling for a filesystem.
  1570. * @fstype: filesystem type
  1571. * @behavior: labeling behavior
  1572. * @sid: SID for filesystem (superblock)
  1573. */
  1574. int security_fs_use(
  1575. const char *fstype,
  1576. unsigned int *behavior,
  1577. u32 *sid)
  1578. {
  1579. int rc = 0;
  1580. struct ocontext *c;
  1581. POLICY_RDLOCK;
  1582. c = policydb.ocontexts[OCON_FSUSE];
  1583. while (c) {
  1584. if (strcmp(fstype, c->u.name) == 0)
  1585. break;
  1586. c = c->next;
  1587. }
  1588. if (c) {
  1589. *behavior = c->v.behavior;
  1590. if (!c->sid[0]) {
  1591. rc = sidtab_context_to_sid(&sidtab,
  1592. &c->context[0],
  1593. &c->sid[0]);
  1594. if (rc)
  1595. goto out;
  1596. }
  1597. *sid = c->sid[0];
  1598. } else {
  1599. rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
  1600. if (rc) {
  1601. *behavior = SECURITY_FS_USE_NONE;
  1602. rc = 0;
  1603. } else {
  1604. *behavior = SECURITY_FS_USE_GENFS;
  1605. }
  1606. }
  1607. out:
  1608. POLICY_RDUNLOCK;
  1609. return rc;
  1610. }
  1611. int security_get_bools(int *len, char ***names, int **values)
  1612. {
  1613. int i, rc = -ENOMEM;
  1614. POLICY_RDLOCK;
  1615. *names = NULL;
  1616. *values = NULL;
  1617. *len = policydb.p_bools.nprim;
  1618. if (!*len) {
  1619. rc = 0;
  1620. goto out;
  1621. }
  1622. *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
  1623. if (!*names)
  1624. goto err;
  1625. *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
  1626. if (!*values)
  1627. goto err;
  1628. for (i = 0; i < *len; i++) {
  1629. size_t name_len;
  1630. (*values)[i] = policydb.bool_val_to_struct[i]->state;
  1631. name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
  1632. (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
  1633. if (!(*names)[i])
  1634. goto err;
  1635. strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
  1636. (*names)[i][name_len - 1] = 0;
  1637. }
  1638. rc = 0;
  1639. out:
  1640. POLICY_RDUNLOCK;
  1641. return rc;
  1642. err:
  1643. if (*names) {
  1644. for (i = 0; i < *len; i++)
  1645. kfree((*names)[i]);
  1646. }
  1647. kfree(*values);
  1648. goto out;
  1649. }
  1650. int security_set_bools(int len, int *values)
  1651. {
  1652. int i, rc = 0;
  1653. int lenp, seqno = 0;
  1654. struct cond_node *cur;
  1655. POLICY_WRLOCK;
  1656. lenp = policydb.p_bools.nprim;
  1657. if (len != lenp) {
  1658. rc = -EFAULT;
  1659. goto out;
  1660. }
  1661. for (i = 0; i < len; i++) {
  1662. if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
  1663. audit_log(current->audit_context, GFP_ATOMIC,
  1664. AUDIT_MAC_CONFIG_CHANGE,
  1665. "bool=%s val=%d old_val=%d auid=%u",
  1666. policydb.p_bool_val_to_name[i],
  1667. !!values[i],
  1668. policydb.bool_val_to_struct[i]->state,
  1669. audit_get_loginuid(current->audit_context));
  1670. }
  1671. if (values[i]) {
  1672. policydb.bool_val_to_struct[i]->state = 1;
  1673. } else {
  1674. policydb.bool_val_to_struct[i]->state = 0;
  1675. }
  1676. }
  1677. for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
  1678. rc = evaluate_cond_node(&policydb, cur);
  1679. if (rc)
  1680. goto out;
  1681. }
  1682. seqno = ++latest_granting;
  1683. out:
  1684. POLICY_WRUNLOCK;
  1685. if (!rc) {
  1686. avc_ss_reset(seqno);
  1687. selnl_notify_policyload(seqno);
  1688. selinux_xfrm_notify_policyload();
  1689. }
  1690. return rc;
  1691. }
  1692. int security_get_bool_value(int bool)
  1693. {
  1694. int rc = 0;
  1695. int len;
  1696. POLICY_RDLOCK;
  1697. len = policydb.p_bools.nprim;
  1698. if (bool >= len) {
  1699. rc = -EFAULT;
  1700. goto out;
  1701. }
  1702. rc = policydb.bool_val_to_struct[bool]->state;
  1703. out:
  1704. POLICY_RDUNLOCK;
  1705. return rc;
  1706. }
  1707. static int security_preserve_bools(struct policydb *p)
  1708. {
  1709. int rc, nbools = 0, *bvalues = NULL, i;
  1710. char **bnames = NULL;
  1711. struct cond_bool_datum *booldatum;
  1712. struct cond_node *cur;
  1713. rc = security_get_bools(&nbools, &bnames, &bvalues);
  1714. if (rc)
  1715. goto out;
  1716. for (i = 0; i < nbools; i++) {
  1717. booldatum = hashtab_search(p->p_bools.table, bnames[i]);
  1718. if (booldatum)
  1719. booldatum->state = bvalues[i];
  1720. }
  1721. for (cur = p->cond_list; cur != NULL; cur = cur->next) {
  1722. rc = evaluate_cond_node(p, cur);
  1723. if (rc)
  1724. goto out;
  1725. }
  1726. out:
  1727. if (bnames) {
  1728. for (i = 0; i < nbools; i++)
  1729. kfree(bnames[i]);
  1730. }
  1731. kfree(bnames);
  1732. kfree(bvalues);
  1733. return rc;
  1734. }
  1735. /*
  1736. * security_sid_mls_copy() - computes a new sid based on the given
  1737. * sid and the mls portion of mls_sid.
  1738. */
  1739. int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
  1740. {
  1741. struct context *context1;
  1742. struct context *context2;
  1743. struct context newcon;
  1744. char *s;
  1745. u32 len;
  1746. int rc = 0;
  1747. if (!ss_initialized || !selinux_mls_enabled) {
  1748. *new_sid = sid;
  1749. goto out;
  1750. }
  1751. context_init(&newcon);
  1752. POLICY_RDLOCK;
  1753. context1 = sidtab_search(&sidtab, sid);
  1754. if (!context1) {
  1755. printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
  1756. "%d\n", sid);
  1757. rc = -EINVAL;
  1758. goto out_unlock;
  1759. }
  1760. context2 = sidtab_search(&sidtab, mls_sid);
  1761. if (!context2) {
  1762. printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
  1763. "%d\n", mls_sid);
  1764. rc = -EINVAL;
  1765. goto out_unlock;
  1766. }
  1767. newcon.user = context1->user;
  1768. newcon.role = context1->role;
  1769. newcon.type = context1->type;
  1770. rc = mls_context_cpy(&newcon, context2);
  1771. if (rc)
  1772. goto out_unlock;
  1773. /* Check the validity of the new context. */
  1774. if (!policydb_context_isvalid(&policydb, &newcon)) {
  1775. rc = convert_context_handle_invalid_context(&newcon);
  1776. if (rc)
  1777. goto bad;
  1778. }
  1779. rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
  1780. goto out_unlock;
  1781. bad:
  1782. if (!context_struct_to_string(&newcon, &s, &len)) {
  1783. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1784. "security_sid_mls_copy: invalid context %s", s);
  1785. kfree(s);
  1786. }
  1787. out_unlock:
  1788. POLICY_RDUNLOCK;
  1789. context_destroy(&newcon);
  1790. out:
  1791. return rc;
  1792. }
  1793. static int get_classes_callback(void *k, void *d, void *args)
  1794. {
  1795. struct class_datum *datum = d;
  1796. char *name = k, **classes = args;
  1797. int value = datum->value - 1;
  1798. classes[value] = kstrdup(name, GFP_ATOMIC);
  1799. if (!classes[value])
  1800. return -ENOMEM;
  1801. return 0;
  1802. }
  1803. int security_get_classes(char ***classes, int *nclasses)
  1804. {
  1805. int rc = -ENOMEM;
  1806. POLICY_RDLOCK;
  1807. *nclasses = policydb.p_classes.nprim;
  1808. *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
  1809. if (!*classes)
  1810. goto out;
  1811. rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
  1812. *classes);
  1813. if (rc < 0) {
  1814. int i;
  1815. for (i = 0; i < *nclasses; i++)
  1816. kfree((*classes)[i]);
  1817. kfree(*classes);
  1818. }
  1819. out:
  1820. POLICY_RDUNLOCK;
  1821. return rc;
  1822. }
  1823. static int get_permissions_callback(void *k, void *d, void *args)
  1824. {
  1825. struct perm_datum *datum = d;
  1826. char *name = k, **perms = args;
  1827. int value = datum->value - 1;
  1828. perms[value] = kstrdup(name, GFP_ATOMIC);
  1829. if (!perms[value])
  1830. return -ENOMEM;
  1831. return 0;
  1832. }
  1833. int security_get_permissions(char *class, char ***perms, int *nperms)
  1834. {
  1835. int rc = -ENOMEM, i;
  1836. struct class_datum *match;
  1837. POLICY_RDLOCK;
  1838. match = hashtab_search(policydb.p_classes.table, class);
  1839. if (!match) {
  1840. printk(KERN_ERR "%s: unrecognized class %s\n",
  1841. __FUNCTION__, class);
  1842. rc = -EINVAL;
  1843. goto out;
  1844. }
  1845. *nperms = match->permissions.nprim;
  1846. *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
  1847. if (!*perms)
  1848. goto out;
  1849. if (match->comdatum) {
  1850. rc = hashtab_map(match->comdatum->permissions.table,
  1851. get_permissions_callback, *perms);
  1852. if (rc < 0)
  1853. goto err;
  1854. }
  1855. rc = hashtab_map(match->permissions.table, get_permissions_callback,
  1856. *perms);
  1857. if (rc < 0)
  1858. goto err;
  1859. out:
  1860. POLICY_RDUNLOCK;
  1861. return rc;
  1862. err:
  1863. POLICY_RDUNLOCK;
  1864. for (i = 0; i < *nperms; i++)
  1865. kfree((*perms)[i]);
  1866. kfree(*perms);
  1867. return rc;
  1868. }
  1869. struct selinux_audit_rule {
  1870. u32 au_seqno;
  1871. struct context au_ctxt;
  1872. };
  1873. void selinux_audit_rule_free(struct selinux_audit_rule *rule)
  1874. {
  1875. if (rule) {
  1876. context_destroy(&rule->au_ctxt);
  1877. kfree(rule);
  1878. }
  1879. }
  1880. int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
  1881. struct selinux_audit_rule **rule)
  1882. {
  1883. struct selinux_audit_rule *tmprule;
  1884. struct role_datum *roledatum;
  1885. struct type_datum *typedatum;
  1886. struct user_datum *userdatum;
  1887. int rc = 0;
  1888. *rule = NULL;
  1889. if (!ss_initialized)
  1890. return -EOPNOTSUPP;
  1891. switch (field) {
  1892. case AUDIT_SUBJ_USER:
  1893. case AUDIT_SUBJ_ROLE:
  1894. case AUDIT_SUBJ_TYPE:
  1895. case AUDIT_OBJ_USER:
  1896. case AUDIT_OBJ_ROLE:
  1897. case AUDIT_OBJ_TYPE:
  1898. /* only 'equals' and 'not equals' fit user, role, and type */
  1899. if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
  1900. return -EINVAL;
  1901. break;
  1902. case AUDIT_SUBJ_SEN:
  1903. case AUDIT_SUBJ_CLR:
  1904. case AUDIT_OBJ_LEV_LOW:
  1905. case AUDIT_OBJ_LEV_HIGH:
  1906. /* we do not allow a range, indicated by the presense of '-' */
  1907. if (strchr(rulestr, '-'))
  1908. return -EINVAL;
  1909. break;
  1910. default:
  1911. /* only the above fields are valid */
  1912. return -EINVAL;
  1913. }
  1914. tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
  1915. if (!tmprule)
  1916. return -ENOMEM;
  1917. context_init(&tmprule->au_ctxt);
  1918. POLICY_RDLOCK;
  1919. tmprule->au_seqno = latest_granting;
  1920. switch (field) {
  1921. case AUDIT_SUBJ_USER:
  1922. case AUDIT_OBJ_USER:
  1923. userdatum = hashtab_search(policydb.p_users.table, rulestr);
  1924. if (!userdatum)
  1925. rc = -EINVAL;
  1926. else
  1927. tmprule->au_ctxt.user = userdatum->value;
  1928. break;
  1929. case AUDIT_SUBJ_ROLE:
  1930. case AUDIT_OBJ_ROLE:
  1931. roledatum = hashtab_search(policydb.p_roles.table, rulestr);
  1932. if (!roledatum)
  1933. rc = -EINVAL;
  1934. else
  1935. tmprule->au_ctxt.role = roledatum->value;
  1936. break;
  1937. case AUDIT_SUBJ_TYPE:
  1938. case AUDIT_OBJ_TYPE:
  1939. typedatum = hashtab_search(policydb.p_types.table, rulestr);
  1940. if (!typedatum)
  1941. rc = -EINVAL;
  1942. else
  1943. tmprule->au_ctxt.type = typedatum->value;
  1944. break;
  1945. case AUDIT_SUBJ_SEN:
  1946. case AUDIT_SUBJ_CLR:
  1947. case AUDIT_OBJ_LEV_LOW:
  1948. case AUDIT_OBJ_LEV_HIGH:
  1949. rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
  1950. break;
  1951. }
  1952. POLICY_RDUNLOCK;
  1953. if (rc) {
  1954. selinux_audit_rule_free(tmprule);
  1955. tmprule = NULL;
  1956. }
  1957. *rule = tmprule;
  1958. return rc;
  1959. }
  1960. int selinux_audit_rule_match(u32 sid, u32 field, u32 op,
  1961. struct selinux_audit_rule *rule,
  1962. struct audit_context *actx)
  1963. {
  1964. struct context *ctxt;
  1965. struct mls_level *level;
  1966. int match = 0;
  1967. if (!rule) {
  1968. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1969. "selinux_audit_rule_match: missing rule\n");
  1970. return -ENOENT;
  1971. }
  1972. POLICY_RDLOCK;
  1973. if (rule->au_seqno < latest_granting) {
  1974. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1975. "selinux_audit_rule_match: stale rule\n");
  1976. match = -ESTALE;
  1977. goto out;
  1978. }
  1979. ctxt = sidtab_search(&sidtab, sid);
  1980. if (!ctxt) {
  1981. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1982. "selinux_audit_rule_match: unrecognized SID %d\n",
  1983. sid);
  1984. match = -ENOENT;
  1985. goto out;
  1986. }
  1987. /* a field/op pair that is not caught here will simply fall through
  1988. without a match */
  1989. switch (field) {
  1990. case AUDIT_SUBJ_USER:
  1991. case AUDIT_OBJ_USER:
  1992. switch (op) {
  1993. case AUDIT_EQUAL:
  1994. match = (ctxt->user == rule->au_ctxt.user);
  1995. break;
  1996. case AUDIT_NOT_EQUAL:
  1997. match = (ctxt->user != rule->au_ctxt.user);
  1998. break;
  1999. }
  2000. break;
  2001. case AUDIT_SUBJ_ROLE:
  2002. case AUDIT_OBJ_ROLE:
  2003. switch (op) {
  2004. case AUDIT_EQUAL:
  2005. match = (ctxt->role == rule->au_ctxt.role);
  2006. break;
  2007. case AUDIT_NOT_EQUAL:
  2008. match = (ctxt->role != rule->au_ctxt.role);
  2009. break;
  2010. }
  2011. break;
  2012. case AUDIT_SUBJ_TYPE:
  2013. case AUDIT_OBJ_TYPE:
  2014. switch (op) {
  2015. case AUDIT_EQUAL:
  2016. match = (ctxt->type == rule->au_ctxt.type);
  2017. break;
  2018. case AUDIT_NOT_EQUAL:
  2019. match = (ctxt->type != rule->au_ctxt.type);
  2020. break;
  2021. }
  2022. break;
  2023. case AUDIT_SUBJ_SEN:
  2024. case AUDIT_SUBJ_CLR:
  2025. case AUDIT_OBJ_LEV_LOW:
  2026. case AUDIT_OBJ_LEV_HIGH:
  2027. level = ((field == AUDIT_SUBJ_SEN ||
  2028. field == AUDIT_OBJ_LEV_LOW) ?
  2029. &ctxt->range.level[0] : &ctxt->range.level[1]);
  2030. switch (op) {
  2031. case AUDIT_EQUAL:
  2032. match = mls_level_eq(&rule->au_ctxt.range.level[0],
  2033. level);
  2034. break;
  2035. case AUDIT_NOT_EQUAL:
  2036. match = !mls_level_eq(&rule->au_ctxt.range.level[0],
  2037. level);
  2038. break;
  2039. case AUDIT_LESS_THAN:
  2040. match = (mls_level_dom(&rule->au_ctxt.range.level[0],
  2041. level) &&
  2042. !mls_level_eq(&rule->au_ctxt.range.level[0],
  2043. level));
  2044. break;
  2045. case AUDIT_LESS_THAN_OR_EQUAL:
  2046. match = mls_level_dom(&rule->au_ctxt.range.level[0],
  2047. level);
  2048. break;
  2049. case AUDIT_GREATER_THAN:
  2050. match = (mls_level_dom(level,
  2051. &rule->au_ctxt.range.level[0]) &&
  2052. !mls_level_eq(level,
  2053. &rule->au_ctxt.range.level[0]));
  2054. break;
  2055. case AUDIT_GREATER_THAN_OR_EQUAL:
  2056. match = mls_level_dom(level,
  2057. &rule->au_ctxt.range.level[0]);
  2058. break;
  2059. }
  2060. }
  2061. out:
  2062. POLICY_RDUNLOCK;
  2063. return match;
  2064. }
  2065. static int (*aurule_callback)(void) = NULL;
  2066. static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
  2067. u16 class, u32 perms, u32 *retained)
  2068. {
  2069. int err = 0;
  2070. if (event == AVC_CALLBACK_RESET && aurule_callback)
  2071. err = aurule_callback();
  2072. return err;
  2073. }
  2074. static int __init aurule_init(void)
  2075. {
  2076. int err;
  2077. err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
  2078. SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
  2079. if (err)
  2080. panic("avc_add_callback() failed, error %d\n", err);
  2081. return err;
  2082. }
  2083. __initcall(aurule_init);
  2084. void selinux_audit_set_callback(int (*callback)(void))
  2085. {
  2086. aurule_callback = callback;
  2087. }
  2088. #ifdef CONFIG_NETLABEL
  2089. /*
  2090. * NetLabel cache structure
  2091. */
  2092. #define NETLBL_CACHE(x) ((struct selinux_netlbl_cache *)(x))
  2093. #define NETLBL_CACHE_T_NONE 0
  2094. #define NETLBL_CACHE_T_SID 1
  2095. #define NETLBL_CACHE_T_MLS 2
  2096. struct selinux_netlbl_cache {
  2097. u32 type;
  2098. union {
  2099. u32 sid;
  2100. struct mls_range mls_label;
  2101. } data;
  2102. };
  2103. /**
  2104. * security_netlbl_cache_free - Free the NetLabel cached data
  2105. * @data: the data to free
  2106. *
  2107. * Description:
  2108. * This function is intended to be used as the free() callback inside the
  2109. * netlbl_lsm_cache structure.
  2110. *
  2111. */
  2112. static void security_netlbl_cache_free(const void *data)
  2113. {
  2114. struct selinux_netlbl_cache *cache;
  2115. if (data == NULL)
  2116. return;
  2117. cache = NETLBL_CACHE(data);
  2118. switch (cache->type) {
  2119. case NETLBL_CACHE_T_MLS:
  2120. ebitmap_destroy(&cache->data.mls_label.level[0].cat);
  2121. break;
  2122. }
  2123. kfree(data);
  2124. }
  2125. /**
  2126. * security_netlbl_cache_add - Add an entry to the NetLabel cache
  2127. * @secattr: the NetLabel packet security attributes
  2128. * @ctx: the SELinux context
  2129. *
  2130. * Description:
  2131. * Attempt to cache the context in @ctx, which was derived from the packet in
  2132. * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
  2133. * already been initialized.
  2134. *
  2135. */
  2136. static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
  2137. struct context *ctx)
  2138. {
  2139. struct selinux_netlbl_cache *cache = NULL;
  2140. secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
  2141. if (secattr->cache == NULL)
  2142. return;
  2143. cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
  2144. if (cache == NULL)
  2145. return;
  2146. cache->type = NETLBL_CACHE_T_MLS;
  2147. if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
  2148. &ctx->range.level[0].cat) != 0) {
  2149. kfree(cache);
  2150. return;
  2151. }
  2152. cache->data.mls_label.level[1].cat.highbit =
  2153. cache->data.mls_label.level[0].cat.highbit;
  2154. cache->data.mls_label.level[1].cat.node =
  2155. cache->data.mls_label.level[0].cat.node;
  2156. cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
  2157. cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
  2158. secattr->cache->free = security_netlbl_cache_free;
  2159. secattr->cache->data = (void *)cache;
  2160. secattr->flags |= NETLBL_SECATTR_CACHE;
  2161. }
  2162. /**
  2163. * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
  2164. * @secattr: the NetLabel packet security attributes
  2165. * @base_sid: the SELinux SID to use as a context for MLS only attributes
  2166. * @sid: the SELinux SID
  2167. *
  2168. * Description:
  2169. * Convert the given NetLabel security attributes in @secattr into a
  2170. * SELinux SID. If the @secattr field does not contain a full SELinux
  2171. * SID/context then use the context in @base_sid as the foundation. If
  2172. * possibile the 'cache' field of @secattr is set and the CACHE flag is set;
  2173. * this is to allow the @secattr to be used by NetLabel to cache the secattr to
  2174. * SID conversion for future lookups. Returns zero on success, negative
  2175. * values on failure.
  2176. *
  2177. */
  2178. int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
  2179. u32 base_sid,
  2180. u32 *sid)
  2181. {
  2182. int rc = -EIDRM;
  2183. struct context *ctx;
  2184. struct context ctx_new;
  2185. struct selinux_netlbl_cache *cache;
  2186. if (!ss_initialized) {
  2187. *sid = SECSID_NULL;
  2188. return 0;
  2189. }
  2190. POLICY_RDLOCK;
  2191. if (secattr->flags & NETLBL_SECATTR_CACHE) {
  2192. cache = NETLBL_CACHE(secattr->cache->data);
  2193. switch (cache->type) {
  2194. case NETLBL_CACHE_T_SID:
  2195. *sid = cache->data.sid;
  2196. rc = 0;
  2197. break;
  2198. case NETLBL_CACHE_T_MLS:
  2199. ctx = sidtab_search(&sidtab, base_sid);
  2200. if (ctx == NULL)
  2201. goto netlbl_secattr_to_sid_return;
  2202. ctx_new.user = ctx->user;
  2203. ctx_new.role = ctx->role;
  2204. ctx_new.type = ctx->type;
  2205. ctx_new.range.level[0].sens =
  2206. cache->data.mls_label.level[0].sens;
  2207. ctx_new.range.level[0].cat.highbit =
  2208. cache->data.mls_label.level[0].cat.highbit;
  2209. ctx_new.range.level[0].cat.node =
  2210. cache->data.mls_label.level[0].cat.node;
  2211. ctx_new.range.level[1].sens =
  2212. cache->data.mls_label.level[1].sens;
  2213. ctx_new.range.level[1].cat.highbit =
  2214. cache->data.mls_label.level[1].cat.highbit;
  2215. ctx_new.range.level[1].cat.node =
  2216. cache->data.mls_label.level[1].cat.node;
  2217. rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
  2218. break;
  2219. default:
  2220. goto netlbl_secattr_to_sid_return;
  2221. }
  2222. } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
  2223. ctx = sidtab_search(&sidtab, base_sid);
  2224. if (ctx == NULL)
  2225. goto netlbl_secattr_to_sid_return;
  2226. ctx_new.user = ctx->user;
  2227. ctx_new.role = ctx->role;
  2228. ctx_new.type = ctx->type;
  2229. mls_import_netlbl_lvl(&ctx_new, secattr);
  2230. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  2231. if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
  2232. secattr->mls_cat) != 0)
  2233. goto netlbl_secattr_to_sid_return;
  2234. ctx_new.range.level[1].cat.highbit =
  2235. ctx_new.range.level[0].cat.highbit;
  2236. ctx_new.range.level[1].cat.node =
  2237. ctx_new.range.level[0].cat.node;
  2238. } else {
  2239. ebitmap_init(&ctx_new.range.level[0].cat);
  2240. ebitmap_init(&ctx_new.range.level[1].cat);
  2241. }
  2242. if (mls_context_isvalid(&policydb, &ctx_new) != 1)
  2243. goto netlbl_secattr_to_sid_return_cleanup;
  2244. rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
  2245. if (rc != 0)
  2246. goto netlbl_secattr_to_sid_return_cleanup;
  2247. security_netlbl_cache_add(secattr, &ctx_new);
  2248. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2249. } else {
  2250. *sid = SECSID_NULL;
  2251. rc = 0;
  2252. }
  2253. netlbl_secattr_to_sid_return:
  2254. POLICY_RDUNLOCK;
  2255. return rc;
  2256. netlbl_secattr_to_sid_return_cleanup:
  2257. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2258. goto netlbl_secattr_to_sid_return;
  2259. }
  2260. /**
  2261. * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
  2262. * @sid: the SELinux SID
  2263. * @secattr: the NetLabel packet security attributes
  2264. *
  2265. * Description:
  2266. * Convert the given SELinux SID in @sid into a NetLabel security attribute.
  2267. * Returns zero on success, negative values on failure.
  2268. *
  2269. */
  2270. int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
  2271. {
  2272. int rc = -ENOENT;
  2273. struct context *ctx;
  2274. netlbl_secattr_init(secattr);
  2275. if (!ss_initialized)
  2276. return 0;
  2277. POLICY_RDLOCK;
  2278. ctx = sidtab_search(&sidtab, sid);
  2279. if (ctx == NULL)
  2280. goto netlbl_sid_to_secattr_failure;
  2281. secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
  2282. GFP_ATOMIC);
  2283. secattr->flags |= NETLBL_SECATTR_DOMAIN;
  2284. mls_export_netlbl_lvl(ctx, secattr);
  2285. rc = mls_export_netlbl_cat(ctx, secattr);
  2286. if (rc != 0)
  2287. goto netlbl_sid_to_secattr_failure;
  2288. POLICY_RDUNLOCK;
  2289. return 0;
  2290. netlbl_sid_to_secattr_failure:
  2291. POLICY_RDUNLOCK;
  2292. netlbl_secattr_destroy(secattr);
  2293. return rc;
  2294. }
  2295. #endif /* CONFIG_NETLABEL */