services.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449
  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@paul-moore.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. * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com>
  26. *
  27. * Added support for bounds domain and audit messaged on masked permissions
  28. *
  29. * Updated: Guido Trentalancia <guido@trentalancia.com>
  30. *
  31. * Added support for runtime switching of the policy type
  32. *
  33. * Copyright (C) 2008, 2009 NEC Corporation
  34. * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
  35. * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
  36. * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
  37. * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
  38. * This program is free software; you can redistribute it and/or modify
  39. * it under the terms of the GNU General Public License as published by
  40. * the Free Software Foundation, version 2.
  41. */
  42. #include <linux/kernel.h>
  43. #include <linux/slab.h>
  44. #include <linux/string.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/rcupdate.h>
  47. #include <linux/errno.h>
  48. #include <linux/in.h>
  49. #include <linux/sched.h>
  50. #include <linux/audit.h>
  51. #include <linux/mutex.h>
  52. #include <linux/selinux.h>
  53. #include <linux/flex_array.h>
  54. #include <linux/vmalloc.h>
  55. #include <net/netlabel.h>
  56. #include "flask.h"
  57. #include "avc.h"
  58. #include "avc_ss.h"
  59. #include "security.h"
  60. #include "context.h"
  61. #include "policydb.h"
  62. #include "sidtab.h"
  63. #include "services.h"
  64. #include "conditional.h"
  65. #include "mls.h"
  66. #include "objsec.h"
  67. #include "netlabel.h"
  68. #include "xfrm.h"
  69. #include "ebitmap.h"
  70. #include "audit.h"
  71. int selinux_policycap_netpeer;
  72. int selinux_policycap_openperm;
  73. int selinux_policycap_alwaysnetwork;
  74. static DEFINE_RWLOCK(policy_rwlock);
  75. static struct sidtab sidtab;
  76. struct policydb policydb;
  77. int ss_initialized;
  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;
  85. /* Forward declaration. */
  86. static int context_struct_to_string(struct context *context, char **scontext,
  87. u32 *scontext_len);
  88. static void context_struct_compute_av(struct context *scontext,
  89. struct context *tcontext,
  90. u16 tclass,
  91. struct av_decision *avd,
  92. struct operation *ops);
  93. struct selinux_mapping {
  94. u16 value; /* policy value */
  95. unsigned num_perms;
  96. u32 perms[sizeof(u32) * 8];
  97. };
  98. static struct selinux_mapping *current_mapping;
  99. static u16 current_mapping_size;
  100. static int selinux_set_mapping(struct policydb *pol,
  101. struct security_class_mapping *map,
  102. struct selinux_mapping **out_map_p,
  103. u16 *out_map_size)
  104. {
  105. struct selinux_mapping *out_map = NULL;
  106. size_t size = sizeof(struct selinux_mapping);
  107. u16 i, j;
  108. unsigned k;
  109. bool print_unknown_handle = false;
  110. /* Find number of classes in the input mapping */
  111. if (!map)
  112. return -EINVAL;
  113. i = 0;
  114. while (map[i].name)
  115. i++;
  116. /* Allocate space for the class records, plus one for class zero */
  117. out_map = kcalloc(++i, size, GFP_ATOMIC);
  118. if (!out_map)
  119. return -ENOMEM;
  120. /* Store the raw class and permission values */
  121. j = 0;
  122. while (map[j].name) {
  123. struct security_class_mapping *p_in = map + (j++);
  124. struct selinux_mapping *p_out = out_map + j;
  125. /* An empty class string skips ahead */
  126. if (!strcmp(p_in->name, "")) {
  127. p_out->num_perms = 0;
  128. continue;
  129. }
  130. p_out->value = string_to_security_class(pol, p_in->name);
  131. if (!p_out->value) {
  132. printk(KERN_INFO
  133. "SELinux: Class %s not defined in policy.\n",
  134. p_in->name);
  135. if (pol->reject_unknown)
  136. goto err;
  137. p_out->num_perms = 0;
  138. print_unknown_handle = true;
  139. continue;
  140. }
  141. k = 0;
  142. while (p_in->perms && p_in->perms[k]) {
  143. /* An empty permission string skips ahead */
  144. if (!*p_in->perms[k]) {
  145. k++;
  146. continue;
  147. }
  148. p_out->perms[k] = string_to_av_perm(pol, p_out->value,
  149. p_in->perms[k]);
  150. if (!p_out->perms[k]) {
  151. printk(KERN_INFO
  152. "SELinux: Permission %s in class %s not defined in policy.\n",
  153. p_in->perms[k], p_in->name);
  154. if (pol->reject_unknown)
  155. goto err;
  156. print_unknown_handle = true;
  157. }
  158. k++;
  159. }
  160. p_out->num_perms = k;
  161. }
  162. if (print_unknown_handle)
  163. printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
  164. pol->allow_unknown ? "allowed" : "denied");
  165. *out_map_p = out_map;
  166. *out_map_size = i;
  167. return 0;
  168. err:
  169. kfree(out_map);
  170. return -EINVAL;
  171. }
  172. /*
  173. * Get real, policy values from mapped values
  174. */
  175. static u16 unmap_class(u16 tclass)
  176. {
  177. if (tclass < current_mapping_size)
  178. return current_mapping[tclass].value;
  179. return tclass;
  180. }
  181. /*
  182. * Get kernel value for class from its policy value
  183. */
  184. static u16 map_class(u16 pol_value)
  185. {
  186. u16 i;
  187. for (i = 1; i < current_mapping_size; i++) {
  188. if (current_mapping[i].value == pol_value)
  189. return i;
  190. }
  191. return SECCLASS_NULL;
  192. }
  193. static void map_decision(u16 tclass, struct av_decision *avd,
  194. int allow_unknown)
  195. {
  196. if (tclass < current_mapping_size) {
  197. unsigned i, n = current_mapping[tclass].num_perms;
  198. u32 result;
  199. for (i = 0, result = 0; i < n; i++) {
  200. if (avd->allowed & current_mapping[tclass].perms[i])
  201. result |= 1<<i;
  202. if (allow_unknown && !current_mapping[tclass].perms[i])
  203. result |= 1<<i;
  204. }
  205. avd->allowed = result;
  206. for (i = 0, result = 0; i < n; i++)
  207. if (avd->auditallow & current_mapping[tclass].perms[i])
  208. result |= 1<<i;
  209. avd->auditallow = result;
  210. for (i = 0, result = 0; i < n; i++) {
  211. if (avd->auditdeny & current_mapping[tclass].perms[i])
  212. result |= 1<<i;
  213. if (!allow_unknown && !current_mapping[tclass].perms[i])
  214. result |= 1<<i;
  215. }
  216. /*
  217. * In case the kernel has a bug and requests a permission
  218. * between num_perms and the maximum permission number, we
  219. * should audit that denial
  220. */
  221. for (; i < (sizeof(u32)*8); i++)
  222. result |= 1<<i;
  223. avd->auditdeny = result;
  224. }
  225. }
  226. int security_mls_enabled(void)
  227. {
  228. return policydb.mls_enabled;
  229. }
  230. /*
  231. * Return the boolean value of a constraint expression
  232. * when it is applied to the specified source and target
  233. * security contexts.
  234. *
  235. * xcontext is a special beast... It is used by the validatetrans rules
  236. * only. For these rules, scontext is the context before the transition,
  237. * tcontext is the context after the transition, and xcontext is the context
  238. * of the process performing the transition. All other callers of
  239. * constraint_expr_eval should pass in NULL for xcontext.
  240. */
  241. static int constraint_expr_eval(struct context *scontext,
  242. struct context *tcontext,
  243. struct context *xcontext,
  244. struct constraint_expr *cexpr)
  245. {
  246. u32 val1, val2;
  247. struct context *c;
  248. struct role_datum *r1, *r2;
  249. struct mls_level *l1, *l2;
  250. struct constraint_expr *e;
  251. int s[CEXPR_MAXDEPTH];
  252. int sp = -1;
  253. for (e = cexpr; e; e = e->next) {
  254. switch (e->expr_type) {
  255. case CEXPR_NOT:
  256. BUG_ON(sp < 0);
  257. s[sp] = !s[sp];
  258. break;
  259. case CEXPR_AND:
  260. BUG_ON(sp < 1);
  261. sp--;
  262. s[sp] &= s[sp + 1];
  263. break;
  264. case CEXPR_OR:
  265. BUG_ON(sp < 1);
  266. sp--;
  267. s[sp] |= s[sp + 1];
  268. break;
  269. case CEXPR_ATTR:
  270. if (sp == (CEXPR_MAXDEPTH - 1))
  271. return 0;
  272. switch (e->attr) {
  273. case CEXPR_USER:
  274. val1 = scontext->user;
  275. val2 = tcontext->user;
  276. break;
  277. case CEXPR_TYPE:
  278. val1 = scontext->type;
  279. val2 = tcontext->type;
  280. break;
  281. case CEXPR_ROLE:
  282. val1 = scontext->role;
  283. val2 = tcontext->role;
  284. r1 = policydb.role_val_to_struct[val1 - 1];
  285. r2 = policydb.role_val_to_struct[val2 - 1];
  286. switch (e->op) {
  287. case CEXPR_DOM:
  288. s[++sp] = ebitmap_get_bit(&r1->dominates,
  289. val2 - 1);
  290. continue;
  291. case CEXPR_DOMBY:
  292. s[++sp] = ebitmap_get_bit(&r2->dominates,
  293. val1 - 1);
  294. continue;
  295. case CEXPR_INCOMP:
  296. s[++sp] = (!ebitmap_get_bit(&r1->dominates,
  297. val2 - 1) &&
  298. !ebitmap_get_bit(&r2->dominates,
  299. val1 - 1));
  300. continue;
  301. default:
  302. break;
  303. }
  304. break;
  305. case CEXPR_L1L2:
  306. l1 = &(scontext->range.level[0]);
  307. l2 = &(tcontext->range.level[0]);
  308. goto mls_ops;
  309. case CEXPR_L1H2:
  310. l1 = &(scontext->range.level[0]);
  311. l2 = &(tcontext->range.level[1]);
  312. goto mls_ops;
  313. case CEXPR_H1L2:
  314. l1 = &(scontext->range.level[1]);
  315. l2 = &(tcontext->range.level[0]);
  316. goto mls_ops;
  317. case CEXPR_H1H2:
  318. l1 = &(scontext->range.level[1]);
  319. l2 = &(tcontext->range.level[1]);
  320. goto mls_ops;
  321. case CEXPR_L1H1:
  322. l1 = &(scontext->range.level[0]);
  323. l2 = &(scontext->range.level[1]);
  324. goto mls_ops;
  325. case CEXPR_L2H2:
  326. l1 = &(tcontext->range.level[0]);
  327. l2 = &(tcontext->range.level[1]);
  328. goto mls_ops;
  329. mls_ops:
  330. switch (e->op) {
  331. case CEXPR_EQ:
  332. s[++sp] = mls_level_eq(l1, l2);
  333. continue;
  334. case CEXPR_NEQ:
  335. s[++sp] = !mls_level_eq(l1, l2);
  336. continue;
  337. case CEXPR_DOM:
  338. s[++sp] = mls_level_dom(l1, l2);
  339. continue;
  340. case CEXPR_DOMBY:
  341. s[++sp] = mls_level_dom(l2, l1);
  342. continue;
  343. case CEXPR_INCOMP:
  344. s[++sp] = mls_level_incomp(l2, l1);
  345. continue;
  346. default:
  347. BUG();
  348. return 0;
  349. }
  350. break;
  351. default:
  352. BUG();
  353. return 0;
  354. }
  355. switch (e->op) {
  356. case CEXPR_EQ:
  357. s[++sp] = (val1 == val2);
  358. break;
  359. case CEXPR_NEQ:
  360. s[++sp] = (val1 != val2);
  361. break;
  362. default:
  363. BUG();
  364. return 0;
  365. }
  366. break;
  367. case CEXPR_NAMES:
  368. if (sp == (CEXPR_MAXDEPTH-1))
  369. return 0;
  370. c = scontext;
  371. if (e->attr & CEXPR_TARGET)
  372. c = tcontext;
  373. else if (e->attr & CEXPR_XTARGET) {
  374. c = xcontext;
  375. if (!c) {
  376. BUG();
  377. return 0;
  378. }
  379. }
  380. if (e->attr & CEXPR_USER)
  381. val1 = c->user;
  382. else if (e->attr & CEXPR_ROLE)
  383. val1 = c->role;
  384. else if (e->attr & CEXPR_TYPE)
  385. val1 = c->type;
  386. else {
  387. BUG();
  388. return 0;
  389. }
  390. switch (e->op) {
  391. case CEXPR_EQ:
  392. s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
  393. break;
  394. case CEXPR_NEQ:
  395. s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
  396. break;
  397. default:
  398. BUG();
  399. return 0;
  400. }
  401. break;
  402. default:
  403. BUG();
  404. return 0;
  405. }
  406. }
  407. BUG_ON(sp != 0);
  408. return s[0];
  409. }
  410. /*
  411. * security_dump_masked_av - dumps masked permissions during
  412. * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
  413. */
  414. static int dump_masked_av_helper(void *k, void *d, void *args)
  415. {
  416. struct perm_datum *pdatum = d;
  417. char **permission_names = args;
  418. BUG_ON(pdatum->value < 1 || pdatum->value > 32);
  419. permission_names[pdatum->value - 1] = (char *)k;
  420. return 0;
  421. }
  422. static void security_dump_masked_av(struct context *scontext,
  423. struct context *tcontext,
  424. u16 tclass,
  425. u32 permissions,
  426. const char *reason)
  427. {
  428. struct common_datum *common_dat;
  429. struct class_datum *tclass_dat;
  430. struct audit_buffer *ab;
  431. char *tclass_name;
  432. char *scontext_name = NULL;
  433. char *tcontext_name = NULL;
  434. char *permission_names[32];
  435. int index;
  436. u32 length;
  437. bool need_comma = false;
  438. if (!permissions)
  439. return;
  440. tclass_name = sym_name(&policydb, SYM_CLASSES, tclass - 1);
  441. tclass_dat = policydb.class_val_to_struct[tclass - 1];
  442. common_dat = tclass_dat->comdatum;
  443. /* init permission_names */
  444. if (common_dat &&
  445. hashtab_map(common_dat->permissions.table,
  446. dump_masked_av_helper, permission_names) < 0)
  447. goto out;
  448. if (hashtab_map(tclass_dat->permissions.table,
  449. dump_masked_av_helper, permission_names) < 0)
  450. goto out;
  451. /* get scontext/tcontext in text form */
  452. if (context_struct_to_string(scontext,
  453. &scontext_name, &length) < 0)
  454. goto out;
  455. if (context_struct_to_string(tcontext,
  456. &tcontext_name, &length) < 0)
  457. goto out;
  458. /* audit a message */
  459. ab = audit_log_start(current->audit_context,
  460. GFP_ATOMIC, AUDIT_SELINUX_ERR);
  461. if (!ab)
  462. goto out;
  463. audit_log_format(ab, "op=security_compute_av reason=%s "
  464. "scontext=%s tcontext=%s tclass=%s perms=",
  465. reason, scontext_name, tcontext_name, tclass_name);
  466. for (index = 0; index < 32; index++) {
  467. u32 mask = (1 << index);
  468. if ((mask & permissions) == 0)
  469. continue;
  470. audit_log_format(ab, "%s%s",
  471. need_comma ? "," : "",
  472. permission_names[index]
  473. ? permission_names[index] : "????");
  474. need_comma = true;
  475. }
  476. audit_log_end(ab);
  477. out:
  478. /* release scontext/tcontext */
  479. kfree(tcontext_name);
  480. kfree(scontext_name);
  481. return;
  482. }
  483. /*
  484. * security_boundary_permission - drops violated permissions
  485. * on boundary constraint.
  486. */
  487. static void type_attribute_bounds_av(struct context *scontext,
  488. struct context *tcontext,
  489. u16 tclass,
  490. struct av_decision *avd)
  491. {
  492. struct context lo_scontext;
  493. struct context lo_tcontext;
  494. struct av_decision lo_avd;
  495. struct type_datum *source;
  496. struct type_datum *target;
  497. u32 masked = 0;
  498. source = flex_array_get_ptr(policydb.type_val_to_struct_array,
  499. scontext->type - 1);
  500. BUG_ON(!source);
  501. target = flex_array_get_ptr(policydb.type_val_to_struct_array,
  502. tcontext->type - 1);
  503. BUG_ON(!target);
  504. if (source->bounds) {
  505. memset(&lo_avd, 0, sizeof(lo_avd));
  506. memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
  507. lo_scontext.type = source->bounds;
  508. context_struct_compute_av(&lo_scontext,
  509. tcontext,
  510. tclass,
  511. &lo_avd,
  512. NULL);
  513. if ((lo_avd.allowed & avd->allowed) == avd->allowed)
  514. return; /* no masked permission */
  515. masked = ~lo_avd.allowed & avd->allowed;
  516. }
  517. if (target->bounds) {
  518. memset(&lo_avd, 0, sizeof(lo_avd));
  519. memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
  520. lo_tcontext.type = target->bounds;
  521. context_struct_compute_av(scontext,
  522. &lo_tcontext,
  523. tclass,
  524. &lo_avd,
  525. NULL);
  526. if ((lo_avd.allowed & avd->allowed) == avd->allowed)
  527. return; /* no masked permission */
  528. masked = ~lo_avd.allowed & avd->allowed;
  529. }
  530. if (source->bounds && target->bounds) {
  531. memset(&lo_avd, 0, sizeof(lo_avd));
  532. /*
  533. * lo_scontext and lo_tcontext are already
  534. * set up.
  535. */
  536. context_struct_compute_av(&lo_scontext,
  537. &lo_tcontext,
  538. tclass,
  539. &lo_avd,
  540. NULL);
  541. if ((lo_avd.allowed & avd->allowed) == avd->allowed)
  542. return; /* no masked permission */
  543. masked = ~lo_avd.allowed & avd->allowed;
  544. }
  545. if (masked) {
  546. /* mask violated permissions */
  547. avd->allowed &= ~masked;
  548. /* audit masked permissions */
  549. security_dump_masked_av(scontext, tcontext,
  550. tclass, masked, "bounds");
  551. }
  552. }
  553. /* flag ioctl types that have operation permissions */
  554. void services_compute_operation_type(
  555. struct operation *ops,
  556. struct avtab_node *node)
  557. {
  558. u8 type;
  559. unsigned int i;
  560. if (node->key.specified & AVTAB_OPTYPE) {
  561. /* if allowing one or more complete types */
  562. for (i = 0; i < ARRAY_SIZE(ops->type); i++)
  563. ops->type[i] |= node->datum.u.ops->op.perms[i];
  564. } else {
  565. /* if allowing operations within a type */
  566. type = node->datum.u.ops->type;
  567. security_operation_set(ops->type, type);
  568. }
  569. /* If no ioctl commands are allowed, ignore auditallow and auditdeny */
  570. if (node->key.specified & AVTAB_OPTYPE_ALLOWED ||
  571. node->key.specified & AVTAB_OPNUM_ALLOWED)
  572. ops->len = 1;
  573. }
  574. /*
  575. * Compute access vectors and operations ranges based on a context
  576. * structure pair for the permissions in a particular class.
  577. */
  578. static void context_struct_compute_av(struct context *scontext,
  579. struct context *tcontext,
  580. u16 tclass,
  581. struct av_decision *avd,
  582. struct operation *ops)
  583. {
  584. struct constraint_node *constraint;
  585. struct role_allow *ra;
  586. struct avtab_key avkey;
  587. struct avtab_node *node;
  588. struct class_datum *tclass_datum;
  589. struct ebitmap *sattr, *tattr;
  590. struct ebitmap_node *snode, *tnode;
  591. unsigned int i, j;
  592. avd->allowed = 0;
  593. avd->auditallow = 0;
  594. avd->auditdeny = 0xffffffff;
  595. if (ops) {
  596. memset(&ops->type, 0, sizeof(ops->type));
  597. ops->len = 0;
  598. }
  599. if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
  600. if (printk_ratelimit())
  601. printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass);
  602. return;
  603. }
  604. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  605. /*
  606. * If a specific type enforcement rule was defined for
  607. * this permission check, then use it.
  608. */
  609. avkey.target_class = tclass;
  610. avkey.specified = AVTAB_AV | AVTAB_OP;
  611. sattr = flex_array_get(policydb.type_attr_map_array, scontext->type - 1);
  612. BUG_ON(!sattr);
  613. tattr = flex_array_get(policydb.type_attr_map_array, tcontext->type - 1);
  614. BUG_ON(!tattr);
  615. ebitmap_for_each_positive_bit(sattr, snode, i) {
  616. ebitmap_for_each_positive_bit(tattr, tnode, j) {
  617. avkey.source_type = i + 1;
  618. avkey.target_type = j + 1;
  619. for (node = avtab_search_node(&policydb.te_avtab, &avkey);
  620. node;
  621. node = avtab_search_node_next(node, avkey.specified)) {
  622. if (node->key.specified == AVTAB_ALLOWED)
  623. avd->allowed |= node->datum.u.data;
  624. else if (node->key.specified == AVTAB_AUDITALLOW)
  625. avd->auditallow |= node->datum.u.data;
  626. else if (node->key.specified == AVTAB_AUDITDENY)
  627. avd->auditdeny &= node->datum.u.data;
  628. else if (ops && (node->key.specified & AVTAB_OP))
  629. services_compute_operation_type(ops, node);
  630. }
  631. /* Check conditional av table for additional permissions */
  632. cond_compute_av(&policydb.te_cond_avtab, &avkey, avd, ops);
  633. }
  634. }
  635. /*
  636. * Remove any permissions prohibited by a constraint (this includes
  637. * the MLS policy).
  638. */
  639. constraint = tclass_datum->constraints;
  640. while (constraint) {
  641. if ((constraint->permissions & (avd->allowed)) &&
  642. !constraint_expr_eval(scontext, tcontext, NULL,
  643. constraint->expr)) {
  644. avd->allowed &= ~(constraint->permissions);
  645. }
  646. constraint = constraint->next;
  647. }
  648. /*
  649. * If checking process transition permission and the
  650. * role is changing, then check the (current_role, new_role)
  651. * pair.
  652. */
  653. if (tclass == policydb.process_class &&
  654. (avd->allowed & policydb.process_trans_perms) &&
  655. scontext->role != tcontext->role) {
  656. for (ra = policydb.role_allow; ra; ra = ra->next) {
  657. if (scontext->role == ra->role &&
  658. tcontext->role == ra->new_role)
  659. break;
  660. }
  661. if (!ra)
  662. avd->allowed &= ~policydb.process_trans_perms;
  663. }
  664. /*
  665. * If the given source and target types have boundary
  666. * constraint, lazy checks have to mask any violated
  667. * permission and notice it to userspace via audit.
  668. */
  669. type_attribute_bounds_av(scontext, tcontext,
  670. tclass, avd);
  671. }
  672. static int security_validtrans_handle_fail(struct context *ocontext,
  673. struct context *ncontext,
  674. struct context *tcontext,
  675. u16 tclass)
  676. {
  677. char *o = NULL, *n = NULL, *t = NULL;
  678. u32 olen, nlen, tlen;
  679. if (context_struct_to_string(ocontext, &o, &olen))
  680. goto out;
  681. if (context_struct_to_string(ncontext, &n, &nlen))
  682. goto out;
  683. if (context_struct_to_string(tcontext, &t, &tlen))
  684. goto out;
  685. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  686. "op=security_validate_transition seresult=denied"
  687. " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
  688. o, n, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
  689. out:
  690. kfree(o);
  691. kfree(n);
  692. kfree(t);
  693. if (!selinux_enforcing)
  694. return 0;
  695. return -EPERM;
  696. }
  697. int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
  698. u16 orig_tclass)
  699. {
  700. struct context *ocontext;
  701. struct context *ncontext;
  702. struct context *tcontext;
  703. struct class_datum *tclass_datum;
  704. struct constraint_node *constraint;
  705. u16 tclass;
  706. int rc = 0;
  707. if (!ss_initialized)
  708. return 0;
  709. read_lock(&policy_rwlock);
  710. tclass = unmap_class(orig_tclass);
  711. if (!tclass || tclass > policydb.p_classes.nprim) {
  712. printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
  713. __func__, tclass);
  714. rc = -EINVAL;
  715. goto out;
  716. }
  717. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  718. ocontext = sidtab_search(&sidtab, oldsid);
  719. if (!ocontext) {
  720. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  721. __func__, oldsid);
  722. rc = -EINVAL;
  723. goto out;
  724. }
  725. ncontext = sidtab_search(&sidtab, newsid);
  726. if (!ncontext) {
  727. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  728. __func__, newsid);
  729. rc = -EINVAL;
  730. goto out;
  731. }
  732. tcontext = sidtab_search(&sidtab, tasksid);
  733. if (!tcontext) {
  734. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  735. __func__, tasksid);
  736. rc = -EINVAL;
  737. goto out;
  738. }
  739. constraint = tclass_datum->validatetrans;
  740. while (constraint) {
  741. if (!constraint_expr_eval(ocontext, ncontext, tcontext,
  742. constraint->expr)) {
  743. rc = security_validtrans_handle_fail(ocontext, ncontext,
  744. tcontext, tclass);
  745. goto out;
  746. }
  747. constraint = constraint->next;
  748. }
  749. out:
  750. read_unlock(&policy_rwlock);
  751. return rc;
  752. }
  753. /*
  754. * security_bounded_transition - check whether the given
  755. * transition is directed to bounded, or not.
  756. * It returns 0, if @newsid is bounded by @oldsid.
  757. * Otherwise, it returns error code.
  758. *
  759. * @oldsid : current security identifier
  760. * @newsid : destinated security identifier
  761. */
  762. int security_bounded_transition(u32 old_sid, u32 new_sid)
  763. {
  764. struct context *old_context, *new_context;
  765. struct type_datum *type;
  766. int index;
  767. int rc;
  768. read_lock(&policy_rwlock);
  769. rc = -EINVAL;
  770. old_context = sidtab_search(&sidtab, old_sid);
  771. if (!old_context) {
  772. printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
  773. __func__, old_sid);
  774. goto out;
  775. }
  776. rc = -EINVAL;
  777. new_context = sidtab_search(&sidtab, new_sid);
  778. if (!new_context) {
  779. printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
  780. __func__, new_sid);
  781. goto out;
  782. }
  783. rc = 0;
  784. /* type/domain unchanged */
  785. if (old_context->type == new_context->type)
  786. goto out;
  787. index = new_context->type;
  788. while (true) {
  789. type = flex_array_get_ptr(policydb.type_val_to_struct_array,
  790. index - 1);
  791. BUG_ON(!type);
  792. /* not bounded anymore */
  793. rc = -EPERM;
  794. if (!type->bounds)
  795. break;
  796. /* @newsid is bounded by @oldsid */
  797. rc = 0;
  798. if (type->bounds == old_context->type)
  799. break;
  800. index = type->bounds;
  801. }
  802. if (rc) {
  803. char *old_name = NULL;
  804. char *new_name = NULL;
  805. u32 length;
  806. if (!context_struct_to_string(old_context,
  807. &old_name, &length) &&
  808. !context_struct_to_string(new_context,
  809. &new_name, &length)) {
  810. audit_log(current->audit_context,
  811. GFP_ATOMIC, AUDIT_SELINUX_ERR,
  812. "op=security_bounded_transition "
  813. "seresult=denied "
  814. "oldcontext=%s newcontext=%s",
  815. old_name, new_name);
  816. }
  817. kfree(new_name);
  818. kfree(old_name);
  819. }
  820. out:
  821. read_unlock(&policy_rwlock);
  822. return rc;
  823. }
  824. static void avd_init(struct av_decision *avd)
  825. {
  826. avd->allowed = 0;
  827. avd->auditallow = 0;
  828. avd->auditdeny = 0xffffffff;
  829. avd->seqno = latest_granting;
  830. avd->flags = 0;
  831. }
  832. void services_compute_operation_num(struct operation_decision *od,
  833. struct avtab_node *node)
  834. {
  835. unsigned int i;
  836. if (node->key.specified & AVTAB_OPNUM) {
  837. if (od->type != node->datum.u.ops->type)
  838. return;
  839. } else {
  840. if (!security_operation_test(node->datum.u.ops->op.perms,
  841. od->type))
  842. return;
  843. }
  844. if (node->key.specified == AVTAB_OPTYPE_ALLOWED) {
  845. od->specified |= OPERATION_ALLOWED;
  846. memset(od->allowed->perms, 0xff,
  847. sizeof(od->allowed->perms));
  848. } else if (node->key.specified == AVTAB_OPTYPE_AUDITALLOW) {
  849. od->specified |= OPERATION_AUDITALLOW;
  850. memset(od->auditallow->perms, 0xff,
  851. sizeof(od->auditallow->perms));
  852. } else if (node->key.specified == AVTAB_OPTYPE_DONTAUDIT) {
  853. od->specified |= OPERATION_DONTAUDIT;
  854. memset(od->dontaudit->perms, 0xff,
  855. sizeof(od->dontaudit->perms));
  856. } else if (node->key.specified == AVTAB_OPNUM_ALLOWED) {
  857. od->specified |= OPERATION_ALLOWED;
  858. for (i = 0; i < ARRAY_SIZE(od->allowed->perms); i++)
  859. od->allowed->perms[i] |=
  860. node->datum.u.ops->op.perms[i];
  861. } else if (node->key.specified == AVTAB_OPNUM_AUDITALLOW) {
  862. od->specified |= OPERATION_AUDITALLOW;
  863. for (i = 0; i < ARRAY_SIZE(od->auditallow->perms); i++)
  864. od->auditallow->perms[i] |=
  865. node->datum.u.ops->op.perms[i];
  866. } else if (node->key.specified == AVTAB_OPNUM_DONTAUDIT) {
  867. od->specified |= OPERATION_DONTAUDIT;
  868. for (i = 0; i < ARRAY_SIZE(od->dontaudit->perms); i++)
  869. od->dontaudit->perms[i] |=
  870. node->datum.u.ops->op.perms[i];
  871. } else {
  872. BUG();
  873. }
  874. }
  875. void security_compute_operation(u32 ssid,
  876. u32 tsid,
  877. u16 orig_tclass,
  878. u8 type,
  879. struct operation_decision *od)
  880. {
  881. u16 tclass;
  882. struct context *scontext, *tcontext;
  883. struct avtab_key avkey;
  884. struct avtab_node *node;
  885. struct ebitmap *sattr, *tattr;
  886. struct ebitmap_node *snode, *tnode;
  887. unsigned int i, j;
  888. od->type = type;
  889. od->specified = 0;
  890. memset(od->allowed->perms, 0, sizeof(od->allowed->perms));
  891. memset(od->auditallow->perms, 0, sizeof(od->auditallow->perms));
  892. memset(od->dontaudit->perms, 0, sizeof(od->dontaudit->perms));
  893. read_lock(&policy_rwlock);
  894. if (!ss_initialized)
  895. goto allow;
  896. scontext = sidtab_search(&sidtab, ssid);
  897. if (!scontext) {
  898. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  899. __func__, ssid);
  900. goto out;
  901. }
  902. tcontext = sidtab_search(&sidtab, tsid);
  903. if (!tcontext) {
  904. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  905. __func__, tsid);
  906. goto out;
  907. }
  908. tclass = unmap_class(orig_tclass);
  909. if (unlikely(orig_tclass && !tclass)) {
  910. if (policydb.allow_unknown)
  911. goto allow;
  912. goto out;
  913. }
  914. if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
  915. pr_warn_ratelimited("SELinux: Invalid class %hu\n", tclass);
  916. goto out;
  917. }
  918. avkey.target_class = tclass;
  919. avkey.specified = AVTAB_OP;
  920. sattr = flex_array_get(policydb.type_attr_map_array,
  921. scontext->type - 1);
  922. BUG_ON(!sattr);
  923. tattr = flex_array_get(policydb.type_attr_map_array,
  924. tcontext->type - 1);
  925. BUG_ON(!tattr);
  926. ebitmap_for_each_positive_bit(sattr, snode, i) {
  927. ebitmap_for_each_positive_bit(tattr, tnode, j) {
  928. avkey.source_type = i + 1;
  929. avkey.target_type = j + 1;
  930. for (node = avtab_search_node(&policydb.te_avtab, &avkey);
  931. node;
  932. node = avtab_search_node_next(node, avkey.specified))
  933. services_compute_operation_num(od, node);
  934. cond_compute_operation(&policydb.te_cond_avtab,
  935. &avkey, od);
  936. }
  937. }
  938. out:
  939. read_unlock(&policy_rwlock);
  940. return;
  941. allow:
  942. memset(od->allowed->perms, 0xff, sizeof(od->allowed->perms));
  943. goto out;
  944. }
  945. /**
  946. * security_compute_av - Compute access vector decisions.
  947. * @ssid: source security identifier
  948. * @tsid: target security identifier
  949. * @tclass: target security class
  950. * @avd: access vector decisions
  951. * @od: operation decisions
  952. *
  953. * Compute a set of access vector decisions based on the
  954. * SID pair (@ssid, @tsid) for the permissions in @tclass.
  955. */
  956. void security_compute_av(u32 ssid,
  957. u32 tsid,
  958. u16 orig_tclass,
  959. struct av_decision *avd,
  960. struct operation *ops)
  961. {
  962. u16 tclass;
  963. struct context *scontext = NULL, *tcontext = NULL;
  964. read_lock(&policy_rwlock);
  965. avd_init(avd);
  966. ops->len = 0;
  967. if (!ss_initialized)
  968. goto allow;
  969. scontext = sidtab_search(&sidtab, ssid);
  970. if (!scontext) {
  971. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  972. __func__, ssid);
  973. goto out;
  974. }
  975. /* permissive domain? */
  976. if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
  977. avd->flags |= AVD_FLAGS_PERMISSIVE;
  978. tcontext = sidtab_search(&sidtab, tsid);
  979. if (!tcontext) {
  980. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  981. __func__, tsid);
  982. goto out;
  983. }
  984. tclass = unmap_class(orig_tclass);
  985. if (unlikely(orig_tclass && !tclass)) {
  986. if (policydb.allow_unknown)
  987. goto allow;
  988. goto out;
  989. }
  990. context_struct_compute_av(scontext, tcontext, tclass, avd, ops);
  991. map_decision(orig_tclass, avd, policydb.allow_unknown);
  992. out:
  993. read_unlock(&policy_rwlock);
  994. return;
  995. allow:
  996. avd->allowed = 0xffffffff;
  997. goto out;
  998. }
  999. void security_compute_av_user(u32 ssid,
  1000. u32 tsid,
  1001. u16 tclass,
  1002. struct av_decision *avd)
  1003. {
  1004. struct context *scontext = NULL, *tcontext = NULL;
  1005. read_lock(&policy_rwlock);
  1006. avd_init(avd);
  1007. if (!ss_initialized)
  1008. goto allow;
  1009. scontext = sidtab_search(&sidtab, ssid);
  1010. if (!scontext) {
  1011. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1012. __func__, ssid);
  1013. goto out;
  1014. }
  1015. /* permissive domain? */
  1016. if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
  1017. avd->flags |= AVD_FLAGS_PERMISSIVE;
  1018. tcontext = sidtab_search(&sidtab, tsid);
  1019. if (!tcontext) {
  1020. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1021. __func__, tsid);
  1022. goto out;
  1023. }
  1024. if (unlikely(!tclass)) {
  1025. if (policydb.allow_unknown)
  1026. goto allow;
  1027. goto out;
  1028. }
  1029. context_struct_compute_av(scontext, tcontext, tclass, avd, NULL);
  1030. out:
  1031. read_unlock(&policy_rwlock);
  1032. return;
  1033. allow:
  1034. avd->allowed = 0xffffffff;
  1035. goto out;
  1036. }
  1037. /*
  1038. * Write the security context string representation of
  1039. * the context structure `context' into a dynamically
  1040. * allocated string of the correct size. Set `*scontext'
  1041. * to point to this string and set `*scontext_len' to
  1042. * the length of the string.
  1043. */
  1044. static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
  1045. {
  1046. char *scontextp;
  1047. if (scontext)
  1048. *scontext = NULL;
  1049. *scontext_len = 0;
  1050. if (context->len) {
  1051. *scontext_len = context->len;
  1052. if (scontext) {
  1053. *scontext = kstrdup(context->str, GFP_ATOMIC);
  1054. if (!(*scontext))
  1055. return -ENOMEM;
  1056. }
  1057. return 0;
  1058. }
  1059. /* Compute the size of the context. */
  1060. *scontext_len += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) + 1;
  1061. *scontext_len += strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) + 1;
  1062. *scontext_len += strlen(sym_name(&policydb, SYM_TYPES, context->type - 1)) + 1;
  1063. *scontext_len += mls_compute_context_len(context);
  1064. if (!scontext)
  1065. return 0;
  1066. /* Allocate space for the context; caller must free this space. */
  1067. scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
  1068. if (!scontextp)
  1069. return -ENOMEM;
  1070. *scontext = scontextp;
  1071. /*
  1072. * Copy the user name, role name and type name into the context.
  1073. */
  1074. sprintf(scontextp, "%s:%s:%s",
  1075. sym_name(&policydb, SYM_USERS, context->user - 1),
  1076. sym_name(&policydb, SYM_ROLES, context->role - 1),
  1077. sym_name(&policydb, SYM_TYPES, context->type - 1));
  1078. scontextp += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) +
  1079. 1 + strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) +
  1080. 1 + strlen(sym_name(&policydb, SYM_TYPES, context->type - 1));
  1081. mls_sid_to_context(context, &scontextp);
  1082. *scontextp = 0;
  1083. return 0;
  1084. }
  1085. #include "initial_sid_to_string.h"
  1086. const char *security_get_initial_sid_context(u32 sid)
  1087. {
  1088. if (unlikely(sid > SECINITSID_NUM))
  1089. return NULL;
  1090. return initial_sid_to_string[sid];
  1091. }
  1092. static int security_sid_to_context_core(u32 sid, char **scontext,
  1093. u32 *scontext_len, int force)
  1094. {
  1095. struct context *context;
  1096. int rc = 0;
  1097. if (scontext)
  1098. *scontext = NULL;
  1099. *scontext_len = 0;
  1100. if (!ss_initialized) {
  1101. if (sid <= SECINITSID_NUM) {
  1102. char *scontextp;
  1103. *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
  1104. if (!scontext)
  1105. goto out;
  1106. scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
  1107. if (!scontextp) {
  1108. rc = -ENOMEM;
  1109. goto out;
  1110. }
  1111. strcpy(scontextp, initial_sid_to_string[sid]);
  1112. *scontext = scontextp;
  1113. goto out;
  1114. }
  1115. printk(KERN_ERR "SELinux: %s: called before initial "
  1116. "load_policy on unknown SID %d\n", __func__, sid);
  1117. rc = -EINVAL;
  1118. goto out;
  1119. }
  1120. read_lock(&policy_rwlock);
  1121. if (force)
  1122. context = sidtab_search_force(&sidtab, sid);
  1123. else
  1124. context = sidtab_search(&sidtab, sid);
  1125. if (!context) {
  1126. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1127. __func__, sid);
  1128. rc = -EINVAL;
  1129. goto out_unlock;
  1130. }
  1131. rc = context_struct_to_string(context, scontext, scontext_len);
  1132. out_unlock:
  1133. read_unlock(&policy_rwlock);
  1134. out:
  1135. return rc;
  1136. }
  1137. /**
  1138. * security_sid_to_context - Obtain a context for a given SID.
  1139. * @sid: security identifier, SID
  1140. * @scontext: security context
  1141. * @scontext_len: length in bytes
  1142. *
  1143. * Write the string representation of the context associated with @sid
  1144. * into a dynamically allocated string of the correct size. Set @scontext
  1145. * to point to this string and set @scontext_len to the length of the string.
  1146. */
  1147. int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
  1148. {
  1149. return security_sid_to_context_core(sid, scontext, scontext_len, 0);
  1150. }
  1151. int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
  1152. {
  1153. return security_sid_to_context_core(sid, scontext, scontext_len, 1);
  1154. }
  1155. /*
  1156. * Caveat: Mutates scontext.
  1157. */
  1158. static int string_to_context_struct(struct policydb *pol,
  1159. struct sidtab *sidtabp,
  1160. char *scontext,
  1161. u32 scontext_len,
  1162. struct context *ctx,
  1163. u32 def_sid)
  1164. {
  1165. struct role_datum *role;
  1166. struct type_datum *typdatum;
  1167. struct user_datum *usrdatum;
  1168. char *scontextp, *p, oldc;
  1169. int rc = 0;
  1170. context_init(ctx);
  1171. /* Parse the security context. */
  1172. rc = -EINVAL;
  1173. scontextp = (char *) scontext;
  1174. /* Extract the user. */
  1175. p = scontextp;
  1176. while (*p && *p != ':')
  1177. p++;
  1178. if (*p == 0)
  1179. goto out;
  1180. *p++ = 0;
  1181. usrdatum = hashtab_search(pol->p_users.table, scontextp);
  1182. if (!usrdatum)
  1183. goto out;
  1184. ctx->user = usrdatum->value;
  1185. /* Extract role. */
  1186. scontextp = p;
  1187. while (*p && *p != ':')
  1188. p++;
  1189. if (*p == 0)
  1190. goto out;
  1191. *p++ = 0;
  1192. role = hashtab_search(pol->p_roles.table, scontextp);
  1193. if (!role)
  1194. goto out;
  1195. ctx->role = role->value;
  1196. /* Extract type. */
  1197. scontextp = p;
  1198. while (*p && *p != ':')
  1199. p++;
  1200. oldc = *p;
  1201. *p++ = 0;
  1202. typdatum = hashtab_search(pol->p_types.table, scontextp);
  1203. if (!typdatum || typdatum->attribute)
  1204. goto out;
  1205. ctx->type = typdatum->value;
  1206. rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
  1207. if (rc)
  1208. goto out;
  1209. rc = -EINVAL;
  1210. if ((p - scontext) < scontext_len)
  1211. goto out;
  1212. /* Check the validity of the new context. */
  1213. if (!policydb_context_isvalid(pol, ctx))
  1214. goto out;
  1215. rc = 0;
  1216. out:
  1217. if (rc)
  1218. context_destroy(ctx);
  1219. return rc;
  1220. }
  1221. static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
  1222. u32 *sid, u32 def_sid, gfp_t gfp_flags,
  1223. int force)
  1224. {
  1225. char *scontext2, *str = NULL;
  1226. struct context context;
  1227. int rc = 0;
  1228. /* An empty security context is never valid. */
  1229. if (!scontext_len)
  1230. return -EINVAL;
  1231. if (!ss_initialized) {
  1232. int i;
  1233. for (i = 1; i < SECINITSID_NUM; i++) {
  1234. if (!strcmp(initial_sid_to_string[i], scontext)) {
  1235. *sid = i;
  1236. return 0;
  1237. }
  1238. }
  1239. *sid = SECINITSID_KERNEL;
  1240. return 0;
  1241. }
  1242. *sid = SECSID_NULL;
  1243. /* Copy the string so that we can modify the copy as we parse it. */
  1244. scontext2 = kmalloc(scontext_len + 1, gfp_flags);
  1245. if (!scontext2)
  1246. return -ENOMEM;
  1247. memcpy(scontext2, scontext, scontext_len);
  1248. scontext2[scontext_len] = 0;
  1249. if (force) {
  1250. /* Save another copy for storing in uninterpreted form */
  1251. rc = -ENOMEM;
  1252. str = kstrdup(scontext2, gfp_flags);
  1253. if (!str)
  1254. goto out;
  1255. }
  1256. read_lock(&policy_rwlock);
  1257. rc = string_to_context_struct(&policydb, &sidtab, scontext2,
  1258. scontext_len, &context, def_sid);
  1259. if (rc == -EINVAL && force) {
  1260. context.str = str;
  1261. context.len = scontext_len;
  1262. str = NULL;
  1263. } else if (rc)
  1264. goto out_unlock;
  1265. rc = sidtab_context_to_sid(&sidtab, &context, sid);
  1266. context_destroy(&context);
  1267. out_unlock:
  1268. read_unlock(&policy_rwlock);
  1269. out:
  1270. kfree(scontext2);
  1271. kfree(str);
  1272. return rc;
  1273. }
  1274. /**
  1275. * security_context_to_sid - Obtain a SID for a given security context.
  1276. * @scontext: security context
  1277. * @scontext_len: length in bytes
  1278. * @sid: security identifier, SID
  1279. * @gfp: context for the allocation
  1280. *
  1281. * Obtains a SID associated with the security context that
  1282. * has the string representation specified by @scontext.
  1283. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  1284. * memory is available, or 0 on success.
  1285. */
  1286. int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid,
  1287. gfp_t gfp)
  1288. {
  1289. return security_context_to_sid_core(scontext, scontext_len,
  1290. sid, SECSID_NULL, gfp, 0);
  1291. }
  1292. /**
  1293. * security_context_to_sid_default - Obtain a SID for a given security context,
  1294. * falling back to specified default if needed.
  1295. *
  1296. * @scontext: security context
  1297. * @scontext_len: length in bytes
  1298. * @sid: security identifier, SID
  1299. * @def_sid: default SID to assign on error
  1300. *
  1301. * Obtains a SID associated with the security context that
  1302. * has the string representation specified by @scontext.
  1303. * The default SID is passed to the MLS layer to be used to allow
  1304. * kernel labeling of the MLS field if the MLS field is not present
  1305. * (for upgrading to MLS without full relabel).
  1306. * Implicitly forces adding of the context even if it cannot be mapped yet.
  1307. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  1308. * memory is available, or 0 on success.
  1309. */
  1310. int security_context_to_sid_default(const char *scontext, u32 scontext_len,
  1311. u32 *sid, u32 def_sid, gfp_t gfp_flags)
  1312. {
  1313. return security_context_to_sid_core(scontext, scontext_len,
  1314. sid, def_sid, gfp_flags, 1);
  1315. }
  1316. int security_context_to_sid_force(const char *scontext, u32 scontext_len,
  1317. u32 *sid)
  1318. {
  1319. return security_context_to_sid_core(scontext, scontext_len,
  1320. sid, SECSID_NULL, GFP_KERNEL, 1);
  1321. }
  1322. static int compute_sid_handle_invalid_context(
  1323. struct context *scontext,
  1324. struct context *tcontext,
  1325. u16 tclass,
  1326. struct context *newcontext)
  1327. {
  1328. char *s = NULL, *t = NULL, *n = NULL;
  1329. u32 slen, tlen, nlen;
  1330. if (context_struct_to_string(scontext, &s, &slen))
  1331. goto out;
  1332. if (context_struct_to_string(tcontext, &t, &tlen))
  1333. goto out;
  1334. if (context_struct_to_string(newcontext, &n, &nlen))
  1335. goto out;
  1336. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1337. "op=security_compute_sid invalid_context=%s"
  1338. " scontext=%s"
  1339. " tcontext=%s"
  1340. " tclass=%s",
  1341. n, s, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
  1342. out:
  1343. kfree(s);
  1344. kfree(t);
  1345. kfree(n);
  1346. if (!selinux_enforcing)
  1347. return 0;
  1348. return -EACCES;
  1349. }
  1350. static void filename_compute_type(struct policydb *p, struct context *newcontext,
  1351. u32 stype, u32 ttype, u16 tclass,
  1352. const char *objname)
  1353. {
  1354. struct filename_trans ft;
  1355. struct filename_trans_datum *otype;
  1356. /*
  1357. * Most filename trans rules are going to live in specific directories
  1358. * like /dev or /var/run. This bitmap will quickly skip rule searches
  1359. * if the ttype does not contain any rules.
  1360. */
  1361. if (!ebitmap_get_bit(&p->filename_trans_ttypes, ttype))
  1362. return;
  1363. ft.stype = stype;
  1364. ft.ttype = ttype;
  1365. ft.tclass = tclass;
  1366. ft.name = objname;
  1367. otype = hashtab_search(p->filename_trans, &ft);
  1368. if (otype)
  1369. newcontext->type = otype->otype;
  1370. }
  1371. static int security_compute_sid(u32 ssid,
  1372. u32 tsid,
  1373. u16 orig_tclass,
  1374. u32 specified,
  1375. const char *objname,
  1376. u32 *out_sid,
  1377. bool kern)
  1378. {
  1379. struct class_datum *cladatum = NULL;
  1380. struct context *scontext = NULL, *tcontext = NULL, newcontext;
  1381. struct role_trans *roletr = NULL;
  1382. struct avtab_key avkey;
  1383. struct avtab_datum *avdatum;
  1384. struct avtab_node *node;
  1385. u16 tclass;
  1386. int rc = 0;
  1387. bool sock;
  1388. if (!ss_initialized) {
  1389. switch (orig_tclass) {
  1390. case SECCLASS_PROCESS: /* kernel value */
  1391. *out_sid = ssid;
  1392. break;
  1393. default:
  1394. *out_sid = tsid;
  1395. break;
  1396. }
  1397. goto out;
  1398. }
  1399. context_init(&newcontext);
  1400. read_lock(&policy_rwlock);
  1401. if (kern) {
  1402. tclass = unmap_class(orig_tclass);
  1403. sock = security_is_socket_class(orig_tclass);
  1404. } else {
  1405. tclass = orig_tclass;
  1406. sock = security_is_socket_class(map_class(tclass));
  1407. }
  1408. scontext = sidtab_search(&sidtab, ssid);
  1409. if (!scontext) {
  1410. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1411. __func__, ssid);
  1412. rc = -EINVAL;
  1413. goto out_unlock;
  1414. }
  1415. tcontext = sidtab_search(&sidtab, tsid);
  1416. if (!tcontext) {
  1417. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1418. __func__, tsid);
  1419. rc = -EINVAL;
  1420. goto out_unlock;
  1421. }
  1422. if (tclass && tclass <= policydb.p_classes.nprim)
  1423. cladatum = policydb.class_val_to_struct[tclass - 1];
  1424. /* Set the user identity. */
  1425. switch (specified) {
  1426. case AVTAB_TRANSITION:
  1427. case AVTAB_CHANGE:
  1428. if (cladatum && cladatum->default_user == DEFAULT_TARGET) {
  1429. newcontext.user = tcontext->user;
  1430. } else {
  1431. /* notice this gets both DEFAULT_SOURCE and unset */
  1432. /* Use the process user identity. */
  1433. newcontext.user = scontext->user;
  1434. }
  1435. break;
  1436. case AVTAB_MEMBER:
  1437. /* Use the related object owner. */
  1438. newcontext.user = tcontext->user;
  1439. break;
  1440. }
  1441. /* Set the role to default values. */
  1442. if (cladatum && cladatum->default_role == DEFAULT_SOURCE) {
  1443. newcontext.role = scontext->role;
  1444. } else if (cladatum && cladatum->default_role == DEFAULT_TARGET) {
  1445. newcontext.role = tcontext->role;
  1446. } else {
  1447. if ((tclass == policydb.process_class) || (sock == true))
  1448. newcontext.role = scontext->role;
  1449. else
  1450. newcontext.role = OBJECT_R_VAL;
  1451. }
  1452. /* Set the type to default values. */
  1453. if (cladatum && cladatum->default_type == DEFAULT_SOURCE) {
  1454. newcontext.type = scontext->type;
  1455. } else if (cladatum && cladatum->default_type == DEFAULT_TARGET) {
  1456. newcontext.type = tcontext->type;
  1457. } else {
  1458. if ((tclass == policydb.process_class) || (sock == true)) {
  1459. /* Use the type of process. */
  1460. newcontext.type = scontext->type;
  1461. } else {
  1462. /* Use the type of the related object. */
  1463. newcontext.type = tcontext->type;
  1464. }
  1465. }
  1466. /* Look for a type transition/member/change rule. */
  1467. avkey.source_type = scontext->type;
  1468. avkey.target_type = tcontext->type;
  1469. avkey.target_class = tclass;
  1470. avkey.specified = specified;
  1471. avdatum = avtab_search(&policydb.te_avtab, &avkey);
  1472. /* If no permanent rule, also check for enabled conditional rules */
  1473. if (!avdatum) {
  1474. node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
  1475. for (; node; node = avtab_search_node_next(node, specified)) {
  1476. if (node->key.specified & AVTAB_ENABLED) {
  1477. avdatum = &node->datum;
  1478. break;
  1479. }
  1480. }
  1481. }
  1482. if (avdatum) {
  1483. /* Use the type from the type transition/member/change rule. */
  1484. newcontext.type = avdatum->u.data;
  1485. }
  1486. /* if we have a objname this is a file trans check so check those rules */
  1487. if (objname)
  1488. filename_compute_type(&policydb, &newcontext, scontext->type,
  1489. tcontext->type, tclass, objname);
  1490. /* Check for class-specific changes. */
  1491. if (specified & AVTAB_TRANSITION) {
  1492. /* Look for a role transition rule. */
  1493. for (roletr = policydb.role_tr; roletr; roletr = roletr->next) {
  1494. if ((roletr->role == scontext->role) &&
  1495. (roletr->type == tcontext->type) &&
  1496. (roletr->tclass == tclass)) {
  1497. /* Use the role transition rule. */
  1498. newcontext.role = roletr->new_role;
  1499. break;
  1500. }
  1501. }
  1502. }
  1503. /* Set the MLS attributes.
  1504. This is done last because it may allocate memory. */
  1505. rc = mls_compute_sid(scontext, tcontext, tclass, specified,
  1506. &newcontext, sock);
  1507. if (rc)
  1508. goto out_unlock;
  1509. /* Check the validity of the context. */
  1510. if (!policydb_context_isvalid(&policydb, &newcontext)) {
  1511. rc = compute_sid_handle_invalid_context(scontext,
  1512. tcontext,
  1513. tclass,
  1514. &newcontext);
  1515. if (rc)
  1516. goto out_unlock;
  1517. }
  1518. /* Obtain the sid for the context. */
  1519. rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
  1520. out_unlock:
  1521. read_unlock(&policy_rwlock);
  1522. context_destroy(&newcontext);
  1523. out:
  1524. return rc;
  1525. }
  1526. /**
  1527. * security_transition_sid - Compute the SID for a new subject/object.
  1528. * @ssid: source security identifier
  1529. * @tsid: target security identifier
  1530. * @tclass: target security class
  1531. * @out_sid: security identifier for new subject/object
  1532. *
  1533. * Compute a SID to use for labeling a new subject or object in the
  1534. * class @tclass based on a SID pair (@ssid, @tsid).
  1535. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1536. * if insufficient memory is available, or %0 if the new SID was
  1537. * computed successfully.
  1538. */
  1539. int security_transition_sid(u32 ssid, u32 tsid, u16 tclass,
  1540. const struct qstr *qstr, u32 *out_sid)
  1541. {
  1542. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
  1543. qstr ? qstr->name : NULL, out_sid, true);
  1544. }
  1545. int security_transition_sid_user(u32 ssid, u32 tsid, u16 tclass,
  1546. const char *objname, u32 *out_sid)
  1547. {
  1548. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
  1549. objname, out_sid, false);
  1550. }
  1551. /**
  1552. * security_member_sid - Compute the SID for member selection.
  1553. * @ssid: source security identifier
  1554. * @tsid: target security identifier
  1555. * @tclass: target security class
  1556. * @out_sid: security identifier for selected member
  1557. *
  1558. * Compute a SID to use when selecting a member of a polyinstantiated
  1559. * object of class @tclass based on a SID pair (@ssid, @tsid).
  1560. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1561. * if insufficient memory is available, or %0 if the SID was
  1562. * computed successfully.
  1563. */
  1564. int security_member_sid(u32 ssid,
  1565. u32 tsid,
  1566. u16 tclass,
  1567. u32 *out_sid)
  1568. {
  1569. return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, NULL,
  1570. out_sid, false);
  1571. }
  1572. /**
  1573. * security_change_sid - Compute the SID for object relabeling.
  1574. * @ssid: source security identifier
  1575. * @tsid: target security identifier
  1576. * @tclass: target security class
  1577. * @out_sid: security identifier for selected member
  1578. *
  1579. * Compute a SID to use for relabeling an object of class @tclass
  1580. * based on a SID pair (@ssid, @tsid).
  1581. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1582. * if insufficient memory is available, or %0 if the SID was
  1583. * computed successfully.
  1584. */
  1585. int security_change_sid(u32 ssid,
  1586. u32 tsid,
  1587. u16 tclass,
  1588. u32 *out_sid)
  1589. {
  1590. return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, NULL,
  1591. out_sid, false);
  1592. }
  1593. /* Clone the SID into the new SID table. */
  1594. static int clone_sid(u32 sid,
  1595. struct context *context,
  1596. void *arg)
  1597. {
  1598. struct sidtab *s = arg;
  1599. if (sid > SECINITSID_NUM)
  1600. return sidtab_insert(s, sid, context);
  1601. else
  1602. return 0;
  1603. }
  1604. static inline int convert_context_handle_invalid_context(struct context *context)
  1605. {
  1606. char *s;
  1607. u32 len;
  1608. if (selinux_enforcing)
  1609. return -EINVAL;
  1610. if (!context_struct_to_string(context, &s, &len)) {
  1611. printk(KERN_WARNING "SELinux: Context %s would be invalid if enforcing\n", s);
  1612. kfree(s);
  1613. }
  1614. return 0;
  1615. }
  1616. struct convert_context_args {
  1617. struct policydb *oldp;
  1618. struct policydb *newp;
  1619. };
  1620. /*
  1621. * Convert the values in the security context
  1622. * structure `c' from the values specified
  1623. * in the policy `p->oldp' to the values specified
  1624. * in the policy `p->newp'. Verify that the
  1625. * context is valid under the new policy.
  1626. */
  1627. static int convert_context(u32 key,
  1628. struct context *c,
  1629. void *p)
  1630. {
  1631. struct convert_context_args *args;
  1632. struct context oldc;
  1633. struct ocontext *oc;
  1634. struct mls_range *range;
  1635. struct role_datum *role;
  1636. struct type_datum *typdatum;
  1637. struct user_datum *usrdatum;
  1638. char *s;
  1639. u32 len;
  1640. int rc = 0;
  1641. if (key <= SECINITSID_NUM)
  1642. goto out;
  1643. args = p;
  1644. if (c->str) {
  1645. struct context ctx;
  1646. rc = -ENOMEM;
  1647. s = kstrdup(c->str, GFP_KERNEL);
  1648. if (!s)
  1649. goto out;
  1650. rc = string_to_context_struct(args->newp, NULL, s,
  1651. c->len, &ctx, SECSID_NULL);
  1652. kfree(s);
  1653. if (!rc) {
  1654. printk(KERN_INFO "SELinux: Context %s became valid (mapped).\n",
  1655. c->str);
  1656. /* Replace string with mapped representation. */
  1657. kfree(c->str);
  1658. memcpy(c, &ctx, sizeof(*c));
  1659. goto out;
  1660. } else if (rc == -EINVAL) {
  1661. /* Retain string representation for later mapping. */
  1662. rc = 0;
  1663. goto out;
  1664. } else {
  1665. /* Other error condition, e.g. ENOMEM. */
  1666. printk(KERN_ERR "SELinux: Unable to map context %s, rc = %d.\n",
  1667. c->str, -rc);
  1668. goto out;
  1669. }
  1670. }
  1671. rc = context_cpy(&oldc, c);
  1672. if (rc)
  1673. goto out;
  1674. /* Convert the user. */
  1675. rc = -EINVAL;
  1676. usrdatum = hashtab_search(args->newp->p_users.table,
  1677. sym_name(args->oldp, SYM_USERS, c->user - 1));
  1678. if (!usrdatum)
  1679. goto bad;
  1680. c->user = usrdatum->value;
  1681. /* Convert the role. */
  1682. rc = -EINVAL;
  1683. role = hashtab_search(args->newp->p_roles.table,
  1684. sym_name(args->oldp, SYM_ROLES, c->role - 1));
  1685. if (!role)
  1686. goto bad;
  1687. c->role = role->value;
  1688. /* Convert the type. */
  1689. rc = -EINVAL;
  1690. typdatum = hashtab_search(args->newp->p_types.table,
  1691. sym_name(args->oldp, SYM_TYPES, c->type - 1));
  1692. if (!typdatum)
  1693. goto bad;
  1694. c->type = typdatum->value;
  1695. /* Convert the MLS fields if dealing with MLS policies */
  1696. if (args->oldp->mls_enabled && args->newp->mls_enabled) {
  1697. rc = mls_convert_context(args->oldp, args->newp, c);
  1698. if (rc)
  1699. goto bad;
  1700. } else if (args->oldp->mls_enabled && !args->newp->mls_enabled) {
  1701. /*
  1702. * Switching between MLS and non-MLS policy:
  1703. * free any storage used by the MLS fields in the
  1704. * context for all existing entries in the sidtab.
  1705. */
  1706. mls_context_destroy(c);
  1707. } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
  1708. /*
  1709. * Switching between non-MLS and MLS policy:
  1710. * ensure that the MLS fields of the context for all
  1711. * existing entries in the sidtab are filled in with a
  1712. * suitable default value, likely taken from one of the
  1713. * initial SIDs.
  1714. */
  1715. oc = args->newp->ocontexts[OCON_ISID];
  1716. while (oc && oc->sid[0] != SECINITSID_UNLABELED)
  1717. oc = oc->next;
  1718. rc = -EINVAL;
  1719. if (!oc) {
  1720. printk(KERN_ERR "SELinux: unable to look up"
  1721. " the initial SIDs list\n");
  1722. goto bad;
  1723. }
  1724. range = &oc->context[0].range;
  1725. rc = mls_range_set(c, range);
  1726. if (rc)
  1727. goto bad;
  1728. }
  1729. /* Check the validity of the new context. */
  1730. if (!policydb_context_isvalid(args->newp, c)) {
  1731. rc = convert_context_handle_invalid_context(&oldc);
  1732. if (rc)
  1733. goto bad;
  1734. }
  1735. context_destroy(&oldc);
  1736. rc = 0;
  1737. out:
  1738. return rc;
  1739. bad:
  1740. /* Map old representation to string and save it. */
  1741. rc = context_struct_to_string(&oldc, &s, &len);
  1742. if (rc)
  1743. return rc;
  1744. context_destroy(&oldc);
  1745. context_destroy(c);
  1746. c->str = s;
  1747. c->len = len;
  1748. printk(KERN_INFO "SELinux: Context %s became invalid (unmapped).\n",
  1749. c->str);
  1750. rc = 0;
  1751. goto out;
  1752. }
  1753. static void security_load_policycaps(void)
  1754. {
  1755. selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
  1756. POLICYDB_CAPABILITY_NETPEER);
  1757. selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
  1758. POLICYDB_CAPABILITY_OPENPERM);
  1759. selinux_policycap_alwaysnetwork = ebitmap_get_bit(&policydb.policycaps,
  1760. POLICYDB_CAPABILITY_ALWAYSNETWORK);
  1761. }
  1762. static int security_preserve_bools(struct policydb *p);
  1763. /**
  1764. * security_load_policy - Load a security policy configuration.
  1765. * @data: binary policy data
  1766. * @len: length of data in bytes
  1767. *
  1768. * Load a new set of security policy configuration data,
  1769. * validate it and convert the SID table as necessary.
  1770. * This function will flush the access vector cache after
  1771. * loading the new policy.
  1772. */
  1773. int security_load_policy(void *data, size_t len)
  1774. {
  1775. struct policydb *oldpolicydb, *newpolicydb;
  1776. struct sidtab oldsidtab, newsidtab;
  1777. struct selinux_mapping *oldmap, *map = NULL;
  1778. struct convert_context_args args;
  1779. u32 seqno;
  1780. u16 map_size;
  1781. int rc = 0;
  1782. struct policy_file file = { data, len }, *fp = &file;
  1783. oldpolicydb = kzalloc(2 * sizeof(*oldpolicydb), GFP_KERNEL);
  1784. if (!oldpolicydb) {
  1785. rc = -ENOMEM;
  1786. goto out;
  1787. }
  1788. newpolicydb = oldpolicydb + 1;
  1789. if (!ss_initialized) {
  1790. avtab_cache_init();
  1791. rc = policydb_read(&policydb, fp);
  1792. if (rc) {
  1793. avtab_cache_destroy();
  1794. goto out;
  1795. }
  1796. policydb.len = len;
  1797. rc = selinux_set_mapping(&policydb, secclass_map,
  1798. &current_mapping,
  1799. &current_mapping_size);
  1800. if (rc) {
  1801. policydb_destroy(&policydb);
  1802. avtab_cache_destroy();
  1803. goto out;
  1804. }
  1805. rc = policydb_load_isids(&policydb, &sidtab);
  1806. if (rc) {
  1807. policydb_destroy(&policydb);
  1808. avtab_cache_destroy();
  1809. goto out;
  1810. }
  1811. security_load_policycaps();
  1812. ss_initialized = 1;
  1813. seqno = ++latest_granting;
  1814. selinux_complete_init();
  1815. avc_ss_reset(seqno);
  1816. selnl_notify_policyload(seqno);
  1817. selinux_status_update_policyload(seqno);
  1818. selinux_netlbl_cache_invalidate();
  1819. selinux_xfrm_notify_policyload();
  1820. goto out;
  1821. }
  1822. #if 0
  1823. sidtab_hash_eval(&sidtab, "sids");
  1824. #endif
  1825. rc = policydb_read(newpolicydb, fp);
  1826. if (rc)
  1827. goto out;
  1828. newpolicydb->len = len;
  1829. /* If switching between different policy types, log MLS status */
  1830. if (policydb.mls_enabled && !newpolicydb->mls_enabled)
  1831. printk(KERN_INFO "SELinux: Disabling MLS support...\n");
  1832. else if (!policydb.mls_enabled && newpolicydb->mls_enabled)
  1833. printk(KERN_INFO "SELinux: Enabling MLS support...\n");
  1834. rc = policydb_load_isids(newpolicydb, &newsidtab);
  1835. if (rc) {
  1836. printk(KERN_ERR "SELinux: unable to load the initial SIDs\n");
  1837. policydb_destroy(newpolicydb);
  1838. goto out;
  1839. }
  1840. rc = selinux_set_mapping(newpolicydb, secclass_map, &map, &map_size);
  1841. if (rc)
  1842. goto err;
  1843. rc = security_preserve_bools(newpolicydb);
  1844. if (rc) {
  1845. printk(KERN_ERR "SELinux: unable to preserve booleans\n");
  1846. goto err;
  1847. }
  1848. /* Clone the SID table. */
  1849. sidtab_shutdown(&sidtab);
  1850. rc = sidtab_map(&sidtab, clone_sid, &newsidtab);
  1851. if (rc)
  1852. goto err;
  1853. /*
  1854. * Convert the internal representations of contexts
  1855. * in the new SID table.
  1856. */
  1857. args.oldp = &policydb;
  1858. args.newp = newpolicydb;
  1859. rc = sidtab_map(&newsidtab, convert_context, &args);
  1860. if (rc) {
  1861. printk(KERN_ERR "SELinux: unable to convert the internal"
  1862. " representation of contexts in the new SID"
  1863. " table\n");
  1864. goto err;
  1865. }
  1866. /* Save the old policydb and SID table to free later. */
  1867. memcpy(oldpolicydb, &policydb, sizeof(policydb));
  1868. sidtab_set(&oldsidtab, &sidtab);
  1869. /* Install the new policydb and SID table. */
  1870. write_lock_irq(&policy_rwlock);
  1871. memcpy(&policydb, newpolicydb, sizeof(policydb));
  1872. sidtab_set(&sidtab, &newsidtab);
  1873. security_load_policycaps();
  1874. oldmap = current_mapping;
  1875. current_mapping = map;
  1876. current_mapping_size = map_size;
  1877. seqno = ++latest_granting;
  1878. write_unlock_irq(&policy_rwlock);
  1879. /* Free the old policydb and SID table. */
  1880. policydb_destroy(oldpolicydb);
  1881. sidtab_destroy(&oldsidtab);
  1882. kfree(oldmap);
  1883. avc_ss_reset(seqno);
  1884. selnl_notify_policyload(seqno);
  1885. selinux_status_update_policyload(seqno);
  1886. selinux_netlbl_cache_invalidate();
  1887. selinux_xfrm_notify_policyload();
  1888. rc = 0;
  1889. goto out;
  1890. err:
  1891. kfree(map);
  1892. sidtab_destroy(&newsidtab);
  1893. policydb_destroy(newpolicydb);
  1894. out:
  1895. kfree(oldpolicydb);
  1896. return rc;
  1897. }
  1898. size_t security_policydb_len(void)
  1899. {
  1900. size_t len;
  1901. read_lock(&policy_rwlock);
  1902. len = policydb.len;
  1903. read_unlock(&policy_rwlock);
  1904. return len;
  1905. }
  1906. /**
  1907. * security_port_sid - Obtain the SID for a port.
  1908. * @protocol: protocol number
  1909. * @port: port number
  1910. * @out_sid: security identifier
  1911. */
  1912. int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
  1913. {
  1914. struct ocontext *c;
  1915. int rc = 0;
  1916. read_lock(&policy_rwlock);
  1917. c = policydb.ocontexts[OCON_PORT];
  1918. while (c) {
  1919. if (c->u.port.protocol == protocol &&
  1920. c->u.port.low_port <= port &&
  1921. c->u.port.high_port >= port)
  1922. break;
  1923. c = c->next;
  1924. }
  1925. if (c) {
  1926. if (!c->sid[0]) {
  1927. rc = sidtab_context_to_sid(&sidtab,
  1928. &c->context[0],
  1929. &c->sid[0]);
  1930. if (rc)
  1931. goto out;
  1932. }
  1933. *out_sid = c->sid[0];
  1934. } else {
  1935. *out_sid = SECINITSID_PORT;
  1936. }
  1937. out:
  1938. read_unlock(&policy_rwlock);
  1939. return rc;
  1940. }
  1941. /**
  1942. * security_netif_sid - Obtain the SID for a network interface.
  1943. * @name: interface name
  1944. * @if_sid: interface SID
  1945. */
  1946. int security_netif_sid(char *name, u32 *if_sid)
  1947. {
  1948. int rc = 0;
  1949. struct ocontext *c;
  1950. read_lock(&policy_rwlock);
  1951. c = policydb.ocontexts[OCON_NETIF];
  1952. while (c) {
  1953. if (strcmp(name, c->u.name) == 0)
  1954. break;
  1955. c = c->next;
  1956. }
  1957. if (c) {
  1958. if (!c->sid[0] || !c->sid[1]) {
  1959. rc = sidtab_context_to_sid(&sidtab,
  1960. &c->context[0],
  1961. &c->sid[0]);
  1962. if (rc)
  1963. goto out;
  1964. rc = sidtab_context_to_sid(&sidtab,
  1965. &c->context[1],
  1966. &c->sid[1]);
  1967. if (rc)
  1968. goto out;
  1969. }
  1970. *if_sid = c->sid[0];
  1971. } else
  1972. *if_sid = SECINITSID_NETIF;
  1973. out:
  1974. read_unlock(&policy_rwlock);
  1975. return rc;
  1976. }
  1977. static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
  1978. {
  1979. int i, fail = 0;
  1980. for (i = 0; i < 4; i++)
  1981. if (addr[i] != (input[i] & mask[i])) {
  1982. fail = 1;
  1983. break;
  1984. }
  1985. return !fail;
  1986. }
  1987. /**
  1988. * security_node_sid - Obtain the SID for a node (host).
  1989. * @domain: communication domain aka address family
  1990. * @addrp: address
  1991. * @addrlen: address length in bytes
  1992. * @out_sid: security identifier
  1993. */
  1994. int security_node_sid(u16 domain,
  1995. void *addrp,
  1996. u32 addrlen,
  1997. u32 *out_sid)
  1998. {
  1999. int rc;
  2000. struct ocontext *c;
  2001. read_lock(&policy_rwlock);
  2002. switch (domain) {
  2003. case AF_INET: {
  2004. u32 addr;
  2005. rc = -EINVAL;
  2006. if (addrlen != sizeof(u32))
  2007. goto out;
  2008. addr = *((u32 *)addrp);
  2009. c = policydb.ocontexts[OCON_NODE];
  2010. while (c) {
  2011. if (c->u.node.addr == (addr & c->u.node.mask))
  2012. break;
  2013. c = c->next;
  2014. }
  2015. break;
  2016. }
  2017. case AF_INET6:
  2018. rc = -EINVAL;
  2019. if (addrlen != sizeof(u64) * 2)
  2020. goto out;
  2021. c = policydb.ocontexts[OCON_NODE6];
  2022. while (c) {
  2023. if (match_ipv6_addrmask(addrp, c->u.node6.addr,
  2024. c->u.node6.mask))
  2025. break;
  2026. c = c->next;
  2027. }
  2028. break;
  2029. default:
  2030. rc = 0;
  2031. *out_sid = SECINITSID_NODE;
  2032. goto out;
  2033. }
  2034. if (c) {
  2035. if (!c->sid[0]) {
  2036. rc = sidtab_context_to_sid(&sidtab,
  2037. &c->context[0],
  2038. &c->sid[0]);
  2039. if (rc)
  2040. goto out;
  2041. }
  2042. *out_sid = c->sid[0];
  2043. } else {
  2044. *out_sid = SECINITSID_NODE;
  2045. }
  2046. rc = 0;
  2047. out:
  2048. read_unlock(&policy_rwlock);
  2049. return rc;
  2050. }
  2051. #define SIDS_NEL 25
  2052. /**
  2053. * security_get_user_sids - Obtain reachable SIDs for a user.
  2054. * @fromsid: starting SID
  2055. * @username: username
  2056. * @sids: array of reachable SIDs for user
  2057. * @nel: number of elements in @sids
  2058. *
  2059. * Generate the set of SIDs for legal security contexts
  2060. * for a given user that can be reached by @fromsid.
  2061. * Set *@sids to point to a dynamically allocated
  2062. * array containing the set of SIDs. Set *@nel to the
  2063. * number of elements in the array.
  2064. */
  2065. int security_get_user_sids(u32 fromsid,
  2066. char *username,
  2067. u32 **sids,
  2068. u32 *nel)
  2069. {
  2070. struct context *fromcon, usercon;
  2071. u32 *mysids = NULL, *mysids2, sid;
  2072. u32 mynel = 0, maxnel = SIDS_NEL;
  2073. struct user_datum *user;
  2074. struct role_datum *role;
  2075. struct ebitmap_node *rnode, *tnode;
  2076. int rc = 0, i, j;
  2077. *sids = NULL;
  2078. *nel = 0;
  2079. if (!ss_initialized)
  2080. goto out;
  2081. read_lock(&policy_rwlock);
  2082. context_init(&usercon);
  2083. rc = -EINVAL;
  2084. fromcon = sidtab_search(&sidtab, fromsid);
  2085. if (!fromcon)
  2086. goto out_unlock;
  2087. rc = -EINVAL;
  2088. user = hashtab_search(policydb.p_users.table, username);
  2089. if (!user)
  2090. goto out_unlock;
  2091. usercon.user = user->value;
  2092. rc = -ENOMEM;
  2093. mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
  2094. if (!mysids)
  2095. goto out_unlock;
  2096. ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
  2097. role = policydb.role_val_to_struct[i];
  2098. usercon.role = i + 1;
  2099. ebitmap_for_each_positive_bit(&role->types, tnode, j) {
  2100. usercon.type = j + 1;
  2101. if (mls_setup_user_range(fromcon, user, &usercon))
  2102. continue;
  2103. rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
  2104. if (rc)
  2105. goto out_unlock;
  2106. if (mynel < maxnel) {
  2107. mysids[mynel++] = sid;
  2108. } else {
  2109. rc = -ENOMEM;
  2110. maxnel += SIDS_NEL;
  2111. mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
  2112. if (!mysids2)
  2113. goto out_unlock;
  2114. memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
  2115. kfree(mysids);
  2116. mysids = mysids2;
  2117. mysids[mynel++] = sid;
  2118. }
  2119. }
  2120. }
  2121. rc = 0;
  2122. out_unlock:
  2123. read_unlock(&policy_rwlock);
  2124. if (rc || !mynel) {
  2125. kfree(mysids);
  2126. goto out;
  2127. }
  2128. rc = -ENOMEM;
  2129. mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
  2130. if (!mysids2) {
  2131. kfree(mysids);
  2132. goto out;
  2133. }
  2134. for (i = 0, j = 0; i < mynel; i++) {
  2135. struct av_decision dummy_avd;
  2136. rc = avc_has_perm_noaudit(fromsid, mysids[i],
  2137. SECCLASS_PROCESS, /* kernel value */
  2138. PROCESS__TRANSITION, AVC_STRICT,
  2139. &dummy_avd);
  2140. if (!rc)
  2141. mysids2[j++] = mysids[i];
  2142. cond_resched();
  2143. }
  2144. rc = 0;
  2145. kfree(mysids);
  2146. *sids = mysids2;
  2147. *nel = j;
  2148. out:
  2149. return rc;
  2150. }
  2151. /**
  2152. * __security_genfs_sid - Helper to obtain a SID for a file in a filesystem
  2153. * @fstype: filesystem type
  2154. * @path: path from root of mount
  2155. * @sclass: file security class
  2156. * @sid: SID for path
  2157. *
  2158. * Obtain a SID to use for a file in a filesystem that
  2159. * cannot support xattr or use a fixed labeling behavior like
  2160. * transition SIDs or task SIDs.
  2161. *
  2162. * The caller must acquire the policy_rwlock before calling this function.
  2163. */
  2164. static inline int __security_genfs_sid(const char *fstype,
  2165. char *path,
  2166. u16 orig_sclass,
  2167. u32 *sid)
  2168. {
  2169. int len;
  2170. u16 sclass;
  2171. struct genfs *genfs;
  2172. struct ocontext *c;
  2173. int rc, cmp = 0;
  2174. while (path[0] == '/' && path[1] == '/')
  2175. path++;
  2176. sclass = unmap_class(orig_sclass);
  2177. *sid = SECINITSID_UNLABELED;
  2178. for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
  2179. cmp = strcmp(fstype, genfs->fstype);
  2180. if (cmp <= 0)
  2181. break;
  2182. }
  2183. rc = -ENOENT;
  2184. if (!genfs || cmp)
  2185. goto out;
  2186. for (c = genfs->head; c; c = c->next) {
  2187. len = strlen(c->u.name);
  2188. if ((!c->v.sclass || sclass == c->v.sclass) &&
  2189. (strncmp(c->u.name, path, len) == 0))
  2190. break;
  2191. }
  2192. rc = -ENOENT;
  2193. if (!c)
  2194. goto out;
  2195. if (!c->sid[0]) {
  2196. rc = sidtab_context_to_sid(&sidtab, &c->context[0], &c->sid[0]);
  2197. if (rc)
  2198. goto out;
  2199. }
  2200. *sid = c->sid[0];
  2201. rc = 0;
  2202. out:
  2203. return rc;
  2204. }
  2205. /**
  2206. * security_genfs_sid - Obtain a SID for a file in a filesystem
  2207. * @fstype: filesystem type
  2208. * @path: path from root of mount
  2209. * @sclass: file security class
  2210. * @sid: SID for path
  2211. *
  2212. * Acquire policy_rwlock before calling __security_genfs_sid() and release
  2213. * it afterward.
  2214. */
  2215. int security_genfs_sid(const char *fstype,
  2216. char *path,
  2217. u16 orig_sclass,
  2218. u32 *sid)
  2219. {
  2220. int retval;
  2221. read_lock(&policy_rwlock);
  2222. retval = __security_genfs_sid(fstype, path, orig_sclass, sid);
  2223. read_unlock(&policy_rwlock);
  2224. return retval;
  2225. }
  2226. /**
  2227. * security_fs_use - Determine how to handle labeling for a filesystem.
  2228. * @sb: superblock in question
  2229. */
  2230. int security_fs_use(struct super_block *sb)
  2231. {
  2232. int rc = 0;
  2233. struct ocontext *c;
  2234. struct superblock_security_struct *sbsec = sb->s_security;
  2235. const char *fstype = sb->s_type->name;
  2236. read_lock(&policy_rwlock);
  2237. c = policydb.ocontexts[OCON_FSUSE];
  2238. while (c) {
  2239. if (strcmp(fstype, c->u.name) == 0)
  2240. break;
  2241. c = c->next;
  2242. }
  2243. if (c) {
  2244. sbsec->behavior = c->v.behavior;
  2245. if (!c->sid[0]) {
  2246. rc = sidtab_context_to_sid(&sidtab, &c->context[0],
  2247. &c->sid[0]);
  2248. if (rc)
  2249. goto out;
  2250. }
  2251. sbsec->sid = c->sid[0];
  2252. } else {
  2253. rc = __security_genfs_sid(fstype, "/", SECCLASS_DIR,
  2254. &sbsec->sid);
  2255. if (rc) {
  2256. sbsec->behavior = SECURITY_FS_USE_NONE;
  2257. rc = 0;
  2258. } else {
  2259. sbsec->behavior = SECURITY_FS_USE_GENFS;
  2260. }
  2261. }
  2262. out:
  2263. read_unlock(&policy_rwlock);
  2264. return rc;
  2265. }
  2266. int security_get_bools(int *len, char ***names, int **values)
  2267. {
  2268. int i, rc;
  2269. read_lock(&policy_rwlock);
  2270. *names = NULL;
  2271. *values = NULL;
  2272. rc = 0;
  2273. *len = policydb.p_bools.nprim;
  2274. if (!*len)
  2275. goto out;
  2276. rc = -ENOMEM;
  2277. *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
  2278. if (!*names)
  2279. goto err;
  2280. rc = -ENOMEM;
  2281. *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
  2282. if (!*values)
  2283. goto err;
  2284. for (i = 0; i < *len; i++) {
  2285. size_t name_len;
  2286. (*values)[i] = policydb.bool_val_to_struct[i]->state;
  2287. name_len = strlen(sym_name(&policydb, SYM_BOOLS, i)) + 1;
  2288. rc = -ENOMEM;
  2289. (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
  2290. if (!(*names)[i])
  2291. goto err;
  2292. strncpy((*names)[i], sym_name(&policydb, SYM_BOOLS, i), name_len);
  2293. (*names)[i][name_len - 1] = 0;
  2294. }
  2295. rc = 0;
  2296. out:
  2297. read_unlock(&policy_rwlock);
  2298. return rc;
  2299. err:
  2300. if (*names) {
  2301. for (i = 0; i < *len; i++)
  2302. kfree((*names)[i]);
  2303. }
  2304. kfree(*values);
  2305. goto out;
  2306. }
  2307. int security_set_bools(int len, int *values)
  2308. {
  2309. int i, rc;
  2310. int lenp, seqno = 0;
  2311. struct cond_node *cur;
  2312. write_lock_irq(&policy_rwlock);
  2313. rc = -EFAULT;
  2314. lenp = policydb.p_bools.nprim;
  2315. if (len != lenp)
  2316. goto out;
  2317. for (i = 0; i < len; i++) {
  2318. if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
  2319. audit_log(current->audit_context, GFP_ATOMIC,
  2320. AUDIT_MAC_CONFIG_CHANGE,
  2321. "bool=%s val=%d old_val=%d auid=%u ses=%u",
  2322. sym_name(&policydb, SYM_BOOLS, i),
  2323. !!values[i],
  2324. policydb.bool_val_to_struct[i]->state,
  2325. from_kuid(&init_user_ns, audit_get_loginuid(current)),
  2326. audit_get_sessionid(current));
  2327. }
  2328. if (values[i])
  2329. policydb.bool_val_to_struct[i]->state = 1;
  2330. else
  2331. policydb.bool_val_to_struct[i]->state = 0;
  2332. }
  2333. for (cur = policydb.cond_list; cur; cur = cur->next) {
  2334. rc = evaluate_cond_node(&policydb, cur);
  2335. if (rc)
  2336. goto out;
  2337. }
  2338. seqno = ++latest_granting;
  2339. rc = 0;
  2340. out:
  2341. write_unlock_irq(&policy_rwlock);
  2342. if (!rc) {
  2343. avc_ss_reset(seqno);
  2344. selnl_notify_policyload(seqno);
  2345. selinux_status_update_policyload(seqno);
  2346. selinux_xfrm_notify_policyload();
  2347. }
  2348. return rc;
  2349. }
  2350. int security_get_bool_value(int bool)
  2351. {
  2352. int rc;
  2353. int len;
  2354. read_lock(&policy_rwlock);
  2355. rc = -EFAULT;
  2356. len = policydb.p_bools.nprim;
  2357. if (bool >= len)
  2358. goto out;
  2359. rc = policydb.bool_val_to_struct[bool]->state;
  2360. out:
  2361. read_unlock(&policy_rwlock);
  2362. return rc;
  2363. }
  2364. static int security_preserve_bools(struct policydb *p)
  2365. {
  2366. int rc, nbools = 0, *bvalues = NULL, i;
  2367. char **bnames = NULL;
  2368. struct cond_bool_datum *booldatum;
  2369. struct cond_node *cur;
  2370. rc = security_get_bools(&nbools, &bnames, &bvalues);
  2371. if (rc)
  2372. goto out;
  2373. for (i = 0; i < nbools; i++) {
  2374. booldatum = hashtab_search(p->p_bools.table, bnames[i]);
  2375. if (booldatum)
  2376. booldatum->state = bvalues[i];
  2377. }
  2378. for (cur = p->cond_list; cur; cur = cur->next) {
  2379. rc = evaluate_cond_node(p, cur);
  2380. if (rc)
  2381. goto out;
  2382. }
  2383. out:
  2384. if (bnames) {
  2385. for (i = 0; i < nbools; i++)
  2386. kfree(bnames[i]);
  2387. }
  2388. kfree(bnames);
  2389. kfree(bvalues);
  2390. return rc;
  2391. }
  2392. /*
  2393. * security_sid_mls_copy() - computes a new sid based on the given
  2394. * sid and the mls portion of mls_sid.
  2395. */
  2396. int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
  2397. {
  2398. struct context *context1;
  2399. struct context *context2;
  2400. struct context newcon;
  2401. char *s;
  2402. u32 len;
  2403. int rc;
  2404. rc = 0;
  2405. if (!ss_initialized || !policydb.mls_enabled) {
  2406. *new_sid = sid;
  2407. goto out;
  2408. }
  2409. context_init(&newcon);
  2410. read_lock(&policy_rwlock);
  2411. rc = -EINVAL;
  2412. context1 = sidtab_search(&sidtab, sid);
  2413. if (!context1) {
  2414. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  2415. __func__, sid);
  2416. goto out_unlock;
  2417. }
  2418. rc = -EINVAL;
  2419. context2 = sidtab_search(&sidtab, mls_sid);
  2420. if (!context2) {
  2421. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  2422. __func__, mls_sid);
  2423. goto out_unlock;
  2424. }
  2425. newcon.user = context1->user;
  2426. newcon.role = context1->role;
  2427. newcon.type = context1->type;
  2428. rc = mls_context_cpy(&newcon, context2);
  2429. if (rc)
  2430. goto out_unlock;
  2431. /* Check the validity of the new context. */
  2432. if (!policydb_context_isvalid(&policydb, &newcon)) {
  2433. rc = convert_context_handle_invalid_context(&newcon);
  2434. if (rc) {
  2435. if (!context_struct_to_string(&newcon, &s, &len)) {
  2436. audit_log(current->audit_context,
  2437. GFP_ATOMIC, AUDIT_SELINUX_ERR,
  2438. "op=security_sid_mls_copy "
  2439. "invalid_context=%s", s);
  2440. kfree(s);
  2441. }
  2442. goto out_unlock;
  2443. }
  2444. }
  2445. rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
  2446. out_unlock:
  2447. read_unlock(&policy_rwlock);
  2448. context_destroy(&newcon);
  2449. out:
  2450. return rc;
  2451. }
  2452. /**
  2453. * security_net_peersid_resolve - Compare and resolve two network peer SIDs
  2454. * @nlbl_sid: NetLabel SID
  2455. * @nlbl_type: NetLabel labeling protocol type
  2456. * @xfrm_sid: XFRM SID
  2457. *
  2458. * Description:
  2459. * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
  2460. * resolved into a single SID it is returned via @peer_sid and the function
  2461. * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
  2462. * returns a negative value. A table summarizing the behavior is below:
  2463. *
  2464. * | function return | @sid
  2465. * ------------------------------+-----------------+-----------------
  2466. * no peer labels | 0 | SECSID_NULL
  2467. * single peer label | 0 | <peer_label>
  2468. * multiple, consistent labels | 0 | <peer_label>
  2469. * multiple, inconsistent labels | -<errno> | SECSID_NULL
  2470. *
  2471. */
  2472. int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
  2473. u32 xfrm_sid,
  2474. u32 *peer_sid)
  2475. {
  2476. int rc;
  2477. struct context *nlbl_ctx;
  2478. struct context *xfrm_ctx;
  2479. *peer_sid = SECSID_NULL;
  2480. /* handle the common (which also happens to be the set of easy) cases
  2481. * right away, these two if statements catch everything involving a
  2482. * single or absent peer SID/label */
  2483. if (xfrm_sid == SECSID_NULL) {
  2484. *peer_sid = nlbl_sid;
  2485. return 0;
  2486. }
  2487. /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
  2488. * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
  2489. * is present */
  2490. if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
  2491. *peer_sid = xfrm_sid;
  2492. return 0;
  2493. }
  2494. /* we don't need to check ss_initialized here since the only way both
  2495. * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
  2496. * security server was initialized and ss_initialized was true */
  2497. if (!policydb.mls_enabled)
  2498. return 0;
  2499. read_lock(&policy_rwlock);
  2500. rc = -EINVAL;
  2501. nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
  2502. if (!nlbl_ctx) {
  2503. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  2504. __func__, nlbl_sid);
  2505. goto out;
  2506. }
  2507. rc = -EINVAL;
  2508. xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
  2509. if (!xfrm_ctx) {
  2510. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  2511. __func__, xfrm_sid);
  2512. goto out;
  2513. }
  2514. rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
  2515. if (rc)
  2516. goto out;
  2517. /* at present NetLabel SIDs/labels really only carry MLS
  2518. * information so if the MLS portion of the NetLabel SID
  2519. * matches the MLS portion of the labeled XFRM SID/label
  2520. * then pass along the XFRM SID as it is the most
  2521. * expressive */
  2522. *peer_sid = xfrm_sid;
  2523. out:
  2524. read_unlock(&policy_rwlock);
  2525. return rc;
  2526. }
  2527. static int get_classes_callback(void *k, void *d, void *args)
  2528. {
  2529. struct class_datum *datum = d;
  2530. char *name = k, **classes = args;
  2531. int value = datum->value - 1;
  2532. classes[value] = kstrdup(name, GFP_ATOMIC);
  2533. if (!classes[value])
  2534. return -ENOMEM;
  2535. return 0;
  2536. }
  2537. int security_get_classes(char ***classes, int *nclasses)
  2538. {
  2539. int rc;
  2540. read_lock(&policy_rwlock);
  2541. rc = -ENOMEM;
  2542. *nclasses = policydb.p_classes.nprim;
  2543. *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
  2544. if (!*classes)
  2545. goto out;
  2546. rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
  2547. *classes);
  2548. if (rc) {
  2549. int i;
  2550. for (i = 0; i < *nclasses; i++)
  2551. kfree((*classes)[i]);
  2552. kfree(*classes);
  2553. }
  2554. out:
  2555. read_unlock(&policy_rwlock);
  2556. return rc;
  2557. }
  2558. static int get_permissions_callback(void *k, void *d, void *args)
  2559. {
  2560. struct perm_datum *datum = d;
  2561. char *name = k, **perms = args;
  2562. int value = datum->value - 1;
  2563. perms[value] = kstrdup(name, GFP_ATOMIC);
  2564. if (!perms[value])
  2565. return -ENOMEM;
  2566. return 0;
  2567. }
  2568. int security_get_permissions(char *class, char ***perms, int *nperms)
  2569. {
  2570. int rc, i;
  2571. struct class_datum *match;
  2572. read_lock(&policy_rwlock);
  2573. rc = -EINVAL;
  2574. match = hashtab_search(policydb.p_classes.table, class);
  2575. if (!match) {
  2576. printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
  2577. __func__, class);
  2578. goto out;
  2579. }
  2580. rc = -ENOMEM;
  2581. *nperms = match->permissions.nprim;
  2582. *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC);
  2583. if (!*perms)
  2584. goto out;
  2585. if (match->comdatum) {
  2586. rc = hashtab_map(match->comdatum->permissions.table,
  2587. get_permissions_callback, *perms);
  2588. if (rc)
  2589. goto err;
  2590. }
  2591. rc = hashtab_map(match->permissions.table, get_permissions_callback,
  2592. *perms);
  2593. if (rc)
  2594. goto err;
  2595. out:
  2596. read_unlock(&policy_rwlock);
  2597. return rc;
  2598. err:
  2599. read_unlock(&policy_rwlock);
  2600. for (i = 0; i < *nperms; i++)
  2601. kfree((*perms)[i]);
  2602. kfree(*perms);
  2603. return rc;
  2604. }
  2605. int security_get_reject_unknown(void)
  2606. {
  2607. return policydb.reject_unknown;
  2608. }
  2609. int security_get_allow_unknown(void)
  2610. {
  2611. return policydb.allow_unknown;
  2612. }
  2613. /**
  2614. * security_policycap_supported - Check for a specific policy capability
  2615. * @req_cap: capability
  2616. *
  2617. * Description:
  2618. * This function queries the currently loaded policy to see if it supports the
  2619. * capability specified by @req_cap. Returns true (1) if the capability is
  2620. * supported, false (0) if it isn't supported.
  2621. *
  2622. */
  2623. int security_policycap_supported(unsigned int req_cap)
  2624. {
  2625. int rc;
  2626. read_lock(&policy_rwlock);
  2627. rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
  2628. read_unlock(&policy_rwlock);
  2629. return rc;
  2630. }
  2631. struct selinux_audit_rule {
  2632. u32 au_seqno;
  2633. struct context au_ctxt;
  2634. };
  2635. void selinux_audit_rule_free(void *vrule)
  2636. {
  2637. struct selinux_audit_rule *rule = vrule;
  2638. if (rule) {
  2639. context_destroy(&rule->au_ctxt);
  2640. kfree(rule);
  2641. }
  2642. }
  2643. int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
  2644. {
  2645. struct selinux_audit_rule *tmprule;
  2646. struct role_datum *roledatum;
  2647. struct type_datum *typedatum;
  2648. struct user_datum *userdatum;
  2649. struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
  2650. int rc = 0;
  2651. *rule = NULL;
  2652. if (!ss_initialized)
  2653. return -EOPNOTSUPP;
  2654. switch (field) {
  2655. case AUDIT_SUBJ_USER:
  2656. case AUDIT_SUBJ_ROLE:
  2657. case AUDIT_SUBJ_TYPE:
  2658. case AUDIT_OBJ_USER:
  2659. case AUDIT_OBJ_ROLE:
  2660. case AUDIT_OBJ_TYPE:
  2661. /* only 'equals' and 'not equals' fit user, role, and type */
  2662. if (op != Audit_equal && op != Audit_not_equal)
  2663. return -EINVAL;
  2664. break;
  2665. case AUDIT_SUBJ_SEN:
  2666. case AUDIT_SUBJ_CLR:
  2667. case AUDIT_OBJ_LEV_LOW:
  2668. case AUDIT_OBJ_LEV_HIGH:
  2669. /* we do not allow a range, indicated by the presence of '-' */
  2670. if (strchr(rulestr, '-'))
  2671. return -EINVAL;
  2672. break;
  2673. default:
  2674. /* only the above fields are valid */
  2675. return -EINVAL;
  2676. }
  2677. tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
  2678. if (!tmprule)
  2679. return -ENOMEM;
  2680. context_init(&tmprule->au_ctxt);
  2681. read_lock(&policy_rwlock);
  2682. tmprule->au_seqno = latest_granting;
  2683. switch (field) {
  2684. case AUDIT_SUBJ_USER:
  2685. case AUDIT_OBJ_USER:
  2686. rc = -EINVAL;
  2687. userdatum = hashtab_search(policydb.p_users.table, rulestr);
  2688. if (!userdatum)
  2689. goto out;
  2690. tmprule->au_ctxt.user = userdatum->value;
  2691. break;
  2692. case AUDIT_SUBJ_ROLE:
  2693. case AUDIT_OBJ_ROLE:
  2694. rc = -EINVAL;
  2695. roledatum = hashtab_search(policydb.p_roles.table, rulestr);
  2696. if (!roledatum)
  2697. goto out;
  2698. tmprule->au_ctxt.role = roledatum->value;
  2699. break;
  2700. case AUDIT_SUBJ_TYPE:
  2701. case AUDIT_OBJ_TYPE:
  2702. rc = -EINVAL;
  2703. typedatum = hashtab_search(policydb.p_types.table, rulestr);
  2704. if (!typedatum)
  2705. goto out;
  2706. tmprule->au_ctxt.type = typedatum->value;
  2707. break;
  2708. case AUDIT_SUBJ_SEN:
  2709. case AUDIT_SUBJ_CLR:
  2710. case AUDIT_OBJ_LEV_LOW:
  2711. case AUDIT_OBJ_LEV_HIGH:
  2712. rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
  2713. if (rc)
  2714. goto out;
  2715. break;
  2716. }
  2717. rc = 0;
  2718. out:
  2719. read_unlock(&policy_rwlock);
  2720. if (rc) {
  2721. selinux_audit_rule_free(tmprule);
  2722. tmprule = NULL;
  2723. }
  2724. *rule = tmprule;
  2725. return rc;
  2726. }
  2727. /* Check to see if the rule contains any selinux fields */
  2728. int selinux_audit_rule_known(struct audit_krule *rule)
  2729. {
  2730. int i;
  2731. for (i = 0; i < rule->field_count; i++) {
  2732. struct audit_field *f = &rule->fields[i];
  2733. switch (f->type) {
  2734. case AUDIT_SUBJ_USER:
  2735. case AUDIT_SUBJ_ROLE:
  2736. case AUDIT_SUBJ_TYPE:
  2737. case AUDIT_SUBJ_SEN:
  2738. case AUDIT_SUBJ_CLR:
  2739. case AUDIT_OBJ_USER:
  2740. case AUDIT_OBJ_ROLE:
  2741. case AUDIT_OBJ_TYPE:
  2742. case AUDIT_OBJ_LEV_LOW:
  2743. case AUDIT_OBJ_LEV_HIGH:
  2744. return 1;
  2745. }
  2746. }
  2747. return 0;
  2748. }
  2749. int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
  2750. struct audit_context *actx)
  2751. {
  2752. struct context *ctxt;
  2753. struct mls_level *level;
  2754. struct selinux_audit_rule *rule = vrule;
  2755. int match = 0;
  2756. if (unlikely(!rule)) {
  2757. WARN_ONCE(1, "selinux_audit_rule_match: missing rule\n");
  2758. return -ENOENT;
  2759. }
  2760. read_lock(&policy_rwlock);
  2761. if (rule->au_seqno < latest_granting) {
  2762. match = -ESTALE;
  2763. goto out;
  2764. }
  2765. ctxt = sidtab_search(&sidtab, sid);
  2766. if (unlikely(!ctxt)) {
  2767. WARN_ONCE(1, "selinux_audit_rule_match: unrecognized SID %d\n",
  2768. sid);
  2769. match = -ENOENT;
  2770. goto out;
  2771. }
  2772. /* a field/op pair that is not caught here will simply fall through
  2773. without a match */
  2774. switch (field) {
  2775. case AUDIT_SUBJ_USER:
  2776. case AUDIT_OBJ_USER:
  2777. switch (op) {
  2778. case Audit_equal:
  2779. match = (ctxt->user == rule->au_ctxt.user);
  2780. break;
  2781. case Audit_not_equal:
  2782. match = (ctxt->user != rule->au_ctxt.user);
  2783. break;
  2784. }
  2785. break;
  2786. case AUDIT_SUBJ_ROLE:
  2787. case AUDIT_OBJ_ROLE:
  2788. switch (op) {
  2789. case Audit_equal:
  2790. match = (ctxt->role == rule->au_ctxt.role);
  2791. break;
  2792. case Audit_not_equal:
  2793. match = (ctxt->role != rule->au_ctxt.role);
  2794. break;
  2795. }
  2796. break;
  2797. case AUDIT_SUBJ_TYPE:
  2798. case AUDIT_OBJ_TYPE:
  2799. switch (op) {
  2800. case Audit_equal:
  2801. match = (ctxt->type == rule->au_ctxt.type);
  2802. break;
  2803. case Audit_not_equal:
  2804. match = (ctxt->type != rule->au_ctxt.type);
  2805. break;
  2806. }
  2807. break;
  2808. case AUDIT_SUBJ_SEN:
  2809. case AUDIT_SUBJ_CLR:
  2810. case AUDIT_OBJ_LEV_LOW:
  2811. case AUDIT_OBJ_LEV_HIGH:
  2812. level = ((field == AUDIT_SUBJ_SEN ||
  2813. field == AUDIT_OBJ_LEV_LOW) ?
  2814. &ctxt->range.level[0] : &ctxt->range.level[1]);
  2815. switch (op) {
  2816. case Audit_equal:
  2817. match = mls_level_eq(&rule->au_ctxt.range.level[0],
  2818. level);
  2819. break;
  2820. case Audit_not_equal:
  2821. match = !mls_level_eq(&rule->au_ctxt.range.level[0],
  2822. level);
  2823. break;
  2824. case Audit_lt:
  2825. match = (mls_level_dom(&rule->au_ctxt.range.level[0],
  2826. level) &&
  2827. !mls_level_eq(&rule->au_ctxt.range.level[0],
  2828. level));
  2829. break;
  2830. case Audit_le:
  2831. match = mls_level_dom(&rule->au_ctxt.range.level[0],
  2832. level);
  2833. break;
  2834. case Audit_gt:
  2835. match = (mls_level_dom(level,
  2836. &rule->au_ctxt.range.level[0]) &&
  2837. !mls_level_eq(level,
  2838. &rule->au_ctxt.range.level[0]));
  2839. break;
  2840. case Audit_ge:
  2841. match = mls_level_dom(level,
  2842. &rule->au_ctxt.range.level[0]);
  2843. break;
  2844. }
  2845. }
  2846. out:
  2847. read_unlock(&policy_rwlock);
  2848. return match;
  2849. }
  2850. static int (*aurule_callback)(void) = audit_update_lsm_rules;
  2851. static int aurule_avc_callback(u32 event)
  2852. {
  2853. int err = 0;
  2854. if (event == AVC_CALLBACK_RESET && aurule_callback)
  2855. err = aurule_callback();
  2856. return err;
  2857. }
  2858. static int __init aurule_init(void)
  2859. {
  2860. int err;
  2861. err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET);
  2862. if (err)
  2863. panic("avc_add_callback() failed, error %d\n", err);
  2864. return err;
  2865. }
  2866. __initcall(aurule_init);
  2867. #ifdef CONFIG_NETLABEL
  2868. /**
  2869. * security_netlbl_cache_add - Add an entry to the NetLabel cache
  2870. * @secattr: the NetLabel packet security attributes
  2871. * @sid: the SELinux SID
  2872. *
  2873. * Description:
  2874. * Attempt to cache the context in @ctx, which was derived from the packet in
  2875. * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
  2876. * already been initialized.
  2877. *
  2878. */
  2879. static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
  2880. u32 sid)
  2881. {
  2882. u32 *sid_cache;
  2883. sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
  2884. if (sid_cache == NULL)
  2885. return;
  2886. secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
  2887. if (secattr->cache == NULL) {
  2888. kfree(sid_cache);
  2889. return;
  2890. }
  2891. *sid_cache = sid;
  2892. secattr->cache->free = kfree;
  2893. secattr->cache->data = sid_cache;
  2894. secattr->flags |= NETLBL_SECATTR_CACHE;
  2895. }
  2896. /**
  2897. * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
  2898. * @secattr: the NetLabel packet security attributes
  2899. * @sid: the SELinux SID
  2900. *
  2901. * Description:
  2902. * Convert the given NetLabel security attributes in @secattr into a
  2903. * SELinux SID. If the @secattr field does not contain a full SELinux
  2904. * SID/context then use SECINITSID_NETMSG as the foundation. If possible the
  2905. * 'cache' field of @secattr is set and the CACHE flag is set; this is to
  2906. * allow the @secattr to be used by NetLabel to cache the secattr to SID
  2907. * conversion for future lookups. Returns zero on success, negative values on
  2908. * failure.
  2909. *
  2910. */
  2911. int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
  2912. u32 *sid)
  2913. {
  2914. int rc;
  2915. struct context *ctx;
  2916. struct context ctx_new;
  2917. if (!ss_initialized) {
  2918. *sid = SECSID_NULL;
  2919. return 0;
  2920. }
  2921. read_lock(&policy_rwlock);
  2922. if (secattr->flags & NETLBL_SECATTR_CACHE)
  2923. *sid = *(u32 *)secattr->cache->data;
  2924. else if (secattr->flags & NETLBL_SECATTR_SECID)
  2925. *sid = secattr->attr.secid;
  2926. else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
  2927. rc = -EIDRM;
  2928. ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
  2929. if (ctx == NULL)
  2930. goto out;
  2931. context_init(&ctx_new);
  2932. ctx_new.user = ctx->user;
  2933. ctx_new.role = ctx->role;
  2934. ctx_new.type = ctx->type;
  2935. mls_import_netlbl_lvl(&ctx_new, secattr);
  2936. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  2937. rc = ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
  2938. secattr->attr.mls.cat);
  2939. if (rc)
  2940. goto out;
  2941. memcpy(&ctx_new.range.level[1].cat,
  2942. &ctx_new.range.level[0].cat,
  2943. sizeof(ctx_new.range.level[0].cat));
  2944. }
  2945. rc = -EIDRM;
  2946. if (!mls_context_isvalid(&policydb, &ctx_new))
  2947. goto out_free;
  2948. rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
  2949. if (rc)
  2950. goto out_free;
  2951. security_netlbl_cache_add(secattr, *sid);
  2952. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2953. } else
  2954. *sid = SECSID_NULL;
  2955. read_unlock(&policy_rwlock);
  2956. return 0;
  2957. out_free:
  2958. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2959. out:
  2960. read_unlock(&policy_rwlock);
  2961. return rc;
  2962. }
  2963. /**
  2964. * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
  2965. * @sid: the SELinux SID
  2966. * @secattr: the NetLabel packet security attributes
  2967. *
  2968. * Description:
  2969. * Convert the given SELinux SID in @sid into a NetLabel security attribute.
  2970. * Returns zero on success, negative values on failure.
  2971. *
  2972. */
  2973. int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
  2974. {
  2975. int rc;
  2976. struct context *ctx;
  2977. if (!ss_initialized)
  2978. return 0;
  2979. read_lock(&policy_rwlock);
  2980. rc = -ENOENT;
  2981. ctx = sidtab_search(&sidtab, sid);
  2982. if (ctx == NULL)
  2983. goto out;
  2984. rc = -ENOMEM;
  2985. secattr->domain = kstrdup(sym_name(&policydb, SYM_TYPES, ctx->type - 1),
  2986. GFP_ATOMIC);
  2987. if (secattr->domain == NULL)
  2988. goto out;
  2989. secattr->attr.secid = sid;
  2990. secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
  2991. mls_export_netlbl_lvl(ctx, secattr);
  2992. rc = mls_export_netlbl_cat(ctx, secattr);
  2993. out:
  2994. read_unlock(&policy_rwlock);
  2995. return rc;
  2996. }
  2997. #endif /* CONFIG_NETLABEL */
  2998. /**
  2999. * security_read_policy - read the policy.
  3000. * @data: binary policy data
  3001. * @len: length of data in bytes
  3002. *
  3003. */
  3004. int security_read_policy(void **data, size_t *len)
  3005. {
  3006. int rc;
  3007. struct policy_file fp;
  3008. if (!ss_initialized)
  3009. return -EINVAL;
  3010. *len = security_policydb_len();
  3011. *data = vmalloc_user(*len);
  3012. if (!*data)
  3013. return -ENOMEM;
  3014. fp.data = *data;
  3015. fp.len = *len;
  3016. read_lock(&policy_rwlock);
  3017. rc = policydb_write(&policydb, &fp);
  3018. read_unlock(&policy_rwlock);
  3019. if (rc)
  3020. return rc;
  3021. *len = (unsigned long)fp.data - (unsigned long)*data;
  3022. return 0;
  3023. }