services.c 81 KB

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