services.c 97 KB

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