smackfs.c 22 KB

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