smack_access.c 14 KB

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