smack_access.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. string[i] = '\0';
  234. }
  235. /**
  236. * smack_log_callback - SMACK specific information
  237. * will be called by generic audit code
  238. * @ab : the audit_buffer
  239. * @a : audit_data
  240. *
  241. */
  242. static void smack_log_callback(struct audit_buffer *ab, void *a)
  243. {
  244. struct common_audit_data *ad = a;
  245. struct smack_audit_data *sad = ad->smack_audit_data;
  246. audit_log_format(ab, "lsm=SMACK fn=%s action=%s",
  247. ad->smack_audit_data->function,
  248. sad->result ? "denied" : "granted");
  249. audit_log_format(ab, " subject=");
  250. audit_log_untrustedstring(ab, sad->subject);
  251. audit_log_format(ab, " object=");
  252. audit_log_untrustedstring(ab, sad->object);
  253. audit_log_format(ab, " requested=%s", sad->request);
  254. }
  255. /**
  256. * smack_log - Audit the granting or denial of permissions.
  257. * @subject_label : smack label of the requester
  258. * @object_label : smack label of the object being accessed
  259. * @request: requested permissions
  260. * @result: result from smk_access
  261. * @a: auxiliary audit data
  262. *
  263. * Audit the granting or denial of permissions in accordance
  264. * with the policy.
  265. */
  266. void smack_log(char *subject_label, char *object_label, int request,
  267. int result, struct smk_audit_info *ad)
  268. {
  269. char request_buffer[SMK_NUM_ACCESS_TYPE + 1];
  270. struct smack_audit_data *sad;
  271. struct common_audit_data *a = &ad->a;
  272. /* check if we have to log the current event */
  273. if (result != 0 && (log_policy & SMACK_AUDIT_DENIED) == 0)
  274. return;
  275. if (result == 0 && (log_policy & SMACK_AUDIT_ACCEPT) == 0)
  276. return;
  277. sad = a->smack_audit_data;
  278. if (sad->function == NULL)
  279. sad->function = "unknown";
  280. /* end preparing the audit data */
  281. smack_str_from_perm(request_buffer, request);
  282. sad->subject = subject_label;
  283. sad->object = object_label;
  284. sad->request = request_buffer;
  285. sad->result = result;
  286. common_lsm_audit(a, smack_log_callback, NULL);
  287. }
  288. #else /* #ifdef CONFIG_AUDIT */
  289. void smack_log(char *subject_label, char *object_label, int request,
  290. int result, struct smk_audit_info *ad)
  291. {
  292. }
  293. #endif
  294. DEFINE_MUTEX(smack_known_lock);
  295. /**
  296. * smk_find_entry - find a label on the list, return the list entry
  297. * @string: a text string that might be a Smack label
  298. *
  299. * Returns a pointer to the entry in the label list that
  300. * matches the passed string.
  301. */
  302. struct smack_known *smk_find_entry(const char *string)
  303. {
  304. struct smack_known *skp;
  305. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  306. if (strcmp(skp->smk_known, string) == 0)
  307. return skp;
  308. }
  309. return NULL;
  310. }
  311. /**
  312. * smk_parse_smack - parse smack label from a text string
  313. * @string: a text string that might contain a Smack label
  314. * @len: the maximum size, or zero if it is NULL terminated.
  315. *
  316. * Returns a pointer to the clean label, or NULL
  317. */
  318. char *smk_parse_smack(const char *string, int len)
  319. {
  320. char *smack;
  321. int i;
  322. if (len <= 0)
  323. len = strlen(string) + 1;
  324. /*
  325. * Reserve a leading '-' as an indicator that
  326. * this isn't a label, but an option to interfaces
  327. * including /smack/cipso and /smack/cipso2
  328. */
  329. if (string[0] == '-')
  330. return NULL;
  331. for (i = 0; i < len; i++)
  332. if (string[i] > '~' || string[i] <= ' ' || string[i] == '/' ||
  333. string[i] == '"' || string[i] == '\\' || string[i] == '\'')
  334. break;
  335. if (i == 0 || i >= SMK_LONGLABEL)
  336. return NULL;
  337. smack = kzalloc(i + 1, GFP_KERNEL);
  338. if (smack != NULL) {
  339. strncpy(smack, string, i + 1);
  340. smack[i] = '\0';
  341. }
  342. return smack;
  343. }
  344. /**
  345. * smk_netlbl_mls - convert a catset to netlabel mls categories
  346. * @catset: the Smack categories
  347. * @sap: where to put the netlabel categories
  348. *
  349. * Allocates and fills attr.mls
  350. * Returns 0 on success, error code on failure.
  351. */
  352. int smk_netlbl_mls(int level, char *catset, struct netlbl_lsm_secattr *sap,
  353. int len)
  354. {
  355. unsigned char *cp;
  356. unsigned char m;
  357. int cat;
  358. int rc;
  359. int byte;
  360. sap->flags |= NETLBL_SECATTR_MLS_CAT;
  361. sap->attr.mls.lvl = level;
  362. sap->attr.mls.cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
  363. sap->attr.mls.cat->startbit = 0;
  364. for (cat = 1, cp = catset, byte = 0; byte < len; cp++, byte++)
  365. for (m = 0x80; m != 0; m >>= 1, cat++) {
  366. if ((m & *cp) == 0)
  367. continue;
  368. rc = netlbl_secattr_catmap_setbit(sap->attr.mls.cat,
  369. cat, GFP_ATOMIC);
  370. if (rc < 0) {
  371. netlbl_secattr_catmap_free(sap->attr.mls.cat);
  372. return rc;
  373. }
  374. }
  375. return 0;
  376. }
  377. /**
  378. * smk_import_entry - import a label, return the list entry
  379. * @string: a text string that might be a Smack label
  380. * @len: the maximum size, or zero if it is NULL terminated.
  381. *
  382. * Returns a pointer to the entry in the label list that
  383. * matches the passed string, adding it if necessary.
  384. */
  385. struct smack_known *smk_import_entry(const char *string, int len)
  386. {
  387. struct smack_known *skp;
  388. char *smack;
  389. int slen;
  390. int rc;
  391. smack = smk_parse_smack(string, len);
  392. if (smack == NULL)
  393. return NULL;
  394. mutex_lock(&smack_known_lock);
  395. skp = smk_find_entry(smack);
  396. if (skp != NULL)
  397. goto freeout;
  398. skp = kzalloc(sizeof(*skp), GFP_KERNEL);
  399. if (skp == NULL)
  400. goto freeout;
  401. skp->smk_known = smack;
  402. skp->smk_secid = smack_next_secid++;
  403. skp->smk_netlabel.domain = skp->smk_known;
  404. skp->smk_netlabel.flags =
  405. NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
  406. /*
  407. * If direct labeling works use it.
  408. * Otherwise use mapped labeling.
  409. */
  410. slen = strlen(smack);
  411. if (slen < SMK_CIPSOLEN)
  412. rc = smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
  413. &skp->smk_netlabel, slen);
  414. else
  415. rc = smk_netlbl_mls(smack_cipso_mapped, (char *)&skp->smk_secid,
  416. &skp->smk_netlabel, sizeof(skp->smk_secid));
  417. if (rc >= 0) {
  418. INIT_LIST_HEAD(&skp->smk_rules);
  419. mutex_init(&skp->smk_rules_lock);
  420. /*
  421. * Make sure that the entry is actually
  422. * filled before putting it on the list.
  423. */
  424. list_add_rcu(&skp->list, &smack_known_list);
  425. goto unlockout;
  426. }
  427. /*
  428. * smk_netlbl_mls failed.
  429. */
  430. kfree(skp);
  431. skp = NULL;
  432. freeout:
  433. kfree(smack);
  434. unlockout:
  435. mutex_unlock(&smack_known_lock);
  436. return skp;
  437. }
  438. /**
  439. * smk_import - import a smack label
  440. * @string: a text string that might be a Smack label
  441. * @len: the maximum size, or zero if it is NULL terminated.
  442. *
  443. * Returns a pointer to the label in the label list that
  444. * matches the passed string, adding it if necessary.
  445. */
  446. char *smk_import(const char *string, int len)
  447. {
  448. struct smack_known *skp;
  449. /* labels cannot begin with a '-' */
  450. if (string[0] == '-')
  451. return NULL;
  452. skp = smk_import_entry(string, len);
  453. if (skp == NULL)
  454. return NULL;
  455. return skp->smk_known;
  456. }
  457. /**
  458. * smack_from_secid - find the Smack label associated with a secid
  459. * @secid: an integer that might be associated with a Smack label
  460. *
  461. * Returns a pointer to the appropriate Smack label if there is one,
  462. * otherwise a pointer to the invalid Smack label.
  463. */
  464. char *smack_from_secid(const u32 secid)
  465. {
  466. struct smack_known *skp;
  467. rcu_read_lock();
  468. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  469. if (skp->smk_secid == secid) {
  470. rcu_read_unlock();
  471. return skp->smk_known;
  472. }
  473. }
  474. /*
  475. * If we got this far someone asked for the translation
  476. * of a secid that is not on the list.
  477. */
  478. rcu_read_unlock();
  479. return smack_known_invalid.smk_known;
  480. }
  481. /**
  482. * smack_to_secid - find the secid associated with a Smack label
  483. * @smack: the Smack label
  484. *
  485. * Returns the appropriate secid if there is one,
  486. * otherwise 0
  487. */
  488. u32 smack_to_secid(const char *smack)
  489. {
  490. struct smack_known *skp = smk_find_entry(smack);
  491. if (skp == NULL)
  492. return 0;
  493. return skp->smk_secid;
  494. }