services.c 76 KB

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