services.c 80 KB

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