Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp4287315ybc; Fri, 15 Nov 2019 02:07:17 -0800 (PST) X-Google-Smtp-Source: APXvYqzWwGeCKdbkJ8ptqb1V8UIf7zBtDvTYoVRiXKHTCXSPpYx8+1g+I7p7mnAvVb3HyqRuc4qu X-Received: by 2002:a17:906:3f8a:: with SMTP id b10mr12645759ejj.315.1573812437740; Fri, 15 Nov 2019 02:07:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573812437; cv=none; d=google.com; s=arc-20160816; b=bUNn5LpQELV3F7O3ZtOlZXBRi3hLKKtw/ZZ4zH79cNa3LdavUi28ilCBTNKyXT8o7z CF28tXUkBwQiJhqWnp5iXWAPwxwAYNQ2bSbocjCq8dNJuASJddI4+CcQ6coGhAmUsK53 ZKvzgPJY7gZbr7UjdCwqIh4kTTNP4WNUX0WxkMhIfbGaYQ69bZBt//nsbwM/t66w56bi vIiA8ZHmyDq1hadzGLX2pKpCmHxakZDq4VMIUJmrJVW7tO4xO9sO6xHmr3rqioqeludB TH7mUZ0KYbGKOpk68zympCOugxgxPOcPWFuDN5I/Cjp/koM8JGdh19RKIsiiljpEq3wS Y18w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:message-id:in-reply-to:date:references:subject:cc:to :from:dkim-signature; bh=E3sXchnQK6r0cbCO41TzSdNyYDZO5b9qiU92SWHzyaY=; b=MCc1CxnBW9pmTUPIj7ZIlXDq4m0VZkvSDm7U2HWb362hdqZdShMvQsTRgMBLOQHjQ1 psq5lEozGdyo8O7IcUdqD3l9b1/CZefrCn+M9QelcTv+pRNyaRr0rp+D7qofPFKwj8ez urrZjbUV83naJlG/iqMgR7raJo9VcdiEqSggXKrMlIyObC4SCrA7WDC6kl0aB5QsX108 OwCK6o3J2BDf5O6mywTKlgk8n0b/YzNj2fdnfBEpZoEUxnR43W/cGl52G4hPMVTtvS4j +UnlgIu1w/M6ecsadfUwcdsVAaQ6Z7tcjLXrBeBmgF+kd7d8xtsH1fK55azQnIsOsFpD ob4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@unikie-com.20150623.gappssmtp.com header.s=20150623 header.b=GGQXyqfA; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j21si5226071ejd.265.2019.11.15.02.06.51; Fri, 15 Nov 2019 02:07:17 -0800 (PST) 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=@unikie-com.20150623.gappssmtp.com header.s=20150623 header.b=GGQXyqfA; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727208AbfKOKF0 (ORCPT + 99 others); Fri, 15 Nov 2019 05:05:26 -0500 Received: from mail-lj1-f195.google.com ([209.85.208.195]:35784 "EHLO mail-lj1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727632AbfKOKFY (ORCPT ); Fri, 15 Nov 2019 05:05:24 -0500 Received: by mail-lj1-f195.google.com with SMTP id r7so10073832ljg.2 for ; Fri, 15 Nov 2019 02:05:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=unikie-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-transfer-encoding; bh=E3sXchnQK6r0cbCO41TzSdNyYDZO5b9qiU92SWHzyaY=; b=GGQXyqfAOOdn2W+1sXwtBvsy92bU6TkWIfV4fpktp026z4PB3TSvxZ0SBhT8E7OlT6 Q0NPLYkSp6Ly+KCZ0MCiexwh4ckM7I+V5D/eNPIuOF+QJXEDzTa25PYbqdH3XS+QYyRT xK0lc680H8iIyhT8xFqqYqJLoGbSe2S3PGPAlDbsEqKs0yKUCU1msu/kMU7uRfHP/nYE EWYzNYe+dL5s9xxB5FsA14YsJeLZAShYgAhjgagOvHfMwGi47t72O4u5Nk085epZU8J1 w7Ms9aP3ZzLHfvR0YEPibjX+UzuHx5M3peE6Hz3bQVElSqHGtAYG8G/LqF6Zf/zBtbzB fIpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-transfer-encoding; bh=E3sXchnQK6r0cbCO41TzSdNyYDZO5b9qiU92SWHzyaY=; b=Vs8/oTD2YvLuhoT9CMaQVlxsSI539zwB4/qqNMKf8qomqEZRUIJ0cc7cJ1WdPGzcL9 4gOYsF/9V6Rr/+v/Kso2MBg0+MLtiDggJHfNWrhKNx2WZRYOPoH55ozJlCn6x14m1KvK KX9+FVxqnZAcVJIDc5dnh6gJ5Y4CrmAdhj2/A/O20F65MWrC66dNa7lygN/ew254ymuX EYtBqPsnDoQLusJ8OemOx8GAejT28p0DyxAd1pcKW9M06pJug+s7IDkEcPck6KkJan8d MMFpLAMIN2VEgCnMS9BMeupEzlj8V0tNhz9eh8acmVeWI7j0PH7tiZwYC4j5ZUdxHd9G PCJA== X-Gm-Message-State: APjAAAU5osjr0Y2SKfqc5pMWA579PQ2JTU1u5enYuR9RuFlaicviYx/Q MHtDTCQ5QmUG3cSWhFkUKyZb6uFqnLfSLw== X-Received: by 2002:a2e:7204:: with SMTP id n4mr9981244ljc.139.1573812318913; Fri, 15 Nov 2019 02:05:18 -0800 (PST) Received: from GL-434 ([109.204.235.119]) by smtp.gmail.com with ESMTPSA id e14sm3727391ljb.75.2019.11.15.02.05.17 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 15 Nov 2019 02:05:18 -0800 (PST) From: jouni.hogander@unikie.com (Jouni =?utf-8?Q?H=C3=B6gander?=) To: Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, "Rafael J. Wysocki" , Lukas Bulwahn Subject: Re: [PATCH] drivers/base: Fix memory leak in error paths References: <20191114121840.5585-1-jouni.hogander@unikie.com> <20191115032603.GG793701@kroah.com> <878soha7tc.fsf@unikie.com> <20191115082022.GB55909@kroah.com> Date: Fri, 15 Nov 2019 12:05:17 +0200 In-Reply-To: <20191115082022.GB55909@kroah.com> (Greg Kroah-Hartman's message of "Fri, 15 Nov 2019 16:20:22 +0800") Message-ID: <8736epa202.fsf@unikie.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Greg Kroah-Hartman writes: >> >> This memory leak was reported by Syzkaller: >> >>=20 >> >> BUG: memory leak unreferenced object 0xffff8880675ca008 (size 256): >> >> comm "netdev_register", pid 281, jiffies 4294696663 (age 6.808s) >> >> hex dump (first 32 bytes): >> >> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ >> >> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ >> >> backtrace: >> >> [<0000000058ca4711>] kmem_cache_alloc_trace+0x167/0x280 >> >> [<000000002340019b>] device_add+0x882/0x1750 >> >> [<000000001d588c3a>] netdev_register_kobject+0x128/0x380 >> >> [<0000000011ef5535>] register_netdevice+0xa1b/0xf00 >> >> [<000000007fcf1c99>] __tun_chr_ioctl+0x20d5/0x3dd0 >> >> [<000000006a5b7b2b>] tun_chr_ioctl+0x2f/0x40 >> >> [<00000000f30f834a>] do_vfs_ioctl+0x1c7/0x1510 >> >> [<00000000fba062ea>] ksys_ioctl+0x99/0xb0 >> >> [<00000000b1c1b8d2>] __x64_sys_ioctl+0x78/0xb0 >> >> [<00000000984cabb9>] do_syscall_64+0x16f/0x580 >> >> [<000000000bde033d>] entry_SYSCALL_64_after_hwframe+0x44/0xa9 >> >> [<00000000e6ca2d9f>] 0xffffffffffffffff >> > >> > How is this a leak? This is in device_add(), not removing the device. >> > When the structure really is freed then it can be removed. >>=20 >> In net/core/net-sysfs.c:netdev_register_kobject device_add allocates >> dev->p. Now if register_queue_kobjects fails the error path is calling >> device_del and dev->p is never freed. Proper fix here could be to call >> put_device after device_del? > > Hm, this sounds like you have a reference count leak here, as > put_device() should be properly called already in this case. You might > want to look further to see where exactly the register_queue_kobjects() > call fails in order to see if we grabbed a reference we forgot to put > back on an error path. Ok, did some more debugging on this. net/core/net-sysfs.c:netdev_register_kobject is doing device_initialize(dev). This is in drivers/base/core.c:device_initialize: * NOTE: Use put_device() to give up your reference instead of freeing * @dev directly once you have called this function. My understanding is that remaining reference on error path is taken by device_initialize and as instructed in the note above it should be given up using put_device? Tested this and it's fixing the memory leak I found in my Syzkaller exercise. Addition to that it seems to be fixing also this one: https://syzkaller.appspot.com/bug?id=3Df5f4af9fb9ffb3112ad6e30f717f769decdc= cdfc BR, Jouni H=C3=B6gander