services.c 72 KB

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