Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp1362439ybg; Tue, 2 Jun 2020 08:02:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxv999zSOjrZuiQOY6KSN4H/f8GMBQ2NOmsZxq1Z8ZJKzLhyfS930IgT0fOr76CrToZjK9a X-Received: by 2002:a17:906:1b48:: with SMTP id p8mr22315666ejg.399.1591110141022; Tue, 02 Jun 2020 08:02:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591110141; cv=none; d=google.com; s=arc-20160816; b=uF6UwPBNsDP5yDqi5DI//aGKE8+N9W4r9soU9GN14vM7EBKFzsW71rqvrKkYHbS6Nv u6l9rNcSZ/sgESN8o/19Qo1zBWIeAO3u4Oiw1bfcgmR15BQRnu/Tn37VzpHRZKb7cWtr rUbVa1VzhVH3ZDlE3fECIItQ0ce3i0GoHBH6NKLhPxqySwhd0/Nu8KSgA/ZVpyBScuCe J5rkah34mB/TbNc/vH9GOQEkP+H1fTzxB7PNWUqpNGK1hYDAC3wFILL7DhEnCSvceVoW ChMFw4CC0UiCLKLp8jsNeJVmMHGP4u5gJuZGzMTqMwiZ4bblMC+KulVSiiXCFrdzdKj6 s2cg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=YkOeRP7ixrxUjRV41/evzXHhbGKPE5POCnQ7Px2to4s=; b=xENoEzRLtH+/c+z1X0QWXVk1mEmdj+wvAvkSFYHCaijOrTUb0qYS/bmFjqfZ/AdYfn wU33+Z/JFa/+uh7przJ29F1a6Pwczc1XcT8VNF7/Td4J2akWfFUWX0te1K8dz2NVFJBw T2DpGRc9KXYiZ6yS4GOGXoUDUBk4Oj8hnSDkee0n8YajiYJcdx0idZZqkuaXIPm/1c5G pwwbLR3KEGNWBzkcvHKNP9ORjv0/PQvKUKcBG1ho9fbRBomMO8IL0YhzztPzrO5ifD7a kUOFegp5nDimFF8AlMe0XDsDGkFNIDW1vlqb1TiVdVq1Niw23KQKdL637Ou4Y85h6Wdh /NNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=N1Ne1IWH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a19si1453848ejf.724.2020.06.02.08.01.54; Tue, 02 Jun 2020 08:02:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=N1Ne1IWH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726625AbgFBO5j (ORCPT + 99 others); Tue, 2 Jun 2020 10:57:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726261AbgFBO5j (ORCPT ); Tue, 2 Jun 2020 10:57:39 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5290DC08C5C0 for ; Tue, 2 Jun 2020 07:57:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=YkOeRP7ixrxUjRV41/evzXHhbGKPE5POCnQ7Px2to4s=; b=N1Ne1IWHT2VVZ68QfUBRERhVcC GLswuMI2i2LAwW8nAzxA8I1Yfb53j2eysHhgitbdFiHKsmzagBHgFrsiO2HfHvAaFEurQQcYhO7J4 K0pSmAG5vkOn+0trmGxLRAx9miQYYaDKKLJBYkuge22VELS7MF8N2m/omJGQeQGSB+ucFnmnfKlSS QThqXz1xS/fjO17Vuqf81wJo3ukUeNgf8ZcH0i3SyfSZB29gBScvUktbi65QpirPDSKon8OrGN5nL HT6omhcISpUFqrdk73ah/c1H+Cvn4FX5cgJBGwualPA1OxmvLys7amsfwQMvDx1kM1qiP+04xPrG1 9Gn5Z92g==; Received: from willy by bombadil.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1jg8Lx-0005bR-AX; Tue, 02 Jun 2020 14:57:21 +0000 Date: Tue, 2 Jun 2020 07:57:21 -0700 From: Matthew Wilcox To: Greg Kroah-Hartman Cc: Wang Hai , cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org, khlebnikov@yandex-team.ru, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: kobject_init_and_add is easy to misuse Message-ID: <20200602145721.GM19604@bombadil.infradead.org> References: <20200602115033.1054-1-wanghai38@huawei.com> <20200602121035.GL19604@bombadil.infradead.org> <20200602140404.GA3280145@kroah.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200602140404.GA3280145@kroah.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 02, 2020 at 04:04:04PM +0200, Greg Kroah-Hartman wrote: > On Tue, Jun 02, 2020 at 05:10:35AM -0700, Matthew Wilcox wrote: > > On Tue, Jun 02, 2020 at 07:50:33PM +0800, Wang Hai wrote: > > > syzkaller reports for memory leak when kobject_init_and_add() > > > returns an error in the function sysfs_slab_add() [1] > > > > > > When this happened, the function kobject_put() is not called for the > > > corresponding kobject, which potentially leads to memory leak. > > > > > > This patch fixes the issue by calling kobject_put() even if > > > kobject_init_and_add() fails. > > > > I think this speaks to a deeper problem with kobject_init_and_add() > > -- the need to call kobject_put() if it fails is not readily apparent > > to most users. This same bug appears in the first three users of > > kobject_init_and_add() that I checked -- > > arch/ia64/kernel/topology.c > > drivers/firmware/dmi-sysfs.c > > drivers/firmware/efi/esrt.c > > drivers/scsi/iscsi_boot_sysfs.c > > > > Some do get it right -- > > arch/powerpc/kernel/cacheinfo.c > > drivers/gpu/drm/ttm/ttm_bo.c > > drivers/gpu/drm/ttm/ttm_memory.c > > drivers/infiniband/hw/mlx4/sysfs.c > > Why are random individual drivers calling kobject* functions? That > speaks to a larger problem here... There's around 120 callers in the kernel today ... large, indeed. > Anyway, yes, it's a tricky function, but the issue usually is that the > kobject is embedded in something else and if you call init_and_add() you > want to tear things down _before_ the final put happens. > > The good thing is, that function is really hard to get to fail except if > you abuse it with syzkaller :) Yes ;-) > > I'd argue that the current behaviour is wrong, that kobject_init_and_add() > > should call kobject_put() if the add fails. This would need a tree-wide > > audit. But somebody needs to do that anyway because based on my random > > sampling, half of the users currently get it wrong. > > As said above, this is "tricky", and might break things. My audit may not be correct then. The kobject_put() may be appropriately being called at a higher level rather than in the same function as the kobject_init_and_add().