services.c 63 KB

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