smackfs.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  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/netlabel.h>
  25. #include <net/cipso_ipv4.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/ctype.h>
  28. #include <linux/audit.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. };
  46. /*
  47. * List locks
  48. */
  49. static DEFINE_MUTEX(smack_list_lock);
  50. static DEFINE_MUTEX(smack_cipso_lock);
  51. static DEFINE_MUTEX(smack_ambient_lock);
  52. static DEFINE_MUTEX(smk_netlbladdr_lock);
  53. /*
  54. * This is the "ambient" label for network traffic.
  55. * If it isn't somehow marked, use this.
  56. * It can be reset via smackfs/ambient
  57. */
  58. char *smack_net_ambient = smack_known_floor.smk_known;
  59. /*
  60. * This is the level in a CIPSO header that indicates a
  61. * smack label is contained directly in the category set.
  62. * It can be reset via smackfs/direct
  63. */
  64. int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
  65. /*
  66. * Unless a process is running with this label even
  67. * having CAP_MAC_OVERRIDE isn't enough to grant
  68. * privilege to violate MAC policy. If no label is
  69. * designated (the NULL case) capabilities apply to
  70. * everyone. It is expected that the hat (^) label
  71. * will be used if any label is used.
  72. */
  73. char *smack_onlycap;
  74. /*
  75. * Certain IP addresses may be designated as single label hosts.
  76. * Packets are sent there unlabeled, but only from tasks that
  77. * can write to the specified label.
  78. */
  79. LIST_HEAD(smk_netlbladdr_list);
  80. /*
  81. * Rule lists are maintained for each label.
  82. * This master list is just for reading /smack/load.
  83. */
  84. struct smack_master_list {
  85. struct list_head list;
  86. struct smack_rule *smk_rule;
  87. };
  88. LIST_HEAD(smack_rule_list);
  89. static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
  90. const char *smack_cipso_option = SMACK_CIPSO_OPTION;
  91. /*
  92. * Values for parsing cipso rules
  93. * SMK_DIGITLEN: Length of a digit field in a rule.
  94. * SMK_CIPSOMIN: Minimum possible cipso rule length.
  95. * SMK_CIPSOMAX: Maximum possible cipso rule length.
  96. */
  97. #define SMK_DIGITLEN 4
  98. #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
  99. #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
  100. /*
  101. * Values for parsing MAC rules
  102. * SMK_ACCESS: Maximum possible combination of access permissions
  103. * SMK_ACCESSLEN: Maximum length for a rule access field
  104. * SMK_LOADLEN: Smack rule length
  105. */
  106. #define SMK_OACCESS "rwxa"
  107. #define SMK_ACCESS "rwxat"
  108. #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
  109. #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
  110. #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
  111. #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
  112. /**
  113. * smk_netlabel_audit_set - fill a netlbl_audit struct
  114. * @nap: structure to fill
  115. */
  116. static void smk_netlabel_audit_set(struct netlbl_audit *nap)
  117. {
  118. nap->loginuid = audit_get_loginuid(current);
  119. nap->sessionid = audit_get_sessionid(current);
  120. nap->secid = smack_to_secid(smk_of_current());
  121. }
  122. /*
  123. * Values for parsing single label host rules
  124. * "1.2.3.4 X"
  125. * "192.168.138.129/32 abcdefghijklmnopqrstuvw"
  126. */
  127. #define SMK_NETLBLADDRMIN 9
  128. #define SMK_NETLBLADDRMAX 42
  129. /**
  130. * smk_set_access - add a rule to the rule list
  131. * @srp: the new rule to add
  132. * @rule_list: the list of rules
  133. * @rule_lock: the rule list lock
  134. *
  135. * Looks through the current subject/object/access list for
  136. * the subject/object pair and replaces the access that was
  137. * there. If the pair isn't found add it with the specified
  138. * access.
  139. *
  140. * Returns 1 if a rule was found to exist already, 0 if it is new
  141. * Returns 0 if nothing goes wrong or -ENOMEM if it fails
  142. * during the allocation of the new pair to add.
  143. */
  144. static int smk_set_access(struct smack_rule *srp, struct list_head *rule_list,
  145. struct mutex *rule_lock)
  146. {
  147. struct smack_rule *sp;
  148. int found = 0;
  149. mutex_lock(rule_lock);
  150. /*
  151. * Because the object label is less likely to match
  152. * than the subject label check it first
  153. */
  154. list_for_each_entry_rcu(sp, rule_list, list) {
  155. if (sp->smk_object == srp->smk_object &&
  156. sp->smk_subject == srp->smk_subject) {
  157. found = 1;
  158. sp->smk_access = srp->smk_access;
  159. break;
  160. }
  161. }
  162. if (found == 0)
  163. list_add_rcu(&srp->list, rule_list);
  164. mutex_unlock(rule_lock);
  165. return found;
  166. }
  167. /**
  168. * smk_parse_rule - parse Smack rule from load string
  169. * @data: string to be parsed whose size is SMK_LOADLEN
  170. * @rule: Smack rule
  171. * @import: if non-zero, import labels
  172. */
  173. static int smk_parse_rule(const char *data, struct smack_rule *rule, int import)
  174. {
  175. char smack[SMK_LABELLEN];
  176. struct smack_known *skp;
  177. if (import) {
  178. rule->smk_subject = smk_import(data, 0);
  179. if (rule->smk_subject == NULL)
  180. return -1;
  181. rule->smk_object = smk_import(data + SMK_LABELLEN, 0);
  182. if (rule->smk_object == NULL)
  183. return -1;
  184. } else {
  185. smk_parse_smack(data, 0, smack);
  186. skp = smk_find_entry(smack);
  187. if (skp == NULL)
  188. return -1;
  189. rule->smk_subject = skp->smk_known;
  190. smk_parse_smack(data + SMK_LABELLEN, 0, smack);
  191. skp = smk_find_entry(smack);
  192. if (skp == NULL)
  193. return -1;
  194. rule->smk_object = skp->smk_known;
  195. }
  196. rule->smk_access = 0;
  197. switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
  198. case '-':
  199. break;
  200. case 'r':
  201. case 'R':
  202. rule->smk_access |= MAY_READ;
  203. break;
  204. default:
  205. return -1;
  206. }
  207. switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
  208. case '-':
  209. break;
  210. case 'w':
  211. case 'W':
  212. rule->smk_access |= MAY_WRITE;
  213. break;
  214. default:
  215. return -1;
  216. }
  217. switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
  218. case '-':
  219. break;
  220. case 'x':
  221. case 'X':
  222. rule->smk_access |= MAY_EXEC;
  223. break;
  224. default:
  225. return -1;
  226. }
  227. switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
  228. case '-':
  229. break;
  230. case 'a':
  231. case 'A':
  232. rule->smk_access |= MAY_APPEND;
  233. break;
  234. default:
  235. return -1;
  236. }
  237. switch (data[SMK_LABELLEN + SMK_LABELLEN + 4]) {
  238. case '-':
  239. break;
  240. case 't':
  241. case 'T':
  242. rule->smk_access |= MAY_TRANSMUTE;
  243. break;
  244. default:
  245. return -1;
  246. }
  247. return 0;
  248. }
  249. /**
  250. * smk_write_load_list - write() for any /smack/load
  251. * @file: file pointer, not actually used
  252. * @buf: where to get the data from
  253. * @count: bytes sent
  254. * @ppos: where to start - must be 0
  255. * @rule_list: the list of rules to write to
  256. * @rule_lock: lock for the rule list
  257. *
  258. * Get one smack access rule from above.
  259. * The format is exactly:
  260. * char subject[SMK_LABELLEN]
  261. * char object[SMK_LABELLEN]
  262. * char access[SMK_ACCESSLEN]
  263. *
  264. * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
  265. */
  266. static ssize_t smk_write_load_list(struct file *file, const char __user *buf,
  267. size_t count, loff_t *ppos,
  268. struct list_head *rule_list,
  269. struct mutex *rule_lock)
  270. {
  271. struct smack_master_list *smlp;
  272. struct smack_known *skp;
  273. struct smack_rule *rule;
  274. char *data;
  275. int rc = -EINVAL;
  276. int load = 0;
  277. /*
  278. * No partial writes.
  279. * Enough data must be present.
  280. */
  281. if (*ppos != 0)
  282. return -EINVAL;
  283. /*
  284. * Minor hack for backward compatibility
  285. */
  286. if (count < (SMK_OLOADLEN) || count > SMK_LOADLEN)
  287. return -EINVAL;
  288. data = kzalloc(SMK_LOADLEN, GFP_KERNEL);
  289. if (data == NULL)
  290. return -ENOMEM;
  291. if (copy_from_user(data, buf, count) != 0) {
  292. rc = -EFAULT;
  293. goto out;
  294. }
  295. /*
  296. * More on the minor hack for backward compatibility
  297. */
  298. if (count == (SMK_OLOADLEN))
  299. data[SMK_OLOADLEN] = '-';
  300. rule = kzalloc(sizeof(*rule), GFP_KERNEL);
  301. if (rule == NULL) {
  302. rc = -ENOMEM;
  303. goto out;
  304. }
  305. if (smk_parse_rule(data, rule, 1))
  306. goto out_free_rule;
  307. if (rule_list == NULL) {
  308. load = 1;
  309. skp = smk_find_entry(rule->smk_subject);
  310. rule_list = &skp->smk_rules;
  311. rule_lock = &skp->smk_rules_lock;
  312. }
  313. rc = count;
  314. /*
  315. * If this is "load" as opposed to "load-self" and a new rule
  316. * it needs to get added for reporting.
  317. * smk_set_access returns true if there was already a rule
  318. * for the subject/object pair, and false if it was new.
  319. */
  320. if (load && !smk_set_access(rule, rule_list, rule_lock)) {
  321. smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
  322. if (smlp != NULL) {
  323. smlp->smk_rule = rule;
  324. list_add_rcu(&smlp->list, &smack_rule_list);
  325. } else
  326. rc = -ENOMEM;
  327. goto out;
  328. }
  329. out_free_rule:
  330. kfree(rule);
  331. out:
  332. kfree(data);
  333. return rc;
  334. }
  335. /*
  336. * Core logic for smackfs seq list operations.
  337. */
  338. static void *smk_seq_start(struct seq_file *s, loff_t *pos,
  339. struct list_head *head)
  340. {
  341. struct list_head *list;
  342. /*
  343. * This is 0 the first time through.
  344. */
  345. if (s->index == 0)
  346. s->private = head;
  347. if (s->private == NULL)
  348. return NULL;
  349. list = s->private;
  350. if (list_empty(list))
  351. return NULL;
  352. if (s->index == 0)
  353. return list->next;
  354. return list;
  355. }
  356. static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
  357. struct list_head *head)
  358. {
  359. struct list_head *list = v;
  360. if (list_is_last(list, head)) {
  361. s->private = NULL;
  362. return NULL;
  363. }
  364. s->private = list->next;
  365. return list->next;
  366. }
  367. static void smk_seq_stop(struct seq_file *s, void *v)
  368. {
  369. /* No-op */
  370. }
  371. /*
  372. * Seq_file read operations for /smack/load
  373. */
  374. static void *load_seq_start(struct seq_file *s, loff_t *pos)
  375. {
  376. return smk_seq_start(s, pos, &smack_rule_list);
  377. }
  378. static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos)
  379. {
  380. return smk_seq_next(s, v, pos, &smack_rule_list);
  381. }
  382. static int load_seq_show(struct seq_file *s, void *v)
  383. {
  384. struct list_head *list = v;
  385. struct smack_master_list *smlp =
  386. list_entry(list, struct smack_master_list, list);
  387. struct smack_rule *srp = smlp->smk_rule;
  388. seq_printf(s, "%s %s", (char *)srp->smk_subject,
  389. (char *)srp->smk_object);
  390. seq_putc(s, ' ');
  391. if (srp->smk_access & MAY_READ)
  392. seq_putc(s, 'r');
  393. if (srp->smk_access & MAY_WRITE)
  394. seq_putc(s, 'w');
  395. if (srp->smk_access & MAY_EXEC)
  396. seq_putc(s, 'x');
  397. if (srp->smk_access & MAY_APPEND)
  398. seq_putc(s, 'a');
  399. if (srp->smk_access & MAY_TRANSMUTE)
  400. seq_putc(s, 't');
  401. if (srp->smk_access == 0)
  402. seq_putc(s, '-');
  403. seq_putc(s, '\n');
  404. return 0;
  405. }
  406. static const struct seq_operations load_seq_ops = {
  407. .start = load_seq_start,
  408. .next = load_seq_next,
  409. .show = load_seq_show,
  410. .stop = smk_seq_stop,
  411. };
  412. /**
  413. * smk_open_load - open() for /smack/load
  414. * @inode: inode structure representing file
  415. * @file: "load" file pointer
  416. *
  417. * For reading, use load_seq_* seq_file reading operations.
  418. */
  419. static int smk_open_load(struct inode *inode, struct file *file)
  420. {
  421. return seq_open(file, &load_seq_ops);
  422. }
  423. /**
  424. * smk_write_load - write() for /smack/load
  425. * @file: file pointer, not actually used
  426. * @buf: where to get the data from
  427. * @count: bytes sent
  428. * @ppos: where to start - must be 0
  429. *
  430. */
  431. static ssize_t smk_write_load(struct file *file, const char __user *buf,
  432. size_t count, loff_t *ppos)
  433. {
  434. /*
  435. * Must have privilege.
  436. * No partial writes.
  437. * Enough data must be present.
  438. */
  439. if (!capable(CAP_MAC_ADMIN))
  440. return -EPERM;
  441. return smk_write_load_list(file, buf, count, ppos, NULL, NULL);
  442. }
  443. static const struct file_operations smk_load_ops = {
  444. .open = smk_open_load,
  445. .read = seq_read,
  446. .llseek = seq_lseek,
  447. .write = smk_write_load,
  448. .release = seq_release,
  449. };
  450. /**
  451. * smk_cipso_doi - initialize the CIPSO domain
  452. */
  453. static void smk_cipso_doi(void)
  454. {
  455. int rc;
  456. struct cipso_v4_doi *doip;
  457. struct netlbl_audit nai;
  458. smk_netlabel_audit_set(&nai);
  459. rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
  460. if (rc != 0)
  461. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  462. __func__, __LINE__, rc);
  463. doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
  464. if (doip == NULL)
  465. panic("smack: Failed to initialize cipso DOI.\n");
  466. doip->map.std = NULL;
  467. doip->doi = smk_cipso_doi_value;
  468. doip->type = CIPSO_V4_MAP_PASS;
  469. doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
  470. for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
  471. doip->tags[rc] = CIPSO_V4_TAG_INVALID;
  472. rc = netlbl_cfg_cipsov4_add(doip, &nai);
  473. if (rc != 0) {
  474. printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
  475. __func__, __LINE__, rc);
  476. kfree(doip);
  477. return;
  478. }
  479. rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
  480. if (rc != 0) {
  481. printk(KERN_WARNING "%s:%d map add rc = %d\n",
  482. __func__, __LINE__, rc);
  483. kfree(doip);
  484. return;
  485. }
  486. }
  487. /**
  488. * smk_unlbl_ambient - initialize the unlabeled domain
  489. * @oldambient: previous domain string
  490. */
  491. static void smk_unlbl_ambient(char *oldambient)
  492. {
  493. int rc;
  494. struct netlbl_audit nai;
  495. smk_netlabel_audit_set(&nai);
  496. if (oldambient != NULL) {
  497. rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
  498. if (rc != 0)
  499. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  500. __func__, __LINE__, rc);
  501. }
  502. rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
  503. NULL, NULL, &nai);
  504. if (rc != 0)
  505. printk(KERN_WARNING "%s:%d add rc = %d\n",
  506. __func__, __LINE__, rc);
  507. }
  508. /*
  509. * Seq_file read operations for /smack/cipso
  510. */
  511. static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
  512. {
  513. return smk_seq_start(s, pos, &smack_known_list);
  514. }
  515. static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
  516. {
  517. return smk_seq_next(s, v, pos, &smack_known_list);
  518. }
  519. /*
  520. * Print cipso labels in format:
  521. * label level[/cat[,cat]]
  522. */
  523. static int cipso_seq_show(struct seq_file *s, void *v)
  524. {
  525. struct list_head *list = v;
  526. struct smack_known *skp =
  527. list_entry(list, struct smack_known, list);
  528. struct smack_cipso *scp = skp->smk_cipso;
  529. char *cbp;
  530. char sep = '/';
  531. int cat = 1;
  532. int i;
  533. unsigned char m;
  534. if (scp == NULL)
  535. return 0;
  536. seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
  537. cbp = scp->smk_catset;
  538. for (i = 0; i < SMK_LABELLEN; i++)
  539. for (m = 0x80; m != 0; m >>= 1) {
  540. if (m & cbp[i]) {
  541. seq_printf(s, "%c%d", sep, cat);
  542. sep = ',';
  543. }
  544. cat++;
  545. }
  546. seq_putc(s, '\n');
  547. return 0;
  548. }
  549. static const struct seq_operations cipso_seq_ops = {
  550. .start = cipso_seq_start,
  551. .next = cipso_seq_next,
  552. .show = cipso_seq_show,
  553. .stop = smk_seq_stop,
  554. };
  555. /**
  556. * smk_open_cipso - open() for /smack/cipso
  557. * @inode: inode structure representing file
  558. * @file: "cipso" file pointer
  559. *
  560. * Connect our cipso_seq_* operations with /smack/cipso
  561. * file_operations
  562. */
  563. static int smk_open_cipso(struct inode *inode, struct file *file)
  564. {
  565. return seq_open(file, &cipso_seq_ops);
  566. }
  567. /**
  568. * smk_write_cipso - write() for /smack/cipso
  569. * @file: file pointer, not actually used
  570. * @buf: where to get the data from
  571. * @count: bytes sent
  572. * @ppos: where to start
  573. *
  574. * Accepts only one cipso rule per write call.
  575. * Returns number of bytes written or error code, as appropriate
  576. */
  577. static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
  578. size_t count, loff_t *ppos)
  579. {
  580. struct smack_known *skp;
  581. struct smack_cipso *scp = NULL;
  582. char mapcatset[SMK_LABELLEN];
  583. int maplevel;
  584. int cat;
  585. int catlen;
  586. ssize_t rc = -EINVAL;
  587. char *data = NULL;
  588. char *rule;
  589. int ret;
  590. int i;
  591. /*
  592. * Must have privilege.
  593. * No partial writes.
  594. * Enough data must be present.
  595. */
  596. if (!capable(CAP_MAC_ADMIN))
  597. return -EPERM;
  598. if (*ppos != 0)
  599. return -EINVAL;
  600. if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
  601. return -EINVAL;
  602. data = kzalloc(count + 1, GFP_KERNEL);
  603. if (data == NULL)
  604. return -ENOMEM;
  605. if (copy_from_user(data, buf, count) != 0) {
  606. rc = -EFAULT;
  607. goto unlockedout;
  608. }
  609. /* labels cannot begin with a '-' */
  610. if (data[0] == '-') {
  611. rc = -EINVAL;
  612. goto unlockedout;
  613. }
  614. data[count] = '\0';
  615. rule = data;
  616. /*
  617. * Only allow one writer at a time. Writes should be
  618. * quite rare and small in any case.
  619. */
  620. mutex_lock(&smack_cipso_lock);
  621. skp = smk_import_entry(rule, 0);
  622. if (skp == NULL)
  623. goto out;
  624. rule += SMK_LABELLEN;
  625. ret = sscanf(rule, "%d", &maplevel);
  626. if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
  627. goto out;
  628. rule += SMK_DIGITLEN;
  629. ret = sscanf(rule, "%d", &catlen);
  630. if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
  631. goto out;
  632. if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
  633. goto out;
  634. memset(mapcatset, 0, sizeof(mapcatset));
  635. for (i = 0; i < catlen; i++) {
  636. rule += SMK_DIGITLEN;
  637. ret = sscanf(rule, "%d", &cat);
  638. if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
  639. goto out;
  640. smack_catset_bit(cat, mapcatset);
  641. }
  642. if (skp->smk_cipso == NULL) {
  643. scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
  644. if (scp == NULL) {
  645. rc = -ENOMEM;
  646. goto out;
  647. }
  648. }
  649. spin_lock_bh(&skp->smk_cipsolock);
  650. if (scp == NULL)
  651. scp = skp->smk_cipso;
  652. else
  653. skp->smk_cipso = scp;
  654. scp->smk_level = maplevel;
  655. memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
  656. spin_unlock_bh(&skp->smk_cipsolock);
  657. rc = count;
  658. out:
  659. mutex_unlock(&smack_cipso_lock);
  660. unlockedout:
  661. kfree(data);
  662. return rc;
  663. }
  664. static const struct file_operations smk_cipso_ops = {
  665. .open = smk_open_cipso,
  666. .read = seq_read,
  667. .llseek = seq_lseek,
  668. .write = smk_write_cipso,
  669. .release = seq_release,
  670. };
  671. /*
  672. * Seq_file read operations for /smack/netlabel
  673. */
  674. static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
  675. {
  676. return smk_seq_start(s, pos, &smk_netlbladdr_list);
  677. }
  678. static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
  679. {
  680. return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
  681. }
  682. #define BEBITS (sizeof(__be32) * 8)
  683. /*
  684. * Print host/label pairs
  685. */
  686. static int netlbladdr_seq_show(struct seq_file *s, void *v)
  687. {
  688. struct list_head *list = v;
  689. struct smk_netlbladdr *skp =
  690. list_entry(list, struct smk_netlbladdr, list);
  691. unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
  692. int maskn;
  693. u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
  694. for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
  695. seq_printf(s, "%u.%u.%u.%u/%d %s\n",
  696. hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
  697. return 0;
  698. }
  699. static const struct seq_operations netlbladdr_seq_ops = {
  700. .start = netlbladdr_seq_start,
  701. .next = netlbladdr_seq_next,
  702. .show = netlbladdr_seq_show,
  703. .stop = smk_seq_stop,
  704. };
  705. /**
  706. * smk_open_netlbladdr - open() for /smack/netlabel
  707. * @inode: inode structure representing file
  708. * @file: "netlabel" file pointer
  709. *
  710. * Connect our netlbladdr_seq_* operations with /smack/netlabel
  711. * file_operations
  712. */
  713. static int smk_open_netlbladdr(struct inode *inode, struct file *file)
  714. {
  715. return seq_open(file, &netlbladdr_seq_ops);
  716. }
  717. /**
  718. * smk_netlbladdr_insert
  719. * @new : netlabel to insert
  720. *
  721. * This helper insert netlabel in the smack_netlbladdrs list
  722. * sorted by netmask length (longest to smallest)
  723. * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
  724. *
  725. */
  726. static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
  727. {
  728. struct smk_netlbladdr *m, *m_next;
  729. if (list_empty(&smk_netlbladdr_list)) {
  730. list_add_rcu(&new->list, &smk_netlbladdr_list);
  731. return;
  732. }
  733. m = list_entry_rcu(smk_netlbladdr_list.next,
  734. struct smk_netlbladdr, list);
  735. /* the comparison '>' is a bit hacky, but works */
  736. if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
  737. list_add_rcu(&new->list, &smk_netlbladdr_list);
  738. return;
  739. }
  740. list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
  741. if (list_is_last(&m->list, &smk_netlbladdr_list)) {
  742. list_add_rcu(&new->list, &m->list);
  743. return;
  744. }
  745. m_next = list_entry_rcu(m->list.next,
  746. struct smk_netlbladdr, list);
  747. if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
  748. list_add_rcu(&new->list, &m->list);
  749. return;
  750. }
  751. }
  752. }
  753. /**
  754. * smk_write_netlbladdr - write() for /smack/netlabel
  755. * @file: file pointer, not actually used
  756. * @buf: where to get the data from
  757. * @count: bytes sent
  758. * @ppos: where to start
  759. *
  760. * Accepts only one netlbladdr per write call.
  761. * Returns number of bytes written or error code, as appropriate
  762. */
  763. static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
  764. size_t count, loff_t *ppos)
  765. {
  766. struct smk_netlbladdr *skp;
  767. struct sockaddr_in newname;
  768. char smack[SMK_LABELLEN];
  769. char *sp;
  770. char data[SMK_NETLBLADDRMAX + 1];
  771. char *host = (char *)&newname.sin_addr.s_addr;
  772. int rc;
  773. struct netlbl_audit audit_info;
  774. struct in_addr mask;
  775. unsigned int m;
  776. int found;
  777. u32 mask_bits = (1<<31);
  778. __be32 nsa;
  779. u32 temp_mask;
  780. /*
  781. * Must have privilege.
  782. * No partial writes.
  783. * Enough data must be present.
  784. * "<addr/mask, as a.b.c.d/e><space><label>"
  785. * "<addr, as a.b.c.d><space><label>"
  786. */
  787. if (!capable(CAP_MAC_ADMIN))
  788. return -EPERM;
  789. if (*ppos != 0)
  790. return -EINVAL;
  791. if (count < SMK_NETLBLADDRMIN || count > SMK_NETLBLADDRMAX)
  792. return -EINVAL;
  793. if (copy_from_user(data, buf, count) != 0)
  794. return -EFAULT;
  795. data[count] = '\0';
  796. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
  797. &host[0], &host[1], &host[2], &host[3], &m, smack);
  798. if (rc != 6) {
  799. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
  800. &host[0], &host[1], &host[2], &host[3], smack);
  801. if (rc != 5)
  802. return -EINVAL;
  803. m = BEBITS;
  804. }
  805. if (m > BEBITS)
  806. return -EINVAL;
  807. /* if smack begins with '-', its an option, don't import it */
  808. if (smack[0] != '-') {
  809. sp = smk_import(smack, 0);
  810. if (sp == NULL)
  811. return -EINVAL;
  812. } else {
  813. /* check known options */
  814. if (strcmp(smack, smack_cipso_option) == 0)
  815. sp = (char *)smack_cipso_option;
  816. else
  817. return -EINVAL;
  818. }
  819. for (temp_mask = 0; m > 0; m--) {
  820. temp_mask |= mask_bits;
  821. mask_bits >>= 1;
  822. }
  823. mask.s_addr = cpu_to_be32(temp_mask);
  824. newname.sin_addr.s_addr &= mask.s_addr;
  825. /*
  826. * Only allow one writer at a time. Writes should be
  827. * quite rare and small in any case.
  828. */
  829. mutex_lock(&smk_netlbladdr_lock);
  830. nsa = newname.sin_addr.s_addr;
  831. /* try to find if the prefix is already in the list */
  832. found = 0;
  833. list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
  834. if (skp->smk_host.sin_addr.s_addr == nsa &&
  835. skp->smk_mask.s_addr == mask.s_addr) {
  836. found = 1;
  837. break;
  838. }
  839. }
  840. smk_netlabel_audit_set(&audit_info);
  841. if (found == 0) {
  842. skp = kzalloc(sizeof(*skp), GFP_KERNEL);
  843. if (skp == NULL)
  844. rc = -ENOMEM;
  845. else {
  846. rc = 0;
  847. skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
  848. skp->smk_mask.s_addr = mask.s_addr;
  849. skp->smk_label = sp;
  850. smk_netlbladdr_insert(skp);
  851. }
  852. } else {
  853. /* we delete the unlabeled entry, only if the previous label
  854. * wasn't the special CIPSO option */
  855. if (skp->smk_label != smack_cipso_option)
  856. rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
  857. &skp->smk_host.sin_addr, &skp->smk_mask,
  858. PF_INET, &audit_info);
  859. else
  860. rc = 0;
  861. skp->smk_label = sp;
  862. }
  863. /*
  864. * Now tell netlabel about the single label nature of
  865. * this host so that incoming packets get labeled.
  866. * but only if we didn't get the special CIPSO option
  867. */
  868. if (rc == 0 && sp != smack_cipso_option)
  869. rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
  870. &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
  871. smack_to_secid(skp->smk_label), &audit_info);
  872. if (rc == 0)
  873. rc = count;
  874. mutex_unlock(&smk_netlbladdr_lock);
  875. return rc;
  876. }
  877. static const struct file_operations smk_netlbladdr_ops = {
  878. .open = smk_open_netlbladdr,
  879. .read = seq_read,
  880. .llseek = seq_lseek,
  881. .write = smk_write_netlbladdr,
  882. .release = seq_release,
  883. };
  884. /**
  885. * smk_read_doi - read() for /smack/doi
  886. * @filp: file pointer, not actually used
  887. * @buf: where to put the result
  888. * @count: maximum to send along
  889. * @ppos: where to start
  890. *
  891. * Returns number of bytes read or error code, as appropriate
  892. */
  893. static ssize_t smk_read_doi(struct file *filp, char __user *buf,
  894. size_t count, loff_t *ppos)
  895. {
  896. char temp[80];
  897. ssize_t rc;
  898. if (*ppos != 0)
  899. return 0;
  900. sprintf(temp, "%d", smk_cipso_doi_value);
  901. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  902. return rc;
  903. }
  904. /**
  905. * smk_write_doi - write() for /smack/doi
  906. * @file: file pointer, not actually used
  907. * @buf: where to get the data from
  908. * @count: bytes sent
  909. * @ppos: where to start
  910. *
  911. * Returns number of bytes written or error code, as appropriate
  912. */
  913. static ssize_t smk_write_doi(struct file *file, const char __user *buf,
  914. size_t count, loff_t *ppos)
  915. {
  916. char temp[80];
  917. int i;
  918. if (!capable(CAP_MAC_ADMIN))
  919. return -EPERM;
  920. if (count >= sizeof(temp) || count == 0)
  921. return -EINVAL;
  922. if (copy_from_user(temp, buf, count) != 0)
  923. return -EFAULT;
  924. temp[count] = '\0';
  925. if (sscanf(temp, "%d", &i) != 1)
  926. return -EINVAL;
  927. smk_cipso_doi_value = i;
  928. smk_cipso_doi();
  929. return count;
  930. }
  931. static const struct file_operations smk_doi_ops = {
  932. .read = smk_read_doi,
  933. .write = smk_write_doi,
  934. .llseek = default_llseek,
  935. };
  936. /**
  937. * smk_read_direct - read() for /smack/direct
  938. * @filp: file pointer, not actually used
  939. * @buf: where to put the result
  940. * @count: maximum to send along
  941. * @ppos: where to start
  942. *
  943. * Returns number of bytes read or error code, as appropriate
  944. */
  945. static ssize_t smk_read_direct(struct file *filp, char __user *buf,
  946. size_t count, loff_t *ppos)
  947. {
  948. char temp[80];
  949. ssize_t rc;
  950. if (*ppos != 0)
  951. return 0;
  952. sprintf(temp, "%d", smack_cipso_direct);
  953. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  954. return rc;
  955. }
  956. /**
  957. * smk_write_direct - write() for /smack/direct
  958. * @file: file pointer, not actually used
  959. * @buf: where to get the data from
  960. * @count: bytes sent
  961. * @ppos: where to start
  962. *
  963. * Returns number of bytes written or error code, as appropriate
  964. */
  965. static ssize_t smk_write_direct(struct file *file, const char __user *buf,
  966. size_t count, loff_t *ppos)
  967. {
  968. char temp[80];
  969. int i;
  970. if (!capable(CAP_MAC_ADMIN))
  971. return -EPERM;
  972. if (count >= sizeof(temp) || count == 0)
  973. return -EINVAL;
  974. if (copy_from_user(temp, buf, count) != 0)
  975. return -EFAULT;
  976. temp[count] = '\0';
  977. if (sscanf(temp, "%d", &i) != 1)
  978. return -EINVAL;
  979. smack_cipso_direct = i;
  980. return count;
  981. }
  982. static const struct file_operations smk_direct_ops = {
  983. .read = smk_read_direct,
  984. .write = smk_write_direct,
  985. .llseek = default_llseek,
  986. };
  987. /**
  988. * smk_read_ambient - read() for /smack/ambient
  989. * @filp: file pointer, not actually used
  990. * @buf: where to put the result
  991. * @cn: maximum to send along
  992. * @ppos: where to start
  993. *
  994. * Returns number of bytes read or error code, as appropriate
  995. */
  996. static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
  997. size_t cn, loff_t *ppos)
  998. {
  999. ssize_t rc;
  1000. int asize;
  1001. if (*ppos != 0)
  1002. return 0;
  1003. /*
  1004. * Being careful to avoid a problem in the case where
  1005. * smack_net_ambient gets changed in midstream.
  1006. */
  1007. mutex_lock(&smack_ambient_lock);
  1008. asize = strlen(smack_net_ambient) + 1;
  1009. if (cn >= asize)
  1010. rc = simple_read_from_buffer(buf, cn, ppos,
  1011. smack_net_ambient, asize);
  1012. else
  1013. rc = -EINVAL;
  1014. mutex_unlock(&smack_ambient_lock);
  1015. return rc;
  1016. }
  1017. /**
  1018. * smk_write_ambient - write() for /smack/ambient
  1019. * @file: file pointer, not actually used
  1020. * @buf: where to get the data from
  1021. * @count: bytes sent
  1022. * @ppos: where to start
  1023. *
  1024. * Returns number of bytes written or error code, as appropriate
  1025. */
  1026. static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
  1027. size_t count, loff_t *ppos)
  1028. {
  1029. char in[SMK_LABELLEN];
  1030. char *oldambient;
  1031. char *smack;
  1032. if (!capable(CAP_MAC_ADMIN))
  1033. return -EPERM;
  1034. if (count >= SMK_LABELLEN)
  1035. return -EINVAL;
  1036. if (copy_from_user(in, buf, count) != 0)
  1037. return -EFAULT;
  1038. smack = smk_import(in, count);
  1039. if (smack == NULL)
  1040. return -EINVAL;
  1041. mutex_lock(&smack_ambient_lock);
  1042. oldambient = smack_net_ambient;
  1043. smack_net_ambient = smack;
  1044. smk_unlbl_ambient(oldambient);
  1045. mutex_unlock(&smack_ambient_lock);
  1046. return count;
  1047. }
  1048. static const struct file_operations smk_ambient_ops = {
  1049. .read = smk_read_ambient,
  1050. .write = smk_write_ambient,
  1051. .llseek = default_llseek,
  1052. };
  1053. /**
  1054. * smk_read_onlycap - read() for /smack/onlycap
  1055. * @filp: file pointer, not actually used
  1056. * @buf: where to put the result
  1057. * @cn: maximum to send along
  1058. * @ppos: where to start
  1059. *
  1060. * Returns number of bytes read or error code, as appropriate
  1061. */
  1062. static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
  1063. size_t cn, loff_t *ppos)
  1064. {
  1065. char *smack = "";
  1066. ssize_t rc = -EINVAL;
  1067. int asize;
  1068. if (*ppos != 0)
  1069. return 0;
  1070. if (smack_onlycap != NULL)
  1071. smack = smack_onlycap;
  1072. asize = strlen(smack) + 1;
  1073. if (cn >= asize)
  1074. rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
  1075. return rc;
  1076. }
  1077. /**
  1078. * smk_write_onlycap - write() for /smack/onlycap
  1079. * @file: file pointer, not actually used
  1080. * @buf: where to get the data from
  1081. * @count: bytes sent
  1082. * @ppos: where to start
  1083. *
  1084. * Returns number of bytes written or error code, as appropriate
  1085. */
  1086. static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
  1087. size_t count, loff_t *ppos)
  1088. {
  1089. char in[SMK_LABELLEN];
  1090. char *sp = smk_of_task(current->cred->security);
  1091. if (!capable(CAP_MAC_ADMIN))
  1092. return -EPERM;
  1093. /*
  1094. * This can be done using smk_access() but is done
  1095. * explicitly for clarity. The smk_access() implementation
  1096. * would use smk_access(smack_onlycap, MAY_WRITE)
  1097. */
  1098. if (smack_onlycap != NULL && smack_onlycap != sp)
  1099. return -EPERM;
  1100. if (count >= SMK_LABELLEN)
  1101. return -EINVAL;
  1102. if (copy_from_user(in, buf, count) != 0)
  1103. return -EFAULT;
  1104. /*
  1105. * Should the null string be passed in unset the onlycap value.
  1106. * This seems like something to be careful with as usually
  1107. * smk_import only expects to return NULL for errors. It
  1108. * is usually the case that a nullstring or "\n" would be
  1109. * bad to pass to smk_import but in fact this is useful here.
  1110. */
  1111. smack_onlycap = smk_import(in, count);
  1112. return count;
  1113. }
  1114. static const struct file_operations smk_onlycap_ops = {
  1115. .read = smk_read_onlycap,
  1116. .write = smk_write_onlycap,
  1117. .llseek = default_llseek,
  1118. };
  1119. /**
  1120. * smk_read_logging - read() for /smack/logging
  1121. * @filp: file pointer, not actually used
  1122. * @buf: where to put the result
  1123. * @cn: maximum to send along
  1124. * @ppos: where to start
  1125. *
  1126. * Returns number of bytes read or error code, as appropriate
  1127. */
  1128. static ssize_t smk_read_logging(struct file *filp, char __user *buf,
  1129. size_t count, loff_t *ppos)
  1130. {
  1131. char temp[32];
  1132. ssize_t rc;
  1133. if (*ppos != 0)
  1134. return 0;
  1135. sprintf(temp, "%d\n", log_policy);
  1136. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1137. return rc;
  1138. }
  1139. /**
  1140. * smk_write_logging - write() for /smack/logging
  1141. * @file: file pointer, not actually used
  1142. * @buf: where to get the data from
  1143. * @count: bytes sent
  1144. * @ppos: where to start
  1145. *
  1146. * Returns number of bytes written or error code, as appropriate
  1147. */
  1148. static ssize_t smk_write_logging(struct file *file, const char __user *buf,
  1149. size_t count, loff_t *ppos)
  1150. {
  1151. char temp[32];
  1152. int i;
  1153. if (!capable(CAP_MAC_ADMIN))
  1154. return -EPERM;
  1155. if (count >= sizeof(temp) || count == 0)
  1156. return -EINVAL;
  1157. if (copy_from_user(temp, buf, count) != 0)
  1158. return -EFAULT;
  1159. temp[count] = '\0';
  1160. if (sscanf(temp, "%d", &i) != 1)
  1161. return -EINVAL;
  1162. if (i < 0 || i > 3)
  1163. return -EINVAL;
  1164. log_policy = i;
  1165. return count;
  1166. }
  1167. static const struct file_operations smk_logging_ops = {
  1168. .read = smk_read_logging,
  1169. .write = smk_write_logging,
  1170. .llseek = default_llseek,
  1171. };
  1172. /*
  1173. * Seq_file read operations for /smack/load-self
  1174. */
  1175. static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
  1176. {
  1177. struct task_smack *tsp = current_security();
  1178. return smk_seq_start(s, pos, &tsp->smk_rules);
  1179. }
  1180. static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1181. {
  1182. struct task_smack *tsp = current_security();
  1183. return smk_seq_next(s, v, pos, &tsp->smk_rules);
  1184. }
  1185. static int load_self_seq_show(struct seq_file *s, void *v)
  1186. {
  1187. struct list_head *list = v;
  1188. struct smack_rule *srp =
  1189. list_entry(list, struct smack_rule, list);
  1190. seq_printf(s, "%s %s", (char *)srp->smk_subject,
  1191. (char *)srp->smk_object);
  1192. seq_putc(s, ' ');
  1193. if (srp->smk_access & MAY_READ)
  1194. seq_putc(s, 'r');
  1195. if (srp->smk_access & MAY_WRITE)
  1196. seq_putc(s, 'w');
  1197. if (srp->smk_access & MAY_EXEC)
  1198. seq_putc(s, 'x');
  1199. if (srp->smk_access & MAY_APPEND)
  1200. seq_putc(s, 'a');
  1201. if (srp->smk_access & MAY_TRANSMUTE)
  1202. seq_putc(s, 't');
  1203. if (srp->smk_access == 0)
  1204. seq_putc(s, '-');
  1205. seq_putc(s, '\n');
  1206. return 0;
  1207. }
  1208. static const struct seq_operations load_self_seq_ops = {
  1209. .start = load_self_seq_start,
  1210. .next = load_self_seq_next,
  1211. .show = load_self_seq_show,
  1212. .stop = smk_seq_stop,
  1213. };
  1214. /**
  1215. * smk_open_load_self - open() for /smack/load-self
  1216. * @inode: inode structure representing file
  1217. * @file: "load" file pointer
  1218. *
  1219. * For reading, use load_seq_* seq_file reading operations.
  1220. */
  1221. static int smk_open_load_self(struct inode *inode, struct file *file)
  1222. {
  1223. return seq_open(file, &load_self_seq_ops);
  1224. }
  1225. /**
  1226. * smk_write_load_self - write() for /smack/load-self
  1227. * @file: file pointer, not actually used
  1228. * @buf: where to get the data from
  1229. * @count: bytes sent
  1230. * @ppos: where to start - must be 0
  1231. *
  1232. */
  1233. static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
  1234. size_t count, loff_t *ppos)
  1235. {
  1236. struct task_smack *tsp = current_security();
  1237. return smk_write_load_list(file, buf, count, ppos, &tsp->smk_rules,
  1238. &tsp->smk_rules_lock);
  1239. }
  1240. static const struct file_operations smk_load_self_ops = {
  1241. .open = smk_open_load_self,
  1242. .read = seq_read,
  1243. .llseek = seq_lseek,
  1244. .write = smk_write_load_self,
  1245. .release = seq_release,
  1246. };
  1247. /**
  1248. * smk_write_access - handle access check transaction
  1249. * @file: file pointer
  1250. * @buf: data from user space
  1251. * @count: bytes sent
  1252. * @ppos: where to start - must be 0
  1253. */
  1254. static ssize_t smk_write_access(struct file *file, const char __user *buf,
  1255. size_t count, loff_t *ppos)
  1256. {
  1257. struct smack_rule rule;
  1258. char *data;
  1259. int res;
  1260. data = simple_transaction_get(file, buf, count);
  1261. if (IS_ERR(data))
  1262. return PTR_ERR(data);
  1263. if (count < SMK_LOADLEN || smk_parse_rule(data, &rule, 0))
  1264. return -EINVAL;
  1265. res = smk_access(rule.smk_subject, rule.smk_object, rule.smk_access,
  1266. NULL);
  1267. data[0] = res == 0 ? '1' : '0';
  1268. data[1] = '\0';
  1269. simple_transaction_set(file, 2);
  1270. return SMK_LOADLEN;
  1271. }
  1272. static const struct file_operations smk_access_ops = {
  1273. .write = smk_write_access,
  1274. .read = simple_transaction_read,
  1275. .release = simple_transaction_release,
  1276. .llseek = generic_file_llseek,
  1277. };
  1278. /**
  1279. * smk_fill_super - fill the /smackfs superblock
  1280. * @sb: the empty superblock
  1281. * @data: unused
  1282. * @silent: unused
  1283. *
  1284. * Fill in the well known entries for /smack
  1285. *
  1286. * Returns 0 on success, an error code on failure
  1287. */
  1288. static int smk_fill_super(struct super_block *sb, void *data, int silent)
  1289. {
  1290. int rc;
  1291. struct inode *root_inode;
  1292. static struct tree_descr smack_files[] = {
  1293. [SMK_LOAD] = {
  1294. "load", &smk_load_ops, S_IRUGO|S_IWUSR},
  1295. [SMK_CIPSO] = {
  1296. "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
  1297. [SMK_DOI] = {
  1298. "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
  1299. [SMK_DIRECT] = {
  1300. "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
  1301. [SMK_AMBIENT] = {
  1302. "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
  1303. [SMK_NETLBLADDR] = {
  1304. "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
  1305. [SMK_ONLYCAP] = {
  1306. "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
  1307. [SMK_LOGGING] = {
  1308. "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
  1309. [SMK_LOAD_SELF] = {
  1310. "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
  1311. [SMK_ACCESSES] = {
  1312. "access", &smk_access_ops, S_IRUGO|S_IWUGO},
  1313. /* last one */
  1314. {""}
  1315. };
  1316. rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
  1317. if (rc != 0) {
  1318. printk(KERN_ERR "%s failed %d while creating inodes\n",
  1319. __func__, rc);
  1320. return rc;
  1321. }
  1322. root_inode = sb->s_root->d_inode;
  1323. root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
  1324. return 0;
  1325. }
  1326. /**
  1327. * smk_mount - get the smackfs superblock
  1328. * @fs_type: passed along without comment
  1329. * @flags: passed along without comment
  1330. * @dev_name: passed along without comment
  1331. * @data: passed along without comment
  1332. *
  1333. * Just passes everything along.
  1334. *
  1335. * Returns what the lower level code does.
  1336. */
  1337. static struct dentry *smk_mount(struct file_system_type *fs_type,
  1338. int flags, const char *dev_name, void *data)
  1339. {
  1340. return mount_single(fs_type, flags, data, smk_fill_super);
  1341. }
  1342. static struct file_system_type smk_fs_type = {
  1343. .name = "smackfs",
  1344. .mount = smk_mount,
  1345. .kill_sb = kill_litter_super,
  1346. };
  1347. static struct vfsmount *smackfs_mount;
  1348. /**
  1349. * init_smk_fs - get the smackfs superblock
  1350. *
  1351. * register the smackfs
  1352. *
  1353. * Do not register smackfs if Smack wasn't enabled
  1354. * on boot. We can not put this method normally under the
  1355. * smack_init() code path since the security subsystem get
  1356. * initialized before the vfs caches.
  1357. *
  1358. * Returns true if we were not chosen on boot or if
  1359. * we were chosen and filesystem registration succeeded.
  1360. */
  1361. static int __init init_smk_fs(void)
  1362. {
  1363. int err;
  1364. if (!security_module_enable(&smack_ops))
  1365. return 0;
  1366. err = register_filesystem(&smk_fs_type);
  1367. if (!err) {
  1368. smackfs_mount = kern_mount(&smk_fs_type);
  1369. if (IS_ERR(smackfs_mount)) {
  1370. printk(KERN_ERR "smackfs: could not mount!\n");
  1371. err = PTR_ERR(smackfs_mount);
  1372. smackfs_mount = NULL;
  1373. }
  1374. }
  1375. smk_cipso_doi();
  1376. smk_unlbl_ambient(NULL);
  1377. return err;
  1378. }
  1379. __initcall(init_smk_fs);