smack_access.c 15 KB

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