smack_access.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 may 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. /*
  156. * Return if a specific label has been designated as the
  157. * only one that gets privilege and current does not
  158. * have that label.
  159. */
  160. if (smack_onlycap != NULL && smack_onlycap != current->security)
  161. return rc;
  162. if (capable(CAP_MAC_OVERRIDE))
  163. return 0;
  164. return rc;
  165. }
  166. static DEFINE_MUTEX(smack_known_lock);
  167. /**
  168. * smk_import_entry - import a label, return the list entry
  169. * @string: a text string that might be a Smack label
  170. * @len: the maximum size, or zero if it is NULL terminated.
  171. *
  172. * Returns a pointer to the entry in the label list that
  173. * matches the passed string, adding it if necessary.
  174. */
  175. struct smack_known *smk_import_entry(const char *string, int len)
  176. {
  177. struct smack_known *skp;
  178. char smack[SMK_LABELLEN];
  179. int found;
  180. int i;
  181. if (len <= 0 || len > SMK_MAXLEN)
  182. len = SMK_MAXLEN;
  183. for (i = 0, found = 0; i < SMK_LABELLEN; i++) {
  184. if (found)
  185. smack[i] = '\0';
  186. else if (i >= len || string[i] > '~' || string[i] <= ' ' ||
  187. string[i] == '/') {
  188. smack[i] = '\0';
  189. found = 1;
  190. } else
  191. smack[i] = string[i];
  192. }
  193. if (smack[0] == '\0')
  194. return NULL;
  195. mutex_lock(&smack_known_lock);
  196. for (skp = smack_known; skp != NULL; skp = skp->smk_next)
  197. if (strncmp(skp->smk_known, smack, SMK_MAXLEN) == 0)
  198. break;
  199. if (skp == NULL) {
  200. skp = kzalloc(sizeof(struct smack_known), GFP_KERNEL);
  201. if (skp != NULL) {
  202. skp->smk_next = smack_known;
  203. strncpy(skp->smk_known, smack, SMK_MAXLEN);
  204. skp->smk_secid = smack_next_secid++;
  205. skp->smk_cipso = NULL;
  206. spin_lock_init(&skp->smk_cipsolock);
  207. /*
  208. * Make sure that the entry is actually
  209. * filled before putting it on the list.
  210. */
  211. smp_mb();
  212. smack_known = skp;
  213. }
  214. }
  215. mutex_unlock(&smack_known_lock);
  216. return skp;
  217. }
  218. /**
  219. * smk_import - import a smack label
  220. * @string: a text string that might be a Smack label
  221. * @len: the maximum size, or zero if it is NULL terminated.
  222. *
  223. * Returns a pointer to the label in the label list that
  224. * matches the passed string, adding it if necessary.
  225. */
  226. char *smk_import(const char *string, int len)
  227. {
  228. struct smack_known *skp;
  229. skp = smk_import_entry(string, len);
  230. if (skp == NULL)
  231. return NULL;
  232. return skp->smk_known;
  233. }
  234. /**
  235. * smack_from_secid - find the Smack label associated with a secid
  236. * @secid: an integer that might be associated with a Smack label
  237. *
  238. * Returns a pointer to the appropraite Smack label if there is one,
  239. * otherwise a pointer to the invalid Smack label.
  240. */
  241. char *smack_from_secid(const u32 secid)
  242. {
  243. struct smack_known *skp;
  244. for (skp = smack_known; skp != NULL; skp = skp->smk_next)
  245. if (skp->smk_secid == secid)
  246. return skp->smk_known;
  247. /*
  248. * If we got this far someone asked for the translation
  249. * of a secid that is not on the list.
  250. */
  251. return smack_known_invalid.smk_known;
  252. }
  253. /**
  254. * smack_to_secid - find the secid associated with a Smack label
  255. * @smack: the Smack label
  256. *
  257. * Returns the appropriate secid if there is one,
  258. * otherwise 0
  259. */
  260. u32 smack_to_secid(const char *smack)
  261. {
  262. struct smack_known *skp;
  263. for (skp = smack_known; skp != NULL; skp = skp->smk_next)
  264. if (strncmp(skp->smk_known, smack, SMK_MAXLEN) == 0)
  265. return skp->smk_secid;
  266. return 0;
  267. }
  268. /**
  269. * smack_from_cipso - find the Smack label associated with a CIPSO option
  270. * @level: Bell & LaPadula level from the network
  271. * @cp: Bell & LaPadula categories from the network
  272. * @result: where to put the Smack value
  273. *
  274. * This is a simple lookup in the label table.
  275. *
  276. * This is an odd duck as far as smack handling goes in that
  277. * it sends back a copy of the smack label rather than a pointer
  278. * to the master list. This is done because it is possible for
  279. * a foreign host to send a smack label that is new to this
  280. * machine and hence not on the list. That would not be an
  281. * issue except that adding an entry to the master list can't
  282. * be done at that point.
  283. */
  284. void smack_from_cipso(u32 level, char *cp, char *result)
  285. {
  286. struct smack_known *kp;
  287. char *final = NULL;
  288. for (kp = smack_known; final == NULL && kp != NULL; kp = kp->smk_next) {
  289. if (kp->smk_cipso == NULL)
  290. continue;
  291. spin_lock_bh(&kp->smk_cipsolock);
  292. if (kp->smk_cipso->smk_level == level &&
  293. memcmp(kp->smk_cipso->smk_catset, cp, SMK_LABELLEN) == 0)
  294. final = kp->smk_known;
  295. spin_unlock_bh(&kp->smk_cipsolock);
  296. }
  297. if (final == NULL)
  298. final = smack_known_huh.smk_known;
  299. strncpy(result, final, SMK_MAXLEN);
  300. return;
  301. }
  302. /**
  303. * smack_to_cipso - find the CIPSO option to go with a Smack label
  304. * @smack: a pointer to the smack label in question
  305. * @cp: where to put the result
  306. *
  307. * Returns zero if a value is available, non-zero otherwise.
  308. */
  309. int smack_to_cipso(const char *smack, struct smack_cipso *cp)
  310. {
  311. struct smack_known *kp;
  312. for (kp = smack_known; kp != NULL; kp = kp->smk_next)
  313. if (kp->smk_known == smack ||
  314. strcmp(kp->smk_known, smack) == 0)
  315. break;
  316. if (kp == NULL || kp->smk_cipso == NULL)
  317. return -ENOENT;
  318. memcpy(cp, kp->smk_cipso, sizeof(struct smack_cipso));
  319. return 0;
  320. }