smack_access.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 == 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_label: a pointer to the subject's Smack label
  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(char *subject_label, char *object_label, int request,
  105. struct smk_audit_info *a)
  106. {
  107. struct smack_known *skp;
  108. int may = MAY_NOT;
  109. int rc = 0;
  110. /*
  111. * Hardcoded comparisons.
  112. *
  113. * A star subject can't access any object.
  114. */
  115. if (subject_label == smack_known_star.smk_known) {
  116. rc = -EACCES;
  117. goto out_audit;
  118. }
  119. /*
  120. * An internet object can be accessed by any subject.
  121. * Tasks cannot be assigned the internet label.
  122. * An internet subject can access any object.
  123. */
  124. if (object_label == smack_known_web.smk_known ||
  125. subject_label == smack_known_web.smk_known)
  126. goto out_audit;
  127. /*
  128. * A star object can be accessed by any subject.
  129. */
  130. if (object_label == smack_known_star.smk_known)
  131. goto out_audit;
  132. /*
  133. * An object can be accessed in any way by a subject
  134. * with the same label.
  135. */
  136. if (subject_label == object_label)
  137. goto out_audit;
  138. /*
  139. * A hat subject can read any object.
  140. * A floor object can be read by any subject.
  141. */
  142. if ((request & MAY_ANYREAD) == request) {
  143. if (object_label == smack_known_floor.smk_known)
  144. goto out_audit;
  145. if (subject_label == smack_known_hat.smk_known)
  146. goto out_audit;
  147. }
  148. /*
  149. * Beyond here an explicit relationship is required.
  150. * If the requested access is contained in the available
  151. * access (e.g. read is included in readwrite) it's
  152. * good. A negative response from smk_access_entry()
  153. * indicates there is no entry for this pair.
  154. */
  155. skp = smk_find_entry(subject_label);
  156. rcu_read_lock();
  157. may = smk_access_entry(subject_label, object_label, &skp->smk_rules);
  158. rcu_read_unlock();
  159. if (may > 0 && (request & may) == request)
  160. goto out_audit;
  161. rc = -EACCES;
  162. out_audit:
  163. #ifdef CONFIG_AUDIT
  164. if (a)
  165. smack_log(subject_label, object_label, request, 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. char *sp = smk_of_task(tsp);
  184. int may;
  185. int rc;
  186. /*
  187. * Check the global rule list
  188. */
  189. rc = smk_access(sp, 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(sp, obj_label, &tsp->smk_rules);
  196. if (may < 0)
  197. goto out_audit;
  198. if ((mode & may) == mode)
  199. goto out_audit;
  200. rc = -EACCES;
  201. }
  202. /*
  203. * Allow for priviliged to override policy.
  204. */
  205. if (rc != 0 && smack_privileged(CAP_MAC_OVERRIDE))
  206. rc = 0;
  207. out_audit:
  208. #ifdef CONFIG_AUDIT
  209. if (a)
  210. smack_log(sp, obj_label, mode, rc, a);
  211. #endif
  212. return rc;
  213. }
  214. #ifdef CONFIG_AUDIT
  215. /**
  216. * smack_str_from_perm : helper to transalate an int to a
  217. * readable string
  218. * @string : the string to fill
  219. * @access : the int
  220. *
  221. */
  222. static inline void smack_str_from_perm(char *string, int access)
  223. {
  224. int i = 0;
  225. if (access & MAY_READ)
  226. string[i++] = 'r';
  227. if (access & MAY_WRITE)
  228. string[i++] = 'w';
  229. if (access & MAY_EXEC)
  230. string[i++] = 'x';
  231. if (access & MAY_APPEND)
  232. string[i++] = 'a';
  233. if (access & MAY_TRANSMUTE)
  234. string[i++] = 't';
  235. string[i] = '\0';
  236. }
  237. /**
  238. * smack_log_callback - SMACK specific information
  239. * will be called by generic audit code
  240. * @ab : the audit_buffer
  241. * @a : audit_data
  242. *
  243. */
  244. static void smack_log_callback(struct audit_buffer *ab, void *a)
  245. {
  246. struct common_audit_data *ad = a;
  247. struct smack_audit_data *sad = ad->smack_audit_data;
  248. audit_log_format(ab, "lsm=SMACK fn=%s action=%s",
  249. ad->smack_audit_data->function,
  250. sad->result ? "denied" : "granted");
  251. audit_log_format(ab, " subject=");
  252. audit_log_untrustedstring(ab, sad->subject);
  253. audit_log_format(ab, " object=");
  254. audit_log_untrustedstring(ab, sad->object);
  255. audit_log_format(ab, " requested=%s", sad->request);
  256. }
  257. /**
  258. * smack_log - Audit the granting or denial of permissions.
  259. * @subject_label : smack label of the requester
  260. * @object_label : smack label of the object being accessed
  261. * @request: requested permissions
  262. * @result: result from smk_access
  263. * @a: auxiliary audit data
  264. *
  265. * Audit the granting or denial of permissions in accordance
  266. * with the policy.
  267. */
  268. void smack_log(char *subject_label, char *object_label, int request,
  269. int result, struct smk_audit_info *ad)
  270. {
  271. char request_buffer[SMK_NUM_ACCESS_TYPE + 1];
  272. struct smack_audit_data *sad;
  273. struct common_audit_data *a = &ad->a;
  274. /* check if we have to log the current event */
  275. if (result != 0 && (log_policy & SMACK_AUDIT_DENIED) == 0)
  276. return;
  277. if (result == 0 && (log_policy & SMACK_AUDIT_ACCEPT) == 0)
  278. return;
  279. sad = a->smack_audit_data;
  280. if (sad->function == NULL)
  281. sad->function = "unknown";
  282. /* end preparing the audit data */
  283. smack_str_from_perm(request_buffer, request);
  284. sad->subject = subject_label;
  285. sad->object = object_label;
  286. sad->request = request_buffer;
  287. sad->result = result;
  288. common_lsm_audit(a, smack_log_callback, NULL);
  289. }
  290. #else /* #ifdef CONFIG_AUDIT */
  291. void smack_log(char *subject_label, char *object_label, int request,
  292. int result, struct smk_audit_info *ad)
  293. {
  294. }
  295. #endif
  296. DEFINE_MUTEX(smack_known_lock);
  297. /**
  298. * smk_find_entry - find a label on the list, return the list entry
  299. * @string: a text string that might be a Smack label
  300. *
  301. * Returns a pointer to the entry in the label list that
  302. * matches the passed string.
  303. */
  304. struct smack_known *smk_find_entry(const char *string)
  305. {
  306. struct smack_known *skp;
  307. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  308. if (strcmp(skp->smk_known, string) == 0)
  309. return skp;
  310. }
  311. return NULL;
  312. }
  313. /**
  314. * smk_parse_smack - parse smack label from a text string
  315. * @string: a text string that might contain a Smack label
  316. * @len: the maximum size, or zero if it is NULL terminated.
  317. *
  318. * Returns a pointer to the clean label, or NULL
  319. */
  320. char *smk_parse_smack(const char *string, int len)
  321. {
  322. char *smack;
  323. int i;
  324. if (len <= 0)
  325. len = strlen(string) + 1;
  326. /*
  327. * Reserve a leading '-' as an indicator that
  328. * this isn't a label, but an option to interfaces
  329. * including /smack/cipso and /smack/cipso2
  330. */
  331. if (string[0] == '-')
  332. return NULL;
  333. for (i = 0; i < len; i++)
  334. if (string[i] > '~' || string[i] <= ' ' || string[i] == '/' ||
  335. string[i] == '"' || string[i] == '\\' || string[i] == '\'')
  336. break;
  337. if (i == 0 || i >= SMK_LONGLABEL)
  338. return NULL;
  339. smack = kzalloc(i + 1, GFP_KERNEL);
  340. if (smack != NULL) {
  341. strncpy(smack, string, i + 1);
  342. smack[i] = '\0';
  343. }
  344. return smack;
  345. }
  346. /**
  347. * smk_netlbl_mls - convert a catset to netlabel mls categories
  348. * @catset: the Smack categories
  349. * @sap: where to put the netlabel categories
  350. *
  351. * Allocates and fills attr.mls
  352. * Returns 0 on success, error code on failure.
  353. */
  354. int smk_netlbl_mls(int level, char *catset, struct netlbl_lsm_secattr *sap,
  355. int len)
  356. {
  357. unsigned char *cp;
  358. unsigned char m;
  359. int cat;
  360. int rc;
  361. int byte;
  362. sap->flags |= NETLBL_SECATTR_MLS_CAT;
  363. sap->attr.mls.lvl = level;
  364. sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
  365. sap->attr.mls.cat->startbit = 0;
  366. for (cat = 1, cp = catset, byte = 0; byte < len; cp++, byte++)
  367. for (m = 0x80; m != 0; m >>= 1, cat++) {
  368. if ((m & *cp) == 0)
  369. continue;
  370. rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
  371. cat, GFP_ATOMIC);
  372. if (rc < 0) {
  373. netlbl_secattr_catmap_free(sap->attr.mls.cat);
  374. return rc;
  375. }
  376. }
  377. return 0;
  378. }
  379. /**
  380. * smk_import_entry - import a label, return the list entry
  381. * @string: a text string that might be a Smack label
  382. * @len: the maximum size, or zero if it is NULL terminated.
  383. *
  384. * Returns a pointer to the entry in the label list that
  385. * matches the passed string, adding it if necessary.
  386. */
  387. struct smack_known *smk_import_entry(const char *string, int len)
  388. {
  389. struct smack_known *skp;
  390. char *smack;
  391. int slen;
  392. int rc;
  393. smack = smk_parse_smack(string, len);
  394. if (smack == NULL)
  395. return NULL;
  396. mutex_lock(&smack_known_lock);
  397. skp = smk_find_entry(smack);
  398. if (skp != NULL)
  399. goto freeout;
  400. skp = kzalloc(sizeof(*skp), GFP_KERNEL);
  401. if (skp == NULL)
  402. goto freeout;
  403. skp->smk_known = smack;
  404. skp->smk_secid = smack_next_secid++;
  405. skp->smk_netlabel.domain = skp->smk_known;
  406. skp->smk_netlabel.flags =
  407. NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
  408. /*
  409. * If direct labeling works use it.
  410. * Otherwise use mapped labeling.
  411. */
  412. slen = strlen(smack);
  413. if (slen < SMK_CIPSOLEN)
  414. rc = smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
  415. &skp->smk_netlabel, slen);
  416. else
  417. rc = smk_netlbl_mls(smack_cipso_mapped, (char *)&skp->smk_secid,
  418. &skp->smk_netlabel, sizeof(skp->smk_secid));
  419. if (rc >= 0) {
  420. INIT_LIST_HEAD(&skp->smk_rules);
  421. mutex_init(&skp->smk_rules_lock);
  422. /*
  423. * Make sure that the entry is actually
  424. * filled before putting it on the list.
  425. */
  426. list_add_rcu(&skp->list, &smack_known_list);
  427. goto unlockout;
  428. }
  429. /*
  430. * smk_netlbl_mls failed.
  431. */
  432. kfree(skp);
  433. skp = NULL;
  434. freeout:
  435. kfree(smack);
  436. unlockout:
  437. mutex_unlock(&smack_known_lock);
  438. return skp;
  439. }
  440. /**
  441. * smk_import - import a smack label
  442. * @string: a text string that might be a Smack label
  443. * @len: the maximum size, or zero if it is NULL terminated.
  444. *
  445. * Returns a pointer to the label in the label list that
  446. * matches the passed string, adding it if necessary.
  447. */
  448. char *smk_import(const char *string, int len)
  449. {
  450. struct smack_known *skp;
  451. /* labels cannot begin with a '-' */
  452. if (string[0] == '-')
  453. return NULL;
  454. skp = smk_import_entry(string, len);
  455. if (skp == NULL)
  456. return NULL;
  457. return skp->smk_known;
  458. }
  459. /**
  460. * smack_from_secid - find the Smack label associated with a secid
  461. * @secid: an integer that might be associated with a Smack label
  462. *
  463. * Returns a pointer to the appropriate Smack label if there is one,
  464. * otherwise a pointer to the invalid Smack label.
  465. */
  466. char *smack_from_secid(const u32 secid)
  467. {
  468. struct smack_known *skp;
  469. rcu_read_lock();
  470. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  471. if (skp->smk_secid == secid) {
  472. rcu_read_unlock();
  473. return skp->smk_known;
  474. }
  475. }
  476. /*
  477. * If we got this far someone asked for the translation
  478. * of a secid that is not on the list.
  479. */
  480. rcu_read_unlock();
  481. return smack_known_invalid.smk_known;
  482. }
  483. /**
  484. * smack_to_secid - find the secid associated with a Smack label
  485. * @smack: the Smack label
  486. *
  487. * Returns the appropriate secid if there is one,
  488. * otherwise 0
  489. */
  490. u32 smack_to_secid(const char *smack)
  491. {
  492. struct smack_known *skp = smk_find_entry(smack);
  493. if (skp == NULL)
  494. return 0;
  495. return skp->smk_secid;
  496. }