ima: move to generic async completion

ima starts several async crypto ops and  waits for their completions.
Move it over to generic code doing the same.

Signed-off-by: Gilad Ben-Yossef <gilad@benyossef.com>
Acked-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This commit is contained in:
Gilad Ben-Yossef 2017-10-18 08:00:47 +01:00 committed by Herbert Xu
parent a5186b85fe
commit 46f1414c8a
1 changed files with 17 additions and 39 deletions

View File

@ -27,11 +27,6 @@
#include "ima.h" #include "ima.h"
struct ahash_completion {
struct completion completion;
int err;
};
/* minimum file size for ahash use */ /* minimum file size for ahash use */
static unsigned long ima_ahash_minsize; static unsigned long ima_ahash_minsize;
module_param_named(ahash_minsize, ima_ahash_minsize, ulong, 0644); module_param_named(ahash_minsize, ima_ahash_minsize, ulong, 0644);
@ -196,30 +191,13 @@ static void ima_free_atfm(struct crypto_ahash *tfm)
crypto_free_ahash(tfm); crypto_free_ahash(tfm);
} }
static void ahash_complete(struct crypto_async_request *req, int err) static inline int ahash_wait(int err, struct crypto_wait *wait)
{ {
struct ahash_completion *res = req->data;
if (err == -EINPROGRESS) err = crypto_wait_req(err, wait);
return;
res->err = err;
complete(&res->completion);
}
static int ahash_wait(int err, struct ahash_completion *res) if (err)
{
switch (err) {
case 0:
break;
case -EINPROGRESS:
case -EBUSY:
wait_for_completion(&res->completion);
reinit_completion(&res->completion);
err = res->err;
/* fall through */
default:
pr_crit_ratelimited("ahash calculation failed: err: %d\n", err); pr_crit_ratelimited("ahash calculation failed: err: %d\n", err);
}
return err; return err;
} }
@ -233,7 +211,7 @@ static int ima_calc_file_hash_atfm(struct file *file,
int rc, read = 0, rbuf_len, active = 0, ahash_rc = 0; 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 crypto_wait wait;
size_t rbuf_size[2]; size_t rbuf_size[2];
hash->length = crypto_ahash_digestsize(tfm); hash->length = crypto_ahash_digestsize(tfm);
@ -242,12 +220,12 @@ static int ima_calc_file_hash_atfm(struct file *file,
if (!req) if (!req)
return -ENOMEM; return -ENOMEM;
init_completion(&res.completion); crypto_init_wait(&wait);
ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
CRYPTO_TFM_REQ_MAY_SLEEP, CRYPTO_TFM_REQ_MAY_SLEEP,
ahash_complete, &res); crypto_req_done, &wait);
rc = ahash_wait(crypto_ahash_init(req), &res); rc = ahash_wait(crypto_ahash_init(req), &wait);
if (rc) if (rc)
goto out1; goto out1;
@ -288,7 +266,7 @@ static int ima_calc_file_hash_atfm(struct file *file,
* read/request, wait for the completion of the * read/request, wait for the completion of the
* previous ahash_update() request. * previous ahash_update() request.
*/ */
rc = ahash_wait(ahash_rc, &res); rc = ahash_wait(ahash_rc, &wait);
if (rc) if (rc)
goto out3; goto out3;
} }
@ -304,7 +282,7 @@ static int ima_calc_file_hash_atfm(struct file *file,
* read/request, wait for the completion of the * read/request, wait for the completion of the
* previous ahash_update() request. * previous ahash_update() request.
*/ */
rc = ahash_wait(ahash_rc, &res); rc = ahash_wait(ahash_rc, &wait);
if (rc) if (rc)
goto out3; goto out3;
} }
@ -318,7 +296,7 @@ static int ima_calc_file_hash_atfm(struct file *file,
active = !active; /* swap buffers, if we use two */ active = !active; /* swap buffers, if we use two */
} }
/* wait for the last update request to complete */ /* wait for the last update request to complete */
rc = ahash_wait(ahash_rc, &res); rc = ahash_wait(ahash_rc, &wait);
out3: out3:
if (read) if (read)
file->f_mode &= ~FMODE_READ; file->f_mode &= ~FMODE_READ;
@ -327,7 +305,7 @@ out3:
out2: out2:
if (!rc) { if (!rc) {
ahash_request_set_crypt(req, NULL, hash->digest, 0); ahash_request_set_crypt(req, NULL, hash->digest, 0);
rc = ahash_wait(crypto_ahash_final(req), &res); rc = ahash_wait(crypto_ahash_final(req), &wait);
} }
out1: out1:
ahash_request_free(req); ahash_request_free(req);
@ -527,7 +505,7 @@ static int calc_buffer_ahash_atfm(const void *buf, loff_t len,
{ {
struct ahash_request *req; struct ahash_request *req;
struct scatterlist sg; struct scatterlist sg;
struct ahash_completion res; struct crypto_wait wait;
int rc, ahash_rc = 0; int rc, ahash_rc = 0;
hash->length = crypto_ahash_digestsize(tfm); hash->length = crypto_ahash_digestsize(tfm);
@ -536,12 +514,12 @@ static int calc_buffer_ahash_atfm(const void *buf, loff_t len,
if (!req) if (!req)
return -ENOMEM; return -ENOMEM;
init_completion(&res.completion); crypto_init_wait(&wait);
ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
CRYPTO_TFM_REQ_MAY_SLEEP, CRYPTO_TFM_REQ_MAY_SLEEP,
ahash_complete, &res); crypto_req_done, &wait);
rc = ahash_wait(crypto_ahash_init(req), &res); rc = ahash_wait(crypto_ahash_init(req), &wait);
if (rc) if (rc)
goto out; goto out;
@ -551,10 +529,10 @@ static int calc_buffer_ahash_atfm(const void *buf, loff_t len,
ahash_rc = crypto_ahash_update(req); ahash_rc = crypto_ahash_update(req);
/* wait for the update request to complete */ /* wait for the update request to complete */
rc = ahash_wait(ahash_rc, &res); rc = ahash_wait(ahash_rc, &wait);
if (!rc) { if (!rc) {
ahash_request_set_crypt(req, NULL, hash->digest, 0); ahash_request_set_crypt(req, NULL, hash->digest, 0);
rc = ahash_wait(crypto_ahash_final(req), &res); rc = ahash_wait(crypto_ahash_final(req), &wait);
} }
out: out:
ahash_request_free(req); ahash_request_free(req);