Skip to contentSkip to conversation

libselinux: sha1: prevent overflow in Sha1Update()

PATCH In review Stephen Smalley 1 participant 2 messages Jun 8 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 libselinux: sha1: prevent overflow in Sha1Update()

With 32-bit i/j and a BufferSize == UINT32_MAX (passed by digest_gen_hash() if the specfile buffer exceeds UINT32_MAX), the (j + BufferSize) > 63 and (i + 63) < BufferSize) can both wrap. Extend them to be 64-bit to avoid such overflows.

---
 libselinux/src/sha1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libselinux/src/sha1.c +2 -2
index 279625ee..02e05811 100644
@@ -226,8 +226,8 @@ void Sha1Initialise(Sha1Context *Context)
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 void Sha1Update(Sha1Context *Context, const void *Buffer, uint32_t BufferSize)
 {
-	uint32_t i;
-	uint32_t j;
+	uint64_t i;
+	uint64_t j;
 
 	j = (Context->Count[0] >> 3) & 63;
 	if ((Context->Count[0] += BufferSize << 3) < (BufferSize << 3)) {
Signature · 1 line
2.54.0
author maintainer
#1 lore raw
Quoted 16 lines · Stephen Smalley

With 32-bit i/j and a BufferSize == UINT32_MAX (passed by digest_gen_hash() if the specfile buffer exceeds UINT32_MAX), the (j + BufferSize) > 63 and (i + 63) < BufferSize) can both wrap. Extend them to be 64-bit to avoid such overflows.

---
 libselinux/src/sha1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libselinux/src/sha1.c +0 -0
index 279625ee..02e05811 100644

libselinux/src/sha1.c void Sha1Initialise(Sha1Context *Context) · line 233

Quoted 10 lines
libselinux/src/sha1.c +2 -2
@@ -226,8 +226,8 @@ void Sha1Initialise(Sha1Context *Context)
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 void Sha1Update(Sha1Context *Context, const void *Buffer, uint32_t BufferSize)
 {
-       uint32_t i;
-       uint32_t j;
+       uint64_t i;
+       uint64_t j;
 
        j = (Context->Count[0] >> 3) & 63;
        if ((Context->Count[0] += BufferSize << 3) < (BufferSize << 3)) {

This one isn't needed if we take the following instead:
https://lore.kernel.org/selinux/[email protected]/T/#u

That one is necessary regardless and will prevent this case from being possible.