services.c 72 KB

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