From nobody Mon Apr 6 21:13:00 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47478C433F5 for ; Thu, 6 Oct 2022 23:41:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232263AbiJFXlt (ORCPT ); Thu, 6 Oct 2022 19:41:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232200AbiJFXlp (ORCPT ); Thu, 6 Oct 2022 19:41:45 -0400 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 630C3B7EE8 for ; Thu, 6 Oct 2022 16:41:44 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id d24so3097302pls.4 for ; Thu, 06 Oct 2022 16:41:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date; bh=qCdxcv6M0CuI1NF0o9w+SopI59ed1qMBjC0oFZix1to=; b=cuOzGu8QPuJKu+27nAW0yvJkZyh+Y0IUvM+g/jA0NnjQ0A9OMNFZsq6HsUHYOBYeCL KjewOTyaimxfA0qnPv7Bw8bk5adAv9A3Pfj/RNVmfZYcsHGsTCFqjDtWH011OG3cB0C/ Nw/2XLNUzKMXeY5OGkIMKMBctyuza1vVQZKAU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=qCdxcv6M0CuI1NF0o9w+SopI59ed1qMBjC0oFZix1to=; b=yIBfuD/dSx1EXTe6uDB4phKYnyIEgchevySUVbicpf39NgXHqn5bI4fr4wpy4d9nj4 /2LLk43hheaO9/zJk0C5eC+qFy8ZsaamFWZf5zFtPFcCIprpKpJh2QKkscK6vQaB4tJb MN7GHFu3Ja+hd1FHVnYQvK4jjXh7pv/kMmlTtMTUsA9WNSpDNnFF/js/1SlURVjxbHx3 aL6cbsVPH5ClCE0/X8XNkhVJ6sMQwYLHirNxWqxKZ9DIXbIOe68PS3TZcJlSEGy6+VLr 4reirC809IYCkyRwxO7YkczJt49BtY+K0Hgcfrhpq8Es28JyBBmKRF1wRF/LoLgdy9tS wBdA== X-Gm-Message-State: ACrzQf1D1dbGoAg0UhtrHaV1CTRVn2BTxIEJCKR9YoqXNfZVdlIGH4gA TndmkuUSVKL6wReY9ukKYxgaSw== X-Google-Smtp-Source: AMsMyM7Et+YU922Se/+kC0Ktf2RCcFl1kp7JkwscCc/OdZuVm4GhFCkFxpMf9aZo8N3RWKgpB+v4CQ== X-Received: by 2002:a17:903:22d0:b0:177:f919:9259 with SMTP id y16-20020a17090322d000b00177f9199259mr1837761plg.71.1665099703864; Thu, 06 Oct 2022 16:41:43 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id q27-20020aa7983b000000b005625d5ae760sm206462pfl.11.2022.10.06.16.41.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Oct 2022 16:41:43 -0700 (PDT) From: Kees Cook To: "Guilherme G. Piccoli" Cc: Kees Cook , Ard Biesheuvel , Anton Vorontsov , Colin Cross , Tony Luck , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH] pstore: migrate to crypto acomp interface (take 2) Date: Thu, 6 Oct 2022 16:41:38 -0700 Message-Id: <20221006234138.1835739-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5644; i=keescook@chromium.org; h=from:subject; bh=nyFzDaejhKcnUUweiZXZHIGXRCrEOLw9LJkJmbn+1Rg=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjP2eymu+LeD9AhiAcJ+NO1EOfHWhzgzP6YRktQ7QS n89k2eOJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCYz9nsgAKCRCJcvTf3G3AJtpSD/ 9n/oFir28xaN+WDAhY0Hc8TenRkOOsHJupmb4YANzKCTMe+ACHdWH9hj+8RX0lnGCdnCFbCTsyQ8X1 ZD0frlnwQlDCTdyvs1qMi2wfNE+o8bKaI/3eC+rhQORcQ6thpYr2q1175SBwkCA5oJvEuVWvdnrAy7 bap5+0WzqcRFysNcdLmuq/hOHCEfHdhMYqveeDg6Z//avkN3ZtNydq2zHbLuzA2kKcuZpszYEjiOPT 55sNMgeyATb3V5kPgJxPaY64On8Lrl1aHzluyfhrOpEBk6xxOTppJqIb7m98tqDzteZzlttmumaKLn RSzB9uIr0n/MTTvy1XCTW9ciX57uhsPCB6oyKu8RaOA4jd8kZl1iDhGgSq9XcSU+n9wVcJgryXzGkP i8oQV74oT5eZpM9F3BBHPB4Zydsle420fcaJDzPw83PA62+jked1QgCP7bj8hAAxgM40/zrOHrcapc rISubO76QW+xGbDYotbMoQ8OPomIuM5BfcCjItymAARqg11jHa7IxKHAZZ2caKduKiDj7hLDpjbshM DrAzzP9uQ6W550VXVUPtcB1mD97/2O8cDZC7pAvzvooLOZnnib102UB8guckM/P1BhdBQqst9rk7dW N0qt7VZmGRE9Csq1q0s9yEZblGUg+3loa8REg+Y5/YaIjEBV8Z0TJFQo2gpQ== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel The crypto 'compress' interface is deprecated, so before adding new features, migrate to the acomp interface. Note that we are only using synchronous implementations of acomp, so we don't have to deal with asynchronous completion. [ Tweaked error paths to avoid memory leak, as pointed out byGuilherme G. Piccoli , and fixed pstore_compress() return value -kees ] Signed-off-by: Ard Biesheuvel Link: https://lore.kernel.org/lkml/CAMj1kXFnoqj+cn-0dT8fg0kgLvVx+Q2Ex-4CUjS= nA9yRprmC-w@mail.gmail.com/ Cc: "Guilherme G. Piccoli" Signed-off-by: Kees Cook Tested-by: Guilherme G. Piccoli --- fs/pstore/platform.c | 74 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 17 deletions(-) diff --git a/fs/pstore/platform.c b/fs/pstore/platform.c index 0c034ea39954..f82256612c19 100644 --- a/fs/pstore/platform.c +++ b/fs/pstore/platform.c @@ -28,11 +28,14 @@ #include #include #include +#include #include #include #include #include =20 +#include + #include "internal.h" =20 /* @@ -90,7 +93,8 @@ module_param(compress, charp, 0444); MODULE_PARM_DESC(compress, "compression to use"); =20 /* Compression parameters */ -static struct crypto_comp *tfm; +static struct crypto_acomp *tfm; +static struct acomp_req *creq; =20 struct pstore_zbackend { int (*zbufsize)(size_t size); @@ -268,23 +272,32 @@ static const struct pstore_zbackend zbackends[] =3D { static int pstore_compress(const void *in, void *out, unsigned int inlen, unsigned int outlen) { + struct scatterlist src, dst; int ret; =20 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS)) return -EINVAL; =20 - ret =3D crypto_comp_compress(tfm, in, inlen, out, &outlen); + sg_init_table(&src, 1); + sg_set_buf(&src, in, inlen); + + sg_init_table(&dst, 1); + sg_set_buf(&dst, out, outlen); + + acomp_request_set_params(creq, &src, &dst, inlen, outlen); + + ret =3D crypto_acomp_compress(creq); if (ret) { pr_err("crypto_comp_compress failed, ret =3D %d!\n", ret); return ret; } =20 - return outlen; + return creq->dlen; } =20 static void allocate_buf_for_compression(void) { - struct crypto_comp *ctx; + struct crypto_acomp *acomp; int size; char *buf; =20 @@ -296,7 +309,7 @@ static void allocate_buf_for_compression(void) if (!psinfo || tfm) return; =20 - if (!crypto_has_comp(zbackend->name, 0, 0)) { + if (!crypto_has_acomp(zbackend->name, 0, CRYPTO_ALG_ASYNC)) { pr_err("Unknown compression: %s\n", zbackend->name); return; } @@ -315,16 +328,24 @@ static void allocate_buf_for_compression(void) return; } =20 - ctx =3D crypto_alloc_comp(zbackend->name, 0, 0); - if (IS_ERR_OR_NULL(ctx)) { + acomp =3D crypto_alloc_acomp(zbackend->name, 0, CRYPTO_ALG_ASYNC); + if (IS_ERR_OR_NULL(acomp)) { kfree(buf); pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name, - PTR_ERR(ctx)); + PTR_ERR(acomp)); + return; + } + + creq =3D acomp_request_alloc(acomp); + if (!creq) { + crypto_free_acomp(acomp); + kfree(buf); + pr_err("acomp_request_alloc('%s') failed\n", zbackend->name); return; } =20 /* A non-NULL big_oops_buf indicates compression is available. */ - tfm =3D ctx; + tfm =3D acomp; big_oops_buf_sz =3D size; big_oops_buf =3D buf; =20 @@ -334,7 +355,8 @@ static void allocate_buf_for_compression(void) static void free_buf_for_compression(void) { if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) { - crypto_free_comp(tfm); + acomp_request_free(creq); + crypto_free_acomp(tfm); tfm =3D NULL; } kfree(big_oops_buf); @@ -671,6 +693,8 @@ static void decompress_record(struct pstore_record *rec= ord) int ret; int unzipped_len; char *unzipped, *workspace; + struct acomp_req *dreq; + struct scatterlist src, dst; =20 if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !record->compressed) return; @@ -694,31 +718,47 @@ static void decompress_record(struct pstore_record *r= ecord) if (!workspace) return; =20 + dreq =3D acomp_request_alloc(tfm); + if (!dreq) + goto out_free_workspace; + + sg_init_table(&src, 1); + sg_set_buf(&src, record->buf, record->size); + + sg_init_table(&dst, 1); + sg_set_buf(&dst, workspace, unzipped_len); + + acomp_request_set_params(dreq, &src, &dst, record->size, unzipped_len); + /* After decompression "unzipped_len" is almost certainly smaller. */ - ret =3D crypto_comp_decompress(tfm, record->buf, record->size, - workspace, &unzipped_len); + ret =3D crypto_acomp_decompress(dreq); if (ret) { - pr_err("crypto_comp_decompress failed, ret =3D %d!\n", ret); - kfree(workspace); - return; + pr_err("crypto_acomp_decompress failed, ret =3D %d!\n", ret); + goto out; } =20 /* Append ECC notice to decompressed buffer. */ + unzipped_len =3D dreq->dlen; memcpy(workspace + unzipped_len, record->buf + record->size, record->ecc_notice_size); =20 /* Copy decompressed contents into an minimum-sized allocation. */ unzipped =3D kmemdup(workspace, unzipped_len + record->ecc_notice_size, GFP_KERNEL); - kfree(workspace); if (!unzipped) - return; + goto out; =20 /* Swap out compressed contents with decompressed contents. */ kfree(record->buf); record->buf =3D unzipped; record->size =3D unzipped_len; record->compressed =3D false; + +out: + acomp_request_free(dreq); +out_free_workspace: + kfree(workspace); + return; } =20 /* --=20 2.34.1