smackfs.c 22 KB

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