Skip to content

Commit 2eb02aa

Browse files
committed
Merge branch 'fixes-v4.16-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security
Pull security subsystem fixes from James Morris: - keys fixes via David Howells: "A collection of fixes for Linux keyrings, mostly thanks to Eric Biggers: - Fix some PKCS#7 verification issues. - Fix handling of unsupported crypto in X.509. - Fix too-large allocation in big_key" - Seccomp updates via Kees Cook: "These are fixes for the get_metadata interface that landed during -rc1. While the new selftest is strictly not a bug fix, I think it's in the same spirit of avoiding bugs" - an IMA build fix from Randy Dunlap * 'fixes-v4.16-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: integrity/security: fix digsig.c build error with header file KEYS: Use individual pages in big_key for crypto buffers X.509: fix NULL dereference when restricting key with unsupported_sig X.509: fix BUG_ON() when hash algorithm is unsupported PKCS#7: fix direct verification of SignerInfo signature PKCS#7: fix certificate blacklisting PKCS#7: fix certificate chain verification seccomp: add a selftest for get_metadata ptrace, seccomp: tweak get_metadata behavior slightly seccomp, ptrace: switch get_metadata types to arch independent
2 parents 65738c6 + 120f3b1 commit 2eb02aa

File tree

9 files changed

+179
-41
lines changed

9 files changed

+179
-41
lines changed

crypto/asymmetric_keys/pkcs7_trust.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,
106106
pr_devel("sinfo %u: Direct signer is key %x\n",
107107
sinfo->index, key_serial(key));
108108
x509 = NULL;
109+
sig = sinfo->sig;
109110
goto matched;
110111
}
111112
if (PTR_ERR(key) != -ENOKEY)

