smackfs.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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/netlabel.h>
  23. #include <net/cipso_ipv4.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/ctype.h>
  26. #include <linux/audit.h>
  27. #include "smack.h"
  28. /*
  29. * smackfs pseudo filesystem.
  30. */
  31. enum smk_inos {
  32. SMK_ROOT_INO = 2,
  33. SMK_LOAD = 3, /* load policy */
  34. SMK_CIPSO = 4, /* load label -> CIPSO mapping */
  35. SMK_DOI = 5, /* CIPSO DOI */
  36. SMK_DIRECT = 6, /* CIPSO level indicating direct label */
  37. SMK_AMBIENT = 7, /* internet ambient label */
  38. SMK_NLTYPE = 8, /* label scheme to use by default */
  39. };
  40. /*
  41. * List locks
  42. */
  43. static DEFINE_MUTEX(smack_list_lock);
  44. static DEFINE_MUTEX(smack_cipso_lock);
  45. static DEFINE_MUTEX(smack_ambient_lock);
  46. /*
  47. * This is the "ambient" label for network traffic.
  48. * If it isn't somehow marked, use this.
  49. * It can be reset via smackfs/ambient
  50. */
  51. char *smack_net_ambient = smack_known_floor.smk_known;
  52. /*
  53. * This is the default packet marking scheme for network traffic.
  54. * It can be reset via smackfs/nltype
  55. */
  56. int smack_net_nltype = NETLBL_NLTYPE_CIPSOV4;
  57. /*
  58. * This is the level in a CIPSO header that indicates a
  59. * smack label is contained directly in the category set.
  60. * It can be reset via smackfs/direct
  61. */
  62. int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
  63. static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
  64. struct smk_list_entry *smack_list;
  65. #define SEQ_READ_FINISHED 1
  66. /*
  67. * Values for parsing cipso rules
  68. * SMK_DIGITLEN: Length of a digit field in a rule.
  69. * SMK_CIPSOMIN: Minimum possible cipso rule length.
  70. * SMK_CIPSOMAX: Maximum possible cipso rule length.
  71. */
  72. #define SMK_DIGITLEN 4
  73. #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
  74. #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
  75. /*
  76. * Values for parsing MAC rules
  77. * SMK_ACCESS: Maximum possible combination of access permissions
  78. * SMK_ACCESSLEN: Maximum length for a rule access field
  79. * SMK_LOADLEN: Smack rule length
  80. */
  81. #define SMK_ACCESS "rwxa"
  82. #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
  83. #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
  84. /*
  85. * Seq_file read operations for /smack/load
  86. */
  87. static void *load_seq_start(struct seq_file *s, loff_t *pos)
  88. {
  89. if (*pos == SEQ_READ_FINISHED)
  90. return NULL;
  91. return smack_list;
  92. }
  93. static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos)
  94. {
  95. struct smk_list_entry *skp = ((struct smk_list_entry *) v)->smk_next;
  96. if (skp == NULL)
  97. *pos = SEQ_READ_FINISHED;
  98. return skp;
  99. }
  100. static int load_seq_show(struct seq_file *s, void *v)
  101. {
  102. struct smk_list_entry *slp = (struct smk_list_entry *) v;
  103. struct smack_rule *srp = &slp->smk_rule;
  104. seq_printf(s, "%s %s", (char *)srp->smk_subject,
  105. (char *)srp->smk_object);
  106. seq_putc(s, ' ');
  107. if (srp->smk_access & MAY_READ)
  108. seq_putc(s, 'r');
  109. if (srp->smk_access & MAY_WRITE)
  110. seq_putc(s, 'w');
  111. if (srp->smk_access & MAY_EXEC)
  112. seq_putc(s, 'x');
  113. if (srp->smk_access & MAY_APPEND)
  114. seq_putc(s, 'a');
  115. if (srp->smk_access == 0)
  116. seq_putc(s, '-');
  117. seq_putc(s, '\n');
  118. return 0;
  119. }
  120. static void load_seq_stop(struct seq_file *s, void *v)
  121. {
  122. /* No-op */
  123. }
  124. static struct seq_operations load_seq_ops = {
  125. .start = load_seq_start,
  126. .next = load_seq_next,
  127. .show = load_seq_show,
  128. .stop = load_seq_stop,
  129. };
  130. /**
  131. * smk_open_load - open() for /smack/load
  132. * @inode: inode structure representing file
  133. * @file: "load" file pointer
  134. *
  135. * For reading, use load_seq_* seq_file reading operations.
  136. */
  137. static int smk_open_load(struct inode *inode, struct file *file)
  138. {
  139. return seq_open(file, &load_seq_ops);
  140. }
  141. /**
  142. * smk_set_access - add a rule to the rule list
  143. * @srp: the new rule to add
  144. *
  145. * Looks through the current subject/object/access list for
  146. * the subject/object pair and replaces the access that was
  147. * there. If the pair isn't found add it with the specified
  148. * access.
  149. */
  150. static void smk_set_access(struct smack_rule *srp)
  151. {
  152. struct smk_list_entry *sp;
  153. struct smk_list_entry *newp;
  154. mutex_lock(&smack_list_lock);
  155. for (sp = smack_list; sp != NULL; sp = sp->smk_next)
  156. if (sp->smk_rule.smk_subject == srp->smk_subject &&
  157. sp->smk_rule.smk_object == srp->smk_object) {
  158. sp->smk_rule.smk_access = srp->smk_access;
  159. break;
  160. }
  161. if (sp == NULL) {
  162. newp = kzalloc(sizeof(struct smk_list_entry), GFP_KERNEL);
  163. newp->smk_rule = *srp;
  164. newp->smk_next = smack_list;
  165. smack_list = newp;
  166. }
  167. mutex_unlock(&smack_list_lock);
  168. return;
  169. }
  170. /**
  171. * smk_write_load - write() for /smack/load
  172. * @filp: file pointer, not actually used
  173. * @buf: where to get the data from
  174. * @count: bytes sent
  175. * @ppos: where to start - must be 0
  176. *
  177. * Get one smack access rule from above.
  178. * The format is exactly:
  179. * char subject[SMK_LABELLEN]
  180. * char object[SMK_LABELLEN]
  181. * char access[SMK_ACCESSLEN]
  182. *
  183. * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes.
  184. */
  185. static ssize_t smk_write_load(struct file *file, const char __user *buf,
  186. size_t count, loff_t *ppos)
  187. {
  188. struct smack_rule rule;
  189. char *data;
  190. int rc = -EINVAL;
  191. /*
  192. * Must have privilege.
  193. * No partial writes.
  194. * Enough data must be present.
  195. */
  196. if (!capable(CAP_MAC_ADMIN))
  197. return -EPERM;
  198. if (*ppos != 0)
  199. return -EINVAL;
  200. if (count != SMK_LOADLEN)
  201. return -EINVAL;
  202. data = kzalloc(count, GFP_KERNEL);
  203. if (data == NULL)
  204. return -ENOMEM;
  205. if (copy_from_user(data, buf, count) != 0) {
  206. rc = -EFAULT;
  207. goto out;
  208. }
  209. rule.smk_subject = smk_import(data, 0);
  210. if (rule.smk_subject == NULL)
  211. goto out;
  212. rule.smk_object = smk_import(data + SMK_LABELLEN, 0);
  213. if (rule.smk_object == NULL)
  214. goto out;
  215. rule.smk_access = 0;
  216. switch (data[SMK_LABELLEN + SMK_LABELLEN]) {
  217. case '-':
  218. break;
  219. case 'r':
  220. case 'R':
  221. rule.smk_access |= MAY_READ;
  222. break;
  223. default:
  224. goto out;
  225. }
  226. switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) {
  227. case '-':
  228. break;
  229. case 'w':
  230. case 'W':
  231. rule.smk_access |= MAY_WRITE;
  232. break;
  233. default:
  234. goto out;
  235. }
  236. switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) {
  237. case '-':
  238. break;
  239. case 'x':
  240. case 'X':
  241. rule.smk_access |= MAY_EXEC;
  242. break;
  243. default:
  244. goto out;
  245. }
  246. switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) {
  247. case '-':
  248. break;
  249. case 'a':
  250. case 'A':
  251. rule.smk_access |= MAY_READ;
  252. break;
  253. default:
  254. goto out;
  255. }
  256. smk_set_access(&rule);
  257. rc = count;
  258. out:
  259. kfree(data);
  260. return rc;
  261. }
  262. static const struct file_operations smk_load_ops = {
  263. .open = smk_open_load,
  264. .read = seq_read,
  265. .llseek = seq_lseek,
  266. .write = smk_write_load,
  267. .release = seq_release,
  268. };
  269. /**
  270. * smk_cipso_doi - initialize the CIPSO domain
  271. */
  272. static void smk_cipso_doi(void)
  273. {
  274. int rc;
  275. struct cipso_v4_doi *doip;
  276. struct netlbl_audit audit_info;
  277. audit_info.loginuid = audit_get_loginuid(current);
  278. audit_info.sessionid = audit_get_sessionid(current);
  279. audit_info.secid = smack_to_secid(current->security);
  280. rc = netlbl_cfg_map_del(NULL, &audit_info);
  281. if (rc != 0)
  282. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  283. __func__, __LINE__, rc);
  284. doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
  285. if (doip == NULL)
  286. panic("smack: Failed to initialize cipso DOI.\n");
  287. doip->map.std = NULL;
  288. doip->doi = smk_cipso_doi_value;
  289. doip->type = CIPSO_V4_MAP_PASS;
  290. doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
  291. for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
  292. doip->tags[rc] = CIPSO_V4_TAG_INVALID;
  293. rc = netlbl_cfg_cipsov4_add_map(doip, NULL, &audit_info);
  294. if (rc != 0) {
  295. printk(KERN_WARNING "%s:%d add rc = %d\n",
  296. __func__, __LINE__, rc);
  297. kfree(doip);
  298. }
  299. }
  300. /**
  301. * smk_unlbl_ambient - initialize the unlabeled domain
  302. */
  303. static void smk_unlbl_ambient(char *oldambient)
  304. {
  305. int rc;
  306. struct netlbl_audit audit_info;
  307. audit_info.loginuid = audit_get_loginuid(current);
  308. audit_info.sessionid = audit_get_sessionid(current);
  309. audit_info.secid = smack_to_secid(current->security);
  310. if (oldambient != NULL) {
  311. rc = netlbl_cfg_map_del(oldambient, &audit_info);
  312. if (rc != 0)
  313. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  314. __func__, __LINE__, rc);
  315. }
  316. rc = netlbl_cfg_unlbl_add_map(smack_net_ambient, &audit_info);
  317. if (rc != 0)
  318. printk(KERN_WARNING "%s:%d add rc = %d\n",
  319. __func__, __LINE__, rc);
  320. }
  321. /*
  322. * Seq_file read operations for /smack/cipso
  323. */
  324. static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
  325. {
  326. if (*pos == SEQ_READ_FINISHED)
  327. return NULL;
  328. return smack_known;
  329. }
  330. static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
  331. {
  332. struct smack_known *skp = ((struct smack_known *) v)->smk_next;
  333. /*
  334. * Omit labels with no associated cipso value
  335. */
  336. while (skp != NULL && !skp->smk_cipso)
  337. skp = skp->smk_next;
  338. if (skp == NULL)
  339. *pos = SEQ_READ_FINISHED;
  340. return skp;
  341. }
  342. /*
  343. * Print cipso labels in format:
  344. * label level[/cat[,cat]]
  345. */
  346. static int cipso_seq_show(struct seq_file *s, void *v)
  347. {
  348. struct smack_known *skp = (struct smack_known *) v;
  349. struct smack_cipso *scp = skp->smk_cipso;
  350. char *cbp;
  351. char sep = '/';
  352. int cat = 1;
  353. int i;
  354. unsigned char m;
  355. if (scp == NULL)
  356. return 0;
  357. seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
  358. cbp = scp->smk_catset;
  359. for (i = 0; i < SMK_LABELLEN; i++)
  360. for (m = 0x80; m != 0; m >>= 1) {
  361. if (m & cbp[i]) {
  362. seq_printf(s, "%c%d", sep, cat);
  363. sep = ',';
  364. }
  365. cat++;
  366. }
  367. seq_putc(s, '\n');
  368. return 0;
  369. }
  370. static void cipso_seq_stop(struct seq_file *s, void *v)
  371. {
  372. /* No-op */
  373. }
  374. static struct seq_operations cipso_seq_ops = {
  375. .start = cipso_seq_start,
  376. .stop = cipso_seq_stop,
  377. .next = cipso_seq_next,
  378. .show = cipso_seq_show,
  379. };
  380. /**
  381. * smk_open_cipso - open() for /smack/cipso
  382. * @inode: inode structure representing file
  383. * @file: "cipso" file pointer
  384. *
  385. * Connect our cipso_seq_* operations with /smack/cipso
  386. * file_operations
  387. */
  388. static int smk_open_cipso(struct inode *inode, struct file *file)
  389. {
  390. return seq_open(file, &cipso_seq_ops);
  391. }
  392. /**
  393. * smk_write_cipso - write() for /smack/cipso
  394. * @filp: file pointer, not actually used
  395. * @buf: where to get the data from
  396. * @count: bytes sent
  397. * @ppos: where to start
  398. *
  399. * Accepts only one cipso rule per write call.
  400. * Returns number of bytes written or error code, as appropriate
  401. */
  402. static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
  403. size_t count, loff_t *ppos)
  404. {
  405. struct smack_known *skp;
  406. struct smack_cipso *scp = NULL;
  407. char mapcatset[SMK_LABELLEN];
  408. int maplevel;
  409. int cat;
  410. int catlen;
  411. ssize_t rc = -EINVAL;
  412. char *data = NULL;
  413. char *rule;
  414. int ret;
  415. int i;
  416. /*
  417. * Must have privilege.
  418. * No partial writes.
  419. * Enough data must be present.
  420. */
  421. if (!capable(CAP_MAC_ADMIN))
  422. return -EPERM;
  423. if (*ppos != 0)
  424. return -EINVAL;
  425. if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
  426. return -EINVAL;
  427. data = kzalloc(count + 1, GFP_KERNEL);
  428. if (data == NULL)
  429. return -ENOMEM;
  430. if (copy_from_user(data, buf, count) != 0) {
  431. rc = -EFAULT;
  432. goto unlockedout;
  433. }
  434. data[count] = '\0';
  435. rule = data;
  436. /*
  437. * Only allow one writer at a time. Writes should be
  438. * quite rare and small in any case.
  439. */
  440. mutex_lock(&smack_cipso_lock);
  441. skp = smk_import_entry(rule, 0);
  442. if (skp == NULL)
  443. goto out;
  444. rule += SMK_LABELLEN;;
  445. ret = sscanf(rule, "%d", &maplevel);
  446. if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
  447. goto out;
  448. rule += SMK_DIGITLEN;
  449. ret = sscanf(rule, "%d", &catlen);
  450. if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
  451. goto out;
  452. if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
  453. goto out;
  454. memset(mapcatset, 0, sizeof(mapcatset));
  455. for (i = 0; i < catlen; i++) {
  456. rule += SMK_DIGITLEN;
  457. ret = sscanf(rule, "%d", &cat);
  458. if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
  459. goto out;
  460. smack_catset_bit(cat, mapcatset);
  461. }
  462. if (skp->smk_cipso == NULL) {
  463. scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
  464. if (scp == NULL) {
  465. rc = -ENOMEM;
  466. goto out;
  467. }
  468. }
  469. spin_lock_bh(&skp->smk_cipsolock);
  470. if (scp == NULL)
  471. scp = skp->smk_cipso;
  472. else
  473. skp->smk_cipso = scp;
  474. scp->smk_level = maplevel;
  475. memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
  476. spin_unlock_bh(&skp->smk_cipsolock);
  477. rc = count;
  478. out:
  479. mutex_unlock(&smack_cipso_lock);
  480. unlockedout:
  481. kfree(data);
  482. return rc;
  483. }
  484. static const struct file_operations smk_cipso_ops = {
  485. .open = smk_open_cipso,
  486. .read = seq_read,
  487. .llseek = seq_lseek,
  488. .write = smk_write_cipso,
  489. .release = seq_release,
  490. };
  491. /**
  492. * smk_read_doi - read() for /smack/doi
  493. * @filp: file pointer, not actually used
  494. * @buf: where to put the result
  495. * @count: maximum to send along
  496. * @ppos: where to start
  497. *
  498. * Returns number of bytes read or error code, as appropriate
  499. */
  500. static ssize_t smk_read_doi(struct file *filp, char __user *buf,
  501. size_t count, loff_t *ppos)
  502. {
  503. char temp[80];
  504. ssize_t rc;
  505. if (*ppos != 0)
  506. return 0;
  507. sprintf(temp, "%d", smk_cipso_doi_value);
  508. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  509. return rc;
  510. }
  511. /**
  512. * smk_write_doi - write() for /smack/doi
  513. * @filp: file pointer, not actually used
  514. * @buf: where to get the data from
  515. * @count: bytes sent
  516. * @ppos: where to start
  517. *
  518. * Returns number of bytes written or error code, as appropriate
  519. */
  520. static ssize_t smk_write_doi(struct file *file, const char __user *buf,
  521. size_t count, loff_t *ppos)
  522. {
  523. char temp[80];
  524. int i;
  525. if (!capable(CAP_MAC_ADMIN))
  526. return -EPERM;
  527. if (count >= sizeof(temp) || count == 0)
  528. return -EINVAL;
  529. if (copy_from_user(temp, buf, count) != 0)
  530. return -EFAULT;
  531. temp[count] = '\0';
  532. if (sscanf(temp, "%d", &i) != 1)
  533. return -EINVAL;
  534. smk_cipso_doi_value = i;
  535. smk_cipso_doi();
  536. return count;
  537. }
  538. static const struct file_operations smk_doi_ops = {
  539. .read = smk_read_doi,
  540. .write = smk_write_doi,
  541. };
  542. /**
  543. * smk_read_direct - read() for /smack/direct
  544. * @filp: file pointer, not actually used
  545. * @buf: where to put the result
  546. * @count: maximum to send along
  547. * @ppos: where to start
  548. *
  549. * Returns number of bytes read or error code, as appropriate
  550. */
  551. static ssize_t smk_read_direct(struct file *filp, char __user *buf,
  552. size_t count, loff_t *ppos)
  553. {
  554. char temp[80];
  555. ssize_t rc;
  556. if (*ppos != 0)
  557. return 0;
  558. sprintf(temp, "%d", smack_cipso_direct);
  559. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  560. return rc;
  561. }
  562. /**
  563. * smk_write_direct - write() for /smack/direct
  564. * @filp: file pointer, not actually used
  565. * @buf: where to get the data from
  566. * @count: bytes sent
  567. * @ppos: where to start
  568. *
  569. * Returns number of bytes written or error code, as appropriate
  570. */
  571. static ssize_t smk_write_direct(struct file *file, const char __user *buf,
  572. size_t count, loff_t *ppos)
  573. {
  574. char temp[80];
  575. int i;
  576. if (!capable(CAP_MAC_ADMIN))
  577. return -EPERM;
  578. if (count >= sizeof(temp) || count == 0)
  579. return -EINVAL;
  580. if (copy_from_user(temp, buf, count) != 0)
  581. return -EFAULT;
  582. temp[count] = '\0';
  583. if (sscanf(temp, "%d", &i) != 1)
  584. return -EINVAL;
  585. smack_cipso_direct = i;
  586. return count;
  587. }
  588. static const struct file_operations smk_direct_ops = {
  589. .read = smk_read_direct,
  590. .write = smk_write_direct,
  591. };
  592. /**
  593. * smk_read_ambient - read() for /smack/ambient
  594. * @filp: file pointer, not actually used
  595. * @buf: where to put the result
  596. * @cn: maximum to send along
  597. * @ppos: where to start
  598. *
  599. * Returns number of bytes read or error code, as appropriate
  600. */
  601. static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
  602. size_t cn, loff_t *ppos)
  603. {
  604. ssize_t rc;
  605. int asize;
  606. if (*ppos != 0)
  607. return 0;
  608. /*
  609. * Being careful to avoid a problem in the case where
  610. * smack_net_ambient gets changed in midstream.
  611. */
  612. mutex_lock(&smack_ambient_lock);
  613. asize = strlen(smack_net_ambient) + 1;
  614. if (cn >= asize)
  615. rc = simple_read_from_buffer(buf, cn, ppos,
  616. smack_net_ambient, asize);
  617. else
  618. rc = -EINVAL;
  619. mutex_unlock(&smack_ambient_lock);
  620. return rc;
  621. }
  622. /**
  623. * smk_write_ambient - write() for /smack/ambient
  624. * @filp: file pointer, not actually used
  625. * @buf: where to get the data from
  626. * @count: bytes sent
  627. * @ppos: where to start
  628. *
  629. * Returns number of bytes written or error code, as appropriate
  630. */
  631. static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
  632. size_t count, loff_t *ppos)
  633. {
  634. char in[SMK_LABELLEN];
  635. char *oldambient;
  636. char *smack;
  637. if (!capable(CAP_MAC_ADMIN))
  638. return -EPERM;
  639. if (count >= SMK_LABELLEN)
  640. return -EINVAL;
  641. if (copy_from_user(in, buf, count) != 0)
  642. return -EFAULT;
  643. smack = smk_import(in, count);
  644. if (smack == NULL)
  645. return -EINVAL;
  646. mutex_lock(&smack_ambient_lock);
  647. oldambient = smack_net_ambient;
  648. smack_net_ambient = smack;
  649. smk_unlbl_ambient(oldambient);
  650. mutex_unlock(&smack_ambient_lock);
  651. return count;
  652. }
  653. static const struct file_operations smk_ambient_ops = {
  654. .read = smk_read_ambient,
  655. .write = smk_write_ambient,
  656. };
  657. struct option_names {
  658. int o_number;
  659. char *o_name;
  660. char *o_alias;
  661. };
  662. static struct option_names netlbl_choices[] = {
  663. { NETLBL_NLTYPE_RIPSO,
  664. NETLBL_NLTYPE_RIPSO_NAME, "ripso" },
  665. { NETLBL_NLTYPE_CIPSOV4,
  666. NETLBL_NLTYPE_CIPSOV4_NAME, "cipsov4" },
  667. { NETLBL_NLTYPE_CIPSOV4,
  668. NETLBL_NLTYPE_CIPSOV4_NAME, "cipso" },
  669. { NETLBL_NLTYPE_CIPSOV6,
  670. NETLBL_NLTYPE_CIPSOV6_NAME, "cipsov6" },
  671. { NETLBL_NLTYPE_UNLABELED,
  672. NETLBL_NLTYPE_UNLABELED_NAME, "unlabeled" },
  673. };
  674. /**
  675. * smk_read_nltype - read() for /smack/nltype
  676. * @filp: file pointer, not actually used
  677. * @buf: where to put the result
  678. * @count: maximum to send along
  679. * @ppos: where to start
  680. *
  681. * Returns number of bytes read or error code, as appropriate
  682. */
  683. static ssize_t smk_read_nltype(struct file *filp, char __user *buf,
  684. size_t count, loff_t *ppos)
  685. {
  686. char bound[40];
  687. ssize_t rc;
  688. int i;
  689. if (count < SMK_LABELLEN)
  690. return -EINVAL;
  691. if (*ppos != 0)
  692. return 0;
  693. sprintf(bound, "unknown");
  694. for (i = 0; i < ARRAY_SIZE(netlbl_choices); i++)
  695. if (smack_net_nltype == netlbl_choices[i].o_number) {
  696. sprintf(bound, "%s", netlbl_choices[i].o_name);
  697. break;
  698. }
  699. rc = simple_read_from_buffer(buf, count, ppos, bound, strlen(bound));
  700. return rc;
  701. }
  702. /**
  703. * smk_write_nltype - write() for /smack/nltype
  704. * @filp: file pointer, not actually used
  705. * @buf: where to get the data from
  706. * @count: bytes sent
  707. * @ppos: where to start
  708. *
  709. * Returns number of bytes written or error code, as appropriate
  710. */
  711. static ssize_t smk_write_nltype(struct file *file, const char __user *buf,
  712. size_t count, loff_t *ppos)
  713. {
  714. char bound[40];
  715. char *cp;
  716. int i;
  717. if (!capable(CAP_MAC_ADMIN))
  718. return -EPERM;
  719. if (count >= 40)
  720. return -EINVAL;
  721. if (copy_from_user(bound, buf, count) != 0)
  722. return -EFAULT;
  723. bound[count] = '\0';
  724. cp = strchr(bound, ' ');
  725. if (cp != NULL)
  726. *cp = '\0';
  727. cp = strchr(bound, '\n');
  728. if (cp != NULL)
  729. *cp = '\0';
  730. for (i = 0; i < ARRAY_SIZE(netlbl_choices); i++)
  731. if (strcmp(bound, netlbl_choices[i].o_name) == 0 ||
  732. strcmp(bound, netlbl_choices[i].o_alias) == 0) {
  733. smack_net_nltype = netlbl_choices[i].o_number;
  734. return count;
  735. }
  736. /*
  737. * Not a valid choice.
  738. */
  739. return -EINVAL;
  740. }
  741. static const struct file_operations smk_nltype_ops = {
  742. .read = smk_read_nltype,
  743. .write = smk_write_nltype,
  744. };
  745. /**
  746. * smk_fill_super - fill the /smackfs superblock
  747. * @sb: the empty superblock
  748. * @data: unused
  749. * @silent: unused
  750. *
  751. * Fill in the well known entries for /smack
  752. *
  753. * Returns 0 on success, an error code on failure
  754. */
  755. static int smk_fill_super(struct super_block *sb, void *data, int silent)
  756. {
  757. int rc;
  758. struct inode *root_inode;
  759. static struct tree_descr smack_files[] = {
  760. [SMK_LOAD] =
  761. {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
  762. [SMK_CIPSO] =
  763. {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
  764. [SMK_DOI] =
  765. {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
  766. [SMK_DIRECT] =
  767. {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
  768. [SMK_AMBIENT] =
  769. {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
  770. [SMK_NLTYPE] =
  771. {"nltype", &smk_nltype_ops, S_IRUGO|S_IWUSR},
  772. /* last one */ {""}
  773. };
  774. rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
  775. if (rc != 0) {
  776. printk(KERN_ERR "%s failed %d while creating inodes\n",
  777. __func__, rc);
  778. return rc;
  779. }
  780. root_inode = sb->s_root->d_inode;
  781. root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
  782. return 0;
  783. }
  784. /**
  785. * smk_get_sb - get the smackfs superblock
  786. * @fs_type: passed along without comment
  787. * @flags: passed along without comment
  788. * @dev_name: passed along without comment
  789. * @data: passed along without comment
  790. * @mnt: passed along without comment
  791. *
  792. * Just passes everything along.
  793. *
  794. * Returns what the lower level code does.
  795. */
  796. static int smk_get_sb(struct file_system_type *fs_type,
  797. int flags, const char *dev_name, void *data,
  798. struct vfsmount *mnt)
  799. {
  800. return get_sb_single(fs_type, flags, data, smk_fill_super, mnt);
  801. }
  802. static struct file_system_type smk_fs_type = {
  803. .name = "smackfs",
  804. .get_sb = smk_get_sb,
  805. .kill_sb = kill_litter_super,
  806. };
  807. static struct vfsmount *smackfs_mount;
  808. /**
  809. * init_smk_fs - get the smackfs superblock
  810. *
  811. * register the smackfs
  812. *
  813. * Do not register smackfs if Smack wasn't enabled
  814. * on boot. We can not put this method normally under the
  815. * smack_init() code path since the security subsystem get
  816. * initialized before the vfs caches.
  817. *
  818. * Returns true if we were not chosen on boot or if
  819. * we were chosen and filesystem registration succeeded.
  820. */
  821. static int __init init_smk_fs(void)
  822. {
  823. int err;
  824. if (!security_module_enable(&smack_ops))
  825. return 0;
  826. err = register_filesystem(&smk_fs_type);
  827. if (!err) {
  828. smackfs_mount = kern_mount(&smk_fs_type);
  829. if (IS_ERR(smackfs_mount)) {
  830. printk(KERN_ERR "smackfs: could not mount!\n");
  831. err = PTR_ERR(smackfs_mount);
  832. smackfs_mount = NULL;
  833. }
  834. }
  835. smk_cipso_doi();
  836. smk_unlbl_ambient(NULL);
  837. return err;
  838. }
  839. __initcall(init_smk_fs);