smackfs.c 60 KB

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