integrity.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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/sha.h>
  15. #include <linux/key.h>
  16. #include <linux/audit.h>
  17. /* iint action cache flags */
  18. #define IMA_MEASURE 0x00000001
  19. #define IMA_MEASURED 0x00000002
  20. #define IMA_APPRAISE 0x00000004
  21. #define IMA_APPRAISED 0x00000008
  22. /*#define IMA_COLLECT 0x00000010 do not use this flag */
  23. #define IMA_COLLECTED 0x00000020
  24. #define IMA_AUDIT 0x00000040
  25. #define IMA_AUDITED 0x00000080
  26. #define IMA_HASH 0x00000100
  27. #define IMA_HASHED 0x00000200
  28. /* iint cache flags */
  29. #define IMA_ACTION_FLAGS 0xff000000
  30. #define IMA_DIGSIG_REQUIRED 0x01000000
  31. #define IMA_PERMIT_DIRECTIO 0x02000000
  32. #define IMA_NEW_FILE 0x04000000
  33. #define EVM_IMMUTABLE_DIGSIG 0x08000000
  34. #define IMA_FAIL_UNVERIFIABLE_SIGS 0x10000000
  35. #define IMA_MODSIG_ALLOWED 0x20000000
  36. #define IMA_CHECK_BLACKLIST 0x40000000
  37. #define IMA_DO_MASK (IMA_MEASURE | IMA_APPRAISE | IMA_AUDIT | \
  38. IMA_HASH | IMA_APPRAISE_SUBMASK)
  39. #define IMA_DONE_MASK (IMA_MEASURED | IMA_APPRAISED | IMA_AUDITED | \
  40. IMA_HASHED | IMA_COLLECTED | \
  41. IMA_APPRAISED_SUBMASK)
  42. /* iint subaction appraise cache flags */
  43. #define IMA_FILE_APPRAISE 0x00001000
  44. #define IMA_FILE_APPRAISED 0x00002000
  45. #define IMA_MMAP_APPRAISE 0x00004000
  46. #define IMA_MMAP_APPRAISED 0x00008000
  47. #define IMA_BPRM_APPRAISE 0x00010000
  48. #define IMA_BPRM_APPRAISED 0x00020000
  49. #define IMA_READ_APPRAISE 0x00040000
  50. #define IMA_READ_APPRAISED 0x00080000
  51. #define IMA_CREDS_APPRAISE 0x00100000
  52. #define IMA_CREDS_APPRAISED 0x00200000
  53. #define IMA_APPRAISE_SUBMASK (IMA_FILE_APPRAISE | IMA_MMAP_APPRAISE | \
  54. IMA_BPRM_APPRAISE | IMA_READ_APPRAISE | \
  55. IMA_CREDS_APPRAISE)
  56. #define IMA_APPRAISED_SUBMASK (IMA_FILE_APPRAISED | IMA_MMAP_APPRAISED | \
  57. IMA_BPRM_APPRAISED | IMA_READ_APPRAISED | \
  58. IMA_CREDS_APPRAISED)
  59. /* iint cache atomic_flags */
  60. #define IMA_CHANGE_XATTR 0
  61. #define IMA_UPDATE_XATTR 1
  62. #define IMA_CHANGE_ATTR 2
  63. #define IMA_DIGSIG 3
  64. #define IMA_MUST_MEASURE 4
  65. enum evm_ima_xattr_type {
  66. IMA_XATTR_DIGEST = 0x01,
  67. EVM_XATTR_HMAC,
  68. EVM_IMA_XATTR_DIGSIG,
  69. IMA_XATTR_DIGEST_NG,
  70. EVM_XATTR_PORTABLE_DIGSIG,
  71. IMA_XATTR_LAST
  72. };
  73. struct evm_ima_xattr_data {
  74. u8 type;
  75. u8 data[];
  76. } __packed;
  77. /* Only used in the EVM HMAC code. */
  78. struct evm_xattr {
  79. struct evm_ima_xattr_data data;
  80. u8 digest[SHA1_DIGEST_SIZE];
  81. } __packed;
  82. #define IMA_MAX_DIGEST_SIZE 64
  83. struct ima_digest_data {
  84. u8 algo;
  85. u8 length;
  86. union {
  87. struct {
  88. u8 unused;
  89. u8 type;
  90. } sha1;
  91. struct {
  92. u8 type;
  93. u8 algo;
  94. } ng;
  95. u8 data[2];
  96. } xattr;
  97. u8 digest[];
  98. } __packed;
  99. /*
  100. * signature format v2 - for using with asymmetric keys
  101. */
  102. struct signature_v2_hdr {
  103. uint8_t type; /* xattr type */
  104. uint8_t version; /* signature format version */
  105. uint8_t hash_algo; /* Digest algorithm [enum hash_algo] */
  106. __be32 keyid; /* IMA key identifier - not X509/PGP specific */
  107. __be16 sig_size; /* signature size */
  108. uint8_t sig[]; /* signature payload */
  109. } __packed;
  110. /* integrity data associated with an inode */
  111. struct integrity_iint_cache {
  112. struct rb_node rb_node; /* rooted in integrity_iint_tree */
  113. struct mutex mutex; /* protects: version, flags, digest */
  114. struct inode *inode; /* back pointer to inode in question */
  115. u64 version; /* track inode changes */
  116. unsigned long flags;
  117. unsigned long measured_pcrs;
  118. unsigned long atomic_flags;
  119. enum integrity_status ima_file_status:4;
  120. enum integrity_status ima_mmap_status:4;
  121. enum integrity_status ima_bprm_status:4;
  122. enum integrity_status ima_read_status:4;
  123. enum integrity_status ima_creds_status:4;
  124. enum integrity_status evm_status:4;
  125. struct ima_digest_data *ima_hash;
  126. };
  127. /* rbtree tree calls to lookup, insert, delete
  128. * integrity data associated with an inode.
  129. */
  130. struct integrity_iint_cache *integrity_iint_find(struct inode *inode);
  131. int integrity_kernel_read(struct file *file, loff_t offset,
  132. void *addr, unsigned long count);
  133. #define INTEGRITY_KEYRING_EVM 0
  134. #define INTEGRITY_KEYRING_IMA 1
  135. #define INTEGRITY_KEYRING_PLATFORM 2
  136. #define INTEGRITY_KEYRING_MAX 3
  137. extern struct dentry *integrity_dir;
  138. struct modsig;
  139. #ifdef CONFIG_INTEGRITY_SIGNATURE
  140. int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen,
  141. const char *digest, int digestlen);
  142. int integrity_modsig_verify(unsigned int id, const struct modsig *modsig);
  143. int __init integrity_init_keyring(const unsigned int id);
  144. int __init integrity_load_x509(const unsigned int id, const char *path);
  145. int __init integrity_load_cert(const unsigned int id, const char *source,
  146. const void *data, size_t len, key_perm_t perm);
  147. #else
  148. static inline int integrity_digsig_verify(const unsigned int id,
  149. const char *sig, int siglen,
  150. const char *digest, int digestlen)
  151. {
  152. return -EOPNOTSUPP;
  153. }
  154. static inline int integrity_modsig_verify(unsigned int id,
  155. const struct modsig *modsig)
  156. {
  157. return -EOPNOTSUPP;
  158. }
  159. static inline int integrity_init_keyring(const unsigned int id)
  160. {
  161. return 0;
  162. }
  163. static inline int __init integrity_load_cert(const unsigned int id,
  164. const char *source,
  165. const void *data, size_t len,
  166. key_perm_t perm)
  167. {
  168. return 0;
  169. }
  170. #endif /* CONFIG_INTEGRITY_SIGNATURE */
  171. #ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS
  172. int asymmetric_verify(struct key *keyring, const char *sig,
  173. int siglen, const char *data, int datalen);
  174. #else
  175. static inline int asymmetric_verify(struct key *keyring, const char *sig,
  176. int siglen, const char *data, int datalen)
  177. {
  178. return -EOPNOTSUPP;
  179. }
  180. #endif
  181. #ifdef CONFIG_IMA_APPRAISE_MODSIG
  182. int ima_modsig_verify(struct key *keyring, const struct modsig *modsig);
  183. #else
  184. static inline int ima_modsig_verify(struct key *keyring,
  185. const struct modsig *modsig)
  186. {
  187. return -EOPNOTSUPP;
  188. }
  189. #endif
  190. #ifdef CONFIG_IMA_LOAD_X509
  191. void __init ima_load_x509(void);
  192. #else
  193. static inline void ima_load_x509(void)
  194. {
  195. }
  196. #endif
  197. #ifdef CONFIG_EVM_LOAD_X509
  198. void __init evm_load_x509(void);
  199. #else
  200. static inline void evm_load_x509(void)
  201. {
  202. }
  203. #endif
  204. #ifdef CONFIG_INTEGRITY_AUDIT
  205. /* declarations */
  206. void integrity_audit_msg(int audit_msgno, struct inode *inode,
  207. const unsigned char *fname, const char *op,
  208. const char *cause, int result, int info);
  209. void integrity_audit_message(int audit_msgno, struct inode *inode,
  210. const unsigned char *fname, const char *op,
  211. const char *cause, int result, int info,
  212. int errno);
  213. static inline struct audit_buffer *
  214. integrity_audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type)
  215. {
  216. return audit_log_start(ctx, gfp_mask, type);
  217. }
  218. #else
  219. static inline void integrity_audit_msg(int audit_msgno, struct inode *inode,
  220. const unsigned char *fname,
  221. const char *op, const char *cause,
  222. int result, int info)
  223. {
  224. }
  225. static inline void integrity_audit_message(int audit_msgno,
  226. struct inode *inode,
  227. const unsigned char *fname,
  228. const char *op, const char *cause,
  229. int result, int info, int errno)
  230. {
  231. }
  232. static inline struct audit_buffer *
  233. integrity_audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type)
  234. {
  235. return NULL;
  236. }
  237. #endif
  238. #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
  239. void __init add_to_platform_keyring(const char *source, const void *data,
  240. size_t len);
  241. #else
  242. static inline void __init add_to_platform_keyring(const char *source,
  243. const void *data, size_t len)
  244. {
  245. }
  246. #endif