smack_access.c 15 KB

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