From patchwork Mon Jun 17 11:15:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Garry X-Patchwork-Id: 166990 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp2725199ilk; Mon, 17 Jun 2019 04:17:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqw1TVoZXlb/tHCOvWSq8RVUAAT+pXnG3ws6tC9fSlAB0gEq+g99hjD8i2X5Vs5nGCf5ggKu X-Received: by 2002:a17:90a:24ac:: with SMTP id i41mr25197350pje.124.1560770238351; Mon, 17 Jun 2019 04:17:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560770238; cv=none; d=google.com; s=arc-20160816; b=EQk8CqqoZijuP7Un9NfGEjTsnuVFHiLD5CHzhvRwC9BvGUbhKACgsXFxyaKKuH7+ZW bA4I88oL38V0mHuBNfnNwq7eNClO54QOir2t4DF0p6h07dlA5uveVyp36XeeBIWQote8 U/XTAB2luc3KZswqDTSUMig0G4LkQaPbjCZV8AqGdB7bpWeVa6vlMX8+PWRwyBP59S+N pqDG9ELedu+5vJUVxKqK0F+/UrnH3FR64mkqrhMO8z5us/j+1brzbUjSi3yh9nLf8ZmO hCsisqIAgF1yefa+yWLNz85c3T+BBPHWxPEktCOJTV/BHIoYystVWzQoXOzixkjlAuac 7doA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=Q9EAcebVdUeYJoAMOjc1FhR1/77P6CCeJwc/nLiZJjQ=; b=p9FYhyh/6bWOrgcwBPIsZYy2YLN4kqD2sUT294DaFyfhoiRZa05npUmYi8P6ct6195 IXPlnsLLz3ABnX1R2QROEPquosGCzLrhhcCCFo0uAootAJx350U25mVmzlSOlVeHz31m 9mYEVWLV/zwF8aoMS/KMjrX2xbVhNTEFj4RPaKT2Qwlo+ZGaMhUdmcpDk1dI81i0GPSr dnfRgKCOBUQTJsM+GnwImPEm6nab/GfLp0gqukxTAY01JfnamwZZr+tmj7YVKHCD2jih 4hR9gkMmFFDwgZYqbdzGpfo24f5qZ/oxfCCf3XRLH54SIWg6wbk/G5HH159l11LXAUbt PIlg== 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 y11si9970979plr.381.2019.06.17.04.17.18; Mon, 17 Jun 2019 04:17:18 -0700 (PDT) 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 S1727979AbfFQLRQ (ORCPT + 26 others); Mon, 17 Jun 2019 07:17:16 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:37082 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727599AbfFQLRQ (ORCPT ); Mon, 17 Jun 2019 07:17:16 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 3F48C621E47ACC282D02; Mon, 17 Jun 2019 19:17:13 +0800 (CST) Received: from localhost.localdomain (10.67.212.75) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.439.0; Mon, 17 Jun 2019 19:17:02 +0800 From: John Garry To: CC: , , , , , John Garry Subject: [PATCH v3] bus: hisi_lpc: Avoid use-after-free from probe failure Date: Mon, 17 Jun 2019 19:15:48 +0800 Message-ID: <1560770148-57960-1-git-send-email-john.garry@huawei.com> X-Mailer: git-send-email 2.8.1 MIME-Version: 1.0 X-Originating-IP: [10.67.212.75] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If, after registering a logical PIO range, the driver probe later fails, the logical PIO range memory will be released automatically. This causes an issue, in that the logical PIO range is not unregistered and the released range memory may be later referenced. As an interim fix, allocate the logical PIO range with kzalloc() to avoid this memory being freed. Further memory will not be leaked from later attempts to probe the driver, as any attempts to allocate logical PIO ranges will fail, and we would release the 'range' memory. The correct fix for this problem would be to tear down what had been setup during the probe, i.e. unregister the logical PIO range, but this is not supported. Support for unregistering logical PIO ranges will need be to added in future. Fixes: adf38bb0b5956 ("HISI LPC: Support the LPC host on Hip06/Hip07 with DT bindings") Signed-off-by: John Garry --- Change in v2: - update commit message Change to v1: - add comment, as advised by Joe Perches -- 2.17.1 diff --git a/drivers/bus/hisi_lpc.c b/drivers/bus/hisi_lpc.c index 19d7b6ff2f17..5f0130a693fe 100644 --- a/drivers/bus/hisi_lpc.c +++ b/drivers/bus/hisi_lpc.c @@ -599,7 +599,8 @@ static int hisi_lpc_probe(struct platform_device *pdev) if (IS_ERR(lpcdev->membase)) return PTR_ERR(lpcdev->membase); - range = devm_kzalloc(dev, sizeof(*range), GFP_KERNEL); + /* Logical PIO may reference 'range' memory even if the probe fails */ + range = kzalloc(sizeof(*range), GFP_KERNEL); if (!range) return -ENOMEM; @@ -610,6 +611,7 @@ static int hisi_lpc_probe(struct platform_device *pdev) ret = logic_pio_register_range(range); if (ret) { dev_err(dev, "register IO range failed (%d)!\n", ret); + kfree(range); return ret; } lpcdev->io_host = range;