services.c 72 KB

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