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=-2.4 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_SANE_1 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 03860C433E0 for ; Tue, 2 Jun 2020 13:48:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 980DA206A2 for ; Tue, 2 Jun 2020 13:48:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=yandex-team.ru header.i=@yandex-team.ru header.b="WSk1w+1a" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 980DA206A2 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=yandex-team.ru Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id F1EAC28008A; Tue, 2 Jun 2020 09:48:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ECEF0280081; Tue, 2 Jun 2020 09:48:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DE45728008A; Tue, 2 Jun 2020 09:48:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0234.hostedemail.com [216.40.44.234]) by kanga.kvack.org (Postfix) with ESMTP id C3096280081 for ; Tue, 2 Jun 2020 09:48:55 -0400 (EDT) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 3F2F1181AEF1A for ; Tue, 2 Jun 2020 13:48:55 +0000 (UTC) X-FDA: 76884402630.06.guide61_6accd7ecb9d23 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin06.hostedemail.com (Postfix) with ESMTP id 0BF771003A85F for ; Tue, 2 Jun 2020 13:48:55 +0000 (UTC) X-HE-Tag: guide61_6accd7ecb9d23 X-Filterd-Recvd-Size: 4030 Received: from forwardcorp1j.mail.yandex.net (forwardcorp1j.mail.yandex.net [5.45.199.163]) by imf22.hostedemail.com (Postfix) with ESMTP for ; Tue, 2 Jun 2020 13:48:53 +0000 (UTC) Received: from mxbackcorp1j.mail.yandex.net (mxbackcorp1j.mail.yandex.net [IPv6:2a02:6b8:0:1619::162]) by forwardcorp1j.mail.yandex.net (Yandex) with ESMTP id 295442E1521; Tue, 2 Jun 2020 16:48:51 +0300 (MSK) Received: from myt5-70c90f7d6d7d.qloud-c.yandex.net (myt5-70c90f7d6d7d.qloud-c.yandex.net [2a02:6b8:c12:3e2c:0:640:70c9:f7d]) by mxbackcorp1j.mail.yandex.net (mxbackcorp/Yandex) with ESMTP id sIM0iAAhv4-mmeWtImL; Tue, 02 Jun 2020 16:48:51 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1591105731; bh=DZ+4m0IpKnoMyohjYdLVqfAqILLUbRSTQBZ19sLaXSo=; h=In-Reply-To:Message-ID:From:Date:References:To:Subject:Cc; b=WSk1w+1a3ejh4K4zEQyKiIetrZHihpYz6AshoFdoA9LCyHQJNcRh+npwxDth8EuwS SXJrST8fc7HNA3lG4Ufy0YgkRREoPcXEzuTGKXbk9eP1q4r3DQM483AYxHRi9uq/24 B9wKQ6ktt7CdcXav7PIArMtK5fuaDo88guQ+y8yg= Authentication-Results: mxbackcorp1j.mail.yandex.net; dkim=pass header.i=@yandex-team.ru Received: from dynamic-vpn.dhcp.yndx.net (dynamic-vpn.dhcp.yndx.net [2a02:6b8:b081:210::1:a]) by myt5-70c90f7d6d7d.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id niHGN11jDl-mmWqcDeN; Tue, 02 Jun 2020 16:48:48 +0300 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client certificate not present) Subject: Re: kobject_init_and_add is easy to misuse To: Matthew Wilcox , Wang Hai Cc: cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Greg Kroah-Hartman References: <20200602115033.1054-1-wanghai38@huawei.com> <20200602121035.GL19604@bombadil.infradead.org> From: Konstantin Khlebnikov Message-ID: Date: Tue, 2 Jun 2020 16:48:48 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 In-Reply-To: <20200602121035.GL19604@bombadil.infradead.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-CA Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 0BF771003A85F X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 02/06/2020 15.10, 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 > > 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. > At his point kobject doesn't own kmem-cache structure itself yet. So calling kobject_put() will free kmem-cache and then it will be freed second time on error path in create_cache(). I suppose freeing in case of error should be pushed from common create_cache() into slab-specific __kmem_cache_create().