Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp370956imu; Thu, 3 Jan 2019 22:27:36 -0800 (PST) X-Google-Smtp-Source: ALg8bN7cBFgClwbxPRH23h9e15j4FLktOPEjn9MPVJCM+1M31tUtSoIa+Sc5FPvaol6VdacmwDaA X-Received: by 2002:a17:902:6502:: with SMTP id b2mr49028563plk.44.1546583256825; Thu, 03 Jan 2019 22:27:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546583256; cv=none; d=google.com; s=arc-20160816; b=lB+D57EAXT+e34Q2l7A0SsslLyTfp5OejsFfyxqtsMSk8tFk/FLD7ZeMwIF3xNC7bG BO8+LGczjvmLJCOa7iwNs+BbYUrdNW2K80f6V8FF94MYruLnIS8nz0dR+7aQR2W9Kxs3 t11qwASVGrwVdB7+mtgEZyluAKyTIQQFPEjGprK3n+SDZPxZn0nNPgY9BC+H6GreF7CL 8kyUiuHMkz+58TqXLgp1WAJAjtvPCP7Ex/hvLUVbg8UCGKYWzxUOM7fAnM3haVsU4Jan 5rrE3djj1b6nurqyBgCWzbyahP9z3ifKgcsJLUPPoq7zBrrWjDI9YlwcgBFv1FlojOm2 MRPQ== 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=8QkitembDs3JxuwT76LhhZE8OtslP8MuvqmttodPV7I=; b=uIdOS+PcawlI/FjbgtGUcgkmPujIaieIId7gA89PtOts/SHhehxB18MFOZ89vOJwNU 7IgX3Wkf24YXu/XFjFMZ4CpyHO8l+XTDbrNMJ1sHQXf1DPm03I86DO2X2CP627zmZ81I bIKYACEca7GPSWJ4j8e4H2zcPiM5hPRuThucYzDxUgHIoF6lWCzLjCJZWUdbKTIAQUdq V8JqOSyLlrvmds426rMaLsqf1HTyOOrot/Lj89xDdOj13fQcX7y9ZWHpWDlHMWZdlD5t BLn+pmsmbOwObTXDE8loWHzVeBOFyFZl+wXrZdfL73k+yw4ZcVynQMZQwjvF5QlEFlFE k6vg== 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 az5si34026169plb.166.2019.01.03.22.27.22; Thu, 03 Jan 2019 22:27:36 -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 S1727450AbfADES2 (ORCPT + 99 others); Thu, 3 Jan 2019 23:18:28 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:16676 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726389AbfADES2 (ORCPT ); Thu, 3 Jan 2019 23:18:28 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 81536AA141AA7; Fri, 4 Jan 2019 12:18:21 +0800 (CST) Received: from huawei.com (10.175.104.208) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.408.0; Fri, 4 Jan 2019 12:18:14 +0800 From: liujian To: CC: , , , Subject: [PATCH v2] driver: uio: fix possible memory leak and use-after-free in __uio_register_device Date: Fri, 4 Jan 2019 22:19:08 +0800 Message-ID: <1546611548-205765-1-git-send-email-liujian56@huawei.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.104.208] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 'idev' is malloced in __uio_register_device() and leak free it before leaving from the uio_get_minor() error handing case, it will cause memory leak. Also, in uio_dev_add_attributes() error handing case, idev is used after device_unregister(), in which 'idev' has been released, touch idev cause use-after-free. Fixes: a93e7b331568 ("uio: Prevent device destruction while fds are open") Fixes: e6789cd3dfb5 ("uio: Simplify uio error path by using devres functions") Signed-off-by: liujian --- v1->v2: change git log and fix code drivers/uio/uio.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/uio/uio.c b/drivers/uio/uio.c index 1313422..be2a943 100644 --- a/drivers/uio/uio.c +++ b/drivers/uio/uio.c @@ -940,9 +940,12 @@ int __uio_register_device(struct module *owner, atomic_set(&idev->event, 0); ret = uio_get_minor(idev); - if (ret) + if (ret) { + kfree(idev); return ret; + } + device_initialize(&idev->dev); idev->dev.devt = MKDEV(uio_major, idev->minor); idev->dev.class = &uio_class; idev->dev.parent = parent; @@ -953,7 +956,7 @@ int __uio_register_device(struct module *owner, if (ret) goto err_device_create; - ret = device_register(&idev->dev); + ret = device_add(&idev->dev); if (ret) goto err_device_create; @@ -985,9 +988,10 @@ int __uio_register_device(struct module *owner, err_request_irq: uio_dev_del_attributes(idev); err_uio_dev_add_attributes: - device_unregister(&idev->dev); + device_del(&idev->dev); err_device_create: uio_free_minor(idev); + put_device(&idev->dev); return ret; } EXPORT_SYMBOL_GPL(__uio_register_device); -- 2.7.4