libselinux: sha1: prevent overflow in Sha1Update()
Stephen Smalley
maintainerPATCH 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
@@ -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
Stephen Smalley
maintainerQuoted 16 lines · Stephen Smalley
With 32-bit i/j and a BufferSize ==
UINT32_MAX(passed bydigest_gen_hash()if the specfile buffer exceedsUINT32_MAX), the (j + BufferSize) > 63 and (i + 63) < BufferSize) can both wrap. Extend them to be 64-bit to avoid such overflows.Signed-off-by Stephen Smalley <[email protected]>--- libselinux/src/sha1.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)libselinux/src/sha1.c +0
-0
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.