services.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708
  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. #define POLICY_RDLOCK read_lock(&policy_rwlock)
  70. #define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
  71. #define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
  72. #define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
  73. static DEFINE_MUTEX(load_mutex);
  74. #define LOAD_LOCK mutex_lock(&load_mutex)
  75. #define LOAD_UNLOCK mutex_unlock(&load_mutex)
  76. static struct sidtab sidtab;
  77. struct policydb policydb;
  78. int ss_initialized;
  79. /*
  80. * The largest sequence number that has been used when
  81. * providing an access decision to the access vector cache.
  82. * The sequence number only changes when a policy change
  83. * occurs.
  84. */
  85. static u32 latest_granting;
  86. /* Forward declaration. */
  87. static int context_struct_to_string(struct context *context, char **scontext,
  88. u32 *scontext_len);
  89. /*
  90. * Return the boolean value of a constraint expression
  91. * when it is applied to the specified source and target
  92. * security contexts.
  93. *
  94. * xcontext is a special beast... It is used by the validatetrans rules
  95. * only. For these rules, scontext is the context before the transition,
  96. * tcontext is the context after the transition, and xcontext is the context
  97. * of the process performing the transition. All other callers of
  98. * constraint_expr_eval should pass in NULL for xcontext.
  99. */
  100. static int constraint_expr_eval(struct context *scontext,
  101. struct context *tcontext,
  102. struct context *xcontext,
  103. struct constraint_expr *cexpr)
  104. {
  105. u32 val1, val2;
  106. struct context *c;
  107. struct role_datum *r1, *r2;
  108. struct mls_level *l1, *l2;
  109. struct constraint_expr *e;
  110. int s[CEXPR_MAXDEPTH];
  111. int sp = -1;
  112. for (e = cexpr; e; e = e->next) {
  113. switch (e->expr_type) {
  114. case CEXPR_NOT:
  115. BUG_ON(sp < 0);
  116. s[sp] = !s[sp];
  117. break;
  118. case CEXPR_AND:
  119. BUG_ON(sp < 1);
  120. sp--;
  121. s[sp] &= s[sp+1];
  122. break;
  123. case CEXPR_OR:
  124. BUG_ON(sp < 1);
  125. sp--;
  126. s[sp] |= s[sp+1];
  127. break;
  128. case CEXPR_ATTR:
  129. if (sp == (CEXPR_MAXDEPTH-1))
  130. return 0;
  131. switch (e->attr) {
  132. case CEXPR_USER:
  133. val1 = scontext->user;
  134. val2 = tcontext->user;
  135. break;
  136. case CEXPR_TYPE:
  137. val1 = scontext->type;
  138. val2 = tcontext->type;
  139. break;
  140. case CEXPR_ROLE:
  141. val1 = scontext->role;
  142. val2 = tcontext->role;
  143. r1 = policydb.role_val_to_struct[val1 - 1];
  144. r2 = policydb.role_val_to_struct[val2 - 1];
  145. switch (e->op) {
  146. case CEXPR_DOM:
  147. s[++sp] = ebitmap_get_bit(&r1->dominates,
  148. val2 - 1);
  149. continue;
  150. case CEXPR_DOMBY:
  151. s[++sp] = ebitmap_get_bit(&r2->dominates,
  152. val1 - 1);
  153. continue;
  154. case CEXPR_INCOMP:
  155. s[++sp] = (!ebitmap_get_bit(&r1->dominates,
  156. val2 - 1) &&
  157. !ebitmap_get_bit(&r2->dominates,
  158. val1 - 1));
  159. continue;
  160. default:
  161. break;
  162. }
  163. break;
  164. case CEXPR_L1L2:
  165. l1 = &(scontext->range.level[0]);
  166. l2 = &(tcontext->range.level[0]);
  167. goto mls_ops;
  168. case CEXPR_L1H2:
  169. l1 = &(scontext->range.level[0]);
  170. l2 = &(tcontext->range.level[1]);
  171. goto mls_ops;
  172. case CEXPR_H1L2:
  173. l1 = &(scontext->range.level[1]);
  174. l2 = &(tcontext->range.level[0]);
  175. goto mls_ops;
  176. case CEXPR_H1H2:
  177. l1 = &(scontext->range.level[1]);
  178. l2 = &(tcontext->range.level[1]);
  179. goto mls_ops;
  180. case CEXPR_L1H1:
  181. l1 = &(scontext->range.level[0]);
  182. l2 = &(scontext->range.level[1]);
  183. goto mls_ops;
  184. case CEXPR_L2H2:
  185. l1 = &(tcontext->range.level[0]);
  186. l2 = &(tcontext->range.level[1]);
  187. goto mls_ops;
  188. mls_ops:
  189. switch (e->op) {
  190. case CEXPR_EQ:
  191. s[++sp] = mls_level_eq(l1, l2);
  192. continue;
  193. case CEXPR_NEQ:
  194. s[++sp] = !mls_level_eq(l1, l2);
  195. continue;
  196. case CEXPR_DOM:
  197. s[++sp] = mls_level_dom(l1, l2);
  198. continue;
  199. case CEXPR_DOMBY:
  200. s[++sp] = mls_level_dom(l2, l1);
  201. continue;
  202. case CEXPR_INCOMP:
  203. s[++sp] = mls_level_incomp(l2, l1);
  204. continue;
  205. default:
  206. BUG();
  207. return 0;
  208. }
  209. break;
  210. default:
  211. BUG();
  212. return 0;
  213. }
  214. switch (e->op) {
  215. case CEXPR_EQ:
  216. s[++sp] = (val1 == val2);
  217. break;
  218. case CEXPR_NEQ:
  219. s[++sp] = (val1 != val2);
  220. break;
  221. default:
  222. BUG();
  223. return 0;
  224. }
  225. break;
  226. case CEXPR_NAMES:
  227. if (sp == (CEXPR_MAXDEPTH-1))
  228. return 0;
  229. c = scontext;
  230. if (e->attr & CEXPR_TARGET)
  231. c = tcontext;
  232. else if (e->attr & CEXPR_XTARGET) {
  233. c = xcontext;
  234. if (!c) {
  235. BUG();
  236. return 0;
  237. }
  238. }
  239. if (e->attr & CEXPR_USER)
  240. val1 = c->user;
  241. else if (e->attr & CEXPR_ROLE)
  242. val1 = c->role;
  243. else if (e->attr & CEXPR_TYPE)
  244. val1 = c->type;
  245. else {
  246. BUG();
  247. return 0;
  248. }
  249. switch (e->op) {
  250. case CEXPR_EQ:
  251. s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
  252. break;
  253. case CEXPR_NEQ:
  254. s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
  255. break;
  256. default:
  257. BUG();
  258. return 0;
  259. }
  260. break;
  261. default:
  262. BUG();
  263. return 0;
  264. }
  265. }
  266. BUG_ON(sp != 0);
  267. return s[0];
  268. }
  269. /*
  270. * Compute access vectors based on a context structure pair for
  271. * the permissions in a particular class.
  272. */
  273. static int context_struct_compute_av(struct context *scontext,
  274. struct context *tcontext,
  275. u16 tclass,
  276. u32 requested,
  277. struct av_decision *avd)
  278. {
  279. struct constraint_node *constraint;
  280. struct role_allow *ra;
  281. struct avtab_key avkey;
  282. struct avtab_node *node;
  283. struct class_datum *tclass_datum;
  284. struct ebitmap *sattr, *tattr;
  285. struct ebitmap_node *snode, *tnode;
  286. const struct selinux_class_perm *kdefs = &selinux_class_perm;
  287. unsigned int i, j;
  288. /*
  289. * Remap extended Netlink classes for old policy versions.
  290. * Do this here rather than socket_type_to_security_class()
  291. * in case a newer policy version is loaded, allowing sockets
  292. * to remain in the correct class.
  293. */
  294. if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
  295. if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
  296. tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
  297. tclass = SECCLASS_NETLINK_SOCKET;
  298. /*
  299. * Initialize the access vectors to the default values.
  300. */
  301. avd->allowed = 0;
  302. avd->decided = 0xffffffff;
  303. avd->auditallow = 0;
  304. avd->auditdeny = 0xffffffff;
  305. avd->seqno = latest_granting;
  306. /*
  307. * Check for all the invalid cases.
  308. * - tclass 0
  309. * - tclass > policy and > kernel
  310. * - tclass > policy but is a userspace class
  311. * - tclass > policy but we do not allow unknowns
  312. */
  313. if (unlikely(!tclass))
  314. goto inval_class;
  315. if (unlikely(tclass > policydb.p_classes.nprim))
  316. if (tclass > kdefs->cts_len ||
  317. !kdefs->class_to_string[tclass - 1] ||
  318. !policydb.allow_unknown)
  319. goto inval_class;
  320. /*
  321. * Kernel class and we allow unknown so pad the allow decision
  322. * the pad will be all 1 for unknown classes.
  323. */
  324. if (tclass <= kdefs->cts_len && policydb.allow_unknown)
  325. avd->allowed = policydb.undefined_perms[tclass - 1];
  326. /*
  327. * Not in policy. Since decision is completed (all 1 or all 0) return.
  328. */
  329. if (unlikely(tclass > policydb.p_classes.nprim))
  330. return 0;
  331. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  332. /*
  333. * If a specific type enforcement rule was defined for
  334. * this permission check, then use it.
  335. */
  336. avkey.target_class = tclass;
  337. avkey.specified = AVTAB_AV;
  338. sattr = &policydb.type_attr_map[scontext->type - 1];
  339. tattr = &policydb.type_attr_map[tcontext->type - 1];
  340. ebitmap_for_each_positive_bit(sattr, snode, i) {
  341. ebitmap_for_each_positive_bit(tattr, tnode, j) {
  342. avkey.source_type = i + 1;
  343. avkey.target_type = j + 1;
  344. for (node = avtab_search_node(&policydb.te_avtab, &avkey);
  345. node != NULL;
  346. node = avtab_search_node_next(node, avkey.specified)) {
  347. if (node->key.specified == AVTAB_ALLOWED)
  348. avd->allowed |= node->datum.data;
  349. else if (node->key.specified == AVTAB_AUDITALLOW)
  350. avd->auditallow |= node->datum.data;
  351. else if (node->key.specified == AVTAB_AUDITDENY)
  352. avd->auditdeny &= node->datum.data;
  353. }
  354. /* Check conditional av table for additional permissions */
  355. cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
  356. }
  357. }
  358. /*
  359. * Remove any permissions prohibited by a constraint (this includes
  360. * the MLS policy).
  361. */
  362. constraint = tclass_datum->constraints;
  363. while (constraint) {
  364. if ((constraint->permissions & (avd->allowed)) &&
  365. !constraint_expr_eval(scontext, tcontext, NULL,
  366. constraint->expr)) {
  367. avd->allowed = (avd->allowed) & ~(constraint->permissions);
  368. }
  369. constraint = constraint->next;
  370. }
  371. /*
  372. * If checking process transition permission and the
  373. * role is changing, then check the (current_role, new_role)
  374. * pair.
  375. */
  376. if (tclass == SECCLASS_PROCESS &&
  377. (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
  378. scontext->role != tcontext->role) {
  379. for (ra = policydb.role_allow; ra; ra = ra->next) {
  380. if (scontext->role == ra->role &&
  381. tcontext->role == ra->new_role)
  382. break;
  383. }
  384. if (!ra)
  385. avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
  386. PROCESS__DYNTRANSITION);
  387. }
  388. return 0;
  389. inval_class:
  390. printk(KERN_ERR "%s: unrecognized class %d\n", __func__, tclass);
  391. return -EINVAL;
  392. }
  393. /*
  394. * Given a sid find if the type has the permissive flag set
  395. */
  396. int security_permissive_sid(u32 sid)
  397. {
  398. struct context *context;
  399. u32 type;
  400. int rc;
  401. POLICY_RDLOCK;
  402. context = sidtab_search(&sidtab, sid);
  403. BUG_ON(!context);
  404. type = context->type;
  405. /*
  406. * we are intentionally using type here, not type-1, the 0th bit may
  407. * someday indicate that we are globally setting permissive in policy.
  408. */
  409. rc = ebitmap_get_bit(&policydb.permissive_map, type);
  410. POLICY_RDUNLOCK;
  411. return rc;
  412. }
  413. static int security_validtrans_handle_fail(struct context *ocontext,
  414. struct context *ncontext,
  415. struct context *tcontext,
  416. u16 tclass)
  417. {
  418. char *o = NULL, *n = NULL, *t = NULL;
  419. u32 olen, nlen, tlen;
  420. if (context_struct_to_string(ocontext, &o, &olen) < 0)
  421. goto out;
  422. if (context_struct_to_string(ncontext, &n, &nlen) < 0)
  423. goto out;
  424. if (context_struct_to_string(tcontext, &t, &tlen) < 0)
  425. goto out;
  426. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  427. "security_validate_transition: denied for"
  428. " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
  429. o, n, t, policydb.p_class_val_to_name[tclass-1]);
  430. out:
  431. kfree(o);
  432. kfree(n);
  433. kfree(t);
  434. if (!selinux_enforcing)
  435. return 0;
  436. return -EPERM;
  437. }
  438. int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
  439. u16 tclass)
  440. {
  441. struct context *ocontext;
  442. struct context *ncontext;
  443. struct context *tcontext;
  444. struct class_datum *tclass_datum;
  445. struct constraint_node *constraint;
  446. int rc = 0;
  447. if (!ss_initialized)
  448. return 0;
  449. POLICY_RDLOCK;
  450. /*
  451. * Remap extended Netlink classes for old policy versions.
  452. * Do this here rather than socket_type_to_security_class()
  453. * in case a newer policy version is loaded, allowing sockets
  454. * to remain in the correct class.
  455. */
  456. if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
  457. if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
  458. tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
  459. tclass = SECCLASS_NETLINK_SOCKET;
  460. if (!tclass || tclass > policydb.p_classes.nprim) {
  461. printk(KERN_ERR "security_validate_transition: "
  462. "unrecognized class %d\n", tclass);
  463. rc = -EINVAL;
  464. goto out;
  465. }
  466. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  467. ocontext = sidtab_search(&sidtab, oldsid);
  468. if (!ocontext) {
  469. printk(KERN_ERR "security_validate_transition: "
  470. " unrecognized SID %d\n", oldsid);
  471. rc = -EINVAL;
  472. goto out;
  473. }
  474. ncontext = sidtab_search(&sidtab, newsid);
  475. if (!ncontext) {
  476. printk(KERN_ERR "security_validate_transition: "
  477. " unrecognized SID %d\n", newsid);
  478. rc = -EINVAL;
  479. goto out;
  480. }
  481. tcontext = sidtab_search(&sidtab, tasksid);
  482. if (!tcontext) {
  483. printk(KERN_ERR "security_validate_transition: "
  484. " unrecognized SID %d\n", tasksid);
  485. rc = -EINVAL;
  486. goto out;
  487. }
  488. constraint = tclass_datum->validatetrans;
  489. while (constraint) {
  490. if (!constraint_expr_eval(ocontext, ncontext, tcontext,
  491. constraint->expr)) {
  492. rc = security_validtrans_handle_fail(ocontext, ncontext,
  493. tcontext, tclass);
  494. goto out;
  495. }
  496. constraint = constraint->next;
  497. }
  498. out:
  499. POLICY_RDUNLOCK;
  500. return rc;
  501. }
  502. /**
  503. * security_compute_av - Compute access vector decisions.
  504. * @ssid: source security identifier
  505. * @tsid: target security identifier
  506. * @tclass: target security class
  507. * @requested: requested permissions
  508. * @avd: access vector decisions
  509. *
  510. * Compute a set of access vector decisions based on the
  511. * SID pair (@ssid, @tsid) for the permissions in @tclass.
  512. * Return -%EINVAL if any of the parameters are invalid or %0
  513. * if the access vector decisions were computed successfully.
  514. */
  515. int security_compute_av(u32 ssid,
  516. u32 tsid,
  517. u16 tclass,
  518. u32 requested,
  519. struct av_decision *avd)
  520. {
  521. struct context *scontext = NULL, *tcontext = NULL;
  522. int rc = 0;
  523. if (!ss_initialized) {
  524. avd->allowed = 0xffffffff;
  525. avd->decided = 0xffffffff;
  526. avd->auditallow = 0;
  527. avd->auditdeny = 0xffffffff;
  528. avd->seqno = latest_granting;
  529. return 0;
  530. }
  531. POLICY_RDLOCK;
  532. scontext = sidtab_search(&sidtab, ssid);
  533. if (!scontext) {
  534. printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
  535. ssid);
  536. rc = -EINVAL;
  537. goto out;
  538. }
  539. tcontext = sidtab_search(&sidtab, tsid);
  540. if (!tcontext) {
  541. printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
  542. tsid);
  543. rc = -EINVAL;
  544. goto out;
  545. }
  546. rc = context_struct_compute_av(scontext, tcontext, tclass,
  547. requested, avd);
  548. out:
  549. POLICY_RDUNLOCK;
  550. return rc;
  551. }
  552. /*
  553. * Write the security context string representation of
  554. * the context structure `context' into a dynamically
  555. * allocated string of the correct size. Set `*scontext'
  556. * to point to this string and set `*scontext_len' to
  557. * the length of the string.
  558. */
  559. static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
  560. {
  561. char *scontextp;
  562. *scontext = NULL;
  563. *scontext_len = 0;
  564. /* Compute the size of the context. */
  565. *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
  566. *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
  567. *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
  568. *scontext_len += mls_compute_context_len(context);
  569. /* Allocate space for the context; caller must free this space. */
  570. scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
  571. if (!scontextp)
  572. return -ENOMEM;
  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. c->user = usrdatum->value;
  1171. /* Convert the role. */
  1172. role = hashtab_search(args->newp->p_roles.table,
  1173. args->oldp->p_role_val_to_name[c->role - 1]);
  1174. if (!role)
  1175. goto bad;
  1176. c->role = role->value;
  1177. /* Convert the type. */
  1178. typdatum = hashtab_search(args->newp->p_types.table,
  1179. args->oldp->p_type_val_to_name[c->type - 1]);
  1180. if (!typdatum)
  1181. goto bad;
  1182. c->type = typdatum->value;
  1183. rc = mls_convert_context(args->oldp, args->newp, c);
  1184. if (rc)
  1185. goto bad;
  1186. /* Check the validity of the new context. */
  1187. if (!policydb_context_isvalid(args->newp, c)) {
  1188. rc = convert_context_handle_invalid_context(&oldc);
  1189. if (rc)
  1190. goto bad;
  1191. }
  1192. context_destroy(&oldc);
  1193. out:
  1194. return rc;
  1195. bad:
  1196. context_struct_to_string(&oldc, &s, &len);
  1197. context_destroy(&oldc);
  1198. printk(KERN_ERR "SELinux: invalidating context %s\n", s);
  1199. kfree(s);
  1200. goto out;
  1201. }
  1202. static void security_load_policycaps(void)
  1203. {
  1204. selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
  1205. POLICYDB_CAPABILITY_NETPEER);
  1206. selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
  1207. POLICYDB_CAPABILITY_OPENPERM);
  1208. }
  1209. extern void selinux_complete_init(void);
  1210. static int security_preserve_bools(struct policydb *p);
  1211. /**
  1212. * security_load_policy - Load a security policy configuration.
  1213. * @data: binary policy data
  1214. * @len: length of data in bytes
  1215. *
  1216. * Load a new set of security policy configuration data,
  1217. * validate it and convert the SID table as necessary.
  1218. * This function will flush the access vector cache after
  1219. * loading the new policy.
  1220. */
  1221. int security_load_policy(void *data, size_t len)
  1222. {
  1223. struct policydb oldpolicydb, newpolicydb;
  1224. struct sidtab oldsidtab, newsidtab;
  1225. struct convert_context_args args;
  1226. u32 seqno;
  1227. int rc = 0;
  1228. struct policy_file file = { data, len }, *fp = &file;
  1229. LOAD_LOCK;
  1230. if (!ss_initialized) {
  1231. avtab_cache_init();
  1232. if (policydb_read(&policydb, fp)) {
  1233. LOAD_UNLOCK;
  1234. avtab_cache_destroy();
  1235. return -EINVAL;
  1236. }
  1237. if (policydb_load_isids(&policydb, &sidtab)) {
  1238. LOAD_UNLOCK;
  1239. policydb_destroy(&policydb);
  1240. avtab_cache_destroy();
  1241. return -EINVAL;
  1242. }
  1243. /* Verify that the kernel defined classes are correct. */
  1244. if (validate_classes(&policydb)) {
  1245. printk(KERN_ERR
  1246. "SELinux: the definition of a class is incorrect\n");
  1247. LOAD_UNLOCK;
  1248. sidtab_destroy(&sidtab);
  1249. policydb_destroy(&policydb);
  1250. avtab_cache_destroy();
  1251. return -EINVAL;
  1252. }
  1253. security_load_policycaps();
  1254. policydb_loaded_version = policydb.policyvers;
  1255. ss_initialized = 1;
  1256. seqno = ++latest_granting;
  1257. LOAD_UNLOCK;
  1258. selinux_complete_init();
  1259. avc_ss_reset(seqno);
  1260. selnl_notify_policyload(seqno);
  1261. selinux_netlbl_cache_invalidate();
  1262. selinux_xfrm_notify_policyload();
  1263. return 0;
  1264. }
  1265. #if 0
  1266. sidtab_hash_eval(&sidtab, "sids");
  1267. #endif
  1268. if (policydb_read(&newpolicydb, fp)) {
  1269. LOAD_UNLOCK;
  1270. return -EINVAL;
  1271. }
  1272. sidtab_init(&newsidtab);
  1273. /* Verify that the kernel defined classes are correct. */
  1274. if (validate_classes(&newpolicydb)) {
  1275. printk(KERN_ERR
  1276. "SELinux: the definition of a class is incorrect\n");
  1277. rc = -EINVAL;
  1278. goto err;
  1279. }
  1280. rc = security_preserve_bools(&newpolicydb);
  1281. if (rc) {
  1282. printk(KERN_ERR "SELinux: unable to preserve booleans\n");
  1283. goto err;
  1284. }
  1285. /* Clone the SID table. */
  1286. sidtab_shutdown(&sidtab);
  1287. if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
  1288. rc = -ENOMEM;
  1289. goto err;
  1290. }
  1291. /* Convert the internal representations of contexts
  1292. in the new SID table and remove invalid SIDs. */
  1293. args.oldp = &policydb;
  1294. args.newp = &newpolicydb;
  1295. sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
  1296. /* Save the old policydb and SID table to free later. */
  1297. memcpy(&oldpolicydb, &policydb, sizeof policydb);
  1298. sidtab_set(&oldsidtab, &sidtab);
  1299. /* Install the new policydb and SID table. */
  1300. POLICY_WRLOCK;
  1301. memcpy(&policydb, &newpolicydb, sizeof policydb);
  1302. sidtab_set(&sidtab, &newsidtab);
  1303. security_load_policycaps();
  1304. seqno = ++latest_granting;
  1305. policydb_loaded_version = policydb.policyvers;
  1306. POLICY_WRUNLOCK;
  1307. LOAD_UNLOCK;
  1308. /* Free the old policydb and SID table. */
  1309. policydb_destroy(&oldpolicydb);
  1310. sidtab_destroy(&oldsidtab);
  1311. avc_ss_reset(seqno);
  1312. selnl_notify_policyload(seqno);
  1313. selinux_netlbl_cache_invalidate();
  1314. selinux_xfrm_notify_policyload();
  1315. return 0;
  1316. err:
  1317. LOAD_UNLOCK;
  1318. sidtab_destroy(&newsidtab);
  1319. policydb_destroy(&newpolicydb);
  1320. return rc;
  1321. }
  1322. /**
  1323. * security_port_sid - Obtain the SID for a port.
  1324. * @protocol: protocol number
  1325. * @port: port number
  1326. * @out_sid: security identifier
  1327. */
  1328. int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
  1329. {
  1330. struct ocontext *c;
  1331. int rc = 0;
  1332. POLICY_RDLOCK;
  1333. c = policydb.ocontexts[OCON_PORT];
  1334. while (c) {
  1335. if (c->u.port.protocol == protocol &&
  1336. c->u.port.low_port <= port &&
  1337. c->u.port.high_port >= port)
  1338. break;
  1339. c = c->next;
  1340. }
  1341. if (c) {
  1342. if (!c->sid[0]) {
  1343. rc = sidtab_context_to_sid(&sidtab,
  1344. &c->context[0],
  1345. &c->sid[0]);
  1346. if (rc)
  1347. goto out;
  1348. }
  1349. *out_sid = c->sid[0];
  1350. } else {
  1351. *out_sid = SECINITSID_PORT;
  1352. }
  1353. out:
  1354. POLICY_RDUNLOCK;
  1355. return rc;
  1356. }
  1357. /**
  1358. * security_netif_sid - Obtain the SID for a network interface.
  1359. * @name: interface name
  1360. * @if_sid: interface SID
  1361. */
  1362. int security_netif_sid(char *name, u32 *if_sid)
  1363. {
  1364. int rc = 0;
  1365. struct ocontext *c;
  1366. POLICY_RDLOCK;
  1367. c = policydb.ocontexts[OCON_NETIF];
  1368. while (c) {
  1369. if (strcmp(name, c->u.name) == 0)
  1370. break;
  1371. c = c->next;
  1372. }
  1373. if (c) {
  1374. if (!c->sid[0] || !c->sid[1]) {
  1375. rc = sidtab_context_to_sid(&sidtab,
  1376. &c->context[0],
  1377. &c->sid[0]);
  1378. if (rc)
  1379. goto out;
  1380. rc = sidtab_context_to_sid(&sidtab,
  1381. &c->context[1],
  1382. &c->sid[1]);
  1383. if (rc)
  1384. goto out;
  1385. }
  1386. *if_sid = c->sid[0];
  1387. } else
  1388. *if_sid = SECINITSID_NETIF;
  1389. out:
  1390. POLICY_RDUNLOCK;
  1391. return rc;
  1392. }
  1393. static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
  1394. {
  1395. int i, fail = 0;
  1396. for (i = 0; i < 4; i++)
  1397. if (addr[i] != (input[i] & mask[i])) {
  1398. fail = 1;
  1399. break;
  1400. }
  1401. return !fail;
  1402. }
  1403. /**
  1404. * security_node_sid - Obtain the SID for a node (host).
  1405. * @domain: communication domain aka address family
  1406. * @addrp: address
  1407. * @addrlen: address length in bytes
  1408. * @out_sid: security identifier
  1409. */
  1410. int security_node_sid(u16 domain,
  1411. void *addrp,
  1412. u32 addrlen,
  1413. u32 *out_sid)
  1414. {
  1415. int rc = 0;
  1416. struct ocontext *c;
  1417. POLICY_RDLOCK;
  1418. switch (domain) {
  1419. case AF_INET: {
  1420. u32 addr;
  1421. if (addrlen != sizeof(u32)) {
  1422. rc = -EINVAL;
  1423. goto out;
  1424. }
  1425. addr = *((u32 *)addrp);
  1426. c = policydb.ocontexts[OCON_NODE];
  1427. while (c) {
  1428. if (c->u.node.addr == (addr & c->u.node.mask))
  1429. break;
  1430. c = c->next;
  1431. }
  1432. break;
  1433. }
  1434. case AF_INET6:
  1435. if (addrlen != sizeof(u64) * 2) {
  1436. rc = -EINVAL;
  1437. goto out;
  1438. }
  1439. c = policydb.ocontexts[OCON_NODE6];
  1440. while (c) {
  1441. if (match_ipv6_addrmask(addrp, c->u.node6.addr,
  1442. c->u.node6.mask))
  1443. break;
  1444. c = c->next;
  1445. }
  1446. break;
  1447. default:
  1448. *out_sid = SECINITSID_NODE;
  1449. goto out;
  1450. }
  1451. if (c) {
  1452. if (!c->sid[0]) {
  1453. rc = sidtab_context_to_sid(&sidtab,
  1454. &c->context[0],
  1455. &c->sid[0]);
  1456. if (rc)
  1457. goto out;
  1458. }
  1459. *out_sid = c->sid[0];
  1460. } else {
  1461. *out_sid = SECINITSID_NODE;
  1462. }
  1463. out:
  1464. POLICY_RDUNLOCK;
  1465. return rc;
  1466. }
  1467. #define SIDS_NEL 25
  1468. /**
  1469. * security_get_user_sids - Obtain reachable SIDs for a user.
  1470. * @fromsid: starting SID
  1471. * @username: username
  1472. * @sids: array of reachable SIDs for user
  1473. * @nel: number of elements in @sids
  1474. *
  1475. * Generate the set of SIDs for legal security contexts
  1476. * for a given user that can be reached by @fromsid.
  1477. * Set *@sids to point to a dynamically allocated
  1478. * array containing the set of SIDs. Set *@nel to the
  1479. * number of elements in the array.
  1480. */
  1481. int security_get_user_sids(u32 fromsid,
  1482. char *username,
  1483. u32 **sids,
  1484. u32 *nel)
  1485. {
  1486. struct context *fromcon, usercon;
  1487. u32 *mysids = NULL, *mysids2, sid;
  1488. u32 mynel = 0, maxnel = SIDS_NEL;
  1489. struct user_datum *user;
  1490. struct role_datum *role;
  1491. struct ebitmap_node *rnode, *tnode;
  1492. int rc = 0, i, j;
  1493. *sids = NULL;
  1494. *nel = 0;
  1495. if (!ss_initialized)
  1496. goto out;
  1497. POLICY_RDLOCK;
  1498. fromcon = sidtab_search(&sidtab, fromsid);
  1499. if (!fromcon) {
  1500. rc = -EINVAL;
  1501. goto out_unlock;
  1502. }
  1503. user = hashtab_search(policydb.p_users.table, username);
  1504. if (!user) {
  1505. rc = -EINVAL;
  1506. goto out_unlock;
  1507. }
  1508. usercon.user = user->value;
  1509. mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
  1510. if (!mysids) {
  1511. rc = -ENOMEM;
  1512. goto out_unlock;
  1513. }
  1514. ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
  1515. role = policydb.role_val_to_struct[i];
  1516. usercon.role = i+1;
  1517. ebitmap_for_each_positive_bit(&role->types, tnode, j) {
  1518. usercon.type = j+1;
  1519. if (mls_setup_user_range(fromcon, user, &usercon))
  1520. continue;
  1521. rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
  1522. if (rc)
  1523. goto out_unlock;
  1524. if (mynel < maxnel) {
  1525. mysids[mynel++] = sid;
  1526. } else {
  1527. maxnel += SIDS_NEL;
  1528. mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
  1529. if (!mysids2) {
  1530. rc = -ENOMEM;
  1531. goto out_unlock;
  1532. }
  1533. memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
  1534. kfree(mysids);
  1535. mysids = mysids2;
  1536. mysids[mynel++] = sid;
  1537. }
  1538. }
  1539. }
  1540. out_unlock:
  1541. POLICY_RDUNLOCK;
  1542. if (rc || !mynel) {
  1543. kfree(mysids);
  1544. goto out;
  1545. }
  1546. mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
  1547. if (!mysids2) {
  1548. rc = -ENOMEM;
  1549. kfree(mysids);
  1550. goto out;
  1551. }
  1552. for (i = 0, j = 0; i < mynel; i++) {
  1553. rc = avc_has_perm_noaudit(fromsid, mysids[i],
  1554. SECCLASS_PROCESS,
  1555. PROCESS__TRANSITION, AVC_STRICT,
  1556. NULL);
  1557. if (!rc)
  1558. mysids2[j++] = mysids[i];
  1559. cond_resched();
  1560. }
  1561. rc = 0;
  1562. kfree(mysids);
  1563. *sids = mysids2;
  1564. *nel = j;
  1565. out:
  1566. return rc;
  1567. }
  1568. /**
  1569. * security_genfs_sid - Obtain a SID for a file in a filesystem
  1570. * @fstype: filesystem type
  1571. * @path: path from root of mount
  1572. * @sclass: file security class
  1573. * @sid: SID for path
  1574. *
  1575. * Obtain a SID to use for a file in a filesystem that
  1576. * cannot support xattr or use a fixed labeling behavior like
  1577. * transition SIDs or task SIDs.
  1578. */
  1579. int security_genfs_sid(const char *fstype,
  1580. char *path,
  1581. u16 sclass,
  1582. u32 *sid)
  1583. {
  1584. int len;
  1585. struct genfs *genfs;
  1586. struct ocontext *c;
  1587. int rc = 0, cmp = 0;
  1588. while (path[0] == '/' && path[1] == '/')
  1589. path++;
  1590. POLICY_RDLOCK;
  1591. for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
  1592. cmp = strcmp(fstype, genfs->fstype);
  1593. if (cmp <= 0)
  1594. break;
  1595. }
  1596. if (!genfs || cmp) {
  1597. *sid = SECINITSID_UNLABELED;
  1598. rc = -ENOENT;
  1599. goto out;
  1600. }
  1601. for (c = genfs->head; c; c = c->next) {
  1602. len = strlen(c->u.name);
  1603. if ((!c->v.sclass || sclass == c->v.sclass) &&
  1604. (strncmp(c->u.name, path, len) == 0))
  1605. break;
  1606. }
  1607. if (!c) {
  1608. *sid = SECINITSID_UNLABELED;
  1609. rc = -ENOENT;
  1610. goto out;
  1611. }
  1612. if (!c->sid[0]) {
  1613. rc = sidtab_context_to_sid(&sidtab,
  1614. &c->context[0],
  1615. &c->sid[0]);
  1616. if (rc)
  1617. goto out;
  1618. }
  1619. *sid = c->sid[0];
  1620. out:
  1621. POLICY_RDUNLOCK;
  1622. return rc;
  1623. }
  1624. /**
  1625. * security_fs_use - Determine how to handle labeling for a filesystem.
  1626. * @fstype: filesystem type
  1627. * @behavior: labeling behavior
  1628. * @sid: SID for filesystem (superblock)
  1629. */
  1630. int security_fs_use(
  1631. const char *fstype,
  1632. unsigned int *behavior,
  1633. u32 *sid)
  1634. {
  1635. int rc = 0;
  1636. struct ocontext *c;
  1637. POLICY_RDLOCK;
  1638. c = policydb.ocontexts[OCON_FSUSE];
  1639. while (c) {
  1640. if (strcmp(fstype, c->u.name) == 0)
  1641. break;
  1642. c = c->next;
  1643. }
  1644. if (c) {
  1645. *behavior = c->v.behavior;
  1646. if (!c->sid[0]) {
  1647. rc = sidtab_context_to_sid(&sidtab,
  1648. &c->context[0],
  1649. &c->sid[0]);
  1650. if (rc)
  1651. goto out;
  1652. }
  1653. *sid = c->sid[0];
  1654. } else {
  1655. rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
  1656. if (rc) {
  1657. *behavior = SECURITY_FS_USE_NONE;
  1658. rc = 0;
  1659. } else {
  1660. *behavior = SECURITY_FS_USE_GENFS;
  1661. }
  1662. }
  1663. out:
  1664. POLICY_RDUNLOCK;
  1665. return rc;
  1666. }
  1667. int security_get_bools(int *len, char ***names, int **values)
  1668. {
  1669. int i, rc = -ENOMEM;
  1670. POLICY_RDLOCK;
  1671. *names = NULL;
  1672. *values = NULL;
  1673. *len = policydb.p_bools.nprim;
  1674. if (!*len) {
  1675. rc = 0;
  1676. goto out;
  1677. }
  1678. *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
  1679. if (!*names)
  1680. goto err;
  1681. *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
  1682. if (!*values)
  1683. goto err;
  1684. for (i = 0; i < *len; i++) {
  1685. size_t name_len;
  1686. (*values)[i] = policydb.bool_val_to_struct[i]->state;
  1687. name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
  1688. (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
  1689. if (!(*names)[i])
  1690. goto err;
  1691. strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
  1692. (*names)[i][name_len - 1] = 0;
  1693. }
  1694. rc = 0;
  1695. out:
  1696. POLICY_RDUNLOCK;
  1697. return rc;
  1698. err:
  1699. if (*names) {
  1700. for (i = 0; i < *len; i++)
  1701. kfree((*names)[i]);
  1702. }
  1703. kfree(*values);
  1704. goto out;
  1705. }
  1706. int security_set_bools(int len, int *values)
  1707. {
  1708. int i, rc = 0;
  1709. int lenp, seqno = 0;
  1710. struct cond_node *cur;
  1711. POLICY_WRLOCK;
  1712. lenp = policydb.p_bools.nprim;
  1713. if (len != lenp) {
  1714. rc = -EFAULT;
  1715. goto out;
  1716. }
  1717. for (i = 0; i < len; i++) {
  1718. if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
  1719. audit_log(current->audit_context, GFP_ATOMIC,
  1720. AUDIT_MAC_CONFIG_CHANGE,
  1721. "bool=%s val=%d old_val=%d auid=%u ses=%u",
  1722. policydb.p_bool_val_to_name[i],
  1723. !!values[i],
  1724. policydb.bool_val_to_struct[i]->state,
  1725. audit_get_loginuid(current),
  1726. audit_get_sessionid(current));
  1727. }
  1728. if (values[i])
  1729. policydb.bool_val_to_struct[i]->state = 1;
  1730. else
  1731. policydb.bool_val_to_struct[i]->state = 0;
  1732. }
  1733. for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
  1734. rc = evaluate_cond_node(&policydb, cur);
  1735. if (rc)
  1736. goto out;
  1737. }
  1738. seqno = ++latest_granting;
  1739. out:
  1740. POLICY_WRUNLOCK;
  1741. if (!rc) {
  1742. avc_ss_reset(seqno);
  1743. selnl_notify_policyload(seqno);
  1744. selinux_xfrm_notify_policyload();
  1745. }
  1746. return rc;
  1747. }
  1748. int security_get_bool_value(int bool)
  1749. {
  1750. int rc = 0;
  1751. int len;
  1752. POLICY_RDLOCK;
  1753. len = policydb.p_bools.nprim;
  1754. if (bool >= len) {
  1755. rc = -EFAULT;
  1756. goto out;
  1757. }
  1758. rc = policydb.bool_val_to_struct[bool]->state;
  1759. out:
  1760. POLICY_RDUNLOCK;
  1761. return rc;
  1762. }
  1763. static int security_preserve_bools(struct policydb *p)
  1764. {
  1765. int rc, nbools = 0, *bvalues = NULL, i;
  1766. char **bnames = NULL;
  1767. struct cond_bool_datum *booldatum;
  1768. struct cond_node *cur;
  1769. rc = security_get_bools(&nbools, &bnames, &bvalues);
  1770. if (rc)
  1771. goto out;
  1772. for (i = 0; i < nbools; i++) {
  1773. booldatum = hashtab_search(p->p_bools.table, bnames[i]);
  1774. if (booldatum)
  1775. booldatum->state = bvalues[i];
  1776. }
  1777. for (cur = p->cond_list; cur != NULL; cur = cur->next) {
  1778. rc = evaluate_cond_node(p, cur);
  1779. if (rc)
  1780. goto out;
  1781. }
  1782. out:
  1783. if (bnames) {
  1784. for (i = 0; i < nbools; i++)
  1785. kfree(bnames[i]);
  1786. }
  1787. kfree(bnames);
  1788. kfree(bvalues);
  1789. return rc;
  1790. }
  1791. /*
  1792. * security_sid_mls_copy() - computes a new sid based on the given
  1793. * sid and the mls portion of mls_sid.
  1794. */
  1795. int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
  1796. {
  1797. struct context *context1;
  1798. struct context *context2;
  1799. struct context newcon;
  1800. char *s;
  1801. u32 len;
  1802. int rc = 0;
  1803. if (!ss_initialized || !selinux_mls_enabled) {
  1804. *new_sid = sid;
  1805. goto out;
  1806. }
  1807. context_init(&newcon);
  1808. POLICY_RDLOCK;
  1809. context1 = sidtab_search(&sidtab, sid);
  1810. if (!context1) {
  1811. printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
  1812. "%d\n", sid);
  1813. rc = -EINVAL;
  1814. goto out_unlock;
  1815. }
  1816. context2 = sidtab_search(&sidtab, mls_sid);
  1817. if (!context2) {
  1818. printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
  1819. "%d\n", mls_sid);
  1820. rc = -EINVAL;
  1821. goto out_unlock;
  1822. }
  1823. newcon.user = context1->user;
  1824. newcon.role = context1->role;
  1825. newcon.type = context1->type;
  1826. rc = mls_context_cpy(&newcon, context2);
  1827. if (rc)
  1828. goto out_unlock;
  1829. /* Check the validity of the new context. */
  1830. if (!policydb_context_isvalid(&policydb, &newcon)) {
  1831. rc = convert_context_handle_invalid_context(&newcon);
  1832. if (rc)
  1833. goto bad;
  1834. }
  1835. rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
  1836. goto out_unlock;
  1837. bad:
  1838. if (!context_struct_to_string(&newcon, &s, &len)) {
  1839. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1840. "security_sid_mls_copy: invalid context %s", s);
  1841. kfree(s);
  1842. }
  1843. out_unlock:
  1844. POLICY_RDUNLOCK;
  1845. context_destroy(&newcon);
  1846. out:
  1847. return rc;
  1848. }
  1849. /**
  1850. * security_net_peersid_resolve - Compare and resolve two network peer SIDs
  1851. * @nlbl_sid: NetLabel SID
  1852. * @nlbl_type: NetLabel labeling protocol type
  1853. * @xfrm_sid: XFRM SID
  1854. *
  1855. * Description:
  1856. * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
  1857. * resolved into a single SID it is returned via @peer_sid and the function
  1858. * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
  1859. * returns a negative value. A table summarizing the behavior is below:
  1860. *
  1861. * | function return | @sid
  1862. * ------------------------------+-----------------+-----------------
  1863. * no peer labels | 0 | SECSID_NULL
  1864. * single peer label | 0 | <peer_label>
  1865. * multiple, consistent labels | 0 | <peer_label>
  1866. * multiple, inconsistent labels | -<errno> | SECSID_NULL
  1867. *
  1868. */
  1869. int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
  1870. u32 xfrm_sid,
  1871. u32 *peer_sid)
  1872. {
  1873. int rc;
  1874. struct context *nlbl_ctx;
  1875. struct context *xfrm_ctx;
  1876. /* handle the common (which also happens to be the set of easy) cases
  1877. * right away, these two if statements catch everything involving a
  1878. * single or absent peer SID/label */
  1879. if (xfrm_sid == SECSID_NULL) {
  1880. *peer_sid = nlbl_sid;
  1881. return 0;
  1882. }
  1883. /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
  1884. * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
  1885. * is present */
  1886. if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
  1887. *peer_sid = xfrm_sid;
  1888. return 0;
  1889. }
  1890. /* we don't need to check ss_initialized here since the only way both
  1891. * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
  1892. * security server was initialized and ss_initialized was true */
  1893. if (!selinux_mls_enabled) {
  1894. *peer_sid = SECSID_NULL;
  1895. return 0;
  1896. }
  1897. POLICY_RDLOCK;
  1898. nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
  1899. if (!nlbl_ctx) {
  1900. printk(KERN_ERR
  1901. "security_sid_mls_cmp: unrecognized SID %d\n",
  1902. nlbl_sid);
  1903. rc = -EINVAL;
  1904. goto out_slowpath;
  1905. }
  1906. xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
  1907. if (!xfrm_ctx) {
  1908. printk(KERN_ERR
  1909. "security_sid_mls_cmp: unrecognized SID %d\n",
  1910. xfrm_sid);
  1911. rc = -EINVAL;
  1912. goto out_slowpath;
  1913. }
  1914. rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
  1915. out_slowpath:
  1916. POLICY_RDUNLOCK;
  1917. if (rc == 0)
  1918. /* at present NetLabel SIDs/labels really only carry MLS
  1919. * information so if the MLS portion of the NetLabel SID
  1920. * matches the MLS portion of the labeled XFRM SID/label
  1921. * then pass along the XFRM SID as it is the most
  1922. * expressive */
  1923. *peer_sid = xfrm_sid;
  1924. else
  1925. *peer_sid = SECSID_NULL;
  1926. return rc;
  1927. }
  1928. static int get_classes_callback(void *k, void *d, void *args)
  1929. {
  1930. struct class_datum *datum = d;
  1931. char *name = k, **classes = args;
  1932. int value = datum->value - 1;
  1933. classes[value] = kstrdup(name, GFP_ATOMIC);
  1934. if (!classes[value])
  1935. return -ENOMEM;
  1936. return 0;
  1937. }
  1938. int security_get_classes(char ***classes, int *nclasses)
  1939. {
  1940. int rc = -ENOMEM;
  1941. POLICY_RDLOCK;
  1942. *nclasses = policydb.p_classes.nprim;
  1943. *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
  1944. if (!*classes)
  1945. goto out;
  1946. rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
  1947. *classes);
  1948. if (rc < 0) {
  1949. int i;
  1950. for (i = 0; i < *nclasses; i++)
  1951. kfree((*classes)[i]);
  1952. kfree(*classes);
  1953. }
  1954. out:
  1955. POLICY_RDUNLOCK;
  1956. return rc;
  1957. }
  1958. static int get_permissions_callback(void *k, void *d, void *args)
  1959. {
  1960. struct perm_datum *datum = d;
  1961. char *name = k, **perms = args;
  1962. int value = datum->value - 1;
  1963. perms[value] = kstrdup(name, GFP_ATOMIC);
  1964. if (!perms[value])
  1965. return -ENOMEM;
  1966. return 0;
  1967. }
  1968. int security_get_permissions(char *class, char ***perms, int *nperms)
  1969. {
  1970. int rc = -ENOMEM, i;
  1971. struct class_datum *match;
  1972. POLICY_RDLOCK;
  1973. match = hashtab_search(policydb.p_classes.table, class);
  1974. if (!match) {
  1975. printk(KERN_ERR "%s: unrecognized class %s\n",
  1976. __func__, class);
  1977. rc = -EINVAL;
  1978. goto out;
  1979. }
  1980. *nperms = match->permissions.nprim;
  1981. *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
  1982. if (!*perms)
  1983. goto out;
  1984. if (match->comdatum) {
  1985. rc = hashtab_map(match->comdatum->permissions.table,
  1986. get_permissions_callback, *perms);
  1987. if (rc < 0)
  1988. goto err;
  1989. }
  1990. rc = hashtab_map(match->permissions.table, get_permissions_callback,
  1991. *perms);
  1992. if (rc < 0)
  1993. goto err;
  1994. out:
  1995. POLICY_RDUNLOCK;
  1996. return rc;
  1997. err:
  1998. POLICY_RDUNLOCK;
  1999. for (i = 0; i < *nperms; i++)
  2000. kfree((*perms)[i]);
  2001. kfree(*perms);
  2002. return rc;
  2003. }
  2004. int security_get_reject_unknown(void)
  2005. {
  2006. return policydb.reject_unknown;
  2007. }
  2008. int security_get_allow_unknown(void)
  2009. {
  2010. return policydb.allow_unknown;
  2011. }
  2012. /**
  2013. * security_policycap_supported - Check for a specific policy capability
  2014. * @req_cap: capability
  2015. *
  2016. * Description:
  2017. * This function queries the currently loaded policy to see if it supports the
  2018. * capability specified by @req_cap. Returns true (1) if the capability is
  2019. * supported, false (0) if it isn't supported.
  2020. *
  2021. */
  2022. int security_policycap_supported(unsigned int req_cap)
  2023. {
  2024. int rc;
  2025. POLICY_RDLOCK;
  2026. rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
  2027. POLICY_RDUNLOCK;
  2028. return rc;
  2029. }
  2030. struct selinux_audit_rule {
  2031. u32 au_seqno;
  2032. struct context au_ctxt;
  2033. };
  2034. void selinux_audit_rule_free(void *vrule)
  2035. {
  2036. struct selinux_audit_rule *rule = vrule;
  2037. if (rule) {
  2038. context_destroy(&rule->au_ctxt);
  2039. kfree(rule);
  2040. }
  2041. }
  2042. int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
  2043. {
  2044. struct selinux_audit_rule *tmprule;
  2045. struct role_datum *roledatum;
  2046. struct type_datum *typedatum;
  2047. struct user_datum *userdatum;
  2048. struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
  2049. int rc = 0;
  2050. *rule = NULL;
  2051. if (!ss_initialized)
  2052. return -EOPNOTSUPP;
  2053. switch (field) {
  2054. case AUDIT_SUBJ_USER:
  2055. case AUDIT_SUBJ_ROLE:
  2056. case AUDIT_SUBJ_TYPE:
  2057. case AUDIT_OBJ_USER:
  2058. case AUDIT_OBJ_ROLE:
  2059. case AUDIT_OBJ_TYPE:
  2060. /* only 'equals' and 'not equals' fit user, role, and type */
  2061. if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
  2062. return -EINVAL;
  2063. break;
  2064. case AUDIT_SUBJ_SEN:
  2065. case AUDIT_SUBJ_CLR:
  2066. case AUDIT_OBJ_LEV_LOW:
  2067. case AUDIT_OBJ_LEV_HIGH:
  2068. /* we do not allow a range, indicated by the presense of '-' */
  2069. if (strchr(rulestr, '-'))
  2070. return -EINVAL;
  2071. break;
  2072. default:
  2073. /* only the above fields are valid */
  2074. return -EINVAL;
  2075. }
  2076. tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
  2077. if (!tmprule)
  2078. return -ENOMEM;
  2079. context_init(&tmprule->au_ctxt);
  2080. POLICY_RDLOCK;
  2081. tmprule->au_seqno = latest_granting;
  2082. switch (field) {
  2083. case AUDIT_SUBJ_USER:
  2084. case AUDIT_OBJ_USER:
  2085. userdatum = hashtab_search(policydb.p_users.table, rulestr);
  2086. if (!userdatum)
  2087. rc = -EINVAL;
  2088. else
  2089. tmprule->au_ctxt.user = userdatum->value;
  2090. break;
  2091. case AUDIT_SUBJ_ROLE:
  2092. case AUDIT_OBJ_ROLE:
  2093. roledatum = hashtab_search(policydb.p_roles.table, rulestr);
  2094. if (!roledatum)
  2095. rc = -EINVAL;
  2096. else
  2097. tmprule->au_ctxt.role = roledatum->value;
  2098. break;
  2099. case AUDIT_SUBJ_TYPE:
  2100. case AUDIT_OBJ_TYPE:
  2101. typedatum = hashtab_search(policydb.p_types.table, rulestr);
  2102. if (!typedatum)
  2103. rc = -EINVAL;
  2104. else
  2105. tmprule->au_ctxt.type = typedatum->value;
  2106. break;
  2107. case AUDIT_SUBJ_SEN:
  2108. case AUDIT_SUBJ_CLR:
  2109. case AUDIT_OBJ_LEV_LOW:
  2110. case AUDIT_OBJ_LEV_HIGH:
  2111. rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
  2112. break;
  2113. }
  2114. POLICY_RDUNLOCK;
  2115. if (rc) {
  2116. selinux_audit_rule_free(tmprule);
  2117. tmprule = NULL;
  2118. }
  2119. *rule = tmprule;
  2120. return rc;
  2121. }
  2122. /* Check to see if the rule contains any selinux fields */
  2123. int selinux_audit_rule_known(struct audit_krule *rule)
  2124. {
  2125. int i;
  2126. for (i = 0; i < rule->field_count; i++) {
  2127. struct audit_field *f = &rule->fields[i];
  2128. switch (f->type) {
  2129. case AUDIT_SUBJ_USER:
  2130. case AUDIT_SUBJ_ROLE:
  2131. case AUDIT_SUBJ_TYPE:
  2132. case AUDIT_SUBJ_SEN:
  2133. case AUDIT_SUBJ_CLR:
  2134. case AUDIT_OBJ_USER:
  2135. case AUDIT_OBJ_ROLE:
  2136. case AUDIT_OBJ_TYPE:
  2137. case AUDIT_OBJ_LEV_LOW:
  2138. case AUDIT_OBJ_LEV_HIGH:
  2139. return 1;
  2140. }
  2141. }
  2142. return 0;
  2143. }
  2144. int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
  2145. struct audit_context *actx)
  2146. {
  2147. struct context *ctxt;
  2148. struct mls_level *level;
  2149. struct selinux_audit_rule *rule = vrule;
  2150. int match = 0;
  2151. if (!rule) {
  2152. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  2153. "selinux_audit_rule_match: missing rule\n");
  2154. return -ENOENT;
  2155. }
  2156. POLICY_RDLOCK;
  2157. if (rule->au_seqno < latest_granting) {
  2158. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  2159. "selinux_audit_rule_match: stale rule\n");
  2160. match = -ESTALE;
  2161. goto out;
  2162. }
  2163. ctxt = sidtab_search(&sidtab, sid);
  2164. if (!ctxt) {
  2165. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  2166. "selinux_audit_rule_match: unrecognized SID %d\n",
  2167. sid);
  2168. match = -ENOENT;
  2169. goto out;
  2170. }
  2171. /* a field/op pair that is not caught here will simply fall through
  2172. without a match */
  2173. switch (field) {
  2174. case AUDIT_SUBJ_USER:
  2175. case AUDIT_OBJ_USER:
  2176. switch (op) {
  2177. case AUDIT_EQUAL:
  2178. match = (ctxt->user == rule->au_ctxt.user);
  2179. break;
  2180. case AUDIT_NOT_EQUAL:
  2181. match = (ctxt->user != rule->au_ctxt.user);
  2182. break;
  2183. }
  2184. break;
  2185. case AUDIT_SUBJ_ROLE:
  2186. case AUDIT_OBJ_ROLE:
  2187. switch (op) {
  2188. case AUDIT_EQUAL:
  2189. match = (ctxt->role == rule->au_ctxt.role);
  2190. break;
  2191. case AUDIT_NOT_EQUAL:
  2192. match = (ctxt->role != rule->au_ctxt.role);
  2193. break;
  2194. }
  2195. break;
  2196. case AUDIT_SUBJ_TYPE:
  2197. case AUDIT_OBJ_TYPE:
  2198. switch (op) {
  2199. case AUDIT_EQUAL:
  2200. match = (ctxt->type == rule->au_ctxt.type);
  2201. break;
  2202. case AUDIT_NOT_EQUAL:
  2203. match = (ctxt->type != rule->au_ctxt.type);
  2204. break;
  2205. }
  2206. break;
  2207. case AUDIT_SUBJ_SEN:
  2208. case AUDIT_SUBJ_CLR:
  2209. case AUDIT_OBJ_LEV_LOW:
  2210. case AUDIT_OBJ_LEV_HIGH:
  2211. level = ((field == AUDIT_SUBJ_SEN ||
  2212. field == AUDIT_OBJ_LEV_LOW) ?
  2213. &ctxt->range.level[0] : &ctxt->range.level[1]);
  2214. switch (op) {
  2215. case AUDIT_EQUAL:
  2216. match = mls_level_eq(&rule->au_ctxt.range.level[0],
  2217. level);
  2218. break;
  2219. case AUDIT_NOT_EQUAL:
  2220. match = !mls_level_eq(&rule->au_ctxt.range.level[0],
  2221. level);
  2222. break;
  2223. case AUDIT_LESS_THAN:
  2224. match = (mls_level_dom(&rule->au_ctxt.range.level[0],
  2225. level) &&
  2226. !mls_level_eq(&rule->au_ctxt.range.level[0],
  2227. level));
  2228. break;
  2229. case AUDIT_LESS_THAN_OR_EQUAL:
  2230. match = mls_level_dom(&rule->au_ctxt.range.level[0],
  2231. level);
  2232. break;
  2233. case AUDIT_GREATER_THAN:
  2234. match = (mls_level_dom(level,
  2235. &rule->au_ctxt.range.level[0]) &&
  2236. !mls_level_eq(level,
  2237. &rule->au_ctxt.range.level[0]));
  2238. break;
  2239. case AUDIT_GREATER_THAN_OR_EQUAL:
  2240. match = mls_level_dom(level,
  2241. &rule->au_ctxt.range.level[0]);
  2242. break;
  2243. }
  2244. }
  2245. out:
  2246. POLICY_RDUNLOCK;
  2247. return match;
  2248. }
  2249. static int (*aurule_callback)(void) = audit_update_lsm_rules;
  2250. static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
  2251. u16 class, u32 perms, u32 *retained)
  2252. {
  2253. int err = 0;
  2254. if (event == AVC_CALLBACK_RESET && aurule_callback)
  2255. err = aurule_callback();
  2256. return err;
  2257. }
  2258. static int __init aurule_init(void)
  2259. {
  2260. int err;
  2261. err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
  2262. SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
  2263. if (err)
  2264. panic("avc_add_callback() failed, error %d\n", err);
  2265. return err;
  2266. }
  2267. __initcall(aurule_init);
  2268. #ifdef CONFIG_NETLABEL
  2269. /**
  2270. * security_netlbl_cache_add - Add an entry to the NetLabel cache
  2271. * @secattr: the NetLabel packet security attributes
  2272. * @sid: the SELinux SID
  2273. *
  2274. * Description:
  2275. * Attempt to cache the context in @ctx, which was derived from the packet in
  2276. * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
  2277. * already been initialized.
  2278. *
  2279. */
  2280. static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
  2281. u32 sid)
  2282. {
  2283. u32 *sid_cache;
  2284. sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
  2285. if (sid_cache == NULL)
  2286. return;
  2287. secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
  2288. if (secattr->cache == NULL) {
  2289. kfree(sid_cache);
  2290. return;
  2291. }
  2292. *sid_cache = sid;
  2293. secattr->cache->free = kfree;
  2294. secattr->cache->data = sid_cache;
  2295. secattr->flags |= NETLBL_SECATTR_CACHE;
  2296. }
  2297. /**
  2298. * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
  2299. * @secattr: the NetLabel packet security attributes
  2300. * @sid: the SELinux SID
  2301. *
  2302. * Description:
  2303. * Convert the given NetLabel security attributes in @secattr into a
  2304. * SELinux SID. If the @secattr field does not contain a full SELinux
  2305. * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
  2306. * 'cache' field of @secattr is set and the CACHE flag is set; this is to
  2307. * allow the @secattr to be used by NetLabel to cache the secattr to SID
  2308. * conversion for future lookups. Returns zero on success, negative values on
  2309. * failure.
  2310. *
  2311. */
  2312. int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
  2313. u32 *sid)
  2314. {
  2315. int rc = -EIDRM;
  2316. struct context *ctx;
  2317. struct context ctx_new;
  2318. if (!ss_initialized) {
  2319. *sid = SECSID_NULL;
  2320. return 0;
  2321. }
  2322. POLICY_RDLOCK;
  2323. if (secattr->flags & NETLBL_SECATTR_CACHE) {
  2324. *sid = *(u32 *)secattr->cache->data;
  2325. rc = 0;
  2326. } else if (secattr->flags & NETLBL_SECATTR_SECID) {
  2327. *sid = secattr->attr.secid;
  2328. rc = 0;
  2329. } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
  2330. ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
  2331. if (ctx == NULL)
  2332. goto netlbl_secattr_to_sid_return;
  2333. ctx_new.user = ctx->user;
  2334. ctx_new.role = ctx->role;
  2335. ctx_new.type = ctx->type;
  2336. mls_import_netlbl_lvl(&ctx_new, secattr);
  2337. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  2338. if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
  2339. secattr->attr.mls.cat) != 0)
  2340. goto netlbl_secattr_to_sid_return;
  2341. ctx_new.range.level[1].cat.highbit =
  2342. ctx_new.range.level[0].cat.highbit;
  2343. ctx_new.range.level[1].cat.node =
  2344. ctx_new.range.level[0].cat.node;
  2345. } else {
  2346. ebitmap_init(&ctx_new.range.level[0].cat);
  2347. ebitmap_init(&ctx_new.range.level[1].cat);
  2348. }
  2349. if (mls_context_isvalid(&policydb, &ctx_new) != 1)
  2350. goto netlbl_secattr_to_sid_return_cleanup;
  2351. rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
  2352. if (rc != 0)
  2353. goto netlbl_secattr_to_sid_return_cleanup;
  2354. security_netlbl_cache_add(secattr, *sid);
  2355. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2356. } else {
  2357. *sid = SECSID_NULL;
  2358. rc = 0;
  2359. }
  2360. netlbl_secattr_to_sid_return:
  2361. POLICY_RDUNLOCK;
  2362. return rc;
  2363. netlbl_secattr_to_sid_return_cleanup:
  2364. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2365. goto netlbl_secattr_to_sid_return;
  2366. }
  2367. /**
  2368. * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
  2369. * @sid: the SELinux SID
  2370. * @secattr: the NetLabel packet security attributes
  2371. *
  2372. * Description:
  2373. * Convert the given SELinux SID in @sid into a NetLabel security attribute.
  2374. * Returns zero on success, negative values on failure.
  2375. *
  2376. */
  2377. int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
  2378. {
  2379. int rc = -ENOENT;
  2380. struct context *ctx;
  2381. if (!ss_initialized)
  2382. return 0;
  2383. POLICY_RDLOCK;
  2384. ctx = sidtab_search(&sidtab, sid);
  2385. if (ctx == NULL)
  2386. goto netlbl_sid_to_secattr_failure;
  2387. secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
  2388. GFP_ATOMIC);
  2389. secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY;
  2390. mls_export_netlbl_lvl(ctx, secattr);
  2391. rc = mls_export_netlbl_cat(ctx, secattr);
  2392. if (rc != 0)
  2393. goto netlbl_sid_to_secattr_failure;
  2394. POLICY_RDUNLOCK;
  2395. return 0;
  2396. netlbl_sid_to_secattr_failure:
  2397. POLICY_RDUNLOCK;
  2398. return rc;
  2399. }
  2400. #endif /* CONFIG_NETLABEL */