Skip to content

Commit d9f4bb1

Browse files
committed
KEYS: Use individual pages in big_key for crypto buffers
kmalloc() can't always allocate large enough buffers for big_key to use for crypto (1MB + some metadata) so we cannot use that to allocate the buffer. Further, vmalloc'd pages can't be passed to sg_init_one() and the aead crypto accessors cannot be called progressively and must be passed all the data in one go (which means we can't pass the data in one block at a time). Fix this by allocating the buffer pages individually and passing them through a multientry scatterlist to the crypto layer. This has the bonus advantage that we don't have to allocate a contiguous series of pages. We then vmap() the page list and pass that through to the VFS read/write routines. This can trigger a warning: WARNING: CPU: 0 PID: 60912 at mm/page_alloc.c:3883 __alloc_pages_nodemask+0xb7c/0x15f8 ([<00000000002acbb6>] __alloc_pages_nodemask+0x1ee/0x15f8) [<00000000002dd356>] kmalloc_order+0x46/0x90 [<00000000002dd3e0>] kmalloc_order_trace+0x40/0x1f8 [<0000000000326a10>] __kmalloc+0x430/0x4c0 [<00000000004343e4>] big_key_preparse+0x7c/0x210 [<000000000042c040>] key_create_or_update+0x128/0x420 [<000000000042e52c>] SyS_add_key+0x124/0x220 [<00000000007bba2c>] system_call+0xc4/0x2b0 from the keyctl/padd/useradd test of the keyutils testsuite on s390x. Note that it might be better to shovel data through in page-sized lumps instead as there's no particular need to use a monolithic buffer unless the kernel itself wants to access the data. Fixes: 13100a7 ("Security: Keys: Big keys stored encrypted") Reported-by: Paul Bunyan <pbunyan@redhat.com> Signed-off-by: David Howells <dhowells@redhat.com> cc: Kirill Marinushkin <k.marinushkin@gmail.com>
1 parent 4b34968 commit d9f4bb1

File tree

1 file changed

+87
-23
lines changed

1 file changed

+87
-23
lines changed

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)