aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2017-04-10 05:27:57 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2017-04-10 07:09:18 -0400
commitef0579b64e93188710d48667cb5e014926af9f1b (patch)
treea2101da972309a21aa17ab489982c66836924c6f
parent4702bbeefb490e315189636a5588628c1151223d (diff)
crypto: ahash - Fix EINPROGRESS notification callback
The ahash API modifies the request's callback function in order to clean up after itself in some corner cases (unaligned final and missing finup). When the request is complete ahash will restore the original callback and everything is fine. However, when the request gets an EBUSY on a full queue, an EINPROGRESS callback is made while the request is still ongoing. In this case the ahash API will incorrectly call its own callback. This patch fixes the problem by creating a temporary request object on the stack which is used to relay EINPROGRESS back to the original completion function. This patch also adds code to preserve the original flags value. Fixes: ab6bf4e5e5e4 ("crypto: hash - Fix the pointer voodoo in...") Cc: <stable@vger.kernel.org> Reported-by: Sabrina Dubroca <sd@queasysnail.net> Tested-by: Sabrina Dubroca <sd@queasysnail.net> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--crypto/ahash.c79
-rw-r--r--include/crypto/internal/hash.h10
2 files changed, 60 insertions, 29 deletions
diff --git a/crypto/ahash.c b/crypto/ahash.c
index e58c4970c22b..826cd7ab4d4a 100644
--- a/crypto/ahash.c
+++ b/crypto/ahash.c
@@ -32,6 +32,7 @@ struct ahash_request_priv {
32 crypto_completion_t complete; 32 crypto_completion_t complete;
33 void *data; 33 void *data;
34 u8 *result; 34 u8 *result;
35 u32 flags;
35 void *ubuf[] CRYPTO_MINALIGN_ATTR; 36 void *ubuf[] CRYPTO_MINALIGN_ATTR;
36}; 37};
37 38
@@ -253,6 +254,8 @@ static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt)
253 priv->result = req->result; 254 priv->result = req->result;
254 priv->complete = req->base.complete; 255 priv->complete = req->base.complete;
255 priv->data = req->base.data; 256 priv->data = req->base.data;
257 priv->flags = req->base.flags;
258
256 /* 259 /*
257 * WARNING: We do not backup req->priv here! The req->priv 260 * WARNING: We do not backup req->priv here! The req->priv
258 * is for internal use of the Crypto API and the 261 * is for internal use of the Crypto API and the
@@ -267,38 +270,44 @@ static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt)
267 return 0; 270 return 0;
268} 271}
269 272
270static void ahash_restore_req(struct ahash_request *req) 273static void ahash_restore_req(struct ahash_request *req, int err)
271{ 274{
272 struct ahash_request_priv *priv = req->priv; 275 struct ahash_request_priv *priv = req->priv;
273 276
277 if (!err)
278 memcpy(priv->result, req->result,
279 crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
280
274 /* Restore the original crypto request. */ 281 /* Restore the original crypto request. */
275 req->result = priv->result; 282 req->result = priv->result;
276 req->base.complete = priv->complete; 283
277 req->base.data = priv->data; 284 ahash_request_set_callback(req, priv->flags,
285 priv->complete, priv->data);
278 req->priv = NULL; 286 req->priv = NULL;
279 287
280 /* Free the req->priv.priv from the ADJUSTED request. */ 288 /* Free the req->priv.priv from the ADJUSTED request. */
281 kzfree(priv); 289 kzfree(priv);
282} 290}
283 291
284static void ahash_op_unaligned_finish(struct ahash_request *req, int err) 292static void ahash_notify_einprogress(struct ahash_request *req)
285{ 293{
286 struct ahash_request_priv *priv = req->priv; 294 struct ahash_request_priv *priv = req->priv;
295 struct crypto_async_request oreq;
287 296
288 if (err == -EINPROGRESS) 297 oreq.data = priv->data;
289 return;
290
291 if (!err)
292 memcpy(priv->result, req->result,
293 crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
294 298
295 ahash_restore_req(req); 299 priv->complete(&oreq, -EINPROGRESS);
296} 300}
297 301
298static void ahash_op_unaligned_done(struct crypto_async_request *req, int err) 302static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
299{ 303{
300 struct ahash_request *areq = req->data; 304 struct ahash_request *areq = req->data;
301 305
306 if (err == -EINPROGRESS) {
307 ahash_notify_einprogress(areq);
308 return;
309 }
310
302 /* 311 /*
303 * Restore the original request, see ahash_op_unaligned() for what 312 * Restore the original request, see ahash_op_unaligned() for what
304 * goes where. 313 * goes where.
@@ -309,7 +318,7 @@ static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
309 */ 318 */
310 319
311 /* First copy req->result into req->priv.result */ 320 /* First copy req->result into req->priv.result */
312 ahash_op_unaligned_finish(areq, err); 321 ahash_restore_req(areq, err);
313 322
314 /* Complete the ORIGINAL request. */ 323 /* Complete the ORIGINAL request. */
315 areq->base.complete(&areq->base, err); 324 areq->base.complete(&areq->base, err);
@@ -325,7 +334,12 @@ static int ahash_op_unaligned(struct ahash_request *req,
325 return err; 334 return err;
326 335
327 err = op(req); 336 err = op(req);
328 ahash_op_unaligned_finish(req, err); 337 if (err == -EINPROGRESS ||
338 (err == -EBUSY && (ahash_request_flags(req) &
339 CRYPTO_TFM_REQ_MAY_BACKLOG)))
340 return err;
341
342 ahash_restore_req(req, err);
329 343
330 return err; 344 return err;
331} 345}
@@ -360,25 +374,14 @@ int crypto_ahash_digest(struct ahash_request *req)
360} 374}
361EXPORT_SYMBOL_GPL(crypto_ahash_digest); 375EXPORT_SYMBOL_GPL(crypto_ahash_digest);
362 376
363static void ahash_def_finup_finish2(struct ahash_request *req, int err) 377static void ahash_def_finup_done2(struct crypto_async_request *req, int err)
364{ 378{
365 struct ahash_request_priv *priv = req->priv; 379 struct ahash_request *areq = req->data;
366 380
367 if (err == -EINPROGRESS) 381 if (err == -EINPROGRESS)
368 return; 382 return;
369 383
370 if (!err) 384 ahash_restore_req(areq, err);
371 memcpy(priv->result, req->result,
372 crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
373
374 ahash_restore_req(req);
375}
376
377static void ahash_def_finup_done2(struct crypto_async_request *req, int err)
378{
379 struct ahash_request *areq = req->data;
380
381 ahash_def_finup_finish2(areq, err);
382 385
383 areq->base.complete(&areq->base, err); 386 areq->base.complete(&areq->base, err);
384} 387}
@@ -389,11 +392,15 @@ static int ahash_def_finup_finish1(struct ahash_request *req, int err)
389 goto out; 392 goto out;
390 393
391 req->base.complete = ahash_def_finup_done2; 394 req->base.complete = ahash_def_finup_done2;
392 req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; 395
393 err = crypto_ahash_reqtfm(req)->final(req); 396 err = crypto_ahash_reqtfm(req)->final(req);
397 if (err == -EINPROGRESS ||
398 (err == -EBUSY && (ahash_request_flags(req) &
399 CRYPTO_TFM_REQ_MAY_BACKLOG)))
400 return err;
394 401
395out: 402out:
396 ahash_def_finup_finish2(req, err); 403 ahash_restore_req(req, err);
397 return err; 404 return err;
398} 405}
399 406
@@ -401,7 +408,16 @@ static void ahash_def_finup_done1(struct crypto_async_request *req, int err)
401{ 408{
402 struct ahash_request *areq = req->data; 409 struct ahash_request *areq = req->data;
403 410
411 if (err == -EINPROGRESS) {
412 ahash_notify_einprogress(areq);
413 return;
414 }
415
416 areq->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
417
404 err = ahash_def_finup_finish1(areq, err); 418 err = ahash_def_finup_finish1(areq, err);
419 if (areq->priv)
420 return;
405 421
406 areq->base.complete(&areq->base, err); 422 areq->base.complete(&areq->base, err);
407} 423}
@@ -416,6 +432,11 @@ static int ahash_def_finup(struct ahash_request *req)
416 return err; 432 return err;
417 433
418 err = tfm->update(req); 434 err = tfm->update(req);
435 if (err == -EINPROGRESS ||
436 (err == -EBUSY && (ahash_request_flags(req) &
437 CRYPTO_TFM_REQ_MAY_BACKLOG)))
438 return err;
439
419 return ahash_def_finup_finish1(req, err); 440 return ahash_def_finup_finish1(req, err);
420} 441}
421 442
diff --git a/include/crypto/internal/hash.h b/include/crypto/internal/hash.h
index 1d4f365d8f03..f6d9af3efa45 100644
--- a/include/crypto/internal/hash.h
+++ b/include/crypto/internal/hash.h
@@ -166,6 +166,16 @@ static inline struct ahash_instance *ahash_alloc_instance(
166 return crypto_alloc_instance2(name, alg, ahash_instance_headroom()); 166 return crypto_alloc_instance2(name, alg, ahash_instance_headroom());
167} 167}
168 168
169static inline void ahash_request_complete(struct ahash_request *req, int err)
170{
171 req->base.complete(&req->base, err);
172}
173
174static inline u32 ahash_request_flags(struct ahash_request *req)
175{
176 return req->base.flags;
177}
178
169static inline struct crypto_ahash *crypto_spawn_ahash( 179static inline struct crypto_ahash *crypto_spawn_ahash(
170 struct crypto_ahash_spawn *spawn) 180 struct crypto_ahash_spawn *spawn)
171{ 181{