Skip to contentSkip to conversation

cups-1.7.0: stringpool corruption fix

PATCH In review Tim Waugh 2 participants 3 messages Nov 1, 2013 – Nov 5, 2013

lore
Share & tools
lore

Tim Waugh

author
#0 lore raw

PATCH 1/1 cups-1.7.0: stringpool corruption fix

There is a long-standing stringpool corruption issue in printers.c.

Attribute values are stored in the global stringpool (implemented in cups/string.c), which is reference-counted storage for strings. Strings are stored in a sorted, unique array (cups_array_t, implemented in cups/array.c).

If the array is found to be unsorted, or a duplicate is found, the underlying cups_array_t object switches into "non-unique" mode and stores new strings by adding to the pool rather than incrementing the reference count for an existing string (see the cups_array_add() function).

The accessor functions for the stringpool return "char*" types, but these are strings which must not be modified -- doing so will trigger the behaviour described above.

In scheduler/printers.c, there are a couple of places which modify attribute values in-place. Attached is a patch to fix them.

Tim.
*/

scheduler/printers.c renamed from scheduler/printers.c.stringpool-corruption +22 -5
diff -up cups-1.7.0/scheduler/printers.c.stringpool-corruption cups-1.7.0/scheduler/printers.c
@@ -1871,6 +1871,7 @@ cupsdSetPrinterAttr(
   ipp_attribute_t	*attr;		/* Attribute */
   int			i,		/* Looping var */
 			count;		/* Number of values */
+  char			*value_dup;	/* Copy of attribute value string */
   char			*ptr,		/* Pointer into value */
 			*start,		/* Start of value */
 			quote;		/* Quote character */
@@ -1939,16 +1940,24 @@ cupsdSetPrinterAttr(
       return;
     }
 
-    for (i = 0; i < count; i ++)
+    if ((value_dup = strdup(value)) == NULL)
     {
-      if ((ptr = strchr(value, ',')) != NULL)
+      cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to copy attribute value.");
+      return;
+    }
+
+    for (i = 0, start = value_dup; i < count; i ++)
+    {
+      if ((ptr = strchr(start, ',')) != NULL)
         *ptr++ = '\0';
 
-      attr->values[i].integer = strtol(value, NULL, 10);
+      attr->values[i].integer = strtol(start, NULL, 10);
 
       if (ptr)
-        value = ptr;
+        start = ptr;
     }
+
+    free(value_dup);
   }
   else
   {
@@ -1989,7 +1998,13 @@ cupsdSetPrinterAttr(
       return;
     }
 
-    for (i = 0, quote = '\0', ptr = value; i < count; i ++)
+    if ((value_dup = strdup(value)) == NULL)
+    {
+      cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to copy attribute value.");
+      return;
+    }
+
+    for (i = 0, quote = '\0', ptr = value_dup; i < count; i ++)
     {
       for (start = ptr; *ptr; ptr ++)
       {
@@ -2017,6 +2032,8 @@ cupsdSetPrinterAttr(
 
       attr->values[i].string.text = _cupsStrAlloc(start);
     }
+
+    free(value_dup);
   }
 }
 
#1 lore raw

Tim,

Thanks, looking into this now and have filed this as:

    <rdar://problem/15382819> cups.org: String pool corruption bug
Quoted 20 lines · Tim Waugh

There is a long-standing stringpool corruption issue in printers.c.

Attribute values are stored in the global stringpool (implemented in cups/string.c), which is reference-counted storage for strings. Strings are stored in a sorted, unique array (cups_array_t, implemented in cups/array.c).

If the array is found to be unsorted, or a duplicate is found, the underlying cups_array_t object switches into "non-unique" mode and stores new strings by adding to the pool rather than incrementing the reference count for an existing string (see the cups_array_add() function).

The accessor functions for the stringpool return "char*" types, but these are strings which must not be modified -- doing so will trigger the behaviour described above.

In scheduler/printers.c, there are a couple of places which modify attribute values in-place. Attached is a patch to fix them.

Quoted 7 lines

Tim.
*/

<cups-stringpool-corruption.patch>_______________________________________________ Printing-architecture mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/printing-architecture

____________________________________________________________
Michael Sweet, Senior Printing System Engineer, PWG Chair

#2 lore raw
In reply to Tim Waugh

Tim,

Updated patch attached; aside from adding the local string copy (which I’ve made outside the int/string check), I also made the “value” parameter const since we definitely don’t want to be fiddling with that string.

Thanks!

printers.c +22 -8
Index: scheduler/printers.c
===================================================================
@@ -1870,12 +1831,13 @@
 cupsdSetPrinterAttr(
     cupsd_printer_t *p,			/* I - Printer */
     const char      *name,		/* I - Attribute name */
-    char            *value)		/* I - Attribute value string */
+    const char      *value)		/* I - Attribute value string */
 {
   ipp_attribute_t	*attr;		/* Attribute */
   int			i,		/* Looping var */
 			count;		/* Number of values */
-  char			*ptr,		/* Pointer into value */
+  char			*temp,		/* Temporary copy of value string */
+			*ptr,		/* Pointer into value */
 			*start,		/* Start of value */
 			quote;		/* Quote character */
   ipp_tag_t		value_tag;	/* Value tag for this attribute */
@@ -1892,10 +1854,21 @@
   }
 
  /*
+  * Copy the value string so we can do what we want with it...
+  */
+
+  if ((temp = strdup(value)) == NULL)
+  {
+    cupsdLogMessage(CUPSD_LOG_ERROR,
+                    "Unable to duplicate value for \"%s\" attribute.", name);
+    return;
+  }
+
+ /*
   * Count the number of values...
   */
 
