smack_access.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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_web = {
  34. .smk_known = "@",
  35. .smk_secid = 7,
  36. };
  37. LIST_HEAD(smack_known_list);
  38. /*
  39. * The initial value needs to be bigger than any of the
  40. * known values above.
  41. */
  42. static u32 smack_next_secid = 10;
  43. /*
  44. * what events do we log
  45. * can be overwritten at run-time by /smack/logging
  46. */
  47. int log_policy = SMACK_AUDIT_DENIED;
  48. /**
  49. * smk_access_entry - look up matching access rule
  50. * @subject_label: a pointer to the subject's Smack label
  51. * @object_label: a pointer to the object's Smack label
  52. * @rule_list: the list of rules to search
  53. *
  54. * This function looks up the subject/object pair in the
  55. * access rule list and returns the access mode. If no
  56. * entry is found returns -ENOENT.
  57. *
  58. * NOTE:
  59. *
  60. * Earlier versions of this function allowed for labels that
  61. * were not on the label list. This was done to allow for
  62. * labels to come over the network that had never been seen
  63. * before on this host. Unless the receiving socket has the
  64. * star label this will always result in a failure check. The
  65. * star labeled socket case is now handled in the networking
  66. * hooks so there is no case where the label is not on the
  67. * label list. Checking to see if the address of two labels
  68. * is the same is now a reliable test.
  69. *
  70. * Do the object check first because that is more
  71. * likely to differ.
  72. *
  73. * Allowing write access implies allowing locking.
  74. */
  75. int smk_access_entry(char *subject_label, char *object_label,
  76. struct list_head *rule_list)
  77. {
  78. int may = -ENOENT;
  79. struct smack_rule *srp;
  80. list_for_each_entry_rcu(srp, rule_list, list) {
  81. if (srp->smk_object->smk_known == object_label &&
  82. srp->smk_subject->smk_known == subject_label) {
  83. may = srp->smk_access;
  84. break;
  85. }
  86. }
  87. /*
  88. * MAY_WRITE implies MAY_LOCK.
  89. */
  90. if ((may & MAY_WRITE) == MAY_WRITE)
  91. may |= MAY_LOCK;
  92. return may;
  93. }
  94. /**
  95. * smk_access - determine if a subject has a specific access to an object
  96. * @subject: a pointer to the subject's Smack label entry
  97. * @object: a pointer to the object's Smack label entry
  98. * @request: the access requested, in "MAY" format
  99. * @a : a pointer to the audit data
  100. *
  101. * This function looks up the subject/object pair in the
  102. * access rule list and returns 0 if the access is permitted,
  103. * non zero otherwise.
  104. *
  105. * Smack labels are shared on smack_list
  106. */
  107. int smk_access(struct smack_known *subject, struct smack_known *object,
  108. int request, struct smk_audit_info *a)
  109. {
  110. int may = MAY_NOT;
  111. int rc = 0;
  112. /*
  113. * Hardcoded comparisons.
  114. */
  115. /*
  116. * A star subject can't access any object.
  117. */
  118. if (subject == &smack_known_star) {
  119. rc = -EACCES;
  120. goto out_audit;
  121. }
  122. /*
  123. * An internet object can be accessed by any subject.
  124. * Tasks cannot be assigned the internet label.
  125. * An internet subject can access any object.
  126. */
  127. if (object == &smack_known_web || subject == &smack_known_web)
  128. goto out_audit;
  129. /*
  130. * A star object can be accessed by any subject.
  131. */
  132. if (object == &smack_known_star)
  133. goto out_audit;
  134. /*
  135. * An object can be accessed in any way by a subject
  136. * with the same label.
  137. */
  138. if (subject->smk_known == object->smk_known)
  139. goto out_audit;
  140. /*
  141. * A hat subject can read or lock any object.
  142. * A floor object can be read or locked by any subject.
  143. */
  144. if ((request & MAY_ANYREAD) == request ||
  145. (request & MAY_LOCK) == request) {
  146. if (object == &smack_known_floor)
  147. goto out_audit;
  148. if (subject == &smack_known_hat)
  149. goto out_audit;
  150. }
  151. /*
  152. * Beyond here an explicit relationship is required.
  153. * If the requested access is contained in the available
  154. * access (e.g. read is included in readwrite) it's
  155. * good. A negative response from smk_access_entry()
  156. * indicates there is no entry for this pair.
  157. */
  158. rcu_read_lock();
  159. may = smk_access_entry(subject->smk_known, object->smk_known,
  160. &subject->smk_rules);
  161. rcu_read_unlock();
  162. if (may <= 0 || (request & may) != request) {
  163. rc = -EACCES;
  164. goto out_audit;
  165. }
  166. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  167. /*
  168. * Return a positive value if using bringup mode.
  169. * This allows the hooks to identify checks that
  170. * succeed because of "b" rules.
  171. */
  172. if (may & MAY_BRINGUP)
  173. rc = SMACK_BRINGUP_ALLOW;
  174. #endif
  175. out_audit:
  176. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  177. if (rc < 0) {
  178. if (object == smack_unconfined)
  179. rc = SMACK_UNCONFINED_OBJECT;
  180. if (subject == smack_unconfined)
  181. rc = SMACK_UNCONFINED_SUBJECT;
  182. }
  183. #endif
  184. #ifdef CONFIG_AUDIT
  185. if (a)
  186. smack_log(subject->smk_known, object->smk_known,
  187. request, rc, a);
  188. #endif
  189. return rc;
  190. }
  191. /**
  192. * smk_tskacc - determine if a task has a specific access to an object
  193. * @tsp: a pointer to the subject's task
  194. * @obj_known: a pointer to the object's label entry
  195. * @mode: the access requested, in "MAY" format
  196. * @a : common audit data
  197. *
  198. * This function checks the subject task's label/object label pair
  199. * in the access rule list and returns 0 if the access is permitted,
  200. * non zero otherwise. It allows that the task may have the capability
  201. * to override the rules.
  202. */
  203. int smk_tskacc(struct task_smack *tsp, struct smack_known *obj_known,
  204. u32 mode, struct smk_audit_info *a)
  205. {
  206. struct smack_known *sbj_known = smk_of_task(tsp);
  207. int may;
  208. int rc;
  209. /*
  210. * Check the global rule list
  211. */
  212. rc = smk_access(sbj_known, obj_known, mode, NULL);
  213. if (rc >= 0) {
  214. /*
  215. * If there is an entry in the task's rule list
  216. * it can further restrict access.
  217. */
  218. may = smk_access_entry(sbj_known->smk_known,
  219. obj_known->smk_known,
  220. &tsp->smk_rules);
  221. if (may < 0)
  222. goto out_audit;
  223. if ((mode & may) == mode)
  224. goto out_audit;
  225. rc = -EACCES;
  226. }
  227. /*
  228. * Allow for priviliged to override policy.
  229. */
  230. if (rc != 0 && smack_privileged(CAP_MAC_OVERRIDE))
  231. rc = 0;
  232. out_audit:
  233. #ifdef CONFIG_AUDIT
  234. if (a)
  235. smack_log(sbj_known->smk_known, obj_known->smk_known,
  236. mode, rc, a);
  237. #endif
  238. return rc;
  239. }
  240. /**
  241. * smk_curacc - determine if current has a specific access to an object
  242. * @obj_known: a pointer to the object's Smack label entry
  243. * @mode: the access requested, in "MAY" format
  244. * @a : common audit data
  245. *
  246. * This function checks the current subject label/object label pair
  247. * in the access rule list and returns 0 if the access is permitted,
  248. * non zero otherwise. It allows that current may have the capability
  249. * to override the rules.
  250. */
  251. int smk_curacc(struct smack_known *obj_known,
  252. u32 mode, struct smk_audit_info *a)
  253. {
  254. struct task_smack *tsp = current_security();
  255. return smk_tskacc(tsp, obj_known, mode, a);
  256. }
  257. #ifdef CONFIG_AUDIT
  258. /**
  259. * smack_str_from_perm : helper to transalate an int to a
  260. * readable string
  261. * @string : the string to fill
  262. * @access : the int
  263. *
  264. */
  265. static inline void smack_str_from_perm(char *string, int access)
  266. {
  267. int i = 0;
  268. if (access & MAY_READ)
  269. string[i++] = 'r';
  270. if (access & MAY_WRITE)
  271. string[i++] = 'w';
  272. if (access & MAY_EXEC)
  273. string[i++] = 'x';
  274. if (access & MAY_APPEND)
  275. string[i++] = 'a';
  276. if (access & MAY_TRANSMUTE)
  277. string[i++] = 't';
  278. if (access & MAY_LOCK)
  279. string[i++] = 'l';
  280. string[i] = '\0';
  281. }
  282. /**
  283. * smack_log_callback - SMACK specific information
  284. * will be called by generic audit code
  285. * @ab : the audit_buffer
  286. * @a : audit_data
  287. *
  288. */
  289. static void smack_log_callback(struct audit_buffer *ab, void *a)
  290. {
  291. struct common_audit_data *ad = a;
  292. struct smack_audit_data *sad = ad->smack_audit_data;
  293. audit_log_format(ab, "lsm=SMACK fn=%s action=%s",
  294. ad->smack_audit_data->function,
  295. sad->result ? "denied" : "granted");
  296. audit_log_format(ab, " subject=");
  297. audit_log_untrustedstring(ab, sad->subject);
  298. audit_log_format(ab, " object=");
  299. audit_log_untrustedstring(ab, sad->object);
  300. if (sad->request[0] == '\0')
  301. audit_log_format(ab, " labels_differ");
  302. else
  303. audit_log_format(ab, " requested=%s", sad->request);
  304. }
  305. /**
  306. * smack_log - Audit the granting or denial of permissions.
  307. * @subject_label : smack label of the requester
  308. * @object_label : smack label of the object being accessed
  309. * @request: requested permissions
  310. * @result: result from smk_access
  311. * @a: auxiliary audit data
  312. *
  313. * Audit the granting or denial of permissions in accordance
  314. * with the policy.
  315. */
  316. void smack_log(char *subject_label, char *object_label, int request,
  317. int result, struct smk_audit_info *ad)
  318. {
  319. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  320. char request_buffer[SMK_NUM_ACCESS_TYPE + 5];
  321. #else
  322. char request_buffer[SMK_NUM_ACCESS_TYPE + 1];
  323. #endif
  324. struct smack_audit_data *sad;
  325. struct common_audit_data *a = &ad->a;
  326. /* check if we have to log the current event */
  327. if (result < 0 && (log_policy & SMACK_AUDIT_DENIED) == 0)
  328. return;
  329. if (result == 0 && (log_policy & SMACK_AUDIT_ACCEPT) == 0)
  330. return;
  331. sad = a->smack_audit_data;
  332. if (sad->function == NULL)
  333. sad->function = "unknown";
  334. /* end preparing the audit data */
  335. smack_str_from_perm(request_buffer, request);
  336. sad->subject = subject_label;
  337. sad->object = object_label;
  338. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  339. /*
  340. * The result may be positive in bringup mode.
  341. * A positive result is an allow, but not for normal reasons.
  342. * Mark it as successful, but don't filter it out even if
  343. * the logging policy says to do so.
  344. */
  345. if (result == SMACK_UNCONFINED_SUBJECT)
  346. strcat(request_buffer, "(US)");
  347. else if (result == SMACK_UNCONFINED_OBJECT)
  348. strcat(request_buffer, "(UO)");
  349. if (result > 0)
  350. result = 0;
  351. #endif
  352. sad->request = request_buffer;
  353. sad->result = result;
  354. common_lsm_audit(a, smack_log_callback, NULL);
  355. }
  356. #else /* #ifdef CONFIG_AUDIT */
  357. void smack_log(char *subject_label, char *object_label, int request,
  358. int result, struct smk_audit_info *ad)
  359. {
  360. }
  361. #endif
  362. DEFINE_MUTEX(smack_known_lock);
  363. struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
  364. /**
  365. * smk_insert_entry - insert a smack label into a hash map,
  366. *
  367. * this function must be called under smack_known_lock
  368. */
  369. void smk_insert_entry(struct smack_known *skp)
  370. {
  371. unsigned int hash;
  372. struct hlist_head *head;
  373. hash = full_name_hash(NULL, skp->smk_known, strlen(skp->smk_known));
  374. head = &smack_known_hash[hash & (SMACK_HASH_SLOTS - 1)];
  375. hlist_add_head_rcu(&skp->smk_hashed, head);
  376. list_add_rcu(&skp->list, &smack_known_list);
  377. }
  378. /**
  379. * smk_find_entry - find a label on the list, return the list entry
  380. * @string: a text string that might be a Smack label
  381. *
  382. * Returns a pointer to the entry in the label list that
  383. * matches the passed string or NULL if not found.
  384. */
  385. struct smack_known *smk_find_entry(const char *string)
  386. {
  387. unsigned int hash;
  388. struct hlist_head *head;
  389. struct smack_known *skp;
  390. hash = full_name_hash(NULL, string, strlen(string));
  391. head = &smack_known_hash[hash & (SMACK_HASH_SLOTS - 1)];
  392. hlist_for_each_entry_rcu(skp, head, smk_hashed)
  393. if (strcmp(skp->smk_known, string) == 0)
  394. return skp;
  395. return NULL;
  396. }
  397. /**
  398. * smk_parse_smack - parse smack label from a text string
  399. * @string: a text string that might contain a Smack label
  400. * @len: the maximum size, or zero if it is NULL terminated.
  401. *
  402. * Returns a pointer to the clean label or an error code.
  403. */
  404. char *smk_parse_smack(const char *string, int len)
  405. {
  406. char *smack;
  407. int i;
  408. if (len <= 0)
  409. len = strlen(string) + 1;
  410. /*
  411. * Reserve a leading '-' as an indicator that
  412. * this isn't a label, but an option to interfaces
  413. * including /smack/cipso and /smack/cipso2
  414. */
  415. if (string[0] == '-')
  416. return ERR_PTR(-EINVAL);
  417. for (i = 0; i < len; i++)
  418. if (string[i] > '~' || string[i] <= ' ' || string[i] == '/' ||
  419. string[i] == '"' || string[i] == '\\' || string[i] == '\'')
  420. break;
  421. if (i == 0 || i >= SMK_LONGLABEL)
  422. return ERR_PTR(-EINVAL);
  423. smack = kzalloc(i + 1, GFP_NOFS);
  424. if (smack == NULL)
  425. return ERR_PTR(-ENOMEM);
  426. strncpy(smack, string, i);
  427. return smack;
  428. }
  429. /**
  430. * smk_netlbl_mls - convert a catset to netlabel mls categories
  431. * @catset: the Smack categories
  432. * @sap: where to put the netlabel categories
  433. *
  434. * Allocates and fills attr.mls
  435. * Returns 0 on success, error code on failure.
  436. */
  437. int smk_netlbl_mls(int level, char *catset, struct netlbl_lsm_secattr *sap,
  438. int len)
  439. {
  440. unsigned char *cp;
  441. unsigned char m;
  442. int cat;
  443. int rc;
  444. int byte;
  445. sap->flags |= NETLBL_SECATTR_MLS_CAT;
  446. sap->attr.mls.lvl = level;
  447. sap->attr.mls.cat = NULL;
  448. for (cat = 1, cp = catset, byte = 0; byte < len; cp++, byte++)
  449. for (m = 0x80; m != 0; m >>= 1, cat++) {
  450. if ((m & *cp) == 0)
  451. continue;
  452. rc = netlbl_catmap_setbit(&sap->attr.mls.cat,
  453. cat, GFP_NOFS);
  454. if (rc < 0) {
  455. netlbl_catmap_free(sap->attr.mls.cat);
  456. return rc;
  457. }
  458. }
  459. return 0;
  460. }
  461. /**
  462. * smk_import_entry - import a label, return the list entry
  463. * @string: a text string that might be a Smack label
  464. * @len: the maximum size, or zero if it is NULL terminated.
  465. *
  466. * Returns a pointer to the entry in the label list that
  467. * matches the passed string, adding it if necessary,
  468. * or an error code.
  469. */
  470. struct smack_known *smk_import_entry(const char *string, int len)
  471. {
  472. struct smack_known *skp;
  473. char *smack;
  474. int slen;
  475. int rc;
  476. smack = smk_parse_smack(string, len);
  477. if (IS_ERR(smack))
  478. return ERR_CAST(smack);
  479. mutex_lock(&smack_known_lock);
  480. skp = smk_find_entry(smack);
  481. if (skp != NULL)
  482. goto freeout;
  483. skp = kzalloc(sizeof(*skp), GFP_NOFS);
  484. if (skp == NULL) {
  485. skp = ERR_PTR(-ENOMEM);
  486. goto freeout;
  487. }
  488. skp->smk_known = smack;
  489. skp->smk_secid = smack_next_secid++;
  490. skp->smk_netlabel.domain = skp->smk_known;
  491. skp->smk_netlabel.flags =
  492. NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
  493. /*
  494. * If direct labeling works use it.
  495. * Otherwise use mapped labeling.
  496. */
  497. slen = strlen(smack);
  498. if (slen < SMK_CIPSOLEN)
  499. rc = smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
  500. &skp->smk_netlabel, slen);
  501. else
  502. rc = smk_netlbl_mls(smack_cipso_mapped, (char *)&skp->smk_secid,
  503. &skp->smk_netlabel, sizeof(skp->smk_secid));
  504. if (rc >= 0) {
  505. INIT_LIST_HEAD(&skp->smk_rules);
  506. mutex_init(&skp->smk_rules_lock);
  507. /*
  508. * Make sure that the entry is actually
  509. * filled before putting it on the list.
  510. */
  511. smk_insert_entry(skp);
  512. goto unlockout;
  513. }
  514. /*
  515. * smk_netlbl_mls failed.
  516. */
  517. kfree(skp);
  518. skp = ERR_PTR(rc);
  519. freeout:
  520. kfree(smack);
  521. unlockout:
  522. mutex_unlock(&smack_known_lock);
  523. return skp;
  524. }
  525. /**
  526. * smack_from_secid - find the Smack label associated with a secid
  527. * @secid: an integer that might be associated with a Smack label
  528. *
  529. * Returns a pointer to the appropriate Smack label entry if there is one,
  530. * otherwise a pointer to the invalid Smack label.
  531. */
  532. struct smack_known *smack_from_secid(const u32 secid)
  533. {
  534. struct smack_known *skp;
  535. rcu_read_lock();
  536. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  537. if (skp->smk_secid == secid) {
  538. rcu_read_unlock();
  539. return skp;
  540. }
  541. }
  542. /*
  543. * If we got this far someone asked for the translation
  544. * of a secid that is not on the list.
  545. */
  546. rcu_read_unlock();
  547. return &smack_known_huh;
  548. }
  549. /*
  550. * Unless a process is running with one of these labels
  551. * even having CAP_MAC_OVERRIDE isn't enough to grant
  552. * privilege to violate MAC policy. If no labels are
  553. * designated (the empty list case) capabilities apply to
  554. * everyone.
  555. */
  556. LIST_HEAD(smack_onlycap_list);
  557. DEFINE_MUTEX(smack_onlycap_lock);
  558. /*
  559. * Is the task privileged and allowed to be privileged
  560. * by the onlycap rule.
  561. *
  562. * Returns true if the task is allowed to be privileged, false if it's not.
  563. */
  564. bool smack_privileged(int cap)
  565. {
  566. struct smack_known *skp = smk_of_current();
  567. struct smack_known_list_elem *sklep;
  568. int rc;
  569. /*
  570. * All kernel tasks are privileged
  571. */
  572. if (unlikely(current->flags & PF_KTHREAD))
  573. return true;
  574. rc = cap_capable(current_cred(), &init_user_ns, cap,
  575. SECURITY_CAP_AUDIT);
  576. if (rc)
  577. return false;
  578. rcu_read_lock();
  579. if (list_empty(&smack_onlycap_list)) {
  580. rcu_read_unlock();
  581. return true;
  582. }
  583. list_for_each_entry_rcu(sklep, &smack_onlycap_list, list) {
  584. if (sklep->smk_label == skp) {
  585. rcu_read_unlock();
  586. return true;
  587. }
  588. }
  589. rcu_read_unlock();
  590. return false;
  591. }