History

Merge tag 'for-next-keys-7.2-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd

Pull keys update from Jarkko Sakkinen: "This contains only bug fixes" * tag 'for-next-keys-7.2-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd: keys: keyctl_pkey: replace BUG with return -EOPNOTSUPP keys: request_key: replace BUG with return -EINVAL keys: Pin request_key_auth payload in instantiate paths keys: prevent slab cache merging for key_jar keys: Replace strcpy(derived_buf, "AUTH_KEY") with strscpy(..., HASH_SIZE) KEYS: Use acquire when reading state in keyring search keys/trusted_keys: mark 'migratable' as __ro_after_init keys: use kmalloc_flex in user_preparse KEYS: trusted: Debugging as a feature KEYS: encrypted: Remove unnecessary selection of CRYPTO_RNG KEYS: fix overflow in keyctl_pkey_params_get_2()

Linus Torvalds committed 4 months ago

Commit e2c05952 parents 0798268 1b9524217 files · +158 −49Browse files

Unified · Split

17 files
  1. Documentation/admin-guide/kernel-parameters.txt +16 −0
  2. include/keys/request_key_auth-type.h +2 −0
  3. include/keys/trusted-type.h +12 −9
  4. security/keys/Kconfig +0 −1
  5. security/keys/encrypted-keys/encrypted.c +2 −2
  6. security/keys/internal.h +2 −0
  7. security/keys/key.c +1 −1
  8. security/keys/keyctl.c +18 −6
  9. security/keys/keyctl_pkey.c +11 −3
  10. security/keys/keyring.c +1 −1
  11. security/keys/request_key.c +1 −1
  12. security/keys/request_key_auth.c +31 −2
  13. security/keys/trusted-keys/Kconfig +23 −0
  14. security/keys/trusted-keys/trusted_caam.c +5 −2
  15. security/keys/trusted-keys/trusted_core.c +7 −1
  16. security/keys/trusted-keys/trusted_tpm1.c +25 −19
  17. security/keys/user_defined.c +1 −1
