smackfs.c 24 KB

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