ima_appraise.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (C) 2011 IBM Corporation
  3. *
  4. * Author:
  5. * Mimi Zohar <zohar@us.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2 of the License.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/file.h>
  13. #include <linux/fs.h>
  14. #include <linux/xattr.h>
  15. #include <linux/magic.h>
  16. #include <linux/ima.h>
  17. #include <linux/evm.h>
  18. #include <crypto/hash_info.h>
  19. #include "ima.h"
  20. static int __init default_appraise_setup(char *str)
  21. {
  22. if (strncmp(str, "off", 3) == 0)
  23. ima_appraise = 0;
  24. else if (strncmp(str, "fix", 3) == 0)
  25. ima_appraise = IMA_APPRAISE_FIX;
  26. return 1;
  27. }
  28. __setup("ima_appraise=", default_appraise_setup);
  29. /*
  30. * ima_must_appraise - set appraise flag
  31. *
  32. * Return 1 to appraise
  33. */
  34. int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
  35. {
  36. if (!ima_appraise)
  37. return 0;
  38. return ima_match_policy(inode, func, mask, IMA_APPRAISE);
  39. }
  40. static int ima_fix_xattr(struct dentry *dentry,
  41. struct integrity_iint_cache *iint)
  42. {
  43. int rc, offset;
  44. u8 algo = iint->ima_hash->algo;
  45. if (algo <= HASH_ALGO_SHA1) {
  46. offset = 1;
  47. iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
  48. } else {
  49. offset = 0;
  50. iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
  51. iint->ima_hash->xattr.ng.algo = algo;
  52. }
  53. rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
  54. &iint->ima_hash->xattr.data[offset],
  55. (sizeof(iint->ima_hash->xattr) - offset) +
  56. iint->ima_hash->length, 0);
  57. return rc;
  58. }
  59. /* Return specific func appraised cached result */
  60. enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
  61. int func)
  62. {
  63. switch (func) {
  64. case MMAP_CHECK:
  65. return iint->ima_mmap_status;
  66. case BPRM_CHECK:
  67. return iint->ima_bprm_status;
  68. case MODULE_CHECK:
  69. return iint->ima_module_status;
  70. case FIRMWARE_CHECK:
  71. return iint->ima_firmware_status;
  72. case FILE_CHECK:
  73. default:
  74. return iint->ima_file_status;
  75. }
  76. }
  77. static void ima_set_cache_status(struct integrity_iint_cache *iint,
  78. int func, enum integrity_status status)
  79. {
  80. switch (func) {
  81. case MMAP_CHECK:
  82. iint->ima_mmap_status = status;
  83. break;
  84. case BPRM_CHECK:
  85. iint->ima_bprm_status = status;
  86. break;
  87. case MODULE_CHECK:
  88. iint->ima_module_status = status;
  89. break;
  90. case FIRMWARE_CHECK:
  91. iint->ima_firmware_status = status;
  92. break;
  93. case FILE_CHECK:
  94. default:
  95. iint->ima_file_status = status;
  96. break;
  97. }
  98. }
  99. static void ima_cache_flags(struct integrity_iint_cache *iint, int func)
  100. {
  101. switch (func) {
  102. case MMAP_CHECK:
  103. iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
  104. break;
  105. case BPRM_CHECK:
  106. iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
  107. break;
  108. case MODULE_CHECK:
  109. iint->flags |= (IMA_MODULE_APPRAISED | IMA_APPRAISED);
  110. break;
  111. case FIRMWARE_CHECK:
  112. iint->flags |= (IMA_FIRMWARE_APPRAISED | IMA_APPRAISED);
  113. break;
  114. case FILE_CHECK:
  115. default:
  116. iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
  117. break;
  118. }
  119. }
  120. void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len,
  121. struct ima_digest_data *hash)
  122. {
  123. struct signature_v2_hdr *sig;
  124. if (!xattr_value || xattr_len < 2)
  125. return;
  126. switch (xattr_value->type) {
  127. case EVM_IMA_XATTR_DIGSIG:
  128. sig = (typeof(sig))xattr_value;
  129. if (sig->version != 2 || xattr_len <= sizeof(*sig))
  130. return;
  131. hash->algo = sig->hash_algo;
  132. break;
  133. case IMA_XATTR_DIGEST_NG:
  134. hash->algo = xattr_value->digest[0];
  135. break;
  136. case IMA_XATTR_DIGEST:
  137. /* this is for backward compatibility */
  138. if (xattr_len == 21) {
  139. unsigned int zero = 0;
  140. if (!memcmp(&xattr_value->digest[16], &zero, 4))
  141. hash->algo = HASH_ALGO_MD5;
  142. else
  143. hash->algo = HASH_ALGO_SHA1;
  144. } else if (xattr_len == 17)
  145. hash->algo = HASH_ALGO_MD5;
  146. break;
  147. }
  148. }
  149. int ima_read_xattr(struct dentry *dentry,
  150. struct evm_ima_xattr_data **xattr_value)
  151. {
  152. struct inode *inode = dentry->d_inode;
  153. if (!inode->i_op->getxattr)
  154. return 0;
  155. return vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
  156. 0, GFP_NOFS);
  157. }
  158. /*
  159. * ima_appraise_measurement - appraise file measurement
  160. *
  161. * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
  162. * Assuming success, compare the xattr hash with the collected measurement.
  163. *
  164. * Return 0 on success, error code otherwise
  165. */
  166. int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
  167. struct file *file, const unsigned char *filename,
  168. struct evm_ima_xattr_data *xattr_value,
  169. int xattr_len)
  170. {
  171. static const char op[] = "appraise_data";
  172. char *cause = "unknown";
  173. struct dentry *dentry = file->f_dentry;
  174. struct inode *inode = dentry->d_inode;
  175. enum integrity_status status = INTEGRITY_UNKNOWN;
  176. int rc = xattr_len, hash_start = 0;
  177. if (!ima_appraise)
  178. return 0;
  179. if (!inode->i_op->getxattr)
  180. return INTEGRITY_UNKNOWN;
  181. if (rc <= 0) {
  182. if (rc && rc != -ENODATA)
  183. goto out;
  184. cause = "missing-hash";
  185. status =
  186. (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
  187. goto out;
  188. }
  189. status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
  190. if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
  191. if ((status == INTEGRITY_NOLABEL)
  192. || (status == INTEGRITY_NOXATTRS))
  193. cause = "missing-HMAC";
  194. else if (status == INTEGRITY_FAIL)
  195. cause = "invalid-HMAC";
  196. goto out;
  197. }
  198. switch (xattr_value->type) {
  199. case IMA_XATTR_DIGEST_NG:
  200. /* first byte contains algorithm id */
  201. hash_start = 1;
  202. case IMA_XATTR_DIGEST:
  203. if (iint->flags & IMA_DIGSIG_REQUIRED) {
  204. cause = "IMA-signature-required";
  205. status = INTEGRITY_FAIL;
  206. break;
  207. }
  208. if (xattr_len - sizeof(xattr_value->type) - hash_start >=
  209. iint->ima_hash->length)
  210. /* xattr length may be longer. md5 hash in previous
  211. version occupied 20 bytes in xattr, instead of 16
  212. */
  213. rc = memcmp(&xattr_value->digest[hash_start],
  214. iint->ima_hash->digest,
  215. iint->ima_hash->length);
  216. else
  217. rc = -EINVAL;
  218. if (rc) {
  219. cause = "invalid-hash";
  220. status = INTEGRITY_FAIL;
  221. break;
  222. }
  223. status = INTEGRITY_PASS;
  224. break;
  225. case EVM_IMA_XATTR_DIGSIG:
  226. iint->flags |= IMA_DIGSIG;
  227. rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
  228. (const char *)xattr_value, rc,
  229. iint->ima_hash->digest,
  230. iint->ima_hash->length);
  231. if (rc == -EOPNOTSUPP) {
  232. status = INTEGRITY_UNKNOWN;
  233. } else if (rc) {
  234. cause = "invalid-signature";
  235. status = INTEGRITY_FAIL;
  236. } else {
  237. status = INTEGRITY_PASS;
  238. }
  239. break;
  240. default:
  241. status = INTEGRITY_UNKNOWN;
  242. cause = "unknown-ima-data";
  243. break;
  244. }
  245. out:
  246. if (status != INTEGRITY_PASS) {
  247. if ((ima_appraise & IMA_APPRAISE_FIX) &&
  248. (!xattr_value ||
  249. xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
  250. if (!ima_fix_xattr(dentry, iint))
  251. status = INTEGRITY_PASS;
  252. }
  253. integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
  254. op, cause, rc, 0);
  255. } else {
  256. ima_cache_flags(iint, func);
  257. }
  258. ima_set_cache_status(iint, func, status);
  259. return status;
  260. }
  261. /*
  262. * ima_update_xattr - update 'security.ima' hash value
  263. */
  264. void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
  265. {
  266. struct dentry *dentry = file->f_dentry;
  267. int rc = 0;
  268. /* do not collect and update hash for digital signatures */
  269. if (iint->flags & IMA_DIGSIG)
  270. return;
  271. rc = ima_collect_measurement(iint, file, NULL, NULL);
  272. if (rc < 0)
  273. return;
  274. ima_fix_xattr(dentry, iint);
  275. }
  276. /**
  277. * ima_inode_post_setattr - reflect file metadata changes
  278. * @dentry: pointer to the affected dentry
  279. *
  280. * Changes to a dentry's metadata might result in needing to appraise.
  281. *
  282. * This function is called from notify_change(), which expects the caller
  283. * to lock the inode's i_mutex.
  284. */
  285. void ima_inode_post_setattr(struct dentry *dentry)
  286. {
  287. struct inode *inode = dentry->d_inode;
  288. struct integrity_iint_cache *iint;
  289. int must_appraise, rc;
  290. if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
  291. || !inode->i_op->removexattr)
  292. return;
  293. must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
  294. iint = integrity_iint_find(inode);
  295. if (iint) {
  296. iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
  297. IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
  298. IMA_ACTION_FLAGS);
  299. if (must_appraise)
  300. iint->flags |= IMA_APPRAISE;
  301. }
  302. if (!must_appraise)
  303. rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
  304. return;
  305. }
  306. /*
  307. * ima_protect_xattr - protect 'security.ima'
  308. *
  309. * Ensure that not just anyone can modify or remove 'security.ima'.
  310. */
  311. static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
  312. const void *xattr_value, size_t xattr_value_len)
  313. {
  314. if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
  315. if (!capable(CAP_SYS_ADMIN))
  316. return -EPERM;
  317. return 1;
  318. }
  319. return 0;
  320. }
  321. static void ima_reset_appraise_flags(struct inode *inode, int digsig)
  322. {
  323. struct integrity_iint_cache *iint;
  324. if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
  325. return;
  326. iint = integrity_iint_find(inode);
  327. if (!iint)
  328. return;
  329. iint->flags &= ~IMA_DONE_MASK;
  330. if (digsig)
  331. iint->flags |= IMA_DIGSIG;
  332. return;
  333. }
  334. int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
  335. const void *xattr_value, size_t xattr_value_len)
  336. {
  337. const struct evm_ima_xattr_data *xvalue = xattr_value;
  338. int result;
  339. result = ima_protect_xattr(dentry, xattr_name, xattr_value,
  340. xattr_value_len);
  341. if (result == 1) {
  342. ima_reset_appraise_flags(dentry->d_inode,
  343. (xvalue->type == EVM_IMA_XATTR_DIGSIG) ? 1 : 0);
  344. result = 0;
  345. }
  346. return result;
  347. }
  348. int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
  349. {
  350. int result;
  351. result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
  352. if (result == 1) {
  353. ima_reset_appraise_flags(dentry->d_inode, 0);
  354. result = 0;
  355. }
  356. return result;
  357. }