smack_access.c 9.0 KB

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