From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EEC0FC76196 for ; Fri, 19 Jul 2019 04:02:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BCBB821882 for ; Fri, 19 Jul 2019 04:02:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563508923; bh=nMJglynZyLcYk9zCBzECY9j3OAE04HiIgAINB5zkQ5E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=SQWhrydMnUf1f0iKagFHBPh5PgpFWgKX83i9TVqlnoTwBSjhIzLa7pIUMJu10+yfJ cM5Q6XuQjUuWYPfWza9x9Vkq2cx9DII/triZNvH/C5Cw0rARz/FvHkLfGw4lUasbpp sHZJMPvuJzpzm/pijmnKEQKDc5NedX27uABMbxlM= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729791AbfGSECC (ORCPT ); Fri, 19 Jul 2019 00:02:02 -0400 Received: from mail.kernel.org ([198.145.29.99]:33546 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729659AbfGSEBs (ORCPT ); Fri, 19 Jul 2019 00:01:48 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7FB4721873; Fri, 19 Jul 2019 04:01:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563508907; bh=nMJglynZyLcYk9zCBzECY9j3OAE04HiIgAINB5zkQ5E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cDJ7rwrsvyEikFpzH964XvPV6286BOJyYL3xn8IWiTG3XTB0V4Lb2JtzEGAfS8DOd 670k1jWzxP9WDp2+fIiC9Yf/HhrlGeEce3QOeKa/fjlNaXLqvfpoR5tbummoIzhRT2 dsS8qM2Kgg55axbNpDOENZDwz5pJryBhs9LztxCw= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Wenwen Wang , Ming Lei , "Martin K . Petersen" , Jens Axboe , Sasha Levin , linux-block@vger.kernel.org Subject: [PATCH AUTOSEL 5.2 152/171] block/bio-integrity: fix a memory leak bug Date: Thu, 18 Jul 2019 23:56:23 -0400 Message-Id: <20190719035643.14300-152-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190719035643.14300-1-sashal@kernel.org> References: <20190719035643.14300-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wenwen Wang [ Upstream commit e7bf90e5afe3aa1d1282c1635a49e17a32c4ecec ] In bio_integrity_prep(), a kernel buffer is allocated through kmalloc() to hold integrity metadata. Later on, the buffer will be attached to the bio structure through bio_integrity_add_page(), which returns the number of bytes of integrity metadata attached. Due to unexpected situations, bio_integrity_add_page() may return 0. As a result, bio_integrity_prep() needs to be terminated with 'false' returned to indicate this error. However, the allocated kernel buffer is not freed on this execution path, leading to a memory leak. To fix this issue, free the allocated buffer before returning from bio_integrity_prep(). Reviewed-by: Ming Lei Acked-by: Martin K. Petersen Signed-off-by: Wenwen Wang Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- block/bio-integrity.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/block/bio-integrity.c b/block/bio-integrity.c index 4db620849515..fb95dbb21dd8 100644 --- a/block/bio-integrity.c +++ b/block/bio-integrity.c @@ -276,8 +276,12 @@ bool bio_integrity_prep(struct bio *bio) ret = bio_integrity_add_page(bio, virt_to_page(buf), bytes, offset); - if (ret == 0) - return false; + if (ret == 0) { + printk(KERN_ERR "could not attach integrity payload\n"); + kfree(buf); + status = BLK_STS_RESOURCE; + goto err_end_io; + } if (ret < bytes) break; -- 2.20.1