smack_access.c 14 KB

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