-  for (count = 1, quote = '\0', ptr = value;
+  for (count = 1, quote = '\0', ptr = temp;
        *ptr;
        ptr ++)
   {
@@ -1943,15 +1916,15 @@
       return;
     }
 
-    for (i = 0; i < count; i ++)
+    for (i = 0, start = temp; i < count; i ++)
     {
-      if ((ptr = strchr(value, ',')) != NULL)
+      if ((ptr = strchr(start, ',')) != NULL)
         *ptr++ = '\0';
 
-      attr->values[i].integer = strtol(value, NULL, 10);
+      attr->values[i].integer = strtol(start, NULL, 10);
 
       if (ptr)
-        value = ptr;
+        start = ptr;
     }
   }
   else
@@ -1993,7 +1966,7 @@
       return;
     }
 
-    for (i = 0, quote = '\0', ptr = value; i < count; i ++)
+    for (i = 0, quote = '\0', ptr = temp; i < count; i ++)
     {
       for (start = ptr; *ptr; ptr ++)
       {
@@ -2022,6 +1995,8 @@
       attr->values[i].string.text = _cupsStrAlloc(start);
     }
   }
+
+  free(temp);
 }
 
 
printers.h +2 -1
Index: scheduler/printers.h
===================================================================
@@ -166,7 +166,8 @@
 						 ipp_attribute_t *attr);
 extern void		cupsdSetDeviceURI(cupsd_printer_t *p, const char *uri);
 extern void		cupsdSetPrinterAttr(cupsd_printer_t *p,
-			                    const char *name, char *value);
+			                    const char *name,
+			                    const char *value);
 extern void		cupsdSetPrinterAttrs(cupsd_printer_t *p);
 extern int		cupsdSetPrinterReasons(cupsd_printer_t *p,
 			                       const char *s);
Quoted 20 lines · Tim Waugh

There is a long-standing stringpool corruption issue in printers.c.

Attribute values are stored in the global stringpool (implemented in cups/string.c), which is reference-counted storage for strings. Strings are stored in a sorted, unique array (cups_array_t, implemented in cups/array.c).

If the array is found to be unsorted, or a duplicate is found, the underlying cups_array_t object switches into "non-unique" mode and stores new strings by adding to the pool rather than incrementing the reference count for an existing string (see the cups_array_add() function).

The accessor functions for the stringpool return "char*" types, but these are strings which must not be modified -- doing so will trigger the behaviour described above.

In scheduler/printers.c, there are a couple of places which modify attribute values in-place. Attached is a patch to fix them.

Quoted 7 lines

Tim.
*/

<cups-stringpool-corruption.patch>_______________________________________________ Printing-architecture mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/printing-architecture

__________________________________________________
Michael Sweet, Senior Printing System Engineer, PWG Chair