Skip to content

Commit 2faac9a

Browse files
committed
Merge tag 'keys-fixes-20230321' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs
Pull keyrings fixes from David Howells: - Fix request_key() so that it doesn't cache a looked up key on the current thread if that thread is a kernel thread. The cache is cleared during notify_resume - but that doesn't happen in kernel threads. This is causing cifs DNS keys to be un-invalidateable. - Fix a wrapper check in verify_pefile() to not round up the length. - Change asymmetric_keys code to log errors to make it easier for users to work out why failures occurred. * tag 'keys-fixes-20230321' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs: asymmetric_keys: log on fatal failures in PE/pkcs7 verify_pefile: relax wrapper length check keys: Do not cache key in task struct if key is requested from kernel thread
2 parents 17214b7 + 3584c1d commit 2faac9a

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

crypto/asymmetric_keys/pkcs7_verify.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,16 @@ static int pkcs7_digest(struct pkcs7_message *pkcs7,
7979
}
8080

8181
if (sinfo->msgdigest_len != sig->digest_size) {
82-
pr_debug("Sig %u: Invalid digest size (%u)\n",
83-
sinfo->index, sinfo->msgdigest_len);
82+
pr_warn("Sig %u: Invalid digest size (%u)\n",
83+
sinfo->index, sinfo->msgdigest_len);
8484
ret = -EBADMSG;
8585
goto error;
8686
}
8787

8888
if (memcmp(sig->digest, sinfo->msgdigest,
8989
sinfo->msgdigest_len) != 0) {
90-
pr_debug("Sig %u: Message digest doesn't match\n",
91-
sinfo->index);
90+
pr_warn("Sig %u: Message digest doesn't match\n",
91+
sinfo->index);
9292
ret = -EKEYREJECTED;
9393
goto error;
9494
}
@@ -478,7 +478,7 @@ int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
478478
const void *data, size_t datalen)
479479
{
480480
if (pkcs7->data) {
481-
pr_debug("Data already supplied\n");
481+
pr_warn("Data already supplied\n");
482482
return -EINVAL;
483483
}
484484
pkcs7->data = data;

crypto/asymmetric_keys/verify_pefile.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static int pefile_parse_binary(const void *pebuf, unsigned int pelen,
7474
break;
7575

7676
default:
77-
pr_debug("Unknown PEOPT magic = %04hx\n", pe32->magic);
77+
pr_warn("Unknown PEOPT magic = %04hx\n", pe32->magic);
7878
return -ELIBBAD;
7979
}
8080

@@ -95,7 +95,7 @@ static int pefile_parse_binary(const void *pebuf, unsigned int pelen,
9595
ctx->certs_size = ddir->certs.size;
9696

9797
if (!ddir->certs.virtual_address || !ddir->certs.size) {
98-
pr_debug("Unsigned PE binary\n");
98+
pr_warn("Unsigned PE binary\n");
9999
return -ENODATA;
100100
}
101101

@@ -127,27 +127,31 @@ static int pefile_strip_sig_wrapper(const void *pebuf,
127127
unsigned len;
128128

129129
if (ctx->sig_len < sizeof(wrapper)) {
130-
pr_debug("Signature wrapper too short\n");
130+
pr_warn("Signature wrapper too short\n");
131131
return -ELIBBAD;
132132
}
133133

134134
memcpy(&wrapper, pebuf + ctx->sig_offset, sizeof(wrapper));
135135
pr_debug("sig wrapper = { %x, %x, %x }\n",
136136
wrapper.length, wrapper.revision, wrapper.cert_type);
137137

138-
/* Both pesign and sbsign round up the length of certificate table
139-
* (in optional header data directories) to 8 byte alignment.
138+
/* sbsign rounds up the length of certificate table (in optional
139+
* header data directories) to 8 byte alignment. However, the PE
140+
* specification states that while entries are 8-byte aligned, this is
141+
* not included in their length, and as a result, pesign has not
142+
* rounded up since 0.110.
140143
*/
141-
if (round_up(wrapper.length, 8) != ctx->sig_len) {
142-
pr_debug("Signature wrapper len wrong\n");
144+
if (wrapper.length > ctx->sig_len) {
145+
pr_warn("Signature wrapper bigger than sig len (%x > %x)\n",
146+
ctx->sig_len, wrapper.length);
143147
return -ELIBBAD;
144148
}
145149
if (wrapper.revision != WIN_CERT_REVISION_2_0) {
146-
pr_debug("Signature is not revision 2.0\n");
150+
pr_warn("Signature is not revision 2.0\n");
147151
return -ENOTSUPP;
148152
}
149153
if (wrapper.cert_type != WIN_CERT_TYPE_PKCS_SIGNED_DATA) {
150-
pr_debug("Signature certificate type is not PKCS\n");
154+
pr_warn("Signature certificate type is not PKCS\n");
151155
return -ENOTSUPP;
152156
}
153157

@@ -160,7 +164,7 @@ static int pefile_strip_sig_wrapper(const void *pebuf,
160164
ctx->sig_offset += sizeof(wrapper);
161165
ctx->sig_len -= sizeof(wrapper);
162166
if (ctx->sig_len < 4) {
163-
pr_debug("Signature data missing\n");
167+
pr_warn("Signature data missing\n");
164168
return -EKEYREJECTED;
165169
}
166170

@@ -194,7 +198,7 @@ static int pefile_strip_sig_wrapper(const void *pebuf,
194198
return 0;
195199
}
196200
not_pkcs7:
197-
pr_debug("Signature data not PKCS#7\n");
201+
pr_warn("Signature data not PKCS#7\n");
198202
return -ELIBBAD;
199203
}
200204

@@ -337,8 +341,8 @@ static int pefile_digest_pe(const void *pebuf, unsigned int pelen,
337341
digest_size = crypto_shash_digestsize(tfm);
338342

339343
if (digest_size != ctx->digest_len) {
340-
pr_debug("Digest size mismatch (%zx != %x)\n",
341-
digest_size, ctx->digest_len);
344+
pr_warn("Digest size mismatch (%zx != %x)\n",
345+
digest_size, ctx->digest_len);
342346
ret = -EBADMSG;
343347
goto error_no_desc;
344348
}
@@ -369,7 +373,7 @@ static int pefile_digest_pe(const void *pebuf, unsigned int pelen,
369373
* PKCS#7 certificate.
370374
*/
371375
if (memcmp(digest, ctx->digest, ctx->digest_len) != 0) {
372-
pr_debug("Digest mismatch\n");
376+
pr_warn("Digest mismatch\n");
373377
ret = -EKEYREJECTED;
374378
} else {
375379
pr_debug("The digests match!\n");

security/keys/request_key.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ static void cache_requested_key(struct key *key)
3838
#ifdef CONFIG_KEYS_REQUEST_CACHE
3939
struct task_struct *t = current;
4040

41-
key_put(t->cached_requested_key);
42-
t->cached_requested_key = key_get(key);
43-
set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
41+
/* Do not cache key if it is a kernel thread */
42+
if (!(t->flags & PF_KTHREAD)) {
43+
key_put(t->cached_requested_key);
44+
t->cached_requested_key = key_get(key);
45+
set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
46+
}
4447
#endif
4548
}
4649

0 commit comments

Comments
 (0)