smack_access.c 16 KB

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