Commit | Line | Data |
---|---|---|
b886d83c | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
f381c272 MZ |
2 | /* |
3 | * Copyright (C) 2009-2010 IBM Corporation | |
4 | * | |
5 | * Authors: | |
6 | * Mimi Zohar <zohar@us.ibm.com> | |
f381c272 MZ |
7 | */ |
8 | ||
555d6d71 TS |
9 | #ifdef pr_fmt |
10 | #undef pr_fmt | |
11 | #endif | |
12 | ||
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
14 | ||
f381c272 MZ |
15 | #include <linux/types.h> |
16 | #include <linux/integrity.h> | |
a24d22b2 | 17 | #include <crypto/sha1.h> |
8c54135e | 18 | #include <crypto/hash.h> |
e0751257 | 19 | #include <linux/key.h> |
2afd020a | 20 | #include <linux/audit.h> |
f381c272 | 21 | |
45e2472e | 22 | /* iint action cache flags */ |
f578c08e MZ |
23 | #define IMA_MEASURE 0x00000001 |
24 | #define IMA_MEASURED 0x00000002 | |
25 | #define IMA_APPRAISE 0x00000004 | |
26 | #define IMA_APPRAISED 0x00000008 | |
27 | /*#define IMA_COLLECT 0x00000010 do not use this flag */ | |
28 | #define IMA_COLLECTED 0x00000020 | |
29 | #define IMA_AUDIT 0x00000040 | |
30 | #define IMA_AUDITED 0x00000080 | |
da1b0029 MZ |
31 | #define IMA_HASH 0x00000100 |
32 | #define IMA_HASHED 0x00000200 | |
45e2472e | 33 | |
aae6ccbd MZ |
34 | /* iint policy rule cache flags */ |
35 | #define IMA_NONACTION_FLAGS 0xff000000 | |
0d73a552 DK |
36 | #define IMA_DIGSIG_REQUIRED 0x01000000 |
37 | #define IMA_PERMIT_DIRECTIO 0x02000000 | |
38 | #define IMA_NEW_FILE 0x04000000 | |
39 | #define EVM_IMMUTABLE_DIGSIG 0x08000000 | |
9e67028e | 40 | #define IMA_FAIL_UNVERIFIABLE_SIGS 0x10000000 |
9044d627 | 41 | #define IMA_MODSIG_ALLOWED 0x20000000 |
273df864 | 42 | #define IMA_CHECK_BLACKLIST 0x40000000 |
54f03916 | 43 | #define IMA_VERITY_REQUIRED 0x80000000 |
45e2472e | 44 | |
d79d72e0 | 45 | #define IMA_DO_MASK (IMA_MEASURE | IMA_APPRAISE | IMA_AUDIT | \ |
da1b0029 | 46 | IMA_HASH | IMA_APPRAISE_SUBMASK) |
d79d72e0 | 47 | #define IMA_DONE_MASK (IMA_MEASURED | IMA_APPRAISED | IMA_AUDITED | \ |
da1b0029 MZ |
48 | IMA_HASHED | IMA_COLLECTED | \ |
49 | IMA_APPRAISED_SUBMASK) | |
d79d72e0 MZ |
50 | |
51 | /* iint subaction appraise cache flags */ | |
da1b0029 MZ |
52 | #define IMA_FILE_APPRAISE 0x00001000 |
53 | #define IMA_FILE_APPRAISED 0x00002000 | |
54 | #define IMA_MMAP_APPRAISE 0x00004000 | |
55 | #define IMA_MMAP_APPRAISED 0x00008000 | |
56 | #define IMA_BPRM_APPRAISE 0x00010000 | |
57 | #define IMA_BPRM_APPRAISED 0x00020000 | |
58 | #define IMA_READ_APPRAISE 0x00040000 | |
59 | #define IMA_READ_APPRAISED 0x00080000 | |
d906c10d MG |
60 | #define IMA_CREDS_APPRAISE 0x00100000 |
61 | #define IMA_CREDS_APPRAISED 0x00200000 | |
d79d72e0 | 62 | #define IMA_APPRAISE_SUBMASK (IMA_FILE_APPRAISE | IMA_MMAP_APPRAISE | \ |
d906c10d MG |
63 | IMA_BPRM_APPRAISE | IMA_READ_APPRAISE | \ |
64 | IMA_CREDS_APPRAISE) | |
d79d72e0 | 65 | #define IMA_APPRAISED_SUBMASK (IMA_FILE_APPRAISED | IMA_MMAP_APPRAISED | \ |
d906c10d MG |
66 | IMA_BPRM_APPRAISED | IMA_READ_APPRAISED | \ |
67 | IMA_CREDS_APPRAISED) | |
f381c272 | 68 | |
0d73a552 DK |
69 | /* iint cache atomic_flags */ |
70 | #define IMA_CHANGE_XATTR 0 | |
71 | #define IMA_UPDATE_XATTR 1 | |
72 | #define IMA_CHANGE_ATTR 2 | |
73 | #define IMA_DIGSIG 3 | |
74 | #define IMA_MUST_MEASURE 4 | |
75 | ||
6be5cc52 DK |
76 | enum evm_ima_xattr_type { |
77 | IMA_XATTR_DIGEST = 0x01, | |
78 | EVM_XATTR_HMAC, | |
79 | EVM_IMA_XATTR_DIGSIG, | |
3ea7a560 | 80 | IMA_XATTR_DIGEST_NG, |
50b97748 | 81 | EVM_XATTR_PORTABLE_DIGSIG, |
398c42e2 | 82 | IMA_VERITY_DIGSIG, |
a48fda9d | 83 | IMA_XATTR_LAST |
6be5cc52 DK |
84 | }; |
85 | ||
86 | struct evm_ima_xattr_data { | |
87 | u8 type; | |
650b29db TJB |
88 | u8 data[]; |
89 | } __packed; | |
90 | ||
91 | /* Only used in the EVM HMAC code. */ | |
92 | struct evm_xattr { | |
93 | struct evm_ima_xattr_data data; | |
6be5cc52 | 94 | u8 digest[SHA1_DIGEST_SIZE]; |
c7c8bb23 DK |
95 | } __packed; |
96 | ||
398c42e2 | 97 | #define IMA_MAX_DIGEST_SIZE HASH_MAX_DIGESTSIZE |
c7c8bb23 DK |
98 | |
99 | struct ima_digest_data { | |
100 | u8 algo; | |
101 | u8 length; | |
3ea7a560 DK |
102 | union { |
103 | struct { | |
104 | u8 unused; | |
105 | u8 type; | |
106 | } sha1; | |
107 | struct { | |
108 | u8 type; | |
109 | u8 algo; | |
110 | } ng; | |
111 | u8 data[2]; | |
112 | } xattr; | |
eb492c62 | 113 | u8 digest[]; |
c7c8bb23 | 114 | } __packed; |
6be5cc52 | 115 | |
8c54135e MZ |
116 | /* |
117 | * Instead of wrapping the ima_digest_data struct inside a local structure | |
118 | * with the maximum hash size, define ima_max_digest_data struct. | |
119 | */ | |
120 | struct ima_max_digest_data { | |
121 | struct ima_digest_data hdr; | |
122 | u8 digest[HASH_MAX_DIGESTSIZE]; | |
123 | } __packed; | |
124 | ||
d3634d0f | 125 | /* |
398c42e2 MZ |
126 | * signature header format v2 - for using with asymmetric keys |
127 | * | |
128 | * The signature_v2_hdr struct includes a signature format version | |
129 | * to simplify defining new signature formats. | |
130 | * | |
131 | * signature format: | |
132 | * version 2: regular file data hash based signature | |
133 | * version 3: struct ima_file_id data based signature | |
d3634d0f DK |
134 | */ |
135 | struct signature_v2_hdr { | |
b1aaab22 | 136 | uint8_t type; /* xattr type */ |
d3634d0f | 137 | uint8_t version; /* signature format version */ |
4e8ae72a | 138 | uint8_t hash_algo; /* Digest algorithm [enum hash_algo] */ |
bb543e39 TJB |
139 | __be32 keyid; /* IMA key identifier - not X509/PGP specific */ |
140 | __be16 sig_size; /* signature size */ | |
eb492c62 | 141 | uint8_t sig[]; /* signature payload */ |
d3634d0f DK |
142 | } __packed; |
143 | ||
398c42e2 MZ |
144 | /* |
145 | * IMA signature version 3 disambiguates the data that is signed, by | |
146 | * indirectly signing the hash of the ima_file_id structure data, | |
147 | * containing either the fsverity_descriptor struct digest or, in the | |
148 | * future, the regular IMA file hash. | |
149 | * | |
150 | * (The hash of the ima_file_id structure is only of the portion used.) | |
151 | */ | |
152 | struct ima_file_id { | |
153 | __u8 hash_type; /* xattr type [enum evm_ima_xattr_type] */ | |
154 | __u8 hash_algorithm; /* Digest algorithm [enum hash_algo] */ | |
155 | __u8 hash[HASH_MAX_DIGESTSIZE]; | |
156 | } __packed; | |
157 | ||
f381c272 MZ |
158 | /* integrity data associated with an inode */ |
159 | struct integrity_iint_cache { | |
c7c8bb23 | 160 | struct rb_node rb_node; /* rooted in integrity_iint_tree */ |
0d73a552 | 161 | struct mutex mutex; /* protects: version, flags, digest */ |
f381c272 MZ |
162 | struct inode *inode; /* back pointer to inode in question */ |
163 | u64 version; /* track inode changes */ | |
f578c08e | 164 | unsigned long flags; |
96d450bb | 165 | unsigned long measured_pcrs; |
0d73a552 | 166 | unsigned long atomic_flags; |
b836c4d2 MZ |
167 | unsigned long real_ino; |
168 | dev_t real_dev; | |
d79d72e0 MZ |
169 | enum integrity_status ima_file_status:4; |
170 | enum integrity_status ima_mmap_status:4; | |
171 | enum integrity_status ima_bprm_status:4; | |
cf222217 | 172 | enum integrity_status ima_read_status:4; |
d906c10d | 173 | enum integrity_status ima_creds_status:4; |
ee866331 | 174 | enum integrity_status evm_status:4; |
a35c3fb6 | 175 | struct ima_digest_data *ima_hash; |
f381c272 MZ |
176 | }; |
177 | ||
178 | /* rbtree tree calls to lookup, insert, delete | |
179 | * integrity data associated with an inode. | |
180 | */ | |
f381c272 | 181 | struct integrity_iint_cache *integrity_iint_find(struct inode *inode); |
4892722e | 182 | |
e3c4abbf | 183 | int integrity_kernel_read(struct file *file, loff_t offset, |
bb543e39 TJB |
184 | void *addr, unsigned long count); |
185 | ||
8607c501 | 186 | #define INTEGRITY_KEYRING_EVM 0 |
f4dc3778 | 187 | #define INTEGRITY_KEYRING_IMA 1 |
c7f7e58f | 188 | #define INTEGRITY_KEYRING_PLATFORM 2 |
d1996776 ES |
189 | #define INTEGRITY_KEYRING_MACHINE 3 |
190 | #define INTEGRITY_KEYRING_MAX 4 | |
8607c501 | 191 | |
0c343af8 MG |
192 | extern struct dentry *integrity_dir; |
193 | ||
39b07096 TJB |
194 | struct modsig; |
195 | ||
f1be242c | 196 | #ifdef CONFIG_INTEGRITY_SIGNATURE |
8607c501 DK |
197 | |
198 | int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen, | |
089bc8e9 | 199 | const char *digest, int digestlen); |
39b07096 | 200 | int integrity_modsig_verify(unsigned int id, const struct modsig *modsig); |
8607c501 | 201 | |
d16a8585 | 202 | int __init integrity_init_keyring(const unsigned int id); |
9d03a721 | 203 | int __init integrity_load_x509(const unsigned int id, const char *path); |
60740acc | 204 | int __init integrity_load_cert(const unsigned int id, const char *source, |
028db3e2 | 205 | const void *data, size_t len, key_perm_t perm); |
8607c501 DK |
206 | #else |
207 | ||
208 | static inline int integrity_digsig_verify(const unsigned int id, | |
209 | const char *sig, int siglen, | |
210 | const char *digest, int digestlen) | |
211 | { | |
212 | return -EOPNOTSUPP; | |
213 | } | |
214 | ||
39b07096 TJB |
215 | static inline int integrity_modsig_verify(unsigned int id, |
216 | const struct modsig *modsig) | |
217 | { | |
218 | return -EOPNOTSUPP; | |
219 | } | |
220 | ||
7d2ce232 MZ |
221 | static inline int integrity_init_keyring(const unsigned int id) |
222 | { | |
223 | return 0; | |
224 | } | |
60740acc NJ |
225 | |
226 | static inline int __init integrity_load_cert(const unsigned int id, | |
227 | const char *source, | |
228 | const void *data, size_t len, | |
028db3e2 | 229 | key_perm_t perm) |
60740acc NJ |
230 | { |
231 | return 0; | |
232 | } | |
f1be242c | 233 | #endif /* CONFIG_INTEGRITY_SIGNATURE */ |
8607c501 | 234 | |
e0751257 DK |
235 | #ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS |
236 | int asymmetric_verify(struct key *keyring, const char *sig, | |
237 | int siglen, const char *data, int datalen); | |
238 | #else | |
239 | static inline int asymmetric_verify(struct key *keyring, const char *sig, | |
240 | int siglen, const char *data, int datalen) | |
241 | { | |
242 | return -EOPNOTSUPP; | |
243 | } | |
244 | #endif | |
245 | ||
39b07096 TJB |
246 | #ifdef CONFIG_IMA_APPRAISE_MODSIG |
247 | int ima_modsig_verify(struct key *keyring, const struct modsig *modsig); | |
248 | #else | |
249 | static inline int ima_modsig_verify(struct key *keyring, | |
250 | const struct modsig *modsig) | |
251 | { | |
252 | return -EOPNOTSUPP; | |
253 | } | |
254 | #endif | |
255 | ||
fd5f4e90 DK |
256 | #ifdef CONFIG_IMA_LOAD_X509 |
257 | void __init ima_load_x509(void); | |
258 | #else | |
259 | static inline void ima_load_x509(void) | |
260 | { | |
261 | } | |
262 | #endif | |
263 | ||
2ce523eb DK |
264 | #ifdef CONFIG_EVM_LOAD_X509 |
265 | void __init evm_load_x509(void); | |
266 | #else | |
267 | static inline void evm_load_x509(void) | |
268 | { | |
269 | } | |
270 | #endif | |
271 | ||
d726d8d7 MZ |
272 | #ifdef CONFIG_INTEGRITY_AUDIT |
273 | /* declarations */ | |
274 | void integrity_audit_msg(int audit_msgno, struct inode *inode, | |
275 | const unsigned char *fname, const char *op, | |
276 | const char *cause, int result, int info); | |
2afd020a | 277 | |
2f845882 LR |
278 | void integrity_audit_message(int audit_msgno, struct inode *inode, |
279 | const unsigned char *fname, const char *op, | |
280 | const char *cause, int result, int info, | |
281 | int errno); | |
282 | ||
2afd020a SB |
283 | static inline struct audit_buffer * |
284 | integrity_audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type) | |
285 | { | |
286 | return audit_log_start(ctx, gfp_mask, type); | |
287 | } | |
288 | ||
d726d8d7 MZ |
289 | #else |
290 | static inline void integrity_audit_msg(int audit_msgno, struct inode *inode, | |
291 | const unsigned char *fname, | |
292 | const char *op, const char *cause, | |
293 | int result, int info) | |
294 | { | |
295 | } | |
2afd020a | 296 | |
2f845882 LR |
297 | static inline void integrity_audit_message(int audit_msgno, |
298 | struct inode *inode, | |
299 | const unsigned char *fname, | |
300 | const char *op, const char *cause, | |
301 | int result, int info, int errno) | |
302 | { | |
303 | } | |
304 | ||
2afd020a SB |
305 | static inline struct audit_buffer * |
306 | integrity_audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type) | |
307 | { | |
308 | return NULL; | |
309 | } | |
310 | ||
d726d8d7 | 311 | #endif |
60740acc NJ |
312 | |
313 | #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING | |
314 | void __init add_to_platform_keyring(const char *source, const void *data, | |
315 | size_t len); | |
316 | #else | |
317 | static inline void __init add_to_platform_keyring(const char *source, | |
318 | const void *data, size_t len) | |
319 | { | |
320 | } | |
321 | #endif | |
d1996776 ES |
322 | |
323 | #ifdef CONFIG_INTEGRITY_MACHINE_KEYRING | |
324 | void __init add_to_machine_keyring(const char *source, const void *data, size_t len); | |
4cb1ed94 | 325 | bool __init imputed_trust_enabled(void); |
d1996776 ES |
326 | #else |
327 | static inline void __init add_to_machine_keyring(const char *source, | |
328 | const void *data, size_t len) | |
329 | { | |
330 | } | |
4cb1ed94 NJ |
331 | |
332 | static inline bool __init imputed_trust_enabled(void) | |
3d6ae1a5 ES |
333 | { |
334 | return false; | |
335 | } | |
d1996776 | 336 | #endif |