smackfs.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056
  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. data = memdup_user_nul(buf, count);
  784. if (IS_ERR(data))
  785. return PTR_ERR(data);
  786. rule = data;
  787. /*
  788. * Only allow one writer at a time. Writes should be
  789. * quite rare and small in any case.
  790. */
  791. mutex_lock(&smack_cipso_lock);
  792. skp = smk_import_entry(rule, 0);
  793. if (IS_ERR(skp)) {
  794. rc = PTR_ERR(skp);
  795. goto out;
  796. }
  797. if (format == SMK_FIXED24_FMT)
  798. rule += SMK_LABELLEN;
  799. else
  800. rule += strlen(skp->smk_known) + 1;
  801. if (rule > data + count) {
  802. rc = -EOVERFLOW;
  803. goto out;
  804. }
  805. ret = sscanf(rule, "%d", &maplevel);
  806. if (ret != 1 || maplevel < 0 || maplevel > SMACK_CIPSO_MAXLEVEL)
  807. goto out;
  808. rule += SMK_DIGITLEN;
  809. if (rule > data + count) {
  810. rc = -EOVERFLOW;
  811. goto out;
  812. }
  813. ret = sscanf(rule, "%d", &catlen);
  814. if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
  815. goto out;
  816. if (format == SMK_FIXED24_FMT &&
  817. count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
  818. goto out;
  819. memset(mapcatset, 0, sizeof(mapcatset));
  820. for (i = 0; i < catlen; i++) {
  821. rule += SMK_DIGITLEN;
  822. if (rule > data + count) {
  823. rc = -EOVERFLOW;
  824. goto out;
  825. }
  826. ret = sscanf(rule, "%u", &cat);
  827. if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
  828. goto out;
  829. smack_catset_bit(cat, mapcatset);
  830. }
  831. rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
  832. if (rc >= 0) {
  833. netlbl_catmap_free(skp->smk_netlabel.attr.mls.cat);
  834. skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
  835. skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
  836. rc = count;
  837. }
  838. out:
  839. mutex_unlock(&smack_cipso_lock);
  840. kfree(data);
  841. return rc;
  842. }
  843. /**
  844. * smk_write_cipso - write() for /smack/cipso
  845. * @file: file pointer, not actually used
  846. * @buf: where to get the data from
  847. * @count: bytes sent
  848. * @ppos: where to start
  849. *
  850. * Accepts only one cipso rule per write call.
  851. * Returns number of bytes written or error code, as appropriate
  852. */
  853. static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
  854. size_t count, loff_t *ppos)
  855. {
  856. return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
  857. }
  858. static const struct file_operations smk_cipso_ops = {
  859. .open = smk_open_cipso,
  860. .read = seq_read,
  861. .llseek = seq_lseek,
  862. .write = smk_write_cipso,
  863. .release = seq_release,
  864. };
  865. /*
  866. * Seq_file read operations for /smack/cipso2
  867. */
  868. /*
  869. * Print cipso labels in format:
  870. * label level[/cat[,cat]]
  871. */
  872. static int cipso2_seq_show(struct seq_file *s, void *v)
  873. {
  874. struct list_head *list = v;
  875. struct smack_known *skp =
  876. list_entry_rcu(list, struct smack_known, list);
  877. struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
  878. char sep = '/';
  879. int i;
  880. seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
  881. for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
  882. i = netlbl_catmap_walk(cmp, i + 1)) {
  883. seq_printf(s, "%c%d", sep, i);
  884. sep = ',';
  885. }
  886. seq_putc(s, '\n');
  887. return 0;
  888. }
  889. static const struct seq_operations cipso2_seq_ops = {
  890. .start = cipso_seq_start,
  891. .next = cipso_seq_next,
  892. .show = cipso2_seq_show,
  893. .stop = smk_seq_stop,
  894. };
  895. /**
  896. * smk_open_cipso2 - open() for /smack/cipso2
  897. * @inode: inode structure representing file
  898. * @file: "cipso2" file pointer
  899. *
  900. * Connect our cipso_seq_* operations with /smack/cipso2
  901. * file_operations
  902. */
  903. static int smk_open_cipso2(struct inode *inode, struct file *file)
  904. {
  905. return seq_open(file, &cipso2_seq_ops);
  906. }
  907. /**
  908. * smk_write_cipso2 - write() for /smack/cipso2
  909. * @file: file pointer, not actually used
  910. * @buf: where to get the data from
  911. * @count: bytes sent
  912. * @ppos: where to start
  913. *
  914. * Accepts only one cipso rule per write call.
  915. * Returns number of bytes written or error code, as appropriate
  916. */
  917. static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
  918. size_t count, loff_t *ppos)
  919. {
  920. return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
  921. }
  922. static const struct file_operations smk_cipso2_ops = {
  923. .open = smk_open_cipso2,
  924. .read = seq_read,
  925. .llseek = seq_lseek,
  926. .write = smk_write_cipso2,
  927. .release = seq_release,
  928. };
  929. /*
  930. * Seq_file read operations for /smack/netlabel
  931. */
  932. static void *net4addr_seq_start(struct seq_file *s, loff_t *pos)
  933. {
  934. return smk_seq_start(s, pos, &smk_net4addr_list);
  935. }
  936. static void *net4addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
  937. {
  938. return smk_seq_next(s, v, pos, &smk_net4addr_list);
  939. }
  940. /*
  941. * Print host/label pairs
  942. */
  943. static int net4addr_seq_show(struct seq_file *s, void *v)
  944. {
  945. struct list_head *list = v;
  946. struct smk_net4addr *skp =
  947. list_entry_rcu(list, struct smk_net4addr, list);
  948. char *kp = SMACK_CIPSO_OPTION;
  949. if (skp->smk_label != NULL)
  950. kp = skp->smk_label->smk_known;
  951. seq_printf(s, "%pI4/%d %s\n", &skp->smk_host.s_addr,
  952. skp->smk_masks, kp);
  953. return 0;
  954. }
  955. static const struct seq_operations net4addr_seq_ops = {
  956. .start = net4addr_seq_start,
  957. .next = net4addr_seq_next,
  958. .show = net4addr_seq_show,
  959. .stop = smk_seq_stop,
  960. };
  961. /**
  962. * smk_open_net4addr - open() for /smack/netlabel
  963. * @inode: inode structure representing file
  964. * @file: "netlabel" file pointer
  965. *
  966. * Connect our net4addr_seq_* operations with /smack/netlabel
  967. * file_operations
  968. */
  969. static int smk_open_net4addr(struct inode *inode, struct file *file)
  970. {
  971. return seq_open(file, &net4addr_seq_ops);
  972. }
  973. /**
  974. * smk_net4addr_insert
  975. * @new : netlabel to insert
  976. *
  977. * This helper insert netlabel in the smack_net4addrs list
  978. * sorted by netmask length (longest to smallest)
  979. * locked by &smk_net4addr_lock in smk_write_net4addr
  980. *
  981. */
  982. static void smk_net4addr_insert(struct smk_net4addr *new)
  983. {
  984. struct smk_net4addr *m;
  985. struct smk_net4addr *m_next;
  986. if (list_empty(&smk_net4addr_list)) {
  987. list_add_rcu(&new->list, &smk_net4addr_list);
  988. return;
  989. }
  990. m = list_entry_rcu(smk_net4addr_list.next,
  991. struct smk_net4addr, list);
  992. /* the comparison '>' is a bit hacky, but works */
  993. if (new->smk_masks > m->smk_masks) {
  994. list_add_rcu(&new->list, &smk_net4addr_list);
  995. return;
  996. }
  997. list_for_each_entry_rcu(m, &smk_net4addr_list, list) {
  998. if (list_is_last(&m->list, &smk_net4addr_list)) {
  999. list_add_rcu(&new->list, &m->list);
  1000. return;
  1001. }
  1002. m_next = list_entry_rcu(m->list.next,
  1003. struct smk_net4addr, list);
  1004. if (new->smk_masks > m_next->smk_masks) {
  1005. list_add_rcu(&new->list, &m->list);
  1006. return;
  1007. }
  1008. }
  1009. }
  1010. /**
  1011. * smk_write_net4addr - write() for /smack/netlabel
  1012. * @file: file pointer, not actually used
  1013. * @buf: where to get the data from
  1014. * @count: bytes sent
  1015. * @ppos: where to start
  1016. *
  1017. * Accepts only one net4addr per write call.
  1018. * Returns number of bytes written or error code, as appropriate
  1019. */
  1020. static ssize_t smk_write_net4addr(struct file *file, const char __user *buf,
  1021. size_t count, loff_t *ppos)
  1022. {
  1023. struct smk_net4addr *snp;
  1024. struct sockaddr_in newname;
  1025. char *smack;
  1026. struct smack_known *skp = NULL;
  1027. char *data;
  1028. char *host = (char *)&newname.sin_addr.s_addr;
  1029. int rc;
  1030. struct netlbl_audit audit_info;
  1031. struct in_addr mask;
  1032. unsigned int m;
  1033. unsigned int masks;
  1034. int found;
  1035. u32 mask_bits = (1<<31);
  1036. __be32 nsa;
  1037. u32 temp_mask;
  1038. /*
  1039. * Must have privilege.
  1040. * No partial writes.
  1041. * Enough data must be present.
  1042. * "<addr/mask, as a.b.c.d/e><space><label>"
  1043. * "<addr, as a.b.c.d><space><label>"
  1044. */
  1045. if (!smack_privileged(CAP_MAC_ADMIN))
  1046. return -EPERM;
  1047. if (*ppos != 0)
  1048. return -EINVAL;
  1049. if (count < SMK_NETLBLADDRMIN || count > PAGE_SIZE - 1)
  1050. return -EINVAL;
  1051. data = memdup_user_nul(buf, count);
  1052. if (IS_ERR(data))
  1053. return PTR_ERR(data);
  1054. smack = kzalloc(count + 1, GFP_KERNEL);
  1055. if (smack == NULL) {
  1056. rc = -ENOMEM;
  1057. goto free_data_out;
  1058. }
  1059. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
  1060. &host[0], &host[1], &host[2], &host[3], &masks, smack);
  1061. if (rc != 6) {
  1062. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
  1063. &host[0], &host[1], &host[2], &host[3], smack);
  1064. if (rc != 5) {
  1065. rc = -EINVAL;
  1066. goto free_out;
  1067. }
  1068. m = BEBITS;
  1069. masks = 32;
  1070. }
  1071. if (masks > BEBITS) {
  1072. rc = -EINVAL;
  1073. goto free_out;
  1074. }
  1075. /*
  1076. * If smack begins with '-', it is an option, don't import it
  1077. */
  1078. if (smack[0] != '-') {
  1079. skp = smk_import_entry(smack, 0);
  1080. if (IS_ERR(skp)) {
  1081. rc = PTR_ERR(skp);
  1082. goto free_out;
  1083. }
  1084. } else {
  1085. /*
  1086. * Only the -CIPSO option is supported for IPv4
  1087. */
  1088. if (strcmp(smack, SMACK_CIPSO_OPTION) != 0) {
  1089. rc = -EINVAL;
  1090. goto free_out;
  1091. }
  1092. }
  1093. for (m = masks, temp_mask = 0; m > 0; m--) {
  1094. temp_mask |= mask_bits;
  1095. mask_bits >>= 1;
  1096. }
  1097. mask.s_addr = cpu_to_be32(temp_mask);
  1098. newname.sin_addr.s_addr &= mask.s_addr;
  1099. /*
  1100. * Only allow one writer at a time. Writes should be
  1101. * quite rare and small in any case.
  1102. */
  1103. mutex_lock(&smk_net4addr_lock);
  1104. nsa = newname.sin_addr.s_addr;
  1105. /* try to find if the prefix is already in the list */
  1106. found = 0;
  1107. list_for_each_entry_rcu(snp, &smk_net4addr_list, list) {
  1108. if (snp->smk_host.s_addr == nsa && snp->smk_masks == masks) {
  1109. found = 1;
  1110. break;
  1111. }
  1112. }
  1113. smk_netlabel_audit_set(&audit_info);
  1114. if (found == 0) {
  1115. snp = kzalloc(sizeof(*snp), GFP_KERNEL);
  1116. if (snp == NULL)
  1117. rc = -ENOMEM;
  1118. else {
  1119. rc = 0;
  1120. snp->smk_host.s_addr = newname.sin_addr.s_addr;
  1121. snp->smk_mask.s_addr = mask.s_addr;
  1122. snp->smk_label = skp;
  1123. snp->smk_masks = masks;
  1124. smk_net4addr_insert(snp);
  1125. }
  1126. } else {
  1127. /*
  1128. * Delete the unlabeled entry, only if the previous label
  1129. * wasn't the special CIPSO option
  1130. */
  1131. if (snp->smk_label != NULL)
  1132. rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
  1133. &snp->smk_host, &snp->smk_mask,
  1134. PF_INET, &audit_info);
  1135. else
  1136. rc = 0;
  1137. snp->smk_label = skp;
  1138. }
  1139. /*
  1140. * Now tell netlabel about the single label nature of
  1141. * this host so that incoming packets get labeled.
  1142. * but only if we didn't get the special CIPSO option
  1143. */
  1144. if (rc == 0 && skp != NULL)
  1145. rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
  1146. &snp->smk_host, &snp->smk_mask, PF_INET,
  1147. snp->smk_label->smk_secid, &audit_info);
  1148. if (rc == 0)
  1149. rc = count;
  1150. mutex_unlock(&smk_net4addr_lock);
  1151. free_out:
  1152. kfree(smack);
  1153. free_data_out:
  1154. kfree(data);
  1155. return rc;
  1156. }
  1157. static const struct file_operations smk_net4addr_ops = {
  1158. .open = smk_open_net4addr,
  1159. .read = seq_read,
  1160. .llseek = seq_lseek,
  1161. .write = smk_write_net4addr,
  1162. .release = seq_release,
  1163. };
  1164. #if IS_ENABLED(CONFIG_IPV6)
  1165. /*
  1166. * Seq_file read operations for /smack/netlabel6
  1167. */
  1168. static void *net6addr_seq_start(struct seq_file *s, loff_t *pos)
  1169. {
  1170. return smk_seq_start(s, pos, &smk_net6addr_list);
  1171. }
  1172. static void *net6addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1173. {
  1174. return smk_seq_next(s, v, pos, &smk_net6addr_list);
  1175. }
  1176. /*
  1177. * Print host/label pairs
  1178. */
  1179. static int net6addr_seq_show(struct seq_file *s, void *v)
  1180. {
  1181. struct list_head *list = v;
  1182. struct smk_net6addr *skp =
  1183. list_entry(list, struct smk_net6addr, list);
  1184. if (skp->smk_label != NULL)
  1185. seq_printf(s, "%pI6/%d %s\n", &skp->smk_host, skp->smk_masks,
  1186. skp->smk_label->smk_known);
  1187. return 0;
  1188. }
  1189. static const struct seq_operations net6addr_seq_ops = {
  1190. .start = net6addr_seq_start,
  1191. .next = net6addr_seq_next,
  1192. .show = net6addr_seq_show,
  1193. .stop = smk_seq_stop,
  1194. };
  1195. /**
  1196. * smk_open_net6addr - open() for /smack/netlabel
  1197. * @inode: inode structure representing file
  1198. * @file: "netlabel" file pointer
  1199. *
  1200. * Connect our net6addr_seq_* operations with /smack/netlabel
  1201. * file_operations
  1202. */
  1203. static int smk_open_net6addr(struct inode *inode, struct file *file)
  1204. {
  1205. return seq_open(file, &net6addr_seq_ops);
  1206. }
  1207. /**
  1208. * smk_net6addr_insert
  1209. * @new : entry to insert
  1210. *
  1211. * This inserts an entry in the smack_net6addrs list
  1212. * sorted by netmask length (longest to smallest)
  1213. * locked by &smk_net6addr_lock in smk_write_net6addr
  1214. *
  1215. */
  1216. static void smk_net6addr_insert(struct smk_net6addr *new)
  1217. {
  1218. struct smk_net6addr *m_next;
  1219. struct smk_net6addr *m;
  1220. if (list_empty(&smk_net6addr_list)) {
  1221. list_add_rcu(&new->list, &smk_net6addr_list);
  1222. return;
  1223. }
  1224. m = list_entry_rcu(smk_net6addr_list.next,
  1225. struct smk_net6addr, list);
  1226. if (new->smk_masks > m->smk_masks) {
  1227. list_add_rcu(&new->list, &smk_net6addr_list);
  1228. return;
  1229. }
  1230. list_for_each_entry_rcu(m, &smk_net6addr_list, list) {
  1231. if (list_is_last(&m->list, &smk_net6addr_list)) {
  1232. list_add_rcu(&new->list, &m->list);
  1233. return;
  1234. }
  1235. m_next = list_entry_rcu(m->list.next,
  1236. struct smk_net6addr, list);
  1237. if (new->smk_masks > m_next->smk_masks) {
  1238. list_add_rcu(&new->list, &m->list);
  1239. return;
  1240. }
  1241. }
  1242. }
  1243. /**
  1244. * smk_write_net6addr - write() for /smack/netlabel
  1245. * @file: file pointer, not actually used
  1246. * @buf: where to get the data from
  1247. * @count: bytes sent
  1248. * @ppos: where to start
  1249. *
  1250. * Accepts only one net6addr per write call.
  1251. * Returns number of bytes written or error code, as appropriate
  1252. */
  1253. static ssize_t smk_write_net6addr(struct file *file, const char __user *buf,
  1254. size_t count, loff_t *ppos)
  1255. {
  1256. struct smk_net6addr *snp;
  1257. struct in6_addr newname;
  1258. struct in6_addr fullmask;
  1259. struct smack_known *skp = NULL;
  1260. char *smack;
  1261. char *data;
  1262. int rc = 0;
  1263. int found = 0;
  1264. int i;
  1265. unsigned int scanned[8];
  1266. unsigned int m;
  1267. unsigned int mask = 128;
  1268. /*
  1269. * Must have privilege.
  1270. * No partial writes.
  1271. * Enough data must be present.
  1272. * "<addr/mask, as a:b:c:d:e:f:g:h/e><space><label>"
  1273. * "<addr, as a:b:c:d:e:f:g:h><space><label>"
  1274. */
  1275. if (!smack_privileged(CAP_MAC_ADMIN))
  1276. return -EPERM;
  1277. if (*ppos != 0)
  1278. return -EINVAL;
  1279. if (count < SMK_NETLBLADDRMIN || count > PAGE_SIZE - 1)
  1280. return -EINVAL;
  1281. data = memdup_user_nul(buf, count);
  1282. if (IS_ERR(data))
  1283. return PTR_ERR(data);
  1284. smack = kzalloc(count + 1, GFP_KERNEL);
  1285. if (smack == NULL) {
  1286. rc = -ENOMEM;
  1287. goto free_data_out;
  1288. }
  1289. i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x/%u %s",
  1290. &scanned[0], &scanned[1], &scanned[2], &scanned[3],
  1291. &scanned[4], &scanned[5], &scanned[6], &scanned[7],
  1292. &mask, smack);
  1293. if (i != 10) {
  1294. i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x %s",
  1295. &scanned[0], &scanned[1], &scanned[2],
  1296. &scanned[3], &scanned[4], &scanned[5],
  1297. &scanned[6], &scanned[7], smack);
  1298. if (i != 9) {
  1299. rc = -EINVAL;
  1300. goto free_out;
  1301. }
  1302. }
  1303. if (mask > 128) {
  1304. rc = -EINVAL;
  1305. goto free_out;
  1306. }
  1307. for (i = 0; i < 8; i++) {
  1308. if (scanned[i] > 0xffff) {
  1309. rc = -EINVAL;
  1310. goto free_out;
  1311. }
  1312. newname.s6_addr16[i] = htons(scanned[i]);
  1313. }
  1314. /*
  1315. * If smack begins with '-', it is an option, don't import it
  1316. */
  1317. if (smack[0] != '-') {
  1318. skp = smk_import_entry(smack, 0);
  1319. if (IS_ERR(skp)) {
  1320. rc = PTR_ERR(skp);
  1321. goto free_out;
  1322. }
  1323. } else {
  1324. /*
  1325. * Only -DELETE is supported for IPv6
  1326. */
  1327. if (strcmp(smack, SMACK_DELETE_OPTION) != 0) {
  1328. rc = -EINVAL;
  1329. goto free_out;
  1330. }
  1331. }
  1332. for (i = 0, m = mask; i < 8; i++) {
  1333. if (m >= 16) {
  1334. fullmask.s6_addr16[i] = 0xffff;
  1335. m -= 16;
  1336. } else if (m > 0) {
  1337. fullmask.s6_addr16[i] = (1 << m) - 1;
  1338. m = 0;
  1339. } else
  1340. fullmask.s6_addr16[i] = 0;
  1341. newname.s6_addr16[i] &= fullmask.s6_addr16[i];
  1342. }
  1343. /*
  1344. * Only allow one writer at a time. Writes should be
  1345. * quite rare and small in any case.
  1346. */
  1347. mutex_lock(&smk_net6addr_lock);
  1348. /*
  1349. * Try to find the prefix in the list
  1350. */
  1351. list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
  1352. if (mask != snp->smk_masks)
  1353. continue;
  1354. for (found = 1, i = 0; i < 8; i++) {
  1355. if (newname.s6_addr16[i] !=
  1356. snp->smk_host.s6_addr16[i]) {
  1357. found = 0;
  1358. break;
  1359. }
  1360. }
  1361. if (found == 1)
  1362. break;
  1363. }
  1364. if (found == 0) {
  1365. snp = kzalloc(sizeof(*snp), GFP_KERNEL);
  1366. if (snp == NULL)
  1367. rc = -ENOMEM;
  1368. else {
  1369. snp->smk_host = newname;
  1370. snp->smk_mask = fullmask;
  1371. snp->smk_masks = mask;
  1372. snp->smk_label = skp;
  1373. smk_net6addr_insert(snp);
  1374. }
  1375. } else {
  1376. snp->smk_label = skp;
  1377. }
  1378. if (rc == 0)
  1379. rc = count;
  1380. mutex_unlock(&smk_net6addr_lock);
  1381. free_out:
  1382. kfree(smack);
  1383. free_data_out:
  1384. kfree(data);
  1385. return rc;
  1386. }
  1387. static const struct file_operations smk_net6addr_ops = {
  1388. .open = smk_open_net6addr,
  1389. .read = seq_read,
  1390. .llseek = seq_lseek,
  1391. .write = smk_write_net6addr,
  1392. .release = seq_release,
  1393. };
  1394. #endif /* CONFIG_IPV6 */
  1395. /**
  1396. * smk_read_doi - read() for /smack/doi
  1397. * @filp: file pointer, not actually used
  1398. * @buf: where to put the result
  1399. * @count: maximum to send along
  1400. * @ppos: where to start
  1401. *
  1402. * Returns number of bytes read or error code, as appropriate
  1403. */
  1404. static ssize_t smk_read_doi(struct file *filp, char __user *buf,
  1405. size_t count, loff_t *ppos)
  1406. {
  1407. char temp[80];
  1408. ssize_t rc;
  1409. if (*ppos != 0)
  1410. return 0;
  1411. sprintf(temp, "%d", smk_cipso_doi_value);
  1412. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1413. return rc;
  1414. }
  1415. /**
  1416. * smk_write_doi - write() for /smack/doi
  1417. * @file: file pointer, not actually used
  1418. * @buf: where to get the data from
  1419. * @count: bytes sent
  1420. * @ppos: where to start
  1421. *
  1422. * Returns number of bytes written or error code, as appropriate
  1423. */
  1424. static ssize_t smk_write_doi(struct file *file, const char __user *buf,
  1425. size_t count, loff_t *ppos)
  1426. {
  1427. char temp[80];
  1428. int i;
  1429. if (!smack_privileged(CAP_MAC_ADMIN))
  1430. return -EPERM;
  1431. if (count >= sizeof(temp) || count == 0)
  1432. return -EINVAL;
  1433. if (copy_from_user(temp, buf, count) != 0)
  1434. return -EFAULT;
  1435. temp[count] = '\0';
  1436. if (sscanf(temp, "%d", &i) != 1)
  1437. return -EINVAL;
  1438. smk_cipso_doi_value = i;
  1439. smk_cipso_doi();
  1440. return count;
  1441. }
  1442. static const struct file_operations smk_doi_ops = {
  1443. .read = smk_read_doi,
  1444. .write = smk_write_doi,
  1445. .llseek = default_llseek,
  1446. };
  1447. /**
  1448. * smk_read_direct - read() for /smack/direct
  1449. * @filp: file pointer, not actually used
  1450. * @buf: where to put the result
  1451. * @count: maximum to send along
  1452. * @ppos: where to start
  1453. *
  1454. * Returns number of bytes read or error code, as appropriate
  1455. */
  1456. static ssize_t smk_read_direct(struct file *filp, char __user *buf,
  1457. size_t count, loff_t *ppos)
  1458. {
  1459. char temp[80];
  1460. ssize_t rc;
  1461. if (*ppos != 0)
  1462. return 0;
  1463. sprintf(temp, "%d", smack_cipso_direct);
  1464. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1465. return rc;
  1466. }
  1467. /**
  1468. * smk_write_direct - write() for /smack/direct
  1469. * @file: file pointer, not actually used
  1470. * @buf: where to get the data from
  1471. * @count: bytes sent
  1472. * @ppos: where to start
  1473. *
  1474. * Returns number of bytes written or error code, as appropriate
  1475. */
  1476. static ssize_t smk_write_direct(struct file *file, const char __user *buf,
  1477. size_t count, loff_t *ppos)
  1478. {
  1479. struct smack_known *skp;
  1480. char temp[80];
  1481. int i;
  1482. if (!smack_privileged(CAP_MAC_ADMIN))
  1483. return -EPERM;
  1484. if (count >= sizeof(temp) || count == 0)
  1485. return -EINVAL;
  1486. if (copy_from_user(temp, buf, count) != 0)
  1487. return -EFAULT;
  1488. temp[count] = '\0';
  1489. if (sscanf(temp, "%d", &i) != 1)
  1490. return -EINVAL;
  1491. /*
  1492. * Don't do anything if the value hasn't actually changed.
  1493. * If it is changing reset the level on entries that were
  1494. * set up to be direct when they were created.
  1495. */
  1496. if (smack_cipso_direct != i) {
  1497. mutex_lock(&smack_known_lock);
  1498. list_for_each_entry_rcu(skp, &smack_known_list, list)
  1499. if (skp->smk_netlabel.attr.mls.lvl ==
  1500. smack_cipso_direct)
  1501. skp->smk_netlabel.attr.mls.lvl = i;
  1502. smack_cipso_direct = i;
  1503. mutex_unlock(&smack_known_lock);
  1504. }
  1505. return count;
  1506. }
  1507. static const struct file_operations smk_direct_ops = {
  1508. .read = smk_read_direct,
  1509. .write = smk_write_direct,
  1510. .llseek = default_llseek,
  1511. };
  1512. /**
  1513. * smk_read_mapped - read() for /smack/mapped
  1514. * @filp: file pointer, not actually used
  1515. * @buf: where to put the result
  1516. * @count: maximum to send along
  1517. * @ppos: where to start
  1518. *
  1519. * Returns number of bytes read or error code, as appropriate
  1520. */
  1521. static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
  1522. size_t count, loff_t *ppos)
  1523. {
  1524. char temp[80];
  1525. ssize_t rc;
  1526. if (*ppos != 0)
  1527. return 0;
  1528. sprintf(temp, "%d", smack_cipso_mapped);
  1529. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1530. return rc;
  1531. }
  1532. /**
  1533. * smk_write_mapped - write() for /smack/mapped
  1534. * @file: file pointer, not actually used
  1535. * @buf: where to get the data from
  1536. * @count: bytes sent
  1537. * @ppos: where to start
  1538. *
  1539. * Returns number of bytes written or error code, as appropriate
  1540. */
  1541. static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
  1542. size_t count, loff_t *ppos)
  1543. {
  1544. struct smack_known *skp;
  1545. char temp[80];
  1546. int i;
  1547. if (!smack_privileged(CAP_MAC_ADMIN))
  1548. return -EPERM;
  1549. if (count >= sizeof(temp) || count == 0)
  1550. return -EINVAL;
  1551. if (copy_from_user(temp, buf, count) != 0)
  1552. return -EFAULT;
  1553. temp[count] = '\0';
  1554. if (sscanf(temp, "%d", &i) != 1)
  1555. return -EINVAL;
  1556. /*
  1557. * Don't do anything if the value hasn't actually changed.
  1558. * If it is changing reset the level on entries that were
  1559. * set up to be mapped when they were created.
  1560. */
  1561. if (smack_cipso_mapped != i) {
  1562. mutex_lock(&smack_known_lock);
  1563. list_for_each_entry_rcu(skp, &smack_known_list, list)
  1564. if (skp->smk_netlabel.attr.mls.lvl ==
  1565. smack_cipso_mapped)
  1566. skp->smk_netlabel.attr.mls.lvl = i;
  1567. smack_cipso_mapped = i;
  1568. mutex_unlock(&smack_known_lock);
  1569. }
  1570. return count;
  1571. }
  1572. static const struct file_operations smk_mapped_ops = {
  1573. .read = smk_read_mapped,
  1574. .write = smk_write_mapped,
  1575. .llseek = default_llseek,
  1576. };
  1577. /**
  1578. * smk_read_ambient - read() for /smack/ambient
  1579. * @filp: file pointer, not actually used
  1580. * @buf: where to put the result
  1581. * @cn: maximum to send along
  1582. * @ppos: where to start
  1583. *
  1584. * Returns number of bytes read or error code, as appropriate
  1585. */
  1586. static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
  1587. size_t cn, loff_t *ppos)
  1588. {
  1589. ssize_t rc;
  1590. int asize;
  1591. if (*ppos != 0)
  1592. return 0;
  1593. /*
  1594. * Being careful to avoid a problem in the case where
  1595. * smack_net_ambient gets changed in midstream.
  1596. */
  1597. mutex_lock(&smack_ambient_lock);
  1598. asize = strlen(smack_net_ambient->smk_known) + 1;
  1599. if (cn >= asize)
  1600. rc = simple_read_from_buffer(buf, cn, ppos,
  1601. smack_net_ambient->smk_known,
  1602. asize);
  1603. else
  1604. rc = -EINVAL;
  1605. mutex_unlock(&smack_ambient_lock);
  1606. return rc;
  1607. }
  1608. /**
  1609. * smk_write_ambient - write() for /smack/ambient
  1610. * @file: file pointer, not actually used
  1611. * @buf: where to get the data from
  1612. * @count: bytes sent
  1613. * @ppos: where to start
  1614. *
  1615. * Returns number of bytes written or error code, as appropriate
  1616. */
  1617. static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
  1618. size_t count, loff_t *ppos)
  1619. {
  1620. struct smack_known *skp;
  1621. char *oldambient;
  1622. char *data;
  1623. int rc = count;
  1624. if (!smack_privileged(CAP_MAC_ADMIN))
  1625. return -EPERM;
  1626. /* Enough data must be present */
  1627. if (count == 0 || count > PAGE_SIZE)
  1628. return -EINVAL;
  1629. data = memdup_user_nul(buf, count);
  1630. if (IS_ERR(data))
  1631. return PTR_ERR(data);
  1632. skp = smk_import_entry(data, count);
  1633. if (IS_ERR(skp)) {
  1634. rc = PTR_ERR(skp);
  1635. goto out;
  1636. }
  1637. mutex_lock(&smack_ambient_lock);
  1638. oldambient = smack_net_ambient->smk_known;
  1639. smack_net_ambient = skp;
  1640. smk_unlbl_ambient(oldambient);
  1641. mutex_unlock(&smack_ambient_lock);
  1642. out:
  1643. kfree(data);
  1644. return rc;
  1645. }
  1646. static const struct file_operations smk_ambient_ops = {
  1647. .read = smk_read_ambient,
  1648. .write = smk_write_ambient,
  1649. .llseek = default_llseek,
  1650. };
  1651. /*
  1652. * Seq_file operations for /smack/onlycap
  1653. */
  1654. static void *onlycap_seq_start(struct seq_file *s, loff_t *pos)
  1655. {
  1656. return smk_seq_start(s, pos, &smack_onlycap_list);
  1657. }
  1658. static void *onlycap_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1659. {
  1660. return smk_seq_next(s, v, pos, &smack_onlycap_list);
  1661. }
  1662. static int onlycap_seq_show(struct seq_file *s, void *v)
  1663. {
  1664. struct list_head *list = v;
  1665. struct smack_known_list_elem *sklep =
  1666. list_entry_rcu(list, struct smack_known_list_elem, list);
  1667. seq_puts(s, sklep->smk_label->smk_known);
  1668. seq_putc(s, ' ');
  1669. return 0;
  1670. }
  1671. static const struct seq_operations onlycap_seq_ops = {
  1672. .start = onlycap_seq_start,
  1673. .next = onlycap_seq_next,
  1674. .show = onlycap_seq_show,
  1675. .stop = smk_seq_stop,
  1676. };
  1677. static int smk_open_onlycap(struct inode *inode, struct file *file)
  1678. {
  1679. return seq_open(file, &onlycap_seq_ops);
  1680. }
  1681. /**
  1682. * smk_list_swap_rcu - swap public list with a private one in RCU-safe way
  1683. * The caller must hold appropriate mutex to prevent concurrent modifications
  1684. * to the public list.
  1685. * Private list is assumed to be not accessible to other threads yet.
  1686. *
  1687. * @public: public list
  1688. * @private: private list
  1689. */
  1690. static void smk_list_swap_rcu(struct list_head *public,
  1691. struct list_head *private)
  1692. {
  1693. struct list_head *first, *last;
  1694. if (list_empty(public)) {
  1695. list_splice_init_rcu(private, public, synchronize_rcu);
  1696. } else {
  1697. /* Remember public list before replacing it */
  1698. first = public->next;
  1699. last = public->prev;
  1700. /* Publish private list in place of public in RCU-safe way */
  1701. private->prev->next = public;
  1702. private->next->prev = public;
  1703. rcu_assign_pointer(public->next, private->next);
  1704. public->prev = private->prev;
  1705. synchronize_rcu();
  1706. /* When all readers are done with the old public list,
  1707. * attach it in place of private */
  1708. private->next = first;
  1709. private->prev = last;
  1710. first->prev = private;
  1711. last->next = private;
  1712. }
  1713. }
  1714. /**
  1715. * smk_parse_label_list - parse list of Smack labels, separated by spaces
  1716. *
  1717. * @data: the string to parse
  1718. * @private: destination list
  1719. *
  1720. * Returns zero on success or error code, as appropriate
  1721. */
  1722. static int smk_parse_label_list(char *data, struct list_head *list)
  1723. {
  1724. char *tok;
  1725. struct smack_known *skp;
  1726. struct smack_known_list_elem *sklep;
  1727. while ((tok = strsep(&data, " ")) != NULL) {
  1728. if (!*tok)
  1729. continue;
  1730. skp = smk_import_entry(tok, 0);
  1731. if (IS_ERR(skp))
  1732. return PTR_ERR(skp);
  1733. sklep = kzalloc(sizeof(*sklep), GFP_KERNEL);
  1734. if (sklep == NULL)
  1735. return -ENOMEM;
  1736. sklep->smk_label = skp;
  1737. list_add(&sklep->list, list);
  1738. }
  1739. return 0;
  1740. }
  1741. /**
  1742. * smk_destroy_label_list - destroy a list of smack_known_list_elem
  1743. * @head: header pointer of the list to destroy
  1744. */
  1745. void smk_destroy_label_list(struct list_head *list)
  1746. {
  1747. struct smack_known_list_elem *sklep;
  1748. struct smack_known_list_elem *sklep2;
  1749. list_for_each_entry_safe(sklep, sklep2, list, list)
  1750. kfree(sklep);
  1751. INIT_LIST_HEAD(list);
  1752. }
  1753. /**
  1754. * smk_write_onlycap - write() for smackfs/onlycap
  1755. * @file: file pointer, not actually used
  1756. * @buf: where to get the data from
  1757. * @count: bytes sent
  1758. * @ppos: where to start
  1759. *
  1760. * Returns number of bytes written or error code, as appropriate
  1761. */
  1762. static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
  1763. size_t count, loff_t *ppos)
  1764. {
  1765. char *data;
  1766. LIST_HEAD(list_tmp);
  1767. int rc;
  1768. if (!smack_privileged(CAP_MAC_ADMIN))
  1769. return -EPERM;
  1770. if (count > PAGE_SIZE)
  1771. return -EINVAL;
  1772. data = memdup_user_nul(buf, count);
  1773. if (IS_ERR(data))
  1774. return PTR_ERR(data);
  1775. rc = smk_parse_label_list(data, &list_tmp);
  1776. kfree(data);
  1777. /*
  1778. * Clear the smack_onlycap on invalid label errors. This means
  1779. * that we can pass a null string to unset the onlycap value.
  1780. *
  1781. * Importing will also reject a label beginning with '-',
  1782. * so "-usecapabilities" will also work.
  1783. *
  1784. * But do so only on invalid label, not on system errors.
  1785. * The invalid label must be first to count as clearing attempt.
  1786. */
  1787. if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
  1788. mutex_lock(&smack_onlycap_lock);
  1789. smk_list_swap_rcu(&smack_onlycap_list, &list_tmp);
  1790. mutex_unlock(&smack_onlycap_lock);
  1791. rc = count;
  1792. }
  1793. smk_destroy_label_list(&list_tmp);
  1794. return rc;
  1795. }
  1796. static const struct file_operations smk_onlycap_ops = {
  1797. .open = smk_open_onlycap,
  1798. .read = seq_read,
  1799. .write = smk_write_onlycap,
  1800. .llseek = seq_lseek,
  1801. .release = seq_release,
  1802. };
  1803. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  1804. /**
  1805. * smk_read_unconfined - read() for smackfs/unconfined
  1806. * @filp: file pointer, not actually used
  1807. * @buf: where to put the result
  1808. * @cn: maximum to send along
  1809. * @ppos: where to start
  1810. *
  1811. * Returns number of bytes read or error code, as appropriate
  1812. */
  1813. static ssize_t smk_read_unconfined(struct file *filp, char __user *buf,
  1814. size_t cn, loff_t *ppos)
  1815. {
  1816. char *smack = "";
  1817. ssize_t rc = -EINVAL;
  1818. int asize;
  1819. if (*ppos != 0)
  1820. return 0;
  1821. if (smack_unconfined != NULL)
  1822. smack = smack_unconfined->smk_known;
  1823. asize = strlen(smack) + 1;
  1824. if (cn >= asize)
  1825. rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
  1826. return rc;
  1827. }
  1828. /**
  1829. * smk_write_unconfined - write() for smackfs/unconfined
  1830. * @file: file pointer, not actually used
  1831. * @buf: where to get the data from
  1832. * @count: bytes sent
  1833. * @ppos: where to start
  1834. *
  1835. * Returns number of bytes written or error code, as appropriate
  1836. */
  1837. static ssize_t smk_write_unconfined(struct file *file, const char __user *buf,
  1838. size_t count, loff_t *ppos)
  1839. {
  1840. char *data;
  1841. struct smack_known *skp;
  1842. int rc = count;
  1843. if (!smack_privileged(CAP_MAC_ADMIN))
  1844. return -EPERM;
  1845. if (count > PAGE_SIZE)
  1846. return -EINVAL;
  1847. data = memdup_user_nul(buf, count);
  1848. if (IS_ERR(data))
  1849. return PTR_ERR(data);
  1850. /*
  1851. * Clear the smack_unconfined on invalid label errors. This means
  1852. * that we can pass a null string to unset the unconfined value.
  1853. *
  1854. * Importing will also reject a label beginning with '-',
  1855. * so "-confine" will also work.
  1856. *
  1857. * But do so only on invalid label, not on system errors.
  1858. */
  1859. skp = smk_import_entry(data, count);
  1860. if (PTR_ERR(skp) == -EINVAL)
  1861. skp = NULL;
  1862. else if (IS_ERR(skp)) {
  1863. rc = PTR_ERR(skp);
  1864. goto freeout;
  1865. }
  1866. smack_unconfined = skp;
  1867. freeout:
  1868. kfree(data);
  1869. return rc;
  1870. }
  1871. static const struct file_operations smk_unconfined_ops = {
  1872. .read = smk_read_unconfined,
  1873. .write = smk_write_unconfined,
  1874. .llseek = default_llseek,
  1875. };
  1876. #endif /* CONFIG_SECURITY_SMACK_BRINGUP */
  1877. /**
  1878. * smk_read_logging - read() for /smack/logging
  1879. * @filp: file pointer, not actually used
  1880. * @buf: where to put the result
  1881. * @cn: maximum to send along
  1882. * @ppos: where to start
  1883. *
  1884. * Returns number of bytes read or error code, as appropriate
  1885. */
  1886. static ssize_t smk_read_logging(struct file *filp, char __user *buf,
  1887. size_t count, loff_t *ppos)
  1888. {
  1889. char temp[32];
  1890. ssize_t rc;
  1891. if (*ppos != 0)
  1892. return 0;
  1893. sprintf(temp, "%d\n", log_policy);
  1894. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1895. return rc;
  1896. }
  1897. /**
  1898. * smk_write_logging - write() for /smack/logging
  1899. * @file: file pointer, not actually used
  1900. * @buf: where to get the data from
  1901. * @count: bytes sent
  1902. * @ppos: where to start
  1903. *
  1904. * Returns number of bytes written or error code, as appropriate
  1905. */
  1906. static ssize_t smk_write_logging(struct file *file, const char __user *buf,
  1907. size_t count, loff_t *ppos)
  1908. {
  1909. char temp[32];
  1910. int i;
  1911. if (!smack_privileged(CAP_MAC_ADMIN))
  1912. return -EPERM;
  1913. if (count >= sizeof(temp) || count == 0)
  1914. return -EINVAL;
  1915. if (copy_from_user(temp, buf, count) != 0)
  1916. return -EFAULT;
  1917. temp[count] = '\0';
  1918. if (sscanf(temp, "%d", &i) != 1)
  1919. return -EINVAL;
  1920. if (i < 0 || i > 3)
  1921. return -EINVAL;
  1922. log_policy = i;
  1923. return count;
  1924. }
  1925. static const struct file_operations smk_logging_ops = {
  1926. .read = smk_read_logging,
  1927. .write = smk_write_logging,
  1928. .llseek = default_llseek,
  1929. };
  1930. /*
  1931. * Seq_file read operations for /smack/load-self
  1932. */
  1933. static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
  1934. {
  1935. struct task_smack *tsp = current_security();
  1936. return smk_seq_start(s, pos, &tsp->smk_rules);
  1937. }
  1938. static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1939. {
  1940. struct task_smack *tsp = current_security();
  1941. return smk_seq_next(s, v, pos, &tsp->smk_rules);
  1942. }
  1943. static int load_self_seq_show(struct seq_file *s, void *v)
  1944. {
  1945. struct list_head *list = v;
  1946. struct smack_rule *srp =
  1947. list_entry_rcu(list, struct smack_rule, list);
  1948. smk_rule_show(s, srp, SMK_LABELLEN);
  1949. return 0;
  1950. }
  1951. static const struct seq_operations load_self_seq_ops = {
  1952. .start = load_self_seq_start,
  1953. .next = load_self_seq_next,
  1954. .show = load_self_seq_show,
  1955. .stop = smk_seq_stop,
  1956. };
  1957. /**
  1958. * smk_open_load_self - open() for /smack/load-self2
  1959. * @inode: inode structure representing file
  1960. * @file: "load" file pointer
  1961. *
  1962. * For reading, use load_seq_* seq_file reading operations.
  1963. */
  1964. static int smk_open_load_self(struct inode *inode, struct file *file)
  1965. {
  1966. return seq_open(file, &load_self_seq_ops);
  1967. }
  1968. /**
  1969. * smk_write_load_self - write() for /smack/load-self
  1970. * @file: file pointer, not actually used
  1971. * @buf: where to get the data from
  1972. * @count: bytes sent
  1973. * @ppos: where to start - must be 0
  1974. *
  1975. */
  1976. static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
  1977. size_t count, loff_t *ppos)
  1978. {
  1979. struct task_smack *tsp = current_security();
  1980. return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
  1981. &tsp->smk_rules_lock, SMK_FIXED24_FMT);
  1982. }
  1983. static const struct file_operations smk_load_self_ops = {
  1984. .open = smk_open_load_self,
  1985. .read = seq_read,
  1986. .llseek = seq_lseek,
  1987. .write = smk_write_load_self,
  1988. .release = seq_release,
  1989. };
  1990. /**
  1991. * smk_user_access - handle access check transaction
  1992. * @file: file pointer
  1993. * @buf: data from user space
  1994. * @count: bytes sent
  1995. * @ppos: where to start - must be 0
  1996. */
  1997. static ssize_t smk_user_access(struct file *file, const char __user *buf,
  1998. size_t count, loff_t *ppos, int format)
  1999. {
  2000. struct smack_parsed_rule rule;
  2001. char *data;
  2002. int res;
  2003. data = simple_transaction_get(file, buf, count);
  2004. if (IS_ERR(data))
  2005. return PTR_ERR(data);
  2006. if (format == SMK_FIXED24_FMT) {
  2007. if (count < SMK_LOADLEN)
  2008. return -EINVAL;
  2009. res = smk_parse_rule(data, &rule, 0);
  2010. } else {
  2011. /*
  2012. * simple_transaction_get() returns null-terminated data
  2013. */
  2014. res = smk_parse_long_rule(data, &rule, 0, 3);
  2015. }
  2016. if (res >= 0)
  2017. res = smk_access(rule.smk_subject, rule.smk_object,
  2018. rule.smk_access1, NULL);
  2019. else if (res != -ENOENT)
  2020. return res;
  2021. /*
  2022. * smk_access() can return a value > 0 in the "bringup" case.
  2023. */
  2024. data[0] = res >= 0 ? '1' : '0';
  2025. data[1] = '\0';
  2026. simple_transaction_set(file, 2);
  2027. if (format == SMK_FIXED24_FMT)
  2028. return SMK_LOADLEN;
  2029. return count;
  2030. }
  2031. /**
  2032. * smk_write_access - handle access check transaction
  2033. * @file: file pointer
  2034. * @buf: data from user space
  2035. * @count: bytes sent
  2036. * @ppos: where to start - must be 0
  2037. */
  2038. static ssize_t smk_write_access(struct file *file, const char __user *buf,
  2039. size_t count, loff_t *ppos)
  2040. {
  2041. return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
  2042. }
  2043. static const struct file_operations smk_access_ops = {
  2044. .write = smk_write_access,
  2045. .read = simple_transaction_read,
  2046. .release = simple_transaction_release,
  2047. .llseek = generic_file_llseek,
  2048. };
  2049. /*
  2050. * Seq_file read operations for /smack/load2
  2051. */
  2052. static int load2_seq_show(struct seq_file *s, void *v)
  2053. {
  2054. struct list_head *list = v;
  2055. struct smack_master_list *smlp =
  2056. list_entry_rcu(list, struct smack_master_list, list);
  2057. smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
  2058. return 0;
  2059. }
  2060. static const struct seq_operations load2_seq_ops = {
  2061. .start = load2_seq_start,
  2062. .next = load2_seq_next,
  2063. .show = load2_seq_show,
  2064. .stop = smk_seq_stop,
  2065. };
  2066. /**
  2067. * smk_open_load2 - open() for /smack/load2
  2068. * @inode: inode structure representing file
  2069. * @file: "load2" file pointer
  2070. *
  2071. * For reading, use load2_seq_* seq_file reading operations.
  2072. */
  2073. static int smk_open_load2(struct inode *inode, struct file *file)
  2074. {
  2075. return seq_open(file, &load2_seq_ops);
  2076. }
  2077. /**
  2078. * smk_write_load2 - write() for /smack/load2
  2079. * @file: file pointer, not actually used
  2080. * @buf: where to get the data from
  2081. * @count: bytes sent
  2082. * @ppos: where to start - must be 0
  2083. *
  2084. */
  2085. static ssize_t smk_write_load2(struct file *file, const char __user *buf,
  2086. size_t count, loff_t *ppos)
  2087. {
  2088. /*
  2089. * Must have privilege.
  2090. */
  2091. if (!smack_privileged(CAP_MAC_ADMIN))
  2092. return -EPERM;
  2093. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  2094. SMK_LONG_FMT);
  2095. }
  2096. static const struct file_operations smk_load2_ops = {
  2097. .open = smk_open_load2,
  2098. .read = seq_read,
  2099. .llseek = seq_lseek,
  2100. .write = smk_write_load2,
  2101. .release = seq_release,
  2102. };
  2103. /*
  2104. * Seq_file read operations for /smack/load-self2
  2105. */
  2106. static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
  2107. {
  2108. struct task_smack *tsp = current_security();
  2109. return smk_seq_start(s, pos, &tsp->smk_rules);
  2110. }
  2111. static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
  2112. {
  2113. struct task_smack *tsp = current_security();
  2114. return smk_seq_next(s, v, pos, &tsp->smk_rules);
  2115. }
  2116. static int load_self2_seq_show(struct seq_file *s, void *v)
  2117. {
  2118. struct list_head *list = v;
  2119. struct smack_rule *srp =
  2120. list_entry_rcu(list, struct smack_rule, list);
  2121. smk_rule_show(s, srp, SMK_LONGLABEL);
  2122. return 0;
  2123. }
  2124. static const struct seq_operations load_self2_seq_ops = {
  2125. .start = load_self2_seq_start,
  2126. .next = load_self2_seq_next,
  2127. .show = load_self2_seq_show,
  2128. .stop = smk_seq_stop,
  2129. };
  2130. /**
  2131. * smk_open_load_self2 - open() for /smack/load-self2
  2132. * @inode: inode structure representing file
  2133. * @file: "load" file pointer
  2134. *
  2135. * For reading, use load_seq_* seq_file reading operations.
  2136. */
  2137. static int smk_open_load_self2(struct inode *inode, struct file *file)
  2138. {
  2139. return seq_open(file, &load_self2_seq_ops);
  2140. }
  2141. /**
  2142. * smk_write_load_self2 - write() for /smack/load-self2
  2143. * @file: file pointer, not actually used
  2144. * @buf: where to get the data from
  2145. * @count: bytes sent
  2146. * @ppos: where to start - must be 0
  2147. *
  2148. */
  2149. static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
  2150. size_t count, loff_t *ppos)
  2151. {
  2152. struct task_smack *tsp = current_security();
  2153. return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
  2154. &tsp->smk_rules_lock, SMK_LONG_FMT);
  2155. }
  2156. static const struct file_operations smk_load_self2_ops = {
  2157. .open = smk_open_load_self2,
  2158. .read = seq_read,
  2159. .llseek = seq_lseek,
  2160. .write = smk_write_load_self2,
  2161. .release = seq_release,
  2162. };
  2163. /**
  2164. * smk_write_access2 - handle access check transaction
  2165. * @file: file pointer
  2166. * @buf: data from user space
  2167. * @count: bytes sent
  2168. * @ppos: where to start - must be 0
  2169. */
  2170. static ssize_t smk_write_access2(struct file *file, const char __user *buf,
  2171. size_t count, loff_t *ppos)
  2172. {
  2173. return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
  2174. }
  2175. static const struct file_operations smk_access2_ops = {
  2176. .write = smk_write_access2,
  2177. .read = simple_transaction_read,
  2178. .release = simple_transaction_release,
  2179. .llseek = generic_file_llseek,
  2180. };
  2181. /**
  2182. * smk_write_revoke_subj - write() for /smack/revoke-subject
  2183. * @file: file pointer
  2184. * @buf: data from user space
  2185. * @count: bytes sent
  2186. * @ppos: where to start - must be 0
  2187. */
  2188. static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
  2189. size_t count, loff_t *ppos)
  2190. {
  2191. char *data;
  2192. const char *cp;
  2193. struct smack_known *skp;
  2194. struct smack_rule *sp;
  2195. struct list_head *rule_list;
  2196. struct mutex *rule_lock;
  2197. int rc = count;
  2198. if (*ppos != 0)
  2199. return -EINVAL;
  2200. if (!smack_privileged(CAP_MAC_ADMIN))
  2201. return -EPERM;
  2202. if (count == 0 || count > SMK_LONGLABEL)
  2203. return -EINVAL;
  2204. data = memdup_user(buf, count);
  2205. if (IS_ERR(data))
  2206. return PTR_ERR(data);
  2207. cp = smk_parse_smack(data, count);
  2208. if (IS_ERR(cp)) {
  2209. rc = PTR_ERR(cp);
  2210. goto out_data;
  2211. }
  2212. skp = smk_find_entry(cp);
  2213. if (skp == NULL)
  2214. goto out_cp;
  2215. rule_list = &skp->smk_rules;
  2216. rule_lock = &skp->smk_rules_lock;
  2217. mutex_lock(rule_lock);
  2218. list_for_each_entry_rcu(sp, rule_list, list)
  2219. sp->smk_access = 0;
  2220. mutex_unlock(rule_lock);
  2221. out_cp:
  2222. kfree(cp);
  2223. out_data:
  2224. kfree(data);
  2225. return rc;
  2226. }
  2227. static const struct file_operations smk_revoke_subj_ops = {
  2228. .write = smk_write_revoke_subj,
  2229. .read = simple_transaction_read,
  2230. .release = simple_transaction_release,
  2231. .llseek = generic_file_llseek,
  2232. };
  2233. /**
  2234. * smk_init_sysfs - initialize /sys/fs/smackfs
  2235. *
  2236. */
  2237. static int smk_init_sysfs(void)
  2238. {
  2239. return sysfs_create_mount_point(fs_kobj, "smackfs");
  2240. }
  2241. /**
  2242. * smk_write_change_rule - write() for /smack/change-rule
  2243. * @file: file pointer
  2244. * @buf: data from user space
  2245. * @count: bytes sent
  2246. * @ppos: where to start - must be 0
  2247. */
  2248. static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
  2249. size_t count, loff_t *ppos)
  2250. {
  2251. /*
  2252. * Must have privilege.
  2253. */
  2254. if (!smack_privileged(CAP_MAC_ADMIN))
  2255. return -EPERM;
  2256. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  2257. SMK_CHANGE_FMT);
  2258. }
  2259. static const struct file_operations smk_change_rule_ops = {
  2260. .write = smk_write_change_rule,
  2261. .read = simple_transaction_read,
  2262. .release = simple_transaction_release,
  2263. .llseek = generic_file_llseek,
  2264. };
  2265. /**
  2266. * smk_read_syslog - read() for smackfs/syslog
  2267. * @filp: file pointer, not actually used
  2268. * @buf: where to put the result
  2269. * @cn: maximum to send along
  2270. * @ppos: where to start
  2271. *
  2272. * Returns number of bytes read or error code, as appropriate
  2273. */
  2274. static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
  2275. size_t cn, loff_t *ppos)
  2276. {
  2277. struct smack_known *skp;
  2278. ssize_t rc = -EINVAL;
  2279. int asize;
  2280. if (*ppos != 0)
  2281. return 0;
  2282. if (smack_syslog_label == NULL)
  2283. skp = &smack_known_star;
  2284. else
  2285. skp = smack_syslog_label;
  2286. asize = strlen(skp->smk_known) + 1;
  2287. if (cn >= asize)
  2288. rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
  2289. asize);
  2290. return rc;
  2291. }
  2292. /**
  2293. * smk_write_syslog - write() for smackfs/syslog
  2294. * @file: file pointer, not actually used
  2295. * @buf: where to get the data from
  2296. * @count: bytes sent
  2297. * @ppos: where to start
  2298. *
  2299. * Returns number of bytes written or error code, as appropriate
  2300. */
  2301. static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
  2302. size_t count, loff_t *ppos)
  2303. {
  2304. char *data;
  2305. struct smack_known *skp;
  2306. int rc = count;
  2307. if (!smack_privileged(CAP_MAC_ADMIN))
  2308. return -EPERM;
  2309. /* Enough data must be present */
  2310. if (count == 0 || count > PAGE_SIZE)
  2311. return -EINVAL;
  2312. data = memdup_user_nul(buf, count);
  2313. if (IS_ERR(data))
  2314. return PTR_ERR(data);
  2315. skp = smk_import_entry(data, count);
  2316. if (IS_ERR(skp))
  2317. rc = PTR_ERR(skp);
  2318. else
  2319. smack_syslog_label = skp;
  2320. kfree(data);
  2321. return rc;
  2322. }
  2323. static const struct file_operations smk_syslog_ops = {
  2324. .read = smk_read_syslog,
  2325. .write = smk_write_syslog,
  2326. .llseek = default_llseek,
  2327. };
  2328. /*
  2329. * Seq_file read operations for /smack/relabel-self
  2330. */
  2331. static void *relabel_self_seq_start(struct seq_file *s, loff_t *pos)
  2332. {
  2333. struct task_smack *tsp = current_security();
  2334. return smk_seq_start(s, pos, &tsp->smk_relabel);
  2335. }
  2336. static void *relabel_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
  2337. {
  2338. struct task_smack *tsp = current_security();
  2339. return smk_seq_next(s, v, pos, &tsp->smk_relabel);
  2340. }
  2341. static int relabel_self_seq_show(struct seq_file *s, void *v)
  2342. {
  2343. struct list_head *list = v;
  2344. struct smack_known_list_elem *sklep =
  2345. list_entry(list, struct smack_known_list_elem, list);
  2346. seq_puts(s, sklep->smk_label->smk_known);
  2347. seq_putc(s, ' ');
  2348. return 0;
  2349. }
  2350. static const struct seq_operations relabel_self_seq_ops = {
  2351. .start = relabel_self_seq_start,
  2352. .next = relabel_self_seq_next,
  2353. .show = relabel_self_seq_show,
  2354. .stop = smk_seq_stop,
  2355. };
  2356. /**
  2357. * smk_open_relabel_self - open() for /smack/relabel-self
  2358. * @inode: inode structure representing file
  2359. * @file: "relabel-self" file pointer
  2360. *
  2361. * Connect our relabel_self_seq_* operations with /smack/relabel-self
  2362. * file_operations
  2363. */
  2364. static int smk_open_relabel_self(struct inode *inode, struct file *file)
  2365. {
  2366. return seq_open(file, &relabel_self_seq_ops);
  2367. }
  2368. /**
  2369. * smk_write_relabel_self - write() for /smack/relabel-self
  2370. * @file: file pointer, not actually used
  2371. * @buf: where to get the data from
  2372. * @count: bytes sent
  2373. * @ppos: where to start - must be 0
  2374. *
  2375. */
  2376. static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
  2377. size_t count, loff_t *ppos)
  2378. {
  2379. char *data;
  2380. int rc;
  2381. LIST_HEAD(list_tmp);
  2382. /*
  2383. * Must have privilege.
  2384. */
  2385. if (!smack_privileged(CAP_MAC_ADMIN))
  2386. return -EPERM;
  2387. /*
  2388. * No partial write.
  2389. * Enough data must be present.
  2390. */
  2391. if (*ppos != 0)
  2392. return -EINVAL;
  2393. if (count == 0 || count > PAGE_SIZE)
  2394. return -EINVAL;
  2395. data = memdup_user_nul(buf, count);
  2396. if (IS_ERR(data))
  2397. return PTR_ERR(data);
  2398. rc = smk_parse_label_list(data, &list_tmp);
  2399. kfree(data);
  2400. if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
  2401. struct cred *new;
  2402. struct task_smack *tsp;
  2403. new = prepare_creds();
  2404. if (!new) {
  2405. rc = -ENOMEM;
  2406. goto out;
  2407. }
  2408. tsp = new->security;
  2409. smk_destroy_label_list(&tsp->smk_relabel);
  2410. list_splice(&list_tmp, &tsp->smk_relabel);
  2411. commit_creds(new);
  2412. return count;
  2413. }
  2414. out:
  2415. smk_destroy_label_list(&list_tmp);
  2416. return rc;
  2417. }
  2418. static const struct file_operations smk_relabel_self_ops = {
  2419. .open = smk_open_relabel_self,
  2420. .read = seq_read,
  2421. .llseek = seq_lseek,
  2422. .write = smk_write_relabel_self,
  2423. .release = seq_release,
  2424. };
  2425. /**
  2426. * smk_read_ptrace - read() for /smack/ptrace
  2427. * @filp: file pointer, not actually used
  2428. * @buf: where to put the result
  2429. * @count: maximum to send along
  2430. * @ppos: where to start
  2431. *
  2432. * Returns number of bytes read or error code, as appropriate
  2433. */
  2434. static ssize_t smk_read_ptrace(struct file *filp, char __user *buf,
  2435. size_t count, loff_t *ppos)
  2436. {
  2437. char temp[32];
  2438. ssize_t rc;
  2439. if (*ppos != 0)
  2440. return 0;
  2441. sprintf(temp, "%d\n", smack_ptrace_rule);
  2442. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  2443. return rc;
  2444. }
  2445. /**
  2446. * smk_write_ptrace - write() for /smack/ptrace
  2447. * @file: file pointer
  2448. * @buf: data from user space
  2449. * @count: bytes sent
  2450. * @ppos: where to start - must be 0
  2451. */
  2452. static ssize_t smk_write_ptrace(struct file *file, const char __user *buf,
  2453. size_t count, loff_t *ppos)
  2454. {
  2455. char temp[32];
  2456. int i;
  2457. if (!smack_privileged(CAP_MAC_ADMIN))
  2458. return -EPERM;
  2459. if (*ppos != 0 || count >= sizeof(temp) || count == 0)
  2460. return -EINVAL;
  2461. if (copy_from_user(temp, buf, count) != 0)
  2462. return -EFAULT;
  2463. temp[count] = '\0';
  2464. if (sscanf(temp, "%d", &i) != 1)
  2465. return -EINVAL;
  2466. if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX)
  2467. return -EINVAL;
  2468. smack_ptrace_rule = i;
  2469. return count;
  2470. }
  2471. static const struct file_operations smk_ptrace_ops = {
  2472. .write = smk_write_ptrace,
  2473. .read = smk_read_ptrace,
  2474. .llseek = default_llseek,
  2475. };
  2476. /**
  2477. * smk_fill_super - fill the smackfs superblock
  2478. * @sb: the empty superblock
  2479. * @data: unused
  2480. * @silent: unused
  2481. *
  2482. * Fill in the well known entries for the smack filesystem
  2483. *
  2484. * Returns 0 on success, an error code on failure
  2485. */
  2486. static int smk_fill_super(struct super_block *sb, void *data, int silent)
  2487. {
  2488. int rc;
  2489. struct inode *root_inode;
  2490. static const struct tree_descr smack_files[] = {
  2491. [SMK_LOAD] = {
  2492. "load", &smk_load_ops, S_IRUGO|S_IWUSR},
  2493. [SMK_CIPSO] = {
  2494. "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
  2495. [SMK_DOI] = {
  2496. "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
  2497. [SMK_DIRECT] = {
  2498. "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
  2499. [SMK_AMBIENT] = {
  2500. "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
  2501. [SMK_NET4ADDR] = {
  2502. "netlabel", &smk_net4addr_ops, S_IRUGO|S_IWUSR},
  2503. [SMK_ONLYCAP] = {
  2504. "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
  2505. [SMK_LOGGING] = {
  2506. "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
  2507. [SMK_LOAD_SELF] = {
  2508. "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
  2509. [SMK_ACCESSES] = {
  2510. "access", &smk_access_ops, S_IRUGO|S_IWUGO},
  2511. [SMK_MAPPED] = {
  2512. "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
  2513. [SMK_LOAD2] = {
  2514. "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
  2515. [SMK_LOAD_SELF2] = {
  2516. "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
  2517. [SMK_ACCESS2] = {
  2518. "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
  2519. [SMK_CIPSO2] = {
  2520. "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
  2521. [SMK_REVOKE_SUBJ] = {
  2522. "revoke-subject", &smk_revoke_subj_ops,
  2523. S_IRUGO|S_IWUSR},
  2524. [SMK_CHANGE_RULE] = {
  2525. "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
  2526. [SMK_SYSLOG] = {
  2527. "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
  2528. [SMK_PTRACE] = {
  2529. "ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR},
  2530. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  2531. [SMK_UNCONFINED] = {
  2532. "unconfined", &smk_unconfined_ops, S_IRUGO|S_IWUSR},
  2533. #endif
  2534. #if IS_ENABLED(CONFIG_IPV6)
  2535. [SMK_NET6ADDR] = {
  2536. "ipv6host", &smk_net6addr_ops, S_IRUGO|S_IWUSR},
  2537. #endif /* CONFIG_IPV6 */
  2538. [SMK_RELABEL_SELF] = {
  2539. "relabel-self", &smk_relabel_self_ops,
  2540. S_IRUGO|S_IWUGO},
  2541. /* last one */
  2542. {""}
  2543. };
  2544. rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
  2545. if (rc != 0) {
  2546. printk(KERN_ERR "%s failed %d while creating inodes\n",
  2547. __func__, rc);
  2548. return rc;
  2549. }
  2550. root_inode = d_inode(sb->s_root);
  2551. return 0;
  2552. }
  2553. /**
  2554. * smk_mount - get the smackfs superblock
  2555. * @fs_type: passed along without comment
  2556. * @flags: passed along without comment
  2557. * @dev_name: passed along without comment
  2558. * @data: passed along without comment
  2559. *
  2560. * Just passes everything along.
  2561. *
  2562. * Returns what the lower level code does.
  2563. */
  2564. static struct dentry *smk_mount(struct file_system_type *fs_type,
  2565. int flags, const char *dev_name, void *data)
  2566. {
  2567. return mount_single(fs_type, flags, data, smk_fill_super);
  2568. }
  2569. static struct file_system_type smk_fs_type = {
  2570. .name = "smackfs",
  2571. .mount = smk_mount,
  2572. .kill_sb = kill_litter_super,
  2573. };
  2574. static struct vfsmount *smackfs_mount;
  2575. static int __init smk_preset_netlabel(struct smack_known *skp)
  2576. {
  2577. skp->smk_netlabel.domain = skp->smk_known;
  2578. skp->smk_netlabel.flags =
  2579. NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
  2580. return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
  2581. &skp->smk_netlabel, strlen(skp->smk_known));
  2582. }
  2583. /**
  2584. * init_smk_fs - get the smackfs superblock
  2585. *
  2586. * register the smackfs
  2587. *
  2588. * Do not register smackfs if Smack wasn't enabled
  2589. * on boot. We can not put this method normally under the
  2590. * smack_init() code path since the security subsystem get
  2591. * initialized before the vfs caches.
  2592. *
  2593. * Returns true if we were not chosen on boot or if
  2594. * we were chosen and filesystem registration succeeded.
  2595. */
  2596. static int __init init_smk_fs(void)
  2597. {
  2598. int err;
  2599. int rc;
  2600. if (smack_enabled == 0)
  2601. return 0;
  2602. err = smk_init_sysfs();
  2603. if (err)
  2604. printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
  2605. err = register_filesystem(&smk_fs_type);
  2606. if (!err) {
  2607. smackfs_mount = kern_mount(&smk_fs_type);
  2608. if (IS_ERR(smackfs_mount)) {
  2609. printk(KERN_ERR "smackfs: could not mount!\n");
  2610. err = PTR_ERR(smackfs_mount);
  2611. smackfs_mount = NULL;
  2612. }
  2613. }
  2614. smk_cipso_doi();
  2615. smk_unlbl_ambient(NULL);
  2616. rc = smk_preset_netlabel(&smack_known_floor);
  2617. if (err == 0 && rc < 0)
  2618. err = rc;
  2619. rc = smk_preset_netlabel(&smack_known_hat);
  2620. if (err == 0 && rc < 0)
  2621. err = rc;
  2622. rc = smk_preset_netlabel(&smack_known_huh);
  2623. if (err == 0 && rc < 0)
  2624. err = rc;
  2625. rc = smk_preset_netlabel(&smack_known_star);
  2626. if (err == 0 && rc < 0)
  2627. err = rc;
  2628. rc = smk_preset_netlabel(&smack_known_web);
  2629. if (err == 0 && rc < 0)
  2630. err = rc;
  2631. return err;
  2632. }
  2633. __initcall(init_smk_fs);