evm_main.c 13 KB

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