Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp10258034ybi; Thu, 11 Jul 2019 02:22:43 -0700 (PDT) X-Google-Smtp-Source: APXvYqx1P4XlwMeMCeHld7aStDBYUPC8bM3grXiDJfUQPSexT12KBvY9XtDpZ9NhKFGhnELublWv X-Received: by 2002:a17:902:2a6b:: with SMTP id i98mr3312452plb.75.1562836963502; Thu, 11 Jul 2019 02:22:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562836963; cv=none; d=google.com; s=arc-20160816; b=R7iH2jfeWLNfvkhlVbzu4seMm+Ym2OCJ9nCam/GfoJbLcaKHSvjcnHQKkjMZeG0Gqz xW9BEoUvLRVE3KLjCFlN8Ujgh62xyaeL2ch1D0PC4J1EGwwzK6aAYA187JToTvBB6JzK s5OohBqfye5NCDosUB6pL+ikG2Ea+RCjtn12El/DFgUNEvO8Lf1AWhpNcqXD6beBiuZ9 6W7nJEoCoVCrSZnbSJzw9n8u/u5cRzJqmvD2Z5rgjtng/sp0MYNe/IMj8nKFEyYksox6 zfL55GBMg81T5PSdfrT25u+Cy43m//O8NXvPj3qadBFQjYq6KzphGjG7qyQ1GnGVUTVO mR9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=xlwYflAq0smJi21KRy7k4mCMg2ux1t1pjB1v5UR/AcA=; b=j1RMInFqM1+nRJ6TrJ52fMH683cJqY/48oO2csvzrj0X9+t9U9q0amjzIGheVpoKwo rxA3S7B3da3FCtRXKEtAhilqTwemaN4n9yyAT3pQshchcLvu73B1+89CzdX95hYrIgCp 1lDte7expuqdzItO1cW9PWBZ6v/IJ82IBcvO176EtUk8JXy/iW01cLmrptHNaU9WPkTn JU+UhAzLLCx+q4yBwFK6klFQRXmCRa/WEt1HxiRbU7yWqIpSTlp7kAJzQ2UXQYkL+sgg SKPt/UfMVkPsc8Y++OOFlaF/mZjTq/wj/viikQqEw4r6mf6UboN7XDjIYr+qva6CtYa5 ubqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ZMUfbc+l; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a9si4935783pfn.104.2019.07.11.02.22.27; Thu, 11 Jul 2019 02:22:43 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ZMUfbc+l; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728083AbfGKJWI (ORCPT + 99 others); Thu, 11 Jul 2019 05:22:08 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:42375 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727595AbfGKJWI (ORCPT ); Thu, 11 Jul 2019 05:22:08 -0400 Received: by mail-wr1-f67.google.com with SMTP id j8so1358838wrj.9; Thu, 11 Jul 2019 02:22:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=xlwYflAq0smJi21KRy7k4mCMg2ux1t1pjB1v5UR/AcA=; b=ZMUfbc+lo/eM2LuIizHmmX3xB85uswxxsVQh/1jdqeXN1oEi1wVOI6kZ1IKFV1kxaN R8LXYQRJ6LG7okRNM9IeK4wPsGza3MX9GtnXM1pYk6qhEwGpRs/1kZkPev8fxIbfW1/3 Tn2/mcAJZVq1G46LxhKiOfcD6bCEES2tRTWcdi4sYkvsJZQ4VkR+5n69Fw2LfUukh6XC SbdxahGhEajGb5/QPy2Cp5dVNnUuLbFsZxkLZIIGPxv5rvYlwovBHo++b//XGGcjLGmN zZaLfD/e+lmTmCZG8q4E6jlE1n04G253PSy+Wf3cr96vAi1DNF49SIAk3NRpalIZo2fb c6vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=xlwYflAq0smJi21KRy7k4mCMg2ux1t1pjB1v5UR/AcA=; b=Q3u3gctdLiVIOuZe6YsQ+we2PyRq7MSg9nfEENfxZl+YUGBvCELETJmVpcUcUowwn2 d9mO7sONjxRZxr6iN5wPrV6ZR8XTqkQhmMfaYW8mKG5UVpichX7sP4ZHinocoM8VusUx G+Smhf8lhTl5SQ/vOk9p6KxieE0hmBD9/2BuQcbVCBJjUc63TH7i6ZTXDkPyHERF7DrQ 5tpsMTQNYo5Ek/s4cLqeNO/V/fhpnICFxo2UhE/KAl5desYJCm3BIEc9CMkvJRtkDv9Q UGTIAfm54YwUj/CHEKBbKs/PcshGLzxjJWEwJfCsGgY8jEtxaql4dKucsbtXYJ2EMeou 17Xw== X-Gm-Message-State: APjAAAVZJwZoBt9BumdMtBpvmoAgjfOgGFG6op6F2nJn8dL09WNZtuU7 lfol1CAf/N09XsxpCGMvpLJgCuo0qnSgSHSiuE0= X-Received: by 2002:adf:e4c6:: with SMTP id v6mr3714252wrm.315.1562836926040; Thu, 11 Jul 2019 02:22:06 -0700 (PDT) MIME-Version: 1.0 References: <1562830033-24239-1-git-send-email-wang6495@umn.edu> In-Reply-To: <1562830033-24239-1-git-send-email-wang6495@umn.edu> From: Ming Lei Date: Thu, 11 Jul 2019 17:21:54 +0800 Message-ID: Subject: Re: [PATCH] block/bio-integrity: fix a memory leak bug To: Wenwen Wang Cc: Wenwen Wang , Jens Axboe , "open list:BLOCK LAYER" , open list Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jul 11, 2019 at 3:36 PM Wenwen Wang wrote: > > From: Wenwen Wang > > 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(). > > Signed-off-by: Wenwen Wang > --- > block/bio-integrity.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/block/bio-integrity.c b/block/bio-integrity.c > index 4db6208..bfae10c 100644 > --- a/block/bio-integrity.c > +++ b/block/bio-integrity.c > @@ -276,8 +276,10 @@ bool bio_integrity_prep(struct bio *bio) > ret = bio_integrity_add_page(bio, virt_to_page(buf), > bytes, offset); > > - if (ret == 0) > + if (ret == 0) { > + kfree(buf); > return false; > + } This way may not be enough, and the bio payload needs to be freed. And you may refer to the error handling for 'IS_ERR(bip)', and bio->bi_status needs to be set, and bio_endio() needs to be called too. Thanks, Ming Lei