smackfs.c 54 KB

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