evm_main.c 13 KB

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