Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp2587964imb; Mon, 4 Mar 2019 08:50:54 -0800 (PST) X-Google-Smtp-Source: APXvYqwyUoRRLUYEUWYfpYlUOE3VGRB+w3/mcJyQdm0EJYkKkdO+GH2kM4a72pTDCwh318KNPIkA X-Received: by 2002:a17:902:bd43:: with SMTP id b3mr21522496plx.186.1551718254668; Mon, 04 Mar 2019 08:50:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551718254; cv=none; d=google.com; s=arc-20160816; b=V0ZEODhs9+Ikue2nCCyLB8ZeoOifzwQ/ddsJBvg5ydNSn+eYbSZIg7B+lTTnSHyeOT r44PfpxZll2pzU8q05785FtFh99j+Zlf2EO0eQe5h3nZ8VTir+7AYG6x1mcKAfIoVpX4 2i2wiykR/bs8PE6vUs1HVp11/IDwks+mGRlghESn8vtzO7ohCQv9paMxKTKWd/XxiQuW 1+iQhK3A8MbY7YYiedgnftqcSJMEVtHEHwsDfmfQf5BLyFF8cWw+BXrgpQF4WpiBiCEk OYISf0vvDH4VMq8BfJF5AJuOE+rh1cVOmwrI0Gp0Gbzz1O2g7cC1qd/gvokUKjTmLzEh QspA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=ze7c7Ip+VLn5lQ9SGdkecFqbPBGWYqPFeRZSa/8f8N0=; b=sUNU+WNf9guneUW853c8YaERdWFPa6bCL3FPlAiarNr5Ln+MxBc/SslViSpgLF7T6K cy7oeL5nrOtwYG33kfY8/7oH1pxZrMxH1jZTA0CHwvQFF9gkTck8JERKL2oLf1wtefuD 58ZxIID9GMQe7EfAonCHFLifv8GoXsqEFCaYkXfwlBi0XEXlKGHpLFvuixyF2EckGHtT 6t0LYJGJSNrcmVtifRNf1zrDa+wKzS9ggyaawBVlGnQ3crHhu71pfvRLcCljD2DUN1TA ZCxpJOiHD6TzR4Gws2dhvLHxMujurm/PrvNRwHHAR5c1EW74vLskVj/pvaZbbLW25+t1 5YAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=V34ObfZt; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l187si5815193pge.71.2019.03.04.08.50.39; Mon, 04 Mar 2019 08:50:54 -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=@oracle.com header.s=corp-2018-07-02 header.b=V34ObfZt; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727084AbfCDPhi (ORCPT + 99 others); Mon, 4 Mar 2019 10:37:38 -0500 Received: from userp2130.oracle.com ([156.151.31.86]:57916 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726961AbfCDPhi (ORCPT ); Mon, 4 Mar 2019 10:37:38 -0500 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x24FSlaM101088; Mon, 4 Mar 2019 15:37:26 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to; s=corp-2018-07-02; bh=ze7c7Ip+VLn5lQ9SGdkecFqbPBGWYqPFeRZSa/8f8N0=; b=V34ObfZtY3w/sMYp8jRd6BSYLWT6IUlrno6Iea2h8ZHJb84HsWqPRsPQXFZJQ5FQFVyD Bkiok/yN48JHLRxD6w3mP4Y67IZ8qwujZcj6XRLolY7LtFrttwbikF0+EUG3mNPvNKmE QhK5sgPy9A12dChkO+kQFRBFKC3/S1HrIlfdwifUhvm0+vxToHUq+wwWYXHb73E1LA8q 0XEBJq70lOg2gd6lmyY8qcRSoeIMkfm4JAD7QrhhBTqZUbGb5hcjbdvmzwuNq/RgpiYW DKrEp3xqcMjdgZOAYQnJ6Hy6jm2p7CxujTgfKiRoDLfV/29sbsxc3+tX5FI+6NK2j5Oi jg== Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by userp2130.oracle.com with ESMTP id 2qyh8tyj7p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 04 Mar 2019 15:37:25 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id x24FbOvo027884 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 4 Mar 2019 15:37:24 GMT Received: from abhmp0002.oracle.com (abhmp0002.oracle.com [141.146.116.8]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id x24FbNrr014364; Mon, 4 Mar 2019 15:37:23 GMT Received: from kadam (/197.157.34.180) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 04 Mar 2019 07:37:22 -0800 Date: Mon, 4 Mar 2019 18:37:14 +0300 From: Dan Carpenter To: Anders Roxell Cc: erico.nunes@datacom.ind.br, wsa@the-dreams.de, Linux Kernel Mailing List Subject: Re: BUG: KASAN: i2c dev use after free Message-ID: <20190304153714.GD13452@kadam> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9184 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1903040113 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org I wasn't trying to fix anything so complicated as this, it was a more obvious use after free. It does feel intuitively that we should call device_destroy() before the cdev_del() so that it's in the reverse order from how it was allocated but I don't see any reason to think that will make a difference. --- drivers/i2c/i2c-dev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index 3f7b9af11137..7e85e2cf26c1 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -687,9 +687,9 @@ static int i2cdev_detach_adapter(struct device *dev, void *dummy) if (!i2c_dev) /* attach_adapter must have failed */ return 0; + device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr)); cdev_del(&i2c_dev->cdev); put_i2c_dev(i2c_dev); - device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr)); pr_debug("i2c-dev: adapter [%s] unregistered\n", adap->name); return 0; -- 2.17.1