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_alwaysnetwork;
  74. static DEFINE_RWLOCK(policy_rwlock);
  75. static struct sidtab sidtab;
  76. struct policydb policydb;
  77. int ss_initialized;
  78. /*
  79. * The largest sequence number that has been used when
  80. * providing an access decision to the access vector cache.
  81. * The sequence number only changes when a policy change
  82. * occurs.
  83. */
  84. static u32 latest_granting;
  85. /* Forward declaration. */
  86. static int context_struct_to_string(struct context *context, char **scontext,
  87. u32 *scontext_len);
  88. static void context_struct_compute_av(struct context *scontext,
  89. struct context *tcontext,
  90. u16 tclass,
  91. struct av_decision *avd,
  92. struct extended_perms *xperms);
  93. struct selinux_mapping {
  94. u16 value; /* policy value */
  95. unsigned num_perms;
  96. u32 perms[sizeof(u32) * 8];
  97. };
  98. static struct selinux_mapping *current_mapping;
  99. static u16 current_mapping_size;
  100. static int selinux_set_mapping(struct policydb *pol,
  101. struct security_class_mapping *map,
  102. struct selinux_mapping **out_map_p,
  103. u16 *out_map_size)
  104. {
  105. struct selinux_mapping *out_map = NULL;
  106. size_t size = sizeof(struct selinux_mapping);
  107. u16 i, j;
  108. unsigned k;
  109. bool print_unknown_handle = false;
  110. /* Find number of classes in the input mapping */
  111. if (!map)
  112. return -EINVAL;
  113. i = 0;
  114. while (map[i].name)
  115. i++;
  116. /* Allocate space for the class records, plus one for class zero */
  117. out_map = kcalloc(++i, size, GFP_ATOMIC);
  118. if (!out_map)
  119. return -ENOMEM;
  120. /* Store the raw class and permission values */
  121. j = 0;
  122. while (map[j].name) {
  123. struct security_class_mapping *p_in = map + (j++);
  124. struct selinux_mapping *p_out = out_map + j;
  125. /* An empty class string skips ahead */
  126. if (!strcmp(p_in->name, "")) {
  127. p_out->num_perms = 0;
  128. continue;
  129. }
  130. p_out->value = string_to_security_class(pol, p_in->name);
  131. if (!p_out->value) {
  132. printk(KERN_INFO
  133. "SELinux: Class %s not defined in policy.\n",
  134. p_in->name);
  135. if (pol->reject_unknown)
  136. goto err;
  137. p_out->num_perms = 0;
  138. print_unknown_handle = true;
  139. continue;
  140. }
  141. k = 0;
  142. while (p_in->perms[k]) {
  143. /* An empty permission string skips ahead */
  144. if (!*p_in->perms[k]) {
  145. k++;
  146. continue;
  147. }
  148. p_out->perms[k] = string_to_av_perm(pol, p_out->value,
  149. p_in->perms[k]);
  150. if (!p_out->perms[k]) {
  151. printk(KERN_INFO
  152. "SELinux: Permission %s in class %s not defined in policy.\n",
  153. p_in->perms[k], p_in->name);
  154. if (pol->reject_unknown)
  155. goto err;
  156. print_unknown_handle = true;
  157. }
  158. k++;
  159. }
  160. p_out->num_perms = k;
  161. }
  162. if (print_unknown_handle)
  163. printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
  164. pol->allow_unknown ? "allowed" : "denied");
  165. *out_map_p = out_map;
  166. *out_map_size = i;
  167. return 0;
  168. err:
  169. kfree(out_map);
  170. return -EINVAL;
  171. }
  172. /*
  173. * Get real, policy values from mapped values
  174. */
  175. static u16 unmap_class(u16 tclass)
  176. {
  177. if (tclass < current_mapping_size)
  178. return current_mapping[tclass].value;
  179. return tclass;
  180. }
  181. /*
  182. * Get kernel value for class from its policy value
  183. */
  184. static u16 map_class(u16 pol_value)
  185. {
  186. u16 i;
  187. for (i = 1; i < current_mapping_size; i++) {
  188. if (current_mapping[i].value == pol_value)
  189. return i;
  190. }
  191. return SECCLASS_NULL;
  192. }
  193. static void map_decision(u16 tclass, struct av_decision *avd,
  194. int allow_unknown)
  195. {
  196. if (tclass < current_mapping_size) {
  197. unsigned i, n = current_mapping[tclass].num_perms;
  198. u32 result;
  199. for (i = 0, result = 0; i < n; i++) {
  200. if (avd->allowed & current_mapping[tclass].perms[i])
  201. result |= 1<<i;
  202. if (allow_unknown && !current_mapping[tclass].perms[i])
  203. result |= 1<<i;
  204. }
  205. avd->allowed = result;
  206. for (i = 0, result = 0; i < n; i++)
  207. if (avd->auditallow & current_mapping[tclass].perms[i])
  208. result |= 1<<i;
  209. avd->auditallow = result;
  210. for (i = 0, result = 0; i < n; i++) {
  211. if (avd->auditdeny & current_mapping[tclass].perms[i])
  212. result |= 1<<i;
  213. if (!allow_unknown && !current_mapping[tclass].perms[i])
  214. result |= 1<<i;
  215. }
  216. /*
  217. * In case the kernel has a bug and requests a permission
  218. * between num_perms and the maximum permission number, we
  219. * should audit that denial
  220. */
  221. for (; i < (sizeof(u32)*8); i++)
  222. result |= 1<<i;
  223. avd->auditdeny = result;
  224. }
  225. }
  226. int security_mls_enabled(void)
  227. {
  228. return policydb.mls_enabled;
  229. }
  230. /*
  231. * Return the boolean value of a constraint expression
  232. * when it is applied to the specified source and target
  233. * security contexts.
  234. *
  235. * xcontext is a special beast... It is used by the validatetrans rules
  236. * only. For these rules, scontext is the context before the transition,
  237. * tcontext is the context after the transition, and xcontext is the context
  238. * of the process performing the transition. All other callers of
  239. * constraint_expr_eval should pass in NULL for xcontext.
  240. */
  241. static int constraint_expr_eval(struct context *scontext,
  242. struct context *tcontext,
  243. struct context *xcontext,
  244. struct constraint_expr *cexpr)
  245. {
  246. u32 val1, val2;
  247. struct context *c;
  248. struct role_datum *r1, *r2;
  249. struct mls_level *l1, *l2;
  250. struct constraint_expr *e;
  251. int s[CEXPR_MAXDEPTH];
  252. int sp = -1;
  253. for (e = cexpr; e; e = e->next) {
  254. switch (e->expr_type) {
  255. case CEXPR_NOT:
  256. BUG_ON(sp < 0);
  257. s[sp] = !s[sp];
  258. break;
  259. case CEXPR_AND:
  260. BUG_ON(sp < 1);
  261. sp--;
  262. s[sp] &= s[sp + 1];
  263. break;
  264. case CEXPR_OR:
  265. BUG_ON(sp < 1);
  266. sp--;
  267. s[sp] |= s[sp + 1];
  268. break;
  269. case CEXPR_ATTR:
  270. if (sp == (CEXPR_MAXDEPTH - 1))
  271. return 0;
  272. switch (e->attr) {
  273. case CEXPR_USER:
  274. val1 = scontext->user;
  275. val2 = tcontext->user;
  276. break;
  277. case CEXPR_TYPE:
  278. val1 = scontext->type;
  279. val2 = tcontext->type;
  280. break;
  281. case CEXPR_ROLE:
  282. val1 = scontext->role;
  283. val2 = tcontext->role;
  284. r1 = policydb.role_val_to_struct[val1 - 1];
  285. r2 = policydb.role_val_to_struct[val2 - 1];
  286. switch (e->op) {
  287. case CEXPR_DOM:
  288. s[++sp] = ebitmap_get_bit(&r1->dominates,
  289. val2 - 1);
  290. continue;
  291. case CEXPR_DOMBY:
  292. s[++sp] = ebitmap_get_bit(&r2->dominates,
  293. val1 - 1);
  294. continue;
  295. case CEXPR_INCOMP:
  296. s[++sp] = (!ebitmap_get_bit(&r1->dominates,
  297. val2 - 1) &&
  298. !ebitmap_get_bit(&r2->dominates,
  299. val1 - 1));
  300. continue;
  301. default:
  302. break;
  303. }
  304. break;
  305. case CEXPR_L1L2:
  306. l1 = &(scontext->range.level[0]);
  307. l2 = &(tcontext->range.level[0]);
  308. goto mls_ops;
  309. case CEXPR_L1H2:
  310. l1 = &(scontext->range.level[0]);
  311. l2 = &(tcontext->range.level[1]);
  312. goto mls_ops;
  313. case CEXPR_H1L2:
  314. l1 = &(scontext->range.level[1]);
  315. l2 = &(tcontext->range.level[0]);
  316. goto mls_ops;
  317. case CEXPR_H1H2:
  318. l1 = &(scontext->range.level[1]);
  319. l2 = &(tcontext->range.level[1]);
  320. goto mls_ops;
  321. case CEXPR_L1H1:
  322. l1 = &(scontext->range.level[0]);
  323. l2 = &(scontext->range.level[1]);
  324. goto mls_ops;
  325. case CEXPR_L2H2:
  326. l1 = &(tcontext->range.level[0]);
  327. l2 = &(tcontext->range.level[1]);
  328. goto mls_ops;
  329. mls_ops:
  330. switch (e->op) {
  331. case CEXPR_EQ:
  332. s[++sp] = mls_level_eq(l1, l2);
  333. continue;
  334. case CEXPR_NEQ:
  335. s[++sp] = !mls_level_eq(l1, l2);
  336. continue;
  337. case CEXPR_DOM:
  338. s[++sp] = mls_level_dom(l1, l2);
  339. continue;
  340. case CEXPR_DOMBY:
  341. s[++sp] = mls_level_dom(l2, l1);
  342. continue;
  343. case CEXPR_INCOMP:
  344. s[++sp] = mls_level_incomp(l2, l1);
  345. continue;
  346. default:
  347. BUG();
  348. return 0;
  349. }
  350. break;
  351. default:
  352. BUG();
  353. return 0;
  354. }
  355. switch (e->op) {
  356. case CEXPR_EQ:
  357. s[++sp] = (val1 == val2);
  358. break;
  359. case CEXPR_NEQ:
  360. s[++sp] = (val1 != val2);
  361. break;
  362. default:
  363. BUG();
  364. return 0;
  365. }
  366. break;
  367. case CEXPR_NAMES:
  368. if (sp == (CEXPR_MAXDEPTH-1))
  369. return 0;
  370. c = scontext;
  371. if (e->attr & CEXPR_TARGET)
  372. c = tcontext;
  373. else if (e->attr & CEXPR_XTARGET) {
  374. c = xcontext;
  375. if (!c) {
  376. BUG();
  377. return 0;
  378. }
  379. }
  380. if (e->attr & CEXPR_USER)
  381. val1 = c->user;
  382. else if (e->attr & CEXPR_ROLE)
  383. val1 = c->role;
  384. else if (e->attr & CEXPR_TYPE)
  385. val1 = c->type;
  386. else {
  387. BUG();
  388. return 0;
  389. }
  390. switch (e->op) {
  391. case CEXPR_EQ:
  392. s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
  393. break;
  394. case CEXPR_NEQ:
  395. s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
  396. break;
  397. default:
  398. BUG();
  399. return 0;
  400. }
  401. break;
  402. default:
  403. BUG();
  404. return 0;
  405. }
  406. }
  407. BUG_ON(sp != 0);
  408. return s[0];
  409. }
  410. /*
  411. * security_dump_masked_av - dumps masked permissions during
  412. * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
  413. */
  414. static int dump_masked_av_helper(void *k, void *d, void *args)
  415. {
  416. struct perm_datum *pdatum = d;
  417. char **permission_names = args;
  418. BUG_ON(pdatum->value < 1 || pdatum->value > 32);
  419. permission_names[pdatum->value - 1] = (char *)k;
  420. return 0;
  421. }
  422. static void security_dump_masked_av(struct context *scontext,
  423. struct context *tcontext,
  424. u16 tclass,
  425. u32 permissions,
  426. const char *reason)
  427. {
  428. struct common_datum *common_dat;
  429. struct class_datum *tclass_dat;
  430. struct audit_buffer *ab;
  431. char *tclass_name;
  432. char *scontext_name = NULL;
  433. char *tcontext_name = NULL;
  434. char *permission_names[32];
  435. int index;
  436. u32 length;
  437. bool need_comma = false;
  438. if (!permissions)
  439. return;
  440. tclass_name = sym_name(&policydb, SYM_CLASSES, tclass - 1);
  441. tclass_dat = policydb.class_val_to_struct[tclass - 1];
  442. common_dat = tclass_dat->comdatum;
  443. /* init permission_names */
  444. if (common_dat &&
  445. hashtab_map(common_dat->permissions.table,
  446. dump_masked_av_helper, permission_names) < 0)
  447. goto out;
  448. if (hashtab_map(tclass_dat->permissions.table,
  449. dump_masked_av_helper, permission_names) < 0)
  450. goto out;
  451. /* get scontext/tcontext in text form */
  452. if (context_struct_to_string(scontext,
  453. &scontext_name, &length) < 0)
  454. goto out;
  455. if (context_struct_to_string(tcontext,
  456. &tcontext_name, &length) < 0)
  457. goto out;
  458. /* audit a message */
  459. ab = audit_log_start(current->audit_context,
  460. GFP_ATOMIC, AUDIT_SELINUX_ERR);
  461. if (!ab)
  462. goto out;
  463. audit_log_format(ab, "op=security_compute_av reason=%s "
  464. "scontext=%s tcontext=%s tclass=%s perms=",
  465. reason, scontext_name, tcontext_name, tclass_name);
  466. for (index = 0; index < 32; index++) {
  467. u32 mask = (1 << index);
  468. if ((mask & permissions) == 0)
  469. continue;
  470. audit_log_format(ab, "%s%s",
  471. need_comma ? "," : "",
  472. permission_names[index]
  473. ? permission_names[index] : "????");
  474. need_comma = true;
  475. }
  476. audit_log_end(ab);
  477. out:
  478. /* release scontext/tcontext */
  479. kfree(tcontext_name);
  480. kfree(scontext_name);
  481. return;
  482. }
  483. /*
  484. * security_boundary_permission - drops violated permissions
  485. * on boundary constraint.
  486. */
  487. static void type_attribute_bounds_av(struct context *scontext,
  488. struct context *tcontext,
  489. u16 tclass,
  490. struct av_decision *avd)
  491. {
  492. struct context lo_scontext;
  493. struct context lo_tcontext;
  494. struct av_decision lo_avd;
  495. struct type_datum *source;
  496. struct type_datum *target;
  497. u32 masked = 0;
  498. source = flex_array_get_ptr(policydb.type_val_to_struct_array,
  499. scontext->type - 1);
  500. BUG_ON(!source);
  501. target = flex_array_get_ptr(policydb.type_val_to_struct_array,
  502. tcontext->type - 1);
  503. BUG_ON(!target);
  504. if (source->bounds) {
  505. memset(&lo_avd, 0, sizeof(lo_avd));
  506. memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
  507. lo_scontext.type = source->bounds;
  508. context_struct_compute_av(&lo_scontext,
  509. tcontext,
  510. tclass,
  511. &lo_avd,
  512. NULL);
  513. if ((lo_avd.allowed & avd->allowed) == avd->allowed)
  514. return; /* no masked permission */
  515. masked = ~lo_avd.allowed & avd->allowed;
  516. }
  517. if (target->bounds) {
  518. memset(&lo_avd, 0, sizeof(lo_avd));
  519. memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
  520. lo_tcontext.type = target->bounds;
  521. context_struct_compute_av(scontext,
  522. &lo_tcontext,
  523. tclass,
  524. &lo_avd,
  525. NULL);
  526. if ((lo_avd.allowed & avd->allowed) == avd->allowed)
  527. return; /* no masked permission */
  528. masked = ~lo_avd.allowed & avd->allowed;
  529. }
  530. if (source->bounds && target->bounds) {
  531. memset(&lo_avd, 0, sizeof(lo_avd));
  532. /*
  533. * lo_scontext and lo_tcontext are already
  534. * set up.
  535. */
  536. context_struct_compute_av(&lo_scontext,
  537. &lo_tcontext,
  538. tclass,
  539. &lo_avd,
  540. NULL);
  541. if ((lo_avd.allowed & avd->allowed) == avd->allowed)
  542. return; /* no masked permission */
  543. masked = ~lo_avd.allowed & avd->allowed;
  544. }
  545. if (masked) {
  546. /* mask violated permissions */
  547. avd->allowed &= ~masked;
  548. /* audit masked permissions */
  549. security_dump_masked_av(scontext, tcontext,
  550. tclass, masked, "bounds");
  551. }
  552. }
  553. /*
  554. * flag which drivers have permissions
  555. * only looking for ioctl based extended permssions
  556. */
  557. void services_compute_xperms_drivers(
  558. struct extended_perms *xperms,
  559. struct avtab_node *node)
  560. {
  561. unsigned int i;
  562. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
  563. /* if one or more driver has all permissions allowed */
  564. for (i = 0; i < ARRAY_SIZE(xperms->drivers.p); i++)
  565. xperms->drivers.p[i] |= node->datum.u.xperms->perms.p[i];
  566. } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
  567. /* if allowing permissions within a driver */
  568. security_xperm_set(xperms->drivers.p,
  569. node->datum.u.xperms->driver);
  570. }
  571. /* If no ioctl commands are allowed, ignore auditallow and auditdeny */
  572. if (node->key.specified & AVTAB_XPERMS_ALLOWED)
  573. xperms->len = 1;
  574. }
  575. /*
  576. * Compute access vectors and extended permissions based on a context
  577. * structure pair for the permissions in a particular class.
  578. */
  579. static void context_struct_compute_av(struct context *scontext,
  580. struct context *tcontext,
  581. u16 tclass,
  582. struct av_decision *avd,
  583. struct extended_perms *xperms)
  584. {
  585. struct constraint_node *constraint;
  586. struct role_allow *ra;
  587. struct avtab_key avkey;
  588. struct avtab_node *node;
  589. struct class_datum *tclass_datum;
  590. struct ebitmap *sattr, *tattr;
  591. struct ebitmap_node *snode, *tnode;
  592. unsigned int i, j;
  593. avd->allowed = 0;
  594. avd->auditallow = 0;
  595. avd->auditdeny = 0xffffffff;
  596. if (xperms) {
  597. memset(&xperms->drivers, 0, sizeof(xperms->drivers));
  598. xperms->len = 0;
  599. }
  600. if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
  601. if (printk_ratelimit())
  602. printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass);
  603. return;
  604. }
  605. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  606. /*
  607. * If a specific type enforcement rule was defined for
  608. * this permission check, then use it.
  609. */
  610. avkey.target_class = tclass;
  611. avkey.specified = AVTAB_AV | AVTAB_XPERMS;
  612. sattr = flex_array_get(policydb.type_attr_map_array, scontext->type - 1);
  613. BUG_ON(!sattr);
  614. tattr = flex_array_get(policydb.type_attr_map_array, tcontext->type - 1);
  615. BUG_ON(!tattr);
  616. ebitmap_for_each_positive_bit(sattr, snode, i) {
  617. ebitmap_for_each_positive_bit(tattr, tnode, j) {
  618. avkey.source_type = i + 1;
  619. avkey.target_type = j + 1;
  620. for (node = avtab_search_node(&policydb.te_avtab, &avkey);
  621. node;
  622. node = avtab_search_node_next(node, avkey.specified)) {
  623. if (node->key.specified == AVTAB_ALLOWED)
  624. avd->allowed |= node->datum.u.data;
  625. else if (node->key.specified == AVTAB_AUDITALLOW)
  626. avd->auditallow |= node->datum.u.data;
  627. else if (node->key.specified == AVTAB_AUDITDENY)
  628. avd->auditdeny &= node->datum.u.data;
  629. else if (xperms && (node->key.specified & AVTAB_XPERMS))
  630. services_compute_xperms_drivers(xperms, node);
  631. }
  632. /* Check conditional av table for additional permissions */
  633. cond_compute_av(&policydb.te_cond_avtab, &avkey,
  634. avd, xperms);
  635. }
  636. }
  637. /*
  638. * Remove any permissions prohibited by a constraint (this includes
  639. * the MLS policy).
  640. */
  641. constraint = tclass_datum->constraints;
  642. while (constraint) {
  643. if ((constraint->permissions & (avd->allowed)) &&
  644. !constraint_expr_eval(scontext, tcontext, NULL,
  645. constraint->expr)) {
  646. avd->allowed &= ~(constraint->permissions);
  647. }
  648. constraint = constraint->next;
  649. }
  650. /*
  651. * If checking process transition permission and the
  652. * role is changing, then check the (current_role, new_role)
  653. * pair.
  654. */
  655. if (tclass == policydb.process_class &&
  656. (avd->allowed & policydb.process_trans_perms) &&
  657. scontext->role != tcontext->role) {
  658. for (ra = policydb.role_allow; ra; ra = ra->next) {
  659. if (scontext->role == ra->role &&
  660. tcontext->role == ra->new_role)
  661. break;
  662. }
  663. if (!ra)
  664. avd->allowed &= ~policydb.process_trans_perms;
  665. }
  666. /*
  667. * If the given source and target types have boundary
  668. * constraint, lazy checks have to mask any violated
  669. * permission and notice it to userspace via audit.
  670. */
  671. type_attribute_bounds_av(scontext, tcontext,
  672. tclass, avd);
  673. }
  674. static int security_validtrans_handle_fail(struct context *ocontext,
  675. struct context *ncontext,
  676. struct context *tcontext,
  677. u16 tclass)
  678. {
  679. char *o = NULL, *n = NULL, *t = NULL;
  680. u32 olen, nlen, tlen;
  681. if (context_struct_to_string(ocontext, &o, &olen))
  682. goto out;
  683. if (context_struct_to_string(ncontext, &n, &nlen))
  684. goto out;
  685. if (context_struct_to_string(tcontext, &t, &tlen))
  686. goto out;
  687. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  688. "op=security_validate_transition seresult=denied"
  689. " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
  690. o, n, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
  691. out:
  692. kfree(o);
  693. kfree(n);
  694. kfree(t);
  695. if (!selinux_enforcing)
  696. return 0;
  697. return -EPERM;
  698. }
  699. int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
  700. u16 orig_tclass)
  701. {
  702. struct context *ocontext;
  703. struct context *ncontext;
  704. struct context *tcontext;
  705. struct class_datum *tclass_datum;
  706. struct constraint_node *constraint;
  707. u16 tclass;
  708. int rc = 0;
  709. if (!ss_initialized)
  710. return 0;
  711. read_lock(&policy_rwlock);
  712. tclass = unmap_class(orig_tclass);
  713. if (!tclass || tclass > policydb.p_classes.nprim) {
  714. printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
  715. __func__, tclass);
  716. rc = -EINVAL;
  717. goto out;
  718. }
  719. tclass_datum = policydb.class_val_to_struct[tclass - 1];
  720. ocontext = sidtab_search(&sidtab, oldsid);
  721. if (!ocontext) {
  722. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  723. __func__, oldsid);
  724. rc = -EINVAL;
  725. goto out;
  726. }
  727. ncontext = sidtab_search(&sidtab, newsid);
  728. if (!ncontext) {
  729. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  730. __func__, newsid);
  731. rc = -EINVAL;
  732. goto out;
  733. }
  734. tcontext = sidtab_search(&sidtab, tasksid);
  735. if (!tcontext) {
  736. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  737. __func__, tasksid);
  738. rc = -EINVAL;
  739. goto out;
  740. }
  741. constraint = tclass_datum->validatetrans;
  742. while (constraint) {
  743. if (!constraint_expr_eval(ocontext, ncontext, tcontext,
  744. constraint->expr)) {
  745. rc = security_validtrans_handle_fail(ocontext, ncontext,
  746. tcontext, tclass);
  747. goto out;
  748. }
  749. constraint = constraint->next;
  750. }
  751. out:
  752. read_unlock(&policy_rwlock);
  753. return rc;
  754. }
  755. /*
  756. * security_bounded_transition - check whether the given
  757. * transition is directed to bounded, or not.
  758. * It returns 0, if @newsid is bounded by @oldsid.
  759. * Otherwise, it returns error code.
  760. *
  761. * @oldsid : current security identifier
  762. * @newsid : destinated security identifier
  763. */
  764. int security_bounded_transition(u32 old_sid, u32 new_sid)
  765. {
  766. struct context *old_context, *new_context;
  767. struct type_datum *type;
  768. int index;
  769. int rc;
  770. if (!ss_initialized)
  771. return 0;
  772. read_lock(&policy_rwlock);
  773. rc = -EINVAL;
  774. old_context = sidtab_search(&sidtab, old_sid);
  775. if (!old_context) {
  776. printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
  777. __func__, old_sid);
  778. goto out;
  779. }
  780. rc = -EINVAL;
  781. new_context = sidtab_search(&sidtab, new_sid);
  782. if (!new_context) {
  783. printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
  784. __func__, new_sid);
  785. goto out;
  786. }
  787. rc = 0;
  788. /* type/domain unchanged */
  789. if (old_context->type == new_context->type)
  790. goto out;
  791. index = new_context->type;
  792. while (true) {
  793. type = flex_array_get_ptr(policydb.type_val_to_struct_array,
  794. index - 1);
  795. BUG_ON(!type);
  796. /* not bounded anymore */
  797. rc = -EPERM;
  798. if (!type->bounds)
  799. break;
  800. /* @newsid is bounded by @oldsid */
  801. rc = 0;
  802. if (type->bounds == old_context->type)
  803. break;
  804. index = type->bounds;
  805. }
  806. if (rc) {
  807. char *old_name = NULL;
  808. char *new_name = NULL;
  809. u32 length;
  810. if (!context_struct_to_string(old_context,
  811. &old_name, &length) &&
  812. !context_struct_to_string(new_context,
  813. &new_name, &length)) {
  814. audit_log(current->audit_context,
  815. GFP_ATOMIC, AUDIT_SELINUX_ERR,
  816. "op=security_bounded_transition "
  817. "seresult=denied "
  818. "oldcontext=%s newcontext=%s",
  819. old_name, new_name);
  820. }
  821. kfree(new_name);
  822. kfree(old_name);
  823. }
  824. out:
  825. read_unlock(&policy_rwlock);
  826. return rc;
  827. }
  828. static void avd_init(struct av_decision *avd)
  829. {
  830. avd->allowed = 0;
  831. avd->auditallow = 0;
  832. avd->auditdeny = 0xffffffff;
  833. avd->seqno = latest_granting;
  834. avd->flags = 0;
  835. }
  836. void services_compute_xperms_decision(struct extended_perms_decision *xpermd,
  837. struct avtab_node *node)
  838. {
  839. unsigned int i;
  840. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
  841. if (xpermd->driver != node->datum.u.xperms->driver)
  842. return;
  843. } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
  844. if (!security_xperm_test(node->datum.u.xperms->perms.p,
  845. xpermd->driver))
  846. return;
  847. } else {
  848. BUG();
  849. }
  850. if (node->key.specified == AVTAB_XPERMS_ALLOWED) {
  851. xpermd->used |= XPERMS_ALLOWED;
  852. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
  853. memset(xpermd->allowed->p, 0xff,
  854. sizeof(xpermd->allowed->p));
  855. }
  856. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
  857. for (i = 0; i < ARRAY_SIZE(xpermd->allowed->p); i++)
  858. xpermd->allowed->p[i] |=
  859. node->datum.u.xperms->perms.p[i];
  860. }
  861. } else if (node->key.specified == AVTAB_XPERMS_AUDITALLOW) {
  862. xpermd->used |= XPERMS_AUDITALLOW;
  863. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
  864. memset(xpermd->auditallow->p, 0xff,
  865. sizeof(xpermd->auditallow->p));
  866. }
  867. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
  868. for (i = 0; i < ARRAY_SIZE(xpermd->auditallow->p); i++)
  869. xpermd->auditallow->p[i] |=
  870. node->datum.u.xperms->perms.p[i];
  871. }
  872. } else if (node->key.specified == AVTAB_XPERMS_DONTAUDIT) {
  873. xpermd->used |= XPERMS_DONTAUDIT;
  874. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
  875. memset(xpermd->dontaudit->p, 0xff,
  876. sizeof(xpermd->dontaudit->p));
  877. }
  878. if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
  879. for (i = 0; i < ARRAY_SIZE(xpermd->dontaudit->p); i++)
  880. xpermd->dontaudit->p[i] |=
  881. node->datum.u.xperms->perms.p[i];
  882. }
  883. } else {
  884. BUG();
  885. }
  886. }
  887. void security_compute_xperms_decision(u32 ssid,
  888. u32 tsid,
  889. u16 orig_tclass,
  890. u8 driver,
  891. struct extended_perms_decision *xpermd)
  892. {
  893. u16 tclass;
  894. struct context *scontext, *tcontext;
  895. struct avtab_key avkey;
  896. struct avtab_node *node;
  897. struct ebitmap *sattr, *tattr;
  898. struct ebitmap_node *snode, *tnode;
  899. unsigned int i, j;
  900. xpermd->driver = driver;
  901. xpermd->used = 0;
  902. memset(xpermd->allowed->p, 0, sizeof(xpermd->allowed->p));
  903. memset(xpermd->auditallow->p, 0, sizeof(xpermd->auditallow->p));
  904. memset(xpermd->dontaudit->p, 0, sizeof(xpermd->dontaudit->p));
  905. read_lock(&policy_rwlock);
  906. if (!ss_initialized)
  907. goto allow;
  908. scontext = sidtab_search(&sidtab, ssid);
  909. if (!scontext) {
  910. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  911. __func__, ssid);
  912. goto out;
  913. }
  914. tcontext = sidtab_search(&sidtab, tsid);
  915. if (!tcontext) {
  916. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  917. __func__, tsid);
  918. goto out;
  919. }
  920. tclass = unmap_class(orig_tclass);
  921. if (unlikely(orig_tclass && !tclass)) {
  922. if (policydb.allow_unknown)
  923. goto allow;
  924. goto out;
  925. }
  926. if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
  927. pr_warn_ratelimited("SELinux: Invalid class %hu\n", tclass);
  928. goto out;
  929. }
  930. avkey.target_class = tclass;
  931. avkey.specified = AVTAB_XPERMS;
  932. sattr = flex_array_get(policydb.type_attr_map_array,
  933. scontext->type - 1);
  934. BUG_ON(!sattr);
  935. tattr = flex_array_get(policydb.type_attr_map_array,
  936. tcontext->type - 1);
  937. BUG_ON(!tattr);
  938. ebitmap_for_each_positive_bit(sattr, snode, i) {
  939. ebitmap_for_each_positive_bit(tattr, tnode, j) {
  940. avkey.source_type = i + 1;
  941. avkey.target_type = j + 1;
  942. for (node = avtab_search_node(&policydb.te_avtab, &avkey);
  943. node;
  944. node = avtab_search_node_next(node, avkey.specified))
  945. services_compute_xperms_decision(xpermd, node);
  946. cond_compute_xperms(&policydb.te_cond_avtab,
  947. &avkey, xpermd);
  948. }
  949. }
  950. out:
  951. read_unlock(&policy_rwlock);
  952. return;
  953. allow:
  954. memset(xpermd->allowed->p, 0xff, sizeof(xpermd->allowed->p));
  955. goto out;
  956. }
  957. /**
  958. * security_compute_av - Compute access vector decisions.
  959. * @ssid: source security identifier
  960. * @tsid: target security identifier
  961. * @tclass: target security class
  962. * @avd: access vector decisions
  963. * @xperms: extended permissions
  964. *
  965. * Compute a set of access vector decisions based on the
  966. * SID pair (@ssid, @tsid) for the permissions in @tclass.
  967. */
  968. void security_compute_av(u32 ssid,
  969. u32 tsid,
  970. u16 orig_tclass,
  971. struct av_decision *avd,
  972. struct extended_perms *xperms)
  973. {
  974. u16 tclass;
  975. struct context *scontext = NULL, *tcontext = NULL;
  976. read_lock(&policy_rwlock);
  977. avd_init(avd);
  978. xperms->len = 0;
  979. if (!ss_initialized)
  980. goto allow;
  981. scontext = sidtab_search(&sidtab, ssid);
  982. if (!scontext) {
  983. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  984. __func__, ssid);
  985. goto out;
  986. }
  987. /* permissive domain? */
  988. if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
  989. avd->flags |= AVD_FLAGS_PERMISSIVE;
  990. tcontext = sidtab_search(&sidtab, tsid);
  991. if (!tcontext) {
  992. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  993. __func__, tsid);
  994. goto out;
  995. }
  996. tclass = unmap_class(orig_tclass);
  997. if (unlikely(orig_tclass && !tclass)) {
  998. if (policydb.allow_unknown)
  999. goto allow;
  1000. goto out;
  1001. }
  1002. context_struct_compute_av(scontext, tcontext, tclass, avd, xperms);
  1003. map_decision(orig_tclass, avd, policydb.allow_unknown);
  1004. out:
  1005. read_unlock(&policy_rwlock);
  1006. return;
  1007. allow:
  1008. avd->allowed = 0xffffffff;
  1009. goto out;
  1010. }
  1011. void security_compute_av_user(u32 ssid,
  1012. u32 tsid,
  1013. u16 tclass,
  1014. struct av_decision *avd)
  1015. {
  1016. struct context *scontext = NULL, *tcontext = NULL;
  1017. read_lock(&policy_rwlock);
  1018. avd_init(avd);
  1019. if (!ss_initialized)
  1020. goto allow;
  1021. scontext = sidtab_search(&sidtab, ssid);
  1022. if (!scontext) {
  1023. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1024. __func__, ssid);
  1025. goto out;
  1026. }
  1027. /* permissive domain? */
  1028. if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
  1029. avd->flags |= AVD_FLAGS_PERMISSIVE;
  1030. tcontext = sidtab_search(&sidtab, tsid);
  1031. if (!tcontext) {
  1032. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1033. __func__, tsid);
  1034. goto out;
  1035. }
  1036. if (unlikely(!tclass)) {
  1037. if (policydb.allow_unknown)
  1038. goto allow;
  1039. goto out;
  1040. }
  1041. context_struct_compute_av(scontext, tcontext, tclass, avd, NULL);
  1042. out:
  1043. read_unlock(&policy_rwlock);
  1044. return;
  1045. allow:
  1046. avd->allowed = 0xffffffff;
  1047. goto out;
  1048. }
  1049. /*
  1050. * Write the security context string representation of
  1051. * the context structure `context' into a dynamically
  1052. * allocated string of the correct size. Set `*scontext'
  1053. * to point to this string and set `*scontext_len' to
  1054. * the length of the string.
  1055. */
  1056. static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
  1057. {
  1058. char *scontextp;
  1059. if (scontext)
  1060. *scontext = NULL;
  1061. *scontext_len = 0;
  1062. if (context->len) {
  1063. *scontext_len = context->len;
  1064. if (scontext) {
  1065. *scontext = kstrdup(context->str, GFP_ATOMIC);
  1066. if (!(*scontext))
  1067. return -ENOMEM;
  1068. }
  1069. return 0;
  1070. }
  1071. /* Compute the size of the context. */
  1072. *scontext_len += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) + 1;
  1073. *scontext_len += strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) + 1;
  1074. *scontext_len += strlen(sym_name(&policydb, SYM_TYPES, context->type - 1)) + 1;
  1075. *scontext_len += mls_compute_context_len(context);
  1076. if (!scontext)
  1077. return 0;
  1078. /* Allocate space for the context; caller must free this space. */
  1079. scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
  1080. if (!scontextp)
  1081. return -ENOMEM;
  1082. *scontext = scontextp;
  1083. /*
  1084. * Copy the user name, role name and type name into the context.
  1085. */
  1086. scontextp += sprintf(scontextp, "%s:%s:%s",
  1087. sym_name(&policydb, SYM_USERS, context->user - 1),
  1088. sym_name(&policydb, SYM_ROLES, context->role - 1),
  1089. sym_name(&policydb, SYM_TYPES, context->type - 1));
  1090. mls_sid_to_context(context, &scontextp);
  1091. *scontextp = 0;
  1092. return 0;
  1093. }
  1094. #include "initial_sid_to_string.h"
  1095. const char *security_get_initial_sid_context(u32 sid)
  1096. {
  1097. if (unlikely(sid > SECINITSID_NUM))
  1098. return NULL;
  1099. return initial_sid_to_string[sid];
  1100. }
  1101. static int security_sid_to_context_core(u32 sid, char **scontext,
  1102. u32 *scontext_len, int force)
  1103. {
  1104. struct context *context;
  1105. int rc = 0;
  1106. if (scontext)
  1107. *scontext = NULL;
  1108. *scontext_len = 0;
  1109. if (!ss_initialized) {
  1110. if (sid <= SECINITSID_NUM) {
  1111. char *scontextp;
  1112. *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
  1113. if (!scontext)
  1114. goto out;
  1115. scontextp = kmemdup(initial_sid_to_string[sid],
  1116. *scontext_len, GFP_ATOMIC);
  1117. if (!scontextp) {
  1118. rc = -ENOMEM;
  1119. goto out;
  1120. }
  1121. *scontext = scontextp;
  1122. goto out;
  1123. }
  1124. printk(KERN_ERR "SELinux: %s: called before initial "
  1125. "load_policy on unknown SID %d\n", __func__, sid);
  1126. rc = -EINVAL;
  1127. goto out;
  1128. }
  1129. read_lock(&policy_rwlock);
  1130. if (force)
  1131. context = sidtab_search_force(&sidtab, sid);
  1132. else
  1133. context = sidtab_search(&sidtab, sid);
  1134. if (!context) {
  1135. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1136. __func__, sid);
  1137. rc = -EINVAL;
  1138. goto out_unlock;
  1139. }
  1140. rc = context_struct_to_string(context, scontext, scontext_len);
  1141. out_unlock:
  1142. read_unlock(&policy_rwlock);
  1143. out:
  1144. return rc;
  1145. }
  1146. /**
  1147. * security_sid_to_context - Obtain a context for a given SID.
  1148. * @sid: security identifier, SID
  1149. * @scontext: security context
  1150. * @scontext_len: length in bytes
  1151. *
  1152. * Write the string representation of the context associated with @sid
  1153. * into a dynamically allocated string of the correct size. Set @scontext
  1154. * to point to this string and set @scontext_len to the length of the string.
  1155. */
  1156. int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
  1157. {
  1158. return security_sid_to_context_core(sid, scontext, scontext_len, 0);
  1159. }
  1160. int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
  1161. {
  1162. return security_sid_to_context_core(sid, scontext, scontext_len, 1);
  1163. }
  1164. /*
  1165. * Caveat: Mutates scontext.
  1166. */
  1167. static int string_to_context_struct(struct policydb *pol,
  1168. struct sidtab *sidtabp,
  1169. char *scontext,
  1170. u32 scontext_len,
  1171. struct context *ctx,
  1172. u32 def_sid)
  1173. {
  1174. struct role_datum *role;
  1175. struct type_datum *typdatum;
  1176. struct user_datum *usrdatum;
  1177. char *scontextp, *p, oldc;
  1178. int rc = 0;
  1179. context_init(ctx);
  1180. /* Parse the security context. */
  1181. rc = -EINVAL;
  1182. scontextp = (char *) scontext;
  1183. /* Extract the user. */
  1184. p = scontextp;
  1185. while (*p && *p != ':')
  1186. p++;
  1187. if (*p == 0)
  1188. goto out;
  1189. *p++ = 0;
  1190. usrdatum = hashtab_search(pol->p_users.table, scontextp);
  1191. if (!usrdatum)
  1192. goto out;
  1193. ctx->user = usrdatum->value;
  1194. /* Extract role. */
  1195. scontextp = p;
  1196. while (*p && *p != ':')
  1197. p++;
  1198. if (*p == 0)
  1199. goto out;
  1200. *p++ = 0;
  1201. role = hashtab_search(pol->p_roles.table, scontextp);
  1202. if (!role)
  1203. goto out;
  1204. ctx->role = role->value;
  1205. /* Extract type. */
  1206. scontextp = p;
  1207. while (*p && *p != ':')
  1208. p++;
  1209. oldc = *p;
  1210. *p++ = 0;
  1211. typdatum = hashtab_search(pol->p_types.table, scontextp);
  1212. if (!typdatum || typdatum->attribute)
  1213. goto out;
  1214. ctx->type = typdatum->value;
  1215. rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
  1216. if (rc)
  1217. goto out;
  1218. rc = -EINVAL;
  1219. if ((p - scontext) < scontext_len)
  1220. goto out;
  1221. /* Check the validity of the new context. */
  1222. if (!policydb_context_isvalid(pol, ctx))
  1223. goto out;
  1224. rc = 0;
  1225. out:
  1226. if (rc)
  1227. context_destroy(ctx);
  1228. return rc;
  1229. }
  1230. static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
  1231. u32 *sid, u32 def_sid, gfp_t gfp_flags,
  1232. int force)
  1233. {
  1234. char *scontext2, *str = NULL;
  1235. struct context context;
  1236. int rc = 0;
  1237. /* An empty security context is never valid. */
  1238. if (!scontext_len)
  1239. return -EINVAL;
  1240. /* Copy the string to allow changes and ensure a NUL terminator */
  1241. scontext2 = kmemdup_nul(scontext, scontext_len, gfp_flags);
  1242. if (!scontext2)
  1243. return -ENOMEM;
  1244. if (!ss_initialized) {
  1245. int i;
  1246. for (i = 1; i < SECINITSID_NUM; i++) {
  1247. if (!strcmp(initial_sid_to_string[i], scontext2)) {
  1248. *sid = i;
  1249. goto out;
  1250. }
  1251. }
  1252. *sid = SECINITSID_KERNEL;
  1253. goto out;
  1254. }
  1255. *sid = SECSID_NULL;
  1256. if (force) {
  1257. /* Save another copy for storing in uninterpreted form */
  1258. rc = -ENOMEM;
  1259. str = kstrdup(scontext2, gfp_flags);
  1260. if (!str)
  1261. goto out;
  1262. }
  1263. read_lock(&policy_rwlock);
  1264. rc = string_to_context_struct(&policydb, &sidtab, scontext2,
  1265. scontext_len, &context, def_sid);
  1266. if (rc == -EINVAL && force) {
  1267. context.str = str;
  1268. context.len = strlen(str) + 1;
  1269. str = NULL;
  1270. } else if (rc)
  1271. goto out_unlock;
  1272. rc = sidtab_context_to_sid(&sidtab, &context, sid);
  1273. context_destroy(&context);
  1274. out_unlock:
  1275. read_unlock(&policy_rwlock);
  1276. out:
  1277. kfree(scontext2);
  1278. kfree(str);
  1279. return rc;
  1280. }
  1281. /**
  1282. * security_context_to_sid - Obtain a SID for a given security context.
  1283. * @scontext: security context
  1284. * @scontext_len: length in bytes
  1285. * @sid: security identifier, SID
  1286. * @gfp: context for the allocation
  1287. *
  1288. * Obtains a SID associated with the security context that
  1289. * has the string representation specified by @scontext.
  1290. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  1291. * memory is available, or 0 on success.
  1292. */
  1293. int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid,
  1294. gfp_t gfp)
  1295. {
  1296. return security_context_to_sid_core(scontext, scontext_len,
  1297. sid, SECSID_NULL, gfp, 0);
  1298. }
  1299. int security_context_str_to_sid(const char *scontext, u32 *sid, gfp_t gfp)
  1300. {
  1301. return security_context_to_sid(scontext, strlen(scontext), sid, gfp);
  1302. }
  1303. /**
  1304. * security_context_to_sid_default - Obtain a SID for a given security context,
  1305. * falling back to specified default if needed.
  1306. *
  1307. * @scontext: security context
  1308. * @scontext_len: length in bytes
  1309. * @sid: security identifier, SID
  1310. * @def_sid: default SID to assign on error
  1311. *
  1312. * Obtains a SID associated with the security context that
  1313. * has the string representation specified by @scontext.
  1314. * The default SID is passed to the MLS layer to be used to allow
  1315. * kernel labeling of the MLS field if the MLS field is not present
  1316. * (for upgrading to MLS without full relabel).
  1317. * Implicitly forces adding of the context even if it cannot be mapped yet.
  1318. * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
  1319. * memory is available, or 0 on success.
  1320. */
  1321. int security_context_to_sid_default(const char *scontext, u32 scontext_len,
  1322. u32 *sid, u32 def_sid, gfp_t gfp_flags)
  1323. {
  1324. return security_context_to_sid_core(scontext, scontext_len,
  1325. sid, def_sid, gfp_flags, 1);
  1326. }
  1327. int security_context_to_sid_force(const char *scontext, u32 scontext_len,
  1328. u32 *sid)
  1329. {
  1330. return security_context_to_sid_core(scontext, scontext_len,
  1331. sid, SECSID_NULL, GFP_KERNEL, 1);
  1332. }
  1333. static int compute_sid_handle_invalid_context(
  1334. struct context *scontext,
  1335. struct context *tcontext,
  1336. u16 tclass,
  1337. struct context *newcontext)
  1338. {
  1339. char *s = NULL, *t = NULL, *n = NULL;
  1340. u32 slen, tlen, nlen;
  1341. if (context_struct_to_string(scontext, &s, &slen))
  1342. goto out;
  1343. if (context_struct_to_string(tcontext, &t, &tlen))
  1344. goto out;
  1345. if (context_struct_to_string(newcontext, &n, &nlen))
  1346. goto out;
  1347. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1348. "op=security_compute_sid invalid_context=%s"
  1349. " scontext=%s"
  1350. " tcontext=%s"
  1351. " tclass=%s",
  1352. n, s, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
  1353. out:
  1354. kfree(s);
  1355. kfree(t);
  1356. kfree(n);
  1357. if (!selinux_enforcing)
  1358. return 0;
  1359. return -EACCES;
  1360. }
  1361. static void filename_compute_type(struct policydb *p, struct context *newcontext,
  1362. u32 stype, u32 ttype, u16 tclass,
  1363. const char *objname)
  1364. {
  1365. struct filename_trans ft;
  1366. struct filename_trans_datum *otype;
  1367. /*
  1368. * Most filename trans rules are going to live in specific directories
  1369. * like /dev or /var/run. This bitmap will quickly skip rule searches
  1370. * if the ttype does not contain any rules.
  1371. */
  1372. if (!ebitmap_get_bit(&p->filename_trans_ttypes, ttype))
  1373. return;
  1374. ft.stype = stype;
  1375. ft.ttype = ttype;
  1376. ft.tclass = tclass;
  1377. ft.name = objname;
  1378. otype = hashtab_search(p->filename_trans, &ft);
  1379. if (otype)
  1380. newcontext->type = otype->otype;
  1381. }
  1382. static int security_compute_sid(u32 ssid,
  1383. u32 tsid,
  1384. u16 orig_tclass,
  1385. u32 specified,
  1386. const char *objname,
  1387. u32 *out_sid,
  1388. bool kern)
  1389. {
  1390. struct class_datum *cladatum = NULL;
  1391. struct context *scontext = NULL, *tcontext = NULL, newcontext;
  1392. struct role_trans *roletr = NULL;
  1393. struct avtab_key avkey;
  1394. struct avtab_datum *avdatum;
  1395. struct avtab_node *node;
  1396. u16 tclass;
  1397. int rc = 0;
  1398. bool sock;
  1399. if (!ss_initialized) {
  1400. switch (orig_tclass) {
  1401. case SECCLASS_PROCESS: /* kernel value */
  1402. *out_sid = ssid;
  1403. break;
  1404. default:
  1405. *out_sid = tsid;
  1406. break;
  1407. }
  1408. goto out;
  1409. }
  1410. context_init(&newcontext);
  1411. read_lock(&policy_rwlock);
  1412. if (kern) {
  1413. tclass = unmap_class(orig_tclass);
  1414. sock = security_is_socket_class(orig_tclass);
  1415. } else {
  1416. tclass = orig_tclass;
  1417. sock = security_is_socket_class(map_class(tclass));
  1418. }
  1419. scontext = sidtab_search(&sidtab, ssid);
  1420. if (!scontext) {
  1421. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1422. __func__, ssid);
  1423. rc = -EINVAL;
  1424. goto out_unlock;
  1425. }
  1426. tcontext = sidtab_search(&sidtab, tsid);
  1427. if (!tcontext) {
  1428. printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
  1429. __func__, tsid);
  1430. rc = -EINVAL;
  1431. goto out_unlock;
  1432. }
  1433. if (tclass && tclass <= policydb.p_classes.nprim)
  1434. cladatum = policydb.class_val_to_struct[tclass - 1];
  1435. /* Set the user identity. */
  1436. switch (specified) {
  1437. case AVTAB_TRANSITION:
  1438. case AVTAB_CHANGE:
  1439. if (cladatum && cladatum->default_user == DEFAULT_TARGET) {
  1440. newcontext.user = tcontext->user;
  1441. } else {
  1442. /* notice this gets both DEFAULT_SOURCE and unset */
  1443. /* Use the process user identity. */
  1444. newcontext.user = scontext->user;
  1445. }
  1446. break;
  1447. case AVTAB_MEMBER:
  1448. /* Use the related object owner. */
  1449. newcontext.user = tcontext->user;
  1450. break;
  1451. }
  1452. /* Set the role to default values. */
  1453. if (cladatum && cladatum->default_role == DEFAULT_SOURCE) {
  1454. newcontext.role = scontext->role;
  1455. } else if (cladatum && cladatum->default_role == DEFAULT_TARGET) {
  1456. newcontext.role = tcontext->role;
  1457. } else {
  1458. if ((tclass == policydb.process_class) || (sock == true))
  1459. newcontext.role = scontext->role;
  1460. else
  1461. newcontext.role = OBJECT_R_VAL;
  1462. }
  1463. /* Set the type to default values. */
  1464. if (cladatum && cladatum->default_type == DEFAULT_SOURCE) {
  1465. newcontext.type = scontext->type;
  1466. } else if (cladatum && cladatum->default_type == DEFAULT_TARGET) {
  1467. newcontext.type = tcontext->type;
  1468. } else {
  1469. if ((tclass == policydb.process_class) || (sock == true)) {
  1470. /* Use the type of process. */
  1471. newcontext.type = scontext->type;
  1472. } else {
  1473. /* Use the type of the related object. */
  1474. newcontext.type = tcontext->type;
  1475. }
  1476. }
  1477. /* Look for a type transition/member/change rule. */
  1478. avkey.source_type = scontext->type;
  1479. avkey.target_type = tcontext->type;
  1480. avkey.target_class = tclass;
  1481. avkey.specified = specified;
  1482. avdatum = avtab_search(&policydb.te_avtab, &avkey);
  1483. /* If no permanent rule, also check for enabled conditional rules */
  1484. if (!avdatum) {
  1485. node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
  1486. for (; node; node = avtab_search_node_next(node, specified)) {
  1487. if (node->key.specified & AVTAB_ENABLED) {
  1488. avdatum = &node->datum;
  1489. break;
  1490. }
  1491. }
  1492. }
  1493. if (avdatum) {
  1494. /* Use the type from the type transition/member/change rule. */
  1495. newcontext.type = avdatum->u.data;
  1496. }
  1497. /* if we have a objname this is a file trans check so check those rules */
  1498. if (objname)
  1499. filename_compute_type(&policydb, &newcontext, scontext->type,
  1500. tcontext->type, tclass, objname);
  1501. /* Check for class-specific changes. */
  1502. if (specified & AVTAB_TRANSITION) {
  1503. /* Look for a role transition rule. */
  1504. for (roletr = policydb.role_tr; roletr; roletr = roletr->next) {
  1505. if ((roletr->role == scontext->role) &&
  1506. (roletr->type == tcontext->type) &&
  1507. (roletr->tclass == tclass)) {
  1508. /* Use the role transition rule. */
  1509. newcontext.role = roletr->new_role;
  1510. break;
  1511. }
  1512. }
  1513. }
  1514. /* Set the MLS attributes.
  1515. This is done last because it may allocate memory. */
  1516. rc = mls_compute_sid(scontext, tcontext, tclass, specified,
  1517. &newcontext, sock);
  1518. if (rc)
  1519. goto out_unlock;
  1520. /* Check the validity of the context. */
  1521. if (!policydb_context_isvalid(&policydb, &newcontext)) {
  1522. rc = compute_sid_handle_invalid_context(scontext,
  1523. tcontext,
  1524. tclass,
  1525. &newcontext);
  1526. if (rc)
  1527. goto out_unlock;
  1528. }
  1529. /* Obtain the sid for the context. */
  1530. rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
  1531. out_unlock:
  1532. read_unlock(&policy_rwlock);
  1533. context_destroy(&newcontext);
  1534. out:
  1535. return rc;
  1536. }
  1537. /**
  1538. * security_transition_sid - Compute the SID for a new subject/object.
  1539. * @ssid: source security identifier
  1540. * @tsid: target security identifier
  1541. * @tclass: target security class
  1542. * @out_sid: security identifier for new subject/object
  1543. *
  1544. * Compute a SID to use for labeling a new subject or object in the
  1545. * class @tclass based on a SID pair (@ssid, @tsid).
  1546. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1547. * if insufficient memory is available, or %0 if the new SID was
  1548. * computed successfully.
  1549. */
  1550. int security_transition_sid(u32 ssid, u32 tsid, u16 tclass,
  1551. const struct qstr *qstr, u32 *out_sid)
  1552. {
  1553. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
  1554. qstr ? qstr->name : NULL, out_sid, true);
  1555. }
  1556. int security_transition_sid_user(u32 ssid, u32 tsid, u16 tclass,
  1557. const char *objname, u32 *out_sid)
  1558. {
  1559. return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
  1560. objname, out_sid, false);
  1561. }
  1562. /**
  1563. * security_member_sid - Compute the SID for member selection.
  1564. * @ssid: source security identifier
  1565. * @tsid: target security identifier
  1566. * @tclass: target security class
  1567. * @out_sid: security identifier for selected member
  1568. *
  1569. * Compute a SID to use when selecting a member of a polyinstantiated
  1570. * object of class @tclass based on a SID pair (@ssid, @tsid).
  1571. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1572. * if insufficient memory is available, or %0 if the SID was
  1573. * computed successfully.
  1574. */
  1575. int security_member_sid(u32 ssid,
  1576. u32 tsid,
  1577. u16 tclass,
  1578. u32 *out_sid)
  1579. {
  1580. return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, NULL,
  1581. out_sid, false);
  1582. }
  1583. /**
  1584. * security_change_sid - Compute the SID for object relabeling.
  1585. * @ssid: source security identifier
  1586. * @tsid: target security identifier
  1587. * @tclass: target security class
  1588. * @out_sid: security identifier for selected member
  1589. *
  1590. * Compute a SID to use for relabeling an object of class @tclass
  1591. * based on a SID pair (@ssid, @tsid).
  1592. * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
  1593. * if insufficient memory is available, or %0 if the SID was
  1594. * computed successfully.
  1595. */
  1596. int security_change_sid(u32 ssid,
  1597. u32 tsid,
  1598. u16 tclass,
  1599. u32 *out_sid)
  1600. {
  1601. return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, NULL,
  1602. out_sid, false);
  1603. }
  1604. /* Clone the SID into the new SID table. */
  1605. static int clone_sid(u32 sid,
  1606. struct context *context,
  1607. void *arg)
  1608. {
  1609. struct sidtab *s = arg;
  1610. if (sid > SECINITSID_NUM)
  1611. return sidtab_insert(s, sid, context);
  1612. else
  1613. return 0;
  1614. }
  1615. static inline int convert_context_handle_invalid_context(struct context *context)
  1616. {
  1617. char *s;
  1618. u32 len;
  1619. if (selinux_enforcing)
  1620. return -EINVAL;
  1621. if (!context_struct_to_string(context, &s, &len)) {
  1622. printk(KERN_WARNING "SELinux: Context %s would be invalid if enforcing\n", s);
  1623. kfree(s);
  1624. }
  1625. return 0;
  1626. }
  1627. struct convert_context_args {
  1628. struct policydb *oldp;
  1629. struct policydb *newp;
  1630. };
  1631. /*
  1632. * Convert the values in the security context
  1633. * structure `c' from the values specified
  1634. * in the policy `p->oldp' to the values specified
  1635. * in the policy `p->newp'. Verify that the
  1636. * context is valid under the new policy.
  1637. */
  1638. static int convert_context(u32 key,
  1639. struct context *c,
  1640. void *p)
  1641. {
  1642. struct convert_context_args *args;
  1643. struct context oldc;
  1644. struct ocontext *oc;
  1645. struct mls_range *range;
  1646. struct role_datum *role;
  1647. struct type_datum *typdatum;
  1648. struct user_datum *usrdatum;
  1649. char *s;
  1650. u32 len;
  1651. int rc = 0;
  1652. if (key <= SECINITSID_NUM)
  1653. goto out;
  1654. args = p;
  1655. if (c->str) {
  1656. struct context ctx;
  1657. rc = -ENOMEM;
  1658. s = kstrdup(c->str, GFP_KERNEL);
  1659. if (!s)
  1660. goto out;
  1661. rc = string_to_context_struct(args->newp, NULL, s,
  1662. c->len, &ctx, SECSID_NULL);
  1663. kfree(s);
  1664. if (!rc) {
  1665. printk(KERN_INFO "SELinux: Context %s became valid (mapped).\n",
  1666. c->str);
  1667. /* Replace string with mapped representation. */
  1668. kfree(c->str);
  1669. memcpy(c, &ctx, sizeof(*c));
  1670. goto out;
  1671. } else if (rc == -EINVAL) {
  1672. /* Retain string representation for later mapping. */
  1673. rc = 0;
  1674. goto out;
  1675. } else {
  1676. /* Other error condition, e.g. ENOMEM. */
  1677. printk(KERN_ERR "SELinux: Unable to map context %s, rc = %d.\n",
  1678. c->str, -rc);
  1679. goto out;
  1680. }
  1681. }
  1682. rc = context_cpy(&oldc, c);
  1683. if (rc)
  1684. goto out;
  1685. /* Convert the user. */
  1686. rc = -EINVAL;
  1687. usrdatum = hashtab_search(args->newp->p_users.table,
  1688. sym_name(args->oldp, SYM_USERS, c->user - 1));
  1689. if (!usrdatum)
  1690. goto bad;
  1691. c->user = usrdatum->value;
  1692. /* Convert the role. */
  1693. rc = -EINVAL;
  1694. role = hashtab_search(args->newp->p_roles.table,
  1695. sym_name(args->oldp, SYM_ROLES, c->role - 1));
  1696. if (!role)
  1697. goto bad;
  1698. c->role = role->value;
  1699. /* Convert the type. */
  1700. rc = -EINVAL;
  1701. typdatum = hashtab_search(args->newp->p_types.table,
  1702. sym_name(args->oldp, SYM_TYPES, c->type - 1));
  1703. if (!typdatum)
  1704. goto bad;
  1705. c->type = typdatum->value;
  1706. /* Convert the MLS fields if dealing with MLS policies */
  1707. if (args->oldp->mls_enabled && args->newp->mls_enabled) {
  1708. rc = mls_convert_context(args->oldp, args->newp, c);
  1709. if (rc)
  1710. goto bad;
  1711. } else if (args->oldp->mls_enabled && !args->newp->mls_enabled) {
  1712. /*
  1713. * Switching between MLS and non-MLS policy:
  1714. * free any storage used by the MLS fields in the
  1715. * context for all existing entries in the sidtab.
  1716. */
  1717. mls_context_destroy(c);
  1718. } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
  1719. /*
  1720. * Switching between non-MLS and MLS policy:
  1721. * ensure that the MLS fields of the context for all
  1722. * existing entries in the sidtab are filled in with a
  1723. * suitable default value, likely taken from one of the
  1724. * initial SIDs.
  1725. */
  1726. oc = args->newp->ocontexts[OCON_ISID];
  1727. while (oc && oc->sid[0] != SECINITSID_UNLABELED)
  1728. oc = oc->next;
  1729. rc = -EINVAL;
  1730. if (!oc) {
  1731. printk(KERN_ERR "SELinux: unable to look up"
  1732. " the initial SIDs list\n");
  1733. goto bad;
  1734. }
  1735. range = &oc->context[0].range;
  1736. rc = mls_range_set(c, range);
  1737. if (rc)
  1738. goto bad;
  1739. }
  1740. /* Check the validity of the new context. */
  1741. if (!policydb_context_isvalid(args->newp, c)) {
  1742. rc = convert_context_handle_invalid_context(&oldc);
  1743. if (rc)
  1744. goto bad;
  1745. }
  1746. context_destroy(&oldc);
  1747. rc = 0;
  1748. out:
  1749. return rc;
  1750. bad:
  1751. /* Map old representation to string and save it. */
  1752. rc = context_struct_to_string(&oldc, &s, &len);
  1753. if (rc)
  1754. return rc;
  1755. context_destroy(&oldc);
  1756. context_destroy(c);
  1757. c->str = s;
  1758. c->len = len;
  1759. printk(KERN_INFO "SELinux: Context %s became invalid (unmapped).\n",
  1760. c->str);
  1761. rc = 0;
  1762. goto out;
  1763. }
  1764. static void security_load_policycaps(void)
  1765. {
  1766. selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
  1767. POLICYDB_CAPABILITY_NETPEER);
  1768. selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
  1769. POLICYDB_CAPABILITY_OPENPERM);
  1770. selinux_policycap_alwaysnetwork = ebitmap_get_bit(&policydb.policycaps,
  1771. POLICYDB_CAPABILITY_ALWAYSNETWORK);
  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 bool)
  2358. {
  2359. int rc;
  2360. int len;
  2361. read_lock(&policy_rwlock);
  2362. rc = -EFAULT;
  2363. len = policydb.p_bools.nprim;
  2364. if (bool >= len)
  2365. goto out;
  2366. rc = policydb.bool_val_to_struct[bool]->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. }