smack_access.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. * Allowing write access implies allowing locking.
  78. */
  79. int smk_access_entry(char *subject_label, char *object_label,
  80. struct list_head *rule_list)
  81. {
  82. int may = -ENOENT;
  83. struct smack_rule *srp;
  84. list_for_each_entry_rcu(srp, rule_list, list) {
  85. if (srp->smk_object == object_label &&
  86. srp->smk_subject->smk_known == subject_label) {
  87. may = srp->smk_access;
  88. break;
  89. }
  90. }
  91. /*
  92. * MAY_WRITE implies MAY_LOCK.
  93. */
  94. if ((may & MAY_WRITE) == MAY_WRITE)
  95. may |= MAY_LOCK;
  96. return may;
  97. }
  98. /**
  99. * smk_access - determine if a subject has a specific access to an object
  100. * @subject_known: a pointer to the subject's Smack label entry
  101. * @object_label: a pointer to the object's Smack label
  102. * @request: the access requested, in "MAY" format
  103. * @a : a pointer to the audit data
  104. *
  105. * This function looks up the subject/object pair in the
  106. * access rule list and returns 0 if the access is permitted,
  107. * non zero otherwise.
  108. *
  109. * Smack labels are shared on smack_list
  110. */
  111. int smk_access(struct smack_known *subject_known, char *object_label,
  112. int request, struct smk_audit_info *a)
  113. {
  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_known == &smack_known_star) {
  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_known == &smack_known_web)
  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_known->smk_known == 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_known == &smack_known_hat)
  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. rcu_read_lock();
  162. may = smk_access_entry(subject_known->smk_known, object_label,
  163. &subject_known->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_known->smk_known, object_label, request,
  172. rc, a);
  173. #endif
  174. return rc;
  175. }
  176. /**
  177. * smk_curacc - determine if current has a specific access to an object
  178. * @obj_label: a pointer to the object's Smack label
  179. * @mode: the access requested, in "MAY" format
  180. * @a : common audit data
  181. *
  182. * This function checks the current subject label/object label pair
  183. * in the access rule list and returns 0 if the access is permitted,
  184. * non zero otherwise. It allows that current may have the capability
  185. * to override the rules.
  186. */
  187. int smk_curacc(char *obj_label, u32 mode, struct smk_audit_info *a)
  188. {
  189. struct task_smack *tsp = current_security();
  190. struct smack_known *skp = smk_of_task(tsp);
  191. int may;
  192. int rc;
  193. /*
  194. * Check the global rule list
  195. */
  196. rc = smk_access(skp, obj_label, mode, NULL);
  197. if (rc == 0) {
  198. /*
  199. * If there is an entry in the task's rule list
  200. * it can further restrict access.
  201. */
  202. may = smk_access_entry(skp->smk_known, obj_label,
  203. &tsp->smk_rules);
  204. if (may < 0)
  205. goto out_audit;
  206. if ((mode & may) == mode)
  207. goto out_audit;
  208. rc = -EACCES;
  209. }
  210. /*
  211. * Allow for priviliged to override policy.
  212. */
  213. if (rc != 0 && smack_privileged(CAP_MAC_OVERRIDE))
  214. rc = 0;
  215. out_audit:
  216. #ifdef CONFIG_AUDIT
  217. if (a)
  218. smack_log(skp->smk_known, obj_label, mode, rc, a);
  219. #endif
  220. return rc;
  221. }
  222. #ifdef CONFIG_AUDIT
  223. /**
  224. * smack_str_from_perm : helper to transalate an int to a
  225. * readable string
  226. * @string : the string to fill
  227. * @access : the int
  228. *
  229. */
  230. static inline void smack_str_from_perm(char *string, int access)
  231. {
  232. int i = 0;
  233. if (access & MAY_READ)
  234. string[i++] = 'r';
  235. if (access & MAY_WRITE)
  236. string[i++] = 'w';
  237. if (access & MAY_EXEC)
  238. string[i++] = 'x';
  239. if (access & MAY_APPEND)
  240. string[i++] = 'a';
  241. if (access & MAY_TRANSMUTE)
  242. string[i++] = 't';
  243. if (access & MAY_LOCK)
  244. string[i++] = 'l';
  245. string[i] = '\0';
  246. }
  247. /**
  248. * smack_log_callback - SMACK specific information
  249. * will be called by generic audit code
  250. * @ab : the audit_buffer
  251. * @a : audit_data
  252. *
  253. */
  254. static void smack_log_callback(struct audit_buffer *ab, void *a)
  255. {
  256. struct common_audit_data *ad = a;
  257. struct smack_audit_data *sad = ad->smack_audit_data;
  258. audit_log_format(ab, "lsm=SMACK fn=%s action=%s",
  259. ad->smack_audit_data->function,
  260. sad->result ? "denied" : "granted");
  261. audit_log_format(ab, " subject=");
  262. audit_log_untrustedstring(ab, sad->subject);
  263. audit_log_format(ab, " object=");
  264. audit_log_untrustedstring(ab, sad->object);
  265. audit_log_format(ab, " requested=%s", sad->request);
  266. }
  267. /**
  268. * smack_log - Audit the granting or denial of permissions.
  269. * @subject_label : smack label of the requester
  270. * @object_label : smack label of the object being accessed
  271. * @request: requested permissions
  272. * @result: result from smk_access
  273. * @a: auxiliary audit data
  274. *
  275. * Audit the granting or denial of permissions in accordance
  276. * with the policy.
  277. */
  278. void smack_log(char *subject_label, char *object_label, int request,
  279. int result, struct smk_audit_info *ad)
  280. {
  281. char request_buffer[SMK_NUM_ACCESS_TYPE + 1];
  282. struct smack_audit_data *sad;
  283. struct common_audit_data *a = &ad->a;
  284. /* check if we have to log the current event */
  285. if (result != 0 && (log_policy & SMACK_AUDIT_DENIED) == 0)
  286. return;
  287. if (result == 0 && (log_policy & SMACK_AUDIT_ACCEPT) == 0)
  288. return;
  289. sad = a->smack_audit_data;
  290. if (sad->function == NULL)
  291. sad->function = "unknown";
  292. /* end preparing the audit data */
  293. smack_str_from_perm(request_buffer, request);
  294. sad->subject = subject_label;
  295. sad->object = object_label;
  296. sad->request = request_buffer;
  297. sad->result = result;
  298. common_lsm_audit(a, smack_log_callback, NULL);
  299. }
  300. #else /* #ifdef CONFIG_AUDIT */
  301. void smack_log(char *subject_label, char *object_label, int request,
  302. int result, struct smk_audit_info *ad)
  303. {
  304. }
  305. #endif
  306. DEFINE_MUTEX(smack_known_lock);
  307. struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
  308. /**
  309. * smk_insert_entry - insert a smack label into a hash map,
  310. *
  311. * this function must be called under smack_known_lock
  312. */
  313. void smk_insert_entry(struct smack_known *skp)
  314. {
  315. unsigned int hash;
  316. struct hlist_head *head;
  317. hash = full_name_hash(skp->smk_known, strlen(skp->smk_known));
  318. head = &smack_known_hash[hash & (SMACK_HASH_SLOTS - 1)];
  319. hlist_add_head_rcu(&skp->smk_hashed, head);
  320. list_add_rcu(&skp->list, &smack_known_list);
  321. }
  322. /**
  323. * smk_find_entry - find a label on the list, return the list entry
  324. * @string: a text string that might be a Smack label
  325. *
  326. * Returns a pointer to the entry in the label list that
  327. * matches the passed string.
  328. */
  329. struct smack_known *smk_find_entry(const char *string)
  330. {
  331. unsigned int hash;
  332. struct hlist_head *head;
  333. struct smack_known *skp;
  334. hash = full_name_hash(string, strlen(string));
  335. head = &smack_known_hash[hash & (SMACK_HASH_SLOTS - 1)];
  336. hlist_for_each_entry_rcu(skp, head, smk_hashed)
  337. if (strcmp(skp->smk_known, string) == 0)
  338. return skp;
  339. return NULL;
  340. }
  341. /**
  342. * smk_parse_smack - parse smack label from a text string
  343. * @string: a text string that might contain a Smack label
  344. * @len: the maximum size, or zero if it is NULL terminated.
  345. *
  346. * Returns a pointer to the clean label, or NULL
  347. */
  348. char *smk_parse_smack(const char *string, int len)
  349. {
  350. char *smack;
  351. int i;
  352. if (len <= 0)
  353. len = strlen(string) + 1;
  354. /*
  355. * Reserve a leading '-' as an indicator that
  356. * this isn't a label, but an option to interfaces
  357. * including /smack/cipso and /smack/cipso2
  358. */
  359. if (string[0] == '-')
  360. return NULL;
  361. for (i = 0; i < len; i++)
  362. if (string[i] > '~' || string[i] <= ' ' || string[i] == '/' ||
  363. string[i] == '"' || string[i] == '\\' || string[i] == '\'')
  364. break;
  365. if (i == 0 || i >= SMK_LONGLABEL)
  366. return NULL;
  367. smack = kzalloc(i + 1, GFP_KERNEL);
  368. if (smack != NULL) {
  369. strncpy(smack, string, i + 1);
  370. smack[i] = '\0';
  371. }
  372. return smack;
  373. }
  374. /**
  375. * smk_netlbl_mls - convert a catset to netlabel mls categories
  376. * @catset: the Smack categories
  377. * @sap: where to put the netlabel categories
  378. *
  379. * Allocates and fills attr.mls
  380. * Returns 0 on success, error code on failure.
  381. */
  382. int smk_netlbl_mls(int level, char *catset, struct netlbl_lsm_secattr *sap,
  383. int len)
  384. {
  385. unsigned char *cp;
  386. unsigned char m;
  387. int cat;
  388. int rc;
  389. int byte;
  390. sap->flags |= NETLBL_SECATTR_MLS_CAT;
  391. sap->attr.mls.lvl = level;
  392. sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
  393. if (!sap->attr.mls.cat)
  394. return -ENOMEM;
  395. sap->attr.mls.cat->startbit = 0;
  396. for (cat = 1, cp = catset, byte = 0; byte < len; cp++, byte++)
  397. for (m = 0x80; m != 0; m >>= 1, cat++) {
  398. if ((m & *cp) == 0)
  399. continue;
  400. rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
  401. cat, GFP_ATOMIC);
  402. if (rc < 0) {
  403. netlbl_secattr_catmap_free(sap->attr.mls.cat);
  404. return rc;
  405. }
  406. }
  407. return 0;
  408. }
  409. /**
  410. * smk_import_entry - import a label, return the list entry
  411. * @string: a text string that might be a Smack label
  412. * @len: the maximum size, or zero if it is NULL terminated.
  413. *
  414. * Returns a pointer to the entry in the label list that
  415. * matches the passed string, adding it if necessary.
  416. */
  417. struct smack_known *smk_import_entry(const char *string, int len)
  418. {
  419. struct smack_known *skp;
  420. char *smack;
  421. int slen;
  422. int rc;
  423. smack = smk_parse_smack(string, len);
  424. if (smack == NULL)
  425. return NULL;
  426. mutex_lock(&smack_known_lock);
  427. skp = smk_find_entry(smack);
  428. if (skp != NULL)
  429. goto freeout;
  430. skp = kzalloc(sizeof(*skp), GFP_KERNEL);
  431. if (skp == NULL)
  432. goto freeout;
  433. skp->smk_known = smack;
  434. skp->smk_secid = smack_next_secid++;
  435. skp->smk_netlabel.domain = skp->smk_known;
  436. skp->smk_netlabel.flags =
  437. NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
  438. /*
  439. * If direct labeling works use it.
  440. * Otherwise use mapped labeling.
  441. */
  442. slen = strlen(smack);
  443. if (slen < SMK_CIPSOLEN)
  444. rc = smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
  445. &skp->smk_netlabel, slen);
  446. else
  447. rc = smk_netlbl_mls(smack_cipso_mapped, (char *)&skp->smk_secid,
  448. &skp->smk_netlabel, sizeof(skp->smk_secid));
  449. if (rc >= 0) {
  450. INIT_LIST_HEAD(&skp->smk_rules);
  451. mutex_init(&skp->smk_rules_lock);
  452. /*
  453. * Make sure that the entry is actually
  454. * filled before putting it on the list.
  455. */
  456. smk_insert_entry(skp);
  457. goto unlockout;
  458. }
  459. /*
  460. * smk_netlbl_mls failed.
  461. */
  462. kfree(skp);
  463. skp = NULL;
  464. freeout:
  465. kfree(smack);
  466. unlockout:
  467. mutex_unlock(&smack_known_lock);
  468. return skp;
  469. }
  470. /**
  471. * smk_import - import a smack label
  472. * @string: a text string that might be a Smack label
  473. * @len: the maximum size, or zero if it is NULL terminated.
  474. *
  475. * Returns a pointer to the label in the label list that
  476. * matches the passed string, adding it if necessary.
  477. */
  478. char *smk_import(const char *string, int len)
  479. {
  480. struct smack_known *skp;
  481. /* labels cannot begin with a '-' */
  482. if (string[0] == '-')
  483. return NULL;
  484. skp = smk_import_entry(string, len);
  485. if (skp == NULL)
  486. return NULL;
  487. return skp->smk_known;
  488. }
  489. /**
  490. * smack_from_secid - find the Smack label associated with a secid
  491. * @secid: an integer that might be associated with a Smack label
  492. *
  493. * Returns a pointer to the appropriate Smack label entry if there is one,
  494. * otherwise a pointer to the invalid Smack label.
  495. */
  496. struct smack_known *smack_from_secid(const u32 secid)
  497. {
  498. struct smack_known *skp;
  499. rcu_read_lock();
  500. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  501. if (skp->smk_secid == secid) {
  502. rcu_read_unlock();
  503. return skp;
  504. }
  505. }
  506. /*
  507. * If we got this far someone asked for the translation
  508. * of a secid that is not on the list.
  509. */
  510. rcu_read_unlock();
  511. return &smack_known_invalid;
  512. }
  513. /**
  514. * smack_to_secid - find the secid associated with a Smack label
  515. * @smack: the Smack label
  516. *
  517. * Returns the appropriate secid if there is one,
  518. * otherwise 0
  519. */
  520. u32 smack_to_secid(const char *smack)
  521. {
  522. struct smack_known *skp = smk_find_entry(smack);
  523. if (skp == NULL)
  524. return 0;
  525. return skp->smk_secid;
  526. }