Browse Source

fix(core): Avoid buffer overflow in base64 decoding (length check)

Stefan Profanter 4 years ago
parent
commit
95b3deb6a1
1 changed files with 10 additions and 0 deletions
  1. 10 0
      deps/base64.c

+ 10 - 0
deps/base64.c

@@ -92,9 +92,19 @@ UA_unbase64(const unsigned char *src, size_t len, size_t *out_len) {
     }
 
     if(pad1) {
+        if (last + 1 >= len) {
+            UA_free(str);
+            *out_len = 0;
+            return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
+        }
         uint32_t n = from_b64[p[last]] << 18 | from_b64[p[last + 1]] << 12;
         *pos++ = (unsigned char)(n >> 16);
         if(pad2) {
+            if (last + 2 >= len) {
+                UA_free(str);
+                *out_len = 0;
+                return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
+            }
             n |= from_b64[p[last + 2]] << 6;
             *pos++ = (unsigned char)(n >> 8 & 0xFF);
         }