services.c 64 KB

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