services.c 72 KB

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