Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp978430ybt; Wed, 17 Jun 2020 19:44:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxyq4RTmHUqesjQZRtqCedMRP/oAoWGu+QQbvF5RlbzHjQAKfFxxMCJzcy+sIfTA8xIhsL2 X-Received: by 2002:aa7:d6d0:: with SMTP id x16mr2051399edr.175.1592448283455; Wed, 17 Jun 2020 19:44:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592448283; cv=none; d=google.com; s=arc-20160816; b=nMTdiZBai/6jkG4pWKA7cMTUu3FBI1eeFwMAo+WiPCtaVrJ4mJpWAmpLyipJ2SQdPh 7b+Pa/H2PFR0OWtvZPqE4vouiwzHKZpfNavg435orzxNEz1HP5FCtJXkh3kpKae2xsFr ObR3G25fx/yywp0oAfTYxtftL/qKomx6A6fUWQtKkKbTDyja6bOkcRSvydvqNyyWlXVI W3gMu61UGqtT4L5ZIl198V9EpIM3uZTfMLsshVYZinaUeRftawkUnZ6rGH6/Sw8pIG5+ vy4lZG+xELN+xTHfeVJijxmJt/UT8ot9GWVYKn+QSxzKSFXpz4VNH6auch/lDm0Vc85z 9zhA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Ox1haxpC2WU8JM58hy9lJIMxErO3S4nYUhMzH3/VXwI=; b=EgnaPTD33/3PT/7MfVtocjuN7xQjz2Ygk2UF4Ie6rHiq1w9gRvDZQsIsn2BsVK4N7d mzNP05n903A++nhlEMYvJMOh58KcRTWIrfzjrMPSOjp9INtK4zFnRI4xur1d/RPsbY9/ Uq1oOD/ylasiL6inwmwnaO9q2143s0vuhZHHThkD6XGmFGYDZlTHE+aXJuFktKX7FzG3 X8Fk+sn6YmJJVHA6xFbtFFY4v1QOIHUI0yVg9OxZO2Mr/hhzQEF00BztUqNCmLlyJdkH GaWdCn//J4V2ivczfaId9h+X8gsNZligukq9/nqRjO3S5xDU9/1z0q+CBQrDt9os+utt lHLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Qcfs1n7C; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a9si1094586edv.489.2020.06.17.19.44.21; Wed, 17 Jun 2020 19:44:43 -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=@kernel.org header.s=default header.b=Qcfs1n7C; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729397AbgFRCmm (ORCPT + 99 others); Wed, 17 Jun 2020 22:42:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:40120 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728816AbgFRBL4 (ORCPT ); Wed, 17 Jun 2020 21:11:56 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 251C120EDD; Thu, 18 Jun 2020 01:11:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592442715; bh=XRWEoNe7kz+2LIoGd2+9A18UJmAwRIuvhETjbiu5TQA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Qcfs1n7CQK+Lhzl7ZyBGjfZfxXg6gdYojTm2U1G6T4G6Btje2OtWvy3COcB1UVyx3 tIqr5QxlDMTQev+33Ku5S6aqpljd889W5K+yhzW1qH3QiFtdEeFCWDlQFCN8rU1B+n aexkqGrdJOdtEcKe21Kc+q5kiFA7vXiL/d11CmmE= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Kuppuswamy Sathyanarayanan , Greg Kroah-Hartman , Sasha Levin Subject: [PATCH AUTOSEL 5.7 175/388] drivers: base: Fix NULL pointer exception in __platform_driver_probe() if a driver developer is foolish Date: Wed, 17 Jun 2020 21:04:32 -0400 Message-Id: <20200618010805.600873-175-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200618010805.600873-1-sashal@kernel.org> References: <20200618010805.600873-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kuppuswamy Sathyanarayanan [ Upstream commit 388bcc6ecc609fca1b4920de7dc3806c98ec535e ] If platform bus driver registration is failed then, accessing platform bus spin lock (&drv->driver.bus->p->klist_drivers.k_lock) in __platform_driver_probe() without verifying the return value __platform_driver_register() can lead to NULL pointer exception. So check the return value before attempting the spin lock. One such example is below: For a custom usecase, I have intentionally failed the platform bus registration and I expected all the platform device/driver registrations to fail gracefully. But I came across this panic issue. [ 1.331067] BUG: kernel NULL pointer dereference, address: 00000000000000c8 [ 1.331118] #PF: supervisor write access in kernel mode [ 1.331163] #PF: error_code(0x0002) - not-present page [ 1.331208] PGD 0 P4D 0 [ 1.331233] Oops: 0002 [#1] PREEMPT SMP [ 1.331268] CPU: 3 PID: 1 Comm: swapper/0 Tainted: G W 5.6.0-00049-g670d35fb0144 #165 [ 1.331341] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 [ 1.331406] RIP: 0010:_raw_spin_lock+0x15/0x30 [ 1.331588] RSP: 0000:ffffc9000001be70 EFLAGS: 00010246 [ 1.331632] RAX: 0000000000000000 RBX: 00000000000000c8 RCX: 0000000000000001 [ 1.331696] RDX: 0000000000000001 RSI: 0000000000000092 RDI: 0000000000000000 [ 1.331754] RBP: 00000000ffffffed R08: 0000000000000501 R09: 0000000000000001 [ 1.331817] R10: ffff88817abcc520 R11: 0000000000000670 R12: 00000000ffffffed [ 1.331881] R13: ffffffff82dbc268 R14: ffffffff832f070a R15: 0000000000000000 [ 1.331945] FS: 0000000000000000(0000) GS:ffff88817bd80000(0000) knlGS:0000000000000000 [ 1.332008] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 1.332062] CR2: 00000000000000c8 CR3: 000000000681e001 CR4: 00000000003606e0 [ 1.332126] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 1.332189] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 1.332252] Call Trace: [ 1.332281] __platform_driver_probe+0x92/0xee [ 1.332323] ? rtc_dev_init+0x2b/0x2b [ 1.332358] cmos_init+0x37/0x67 [ 1.332396] do_one_initcall+0x7d/0x168 [ 1.332428] kernel_init_freeable+0x16c/0x1c9 [ 1.332473] ? rest_init+0xc0/0xc0 [ 1.332508] kernel_init+0x5/0x100 [ 1.332543] ret_from_fork+0x1f/0x30 [ 1.332579] CR2: 00000000000000c8 [ 1.332616] ---[ end trace 3bd87f12e9010b87 ]--- [ 1.333549] note: swapper/0[1] exited with preempt_count 1 [ 1.333592] Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000009 [ 1.333736] Kernel Offset: disabled Note, this can only be triggered if a driver errors out from this call, which should never happen. If it does, the driver needs to be fixed. Signed-off-by: Kuppuswamy Sathyanarayanan Link: https://lore.kernel.org/r/20200408214003.3356-1-sathyanarayanan.kuppuswamy@linux.intel.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/base/platform.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/base/platform.c b/drivers/base/platform.c index b27d0f6c18c9..f5d485166fd3 100644 --- a/drivers/base/platform.c +++ b/drivers/base/platform.c @@ -851,6 +851,8 @@ int __init_or_module __platform_driver_probe(struct platform_driver *drv, /* temporary section violation during probe() */ drv->probe = probe; retval = code = __platform_driver_register(drv, module); + if (retval) + return retval; /* * Fixup that section violation, being paranoid about code scanning -- 2.25.1