services.c 74 KB

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