smackfs.c 30 KB

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