[SCSI] sd: Fix potential out-of-bounds access

This patch fixes an out-of-bounds error in sd_read_cache_type(), found
by Google's AddressSanitizer tool. When the loop ends, we know that
"offset" lies beyond the end of the data in the buffer, so no Caching
mode page was found. In theory it may be present, but the buffer size
is limited to 512 bytes.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Reported-by: Dmitry Vyukov <dvyukov@google.com>
CC: <stable@vger.kernel.org>
Signed-off-by: James Bottomley <JBottomley@Parallels.com>

authored by Alan Stern and committed by James Bottomley 984f1733 76c4f79d

+3 -8
+3 -8
drivers/scsi/sd.c
··· 2420 2420 } 2421 2421 } 2422 2422 2423 - if (modepage == 0x3F) { 2424 - sd_printk(KERN_ERR, sdkp, "No Caching mode page " 2425 - "present\n"); 2426 - goto defaults; 2427 - } else if ((buffer[offset] & 0x3f) != modepage) { 2428 - sd_printk(KERN_ERR, sdkp, "Got wrong page\n"); 2429 - goto defaults; 2430 - } 2423 + sd_printk(KERN_ERR, sdkp, "No Caching mode page found\n"); 2424 + goto defaults; 2425 + 2431 2426 Page_found: 2432 2427 if (modepage == 8) { 2433 2428 sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);