Skip to contentSkip to conversation

vsprintf: Make sure argument to %*ph/%pv is valid

PATCH Superseded Boris Ostrovsky 3 participants 4 messages Feb 10, 2015 – Feb 11, 2015

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vsprintf: Make sure argument to %*ph/%pv is valid

If NULL pointer is passed for these specifiers then print '-' or 'd-v-'.

---
 xen/common/vsprintf.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
xen/common/vsprintf.c +19 -0
index 065cc42..84cfa85 100644
@@ -280,6 +280,13 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
         /* Consumed 'h' from the format string. */
         ++*fmt_ptr;
 
+        if ( hex_buffer == NULL )
+        {
+            if ( str < end )
+                *str++ = '-';
+            return str;
+        }
+
         /* Bound user count from %* to between 0 and 64 bytes. */
         if ( field_width <= 0 )
             return str;
@@ -338,6 +345,18 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
         ++*fmt_ptr;
         if ( str < end )
             *str = 'd';
+        if ( v == NULL )
+        {
+            /* If v is NULL then print 'd-v-' */
+            str++;
+            if ( str < end )
+                *str++ = '-';
+            if ( str < end )
+                *str++ = 'v';
+            if ( str < end )
+                *str++ = '-';
+            return str;
+        }
         str = number(str + 1, end, v->domain->domain_id, 10, -1, -1, 0);
         if ( str < end )
             *str = 'v';
Signature · 1 line
1.7.1
#1 lore raw
Quoted 4 lines · Boris Ostrovsky

If NULL pointer is passed for these specifiers then print '-' or 'd-v-'.

Instead of special casing each custom format, I would be tempted just to put the switch() in an if(arg) conditional, and fall back to the regular number for a NULL pointer, or go along the glibc route and print "(NULL)".

I presume you found this by falling over a NULL pointer while debugging? I can't see a legitimate reason for formally supporting NULL pointers in each context, but crashing is certainly better avoided.

~Andrew

Quoted 41 lines
---
 xen/common/vsprintf.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
xen/common/vsprintf.c +19 -0
index 065cc42..84cfa85 100644
@@ -280,6 +280,13 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
         /* Consumed 'h' from the format string. */
         ++*fmt_ptr;
 
+        if ( hex_buffer == NULL )
+        {
+            if ( str < end )
+                *str++ = '-';
+            return str;
+        }
+
         /* Bound user count from %* to between 0 and 64 bytes. */
         if ( field_width <= 0 )
             return str;
@@ -338,6 +345,18 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
         ++*fmt_ptr;
         if ( str < end )
             *str = 'd';
+        if ( v == NULL )
+        {
+            /* If v is NULL then print 'd-v-' */
+            str++;
+            if ( str < end )
+                *str++ = '-';
+            if ( str < end )
+                *str++ = 'v';
+            if ( str < end )
+                *str++ = '-';
+            return str;
+        }
         str = number(str + 1, end, v->domain->domain_id, 10, -1, -1, 0);
         if ( str < end )
             *str = 'v';
#2 lore raw
Quoted 8 lines · Andrew Cooper
Quoted 4 lines · Boris Ostrovsky

If NULL pointer is passed for these specifiers then print '-' or 'd-v-'.

Instead of special casing each custom format, I would be tempted just to put the switch() in an if(arg) conditional, and fall back to the regular number for a NULL pointer, or go along the glibc route and print "(NULL)".

OK, I could do that.

Quoted 4 lines

I presume you found this by falling over a NULL pointer while debugging? I can't see a legitimate reason for formally supporting NULL pointers in each context, but crashing is certainly better avoided.

Yes, I hit %pv case when the vcpu I was tracking turned NULL. And yes, we then crash.

-boris

Quoted 44 lines

~Andrew

Quoted 41 lines
---
  xen/common/vsprintf.c |   19 +++++++++++++++++++
  1 files changed, 19 insertions(+), 0 deletions(-)
xen/common/vsprintf.c +19 -0
index 065cc42..84cfa85 100644
@@ -280,6 +280,13 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
          /* Consumed 'h' from the format string. */
          ++*fmt_ptr;
 
+        if ( hex_buffer == NULL )
+        {
+            if ( str < end )
+                *str++ = '-';
+            return str;
+        }
+
          /* Bound user count from %* to between 0 and 64 bytes. */
          if ( field_width <= 0 )
              return str;
@@ -338,6 +345,18 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
          ++*fmt_ptr;
          if ( str < end )
              *str = 'd';
+        if ( v == NULL )
+        {
+            /* If v is NULL then print 'd-v-' */
+            str++;
+            if ( str < end )
+                *str++ = '-';
+            if ( str < end )
+                *str++ = 'v';
+            if ( str < end )
+                *str++ = '-';
+            return str;
+        }
          str = number(str + 1, end, v->domain->domain_id, 10, -1, -1, 0);
          if ( str < end )
              *str = 'v';
#3 lore raw
In reply to Andrew Cooper
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 10.02.15 at 22:49, <[email protected]> wrote:
Quoted 4 lines · Boris Ostrovsky

If NULL pointer is passed for these specifiers then print '-' or 'd-v-'.

Quoted 7 lines

Instead of special casing each custom format, I would be tempted just to put the switch() in an if(arg) conditional, and fall back to the regular number for a NULL pointer, or go along the glibc route and print "(NULL)".

I presume you found this by falling over a NULL pointer while debugging? I can't see a legitimate reason for formally supporting NULL pointers in each context, but crashing is certainly better avoided.

I tend to disagree - lots of functions get passed pointers that they dereference _assuming_ they're not NULL (or otherwise invalid). vsprintf() doesn't need to be a special case - it's the caller's responsibility to ensure it doesn't pass NULL in such cases.

That said, I wouldn't object to a patch doing what is outlined in the first paragraph of Andrew's reply above, but extended to cover pointing anywhere into known bad ranges (i.e. at least for x86 in particular anywhere below HYPERVISOR_VIRT_START).

Jan