smackfs.c 35 KB

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