smackfs.c 22 KB

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