smackfs.c 21 KB

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