integrity.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. int __init integrity_fs_init(void);
  106. void __init integrity_fs_fini(void);
  107. #define INTEGRITY_KEYRING_EVM 0
  108. #define INTEGRITY_KEYRING_IMA 1
  109. #define INTEGRITY_KEYRING_PLATFORM 2
  110. #define INTEGRITY_KEYRING_MACHINE 3
  111. #define INTEGRITY_KEYRING_MAX 4
  112. extern struct dentry *integrity_dir;
  113. struct modsig;
  114. #ifdef CONFIG_INTEGRITY_SIGNATURE
  115. int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen,
  116. const char *digest, int digestlen);
  117. int integrity_modsig_verify(unsigned int id, const struct modsig *modsig);
  118. int __init integrity_init_keyring(const unsigned int id);
  119. int __init integrity_load_x509(const unsigned int id, const char *path);
  120. int __init integrity_load_cert(const unsigned int id, const char *source,
  121. const void *data, size_t len, key_perm_t perm);
  122. #else
  123. static inline int integrity_digsig_verify(const unsigned int id,
  124. const char *sig, int siglen,
  125. const char *digest, int digestlen)
  126. {
  127. return -EOPNOTSUPP;
  128. }
  129. static inline int integrity_modsig_verify(unsigned int id,
  130. const struct modsig *modsig)
  131. {
  132. return -EOPNOTSUPP;
  133. }
  134. static inline int integrity_init_keyring(const unsigned int id)
  135. {
  136. return 0;
  137. }
  138. static inline int __init integrity_load_cert(const unsigned int id,
  139. const char *source,
  140. const void *data, size_t len,
  141. key_perm_t perm)
  142. {
  143. return 0;
  144. }
  145. #endif /* CONFIG_INTEGRITY_SIGNATURE */
  146. #ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS
  147. int asymmetric_verify(struct key *keyring, const char *sig,
  148. int siglen, const char *data, int datalen);
  149. #else
  150. static inline int asymmetric_verify(struct key *keyring, const char *sig,
  151. int siglen, const char *data, int datalen)
  152. {
  153. return -EOPNOTSUPP;
  154. }
  155. #endif
  156. #ifdef CONFIG_IMA_APPRAISE_MODSIG
  157. int ima_modsig_verify(struct key *keyring, const struct modsig *modsig);
  158. #else
  159. static inline int ima_modsig_verify(struct key *keyring,
  160. const struct modsig *modsig)
  161. {
  162. return -EOPNOTSUPP;
  163. }
  164. #endif
  165. #ifdef CONFIG_IMA_LOAD_X509
  166. void __init ima_load_x509(void);
  167. #else
  168. static inline void ima_load_x509(void)
  169. {
  170. }
  171. #endif
  172. #ifdef CONFIG_EVM_LOAD_X509
  173. void __init evm_load_x509(void);
  174. #else
  175. static inline void evm_load_x509(void)
  176. {
  177. }
  178. #endif
  179. #ifdef CONFIG_INTEGRITY_AUDIT
  180. /* declarations */
  181. void integrity_audit_msg(int audit_msgno, struct inode *inode,
  182. const unsigned char *fname, const char *op,
  183. const char *cause, int result, int info);
  184. void integrity_audit_message(int audit_msgno, struct inode *inode,
  185. const unsigned char *fname, const char *op,
  186. const char *cause, int result, int info,
  187. int errno);
  188. static inline struct audit_buffer *
  189. integrity_audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type)
  190. {
  191. return audit_log_start(ctx, gfp_mask, type);
  192. }
  193. #else
  194. static inline void integrity_audit_msg(int audit_msgno, struct inode *inode,
  195. const unsigned char *fname,
  196. const char *op, const char *cause,
  197. int result, int info)
  198. {
  199. }
  200. static inline void integrity_audit_message(int audit_msgno,
  201. struct inode *inode,
  202. const unsigned char *fname,
  203. const char *op, const char *cause,
  204. int result, int info, int errno)
  205. {
  206. }
  207. static inline struct audit_buffer *
  208. integrity_audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type)
  209. {
  210. return NULL;
  211. }
  212. #endif
  213. #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
  214. void __init add_to_platform_keyring(const char *source, const void *data,
  215. size_t len);
  216. #else
  217. static inline void __init add_to_platform_keyring(const char *source,
  218. const void *data, size_t len)
  219. {
  220. }
  221. #endif
  222. #ifdef CONFIG_INTEGRITY_MACHINE_KEYRING
  223. void __init add_to_machine_keyring(const char *source, const void *data, size_t len);
  224. bool __init imputed_trust_enabled(void);
  225. #else
  226. static inline void __init add_to_machine_keyring(const char *source,
  227. const void *data, size_t len)
  228. {
  229. }
  230. static inline bool __init imputed_trust_enabled(void)
  231. {
  232. return false;
  233. }
  234. #endif