From: Phong Tran <tranmanphong@gmail.com>
To: Oliver Neukum <oneukum@suse.com>,
syzbot+495dab1f175edc9c2f13@syzkaller.appspotmail.com
Cc: tranmanphong@gmail.com, 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
Subject: Re: [PATCH] usb: appledisplay: fix use-after-free in bl_get_brightness
Date: Wed, 6 Nov 2019 21:22:34 +0700 [thread overview]
Message-ID: <d6c628d0-71ad-7343-d80e-1b0cd0795242@gmail.com> (raw)
In-Reply-To: <1573040577.3090.22.camel@suse.com>
On 11/6/19 6:42 PM, Oliver Neukum wrote:
> 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.
>
Hello Oliver,
argee, there need a cancel workqueue in case error of probe().
> 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 <oneukum@suse.com>
> 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 <oneukum@suse.com>
> ---
> 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];
> + }
compare with message size (2) can be considered.
if (retval == 2) {
brightness = pdata->msgdata[1];
} else {
retval = -EMSGSIZE;
}
Regards,
Phong.
> 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);
>
next prev parent reply other threads:[~2019-11-06 14:22 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-23 14:31 KASAN: use-after-free Read in appledisplay_bl_get_brightness syzbot
2019-11-05 23:36 ` [PATCH] usb: appledisplay: fix use-after-free in bl_get_brightness Phong Tran
2019-11-06 11:42 ` Oliver Neukum
2019-11-06 12:26 ` KASAN: use-after-free Read in appledisplay_bl_get_brightness syzbot
2019-11-06 14:22 ` Phong Tran [this message]
2019-11-07 8:50 ` [PATCH] usb: appledisplay: fix use-after-free in bl_get_brightness Oliver Neukum
2019-11-06 12:10 ` Andrey Konovalov
2019-11-06 12:11 ` Andrey Konovalov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d6c628d0-71ad-7343-d80e-1b0cd0795242@gmail.com \
--to=tranmanphong@gmail.com \
--cc=2pi@mok.nu \
--cc=alex.theissen@me.com \
--cc=andreyknvl@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel-mentees@lists.linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=oneukum@suse.com \
--cc=syzbot+495dab1f175edc9c2f13@syzkaller.appspotmail.com \
--cc=syzkaller-bugs@googlegroups.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome