Skip to content

Commit fd2d17b

Browse files
xairya-nogikh
authored andcommitted
sys/linux: patch in hardcoded USB IDs for USB printer driver
Some USB drivers contain quirks (special handling code) for USB devices with specific USB IDs. Sometimes the IDs for these quirks are encoded in the driver matching rules (and thus are auto-extracted into sys/linux/init_vusb_ids.go), but sometimes these IDs are hardcoded in the driver itself. This patch extends the generateUsbPrinterDeviceDescriptor function to also sometimes generate USB IDs to exercise the hardcoded quirks for the USB printer class. Similar functionality can be implemented for other USB drivers later.
1 parent a083215 commit fd2d17b

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

sys/linux/init_vusb.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,34 @@ func (arch *arch) generateUsbPrinterDeviceDescriptor(g *prog.Gen, typ0 prog.Type
7979
return
8080
}
8181

82-
// syzlang descriptions already contain passable IDs.
83-
// Roll the dice to decide if we want to patch them.
84-
if g.Rand().Intn(2) == 0 {
82+
// Roll the dice to decide if and how we want to patch printer USB IDs.
83+
switch {
84+
case g.Rand().Intn(3) == 0:
85+
// Syzlang descriptions already contain passable IDs, leave them as is.
8586
return
86-
}
87-
88-
// Patch in IDs specific to the USB printer class.
89-
// Only patch IDs that are used in the driver matching rules.
90-
if ids, ok := usbIds["usblp"]; ok {
91-
patchUsbDeviceID(g, &arg, calls, ids, false)
87+
case g.Rand().Intn(2) == 0:
88+
// Patch in quirk IDs that are hardcoded in the USB printer class driver
89+
// (and thus are not auto-extractable) to allow exercising driver quirks;
90+
// see quirk_printers in drivers/usb/class/usblp.c.
91+
var idVendor int16
92+
var idProduct int16
93+
if g.Rand().Intn(2) == 0 { // USBLP_QUIRK_BIDIR
94+
idVendor = 0x03f0
95+
idProduct = 0x0004
96+
} else { // USBLP_QUIRK_BAD_CLASS
97+
idVendor = 0x04b8
98+
idProduct = 0x0202
99+
}
100+
devArg := arg.(*prog.GroupArg).Inner[0]
101+
patchGroupArg(devArg, 7, "idVendor", uint64(idVendor))
102+
patchGroupArg(devArg, 8, "idProduct", uint64(idProduct))
103+
default:
104+
// Patch in IDs auto-extracted from the matching rules for the USB printer class.
105+
// Do not patch IDs that are not used in the matching rules to avoid subverting
106+
// the kernel into matching the device to a different driver.
107+
if ids, ok := usbIds["usblp"]; ok {
108+
patchUsbDeviceID(g, &arg, calls, ids, false)
109+
}
92110
}
93111

94112
return

0 commit comments

Comments
 (0)