From patchwork Wed Mar 12 04:29:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 14012858 Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9AD1E33F6 for ; Wed, 12 Mar 2025 04:30:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741753809; cv=none; b=kK3UgWDfEwfdlLHsVU4/sePUVFh8QG+mLMvW6948GrrDQ8AheRxTRxFSdlNkkzbbL7oYwp33pu0SzTh7ILinu5Vy/xjylD2ZNJj188KK9bONiRyADfyUS3+0R6s7qaI7wgJ4PdazYBO24jAi4DjAjvEBTX99lpwzksaxW19mgEc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741753809; c=relaxed/simple; bh=HTnXIOyu15Zlx7eQzAeQm8j+Mf5oK3TEq7XOJ++MXYo=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To:Cc; b=qMwX0r00uLItiZ6zJrD35CI0bxT0qIokAwGiieLoV9JeCuckCkNRbm8PncyarAuOyPAykIUAJLXutYH+zwqu8hbyM9LSTq7J7Q6FuBHJpNamIgAPQG/9wqSTvuxzFNlZ02Sbi/ZWsNQ7m6bwYQUAPeimFURVP+7u0Q+12P8gjk0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; dkim=pass (2048-bit key) header.d=hmeau.com header.i=@hmeau.com header.b=F2EG358I; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=hmeau.com header.i=@hmeau.com header.b="F2EG358I" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=hmeau.com; s=formenos; h=Cc:To:Subject:From:References:In-Reply-To:Message-Id:Date: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=qh9OjMVOyQUs1HuYmcAdi7OtBjnhT/8zyguovqQhU0Y=; b=F2EG358IQZ6/vkfIZd08uxu0yT o7A4zXWdpLcRpenwukllUV05Z+OVLzBTLrKfoBUL2QFoEHkyv/QHcw9YzitIH4eZlsJ9OrhVi1OVk Kjp/ZoHEsCOB6hEXzYhJrp8uUGu1L8NAP33BiBAU7REa+Kg88e8nQsUaquVImhw3TRkjOHwDatagr Xqw1qBxX0dtBqXbn9nB4jqEKEFGvDmDWbe8xdL0/5xqWNqeXMpErhtQQbSTdBkdtVfbxAMCAnN8UM mlUlvkY+wLAVIzke4TR1BJ/V3vb71CYjFK20BnXya7T/bgbnvEr42DQ5aEuWNSYJQebvn4XO0786b fDe3GZ1g==; Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1tsDjB-005mXQ-2q; Wed, 12 Mar 2025 12:29:58 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Wed, 12 Mar 2025 12:29:57 +0800 Date: Wed, 12 Mar 2025 12:29:57 +0800 Message-Id: <03f40ecd970de816686b233bd79406768dc66bbc.1741753576.git.herbert@gondor.apana.org.au> In-Reply-To: References: From: Herbert Xu Subject: [v2 PATCH 1/2] crypto: scatterwalk - Use nth_page instead of doing it by hand To: Linux Crypto Mailing List Cc: Eric Biggers Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Curiously, the Crypto API scatterwalk incremented pages by hand rather than using nth_page. Possibly because scatterwalk predates nth_page (the following commit is from the history tree): commit 3957f2b34960d85b63e814262a8be7d5ad91444d Author: James Morris Date: Sun Feb 2 07:35:32 2003 -0800 [CRYPTO]: in/out scatterlist support for ciphers. Fix this by using nth_page. Signed-off-by: Herbert Xu --- include/crypto/scatterwalk.h | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/include/crypto/scatterwalk.h b/include/crypto/scatterwalk.h index b7e617ae4442..0ba5be363abf 100644 --- a/include/crypto/scatterwalk.h +++ b/include/crypto/scatterwalk.h @@ -100,11 +100,15 @@ static inline void scatterwalk_get_sglist(struct scatter_walk *walk, static inline void scatterwalk_map(struct scatter_walk *walk) { struct page *base_page = sg_page(walk->sg); + unsigned int offset = walk->offset; + void *addr; if (IS_ENABLED(CONFIG_HIGHMEM)) { - walk->__addr = kmap_local_page(base_page + - (walk->offset >> PAGE_SHIFT)) + - offset_in_page(walk->offset); + struct page *page; + + page = nth_page(base_page, offset >> PAGE_SHIFT); + offset = offset_in_page(offset); + addr = kmap_local_page(page) + offset; } else { /* * When !HIGHMEM we allow the walker to return segments that @@ -112,13 +116,14 @@ static inline void scatterwalk_map(struct scatter_walk *walk) * clear that in this case we're working in the linear buffer of * the whole sg entry in the kernel's direct map rather than * within the mapped buffer of a single page, compute the - * address as an offset from the page_address() of the first - * page of the sg entry. Either way the result is the address - * in the direct map, but this makes it clearer what is really - * going on. + * address as an offset from the lowmem_page_address() of + * the first page of the sg entry. Either way the result + * is the address in the direct map, but this makes it clearer + * what is really going on. */ - walk->__addr = page_address(base_page) + walk->offset; + addr = lowmem_page_address(base_page) + offset; } + walk->__addr = addr; } /** From patchwork Wed Mar 12 04:30:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 14012859 Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6BD2F1DB377 for ; Wed, 12 Mar 2025 04:30:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741753811; cv=none; b=X6YO1ZBN51+fcLHvtZtyFq204eeWrXaQqeKFb6BCk2EL+CoJp4LfXrC4gm8LNv5YCzM0gvAsbQnXq8KQkf+X2rimtlTW8EUwkm3eQc1g+JxyKAdMBMLlw06TaLFCK1qqRLzMaJFgRJxbZCxDmYRUXuZf6mM+LcpDWoXZYE2Oj6U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741753811; c=relaxed/simple; bh=Rs7keBjnbhKwshc3mzQiqKSQ+xrxExDPHpVOaSiLd60=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To:Cc; b=DQpSval4IZOxhuXNMRBv7JLOS772OazoUsKWIsc3V/LmLB1mD0sXn03DgGu01dlv+XbDiFVhGnyInuXc6MOvT2TINe/6SZezf3+Xh30Z8xS2C/h2tpcGxj2Pv6NauxSTZ/UsxYuHFjdje/omDXjiAdLVWFsnO4Rkyu/vXE3OUtc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; dkim=pass (2048-bit key) header.d=hmeau.com header.i=@hmeau.com header.b=CbpCHe2B; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=hmeau.com header.i=@hmeau.com header.b="CbpCHe2B" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=hmeau.com; s=formenos; h=Cc:To:Subject:From:References:In-Reply-To:Message-Id:Date: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=i+MXSm3LGhwg0rO3pMtdxVlYRXDl42BeiQqM9NJuxZw=; b=CbpCHe2B6CpGCc0rI4aWsfS9XX grbLcRW0XA0JUlyfCTdgWqQNHQidEDvZ276uCpOD/7juiF9UsDNMPVUVSAYIPHUoH1dO01BXA0hPB 6Dr9yZd6VsuGpzioGYznk8XCdL5Y4LZKheQRKfq9vsG8FsysZl2RkwWUEcT3MmRtky3n83J/HkaHy bS6vyVmedB/lj/afG6y78As5rblDQdboXZVQzyzArbhDBh2jL1cpQQomc8nQl6bFZl5R23LRvpWVZ TpQWcMt1qglLHh6T+nfcWaEZU9taXdda0ISMrbLI3MpadnUa1F8E62QwEdIWX+bz76by824/BBCiK U2LhrbQQ==; Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1tsDjE-005mXb-0b; Wed, 12 Mar 2025 12:30:01 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Wed, 12 Mar 2025 12:30:00 +0800 Date: Wed, 12 Mar 2025 12:30:00 +0800 Message-Id: In-Reply-To: References: From: Herbert Xu Subject: [v2 PATCH 2/2] crypto: hash - Use nth_page instead of doing it by hand To: Linux Crypto Mailing List Cc: Eric Biggers Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Use nth_page instead of adding n to the page pointer. This also fixes a real bug in shash_ahash_digest where the the check for continguous hash data may be incorrect in the presence of highmem. This could result in an incorrect hash or worse. Fixes: 5f7082ed4f48 ("crypto: hash - Export shash through hash") Signed-off-by: Herbert Xu --- crypto/ahash.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/crypto/ahash.c b/crypto/ahash.c index 9c26175c21a8..aff0d3387f3a 100644 --- a/crypto/ahash.c +++ b/crypto/ahash.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -79,7 +80,7 @@ static int hash_walk_new_entry(struct crypto_hash_walk *walk) sg = walk->sg; walk->offset = sg->offset; - walk->pg = sg_page(walk->sg) + (walk->offset >> PAGE_SHIFT); + walk->pg = nth_page(sg_page(walk->sg), walk->offset >> PAGE_SHIFT); walk->offset = offset_in_page(walk->offset); walk->entrylen = sg->length; @@ -201,25 +202,36 @@ int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc) unsigned int nbytes = req->nbytes; struct scatterlist *sg; unsigned int offset; + struct page *page; + const u8 *data; int err; - if (ahash_request_isvirt(req)) - return crypto_shash_digest(desc, req->svirt, nbytes, - req->result); + data = req->svirt; + if (!nbytes || ahash_request_isvirt(req)) + return crypto_shash_digest(desc, data, nbytes, req->result); - if (nbytes && - (sg = req->src, offset = sg->offset, - nbytes <= min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset))) { - void *data; + sg = req->src; + if (nbytes > sg->length) + return crypto_shash_init(desc) ?: + shash_ahash_finup(req, desc); - data = kmap_local_page(sg_page(sg)); - err = crypto_shash_digest(desc, data + offset, nbytes, - req->result); - kunmap_local(data); - } else - err = crypto_shash_init(desc) ?: - shash_ahash_finup(req, desc); + page = sg_page(sg); + data = lowmem_page_address(page) + offset; + if (!IS_ENABLED(CONFIG_HIGHMEM)) + return crypto_shash_digest(desc, data, nbytes, req->result); + offset = sg->offset; + page = nth_page(page, offset >> PAGE_SHIFT); + offset = offset_in_page(offset); + + if (nbytes > (unsigned int)PAGE_SIZE - offset) + return crypto_shash_init(desc) ?: + shash_ahash_finup(req, desc); + + data = kmap_local_page(page); + err = crypto_shash_digest(desc, data + offset, nbytes, + req->result); + kunmap_local(data); return err; } EXPORT_SYMBOL_GPL(shash_ahash_digest);