smackfs.c 54 KB

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