smack_access.c 15 KB

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