smackfs.c 69 KB

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