Instead of setting the configuration in the *match() routine let the
stack do it for us.  This operation can fail, so it's better to not
attach any driver if something bad happens.

This change is similar to what I did with the rest of the drivers some
years ago when I fuzzed the stack.

I'm looking for tests and oks.


Index: ubsa.c
===================================================================
RCS file: /cvs/src/sys/dev/usb/ubsa.c,v
retrieving revision 1.66
diff -u -p -r1.66 ubsa.c
--- ubsa.c      27 Apr 2018 08:08:06 -0000      1.66
+++ ubsa.c      18 Nov 2018 17:01:50 -0000
@@ -86,9 +86,6 @@ int   ubsadebug = 0;
 
 #define        UBSA_MODVER             1       /* module version */
 
-#define        UBSA_CONFIG_INDEX       1
-#define        UBSA_IFACE_INDEX        0
-
 #define        UBSA_INTR_INTERVAL      100     /* ms */
 
 #define        UBSA_SET_BAUDRATE       0x00
@@ -225,7 +222,7 @@ ubsa_match(struct device *parent, void *
 {
        struct usb_attach_arg *uaa = aux;
 
-       if (uaa->iface != NULL)
+       if (uaa->iface == NULL || uaa->configno != 1)
                return (UMATCH_NONE);
 
        return (usb_lookup(ubsa_devs, uaa->vendor, uaa->product) != NULL ?
@@ -237,16 +234,15 @@ ubsa_attach(struct device *parent, struc
 {
        struct ubsa_softc *sc = (struct ubsa_softc *)self;
        struct usb_attach_arg *uaa = aux;
-       struct usbd_device *dev = uaa->device;
        usb_config_descriptor_t *cdesc;
        usb_interface_descriptor_t *id;
        usb_endpoint_descriptor_t *ed;
        const char *devname = sc->sc_dev.dv_xname;
-       usbd_status err;
        struct ucom_attach_args uca;
        int i;
 
-       sc->sc_udev = dev;
+       sc->sc_udev = uaa->device;
+       sc->sc_iface = uaa->iface;
 
        /*
         * initialize rts, dtr variables to something
@@ -262,15 +258,6 @@ ubsa_attach(struct device *parent, struc
        sc->sc_intr_number = -1;
        sc->sc_intr_pipe = NULL;
 
-       /* Move the device into the configured state. */
-       err = usbd_set_config_index(dev, UBSA_CONFIG_INDEX, 1);
-       if (err) {
-               printf("%s: failed to set configuration: %s\n",
-                   devname, usbd_errstr(err));
-               usbd_deactivate(sc->sc_udev);
-               goto error;
-       }
-
        /* get the config descriptor */
        cdesc = usbd_get_config_descriptor(sc->sc_udev);
 
@@ -281,16 +268,6 @@ ubsa_attach(struct device *parent, struc
                goto error;
        }
 
-       /* get the first interface */
-       err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX,
-           &sc->sc_iface);
-       if (err) {
-               printf("%s: failed to get interface: %s\n",
-                       devname, usbd_errstr(err));
-               usbd_deactivate(sc->sc_udev);
-               goto error;
-       }
-
        /* Find the endpoints */
 
        id = usbd_get_interface_descriptor(sc->sc_iface);
@@ -342,7 +319,7 @@ ubsa_attach(struct device *parent, struc
        /* bulkin, bulkout set above */
        uca.ibufsizepad = uca.ibufsize;
        uca.opkthdrlen = 0;
-       uca.device = dev;
+       uca.device = sc->sc_udev;
        uca.iface = sc->sc_iface;
        uca.methods = &ubsa_methods;
        uca.arg = sc;

Reply via email to