smackfs.c 38 KB

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