services.c 88 KB

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