hooks.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020-2024 Microsoft Corporation. All rights reserved.
  4. */
  5. #include <linux/fs.h>
  6. #include <linux/fs_struct.h>
  7. #include <linux/types.h>
  8. #include <linux/binfmts.h>
  9. #include <linux/mman.h>
  10. #include <linux/blk_types.h>
  11. #include "ipe.h"
  12. #include "hooks.h"
  13. #include "eval.h"
  14. #include "digest.h"
  15. /**
  16. * ipe_bprm_check_security() - ipe security hook function for bprm check.
  17. * @bprm: Supplies a pointer to a linux_binprm structure to source the file
  18. * being evaluated.
  19. *
  20. * This LSM hook is called when a binary is loaded through the exec
  21. * family of system calls.
  22. *
  23. * Return:
  24. * * %0 - Success
  25. * * %-EACCES - Did not pass IPE policy
  26. */
  27. int ipe_bprm_check_security(struct linux_binprm *bprm)
  28. {
  29. struct ipe_eval_ctx ctx = IPE_EVAL_CTX_INIT;
  30. ipe_build_eval_ctx(&ctx, bprm->file, IPE_OP_EXEC, IPE_HOOK_BPRM_CHECK);
  31. return ipe_evaluate_event(&ctx);
  32. }
  33. /**
  34. * ipe_mmap_file() - ipe security hook function for mmap check.
  35. * @f: File being mmap'd. Can be NULL in the case of anonymous memory.
  36. * @reqprot: The requested protection on the mmap, passed from usermode.
  37. * @prot: The effective protection on the mmap, resolved from reqprot and
  38. * system configuration.
  39. * @flags: Unused.
  40. *
  41. * This hook is called when a file is loaded through the mmap
  42. * family of system calls.
  43. *
  44. * Return:
  45. * * %0 - Success
  46. * * %-EACCES - Did not pass IPE policy
  47. */
  48. int ipe_mmap_file(struct file *f, unsigned long reqprot __always_unused,
  49. unsigned long prot, unsigned long flags)
  50. {
  51. struct ipe_eval_ctx ctx = IPE_EVAL_CTX_INIT;
  52. if (prot & PROT_EXEC) {
  53. ipe_build_eval_ctx(&ctx, f, IPE_OP_EXEC, IPE_HOOK_MMAP);
  54. return ipe_evaluate_event(&ctx);
  55. }
  56. return 0;
  57. }
  58. /**
  59. * ipe_file_mprotect() - ipe security hook function for mprotect check.
  60. * @vma: Existing virtual memory area created by mmap or similar.
  61. * @reqprot: The requested protection on the mmap, passed from usermode.
  62. * @prot: The effective protection on the mmap, resolved from reqprot and
  63. * system configuration.
  64. *
  65. * This LSM hook is called when a mmap'd region of memory is changing
  66. * its protections via mprotect.
  67. *
  68. * Return:
  69. * * %0 - Success
  70. * * %-EACCES - Did not pass IPE policy
  71. */
  72. int ipe_file_mprotect(struct vm_area_struct *vma,
  73. unsigned long reqprot __always_unused,
  74. unsigned long prot)
  75. {
  76. struct ipe_eval_ctx ctx = IPE_EVAL_CTX_INIT;
  77. /* Already Executable */
  78. if (vma->vm_flags & VM_EXEC)
  79. return 0;
  80. if (prot & PROT_EXEC) {
  81. ipe_build_eval_ctx(&ctx, vma->vm_file, IPE_OP_EXEC, IPE_HOOK_MPROTECT);
  82. return ipe_evaluate_event(&ctx);
  83. }
  84. return 0;
  85. }
  86. /**
  87. * ipe_kernel_read_file() - ipe security hook function for kernel read.
  88. * @file: Supplies a pointer to the file structure being read in from disk.
  89. * @id: Supplies the enumeration identifying the purpose of the read.
  90. * @contents: Unused.
  91. *
  92. * This LSM hook is called when a file is read from disk in the kernel.
  93. *
  94. * Return:
  95. * * %0 - Success
  96. * * %-EACCES - Did not pass IPE policy
  97. */
  98. int ipe_kernel_read_file(struct file *file, enum kernel_read_file_id id,
  99. bool contents)
  100. {
  101. struct ipe_eval_ctx ctx = IPE_EVAL_CTX_INIT;
  102. enum ipe_op_type op;
  103. switch (id) {
  104. case READING_FIRMWARE:
  105. op = IPE_OP_FIRMWARE;
  106. break;
  107. case READING_MODULE:
  108. op = IPE_OP_KERNEL_MODULE;
  109. break;
  110. case READING_KEXEC_INITRAMFS:
  111. op = IPE_OP_KEXEC_INITRAMFS;
  112. break;
  113. case READING_KEXEC_IMAGE:
  114. op = IPE_OP_KEXEC_IMAGE;
  115. break;
  116. case READING_POLICY:
  117. op = IPE_OP_POLICY;
  118. break;
  119. case READING_X509_CERTIFICATE:
  120. op = IPE_OP_X509;
  121. break;
  122. default:
  123. op = IPE_OP_INVALID;
  124. WARN(1, "no rule setup for kernel_read_file enum %d", id);
  125. }
  126. ipe_build_eval_ctx(&ctx, file, op, IPE_HOOK_KERNEL_READ);
  127. return ipe_evaluate_event(&ctx);
  128. }
  129. /**
  130. * ipe_kernel_load_data() - ipe security hook function for kernel load data.
  131. * @id: Supplies the enumeration identifying the purpose of the load.
  132. * @contents: Unused.
  133. *
  134. * This LSM hook is called when a data buffer provided by userspace is loading
  135. * into the kernel.
  136. *
  137. * Return:
  138. * * %0 - Success
  139. * * %-EACCES - Did not pass IPE policy
  140. */
  141. int ipe_kernel_load_data(enum kernel_load_data_id id, bool contents)
  142. {
  143. struct ipe_eval_ctx ctx = IPE_EVAL_CTX_INIT;
  144. enum ipe_op_type op;
  145. switch (id) {
  146. case LOADING_FIRMWARE:
  147. op = IPE_OP_FIRMWARE;
  148. break;
  149. case LOADING_MODULE:
  150. op = IPE_OP_KERNEL_MODULE;
  151. break;
  152. case LOADING_KEXEC_INITRAMFS:
  153. op = IPE_OP_KEXEC_INITRAMFS;
  154. break;
  155. case LOADING_KEXEC_IMAGE:
  156. op = IPE_OP_KEXEC_IMAGE;
  157. break;
  158. case LOADING_POLICY:
  159. op = IPE_OP_POLICY;
  160. break;
  161. case LOADING_X509_CERTIFICATE:
  162. op = IPE_OP_X509;
  163. break;
  164. default:
  165. op = IPE_OP_INVALID;
  166. WARN(1, "no rule setup for kernel_load_data enum %d", id);
  167. }
  168. ipe_build_eval_ctx(&ctx, NULL, op, IPE_HOOK_KERNEL_LOAD);
  169. return ipe_evaluate_event(&ctx);
  170. }
  171. /**
  172. * ipe_unpack_initramfs() - Mark the current rootfs as initramfs.
  173. */
  174. void ipe_unpack_initramfs(void)
  175. {
  176. ipe_sb(current->fs->root.mnt->mnt_sb)->initramfs = true;
  177. }
  178. #ifdef CONFIG_IPE_PROP_DM_VERITY
  179. /**
  180. * ipe_bdev_free_security() - Free IPE's LSM blob of block_devices.
  181. * @bdev: Supplies a pointer to a block_device that contains the structure
  182. * to free.
  183. */
  184. void ipe_bdev_free_security(struct block_device *bdev)
  185. {
  186. struct ipe_bdev *blob = ipe_bdev(bdev);
  187. ipe_digest_free(blob->root_hash);
  188. }
  189. #ifdef CONFIG_IPE_PROP_DM_VERITY_SIGNATURE
  190. static void ipe_set_dmverity_signature(struct ipe_bdev *blob,
  191. const void *value,
  192. size_t size)
  193. {
  194. blob->dm_verity_signed = size > 0 && value;
  195. }
  196. #else
  197. static inline void ipe_set_dmverity_signature(struct ipe_bdev *blob,
  198. const void *value,
  199. size_t size)
  200. {
  201. }
  202. #endif /* CONFIG_IPE_PROP_DM_VERITY_SIGNATURE */
  203. /**
  204. * ipe_bdev_setintegrity() - Save integrity data from a bdev to IPE's LSM blob.
  205. * @bdev: Supplies a pointer to a block_device that contains the LSM blob.
  206. * @type: Supplies the integrity type.
  207. * @value: Supplies the value to store.
  208. * @size: The size of @value.
  209. *
  210. * This hook is currently used to save dm-verity's root hash or the existence
  211. * of a validated signed dm-verity root hash into LSM blob.
  212. *
  213. * Return: %0 on success. If an error occurs, the function will return the
  214. * -errno.
  215. */
  216. int ipe_bdev_setintegrity(struct block_device *bdev, enum lsm_integrity_type type,
  217. const void *value, size_t size)
  218. {
  219. const struct dm_verity_digest *digest = NULL;
  220. struct ipe_bdev *blob = ipe_bdev(bdev);
  221. struct digest_info *info = NULL;
  222. if (type == LSM_INT_DMVERITY_SIG_VALID) {
  223. ipe_set_dmverity_signature(blob, value, size);
  224. return 0;
  225. }
  226. if (type != LSM_INT_DMVERITY_ROOTHASH)
  227. return -EINVAL;
  228. if (!value) {
  229. ipe_digest_free(blob->root_hash);
  230. blob->root_hash = NULL;
  231. return 0;
  232. }
  233. digest = value;
  234. info = kzalloc(sizeof(*info), GFP_KERNEL);
  235. if (!info)
  236. return -ENOMEM;
  237. info->digest = kmemdup(digest->digest, digest->digest_len, GFP_KERNEL);
  238. if (!info->digest)
  239. goto err;
  240. info->alg = kstrdup(digest->alg, GFP_KERNEL);
  241. if (!info->alg)
  242. goto err;
  243. info->digest_len = digest->digest_len;
  244. ipe_digest_free(blob->root_hash);
  245. blob->root_hash = info;
  246. return 0;
  247. err:
  248. ipe_digest_free(info);
  249. return -ENOMEM;
  250. }
  251. #endif /* CONFIG_IPE_PROP_DM_VERITY */
  252. #ifdef CONFIG_IPE_PROP_FS_VERITY_BUILTIN_SIG
  253. /**
  254. * ipe_inode_setintegrity() - save integrity data from a inode to IPE's LSM blob.
  255. * @inode: The inode to source the security blob from.
  256. * @type: Supplies the integrity type.
  257. * @value: The value to be stored.
  258. * @size: The size of @value.
  259. *
  260. * This hook is currently used to save the existence of a validated fs-verity
  261. * builtin signature into LSM blob.
  262. *
  263. * Return: %0 on success. If an error occurs, the function will return the
  264. * -errno.
  265. */
  266. int ipe_inode_setintegrity(const struct inode *inode,
  267. enum lsm_integrity_type type,
  268. const void *value, size_t size)
  269. {
  270. struct ipe_inode *inode_sec = ipe_inode(inode);
  271. if (type == LSM_INT_FSVERITY_BUILTINSIG_VALID) {
  272. inode_sec->fs_verity_signed = size > 0 && value;
  273. return 0;
  274. }
  275. return -EINVAL;
  276. }
  277. #endif /* CONFIG_CONFIG_IPE_PROP_FS_VERITY_BUILTIN_SIG */