smack_access.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. * what events do we log
  54. * can be overwritten at run-time by /smack/logging
  55. */
  56. int log_policy = SMACK_AUDIT_DENIED;
  57. /**
  58. * smk_access - determine if a subject has a specific access to an object
  59. * @subject_label: a pointer to the subject's Smack label
  60. * @object_label: a pointer to the object's Smack label
  61. * @request: the access requested, in "MAY" format
  62. * @a : a pointer to the audit data
  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. struct smk_audit_info *a)
  79. {
  80. u32 may = MAY_NOT;
  81. struct smack_rule *srp;
  82. int rc = 0;
  83. /*
  84. * Hardcoded comparisons.
  85. *
  86. * A star subject can't access any object.
  87. */
  88. if (subject_label == smack_known_star.smk_known ||
  89. strcmp(subject_label, smack_known_star.smk_known) == 0) {
  90. rc = -EACCES;
  91. goto out_audit;
  92. }
  93. /*
  94. * An internet object can be accessed by any subject.
  95. * Tasks cannot be assigned the internet label.
  96. * An internet subject can access any object.
  97. */
  98. if (object_label == smack_known_web.smk_known ||
  99. subject_label == smack_known_web.smk_known ||
  100. strcmp(object_label, smack_known_web.smk_known) == 0 ||
  101. strcmp(subject_label, smack_known_web.smk_known) == 0)
  102. goto out_audit;
  103. /*
  104. * A star object can be accessed by any subject.
  105. */
  106. if (object_label == smack_known_star.smk_known ||
  107. strcmp(object_label, smack_known_star.smk_known) == 0)
  108. goto out_audit;
  109. /*
  110. * An object can be accessed in any way by a subject
  111. * with the same label.
  112. */
  113. if (subject_label == object_label ||
  114. strcmp(subject_label, object_label) == 0)
  115. goto out_audit;
  116. /*
  117. * A hat subject can read any object.
  118. * A floor object can be read by any subject.
  119. */
  120. if ((request & MAY_ANYREAD) == request) {
  121. if (object_label == smack_known_floor.smk_known ||
  122. strcmp(object_label, smack_known_floor.smk_known) == 0)
  123. goto out_audit;
  124. if (subject_label == smack_known_hat.smk_known ||
  125. strcmp(subject_label, smack_known_hat.smk_known) == 0)
  126. goto out_audit;
  127. }
  128. /*
  129. * Beyond here an explicit relationship is required.
  130. * If the requested access is contained in the available
  131. * access (e.g. read is included in readwrite) it's
  132. * good.
  133. */
  134. rcu_read_lock();
  135. list_for_each_entry_rcu(srp, &smack_rule_list, list) {
  136. if (srp->smk_subject == subject_label ||
  137. strcmp(srp->smk_subject, subject_label) == 0) {
  138. if (srp->smk_object == object_label ||
  139. strcmp(srp->smk_object, object_label) == 0) {
  140. may = srp->smk_access;
  141. break;
  142. }
  143. }
  144. }
  145. rcu_read_unlock();
  146. /*
  147. * This is a bit map operation.
  148. */
  149. if ((request & may) == request)
  150. goto out_audit;
  151. rc = -EACCES;
  152. out_audit:
  153. #ifdef CONFIG_AUDIT
  154. if (a)
  155. smack_log(subject_label, object_label, request, rc, a);
  156. #endif
  157. return rc;
  158. }
  159. /**
  160. * smk_curacc - determine if current has a specific access to an object
  161. * @obj_label: a pointer to the object's Smack label
  162. * @mode: the access requested, in "MAY" format
  163. * @a : common audit data
  164. *
  165. * This function checks the current subject label/object label pair
  166. * in the access rule list and returns 0 if the access is permitted,
  167. * non zero otherwise. It allows that current may have the capability
  168. * to override the rules.
  169. */
  170. int smk_curacc(char *obj_label, u32 mode, struct smk_audit_info *a)
  171. {
  172. int rc;
  173. char *sp = current_security();
  174. rc = smk_access(sp, obj_label, mode, NULL);
  175. if (rc == 0)
  176. goto out_audit;
  177. /*
  178. * Return if a specific label has been designated as the
  179. * only one that gets privilege and current does not
  180. * have that label.
  181. */
  182. if (smack_onlycap != NULL && smack_onlycap != current->cred->security)
  183. goto out_audit;
  184. if (capable(CAP_MAC_OVERRIDE))
  185. return 0;
  186. out_audit:
  187. #ifdef CONFIG_AUDIT
  188. if (a)
  189. smack_log(sp, obj_label, mode, rc, a);
  190. #endif
  191. return rc;
  192. }
  193. #ifdef CONFIG_AUDIT
  194. /**
  195. * smack_str_from_perm : helper to transalate an int to a
  196. * readable string
  197. * @string : the string to fill
  198. * @access : the int
  199. *
  200. */
  201. static inline void smack_str_from_perm(char *string, int access)
  202. {
  203. int i = 0;
  204. if (access & MAY_READ)
  205. string[i++] = 'r';
  206. if (access & MAY_WRITE)
  207. string[i++] = 'w';
  208. if (access & MAY_EXEC)
  209. string[i++] = 'x';
  210. if (access & MAY_APPEND)
  211. string[i++] = 'a';
  212. string[i] = '\0';
  213. }
  214. /**
  215. * smack_log_callback - SMACK specific information
  216. * will be called by generic audit code
  217. * @ab : the audit_buffer
  218. * @a : audit_data
  219. *
  220. */
  221. static void smack_log_callback(struct audit_buffer *ab, void *a)
  222. {
  223. struct common_audit_data *ad = a;
  224. struct smack_audit_data *sad = &ad->lsm_priv.smack_audit_data;
  225. audit_log_format(ab, "lsm=SMACK fn=%s action=%s", ad->function,
  226. sad->result ? "denied" : "granted");
  227. audit_log_format(ab, " subject=");
  228. audit_log_untrustedstring(ab, sad->subject);
  229. audit_log_format(ab, " object=");
  230. audit_log_untrustedstring(ab, sad->object);
  231. audit_log_format(ab, " requested=%s", sad->request);
  232. }
  233. /**
  234. * smack_log - Audit the granting or denial of permissions.
  235. * @subject_label : smack label of the requester
  236. * @object_label : smack label of the object being accessed
  237. * @request: requested permissions
  238. * @result: result from smk_access
  239. * @a: auxiliary audit data
  240. *
  241. * Audit the granting or denial of permissions in accordance
  242. * with the policy.
  243. */
  244. void smack_log(char *subject_label, char *object_label, int request,
  245. int result, struct smk_audit_info *ad)
  246. {
  247. char request_buffer[SMK_NUM_ACCESS_TYPE + 1];
  248. struct smack_audit_data *sad;
  249. struct common_audit_data *a = &ad->a;
  250. /* check if we have to log the current event */
  251. if (result != 0 && (log_policy & SMACK_AUDIT_DENIED) == 0)
  252. return;
  253. if (result == 0 && (log_policy & SMACK_AUDIT_ACCEPT) == 0)
  254. return;
  255. if (a->function == NULL)
  256. a->function = "unknown";
  257. /* end preparing the audit data */
  258. sad = &a->lsm_priv.smack_audit_data;
  259. smack_str_from_perm(request_buffer, request);
  260. sad->subject = subject_label;
  261. sad->object = object_label;
  262. sad->request = request_buffer;
  263. sad->result = result;
  264. a->lsm_pre_audit = smack_log_callback;
  265. common_lsm_audit(a);
  266. }
  267. #else /* #ifdef CONFIG_AUDIT */
  268. void smack_log(char *subject_label, char *object_label, int request,
  269. int result, struct smk_audit_info *ad)
  270. {
  271. }
  272. #endif
  273. static DEFINE_MUTEX(smack_known_lock);
  274. /**
  275. * smk_import_entry - import a label, return the list entry
  276. * @string: a text string that might be a Smack label
  277. * @len: the maximum size, or zero if it is NULL terminated.
  278. *
  279. * Returns a pointer to the entry in the label list that
  280. * matches the passed string, adding it if necessary.
  281. */
  282. struct smack_known *smk_import_entry(const char *string, int len)
  283. {
  284. struct smack_known *skp;
  285. char smack[SMK_LABELLEN];
  286. int found;
  287. int i;
  288. if (len <= 0 || len > SMK_MAXLEN)
  289. len = SMK_MAXLEN;
  290. for (i = 0, found = 0; i < SMK_LABELLEN; i++) {
  291. if (found)
  292. smack[i] = '\0';
  293. else if (i >= len || string[i] > '~' || string[i] <= ' ' ||
  294. string[i] == '/' || string[i] == '"' ||
  295. string[i] == '\\' || string[i] == '\'') {
  296. smack[i] = '\0';
  297. found = 1;
  298. } else
  299. smack[i] = string[i];
  300. }
  301. if (smack[0] == '\0')
  302. return NULL;
  303. mutex_lock(&smack_known_lock);
  304. found = 0;
  305. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  306. if (strncmp(skp->smk_known, smack, SMK_MAXLEN) == 0) {
  307. found = 1;
  308. break;
  309. }
  310. }
  311. if (found == 0) {
  312. skp = kzalloc(sizeof(struct smack_known), GFP_KERNEL);
  313. if (skp != NULL) {
  314. strncpy(skp->smk_known, smack, SMK_MAXLEN);
  315. skp->smk_secid = smack_next_secid++;
  316. skp->smk_cipso = NULL;
  317. spin_lock_init(&skp->smk_cipsolock);
  318. /*
  319. * Make sure that the entry is actually
  320. * filled before putting it on the list.
  321. */
  322. list_add_rcu(&skp->list, &smack_known_list);
  323. }
  324. }
  325. mutex_unlock(&smack_known_lock);
  326. return skp;
  327. }
  328. /**
  329. * smk_import - import a smack label
  330. * @string: a text string that might be a Smack label
  331. * @len: the maximum size, or zero if it is NULL terminated.
  332. *
  333. * Returns a pointer to the label in the label list that
  334. * matches the passed string, adding it if necessary.
  335. */
  336. char *smk_import(const char *string, int len)
  337. {
  338. struct smack_known *skp;
  339. /* labels cannot begin with a '-' */
  340. if (string[0] == '-')
  341. return NULL;
  342. skp = smk_import_entry(string, len);
  343. if (skp == NULL)
  344. return NULL;
  345. return skp->smk_known;
  346. }
  347. /**
  348. * smack_from_secid - find the Smack label associated with a secid
  349. * @secid: an integer that might be associated with a Smack label
  350. *
  351. * Returns a pointer to the appropraite Smack label if there is one,
  352. * otherwise a pointer to the invalid Smack label.
  353. */
  354. char *smack_from_secid(const u32 secid)
  355. {
  356. struct smack_known *skp;
  357. rcu_read_lock();
  358. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  359. if (skp->smk_secid == secid) {
  360. rcu_read_unlock();
  361. return skp->smk_known;
  362. }
  363. }
  364. /*
  365. * If we got this far someone asked for the translation
  366. * of a secid that is not on the list.
  367. */
  368. rcu_read_unlock();
  369. return smack_known_invalid.smk_known;
  370. }
  371. /**
  372. * smack_to_secid - find the secid associated with a Smack label
  373. * @smack: the Smack label
  374. *
  375. * Returns the appropriate secid if there is one,
  376. * otherwise 0
  377. */
  378. u32 smack_to_secid(const char *smack)
  379. {
  380. struct smack_known *skp;
  381. rcu_read_lock();
  382. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  383. if (strncmp(skp->smk_known, smack, SMK_MAXLEN) == 0) {
  384. rcu_read_unlock();
  385. return skp->smk_secid;
  386. }
  387. }
  388. rcu_read_unlock();
  389. return 0;
  390. }
  391. /**
  392. * smack_from_cipso - find the Smack label associated with a CIPSO option
  393. * @level: Bell & LaPadula level from the network
  394. * @cp: Bell & LaPadula categories from the network
  395. * @result: where to put the Smack value
  396. *
  397. * This is a simple lookup in the label table.
  398. *
  399. * This is an odd duck as far as smack handling goes in that
  400. * it sends back a copy of the smack label rather than a pointer
  401. * to the master list. This is done because it is possible for
  402. * a foreign host to send a smack label that is new to this
  403. * machine and hence not on the list. That would not be an
  404. * issue except that adding an entry to the master list can't
  405. * be done at that point.
  406. */
  407. void smack_from_cipso(u32 level, char *cp, char *result)
  408. {
  409. struct smack_known *kp;
  410. char *final = NULL;
  411. rcu_read_lock();
  412. list_for_each_entry(kp, &smack_known_list, list) {
  413. if (kp->smk_cipso == NULL)
  414. continue;
  415. spin_lock_bh(&kp->smk_cipsolock);
  416. if (kp->smk_cipso->smk_level == level &&
  417. memcmp(kp->smk_cipso->smk_catset, cp, SMK_LABELLEN) == 0)
  418. final = kp->smk_known;
  419. spin_unlock_bh(&kp->smk_cipsolock);
  420. }
  421. rcu_read_unlock();
  422. if (final == NULL)
  423. final = smack_known_huh.smk_known;
  424. strncpy(result, final, SMK_MAXLEN);
  425. return;
  426. }
  427. /**
  428. * smack_to_cipso - find the CIPSO option to go with a Smack label
  429. * @smack: a pointer to the smack label in question
  430. * @cp: where to put the result
  431. *
  432. * Returns zero if a value is available, non-zero otherwise.
  433. */
  434. int smack_to_cipso(const char *smack, struct smack_cipso *cp)
  435. {
  436. struct smack_known *kp;
  437. int found = 0;
  438. rcu_read_lock();
  439. list_for_each_entry_rcu(kp, &smack_known_list, list) {
  440. if (kp->smk_known == smack ||
  441. strcmp(kp->smk_known, smack) == 0) {
  442. found = 1;
  443. break;
  444. }
  445. }
  446. rcu_read_unlock();
  447. if (found == 0 || kp->smk_cipso == NULL)
  448. return -ENOENT;
  449. memcpy(cp, kp->smk_cipso, sizeof(struct smack_cipso));
  450. return 0;
  451. }