crypto/asymmetric_keys/pkcs7_verify.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
270270
sinfo->index);
271271
return 0;
272272
}
273-
ret = public_key_verify_signature(p->pub, p->sig);
273+
ret = public_key_verify_signature(p->pub, x509->sig);
274274
if (ret < 0)
275275
return ret;
276276
x509->signer = p;
@@ -366,8 +366,7 @@ static int pkcs7_verify_one(struct pkcs7_message *pkcs7,
366366
*
367367
* (*) -EBADMSG if some part of the message was invalid, or:
368368
*
369-
* (*) 0 if no signature chains were found to be blacklisted or to contain
370-
* unsupported crypto, or:
369+
* (*) 0 if a signature chain passed verification, or:
371370
*
372371
* (*) -EKEYREJECTED if a blacklisted key was encountered, or:
373372
*
@@ -423,8 +422,11 @@ int pkcs7_verify(struct pkcs7_message *pkcs7,
423422

424423
for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
425424
ret = pkcs7_verify_one(pkcs7, sinfo);
426-
if (sinfo->blacklisted && actual_ret == -ENOPKG)
427-
actual_ret = -EKEYREJECTED;
425+
if (sinfo->blacklisted) {
426+
if (actual_ret == -ENOPKG)
427+
actual_ret = -EKEYREJECTED;
428+
continue;
429+
}
428430
if (ret < 0) {
429431
if (ret == -ENOPKG) {
430432
sinfo->unsupported_crypto = true;

crypto/asymmetric_keys/public_key.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ int public_key_verify_signature(const struct public_key *pkey,
7979

8080
BUG_ON(!pkey);
8181
BUG_ON(!sig);
82-
BUG_ON(!sig->digest);
8382
BUG_ON(!sig->s);
8483

84+
if (!sig->digest)
85+
return -ENOPKG;
86+
8587
alg_name = sig->pkey_algo;
8688
if (strcmp(sig->pkey_algo, "rsa") == 0) {
8789
/* The data wangled by the RSA algorithm is typically padded

crypto/asymmetric_keys/restrict.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ __setup("ca_keys=", ca_keys_setup);
6767
*
6868
* Returns 0 if the new certificate was accepted, -ENOKEY if we couldn't find a
6969
* matching parent certificate in the trusted list, -EKEYREJECTED if the
70-
* signature check fails or the key is blacklisted and some other error if
71-
* there is a matching certificate but the signature check cannot be performed.
70+
* signature check fails or the key is blacklisted, -ENOPKG if the signature
71+
* uses unsupported crypto, or some other error if there is a matching
72+
* certificate but the signature check cannot be performed.
7273
*/
7374
int restrict_link_by_signature(struct key *dest_keyring,
7475
const struct key_type *type,
@@ -88,6 +89,8 @@ int restrict_link_by_signature(struct key *dest_keyring,
8889
return -EOPNOTSUPP;
8990

9091
sig = payload->data[asym_auth];
92+
if (!sig)
93+
return -ENOPKG;
9194
if (!sig->auth_ids[0] && !sig->auth_ids[1])
9295
return -ENOKEY;
9396

@@ -139,6 +142,8 @@ static int key_or_keyring_common(struct key *dest_keyring,
139142
return -EOPNOTSUPP;
140143

141144
sig = payload->data[asym_auth];
145+
if (!sig)
146+
return -ENOPKG;
142147
if (!sig->auth_ids[0] && !sig->auth_ids[1])
143148
return -ENOKEY;
144149

@@ -222,9 +227,9 @@ static int key_or_keyring_common(struct key *dest_keyring,
222227
*
223228
* Returns 0 if the new certificate was accepted, -ENOKEY if we
224229
* couldn't find a matching parent certificate in the trusted list,
225-
* -EKEYREJECTED if the signature check fails, and some other error if
226-
* there is a matching certificate but the signature check cannot be
227-
* performed.
230+
* -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
231+
* unsupported crypto, or some other error if there is a matching certificate
232+
* but the signature check cannot be performed.
228233
*/
229234
int restrict_link_by_key_or_keyring(struct key *dest_keyring,
230235
const struct key_type *type,
@@ -249,9 +254,9 @@ int restrict_link_by_key_or_keyring(struct key *dest_keyring,
249254
*
250255
* Returns 0 if the new certificate was accepted, -ENOKEY if we
251256
* couldn't find a matching parent certificate in the trusted list,
252-
* -EKEYREJECTED if the signature check fails, and some other error if
253-
* there is a matching certificate but the signature check cannot be
254-
* performed.
257+
* -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
258+
* unsupported crypto, or some other error if there is a matching certificate
259+
* but the signature check cannot be performed.
255260
*/
256261
int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring,
257262
const struct key_type *type,

include/uapi/linux/ptrace.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ struct ptrace_peeksiginfo_args {
6969
#define PTRACE_SECCOMP_GET_METADATA 0x420d
7070

7171
struct seccomp_metadata {
72-
unsigned long filter_off; /* Input: which filter */
73-
unsigned int flags; /* Output: filter's flags */
72+
__u64 filter_off; /* Input: which filter */
73+
__u64 flags; /* Output: filter's flags */
7474
};
7575

7676
/* Read signals from a shared (process wide) queue */

kernel/seccomp.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,14 +1076,16 @@ long seccomp_get_metadata(struct task_struct *task,
10761076

10771077
size = min_t(unsigned long, size, sizeof(kmd));
10781078

1079-
if (copy_from_user(&kmd, data, size))
1079+
if (size < sizeof(kmd.filter_off))
1080+
return -EINVAL;
1081+
1082+
if (copy_from_user(&kmd.filter_off, data, sizeof(kmd.filter_off)))
10801083
return -EFAULT;
10811084

10821085
filter = get_nth_filter(task, kmd.filter_off);
10831086
if (IS_ERR(filter))
10841087
return PTR_ERR(filter);
10851088

1086-
memset(&kmd, 0, sizeof(kmd));
10871089
if (filter->log)
10881090
kmd.flags |= SECCOMP_FILTER_FLAG_LOG;
10891091

security/integrity/digsig.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/cred.h>
1919
#include <linux/key-type.h>
2020
#include <linux/digsig.h>
21+
#include <linux/vmalloc.h>
2122
#include <crypto/public_key.h>
2223
#include <keys/system_keyring.h>
2324

security/keys/big_key.c

Lines changed: 87 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
#include <keys/big_key-type.h>
2323
#include <crypto/aead.h>
2424

25+
struct big_key_buf {
26+
unsigned int nr_pages;
27+
void *virt;
28+
struct scatterlist *sg;
29+
struct page *pages[];
30+
};
31+
2532
/*
2633
* Layout of key payload words.
2734
*/
@@ -91,10 +98,9 @@ static DEFINE_MUTEX(big_key_aead_lock);
9198
/*
9299
* Encrypt/decrypt big_key data
93100
*/
94-
static int big_key_crypt(enum big_key_op op, u8 *data, size_t datalen, u8 *key)
101+
static int big_key_crypt(enum big_key_op op, struct big_key_buf *buf, size_t datalen, u8 *key)
95102
{
96103
int ret;
97-
struct scatterlist sgio;
98104
struct aead_request *aead_req;
99105
/* We always use a zero nonce. The reason we can get away with this is
100106
* because we're using a different randomly generated key for every
@@ -109,8 +115,7 @@ static int big_key_crypt(enum big_key_op op, u8 *data, size_t datalen, u8 *key)
109115
return -ENOMEM;
110116

111117
memset(zero_nonce, 0, sizeof(zero_nonce));
112-
sg_init_one(&sgio, data, datalen + (op == BIG_KEY_ENC ? ENC_AUTHTAG_SIZE : 0));
113-
aead_request_set_crypt(aead_req, &sgio, &sgio, datalen, zero_nonce);
118+
aead_request_set_crypt(aead_req, buf->sg, buf->sg, datalen, zero_nonce);
114119
aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
115120
aead_request_set_ad(aead_req, 0);
116121

@@ -129,22 +134,82 @@ static int big_key_crypt(enum big_key_op op, u8 *data, size_t datalen, u8 *key)
129134
return ret;
130135
}
131136

137+
/*
138+
* Free up the buffer.
139+
*/
140+
static void big_key_free_buffer(struct big_key_buf *buf)
141+
{
142+
unsigned int i;
143+
144+
if (buf->virt) {
145+
memset(buf->virt, 0, buf->nr_pages * PAGE_SIZE);
146+
vunmap(buf->virt);
147+
}
148+
149+
for (i = 0; i < buf->nr_pages; i++)
150+
if (buf->pages[i])
151+
__free_page(buf->pages[i]);
152+
153+
kfree(buf);
154+
}
155+
156+
/*
157+
* Allocate a buffer consisting of a set of pages with a virtual mapping
158+
* applied over them.
159+
*/
160+
static void *big_key_alloc_buffer(size_t len)
161+
{
162+
struct big_key_buf *buf;
163+
unsigned int npg = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
164+
unsigned int i, l;
165+
166+
buf = kzalloc(sizeof(struct big_key_buf) +
167+
sizeof(struct page) * npg +
168+
sizeof(struct scatterlist) * npg,
169+
GFP_KERNEL);
170+
if (!buf)
171+
return NULL;
172+
173+
buf->nr_pages = npg;
174+
buf->sg = (void *)(buf->pages + npg);
175+
sg_init_table(buf->sg, npg);
176+
177+
for (i = 0; i < buf->nr_pages; i++) {
178+
buf->pages[i] = alloc_page(GFP_KERNEL);
179+
if (!buf->pages[i])
180+
goto nomem;
181+
182+
l = min_t(size_t, len, PAGE_SIZE);
183+
sg_set_page(&buf->sg[i], buf->pages[i], l, 0);
184+
len -= l;
185+
}
186+
187+
buf->virt = vmap(buf->pages, buf->nr_pages, VM_MAP, PAGE_KERNEL);
188+
if (!buf->virt)
189+
goto nomem;
190+
191+
return buf;
192+
193+
nomem:
194+
big_key_free_buffer(buf);
195+
return NULL;
196+
}
197+
132198
/*
133199
* Preparse a big key
134200
*/
135201
int big_key_preparse(struct key_preparsed_payload *prep)
136202
{
203+
struct big_key_buf *buf;
137204
struct path *path = (struct path *)&prep->payload.data[big_key_path];
138205
struct file *file;
139206
u8 *enckey;
140-
u8 *data = NULL;
141207
ssize_t written;
142-
size_t datalen = prep->datalen;
208+
size_t datalen = prep->datalen, enclen = datalen + ENC_AUTHTAG_SIZE;
143209
int ret;
144210

145-
ret = -EINVAL;
146211
if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
147-
goto error;
212+
return -EINVAL;
148213

149214
/* Set an arbitrary quota */
150215
prep->quotalen = 16;
@@ -157,13 +222,12 @@ int big_key_preparse(struct key_preparsed_payload *prep)
157222
*
158223
* File content is stored encrypted with randomly generated key.
159224
*/
160-
size_t enclen = datalen + ENC_AUTHTAG_SIZE;
161225
loff_t pos = 0;
162226

163-
data = kmalloc(enclen, GFP_KERNEL);
164-
if (!data)
227+
buf = big_key_alloc_buffer(enclen);
228+
if (!buf)
165229
return -ENOMEM;
166-
memcpy(data, prep->data, datalen);
230+
memcpy(buf->virt, prep->data, datalen);
167231

168232
/* generate random key */
169233
enckey = kmalloc(ENC_KEY_SIZE, GFP_KERNEL);
@@ -176,7 +240,7 @@ int big_key_preparse(struct key_preparsed_payload *prep)
176240
goto err_enckey;
177241

178242
/* encrypt aligned data */
179-
ret = big_key_crypt(BIG_KEY_ENC, data, datalen, enckey);
243+
ret = big_key_crypt(BIG_KEY_ENC, buf, datalen, enckey);
180244
if (ret)
181245
goto err_enckey;
182246

@@ -187,7 +251,7 @@ int big_key_preparse(struct key_preparsed_payload *prep)
187251
goto err_enckey;
188252
}
189253

190-
written = kernel_write(file, data, enclen, &pos);
254+
written = kernel_write(file, buf->virt, enclen, &pos);
191255
if (written != enclen) {
192256
ret = written;
193257
if (written >= 0)
@@ -202,7 +266,7 @@ int big_key_preparse(struct key_preparsed_payload *prep)
202266
*path = file->f_path;
203267
path_get(path);
204268
fput(file);
205-
kzfree(data);
269+
big_key_free_buffer(buf);
206270
} else {
207271
/* Just store the data in a buffer */
208272
void *data = kmalloc(datalen, GFP_KERNEL);
@@ -220,7 +284,7 @@ int big_key_preparse(struct key_preparsed_payload *prep)
220284
err_enckey:
221285
kzfree(enckey);
222286
error:
223-
kzfree(data);
287+
big_key_free_buffer(buf);
224288
return ret;
225289
}
226290

@@ -298,15 +362,15 @@ long big_key_read(const struct key *key, char __user *buffer, size_t buflen)
298362
return datalen;
299363

300364
if (datalen > BIG_KEY_FILE_THRESHOLD) {
365+
struct big_key_buf *buf;
301366
struct path *path = (struct path *)&key->payload.data[big_key_path];
302367
struct file *file;
303-
u8 *data;
304368
u8 *enckey = (u8 *)key->payload.data[big_key_data];
305369
size_t enclen = datalen + ENC_AUTHTAG_SIZE;
306370
loff_t pos = 0;
307371

308-
data = kmalloc(enclen, GFP_KERNEL);
309-
if (!data)
372+
buf = big_key_alloc_buffer(enclen);
373+
if (!buf)
310374
return -ENOMEM;
311375

312376
file = dentry_open(path, O_RDONLY, current_cred());
@@ -316,26 +380,26 @@ long big_key_read(const struct key *key, char __user *buffer, size_t buflen)
316380
}
317381

318382
/* read file to kernel and decrypt */
319-
ret = kernel_read(file, data, enclen, &pos);
383+
ret = kernel_read(file, buf->virt, enclen, &pos);
320384
if (ret >= 0 && ret != enclen) {
321385
ret = -EIO;
322386
goto err_fput;
323387
}
324388

325-
ret = big_key_crypt(BIG_KEY_DEC, data, enclen, enckey);
389+
ret = big_key_crypt(BIG_KEY_DEC, buf, enclen, enckey);
326390
if (ret)
327391
goto err_fput;
328392

329393
ret = datalen;
330394

331395
/* copy decrypted data to user */
332-
if (copy_to_user(buffer, data, datalen) != 0)
396+
if (copy_to_user(buffer, buf->virt, datalen) != 0)
333397
ret = -EFAULT;
334398

335399
err_fput:
336400
fput(file);
337401
error:
338-
kzfree(data);
402+
big_key_free_buffer(buf);
339403
} else {
340404
ret = datalen;
341405
if (copy_to_user(buffer, key->payload.data[big_key_data],

0 commit comments

Comments
 (0)