smackfs.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014
  1. /*
  2. * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, version 2.
  7. *
  8. * Authors:
  9. * Casey Schaufler <casey@schaufler-ca.com>
  10. * Ahmed S. Darwish <darwish.07@gmail.com>
  11. *
  12. * Special thanks to the authors of selinuxfs.
  13. *
  14. * Karl MacMillan <kmacmillan@tresys.com>
  15. * James Morris <jmorris@redhat.com>
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/security.h>
  21. #include <linux/mutex.h>
  22. #include <linux/slab.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cipso_ipv4.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/ctype.h>
  27. #include <linux/audit.h>
  28. #include <linux/magic.h>
  29. #include "smack.h"
  30. #define BEBITS (sizeof(__be32) * 8)
  31. /*
  32. * smackfs pseudo filesystem.
  33. */
  34. enum smk_inos {
  35. SMK_ROOT_INO = 2,
  36. SMK_LOAD = 3, /* load policy */
  37. SMK_CIPSO = 4, /* load label -> CIPSO mapping */
  38. SMK_DOI = 5, /* CIPSO DOI */
  39. SMK_DIRECT = 6, /* CIPSO level indicating direct label */
  40. SMK_AMBIENT = 7, /* internet ambient label */
  41. SMK_NET4ADDR = 8, /* single label hosts */
  42. SMK_ONLYCAP = 9, /* the only "capable" label */
  43. SMK_LOGGING = 10, /* logging */
  44. SMK_LOAD_SELF = 11, /* task specific rules */
  45. SMK_ACCESSES = 12, /* access policy */
  46. SMK_MAPPED = 13, /* CIPSO level indicating mapped label */
  47. SMK_LOAD2 = 14, /* load policy with long labels */
  48. SMK_LOAD_SELF2 = 15, /* load task specific rules with long labels */
  49. SMK_ACCESS2 = 16, /* make an access check with long labels */
  50. SMK_CIPSO2 = 17, /* load long label -> CIPSO mapping */
  51. SMK_REVOKE_SUBJ = 18, /* set rules with subject label to '-' */
  52. SMK_CHANGE_RULE = 19, /* change or add rules (long labels) */
  53. SMK_SYSLOG = 20, /* change syslog label) */
  54. SMK_PTRACE = 21, /* set ptrace rule */
  55. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  56. SMK_UNCONFINED = 22, /* define an unconfined label */
  57. #endif
  58. #if IS_ENABLED(CONFIG_IPV6)
  59. SMK_NET6ADDR = 23, /* single label IPv6 hosts */
  60. #endif /* CONFIG_IPV6 */
  61. SMK_RELABEL_SELF = 24, /* relabel possible without CAP_MAC_ADMIN */
  62. };
  63. /*
  64. * List locks
  65. */
  66. static DEFINE_MUTEX(smack_cipso_lock);
  67. static DEFINE_MUTEX(smack_ambient_lock);
  68. static DEFINE_MUTEX(smk_net4addr_lock);
  69. #if IS_ENABLED(CONFIG_IPV6)
  70. static DEFINE_MUTEX(smk_net6addr_lock);
  71. #endif /* CONFIG_IPV6 */
  72. /*
  73. * This is the "ambient" label for network traffic.
  74. * If it isn't somehow marked, use this.
  75. * It can be reset via smackfs/ambient
  76. */
  77. struct smack_known *smack_net_ambient;
  78. /*
  79. * This is the level in a CIPSO header that indicates a
  80. * smack label is contained directly in the category set.
  81. * It can be reset via smackfs/direct
  82. */
  83. int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
  84. /*
  85. * This is the level in a CIPSO header that indicates a
  86. * secid is contained directly in the category set.
  87. * It can be reset via smackfs/mapped
  88. */
  89. int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
  90. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  91. /*
  92. * Allow one label to be unconfined. This is for
  93. * debugging and application bring-up purposes only.
  94. * It is bad and wrong, but everyone seems to expect
  95. * to have it.
  96. */
  97. struct smack_known *smack_unconfined;
  98. #endif
  99. /*
  100. * If this value is set restrict syslog use to the label specified.
  101. * It can be reset via smackfs/syslog
  102. */
  103. struct smack_known *smack_syslog_label;
  104. /*
  105. * Ptrace current rule
  106. * SMACK_PTRACE_DEFAULT regular smack ptrace rules (/proc based)
  107. * SMACK_PTRACE_EXACT labels must match, but can be overriden with
  108. * CAP_SYS_PTRACE
  109. * SMACK_PTRACE_DRACONIAN lables must match, CAP_SYS_PTRACE has no effect
  110. */
  111. int smack_ptrace_rule = SMACK_PTRACE_DEFAULT;
  112. /*
  113. * Certain IP addresses may be designated as single label hosts.
  114. * Packets are sent there unlabeled, but only from tasks that
  115. * can write to the specified label.
  116. */
  117. LIST_HEAD(smk_net4addr_list);
  118. #if IS_ENABLED(CONFIG_IPV6)
  119. LIST_HEAD(smk_net6addr_list);
  120. #endif /* CONFIG_IPV6 */
  121. /*
  122. * Rule lists are maintained for each label.
  123. * This master list is just for reading /smack/load and /smack/load2.
  124. */
  125. struct smack_master_list {
  126. struct list_head list;
  127. struct smack_rule *smk_rule;
  128. };
  129. static LIST_HEAD(smack_rule_list);
  130. struct smack_parsed_rule {
  131. struct smack_known *smk_subject;
  132. struct smack_known *smk_object;
  133. int smk_access1;
  134. int smk_access2;
  135. };
  136. static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
  137. /*
  138. * Values for parsing cipso rules
  139. * SMK_DIGITLEN: Length of a digit field in a rule.
  140. * SMK_CIPSOMIN: Minimum possible cipso rule length.
  141. * SMK_CIPSOMAX: Maximum possible cipso rule length.
  142. */
  143. #define SMK_DIGITLEN 4
  144. #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
  145. #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
  146. /*
  147. * Values for parsing MAC rules
  148. * SMK_ACCESS: Maximum possible combination of access permissions
  149. * SMK_ACCESSLEN: Maximum length for a rule access field
  150. * SMK_LOADLEN: Smack rule length
  151. */
  152. #define SMK_OACCESS "rwxa"
  153. #define SMK_ACCESS "rwxatl"
  154. #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
  155. #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
  156. #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
  157. #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
  158. /*
  159. * Stricly for CIPSO level manipulation.
  160. * Set the category bit number in a smack label sized buffer.
  161. */
  162. static inline void smack_catset_bit(unsigned int cat, char *catsetp)
  163. {
  164. if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
  165. return;
  166. catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
  167. }
  168. /**
  169. * smk_netlabel_audit_set - fill a netlbl_audit struct
  170. * @nap: structure to fill
  171. */
  172. static void smk_netlabel_audit_set(struct netlbl_audit *nap)
  173. {
  174. struct smack_known *skp = smk_of_current();
  175. nap->loginuid = audit_get_loginuid(current);
  176. nap->sessionid = audit_get_sessionid(current);
  177. nap->secid = skp->smk_secid;
  178. }
  179. /*
  180. * Value for parsing single label host rules
  181. * "1.2.3.4 X"
  182. */
  183. #define SMK_NETLBLADDRMIN 9
  184. /**
  185. * smk_set_access - add a rule to the rule list or replace an old rule
  186. * @srp: the rule to add or replace
  187. * @rule_list: the list of rules
  188. * @rule_lock: the rule list lock
  189. * @global: if non-zero, indicates a global rule
  190. *
  191. * Looks through the current subject/object/access list for
  192. * the subject/object pair and replaces the access that was
  193. * there. If the pair isn't found add it with the specified
  194. * access.
  195. *
  196. * Returns 0 if nothing goes wrong or -ENOMEM if it fails
  197. * during the allocation of the new pair to add.
  198. */
  199. static int smk_set_access(struct smack_parsed_rule *srp,
  200. struct list_head *rule_list,
  201. struct mutex *rule_lock, int global)
  202. {
  203. struct smack_rule *sp;
  204. struct smack_master_list *smlp;
  205. int found = 0;
  206. int rc = 0;
  207. mutex_lock(rule_lock);
  208. /*
  209. * Because the object label is less likely to match
  210. * than the subject label check it first
  211. */
  212. list_for_each_entry_rcu(sp, rule_list, list) {
  213. if (sp->smk_object == srp->smk_object &&
  214. sp->smk_subject == srp->smk_subject) {
  215. found = 1;
  216. sp->smk_access |= srp->smk_access1;
  217. sp->smk_access &= ~srp->smk_access2;
  218. break;
  219. }
  220. }
  221. if (found == 0) {
  222. sp = kzalloc(sizeof(*sp), GFP_KERNEL);
  223. if (sp == NULL) {
  224. rc = -ENOMEM;
  225. goto out;
  226. }
  227. sp->smk_subject = srp->smk_subject;
  228. sp->smk_object = srp->smk_object;
  229. sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
  230. list_add_rcu(&sp->list, rule_list);
  231. /*
  232. * If this is a global as opposed to self and a new rule
  233. * it needs to get added for reporting.
  234. */
  235. if (global) {
  236. smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
  237. if (smlp != NULL) {
  238. smlp->smk_rule = sp;
  239. list_add_rcu(&smlp->list, &smack_rule_list);
  240. } else
  241. rc = -ENOMEM;
  242. }
  243. }
  244. out:
  245. mutex_unlock(rule_lock);
  246. return rc;
  247. }
  248. /**
  249. * smk_perm_from_str - parse smack accesses from a text string
  250. * @string: a text string that contains a Smack accesses code
  251. *
  252. * Returns an integer with respective bits set for specified accesses.
  253. */
  254. static int smk_perm_from_str(const char *string)
  255. {
  256. int perm = 0;
  257. const char *cp;
  258. for (cp = string; ; cp++)
  259. switch (*cp) {
  260. case '-':
  261. break;
  262. case 'r':
  263. case 'R':
  264. perm |= MAY_READ;
  265. break;
  266. case 'w':
  267. case 'W':
  268. perm |= MAY_WRITE;
  269. break;
  270. case 'x':
  271. case 'X':
  272. perm |= MAY_EXEC;
  273. break;
  274. case 'a':
  275. case 'A':
  276. perm |= MAY_APPEND;
  277. break;
  278. case 't':
  279. case 'T':
  280. perm |= MAY_TRANSMUTE;
  281. break;
  282. case 'l':
  283. case 'L':
  284. perm |= MAY_LOCK;
  285. break;
  286. case 'b':
  287. case 'B':
  288. perm |= MAY_BRINGUP;
  289. break;
  290. default:
  291. return perm;
  292. }
  293. }
  294. /**
  295. * smk_fill_rule - Fill Smack rule from strings
  296. * @subject: subject label string
  297. * @object: object label string
  298. * @access1: access string
  299. * @access2: string with permissions to be removed
  300. * @rule: Smack rule
  301. * @import: if non-zero, import labels
  302. * @len: label length limit
  303. *
  304. * Returns 0 on success, appropriate error code on failure.
  305. */
  306. static int smk_fill_rule(const char *subject, const char *object,
  307. const char *access1, const char *access2,
  308. struct smack_parsed_rule *rule, int import,
  309. int len)
  310. {
  311. const char *cp;
  312. struct smack_known *skp;
  313. if (import) {
  314. rule->smk_subject = smk_import_entry(subject, len);
  315. if (IS_ERR(rule->smk_subject))
  316. return PTR_ERR(rule->smk_subject);
  317. rule->smk_object = smk_import_entry(object, len);
  318. if (IS_ERR(rule->smk_object))
  319. return PTR_ERR(rule->smk_object);
  320. } else {
  321. cp = smk_parse_smack(subject, len);
  322. if (IS_ERR(cp))
  323. return PTR_ERR(cp);
  324. skp = smk_find_entry(cp);
  325. kfree(cp);
  326. if (skp == NULL)
  327. return -ENOENT;
  328. rule->smk_subject = skp;
  329. cp = smk_parse_smack(object, len);
  330. if (IS_ERR(cp))
  331. return PTR_ERR(cp);
  332. skp = smk_find_entry(cp);
  333. kfree(cp);
  334. if (skp == NULL)
  335. return -ENOENT;
  336. rule->smk_object = skp;
  337. }
  338. rule->smk_access1 = smk_perm_from_str(access1);
  339. if (access2)
  340. rule->smk_access2 = smk_perm_from_str(access2);
  341. else
  342. rule->smk_access2 = ~rule->smk_access1;
  343. return 0;
  344. }
  345. /**
  346. * smk_parse_rule - parse Smack rule from load string
  347. * @data: string to be parsed whose size is SMK_LOADLEN
  348. * @rule: Smack rule
  349. * @import: if non-zero, import labels
  350. *
  351. * Returns 0 on success, -1 on errors.
  352. */
  353. static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
  354. int import)
  355. {
  356. int rc;
  357. rc = smk_fill_rule(data, data + SMK_LABELLEN,
  358. data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
  359. import, SMK_LABELLEN);
  360. return rc;
  361. }
  362. /**
  363. * smk_parse_long_rule - parse Smack rule from rule string
  364. * @data: string to be parsed, null terminated
  365. * @rule: Will be filled with Smack parsed rule
  366. * @import: if non-zero, import labels
  367. * @tokens: numer of substrings expected in data
  368. *
  369. * Returns number of processed bytes on success, -ERRNO on failure.
  370. */
  371. static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
  372. int import, int tokens)
  373. {
  374. ssize_t cnt = 0;
  375. char *tok[4];
  376. int rc;
  377. int i;
  378. /*
  379. * Parsing the rule in-place, filling all white-spaces with '\0'
  380. */
  381. for (i = 0; i < tokens; ++i) {
  382. while (isspace(data[cnt]))
  383. data[cnt++] = '\0';
  384. if (data[cnt] == '\0')
  385. /* Unexpected end of data */
  386. return -EINVAL;
  387. tok[i] = data + cnt;
  388. while (data[cnt] && !isspace(data[cnt]))
  389. ++cnt;
  390. }
  391. while (isspace(data[cnt]))
  392. data[cnt++] = '\0';
  393. while (i < 4)
  394. tok[i++] = NULL;
  395. rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
  396. return rc == 0 ? cnt : rc;
  397. }
  398. #define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
  399. #define SMK_LONG_FMT 1 /* Variable long label format */
  400. #define SMK_CHANGE_FMT 2 /* Rule modification format */
  401. /**
  402. * smk_write_rules_list - write() for any /smack rule file
  403. * @file: file pointer, not actually used
  404. * @buf: where to get the data from
  405. * @count: bytes sent
  406. * @ppos: where to start - must be 0
  407. * @rule_list: the list of rules to write to
  408. * @rule_lock: lock for the rule list
  409. * @format: /smack/load or /smack/load2 or /smack/change-rule format.
  410. *
  411. * Get one smack access rule from above.
  412. * The format for SMK_LONG_FMT is:
  413. * "subject<whitespace>object<whitespace>access[<whitespace>...]"
  414. * The format for SMK_FIXED24_FMT is exactly:
  415. * "subject object rwxat"
  416. * The format for SMK_CHANGE_FMT is:
  417. * "subject<whitespace>object<whitespace>
  418. * acc_enable<whitespace>acc_disable[<whitespace>...]"
  419. */
  420. static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
  421. size_t count, loff_t *ppos,
  422. struct list_head *rule_list,
  423. struct mutex *rule_lock, int format)
  424. {
  425. struct smack_parsed_rule rule;
  426. char *data;
  427. int rc;
  428. int trunc = 0;
  429. int tokens;
  430. ssize_t cnt = 0;
  431. /*
  432. * No partial writes.
  433. * Enough data must be present.
  434. */
  435. if (*ppos != 0)
  436. return -EINVAL;
  437. if (format == SMK_FIXED24_FMT) {
  438. /*
  439. * Minor hack for backward compatibility
  440. */
  441. if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
  442. return -EINVAL;
  443. } else {
  444. if (count >= PAGE_SIZE) {
  445. count = PAGE_SIZE - 1;
  446. trunc = 1;
  447. }
  448. }
  449. data = memdup_user_nul(buf, count);
  450. if (IS_ERR(data))
  451. return PTR_ERR(data);
  452. /*
  453. * In case of parsing only part of user buf,
  454. * avoid having partial rule at the data buffer
  455. */
  456. if (trunc) {
  457. while (count > 0 && (data[count - 1] != '\n'))
  458. --count;
  459. if (count == 0) {
  460. rc = -EINVAL;
  461. goto out;
  462. }
  463. }
  464. data[count] = '\0';
  465. tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
  466. while (cnt < count) {
  467. if (format == SMK_FIXED24_FMT) {
  468. rc = smk_parse_rule(data, &rule, 1);
  469. if (rc < 0)
  470. goto out;
  471. cnt = count;
  472. } else {
  473. rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
  474. if (rc < 0)
  475. goto out;
  476. if (rc == 0) {
  477. rc = -EINVAL;
  478. goto out;
  479. }
  480. cnt += rc;
  481. }
  482. if (rule_list == NULL)
  483. rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
  484. &rule.smk_subject->smk_rules_lock, 1);
  485. else
  486. rc = smk_set_access(&rule, rule_list, rule_lock, 0);
  487. if (rc)
  488. goto out;
  489. }
  490. rc = cnt;
  491. out:
  492. kfree(data);
  493. return rc;
  494. }
  495. /*
  496. * Core logic for smackfs seq list operations.
  497. */
  498. static void *smk_seq_start(struct seq_file *s, loff_t *pos,
  499. struct list_head *head)
  500. {
  501. struct list_head *list;
  502. int i = *pos;
  503. rcu_read_lock();
  504. for (list = rcu_dereference(list_next_rcu(head));
  505. list != head;
  506. list = rcu_dereference(list_next_rcu(list))) {
  507. if (i-- == 0)
  508. return list;
  509. }
  510. return NULL;
  511. }
  512. static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
  513. struct list_head *head)
  514. {
  515. struct list_head *list = v;
  516. ++*pos;
  517. list = rcu_dereference(list_next_rcu(list));
  518. return (list == head) ? NULL : list;
  519. }
  520. static void smk_seq_stop(struct seq_file *s, void *v)
  521. {
  522. rcu_read_unlock();
  523. }
  524. static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
  525. {
  526. /*
  527. * Don't show any rules with label names too long for
  528. * interface file (/smack/load or /smack/load2)
  529. * because you should expect to be able to write
  530. * anything you read back.
  531. */
  532. if (strlen(srp->smk_subject->smk_known) >= max ||
  533. strlen(srp->smk_object->smk_known) >= max)
  534. return;
  535. if (srp->smk_access == 0)
  536. return;
  537. seq_printf(s, "%s %s",
  538. srp->smk_subject->smk_known,
  539. srp->smk_object->smk_known);
  540. seq_putc(s, ' ');
  541. if (srp->smk_access & MAY_READ)
  542. seq_putc(s, 'r');
  543. if (srp->smk_access & MAY_WRITE)
  544. seq_putc(s, 'w');
  545. if (srp->smk_access & MAY_EXEC)
  546. seq_putc(s, 'x');
  547. if (srp->smk_access & MAY_APPEND)
  548. seq_putc(s, 'a');
  549. if (srp->smk_access & MAY_TRANSMUTE)
  550. seq_putc(s, 't');
  551. if (srp->smk_access & MAY_LOCK)
  552. seq_putc(s, 'l');
  553. if (srp->smk_access & MAY_BRINGUP)
  554. seq_putc(s, 'b');
  555. seq_putc(s, '\n');
  556. }
  557. /*
  558. * Seq_file read operations for /smack/load
  559. */
  560. static void *load2_seq_start(struct seq_file *s, loff_t *pos)
  561. {
  562. return smk_seq_start(s, pos, &smack_rule_list);
  563. }
  564. static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
  565. {
  566. return smk_seq_next(s, v, pos, &smack_rule_list);
  567. }
  568. static int load_seq_show(struct seq_file *s, void *v)
  569. {
  570. struct list_head *list = v;
  571. struct smack_master_list *smlp =
  572. list_entry_rcu(list, struct smack_master_list, list);
  573. smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
  574. return 0;
  575. }
  576. static const struct seq_operations load_seq_ops = {
  577. .start = load2_seq_start,
  578. .next = load2_seq_next,
  579. .show = load_seq_show,
  580. .stop = smk_seq_stop,
  581. };
  582. /**
  583. * smk_open_load - open() for /smack/load
  584. * @inode: inode structure representing file
  585. * @file: "load" file pointer
  586. *
  587. * For reading, use load_seq_* seq_file reading operations.
  588. */
  589. static int smk_open_load(struct inode *inode, struct file *file)
  590. {
  591. return seq_open(file, &load_seq_ops);
  592. }
  593. /**
  594. * smk_write_load - write() for /smack/load
  595. * @file: file pointer, not actually used
  596. * @buf: where to get the data from
  597. * @count: bytes sent
  598. * @ppos: where to start - must be 0
  599. *
  600. */
  601. static ssize_t smk_write_load(struct file *file, const char __user *buf,
  602. size_t count, loff_t *ppos)
  603. {
  604. /*
  605. * Must have privilege.
  606. * No partial writes.
  607. * Enough data must be present.
  608. */
  609. if (!smack_privileged(CAP_MAC_ADMIN))
  610. return -EPERM;
  611. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  612. SMK_FIXED24_FMT);
  613. }
  614. static const struct file_operations smk_load_ops = {
  615. .open = smk_open_load,
  616. .read = seq_read,
  617. .llseek = seq_lseek,
  618. .write = smk_write_load,
  619. .release = seq_release,
  620. };
  621. /**
  622. * smk_cipso_doi - initialize the CIPSO domain
  623. */
  624. static void smk_cipso_doi(void)
  625. {
  626. int rc;
  627. struct cipso_v4_doi *doip;
  628. struct netlbl_audit nai;
  629. smk_netlabel_audit_set(&nai);
  630. rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
  631. if (rc != 0)
  632. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  633. __func__, __LINE__, rc);
  634. doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
  635. if (doip == NULL)
  636. panic("smack: Failed to initialize cipso DOI.\n");
  637. doip->map.std = NULL;
  638. doip->doi = smk_cipso_doi_value;
  639. doip->type = CIPSO_V4_MAP_PASS;
  640. doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
  641. for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
  642. doip->tags[rc] = CIPSO_V4_TAG_INVALID;
  643. rc = netlbl_cfg_cipsov4_add(doip, &nai);
  644. if (rc != 0) {
  645. printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
  646. __func__, __LINE__, rc);
  647. kfree(doip);
  648. return;
  649. }
  650. rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
  651. if (rc != 0) {
  652. printk(KERN_WARNING "%s:%d map add rc = %d\n",
  653. __func__, __LINE__, rc);
  654. kfree(doip);
  655. return;
  656. }
  657. }
  658. /**
  659. * smk_unlbl_ambient - initialize the unlabeled domain
  660. * @oldambient: previous domain string
  661. */
  662. static void smk_unlbl_ambient(char *oldambient)
  663. {
  664. int rc;
  665. struct netlbl_audit nai;
  666. smk_netlabel_audit_set(&nai);
  667. if (oldambient != NULL) {
  668. rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
  669. if (rc != 0)
  670. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  671. __func__, __LINE__, rc);
  672. }
  673. if (smack_net_ambient == NULL)
  674. smack_net_ambient = &smack_known_floor;
  675. rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
  676. NULL, NULL, &nai);
  677. if (rc != 0)
  678. printk(KERN_WARNING "%s:%d add rc = %d\n",
  679. __func__, __LINE__, rc);
  680. }
  681. /*
  682. * Seq_file read operations for /smack/cipso
  683. */
  684. static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
  685. {
  686. return smk_seq_start(s, pos, &smack_known_list);
  687. }
  688. static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
  689. {
  690. return smk_seq_next(s, v, pos, &smack_known_list);
  691. }
  692. /*
  693. * Print cipso labels in format:
  694. * label level[/cat[,cat]]
  695. */
  696. static int cipso_seq_show(struct seq_file *s, void *v)
  697. {
  698. struct list_head *list = v;
  699. struct smack_known *skp =
  700. list_entry_rcu(list, struct smack_known, list);
  701. struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
  702. char sep = '/';
  703. int i;
  704. /*
  705. * Don't show a label that could not have been set using
  706. * /smack/cipso. This is in support of the notion that
  707. * anything read from /smack/cipso ought to be writeable
  708. * to /smack/cipso.
  709. *
  710. * /smack/cipso2 should be used instead.
  711. */
  712. if (strlen(skp->smk_known) >= SMK_LABELLEN)
  713. return 0;
  714. seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
  715. for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
  716. i = netlbl_catmap_walk(cmp, i + 1)) {
  717. seq_printf(s, "%c%d", sep, i);
  718. sep = ',';
  719. }
  720. seq_putc(s, '\n');
  721. return 0;
  722. }
  723. static const struct seq_operations cipso_seq_ops = {
  724. .start = cipso_seq_start,
  725. .next = cipso_seq_next,
  726. .show = cipso_seq_show,
  727. .stop = smk_seq_stop,
  728. };
  729. /**
  730. * smk_open_cipso - open() for /smack/cipso
  731. * @inode: inode structure representing file
  732. * @file: "cipso" file pointer
  733. *
  734. * Connect our cipso_seq_* operations with /smack/cipso
  735. * file_operations
  736. */
  737. static int smk_open_cipso(struct inode *inode, struct file *file)
  738. {
  739. return seq_open(file, &cipso_seq_ops);
  740. }
  741. /**
  742. * smk_set_cipso - do the work for write() for cipso and cipso2
  743. * @file: file pointer, not actually used
  744. * @buf: where to get the data from
  745. * @count: bytes sent
  746. * @ppos: where to start
  747. * @format: /smack/cipso or /smack/cipso2
  748. *
  749. * Accepts only one cipso rule per write call.
  750. * Returns number of bytes written or error code, as appropriate
  751. */
  752. static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
  753. size_t count, loff_t *ppos, int format)
  754. {
  755. struct smack_known *skp;
  756. struct netlbl_lsm_secattr ncats;
  757. char mapcatset[SMK_CIPSOLEN];
  758. int maplevel;
  759. unsigned int cat;
  760. int catlen;
  761. ssize_t rc = -EINVAL;
  762. char *data = NULL;
  763. char *rule;
  764. int ret;
  765. int i;
  766. /*
  767. * Must have privilege.
  768. * No partial writes.
  769. * Enough data must be present.
  770. */
  771. if (!smack_privileged(CAP_MAC_ADMIN))
  772. return -EPERM;
  773. if (*ppos != 0)
  774. return -EINVAL;
  775. if (format == SMK_FIXED24_FMT &&
  776. (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
  777. return -EINVAL;
  778. data = memdup_user_nul(buf, count);
  779. if (IS_ERR(data))
  780. return PTR_ERR(data);
  781. rule = data;
  782. /*
  783. * Only allow one writer at a time. Writes should be
  784. * quite rare and small in any case.
  785. */
  786. mutex_lock(&smack_cipso_lock);
  787. skp = smk_import_entry(rule, 0);
  788. if (IS_ERR(skp)) {
  789. rc = PTR_ERR(skp);
  790. goto out;
  791. }
  792. if (format == SMK_FIXED24_FMT)
  793. rule += SMK_LABELLEN;
  794. else
  795. rule += strlen(skp->smk_known) + 1;
  796. ret = sscanf(rule, "%d", &maplevel);
  797. if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
  798. goto out;
  799. rule += SMK_DIGITLEN;
  800. ret = sscanf(rule, "%d", &catlen);
  801. if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
  802. goto out;
  803. if (format == SMK_FIXED24_FMT &&
  804. count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
  805. goto out;
  806. memset(mapcatset, 0, sizeof(mapcatset));
  807. for (i = 0; i < catlen; i++) {
  808. rule += SMK_DIGITLEN;
  809. ret = sscanf(rule, "%u", &cat);
  810. if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
  811. goto out;
  812. smack_catset_bit(cat, mapcatset);
  813. }
  814. rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
  815. if (rc >= 0) {
  816. netlbl_catmap_free(skp->smk_netlabel.attr.mls.cat);
  817. skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
  818. skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
  819. rc = count;
  820. }
  821. out:
  822. mutex_unlock(&smack_cipso_lock);
  823. kfree(data);
  824. return rc;
  825. }
  826. /**
  827. * smk_write_cipso - write() for /smack/cipso
  828. * @file: file pointer, not actually used
  829. * @buf: where to get the data from
  830. * @count: bytes sent
  831. * @ppos: where to start
  832. *
  833. * Accepts only one cipso rule per write call.
  834. * Returns number of bytes written or error code, as appropriate
  835. */
  836. static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
  837. size_t count, loff_t *ppos)
  838. {
  839. return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
  840. }
  841. static const struct file_operations smk_cipso_ops = {
  842. .open = smk_open_cipso,
  843. .read = seq_read,
  844. .llseek = seq_lseek,
  845. .write = smk_write_cipso,
  846. .release = seq_release,
  847. };
  848. /*
  849. * Seq_file read operations for /smack/cipso2
  850. */
  851. /*
  852. * Print cipso labels in format:
  853. * label level[/cat[,cat]]
  854. */
  855. static int cipso2_seq_show(struct seq_file *s, void *v)
  856. {
  857. struct list_head *list = v;
  858. struct smack_known *skp =
  859. list_entry_rcu(list, struct smack_known, list);
  860. struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
  861. char sep = '/';
  862. int i;
  863. seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
  864. for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
  865. i = netlbl_catmap_walk(cmp, i + 1)) {
  866. seq_printf(s, "%c%d", sep, i);
  867. sep = ',';
  868. }
  869. seq_putc(s, '\n');
  870. return 0;
  871. }
  872. static const struct seq_operations cipso2_seq_ops = {
  873. .start = cipso_seq_start,
  874. .next = cipso_seq_next,
  875. .show = cipso2_seq_show,
  876. .stop = smk_seq_stop,
  877. };
  878. /**
  879. * smk_open_cipso2 - open() for /smack/cipso2
  880. * @inode: inode structure representing file
  881. * @file: "cipso2" file pointer
  882. *
  883. * Connect our cipso_seq_* operations with /smack/cipso2
  884. * file_operations
  885. */
  886. static int smk_open_cipso2(struct inode *inode, struct file *file)
  887. {
  888. return seq_open(file, &cipso2_seq_ops);
  889. }
  890. /**
  891. * smk_write_cipso2 - write() for /smack/cipso2
  892. * @file: file pointer, not actually used
  893. * @buf: where to get the data from
  894. * @count: bytes sent
  895. * @ppos: where to start
  896. *
  897. * Accepts only one cipso rule per write call.
  898. * Returns number of bytes written or error code, as appropriate
  899. */
  900. static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
  901. size_t count, loff_t *ppos)
  902. {
  903. return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
  904. }
  905. static const struct file_operations smk_cipso2_ops = {
  906. .open = smk_open_cipso2,
  907. .read = seq_read,
  908. .llseek = seq_lseek,
  909. .write = smk_write_cipso2,
  910. .release = seq_release,
  911. };
  912. /*
  913. * Seq_file read operations for /smack/netlabel
  914. */
  915. static void *net4addr_seq_start(struct seq_file *s, loff_t *pos)
  916. {
  917. return smk_seq_start(s, pos, &smk_net4addr_list);
  918. }
  919. static void *net4addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
  920. {
  921. return smk_seq_next(s, v, pos, &smk_net4addr_list);
  922. }
  923. /*
  924. * Print host/label pairs
  925. */
  926. static int net4addr_seq_show(struct seq_file *s, void *v)
  927. {
  928. struct list_head *list = v;
  929. struct smk_net4addr *skp =
  930. list_entry_rcu(list, struct smk_net4addr, list);
  931. char *kp = SMACK_CIPSO_OPTION;
  932. if (skp->smk_label != NULL)
  933. kp = skp->smk_label->smk_known;
  934. seq_printf(s, "%pI4/%d %s\n", &skp->smk_host.s_addr,
  935. skp->smk_masks, kp);
  936. return 0;
  937. }
  938. static const struct seq_operations net4addr_seq_ops = {
  939. .start = net4addr_seq_start,
  940. .next = net4addr_seq_next,
  941. .show = net4addr_seq_show,
  942. .stop = smk_seq_stop,
  943. };
  944. /**
  945. * smk_open_net4addr - open() for /smack/netlabel
  946. * @inode: inode structure representing file
  947. * @file: "netlabel" file pointer
  948. *
  949. * Connect our net4addr_seq_* operations with /smack/netlabel
  950. * file_operations
  951. */
  952. static int smk_open_net4addr(struct inode *inode, struct file *file)
  953. {
  954. return seq_open(file, &net4addr_seq_ops);
  955. }
  956. /**
  957. * smk_net4addr_insert
  958. * @new : netlabel to insert
  959. *
  960. * This helper insert netlabel in the smack_net4addrs list
  961. * sorted by netmask length (longest to smallest)
  962. * locked by &smk_net4addr_lock in smk_write_net4addr
  963. *
  964. */
  965. static void smk_net4addr_insert(struct smk_net4addr *new)
  966. {
  967. struct smk_net4addr *m;
  968. struct smk_net4addr *m_next;
  969. if (list_empty(&smk_net4addr_list)) {
  970. list_add_rcu(&new->list, &smk_net4addr_list);
  971. return;
  972. }
  973. m = list_entry_rcu(smk_net4addr_list.next,
  974. struct smk_net4addr, list);
  975. /* the comparison '>' is a bit hacky, but works */
  976. if (new->smk_masks > m->smk_masks) {
  977. list_add_rcu(&new->list, &smk_net4addr_list);
  978. return;
  979. }
  980. list_for_each_entry_rcu(m, &smk_net4addr_list, list) {
  981. if (list_is_last(&m->list, &smk_net4addr_list)) {
  982. list_add_rcu(&new->list, &m->list);
  983. return;
  984. }
  985. m_next = list_entry_rcu(m->list.next,
  986. struct smk_net4addr, list);
  987. if (new->smk_masks > m_next->smk_masks) {
  988. list_add_rcu(&new->list, &m->list);
  989. return;
  990. }
  991. }
  992. }
  993. /**
  994. * smk_write_net4addr - write() for /smack/netlabel
  995. * @file: file pointer, not actually used
  996. * @buf: where to get the data from
  997. * @count: bytes sent
  998. * @ppos: where to start
  999. *
  1000. * Accepts only one net4addr per write call.
  1001. * Returns number of bytes written or error code, as appropriate
  1002. */
  1003. static ssize_t smk_write_net4addr(struct file *file, const char __user *buf,
  1004. size_t count, loff_t *ppos)
  1005. {
  1006. struct smk_net4addr *snp;
  1007. struct sockaddr_in newname;
  1008. char *smack;
  1009. struct smack_known *skp = NULL;
  1010. char *data;
  1011. char *host = (char *)&newname.sin_addr.s_addr;
  1012. int rc;
  1013. struct netlbl_audit audit_info;
  1014. struct in_addr mask;
  1015. unsigned int m;
  1016. unsigned int masks;
  1017. int found;
  1018. u32 mask_bits = (1<<31);
  1019. __be32 nsa;
  1020. u32 temp_mask;
  1021. /*
  1022. * Must have privilege.
  1023. * No partial writes.
  1024. * Enough data must be present.
  1025. * "<addr/mask, as a.b.c.d/e><space><label>"
  1026. * "<addr, as a.b.c.d><space><label>"
  1027. */
  1028. if (!smack_privileged(CAP_MAC_ADMIN))
  1029. return -EPERM;
  1030. if (*ppos != 0)
  1031. return -EINVAL;
  1032. if (count < SMK_NETLBLADDRMIN)
  1033. return -EINVAL;
  1034. data = memdup_user_nul(buf, count);
  1035. if (IS_ERR(data))
  1036. return PTR_ERR(data);
  1037. smack = kzalloc(count + 1, GFP_KERNEL);
  1038. if (smack == NULL) {
  1039. rc = -ENOMEM;
  1040. goto free_data_out;
  1041. }
  1042. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
  1043. &host[0], &host[1], &host[2], &host[3], &masks, smack);
  1044. if (rc != 6) {
  1045. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
  1046. &host[0], &host[1], &host[2], &host[3], smack);
  1047. if (rc != 5) {
  1048. rc = -EINVAL;
  1049. goto free_out;
  1050. }
  1051. m = BEBITS;
  1052. masks = 32;
  1053. }
  1054. if (masks > BEBITS) {
  1055. rc = -EINVAL;
  1056. goto free_out;
  1057. }
  1058. /*
  1059. * If smack begins with '-', it is an option, don't import it
  1060. */
  1061. if (smack[0] != '-') {
  1062. skp = smk_import_entry(smack, 0);
  1063. if (IS_ERR(skp)) {
  1064. rc = PTR_ERR(skp);
  1065. goto free_out;
  1066. }
  1067. } else {
  1068. /*
  1069. * Only the -CIPSO option is supported for IPv4
  1070. */
  1071. if (strcmp(smack, SMACK_CIPSO_OPTION) != 0) {
  1072. rc = -EINVAL;
  1073. goto free_out;
  1074. }
  1075. }
  1076. for (m = masks, temp_mask = 0; m > 0; m--) {
  1077. temp_mask |= mask_bits;
  1078. mask_bits >>= 1;
  1079. }
  1080. mask.s_addr = cpu_to_be32(temp_mask);
  1081. newname.sin_addr.s_addr &= mask.s_addr;
  1082. /*
  1083. * Only allow one writer at a time. Writes should be
  1084. * quite rare and small in any case.
  1085. */
  1086. mutex_lock(&smk_net4addr_lock);
  1087. nsa = newname.sin_addr.s_addr;
  1088. /* try to find if the prefix is already in the list */
  1089. found = 0;
  1090. list_for_each_entry_rcu(snp, &smk_net4addr_list, list) {
  1091. if (snp->smk_host.s_addr == nsa && snp->smk_masks == masks) {
  1092. found = 1;
  1093. break;
  1094. }
  1095. }
  1096. smk_netlabel_audit_set(&audit_info);
  1097. if (found == 0) {
  1098. snp = kzalloc(sizeof(*snp), GFP_KERNEL);
  1099. if (snp == NULL)
  1100. rc = -ENOMEM;
  1101. else {
  1102. rc = 0;
  1103. snp->smk_host.s_addr = newname.sin_addr.s_addr;
  1104. snp->smk_mask.s_addr = mask.s_addr;
  1105. snp->smk_label = skp;
  1106. snp->smk_masks = masks;
  1107. smk_net4addr_insert(snp);
  1108. }
  1109. } else {
  1110. /*
  1111. * Delete the unlabeled entry, only if the previous label
  1112. * wasn't the special CIPSO option
  1113. */
  1114. if (snp->smk_label != NULL)
  1115. rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
  1116. &snp->smk_host, &snp->smk_mask,
  1117. PF_INET, &audit_info);
  1118. else
  1119. rc = 0;
  1120. snp->smk_label = skp;
  1121. }
  1122. /*
  1123. * Now tell netlabel about the single label nature of
  1124. * this host so that incoming packets get labeled.
  1125. * but only if we didn't get the special CIPSO option
  1126. */
  1127. if (rc == 0 && skp != NULL)
  1128. rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
  1129. &snp->smk_host, &snp->smk_mask, PF_INET,
  1130. snp->smk_label->smk_secid, &audit_info);
  1131. if (rc == 0)
  1132. rc = count;
  1133. mutex_unlock(&smk_net4addr_lock);
  1134. free_out:
  1135. kfree(smack);
  1136. free_data_out:
  1137. kfree(data);
  1138. return rc;
  1139. }
  1140. static const struct file_operations smk_net4addr_ops = {
  1141. .open = smk_open_net4addr,
  1142. .read = seq_read,
  1143. .llseek = seq_lseek,
  1144. .write = smk_write_net4addr,
  1145. .release = seq_release,
  1146. };
  1147. #if IS_ENABLED(CONFIG_IPV6)
  1148. /*
  1149. * Seq_file read operations for /smack/netlabel6
  1150. */
  1151. static void *net6addr_seq_start(struct seq_file *s, loff_t *pos)
  1152. {
  1153. return smk_seq_start(s, pos, &smk_net6addr_list);
  1154. }
  1155. static void *net6addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1156. {
  1157. return smk_seq_next(s, v, pos, &smk_net6addr_list);
  1158. }
  1159. /*
  1160. * Print host/label pairs
  1161. */
  1162. static int net6addr_seq_show(struct seq_file *s, void *v)
  1163. {
  1164. struct list_head *list = v;
  1165. struct smk_net6addr *skp =
  1166. list_entry(list, struct smk_net6addr, list);
  1167. if (skp->smk_label != NULL)
  1168. seq_printf(s, "%pI6/%d %s\n", &skp->smk_host, skp->smk_masks,
  1169. skp->smk_label->smk_known);
  1170. return 0;
  1171. }
  1172. static const struct seq_operations net6addr_seq_ops = {
  1173. .start = net6addr_seq_start,
  1174. .next = net6addr_seq_next,
  1175. .show = net6addr_seq_show,
  1176. .stop = smk_seq_stop,
  1177. };
  1178. /**
  1179. * smk_open_net6addr - open() for /smack/netlabel
  1180. * @inode: inode structure representing file
  1181. * @file: "netlabel" file pointer
  1182. *
  1183. * Connect our net6addr_seq_* operations with /smack/netlabel
  1184. * file_operations
  1185. */
  1186. static int smk_open_net6addr(struct inode *inode, struct file *file)
  1187. {
  1188. return seq_open(file, &net6addr_seq_ops);
  1189. }
  1190. /**
  1191. * smk_net6addr_insert
  1192. * @new : entry to insert
  1193. *
  1194. * This inserts an entry in the smack_net6addrs list
  1195. * sorted by netmask length (longest to smallest)
  1196. * locked by &smk_net6addr_lock in smk_write_net6addr
  1197. *
  1198. */
  1199. static void smk_net6addr_insert(struct smk_net6addr *new)
  1200. {
  1201. struct smk_net6addr *m_next;
  1202. struct smk_net6addr *m;
  1203. if (list_empty(&smk_net6addr_list)) {
  1204. list_add_rcu(&new->list, &smk_net6addr_list);
  1205. return;
  1206. }
  1207. m = list_entry_rcu(smk_net6addr_list.next,
  1208. struct smk_net6addr, list);
  1209. if (new->smk_masks > m->smk_masks) {
  1210. list_add_rcu(&new->list, &smk_net6addr_list);
  1211. return;
  1212. }
  1213. list_for_each_entry_rcu(m, &smk_net6addr_list, list) {
  1214. if (list_is_last(&m->list, &smk_net6addr_list)) {
  1215. list_add_rcu(&new->list, &m->list);
  1216. return;
  1217. }
  1218. m_next = list_entry_rcu(m->list.next,
  1219. struct smk_net6addr, list);
  1220. if (new->smk_masks > m_next->smk_masks) {
  1221. list_add_rcu(&new->list, &m->list);
  1222. return;
  1223. }
  1224. }
  1225. }
  1226. /**
  1227. * smk_write_net6addr - write() for /smack/netlabel
  1228. * @file: file pointer, not actually used
  1229. * @buf: where to get the data from
  1230. * @count: bytes sent
  1231. * @ppos: where to start
  1232. *
  1233. * Accepts only one net6addr per write call.
  1234. * Returns number of bytes written or error code, as appropriate
  1235. */
  1236. static ssize_t smk_write_net6addr(struct file *file, const char __user *buf,
  1237. size_t count, loff_t *ppos)
  1238. {
  1239. struct smk_net6addr *snp;
  1240. struct in6_addr newname;
  1241. struct in6_addr fullmask;
  1242. struct smack_known *skp = NULL;
  1243. char *smack;
  1244. char *data;
  1245. int rc = 0;
  1246. int found = 0;
  1247. int i;
  1248. unsigned int scanned[8];
  1249. unsigned int m;
  1250. unsigned int mask = 128;
  1251. /*
  1252. * Must have privilege.
  1253. * No partial writes.
  1254. * Enough data must be present.
  1255. * "<addr/mask, as a:b:c:d:e:f:g:h/e><space><label>"
  1256. * "<addr, as a:b:c:d:e:f:g:h><space><label>"
  1257. */
  1258. if (!smack_privileged(CAP_MAC_ADMIN))
  1259. return -EPERM;
  1260. if (*ppos != 0)
  1261. return -EINVAL;
  1262. if (count < SMK_NETLBLADDRMIN)
  1263. return -EINVAL;
  1264. data = memdup_user_nul(buf, count);
  1265. if (IS_ERR(data))
  1266. return PTR_ERR(data);
  1267. smack = kzalloc(count + 1, GFP_KERNEL);
  1268. if (smack == NULL) {
  1269. rc = -ENOMEM;
  1270. goto free_data_out;
  1271. }
  1272. i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x/%u %s",
  1273. &scanned[0], &scanned[1], &scanned[2], &scanned[3],
  1274. &scanned[4], &scanned[5], &scanned[6], &scanned[7],
  1275. &mask, smack);
  1276. if (i != 10) {
  1277. i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x %s",
  1278. &scanned[0], &scanned[1], &scanned[2],
  1279. &scanned[3], &scanned[4], &scanned[5],
  1280. &scanned[6], &scanned[7], smack);
  1281. if (i != 9) {
  1282. rc = -EINVAL;
  1283. goto free_out;
  1284. }
  1285. }
  1286. if (mask > 128) {
  1287. rc = -EINVAL;
  1288. goto free_out;
  1289. }
  1290. for (i = 0; i < 8; i++) {
  1291. if (scanned[i] > 0xffff) {
  1292. rc = -EINVAL;
  1293. goto free_out;
  1294. }
  1295. newname.s6_addr16[i] = htons(scanned[i]);
  1296. }
  1297. /*
  1298. * If smack begins with '-', it is an option, don't import it
  1299. */
  1300. if (smack[0] != '-') {
  1301. skp = smk_import_entry(smack, 0);
  1302. if (IS_ERR(skp)) {
  1303. rc = PTR_ERR(skp);
  1304. goto free_out;
  1305. }
  1306. } else {
  1307. /*
  1308. * Only -DELETE is supported for IPv6
  1309. */
  1310. if (strcmp(smack, SMACK_DELETE_OPTION) != 0) {
  1311. rc = -EINVAL;
  1312. goto free_out;
  1313. }
  1314. }
  1315. for (i = 0, m = mask; i < 8; i++) {
  1316. if (m >= 16) {
  1317. fullmask.s6_addr16[i] = 0xffff;
  1318. m -= 16;
  1319. } else if (m > 0) {
  1320. fullmask.s6_addr16[i] = (1 << m) - 1;
  1321. m = 0;
  1322. } else
  1323. fullmask.s6_addr16[i] = 0;
  1324. newname.s6_addr16[i] &= fullmask.s6_addr16[i];
  1325. }
  1326. /*
  1327. * Only allow one writer at a time. Writes should be
  1328. * quite rare and small in any case.
  1329. */
  1330. mutex_lock(&smk_net6addr_lock);
  1331. /*
  1332. * Try to find the prefix in the list
  1333. */
  1334. list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
  1335. if (mask != snp->smk_masks)
  1336. continue;
  1337. for (found = 1, i = 0; i < 8; i++) {
  1338. if (newname.s6_addr16[i] !=
  1339. snp->smk_host.s6_addr16[i]) {
  1340. found = 0;
  1341. break;
  1342. }
  1343. }
  1344. if (found == 1)
  1345. break;
  1346. }
  1347. if (found == 0) {
  1348. snp = kzalloc(sizeof(*snp), GFP_KERNEL);
  1349. if (snp == NULL)
  1350. rc = -ENOMEM;
  1351. else {
  1352. snp->smk_host = newname;
  1353. snp->smk_mask = fullmask;
  1354. snp->smk_masks = mask;
  1355. snp->smk_label = skp;
  1356. smk_net6addr_insert(snp);
  1357. }
  1358. } else {
  1359. snp->smk_label = skp;
  1360. }
  1361. if (rc == 0)
  1362. rc = count;
  1363. mutex_unlock(&smk_net6addr_lock);
  1364. free_out:
  1365. kfree(smack);
  1366. free_data_out:
  1367. kfree(data);
  1368. return rc;
  1369. }
  1370. static const struct file_operations smk_net6addr_ops = {
  1371. .open = smk_open_net6addr,
  1372. .read = seq_read,
  1373. .llseek = seq_lseek,
  1374. .write = smk_write_net6addr,
  1375. .release = seq_release,
  1376. };
  1377. #endif /* CONFIG_IPV6 */
  1378. /**
  1379. * smk_read_doi - read() for /smack/doi
  1380. * @filp: file pointer, not actually used
  1381. * @buf: where to put the result
  1382. * @count: maximum to send along
  1383. * @ppos: where to start
  1384. *
  1385. * Returns number of bytes read or error code, as appropriate
  1386. */
  1387. static ssize_t smk_read_doi(struct file *filp, char __user *buf,
  1388. size_t count, loff_t *ppos)
  1389. {
  1390. char temp[80];
  1391. ssize_t rc;
  1392. if (*ppos != 0)
  1393. return 0;
  1394. sprintf(temp, "%d", smk_cipso_doi_value);
  1395. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1396. return rc;
  1397. }
  1398. /**
  1399. * smk_write_doi - write() for /smack/doi
  1400. * @file: file pointer, not actually used
  1401. * @buf: where to get the data from
  1402. * @count: bytes sent
  1403. * @ppos: where to start
  1404. *
  1405. * Returns number of bytes written or error code, as appropriate
  1406. */
  1407. static ssize_t smk_write_doi(struct file *file, const char __user *buf,
  1408. size_t count, loff_t *ppos)
  1409. {
  1410. char temp[80];
  1411. int i;
  1412. if (!smack_privileged(CAP_MAC_ADMIN))
  1413. return -EPERM;
  1414. if (count >= sizeof(temp) || count == 0)
  1415. return -EINVAL;
  1416. if (copy_from_user(temp, buf, count) != 0)
  1417. return -EFAULT;
  1418. temp[count] = '\0';
  1419. if (sscanf(temp, "%d", &i) != 1)
  1420. return -EINVAL;
  1421. smk_cipso_doi_value = i;
  1422. smk_cipso_doi();
  1423. return count;
  1424. }
  1425. static const struct file_operations smk_doi_ops = {
  1426. .read = smk_read_doi,
  1427. .write = smk_write_doi,
  1428. .llseek = default_llseek,
  1429. };
  1430. /**
  1431. * smk_read_direct - read() for /smack/direct
  1432. * @filp: file pointer, not actually used
  1433. * @buf: where to put the result
  1434. * @count: maximum to send along
  1435. * @ppos: where to start
  1436. *
  1437. * Returns number of bytes read or error code, as appropriate
  1438. */
  1439. static ssize_t smk_read_direct(struct file *filp, char __user *buf,
  1440. size_t count, loff_t *ppos)
  1441. {
  1442. char temp[80];
  1443. ssize_t rc;
  1444. if (*ppos != 0)
  1445. return 0;
  1446. sprintf(temp, "%d", smack_cipso_direct);
  1447. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1448. return rc;
  1449. }
  1450. /**
  1451. * smk_write_direct - write() for /smack/direct
  1452. * @file: file pointer, not actually used
  1453. * @buf: where to get the data from
  1454. * @count: bytes sent
  1455. * @ppos: where to start
  1456. *
  1457. * Returns number of bytes written or error code, as appropriate
  1458. */
  1459. static ssize_t smk_write_direct(struct file *file, const char __user *buf,
  1460. size_t count, loff_t *ppos)
  1461. {
  1462. struct smack_known *skp;
  1463. char temp[80];
  1464. int i;
  1465. if (!smack_privileged(CAP_MAC_ADMIN))
  1466. return -EPERM;
  1467. if (count >= sizeof(temp) || count == 0)
  1468. return -EINVAL;
  1469. if (copy_from_user(temp, buf, count) != 0)
  1470. return -EFAULT;
  1471. temp[count] = '\0';
  1472. if (sscanf(temp, "%d", &i) != 1)
  1473. return -EINVAL;
  1474. /*
  1475. * Don't do anything if the value hasn't actually changed.
  1476. * If it is changing reset the level on entries that were
  1477. * set up to be direct when they were created.
  1478. */
  1479. if (smack_cipso_direct != i) {
  1480. mutex_lock(&smack_known_lock);
  1481. list_for_each_entry_rcu(skp, &smack_known_list, list)
  1482. if (skp->smk_netlabel.attr.mls.lvl ==
  1483. smack_cipso_direct)
  1484. skp->smk_netlabel.attr.mls.lvl = i;
  1485. smack_cipso_direct = i;
  1486. mutex_unlock(&smack_known_lock);
  1487. }
  1488. return count;
  1489. }
  1490. static const struct file_operations smk_direct_ops = {
  1491. .read = smk_read_direct,
  1492. .write = smk_write_direct,
  1493. .llseek = default_llseek,
  1494. };
  1495. /**
  1496. * smk_read_mapped - read() for /smack/mapped
  1497. * @filp: file pointer, not actually used
  1498. * @buf: where to put the result
  1499. * @count: maximum to send along
  1500. * @ppos: where to start
  1501. *
  1502. * Returns number of bytes read or error code, as appropriate
  1503. */
  1504. static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
  1505. size_t count, loff_t *ppos)
  1506. {
  1507. char temp[80];
  1508. ssize_t rc;
  1509. if (*ppos != 0)
  1510. return 0;
  1511. sprintf(temp, "%d", smack_cipso_mapped);
  1512. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1513. return rc;
  1514. }
  1515. /**
  1516. * smk_write_mapped - write() for /smack/mapped
  1517. * @file: file pointer, not actually used
  1518. * @buf: where to get the data from
  1519. * @count: bytes sent
  1520. * @ppos: where to start
  1521. *
  1522. * Returns number of bytes written or error code, as appropriate
  1523. */
  1524. static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
  1525. size_t count, loff_t *ppos)
  1526. {
  1527. struct smack_known *skp;
  1528. char temp[80];
  1529. int i;
  1530. if (!smack_privileged(CAP_MAC_ADMIN))
  1531. return -EPERM;
  1532. if (count >= sizeof(temp) || count == 0)
  1533. return -EINVAL;
  1534. if (copy_from_user(temp, buf, count) != 0)
  1535. return -EFAULT;
  1536. temp[count] = '\0';
  1537. if (sscanf(temp, "%d", &i) != 1)
  1538. return -EINVAL;
  1539. /*
  1540. * Don't do anything if the value hasn't actually changed.
  1541. * If it is changing reset the level on entries that were
  1542. * set up to be mapped when they were created.
  1543. */
  1544. if (smack_cipso_mapped != i) {
  1545. mutex_lock(&smack_known_lock);
  1546. list_for_each_entry_rcu(skp, &smack_known_list, list)
  1547. if (skp->smk_netlabel.attr.mls.lvl ==
  1548. smack_cipso_mapped)
  1549. skp->smk_netlabel.attr.mls.lvl = i;
  1550. smack_cipso_mapped = i;
  1551. mutex_unlock(&smack_known_lock);
  1552. }
  1553. return count;
  1554. }
  1555. static const struct file_operations smk_mapped_ops = {
  1556. .read = smk_read_mapped,
  1557. .write = smk_write_mapped,
  1558. .llseek = default_llseek,
  1559. };
  1560. /**
  1561. * smk_read_ambient - read() for /smack/ambient
  1562. * @filp: file pointer, not actually used
  1563. * @buf: where to put the result
  1564. * @cn: maximum to send along
  1565. * @ppos: where to start
  1566. *
  1567. * Returns number of bytes read or error code, as appropriate
  1568. */
  1569. static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
  1570. size_t cn, loff_t *ppos)
  1571. {
  1572. ssize_t rc;
  1573. int asize;
  1574. if (*ppos != 0)
  1575. return 0;
  1576. /*
  1577. * Being careful to avoid a problem in the case where
  1578. * smack_net_ambient gets changed in midstream.
  1579. */
  1580. mutex_lock(&smack_ambient_lock);
  1581. asize = strlen(smack_net_ambient->smk_known) + 1;
  1582. if (cn >= asize)
  1583. rc = simple_read_from_buffer(buf, cn, ppos,
  1584. smack_net_ambient->smk_known,
  1585. asize);
  1586. else
  1587. rc = -EINVAL;
  1588. mutex_unlock(&smack_ambient_lock);
  1589. return rc;
  1590. }
  1591. /**
  1592. * smk_write_ambient - write() for /smack/ambient
  1593. * @file: file pointer, not actually used
  1594. * @buf: where to get the data from
  1595. * @count: bytes sent
  1596. * @ppos: where to start
  1597. *
  1598. * Returns number of bytes written or error code, as appropriate
  1599. */
  1600. static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
  1601. size_t count, loff_t *ppos)
  1602. {
  1603. struct smack_known *skp;
  1604. char *oldambient;
  1605. char *data;
  1606. int rc = count;
  1607. if (!smack_privileged(CAP_MAC_ADMIN))
  1608. return -EPERM;
  1609. data = memdup_user_nul(buf, count);
  1610. if (IS_ERR(data))
  1611. return PTR_ERR(data);
  1612. skp = smk_import_entry(data, count);
  1613. if (IS_ERR(skp)) {
  1614. rc = PTR_ERR(skp);
  1615. goto out;
  1616. }
  1617. mutex_lock(&smack_ambient_lock);
  1618. oldambient = smack_net_ambient->smk_known;
  1619. smack_net_ambient = skp;
  1620. smk_unlbl_ambient(oldambient);
  1621. mutex_unlock(&smack_ambient_lock);
  1622. out:
  1623. kfree(data);
  1624. return rc;
  1625. }
  1626. static const struct file_operations smk_ambient_ops = {
  1627. .read = smk_read_ambient,
  1628. .write = smk_write_ambient,
  1629. .llseek = default_llseek,
  1630. };
  1631. /*
  1632. * Seq_file operations for /smack/onlycap
  1633. */
  1634. static void *onlycap_seq_start(struct seq_file *s, loff_t *pos)
  1635. {
  1636. return smk_seq_start(s, pos, &smack_onlycap_list);
  1637. }
  1638. static void *onlycap_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1639. {
  1640. return smk_seq_next(s, v, pos, &smack_onlycap_list);
  1641. }
  1642. static int onlycap_seq_show(struct seq_file *s, void *v)
  1643. {
  1644. struct list_head *list = v;
  1645. struct smack_known_list_elem *sklep =
  1646. list_entry_rcu(list, struct smack_known_list_elem, list);
  1647. seq_puts(s, sklep->smk_label->smk_known);
  1648. seq_putc(s, ' ');
  1649. return 0;
  1650. }
  1651. static const struct seq_operations onlycap_seq_ops = {
  1652. .start = onlycap_seq_start,
  1653. .next = onlycap_seq_next,
  1654. .show = onlycap_seq_show,
  1655. .stop = smk_seq_stop,
  1656. };
  1657. static int smk_open_onlycap(struct inode *inode, struct file *file)
  1658. {
  1659. return seq_open(file, &onlycap_seq_ops);
  1660. }
  1661. /**
  1662. * smk_list_swap_rcu - swap public list with a private one in RCU-safe way
  1663. * The caller must hold appropriate mutex to prevent concurrent modifications
  1664. * to the public list.
  1665. * Private list is assumed to be not accessible to other threads yet.
  1666. *
  1667. * @public: public list
  1668. * @private: private list
  1669. */
  1670. static void smk_list_swap_rcu(struct list_head *public,
  1671. struct list_head *private)
  1672. {
  1673. struct list_head *first, *last;
  1674. if (list_empty(public)) {
  1675. list_splice_init_rcu(private, public, synchronize_rcu);
  1676. } else {
  1677. /* Remember public list before replacing it */
  1678. first = public->next;
  1679. last = public->prev;
  1680. /* Publish private list in place of public in RCU-safe way */
  1681. private->prev->next = public;
  1682. private->next->prev = public;
  1683. rcu_assign_pointer(public->next, private->next);
  1684. public->prev = private->prev;
  1685. synchronize_rcu();
  1686. /* When all readers are done with the old public list,
  1687. * attach it in place of private */
  1688. private->next = first;
  1689. private->prev = last;
  1690. first->prev = private;
  1691. last->next = private;
  1692. }
  1693. }
  1694. /**
  1695. * smk_parse_label_list - parse list of Smack labels, separated by spaces
  1696. *
  1697. * @data: the string to parse
  1698. * @private: destination list
  1699. *
  1700. * Returns zero on success or error code, as appropriate
  1701. */
  1702. static int smk_parse_label_list(char *data, struct list_head *list)
  1703. {
  1704. char *tok;
  1705. struct smack_known *skp;
  1706. struct smack_known_list_elem *sklep;
  1707. while ((tok = strsep(&data, " ")) != NULL) {
  1708. if (!*tok)
  1709. continue;
  1710. skp = smk_import_entry(tok, 0);
  1711. if (IS_ERR(skp))
  1712. return PTR_ERR(skp);
  1713. sklep = kzalloc(sizeof(*sklep), GFP_KERNEL);
  1714. if (sklep == NULL)
  1715. return -ENOMEM;
  1716. sklep->smk_label = skp;
  1717. list_add(&sklep->list, list);
  1718. }
  1719. return 0;
  1720. }
  1721. /**
  1722. * smk_destroy_label_list - destroy a list of smack_known_list_elem
  1723. * @head: header pointer of the list to destroy
  1724. */
  1725. void smk_destroy_label_list(struct list_head *list)
  1726. {
  1727. struct smack_known_list_elem *sklep;
  1728. struct smack_known_list_elem *sklep2;
  1729. list_for_each_entry_safe(sklep, sklep2, list, list)
  1730. kfree(sklep);
  1731. INIT_LIST_HEAD(list);
  1732. }
  1733. /**
  1734. * smk_write_onlycap - write() for smackfs/onlycap
  1735. * @file: file pointer, not actually used
  1736. * @buf: where to get the data from
  1737. * @count: bytes sent
  1738. * @ppos: where to start
  1739. *
  1740. * Returns number of bytes written or error code, as appropriate
  1741. */
  1742. static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
  1743. size_t count, loff_t *ppos)
  1744. {
  1745. char *data;
  1746. LIST_HEAD(list_tmp);
  1747. int rc;
  1748. if (!smack_privileged(CAP_MAC_ADMIN))
  1749. return -EPERM;
  1750. data = memdup_user_nul(buf, count);
  1751. if (IS_ERR(data))
  1752. return PTR_ERR(data);
  1753. rc = smk_parse_label_list(data, &list_tmp);
  1754. kfree(data);
  1755. /*
  1756. * Clear the smack_onlycap on invalid label errors. This means
  1757. * that we can pass a null string to unset the onlycap value.
  1758. *
  1759. * Importing will also reject a label beginning with '-',
  1760. * so "-usecapabilities" will also work.
  1761. *
  1762. * But do so only on invalid label, not on system errors.
  1763. * The invalid label must be first to count as clearing attempt.
  1764. */
  1765. if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
  1766. mutex_lock(&smack_onlycap_lock);
  1767. smk_list_swap_rcu(&smack_onlycap_list, &list_tmp);
  1768. mutex_unlock(&smack_onlycap_lock);
  1769. rc = count;
  1770. }
  1771. smk_destroy_label_list(&list_tmp);
  1772. return rc;
  1773. }
  1774. static const struct file_operations smk_onlycap_ops = {
  1775. .open = smk_open_onlycap,
  1776. .read = seq_read,
  1777. .write = smk_write_onlycap,
  1778. .llseek = seq_lseek,
  1779. .release = seq_release,
  1780. };
  1781. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  1782. /**
  1783. * smk_read_unconfined - read() for smackfs/unconfined
  1784. * @filp: file pointer, not actually used
  1785. * @buf: where to put the result
  1786. * @cn: maximum to send along
  1787. * @ppos: where to start
  1788. *
  1789. * Returns number of bytes read or error code, as appropriate
  1790. */
  1791. static ssize_t smk_read_unconfined(struct file *filp, char __user *buf,
  1792. size_t cn, loff_t *ppos)
  1793. {
  1794. char *smack = "";
  1795. ssize_t rc = -EINVAL;
  1796. int asize;
  1797. if (*ppos != 0)
  1798. return 0;
  1799. if (smack_unconfined != NULL)
  1800. smack = smack_unconfined->smk_known;
  1801. asize = strlen(smack) + 1;
  1802. if (cn >= asize)
  1803. rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
  1804. return rc;
  1805. }
  1806. /**
  1807. * smk_write_unconfined - write() for smackfs/unconfined
  1808. * @file: file pointer, not actually used
  1809. * @buf: where to get the data from
  1810. * @count: bytes sent
  1811. * @ppos: where to start
  1812. *
  1813. * Returns number of bytes written or error code, as appropriate
  1814. */
  1815. static ssize_t smk_write_unconfined(struct file *file, const char __user *buf,
  1816. size_t count, loff_t *ppos)
  1817. {
  1818. char *data;
  1819. struct smack_known *skp;
  1820. int rc = count;
  1821. if (!smack_privileged(CAP_MAC_ADMIN))
  1822. return -EPERM;
  1823. data = memdup_user_nul(buf, count);
  1824. if (IS_ERR(data))
  1825. return PTR_ERR(data);
  1826. /*
  1827. * Clear the smack_unconfined on invalid label errors. This means
  1828. * that we can pass a null string to unset the unconfined value.
  1829. *
  1830. * Importing will also reject a label beginning with '-',
  1831. * so "-confine" will also work.
  1832. *
  1833. * But do so only on invalid label, not on system errors.
  1834. */
  1835. skp = smk_import_entry(data, count);
  1836. if (PTR_ERR(skp) == -EINVAL)
  1837. skp = NULL;
  1838. else if (IS_ERR(skp)) {
  1839. rc = PTR_ERR(skp);
  1840. goto freeout;
  1841. }
  1842. smack_unconfined = skp;
  1843. freeout:
  1844. kfree(data);
  1845. return rc;
  1846. }
  1847. static const struct file_operations smk_unconfined_ops = {
  1848. .read = smk_read_unconfined,
  1849. .write = smk_write_unconfined,
  1850. .llseek = default_llseek,
  1851. };
  1852. #endif /* CONFIG_SECURITY_SMACK_BRINGUP */
  1853. /**
  1854. * smk_read_logging - read() for /smack/logging
  1855. * @filp: file pointer, not actually used
  1856. * @buf: where to put the result
  1857. * @cn: maximum to send along
  1858. * @ppos: where to start
  1859. *
  1860. * Returns number of bytes read or error code, as appropriate
  1861. */
  1862. static ssize_t smk_read_logging(struct file *filp, char __user *buf,
  1863. size_t count, loff_t *ppos)
  1864. {
  1865. char temp[32];
  1866. ssize_t rc;
  1867. if (*ppos != 0)
  1868. return 0;
  1869. sprintf(temp, "%d\n", log_policy);
  1870. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1871. return rc;
  1872. }
  1873. /**
  1874. * smk_write_logging - write() for /smack/logging
  1875. * @file: file pointer, not actually used
  1876. * @buf: where to get the data from
  1877. * @count: bytes sent
  1878. * @ppos: where to start
  1879. *
  1880. * Returns number of bytes written or error code, as appropriate
  1881. */
  1882. static ssize_t smk_write_logging(struct file *file, const char __user *buf,
  1883. size_t count, loff_t *ppos)
  1884. {
  1885. char temp[32];
  1886. int i;
  1887. if (!smack_privileged(CAP_MAC_ADMIN))
  1888. return -EPERM;
  1889. if (count >= sizeof(temp) || count == 0)
  1890. return -EINVAL;
  1891. if (copy_from_user(temp, buf, count) != 0)
  1892. return -EFAULT;
  1893. temp[count] = '\0';
  1894. if (sscanf(temp, "%d", &i) != 1)
  1895. return -EINVAL;
  1896. if (i < 0 || i > 3)
  1897. return -EINVAL;
  1898. log_policy = i;
  1899. return count;
  1900. }
  1901. static const struct file_operations smk_logging_ops = {
  1902. .read = smk_read_logging,
  1903. .write = smk_write_logging,
  1904. .llseek = default_llseek,
  1905. };
  1906. /*
  1907. * Seq_file read operations for /smack/load-self
  1908. */
  1909. static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
  1910. {
  1911. struct task_smack *tsp = current_security();
  1912. return smk_seq_start(s, pos, &tsp->smk_rules);
  1913. }
  1914. static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1915. {
  1916. struct task_smack *tsp = current_security();
  1917. return smk_seq_next(s, v, pos, &tsp->smk_rules);
  1918. }
  1919. static int load_self_seq_show(struct seq_file *s, void *v)
  1920. {
  1921. struct list_head *list = v;
  1922. struct smack_rule *srp =
  1923. list_entry_rcu(list, struct smack_rule, list);
  1924. smk_rule_show(s, srp, SMK_LABELLEN);
  1925. return 0;
  1926. }
  1927. static const struct seq_operations load_self_seq_ops = {
  1928. .start = load_self_seq_start,
  1929. .next = load_self_seq_next,
  1930. .show = load_self_seq_show,
  1931. .stop = smk_seq_stop,
  1932. };
  1933. /**
  1934. * smk_open_load_self - open() for /smack/load-self2
  1935. * @inode: inode structure representing file
  1936. * @file: "load" file pointer
  1937. *
  1938. * For reading, use load_seq_* seq_file reading operations.
  1939. */
  1940. static int smk_open_load_self(struct inode *inode, struct file *file)
  1941. {
  1942. return seq_open(file, &load_self_seq_ops);
  1943. }
  1944. /**
  1945. * smk_write_load_self - write() for /smack/load-self
  1946. * @file: file pointer, not actually used
  1947. * @buf: where to get the data from
  1948. * @count: bytes sent
  1949. * @ppos: where to start - must be 0
  1950. *
  1951. */
  1952. static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
  1953. size_t count, loff_t *ppos)
  1954. {
  1955. struct task_smack *tsp = current_security();
  1956. return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
  1957. &tsp->smk_rules_lock, SMK_FIXED24_FMT);
  1958. }
  1959. static const struct file_operations smk_load_self_ops = {
  1960. .open = smk_open_load_self,
  1961. .read = seq_read,
  1962. .llseek = seq_lseek,
  1963. .write = smk_write_load_self,
  1964. .release = seq_release,
  1965. };
  1966. /**
  1967. * smk_user_access - handle access check transaction
  1968. * @file: file pointer
  1969. * @buf: data from user space
  1970. * @count: bytes sent
  1971. * @ppos: where to start - must be 0
  1972. */
  1973. static ssize_t smk_user_access(struct file *file, const char __user *buf,
  1974. size_t count, loff_t *ppos, int format)
  1975. {
  1976. struct smack_parsed_rule rule;
  1977. char *data;
  1978. int res;
  1979. data = simple_transaction_get(file, buf, count);
  1980. if (IS_ERR(data))
  1981. return PTR_ERR(data);
  1982. if (format == SMK_FIXED24_FMT) {
  1983. if (count < SMK_LOADLEN)
  1984. return -EINVAL;
  1985. res = smk_parse_rule(data, &rule, 0);
  1986. } else {
  1987. /*
  1988. * simple_transaction_get() returns null-terminated data
  1989. */
  1990. res = smk_parse_long_rule(data, &rule, 0, 3);
  1991. }
  1992. if (res >= 0)
  1993. res = smk_access(rule.smk_subject, rule.smk_object,
  1994. rule.smk_access1, NULL);
  1995. else if (res != -ENOENT)
  1996. return res;
  1997. /*
  1998. * smk_access() can return a value > 0 in the "bringup" case.
  1999. */
  2000. data[0] = res >= 0 ? '1' : '0';
  2001. data[1] = '\0';
  2002. simple_transaction_set(file, 2);
  2003. if (format == SMK_FIXED24_FMT)
  2004. return SMK_LOADLEN;
  2005. return count;
  2006. }
  2007. /**
  2008. * smk_write_access - handle access check transaction
  2009. * @file: file pointer
  2010. * @buf: data from user space
  2011. * @count: bytes sent
  2012. * @ppos: where to start - must be 0
  2013. */
  2014. static ssize_t smk_write_access(struct file *file, const char __user *buf,
  2015. size_t count, loff_t *ppos)
  2016. {
  2017. return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
  2018. }
  2019. static const struct file_operations smk_access_ops = {
  2020. .write = smk_write_access,
  2021. .read = simple_transaction_read,
  2022. .release = simple_transaction_release,
  2023. .llseek = generic_file_llseek,
  2024. };
  2025. /*
  2026. * Seq_file read operations for /smack/load2
  2027. */
  2028. static int load2_seq_show(struct seq_file *s, void *v)
  2029. {
  2030. struct list_head *list = v;
  2031. struct smack_master_list *smlp =
  2032. list_entry_rcu(list, struct smack_master_list, list);
  2033. smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
  2034. return 0;
  2035. }
  2036. static const struct seq_operations load2_seq_ops = {
  2037. .start = load2_seq_start,
  2038. .next = load2_seq_next,
  2039. .show = load2_seq_show,
  2040. .stop = smk_seq_stop,
  2041. };
  2042. /**
  2043. * smk_open_load2 - open() for /smack/load2
  2044. * @inode: inode structure representing file
  2045. * @file: "load2" file pointer
  2046. *
  2047. * For reading, use load2_seq_* seq_file reading operations.
  2048. */
  2049. static int smk_open_load2(struct inode *inode, struct file *file)
  2050. {
  2051. return seq_open(file, &load2_seq_ops);
  2052. }
  2053. /**
  2054. * smk_write_load2 - write() for /smack/load2
  2055. * @file: file pointer, not actually used
  2056. * @buf: where to get the data from
  2057. * @count: bytes sent
  2058. * @ppos: where to start - must be 0
  2059. *
  2060. */
  2061. static ssize_t smk_write_load2(struct file *file, const char __user *buf,
  2062. size_t count, loff_t *ppos)
  2063. {
  2064. /*
  2065. * Must have privilege.
  2066. */
  2067. if (!smack_privileged(CAP_MAC_ADMIN))
  2068. return -EPERM;
  2069. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  2070. SMK_LONG_FMT);
  2071. }
  2072. static const struct file_operations smk_load2_ops = {
  2073. .open = smk_open_load2,
  2074. .read = seq_read,
  2075. .llseek = seq_lseek,
  2076. .write = smk_write_load2,
  2077. .release = seq_release,
  2078. };
  2079. /*
  2080. * Seq_file read operations for /smack/load-self2
  2081. */
  2082. static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
  2083. {
  2084. struct task_smack *tsp = current_security();
  2085. return smk_seq_start(s, pos, &tsp->smk_rules);
  2086. }
  2087. static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
  2088. {
  2089. struct task_smack *tsp = current_security();
  2090. return smk_seq_next(s, v, pos, &tsp->smk_rules);
  2091. }
  2092. static int load_self2_seq_show(struct seq_file *s, void *v)
  2093. {
  2094. struct list_head *list = v;
  2095. struct smack_rule *srp =
  2096. list_entry_rcu(list, struct smack_rule, list);
  2097. smk_rule_show(s, srp, SMK_LONGLABEL);
  2098. return 0;
  2099. }
  2100. static const struct seq_operations load_self2_seq_ops = {
  2101. .start = load_self2_seq_start,
  2102. .next = load_self2_seq_next,
  2103. .show = load_self2_seq_show,
  2104. .stop = smk_seq_stop,
  2105. };
  2106. /**
  2107. * smk_open_load_self2 - open() for /smack/load-self2
  2108. * @inode: inode structure representing file
  2109. * @file: "load" file pointer
  2110. *
  2111. * For reading, use load_seq_* seq_file reading operations.
  2112. */
  2113. static int smk_open_load_self2(struct inode *inode, struct file *file)
  2114. {
  2115. return seq_open(file, &load_self2_seq_ops);
  2116. }
  2117. /**
  2118. * smk_write_load_self2 - write() for /smack/load-self2
  2119. * @file: file pointer, not actually used
  2120. * @buf: where to get the data from
  2121. * @count: bytes sent
  2122. * @ppos: where to start - must be 0
  2123. *
  2124. */
  2125. static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
  2126. size_t count, loff_t *ppos)
  2127. {
  2128. struct task_smack *tsp = current_security();
  2129. return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
  2130. &tsp->smk_rules_lock, SMK_LONG_FMT);
  2131. }
  2132. static const struct file_operations smk_load_self2_ops = {
  2133. .open = smk_open_load_self2,
  2134. .read = seq_read,
  2135. .llseek = seq_lseek,
  2136. .write = smk_write_load_self2,
  2137. .release = seq_release,
  2138. };
  2139. /**
  2140. * smk_write_access2 - handle access check transaction
  2141. * @file: file pointer
  2142. * @buf: data from user space
  2143. * @count: bytes sent
  2144. * @ppos: where to start - must be 0
  2145. */
  2146. static ssize_t smk_write_access2(struct file *file, const char __user *buf,
  2147. size_t count, loff_t *ppos)
  2148. {
  2149. return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
  2150. }
  2151. static const struct file_operations smk_access2_ops = {
  2152. .write = smk_write_access2,
  2153. .read = simple_transaction_read,
  2154. .release = simple_transaction_release,
  2155. .llseek = generic_file_llseek,
  2156. };
  2157. /**
  2158. * smk_write_revoke_subj - write() for /smack/revoke-subject
  2159. * @file: file pointer
  2160. * @buf: data from user space
  2161. * @count: bytes sent
  2162. * @ppos: where to start - must be 0
  2163. */
  2164. static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
  2165. size_t count, loff_t *ppos)
  2166. {
  2167. char *data;
  2168. const char *cp;
  2169. struct smack_known *skp;
  2170. struct smack_rule *sp;
  2171. struct list_head *rule_list;
  2172. struct mutex *rule_lock;
  2173. int rc = count;
  2174. if (*ppos != 0)
  2175. return -EINVAL;
  2176. if (!smack_privileged(CAP_MAC_ADMIN))
  2177. return -EPERM;
  2178. if (count == 0 || count > SMK_LONGLABEL)
  2179. return -EINVAL;
  2180. data = memdup_user(buf, count);
  2181. if (IS_ERR(data))
  2182. return PTR_ERR(data);
  2183. cp = smk_parse_smack(data, count);
  2184. if (IS_ERR(cp)) {
  2185. rc = PTR_ERR(cp);
  2186. goto out_data;
  2187. }
  2188. skp = smk_find_entry(cp);
  2189. if (skp == NULL)
  2190. goto out_cp;
  2191. rule_list = &skp->smk_rules;
  2192. rule_lock = &skp->smk_rules_lock;
  2193. mutex_lock(rule_lock);
  2194. list_for_each_entry_rcu(sp, rule_list, list)
  2195. sp->smk_access = 0;
  2196. mutex_unlock(rule_lock);
  2197. out_cp:
  2198. kfree(cp);
  2199. out_data:
  2200. kfree(data);
  2201. return rc;
  2202. }
  2203. static const struct file_operations smk_revoke_subj_ops = {
  2204. .write = smk_write_revoke_subj,
  2205. .read = simple_transaction_read,
  2206. .release = simple_transaction_release,
  2207. .llseek = generic_file_llseek,
  2208. };
  2209. /**
  2210. * smk_init_sysfs - initialize /sys/fs/smackfs
  2211. *
  2212. */
  2213. static int smk_init_sysfs(void)
  2214. {
  2215. return sysfs_create_mount_point(fs_kobj, "smackfs");
  2216. }
  2217. /**
  2218. * smk_write_change_rule - write() for /smack/change-rule
  2219. * @file: file pointer
  2220. * @buf: data from user space
  2221. * @count: bytes sent
  2222. * @ppos: where to start - must be 0
  2223. */
  2224. static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
  2225. size_t count, loff_t *ppos)
  2226. {
  2227. /*
  2228. * Must have privilege.
  2229. */
  2230. if (!smack_privileged(CAP_MAC_ADMIN))
  2231. return -EPERM;
  2232. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  2233. SMK_CHANGE_FMT);
  2234. }
  2235. static const struct file_operations smk_change_rule_ops = {
  2236. .write = smk_write_change_rule,
  2237. .read = simple_transaction_read,
  2238. .release = simple_transaction_release,
  2239. .llseek = generic_file_llseek,
  2240. };
  2241. /**
  2242. * smk_read_syslog - read() for smackfs/syslog
  2243. * @filp: file pointer, not actually used
  2244. * @buf: where to put the result
  2245. * @cn: maximum to send along
  2246. * @ppos: where to start
  2247. *
  2248. * Returns number of bytes read or error code, as appropriate
  2249. */
  2250. static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
  2251. size_t cn, loff_t *ppos)
  2252. {
  2253. struct smack_known *skp;
  2254. ssize_t rc = -EINVAL;
  2255. int asize;
  2256. if (*ppos != 0)
  2257. return 0;
  2258. if (smack_syslog_label == NULL)
  2259. skp = &smack_known_star;
  2260. else
  2261. skp = smack_syslog_label;
  2262. asize = strlen(skp->smk_known) + 1;
  2263. if (cn >= asize)
  2264. rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
  2265. asize);
  2266. return rc;
  2267. }
  2268. /**
  2269. * smk_write_syslog - write() for smackfs/syslog
  2270. * @file: file pointer, not actually used
  2271. * @buf: where to get the data from
  2272. * @count: bytes sent
  2273. * @ppos: where to start
  2274. *
  2275. * Returns number of bytes written or error code, as appropriate
  2276. */
  2277. static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
  2278. size_t count, loff_t *ppos)
  2279. {
  2280. char *data;
  2281. struct smack_known *skp;
  2282. int rc = count;
  2283. if (!smack_privileged(CAP_MAC_ADMIN))
  2284. return -EPERM;
  2285. data = memdup_user_nul(buf, count);
  2286. if (IS_ERR(data))
  2287. return PTR_ERR(data);
  2288. skp = smk_import_entry(data, count);
  2289. if (IS_ERR(skp))
  2290. rc = PTR_ERR(skp);
  2291. else
  2292. smack_syslog_label = skp;
  2293. kfree(data);
  2294. return rc;
  2295. }
  2296. static const struct file_operations smk_syslog_ops = {
  2297. .read = smk_read_syslog,
  2298. .write = smk_write_syslog,
  2299. .llseek = default_llseek,
  2300. };
  2301. /*
  2302. * Seq_file read operations for /smack/relabel-self
  2303. */
  2304. static void *relabel_self_seq_start(struct seq_file *s, loff_t *pos)
  2305. {
  2306. struct task_smack *tsp = current_security();
  2307. return smk_seq_start(s, pos, &tsp->smk_relabel);
  2308. }
  2309. static void *relabel_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
  2310. {
  2311. struct task_smack *tsp = current_security();
  2312. return smk_seq_next(s, v, pos, &tsp->smk_relabel);
  2313. }
  2314. static int relabel_self_seq_show(struct seq_file *s, void *v)
  2315. {
  2316. struct list_head *list = v;
  2317. struct smack_known_list_elem *sklep =
  2318. list_entry(list, struct smack_known_list_elem, list);
  2319. seq_puts(s, sklep->smk_label->smk_known);
  2320. seq_putc(s, ' ');
  2321. return 0;
  2322. }
  2323. static const struct seq_operations relabel_self_seq_ops = {
  2324. .start = relabel_self_seq_start,
  2325. .next = relabel_self_seq_next,
  2326. .show = relabel_self_seq_show,
  2327. .stop = smk_seq_stop,
  2328. };
  2329. /**
  2330. * smk_open_relabel_self - open() for /smack/relabel-self
  2331. * @inode: inode structure representing file
  2332. * @file: "relabel-self" file pointer
  2333. *
  2334. * Connect our relabel_self_seq_* operations with /smack/relabel-self
  2335. * file_operations
  2336. */
  2337. static int smk_open_relabel_self(struct inode *inode, struct file *file)
  2338. {
  2339. return seq_open(file, &relabel_self_seq_ops);
  2340. }
  2341. /**
  2342. * smk_write_relabel_self - write() for /smack/relabel-self
  2343. * @file: file pointer, not actually used
  2344. * @buf: where to get the data from
  2345. * @count: bytes sent
  2346. * @ppos: where to start - must be 0
  2347. *
  2348. */
  2349. static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
  2350. size_t count, loff_t *ppos)
  2351. {
  2352. struct task_smack *tsp = current_security();
  2353. char *data;
  2354. int rc;
  2355. LIST_HEAD(list_tmp);
  2356. /*
  2357. * Must have privilege.
  2358. */
  2359. if (!smack_privileged(CAP_MAC_ADMIN))
  2360. return -EPERM;
  2361. /*
  2362. * Enough data must be present.
  2363. */
  2364. if (*ppos != 0)
  2365. return -EINVAL;
  2366. data = memdup_user_nul(buf, count);
  2367. if (IS_ERR(data))
  2368. return PTR_ERR(data);
  2369. rc = smk_parse_label_list(data, &list_tmp);
  2370. kfree(data);
  2371. if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
  2372. smk_destroy_label_list(&tsp->smk_relabel);
  2373. list_splice(&list_tmp, &tsp->smk_relabel);
  2374. return count;
  2375. }
  2376. smk_destroy_label_list(&list_tmp);
  2377. return rc;
  2378. }
  2379. static const struct file_operations smk_relabel_self_ops = {
  2380. .open = smk_open_relabel_self,
  2381. .read = seq_read,
  2382. .llseek = seq_lseek,
  2383. .write = smk_write_relabel_self,
  2384. .release = seq_release,
  2385. };
  2386. /**
  2387. * smk_read_ptrace - read() for /smack/ptrace
  2388. * @filp: file pointer, not actually used
  2389. * @buf: where to put the result
  2390. * @count: maximum to send along
  2391. * @ppos: where to start
  2392. *
  2393. * Returns number of bytes read or error code, as appropriate
  2394. */
  2395. static ssize_t smk_read_ptrace(struct file *filp, char __user *buf,
  2396. size_t count, loff_t *ppos)
  2397. {
  2398. char temp[32];
  2399. ssize_t rc;
  2400. if (*ppos != 0)
  2401. return 0;
  2402. sprintf(temp, "%d\n", smack_ptrace_rule);
  2403. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  2404. return rc;
  2405. }
  2406. /**
  2407. * smk_write_ptrace - write() for /smack/ptrace
  2408. * @file: file pointer
  2409. * @buf: data from user space
  2410. * @count: bytes sent
  2411. * @ppos: where to start - must be 0
  2412. */
  2413. static ssize_t smk_write_ptrace(struct file *file, const char __user *buf,
  2414. size_t count, loff_t *ppos)
  2415. {
  2416. char temp[32];
  2417. int i;
  2418. if (!smack_privileged(CAP_MAC_ADMIN))
  2419. return -EPERM;
  2420. if (*ppos != 0 || count >= sizeof(temp) || count == 0)
  2421. return -EINVAL;
  2422. if (copy_from_user(temp, buf, count) != 0)
  2423. return -EFAULT;
  2424. temp[count] = '\0';
  2425. if (sscanf(temp, "%d", &i) != 1)
  2426. return -EINVAL;
  2427. if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX)
  2428. return -EINVAL;
  2429. smack_ptrace_rule = i;
  2430. return count;
  2431. }
  2432. static const struct file_operations smk_ptrace_ops = {
  2433. .write = smk_write_ptrace,
  2434. .read = smk_read_ptrace,
  2435. .llseek = default_llseek,
  2436. };
  2437. /**
  2438. * smk_fill_super - fill the smackfs superblock
  2439. * @sb: the empty superblock
  2440. * @data: unused
  2441. * @silent: unused
  2442. *
  2443. * Fill in the well known entries for the smack filesystem
  2444. *
  2445. * Returns 0 on success, an error code on failure
  2446. */
  2447. static int smk_fill_super(struct super_block *sb, void *data, int silent)
  2448. {
  2449. int rc;
  2450. struct inode *root_inode;
  2451. static struct tree_descr smack_files[] = {
  2452. [SMK_LOAD] = {
  2453. "load", &smk_load_ops, S_IRUGO|S_IWUSR},
  2454. [SMK_CIPSO] = {
  2455. "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
  2456. [SMK_DOI] = {
  2457. "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
  2458. [SMK_DIRECT] = {
  2459. "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
  2460. [SMK_AMBIENT] = {
  2461. "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
  2462. [SMK_NET4ADDR] = {
  2463. "netlabel", &smk_net4addr_ops, S_IRUGO|S_IWUSR},
  2464. [SMK_ONLYCAP] = {
  2465. "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
  2466. [SMK_LOGGING] = {
  2467. "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
  2468. [SMK_LOAD_SELF] = {
  2469. "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
  2470. [SMK_ACCESSES] = {
  2471. "access", &smk_access_ops, S_IRUGO|S_IWUGO},
  2472. [SMK_MAPPED] = {
  2473. "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
  2474. [SMK_LOAD2] = {
  2475. "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
  2476. [SMK_LOAD_SELF2] = {
  2477. "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
  2478. [SMK_ACCESS2] = {
  2479. "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
  2480. [SMK_CIPSO2] = {
  2481. "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
  2482. [SMK_REVOKE_SUBJ] = {
  2483. "revoke-subject", &smk_revoke_subj_ops,
  2484. S_IRUGO|S_IWUSR},
  2485. [SMK_CHANGE_RULE] = {
  2486. "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
  2487. [SMK_SYSLOG] = {
  2488. "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
  2489. [SMK_PTRACE] = {
  2490. "ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR},
  2491. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  2492. [SMK_UNCONFINED] = {
  2493. "unconfined", &smk_unconfined_ops, S_IRUGO|S_IWUSR},
  2494. #endif
  2495. #if IS_ENABLED(CONFIG_IPV6)
  2496. [SMK_NET6ADDR] = {
  2497. "ipv6host", &smk_net6addr_ops, S_IRUGO|S_IWUSR},
  2498. #endif /* CONFIG_IPV6 */
  2499. [SMK_RELABEL_SELF] = {
  2500. "relabel-self", &smk_relabel_self_ops,
  2501. S_IRUGO|S_IWUGO},
  2502. /* last one */
  2503. {""}
  2504. };
  2505. rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
  2506. if (rc != 0) {
  2507. printk(KERN_ERR "%s failed %d while creating inodes\n",
  2508. __func__, rc);
  2509. return rc;
  2510. }
  2511. root_inode = d_inode(sb->s_root);
  2512. return 0;
  2513. }
  2514. /**
  2515. * smk_mount - get the smackfs superblock
  2516. * @fs_type: passed along without comment
  2517. * @flags: passed along without comment
  2518. * @dev_name: passed along without comment
  2519. * @data: passed along without comment
  2520. *
  2521. * Just passes everything along.
  2522. *
  2523. * Returns what the lower level code does.
  2524. */
  2525. static struct dentry *smk_mount(struct file_system_type *fs_type,
  2526. int flags, const char *dev_name, void *data)
  2527. {
  2528. return mount_single(fs_type, flags, data, smk_fill_super);
  2529. }
  2530. static struct file_system_type smk_fs_type = {
  2531. .name = "smackfs",
  2532. .mount = smk_mount,
  2533. .kill_sb = kill_litter_super,
  2534. };
  2535. static struct vfsmount *smackfs_mount;
  2536. static int __init smk_preset_netlabel(struct smack_known *skp)
  2537. {
  2538. skp->smk_netlabel.domain = skp->smk_known;
  2539. skp->smk_netlabel.flags =
  2540. NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
  2541. return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
  2542. &skp->smk_netlabel, strlen(skp->smk_known));
  2543. }
  2544. /**
  2545. * init_smk_fs - get the smackfs superblock
  2546. *
  2547. * register the smackfs
  2548. *
  2549. * Do not register smackfs if Smack wasn't enabled
  2550. * on boot. We can not put this method normally under the
  2551. * smack_init() code path since the security subsystem get
  2552. * initialized before the vfs caches.
  2553. *
  2554. * Returns true if we were not chosen on boot or if
  2555. * we were chosen and filesystem registration succeeded.
  2556. */
  2557. static int __init init_smk_fs(void)
  2558. {
  2559. int err;
  2560. int rc;
  2561. if (smack_enabled == 0)
  2562. return 0;
  2563. err = smk_init_sysfs();
  2564. if (err)
  2565. printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
  2566. err = register_filesystem(&smk_fs_type);
  2567. if (!err) {
  2568. smackfs_mount = kern_mount(&smk_fs_type);
  2569. if (IS_ERR(smackfs_mount)) {
  2570. printk(KERN_ERR "smackfs: could not mount!\n");
  2571. err = PTR_ERR(smackfs_mount);
  2572. smackfs_mount = NULL;
  2573. }
  2574. }
  2575. smk_cipso_doi();
  2576. smk_unlbl_ambient(NULL);
  2577. rc = smk_preset_netlabel(&smack_known_floor);
  2578. if (err == 0 && rc < 0)
  2579. err = rc;
  2580. rc = smk_preset_netlabel(&smack_known_hat);
  2581. if (err == 0 && rc < 0)
  2582. err = rc;
  2583. rc = smk_preset_netlabel(&smack_known_huh);
  2584. if (err == 0 && rc < 0)
  2585. err = rc;
  2586. rc = smk_preset_netlabel(&smack_known_invalid);
  2587. if (err == 0 && rc < 0)
  2588. err = rc;
  2589. rc = smk_preset_netlabel(&smack_known_star);
  2590. if (err == 0 && rc < 0)
  2591. err = rc;
  2592. rc = smk_preset_netlabel(&smack_known_web);
  2593. if (err == 0 && rc < 0)
  2594. err = rc;
  2595. return err;
  2596. }
  2597. __initcall(init_smk_fs);