Skip to content

Commit 1892bf9

Browse files
AlanSterngregkh
authored andcommitted
USB: usb-storage: Fix use of bitfields for hardware data in ene_ub6250.c
The kernel test robot found a problem with the ene_ub6250 subdriver in usb-storage: It uses structures containing bitfields to represent hardware bits in its SD_STATUS, MS_STATUS, and SM_STATUS bytes. This is not safe; it presumes a particular bit ordering and it assumes the compiler will not insert padding, neither of which is guaranteed. This patch fixes the problem by changing the structures to simple u8 values, with the bitfields replaced by bitmask constants. CC: <[email protected]> Signed-off-by: Alan Stern <[email protected]> Link: https://lore.kernel.org/r/YjOcbuU106UpJ/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 6653b82 commit 1892bf9

File tree

1 file changed

+76
-79
lines changed

1 file changed

+76
-79
lines changed

drivers/usb/storage/ene_ub6250.c

Lines changed: 76 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -237,36 +237,33 @@ static struct us_unusual_dev ene_ub6250_unusual_dev_list[] = {
237237
#define memstick_logaddr(logadr1, logadr0) ((((u16)(logadr1)) << 8) | (logadr0))
238238

239239

240-
struct SD_STATUS {
241-
u8 Insert:1;
242-
u8 Ready:1;
243-
u8 MediaChange:1;
244-
u8 IsMMC:1;
245-
u8 HiCapacity:1;
246-
u8 HiSpeed:1;
247-
u8 WtP:1;
248-
u8 Reserved:1;
249-
};
250-
251-
struct MS_STATUS {
252-
u8 Insert:1;
253-
u8 Ready:1;
254-
u8 MediaChange:1;
255-
u8 IsMSPro:1;
256-
u8 IsMSPHG:1;
257-
u8 Reserved1:1;
258-
u8 WtP:1;
259-
u8 Reserved2:1;
260-
};
261-
262-
struct SM_STATUS {
263-
u8 Insert:1;
264-
u8 Ready:1;
265-
u8 MediaChange:1;
266-
u8 Reserved:3;
267-
u8 WtP:1;
268-
u8 IsMS:1;
269-
};
240+
/* SD_STATUS bits */
241+
#define SD_Insert BIT(0)
242+
#define SD_Ready BIT(1)
243+
#define SD_MediaChange BIT(2)
244+
#define SD_IsMMC BIT(3)
245+
#define SD_HiCapacity BIT(4)
246+
#define SD_HiSpeed BIT(5)
247+
#define SD_WtP BIT(6)
248+
/* Bit 7 reserved */
249+
250+
/* MS_STATUS bits */
251+
#define MS_Insert BIT(0)
252+
#define MS_Ready BIT(1)
253+
#define MS_MediaChange BIT(2)
254+
#define MS_IsMSPro BIT(3)
255+
#define MS_IsMSPHG BIT(4)
256+
/* Bit 5 reserved */
257+
#define MS_WtP BIT(6)
258+
/* Bit 7 reserved */
259+
260+
/* SM_STATUS bits */
261+
#define SM_Insert BIT(0)
262+
#define SM_Ready BIT(1)
263+
#define SM_MediaChange BIT(2)
264+
/* Bits 3-5 reserved */
265+
#define SM_WtP BIT(6)
266+
#define SM_IsMS BIT(7)
270267

271268
struct ms_bootblock_cis {
272269
u8 bCistplDEVICE[6]; /* 0 */
@@ -437,9 +434,9 @@ struct ene_ub6250_info {
437434
u8 *bbuf;
438435

439436
/* for 6250 code */
440-
struct SD_STATUS SD_Status;
441-
struct MS_STATUS MS_Status;
442-
struct SM_STATUS SM_Status;
437+
u8 SD_Status;
438+
u8 MS_Status;
439+
u8 SM_Status;
443440

444441
/* ----- SD Control Data ---------------- */
445442
/*SD_REGISTER SD_Regs; */
@@ -602,7 +599,7 @@ static int sd_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
602599
{
603600
struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
604601

605-
if (info->SD_Status.Insert && info->SD_Status.Ready)
602+
if ((info->SD_Status & SD_Insert) && (info->SD_Status & SD_Ready))
606603
return USB_STOR_TRANSPORT_GOOD;
607604
else {
608605
ene_sd_init(us);
@@ -622,7 +619,7 @@ static int sd_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
622619
0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
623620
0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
624621

625-
if (info->SD_Status.WtP)
622+
if (info->SD_Status & SD_WtP)
626623
usb_stor_set_xfer_buf(mediaWP, 12, srb);
627624
else
628625
usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
@@ -641,9 +638,9 @@ static int sd_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
641638
struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
642639

643640
usb_stor_dbg(us, "sd_scsi_read_capacity\n");
644-
if (info->SD_Status.HiCapacity) {
641+
if (info->SD_Status & SD_HiCapacity) {
645642
bl_len = 0x200;
646-
if (info->SD_Status.IsMMC)
643+
if (info->SD_Status & SD_IsMMC)
647644
bl_num = info->HC_C_SIZE-1;
648645
else
649646
bl_num = (info->HC_C_SIZE + 1) * 1024 - 1;
@@ -693,7 +690,7 @@ static int sd_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
693690
return USB_STOR_TRANSPORT_ERROR;
694691
}
695692

696-
if (info->SD_Status.HiCapacity)
693+
if (info->SD_Status & SD_HiCapacity)
697694
bnByte = bn;
698695

699696
/* set up the command wrapper */
@@ -733,7 +730,7 @@ static int sd_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
733730
return USB_STOR_TRANSPORT_ERROR;
734731
}
735732

736-
if (info->SD_Status.HiCapacity)
733+
if (info->SD_Status & SD_HiCapacity)
737734
bnByte = bn;
738735

739736
/* set up the command wrapper */
@@ -1456,7 +1453,7 @@ static int ms_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
14561453
struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
14571454

14581455
/* pr_info("MS_SCSI_Test_Unit_Ready\n"); */
1459-
if (info->MS_Status.Insert && info->MS_Status.Ready) {
1456+
if ((info->MS_Status & MS_Insert) && (info->MS_Status & MS_Ready)) {
14601457
return USB_STOR_TRANSPORT_GOOD;
14611458
} else {
14621459
ene_ms_init(us);
@@ -1476,7 +1473,7 @@ static int ms_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
14761473
0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
14771474
0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
14781475

1479-
if (info->MS_Status.WtP)
1476+
if (info->MS_Status & MS_WtP)
14801477
usb_stor_set_xfer_buf(mediaWP, 12, srb);
14811478
else
14821479
usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
@@ -1495,7 +1492,7 @@ static int ms_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
14951492

14961493
usb_stor_dbg(us, "ms_scsi_read_capacity\n");
14971494
bl_len = 0x200;
1498-
if (info->MS_Status.IsMSPro)
1495+
if (info->MS_Status & MS_IsMSPro)
14991496
bl_num = info->MSP_TotalBlock - 1;
15001497
else
15011498
bl_num = info->MS_Lib.NumberOfLogBlock * info->MS_Lib.blockSize * 2 - 1;
@@ -1650,7 +1647,7 @@ static int ms_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
16501647
if (bn > info->bl_num)
16511648
return USB_STOR_TRANSPORT_ERROR;
16521649

1653-
if (info->MS_Status.IsMSPro) {
1650+
if (info->MS_Status & MS_IsMSPro) {
16541651
result = ene_load_bincode(us, MSP_RW_PATTERN);
16551652
if (result != USB_STOR_XFER_GOOD) {
16561653
usb_stor_dbg(us, "Load MPS RW pattern Fail !!\n");
@@ -1751,7 +1748,7 @@ static int ms_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
17511748
if (bn > info->bl_num)
17521749
return USB_STOR_TRANSPORT_ERROR;
17531750

1754-
if (info->MS_Status.IsMSPro) {
1751+
if (info->MS_Status & MS_IsMSPro) {
17551752
result = ene_load_bincode(us, MSP_RW_PATTERN);
17561753
if (result != USB_STOR_XFER_GOOD) {
17571754
pr_info("Load MSP RW pattern Fail !!\n");
@@ -1859,12 +1856,12 @@ static int ene_get_card_status(struct us_data *us, u8 *buf)
18591856

18601857
tmpreg = (u16) reg4b;
18611858
reg4b = *(u32 *)(&buf[0x14]);
1862-
if (info->SD_Status.HiCapacity && !info->SD_Status.IsMMC)
1859+
if ((info->SD_Status & SD_HiCapacity) && !(info->SD_Status & SD_IsMMC))
18631860
info->HC_C_SIZE = (reg4b >> 8) & 0x3fffff;
18641861

18651862
info->SD_C_SIZE = ((tmpreg & 0x03) << 10) | (u16)(reg4b >> 22);
18661863
info->SD_C_SIZE_MULT = (u8)(reg4b >> 7) & 0x07;
1867-
if (info->SD_Status.HiCapacity && info->SD_Status.IsMMC)
1864+
if ((info->SD_Status & SD_HiCapacity) && (info->SD_Status & SD_IsMMC))
18681865
info->HC_C_SIZE = *(u32 *)(&buf[0x100]);
18691866

18701867
if (info->SD_READ_BL_LEN > SD_BLOCK_LEN) {
@@ -2076,6 +2073,7 @@ static int ene_ms_init(struct us_data *us)
20762073
u16 MSP_BlockSize, MSP_UserAreaBlocks;
20772074
struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
20782075
u8 *bbuf = info->bbuf;
2076+
unsigned int s;
20792077

20802078
printk(KERN_INFO "transport --- ENE_MSInit\n");
20812079

@@ -2100,15 +2098,16 @@ static int ene_ms_init(struct us_data *us)
21002098
return USB_STOR_TRANSPORT_ERROR;
21012099
}
21022100
/* the same part to test ENE */
2103-
info->MS_Status = *(struct MS_STATUS *) bbuf;
2104-
2105-
if (info->MS_Status.Insert && info->MS_Status.Ready) {
2106-
printk(KERN_INFO "Insert = %x\n", info->MS_Status.Insert);
2107-
printk(KERN_INFO "Ready = %x\n", info->MS_Status.Ready);
2108-
printk(KERN_INFO "IsMSPro = %x\n", info->MS_Status.IsMSPro);
2109-
printk(KERN_INFO "IsMSPHG = %x\n", info->MS_Status.IsMSPHG);
2110-
printk(KERN_INFO "WtP= %x\n", info->MS_Status.WtP);
2111-
if (info->MS_Status.IsMSPro) {
2101+
info->MS_Status = bbuf[0];
2102+
2103+
s = info->MS_Status;
2104+
if ((s & MS_Insert) && (s & MS_Ready)) {
2105+
printk(KERN_INFO "Insert = %x\n", !!(s & MS_Insert));
2106+
printk(KERN_INFO "Ready = %x\n", !!(s & MS_Ready));
2107+
printk(KERN_INFO "IsMSPro = %x\n", !!(s & MS_IsMSPro));
2108+
printk(KERN_INFO "IsMSPHG = %x\n", !!(s & MS_IsMSPHG));
2109+
printk(KERN_INFO "WtP= %x\n", !!(s & MS_WtP));
2110+
if (s & MS_IsMSPro) {
21122111
MSP_BlockSize = (bbuf[6] << 8) | bbuf[7];
21132112
MSP_UserAreaBlocks = (bbuf[10] << 8) | bbuf[11];
21142113
info->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks;
@@ -2169,17 +2168,17 @@ static int ene_sd_init(struct us_data *us)
21692168
return USB_STOR_TRANSPORT_ERROR;
21702169
}
21712170

2172-
info->SD_Status = *(struct SD_STATUS *) bbuf;
2173-
if (info->SD_Status.Insert && info->SD_Status.Ready) {
2174-
struct SD_STATUS *s = &info->SD_Status;
2171+
info->SD_Status = bbuf[0];
2172+
if ((info->SD_Status & SD_Insert) && (info->SD_Status & SD_Ready)) {
2173+
unsigned int s = info->SD_Status;
21752174

21762175
ene_get_card_status(us, bbuf);
2177-
usb_stor_dbg(us, "Insert = %x\n", s->Insert);
2178-
usb_stor_dbg(us, "Ready = %x\n", s->Ready);
2179-
usb_stor_dbg(us, "IsMMC = %x\n", s->IsMMC);
2180-
usb_stor_dbg(us, "HiCapacity = %x\n", s->HiCapacity);
2181-
usb_stor_dbg(us, "HiSpeed = %x\n", s->HiSpeed);
2182-
usb_stor_dbg(us, "WtP = %x\n", s->WtP);
2176+
usb_stor_dbg(us, "Insert = %x\n", !!(s & SD_Insert));
2177+
usb_stor_dbg(us, "Ready = %x\n", !!(s & SD_Ready));
2178+
usb_stor_dbg(us, "IsMMC = %x\n", !!(s & SD_IsMMC));
2179+
usb_stor_dbg(us, "HiCapacity = %x\n", !!(s & SD_HiCapacity));
2180+
usb_stor_dbg(us, "HiSpeed = %x\n", !!(s & SD_HiSpeed));
2181+
usb_stor_dbg(us, "WtP = %x\n", !!(s & SD_WtP));
21832182
} else {
21842183
usb_stor_dbg(us, "SD Card Not Ready --- %x\n", bbuf[0]);
21852184
return USB_STOR_TRANSPORT_ERROR;
@@ -2201,14 +2200,14 @@ static int ene_init(struct us_data *us)
22012200

22022201
misc_reg03 = bbuf[0];
22032202
if (misc_reg03 & 0x01) {
2204-
if (!info->SD_Status.Ready) {
2203+
if (!(info->SD_Status & SD_Ready)) {
22052204
result = ene_sd_init(us);
22062205
if (result != USB_STOR_XFER_GOOD)
22072206
return USB_STOR_TRANSPORT_ERROR;
22082207
}
22092208
}
22102209
if (misc_reg03 & 0x02) {
2211-
if (!info->MS_Status.Ready) {
2210+
if (!(info->MS_Status & MS_Ready)) {
22122211
result = ene_ms_init(us);
22132212
if (result != USB_STOR_XFER_GOOD)
22142213
return USB_STOR_TRANSPORT_ERROR;
@@ -2307,14 +2306,14 @@ static int ene_transport(struct scsi_cmnd *srb, struct us_data *us)
23072306

23082307
/*US_DEBUG(usb_stor_show_command(us, srb)); */
23092308
scsi_set_resid(srb, 0);
2310-
if (unlikely(!(info->SD_Status.Ready || info->MS_Status.Ready)))
2309+
if (unlikely(!(info->SD_Status & SD_Ready) || (info->MS_Status & MS_Ready)))
23112310
result = ene_init(us);
23122311
if (result == USB_STOR_XFER_GOOD) {
23132312
result = USB_STOR_TRANSPORT_ERROR;
2314-
if (info->SD_Status.Ready)
2313+
if (info->SD_Status & SD_Ready)
23152314
result = sd_scsi_irp(us, srb);
23162315

2317-
if (info->MS_Status.Ready)
2316+
if (info->MS_Status & MS_Ready)
23182317
result = ms_scsi_irp(us, srb);
23192318
}
23202319
return result;
@@ -2378,7 +2377,6 @@ static int ene_ub6250_probe(struct usb_interface *intf,
23782377

23792378
static int ene_ub6250_resume(struct usb_interface *iface)
23802379
{
2381-
u8 tmp = 0;
23822380
struct us_data *us = usb_get_intfdata(iface);
23832381
struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
23842382

@@ -2390,17 +2388,16 @@ static int ene_ub6250_resume(struct usb_interface *iface)
23902388
mutex_unlock(&us->dev_mutex);
23912389

23922390
info->Power_IsResum = true;
2393-
/*info->SD_Status.Ready = 0; */
2394-
info->SD_Status = *(struct SD_STATUS *)&tmp;
2395-
info->MS_Status = *(struct MS_STATUS *)&tmp;
2396-
info->SM_Status = *(struct SM_STATUS *)&tmp;
2391+
/* info->SD_Status &= ~SD_Ready; */
2392+
info->SD_Status = 0;
2393+
info->MS_Status = 0;
2394+
info->SM_Status = 0;
23972395

23982396
return 0;
23992397
}
24002398

24012399
static int ene_ub6250_reset_resume(struct usb_interface *iface)
24022400
{
2403-
u8 tmp = 0;
24042401
struct us_data *us = usb_get_intfdata(iface);
24052402
struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
24062403

@@ -2412,10 +2409,10 @@ static int ene_ub6250_reset_resume(struct usb_interface *iface)
24122409
* the device
24132410
*/
24142411
info->Power_IsResum = true;
2415-
/*info->SD_Status.Ready = 0; */
2416-
info->SD_Status = *(struct SD_STATUS *)&tmp;
2417-
info->MS_Status = *(struct MS_STATUS *)&tmp;
2418-
info->SM_Status = *(struct SM_STATUS *)&tmp;
2412+
/* info->SD_Status &= ~SD_Ready; */
2413+
info->SD_Status = 0;
2414+
info->MS_Status = 0;
2415+
info->SM_Status = 0;
24192416

24202417
return 0;
24212418
}

0 commit comments

Comments
 (0)