Skip to contentSkip to conversation

unlzma: avoid UB shift

PATCH In review Jan Beulich 3 participants 5 messages Mar 24 – Mar 30

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 unlzma: avoid UB shift

Shifting signed quantities has restrictions. Since the wrapping macro of read_int() type-casts the result anyway, switch function return type as well as the local variable to the corresponding unsigned type.

---
We've inherited that code from Linux, and the same code still exists there. As I'm entirely uncertain whether they would even care, I'd prefer to not take the route of posting a patch against Linux first.

xen/common/unlzma.c +2 -2
@@ -30,10 +30,10 @@
 
 #include "decompress.h"
 
-static long long __init read_int(unsigned char *ptr, int size)
+static unsigned long long __init read_int(unsigned char *ptr, int size)
 {
 	int i;
-	long long ret = 0;
+	unsigned long long ret = 0;
 
 	for (i = 0; i < size; i++)
 		ret = (ret << 8) | ptr[size-i-1];
#1 lore raw
Quoted 15 lines · Jan Beulich

Shifting signed quantities has restrictions. Since the wrapping macro of read_int() type-casts the result anyway, switch function return type as well as the local variable to the corresponding unsigned type.

---
We've inherited that code from Linux, and the same code still exists there. As I'm entirely uncertain whether they would even care, I'd prefer to not take the route of posting a patch against Linux first.

xen/common/unlzma.c line 33

Quoted 5 lines
xen/common/unlzma.c +1 -1
@@ -30,10 +30,10 @@
 
 #include "decompress.h"
 
-static long long __init read_int(unsigned char *ptr, int size)
+static unsigned long long __init read_int(unsigned char *ptr, int size)

nit: Since we're touching read_int() anyway, would it make sense to also tighten the helper's interface, i.e. make ptr const and use size_t for size?

That would better match the actual usage: the buffer is only read from, and size is really a byte count, usually coming from sizeof().

xen/common/unlzma.c line 39

Quoted 7 lines
xen/common/unlzma.c +1 -1
 {
        int i;
-       long long ret = 0;
+       unsigned long long ret = 0;
 
        for (i = 0; i < size; i++)
                ret = (ret << 8) | ptr[size-i-1];

Separately, the loop could also be written without the temporary i, using a simpler reverse traversal, for example:

while ( size )

    ret = (ret << 8) | ptr[--size];
Quoted 1 line

Best regards,
Mykola

#2 lore raw
Quoted 16 lines · Mykola Kvach
Quoted 15 lines · Jan Beulich

Shifting signed quantities has restrictions. Since the wrapping macro of read_int() type-casts the result anyway, switch function return type as well as the local variable to the corresponding unsigned type.

---
We've inherited that code from Linux, and the same code still exists there. As I'm entirely uncertain whether they would even care, I'd prefer to not take the route of posting a patch against Linux first.

xen/common/unlzma.c +0 -0
@@ -30,10 +30,10 @@
 
Quoted 7 lines

xen/common/unlzma.c line 33

Quoted 4 lines
xen/common/unlzma.c +1 -1
 #include "decompress.h"
 
-static long long __init read_int(unsigned char *ptr, int size)
+static unsigned long long __init read_int(unsigned char *ptr, int size)

nit: Since we're touching read_int() anyway, would it make sense to also tighten the helper's interface, i.e. make ptr const and use size_t for size?

No, not mixed into a patch which is fixing a real bug.  Cleanup can come later.

This code is vendored from Linux.  For better or worse we try not to deviate, although that is going to have to change at some point.

~Andrew

#3 lore raw
In reply to Mykola Kvach
Quoted 6 lines · Mykola Kvach
Quoted 5 lines · Jan Beulich

Shifting signed quantities has restrictions. Since the wrapping macro of read_int() type-casts the result anyway, switch function return type as well as the local variable to the corresponding unsigned type.

Quoted 6 lines
Quoted 6 lines

---
We've inherited that code from Linux, and the same code still exists there. As I'm entirely uncertain whether they would even care, I'd prefer to not take the route of posting a patch against Linux first.

As following from this, imo we want to diverge from the original as little as possible. IOW ...

Quoted 21 lines

xen/common/unlzma.c line 33

Quoted 8 lines
xen/common/unlzma.c +1 -1
@@ -30,10 +30,10 @@
 
 #include "decompress.h"
 
-static long long __init read_int(unsigned char *ptr, int size)
+static unsigned long long __init read_int(unsigned char *ptr, int size)

nit: Since we're touching read_int() anyway, would it make sense to also tighten the helper's interface, i.e. make ptr const and use size_t for size?

That would better match the actual usage: the buffer is only read from, and size is really a byte count, usually coming from sizeof().

Quoted 5 lines
xen/common/unlzma.c +1 -1
 {
        int i;
-       long long ret = 0;
+       unsigned long long ret = 0;
 
Quoted 8 lines

xen/common/unlzma.c line 39

Quoted 2 lines
xen/common/unlzma.c +0 -0
        for (i = 0; i < size; i++)
                ret = (ret << 8) | ptr[size-i-1];

Separately, the loop could also be written without the temporary i, using a simpler reverse traversal, for example:

while ( size )

    ret = (ret << 8) | ptr[--size];

... I'd suggest that you submit patches against Linux, for us to then re-sync.

Jan