services.c 81 KB

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