smackfs.c 36 KB

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