services.c 89 KB

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