smackfs.c 30 KB

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