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=-4.2 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_2 autolearn=no 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 89A07C433B4 for ; Thu, 29 Apr 2021 20:10:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 45D9B613EA for ; Thu, 29 Apr 2021 20:10:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236773AbhD2UKu (ORCPT ); Thu, 29 Apr 2021 16:10:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234006AbhD2UKt (ORCPT ); Thu, 29 Apr 2021 16:10:49 -0400 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE547C06138B; Thu, 29 Apr 2021 13:10:00 -0700 (PDT) Received: by mail-lf1-x12d.google.com with SMTP id x2so792505lff.10; Thu, 29 Apr 2021 13:10:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qVxRIljlMWj/xaAgQFr2JbubgIjJiWSc0KSUhA2z/UA=; b=kWWBpjASwhbzasukQ6Trnitmid+QJ+rW2vQ8FiUZMnmf2AjLMAAyXw+n3RxmbGPOcd JKMOoGcxglMZ/niMgUMjYanAeKwzKF3pkgGPxDbSN9WtUgMvWAWzwPn11uLqZ1C/XTbV NiW1yWr7bjQ7GKFYXkE3Xt8jmQiD59GVvR5iMoPS6GczTE1dRfTZkvTp/MgbIo0rnEB4 7r3AbTSIs1FJiIa4VuGpvFTXM7LsB8fSetiFXE61y/ypG19w5uR70MAQEVlrxUGdfVob 2/pKbVV6pUKgCWHmeCWb0/fv/M72MohQa934Q1JHME+J6sCv8CGmqbfc4DCKg2EcrlSQ nrYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qVxRIljlMWj/xaAgQFr2JbubgIjJiWSc0KSUhA2z/UA=; b=BFkbC11W9ueLM78G6mu4BmCcxFYeq/4NjR5cof5wPE1dNTI9Aw38l5BDpi/nIPGTqL 10KDFckaSN2hytF8w+ASQbOTaFKiI+GOaH4E+6u/Bn6E64v/DZt50KV3/nIjivPa7xlC fXVkDDTRZfnCQH79MupqaRrCie/kN9flJR89M5OKGTklft2/px8VzTmduJ5Hwgjuaiii Kogvbt7wp1LLUgm1PFMQrayamSpHe7K69YflEDr1dkVoA6ReLcsVl5om3O9TDg1ODo0t vT9oHPdgbaEaKBX0HKlJ/OWaxDjNRFbhTQDOA934duRUjxaYfrSgx5TSQXvUOpHxwWpA uF2A== X-Gm-Message-State: AOAM532boRqhtim8nG7REVmOmoZEMg8KlFpgw2JZa4g1oidvpN5KYmvn plEsjYJae4KkzOJidilj+04= X-Google-Smtp-Source: ABdhPJzNquVt0QBgGnf9i8u/yXkXvH6j8PoBe0EQei3rCagyJJ4+mT6EFtpQIrBB60QXUPTyBR76VA== X-Received: by 2002:a05:6512:15d:: with SMTP id m29mr841726lfo.515.1619726999262; Thu, 29 Apr 2021 13:09:59 -0700 (PDT) Received: from localhost.localdomain ([94.103.226.84]) by smtp.gmail.com with ESMTPSA id f23sm79067lfh.10.2021.04.29.13.09.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Apr 2021 13:09:58 -0700 (PDT) Date: Thu, 29 Apr 2021 23:09:56 +0300 From: Pavel Skripkin To: "Theodore Ts'o" Cc: Vegard Nossum , akpm@linux-foundation.org, peterz@infradead.org, axboe@kernel.dk, pmladek@suse.com, adilger.kernel@dilger.ca, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+d9e482e303930fa4f6ff@syzkaller.appspotmail.com Subject: Re: [PATCH] ext4: fix memory leak in ext4_fill_super Message-ID: <20210429230956.6ad23897@gmail.com> In-Reply-To: References: <20210428172828.12589-1-paskripkin@gmail.com> <3c3877a4-fef2-9e24-f99f-2ecc46deb7e4@oracle.com> <20210429143354.418248a7@gmail.com> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-suse-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Thu, 29 Apr 2021 13:05:01 -0400 "Theodore Ts'o" wrote: > On Thu, Apr 29, 2021 at 02:33:54PM +0300, Pavel Skripkin wrote: > > > > There is a chance, that kthread_stop() call will happen before > > threadfn call. It means, that kthread_stop() return value must be > > checked everywhere, isn't it? Otherwise, there are a lot of > > potential memory leaks, because some developers rely on the fact, > > that data allocated for the thread will be freed _inside_ thread > > function. > > That's not the only potential way that we could leak memory. Earlier > in kthread(), if this memory allocation fails, > > self = kzalloc(sizeof(*self), GFP_KERNEL); > > we will exit with -ENOMEM. So at the very least all callers of > kthread_stop() also need to check for -ENOMEM as well as -EINTR --- > or, be somehow sure that the thread function was successfully called > and started. In this particular case, the ext4 mount code had just > started the kmmpd thread, and then detected that something else had > gone wrong, and failed the mount before the kmmpd thread ever had a > chance to run. There is a small problem about -ENOMEM: static int kmmpd(void *data) { ... retval = read_mmp_block(sb, &bh_check, mmp_block); if (retval) { ext4_error_err(sb, -retval, "error reading MMP data: %d", retval); goto exit_thread; } ... exit_thread: EXT4_SB(sb)->s_mmp_tsk = NULL; kfree(data); brelse(bh); return retval; } read_mmp_block can return -ENOMEM. In this case double free will happen. I believe, we can change `return retval;` to `return 0;`, because kthread return value isn't checked anywhere. What do You think about it? With regards, Pavel Skripkin