smackfs.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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. * @filp: 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. */
  349. static void smk_unlbl_ambient(char *oldambient)
  350. {
  351. int rc;
  352. struct netlbl_audit nai;
  353. smk_netlabel_audit_set(&nai);
  354. if (oldambient != NULL) {
  355. rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
  356. if (rc != 0)
  357. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  358. __func__, __LINE__, rc);
  359. }
  360. rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
  361. NULL, NULL, &nai);
  362. if (rc != 0)
  363. printk(KERN_WARNING "%s:%d add rc = %d\n",
  364. __func__, __LINE__, rc);
  365. }
  366. /*
  367. * Seq_file read operations for /smack/cipso
  368. */
  369. static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
  370. {
  371. if (*pos == SEQ_READ_FINISHED)
  372. return NULL;
  373. return smack_known;
  374. }
  375. static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
  376. {
  377. struct smack_known *skp = ((struct smack_known *) v)->smk_next;
  378. /*
  379. * Omit labels with no associated cipso value
  380. */
  381. while (skp != NULL && !skp->smk_cipso)
  382. skp = skp->smk_next;
  383. if (skp == NULL)
  384. *pos = SEQ_READ_FINISHED;
  385. return skp;
  386. }
  387. /*
  388. * Print cipso labels in format:
  389. * label level[/cat[,cat]]
  390. */
  391. static int cipso_seq_show(struct seq_file *s, void *v)
  392. {
  393. struct smack_known *skp = (struct smack_known *) v;
  394. struct smack_cipso *scp = skp->smk_cipso;
  395. char *cbp;
  396. char sep = '/';
  397. int cat = 1;
  398. int i;
  399. unsigned char m;
  400. if (scp == NULL)
  401. return 0;
  402. seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
  403. cbp = scp->smk_catset;
  404. for (i = 0; i < SMK_LABELLEN; i++)
  405. for (m = 0x80; m != 0; m >>= 1) {
  406. if (m & cbp[i]) {
  407. seq_printf(s, "%c%d", sep, cat);
  408. sep = ',';
  409. }
  410. cat++;
  411. }
  412. seq_putc(s, '\n');
  413. return 0;
  414. }
  415. static void cipso_seq_stop(struct seq_file *s, void *v)
  416. {
  417. /* No-op */
  418. }
  419. static struct seq_operations cipso_seq_ops = {
  420. .start = cipso_seq_start,
  421. .stop = cipso_seq_stop,
  422. .next = cipso_seq_next,
  423. .show = cipso_seq_show,
  424. };
  425. /**
  426. * smk_open_cipso - open() for /smack/cipso
  427. * @inode: inode structure representing file
  428. * @file: "cipso" file pointer
  429. *
  430. * Connect our cipso_seq_* operations with /smack/cipso
  431. * file_operations
  432. */
  433. static int smk_open_cipso(struct inode *inode, struct file *file)
  434. {
  435. return seq_open(file, &cipso_seq_ops);
  436. }
  437. /**
  438. * smk_write_cipso - write() for /smack/cipso
  439. * @filp: file pointer, not actually used
  440. * @buf: where to get the data from
  441. * @count: bytes sent
  442. * @ppos: where to start
  443. *
  444. * Accepts only one cipso rule per write call.
  445. * Returns number of bytes written or error code, as appropriate
  446. */
  447. static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
  448. size_t count, loff_t *ppos)
  449. {
  450. struct smack_known *skp;
  451. struct smack_cipso *scp = NULL;
  452. char mapcatset[SMK_LABELLEN];
  453. int maplevel;
  454. int cat;
  455. int catlen;
  456. ssize_t rc = -EINVAL;
  457. char *data = NULL;
  458. char *rule;
  459. int ret;
  460. int i;
  461. /*
  462. * Must have privilege.
  463. * No partial writes.
  464. * Enough data must be present.
  465. */
  466. if (!capable(CAP_MAC_ADMIN))
  467. return -EPERM;
  468. if (*ppos != 0)
  469. return -EINVAL;
  470. if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
  471. return -EINVAL;
  472. data = kzalloc(count + 1, GFP_KERNEL);
  473. if (data == NULL)
  474. return -ENOMEM;
  475. if (copy_from_user(data, buf, count) != 0) {
  476. rc = -EFAULT;
  477. goto unlockedout;
  478. }
  479. data[count] = '\0';
  480. rule = data;
  481. /*
  482. * Only allow one writer at a time. Writes should be
  483. * quite rare and small in any case.
  484. */
  485. mutex_lock(&smack_cipso_lock);
  486. skp = smk_import_entry(rule, 0);
  487. if (skp == NULL)
  488. goto out;
  489. rule += SMK_LABELLEN;
  490. ret = sscanf(rule, "%d", &maplevel);
  491. if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
  492. goto out;
  493. rule += SMK_DIGITLEN;
  494. ret = sscanf(rule, "%d", &catlen);
  495. if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
  496. goto out;
  497. if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
  498. goto out;
  499. memset(mapcatset, 0, sizeof(mapcatset));
  500. for (i = 0; i < catlen; i++) {
  501. rule += SMK_DIGITLEN;
  502. ret = sscanf(rule, "%d", &cat);
  503. if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
  504. goto out;
  505. smack_catset_bit(cat, mapcatset);
  506. }
  507. if (skp->smk_cipso == NULL) {
  508. scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
  509. if (scp == NULL) {
  510. rc = -ENOMEM;
  511. goto out;
  512. }
  513. }
  514. spin_lock_bh(&skp->smk_cipsolock);
  515. if (scp == NULL)
  516. scp = skp->smk_cipso;
  517. else
  518. skp->smk_cipso = scp;
  519. scp->smk_level = maplevel;
  520. memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
  521. spin_unlock_bh(&skp->smk_cipsolock);
  522. rc = count;
  523. out:
  524. mutex_unlock(&smack_cipso_lock);
  525. unlockedout:
  526. kfree(data);
  527. return rc;
  528. }
  529. static const struct file_operations smk_cipso_ops = {
  530. .open = smk_open_cipso,
  531. .read = seq_read,
  532. .llseek = seq_lseek,
  533. .write = smk_write_cipso,
  534. .release = seq_release,
  535. };
  536. /*
  537. * Seq_file read operations for /smack/netlabel
  538. */
  539. static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
  540. {
  541. if (*pos == SEQ_READ_FINISHED)
  542. return NULL;
  543. return smack_netlbladdrs;
  544. }
  545. static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
  546. {
  547. struct smk_netlbladdr *skp = ((struct smk_netlbladdr *) v)->smk_next;
  548. if (skp == NULL)
  549. *pos = SEQ_READ_FINISHED;
  550. return skp;
  551. }
  552. #define BEBITS (sizeof(__be32) * 8)
  553. /*
  554. * Print host/label pairs
  555. */
  556. static int netlbladdr_seq_show(struct seq_file *s, void *v)
  557. {
  558. struct smk_netlbladdr *skp = (struct smk_netlbladdr *) v;
  559. unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
  560. int maskn;
  561. u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
  562. for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
  563. seq_printf(s, "%u.%u.%u.%u/%d %s\n",
  564. hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
  565. return 0;
  566. }
  567. static void netlbladdr_seq_stop(struct seq_file *s, void *v)
  568. {
  569. /* No-op */
  570. }
  571. static struct seq_operations netlbladdr_seq_ops = {
  572. .start = netlbladdr_seq_start,
  573. .stop = netlbladdr_seq_stop,
  574. .next = netlbladdr_seq_next,
  575. .show = netlbladdr_seq_show,
  576. };
  577. /**
  578. * smk_open_netlbladdr - open() for /smack/netlabel
  579. * @inode: inode structure representing file
  580. * @file: "netlabel" file pointer
  581. *
  582. * Connect our netlbladdr_seq_* operations with /smack/netlabel
  583. * file_operations
  584. */
  585. static int smk_open_netlbladdr(struct inode *inode, struct file *file)
  586. {
  587. return seq_open(file, &netlbladdr_seq_ops);
  588. }
  589. /**
  590. * smk_netlbladdr_insert
  591. * @new : netlabel to insert
  592. *
  593. * This helper insert netlabel in the smack_netlbladdrs list
  594. * sorted by netmask length (longest to smallest)
  595. */
  596. static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
  597. {
  598. struct smk_netlbladdr *m;
  599. if (smack_netlbladdrs == NULL) {
  600. smack_netlbladdrs = new;
  601. return;
  602. }
  603. /* the comparison '>' is a bit hacky, but works */
  604. if (new->smk_mask.s_addr > smack_netlbladdrs->smk_mask.s_addr) {
  605. new->smk_next = smack_netlbladdrs;
  606. smack_netlbladdrs = new;
  607. return;
  608. }
  609. for (m = smack_netlbladdrs; m != NULL; m = m->smk_next) {
  610. if (m->smk_next == NULL) {
  611. m->smk_next = new;
  612. return;
  613. }
  614. if (new->smk_mask.s_addr > m->smk_next->smk_mask.s_addr) {
  615. new->smk_next = m->smk_next;
  616. m->smk_next = new;
  617. return;
  618. }
  619. }
  620. }
  621. /**
  622. * smk_write_netlbladdr - write() for /smack/netlabel
  623. * @filp: file pointer, not actually used
  624. * @buf: where to get the data from
  625. * @count: bytes sent
  626. * @ppos: where to start
  627. *
  628. * Accepts only one netlbladdr per write call.
  629. * Returns number of bytes written or error code, as appropriate
  630. */
  631. static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
  632. size_t count, loff_t *ppos)
  633. {
  634. struct smk_netlbladdr *skp;
  635. struct sockaddr_in newname;
  636. char smack[SMK_LABELLEN];
  637. char *sp;
  638. char data[SMK_NETLBLADDRMAX];
  639. char *host = (char *)&newname.sin_addr.s_addr;
  640. int rc;
  641. struct netlbl_audit audit_info;
  642. struct in_addr mask;
  643. unsigned int m;
  644. u32 mask_bits = (1<<31);
  645. __be32 nsa;
  646. u32 temp_mask;
  647. /*
  648. * Must have privilege.
  649. * No partial writes.
  650. * Enough data must be present.
  651. * "<addr/mask, as a.b.c.d/e><space><label>"
  652. * "<addr, as a.b.c.d><space><label>"
  653. */
  654. if (!capable(CAP_MAC_ADMIN))
  655. return -EPERM;
  656. if (*ppos != 0)
  657. return -EINVAL;
  658. if (count < SMK_NETLBLADDRMIN || count > SMK_NETLBLADDRMAX)
  659. return -EINVAL;
  660. if (copy_from_user(data, buf, count) != 0)
  661. return -EFAULT;
  662. data[count] = '\0';
  663. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
  664. &host[0], &host[1], &host[2], &host[3], &m, smack);
  665. if (rc != 6) {
  666. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
  667. &host[0], &host[1], &host[2], &host[3], smack);
  668. if (rc != 5)
  669. return -EINVAL;
  670. m = BEBITS;
  671. }
  672. if (m > BEBITS)
  673. return -EINVAL;
  674. sp = smk_import(smack, 0);
  675. if (sp == NULL)
  676. return -EINVAL;
  677. for (temp_mask = 0; m > 0; m--) {
  678. temp_mask |= mask_bits;
  679. mask_bits >>= 1;
  680. }
  681. mask.s_addr = cpu_to_be32(temp_mask);
  682. newname.sin_addr.s_addr &= mask.s_addr;
  683. /*
  684. * Only allow one writer at a time. Writes should be
  685. * quite rare and small in any case.
  686. */
  687. mutex_lock(&smk_netlbladdr_lock);
  688. nsa = newname.sin_addr.s_addr;
  689. /* try to find if the prefix is already in the list */
  690. for (skp = smack_netlbladdrs; skp != NULL; skp = skp->smk_next)
  691. if (skp->smk_host.sin_addr.s_addr == nsa &&
  692. skp->smk_mask.s_addr == mask.s_addr)
  693. break;
  694. smk_netlabel_audit_set(&audit_info);
  695. if (skp == NULL) {
  696. skp = kzalloc(sizeof(*skp), GFP_KERNEL);
  697. if (skp == NULL)
  698. rc = -ENOMEM;
  699. else {
  700. rc = 0;
  701. skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
  702. skp->smk_mask.s_addr = mask.s_addr;
  703. skp->smk_label = sp;
  704. smk_netlbladdr_insert(skp);
  705. }
  706. } else {
  707. rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
  708. &skp->smk_host.sin_addr, &skp->smk_mask,
  709. PF_INET, &audit_info);
  710. skp->smk_label = sp;
  711. }
  712. /*
  713. * Now tell netlabel about the single label nature of
  714. * this host so that incoming packets get labeled.
  715. */
  716. if (rc == 0)
  717. rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
  718. &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
  719. smack_to_secid(skp->smk_label), &audit_info);
  720. if (rc == 0)
  721. rc = count;
  722. mutex_unlock(&smk_netlbladdr_lock);
  723. return rc;
  724. }
  725. static const struct file_operations smk_netlbladdr_ops = {
  726. .open = smk_open_netlbladdr,
  727. .read = seq_read,
  728. .llseek = seq_lseek,
  729. .write = smk_write_netlbladdr,
  730. .release = seq_release,
  731. };
  732. /**
  733. * smk_read_doi - read() for /smack/doi
  734. * @filp: file pointer, not actually used
  735. * @buf: where to put the result
  736. * @count: maximum to send along
  737. * @ppos: where to start
  738. *
  739. * Returns number of bytes read or error code, as appropriate
  740. */
  741. static ssize_t smk_read_doi(struct file *filp, char __user *buf,
  742. size_t count, loff_t *ppos)
  743. {
  744. char temp[80];
  745. ssize_t rc;
  746. if (*ppos != 0)
  747. return 0;
  748. sprintf(temp, "%d", smk_cipso_doi_value);
  749. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  750. return rc;
  751. }
  752. /**
  753. * smk_write_doi - write() for /smack/doi
  754. * @filp: file pointer, not actually used
  755. * @buf: where to get the data from
  756. * @count: bytes sent
  757. * @ppos: where to start
  758. *
  759. * Returns number of bytes written or error code, as appropriate
  760. */
  761. static ssize_t smk_write_doi(struct file *file, const char __user *buf,
  762. size_t count, loff_t *ppos)
  763. {
  764. char temp[80];
  765. int i;
  766. if (!capable(CAP_MAC_ADMIN))
  767. return -EPERM;
  768. if (count >= sizeof(temp) || count == 0)
  769. return -EINVAL;
  770. if (copy_from_user(temp, buf, count) != 0)
  771. return -EFAULT;
  772. temp[count] = '\0';
  773. if (sscanf(temp, "%d", &i) != 1)
  774. return -EINVAL;
  775. smk_cipso_doi_value = i;
  776. smk_cipso_doi();
  777. return count;
  778. }
  779. static const struct file_operations smk_doi_ops = {
  780. .read = smk_read_doi,
  781. .write = smk_write_doi,
  782. };
  783. /**
  784. * smk_read_direct - read() for /smack/direct
  785. * @filp: file pointer, not actually used
  786. * @buf: where to put the result
  787. * @count: maximum to send along
  788. * @ppos: where to start
  789. *
  790. * Returns number of bytes read or error code, as appropriate
  791. */
  792. static ssize_t smk_read_direct(struct file *filp, char __user *buf,
  793. size_t count, loff_t *ppos)
  794. {
  795. char temp[80];
  796. ssize_t rc;
  797. if (*ppos != 0)
  798. return 0;
  799. sprintf(temp, "%d", smack_cipso_direct);
  800. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  801. return rc;
  802. }
  803. /**
  804. * smk_write_direct - write() for /smack/direct
  805. * @filp: file pointer, not actually used
  806. * @buf: where to get the data from
  807. * @count: bytes sent
  808. * @ppos: where to start
  809. *
  810. * Returns number of bytes written or error code, as appropriate
  811. */
  812. static ssize_t smk_write_direct(struct file *file, const char __user *buf,
  813. size_t count, loff_t *ppos)
  814. {
  815. char temp[80];
  816. int i;
  817. if (!capable(CAP_MAC_ADMIN))
  818. return -EPERM;
  819. if (count >= sizeof(temp) || count == 0)
  820. return -EINVAL;
  821. if (copy_from_user(temp, buf, count) != 0)
  822. return -EFAULT;
  823. temp[count] = '\0';
  824. if (sscanf(temp, "%d", &i) != 1)
  825. return -EINVAL;
  826. smack_cipso_direct = i;
  827. return count;
  828. }
  829. static const struct file_operations smk_direct_ops = {
  830. .read = smk_read_direct,
  831. .write = smk_write_direct,
  832. };
  833. /**
  834. * smk_read_ambient - read() for /smack/ambient
  835. * @filp: file pointer, not actually used
  836. * @buf: where to put the result
  837. * @cn: maximum to send along
  838. * @ppos: where to start
  839. *
  840. * Returns number of bytes read or error code, as appropriate
  841. */
  842. static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
  843. size_t cn, loff_t *ppos)
  844. {
  845. ssize_t rc;
  846. int asize;
  847. if (*ppos != 0)
  848. return 0;
  849. /*
  850. * Being careful to avoid a problem in the case where
  851. * smack_net_ambient gets changed in midstream.
  852. */
  853. mutex_lock(&smack_ambient_lock);
  854. asize = strlen(smack_net_ambient) + 1;
  855. if (cn >= asize)
  856. rc = simple_read_from_buffer(buf, cn, ppos,
  857. smack_net_ambient, asize);
  858. else
  859. rc = -EINVAL;
  860. mutex_unlock(&smack_ambient_lock);
  861. return rc;
  862. }
  863. /**
  864. * smk_write_ambient - write() for /smack/ambient
  865. * @filp: file pointer, not actually used
  866. * @buf: where to get the data from
  867. * @count: bytes sent
  868. * @ppos: where to start
  869. *
  870. * Returns number of bytes written or error code, as appropriate
  871. */
  872. static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
  873. size_t count, loff_t *ppos)
  874. {
  875. char in[SMK_LABELLEN];
  876. char *oldambient;
  877. char *smack;
  878. if (!capable(CAP_MAC_ADMIN))
  879. return -EPERM;
  880. if (count >= SMK_LABELLEN)
  881. return -EINVAL;
  882. if (copy_from_user(in, buf, count) != 0)
  883. return -EFAULT;
  884. smack = smk_import(in, count);
  885. if (smack == NULL)
  886. return -EINVAL;
  887. mutex_lock(&smack_ambient_lock);
  888. oldambient = smack_net_ambient;
  889. smack_net_ambient = smack;
  890. smk_unlbl_ambient(oldambient);
  891. mutex_unlock(&smack_ambient_lock);
  892. return count;
  893. }
  894. static const struct file_operations smk_ambient_ops = {
  895. .read = smk_read_ambient,
  896. .write = smk_write_ambient,
  897. };
  898. /**
  899. * smk_read_onlycap - read() for /smack/onlycap
  900. * @filp: file pointer, not actually used
  901. * @buf: where to put the result
  902. * @cn: maximum to send along
  903. * @ppos: where to start
  904. *
  905. * Returns number of bytes read or error code, as appropriate
  906. */
  907. static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
  908. size_t cn, loff_t *ppos)
  909. {
  910. char *smack = "";
  911. ssize_t rc = -EINVAL;
  912. int asize;
  913. if (*ppos != 0)
  914. return 0;
  915. if (smack_onlycap != NULL)
  916. smack = smack_onlycap;
  917. asize = strlen(smack) + 1;
  918. if (cn >= asize)
  919. rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
  920. return rc;
  921. }
  922. /**
  923. * smk_write_onlycap - write() for /smack/onlycap
  924. * @filp: file pointer, not actually used
  925. * @buf: where to get the data from
  926. * @count: bytes sent
  927. * @ppos: where to start
  928. *
  929. * Returns number of bytes written or error code, as appropriate
  930. */
  931. static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
  932. size_t count, loff_t *ppos)
  933. {
  934. char in[SMK_LABELLEN];
  935. char *sp = current->cred->security;
  936. if (!capable(CAP_MAC_ADMIN))
  937. return -EPERM;
  938. /*
  939. * This can be done using smk_access() but is done
  940. * explicitly for clarity. The smk_access() implementation
  941. * would use smk_access(smack_onlycap, MAY_WRITE)
  942. */
  943. if (smack_onlycap != NULL && smack_onlycap != sp)
  944. return -EPERM;
  945. if (count >= SMK_LABELLEN)
  946. return -EINVAL;
  947. if (copy_from_user(in, buf, count) != 0)
  948. return -EFAULT;
  949. /*
  950. * Should the null string be passed in unset the onlycap value.
  951. * This seems like something to be careful with as usually
  952. * smk_import only expects to return NULL for errors. It
  953. * is usually the case that a nullstring or "\n" would be
  954. * bad to pass to smk_import but in fact this is useful here.
  955. */
  956. smack_onlycap = smk_import(in, count);
  957. return count;
  958. }
  959. static const struct file_operations smk_onlycap_ops = {
  960. .read = smk_read_onlycap,
  961. .write = smk_write_onlycap,
  962. };
  963. /**
  964. * smk_fill_super - fill the /smackfs superblock
  965. * @sb: the empty superblock
  966. * @data: unused
  967. * @silent: unused
  968. *
  969. * Fill in the well known entries for /smack
  970. *
  971. * Returns 0 on success, an error code on failure
  972. */
  973. static int smk_fill_super(struct super_block *sb, void *data, int silent)
  974. {
  975. int rc;
  976. struct inode *root_inode;
  977. static struct tree_descr smack_files[] = {
  978. [SMK_LOAD] =
  979. {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
  980. [SMK_CIPSO] =
  981. {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
  982. [SMK_DOI] =
  983. {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
  984. [SMK_DIRECT] =
  985. {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
  986. [SMK_AMBIENT] =
  987. {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
  988. [SMK_NETLBLADDR] =
  989. {"netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
  990. [SMK_ONLYCAP] =
  991. {"onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
  992. /* last one */ {""}
  993. };
  994. rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
  995. if (rc != 0) {
  996. printk(KERN_ERR "%s failed %d while creating inodes\n",
  997. __func__, rc);
  998. return rc;
  999. }
  1000. root_inode = sb->s_root->d_inode;
  1001. root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
  1002. return 0;
  1003. }
  1004. /**
  1005. * smk_get_sb - get the smackfs superblock
  1006. * @fs_type: passed along without comment
  1007. * @flags: passed along without comment
  1008. * @dev_name: passed along without comment
  1009. * @data: passed along without comment
  1010. * @mnt: passed along without comment
  1011. *
  1012. * Just passes everything along.
  1013. *
  1014. * Returns what the lower level code does.
  1015. */
  1016. static int smk_get_sb(struct file_system_type *fs_type,
  1017. int flags, const char *dev_name, void *data,
  1018. struct vfsmount *mnt)
  1019. {
  1020. return get_sb_single(fs_type, flags, data, smk_fill_super, mnt);
  1021. }
  1022. static struct file_system_type smk_fs_type = {
  1023. .name = "smackfs",
  1024. .get_sb = smk_get_sb,
  1025. .kill_sb = kill_litter_super,
  1026. };
  1027. static struct vfsmount *smackfs_mount;
  1028. /**
  1029. * init_smk_fs - get the smackfs superblock
  1030. *
  1031. * register the smackfs
  1032. *
  1033. * Do not register smackfs if Smack wasn't enabled
  1034. * on boot. We can not put this method normally under the
  1035. * smack_init() code path since the security subsystem get
  1036. * initialized before the vfs caches.
  1037. *
  1038. * Returns true if we were not chosen on boot or if
  1039. * we were chosen and filesystem registration succeeded.
  1040. */
  1041. static int __init init_smk_fs(void)
  1042. {
  1043. int err;
  1044. if (!security_module_enable(&smack_ops))
  1045. return 0;
  1046. err = register_filesystem(&smk_fs_type);
  1047. if (!err) {
  1048. smackfs_mount = kern_mount(&smk_fs_type);
  1049. if (IS_ERR(smackfs_mount)) {
  1050. printk(KERN_ERR "smackfs: could not mount!\n");
  1051. err = PTR_ERR(smackfs_mount);
  1052. smackfs_mount = NULL;
  1053. }
  1054. }
  1055. smk_cipso_doi();
  1056. smk_unlbl_ambient(NULL);
  1057. return err;
  1058. }
  1059. __initcall(init_smk_fs);