Documentation/admin-guide/kernel-parameters.txt+16−0
@@ -7872,6 +7872,22 @@ Kernel parameters · 7871 unmodified lines
78727872 first trust source as a backend which is initialized
78737873 successfully during iteration.
78747874
7875+ trusted.debug= [KEYS]
7876+ Format: <bool>
7877+ Enable trusted keys debug traces at runtime when
7878+ CONFIG_TRUSTED_KEYS_DEBUG=y.
7879+
7880+ To make the traces visible after enabling the option,
7881+ use trusted.dyndbg='+p' as needed. By convention,
7882+ the subsystem uses pr_debug() for these traces.
7883+
7884+ SAFETY: The traces can leak sensitive data, so be
7885+ cautious before enabling this. They remain inactive
7886+ unless this parameter is set this option to a true
7887+ value.
7888+
7889+ Default: false
7890+
78757891 trusted.rng= [KEYS]
78767892 Format: <string>
78777893 The RNG used to generate key material for trusted keys.
include/keys/request_key_auth-type.h+2−0
@@ -9,12 +9,14 @@ · 8 unmodified lines
99 #define _KEYS_REQUEST_KEY_AUTH_TYPE_H
1010
1111 #include <linux/key.h>
12+#include <linux/refcount.h>
1213
1314 /*
1415 * Authorisation record for request_key().
1516 */
1617 struct request_key_auth {
1718 struct rcu_head rcu;
19+ refcount_t usage;
1820 struct key *target_key;
1921 struct key *dest_keyring;
2022 const struct cred *cred;
include/keys/trusted-type.h+12−9
@@ -83,18 +83,21 @@ struct trusted_key_source { · 82 unmodified lines
8383
8484 extern struct key_type key_type_trusted;
8585
86−#define TRUSTED_DEBUG 0
86+#ifdef CONFIG_TRUSTED_KEYS_DEBUG
87+extern bool trusted_debug;
8788
88−#if TRUSTED_DEBUG
8989 static inline void dump_payload(struct trusted_key_payload *p)
9090 {
91− pr_info("key_len %d\n", p->key_len);
92− print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE,
93− 16, 1, p->key, p->key_len, 0);
94− pr_info("bloblen %d\n", p->blob_len);
95− print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE,
96− 16, 1, p->blob, p->blob_len, 0);
97− pr_info("migratable %d\n", p->migratable);
91+ if (!trusted_debug)
92+ return;
93+
94+ pr_debug("key_len %d\n", p->key_len);
95+ print_hex_dump_debug("key ", DUMP_PREFIX_NONE,
96+ 16, 1, p->key, p->key_len, 0);
97+ pr_debug("bloblen %d\n", p->blob_len);
98+ print_hex_dump_debug("blob ", DUMP_PREFIX_NONE,
99+ 16, 1, p->blob, p->blob_len, 0);
100+ pr_debug("migratable %d\n", p->migratable);
98101 }
99102 #else
100103 static inline void dump_payload(struct trusted_key_payload *p)
security/keys/Kconfig+0−1
@@ -87,7 +87,6 @@ config ENCRYPTED_KEYS · 86 unmodified lines
8787 select CRYPTO_AES
8888 select CRYPTO_CBC
8989 select CRYPTO_LIB_SHA256
90− select CRYPTO_RNG
9190 help
9291 This option provides support for create/encrypting/decrypting keys
9392 in the kernel. Encrypted keys are instantiated using kernel
security/keys/encrypted-keys/encrypted.c+2−2
@@ -343,9 +343,9 @@ static int get_derived_key(u8 *derived_key, enum derived_key_type key_type, · 342 unmodified lines
343343 return -ENOMEM;
344344
345345 if (key_type)
346− strcpy(derived_buf, "AUTH_KEY");
346+ strscpy(derived_buf, "AUTH_KEY", HASH_SIZE);
347347 else
348− strcpy(derived_buf, "ENC_KEY");
348+ strscpy(derived_buf, "ENC_KEY", HASH_SIZE);
349349
350350 memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
351351 master_keylen);
security/keys/internal.h+2−0
@@ -208,6 +208,8 @@ extern struct key *request_key_auth_new(struct key *target, · 207 unmodified lines
208208 const void *callout_info,
209209 size_t callout_len,
210210 struct key *dest_keyring);
211+struct request_key_auth *request_key_auth_get(struct key *authkey);
212+void request_key_auth_put(struct request_key_auth *rka);
211213
212214 extern struct key *key_get_instantiation_authkey(key_serial_t target_id);
213215
security/keys/key.c+1−1
@@ -1275,7 +1275,7 @@ void __init key_init(void) · 1274 unmodified lines
12751275 {
12761276 /* allocate a slab in which we can store keys */
12771277 key_jar = kmem_cache_create("key_jar", sizeof(struct key),
1278− 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1278+ 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_NO_MERGE, NULL);
12791279
12801280 /* add the special key types */
12811281 list_add_tail(&key_type_keyring.link, &key_types_list);
security/keys/keyctl.c+18−6
@@ -1197,9 +1197,13 @@ static long keyctl_instantiate_key_common(key_serial_t id, · 1196 unmodified lines
11971197 if (!instkey)
11981198 goto error;
11991199
1200− rka = instkey->payload.data[0];
1201− if (rka->target_key->serial != id)
1200+ rka = request_key_auth_get(instkey);
1201+ if (!rka) {
1202+ ret = -EKEYREVOKED;
12021203 goto error;
1204+ }
1205+ if (rka->target_key->serial != id)
1206+ goto error_put_rka;
12031207
12041208 /* pull the payload in if one was supplied */
12051209 payload = NULL;
@@ -1208,7 +1212,7 @@ static long keyctl_instantiate_key_common(key_serial_t id, · 2 unmodified lines
12081212 ret = -ENOMEM;
12091213 payload = kvmalloc(plen, GFP_KERNEL);
12101214 if (!payload)
1211− goto error;
1215+ goto error_put_rka;
12121216
12131217 ret = -EFAULT;
12141218 if (!copy_from_iter_full(payload, plen, from))
@@ -1234,6 +1238,8 @@ static long keyctl_instantiate_key_common(key_serial_t id, · 19 unmodified lines
12341238
12351239 error2:
12361240 kvfree_sensitive(payload, plen);
1241+error_put_rka:
1242+ request_key_auth_put(rka);
12371243 error:
12381244 return ret;
12391245 }
@@ -1358,15 +1364,19 @@ long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error, · 118 unmodified lines
13581364 if (!instkey)
13591365 goto error;
13601366
1361− rka = instkey->payload.data[0];
1362− if (rka->target_key->serial != id)
1367+ rka = request_key_auth_get(instkey);
1368+ if (!rka) {
1369+ ret = -EKEYREVOKED;
13631370 goto error;
1371+ }
1372+ if (rka->target_key->serial != id)
1373+ goto error_put_rka;
13641374
13651375 /* find the destination keyring if present (which must also be
13661376 * writable) */
13671377 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
13681378 if (ret < 0)
1369− goto error;
1379+ goto error_put_rka;
13701380
13711381 /* instantiate the key and link it into a keyring */
13721382 ret = key_reject_and_link(rka->target_key, timeout, error,
@@ -1379,6 +1389,8 @@ long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error, · 6 unmodified lines
13791389 if (ret == 0)
13801390 keyctl_change_reqkey_auth(NULL);
13811391
1392+error_put_rka:
1393+ request_key_auth_put(rka);
13821394 error:
13831395 return ret;
13841396 }
security/keys/keyctl_pkey.c+11−3
@@ -138,28 +138,35 @@ static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_par · 137 unmodified lines
138138 if (uparams.in_len > info.max_dec_size ||
139139 uparams.out_len > info.max_enc_size)
140140 return -EINVAL;
141+
142+ params->out_len = info.max_enc_size;
141143 break;
142144 case KEYCTL_PKEY_DECRYPT:
143145 if (uparams.in_len > info.max_enc_size ||
144146 uparams.out_len > info.max_dec_size)
145147 return -EINVAL;
148+
149+ params->out_len = info.max_dec_size;
146150 break;
147151 case KEYCTL_PKEY_SIGN:
148152 if (uparams.in_len > info.max_data_size ||
149153 uparams.out_len > info.max_sig_size)
150154 return -EINVAL;
155+
156+ params->out_len = info.max_sig_size;
151157 break;
152158 case KEYCTL_PKEY_VERIFY:
153159 if (uparams.in_len > info.max_data_size ||
154160 uparams.in2_len > info.max_sig_size)
155161 return -EINVAL;
162+
163+ params->out_len = info.max_sig_size;
156164 break;
157165 default:
158− BUG();
166+ return -EOPNOTSUPP;
159167 }
160168
161169 params->in_len = uparams.in_len;
162− params->out_len = uparams.out_len; /* Note: same as in2_len */
163170 return 0;
164171 }
165172
@@ -238,7 +245,8 @@ long keyctl_pkey_e_d_s(int op, · 72 unmodified lines
238245 params.op = kernel_pkey_sign;
239246 break;
240247 default:
241− BUG();
248+ ret = -EOPNOTSUPP;
249+ goto error_params;
242250 }
243251
244252 in = memdup_user(_in, params.in_len);
security/keys/keyring.c+1−1
@@ -576,7 +576,7 @@ static int keyring_search_iterator(const void *object, void *iterator_data) · 575 unmodified lines
576576 struct keyring_search_context *ctx = iterator_data;
577577 const struct key *key = keyring_ptr_to_key(object);
578578 unsigned long kflags = READ_ONCE(key->flags);
579− short state = READ_ONCE(key->state);
579+ short state = key_read_state(key);
580580
581581 kenter("{%d}", key->serial);
582582
security/keys/request_key.c+1−1
@@ -332,7 +332,7 @@ static int construct_get_dest_keyring(struct key **_dest_keyring) · 331 unmodified lines
332332
333333 case KEY_REQKEY_DEFL_GROUP_KEYRING:
334334 default:
335− BUG();
335+ return -EINVAL;
336336 }
337337
338338 /*
security/keys/request_key_auth.c+31−2
@@ -23,6 +23,7 @@ static void request_key_auth_describe(const struct key *, struct seq_file *); · 22 unmodified lines
2323 static void request_key_auth_revoke(struct key *);
2424 static void request_key_auth_destroy(struct key *);
2525 static long request_key_auth_read(const struct key *, char *, size_t);
26+static void request_key_auth_rcu_disposal(struct rcu_head *);
2627
2728 /*
2829 * The request-key authorisation key type definition.
@@ -115,6 +116,31 @@ static void free_request_key_auth(struct request_key_auth *rka) · 86 unmodified lines
115116 kfree(rka);
116117 }
117118
119+/*
120+ * Take a reference to the request-key authorisation payload so callers can
121+ * drop authkey->sem before doing operations that may sleep.
122+ */
123+struct request_key_auth *request_key_auth_get(struct key *authkey)
124+{
125+ struct request_key_auth *rka;
126+
127+ down_read(&authkey->sem);
128+ rka = dereference_key_locked(authkey);
129+ if (rka && !test_bit(KEY_FLAG_REVOKED, &authkey->flags))
130+ refcount_inc(&rka->usage);
131+ else
132+ rka = NULL;
133+ up_read(&authkey->sem);
134+
135+ return rka;
136+}
137+
138+void request_key_auth_put(struct request_key_auth *rka)
139+{
140+ if (rka && refcount_dec_and_test(&rka->usage))
141+ call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
142+}
143+
118144 /*
119145 * Dispose of the request_key_auth record under RCU conditions
120146 */
@@ -136,8 +162,10 @@ static void request_key_auth_revoke(struct key *key) · 15 unmodified lines
136162 struct request_key_auth *rka = dereference_key_locked(key);
137163
138164 kenter("{%d}", key->serial);
165+ if (!rka)
166+ return;
139167 rcu_assign_keypointer(key, NULL);
140− call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
168+ request_key_auth_put(rka);
141169 }
142170
143171 /*
@@ -150,7 +178,7 @@ static void request_key_auth_destroy(struct key *key) · 6 unmodified lines
150178 kenter("{%d}", key->serial);
151179 if (rka) {
152180 rcu_assign_keypointer(key, NULL);
153− call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
181+ request_key_auth_put(rka);
154182 }
155183 }
156184
@@ -174,6 +202,7 @@ struct key *request_key_auth_new(struct key *target, const char *op, · 17 unmodified lines
174202 rka = kzalloc_obj(*rka);
175203 if (!rka)
176204 goto error;
205+ refcount_set(&rka->usage, 1);
177206 rka->callout_info = kmemdup(callout_info, callout_len, GFP_KERNEL);
178207 if (!rka->callout_info)
179208 goto error_free_rka;
security/keys/trusted-keys/Kconfig+23−0
@@ -1,10 +1,29 @@
11 config HAVE_TRUSTED_KEYS
22 bool
33
4+config HAVE_TRUSTED_KEYS_DEBUG
5+ bool
6+
7+config TRUSTED_KEYS_DEBUG
8+ bool "Debug trusted keys"
9+ depends on HAVE_TRUSTED_KEYS_DEBUG
10+ default n
11+ help
12+ Trusted key backends and core code that support debug traces can
13+ opt-in that feature here. Traces must only use debug level output, as
14+ sensitive data may pass by. In the kernel-command line traces can be
15+ enabled via trusted.dyndbg='+p'.
16+
17+ SAFETY: Debug dumps are inactive at runtime until trusted.debug is set
18+ to a true value on the kernel command-line. Use at your utmost
19+ consideration when enabling this feature on a production build. The
20+ general advice is not to do this.
21+
422 config TRUSTED_KEYS_TPM
523 bool "TPM-based trusted keys"
624 depends on TCG_TPM >= TRUSTED_KEYS
725 default y
26+ select HAVE_TRUSTED_KEYS_DEBUG
827 select CRYPTO_HASH_INFO
928 select CRYPTO_LIB_SHA1
1029 select CRYPTO_LIB_UTILS
@@ -23,6 +42,7 @@ config TRUSTED_KEYS_TEE · 12 unmodified lines
2342 bool "TEE-based trusted keys"
2443 depends on TEE >= TRUSTED_KEYS
2544 default y
45+ select HAVE_TRUSTED_KEYS_DEBUG
2646 select HAVE_TRUSTED_KEYS
2747 help
2848 Enable use of the Trusted Execution Environment (TEE) as trusted
@@ -33,6 +53,7 @@ config TRUSTED_KEYS_CAAM · 4 unmodified lines
3353 depends on CRYPTO_DEV_FSL_CAAM_JR >= TRUSTED_KEYS
3454 select CRYPTO_DEV_FSL_CAAM_BLOB_GEN
3555 default y
56+ select HAVE_TRUSTED_KEYS_DEBUG
3657 select HAVE_TRUSTED_KEYS
3758 help
3859 Enable use of NXP's Cryptographic Accelerator and Assurance Module
@@ -42,6 +63,7 @@ config TRUSTED_KEYS_DCP · 3 unmodified lines
4263 bool "DCP-based trusted keys"
4364 depends on CRYPTO_DEV_MXS_DCP >= TRUSTED_KEYS
4465 default y
66+ select HAVE_TRUSTED_KEYS_DEBUG
4567 select HAVE_TRUSTED_KEYS
4668 help
4769 Enable use of NXP's DCP (Data Co-Processor) as trusted key backend.
@@ -50,6 +72,7 @@ config TRUSTED_KEYS_PKWM · 2 unmodified lines
5072 bool "PKWM-based trusted keys"
5173 depends on PSERIES_PLPKS >= TRUSTED_KEYS
5274 default y
75+ select HAVE_TRUSTED_KEYS_DEBUG
5376 select HAVE_TRUSTED_KEYS
5477 help
5578 Enable use of IBM PowerVM Key Wrapping Module (PKWM) as a trusted key backend.
security/keys/trusted-keys/trusted_caam.c+5−2
@@ -28,10 +28,13 @@ static const match_table_t key_tokens = { · 27 unmodified lines
2828 {opt_err, NULL}
2929 };
3030
31−#ifdef CAAM_DEBUG
31+#ifdef CONFIG_TRUSTED_KEYS_DEBUG
3232 static inline void dump_options(const struct caam_pkey_info *pkey_info)
3333 {
34− pr_info("key encryption algo %d\n", pkey_info->key_enc_algo);
34+ if (!trusted_debug)
35+ return;
36+
37+ pr_debug("key encryption algo %d\n", pkey_info->key_enc_algo);
3538 }
3639 #else
3740 static inline void dump_options(const struct caam_pkey_info *pkey_info)
security/keys/trusted-keys/trusted_core.c+7−1
@@ -31,6 +31,12 @@ static char *trusted_rng = "default"; · 30 unmodified lines
3131 module_param_named(rng, trusted_rng, charp, 0);
3232 MODULE_PARM_DESC(rng, "Select trusted key RNG");
3333
34+#ifdef CONFIG_TRUSTED_KEYS_DEBUG
35+bool trusted_debug;
36+module_param_named(debug, trusted_debug, bool, 0);
37+MODULE_PARM_DESC(debug, "Enable trusted keys debug traces (default: 0)");
38+#endif
39+
3440 static char *trusted_key_source;
3541 module_param_named(source, trusted_key_source, charp, 0);
3642 MODULE_PARM_DESC(source, "Select trusted keys source (tpm, tee, caam, dcp or pkwm)");
@@ -59,7 +65,7 @@ DEFINE_STATIC_CALL_NULL(trusted_key_unseal, · 22 unmodified lines
5965 DEFINE_STATIC_CALL_NULL(trusted_key_get_random,
6066 *trusted_key_sources[0].ops->get_random);
6167 static void (*trusted_key_exit)(void);
62−static unsigned char migratable;
68+static unsigned char migratable __ro_after_init;
6369
6470 enum {
6571 Opt_err,
security/keys/trusted-keys/trusted_tpm1.c+25−19
@@ -46,38 +46,44 @@ enum { · 45 unmodified lines
4646 SRK_keytype = 4
4747 };
4848
49−#define TPM_DEBUG 0
50−
51−#if TPM_DEBUG
49+#ifdef CONFIG_TRUSTED_KEYS_DEBUG
5250 static inline void dump_options(struct trusted_key_options *o)
5351 {
54− pr_info("sealing key type %d\n", o->keytype);
55− pr_info("sealing key handle %0X\n", o->keyhandle);
56− pr_info("pcrlock %d\n", o->pcrlock);
57− pr_info("pcrinfo %d\n", o->pcrinfo_len);
58− print_hex_dump(KERN_INFO, "pcrinfo ", DUMP_PREFIX_NONE,
59− 16, 1, o->pcrinfo, o->pcrinfo_len, 0);
52+ if (!trusted_debug)
53+ return;
54+
55+ pr_debug("sealing key type %d\n", o->keytype);
56+ pr_debug("sealing key handle %0X\n", o->keyhandle);
57+ pr_debug("pcrlock %d\n", o->pcrlock);
58+ pr_debug("pcrinfo %d\n", o->pcrinfo_len);
59+ print_hex_dump_debug("pcrinfo ", DUMP_PREFIX_NONE,
60+ 16, 1, o->pcrinfo, o->pcrinfo_len, 0);
6061 }
6162
6263 static inline void dump_sess(struct osapsess *s)
6364 {
64− print_hex_dump(KERN_INFO, "trusted-key: handle ", DUMP_PREFIX_NONE,
65− 16, 1, &s->handle, 4, 0);
66− pr_info("secret:\n");
67− print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
68− 16, 1, &s->secret, SHA1_DIGEST_SIZE, 0);
69− pr_info("trusted-key: enonce:\n");
70− print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
71− 16, 1, &s->enonce, SHA1_DIGEST_SIZE, 0);
65+ if (!trusted_debug)
66+ return;
67+
68+ print_hex_dump_debug("trusted-key: handle ", DUMP_PREFIX_NONE,
69+ 16, 1, &s->handle, 4, 0);
70+ pr_debug("secret:\n");
71+ print_hex_dump_debug("", DUMP_PREFIX_NONE,
72+ 16, 1, &s->secret, SHA1_DIGEST_SIZE, 0);
73+ pr_debug("trusted-key: enonce:\n");
74+ print_hex_dump_debug("", DUMP_PREFIX_NONE,
75+ 16, 1, &s->enonce, SHA1_DIGEST_SIZE, 0);
7276 }
7377
7478 static inline void dump_tpm_buf(unsigned char *buf)
7579 {
7680 int len;
7781
78− pr_info("\ntpm buffer\n");
82+ if (!trusted_debug)
83+ return;
84+ pr_debug("\ntpm buffer\n");
7985 len = LOAD32(buf, TPM_SIZE_OFFSET);
80− print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, buf, len, 0);
86+ print_hex_dump_debug("", DUMP_PREFIX_NONE, 16, 1, buf, len, 0);
8187 }
8288 #else
8389 static inline void dump_options(struct trusted_key_options *o)
security/keys/user_defined.c+1−1
@@ -64,7 +64,7 @@ int user_preparse(struct key_preparsed_payload *prep) · 63 unmodified lines
6464 if (datalen == 0 || datalen > 32767 || !prep->data)
6565 return -EINVAL;
6666
67− upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
67+ upayload = kmalloc_flex(*upayload, data, datalen);
6868 if (!upayload)
6969 return -ENOMEM;
7070