Skip to contentSkip to conversation

vsprintf: Make sure argument to %pX specifier is valid

PATCH Superseded Boris Ostrovsky 2 participants 9 messages Feb 11, 2015 – Feb 12, 2015

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 vsprintf: Make sure argument to %pX specifier is valid

If invalid pointer (i.e. something smaller than HYPERVISOR_VIRT_START) is passed for %*ph/%pv/%ps/%pS format specifiers then print "(NULL)"

---
 xen/common/vsprintf.c |   23 ++++++++++++++++-------
 1 files changed, 16 insertions(+), 7 deletions(-)

v2:
* Print "(NULL)" instead of specifier-specific string
* Consider all addresses under HYPERVISOR_VIRT_START as invalid. (I think this is true for both x86 and ARM but I don't have ARM platform to test).

xen/common/vsprintf.c +16 -7
index 065cc42..b9542b5 100644
@@ -270,6 +270,22 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
     const char *fmt = *fmt_ptr, *s;
 
     /* Custom %p suffixes. See XEN_ROOT/docs/misc/printk-formats.txt */
+
+    switch ( fmt[1] )
+    {
+        case 'h':
+        case 's':
+        case 'S':
+        case 'v':
+            ++*fmt_ptr;
+    }
+
+    if ( (unsigned long)arg < HYPERVISOR_VIRT_START )
+    {
+        char *s = "(NULL)";
+        return string(str, end, s, -1, -1, 0);
+    }
+
     switch ( fmt[1] )
     {
     case 'h': /* Raw buffer as hex string. */
@@ -277,9 +293,6 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
         const uint8_t *hex_buffer = arg;
         unsigned int i;
 
-        /* Consumed 'h' from the format string. */
-        ++*fmt_ptr;
-
         /* Bound user count from %* to between 0 and 64 bytes. */
         if ( field_width <= 0 )
             return str;
@@ -306,9 +319,6 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
         unsigned long sym_size, sym_offset;
         char namebuf[KSYM_NAME_LEN+1];
 
-        /* Advance parents fmt string, as we have consumed 's' or 'S' */
-        ++*fmt_ptr;
-
         s = symbols_lookup((unsigned long)arg, &sym_size, &sym_offset, namebuf);
 
         /* If the symbol is not found, fall back to printing the address */
@@ -335,7 +345,6 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
     {
         const struct vcpu *v = arg;
 
-        ++*fmt_ptr;
         if ( str < end )
             *str = 'd';
         str = number(str + 1, end, v->domain->domain_id, 10, -1, -1, 0);
Signature · 1 line
1.7.1
#1 lore raw
Quoted 29 lines · Boris Ostrovsky

If invalid pointer (i.e. something smaller than HYPERVISOR_VIRT_START) is passed for %*ph/%pv/%ps/%pS format specifiers then print "(NULL)"

---
 xen/common/vsprintf.c |   23 ++++++++++++++++-------
 1 files changed, 16 insertions(+), 7 deletions(-)

v2:
* Print "(NULL)" instead of specifier-specific string
* Consider all addresses under HYPERVISOR_VIRT_START as invalid. (I think this is true for both x86 and ARM but I don't have ARM platform to test).

xen/common/vsprintf.c +7 -0
index 065cc42..b9542b5 100644
@@ -270,6 +270,22 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
     const char *fmt = *fmt_ptr, *s;
 
     /* Custom %p suffixes. See XEN_ROOT/docs/misc/printk-formats.txt */
+
+    switch ( fmt[1] )
+    {
+        case 'h':
+        case 's':
+        case 'S':
+        case 'v':

xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 289

Quoted 10 lines
xen/common/vsprintf.c +9 -0
+            ++*fmt_ptr;
+    }
+
+    if ( (unsigned long)arg < HYPERVISOR_VIRT_START )
+    {
+        char *s = "(NULL)";
+        return string(str, end, s, -1, -1, 0);
+    }
+
     switch ( fmt[1] )

This wont function, as you have inverted the increment of *fmt_ptr and check of fmt[1].

"(NULL)" is inappropriate for non-null pointers less than VIRT_START.

Given the VIRT check, I would just put the entire switch statement inside an "if ( (unsigned long)arg < HYPERVISOR_VIRT_START )" block and let it fall through to the plain number case for a bogus pointer.

~Andrew

Quoted 30 lines
xen/common/vsprintf.c +0 -7
     {
     case 'h': /* Raw buffer as hex string. */
@@ -277,9 +293,6 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
         const uint8_t *hex_buffer = arg;
         unsigned int i;
 
-        /* Consumed 'h' from the format string. */
-        ++*fmt_ptr;
-
         /* Bound user count from %* to between 0 and 64 bytes. */
         if ( field_width <= 0 )
             return str;
@@ -306,9 +319,6 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
         unsigned long sym_size, sym_offset;
         char namebuf[KSYM_NAME_LEN+1];
 
-        /* Advance parents fmt string, as we have consumed 's' or 'S' */
-        ++*fmt_ptr;
-
         s = symbols_lookup((unsigned long)arg, &sym_size, &sym_offset, namebuf);
 
         /* If the symbol is not found, fall back to printing the address */
@@ -335,7 +345,6 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
     {
         const struct vcpu *v = arg;
 
-        ++*fmt_ptr;
         if ( str < end )
             *str = 'd';
         str = number(str + 1, end, v->domain->domain_id, 10, -1, -1, 0);
#2 lore raw
Quoted 34 lines · Andrew Cooper
Quoted 33 lines · Boris Ostrovsky

If invalid pointer (i.e. something smaller than HYPERVISOR_VIRT_START) is passed for %*ph/%pv/%ps/%pS format specifiers then print "(NULL)"

---
  xen/common/vsprintf.c |   23 ++++++++++++++++-------
  1 files changed, 16 insertions(+), 7 deletions(-)

v2:
* Print "(NULL)" instead of specifier-specific string
* Consider all addresses under HYPERVISOR_VIRT_START as invalid. (I think this is true for both x86 and ARM but I don't have ARM platform to test).

xen/common/vsprintf.c +11 -0
index 065cc42..b9542b5 100644
@@ -270,6 +270,22 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
      const char *fmt = *fmt_ptr, *s;
 
      /* Custom %p suffixes. See XEN_ROOT/docs/misc/printk-formats.txt */
+
+    switch ( fmt[1] )
+    {
+        case 'h':
+        case 's':
+        case 'S':
+        case 'v':
+            ++*fmt_ptr;
+    }
+
+    if ( (unsigned long)arg < HYPERVISOR_VIRT_START )
Quoted 8 lines

xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 289

Quoted 6 lines
xen/common/vsprintf.c +5 -0
+    {
+        char *s = "(NULL)";
+        return string(str, end, s, -1, -1, 0);
+    }
+
      switch ( fmt[1] )

This wont function, as you have inverted the increment of *fmt_ptr and check of fmt[1].

fmt value doesn't change, it is stashed at the top of the routine.

(What *is* wrong in the above code is the fact that the arg test is done outside the switch. It should be part of the four case statements, otherwise we will print plain %p arguments as "NULL").

Quoted 2 lines

"(NULL)" is inappropriate for non-null pointers less than VIRT_START.

Yes, I thought about it after I sent it. "(invalid)"?

Quoted 4 lines

Given the VIRT check, I would just put the entire switch statement inside an "if ( (unsigned long)arg < HYPERVISOR_VIRT_START )" block and let it fall through to the plain number case for a bogus pointer.

Not sure I understand what you are suggesting here, sorry.

-boris

Quoted 33 lines

~Andrew

Quoted 30 lines
xen/common/vsprintf.c +0 -7
      {
      case 'h': /* Raw buffer as hex string. */
@@ -277,9 +293,6 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
          const uint8_t *hex_buffer = arg;
          unsigned int i;
 
-        /* Consumed 'h' from the format string. */
-        ++*fmt_ptr;
-
          /* Bound user count from %* to between 0 and 64 bytes. */
          if ( field_width <= 0 )
              return str;
@@ -306,9 +319,6 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
          unsigned long sym_size, sym_offset;
          char namebuf[KSYM_NAME_LEN+1];
 
-        /* Advance parents fmt string, as we have consumed 's' or 'S' */
-        ++*fmt_ptr;
-
          s = symbols_lookup((unsigned long)arg, &sym_size, &sym_offset, namebuf);
 
          /* If the symbol is not found, fall back to printing the address */
@@ -335,7 +345,6 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
      {
          const struct vcpu *v = arg;
 
-        ++*fmt_ptr;
          if ( str < end )
              *str = 'd';
          str = number(str + 1, end, v->domain->domain_id, 10, -1, -1, 0);
#3 lore raw
Quoted 47 lines · Boris Ostrovsky
Quoted 37 lines · Andrew Cooper
Quoted 36 lines · Boris Ostrovsky

If invalid pointer (i.e. something smaller than HYPERVISOR_VIRT_START) is passed for %*ph/%pv/%ps/%pS format specifiers then print "(NULL)"

---
  xen/common/vsprintf.c |   23 ++++++++++++++++-------
  1 files changed, 16 insertions(+), 7 deletions(-)

v2:
* Print "(NULL)" instead of specifier-specific string
* Consider all addresses under HYPERVISOR_VIRT_START as invalid.
(I think
this is true for both x86 and ARM but I don't have ARM platform
to test).

xen/common/vsprintf.c +0 -0
index 065cc42..b9542b5 100644
@@ -270,6 +270,22 @@ static char *pointer(char *str, char *end,
 const char **fmt_ptr,
      const char *fmt = *fmt_ptr, *s;
        /* Custom %p suffixes. See

XEN_ROOT/docs/misc/printk-formats.txt */

xen/common/vsprintf.c +11 -0
+
+    switch ( fmt[1] )
+    {
+        case 'h':
+        case 's':
+        case 'S':
+        case 'v':
+            ++*fmt_ptr;
+    }
+
+    if ( (unsigned long)arg < HYPERVISOR_VIRT_START )
Quoted 8 lines

xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 289

Quoted 6 lines
xen/common/vsprintf.c +5 -0
+    {
+        char *s = "(NULL)";
+        return string(str, end, s, -1, -1, 0);
+    }
+
      switch ( fmt[1] )

This wont function, as you have inverted the increment of *fmt_ptr and check of fmt[1].

Quoted 1 line

fmt value doesn't change, it is stashed at the top of the routine.

You are correct. My apologies. I however dislike the splitting of the switch into two.

Quoted 10 lines

(What *is* wrong in the above code is the fact that the arg test is done outside the switch. It should be part of the four case statements, otherwise we will print plain %p arguments as "NULL").

Quoted 2 lines

"(NULL)" is inappropriate for non-null pointers less than VIRT_START.

Yes, I thought about it after I sent it. "(invalid)"?

Better, but overriding the number with a string does hide information. In the case that the pointer is invalid, it would be useful to see its contents.

Quoted 9 lines
Quoted 5 lines

Given the VIRT check, I would just put the entire switch statement inside an "if ( (unsigned long)arg < HYPERVISOR_VIRT_START )" block and let it fall through to the plain number case for a bogus pointer.

Not sure I understand what you are suggesting here, sorry.

-boris

if ( (unsigned long)arg < HYPERVISOR_VIRT_START )

{
    switch ( fmt[1] )
    {
    ....
    }
}

This makes the patch a whole 3 line addition and indenting the whole switch block by 4 spaces.

~Andrew

#4 lore raw
Quoted 60 lines · Andrew Cooper
Quoted 47 lines · Boris Ostrovsky
Quoted 37 lines · Andrew Cooper
Quoted 36 lines · Boris Ostrovsky

If invalid pointer (i.e. something smaller than HYPERVISOR_VIRT_START) is passed for %*ph/%pv/%ps/%pS format specifiers then print "(NULL)"

---
   xen/common/vsprintf.c |   23 ++++++++++++++++-------
   1 files changed, 16 insertions(+), 7 deletions(-)

v2:
* Print "(NULL)" instead of specifier-specific string
* Consider all addresses under HYPERVISOR_VIRT_START as invalid.
(I think
this is true for both x86 and ARM but I don't have ARM platform
to test).

xen/common/vsprintf.c +0 -0
index 065cc42..b9542b5 100644
@@ -270,6 +270,22 @@ static char *pointer(char *str, char *end,
 const char **fmt_ptr,
       const char *fmt = *fmt_ptr, *s;
         /* Custom %p suffixes. See

XEN_ROOT/docs/misc/printk-formats.txt */

xen/common/vsprintf.c +11 -0
+
+    switch ( fmt[1] )
+    {
+        case 'h':
+        case 's':
+        case 'S':
+        case 'v':
+            ++*fmt_ptr;
+    }
+
+    if ( (unsigned long)arg < HYPERVISOR_VIRT_START )
Quoted 8 lines

xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 289

Quoted 6 lines
xen/common/vsprintf.c +5 -0
+    {
+        char *s = "(NULL)";
+        return string(str, end, s, -1, -1, 0);
+    }
+
       switch ( fmt[1] )

This wont function, as you have inverted the increment of *fmt_ptr and check of fmt[1].

Quoted 1 line

fmt value doesn't change, it is stashed at the top of the routine.

You are correct. My apologies. I however dislike the splitting of the switch into two.

Quoted 7 lines

(What *is* wrong in the above code is the fact that the arg test is done outside the switch. It should be part of the four case statements, otherwise we will print plain %p arguments as "NULL").

Quoted 1 line
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

"(NULL)" is inappropriate for non-null pointers less than VIRT_START.

Yes, I thought about it after I sent it. "(invalid)"?

Better, but overriding the number with a string does hide information. In the case that the pointer is invalid, it would be useful to see its contents.

How about "<0xXXXXXX>" (i.e. effectively replace "%pv" with "<%p>", with angle brackets indicating invalid pointer)?

Quoted 20 lines
Quoted 10 lines
Quoted 6 lines

Given the VIRT check, I would just put the entire switch statement inside an "if ( (unsigned long)arg < HYPERVISOR_VIRT_START )" block and let it fall through to the plain number case for a bogus pointer.

Not sure I understand what you are suggesting here, sorry.

-boris

if ( (unsigned long)arg < HYPERVISOR_VIRT_START )

{
     switch ( fmt[1] )
     {
     ....
     }
}
Quoted 2 lines

This makes the patch a whole 3 line addition and indenting the whole switch block by 4 spaces.

Still don't understand. This will never print anything unless it's a bad pointer, won't it?

(And if you meant '>=' then we will simply print the invalid pointer in plain %p format. Which, btw, may be the solution but we will still need to bump fmt_ptr, so we again will need another switch or something to test for sub-specifier)

-boris

#5 lore raw
Quoted 66 lines · Boris Ostrovsky
Quoted 65 lines · Andrew Cooper
Quoted 48 lines · Boris Ostrovsky
Quoted 38 lines · Andrew Cooper
Quoted 37 lines · Boris Ostrovsky

If invalid pointer (i.e. something smaller than HYPERVISOR_VIRT_START)
is passed for %*ph/%pv/%ps/%pS format specifiers then print "(NULL)"

---
   xen/common/vsprintf.c |   23 ++++++++++++++++-------
   1 files changed, 16 insertions(+), 7 deletions(-)

v2:
* Print "(NULL)" instead of specifier-specific string
* Consider all addresses under HYPERVISOR_VIRT_START as invalid.
(I think
this is true for both x86 and ARM but I don't have ARM platform
to test).

xen/common/vsprintf.c +0 -0
index 065cc42..b9542b5 100644
@@ -270,6 +270,22 @@ static char *pointer(char *str, char *end,
 const char **fmt_ptr,
       const char *fmt = *fmt_ptr, *s;
         /* Custom %p suffixes. See

XEN_ROOT/docs/misc/printk-formats.txt */

xen/common/vsprintf.c +11 -0
+
+    switch ( fmt[1] )
+    {
+        case 'h':
+        case 's':
+        case 'S':
+        case 'v':
+            ++*fmt_ptr;
+    }
+
+    if ( (unsigned long)arg < HYPERVISOR_VIRT_START )
Quoted 8 lines

xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 289

Quoted 6 lines
xen/common/vsprintf.c +5 -0
+    {
+        char *s = "(NULL)";
+        return string(str, end, s, -1, -1, 0);
+    }
+
       switch ( fmt[1] )

This wont function, as you have inverted the increment of *fmt_ptr and check of fmt[1].

Quoted 1 line

fmt value doesn't change, it is stashed at the top of the routine.

You are correct. My apologies. I however dislike the splitting of the switch into two.

Quoted 10 lines

(What *is* wrong in the above code is the fact that the arg test is done outside the switch. It should be part of the four case statements, otherwise we will print plain %p arguments as "NULL").

Quoted 2 lines

"(NULL)" is inappropriate for non-null pointers less than VIRT_START.

Yes, I thought about it after I sent it. "(invalid)"?

Quoted 8 lines
Quoted 3 lines

Better, but overriding the number with a string does hide information. In the case that the pointer is invalid, it would be useful to see its contents.

How about "<0xXXXXXX>" (i.e. effectively replace "%pv" with "<%p>", with angle brackets indicating invalid pointer)?

It feels like change for change sake, especially as there is a perfectly good hex decode for plain %p.

Quoted 25 lines
Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
Quoted 8 lines
Quoted 4 lines

Given the VIRT check, I would just put the entire switch statement inside an "if ( (unsigned long)arg < HYPERVISOR_VIRT_START )" block and
let it fall through to the plain number case for a bogus pointer.

Not sure I understand what you are suggesting here, sorry.

-boris

if ( (unsigned long)arg < HYPERVISOR_VIRT_START )

{
     switch ( fmt[1] )
     {
     ....
     }
}
Quoted 2 lines

This makes the patch a whole 3 line addition and indenting the whole switch block by 4 spaces.

Quoted 7 lines

Still don't understand. This will never print anything unless it's a bad pointer, won't it?

(And if you meant '>=' then we will simply print the invalid pointer in plain %p format. Which, btw, may be the solution but we will still need to bump fmt_ptr, so we again will need another switch or something to test for sub-specifier)

Oops - I did mean >=. I.e. only do the custom %pX decoding in the case that arg is a plausible pointer.

There is no need I can see to alter the fmt_ptr handling. The code currently works, other than the issue at hand of falling over a bad pointer.

~Andrew

#6 lore raw
Quoted 96 lines · Andrew Cooper
Quoted 58 lines · Boris Ostrovsky
Quoted 57 lines · Andrew Cooper
Quoted 47 lines · Boris Ostrovsky
Quoted 38 lines · Andrew Cooper
Quoted 37 lines · Boris Ostrovsky

If invalid pointer (i.e. something smaller than HYPERVISOR_VIRT_START)
is passed for %*ph/%pv/%ps/%pS format specifiers then print "(NULL)"

---
    xen/common/vsprintf.c |   23 ++++++++++++++++-------
    1 files changed, 16 insertions(+), 7 deletions(-)

v2:
* Print "(NULL)" instead of specifier-specific string
* Consider all addresses under HYPERVISOR_VIRT_START as invalid.
(I think
this is true for both x86 and ARM but I don't have ARM platform
to test).

xen/common/vsprintf.c +0 -0
index 065cc42..b9542b5 100644
@@ -270,6 +270,22 @@ static char *pointer(char *str, char *end,
 const char **fmt_ptr,
        const char *fmt = *fmt_ptr, *s;
          /* Custom %p suffixes. See

XEN_ROOT/docs/misc/printk-formats.txt */

xen/common/vsprintf.c +11 -0
+
+    switch ( fmt[1] )
+    {
+        case 'h':
+        case 's':
+        case 'S':
+        case 'v':
+            ++*fmt_ptr;
+    }
+
+    if ( (unsigned long)arg < HYPERVISOR_VIRT_START )
Quoted 8 lines

xen/common/vsprintf.c static char *pointer(char *str, char *end, const char **fmt_ptr, · line 289

Quoted 6 lines
xen/common/vsprintf.c +5 -0
+    {
+        char *s = "(NULL)";
+        return string(str, end, s, -1, -1, 0);
+    }
+
        switch ( fmt[1] )

This wont function, as you have inverted the increment of *fmt_ptr and check of fmt[1].

Quoted 1 line

fmt value doesn't change, it is stashed at the top of the routine.

You are correct. My apologies. I however dislike the splitting of the switch into two.

Quoted 5 lines

(What *is* wrong in the above code is the fact that the arg test is done outside the switch. It should be part of the four case statements, otherwise we will print plain %p arguments as "NULL").

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line

"(NULL)" is inappropriate for non-null pointers less than VIRT_START.

Yes, I thought about it after I sent it. "(invalid)"?

Better, but overriding the number with a string does hide information. In the case that the pointer is invalid, it would be useful to see its contents.

How about "<0xXXXXXX>" (i.e. effectively replace "%pv" with "<%p>", with angle brackets indicating invalid pointer)?

It feels like change for change sake, especially as there is a perfectly good hex decode for plain %p.

Quoted 18 lines
Quoted 16 lines
Quoted 7 lines
Quoted 4 lines

Given the VIRT check, I would just put the entire switch statement inside an "if ( (unsigned long)arg < HYPERVISOR_VIRT_START )" block and
let it fall through to the plain number case for a bogus pointer.

Not sure I understand what you are suggesting here, sorry.

-boris

if ( (unsigned long)arg < HYPERVISOR_VIRT_START )

{
      switch ( fmt[1] )
      {
      ....
      }
}
Quoted 2 lines

This makes the patch a whole 3 line addition and indenting the whole switch block by 4 spaces.

Quoted 7 lines

Still don't understand. This will never print anything unless it's a bad pointer, won't it?

(And if you meant '>=' then we will simply print the invalid pointer in plain %p format. Which, btw, may be the solution but we will still need to bump fmt_ptr, so we again will need another switch or something to test for sub-specifier)

Quoted 5 lines

Oops - I did mean >=. I.e. only do the custom %pX decoding in the case that arg is a plausible pointer.

There is no need I can see to alter the fmt_ptr handling. The code currently works, other than the issue at hand of falling over a bad pointer.

We do, otherwise we will be printing the sub-specifier. Here is example of what happens if we don't bump it:

    struct vcpu *badvcpu = NULL;
    printk("badvcpu: %pv current: %pv\n", badvcpu, current);

console:

    badvcpu: 0000000000000000v current: d0v0

Also, for %*ph format, if we just go with falling through to plain format and not marking somehow that we are printing a bad pointer:

    unsigned badval = 0xab;
    unsigned *badptr = &badval;
    printk("badptr = %*ph\n", 1, badptr);

console:
badptr = ab

We don't know here whether badptr was pointing to 0xab or it itself was 0xab.

-boris

#7 lore raw
Quoted 4 lines · Boris Ostrovsky

Also, for %*ph format, if we just go with falling through to plain format and not marking somehow that we are printing a bad pointer:

Quoted 9 lines
    unsigned badval = 0xab;
    unsigned *badptr = &badval;
    printk("badptr = %*ph\n", 1, badptr);

console:
badptr = ab

We don't know here whether badptr was pointing to 0xab or it itself was 0xab.

Ugh, bad example. Here is what I meant:

    unsigned badval = 0xab;
    unsigned *badptr = &badval;
    unsigned *badvalptr = (void *)0xab;
    printk("badvalptr = %*ph badptr = %*ph\n", 1, badvalptr, 1, badptr);

console:
badvalptr = ab badptr = ab

Sorry.

-boris

#8 lore raw
In reply to Boris Ostrovsky
Quoted 110 lines · Boris Ostrovsky
Quoted 102 lines · Andrew Cooper
Quoted 65 lines · Boris Ostrovsky
Quoted 64 lines · Andrew Cooper
Quoted 51 lines · Boris Ostrovsky
Quoted 42 lines · Andrew Cooper
Quoted 41 lines · Boris Ostrovsky

If invalid pointer (i.e. something smaller than HYPERVISOR_VIRT_START)
is passed for %*ph/%pv/%ps/%pS format specifiers then print "(NULL)"

---
    xen/common/vsprintf.c |   23 ++++++++++++++++-------
    1 files changed, 16 insertions(+), 7 deletions(-)

v2:
* Print "(NULL)" instead of specifier-specific string
* Consider all addresses under HYPERVISOR_VIRT_START as
invalid.
(I think
this is true for both x86 and ARM but I don't have ARM
platform
to test).

xen/common/vsprintf.c +0 -0
index 065cc42..b9542b5 100644
@@ -270,6 +270,22 @@ static char *pointer(char *str, char *end,
 const char **fmt_ptr,
        const char *fmt = *fmt_ptr, *s;
          /* Custom %p suffixes. See

XEN_ROOT/docs/misc/printk-formats.txt */

xen/common/vsprintf.c +12 -0
+
+    switch ( fmt[1] )
+    {
+        case 'h':
+        case 's':
+        case 'S':
+        case 'v':
+            ++*fmt_ptr;
+    }
+
+    if ( (unsigned long)arg < HYPERVISOR_VIRT_START )
+    {
Quoted 8 lines

xen/common/vsprintf.c

Quoted 5 lines
xen/common/vsprintf.c +4 -0
+        char *s = "(NULL)";
+        return string(str, end, s, -1, -1, 0);
+    }
+
        switch ( fmt[1] )

This wont function, as you have inverted the increment of *fmt_ptr and
check of fmt[1].

Quoted 1 line

fmt value doesn't change, it is stashed at the top of the routine.

You are correct. My apologies. I however dislike the splitting of the
switch into two.

Quoted 7 lines

(What *is* wrong in the above code is the fact that the arg test is done outside the switch. It should be part of the four case statements, otherwise we will print plain %p arguments as "NULL").

Quoted 2 lines

"(NULL)" is inappropriate for non-null pointers less than VIRT_START.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Yes, I thought about it after I sent it. "(invalid)"?

Better, but overriding the number with a string does hide information. In the case that the pointer is invalid, it would be useful to see its contents.

How about "<0xXXXXXX>" (i.e. effectively replace "%pv" with "<%p>", with angle brackets indicating invalid pointer)?

It feels like change for change sake, especially as there is a perfectly good hex decode for plain %p.

Quoted 18 lines
Quoted 16 lines
Quoted 7 lines
Quoted 4 lines

Given the VIRT check, I would just put the entire switch statement inside an "if ( (unsigned long)arg < HYPERVISOR_VIRT_START )" block and
let it fall through to the plain number case for a bogus pointer.

Not sure I understand what you are suggesting here, sorry.

-boris

if ( (unsigned long)arg < HYPERVISOR_VIRT_START )

{
      switch ( fmt[1] )
      {
      ....
      }
}
Quoted 2 lines

This makes the patch a whole 3 line addition and indenting the whole switch block by 4 spaces.

Quoted 7 lines

Still don't understand. This will never print anything unless it's a bad pointer, won't it?

(And if you meant '>=' then we will simply print the invalid pointer in plain %p format. Which, btw, may be the solution but we will still need to bump fmt_ptr, so we again will need another switch or something to test for sub-specifier)

Quoted 6 lines

Oops - I did mean >=. I.e. only do the custom %pX decoding in the case that arg is a plausible pointer.

There is no need I can see to alter the fmt_ptr handling. The code currently works, other than the issue at hand of falling over a bad pointer.

Quoted 9 lines

We do, otherwise we will be printing the sub-specifier. Here is example of what happens if we don't bump it:

    struct vcpu *badvcpu = NULL;
    printk("badvcpu: %pv current: %pv\n", badvcpu, current);

console:

    badvcpu: 0000000000000000v current: d0v0

Ah - I see what you mean now.

Quoted 8 lines

Also, for %*ph format, if we just go with falling through to plain format and not marking somehow that we are printing a bad pointer:

    unsigned badval = 0xab;
    unsigned *badptr = &badval;
    printk("badptr = %*ph\n", 1, badptr);
Quoted 5 lines

console:
badptr = ab

We don't know here whether badptr was pointing to 0xab or it itself was 0xab.

Ok. As this is only for making debug code less fragile, it probably is better to explicitly call out a bad pointer differently.

~Andrew