smackfs.c 31 KB

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