smackfs.c 70 KB

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