smack_access.c 19 KB

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