smack_access.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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/slab.h>
  14. #include <linux/fs.h>
  15. #include <linux/sched.h>
  16. #include "smack.h"
  17. struct smack_known smack_known_huh = {
  18. .smk_known = "?",
  19. .smk_secid = 2,
  20. .smk_cipso = NULL,
  21. };
  22. struct smack_known smack_known_hat = {
  23. .smk_known = "^",
  24. .smk_secid = 3,
  25. .smk_cipso = NULL,
  26. };
  27. struct smack_known smack_known_star = {
  28. .smk_known = "*",
  29. .smk_secid = 4,
  30. .smk_cipso = NULL,
  31. };
  32. struct smack_known smack_known_floor = {
  33. .smk_known = "_",
  34. .smk_secid = 5,
  35. .smk_cipso = NULL,
  36. };
  37. struct smack_known smack_known_invalid = {
  38. .smk_known = "",
  39. .smk_secid = 6,
  40. .smk_cipso = NULL,
  41. };
  42. struct smack_known smack_known_web = {
  43. .smk_known = "@",
  44. .smk_secid = 7,
  45. .smk_cipso = NULL,
  46. };
  47. LIST_HEAD(smack_known_list);
  48. /*
  49. * The initial value needs to be bigger than any of the
  50. * known values above.
  51. */
  52. static u32 smack_next_secid = 10;
  53. /*
  54. * what events do we log
  55. * can be overwritten at run-time by /smack/logging
  56. */
  57. int log_policy = SMACK_AUDIT_DENIED;
  58. /**
  59. * smk_access_entry - look up matching access rule
  60. * @subject_label: a pointer to the subject's Smack label
  61. * @object_label: a pointer to the object's Smack label
  62. * @rule_list: the list of rules to search
  63. *
  64. * This function looks up the subject/object pair in the
  65. * access rule list and returns the access mode. If no
  66. * entry is found returns -ENOENT.
  67. *
  68. * NOTE:
  69. *
  70. * Earlier versions of this function allowed for labels that
  71. * were not on the label list. This was done to allow for
  72. * labels to come over the network that had never been seen
  73. * before on this host. Unless the receiving socket has the
  74. * star label this will always result in a failure check. The
  75. * star labeled socket case is now handled in the networking
  76. * hooks so there is no case where the label is not on the
  77. * label list. Checking to see if the address of two labels
  78. * is the same is now a reliable test.
  79. *
  80. * Do the object check first because that is more
  81. * likely to differ.
  82. */
  83. int smk_access_entry(char *subject_label, char *object_label,
  84. struct list_head *rule_list)
  85. {
  86. int may = -ENOENT;
  87. struct smack_rule *srp;
  88. list_for_each_entry_rcu(srp, rule_list, list) {
  89. if (srp->smk_object == object_label &&
  90. srp->smk_subject == subject_label) {
  91. may = srp->smk_access;
  92. break;
  93. }
  94. }
  95. return may;
  96. }
  97. /**
  98. * smk_access - determine if a subject has a specific access to an object
  99. * @subject_label: a pointer to the subject's Smack label
  100. * @object_label: a pointer to the object's Smack label
  101. * @request: the access requested, in "MAY" format
  102. * @a : a pointer to the audit data
  103. *
  104. * This function looks up the subject/object pair in the
  105. * access rule list and returns 0 if the access is permitted,
  106. * non zero otherwise.
  107. *
  108. * Smack labels are shared on smack_list
  109. */
  110. int smk_access(char *subject_label, char *object_label, int request,
  111. struct smk_audit_info *a)
  112. {
  113. struct smack_known *skp;
  114. int may = MAY_NOT;
  115. int rc = 0;
  116. /*
  117. * Hardcoded comparisons.
  118. *
  119. * A star subject can't access any object.
  120. */
  121. if (subject_label == smack_known_star.smk_known) {
  122. rc = -EACCES;
  123. goto out_audit;
  124. }
  125. /*
  126. * An internet object can be accessed by any subject.
  127. * Tasks cannot be assigned the internet label.
  128. * An internet subject can access any object.
  129. */
  130. if (object_label == smack_known_web.smk_known ||
  131. subject_label == smack_known_web.smk_known)
  132. goto out_audit;
  133. /*
  134. * A star object can be accessed by any subject.
  135. */
  136. if (object_label == smack_known_star.smk_known)
  137. goto out_audit;
  138. /*
  139. * An object can be accessed in any way by a subject
  140. * with the same label.
  141. */
  142. if (subject_label == object_label)
  143. goto out_audit;
  144. /*
  145. * A hat subject can read any object.
  146. * A floor object can be read by any subject.
  147. */
  148. if ((request & MAY_ANYREAD) == request) {
  149. if (object_label == smack_known_floor.smk_known)
  150. goto out_audit;
  151. if (subject_label == smack_known_hat.smk_known)
  152. goto out_audit;
  153. }
  154. /*
  155. * Beyond here an explicit relationship is required.
  156. * If the requested access is contained in the available
  157. * access (e.g. read is included in readwrite) it's
  158. * good. A negative response from smk_access_entry()
  159. * indicates there is no entry for this pair.
  160. */
  161. skp = smk_find_entry(subject_label);
  162. rcu_read_lock();
  163. may = smk_access_entry(subject_label, object_label, &skp->smk_rules);
  164. rcu_read_unlock();
  165. if (may > 0 && (request & may) == request)
  166. goto out_audit;
  167. rc = -EACCES;
  168. out_audit:
  169. #ifdef CONFIG_AUDIT
  170. if (a)
  171. smack_log(subject_label, object_label, request, rc, a);
  172. #endif
  173. return rc;
  174. }
  175. /**
  176. * smk_curacc - determine if current has a specific access to an object
  177. * @obj_label: a pointer to the object's Smack label
  178. * @mode: the access requested, in "MAY" format
  179. * @a : common audit data
  180. *
  181. * This function checks the current subject label/object label pair
  182. * in the access rule list and returns 0 if the access is permitted,
  183. * non zero otherwise. It allows that current may have the capability
  184. * to override the rules.
  185. */
  186. int smk_curacc(char *obj_label, u32 mode, struct smk_audit_info *a)
  187. {
  188. struct task_smack *tsp = current_security();
  189. char *sp = smk_of_task(tsp);
  190. int may;
  191. int rc;
  192. /*
  193. * Check the global rule list
  194. */
  195. rc = smk_access(sp, obj_label, mode, NULL);
  196. if (rc == 0) {
  197. /*
  198. * If there is an entry in the task's rule list
  199. * it can further restrict access.
  200. */
  201. may = smk_access_entry(sp, obj_label, &tsp->smk_rules);
  202. if (may < 0)
  203. goto out_audit;
  204. if ((mode & may) == mode)
  205. goto out_audit;
  206. rc = -EACCES;
  207. }
  208. /*
  209. * Return if a specific label has been designated as the
  210. * only one that gets privilege and current does not
  211. * have that label.
  212. */
  213. if (smack_onlycap != NULL && smack_onlycap != sp)
  214. goto out_audit;
  215. if (capable(CAP_MAC_OVERRIDE))
  216. rc = 0;
  217. out_audit:
  218. #ifdef CONFIG_AUDIT
  219. if (a)
  220. smack_log(sp, obj_label, mode, rc, a);
  221. #endif
  222. return rc;
  223. }
  224. #ifdef CONFIG_AUDIT
  225. /**
  226. * smack_str_from_perm : helper to transalate an int to a
  227. * readable string
  228. * @string : the string to fill
  229. * @access : the int
  230. *
  231. */
  232. static inline void smack_str_from_perm(char *string, int access)
  233. {
  234. int i = 0;
  235. if (access & MAY_READ)
  236. string[i++] = 'r';
  237. if (access & MAY_WRITE)
  238. string[i++] = 'w';
  239. if (access & MAY_EXEC)
  240. string[i++] = 'x';
  241. if (access & MAY_APPEND)
  242. string[i++] = 'a';
  243. string[i] = '\0';
  244. }
  245. /**
  246. * smack_log_callback - SMACK specific information
  247. * will be called by generic audit code
  248. * @ab : the audit_buffer
  249. * @a : audit_data
  250. *
  251. */
  252. static void smack_log_callback(struct audit_buffer *ab, void *a)
  253. {
  254. struct common_audit_data *ad = a;
  255. struct smack_audit_data *sad = ad->smack_audit_data;
  256. audit_log_format(ab, "lsm=SMACK fn=%s action=%s",
  257. ad->smack_audit_data->function,
  258. sad->result ? "denied" : "granted");
  259. audit_log_format(ab, " subject=");
  260. audit_log_untrustedstring(ab, sad->subject);
  261. audit_log_format(ab, " object=");
  262. audit_log_untrustedstring(ab, sad->object);
  263. audit_log_format(ab, " requested=%s", sad->request);
  264. }
  265. /**
  266. * smack_log - Audit the granting or denial of permissions.
  267. * @subject_label : smack label of the requester
  268. * @object_label : smack label of the object being accessed
  269. * @request: requested permissions
  270. * @result: result from smk_access
  271. * @a: auxiliary audit data
  272. *
  273. * Audit the granting or denial of permissions in accordance
  274. * with the policy.
  275. */
  276. void smack_log(char *subject_label, char *object_label, int request,
  277. int result, struct smk_audit_info *ad)
  278. {
  279. char request_buffer[SMK_NUM_ACCESS_TYPE + 1];
  280. struct smack_audit_data *sad;
  281. struct common_audit_data *a = &ad->a;
  282. /* check if we have to log the current event */
  283. if (result != 0 && (log_policy & SMACK_AUDIT_DENIED) == 0)
  284. return;
  285. if (result == 0 && (log_policy & SMACK_AUDIT_ACCEPT) == 0)
  286. return;
  287. sad = a->smack_audit_data;
  288. if (sad->function == NULL)
  289. sad->function = "unknown";
  290. /* end preparing the audit data */
  291. smack_str_from_perm(request_buffer, request);
  292. sad->subject = subject_label;
  293. sad->object = object_label;
  294. sad->request = request_buffer;
  295. sad->result = result;
  296. common_lsm_audit(a, smack_log_callback, NULL);
  297. }
  298. #else /* #ifdef CONFIG_AUDIT */
  299. void smack_log(char *subject_label, char *object_label, int request,
  300. int result, struct smk_audit_info *ad)
  301. {
  302. }
  303. #endif
  304. static DEFINE_MUTEX(smack_known_lock);
  305. /**
  306. * smk_find_entry - find a label on the list, return the list entry
  307. * @string: a text string that might be a Smack label
  308. *
  309. * Returns a pointer to the entry in the label list that
  310. * matches the passed string.
  311. */
  312. struct smack_known *smk_find_entry(const char *string)
  313. {
  314. struct smack_known *skp;
  315. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  316. if (strncmp(skp->smk_known, string, SMK_MAXLEN) == 0)
  317. return skp;
  318. }
  319. return NULL;
  320. }
  321. /**
  322. * smk_parse_smack - parse smack label from a text string
  323. * @string: a text string that might contain a Smack label
  324. * @len: the maximum size, or zero if it is NULL terminated.
  325. * @smack: parsed smack label, or NULL if parse error
  326. */
  327. void smk_parse_smack(const char *string, int len, char *smack)
  328. {
  329. int found;
  330. int i;
  331. if (len <= 0 || len > SMK_MAXLEN)
  332. len = SMK_MAXLEN;
  333. for (i = 0, found = 0; i < SMK_LABELLEN; i++) {
  334. if (found)
  335. smack[i] = '\0';
  336. else if (i >= len || string[i] > '~' || string[i] <= ' ' ||
  337. string[i] == '/' || string[i] == '"' ||
  338. string[i] == '\\' || string[i] == '\'') {
  339. smack[i] = '\0';
  340. found = 1;
  341. } else
  342. smack[i] = string[i];
  343. }
  344. }
  345. /**
  346. * smk_import_entry - import a label, return the list entry
  347. * @string: a text string that might be a Smack label
  348. * @len: the maximum size, or zero if it is NULL terminated.
  349. *
  350. * Returns a pointer to the entry in the label list that
  351. * matches the passed string, adding it if necessary.
  352. */
  353. struct smack_known *smk_import_entry(const char *string, int len)
  354. {
  355. struct smack_known *skp;
  356. char smack[SMK_LABELLEN];
  357. smk_parse_smack(string, len, smack);
  358. if (smack[0] == '\0')
  359. return NULL;
  360. mutex_lock(&smack_known_lock);
  361. skp = smk_find_entry(smack);
  362. if (skp == NULL) {
  363. skp = kzalloc(sizeof(struct smack_known), GFP_KERNEL);
  364. if (skp != NULL) {
  365. strncpy(skp->smk_known, smack, SMK_MAXLEN);
  366. skp->smk_secid = smack_next_secid++;
  367. skp->smk_cipso = NULL;
  368. INIT_LIST_HEAD(&skp->smk_rules);
  369. spin_lock_init(&skp->smk_cipsolock);
  370. mutex_init(&skp->smk_rules_lock);
  371. /*
  372. * Make sure that the entry is actually
  373. * filled before putting it on the list.
  374. */
  375. list_add_rcu(&skp->list, &smack_known_list);
  376. }
  377. }
  378. mutex_unlock(&smack_known_lock);
  379. return skp;
  380. }
  381. /**
  382. * smk_import - import a smack label
  383. * @string: a text string that might be a Smack label
  384. * @len: the maximum size, or zero if it is NULL terminated.
  385. *
  386. * Returns a pointer to the label in the label list that
  387. * matches the passed string, adding it if necessary.
  388. */
  389. char *smk_import(const char *string, int len)
  390. {
  391. struct smack_known *skp;
  392. /* labels cannot begin with a '-' */
  393. if (string[0] == '-')
  394. return NULL;
  395. skp = smk_import_entry(string, len);
  396. if (skp == NULL)
  397. return NULL;
  398. return skp->smk_known;
  399. }
  400. /**
  401. * smack_from_secid - find the Smack label associated with a secid
  402. * @secid: an integer that might be associated with a Smack label
  403. *
  404. * Returns a pointer to the appropriate Smack label if there is one,
  405. * otherwise a pointer to the invalid Smack label.
  406. */
  407. char *smack_from_secid(const u32 secid)
  408. {
  409. struct smack_known *skp;
  410. rcu_read_lock();
  411. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  412. if (skp->smk_secid == secid) {
  413. rcu_read_unlock();
  414. return skp->smk_known;
  415. }
  416. }
  417. /*
  418. * If we got this far someone asked for the translation
  419. * of a secid that is not on the list.
  420. */
  421. rcu_read_unlock();
  422. return smack_known_invalid.smk_known;
  423. }
  424. /**
  425. * smack_to_secid - find the secid associated with a Smack label
  426. * @smack: the Smack label
  427. *
  428. * Returns the appropriate secid if there is one,
  429. * otherwise 0
  430. */
  431. u32 smack_to_secid(const char *smack)
  432. {
  433. struct smack_known *skp;
  434. rcu_read_lock();
  435. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  436. if (strncmp(skp->smk_known, smack, SMK_MAXLEN) == 0) {
  437. rcu_read_unlock();
  438. return skp->smk_secid;
  439. }
  440. }
  441. rcu_read_unlock();
  442. return 0;
  443. }
  444. /**
  445. * smack_from_cipso - find the Smack label associated with a CIPSO option
  446. * @level: Bell & LaPadula level from the network
  447. * @cp: Bell & LaPadula categories from the network
  448. *
  449. * This is a simple lookup in the label table.
  450. *
  451. * Return the matching label from the label list or NULL.
  452. */
  453. char *smack_from_cipso(u32 level, char *cp)
  454. {
  455. struct smack_known *kp;
  456. char *final = NULL;
  457. rcu_read_lock();
  458. list_for_each_entry(kp, &smack_known_list, list) {
  459. if (kp->smk_cipso == NULL)
  460. continue;
  461. spin_lock_bh(&kp->smk_cipsolock);
  462. if (kp->smk_cipso->smk_level == level &&
  463. memcmp(kp->smk_cipso->smk_catset, cp, SMK_LABELLEN) == 0)
  464. final = kp->smk_known;
  465. spin_unlock_bh(&kp->smk_cipsolock);
  466. if (final != NULL)
  467. break;
  468. }
  469. rcu_read_unlock();
  470. return final;
  471. }
  472. /**
  473. * smack_to_cipso - find the CIPSO option to go with a Smack label
  474. * @smack: a pointer to the smack label in question
  475. * @cp: where to put the result
  476. *
  477. * Returns zero if a value is available, non-zero otherwise.
  478. */
  479. int smack_to_cipso(const char *smack, struct smack_cipso *cp)
  480. {
  481. struct smack_known *kp;
  482. int found = 0;
  483. rcu_read_lock();
  484. list_for_each_entry_rcu(kp, &smack_known_list, list) {
  485. if (kp->smk_known == smack ||
  486. strcmp(kp->smk_known, smack) == 0) {
  487. found = 1;
  488. break;
  489. }
  490. }
  491. rcu_read_unlock();
  492. if (found == 0 || kp->smk_cipso == NULL)
  493. return -ENOENT;
  494. memcpy(cp, kp->smk_cipso, sizeof(struct smack_cipso));
  495. return 0;
  496. }