Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2974639imu; Sun, 9 Dec 2018 14:04:17 -0800 (PST) X-Google-Smtp-Source: AFSGD/VhjPsSP/VPEg/1acZnPH9j2xlsCHULzZjPizZyjcEwe2wAWJ34zlJvCYyb4gnb6tD7j9pI X-Received: by 2002:a63:a112:: with SMTP id b18mr8877531pgf.440.1544393057647; Sun, 09 Dec 2018 14:04:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544393057; cv=none; d=google.com; s=arc-20160816; b=LlIFhF0Mhr1YZG4/BEo32WJm7FSdHaMIhL6tOrVdxRX8tJKkgqdIZXHD1LtBOzk0Mn OksbrkzTkh8MWIbRm7APuei+7hTq9zYt0I4tj8L9elM4gaNYb2E7V7bGAX2dPA+2iWxu krENMYoJrlWivD0Tuvh9UWptgRjLcnoar468YMGI+paLZQS4JPAEthpsqHtyUtDINlht FN65McYv/He7faBiCGMB2LtdPthm6kg+BlhLQ+7r9i1otAsMK+JrebAx7GY4X6WjBpbL vaHwH5MrzED2+pAkdlEuzEMrSgqxEHhyU5d0od36wuYXrmPDvkjpdy2KkHZ6+y07igLO EGFg== 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:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=mCxfEa4hQ6sLVwEMj7L4SyJ/K/VBgprnIKSXf/OB4i4=; b=Dy5z1lY3Einhi0FwWIrMZcn9t8gzJjfQoLz6NsninIXMtD2aEoHmuLR6CkBdt+QRmw QYY4lHNTiYvacWBHNGEYIpEDqMe60imFbiWOH10k6OP80+uoza7X2dZ5lfFR1IpohEoU ybuBUHYoxHp7xb4MtFecG6vH1wXccShTZsWwbtIJYAceyE/LaX6fp7sua6Rd7bmd2qRA aUioff5pwZE2Ff8deBbvD9vDeixzEN6ZTEGMKeZXIiU13SvBap9RNxrX+AFPv1N/I4nj 7dj7YsNPhrlLjlof8M4smFTtJ7C0s0SwqC1zW5OIbVDFmrHjPwtaYLGg2ZhmWZAT1/Fe O1tw== ARC-Authentication-Results: i=1; mx.google.com; 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 ca19si9076663plb.238.2018.12.09.14.04.02; Sun, 09 Dec 2018 14:04: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; 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 S1727333AbeLIWCj (ORCPT + 99 others); Sun, 9 Dec 2018 17:02:39 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:35830 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726649AbeLIVzb (ORCPT ); Sun, 9 Dec 2018 16:55:31 -0500 Received: from pub.yeoldevic.com ([81.174.156.145] helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gW72u-0002ia-7e; Sun, 09 Dec 2018 21:55:28 +0000 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1gW72k-0003dF-1j; Sun, 09 Dec 2018 21:55:18 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Anton Vorontsov" , "Kees Cook" , "Joel Fernandes" , "Colin Cross" , "Tony Luck" , "nixiaoming" , "Geliang Tang" Date: Sun, 09 Dec 2018 21:50:33 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) X-Patchwork-Hint: ignore Subject: [PATCH 3.16 281/328] pstore/ram: Fix failure-path memory leak in ramoops_init In-Reply-To: X-SA-Exim-Connect-IP: 81.174.156.145 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.62-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Kees Cook commit bac6f6cda206ad7cbe0c73c35e494377ce9c4749 upstream. As reported by nixiaoming, with some minor clarifications: 1) memory leak in ramoops_register_dummy(): dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL); but no kfree() if platform_device_register_data() fails. 2) memory leak in ramoops_init(): Missing platform_device_unregister(dummy) and kfree(dummy_data) if platform_driver_register(&ramoops_driver) fails. I've clarified the purpose of ramoops_register_dummy(), and added a common cleanup routine for all three failure paths to call. Reported-by: nixiaoming Cc: Anton Vorontsov Cc: Colin Cross Cc: Tony Luck Cc: Joel Fernandes Cc: Geliang Tang Signed-off-by: Kees Cook [bwh: Backported to 3.16: adjust context] Signed-off-by: Ben Hutchings --- fs/pstore/ram.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) --- a/fs/pstore/ram.c +++ b/fs/pstore/ram.c @@ -557,8 +557,22 @@ static struct platform_driver ramoops_dr }, }; -static void ramoops_register_dummy(void) +static inline void ramoops_unregister_dummy(void) { + platform_device_unregister(dummy); + dummy = NULL; + + kfree(dummy_data); + dummy_data = NULL; +} + +static void __init ramoops_register_dummy(void) +{ + /* + * Prepare a dummy platform data structure to carry the module + * parameters. If mem_size isn't set, then there are no module + * parameters, and we can skip this. + */ if (!mem_size) return; @@ -588,21 +602,28 @@ static void ramoops_register_dummy(void) if (IS_ERR(dummy)) { pr_info("could not create platform device: %ld\n", PTR_ERR(dummy)); + dummy = NULL; + ramoops_unregister_dummy(); } } static int __init ramoops_init(void) { + int ret; + ramoops_register_dummy(); - return platform_driver_register(&ramoops_driver); + ret = platform_driver_register(&ramoops_driver); + if (ret != 0) + ramoops_unregister_dummy(); + + return ret; } postcore_initcall(ramoops_init); static void __exit ramoops_exit(void) { platform_driver_unregister(&ramoops_driver); - platform_device_unregister(dummy); - kfree(dummy_data); + ramoops_unregister_dummy(); } module_exit(ramoops_exit);