smackfs.c 70 KB

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