services.c 66 KB

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