smackfs.c 54 KB

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