smackfs.c 37 KB

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