From patchwork Mon Mar 3 00:21:46 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Vasut X-Patchwork-Id: 3750421 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0FF609F376 for ; Mon, 3 Mar 2014 00:23:25 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id F326420374 for ; Mon, 3 Mar 2014 00:23:23 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C3CE220364 for ; Mon, 3 Mar 2014 00:23:22 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1WKGec-0002oe-Gb; Mon, 03 Mar 2014 00:22:46 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1WKGeW-0007Xw-4c; Mon, 03 Mar 2014 00:22:40 +0000 Received: from mail-out.m-online.net ([2001:a60:0:28:0:1:25:1]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1WKGeJ-0007V0-47 for linux-arm-kernel@lists.infradead.org; Mon, 03 Mar 2014 00:22:30 +0000 Received: from frontend1.mail.m-online.net (frontend1.mail.intern.m-online.net [192.168.8.180]) by mail-out.m-online.net (Postfix) with ESMTP id 3fchqn1cs4z3hhVZ; Mon, 3 Mar 2014 01:21:56 +0100 (CET) X-Auth-Info: BQLm+rnmF/BhjHIELKQkJ3pwvzlWomwgZK1/ROTo+CY= Received: from chi.lan (unknown [195.140.253.167]) by smtp-auth.mnet-online.de (Postfix) with ESMTPA id 3fchqm3dkSzbbgx; Mon, 3 Mar 2014 01:21:56 +0100 (CET) From: Marek Vasut To: linux-arm-kernel@lists.infradead.org Subject: [PATCH 1/3 V2] crypto: Fix the pointer voodoo in unaligned ahash Date: Mon, 3 Mar 2014 01:21:46 +0100 Message-Id: <1393806108-6374-1-git-send-email-marex@denx.de> X-Mailer: git-send-email 1.8.5.3 In-Reply-To: <1389720829-5963-1-git-send-email-marex@denx.de> References: <1389720829-5963-1-git-send-email-marex@denx.de> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140302_192227_846941_A5DF4BD8 X-CRM114-Status: GOOD ( 17.20 ) X-Spam-Score: -1.9 (-) Cc: Marek Vasut , Fabio Estevam , Herbert Xu , Tom Lendacky , linux-crypto@vger.kernel.org, Shawn Guo , "David S. Miller" X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add documentation for the pointer voodoo that is happening in crypto/ahash.c in ahash_op_unaligned(). This code is quite confusing, so add a beefy chunk of documentation. Moreover, make sure the mangled request is completely restored after finishing this unaligned operation. This means restoring all of .result, .priv, .base.data and .base.complete . Also, remove the crypto_completion_t complete = ... line present in the ahash_op_unaligned_done() function. This type actually declares a function pointer, which is very confusing. Finally, yet very important nonetheless, make sure the req->priv is free()'d only after the original request is restored in ahash_op_unaligned_done(). The req->priv data must not be free()'d before that in ahash_op_unaligned_finish(), since we would be accessing previously free()'d data in ahash_op_unaligned_done() and cause corruption. Signed-off-by: Marek Vasut Cc: David S. Miller Cc: Fabio Estevam Cc: Herbert Xu Cc: Shawn Guo Cc: Tom Lendacky --- crypto/ahash.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 9 deletions(-) V2: Move the restoration of the ORIGINAL crypto request fields from ahash_op_unaligned_done() to ahash_op_unaligned_finish(). This way, we handle both Sync-HASH and Async-HASH cases properly: SYNC: ahash_op_unaligned_finish() is called with err=0 . Data are copied from ADJUSTED request to ORIGINAL request, ORIGINAL request is correctly restored and free'd. ASYNC: ahash_op_unaligned_finish() is called with err=-EINPROGRESS, returns. Later, ahash_op_unaligned_finish() is called again from ahash_op_unaligned_done() callback. Data are copied from ADJUSTED request to ORIGINAL request, ORIGINAL request is correctly restored and free'd. diff --git a/crypto/ahash.c b/crypto/ahash.c index a92dc38..affebb5 100644 --- a/crypto/ahash.c +++ b/crypto/ahash.c @@ -29,6 +29,7 @@ struct ahash_request_priv { crypto_completion_t complete; void *data; + void *priv; u8 *result; void *ubuf[] CRYPTO_MINALIGN_ATTR; }; @@ -201,22 +202,34 @@ static void ahash_op_unaligned_finish(struct ahash_request *req, int err) memcpy(priv->result, req->result, crypto_ahash_digestsize(crypto_ahash_reqtfm(req))); + /* Restore the original crypto request. */ + req->result = priv->result; + req->base.complete = priv->complete; + req->base.data = priv->data; + req->priv = priv->priv; + + /* Free the req->priv.priv from the ADJUSTED request. */ kzfree(priv); } -static void ahash_op_unaligned_done(struct crypto_async_request *req, int err) +static void ahash_op_unaligned_done(struct crypto_async_request *areq, int err) { - struct ahash_request *areq = req->data; - struct ahash_request_priv *priv = areq->priv; - crypto_completion_t complete = priv->complete; - void *data = priv->data; + struct ahash_request *req = areq->data; - ahash_op_unaligned_finish(areq, err); + /* + * Restore the original request, see ahash_op_unaligned() for what + * goes where. + * + * The "struct ahash_request *req" here is in fact the "req.base" + * from the ADJUSTED request from ahash_op_unaligned(), thus as it + * is a pointer to self, it is also the ADJUSTED "req" . + */ - areq->base.complete = complete; - areq->base.data = data; + /* First copy req->result into req->priv.result */ + ahash_op_unaligned_finish(req, err); - complete(&areq->base, err); + /* Complete the ORIGINAL request. */ + req->base.complete(&req->base, err); } static int ahash_op_unaligned(struct ahash_request *req, @@ -234,9 +247,36 @@ static int ahash_op_unaligned(struct ahash_request *req, if (!priv) return -ENOMEM; + /* + * WARNING: Voodoo programming below! + * + * The code below is obscure and hard to understand, thus explanation + * is necessary. See include/crypto/hash.h and include/linux/crypto.h + * to understand the layout of structures used here! + * + * The code here will replace portions of the ORIGINAL request with + * pointers to new code and buffers so the hashing operation can store + * the result in aligned buffer. We will call the modified request + * an ADJUSTED request. + * + * The newly mangled request will look as such: + * + * req { + * .result = ADJUSTED[new aligned buffer] + * .base.complete = ADJUSTED[pointer to completion function] + * .base.data = ADJUSTED[*req (pointer to self)] + * .priv = ADJUSTED[new priv] { + * .result = ORIGINAL(result) + * .complete = ORIGINAL(base.complete) + * .data = ORIGINAL(base.data) + * .priv = ORIGINAL(priv) + * } + */ + priv->result = req->result; priv->complete = req->base.complete; priv->data = req->base.data; + priv->priv = req->priv; req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1); req->base.complete = ahash_op_unaligned_done;