Skip to contentSkip to conversation

tools: libxl: do not overrun input buffer in libxl__parse_mac

PATCH In review Ian Campbell 4 participants 5 messages Nov 6, 2014 – Jan 12, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: libxl: do not overrun input buffer in libxl__parse_mac

Valgrind reports:
==7971== Invalid read of size 1

==7971==    at 0x40877BE: libxl__parse_mac (libxl_internal.c:288)
==7971==    by 0x405C5F8: libxl__device_nic_from_xs_be (libxl.c:3405)
==7971==    by 0x4065542: libxl__append_nic_list_of_type (libxl.c:3484)
==7971==    by 0x4065542: libxl_device_nic_list (libxl.c:3504)
==7971==    by 0x406F561: libxl_retrieve_domain_configuration (libxl.c:6661)
==7971==    by 0x805671C: reload_domain_config (xl_cmdimpl.c:2037)
==7971==    by 0x8057F30: handle_domain_death (xl_cmdimpl.c:2116)
==7971==    by 0x8057F30: create_domain (xl_cmdimpl.c:2580)
==7971==    by 0x805B4B2: main_create (xl_cmdimpl.c:4652)
==7971==    by 0x804EAB2: main (xl.c:378)

This is because on the final iteration the tok += 3 skips over the terminating NUL to the next byte, and then *tok reads it. Fix this by using endptr as the iterator.

---
 tools/libxl/libxl_internal.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/libxl/libxl_internal.c +3 -1
index 02a71cb..00c3b1e 100644
@@ -284,10 +284,12 @@ _hidden int libxl__parse_mac(const char *s, libxl_mac mac)
     char *endptr;
     int i;
 
-    for (i = 0, tok = s; *tok && (i < 6); ++i, tok += 3) {
+    for (i = 0, tok = s; *tok && (i < 6); ++i, tok = endptr) {
         mac[i] = strtol(tok, &endptr, 16);
         if (endptr != (tok + 2) || (*endptr != '\0' && *endptr != ':') )
             return ERROR_INVAL;
+        if (*endptr == ':')
+            endptr++;
     }
     if ( i != 6 )
         return ERROR_INVAL;
Signature · 1 line
1.7.10.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 30 lines · Ian Campbell

Valgrind reports:
==7971== Invalid read of size 1

==7971==    at 0x40877BE: libxl__parse_mac (libxl_internal.c:288)
==7971==    by 0x405C5F8: libxl__device_nic_from_xs_be (libxl.c:3405)
==7971==    by 0x4065542: libxl__append_nic_list_of_type (libxl.c:3484)
==7971==    by 0x4065542: libxl_device_nic_list (libxl.c:3504)
==7971==    by 0x406F561: libxl_retrieve_domain_configuration (libxl.c:6661)
==7971==    by 0x805671C: reload_domain_config (xl_cmdimpl.c:2037)
==7971==    by 0x8057F30: handle_domain_death (xl_cmdimpl.c:2116)
==7971==    by 0x8057F30: create_domain (xl_cmdimpl.c:2580)
==7971==    by 0x805B4B2: main_create (xl_cmdimpl.c:4652)
==7971==    by 0x804EAB2: main (xl.c:378)

This is because on the final iteration the tok += 3 skips over the terminating NUL to the next byte, and then *tok reads it. Fix this by using endptr as the iterator.

---
  tools/libxl/libxl_internal.c |    4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
tools/libxl/libxl_internal.c +0 -0
index 02a71cb..00c3b1e 100644
@@ -284,10 +284,12 @@ _hidden int libxl__parse_mac(const char *s, libxl_mac mac)
      char *endptr;
      int i;
 

tools/libxl/libxl_internal.c _hidden int libxl__parse_mac(const char *s, libxl_mac mac) · line 295

Quoted 10 lines
tools/libxl/libxl_internal.c +3 -1
-    for (i = 0, tok = s; *tok && (i < 6); ++i, tok += 3) {
+    for (i = 0, tok = s; *tok && (i < 6); ++i, tok = endptr) {
          mac[i] = strtol(tok, &endptr, 16);
          if (endptr != (tok + 2) || (*endptr != '\0' && *endptr != ':') )
              return ERROR_INVAL;
+        if (*endptr == ':')
+            endptr++;
      }
      if ( i != 6 )
          return ERROR_INVAL;

Looks good to me.

-Don Slutz

Acked-by on v1 1/1
#2 lore raw
In reply to Ian Campbell
Quoted 13 lines · Ian Campbell

Valgrind reports:
==7971== Invalid read of size 1

==7971==    at 0x40877BE: libxl__parse_mac (libxl_internal.c:288)
==7971==    by 0x405C5F8: libxl__device_nic_from_xs_be (libxl.c:3405)
==7971==    by 0x4065542: libxl__append_nic_list_of_type (libxl.c:3484)
==7971==    by 0x4065542: libxl_device_nic_list (libxl.c:3504)
==7971==    by 0x406F561: libxl_retrieve_domain_configuration (libxl.c:6661)
==7971==    by 0x805671C: reload_domain_config (xl_cmdimpl.c:2037)
==7971==    by 0x8057F30: handle_domain_death (xl_cmdimpl.c:2116)
==7971==    by 0x8057F30: create_domain (xl_cmdimpl.c:2580)
==7971==    by 0x805B4B2: main_create (xl_cmdimpl.c:4652)
==7971==    by 0x804EAB2: main (xl.c:378)
Quoted 5 lines

This is because on the final iteration the tok += 3 skips over the terminating NUL to the next byte, and then *tok reads it. Fix this by using endptr as the iterator.

This is a candidate for backporting.

Wei.

#3 lore raw

Wei Liu writes ("Re: [PATCH] tools: libxl: do not overrun input buffer in libx\
l__parse_mac"):

Quoted 1 line
Quoted 1 line

This is because on the final iteration the tok += 3 skips over the termina\

ting

Quoted 1 line
Quoted 1 line

NUL to the next byte, and then *tok reads it. Fix this by using endptr as \

the

Quoted 5 lines
Quoted 3 lines

iterator.

Quoted 1 line

This is a candidate for backporting.

Queued.

Ian, I don't suppose you could persuade your text editor to wrap your commit messages a bit narrower ? By the time they've been quoted a couple of times they look to me like what you see above.

Thanks,
Ian.

#4 lore raw

Ian Jackson writes ("Re: [PATCH] tools: libxl: do not overrun input buffer in libxl__parse_mac"):

Quoted 4 lines

Wei Liu writes ("Re: [PATCH] tools: libxl: do not overrun input buffer in libx\

Quoted 1 line

This is a candidate for backporting.

Queued.

Backported to 4.3 and 4.4.

Thanks,
Ian.