smackfs.c 27 KB

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