smackfs.c 24 KB

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