sandbox: usb: Fix out-of-bounds read when fd=-1
authorSean Anderson <seanga2@gmail.com>
Wed, 23 Mar 2022 22:24:38 +0000 (18:24 -0400)
committerSimon Glass <sjg@chromium.org>
Tue, 28 Jun 2022 02:09:51 +0000 (03:09 +0100)
commit24057fe0a8f70ae872da0a8f4889fe7b8cfa09db
treed6f7949dca9f791d2ccca12cc2927822dfe295d2
parent42ae363ddd99c38ab26434b9d1c8b68610844e79
sandbox: usb: Fix out-of-bounds read when fd=-1

sandbox_flash_bulk uses priv->read_len to determine if priv->buff contains
the response data (such as from SCSI_INQUIRY). However, if priv->fd=-1 in
handle_read, then priv->read_len is not set even though we are going to
PHASE_DATA. This causes sandbox_flash_bulk to try and read len bytes from
priv->buff, which likely goes past the end of the buffer. Fix this by always
setting priv->read_len even if we aren't going to read anything.

Fixes: f4f715360c ("dm: usb: sandbox: Add an emulator for USB flash devices")
Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
drivers/usb/emul/sandbox_flash.c