Thomas Gleixner | b886d83 | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2009-2010 IBM Corporation |
| 4 | * |
| 5 | * Authors: |
| 6 | * Mimi Zohar <zohar@us.ibm.com> |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/integrity.h> |
| 11 | #include <crypto/sha.h> |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 12 | #include <linux/key.h> |
Stefan Berger | 2afd020 | 2018-06-04 16:54:54 -0400 | [diff] [blame] | 13 | #include <linux/audit.h> |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 14 | |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 15 | /* iint action cache flags */ |
Mimi Zohar | f578c08 | 2012-12-05 09:29:09 -0500 | [diff] [blame] | 16 | #define IMA_MEASURE 0x00000001 |
| 17 | #define IMA_MEASURED 0x00000002 |
| 18 | #define IMA_APPRAISE 0x00000004 |
| 19 | #define IMA_APPRAISED 0x00000008 |
| 20 | /*#define IMA_COLLECT 0x00000010 do not use this flag */ |
| 21 | #define IMA_COLLECTED 0x00000020 |
| 22 | #define IMA_AUDIT 0x00000040 |
| 23 | #define IMA_AUDITED 0x00000080 |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 24 | #define IMA_HASH 0x00000100 |
| 25 | #define IMA_HASHED 0x00000200 |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 26 | |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 27 | /* iint cache flags */ |
Mimi Zohar | f578c08 | 2012-12-05 09:29:09 -0500 | [diff] [blame] | 28 | #define IMA_ACTION_FLAGS 0xff000000 |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 29 | #define IMA_DIGSIG_REQUIRED 0x01000000 |
| 30 | #define IMA_PERMIT_DIRECTIO 0x02000000 |
| 31 | #define IMA_NEW_FILE 0x04000000 |
| 32 | #define EVM_IMMUTABLE_DIGSIG 0x08000000 |
Mimi Zohar | 9e67028 | 2018-02-21 11:36:32 -0500 | [diff] [blame] | 33 | #define IMA_FAIL_UNVERIFIABLE_SIGS 0x10000000 |
Dmitry Kasatkin | 45e2472 | 2012-09-12 20:51:32 +0300 | [diff] [blame] | 34 | |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 35 | #define IMA_DO_MASK (IMA_MEASURE | IMA_APPRAISE | IMA_AUDIT | \ |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 36 | IMA_HASH | IMA_APPRAISE_SUBMASK) |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 37 | #define IMA_DONE_MASK (IMA_MEASURED | IMA_APPRAISED | IMA_AUDITED | \ |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 38 | IMA_HASHED | IMA_COLLECTED | \ |
| 39 | IMA_APPRAISED_SUBMASK) |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 40 | |
| 41 | /* iint subaction appraise cache flags */ |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 42 | #define IMA_FILE_APPRAISE 0x00001000 |
| 43 | #define IMA_FILE_APPRAISED 0x00002000 |
| 44 | #define IMA_MMAP_APPRAISE 0x00004000 |
| 45 | #define IMA_MMAP_APPRAISED 0x00008000 |
| 46 | #define IMA_BPRM_APPRAISE 0x00010000 |
| 47 | #define IMA_BPRM_APPRAISED 0x00020000 |
| 48 | #define IMA_READ_APPRAISE 0x00040000 |
| 49 | #define IMA_READ_APPRAISED 0x00080000 |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 50 | #define IMA_CREDS_APPRAISE 0x00100000 |
| 51 | #define IMA_CREDS_APPRAISED 0x00200000 |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 52 | #define IMA_APPRAISE_SUBMASK (IMA_FILE_APPRAISE | IMA_MMAP_APPRAISE | \ |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 53 | IMA_BPRM_APPRAISE | IMA_READ_APPRAISE | \ |
| 54 | IMA_CREDS_APPRAISE) |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 55 | #define IMA_APPRAISED_SUBMASK (IMA_FILE_APPRAISED | IMA_MMAP_APPRAISED | \ |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 56 | IMA_BPRM_APPRAISED | IMA_READ_APPRAISED | \ |
| 57 | IMA_CREDS_APPRAISED) |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 58 | |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 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 | |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 66 | enum evm_ima_xattr_type { |
| 67 | IMA_XATTR_DIGEST = 0x01, |
| 68 | EVM_XATTR_HMAC, |
| 69 | EVM_IMA_XATTR_DIGSIG, |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 70 | IMA_XATTR_DIGEST_NG, |
Matthew Garrett | 50b9774 | 2017-11-07 07:17:42 -0800 | [diff] [blame] | 71 | EVM_XATTR_PORTABLE_DIGSIG, |
Dmitry Kasatkin | a48fda9 | 2014-10-28 13:31:22 +0200 | [diff] [blame] | 72 | IMA_XATTR_LAST |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | struct evm_ima_xattr_data { |
| 76 | u8 type; |
Thiago Jung Bauermann | 650b29d | 2019-06-11 03:28:08 -0300 | [diff] [blame] | 77 | u8 data[]; |
| 78 | } __packed; |
| 79 | |
| 80 | /* Only used in the EVM HMAC code. */ |
| 81 | struct evm_xattr { |
| 82 | struct evm_ima_xattr_data data; |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 83 | u8 digest[SHA1_DIGEST_SIZE]; |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 84 | } __packed; |
| 85 | |
| 86 | #define IMA_MAX_DIGEST_SIZE 64 |
| 87 | |
| 88 | struct ima_digest_data { |
| 89 | u8 algo; |
| 90 | u8 length; |
Dmitry Kasatkin | 3ea7a56 | 2013-08-12 11:22:51 +0300 | [diff] [blame] | 91 | union { |
| 92 | struct { |
| 93 | u8 unused; |
| 94 | u8 type; |
| 95 | } sha1; |
| 96 | struct { |
| 97 | u8 type; |
| 98 | u8 algo; |
| 99 | } ng; |
| 100 | u8 data[2]; |
| 101 | } xattr; |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 102 | u8 digest[0]; |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 103 | } __packed; |
Dmitry Kasatkin | 6be5cc5 | 2011-03-09 14:28:20 -0500 | [diff] [blame] | 104 | |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 105 | /* |
| 106 | * signature format v2 - for using with asymmetric keys |
| 107 | */ |
| 108 | struct signature_v2_hdr { |
Dmitry Kasatkin | b1aaab2 | 2013-10-10 16:12:03 +0900 | [diff] [blame] | 109 | uint8_t type; /* xattr type */ |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 110 | uint8_t version; /* signature format version */ |
David Howells | 4e8ae72 | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 111 | uint8_t hash_algo; /* Digest algorithm [enum hash_algo] */ |
Thiago Jung Bauermann | bb543e3 | 2017-06-07 22:49:10 -0300 | [diff] [blame] | 112 | __be32 keyid; /* IMA key identifier - not X509/PGP specific */ |
| 113 | __be16 sig_size; /* signature size */ |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 114 | uint8_t sig[0]; /* signature payload */ |
| 115 | } __packed; |
| 116 | |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 117 | /* integrity data associated with an inode */ |
| 118 | struct integrity_iint_cache { |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 119 | struct rb_node rb_node; /* rooted in integrity_iint_tree */ |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 120 | struct mutex mutex; /* protects: version, flags, digest */ |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 121 | struct inode *inode; /* back pointer to inode in question */ |
| 122 | u64 version; /* track inode changes */ |
Mimi Zohar | f578c08 | 2012-12-05 09:29:09 -0500 | [diff] [blame] | 123 | unsigned long flags; |
Eric Richter | 96d450b | 2016-06-01 13:14:00 -0500 | [diff] [blame] | 124 | unsigned long measured_pcrs; |
Dmitry Kasatkin | 0d73a55 | 2017-12-05 21:06:34 +0200 | [diff] [blame] | 125 | unsigned long atomic_flags; |
Mimi Zohar | d79d72e | 2012-12-03 17:08:11 -0500 | [diff] [blame] | 126 | enum integrity_status ima_file_status:4; |
| 127 | enum integrity_status ima_mmap_status:4; |
| 128 | enum integrity_status ima_bprm_status:4; |
Mimi Zohar | cf22221 | 2016-01-14 17:57:47 -0500 | [diff] [blame] | 129 | enum integrity_status ima_read_status:4; |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 130 | enum integrity_status ima_creds_status:4; |
Dmitry Kasatkin | ee86633 | 2012-09-21 17:00:43 +0300 | [diff] [blame] | 131 | enum integrity_status evm_status:4; |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 132 | struct ima_digest_data *ima_hash; |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | /* rbtree tree calls to lookup, insert, delete |
| 136 | * integrity data associated with an inode. |
| 137 | */ |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 138 | struct integrity_iint_cache *integrity_iint_find(struct inode *inode); |
James Morris | 4892722 | 2011-08-17 10:34:33 +1000 | [diff] [blame] | 139 | |
Dmitry Kasatkin | e3c4abb | 2014-11-05 17:01:12 +0200 | [diff] [blame] | 140 | int integrity_kernel_read(struct file *file, loff_t offset, |
Thiago Jung Bauermann | bb543e3 | 2017-06-07 22:49:10 -0300 | [diff] [blame] | 141 | void *addr, unsigned long count); |
| 142 | |
Dmitry Kasatkin | 8607c50 | 2011-10-05 11:54:46 +0300 | [diff] [blame] | 143 | #define INTEGRITY_KEYRING_EVM 0 |
Dmitry Kasatkin | f4dc377 | 2015-10-22 21:26:10 +0300 | [diff] [blame] | 144 | #define INTEGRITY_KEYRING_IMA 1 |
Thiago Jung Bauermann | c7f7e58 | 2018-12-12 23:39:09 -0200 | [diff] [blame] | 145 | #define INTEGRITY_KEYRING_PLATFORM 2 |
| 146 | #define INTEGRITY_KEYRING_MAX 3 |
Dmitry Kasatkin | 8607c50 | 2011-10-05 11:54:46 +0300 | [diff] [blame] | 147 | |
Matthew Garrett | 0c343af | 2018-05-11 16:12:34 -0700 | [diff] [blame] | 148 | extern struct dentry *integrity_dir; |
| 149 | |
Dmitry Kasatkin | f1be242 | 2012-01-17 17:12:07 +0200 | [diff] [blame] | 150 | #ifdef CONFIG_INTEGRITY_SIGNATURE |
Dmitry Kasatkin | 8607c50 | 2011-10-05 11:54:46 +0300 | [diff] [blame] | 151 | |
| 152 | int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen, |
Dmitry Kasatkin | 089bc8e | 2013-10-10 15:56:13 +0900 | [diff] [blame] | 153 | const char *digest, int digestlen); |
Dmitry Kasatkin | 8607c50 | 2011-10-05 11:54:46 +0300 | [diff] [blame] | 154 | |
Dmitry Kasatkin | d16a858 | 2014-10-01 21:43:07 +0300 | [diff] [blame] | 155 | int __init integrity_init_keyring(const unsigned int id); |
Dmitry Kasatkin | 9d03a72 | 2014-11-26 16:55:00 +0200 | [diff] [blame] | 156 | int __init integrity_load_x509(const unsigned int id, const char *path); |
Nayna Jain | 60740ac | 2018-12-09 01:57:00 +0530 | [diff] [blame] | 157 | int __init integrity_load_cert(const unsigned int id, const char *source, |
Linus Torvalds | 028db3e | 2019-07-10 18:43:43 -0700 | [diff] [blame^] | 158 | const void *data, size_t len, key_perm_t perm); |
Dmitry Kasatkin | 8607c50 | 2011-10-05 11:54:46 +0300 | [diff] [blame] | 159 | #else |
| 160 | |
| 161 | static inline int integrity_digsig_verify(const unsigned int id, |
| 162 | const char *sig, int siglen, |
| 163 | const char *digest, int digestlen) |
| 164 | { |
| 165 | return -EOPNOTSUPP; |
| 166 | } |
| 167 | |
Mimi Zohar | 7d2ce23 | 2013-08-13 08:47:43 -0400 | [diff] [blame] | 168 | static inline int integrity_init_keyring(const unsigned int id) |
| 169 | { |
| 170 | return 0; |
| 171 | } |
Nayna Jain | 60740ac | 2018-12-09 01:57:00 +0530 | [diff] [blame] | 172 | |
| 173 | static inline int __init integrity_load_cert(const unsigned int id, |
| 174 | const char *source, |
| 175 | const void *data, size_t len, |
Linus Torvalds | 028db3e | 2019-07-10 18:43:43 -0700 | [diff] [blame^] | 176 | key_perm_t perm) |
Nayna Jain | 60740ac | 2018-12-09 01:57:00 +0530 | [diff] [blame] | 177 | { |
| 178 | return 0; |
| 179 | } |
Dmitry Kasatkin | f1be242 | 2012-01-17 17:12:07 +0200 | [diff] [blame] | 180 | #endif /* CONFIG_INTEGRITY_SIGNATURE */ |
Dmitry Kasatkin | 8607c50 | 2011-10-05 11:54:46 +0300 | [diff] [blame] | 181 | |
Dmitry Kasatkin | e075125 | 2013-02-07 00:12:08 +0200 | [diff] [blame] | 182 | #ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS |
| 183 | int asymmetric_verify(struct key *keyring, const char *sig, |
| 184 | int siglen, const char *data, int datalen); |
| 185 | #else |
| 186 | static inline int asymmetric_verify(struct key *keyring, const char *sig, |
| 187 | int siglen, const char *data, int datalen) |
| 188 | { |
| 189 | return -EOPNOTSUPP; |
| 190 | } |
| 191 | #endif |
| 192 | |
Dmitry Kasatkin | fd5f4e90 | 2014-11-05 17:01:14 +0200 | [diff] [blame] | 193 | #ifdef CONFIG_IMA_LOAD_X509 |
| 194 | void __init ima_load_x509(void); |
| 195 | #else |
| 196 | static inline void ima_load_x509(void) |
| 197 | { |
| 198 | } |
| 199 | #endif |
| 200 | |
Dmitry Kasatkin | 2ce523e | 2015-10-22 21:26:21 +0300 | [diff] [blame] | 201 | #ifdef CONFIG_EVM_LOAD_X509 |
| 202 | void __init evm_load_x509(void); |
| 203 | #else |
| 204 | static inline void evm_load_x509(void) |
| 205 | { |
| 206 | } |
| 207 | #endif |
| 208 | |
Mimi Zohar | d726d8d | 2013-03-18 14:48:02 -0400 | [diff] [blame] | 209 | #ifdef CONFIG_INTEGRITY_AUDIT |
| 210 | /* declarations */ |
| 211 | void integrity_audit_msg(int audit_msgno, struct inode *inode, |
| 212 | const unsigned char *fname, const char *op, |
| 213 | const char *cause, int result, int info); |
Stefan Berger | 2afd020 | 2018-06-04 16:54:54 -0400 | [diff] [blame] | 214 | |
| 215 | static inline struct audit_buffer * |
| 216 | integrity_audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type) |
| 217 | { |
| 218 | return audit_log_start(ctx, gfp_mask, type); |
| 219 | } |
| 220 | |
Mimi Zohar | d726d8d | 2013-03-18 14:48:02 -0400 | [diff] [blame] | 221 | #else |
| 222 | static inline void integrity_audit_msg(int audit_msgno, struct inode *inode, |
| 223 | const unsigned char *fname, |
| 224 | const char *op, const char *cause, |
| 225 | int result, int info) |
| 226 | { |
| 227 | } |
Stefan Berger | 2afd020 | 2018-06-04 16:54:54 -0400 | [diff] [blame] | 228 | |
| 229 | static inline struct audit_buffer * |
| 230 | integrity_audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, int type) |
| 231 | { |
| 232 | return NULL; |
| 233 | } |
| 234 | |
Mimi Zohar | d726d8d | 2013-03-18 14:48:02 -0400 | [diff] [blame] | 235 | #endif |
Nayna Jain | 60740ac | 2018-12-09 01:57:00 +0530 | [diff] [blame] | 236 | |
| 237 | #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING |
| 238 | void __init add_to_platform_keyring(const char *source, const void *data, |
| 239 | size_t len); |
| 240 | #else |
| 241 | static inline void __init add_to_platform_keyring(const char *source, |
| 242 | const void *data, size_t len) |
| 243 | { |
| 244 | } |
| 245 | #endif |