smack_access.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
  4. *
  5. * Author:
  6. * Casey Schaufler <casey@schaufler-ca.com>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/slab.h>
  10. #include <linux/fs.h>
  11. #include <linux/sched.h>
  12. #include "smack.h"
  13. struct smack_known smack_known_huh = {
  14. .smk_known = "?",
  15. .smk_secid = 2,
  16. };
  17. struct smack_known smack_known_hat = {
  18. .smk_known = "^",
  19. .smk_secid = 3,
  20. };
  21. struct smack_known smack_known_star = {
  22. .smk_known = "*",
  23. .smk_secid = 4,
  24. };
  25. struct smack_known smack_known_floor = {
  26. .smk_known = "_",
  27. .smk_secid = 5,
  28. };
  29. struct smack_known smack_known_web = {
  30. .smk_known = "@",
  31. .smk_secid = 7,
  32. };
  33. LIST_HEAD(smack_known_list);
  34. /*
  35. * The initial value needs to be bigger than any of the
  36. * known values above.
  37. */
  38. static u32 smack_next_secid = 10;
  39. /*
  40. * what events do we log
  41. * can be overwritten at run-time by /smack/logging
  42. */
  43. int log_policy = SMACK_AUDIT_DENIED;
  44. /**
  45. * smk_access_entry - look up matching access rule
  46. * @subject_label: a pointer to the subject's Smack label
  47. * @object_label: a pointer to the object's Smack label
  48. * @rule_list: the list of rules to search
  49. *
  50. * This function looks up the subject/object pair in the
  51. * access rule list and returns the access mode. If no
  52. * entry is found returns -ENOENT.
  53. *
  54. * NOTE:
  55. *
  56. * Earlier versions of this function allowed for labels that
  57. * were not on the label list. This was done to allow for
  58. * labels to come over the network that had never been seen
  59. * before on this host. Unless the receiving socket has the
  60. * star label this will always result in a failure check. The
  61. * star labeled socket case is now handled in the networking
  62. * hooks so there is no case where the label is not on the
  63. * label list. Checking to see if the address of two labels
  64. * is the same is now a reliable test.
  65. *
  66. * Do the object check first because that is more
  67. * likely to differ.
  68. *
  69. * Allowing write access implies allowing locking.
  70. */
  71. int smk_access_entry(char *subject_label, char *object_label,
  72. struct list_head *rule_list)
  73. {
  74. struct smack_rule *srp;
  75. list_for_each_entry_rcu(srp, rule_list, list) {
  76. if (srp->smk_object->smk_known == object_label &&
  77. srp->smk_subject->smk_known == subject_label) {
  78. int may = srp->smk_access;
  79. /*
  80. * MAY_WRITE implies MAY_LOCK.
  81. */
  82. if ((may & MAY_WRITE) == MAY_WRITE)
  83. may |= MAY_LOCK;
  84. return may;
  85. }
  86. }
  87. return -ENOENT;
  88. }
  89. /**
  90. * smk_access - determine if a subject has a specific access to an object
  91. * @subject: a pointer to the subject's Smack label entry
  92. * @object: a pointer to the object's Smack label entry
  93. * @request: the access requested, in "MAY" format
  94. * @a : a pointer to the audit data
  95. *
  96. * This function looks up the subject/object pair in the
  97. * access rule list and returns 0 if the access is permitted,
  98. * non zero otherwise.
  99. *
  100. * Smack labels are shared on smack_list
  101. */
  102. int smk_access(struct smack_known *subject, struct smack_known *object,
  103. int request, struct smk_audit_info *a)
  104. {
  105. int may = MAY_NOT;
  106. int rc = 0;
  107. /*
  108. * Hardcoded comparisons.
  109. */
  110. /*
  111. * A star subject can't access any object.
  112. */
  113. if (subject == &smack_known_star) {
  114. rc = -EACCES;
  115. goto out_audit;
  116. }
  117. /*
  118. * An internet object can be accessed by any subject.
  119. * Tasks cannot be assigned the internet label.
  120. * An internet subject can access any object.
  121. */
  122. if (object == &smack_known_web || subject == &smack_known_web)
  123. goto out_audit;
  124. /*
  125. * A star object can be accessed by any subject.
  126. */
  127. if (object == &smack_known_star)
  128. goto out_audit;
  129. /*
  130. * An object can be accessed in any way by a subject
  131. * with the same label.
  132. */
  133. if (subject->smk_known == object->smk_known)
  134. goto out_audit;
  135. /*
  136. * A hat subject can read or lock any object.
  137. * A floor object can be read or locked by any subject.
  138. */
  139. if ((request & MAY_ANYREAD) == request ||
  140. (request & MAY_LOCK) == request) {
  141. if (object == &smack_known_floor)
  142. goto out_audit;
  143. if (subject == &smack_known_hat)
  144. goto out_audit;
  145. }
  146. /*
  147. * Beyond here an explicit relationship is required.
  148. * If the requested access is contained in the available
  149. * access (e.g. read is included in readwrite) it's
  150. * good. A negative response from smk_access_entry()
  151. * indicates there is no entry for this pair.
  152. */
  153. rcu_read_lock();
  154. may = smk_access_entry(subject->smk_known, object->smk_known,
  155. &subject->smk_rules);
  156. rcu_read_unlock();
  157. if (may <= 0 || (request & may) != request) {
  158. rc = -EACCES;
  159. goto out_audit;
  160. }
  161. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  162. /*
  163. * Return a positive value if using bringup mode.
  164. * This allows the hooks to identify checks that
  165. * succeed because of "b" rules.
  166. */
  167. if (may & MAY_BRINGUP)
  168. rc = SMACK_BRINGUP_ALLOW;
  169. #endif
  170. out_audit:
  171. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  172. if (rc < 0) {
  173. if (object == smack_unconfined)
  174. rc = SMACK_UNCONFINED_OBJECT;
  175. if (subject == smack_unconfined)
  176. rc = SMACK_UNCONFINED_SUBJECT;
  177. }
  178. #endif
  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 = smack_cred(current_cred());
  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. * @ad: 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. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  315. char request_buffer[SMK_NUM_ACCESS_TYPE + 5];
  316. #else
  317. char request_buffer[SMK_NUM_ACCESS_TYPE + 1];
  318. #endif
  319. struct smack_audit_data *sad;
  320. struct common_audit_data *a = &ad->a;
  321. /* check if we have to log the current event */
  322. if (result < 0 && (log_policy & SMACK_AUDIT_DENIED) == 0)
  323. return;
  324. if (result == 0 && (log_policy & SMACK_AUDIT_ACCEPT) == 0)
  325. return;
  326. sad = a->smack_audit_data;
  327. if (sad->function == NULL)
  328. sad->function = "unknown";
  329. /* end preparing the audit data */
  330. smack_str_from_perm(request_buffer, request);
  331. sad->subject = subject_label;
  332. sad->object = object_label;
  333. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  334. /*
  335. * The result may be positive in bringup mode.
  336. * A positive result is an allow, but not for normal reasons.
  337. * Mark it as successful, but don't filter it out even if
  338. * the logging policy says to do so.
  339. */
  340. if (result == SMACK_UNCONFINED_SUBJECT)
  341. strcat(request_buffer, "(US)");
  342. else if (result == SMACK_UNCONFINED_OBJECT)
  343. strcat(request_buffer, "(UO)");
  344. if (result > 0)
  345. result = 0;
  346. #endif
  347. sad->request = request_buffer;
  348. sad->result = result;
  349. common_lsm_audit(a, smack_log_callback, NULL);
  350. }
  351. #else /* #ifdef CONFIG_AUDIT */
  352. void smack_log(char *subject_label, char *object_label, int request,
  353. int result, struct smk_audit_info *ad)
  354. {
  355. }
  356. #endif
  357. DEFINE_MUTEX(smack_known_lock);
  358. struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
  359. /**
  360. * smk_insert_entry - insert a smack label into a hash map,
  361. * @skp: smack label
  362. *
  363. * this function must be called under smack_known_lock
  364. */
  365. void smk_insert_entry(struct smack_known *skp)
  366. {
  367. unsigned int hash;
  368. struct hlist_head *head;
  369. hash = full_name_hash(NULL, skp->smk_known, strlen(skp->smk_known));
  370. head = &smack_known_hash[hash & (SMACK_HASH_SLOTS - 1)];
  371. hlist_add_head_rcu(&skp->smk_hashed, head);
  372. list_add_rcu(&skp->list, &smack_known_list);
  373. }
  374. /**
  375. * smk_find_entry - find a label on the list, return the list entry
  376. * @string: a text string that might be a Smack label
  377. *
  378. * Returns a pointer to the entry in the label list that
  379. * matches the passed string or NULL if not found.
  380. */
  381. struct smack_known *smk_find_entry(const char *string)
  382. {
  383. unsigned int hash;
  384. struct hlist_head *head;
  385. struct smack_known *skp;
  386. hash = full_name_hash(NULL, string, strlen(string));
  387. head = &smack_known_hash[hash & (SMACK_HASH_SLOTS - 1)];
  388. hlist_for_each_entry_rcu(skp, head, smk_hashed)
  389. if (strcmp(skp->smk_known, string) == 0)
  390. return skp;
  391. return NULL;
  392. }
  393. /**
  394. * smk_parse_smack - parse smack label from a text string
  395. * @string: a text string that might contain a Smack label
  396. * @len: the maximum size, or zero if it is NULL terminated.
  397. *
  398. * Returns a pointer to the clean label or an error code.
  399. */
  400. char *smk_parse_smack(const char *string, int len)
  401. {
  402. char *smack;
  403. int i;
  404. if (len <= 0)
  405. len = strlen(string) + 1;
  406. /*
  407. * Reserve a leading '-' as an indicator that
  408. * this isn't a label, but an option to interfaces
  409. * including /smack/cipso and /smack/cipso2
  410. */
  411. if (string[0] == '-')
  412. return ERR_PTR(-EINVAL);
  413. for (i = 0; i < len; i++)
  414. if (string[i] > '~' || string[i] <= ' ' || string[i] == '/' ||
  415. string[i] == '"' || string[i] == '\\' || string[i] == '\'')
  416. break;
  417. if (i == 0 || i >= SMK_LONGLABEL)
  418. return ERR_PTR(-EINVAL);
  419. smack = kstrndup(string, i, GFP_NOFS);
  420. if (!smack)
  421. return ERR_PTR(-ENOMEM);
  422. return smack;
  423. }
  424. /**
  425. * smk_netlbl_mls - convert a catset to netlabel mls categories
  426. * @level: MLS sensitivity level
  427. * @catset: the Smack categories
  428. * @sap: where to put the netlabel categories
  429. * @len: number of bytes for the levels in a CIPSO IP option
  430. *
  431. * Allocates and fills attr.mls
  432. * Returns 0 on success, error code on failure.
  433. */
  434. int smk_netlbl_mls(int level, char *catset, struct netlbl_lsm_secattr *sap,
  435. int len)
  436. {
  437. unsigned char *cp;
  438. unsigned char m;
  439. int cat;
  440. int rc;
  441. int byte;
  442. sap->flags |= NETLBL_SECATTR_MLS_CAT;
  443. sap->attr.mls.lvl = level;
  444. sap->attr.mls.cat = NULL;
  445. for (cat = 1, cp = catset, byte = 0; byte < len; cp++, byte++)
  446. for (m = 0x80; m != 0; m >>= 1, cat++) {
  447. if ((m & *cp) == 0)
  448. continue;
  449. rc = netlbl_catmap_setbit(&sap->attr.mls.cat,
  450. cat, GFP_NOFS);
  451. if (rc < 0) {
  452. netlbl_catmap_free(sap->attr.mls.cat);
  453. return rc;
  454. }
  455. }
  456. return 0;
  457. }
  458. /**
  459. * smack_populate_secattr - fill in the smack_known netlabel information
  460. * @skp: pointer to the structure to fill
  461. *
  462. * Populate the netlabel secattr structure for a Smack label.
  463. *
  464. * Returns 0 unless creating the category mapping fails
  465. */
  466. int smack_populate_secattr(struct smack_known *skp)
  467. {
  468. int slen;
  469. skp->smk_netlabel.attr.secid = skp->smk_secid;
  470. skp->smk_netlabel.domain = skp->smk_known;
  471. skp->smk_netlabel.cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
  472. if (skp->smk_netlabel.cache != NULL) {
  473. skp->smk_netlabel.flags |= NETLBL_SECATTR_CACHE;
  474. skp->smk_netlabel.cache->free = NULL;
  475. skp->smk_netlabel.cache->data = skp;
  476. }
  477. skp->smk_netlabel.flags |= NETLBL_SECATTR_SECID |
  478. NETLBL_SECATTR_MLS_LVL |
  479. NETLBL_SECATTR_DOMAIN;
  480. /*
  481. * If direct labeling works use it.
  482. * Otherwise use mapped labeling.
  483. */
  484. slen = strlen(skp->smk_known);
  485. if (slen < SMK_CIPSOLEN)
  486. return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
  487. &skp->smk_netlabel, slen);
  488. return smk_netlbl_mls(smack_cipso_mapped, (char *)&skp->smk_secid,
  489. &skp->smk_netlabel, sizeof(skp->smk_secid));
  490. }
  491. /**
  492. * smk_import_entry - import a label, return the list entry
  493. * @string: a text string that might be a Smack label
  494. * @len: the maximum size, or zero if it is NULL terminated.
  495. *
  496. * Returns a pointer to the entry in the label list that
  497. * matches the passed string, adding it if necessary,
  498. * or an error code.
  499. */
  500. struct smack_known *smk_import_entry(const char *string, int len)
  501. {
  502. struct smack_known *skp;
  503. char *smack;
  504. int rc;
  505. smack = smk_parse_smack(string, len);
  506. if (IS_ERR(smack))
  507. return ERR_CAST(smack);
  508. mutex_lock(&smack_known_lock);
  509. skp = smk_find_entry(smack);
  510. if (skp != NULL)
  511. goto freeout;
  512. skp = kzalloc(sizeof(*skp), GFP_NOFS);
  513. if (skp == NULL) {
  514. skp = ERR_PTR(-ENOMEM);
  515. goto freeout;
  516. }
  517. skp->smk_known = smack;
  518. skp->smk_secid = smack_next_secid++;
  519. rc = smack_populate_secattr(skp);
  520. if (rc >= 0) {
  521. INIT_LIST_HEAD(&skp->smk_rules);
  522. mutex_init(&skp->smk_rules_lock);
  523. /*
  524. * Make sure that the entry is actually
  525. * filled before putting it on the list.
  526. */
  527. smk_insert_entry(skp);
  528. goto unlockout;
  529. }
  530. kfree(skp);
  531. skp = ERR_PTR(rc);
  532. freeout:
  533. kfree(smack);
  534. unlockout:
  535. mutex_unlock(&smack_known_lock);
  536. return skp;
  537. }
  538. /**
  539. * smack_from_secid - find the Smack label associated with a secid
  540. * @secid: an integer that might be associated with a Smack label
  541. *
  542. * Returns a pointer to the appropriate Smack label entry if there is one,
  543. * otherwise a pointer to the invalid Smack label.
  544. */
  545. struct smack_known *smack_from_secid(const u32 secid)
  546. {
  547. struct smack_known *skp;
  548. rcu_read_lock();
  549. list_for_each_entry_rcu(skp, &smack_known_list, list) {
  550. if (skp->smk_secid == secid) {
  551. rcu_read_unlock();
  552. return skp;
  553. }
  554. }
  555. /*
  556. * If we got this far someone asked for the translation
  557. * of a secid that is not on the list.
  558. */
  559. rcu_read_unlock();
  560. return &smack_known_huh;
  561. }
  562. /*
  563. * Unless a process is running with one of these labels
  564. * even having CAP_MAC_OVERRIDE isn't enough to grant
  565. * privilege to violate MAC policy. If no labels are
  566. * designated (the empty list case) capabilities apply to
  567. * everyone.
  568. */
  569. LIST_HEAD(smack_onlycap_list);
  570. DEFINE_MUTEX(smack_onlycap_lock);
  571. /**
  572. * smack_privileged_cred - are all privilege requirements met by cred
  573. * @cap: The requested capability
  574. * @cred: the credential to use
  575. *
  576. * Is the task privileged and allowed to be privileged
  577. * by the onlycap rule.
  578. *
  579. * Returns true if the task is allowed to be privileged, false if it's not.
  580. */
  581. bool smack_privileged_cred(int cap, const struct cred *cred)
  582. {
  583. struct task_smack *tsp = smack_cred(cred);
  584. struct smack_known *skp = tsp->smk_task;
  585. struct smack_known_list_elem *sklep;
  586. int rc;
  587. rc = cap_capable(cred, &init_user_ns, cap, CAP_OPT_NONE);
  588. if (rc)
  589. return false;
  590. rcu_read_lock();
  591. if (list_empty(&smack_onlycap_list)) {
  592. rcu_read_unlock();
  593. return true;
  594. }
  595. list_for_each_entry_rcu(sklep, &smack_onlycap_list, list) {
  596. if (sklep->smk_label == skp) {
  597. rcu_read_unlock();
  598. return true;
  599. }
  600. }
  601. rcu_read_unlock();
  602. return false;
  603. }
  604. /**
  605. * smack_privileged - are all privilege requirements met
  606. * @cap: The requested capability
  607. *
  608. * Is the task privileged and allowed to be privileged
  609. * by the onlycap rule.
  610. *
  611. * Returns true if the task is allowed to be privileged, false if it's not.
  612. */
  613. bool smack_privileged(int cap)
  614. {
  615. /*
  616. * All kernel tasks are privileged
  617. */
  618. if (unlikely(current->flags & PF_KTHREAD))
  619. return true;
  620. return smack_privileged_cred(cap, current_cred());
  621. }