smackfs.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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. * @file: 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_APPEND;
  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. * @oldambient: previous domain string
  349. */
  350. static void smk_unlbl_ambient(char *oldambient)
  351. {
  352. int rc;
  353. struct netlbl_audit nai;
  354. smk_netlabel_audit_set(&nai);
  355. if (oldambient != NULL) {
  356. rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
  357. if (rc != 0)
  358. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  359. __func__, __LINE__, rc);
  360. }
  361. rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
  362. NULL, NULL, &nai);
  363. if (rc != 0)
  364. printk(KERN_WARNING "%s:%d add rc = %d\n",
  365. __func__, __LINE__, rc);
  366. }
  367. /*
  368. * Seq_file read operations for /smack/cipso
  369. */
  370. static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
  371. {
  372. if (*pos == SEQ_READ_FINISHED)
  373. return NULL;
  374. return smack_known;
  375. }
  376. static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
  377. {
  378. struct smack_known *skp = ((struct smack_known *) v)->smk_next;
  379. /*
  380. * Omit labels with no associated cipso value
  381. */
  382. while (skp != NULL && !skp->smk_cipso)
  383. skp = skp->smk_next;
  384. if (skp == NULL)
  385. *pos = SEQ_READ_FINISHED;
  386. return skp;
  387. }
  388. /*
  389. * Print cipso labels in format:
  390. * label level[/cat[,cat]]
  391. */
  392. static int cipso_seq_show(struct seq_file *s, void *v)
  393. {
  394. struct smack_known *skp = (struct smack_known *) v;
  395. struct smack_cipso *scp = skp->smk_cipso;
  396. char *cbp;
  397. char sep = '/';
  398. int cat = 1;
  399. int i;
  400. unsigned char m;
  401. if (scp == NULL)
  402. return 0;
  403. seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level);
  404. cbp = scp->smk_catset;
  405. for (i = 0; i < SMK_LABELLEN; i++)
  406. for (m = 0x80; m != 0; m >>= 1) {
  407. if (m & cbp[i]) {
  408. seq_printf(s, "%c%d", sep, cat);
  409. sep = ',';
  410. }
  411. cat++;
  412. }
  413. seq_putc(s, '\n');
  414. return 0;
  415. }
  416. static void cipso_seq_stop(struct seq_file *s, void *v)
  417. {
  418. /* No-op */
  419. }
  420. static struct seq_operations cipso_seq_ops = {
  421. .start = cipso_seq_start,
  422. .stop = cipso_seq_stop,
  423. .next = cipso_seq_next,
  424. .show = cipso_seq_show,
  425. };
  426. /**
  427. * smk_open_cipso - open() for /smack/cipso
  428. * @inode: inode structure representing file
  429. * @file: "cipso" file pointer
  430. *
  431. * Connect our cipso_seq_* operations with /smack/cipso
  432. * file_operations
  433. */
  434. static int smk_open_cipso(struct inode *inode, struct file *file)
  435. {
  436. return seq_open(file, &cipso_seq_ops);
  437. }
  438. /**
  439. * smk_write_cipso - write() for /smack/cipso
  440. * @file: file pointer, not actually used
  441. * @buf: where to get the data from
  442. * @count: bytes sent
  443. * @ppos: where to start
  444. *
  445. * Accepts only one cipso rule per write call.
  446. * Returns number of bytes written or error code, as appropriate
  447. */
  448. static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
  449. size_t count, loff_t *ppos)
  450. {
  451. struct smack_known *skp;
  452. struct smack_cipso *scp = NULL;
  453. char mapcatset[SMK_LABELLEN];
  454. int maplevel;
  455. int cat;
  456. int catlen;
  457. ssize_t rc = -EINVAL;
  458. char *data = NULL;
  459. char *rule;
  460. int ret;
  461. int i;
  462. /*
  463. * Must have privilege.
  464. * No partial writes.
  465. * Enough data must be present.
  466. */
  467. if (!capable(CAP_MAC_ADMIN))
  468. return -EPERM;
  469. if (*ppos != 0)
  470. return -EINVAL;
  471. if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)
  472. return -EINVAL;
  473. data = kzalloc(count + 1, GFP_KERNEL);
  474. if (data == NULL)
  475. return -ENOMEM;
  476. if (copy_from_user(data, buf, count) != 0) {
  477. rc = -EFAULT;
  478. goto unlockedout;
  479. }
  480. data[count] = '\0';
  481. rule = data;
  482. /*
  483. * Only allow one writer at a time. Writes should be
  484. * quite rare and small in any case.
  485. */
  486. mutex_lock(&smack_cipso_lock);
  487. skp = smk_import_entry(rule, 0);
  488. if (skp == NULL)
  489. goto out;
  490. rule += SMK_LABELLEN;
  491. ret = sscanf(rule, "%d", &maplevel);
  492. if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
  493. goto out;
  494. rule += SMK_DIGITLEN;
  495. ret = sscanf(rule, "%d", &catlen);
  496. if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
  497. goto out;
  498. if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
  499. goto out;
  500. memset(mapcatset, 0, sizeof(mapcatset));
  501. for (i = 0; i < catlen; i++) {
  502. rule += SMK_DIGITLEN;
  503. ret = sscanf(rule, "%d", &cat);
  504. if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
  505. goto out;
  506. smack_catset_bit(cat, mapcatset);
  507. }
  508. if (skp->smk_cipso == NULL) {
  509. scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL);
  510. if (scp == NULL) {
  511. rc = -ENOMEM;
  512. goto out;
  513. }
  514. }
  515. spin_lock_bh(&skp->smk_cipsolock);
  516. if (scp == NULL)
  517. scp = skp->smk_cipso;
  518. else
  519. skp->smk_cipso = scp;
  520. scp->smk_level = maplevel;
  521. memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset));
  522. spin_unlock_bh(&skp->smk_cipsolock);
  523. rc = count;
  524. out:
  525. mutex_unlock(&smack_cipso_lock);
  526. unlockedout:
  527. kfree(data);
  528. return rc;
  529. }
  530. static const struct file_operations smk_cipso_ops = {
  531. .open = smk_open_cipso,
  532. .read = seq_read,
  533. .llseek = seq_lseek,
  534. .write = smk_write_cipso,
  535. .release = seq_release,
  536. };
  537. /*
  538. * Seq_file read operations for /smack/netlabel
  539. */
  540. static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
  541. {
  542. if (*pos == SEQ_READ_FINISHED)
  543. return NULL;
  544. return smack_netlbladdrs;
  545. }
  546. static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
  547. {
  548. struct smk_netlbladdr *skp = ((struct smk_netlbladdr *) v)->smk_next;
  549. if (skp == NULL)
  550. *pos = SEQ_READ_FINISHED;
  551. return skp;
  552. }
  553. #define BEBITS (sizeof(__be32) * 8)
  554. /*
  555. * Print host/label pairs
  556. */
  557. static int netlbladdr_seq_show(struct seq_file *s, void *v)
  558. {
  559. struct smk_netlbladdr *skp = (struct smk_netlbladdr *) v;
  560. unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
  561. int maskn;
  562. u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
  563. for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
  564. seq_printf(s, "%u.%u.%u.%u/%d %s\n",
  565. hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
  566. return 0;
  567. }
  568. static void netlbladdr_seq_stop(struct seq_file *s, void *v)
  569. {
  570. /* No-op */
  571. }
  572. static struct seq_operations netlbladdr_seq_ops = {
  573. .start = netlbladdr_seq_start,
  574. .stop = netlbladdr_seq_stop,
  575. .next = netlbladdr_seq_next,
  576. .show = netlbladdr_seq_show,
  577. };
  578. /**
  579. * smk_open_netlbladdr - open() for /smack/netlabel
  580. * @inode: inode structure representing file
  581. * @file: "netlabel" file pointer
  582. *
  583. * Connect our netlbladdr_seq_* operations with /smack/netlabel
  584. * file_operations
  585. */
  586. static int smk_open_netlbladdr(struct inode *inode, struct file *file)
  587. {
  588. return seq_open(file, &netlbladdr_seq_ops);
  589. }
  590. /**
  591. * smk_netlbladdr_insert
  592. * @new : netlabel to insert
  593. *
  594. * This helper insert netlabel in the smack_netlbladdrs list
  595. * sorted by netmask length (longest to smallest)
  596. */
  597. static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
  598. {
  599. struct smk_netlbladdr *m;
  600. if (smack_netlbladdrs == NULL) {
  601. smack_netlbladdrs = new;
  602. return;
  603. }
  604. /* the comparison '>' is a bit hacky, but works */
  605. if (new->smk_mask.s_addr > smack_netlbladdrs->smk_mask.s_addr) {
  606. new->smk_next = smack_netlbladdrs;
  607. smack_netlbladdrs = new;
  608. return;
  609. }
  610. for (m = smack_netlbladdrs; m != NULL; m = m->smk_next) {
  611. if (m->smk_next == NULL) {
  612. m->smk_next = new;
  613. return;
  614. }
  615. if (new->smk_mask.s_addr > m->smk_next->smk_mask.s_addr) {
  616. new->smk_next = m->smk_next;
  617. m->smk_next = new;
  618. return;
  619. }
  620. }
  621. }
  622. /**
  623. * smk_write_netlbladdr - write() for /smack/netlabel
  624. * @file: file pointer, not actually used
  625. * @buf: where to get the data from
  626. * @count: bytes sent
  627. * @ppos: where to start
  628. *
  629. * Accepts only one netlbladdr per write call.
  630. * Returns number of bytes written or error code, as appropriate
  631. */
  632. static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
  633. size_t count, loff_t *ppos)
  634. {
  635. struct smk_netlbladdr *skp;
  636. struct sockaddr_in newname;
  637. char smack[SMK_LABELLEN];
  638. char *sp;
  639. char data[SMK_NETLBLADDRMAX];
  640. char *host = (char *)&newname.sin_addr.s_addr;
  641. int rc;
  642. struct netlbl_audit audit_info;
  643. struct in_addr mask;
  644. unsigned int m;
  645. u32 mask_bits = (1<<31);
  646. __be32 nsa;
  647. u32 temp_mask;
  648. /*
  649. * Must have privilege.
  650. * No partial writes.
  651. * Enough data must be present.
  652. * "<addr/mask, as a.b.c.d/e><space><label>"
  653. * "<addr, as a.b.c.d><space><label>"
  654. */
  655. if (!capable(CAP_MAC_ADMIN))
  656. return -EPERM;
  657. if (*ppos != 0)
  658. return -EINVAL;
  659. if (count < SMK_NETLBLADDRMIN || count > SMK_NETLBLADDRMAX)
  660. return -EINVAL;
  661. if (copy_from_user(data, buf, count) != 0)
  662. return -EFAULT;
  663. data[count] = '\0';
  664. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
  665. &host[0], &host[1], &host[2], &host[3], &m, smack);
  666. if (rc != 6) {
  667. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
  668. &host[0], &host[1], &host[2], &host[3], smack);
  669. if (rc != 5)
  670. return -EINVAL;
  671. m = BEBITS;
  672. }
  673. if (m > BEBITS)
  674. return -EINVAL;
  675. sp = smk_import(smack, 0);
  676. if (sp == NULL)
  677. return -EINVAL;
  678. for (temp_mask = 0; m > 0; m--) {
  679. temp_mask |= mask_bits;
  680. mask_bits >>= 1;
  681. }
  682. mask.s_addr = cpu_to_be32(temp_mask);
  683. newname.sin_addr.s_addr &= mask.s_addr;
  684. /*
  685. * Only allow one writer at a time. Writes should be
  686. * quite rare and small in any case.
  687. */
  688. mutex_lock(&smk_netlbladdr_lock);
  689. nsa = newname.sin_addr.s_addr;
  690. /* try to find if the prefix is already in the list */
  691. for (skp = smack_netlbladdrs; skp != NULL; skp = skp->smk_next)
  692. if (skp->smk_host.sin_addr.s_addr == nsa &&
  693. skp->smk_mask.s_addr == mask.s_addr)
  694. break;
  695. smk_netlabel_audit_set(&audit_info);
  696. if (skp == NULL) {
  697. skp = kzalloc(sizeof(*skp), GFP_KERNEL);
  698. if (skp == NULL)
  699. rc = -ENOMEM;
  700. else {
  701. rc = 0;
  702. skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
  703. skp->smk_mask.s_addr = mask.s_addr;
  704. skp->smk_label = sp;
  705. smk_netlbladdr_insert(skp);
  706. }
  707. } else {
  708. rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
  709. &skp->smk_host.sin_addr, &skp->smk_mask,
  710. PF_INET, &audit_info);
  711. skp->smk_label = sp;
  712. }
  713. /*
  714. * Now tell netlabel about the single label nature of
  715. * this host so that incoming packets get labeled.
  716. */
  717. if (rc == 0)
  718. rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
  719. &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
  720. smack_to_secid(skp->smk_label), &audit_info);
  721. if (rc == 0)
  722. rc = count;
  723. mutex_unlock(&smk_netlbladdr_lock);
  724. return rc;
  725. }
  726. static const struct file_operations smk_netlbladdr_ops = {
  727. .open = smk_open_netlbladdr,
  728. .read = seq_read,
  729. .llseek = seq_lseek,
  730. .write = smk_write_netlbladdr,
  731. .release = seq_release,
  732. };
  733. /**
  734. * smk_read_doi - read() for /smack/doi
  735. * @filp: file pointer, not actually used
  736. * @buf: where to put the result
  737. * @count: maximum to send along
  738. * @ppos: where to start
  739. *
  740. * Returns number of bytes read or error code, as appropriate
  741. */
  742. static ssize_t smk_read_doi(struct file *filp, char __user *buf,
  743. size_t count, loff_t *ppos)
  744. {
  745. char temp[80];
  746. ssize_t rc;
  747. if (*ppos != 0)
  748. return 0;
  749. sprintf(temp, "%d", smk_cipso_doi_value);
  750. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  751. return rc;
  752. }
  753. /**
  754. * smk_write_doi - write() for /smack/doi
  755. * @file: file pointer, not actually used
  756. * @buf: where to get the data from
  757. * @count: bytes sent
  758. * @ppos: where to start
  759. *
  760. * Returns number of bytes written or error code, as appropriate
  761. */
  762. static ssize_t smk_write_doi(struct file *file, const char __user *buf,
  763. size_t count, loff_t *ppos)
  764. {
  765. char temp[80];
  766. int i;
  767. if (!capable(CAP_MAC_ADMIN))
  768. return -EPERM;
  769. if (count >= sizeof(temp) || count == 0)
  770. return -EINVAL;
  771. if (copy_from_user(temp, buf, count) != 0)
  772. return -EFAULT;
  773. temp[count] = '\0';
  774. if (sscanf(temp, "%d", &i) != 1)
  775. return -EINVAL;
  776. smk_cipso_doi_value = i;
  777. smk_cipso_doi();
  778. return count;
  779. }
  780. static const struct file_operations smk_doi_ops = {
  781. .read = smk_read_doi,
  782. .write = smk_write_doi,
  783. };
  784. /**
  785. * smk_read_direct - read() for /smack/direct
  786. * @filp: file pointer, not actually used
  787. * @buf: where to put the result
  788. * @count: maximum to send along
  789. * @ppos: where to start
  790. *
  791. * Returns number of bytes read or error code, as appropriate
  792. */
  793. static ssize_t smk_read_direct(struct file *filp, char __user *buf,
  794. size_t count, loff_t *ppos)
  795. {
  796. char temp[80];
  797. ssize_t rc;
  798. if (*ppos != 0)
  799. return 0;
  800. sprintf(temp, "%d", smack_cipso_direct);
  801. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  802. return rc;
  803. }
  804. /**
  805. * smk_write_direct - write() for /smack/direct
  806. * @file: file pointer, not actually used
  807. * @buf: where to get the data from
  808. * @count: bytes sent
  809. * @ppos: where to start
  810. *
  811. * Returns number of bytes written or error code, as appropriate
  812. */
  813. static ssize_t smk_write_direct(struct file *file, const char __user *buf,
  814. size_t count, loff_t *ppos)
  815. {
  816. char temp[80];
  817. int i;
  818. if (!capable(CAP_MAC_ADMIN))
  819. return -EPERM;
  820. if (count >= sizeof(temp) || count == 0)
  821. return -EINVAL;
  822. if (copy_from_user(temp, buf, count) != 0)
  823. return -EFAULT;
  824. temp[count] = '\0';
  825. if (sscanf(temp, "%d", &i) != 1)
  826. return -EINVAL;
  827. smack_cipso_direct = i;
  828. return count;
  829. }
  830. static const struct file_operations smk_direct_ops = {
  831. .read = smk_read_direct,
  832. .write = smk_write_direct,
  833. };
  834. /**
  835. * smk_read_ambient - read() for /smack/ambient
  836. * @filp: file pointer, not actually used
  837. * @buf: where to put the result
  838. * @cn: maximum to send along
  839. * @ppos: where to start
  840. *
  841. * Returns number of bytes read or error code, as appropriate
  842. */
  843. static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
  844. size_t cn, loff_t *ppos)
  845. {
  846. ssize_t rc;
  847. int asize;
  848. if (*ppos != 0)
  849. return 0;
  850. /*
  851. * Being careful to avoid a problem in the case where
  852. * smack_net_ambient gets changed in midstream.
  853. */
  854. mutex_lock(&smack_ambient_lock);
  855. asize = strlen(smack_net_ambient) + 1;
  856. if (cn >= asize)
  857. rc = simple_read_from_buffer(buf, cn, ppos,
  858. smack_net_ambient, asize);
  859. else
  860. rc = -EINVAL;
  861. mutex_unlock(&smack_ambient_lock);
  862. return rc;
  863. }
  864. /**
  865. * smk_write_ambient - write() for /smack/ambient
  866. * @file: file pointer, not actually used
  867. * @buf: where to get the data from
  868. * @count: bytes sent
  869. * @ppos: where to start
  870. *
  871. * Returns number of bytes written or error code, as appropriate
  872. */
  873. static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
  874. size_t count, loff_t *ppos)
  875. {
  876. char in[SMK_LABELLEN];
  877. char *oldambient;
  878. char *smack;
  879. if (!capable(CAP_MAC_ADMIN))
  880. return -EPERM;
  881. if (count >= SMK_LABELLEN)
  882. return -EINVAL;
  883. if (copy_from_user(in, buf, count) != 0)
  884. return -EFAULT;
  885. smack = smk_import(in, count);
  886. if (smack == NULL)
  887. return -EINVAL;
  888. mutex_lock(&smack_ambient_lock);
  889. oldambient = smack_net_ambient;
  890. smack_net_ambient = smack;
  891. smk_unlbl_ambient(oldambient);
  892. mutex_unlock(&smack_ambient_lock);
  893. return count;
  894. }
  895. static const struct file_operations smk_ambient_ops = {
  896. .read = smk_read_ambient,
  897. .write = smk_write_ambient,
  898. };
  899. /**
  900. * smk_read_onlycap - read() for /smack/onlycap
  901. * @filp: file pointer, not actually used
  902. * @buf: where to put the result
  903. * @cn: maximum to send along
  904. * @ppos: where to start
  905. *
  906. * Returns number of bytes read or error code, as appropriate
  907. */
  908. static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
  909. size_t cn, loff_t *ppos)
  910. {
  911. char *smack = "";
  912. ssize_t rc = -EINVAL;
  913. int asize;
  914. if (*ppos != 0)
  915. return 0;
  916. if (smack_onlycap != NULL)
  917. smack = smack_onlycap;
  918. asize = strlen(smack) + 1;
  919. if (cn >= asize)
  920. rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
  921. return rc;
  922. }
  923. /**
  924. * smk_write_onlycap - write() for /smack/onlycap
  925. * @file: file pointer, not actually used
  926. * @buf: where to get the data from
  927. * @count: bytes sent
  928. * @ppos: where to start
  929. *
  930. * Returns number of bytes written or error code, as appropriate
  931. */
  932. static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
  933. size_t count, loff_t *ppos)
  934. {
  935. char in[SMK_LABELLEN];
  936. char *sp = current->cred->security;
  937. if (!capable(CAP_MAC_ADMIN))
  938. return -EPERM;
  939. /*
  940. * This can be done using smk_access() but is done
  941. * explicitly for clarity. The smk_access() implementation
  942. * would use smk_access(smack_onlycap, MAY_WRITE)
  943. */
  944. if (smack_onlycap != NULL && smack_onlycap != sp)
  945. return -EPERM;
  946. if (count >= SMK_LABELLEN)
  947. return -EINVAL;
  948. if (copy_from_user(in, buf, count) != 0)
  949. return -EFAULT;
  950. /*
  951. * Should the null string be passed in unset the onlycap value.
  952. * This seems like something to be careful with as usually
  953. * smk_import only expects to return NULL for errors. It
  954. * is usually the case that a nullstring or "\n" would be
  955. * bad to pass to smk_import but in fact this is useful here.
  956. */
  957. smack_onlycap = smk_import(in, count);
  958. return count;
  959. }
  960. static const struct file_operations smk_onlycap_ops = {
  961. .read = smk_read_onlycap,
  962. .write = smk_write_onlycap,
  963. };
  964. /**
  965. * smk_fill_super - fill the /smackfs superblock
  966. * @sb: the empty superblock
  967. * @data: unused
  968. * @silent: unused
  969. *
  970. * Fill in the well known entries for /smack
  971. *
  972. * Returns 0 on success, an error code on failure
  973. */
  974. static int smk_fill_super(struct super_block *sb, void *data, int silent)
  975. {
  976. int rc;
  977. struct inode *root_inode;
  978. static struct tree_descr smack_files[] = {
  979. [SMK_LOAD] =
  980. {"load", &smk_load_ops, S_IRUGO|S_IWUSR},
  981. [SMK_CIPSO] =
  982. {"cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
  983. [SMK_DOI] =
  984. {"doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
  985. [SMK_DIRECT] =
  986. {"direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
  987. [SMK_AMBIENT] =
  988. {"ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
  989. [SMK_NETLBLADDR] =
  990. {"netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
  991. [SMK_ONLYCAP] =
  992. {"onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
  993. /* last one */ {""}
  994. };
  995. rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
  996. if (rc != 0) {
  997. printk(KERN_ERR "%s failed %d while creating inodes\n",
  998. __func__, rc);
  999. return rc;
  1000. }
  1001. root_inode = sb->s_root->d_inode;
  1002. root_inode->i_security = new_inode_smack(smack_known_floor.smk_known);
  1003. return 0;
  1004. }
  1005. /**
  1006. * smk_get_sb - get the smackfs superblock
  1007. * @fs_type: passed along without comment
  1008. * @flags: passed along without comment
  1009. * @dev_name: passed along without comment
  1010. * @data: passed along without comment
  1011. * @mnt: passed along without comment
  1012. *
  1013. * Just passes everything along.
  1014. *
  1015. * Returns what the lower level code does.
  1016. */
  1017. static int smk_get_sb(struct file_system_type *fs_type,
  1018. int flags, const char *dev_name, void *data,
  1019. struct vfsmount *mnt)
  1020. {
  1021. return get_sb_single(fs_type, flags, data, smk_fill_super, mnt);
  1022. }
  1023. static struct file_system_type smk_fs_type = {
  1024. .name = "smackfs",
  1025. .get_sb = smk_get_sb,
  1026. .kill_sb = kill_litter_super,
  1027. };
  1028. static struct vfsmount *smackfs_mount;
  1029. /**
  1030. * init_smk_fs - get the smackfs superblock
  1031. *
  1032. * register the smackfs
  1033. *
  1034. * Do not register smackfs if Smack wasn't enabled
  1035. * on boot. We can not put this method normally under the
  1036. * smack_init() code path since the security subsystem get
  1037. * initialized before the vfs caches.
  1038. *
  1039. * Returns true if we were not chosen on boot or if
  1040. * we were chosen and filesystem registration succeeded.
  1041. */
  1042. static int __init init_smk_fs(void)
  1043. {
  1044. int err;
  1045. if (!security_module_enable(&smack_ops))
  1046. return 0;
  1047. err = register_filesystem(&smk_fs_type);
  1048. if (!err) {
  1049. smackfs_mount = kern_mount(&smk_fs_type);
  1050. if (IS_ERR(smackfs_mount)) {
  1051. printk(KERN_ERR "smackfs: could not mount!\n");
  1052. err = PTR_ERR(smackfs_mount);
  1053. smackfs_mount = NULL;
  1054. }
  1055. }
  1056. smk_cipso_doi();
  1057. smk_unlbl_ambient(NULL);
  1058. return err;
  1059. }
  1060. __initcall(init_smk_fs);