services.c 73 KB

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