services.c 70 KB

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