ipe.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020-2024 Microsoft Corporation. All rights reserved.
  4. */
  5. #include <uapi/linux/lsm.h>
  6. #include "ipe.h"
  7. #include "eval.h"
  8. #include "hooks.h"
  9. extern const char *const ipe_boot_policy;
  10. bool ipe_enabled;
  11. static struct lsm_blob_sizes ipe_blobs __ro_after_init = {
  12. .lbs_superblock = sizeof(struct ipe_superblock),
  13. #ifdef CONFIG_IPE_PROP_DM_VERITY
  14. .lbs_bdev = sizeof(struct ipe_bdev),
  15. #endif /* CONFIG_IPE_PROP_DM_VERITY */
  16. #ifdef CONFIG_IPE_PROP_FS_VERITY_BUILTIN_SIG
  17. .lbs_inode = sizeof(struct ipe_inode),
  18. #endif /* CONFIG_IPE_PROP_FS_VERITY_BUILTIN_SIG */
  19. };
  20. static const struct lsm_id ipe_lsmid = {
  21. .name = "ipe",
  22. .id = LSM_ID_IPE,
  23. };
  24. struct ipe_superblock *ipe_sb(const struct super_block *sb)
  25. {
  26. return sb->s_security + ipe_blobs.lbs_superblock;
  27. }
  28. #ifdef CONFIG_IPE_PROP_DM_VERITY
  29. struct ipe_bdev *ipe_bdev(struct block_device *b)
  30. {
  31. return b->bd_security + ipe_blobs.lbs_bdev;
  32. }
  33. #endif /* CONFIG_IPE_PROP_DM_VERITY */
  34. #ifdef CONFIG_IPE_PROP_FS_VERITY_BUILTIN_SIG
  35. struct ipe_inode *ipe_inode(const struct inode *inode)
  36. {
  37. return inode->i_security + ipe_blobs.lbs_inode;
  38. }
  39. #endif /* CONFIG_IPE_PROP_FS_VERITY_BUILTIN_SIG */
  40. static struct security_hook_list ipe_hooks[] __ro_after_init = {
  41. LSM_HOOK_INIT(bprm_check_security, ipe_bprm_check_security),
  42. LSM_HOOK_INIT(bprm_creds_for_exec, ipe_bprm_creds_for_exec),
  43. LSM_HOOK_INIT(mmap_file, ipe_mmap_file),
  44. LSM_HOOK_INIT(file_mprotect, ipe_file_mprotect),
  45. LSM_HOOK_INIT(kernel_read_file, ipe_kernel_read_file),
  46. LSM_HOOK_INIT(kernel_load_data, ipe_kernel_load_data),
  47. LSM_HOOK_INIT(initramfs_populated, ipe_unpack_initramfs),
  48. #ifdef CONFIG_IPE_PROP_DM_VERITY
  49. LSM_HOOK_INIT(bdev_free_security, ipe_bdev_free_security),
  50. LSM_HOOK_INIT(bdev_setintegrity, ipe_bdev_setintegrity),
  51. #endif /* CONFIG_IPE_PROP_DM_VERITY */
  52. #ifdef CONFIG_IPE_PROP_FS_VERITY_BUILTIN_SIG
  53. LSM_HOOK_INIT(inode_setintegrity, ipe_inode_setintegrity),
  54. #endif /* CONFIG_IPE_PROP_FS_VERITY_BUILTIN_SIG */
  55. };
  56. /**
  57. * ipe_init() - Entry point of IPE.
  58. *
  59. * This is called at LSM init, which happens occurs early during kernel
  60. * start up. During this phase, IPE registers its hooks and loads the
  61. * builtin boot policy.
  62. *
  63. * Return:
  64. * * %0 - OK
  65. * * %-ENOMEM - Out of memory (OOM)
  66. */
  67. static int __init ipe_init(void)
  68. {
  69. struct ipe_policy *p = NULL;
  70. security_add_hooks(ipe_hooks, ARRAY_SIZE(ipe_hooks), &ipe_lsmid);
  71. ipe_enabled = true;
  72. if (ipe_boot_policy) {
  73. p = ipe_new_policy(ipe_boot_policy, strlen(ipe_boot_policy),
  74. NULL, 0);
  75. if (IS_ERR(p))
  76. return PTR_ERR(p);
  77. rcu_assign_pointer(ipe_active_policy, p);
  78. }
  79. return 0;
  80. }
  81. DEFINE_LSM(ipe) = {
  82. .id = &ipe_lsmid,
  83. .init = ipe_init,
  84. .blobs = &ipe_blobs,
  85. .initcall_fs = ipe_init_securityfs,
  86. };