services.c 63 KB

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