services.c 64 KB

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