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