Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp3156111pxb; Mon, 18 Apr 2022 17:31:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZOazKuJaosoIlW2cHCFss5WEhyxMwdeulhqH9L222MuiUvoX9MuRTyuQXPK/29dLBoWq2 X-Received: by 2002:aa7:9ad8:0:b0:50a:49e1:164c with SMTP id x24-20020aa79ad8000000b0050a49e1164cmr14907707pfp.8.1650328318846; Mon, 18 Apr 2022 17:31:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650328318; cv=none; d=google.com; s=arc-20160816; b=P0JTT1WBc3711D5k4zYHRUg2fl345xd2JSxuMh4ECqqtYwWrSjYcrJeXru7vKdmhgh DFSPDoAag8fbBc0ergO6p/kCkcwFKaiKiXv1yRSW/5Du8+e0EbF7USCLyukPjjAOGvxB L7Xoh3cTpA72Y2xuNTySOmMCJ1Ubo57uVMZ3nNTNwamX4cqc4LW8ak5IxBSEQYi/TYnQ Wl/VFdI2Vo/8Y+LYAu8V88UW0hCkGyaIWmTpB842re9cDsYF85W87ONCf0600zIG9c/7 1ttBcfyoWNlsuY1vm4T4RTB7Z1C+/xSqB3SbKN7WiQjeT9YvFd1JpmywqgqpBCVTO87P zBDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=WZdTwIATUbRGRTPIJsVKSWhhL+KHW0wy8jvvYPsUXY0=; b=RbrVu/Acb+ximZ0kGc3Zy0qc9pmF5J8uC2gfSmM0cuRETyodWC5SyLao1+NCH0/9sH QyhaIkqmZ3EiUKioxQEwCb8KbS9bWuUQXXpH40Q1NVPLBmvBqcazySWCUQUQG6yeKCuE RzDte6hPRFvrL+RIA8bMMT7pUiUSkQfXWo2Oz4EPrS2zD972aUfRMFUXfnVgc28LNm2e KyDt/50NxFTIPSOeGQiTDVT0cdTsFUpujJG8nRZtpr2NQuACA2NAiucyQSMUBpeGQirK pP5MUcQR37iny6E61/3OPotKOadI0YZ3F7IroXfDDLMylwaJ8KKRdz2ymOwB/I/HFcdv eKfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GLIO7cFa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ng1-20020a17090b1a8100b001c7e8ae7620si857063pjb.157.2022.04.18.17.31.40; Mon, 18 Apr 2022 17:31:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GLIO7cFa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240129AbiDRMim (ORCPT + 99 others); Mon, 18 Apr 2022 08:38:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239743AbiDRMdW (ORCPT ); Mon, 18 Apr 2022 08:33:22 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 381F71E3E3; Mon, 18 Apr 2022 05:25:28 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 71832B80ED1; Mon, 18 Apr 2022 12:25:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D7198C385A1; Mon, 18 Apr 2022 12:25:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650284726; bh=6xnc8GCF0VylzUgLpI+zJ8wz47soPErwv3vp/zZrPmk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GLIO7cFaT19V7lydSyjiKGYxXtz7X1RSuwJPWE9in3Ai3lo/JmhdjVLVbaBjlml/h S/bxatC1APauobPxVS2WmxSlQ0GsAwgCjMCk+AIjrxVLcxPZvL42VbrxnXPxishYhR XrXkaE2VS70n8s0FEMq3IJv4FdEvjVsDds+hJozU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mikulas Patocka , Mike Snitzer Subject: [PATCH 5.17 207/219] dm integrity: fix memory corruption when tag_size is less than digest size Date: Mon, 18 Apr 2022 14:12:56 +0200 Message-Id: <20220418121212.661717902@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121203.462784814@linuxfoundation.org> References: <20220418121203.462784814@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mikulas Patocka commit 08c1af8f1c13bbf210f1760132f4df24d0ed46d6 upstream. It is possible to set up dm-integrity in such a way that the "tag_size" parameter is less than the actual digest size. In this situation, a part of the digest beyond tag_size is ignored. In this case, dm-integrity would write beyond the end of the ic->recalc_tags array and corrupt memory. The corruption happened in integrity_recalc->integrity_sector_checksum->crypto_shash_final. Fix this corruption by increasing the tags array so that it has enough padding at the end to accomodate the loop in integrity_recalc() being able to write a full digest size for the last member of the tags array. Cc: stable@vger.kernel.org # v4.19+ Signed-off-by: Mikulas Patocka Signed-off-by: Mike Snitzer Signed-off-by: Greg Kroah-Hartman --- drivers/md/dm-integrity.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) --- a/drivers/md/dm-integrity.c +++ b/drivers/md/dm-integrity.c @@ -4400,6 +4400,7 @@ try_smaller_buffer: } if (ic->internal_hash) { + size_t recalc_tags_size; ic->recalc_wq = alloc_workqueue("dm-integrity-recalc", WQ_MEM_RECLAIM, 1); if (!ic->recalc_wq ) { ti->error = "Cannot allocate workqueue"; @@ -4413,8 +4414,10 @@ try_smaller_buffer: r = -ENOMEM; goto bad; } - ic->recalc_tags = kvmalloc_array(RECALC_SECTORS >> ic->sb->log2_sectors_per_block, - ic->tag_size, GFP_KERNEL); + recalc_tags_size = (RECALC_SECTORS >> ic->sb->log2_sectors_per_block) * ic->tag_size; + if (crypto_shash_digestsize(ic->internal_hash) > ic->tag_size) + recalc_tags_size += crypto_shash_digestsize(ic->internal_hash) - ic->tag_size; + ic->recalc_tags = kvmalloc(recalc_tags_size, GFP_KERNEL); if (!ic->recalc_tags) { ti->error = "Cannot allocate tags for recalculating"; r = -ENOMEM;