services.c 80 KB

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