evm_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Copyright (C) 2005-2010 IBM Corporation
  3. *
  4. * Author:
  5. * Mimi Zohar <zohar@us.ibm.com>
  6. * Kylene Hall <kjhall@us.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, version 2 of the License.
  11. *
  12. * File: evm_main.c
  13. * implements evm_inode_setxattr, evm_inode_post_setxattr,
  14. * evm_inode_removexattr, and evm_verifyxattr
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/module.h>
  18. #include <linux/crypto.h>
  19. #include <linux/audit.h>
  20. #include <linux/xattr.h>
  21. #include <linux/integrity.h>
  22. #include <linux/evm.h>
  23. #include <crypto/hash.h>
  24. #include "evm.h"
  25. int evm_initialized;
  26. static char *integrity_status_msg[] = {
  27. "pass", "fail", "no_label", "no_xattrs", "unknown"
  28. };
  29. char *evm_hmac = "hmac(sha1)";
  30. char *evm_hash = "sha1";
  31. int evm_hmac_version = CONFIG_EVM_HMAC_VERSION;
  32. char *evm_config_xattrnames[] = {
  33. #ifdef CONFIG_SECURITY_SELINUX
  34. XATTR_NAME_SELINUX,
  35. #endif
  36. #ifdef CONFIG_SECURITY_SMACK
  37. XATTR_NAME_SMACK,
  38. #endif
  39. #ifdef CONFIG_IMA_APPRAISE
  40. XATTR_NAME_IMA,
  41. #endif
  42. XATTR_NAME_CAPS,
  43. NULL
  44. };
  45. static int evm_fixmode;
  46. static int __init evm_set_fixmode(char *str)
  47. {
  48. if (strncmp(str, "fix", 3) == 0)
  49. evm_fixmode = 1;
  50. return 0;
  51. }
  52. __setup("evm=", evm_set_fixmode);
  53. static int evm_find_protected_xattrs(struct dentry *dentry)
  54. {
  55. struct inode *inode = dentry->d_inode;
  56. char **xattr;
  57. int error;
  58. int count = 0;
  59. if (!inode->i_op->getxattr)
  60. return -EOPNOTSUPP;
  61. for (xattr = evm_config_xattrnames; *xattr != NULL; xattr++) {
  62. error = inode->i_op->getxattr(dentry, *xattr, NULL, 0);
  63. if (error < 0) {
  64. if (error == -ENODATA)
  65. continue;
  66. return error;
  67. }
  68. count++;
  69. }
  70. return count;
  71. }
  72. /*
  73. * evm_verify_hmac - calculate and compare the HMAC with the EVM xattr
  74. *
  75. * Compute the HMAC on the dentry's protected set of extended attributes
  76. * and compare it against the stored security.evm xattr.
  77. *
  78. * For performance:
  79. * - use the previoulsy retrieved xattr value and length to calculate the
  80. * HMAC.)
  81. * - cache the verification result in the iint, when available.
  82. *
  83. * Returns integrity status
  84. */
  85. static enum integrity_status evm_verify_hmac(struct dentry *dentry,
  86. const char *xattr_name,
  87. char *xattr_value,
  88. size_t xattr_value_len,
  89. struct integrity_iint_cache *iint)
  90. {
  91. struct evm_ima_xattr_data *xattr_data = NULL;
  92. struct evm_ima_xattr_data calc;
  93. enum integrity_status evm_status = INTEGRITY_PASS;
  94. int rc, xattr_len;
  95. if (iint && iint->evm_status == INTEGRITY_PASS)
  96. return iint->evm_status;
  97. /* if status is not PASS, try to check again - against -ENOMEM */
  98. /* first need to know the sig type */
  99. rc = vfs_getxattr_alloc(dentry, XATTR_NAME_EVM, (char **)&xattr_data, 0,
  100. GFP_NOFS);
  101. if (rc <= 0) {
  102. if (rc == 0)
  103. evm_status = INTEGRITY_FAIL; /* empty */
  104. else if (rc == -ENODATA) {
  105. rc = evm_find_protected_xattrs(dentry);
  106. if (rc > 0)
  107. evm_status = INTEGRITY_NOLABEL;
  108. else if (rc == 0)
  109. evm_status = INTEGRITY_NOXATTRS; /* new file */
  110. }
  111. goto out;
  112. }
  113. xattr_len = rc;
  114. /* check value type */
  115. switch (xattr_data->type) {
  116. case EVM_XATTR_HMAC:
  117. rc = evm_calc_hmac(dentry, xattr_name, xattr_value,
  118. xattr_value_len, calc.digest);
  119. if (rc)
  120. break;
  121. rc = memcmp(xattr_data->digest, calc.digest,
  122. sizeof(calc.digest));
  123. if (rc)
  124. rc = -EINVAL;
  125. break;
  126. case EVM_IMA_XATTR_DIGSIG:
  127. rc = evm_calc_hash(dentry, xattr_name, xattr_value,
  128. xattr_value_len, calc.digest);
  129. if (rc)
  130. break;
  131. rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
  132. (const char *)xattr_data, xattr_len,
  133. calc.digest, sizeof(calc.digest));
  134. if (!rc) {
  135. /* we probably want to replace rsa with hmac here */
  136. evm_update_evmxattr(dentry, xattr_name, xattr_value,
  137. xattr_value_len);
  138. }
  139. break;
  140. default:
  141. rc = -EINVAL;
  142. break;
  143. }
  144. if (rc)
  145. evm_status = (rc == -ENODATA) ?
  146. INTEGRITY_NOXATTRS : INTEGRITY_FAIL;
  147. out:
  148. if (iint)
  149. iint->evm_status = evm_status;
  150. kfree(xattr_data);
  151. return evm_status;
  152. }
  153. static int evm_protected_xattr(const char *req_xattr_name)
  154. {
  155. char **xattrname;
  156. int namelen;
  157. int found = 0;
  158. namelen = strlen(req_xattr_name);
  159. for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) {
  160. if ((strlen(*xattrname) == namelen)
  161. && (strncmp(req_xattr_name, *xattrname, namelen) == 0)) {
  162. found = 1;
  163. break;
  164. }
  165. if (strncmp(req_xattr_name,
  166. *xattrname + XATTR_SECURITY_PREFIX_LEN,
  167. strlen(req_xattr_name)) == 0) {
  168. found = 1;
  169. break;
  170. }
  171. }
  172. return found;
  173. }
  174. /**
  175. * evm_verifyxattr - verify the integrity of the requested xattr
  176. * @dentry: object of the verify xattr
  177. * @xattr_name: requested xattr
  178. * @xattr_value: requested xattr value
  179. * @xattr_value_len: requested xattr value length
  180. *
  181. * Calculate the HMAC for the given dentry and verify it against the stored
  182. * security.evm xattr. For performance, use the xattr value and length
  183. * previously retrieved to calculate the HMAC.
  184. *
  185. * Returns the xattr integrity status.
  186. *
  187. * This function requires the caller to lock the inode's i_mutex before it
  188. * is executed.
  189. */
  190. enum integrity_status evm_verifyxattr(struct dentry *dentry,
  191. const char *xattr_name,
  192. void *xattr_value, size_t xattr_value_len,
  193. struct integrity_iint_cache *iint)
  194. {
  195. if (!evm_initialized || !evm_protected_xattr(xattr_name))
  196. return INTEGRITY_UNKNOWN;
  197. if (!iint) {
  198. iint = integrity_iint_find(dentry->d_inode);
  199. if (!iint)
  200. return INTEGRITY_UNKNOWN;
  201. }
  202. return evm_verify_hmac(dentry, xattr_name, xattr_value,
  203. xattr_value_len, iint);
  204. }
  205. EXPORT_SYMBOL_GPL(evm_verifyxattr);
  206. /*
  207. * evm_verify_current_integrity - verify the dentry's metadata integrity
  208. * @dentry: pointer to the affected dentry
  209. *
  210. * Verify and return the dentry's metadata integrity. The exceptions are
  211. * before EVM is initialized or in 'fix' mode.
  212. */
  213. static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
  214. {
  215. struct inode *inode = dentry->d_inode;
  216. if (!evm_initialized || !S_ISREG(inode->i_mode) || evm_fixmode)
  217. return 0;
  218. return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
  219. }
  220. /*
  221. * evm_protect_xattr - protect the EVM extended attribute
  222. *
  223. * Prevent security.evm from being modified or removed without the
  224. * necessary permissions or when the existing value is invalid.
  225. *
  226. * The posix xattr acls are 'system' prefixed, which normally would not
  227. * affect security.evm. An interesting side affect of writing posix xattr
  228. * acls is their modifying of the i_mode, which is included in security.evm.
  229. * For posix xattr acls only, permit security.evm, even if it currently
  230. * doesn't exist, to be updated.
  231. */
  232. static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name,
  233. const void *xattr_value, size_t xattr_value_len)
  234. {
  235. enum integrity_status evm_status;
  236. if (strcmp(xattr_name, XATTR_NAME_EVM) == 0) {
  237. if (!capable(CAP_SYS_ADMIN))
  238. return -EPERM;
  239. } else if (!evm_protected_xattr(xattr_name)) {
  240. if (!posix_xattr_acl(xattr_name))
  241. return 0;
  242. evm_status = evm_verify_current_integrity(dentry);
  243. if ((evm_status == INTEGRITY_PASS) ||
  244. (evm_status == INTEGRITY_NOXATTRS))
  245. return 0;
  246. goto out;
  247. }
  248. evm_status = evm_verify_current_integrity(dentry);
  249. out:
  250. if (evm_status != INTEGRITY_PASS)
  251. integrity_audit_msg(AUDIT_INTEGRITY_METADATA, dentry->d_inode,
  252. dentry->d_name.name, "appraise_metadata",
  253. integrity_status_msg[evm_status],
  254. -EPERM, 0);
  255. return evm_status == INTEGRITY_PASS ? 0 : -EPERM;
  256. }
  257. /**
  258. * evm_inode_setxattr - protect the EVM extended attribute
  259. * @dentry: pointer to the affected dentry
  260. * @xattr_name: pointer to the affected extended attribute name
  261. * @xattr_value: pointer to the new extended attribute value
  262. * @xattr_value_len: pointer to the new extended attribute value length
  263. *
  264. * Updating 'security.evm' requires CAP_SYS_ADMIN privileges and that
  265. * the current value is valid.
  266. */
  267. int evm_inode_setxattr(struct dentry *dentry, const char *xattr_name,
  268. const void *xattr_value, size_t xattr_value_len)
  269. {
  270. return evm_protect_xattr(dentry, xattr_name, xattr_value,
  271. xattr_value_len);
  272. }
  273. /**
  274. * evm_inode_removexattr - protect the EVM extended attribute
  275. * @dentry: pointer to the affected dentry
  276. * @xattr_name: pointer to the affected extended attribute name
  277. *
  278. * Removing 'security.evm' requires CAP_SYS_ADMIN privileges and that
  279. * the current value is valid.
  280. */
  281. int evm_inode_removexattr(struct dentry *dentry, const char *xattr_name)
  282. {
  283. return evm_protect_xattr(dentry, xattr_name, NULL, 0);
  284. }
  285. /**
  286. * evm_inode_post_setxattr - update 'security.evm' to reflect the changes
  287. * @dentry: pointer to the affected dentry
  288. * @xattr_name: pointer to the affected extended attribute name
  289. * @xattr_value: pointer to the new extended attribute value
  290. * @xattr_value_len: pointer to the new extended attribute value length
  291. *
  292. * Update the HMAC stored in 'security.evm' to reflect the change.
  293. *
  294. * No need to take the i_mutex lock here, as this function is called from
  295. * __vfs_setxattr_noperm(). The caller of which has taken the inode's
  296. * i_mutex lock.
  297. */
  298. void evm_inode_post_setxattr(struct dentry *dentry, const char *xattr_name,
  299. const void *xattr_value, size_t xattr_value_len)
  300. {
  301. if (!evm_initialized || (!evm_protected_xattr(xattr_name)
  302. && !posix_xattr_acl(xattr_name)))
  303. return;
  304. evm_update_evmxattr(dentry, xattr_name, xattr_value, xattr_value_len);
  305. return;
  306. }
  307. /**
  308. * evm_inode_post_removexattr - update 'security.evm' after removing the xattr
  309. * @dentry: pointer to the affected dentry
  310. * @xattr_name: pointer to the affected extended attribute name
  311. *
  312. * Update the HMAC stored in 'security.evm' to reflect removal of the xattr.
  313. */
  314. void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name)
  315. {
  316. struct inode *inode = dentry->d_inode;
  317. if (!evm_initialized || !evm_protected_xattr(xattr_name))
  318. return;
  319. mutex_lock(&inode->i_mutex);
  320. evm_update_evmxattr(dentry, xattr_name, NULL, 0);
  321. mutex_unlock(&inode->i_mutex);
  322. return;
  323. }
  324. /**
  325. * evm_inode_setattr - prevent updating an invalid EVM extended attribute
  326. * @dentry: pointer to the affected dentry
  327. */
  328. int evm_inode_setattr(struct dentry *dentry, struct iattr *attr)
  329. {
  330. unsigned int ia_valid = attr->ia_valid;
  331. enum integrity_status evm_status;
  332. if (!(ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
  333. return 0;
  334. evm_status = evm_verify_current_integrity(dentry);
  335. if ((evm_status == INTEGRITY_PASS) ||
  336. (evm_status == INTEGRITY_NOXATTRS))
  337. return 0;
  338. integrity_audit_msg(AUDIT_INTEGRITY_METADATA, dentry->d_inode,
  339. dentry->d_name.name, "appraise_metadata",
  340. integrity_status_msg[evm_status], -EPERM, 0);
  341. return -EPERM;
  342. }
  343. /**
  344. * evm_inode_post_setattr - update 'security.evm' after modifying metadata
  345. * @dentry: pointer to the affected dentry
  346. * @ia_valid: for the UID and GID status
  347. *
  348. * For now, update the HMAC stored in 'security.evm' to reflect UID/GID
  349. * changes.
  350. *
  351. * This function is called from notify_change(), which expects the caller
  352. * to lock the inode's i_mutex.
  353. */
  354. void evm_inode_post_setattr(struct dentry *dentry, int ia_valid)
  355. {
  356. if (!evm_initialized)
  357. return;
  358. if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  359. evm_update_evmxattr(dentry, NULL, NULL, 0);
  360. return;
  361. }
  362. /*
  363. * evm_inode_init_security - initializes security.evm
  364. */
  365. int evm_inode_init_security(struct inode *inode,
  366. const struct xattr *lsm_xattr,
  367. struct xattr *evm_xattr)
  368. {
  369. struct evm_ima_xattr_data *xattr_data;
  370. int rc;
  371. if (!evm_initialized || !evm_protected_xattr(lsm_xattr->name))
  372. return 0;
  373. xattr_data = kzalloc(sizeof(*xattr_data), GFP_NOFS);
  374. if (!xattr_data)
  375. return -ENOMEM;
  376. xattr_data->type = EVM_XATTR_HMAC;
  377. rc = evm_init_hmac(inode, lsm_xattr, xattr_data->digest);
  378. if (rc < 0)
  379. goto out;
  380. evm_xattr->value = xattr_data;
  381. evm_xattr->value_len = sizeof(*xattr_data);
  382. evm_xattr->name = XATTR_EVM_SUFFIX;
  383. return 0;
  384. out:
  385. kfree(xattr_data);
  386. return rc;
  387. }
  388. EXPORT_SYMBOL_GPL(evm_inode_init_security);
  389. static int __init init_evm(void)
  390. {
  391. int error;
  392. error = evm_init_secfs();
  393. if (error < 0) {
  394. pr_info("Error registering secfs\n");
  395. goto err;
  396. }
  397. return 0;
  398. err:
  399. return error;
  400. }
  401. /*
  402. * evm_display_config - list the EVM protected security extended attributes
  403. */
  404. static int __init evm_display_config(void)
  405. {
  406. char **xattrname;
  407. for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++)
  408. pr_info("%s\n", *xattrname);
  409. return 0;
  410. }
  411. pure_initcall(evm_display_config);
  412. late_initcall(init_evm);
  413. MODULE_DESCRIPTION("Extended Verification Module");
  414. MODULE_LICENSE("GPL");