smack_access.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. if (a->smack_audit_data.function == NULL)
  288. a->smack_audit_data.function = "unknown";
  289. /* end preparing the audit data */
  290. sad = &a->smack_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. a->lsm_pre_audit = smack_log_callback;
  297. common_lsm_audit(a);
  298. }
  299. #else /* #ifdef CONFIG_AUDIT */
  300. void smack_log(char *subject_label, char *object_label, int request,
  301. int result, struct smk_audit_info *ad)
  302. {
  303. }
  304. #endif
  305. static DEFINE_MUTEX(smack_known_lock);
  306. /**
  307. * smk_find_entry - find a label on the list, return the list entry
  308. * @string: a text string that might be a Smack label
  309. *
  310. * Returns a pointer to the entry in the label list that
  311. * matches the passed string.
  312. */
  313. struct smack_known *smk_find_entry(const char *string)
  314. {
  315. struct smack_known *skp;
  316. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  317. if (strncmp(skp->smk_known, string, SMK_MAXLEN) == 0)
  318. return skp;
  319. }
  320. return NULL;
  321. }
  322. /**
  323. * smk_parse_smack - parse smack label from a text string
  324. * @string: a text string that might contain a Smack label
  325. * @len: the maximum size, or zero if it is NULL terminated.
  326. * @smack: parsed smack label, or NULL if parse error
  327. */
  328. void smk_parse_smack(const char *string, int len, char *smack)
  329. {
  330. int found;
  331. int i;
  332. if (len <= 0 || len > SMK_MAXLEN)
  333. len = SMK_MAXLEN;
  334. for (i = 0, found = 0; i < SMK_LABELLEN; i++) {
  335. if (found)
  336. smack[i] = '\0';
  337. else if (i >= len || string[i] > '~' || string[i] <= ' ' ||
  338. string[i] == '/' || string[i] == '"' ||
  339. string[i] == '\\' || string[i] == '\'') {
  340. smack[i] = '\0';
  341. found = 1;
  342. } else
  343. smack[i] = string[i];
  344. }
  345. }
  346. /**
  347. * smk_import_entry - import a label, return the list entry
  348. * @string: a text string that might be a Smack label
  349. * @len: the maximum size, or zero if it is NULL terminated.
  350. *
  351. * Returns a pointer to the entry in the label list that
  352. * matches the passed string, adding it if necessary.
  353. */
  354. struct smack_known *smk_import_entry(const char *string, int len)
  355. {
  356. struct smack_known *skp;
  357. char smack[SMK_LABELLEN];
  358. smk_parse_smack(string, len, smack);
  359. if (smack[0] == '\0')
  360. return NULL;
  361. mutex_lock(&smack_known_lock);
  362. skp = smk_find_entry(smack);
  363. if (skp == NULL) {
  364. skp = kzalloc(sizeof(struct smack_known), GFP_KERNEL);
  365. if (skp != NULL) {
  366. strncpy(skp->smk_known, smack, SMK_MAXLEN);
  367. skp->smk_secid = smack_next_secid++;
  368. skp->smk_cipso = NULL;
  369. INIT_LIST_HEAD(&skp->smk_rules);
  370. spin_lock_init(&skp->smk_cipsolock);
  371. mutex_init(&skp->smk_rules_lock);
  372. /*
  373. * Make sure that the entry is actually
  374. * filled before putting it on the list.
  375. */
  376. list_add_rcu(&skp->list, &smack_known_list);
  377. }
  378. }
  379. mutex_unlock(&smack_known_lock);
  380. return skp;
  381. }
  382. /**
  383. * smk_import - import a smack label
  384. * @string: a text string that might be a Smack label
  385. * @len: the maximum size, or zero if it is NULL terminated.
  386. *
  387. * Returns a pointer to the label in the label list that
  388. * matches the passed string, adding it if necessary.
  389. */
  390. char *smk_import(const char *string, int len)
  391. {
  392. struct smack_known *skp;
  393. /* labels cannot begin with a '-' */
  394. if (string[0] == '-')
  395. return NULL;
  396. skp = smk_import_entry(string, len);
  397. if (skp == NULL)
  398. return NULL;
  399. return skp->smk_known;
  400. }
  401. /**
  402. * smack_from_secid - find the Smack label associated with a secid
  403. * @secid: an integer that might be associated with a Smack label
  404. *
  405. * Returns a pointer to the appropriate Smack label if there is one,
  406. * otherwise a pointer to the invalid Smack label.
  407. */
  408. char *smack_from_secid(const u32 secid)
  409. {
  410. struct smack_known *skp;
  411. rcu_read_lock();
  412. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  413. if (skp->smk_secid == secid) {
  414. rcu_read_unlock();
  415. return skp->smk_known;
  416. }
  417. }
  418. /*
  419. * If we got this far someone asked for the translation
  420. * of a secid that is not on the list.
  421. */
  422. rcu_read_unlock();
  423. return smack_known_invalid.smk_known;
  424. }
  425. /**
  426. * smack_to_secid - find the secid associated with a Smack label
  427. * @smack: the Smack label
  428. *
  429. * Returns the appropriate secid if there is one,
  430. * otherwise 0
  431. */
  432. u32 smack_to_secid(const char *smack)
  433. {
  434. struct smack_known *skp;
  435. rcu_read_lock();
  436. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  437. if (strncmp(skp->smk_known, smack, SMK_MAXLEN) == 0) {
  438. rcu_read_unlock();
  439. return skp->smk_secid;
  440. }
  441. }
  442. rcu_read_unlock();
  443. return 0;
  444. }
  445. /**
  446. * smack_from_cipso - find the Smack label associated with a CIPSO option
  447. * @level: Bell & LaPadula level from the network
  448. * @cp: Bell & LaPadula categories from the network
  449. *
  450. * This is a simple lookup in the label table.
  451. *
  452. * Return the matching label from the label list or NULL.
  453. */
  454. char *smack_from_cipso(u32 level, char *cp)
  455. {
  456. struct smack_known *kp;
  457. char *final = NULL;
  458. rcu_read_lock();
  459. list_for_each_entry(kp, &smack_known_list, list) {
  460. if (kp->smk_cipso == NULL)
  461. continue;
  462. spin_lock_bh(&kp->smk_cipsolock);
  463. if (kp->smk_cipso->smk_level == level &&
  464. memcmp(kp->smk_cipso->smk_catset, cp, SMK_LABELLEN) == 0)
  465. final = kp->smk_known;
  466. spin_unlock_bh(&kp->smk_cipsolock);
  467. if (final != NULL)
  468. break;
  469. }
  470. rcu_read_unlock();
  471. return final;
  472. }
  473. /**
  474. * smack_to_cipso - find the CIPSO option to go with a Smack label
  475. * @smack: a pointer to the smack label in question
  476. * @cp: where to put the result
  477. *
  478. * Returns zero if a value is available, non-zero otherwise.
  479. */
  480. int smack_to_cipso(const char *smack, struct smack_cipso *cp)
  481. {
  482. struct smack_known *kp;
  483. int found = 0;
  484. rcu_read_lock();
  485. list_for_each_entry_rcu(kp, &smack_known_list, list) {
  486. if (kp->smk_known == smack ||
  487. strcmp(kp->smk_known, smack) == 0) {
  488. found = 1;
  489. break;
  490. }
  491. }
  492. rcu_read_unlock();
  493. if (found == 0 || kp->smk_cipso == NULL)
  494. return -ENOENT;
  495. memcpy(cp, kp->smk_cipso, sizeof(struct smack_cipso));
  496. return 0;
  497. }