smackfs.c 54 KB

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