evm_main.c 12 KB

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