Skip to content
This repository was archived by the owner on Mar 10, 2026. It is now read-only.
This repository was archived by the owner on Mar 10, 2026. It is now read-only.

USB backend crashes from time to time #3978

Closed

Description

@michaelrsweet

Version: 1.5.0
CUPS.org User: till.kamppeter

See

https://bugs.launchpad.net/ubuntu/+source/cups/+bug/887094

a Ubuntu user complains that the "usb" CUPS backend is crashing from time to time when he is printing and so some jobs do not get printed.

With the help of his core dump I could find out that if the reading of the device ID from the device fails in a certain way, having the IOCTL function not reporting an error but returning invalid data, the USB backend crashes. This happens if the reported length of the ID string (first two bytes) is zero or at least very small. I have created a patch which makes failure reported then instead of trying to convert the read ID string into a C string.

The patch is attached and the original bug reporter tells that the patch fixes the problem.

Activity

  1. michaelrsweet commented on Nov 11, 2011

    @michaelrsweet
    ContributorAuthor

    CUPS.org User: till.kamppeter

    I have applied this patch and the patch of STR #3965 to the package in Ubuntu Oneiric now and asked the people suffering the following Ubuntu bugs for testing:

    https://bugs.launchpad.net/ubuntu/+source/cups/+bug/887094
    usb printer backend crashes

    https://bugs.launchpad.net/ubuntu/+source/cups/+bug/872483
    laser printer only prints first job correct

    https://bugs.launchpad.net/ubuntu/+source/cups/+bug/883169
    Brother MFC-8840D usb printer not detected

    https://bugs.launchpad.net/ubuntu/+source/cups/+bug/793244
    After suspend/hibernate usb printer backend repeatedly crashes

    https://bugs.launchpad.net/ubuntu/+source/cups/+bug/564917
    Ubuntu 10.04 does not recognize local printer connected via
    USB/parallel adapter cable

    For the first three I got answers and they are definitely fixed by the two patches. On the other two I am still waiting for an answer but they are most probably also fixed.

    Together with the USB fix on the Linux kernel (STR #3884) it looks like that all known problems of libusb-based USB printing (after deprecation of the "usblp" kernel module) are solved together with these two patches on the libusb-based "usb" CUPS backend. So I highly recommend the inclusion of the two patches in the upstream repository of CUPS, for both CUPS 1.5.x and 1.6.x.

  2. michaelrsweet commented on Jan 27, 2012

    @michaelrsweet
    ContributorAuthor

    CUPS.org User: mike

    Fixed in Subversion repository.

  3. michaelrsweet commented on Jan 27, 2012

    @michaelrsweet
    ContributorAuthor

    "usb-backend-gracefully-fail-on-more-cases-of-bad-device-id.patch":

    --- a/backend/usb-libusb.c
    +++ b/backend/usb-libusb.c
    @@ -430,15 +430,26 @@

    • Check to see if the length is larger than our buffer; first
    • assume that the vendor incorrectly implemented the 1284 spec,
    • and then limit the length to the size of our buffer...
      • * Consider a length < 14 as too short, as the minimum valid device
      • * ID ("MFG:x;MDL:y;") is 12 bytes long and so we have at least 14
      • * bytes with the two length bytes...
      • * Especially the length in the memmove() call cannot get negative then,
      • * causing the backend to segfault.
        */
    • if (length > bufsize)
    • if ((length > bufsize) || (length < 14))
      length = (((unsigned)buffer[1] & 255) << 8) +
      ((unsigned)buffer[0] & 255);

    if (length > bufsize)
    length = bufsize;

    • if (length < 14)
    • {
    • *buffer = '\0';
    • return (-1);
    • }

    length -= 2;

    /*

  4. michaelrsweet commented on Jan 27, 2012

    @michaelrsweet
    ContributorAuthor

    "str3965+3978.patch":

    Index: backend/usb-libusb.c

    --- backend/usb-libusb.c (revision 10195)
    +++ backend/usb-libusb.c (working copy)
    @@ -3,7 +3,7 @@
    *

    • Libusb interface code for CUPS.
    • * Copyright 2007-2011 by Apple Inc.
    • * Copyright 2007-2012 by Apple Inc.
      *
    • These coded instructions, statements, and computer programs are the
    • property of Apple Inc. and are protected by Federal copyright
      @@ -423,22 +423,34 @@
    • bytes. The 1284 spec says the length is stored MSB first...
      */
    • length = (((unsigned)buffer[0] & 255) << 8) +
    • length = (((unsigned)buffer[0] & 255) << 8) |
      ((unsigned)buffer[1] & 255);

    /*

    • * Check to see if the length is larger than our buffer; first
    • * assume that the vendor incorrectly implemented the 1284 spec,
    • * and then limit the length to the size of our buffer...
    • * Check to see if the length is larger than our buffer or less than 14 bytes
    • * (the minimum valid device ID is "MFG:x;MDL:y;" with 2 bytes for the length).
    • * If the length is out-of-range, assume that the vendor incorrectly
      • implemented the 1284 spec and re-read the length as LSB first,..
        */
    • if (length > bufsize)
    • length = (((unsigned)buffer[1] & 255) << 8) +
    • if (length > bufsize || length < 14)
    • length = (((unsigned)buffer[1] & 255) << 8) |
      ((unsigned)buffer[0] & 255);

    if (length > bufsize)
    length = bufsize;

    • if (length < 14)
    • {
    • /*
    • * Invalid device ID, clear it!
    • */
    • *buffer = '\0';
    • return (-1);
    • }

    length -= 2;

    /*
    @@ -631,6 +643,7 @@
    int verbose) /* I - Update connecting-to-device state? /
    {
    int number; /
    Configuration/interface/altset numbers */

    • char current; /* Current configuration */

    /*
    @@ -647,27 +660,37 @@
    if ((printer->handle = usb_open(printer->device)) == NULL)
    return (-1);

    • if (verbose)
    • fputs("STATE: +connecting-to-device\n", stderr);

    /*

    • * Then set the desired configuration...
    • * Set the desired configuration, but only if it needs changing. Some
    • * printers (e.g., Samsung) don't like usb_set_configuration. It will succeed,
      • but the following print job is sometimes silently lost by the printer.
        */
    • if (verbose)
    • fputs("STATE: +connecting-to-device\n", stderr);
    • if (usb_control_msg(printer->handle,
    •                  USB_TYPE_STANDARD | USB_ENDPOINT_IN | USB_RECIP_DEVICE,
      
    •                  8, /\* GET_CONFIGURATION */
      
    •                  0, 0, &current, 1, 5000) != 1)
      
    • current = 0; /* Assume not configured */

    number = printer->device->config[printer->conf].bConfigurationValue;

    • if (usb_set_configuration(printer->handle, number) < 0)
    • if (number != current)
      {
    • /*
    • * If the set fails, chances are that the printer only supports a
    • * single configuration. Technically these printers don't conform to
    • * the USB printer specification, but otherwise they'll work...
    • */
    • if (usb_set_configuration(printer->handle, number) < 0)
    • {
    • /*
      
    •  \* If the set fails, chances are that the printer only supports a
      
    •  \* single configuration.  Technically these printers don't conform to
      
    •  \* the USB printer specification, but otherwise they'll work...
      
    •  */
      
    • if (errno != EBUSY)
    •  fprintf(stderr, "DEBUG: Failed to set configuration %d for %04x:%04x\n",
      
    •          number, printer->device->descriptor.idVendor,
      
    •     printer->device->descriptor.idProduct);
      
    •  if (errno != EBUSY)
      
    •    fprintf(stderr, "DEBUG: Failed to set configuration %d for %04x:%04x\n",
      
    •            number, printer->device->descriptor.idVendor,
      
    •       printer->device->descriptor.idProduct);
      
    • }
      }

    /*
    @@ -679,41 +702,35 @@
    while (usb_claim_interface(printer->handle, number) < 0)
    {
    if (errno != EBUSY)

    •  fprintf(stderr, "DEBUG: Failed to claim interface %d for %04x:%04x: %s\n",
      
    •  fprintf(stderr,
      
    •          "DEBUG: Failed to claim interface %d for %04x:%04x: %s\n",
             number, printer->device->descriptor.idVendor,
        printer->device->descriptor.idProduct, strerror(errno));
      

      goto error;
      }

    -#if 0 /* STR #3801: Claiming interface 0 causes problems with some printers */

    • if (number != 0)
    • while (usb_claim_interface(printer->handle, 0) < 0)
    • /*
    • * Set alternate setting, but only if there is more than one option. Some
    • * printers (e.g., Samsung) don't like usb_set_altinterface.
    • */
    • if (printer->device->config[printer->conf].interface[printer->iface].

    •      num_altsetting > 1)
      
    • {

    • number = printer->device->config[printer->conf].interface[printer->iface].

    •             altsetting[printer->altset].bAlternateSetting;
      
    • while (usb_set_altinterface(printer->handle, number) < 0)
      {
      if (errno != EBUSY)

    • fprintf(stderr, "DEBUG: Failed to claim interface 0 for %04x:%04x: %s\n",

    •   printer->device->descriptor.idVendor,
      
    •   printer->device->descriptor.idProduct, strerror(errno));
      
    •    fprintf(stderr,
      
    •            "DEBUG: Failed to set alternate interface %d for %04x:%04x: "
      
    •            "%s\n", number, printer->device->descriptor.idVendor,
      
    •       printer->device->descriptor.idProduct, strerror(errno));
      

      goto error;
      }

      -#endif /* 0 */

    • /*

    • * Set alternate setting...

    - */

    • number = printer->device->config[printer->conf].interface[printer->iface].
    •           altsetting[printer->altset].bAlternateSetting;
      
    • while (usb_set_altinterface(printer->handle, number) < 0)
    • {
    • if (errno != EBUSY)
    •  fprintf(stderr,
      
    •          "DEBUG: Failed to set alternate interface %d for %04x:%04x: %s\n",
      
    •          number, printer->device->descriptor.idVendor,
      

    - printer->device->descriptor.idProduct, strerror(errno));

    • goto error;
      }

    if (verbose)

  5. added this to the Stable milestone on Mar 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions