integrity.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2009-2010 IBM Corporation
  4. *
  5. * Authors:
  6. * Mimi Zohar <zohar@us.ibm.com>
  7. */
  8. #ifdef pr_fmt
  9. #undef pr_fmt
  10. #endif
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/types.h>
  13. #include <linux/integrity.h>
  14. #include <crypto/sha1.h>
  15. #include <crypto/hash.h>
  16. #include <linux/key.h>
  17. #include <linux/audit.h>
  18. #include <linux/lsm_hooks.h>
  19. enum evm_ima_xattr_type {
  20. IMA_XATTR_DIGEST = 0x01,
  21. EVM_XATTR_HMAC,
  22. EVM_IMA_XATTR_DIGSIG,
  23. IMA_XATTR_DIGEST_NG,
  24. EVM_XATTR_PORTABLE_DIGSIG,
  25. IMA_VERITY_DIGSIG,
  26. IMA_XATTR_LAST
  27. };
  28. struct evm_ima_xattr_data {
  29. /* New members must be added within the __struct_group() macro below. */
  30. __struct_group(evm_ima_xattr_data_hdr, hdr, __packed,
  31. u8 type;
  32. );
  33. u8 data[];
  34. } __packed;
  35. static_assert(offsetof(struct evm_ima_xattr_data, data) == sizeof(struct evm_ima_xattr_data_hdr),
  36. "struct member likely outside of __struct_group()");
  37. /* Only used in the EVM HMAC code. */
  38. struct evm_xattr {
  39. struct evm_ima_xattr_data_hdr data;
  40. u8 digest[SHA1_DIGEST_SIZE];
  41. } __packed;
  42. #define IMA_MAX_DIGEST_SIZE HASH_MAX_DIGESTSIZE
  43. struct ima_digest_data {
  44. /* New members must be added within the __struct_group() macro below. */
  45. __struct_group(ima_digest_data_hdr, hdr, __packed,
  46. u8 algo;
  47. u8 length;
  48. union {
  49. struct {
  50. u8 unused;
  51. u8 type;
  52. } sha1;
  53. struct {
  54. u8 type;
  55. u8 algo;
  56. } ng;
  57. u8 data[2];
  58. } xattr;
  59. );
  60. u8 digest[];
  61. } __packed;
  62. static_assert(offsetof(struct ima_digest_data, digest) == sizeof(struct ima_digest_data_hdr),
  63. "struct member likely outside of __struct_group()");
  64. /*
  65. * Instead of wrapping the ima_digest_data struct inside a local structure
  66. * with the maximum hash size, define ima_max_digest_data struct.
  67. */
  68. struct ima_max_digest_data {
  69. struct ima_digest_data_hdr hdr;
  70. u8 digest[HASH_MAX_DIGESTSIZE];
  71. } __packed;
  72. /*
  73. * signature header format v2 - for using with asymmetric keys
  74. *
  75. * The signature_v2_hdr struct includes a signature format version
  76. * to simplify defining new signature formats.
  77. *
  78. * signature format:
  79. * version 2: regular file data hash based signature
  80. * version 3: struct ima_file_id data based signature
  81. */
  82. struct signature_v2_hdr {
  83. uint8_t type; /* xattr type */
  84. uint8_t version; /* signature format version */
  85. uint8_t hash_algo; /* Digest algorithm [enum hash_algo] */
  86. __be32 keyid; /* IMA key identifier - not X509/PGP specific */
  87. __be16 sig_size; /* signature size */
  88. uint8_t sig[]; /* signature payload */
  89. } __packed;
  90. /*
  91. * IMA signature version 3 disambiguates the data that is signed, by
  92. * indirectly signing the hash of the ima_file_id structure data,
  93. * containing either the fsverity_descriptor struct digest or, in the
  94. * future, the regular IMA file hash.
  95. *
  96. * (The hash of the ima_file_id structure is only of the portion used.)
  97. */
  98. struct ima_file_id {
  99. __u8 hash_type; /* xattr type [enum evm_ima_xattr_type] */
  100. __u8 hash_algorithm; /* Digest algorithm [enum hash_algo] */
  101. __u8 hash[HASH_MAX_DIGESTSIZE];
  102. } __packed;
  103. int integrity_kernel_read(struct file *file, loff_t offset,
  104. void *addr, unsigned long count);
  105. #define INTEGRITY_KEYRING_EVM 0
  106. #define INTEGRITY_KEYRING_IMA 1
  107. #define INTEGRITY_KEYRING_PLATFORM 2
  108. #define INTEGRITY_KEYRING_MACHINE 3
  109. #define INTEGRITY_KEYRING_MAX 4
  110. extern struct dentry *integrity_dir;
  111. struct modsig;
  112. #ifdef CONFIG_INTEGRITY_SIGNATURE
  113. int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen,
  114. const char *digest, int digestlen);
  115. int integrity_modsig_verify(unsigned int id, const struct modsig *modsig);
  116. int __init integrity_init_keyring(const unsigned int id);
  117. int __init integrity_load_x509(const unsigned int id, const char *path);
  118. int __init integrity_load_cert(const unsigned int id, const char *source,
  119. const void *data, size_t len, key_perm_t perm);
  120. #else
  121. static inline int integrity_digsig_verify(const unsigned int id,
  122. const char *sig, int siglen,
  123. const char *digest, int digestlen)
  124. {
  125. return -EOPNOTSUPP;
  126. }
  127. static inline int integrity_modsig_verify(unsigned int id,
  128. const struct modsig *modsig)
  129. {
  130. return -EOPNOTSUPP;
  131. }
  132. static inline int integrity_init_keyring(const unsigned int id)
  133. {
  134. return 0;
  135. }
  136. static inline int __init integrity_load_cert(const unsigned int id,
  137. const char *source,
  138. const void *data, size_t len,
  139. key_perm_t perm)
  140. {
  141. return 0;
  142. }
  143. #endif /* CONFIG_INTEGRITY_SIGNATURE */
  144. #ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS
  145. int asymmetric_verify(struct key *keyring, const char *sig,
  146. int siglen, const char *data, int datalen);
  147. #else
  148. static inline int asymmetric_verify(struct key *keyring, const char *sig,
  149. int siglen, const char *data, int datalen)
  150. {
  151. return -EOPNOTSUPP;
  152. }
  153. #endif
  154. #ifdef CONFIG_IMA_APPRAISE_MODSIG
  155. int ima_modsig_verify(struct key *keyring, const struct modsig *modsig);
  156. #else
  157. static inline int ima_modsig_verify(struct key *keyring,
  158. const struct modsig *modsig)
  159. {
  160. return -EOPNOTSUPP;
  161. }
  162. #endif
  163. #ifdef CONFIG_IMA_LOAD_X509
  164. void __init ima_load_x509(void);
  165. #else
  166. static inline void ima_load_x509(void)
  167. {
  168. }
  169. #endif
  170. #ifdef CONFIG_EVM_LOAD_X509
  171. void __init evm_load_x509(void);
  172. #else
  173. static inline void evm_load_x509(void)
  174. {
  175. }
  176. #endif
  177. #ifdef CONFIG_INTEGRITY_AUDIT
  178. /* declarations */
  179. void integrity_audit_msg(int audit_msgno, struct inode *inode,
  180. const unsigned char *fname, const char *op,
  181. const char *cause, int result, int info);
  182. void integrity_audit_message(int audit_msgno, struct inode *inode,
  183. const unsigned char *fname, const char *op,
  184. const char *cause, int result, int info,
  185. int errno);
  186. static inline struct audit_buffer *
  187. integrity_audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type)
  188. {
  189. return audit_log_start(ctx, gfp_mask, type);
  190. }
  191. #else
  192. static inline void integrity_audit_msg(int audit_msgno, struct inode *inode,
  193. const unsigned char *fname,
  194. const char *op, const char *cause,
  195. int result, int info)
  196. {
  197. }
  198. static inline void integrity_audit_message(int audit_msgno,
  199. struct inode *inode,
  200. const unsigned char *fname,
  201. const char *op, const char *cause,
  202. int result, int info, int errno)
  203. {
  204. }
  205. static inline struct audit_buffer *
  206. integrity_audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type)
  207. {
  208. return NULL;
  209. }
  210. #endif
  211. #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
  212. void __init add_to_platform_keyring(const char *source, const void *data,
  213. size_t len);
  214. #else
  215. static inline void __init add_to_platform_keyring(const char *source,
  216. const void *data, size_t len)
  217. {
  218. }
  219. #endif
  220. #ifdef CONFIG_INTEGRITY_MACHINE_KEYRING
  221. void __init add_to_machine_keyring(const char *source, const void *data, size_t len);
  222. bool __init imputed_trust_enabled(void);
  223. #else
  224. static inline void __init add_to_machine_keyring(const char *source,
  225. const void *data, size_t len)
  226. {
  227. }
  228. static inline bool __init imputed_trust_enabled(void)
  229. {
  230. return false;
  231. }
  232. #endif