services.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685
  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. /*
  925. * Verify that each permission that is defined under the
  926. * existing policy is still defined with the same value
  927. * in the new policy.
  928. */
  929. static int validate_perm(void *key, void *datum, void *p)
  930. {
  931. struct hashtab *h;
  932. struct perm_datum *perdatum, *perdatum2;
  933. int rc = 0;
  934. h = p;
  935. perdatum = datum;
  936. perdatum2 = hashtab_search(h, key);
  937. if (!perdatum2) {
  938. printk(KERN_ERR "security: permission %s disappeared",
  939. (char *)key);
  940. rc = -ENOENT;
  941. goto out;
  942. }
  943. if (perdatum->value != perdatum2->value) {
  944. printk(KERN_ERR "security: the value of permission %s changed",
  945. (char *)key);
  946. rc = -EINVAL;
  947. }
  948. out:
  949. return rc;
  950. }
  951. /*
  952. * Verify that each class that is defined under the
  953. * existing policy is still defined with the same
  954. * attributes in the new policy.
  955. */
  956. static int validate_class(void *key, void *datum, void *p)
  957. {
  958. struct policydb *newp;
  959. struct class_datum *cladatum, *cladatum2;
  960. int rc;
  961. newp = p;
  962. cladatum = datum;
  963. cladatum2 = hashtab_search(newp->p_classes.table, key);
  964. if (!cladatum2) {
  965. printk(KERN_ERR "security: class %s disappeared\n",
  966. (char *)key);
  967. rc = -ENOENT;
  968. goto out;
  969. }
  970. if (cladatum->value != cladatum2->value) {
  971. printk(KERN_ERR "security: the value of class %s changed\n",
  972. (char *)key);
  973. rc = -EINVAL;
  974. goto out;
  975. }
  976. if ((cladatum->comdatum && !cladatum2->comdatum) ||
  977. (!cladatum->comdatum && cladatum2->comdatum)) {
  978. printk(KERN_ERR "security: the inherits clause for the access "
  979. "vector definition for class %s changed\n", (char *)key);
  980. rc = -EINVAL;
  981. goto out;
  982. }
  983. if (cladatum->comdatum) {
  984. rc = hashtab_map(cladatum->comdatum->permissions.table, validate_perm,
  985. cladatum2->comdatum->permissions.table);
  986. if (rc) {
  987. printk(" in the access vector definition for class "
  988. "%s\n", (char *)key);
  989. goto out;
  990. }
  991. }
  992. rc = hashtab_map(cladatum->permissions.table, validate_perm,
  993. cladatum2->permissions.table);
  994. if (rc)
  995. printk(" in access vector definition for class %s\n",
  996. (char *)key);
  997. out:
  998. return rc;
  999. }
  1000. /* Clone the SID into the new SID table. */
  1001. static int clone_sid(u32 sid,
  1002. struct context *context,
  1003. void *arg)
  1004. {
  1005. struct sidtab *s = arg;
  1006. return sidtab_insert(s, sid, context);
  1007. }
  1008. static inline int convert_context_handle_invalid_context(struct context *context)
  1009. {
  1010. int rc = 0;
  1011. if (selinux_enforcing) {
  1012. rc = -EINVAL;
  1013. } else {
  1014. char *s;
  1015. u32 len;
  1016. context_struct_to_string(context, &s, &len);
  1017. printk(KERN_ERR "security: context %s is invalid\n", s);
  1018. kfree(s);
  1019. }
  1020. return rc;
  1021. }
  1022. struct convert_context_args {
  1023. struct policydb *oldp;
  1024. struct policydb *newp;
  1025. };
  1026. /*
  1027. * Convert the values in the security context
  1028. * structure `c' from the values specified
  1029. * in the policy `p->oldp' to the values specified
  1030. * in the policy `p->newp'. Verify that the
  1031. * context is valid under the new policy.
  1032. */
  1033. static int convert_context(u32 key,
  1034. struct context *c,
  1035. void *p)
  1036. {
  1037. struct convert_context_args *args;
  1038. struct context oldc;
  1039. struct role_datum *role;
  1040. struct type_datum *typdatum;
  1041. struct user_datum *usrdatum;
  1042. char *s;
  1043. u32 len;
  1044. int rc;
  1045. args = p;
  1046. rc = context_cpy(&oldc, c);
  1047. if (rc)
  1048. goto out;
  1049. rc = -EINVAL;
  1050. /* Convert the user. */
  1051. usrdatum = hashtab_search(args->newp->p_users.table,
  1052. args->oldp->p_user_val_to_name[c->user - 1]);
  1053. if (!usrdatum) {
  1054. goto bad;
  1055. }
  1056. c->user = usrdatum->value;
  1057. /* Convert the role. */
  1058. role = hashtab_search(args->newp->p_roles.table,
  1059. args->oldp->p_role_val_to_name[c->role - 1]);
  1060. if (!role) {
  1061. goto bad;
  1062. }
  1063. c->role = role->value;
  1064. /* Convert the type. */
  1065. typdatum = hashtab_search(args->newp->p_types.table,
  1066. args->oldp->p_type_val_to_name[c->type - 1]);
  1067. if (!typdatum) {
  1068. goto bad;
  1069. }
  1070. c->type = typdatum->value;
  1071. rc = mls_convert_context(args->oldp, args->newp, c);
  1072. if (rc)
  1073. goto bad;
  1074. /* Check the validity of the new context. */
  1075. if (!policydb_context_isvalid(args->newp, c)) {
  1076. rc = convert_context_handle_invalid_context(&oldc);
  1077. if (rc)
  1078. goto bad;
  1079. }
  1080. context_destroy(&oldc);
  1081. out:
  1082. return rc;
  1083. bad:
  1084. context_struct_to_string(&oldc, &s, &len);
  1085. context_destroy(&oldc);
  1086. printk(KERN_ERR "security: invalidating context %s\n", s);
  1087. kfree(s);
  1088. goto out;
  1089. }
  1090. extern void selinux_complete_init(void);
  1091. /**
  1092. * security_load_policy - Load a security policy configuration.
  1093. * @data: binary policy data
  1094. * @len: length of data in bytes
  1095. *
  1096. * Load a new set of security policy configuration data,
  1097. * validate it and convert the SID table as necessary.
  1098. * This function will flush the access vector cache after
  1099. * loading the new policy.
  1100. */
  1101. int security_load_policy(void *data, size_t len)
  1102. {
  1103. struct policydb oldpolicydb, newpolicydb;
  1104. struct sidtab oldsidtab, newsidtab;
  1105. struct convert_context_args args;
  1106. u32 seqno;
  1107. int rc = 0;
  1108. struct policy_file file = { data, len }, *fp = &file;
  1109. LOAD_LOCK;
  1110. if (!ss_initialized) {
  1111. avtab_cache_init();
  1112. if (policydb_read(&policydb, fp)) {
  1113. LOAD_UNLOCK;
  1114. avtab_cache_destroy();
  1115. return -EINVAL;
  1116. }
  1117. if (policydb_load_isids(&policydb, &sidtab)) {
  1118. LOAD_UNLOCK;
  1119. policydb_destroy(&policydb);
  1120. avtab_cache_destroy();
  1121. return -EINVAL;
  1122. }
  1123. policydb_loaded_version = policydb.policyvers;
  1124. ss_initialized = 1;
  1125. seqno = ++latest_granting;
  1126. LOAD_UNLOCK;
  1127. selinux_complete_init();
  1128. avc_ss_reset(seqno);
  1129. selnl_notify_policyload(seqno);
  1130. selinux_netlbl_cache_invalidate();
  1131. return 0;
  1132. }
  1133. #if 0
  1134. sidtab_hash_eval(&sidtab, "sids");
  1135. #endif
  1136. if (policydb_read(&newpolicydb, fp)) {
  1137. LOAD_UNLOCK;
  1138. return -EINVAL;
  1139. }
  1140. sidtab_init(&newsidtab);
  1141. /* Verify that the existing classes did not change. */
  1142. if (hashtab_map(policydb.p_classes.table, validate_class, &newpolicydb)) {
  1143. printk(KERN_ERR "security: the definition of an existing "
  1144. "class changed\n");
  1145. rc = -EINVAL;
  1146. goto err;
  1147. }
  1148. /* Clone the SID table. */
  1149. sidtab_shutdown(&sidtab);
  1150. if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
  1151. rc = -ENOMEM;
  1152. goto err;
  1153. }
  1154. /* Convert the internal representations of contexts
  1155. in the new SID table and remove invalid SIDs. */
  1156. args.oldp = &policydb;
  1157. args.newp = &newpolicydb;
  1158. sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
  1159. /* Save the old policydb and SID table to free later. */
  1160. memcpy(&oldpolicydb, &policydb, sizeof policydb);
  1161. sidtab_set(&oldsidtab, &sidtab);
  1162. /* Install the new policydb and SID table. */
  1163. POLICY_WRLOCK;
  1164. memcpy(&policydb, &newpolicydb, sizeof policydb);
  1165. sidtab_set(&sidtab, &newsidtab);
  1166. seqno = ++latest_granting;
  1167. policydb_loaded_version = policydb.policyvers;
  1168. POLICY_WRUNLOCK;
  1169. LOAD_UNLOCK;
  1170. /* Free the old policydb and SID table. */
  1171. policydb_destroy(&oldpolicydb);
  1172. sidtab_destroy(&oldsidtab);
  1173. avc_ss_reset(seqno);
  1174. selnl_notify_policyload(seqno);
  1175. selinux_netlbl_cache_invalidate();
  1176. return 0;
  1177. err:
  1178. LOAD_UNLOCK;
  1179. sidtab_destroy(&newsidtab);
  1180. policydb_destroy(&newpolicydb);
  1181. return rc;
  1182. }
  1183. /**
  1184. * security_port_sid - Obtain the SID for a port.
  1185. * @domain: communication domain aka address family
  1186. * @type: socket type
  1187. * @protocol: protocol number
  1188. * @port: port number
  1189. * @out_sid: security identifier
  1190. */
  1191. int security_port_sid(u16 domain,
  1192. u16 type,
  1193. u8 protocol,
  1194. u16 port,
  1195. u32 *out_sid)
  1196. {
  1197. struct ocontext *c;
  1198. int rc = 0;
  1199. POLICY_RDLOCK;
  1200. c = policydb.ocontexts[OCON_PORT];
  1201. while (c) {
  1202. if (c->u.port.protocol == protocol &&
  1203. c->u.port.low_port <= port &&
  1204. c->u.port.high_port >= port)
  1205. break;
  1206. c = c->next;
  1207. }
  1208. if (c) {
  1209. if (!c->sid[0]) {
  1210. rc = sidtab_context_to_sid(&sidtab,
  1211. &c->context[0],
  1212. &c->sid[0]);
  1213. if (rc)
  1214. goto out;
  1215. }
  1216. *out_sid = c->sid[0];
  1217. } else {
  1218. *out_sid = SECINITSID_PORT;
  1219. }
  1220. out:
  1221. POLICY_RDUNLOCK;
  1222. return rc;
  1223. }
  1224. /**
  1225. * security_netif_sid - Obtain the SID for a network interface.
  1226. * @name: interface name
  1227. * @if_sid: interface SID
  1228. * @msg_sid: default SID for received packets
  1229. */
  1230. int security_netif_sid(char *name,
  1231. u32 *if_sid,
  1232. u32 *msg_sid)
  1233. {
  1234. int rc = 0;
  1235. struct ocontext *c;
  1236. POLICY_RDLOCK;
  1237. c = policydb.ocontexts[OCON_NETIF];
  1238. while (c) {
  1239. if (strcmp(name, c->u.name) == 0)
  1240. break;
  1241. c = c->next;
  1242. }
  1243. if (c) {
  1244. if (!c->sid[0] || !c->sid[1]) {
  1245. rc = sidtab_context_to_sid(&sidtab,
  1246. &c->context[0],
  1247. &c->sid[0]);
  1248. if (rc)
  1249. goto out;
  1250. rc = sidtab_context_to_sid(&sidtab,
  1251. &c->context[1],
  1252. &c->sid[1]);
  1253. if (rc)
  1254. goto out;
  1255. }
  1256. *if_sid = c->sid[0];
  1257. *msg_sid = c->sid[1];
  1258. } else {
  1259. *if_sid = SECINITSID_NETIF;
  1260. *msg_sid = SECINITSID_NETMSG;
  1261. }
  1262. out:
  1263. POLICY_RDUNLOCK;
  1264. return rc;
  1265. }
  1266. static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
  1267. {
  1268. int i, fail = 0;
  1269. for(i = 0; i < 4; i++)
  1270. if(addr[i] != (input[i] & mask[i])) {
  1271. fail = 1;
  1272. break;
  1273. }
  1274. return !fail;
  1275. }
  1276. /**
  1277. * security_node_sid - Obtain the SID for a node (host).
  1278. * @domain: communication domain aka address family
  1279. * @addrp: address
  1280. * @addrlen: address length in bytes
  1281. * @out_sid: security identifier
  1282. */
  1283. int security_node_sid(u16 domain,
  1284. void *addrp,
  1285. u32 addrlen,
  1286. u32 *out_sid)
  1287. {
  1288. int rc = 0;
  1289. struct ocontext *c;
  1290. POLICY_RDLOCK;
  1291. switch (domain) {
  1292. case AF_INET: {
  1293. u32 addr;
  1294. if (addrlen != sizeof(u32)) {
  1295. rc = -EINVAL;
  1296. goto out;
  1297. }
  1298. addr = *((u32 *)addrp);
  1299. c = policydb.ocontexts[OCON_NODE];
  1300. while (c) {
  1301. if (c->u.node.addr == (addr & c->u.node.mask))
  1302. break;
  1303. c = c->next;
  1304. }
  1305. break;
  1306. }
  1307. case AF_INET6:
  1308. if (addrlen != sizeof(u64) * 2) {
  1309. rc = -EINVAL;
  1310. goto out;
  1311. }
  1312. c = policydb.ocontexts[OCON_NODE6];
  1313. while (c) {
  1314. if (match_ipv6_addrmask(addrp, c->u.node6.addr,
  1315. c->u.node6.mask))
  1316. break;
  1317. c = c->next;
  1318. }
  1319. break;
  1320. default:
  1321. *out_sid = SECINITSID_NODE;
  1322. goto out;
  1323. }
  1324. if (c) {
  1325. if (!c->sid[0]) {
  1326. rc = sidtab_context_to_sid(&sidtab,
  1327. &c->context[0],
  1328. &c->sid[0]);
  1329. if (rc)
  1330. goto out;
  1331. }
  1332. *out_sid = c->sid[0];
  1333. } else {
  1334. *out_sid = SECINITSID_NODE;
  1335. }
  1336. out:
  1337. POLICY_RDUNLOCK;
  1338. return rc;
  1339. }
  1340. #define SIDS_NEL 25
  1341. /**
  1342. * security_get_user_sids - Obtain reachable SIDs for a user.
  1343. * @fromsid: starting SID
  1344. * @username: username
  1345. * @sids: array of reachable SIDs for user
  1346. * @nel: number of elements in @sids
  1347. *
  1348. * Generate the set of SIDs for legal security contexts
  1349. * for a given user that can be reached by @fromsid.
  1350. * Set *@sids to point to a dynamically allocated
  1351. * array containing the set of SIDs. Set *@nel to the
  1352. * number of elements in the array.
  1353. */
  1354. int security_get_user_sids(u32 fromsid,
  1355. char *username,
  1356. u32 **sids,
  1357. u32 *nel)
  1358. {
  1359. struct context *fromcon, usercon;
  1360. u32 *mysids, *mysids2, sid;
  1361. u32 mynel = 0, maxnel = SIDS_NEL;
  1362. struct user_datum *user;
  1363. struct role_datum *role;
  1364. struct av_decision avd;
  1365. struct ebitmap_node *rnode, *tnode;
  1366. int rc = 0, i, j;
  1367. if (!ss_initialized) {
  1368. *sids = NULL;
  1369. *nel = 0;
  1370. goto out;
  1371. }
  1372. POLICY_RDLOCK;
  1373. fromcon = sidtab_search(&sidtab, fromsid);
  1374. if (!fromcon) {
  1375. rc = -EINVAL;
  1376. goto out_unlock;
  1377. }
  1378. user = hashtab_search(policydb.p_users.table, username);
  1379. if (!user) {
  1380. rc = -EINVAL;
  1381. goto out_unlock;
  1382. }
  1383. usercon.user = user->value;
  1384. mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
  1385. if (!mysids) {
  1386. rc = -ENOMEM;
  1387. goto out_unlock;
  1388. }
  1389. ebitmap_for_each_bit(&user->roles, rnode, i) {
  1390. if (!ebitmap_node_get_bit(rnode, i))
  1391. continue;
  1392. role = policydb.role_val_to_struct[i];
  1393. usercon.role = i+1;
  1394. ebitmap_for_each_bit(&role->types, tnode, j) {
  1395. if (!ebitmap_node_get_bit(tnode, j))
  1396. continue;
  1397. usercon.type = j+1;
  1398. if (mls_setup_user_range(fromcon, user, &usercon))
  1399. continue;
  1400. rc = context_struct_compute_av(fromcon, &usercon,
  1401. SECCLASS_PROCESS,
  1402. PROCESS__TRANSITION,
  1403. &avd);
  1404. if (rc || !(avd.allowed & PROCESS__TRANSITION))
  1405. continue;
  1406. rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
  1407. if (rc) {
  1408. kfree(mysids);
  1409. goto out_unlock;
  1410. }
  1411. if (mynel < maxnel) {
  1412. mysids[mynel++] = sid;
  1413. } else {
  1414. maxnel += SIDS_NEL;
  1415. mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
  1416. if (!mysids2) {
  1417. rc = -ENOMEM;
  1418. kfree(mysids);
  1419. goto out_unlock;
  1420. }
  1421. memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
  1422. kfree(mysids);
  1423. mysids = mysids2;
  1424. mysids[mynel++] = sid;
  1425. }
  1426. }
  1427. }
  1428. *sids = mysids;
  1429. *nel = mynel;
  1430. out_unlock:
  1431. POLICY_RDUNLOCK;
  1432. out:
  1433. return rc;
  1434. }
  1435. /**
  1436. * security_genfs_sid - Obtain a SID for a file in a filesystem
  1437. * @fstype: filesystem type
  1438. * @path: path from root of mount
  1439. * @sclass: file security class
  1440. * @sid: SID for path
  1441. *
  1442. * Obtain a SID to use for a file in a filesystem that
  1443. * cannot support xattr or use a fixed labeling behavior like
  1444. * transition SIDs or task SIDs.
  1445. */
  1446. int security_genfs_sid(const char *fstype,
  1447. char *path,
  1448. u16 sclass,
  1449. u32 *sid)
  1450. {
  1451. int len;
  1452. struct genfs *genfs;
  1453. struct ocontext *c;
  1454. int rc = 0, cmp = 0;
  1455. POLICY_RDLOCK;
  1456. for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
  1457. cmp = strcmp(fstype, genfs->fstype);
  1458. if (cmp <= 0)
  1459. break;
  1460. }
  1461. if (!genfs || cmp) {
  1462. *sid = SECINITSID_UNLABELED;
  1463. rc = -ENOENT;
  1464. goto out;
  1465. }
  1466. for (c = genfs->head; c; c = c->next) {
  1467. len = strlen(c->u.name);
  1468. if ((!c->v.sclass || sclass == c->v.sclass) &&
  1469. (strncmp(c->u.name, path, len) == 0))
  1470. break;
  1471. }
  1472. if (!c) {
  1473. *sid = SECINITSID_UNLABELED;
  1474. rc = -ENOENT;
  1475. goto out;
  1476. }
  1477. if (!c->sid[0]) {
  1478. rc = sidtab_context_to_sid(&sidtab,
  1479. &c->context[0],
  1480. &c->sid[0]);
  1481. if (rc)
  1482. goto out;
  1483. }
  1484. *sid = c->sid[0];
  1485. out:
  1486. POLICY_RDUNLOCK;
  1487. return rc;
  1488. }
  1489. /**
  1490. * security_fs_use - Determine how to handle labeling for a filesystem.
  1491. * @fstype: filesystem type
  1492. * @behavior: labeling behavior
  1493. * @sid: SID for filesystem (superblock)
  1494. */
  1495. int security_fs_use(
  1496. const char *fstype,
  1497. unsigned int *behavior,
  1498. u32 *sid)
  1499. {
  1500. int rc = 0;
  1501. struct ocontext *c;
  1502. POLICY_RDLOCK;
  1503. c = policydb.ocontexts[OCON_FSUSE];
  1504. while (c) {
  1505. if (strcmp(fstype, c->u.name) == 0)
  1506. break;
  1507. c = c->next;
  1508. }
  1509. if (c) {
  1510. *behavior = c->v.behavior;
  1511. if (!c->sid[0]) {
  1512. rc = sidtab_context_to_sid(&sidtab,
  1513. &c->context[0],
  1514. &c->sid[0]);
  1515. if (rc)
  1516. goto out;
  1517. }
  1518. *sid = c->sid[0];
  1519. } else {
  1520. rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
  1521. if (rc) {
  1522. *behavior = SECURITY_FS_USE_NONE;
  1523. rc = 0;
  1524. } else {
  1525. *behavior = SECURITY_FS_USE_GENFS;
  1526. }
  1527. }
  1528. out:
  1529. POLICY_RDUNLOCK;
  1530. return rc;
  1531. }
  1532. int security_get_bools(int *len, char ***names, int **values)
  1533. {
  1534. int i, rc = -ENOMEM;
  1535. POLICY_RDLOCK;
  1536. *names = NULL;
  1537. *values = NULL;
  1538. *len = policydb.p_bools.nprim;
  1539. if (!*len) {
  1540. rc = 0;
  1541. goto out;
  1542. }
  1543. *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
  1544. if (!*names)
  1545. goto err;
  1546. *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
  1547. if (!*values)
  1548. goto err;
  1549. for (i = 0; i < *len; i++) {
  1550. size_t name_len;
  1551. (*values)[i] = policydb.bool_val_to_struct[i]->state;
  1552. name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
  1553. (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
  1554. if (!(*names)[i])
  1555. goto err;
  1556. strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
  1557. (*names)[i][name_len - 1] = 0;
  1558. }
  1559. rc = 0;
  1560. out:
  1561. POLICY_RDUNLOCK;
  1562. return rc;
  1563. err:
  1564. if (*names) {
  1565. for (i = 0; i < *len; i++)
  1566. kfree((*names)[i]);
  1567. }
  1568. kfree(*values);
  1569. goto out;
  1570. }
  1571. int security_set_bools(int len, int *values)
  1572. {
  1573. int i, rc = 0;
  1574. int lenp, seqno = 0;
  1575. struct cond_node *cur;
  1576. POLICY_WRLOCK;
  1577. lenp = policydb.p_bools.nprim;
  1578. if (len != lenp) {
  1579. rc = -EFAULT;
  1580. goto out;
  1581. }
  1582. for (i = 0; i < len; i++) {
  1583. if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
  1584. audit_log(current->audit_context, GFP_ATOMIC,
  1585. AUDIT_MAC_CONFIG_CHANGE,
  1586. "bool=%s val=%d old_val=%d auid=%u",
  1587. policydb.p_bool_val_to_name[i],
  1588. !!values[i],
  1589. policydb.bool_val_to_struct[i]->state,
  1590. audit_get_loginuid(current->audit_context));
  1591. }
  1592. if (values[i]) {
  1593. policydb.bool_val_to_struct[i]->state = 1;
  1594. } else {
  1595. policydb.bool_val_to_struct[i]->state = 0;
  1596. }
  1597. }
  1598. for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
  1599. rc = evaluate_cond_node(&policydb, cur);
  1600. if (rc)
  1601. goto out;
  1602. }
  1603. seqno = ++latest_granting;
  1604. out:
  1605. POLICY_WRUNLOCK;
  1606. if (!rc) {
  1607. avc_ss_reset(seqno);
  1608. selnl_notify_policyload(seqno);
  1609. }
  1610. return rc;
  1611. }
  1612. int security_get_bool_value(int bool)
  1613. {
  1614. int rc = 0;
  1615. int len;
  1616. POLICY_RDLOCK;
  1617. len = policydb.p_bools.nprim;
  1618. if (bool >= len) {
  1619. rc = -EFAULT;
  1620. goto out;
  1621. }
  1622. rc = policydb.bool_val_to_struct[bool]->state;
  1623. out:
  1624. POLICY_RDUNLOCK;
  1625. return rc;
  1626. }
  1627. /*
  1628. * security_sid_mls_copy() - computes a new sid based on the given
  1629. * sid and the mls portion of mls_sid.
  1630. */
  1631. int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
  1632. {
  1633. struct context *context1;
  1634. struct context *context2;
  1635. struct context newcon;
  1636. char *s;
  1637. u32 len;
  1638. int rc = 0;
  1639. if (!ss_initialized || !selinux_mls_enabled) {
  1640. *new_sid = sid;
  1641. goto out;
  1642. }
  1643. context_init(&newcon);
  1644. POLICY_RDLOCK;
  1645. context1 = sidtab_search(&sidtab, sid);
  1646. if (!context1) {
  1647. printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
  1648. "%d\n", sid);
  1649. rc = -EINVAL;
  1650. goto out_unlock;
  1651. }
  1652. context2 = sidtab_search(&sidtab, mls_sid);
  1653. if (!context2) {
  1654. printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
  1655. "%d\n", mls_sid);
  1656. rc = -EINVAL;
  1657. goto out_unlock;
  1658. }
  1659. newcon.user = context1->user;
  1660. newcon.role = context1->role;
  1661. newcon.type = context1->type;
  1662. rc = mls_copy_context(&newcon, context2);
  1663. if (rc)
  1664. goto out_unlock;
  1665. /* Check the validity of the new context. */
  1666. if (!policydb_context_isvalid(&policydb, &newcon)) {
  1667. rc = convert_context_handle_invalid_context(&newcon);
  1668. if (rc)
  1669. goto bad;
  1670. }
  1671. rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
  1672. goto out_unlock;
  1673. bad:
  1674. if (!context_struct_to_string(&newcon, &s, &len)) {
  1675. audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1676. "security_sid_mls_copy: invalid context %s", s);
  1677. kfree(s);
  1678. }
  1679. out_unlock:
  1680. POLICY_RDUNLOCK;
  1681. context_destroy(&newcon);
  1682. out:
  1683. return rc;
  1684. }
  1685. struct selinux_audit_rule {
  1686. u32 au_seqno;
  1687. struct context au_ctxt;
  1688. };
  1689. void selinux_audit_rule_free(struct selinux_audit_rule *rule)
  1690. {
  1691. if (rule) {
  1692. context_destroy(&rule->au_ctxt);
  1693. kfree(rule);
  1694. }
  1695. }
  1696. int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
  1697. struct selinux_audit_rule **rule)
  1698. {
  1699. struct selinux_audit_rule *tmprule;
  1700. struct role_datum *roledatum;
  1701. struct type_datum *typedatum;
  1702. struct user_datum *userdatum;
  1703. int rc = 0;
  1704. *rule = NULL;
  1705. if (!ss_initialized)
  1706. return -ENOTSUPP;
  1707. switch (field) {
  1708. case AUDIT_SUBJ_USER:
  1709. case AUDIT_SUBJ_ROLE:
  1710. case AUDIT_SUBJ_TYPE:
  1711. case AUDIT_OBJ_USER:
  1712. case AUDIT_OBJ_ROLE:
  1713. case AUDIT_OBJ_TYPE:
  1714. /* only 'equals' and 'not equals' fit user, role, and type */
  1715. if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
  1716. return -EINVAL;
  1717. break;
  1718. case AUDIT_SUBJ_SEN:
  1719. case AUDIT_SUBJ_CLR:
  1720. case AUDIT_OBJ_LEV_LOW:
  1721. case AUDIT_OBJ_LEV_HIGH:
  1722. /* we do not allow a range, indicated by the presense of '-' */
  1723. if (strchr(rulestr, '-'))
  1724. return -EINVAL;
  1725. break;
  1726. default:
  1727. /* only the above fields are valid */
  1728. return -EINVAL;
  1729. }
  1730. tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
  1731. if (!tmprule)
  1732. return -ENOMEM;
  1733. context_init(&tmprule->au_ctxt);
  1734. POLICY_RDLOCK;
  1735. tmprule->au_seqno = latest_granting;
  1736. switch (field) {
  1737. case AUDIT_SUBJ_USER:
  1738. case AUDIT_OBJ_USER:
  1739. userdatum = hashtab_search(policydb.p_users.table, rulestr);
  1740. if (!userdatum)
  1741. rc = -EINVAL;
  1742. else
  1743. tmprule->au_ctxt.user = userdatum->value;
  1744. break;
  1745. case AUDIT_SUBJ_ROLE:
  1746. case AUDIT_OBJ_ROLE:
  1747. roledatum = hashtab_search(policydb.p_roles.table, rulestr);
  1748. if (!roledatum)
  1749. rc = -EINVAL;
  1750. else
  1751. tmprule->au_ctxt.role = roledatum->value;
  1752. break;
  1753. case AUDIT_SUBJ_TYPE:
  1754. case AUDIT_OBJ_TYPE:
  1755. typedatum = hashtab_search(policydb.p_types.table, rulestr);
  1756. if (!typedatum)
  1757. rc = -EINVAL;
  1758. else
  1759. tmprule->au_ctxt.type = typedatum->value;
  1760. break;
  1761. case AUDIT_SUBJ_SEN:
  1762. case AUDIT_SUBJ_CLR:
  1763. case AUDIT_OBJ_LEV_LOW:
  1764. case AUDIT_OBJ_LEV_HIGH:
  1765. rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
  1766. break;
  1767. }
  1768. POLICY_RDUNLOCK;
  1769. if (rc) {
  1770. selinux_audit_rule_free(tmprule);
  1771. tmprule = NULL;
  1772. }
  1773. *rule = tmprule;
  1774. return rc;
  1775. }
  1776. int selinux_audit_rule_match(u32 sid, u32 field, u32 op,
  1777. struct selinux_audit_rule *rule,
  1778. struct audit_context *actx)
  1779. {
  1780. struct context *ctxt;
  1781. struct mls_level *level;
  1782. int match = 0;
  1783. if (!rule) {
  1784. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1785. "selinux_audit_rule_match: missing rule\n");
  1786. return -ENOENT;
  1787. }
  1788. POLICY_RDLOCK;
  1789. if (rule->au_seqno < latest_granting) {
  1790. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1791. "selinux_audit_rule_match: stale rule\n");
  1792. match = -ESTALE;
  1793. goto out;
  1794. }
  1795. ctxt = sidtab_search(&sidtab, sid);
  1796. if (!ctxt) {
  1797. audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
  1798. "selinux_audit_rule_match: unrecognized SID %d\n",
  1799. sid);
  1800. match = -ENOENT;
  1801. goto out;
  1802. }
  1803. /* a field/op pair that is not caught here will simply fall through
  1804. without a match */
  1805. switch (field) {
  1806. case AUDIT_SUBJ_USER:
  1807. case AUDIT_OBJ_USER:
  1808. switch (op) {
  1809. case AUDIT_EQUAL:
  1810. match = (ctxt->user == rule->au_ctxt.user);
  1811. break;
  1812. case AUDIT_NOT_EQUAL:
  1813. match = (ctxt->user != rule->au_ctxt.user);
  1814. break;
  1815. }
  1816. break;
  1817. case AUDIT_SUBJ_ROLE:
  1818. case AUDIT_OBJ_ROLE:
  1819. switch (op) {
  1820. case AUDIT_EQUAL:
  1821. match = (ctxt->role == rule->au_ctxt.role);
  1822. break;
  1823. case AUDIT_NOT_EQUAL:
  1824. match = (ctxt->role != rule->au_ctxt.role);
  1825. break;
  1826. }
  1827. break;
  1828. case AUDIT_SUBJ_TYPE:
  1829. case AUDIT_OBJ_TYPE:
  1830. switch (op) {
  1831. case AUDIT_EQUAL:
  1832. match = (ctxt->type == rule->au_ctxt.type);
  1833. break;
  1834. case AUDIT_NOT_EQUAL:
  1835. match = (ctxt->type != rule->au_ctxt.type);
  1836. break;
  1837. }
  1838. break;
  1839. case AUDIT_SUBJ_SEN:
  1840. case AUDIT_SUBJ_CLR:
  1841. case AUDIT_OBJ_LEV_LOW:
  1842. case AUDIT_OBJ_LEV_HIGH:
  1843. level = ((field == AUDIT_SUBJ_SEN ||
  1844. field == AUDIT_OBJ_LEV_LOW) ?
  1845. &ctxt->range.level[0] : &ctxt->range.level[1]);
  1846. switch (op) {
  1847. case AUDIT_EQUAL:
  1848. match = mls_level_eq(&rule->au_ctxt.range.level[0],
  1849. level);
  1850. break;
  1851. case AUDIT_NOT_EQUAL:
  1852. match = !mls_level_eq(&rule->au_ctxt.range.level[0],
  1853. level);
  1854. break;
  1855. case AUDIT_LESS_THAN:
  1856. match = (mls_level_dom(&rule->au_ctxt.range.level[0],
  1857. level) &&
  1858. !mls_level_eq(&rule->au_ctxt.range.level[0],
  1859. level));
  1860. break;
  1861. case AUDIT_LESS_THAN_OR_EQUAL:
  1862. match = mls_level_dom(&rule->au_ctxt.range.level[0],
  1863. level);
  1864. break;
  1865. case AUDIT_GREATER_THAN:
  1866. match = (mls_level_dom(level,
  1867. &rule->au_ctxt.range.level[0]) &&
  1868. !mls_level_eq(level,
  1869. &rule->au_ctxt.range.level[0]));
  1870. break;
  1871. case AUDIT_GREATER_THAN_OR_EQUAL:
  1872. match = mls_level_dom(level,
  1873. &rule->au_ctxt.range.level[0]);
  1874. break;
  1875. }
  1876. }
  1877. out:
  1878. POLICY_RDUNLOCK;
  1879. return match;
  1880. }
  1881. static int (*aurule_callback)(void) = NULL;
  1882. static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
  1883. u16 class, u32 perms, u32 *retained)
  1884. {
  1885. int err = 0;
  1886. if (event == AVC_CALLBACK_RESET && aurule_callback)
  1887. err = aurule_callback();
  1888. return err;
  1889. }
  1890. static int __init aurule_init(void)
  1891. {
  1892. int err;
  1893. err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
  1894. SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
  1895. if (err)
  1896. panic("avc_add_callback() failed, error %d\n", err);
  1897. return err;
  1898. }
  1899. __initcall(aurule_init);
  1900. void selinux_audit_set_callback(int (*callback)(void))
  1901. {
  1902. aurule_callback = callback;
  1903. }
  1904. #ifdef CONFIG_NETLABEL
  1905. /*
  1906. * This is the structure we store inside the NetLabel cache block.
  1907. */
  1908. #define NETLBL_CACHE(x) ((struct netlbl_cache *)(x))
  1909. #define NETLBL_CACHE_T_NONE 0
  1910. #define NETLBL_CACHE_T_SID 1
  1911. #define NETLBL_CACHE_T_MLS 2
  1912. struct netlbl_cache {
  1913. u32 type;
  1914. union {
  1915. u32 sid;
  1916. struct mls_range mls_label;
  1917. } data;
  1918. };
  1919. /**
  1920. * selinux_netlbl_cache_free - Free the NetLabel cached data
  1921. * @data: the data to free
  1922. *
  1923. * Description:
  1924. * This function is intended to be used as the free() callback inside the
  1925. * netlbl_lsm_cache structure.
  1926. *
  1927. */
  1928. static void selinux_netlbl_cache_free(const void *data)
  1929. {
  1930. struct netlbl_cache *cache;
  1931. if (data == NULL)
  1932. return;
  1933. cache = NETLBL_CACHE(data);
  1934. switch (cache->type) {
  1935. case NETLBL_CACHE_T_MLS:
  1936. ebitmap_destroy(&cache->data.mls_label.level[0].cat);
  1937. break;
  1938. }
  1939. kfree(data);
  1940. }
  1941. /**
  1942. * selinux_netlbl_cache_add - Add an entry to the NetLabel cache
  1943. * @skb: the packet
  1944. * @ctx: the SELinux context
  1945. *
  1946. * Description:
  1947. * Attempt to cache the context in @ctx, which was derived from the packet in
  1948. * @skb, in the NetLabel subsystem cache.
  1949. *
  1950. */
  1951. static void selinux_netlbl_cache_add(struct sk_buff *skb, struct context *ctx)
  1952. {
  1953. struct netlbl_cache *cache = NULL;
  1954. struct netlbl_lsm_secattr secattr;
  1955. netlbl_secattr_init(&secattr);
  1956. secattr.cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
  1957. if (secattr.cache == NULL)
  1958. goto netlbl_cache_add_return;
  1959. cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
  1960. if (cache == NULL)
  1961. goto netlbl_cache_add_return;
  1962. secattr.cache->free = selinux_netlbl_cache_free;
  1963. secattr.cache->data = (void *)cache;
  1964. cache->type = NETLBL_CACHE_T_MLS;
  1965. if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
  1966. &ctx->range.level[0].cat) != 0)
  1967. goto netlbl_cache_add_return;
  1968. cache->data.mls_label.level[1].cat.highbit =
  1969. cache->data.mls_label.level[0].cat.highbit;
  1970. cache->data.mls_label.level[1].cat.node =
  1971. cache->data.mls_label.level[0].cat.node;
  1972. cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
  1973. cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
  1974. netlbl_cache_add(skb, &secattr);
  1975. netlbl_cache_add_return:
  1976. netlbl_secattr_destroy(&secattr);
  1977. }
  1978. /**
  1979. * selinux_netlbl_cache_invalidate - Invalidate the NetLabel cache
  1980. *
  1981. * Description:
  1982. * Invalidate the NetLabel security attribute mapping cache.
  1983. *
  1984. */
  1985. void selinux_netlbl_cache_invalidate(void)
  1986. {
  1987. netlbl_cache_invalidate();
  1988. }
  1989. /**
  1990. * selinux_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
  1991. * @skb: the network packet
  1992. * @secattr: the NetLabel packet security attributes
  1993. * @base_sid: the SELinux SID to use as a context for MLS only attributes
  1994. * @sid: the SELinux SID
  1995. *
  1996. * Description:
  1997. * Convert the given NetLabel packet security attributes in @secattr into a
  1998. * SELinux SID. If the @secattr field does not contain a full SELinux
  1999. * SID/context then use the context in @base_sid as the foundation. If @skb
  2000. * is not NULL attempt to cache as much data as possibile. Returns zero on
  2001. * success, negative values on failure.
  2002. *
  2003. */
  2004. static int selinux_netlbl_secattr_to_sid(struct sk_buff *skb,
  2005. struct netlbl_lsm_secattr *secattr,
  2006. u32 base_sid,
  2007. u32 *sid)
  2008. {
  2009. int rc = -EIDRM;
  2010. struct context *ctx;
  2011. struct context ctx_new;
  2012. struct netlbl_cache *cache;
  2013. POLICY_RDLOCK;
  2014. if (secattr->cache) {
  2015. cache = NETLBL_CACHE(secattr->cache->data);
  2016. switch (cache->type) {
  2017. case NETLBL_CACHE_T_SID:
  2018. *sid = cache->data.sid;
  2019. rc = 0;
  2020. break;
  2021. case NETLBL_CACHE_T_MLS:
  2022. ctx = sidtab_search(&sidtab, base_sid);
  2023. if (ctx == NULL)
  2024. goto netlbl_secattr_to_sid_return;
  2025. ctx_new.user = ctx->user;
  2026. ctx_new.role = ctx->role;
  2027. ctx_new.type = ctx->type;
  2028. ctx_new.range.level[0].sens =
  2029. cache->data.mls_label.level[0].sens;
  2030. ctx_new.range.level[0].cat.highbit =
  2031. cache->data.mls_label.level[0].cat.highbit;
  2032. ctx_new.range.level[0].cat.node =
  2033. cache->data.mls_label.level[0].cat.node;
  2034. ctx_new.range.level[1].sens =
  2035. cache->data.mls_label.level[1].sens;
  2036. ctx_new.range.level[1].cat.highbit =
  2037. cache->data.mls_label.level[1].cat.highbit;
  2038. ctx_new.range.level[1].cat.node =
  2039. cache->data.mls_label.level[1].cat.node;
  2040. rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
  2041. break;
  2042. default:
  2043. goto netlbl_secattr_to_sid_return;
  2044. }
  2045. } else if (secattr->mls_lvl_vld) {
  2046. ctx = sidtab_search(&sidtab, base_sid);
  2047. if (ctx == NULL)
  2048. goto netlbl_secattr_to_sid_return;
  2049. ctx_new.user = ctx->user;
  2050. ctx_new.role = ctx->role;
  2051. ctx_new.type = ctx->type;
  2052. mls_import_lvl(&ctx_new, secattr->mls_lvl, secattr->mls_lvl);
  2053. if (secattr->mls_cat) {
  2054. if (mls_import_cat(&ctx_new,
  2055. secattr->mls_cat,
  2056. secattr->mls_cat_len,
  2057. NULL,
  2058. 0) != 0)
  2059. goto netlbl_secattr_to_sid_return;
  2060. ctx_new.range.level[1].cat.highbit =
  2061. ctx_new.range.level[0].cat.highbit;
  2062. ctx_new.range.level[1].cat.node =
  2063. ctx_new.range.level[0].cat.node;
  2064. } else {
  2065. ebitmap_init(&ctx_new.range.level[0].cat);
  2066. ebitmap_init(&ctx_new.range.level[1].cat);
  2067. }
  2068. if (mls_context_isvalid(&policydb, &ctx_new) != 1)
  2069. goto netlbl_secattr_to_sid_return_cleanup;
  2070. rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
  2071. if (rc != 0)
  2072. goto netlbl_secattr_to_sid_return_cleanup;
  2073. if (skb != NULL)
  2074. selinux_netlbl_cache_add(skb, &ctx_new);
  2075. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2076. } else {
  2077. *sid = SECSID_NULL;
  2078. rc = 0;
  2079. }
  2080. netlbl_secattr_to_sid_return:
  2081. POLICY_RDUNLOCK;
  2082. return rc;
  2083. netlbl_secattr_to_sid_return_cleanup:
  2084. ebitmap_destroy(&ctx_new.range.level[0].cat);
  2085. goto netlbl_secattr_to_sid_return;
  2086. }
  2087. /**
  2088. * selinux_netlbl_skbuff_getsid - Get the sid of a packet using NetLabel
  2089. * @skb: the packet
  2090. * @base_sid: the SELinux SID to use as a context for MLS only attributes
  2091. * @sid: the SID
  2092. *
  2093. * Description:
  2094. * Call the NetLabel mechanism to get the security attributes of the given
  2095. * packet and use those attributes to determine the correct context/SID to
  2096. * assign to the packet. Returns zero on success, negative values on failure.
  2097. *
  2098. */
  2099. static int selinux_netlbl_skbuff_getsid(struct sk_buff *skb,
  2100. u32 base_sid,
  2101. u32 *sid)
  2102. {
  2103. int rc;
  2104. struct netlbl_lsm_secattr secattr;
  2105. netlbl_secattr_init(&secattr);
  2106. rc = netlbl_skbuff_getattr(skb, &secattr);
  2107. if (rc == 0)
  2108. rc = selinux_netlbl_secattr_to_sid(skb,
  2109. &secattr,
  2110. base_sid,
  2111. sid);
  2112. netlbl_secattr_destroy(&secattr);
  2113. return rc;
  2114. }
  2115. /**
  2116. * selinux_netlbl_socket_setsid - Label a socket using the NetLabel mechanism
  2117. * @sock: the socket to label
  2118. * @sid: the SID to use
  2119. *
  2120. * Description:
  2121. * Attempt to label a socket using the NetLabel mechanism using the given
  2122. * SID. Returns zero values on success, negative values on failure.
  2123. *
  2124. */
  2125. static int selinux_netlbl_socket_setsid(struct socket *sock, u32 sid)
  2126. {
  2127. int rc = -ENOENT;
  2128. struct sk_security_struct *sksec = sock->sk->sk_security;
  2129. struct netlbl_lsm_secattr secattr;
  2130. struct context *ctx;
  2131. if (!ss_initialized)
  2132. return 0;
  2133. netlbl_secattr_init(&secattr);
  2134. POLICY_RDLOCK;
  2135. ctx = sidtab_search(&sidtab, sid);
  2136. if (ctx == NULL)
  2137. goto netlbl_socket_setsid_return;
  2138. secattr.domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
  2139. GFP_ATOMIC);
  2140. mls_export_lvl(ctx, &secattr.mls_lvl, NULL);
  2141. secattr.mls_lvl_vld = 1;
  2142. rc = mls_export_cat(ctx,
  2143. &secattr.mls_cat,
  2144. &secattr.mls_cat_len,
  2145. NULL,
  2146. NULL);
  2147. if (rc != 0)
  2148. goto netlbl_socket_setsid_return;
  2149. rc = netlbl_socket_setattr(sock, &secattr);
  2150. if (rc == 0)
  2151. sksec->nlbl_state = NLBL_LABELED;
  2152. netlbl_socket_setsid_return:
  2153. POLICY_RDUNLOCK;
  2154. netlbl_secattr_destroy(&secattr);
  2155. return rc;
  2156. }
  2157. /**
  2158. * selinux_netlbl_sk_security_init - Setup the NetLabel fields
  2159. * @ssec: the sk_security_struct
  2160. * @family: the socket family
  2161. *
  2162. * Description:
  2163. * Called when a new sk_security_struct is allocated to initialize the NetLabel
  2164. * fields.
  2165. *
  2166. */
  2167. void selinux_netlbl_sk_security_init(struct sk_security_struct *ssec,
  2168. int family)
  2169. {
  2170. if (family == PF_INET)
  2171. ssec->nlbl_state = NLBL_REQUIRE;
  2172. else
  2173. ssec->nlbl_state = NLBL_UNSET;
  2174. }
  2175. /**
  2176. * selinux_netlbl_sk_clone_security - Copy the NetLabel fields
  2177. * @ssec: the original sk_security_struct
  2178. * @newssec: the cloned sk_security_struct
  2179. *
  2180. * Description:
  2181. * Clone the NetLabel specific sk_security_struct fields from @ssec to
  2182. * @newssec.
  2183. *
  2184. */
  2185. void selinux_netlbl_sk_clone_security(struct sk_security_struct *ssec,
  2186. struct sk_security_struct *newssec)
  2187. {
  2188. newssec->sclass = ssec->sclass;
  2189. if (ssec->nlbl_state != NLBL_UNSET)
  2190. newssec->nlbl_state = NLBL_REQUIRE;
  2191. else
  2192. newssec->nlbl_state = NLBL_UNSET;
  2193. }
  2194. /**
  2195. * selinux_netlbl_socket_post_create - Label a socket using NetLabel
  2196. * @sock: the socket to label
  2197. * @sock_family: the socket family
  2198. * @sid: the SID to use
  2199. *
  2200. * Description:
  2201. * Attempt to label a socket using the NetLabel mechanism using the given
  2202. * SID. Returns zero values on success, negative values on failure.
  2203. *
  2204. */
  2205. int selinux_netlbl_socket_post_create(struct socket *sock,
  2206. int sock_family,
  2207. u32 sid)
  2208. {
  2209. struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
  2210. struct sk_security_struct *sksec = sock->sk->sk_security;
  2211. sksec->sclass = isec->sclass;
  2212. if (sock_family != PF_INET)
  2213. return 0;
  2214. sksec->nlbl_state = NLBL_REQUIRE;
  2215. return selinux_netlbl_socket_setsid(sock, sid);
  2216. }
  2217. /**
  2218. * selinux_netlbl_sock_graft - Netlabel the new socket
  2219. * @sk: the new connection
  2220. * @sock: the new socket
  2221. *
  2222. * Description:
  2223. * The connection represented by @sk is being grafted onto @sock so set the
  2224. * socket's NetLabel to match the SID of @sk.
  2225. *
  2226. */
  2227. void selinux_netlbl_sock_graft(struct sock *sk, struct socket *sock)
  2228. {
  2229. struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
  2230. struct sk_security_struct *sksec = sk->sk_security;
  2231. struct netlbl_lsm_secattr secattr;
  2232. u32 nlbl_peer_sid;
  2233. sksec->sclass = isec->sclass;
  2234. if (sk->sk_family != PF_INET)
  2235. return;
  2236. netlbl_secattr_init(&secattr);
  2237. if (netlbl_sock_getattr(sk, &secattr) == 0 &&
  2238. selinux_netlbl_secattr_to_sid(NULL,
  2239. &secattr,
  2240. SECINITSID_UNLABELED,
  2241. &nlbl_peer_sid) == 0)
  2242. sksec->peer_sid = nlbl_peer_sid;
  2243. netlbl_secattr_destroy(&secattr);
  2244. sksec->nlbl_state = NLBL_REQUIRE;
  2245. /* Try to set the NetLabel on the socket to save time later, if we fail
  2246. * here we will pick up the pieces in later calls to
  2247. * selinux_netlbl_inode_permission(). */
  2248. selinux_netlbl_socket_setsid(sock, sksec->sid);
  2249. }
  2250. /**
  2251. * selinux_netlbl_inet_conn_request - Handle a new connection request
  2252. * @skb: the packet
  2253. * @sock_sid: the SID of the parent socket
  2254. *
  2255. * Description:
  2256. * If present, use the security attributes of the packet in @skb and the
  2257. * parent sock's SID to arrive at a SID for the new child sock. Returns the
  2258. * SID of the connection or SECSID_NULL on failure.
  2259. *
  2260. */
  2261. u32 selinux_netlbl_inet_conn_request(struct sk_buff *skb, u32 sock_sid)
  2262. {
  2263. int rc;
  2264. u32 peer_sid;
  2265. rc = selinux_netlbl_skbuff_getsid(skb, sock_sid, &peer_sid);
  2266. if (rc != 0)
  2267. return SECSID_NULL;
  2268. return peer_sid;
  2269. }
  2270. /**
  2271. * selinux_netlbl_inode_permission - Verify the socket is NetLabel labeled
  2272. * @inode: the file descriptor's inode
  2273. * @mask: the permission mask
  2274. *
  2275. * Description:
  2276. * Looks at a file's inode and if it is marked as a socket protected by
  2277. * NetLabel then verify that the socket has been labeled, if not try to label
  2278. * the socket now with the inode's SID. Returns zero on success, negative
  2279. * values on failure.
  2280. *
  2281. */
  2282. int selinux_netlbl_inode_permission(struct inode *inode, int mask)
  2283. {
  2284. int rc;
  2285. struct inode_security_struct *isec;
  2286. struct sk_security_struct *sksec;
  2287. struct socket *sock;
  2288. if (!S_ISSOCK(inode->i_mode))
  2289. return 0;
  2290. sock = SOCKET_I(inode);
  2291. isec = inode->i_security;
  2292. sksec = sock->sk->sk_security;
  2293. mutex_lock(&isec->lock);
  2294. if (unlikely(sksec->nlbl_state == NLBL_REQUIRE &&
  2295. (mask & (MAY_WRITE | MAY_APPEND)))) {
  2296. lock_sock(sock->sk);
  2297. rc = selinux_netlbl_socket_setsid(sock, sksec->sid);
  2298. release_sock(sock->sk);
  2299. } else
  2300. rc = 0;
  2301. mutex_unlock(&isec->lock);
  2302. return rc;
  2303. }
  2304. /**
  2305. * selinux_netlbl_sock_rcv_skb - Do an inbound access check using NetLabel
  2306. * @sksec: the sock's sk_security_struct
  2307. * @skb: the packet
  2308. * @ad: the audit data
  2309. *
  2310. * Description:
  2311. * Fetch the NetLabel security attributes from @skb and perform an access check
  2312. * against the receiving socket. Returns zero on success, negative values on
  2313. * error.
  2314. *
  2315. */
  2316. int selinux_netlbl_sock_rcv_skb(struct sk_security_struct *sksec,
  2317. struct sk_buff *skb,
  2318. struct avc_audit_data *ad)
  2319. {
  2320. int rc;
  2321. u32 netlbl_sid;
  2322. u32 recv_perm;
  2323. rc = selinux_netlbl_skbuff_getsid(skb,
  2324. SECINITSID_UNLABELED,
  2325. &netlbl_sid);
  2326. if (rc != 0)
  2327. return rc;
  2328. if (netlbl_sid == SECSID_NULL)
  2329. return 0;
  2330. switch (sksec->sclass) {
  2331. case SECCLASS_UDP_SOCKET:
  2332. recv_perm = UDP_SOCKET__RECVFROM;
  2333. break;
  2334. case SECCLASS_TCP_SOCKET:
  2335. recv_perm = TCP_SOCKET__RECVFROM;
  2336. break;
  2337. default:
  2338. recv_perm = RAWIP_SOCKET__RECVFROM;
  2339. }
  2340. rc = avc_has_perm(sksec->sid,
  2341. netlbl_sid,
  2342. sksec->sclass,
  2343. recv_perm,
  2344. ad);
  2345. if (rc == 0)
  2346. return 0;
  2347. netlbl_skbuff_err(skb, rc);
  2348. return rc;
  2349. }
  2350. /**
  2351. * selinux_netlbl_socket_getpeersec_stream - Return the connected peer's SID
  2352. * @sock: the socket
  2353. *
  2354. * Description:
  2355. * Examine @sock to find the connected peer's SID. Returns the SID on success
  2356. * or SECSID_NULL on error.
  2357. *
  2358. */
  2359. u32 selinux_netlbl_socket_getpeersec_stream(struct socket *sock)
  2360. {
  2361. struct sk_security_struct *sksec = sock->sk->sk_security;
  2362. return sksec->peer_sid;
  2363. }
  2364. /**
  2365. * selinux_netlbl_socket_getpeersec_dgram - Return the SID of a NetLabel packet
  2366. * @skb: the packet
  2367. *
  2368. * Description:
  2369. * Examine @skb to find the SID assigned to it by NetLabel. Returns the SID on
  2370. * success, SECSID_NULL on error.
  2371. *
  2372. */
  2373. u32 selinux_netlbl_socket_getpeersec_dgram(struct sk_buff *skb)
  2374. {
  2375. int peer_sid;
  2376. if (selinux_netlbl_skbuff_getsid(skb,
  2377. SECINITSID_UNLABELED,
  2378. &peer_sid) != 0)
  2379. return SECSID_NULL;
  2380. return peer_sid;
  2381. }
  2382. #endif /* CONFIG_NETLABEL */