services.c 73 KB

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