services.c 71 KB

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