services.c 62 KB

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