smack_access.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. * Author:
  9. * Casey Schaufler <casey@schaufler-ca.com>
  10. *
  11. */
  12. #include <linux/types.h>
  13. #include <linux/fs.h>
  14. #include <linux/sched.h>
  15. #include "smack.h"
  16. struct smack_known smack_known_huh = {
  17. .smk_known = "?",
  18. .smk_secid = 2,
  19. .smk_cipso = NULL,
  20. };
  21. struct smack_known smack_known_hat = {
  22. .smk_known = "^",
  23. .smk_secid = 3,
  24. .smk_cipso = NULL,
  25. };
  26. struct smack_known smack_known_star = {
  27. .smk_known = "*",
  28. .smk_secid = 4,
  29. .smk_cipso = NULL,
  30. };
  31. struct smack_known smack_known_floor = {
  32. .smk_known = "_",
  33. .smk_secid = 5,
  34. .smk_cipso = NULL,
  35. };
  36. struct smack_known smack_known_invalid = {
  37. .smk_known = "",
  38. .smk_secid = 6,
  39. .smk_cipso = NULL,
  40. };
  41. struct smack_known smack_known_web = {
  42. .smk_known = "@",
  43. .smk_secid = 7,
  44. .smk_cipso = NULL,
  45. };
  46. LIST_HEAD(smack_known_list);
  47. /*
  48. * The initial value needs to be bigger than any of the
  49. * known values above.
  50. */
  51. static u32 smack_next_secid = 10;
  52. /**
  53. * smk_access - determine if a subject has a specific access to an object
  54. * @subject_label: a pointer to the subject's Smack label
  55. * @object_label: a pointer to the object's Smack label
  56. * @request: the access requested, in "MAY" format
  57. *
  58. * This function looks up the subject/object pair in the
  59. * access rule list and returns 0 if the access is permitted,
  60. * non zero otherwise.
  61. *
  62. * Even though Smack labels are usually shared on smack_list
  63. * labels that come in off the network can't be imported
  64. * and added to the list for locking reasons.
  65. *
  66. * Therefore, it is necessary to check the contents of the labels,
  67. * not just the pointer values. Of course, in most cases the labels
  68. * will be on the list, so checking the pointers may be a worthwhile
  69. * optimization.
  70. */
  71. int smk_access(char *subject_label, char *object_label, int request)
  72. {
  73. u32 may = MAY_NOT;
  74. struct smack_rule *srp;
  75. /*
  76. * Hardcoded comparisons.
  77. *
  78. * A star subject can't access any object.
  79. */
  80. if (subject_label == smack_known_star.smk_known ||
  81. strcmp(subject_label, smack_known_star.smk_known) == 0)
  82. return -EACCES;
  83. /*
  84. * An internet object can be accessed by any subject.
  85. * Tasks cannot be assigned the internet label.
  86. * An internet subject can access any object.
  87. */
  88. if (object_label == smack_known_web.smk_known ||
  89. subject_label == smack_known_web.smk_known ||
  90. strcmp(object_label, smack_known_web.smk_known) == 0 ||
  91. strcmp(subject_label, smack_known_web.smk_known) == 0)
  92. return 0;
  93. /*
  94. * A star object can be accessed by any subject.
  95. */
  96. if (object_label == smack_known_star.smk_known ||
  97. strcmp(object_label, smack_known_star.smk_known) == 0)
  98. return 0;
  99. /*
  100. * An object can be accessed in any way by a subject
  101. * with the same label.
  102. */
  103. if (subject_label == object_label ||
  104. strcmp(subject_label, object_label) == 0)
  105. return 0;
  106. /*
  107. * A hat subject can read any object.
  108. * A floor object can be read by any subject.
  109. */
  110. if ((request & MAY_ANYREAD) == request) {
  111. if (object_label == smack_known_floor.smk_known ||
  112. strcmp(object_label, smack_known_floor.smk_known) == 0)
  113. return 0;
  114. if (subject_label == smack_known_hat.smk_known ||
  115. strcmp(subject_label, smack_known_hat.smk_known) == 0)
  116. return 0;
  117. }
  118. /*
  119. * Beyond here an explicit relationship is required.
  120. * If the requested access is contained in the available
  121. * access (e.g. read is included in readwrite) it's
  122. * good.
  123. */
  124. rcu_read_lock();
  125. list_for_each_entry_rcu(srp, &smack_rule_list, list) {
  126. if (srp->smk_subject == subject_label ||
  127. strcmp(srp->smk_subject, subject_label) == 0) {
  128. if (srp->smk_object == object_label ||
  129. strcmp(srp->smk_object, object_label) == 0) {
  130. may = srp->smk_access;
  131. break;
  132. }
  133. }
  134. }
  135. rcu_read_unlock();
  136. /*
  137. * This is a bit map operation.
  138. */
  139. if ((request & may) == request)
  140. return 0;
  141. return -EACCES;
  142. }
  143. /**
  144. * smk_curacc - determine if current has a specific access to an object
  145. * @obj_label: a pointer to the object's Smack label
  146. * @mode: the access requested, in "MAY" format
  147. *
  148. * This function checks the current subject label/object label pair
  149. * in the access rule list and returns 0 if the access is permitted,
  150. * non zero otherwise. It allows that current may have the capability
  151. * to override the rules.
  152. */
  153. int smk_curacc(char *obj_label, u32 mode)
  154. {
  155. int rc;
  156. rc = smk_access(current_security(), obj_label, mode);
  157. if (rc == 0)
  158. return 0;
  159. /*
  160. * Return if a specific label has been designated as the
  161. * only one that gets privilege and current does not
  162. * have that label.
  163. */
  164. if (smack_onlycap != NULL && smack_onlycap != current->cred->security)
  165. return rc;
  166. if (capable(CAP_MAC_OVERRIDE))
  167. return 0;
  168. return rc;
  169. }
  170. static DEFINE_MUTEX(smack_known_lock);
  171. /**
  172. * smk_import_entry - import a label, return the list entry
  173. * @string: a text string that might be a Smack label
  174. * @len: the maximum size, or zero if it is NULL terminated.
  175. *
  176. * Returns a pointer to the entry in the label list that
  177. * matches the passed string, adding it if necessary.
  178. */
  179. struct smack_known *smk_import_entry(const char *string, int len)
  180. {
  181. struct smack_known *skp;
  182. char smack[SMK_LABELLEN];
  183. int found;
  184. int i;
  185. if (len <= 0 || len > SMK_MAXLEN)
  186. len = SMK_MAXLEN;
  187. for (i = 0, found = 0; i < SMK_LABELLEN; i++) {
  188. if (found)
  189. smack[i] = '\0';
  190. else if (i >= len || string[i] > '~' || string[i] <= ' ' ||
  191. string[i] == '/') {
  192. smack[i] = '\0';
  193. found = 1;
  194. } else
  195. smack[i] = string[i];
  196. }
  197. if (smack[0] == '\0')
  198. return NULL;
  199. mutex_lock(&smack_known_lock);
  200. found = 0;
  201. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  202. if (strncmp(skp->smk_known, smack, SMK_MAXLEN) == 0) {
  203. found = 1;
  204. break;
  205. }
  206. }
  207. if (found == 0) {
  208. skp = kzalloc(sizeof(struct smack_known), GFP_KERNEL);
  209. if (skp != NULL) {
  210. strncpy(skp->smk_known, smack, SMK_MAXLEN);
  211. skp->smk_secid = smack_next_secid++;
  212. skp->smk_cipso = NULL;
  213. spin_lock_init(&skp->smk_cipsolock);
  214. /*
  215. * Make sure that the entry is actually
  216. * filled before putting it on the list.
  217. */
  218. list_add_rcu(&skp->list, &smack_known_list);
  219. }
  220. }
  221. mutex_unlock(&smack_known_lock);
  222. return skp;
  223. }
  224. /**
  225. * smk_import - import a smack label
  226. * @string: a text string that might be a Smack label
  227. * @len: the maximum size, or zero if it is NULL terminated.
  228. *
  229. * Returns a pointer to the label in the label list that
  230. * matches the passed string, adding it if necessary.
  231. */
  232. char *smk_import(const char *string, int len)
  233. {
  234. struct smack_known *skp;
  235. /* labels cannot begin with a '-' */
  236. if (string[0] == '-')
  237. return NULL;
  238. skp = smk_import_entry(string, len);
  239. if (skp == NULL)
  240. return NULL;
  241. return skp->smk_known;
  242. }
  243. /**
  244. * smack_from_secid - find the Smack label associated with a secid
  245. * @secid: an integer that might be associated with a Smack label
  246. *
  247. * Returns a pointer to the appropraite Smack label if there is one,
  248. * otherwise a pointer to the invalid Smack label.
  249. */
  250. char *smack_from_secid(const u32 secid)
  251. {
  252. struct smack_known *skp;
  253. rcu_read_lock();
  254. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  255. if (skp->smk_secid == secid) {
  256. rcu_read_unlock();
  257. return skp->smk_known;
  258. }
  259. }
  260. /*
  261. * If we got this far someone asked for the translation
  262. * of a secid that is not on the list.
  263. */
  264. rcu_read_unlock();
  265. return smack_known_invalid.smk_known;
  266. }
  267. /**
  268. * smack_to_secid - find the secid associated with a Smack label
  269. * @smack: the Smack label
  270. *
  271. * Returns the appropriate secid if there is one,
  272. * otherwise 0
  273. */
  274. u32 smack_to_secid(const char *smack)
  275. {
  276. struct smack_known *skp;
  277. rcu_read_lock();
  278. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  279. if (strncmp(skp->smk_known, smack, SMK_MAXLEN) == 0) {
  280. rcu_read_unlock();
  281. return skp->smk_secid;
  282. }
  283. }
  284. rcu_read_unlock();
  285. return 0;
  286. }
  287. /**
  288. * smack_from_cipso - find the Smack label associated with a CIPSO option
  289. * @level: Bell & LaPadula level from the network
  290. * @cp: Bell & LaPadula categories from the network
  291. * @result: where to put the Smack value
  292. *
  293. * This is a simple lookup in the label table.
  294. *
  295. * This is an odd duck as far as smack handling goes in that
  296. * it sends back a copy of the smack label rather than a pointer
  297. * to the master list. This is done because it is possible for
  298. * a foreign host to send a smack label that is new to this
  299. * machine and hence not on the list. That would not be an
  300. * issue except that adding an entry to the master list can't
  301. * be done at that point.
  302. */
  303. void smack_from_cipso(u32 level, char *cp, char *result)
  304. {
  305. struct smack_known *kp;
  306. char *final = NULL;
  307. rcu_read_lock();
  308. list_for_each_entry(kp, &smack_known_list, list) {
  309. if (kp->smk_cipso == NULL)
  310. continue;
  311. spin_lock_bh(&kp->smk_cipsolock);
  312. if (kp->smk_cipso->smk_level == level &&
  313. memcmp(kp->smk_cipso->smk_catset, cp, SMK_LABELLEN) == 0)
  314. final = kp->smk_known;
  315. spin_unlock_bh(&kp->smk_cipsolock);
  316. }
  317. rcu_read_unlock();
  318. if (final == NULL)
  319. final = smack_known_huh.smk_known;
  320. strncpy(result, final, SMK_MAXLEN);
  321. return;
  322. }
  323. /**
  324. * smack_to_cipso - find the CIPSO option to go with a Smack label
  325. * @smack: a pointer to the smack label in question
  326. * @cp: where to put the result
  327. *
  328. * Returns zero if a value is available, non-zero otherwise.
  329. */
  330. int smack_to_cipso(const char *smack, struct smack_cipso *cp)
  331. {
  332. struct smack_known *kp;
  333. int found = 0;
  334. rcu_read_lock();
  335. list_for_each_entry_rcu(kp, &smack_known_list, list) {
  336. if (kp->smk_known == smack ||
  337. strcmp(kp->smk_known, smack) == 0) {
  338. found = 1;
  339. break;
  340. }
  341. }
  342. rcu_read_unlock();
  343. if (found == 0 || kp->smk_cipso == NULL)
  344. return -ENOENT;
  345. memcpy(cp, kp->smk_cipso, sizeof(struct smack_cipso));
  346. return 0;
  347. }