smackfs.c 24 KB

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