Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp1541151ybg; Tue, 2 Jun 2020 12:50:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyFq3wgFVkPC+usuf70NqkHDCa3SYB9TwddvVvFKP4NzokEPD5xEAX69MU8DQ3iyn3zqr5+ X-Received: by 2002:aa7:cac8:: with SMTP id l8mr28593084edt.103.1591127412085; Tue, 02 Jun 2020 12:50:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591127412; cv=none; d=google.com; s=arc-20160816; b=J+bazgwz4t1WafUX/IctjOdO0pD0V2dVboBveDI74kncRs2OwhahiGMI3/CTmUAy+n OmrhEzWFSH7O8xoGdAIv2Rg4yFdA3U6IVxoLsiVozXiKeR04+TKKZ0aDZ6JXbO93eZQo e1+loyKvBPNOhap011lihMTQGJZddPCooS3TJcYjHdD/izyjJlEFg9ekDphn6xJWGxXe CyStGizVhWNmUBqLOU6K5HrqIMh6ikS7IoiR04PYqRNRP+IlDYbPhhWqdsoy4P3Iv3+J 6sY/KOEoIZprIe/2wnOe9RGjFjzTJ0CGwR4l+aUs8nhBghKmWyuXB3eYTebU/lzSmhSn cYlQ== 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=cDCbUswdOzWPBxtVOkNTOR8EXwqtruy6d+xYE1G/nrs=; b=sPuykVNEEua1XvOQyda0VtCxnVMhy0dN54s8sdLXcaQ1mij/mVDc8MOWZRo4CfB0ze JxemEqCaqdXKJWgi7y+wvhTgShjImayQEpgDkt7o9hnxus7sALaAcvWFk7ohoMbqA3DU wmBirW+2RQFpcVjopwAB233rMTro0tut0aEQ9PMjQbuTTbDSp2/nKuWAzE5GqiryfWGM GB2V8jfl5oU4mUSgdZGi4IpsBOdwcVIkklF4IZ/nErp3sV2YzakMLhjA0R4v+lMEV/B7 5rFUqrr+t4EQUI8k5ByjTm+bW8MjP6Idpf8XYnUODW96NY44yZ7dXroILQwRyVBLcSFd 25pw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=MejNCIwg; 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 m19si1904079ejd.738.2020.06.02.12.49.05; Tue, 02 Jun 2020 12:50:12 -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=pass header.i=@ziepe.ca header.s=google header.b=MejNCIwg; 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 S1727032AbgFBTqi (ORCPT + 99 others); Tue, 2 Jun 2020 15:46:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726139AbgFBTqh (ORCPT ); Tue, 2 Jun 2020 15:46:37 -0400 Received: from mail-qt1-x842.google.com (mail-qt1-x842.google.com [IPv6:2607:f8b0:4864:20::842]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15887C08C5C0 for ; Tue, 2 Jun 2020 12:46:36 -0700 (PDT) Received: by mail-qt1-x842.google.com with SMTP id k22so11675989qtm.6 for ; Tue, 02 Jun 2020 12:46:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=cDCbUswdOzWPBxtVOkNTOR8EXwqtruy6d+xYE1G/nrs=; b=MejNCIwgWGcd8L2XowGwm3NoBogEtmKYfDuWZoqncV+MARHDUvpzuuHI9oIfOFnoN9 dxhDF+ZfE6GSaT/0KMRiWYW0kI45FW6Df355Rqa+m5TWa7hDoJm8M7K7wgQBtutzHXiS rBy1T40rd4pXrsT7bnff1tVrYHCu3R9x3xXXeIfJJP/cNsh5yyZdOneP1oF26Xw0qjk3 JSkwyNEpo8k5KkuphfemPhDhMRpY4qdF+UyTE4AIh8UMf4pZ3NJ4lX4p9hrqCAgmILmH PTBvt1K9SUIjU9gMAZvkys0PA/VDHMA6wdk/3vtCpQqs14Hs6GbRytOxuG7VtJLq/4CD hMow== 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:references :mime-version:content-disposition:in-reply-to; bh=cDCbUswdOzWPBxtVOkNTOR8EXwqtruy6d+xYE1G/nrs=; b=ESIbWubp9I17S4ErR4urJg6ympUq5RHnx0D1I3FbXUVc0X54GSU5IGPHJGuhz95r+v J8r1/jGw7KN7I12ZY5dd0OliifwPX/AWTXzTlf1djSKdDJoDGld2rrD08aQma+VhwcK8 uttf5krjAugsUUCgUPM8bS5gSpwr+itV8bK9oCSPg90bYw+Z2+9DtQrjH2jpmFoXyuZI nGSD/EQB3NqeIfhEwcyhhmBoYjjfmuKyvDdi5zQr2IGwTISG5cYmWTZfvWBpf7+MAoXw S2mJ0KKKjOM6w0Jq4Qb0EkXQSt8JtNlnT7ZCKvdB6aC3c9uW6+uRrSX/LI5pXdvd1rie bARA== X-Gm-Message-State: AOAM532ajV80bkrQ7rLQRkJuvhoV/2Kfglz/fDs8/XKhhebdH47tL/vo JNQ0estNgzeBhK2LTcJS5DW2Kw== X-Received: by 2002:ac8:6d1b:: with SMTP id o27mr29064704qtt.102.1591127195246; Tue, 02 Jun 2020 12:46:35 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-156-34-48-30.dhcp-dynamic.fibreop.ns.bellaliant.net. [156.34.48.30]) by smtp.gmail.com with ESMTPSA id a1sm2418465qkn.87.2020.06.02.12.46.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jun 2020 12:46:34 -0700 (PDT) Received: from jgg by mlx with local (Exim 4.93) (envelope-from ) id 1jgCrp-000JR5-Qn; Tue, 02 Jun 2020 16:46:33 -0300 Date: Tue, 2 Jun 2020 16:46:33 -0300 From: Jason Gunthorpe To: Matthew Wilcox 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, Greg Kroah-Hartman Subject: Re: kobject_init_and_add is easy to misuse Message-ID: <20200602194633.GC6578@ziepe.ca> References: <20200602115033.1054-1-wanghai38@huawei.com> <20200602121035.GL19604@bombadil.infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200602121035.GL19604@bombadil.infradead.org> 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 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() > 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. There are APIs that auto-free their argument on failure and last times I checked one, about half the tree had a tricky use-after free bug on the error path. > 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. IMHO these functions that hide an 'init' inside (eg the switch from kfree to refcount in the error unwind) are tricky. The caller must switch from some kfree goto error unwind to a put goto error unwind, which is very unnatural and strange. I think it is better if the init is near the kalloc and the entire error unwind always uses put. No switching. Jason