提交 32c2e675 编写于 作者: D Dmitry Kasatkin 提交者: Mimi Zohar

ima: provide double buffering for hash calculation

The asynchronous hash API allows initiating a hash calculation and
then performing other tasks, while waiting for the hash calculation
to complete.

This patch introduces usage of double buffering for simultaneous
hashing and reading of the next chunk of data from storage.

Changes in v3:
- better comments
Signed-off-by: NDmitry Kasatkin <d.kasatkin@samsung.com>
Signed-off-by: NMimi Zohar <zohar@linux.vnet.ibm.com>
上级 6edf7a89
...@@ -253,12 +253,12 @@ static int ima_calc_file_hash_atfm(struct file *file, ...@@ -253,12 +253,12 @@ static int ima_calc_file_hash_atfm(struct file *file,
struct crypto_ahash *tfm) struct crypto_ahash *tfm)
{ {
loff_t i_size, offset; loff_t i_size, offset;
char *rbuf; char *rbuf[2] = { NULL, };
int rc, read = 0, rbuf_len; int rc, read = 0, rbuf_len, active = 0, ahash_rc = 0;
struct ahash_request *req; struct ahash_request *req;
struct scatterlist sg[1]; struct scatterlist sg[1];
struct ahash_completion res; struct ahash_completion res;
size_t rbuf_size; size_t rbuf_size[2];
hash->length = crypto_ahash_digestsize(tfm); hash->length = crypto_ahash_digestsize(tfm);
...@@ -284,36 +284,69 @@ static int ima_calc_file_hash_atfm(struct file *file, ...@@ -284,36 +284,69 @@ static int ima_calc_file_hash_atfm(struct file *file,
* Try to allocate maximum size of memory. * Try to allocate maximum size of memory.
* Fail if even a single page cannot be allocated. * Fail if even a single page cannot be allocated.
*/ */
rbuf = ima_alloc_pages(i_size, &rbuf_size, 1); rbuf[0] = ima_alloc_pages(i_size, &rbuf_size[0], 1);
if (!rbuf) { if (!rbuf[0]) {
rc = -ENOMEM; rc = -ENOMEM;
goto out1; goto out1;
} }
/* Only allocate one buffer if that is enough. */
if (i_size > rbuf_size[0]) {
/*
* Try to allocate secondary buffer. If that fails fallback to
* using single buffering. Use previous memory allocation size
* as baseline for possible allocation size.
*/
rbuf[1] = ima_alloc_pages(i_size - rbuf_size[0],
&rbuf_size[1], 0);
}
if (!(file->f_mode & FMODE_READ)) { if (!(file->f_mode & FMODE_READ)) {
file->f_mode |= FMODE_READ; file->f_mode |= FMODE_READ;
read = 1; read = 1;
} }
for (offset = 0; offset < i_size; offset += rbuf_len) { for (offset = 0; offset < i_size; offset += rbuf_len) {
rbuf_len = ima_kernel_read(file, offset, rbuf, PAGE_SIZE); if (!rbuf[1] && offset) {
if (rbuf_len < 0) { /* Not using two buffers, and it is not the first
rc = rbuf_len; * read/request, wait for the completion of the
break; * previous ahash_update() request.
*/
rc = ahash_wait(ahash_rc, &res);
if (rc)
goto out3;
}
/* read buffer */
rbuf_len = min_t(loff_t, i_size - offset, rbuf_size[active]);
rc = ima_kernel_read(file, offset, rbuf[active], rbuf_len);
if (rc != rbuf_len)
goto out3;
if (rbuf[1] && offset) {
/* Using two buffers, and it is not the first
* read/request, wait for the completion of the
* previous ahash_update() request.
*/
rc = ahash_wait(ahash_rc, &res);
if (rc)
goto out3;
} }
if (rbuf_len == 0)
break;
sg_init_one(&sg[0], rbuf, rbuf_len); sg_init_one(&sg[0], rbuf[active], rbuf_len);
ahash_request_set_crypt(req, sg, NULL, rbuf_len); ahash_request_set_crypt(req, sg, NULL, rbuf_len);
rc = ahash_wait(crypto_ahash_update(req), &res); ahash_rc = crypto_ahash_update(req);
if (rc)
break; if (rbuf[1])
active = !active; /* swap buffers, if we use two */
} }
/* wait for the last update request to complete */
rc = ahash_wait(ahash_rc, &res);
out3:
if (read) if (read)
file->f_mode &= ~FMODE_READ; file->f_mode &= ~FMODE_READ;
ima_free_pages(rbuf, rbuf_size); ima_free_pages(rbuf[0], rbuf_size[0]);
ima_free_pages(rbuf[1], rbuf_size[1]);
out2: out2:
if (!rc) { if (!rc) {
ahash_request_set_crypt(req, NULL, hash->digest, 0); ahash_request_set_crypt(req, NULL, hash->digest, 0);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册