smackfs.c 27 KB

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