smackfs.c 29 KB

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