smackfs.c 50 KB

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