integrity.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2009-2010 IBM Corporation
  4. *
  5. * Authors:
  6. * Mimi Zohar <[email protected]>
  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. /* iint action cache flags */
  19. #define IMA_MEASURE 0x00000001
  20. #define IMA_MEASURED 0x00000002
  21. #define IMA_APPRAISE 0x00000004
  22. #define IMA_APPRAISED 0x00000008
  23. /*#define IMA_COLLECT 0x00000010 do not use this flag */
  24. #define IMA_COLLECTED 0x00000020
  25. #define IMA_AUDIT 0x00000040
  26. #define IMA_AUDITED 0x00000080
  27. #define IMA_HASH 0x00000100
  28. #define IMA_HASHED 0x00000200
  29. /* iint policy rule cache flags */
  30. #define IMA_NONACTION_FLAGS 0xff000000
  31. #define IMA_DIGSIG_REQUIRED 0x01000000
  32. #define IMA_PERMIT_DIRECTIO 0x02000000
  33. #define IMA_NEW_FILE 0x04000000
  34. #define EVM_IMMUTABLE_DIGSIG 0x08000000
  35. #define IMA_FAIL_UNVERIFIABLE_SIGS 0x10000000
  36. #define IMA_MODSIG_ALLOWED 0x20000000
  37. #define IMA_CHECK_BLACKLIST 0x40000000
  38. #define IMA_VERITY_REQUIRED 0x80000000
  39. #define IMA_DO_MASK (IMA_MEASURE | IMA_APPRAISE | IMA_AUDIT | \
  40. IMA_HASH | IMA_APPRAISE_SUBMASK)
  41. #define IMA_DONE_MASK (IMA_MEASURED | IMA_APPRAISED | IMA_AUDITED | \
  42. IMA_HASHED | IMA_COLLECTED | \
  43. IMA_APPRAISED_SUBMASK)
  44. /* iint subaction appraise cache flags */
  45. #define IMA_FILE_APPRAISE 0x00001000
  46. #define IMA_FILE_APPRAISED 0x00002000
  47. #define IMA_MMAP_APPRAISE 0x00004000
  48. #define IMA_MMAP_APPRAISED 0x00008000
  49. #define IMA_BPRM_APPRAISE 0x00010000
  50. #define IMA_BPRM_APPRAISED 0x00020000
  51. #define IMA_READ_APPRAISE 0x00040000
  52. #define IMA_READ_APPRAISED 0x00080000
  53. #define IMA_CREDS_APPRAISE 0x00100000
  54. #define IMA_CREDS_APPRAISED 0x00200000
  55. #define IMA_APPRAISE_SUBMASK (IMA_FILE_APPRAISE | IMA_MMAP_APPRAISE | \
  56. IMA_BPRM_APPRAISE | IMA_READ_APPRAISE | \
  57. IMA_CREDS_APPRAISE)
  58. #define IMA_APPRAISED_SUBMASK (IMA_FILE_APPRAISED | IMA_MMAP_APPRAISED | \
  59. IMA_BPRM_APPRAISED | IMA_READ_APPRAISED | \
  60. IMA_CREDS_APPRAISED)
  61. /* iint cache atomic_flags */
  62. #define IMA_CHANGE_XATTR 0
  63. #define IMA_UPDATE_XATTR 1
  64. #define IMA_CHANGE_ATTR 2
  65. #define IMA_DIGSIG 3
  66. #define IMA_MUST_MEASURE 4
  67. enum evm_ima_xattr_type {
  68. IMA_XATTR_DIGEST = 0x01,
  69. EVM_XATTR_HMAC,
  70. EVM_IMA_XATTR_DIGSIG,
  71. IMA_XATTR_DIGEST_NG,
  72. EVM_XATTR_PORTABLE_DIGSIG,
  73. IMA_VERITY_DIGSIG,
  74. IMA_XATTR_LAST
  75. };
  76. struct evm_ima_xattr_data {
  77. u8 type;
  78. u8 data[];
  79. } __packed;
  80. /* Only used in the EVM HMAC code. */
  81. struct evm_xattr {
  82. struct evm_ima_xattr_data data;
  83. u8 digest[SHA1_DIGEST_SIZE];
  84. } __packed;
  85. #define IMA_MAX_DIGEST_SIZE HASH_MAX_DIGESTSIZE
  86. struct ima_digest_data {
  87. u8 algo;
  88. u8 length;
  89. union {
  90. struct {
  91. u8 unused;
  92. u8 type;
  93. } sha1;
  94. struct {
  95. u8 type;
  96. u8 algo;
  97. } ng;
  98. u8 data[2];
  99. } xattr;
  100. u8 digest[];
  101. } __packed;
  102. /*
  103. * Instead of wrapping the ima_digest_data struct inside a local structure
  104. * with the maximum hash size, define ima_max_digest_data struct.
  105. */
  106. struct ima_max_digest_data {
  107. struct ima_digest_data hdr;
  108. u8 digest[HASH_MAX_DIGESTSIZE];
  109. } __packed;
  110. /*
  111. * signature header format v2 - for using with asymmetric keys
  112. *
  113. * The signature_v2_hdr struct includes a signature format version
  114. * to simplify defining new signature formats.
  115. *
  116. * signature format:
  117. * version 2: regular file data hash based signature
  118. * version 3: struct ima_file_id data based signature
  119. */
  120. struct signature_v2_hdr {
  121. uint8_t type; /* xattr type */
  122. uint8_t version; /* signature format version */
  123. uint8_t hash_algo; /* Digest algorithm [enum hash_algo] */
  124. __be32 keyid; /* IMA key identifier - not X509/PGP specific */
  125. __be16 sig_size; /* signature size */
  126. uint8_t sig[]; /* signature payload */
  127. } __packed;
  128. /*
  129. * IMA signature version 3 disambiguates the data that is signed, by
  130. * indirectly signing the hash of the ima_file_id structure data,
  131. * containing either the fsverity_descriptor struct digest or, in the
  132. * future, the regular IMA file hash.
  133. *
  134. * (The hash of the ima_file_id structure is only of the portion used.)
  135. */
  136. struct ima_file_id {
  137. __u8 hash_type; /* xattr type [enum evm_ima_xattr_type] */
  138. __u8 hash_algorithm; /* Digest algorithm [enum hash_algo] */
  139. __u8 hash[HASH_MAX_DIGESTSIZE];
  140. } __packed;
  141. /* integrity data associated with an inode */
  142. struct integrity_iint_cache {
  143. struct rb_node rb_node; /* rooted in integrity_iint_tree */
  144. struct mutex mutex; /* protects: version, flags, digest */
  145. struct inode *inode; /* back pointer to inode in question */
  146. u64 version; /* track inode changes */
  147. unsigned long flags;
  148. unsigned long measured_pcrs;
  149. unsigned long atomic_flags;
  150. unsigned long real_ino;
  151. dev_t real_dev;
  152. enum integrity_status ima_file_status:4;
  153. enum integrity_status ima_mmap_status:4;
  154. enum integrity_status ima_bprm_status:4;
  155. enum integrity_status ima_read_status:4;
  156. enum integrity_status ima_creds_status:4;
  157. enum integrity_status evm_status:4;
  158. struct ima_digest_data *ima_hash;
  159. };
  160. /* rbtree tree calls to lookup, insert, delete
  161. * integrity data associated with an inode.
  162. */
  163. struct integrity_iint_cache *integrity_iint_find(struct inode *inode);
  164. int integrity_kernel_read(struct file *file, loff_t offset,
  165. void *addr, unsigned long count);
  166. #define INTEGRITY_KEYRING_EVM 0
  167. #define INTEGRITY_KEYRING_IMA 1
  168. #define INTEGRITY_KEYRING_PLATFORM 2
  169. #define INTEGRITY_KEYRING_MACHINE 3
  170. #define INTEGRITY_KEYRING_MAX 4
  171. extern struct dentry *integrity_dir;
  172. struct modsig;
  173. #ifdef CONFIG_INTEGRITY_SIGNATURE
  174. int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen,
  175. const char *digest, int digestlen);
  176. int integrity_modsig_verify(unsigned int id, const struct modsig *modsig);
  177. int __init integrity_init_keyring(const unsigned int id);
  178. int __init integrity_load_x509(const unsigned int id, const char *path);
  179. int __init integrity_load_cert(const unsigned int id, const char *source,
  180. const void *data, size_t len, key_perm_t perm);
  181. #else
  182. static inline int integrity_digsig_verify(const unsigned int id,
  183. const char *sig, int siglen,
  184. const char *digest, int digestlen)
  185. {
  186. return -EOPNOTSUPP;
  187. }
  188. static inline int integrity_modsig_verify(unsigned int id,
  189. const struct modsig *modsig)
  190. {
  191. return -EOPNOTSUPP;
  192. }
  193. static inline int integrity_init_keyring(const unsigned int id)
  194. {
  195. return 0;
  196. }
  197. static inline int __init integrity_load_cert(const unsigned int id,
  198. const char *source,
  199. const void *data, size_t len,
  200. key_perm_t perm)
  201. {
  202. return 0;
  203. }
  204. #endif /* CONFIG_INTEGRITY_SIGNATURE */
  205. #ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS
  206. int asymmetric_verify(struct key *keyring, const char *sig,
  207. int siglen, const char *data, int datalen);
  208. #else
  209. static inline int asymmetric_verify(struct key *keyring, const char *sig,
  210. int siglen, const char *data, int datalen)
  211. {
  212. return -EOPNOTSUPP;
  213. }
  214. #endif
  215. #ifdef CONFIG_IMA_APPRAISE_MODSIG
  216. int ima_modsig_verify(struct key *keyring, const struct modsig *modsig);
  217. #else
  218. static inline int ima_modsig_verify(struct key *keyring,
  219. const struct modsig *modsig)
  220. {
  221. return -EOPNOTSUPP;
  222. }
  223. #endif
  224. #ifdef CONFIG_IMA_LOAD_X509
  225. void __init ima_load_x509(void);
  226. #else
  227. static inline void ima_load_x509(void)
  228. {
  229. }
  230. #endif
  231. #ifdef CONFIG_EVM_LOAD_X509
  232. void __init evm_load_x509(void);
  233. #else
  234. static inline void evm_load_x509(void)
  235. {
  236. }
  237. #endif
  238. #ifdef CONFIG_INTEGRITY_AUDIT
  239. /* declarations */
  240. void integrity_audit_msg(int audit_msgno, struct inode *inode,
  241. const unsigned char *fname, const char *op,
  242. const char *cause, int result, int info);
  243. void integrity_audit_message(int audit_msgno, struct inode *inode,
  244. const unsigned char *fname, const char *op,
  245. const char *cause, int result, int info,
  246. int errno);
  247. static inline struct audit_buffer *
  248. integrity_audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type)
  249. {
  250. return audit_log_start(ctx, gfp_mask, type);
  251. }
  252. #else
  253. static inline void integrity_audit_msg(int audit_msgno, struct inode *inode,
  254. const unsigned char *fname,
  255. const char *op, const char *cause,
  256. int result, int info)
  257. {
  258. }
  259. static inline void integrity_audit_message(int audit_msgno,
  260. struct inode *inode,
  261. const unsigned char *fname,
  262. const char *op, const char *cause,
  263. int result, int info, int errno)
  264. {
  265. }
  266. static inline struct audit_buffer *
  267. integrity_audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type)
  268. {
  269. return NULL;
  270. }
  271. #endif
  272. #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
  273. void __init add_to_platform_keyring(const char *source, const void *data,
  274. size_t len);
  275. #else
  276. static inline void __init add_to_platform_keyring(const char *source,
  277. const void *data, size_t len)
  278. {
  279. }
  280. #endif
  281. #ifdef CONFIG_INTEGRITY_MACHINE_KEYRING
  282. void __init add_to_machine_keyring(const char *source, const void *data, size_t len);
  283. bool __init trust_moklist(void);
  284. #else
  285. static inline void __init add_to_machine_keyring(const char *source,
  286. const void *data, size_t len)
  287. {
  288. }
  289. static inline bool __init trust_moklist(void)
  290. {
  291. return false;
  292. }
  293. #endif