Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp491747ybx; Wed, 6 Nov 2019 04:00:01 -0800 (PST) X-Google-Smtp-Source: APXvYqynf9QI2Eh5gBjiVkI187gi8C+5awcoWED74QxiajA/EGrWSm9XZMnkZ18XbgR9ox8d3niT X-Received: by 2002:a17:906:7e41:: with SMTP id z1mr26917126ejr.63.1573041601213; Wed, 06 Nov 2019 04:00:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573041601; cv=none; d=google.com; s=arc-20160816; b=tBC+7g47PRaelhYgTbSuqYyBezlENCLgAtLh4/Dtot4UfHe9VJMMkbPm6xGMWzuuZr 1qBLPjPz+4pcnso0d9KmuL2C6jYoMqVXlINJUyxJIzBFMKgzRpXhA4joU79SlRrjMNII AZ7p/nncYHl9dHAbez20092m6/U0Fme+Kx9Vi/GOSNHxJajOxDwDyBWxGttMbf8tRENt 4Tm0jDpaMtVIU3pwvL567qOJgbzdd7L8nGW+lcb8U+pJjnqE0BdGDqBpL03ydA7ZaVSs ilTi9hZxuBRTCyHMTIoP/b3Mtf690G31KXKR55+0Ca9jKxGEN3/uKf5MhatG04kNB3DS m2UQ== 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:date:cc:to:from:subject:message-id; bh=Uo3khRHP1F88UIYUZIAOH6bJTTpcrA8YCaScC+ii5ME=; b=X506rwWH5jG40bw0XRWtkNo5LN9Jg51wdvulqnzhuNd6fqIXVcdYUAVBho65bMe3tE m5OK/qLbpfzW0r0GAzRjT8bjsQ0OIZ37smBqy6WR7NgdjYkBZdGUgRIB/CJKrYz/UMYb /SGG2LCvX/Bqp4A73wWedEr7Y+UgB7UdryTWDKbO+naid2zRPw7h/SuIxvlB5wyogERU NwlecefCq1usyQDF0FvGz028nVYwyGeyK5D+yWzZLzsCMOQ5R+tfP+J++NAynKdengbP 6teTm63w3LYZ/vSzBv9UuzOCCD3acdqsZSCtqZ7QFqlCm0psF/lRTFD8op4ZEhzXfcr7 lshQ== 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 z58si12797724edd.283.2019.11.06.03.59.37; Wed, 06 Nov 2019 04:00:01 -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 S1730005AbfKFL7C (ORCPT + 99 others); Wed, 6 Nov 2019 06:59:02 -0500 Received: from mx2.suse.de ([195.135.220.15]:38930 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728716AbfKFL7C (ORCPT ); Wed, 6 Nov 2019 06:59:02 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 02267B162; Wed, 6 Nov 2019 11:59:00 +0000 (UTC) Message-ID: <1573040577.3090.22.camel@suse.com> Subject: Re: [PATCH] usb: appledisplay: fix use-after-free in bl_get_brightness From: Oliver Neukum To: Phong Tran , syzbot+495dab1f175edc9c2f13@syzkaller.appspotmail.com Cc: 2pi@mok.nu, alex.theissen@me.com, andreyknvl@google.com, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, syzkaller-bugs@googlegroups.com, linux-kernel-mentees@lists.linuxfoundation.org Date: Wed, 06 Nov 2019 12:42:57 +0100 In-Reply-To: <20191105233652.21033-1-tranmanphong@gmail.com> References: <00000000000042d60805933945b5@google.com> <20191105233652.21033-1-tranmanphong@gmail.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.26.6 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Am Mittwoch, den 06.11.2019, 06:36 +0700 schrieb Phong Tran: > In context of USB disconnect, the delaywork trigger and calling > appledisplay_bl_get_brightness() and the msgdata was freed. > > add the checking return value of usb_control_msg() and only update the > data while the retval is valid. Hi, I am afraid there are some issues with your patch. First, let me stress that you found the right place to fix an issue and you partially fixed an issue. But the the fix you applied is incomplete and left another issue open. Your patch still allows doing IO to a device that may already be bound to another driver. That is bad, especially as the buffer is already free. Yes, if IO is failing, you have fixed that narrow issue. But it need not fail. If you look into appledisplay_probe() you will see that it can fail because backlight_device_register() fails. The error handling will thereupon kill the URB and free memory. But it will not kill an already scheduled work. The scheduled work will then call usb_control_msg() on pdata->msgdata, which has been freed. If that IO fails, all is well. If not, the issue still exists. Secondly, your error check is off by 2. You are checking only for usb_control_msg() failing. But it can return only one byte instead of two. If that happens, the value you return is stale, although the buffer is correctly allocated. Regards Oliver The correct fix for both issues would be: #syz test: https://github.com/google/kasan.git e0bd8d79 From 2497a62bdbeb9bd94f690c22d96dd1b8cf22861f Mon Sep 17 00:00:00 2001 From: Oliver Neukum Date: Wed, 6 Nov 2019 12:36:28 +0100 Subject: [PATCH] appledisplay: fix error handling in the scheduled work The work item can operate on 1. stale memory left over from the last transfer the actual length of the data transfered needs to be checked 2. memory already freed the error handling in appledisplay_probe() needs to cancel the work in that case Signed-off-by: Oliver Neukum --- drivers/usb/misc/appledisplay.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/usb/misc/appledisplay.c b/drivers/usb/misc/appledisplay.c index ac92725458b5..ba1eaabc7796 100644 --- a/drivers/usb/misc/appledisplay.c +++ b/drivers/usb/misc/appledisplay.c @@ -164,7 +164,12 @@ static int appledisplay_bl_get_brightness(struct backlight_device *bd) 0, pdata->msgdata, 2, ACD_USB_TIMEOUT); - brightness = pdata->msgdata[1]; + if (retval < 2) { + if (retval >= 0) + retval = -EMSGSIZE; + } else { + brightness = pdata->msgdata[1]; + } mutex_unlock(&pdata->sysfslock); if (retval < 0) @@ -299,6 +304,7 @@ static int appledisplay_probe(struct usb_interface *iface, if (pdata) { if (pdata->urb) { usb_kill_urb(pdata->urb); + cancel_delayed_work_sync(&pdata->work); if (pdata->urbdata) usb_free_coherent(pdata->udev, ACD_URB_BUFFER_LEN, pdata->urbdata, pdata->urb->transfer_dma); -- 2.16.4