services.c 73 KB

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