smackfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. 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.secid = smack_to_secid(current->security);
  279. rc = netlbl_cfg_map_del(NULL, &audit_info);
  280. if (rc != 0)
  281. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  282. __func__, __LINE__, rc);
  283. doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
  284. if (doip == NULL)
  285. panic("smack: Failed to initialize cipso DOI.\n");
  286. doip->map.std = NULL;
  287. doip->doi = smk_cipso_doi_value;
  288. doip->type = CIPSO_V4_MAP_PASS;
  289. doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
  290. for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
  291. doip->tags[rc] = CIPSO_V4_TAG_INVALID;
  292. rc = netlbl_cfg_cipsov4_add_map(doip, NULL, &audit_info);
  293. if (rc != 0)
  294. printk(KERN_WARNING "%s:%d add rc = %d\n",
  295. __func__, __LINE__, rc);
  296. }
  297. /**
  298. * smk_unlbl_ambient - initialize the unlabeled domain
  299. */
  300. void smk_unlbl_ambient(char *oldambient)
  301. {
  302. int rc;
  303. struct netlbl_audit audit_info;
  304. audit_info.loginuid = audit_get_loginuid(current);
  305. audit_info.secid = smack_to_secid(current->security);
  306. if (oldambient != NULL) {
  307. rc = netlbl_cfg_map_del(oldambient, &audit_info);
  308. if (rc != 0)
  309. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  310. __func__, __LINE__, rc);
  311. }
  312. rc = netlbl_cfg_unlbl_add_map(smack_net_ambient, &audit_info);
  313. if (rc != 0)
  314. printk(KERN_WARNING "%s:%d add rc = %d\n",
  315. __func__, __LINE__, rc);
  316. }
  317. /*
  318. * Seq_file read operations for /smack/cipso
  319. */
  320. static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
  321. {
  322. if (*pos == SEQ_READ_FINISHED)
  323. return NULL;
  324. return smack_known;
  325. }
  326. static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
  327. {
  328. struct smack_known *skp = ((struct smack_known *) v)->smk_next;
  329. /*
  330. * Omit labels with no associated cipso value
  331. */
  332. while (skp != NULL && !skp->smk_cipso)
  333. skp = skp->smk_next;
  334. if (skp == NULL)
  335. *pos = SEQ_READ_FINISHED;
  336. return skp;
  337. }
  338. /*
  339. * Print cipso labels in format:
  340. * label level[/cat[,cat]]
  341. */
  342. static int cipso_seq_show(struct seq_file *s, void *v)
  343. {
  344. struct smack_known *skp = (struct smack_known *) v;
  345. struct smack_cipso *scp = skp->smk_cipso;
  346. char *cbp;
  347. char sep = '/';
  348. int cat = 1;
  349. int i;
  350. unsigned char m;
  351. if (scp == NULL)
  352. return 0;
  353. seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
  354. cbp = scp->smk_catset;
  355. for (i = 0; i < SMK_LABELLEN; i++)
  356. for (m = 0x80; m != 0; m >>= 1) {
  357. if (m & cbp[i]) {
  358. seq_printf(s, "%c%d", sep, cat);
  359. sep = ',';
  360. }
  361. cat++;
  362. }
  363. seq_putc(s, '\n');
  364. return 0;
  365. }
  366. static void cipso_seq_stop(struct seq_file *s, void *v)
  367. {
  368. /* No-op */
  369. }
  370. static struct seq_operations cipso_seq_ops = {
  371. .start = cipso_seq_start,
  372. .stop = cipso_seq_stop,
  373. .next = cipso_seq_next,
  374. .show = cipso_seq_show,
  375. };
  376. /**
  377. * smk_open_cipso - open() for /smack/cipso
  378. * @inode: inode structure representing file
  379. * @file: "cipso" file pointer
  380. *
  381. * Connect our cipso_seq_* operations with /smack/cipso
  382. * file_operations
  383. */
  384. static int smk_open_cipso(struct inode *inode, struct file *file)
  385. {
  386. return seq_open(file, &cipso_seq_ops);
  387. }
  388. /**
  389. * smk_write_cipso - write() for /smack/cipso
  390. * @filp: file pointer, not actually used
  391. * @buf: where to get the data from
  392. * @count: bytes sent
  393. * @ppos: where to start
  394. *
  395. * Accepts only one cipso rule per write call.
  396. * Returns number of bytes written or error code, as appropriate
  397. */
  398. static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
  399. size_t count, loff_t *ppos)
  400. {
  401. struct smack_known *skp;
  402. struct smack_cipso *scp = NULL;
  403. char mapcatset[SMK_LABELLEN];
  404. int maplevel;
  405. int cat;
  406. int catlen;
  407. ssize_t rc = -EINVAL;
  408. char *data = NULL;
  409. char *rule;
  410. int ret;
  411. int i;
  412. /*
  413. * Must have privilege.
  414. * No partial writes.
  415. * Enough data must be present.
  416. */
  417. if (!capable(CAP_MAC_ADMIN))
  418. return -EPERM;
  419. if (*ppos != 0)
  420. return -EINVAL;
  421. if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
  422. return -EINVAL;
  423. data = kzalloc(count + 1, GFP_KERNEL);
  424. if (data == NULL)
  425. return -ENOMEM;
  426. if (copy_from_user(data, buf, count) != 0) {
  427. rc = -EFAULT;
  428. goto unlockedout;
  429. }
  430. data[count] = '\0';
  431. rule = data;
  432. /*
  433. * Only allow one writer at a time. Writes should be
  434. * quite rare and small in any case.
  435. */
  436. mutex_lock(&smack_cipso_lock);
  437. skp = smk_import_entry(rule, 0);
  438. if (skp == NULL)
  439. goto out;
  440. rule += SMK_LABELLEN;;
  441. ret = sscanf(rule, "%d", &maplevel);
  442. if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
  443. goto out;
  444. rule += SMK_DIGITLEN;
  445. ret = sscanf(rule, "%d", &catlen);
  446. if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
  447. goto out;
  448. if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
  449. goto out;
  450. memset(mapcatset, 0, sizeof(mapcatset));
  451. for (i = 0; i < catlen; i++) {
  452. rule += SMK_DIGITLEN;
  453. ret = sscanf(rule, "%d", &cat);
  454. if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
  455. goto out;
  456. smack_catset_bit(cat, mapcatset);
  457. }
  458. if (skp->smk_cipso == NULL) {
  459. scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
  460. if (scp == NULL) {
  461. rc = -ENOMEM;
  462. goto out;
  463. }
  464. }
  465. spin_lock_bh(&skp->smk_cipsolock);
  466. if (scp == NULL)
  467. scp = skp->smk_cipso;
  468. else
  469. skp->smk_cipso = scp;
  470. scp->smk_level = maplevel;
  471. memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
  472. spin_unlock_bh(&skp->smk_cipsolock);
  473. rc = count;
  474. out:
  475. mutex_unlock(&smack_cipso_lock);
  476. unlockedout:
  477. kfree(data);
  478. return rc;
  479. }
  480. static const struct file_operations smk_cipso_ops = {
  481. .open = smk_open_cipso,
  482. .read = seq_read,
  483. .llseek = seq_lseek,
  484. .write = smk_write_cipso,
  485. .release = seq_release,
  486. };
  487. /**
  488. * smk_read_doi - read() for /smack/doi
  489. * @filp: file pointer, not actually used
  490. * @buf: where to put the result
  491. * @count: maximum to send along
  492. * @ppos: where to start
  493. *
  494. * Returns number of bytes read or error code, as appropriate
  495. */
  496. static ssize_t smk_read_doi(struct file *filp, char __user *buf,
  497. size_t count, loff_t *ppos)
  498. {
  499. char temp[80];
  500. ssize_t rc;
  501. if (*ppos != 0)
  502. return 0;
  503. sprintf(temp, "%d", smk_cipso_doi_value);
  504. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  505. return rc;
  506. }
  507. /**
  508. * smk_write_doi - write() for /smack/doi
  509. * @filp: file pointer, not actually used
  510. * @buf: where to get the data from
  511. * @count: bytes sent
  512. * @ppos: where to start
  513. *
  514. * Returns number of bytes written or error code, as appropriate
  515. */
  516. static ssize_t smk_write_doi(struct file *file, const char __user *buf,
  517. size_t count, loff_t *ppos)
  518. {
  519. char temp[80];
  520. int i;
  521. if (!capable(CAP_MAC_ADMIN))
  522. return -EPERM;
  523. if (count >= sizeof(temp) || count == 0)
  524. return -EINVAL;
  525. if (copy_from_user(temp, buf, count) != 0)
  526. return -EFAULT;
  527. temp[count] = '\0';
  528. if (sscanf(temp, "%d", &i) != 1)
  529. return -EINVAL;
  530. smk_cipso_doi_value = i;
  531. smk_cipso_doi();
  532. return count;
  533. }
  534. static const struct file_operations smk_doi_ops = {
  535. .read = smk_read_doi,
  536. .write = smk_write_doi,
  537. };
  538. /**
  539. * smk_read_direct - read() for /smack/direct
  540. * @filp: file pointer, not actually used
  541. * @buf: where to put the result
  542. * @count: maximum to send along
  543. * @ppos: where to start
  544. *
  545. * Returns number of bytes read or error code, as appropriate
  546. */
  547. static ssize_t smk_read_direct(struct file *filp, char __user *buf,
  548. size_t count, loff_t *ppos)
  549. {
  550. char temp[80];
  551. ssize_t rc;
  552. if (*ppos != 0)
  553. return 0;
  554. sprintf(temp, "%d", smack_cipso_direct);
  555. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  556. return rc;
  557. }
  558. /**
  559. * smk_write_direct - write() for /smack/direct
  560. * @filp: file pointer, not actually used
  561. * @buf: where to get the data from
  562. * @count: bytes sent
  563. * @ppos: where to start
  564. *
  565. * Returns number of bytes written or error code, as appropriate
  566. */
  567. static ssize_t smk_write_direct(struct file *file, const char __user *buf,
  568. size_t count, loff_t *ppos)
  569. {
  570. char temp[80];
  571. int i;
  572. if (!capable(CAP_MAC_ADMIN))
  573. return -EPERM;
  574. if (count >= sizeof(temp) || count == 0)
  575. return -EINVAL;
  576. if (copy_from_user(temp, buf, count) != 0)
  577. return -EFAULT;
  578. temp[count] = '\0';
  579. if (sscanf(temp, "%d", &i) != 1)
  580. return -EINVAL;
  581. smack_cipso_direct = i;
  582. return count;
  583. }
  584. static const struct file_operations smk_direct_ops = {
  585. .read = smk_read_direct,
  586. .write = smk_write_direct,
  587. };
  588. /**
  589. * smk_read_ambient - read() for /smack/ambient
  590. * @filp: file pointer, not actually used
  591. * @buf: where to put the result
  592. * @cn: maximum to send along
  593. * @ppos: where to start
  594. *
  595. * Returns number of bytes read or error code, as appropriate
  596. */
  597. static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
  598. size_t cn, loff_t *ppos)
  599. {
  600. ssize_t rc;
  601. int asize;
  602. if (*ppos != 0)
  603. return 0;
  604. /*
  605. * Being careful to avoid a problem in the case where
  606. * smack_net_ambient gets changed in midstream.
  607. */
  608. mutex_lock(&smack_ambient_lock);
  609. asize = strlen(smack_net_ambient) + 1;
  610. if (cn >= asize)
  611. rc = simple_read_from_buffer(buf, cn, ppos,
  612. smack_net_ambient, asize);
  613. else
  614. rc = -EINVAL;
  615. mutex_unlock(&smack_ambient_lock);
  616. return rc;
  617. }
  618. /**
  619. * smk_write_ambient - write() for /smack/ambient
  620. * @filp: file pointer, not actually used
  621. * @buf: where to get the data from
  622. * @count: bytes sent
  623. * @ppos: where to start
  624. *
  625. * Returns number of bytes written or error code, as appropriate
  626. */
  627. static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
  628. size_t count, loff_t *ppos)
  629. {
  630. char in[SMK_LABELLEN];
  631. char *oldambient;
  632. char *smack;
  633. if (!capable(CAP_MAC_ADMIN))
  634. return -EPERM;
  635. if (count >= SMK_LABELLEN)
  636. return -EINVAL;
  637. if (copy_from_user(in, buf, count) != 0)
  638. return -EFAULT;
  639. smack = smk_import(in, count);
  640. if (smack == NULL)
  641. return -EINVAL;
  642. mutex_lock(&smack_ambient_lock);
  643. oldambient = smack_net_ambient;
  644. smack_net_ambient = smack;
  645. smk_unlbl_ambient(oldambient);
  646. mutex_unlock(&smack_ambient_lock);
  647. return count;
  648. }
  649. static const struct file_operations smk_ambient_ops = {
  650. .read = smk_read_ambient,
  651. .write = smk_write_ambient,
  652. };
  653. struct option_names {
  654. int o_number;
  655. char *o_name;
  656. char *o_alias;
  657. };
  658. static struct option_names netlbl_choices[] = {
  659. { NETLBL_NLTYPE_RIPSO,
  660. NETLBL_NLTYPE_RIPSO_NAME, "ripso" },
  661. { NETLBL_NLTYPE_CIPSOV4,
  662. NETLBL_NLTYPE_CIPSOV4_NAME, "cipsov4" },
  663. { NETLBL_NLTYPE_CIPSOV4,
  664. NETLBL_NLTYPE_CIPSOV4_NAME, "cipso" },
  665. { NETLBL_NLTYPE_CIPSOV6,
  666. NETLBL_NLTYPE_CIPSOV6_NAME, "cipsov6" },
  667. { NETLBL_NLTYPE_UNLABELED,
  668. NETLBL_NLTYPE_UNLABELED_NAME, "unlabeled" },
  669. };
  670. /**
  671. * smk_read_nltype - read() for /smack/nltype
  672. * @filp: file pointer, not actually used
  673. * @buf: where to put the result
  674. * @count: maximum to send along
  675. * @ppos: where to start
  676. *
  677. * Returns number of bytes read or error code, as appropriate
  678. */
  679. static ssize_t smk_read_nltype(struct file *filp, char __user *buf,
  680. size_t count, loff_t *ppos)
  681. {
  682. char bound[40];
  683. ssize_t rc;
  684. int i;
  685. if (count < SMK_LABELLEN)
  686. return -EINVAL;
  687. if (*ppos != 0)
  688. return 0;
  689. sprintf(bound, "unknown");
  690. for (i = 0; i < ARRAY_SIZE(netlbl_choices); i++)
  691. if (smack_net_nltype == netlbl_choices[i].o_number) {
  692. sprintf(bound, "%s", netlbl_choices[i].o_name);
  693. break;
  694. }
  695. rc = simple_read_from_buffer(buf, count, ppos, bound, strlen(bound));
  696. return rc;
  697. }
  698. /**
  699. * smk_write_nltype - write() for /smack/nltype
  700. * @filp: file pointer, not actually used
  701. * @buf: where to get the data from
  702. * @count: bytes sent
  703. * @ppos: where to start
  704. *
  705. * Returns number of bytes written or error code, as appropriate
  706. */
  707. static ssize_t smk_write_nltype(struct file *file, const char __user *buf,
  708. size_t count, loff_t *ppos)
  709. {
  710. char bound[40];
  711. char *cp;
  712. int i;
  713. if (!capable(CAP_MAC_ADMIN))
  714. return -EPERM;
  715. if (count >= 40)
  716. return -EINVAL;
  717. if (copy_from_user(bound, buf, count) != 0)
  718. return -EFAULT;
  719. bound[count] = '\0';
  720. cp = strchr(bound, ' ');
  721. if (cp != NULL)
  722. *cp = '\0';
  723. cp = strchr(bound, '\n');
  724. if (cp != NULL)
  725. *cp = '\0';
  726. for (i = 0; i < ARRAY_SIZE(netlbl_choices); i++)
  727. if (strcmp(bound, netlbl_choices[i].o_name) == 0 ||
  728. strcmp(bound, netlbl_choices[i].o_alias) == 0) {
  729. smack_net_nltype = netlbl_choices[i].o_number;
  730. return count;
  731. }
  732. /*
  733. * Not a valid choice.
  734. */
  735. return -EINVAL;
  736. }
  737. static const struct file_operations smk_nltype_ops = {
  738. .read = smk_read_nltype,
  739. .write = smk_write_nltype,
  740. };
  741. /**
  742. * smk_fill_super - fill the /smackfs superblock
  743. * @sb: the empty superblock
  744. * @data: unused
  745. * @silent: unused
  746. *
  747. * Fill in the well known entries for /smack
  748. *
  749. * Returns 0 on success, an error code on failure
  750. */
  751. static int smk_fill_super(struct super_block *sb, void *data, int silent)
  752. {
  753. int rc;
  754. struct inode *root_inode;
  755. static struct tree_descr smack_files[] = {
  756. [SMK_LOAD] =
  757. {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
  758. [SMK_CIPSO] =
  759. {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
  760. [SMK_DOI] =
  761. {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
  762. [SMK_DIRECT] =
  763. {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
  764. [SMK_AMBIENT] =
  765. {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
  766. [SMK_NLTYPE] =
  767. {"nltype", &smk_nltype_ops, S_IRUGO|S_IWUSR},
  768. /* last one */ {""}
  769. };
  770. rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
  771. if (rc != 0) {
  772. printk(KERN_ERR "%s failed %d while creating inodes\n",
  773. __func__, rc);
  774. return rc;
  775. }
  776. root_inode = sb->s_root->d_inode;
  777. root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
  778. return 0;
  779. }
  780. /**
  781. * smk_get_sb - get the smackfs superblock
  782. * @fs_type: passed along without comment
  783. * @flags: passed along without comment
  784. * @dev_name: passed along without comment
  785. * @data: passed along without comment
  786. * @mnt: passed along without comment
  787. *
  788. * Just passes everything along.
  789. *
  790. * Returns what the lower level code does.
  791. */
  792. static int smk_get_sb(struct file_system_type *fs_type,
  793. int flags, const char *dev_name, void *data,
  794. struct vfsmount *mnt)
  795. {
  796. return get_sb_single(fs_type, flags, data, smk_fill_super, mnt);
  797. }
  798. static struct file_system_type smk_fs_type = {
  799. .name = "smackfs",
  800. .get_sb = smk_get_sb,
  801. .kill_sb = kill_litter_super,
  802. };
  803. static struct vfsmount *smackfs_mount;
  804. /**
  805. * init_smk_fs - get the smackfs superblock
  806. *
  807. * register the smackfs
  808. *
  809. * Do not register smackfs if Smack wasn't enabled
  810. * on boot. We can not put this method normally under the
  811. * smack_init() code path since the security subsystem get
  812. * initialized before the vfs caches.
  813. *
  814. * Returns true if we were not chosen on boot or if
  815. * we were chosen and filesystem registration succeeded.
  816. */
  817. static int __init init_smk_fs(void)
  818. {
  819. int err;
  820. if (!security_module_enable(&smack_ops))
  821. return 0;
  822. err = register_filesystem(&smk_fs_type);
  823. if (!err) {
  824. smackfs_mount = kern_mount(&smk_fs_type);
  825. if (IS_ERR(smackfs_mount)) {
  826. printk(KERN_ERR "smackfs: could not mount!\n");
  827. err = PTR_ERR(smackfs_mount);
  828. smackfs_mount = NULL;
  829. }
  830. }
  831. smk_cipso_doi();
  832. smk_unlbl_ambient(NULL);
  833. return err;
  834. }
  835. __initcall(init_smk_fs);