Blame SOURCES/aide-0.16-CVE-2021-45417.patch

9890ad
diff --git a/include/base64.h b/include/base64.h
9890ad
index 0ff7116..381ef5d 100644
9890ad
--- a/include/base64.h
9890ad
+++ b/include/base64.h
9890ad
@@ -36,7 +36,6 @@
9890ad
 #include <assert.h>
9890ad
 #include "types.h"
9890ad
 
9890ad
-#define B64_BUF 16384
9890ad
 #define FAIL -1
9890ad
 #define SKIP -2
9890ad
 
9890ad
diff --git a/src/base64.c b/src/base64.c
9890ad
index fd01bac..1b0f301 100644
9890ad
--- a/src/base64.c
9890ad
+++ b/src/base64.c
9890ad
@@ -85,11 +85,9 @@ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL
9890ad
 };
9890ad
 
9890ad
 /* Returns NULL on error */
9890ad
-/* FIXME Possible buffer overflow on outputs larger than B64_BUF */
9890ad
 char* encode_base64(byte* src,size_t ssize)
9890ad
 {
9890ad
   char* outbuf;
9890ad
-  char* retbuf;
9890ad
   int pos;
9890ad
   int i, l, left;
9890ad
   unsigned long triple;
9890ad
@@ -101,7 +99,10 @@ char* encode_base64(byte* src,size_t ssize)
9890ad
     error(240,"\n");
9890ad
     return NULL;
9890ad
   }
9890ad
-  outbuf = (char *)malloc(sizeof(char)*B64_BUF);
9890ad
+
9890ad
+  /* length of encoded base64 string (padded) */
9890ad
+  size_t length = sizeof(char)* ((ssize + 2) / 3) * 4;
9890ad
+  outbuf = (char *)malloc(length + 1);
9890ad
   
9890ad
   /* Initialize working pointers */
9890ad
   inb = src;
9890ad
@@ -162,20 +163,14 @@ char* encode_base64(byte* src,size_t ssize)
9890ad
       inb++;
9890ad
   }
9890ad
   
9890ad
-  /* outbuf is not completely used so we use retbuf */
9890ad
-  retbuf=(char*)malloc(sizeof(char)*(pos+1));
9890ad
-  memcpy(retbuf,outbuf,pos);
9890ad
-  retbuf[pos]='\0';
9890ad
-  free(outbuf);
9890ad
+  outbuf[pos]='\0';
9890ad
 
9890ad
-  return retbuf;
9890ad
+  return outbuf;
9890ad
 }
9890ad
 
9890ad
-/* FIXME Possible buffer overflow on outputs larger than B64_BUF */
9890ad
 byte* decode_base64(char* src,size_t ssize, size_t *ret_len)
9890ad
 {
9890ad
   byte* outbuf;
9890ad
-  byte* retbuf;
9890ad
   char* inb;
9890ad
   int i;
9890ad
   int l;
9890ad
@@ -188,10 +183,18 @@ byte* decode_base64(char* src,size_t ssize, size_t *ret_len)
9890ad
   if (!ssize||src==NULL)
9890ad
     return NULL;
9890ad
 
9890ad
+  /* exit on unpadded input */
9890ad
+  if (ssize % 4) {
9890ad
+    error(3, "decode_base64: '%s' has invalid length (missing padding characters?)", src);
9890ad
+    return NULL;
9890ad
+  }
9890ad
+
9890ad
+  /* calculate length of decoded string, substract padding chars if any (ssize is >= 4) */
9890ad
+  size_t length = sizeof(byte) * ((ssize / 4) * 3)- (src[ssize-1] == '=') - (src[ssize-2] == '=');
9890ad
 
9890ad
   /* Initialize working pointers */
9890ad
   inb = src;
9890ad
-  outbuf = (byte *)malloc(sizeof(byte)*B64_BUF);
9890ad
+  outbuf = (byte *)malloc(length + 1);
9890ad
 
9890ad
   l = 0;
9890ad
   triple = 0;
9890ad
@@ -243,15 +246,11 @@ byte* decode_base64(char* src,size_t ssize, size_t *ret_len)
9890ad
       inb++;
9890ad
     }
9890ad
   
9890ad
-  retbuf=(byte*)malloc(sizeof(byte)*(pos+1));
9890ad
-  memcpy(retbuf,outbuf,pos);
9890ad
-  retbuf[pos]='\0';
9890ad
-  
9890ad
-  free(outbuf);
9890ad
+  outbuf[pos]='\0';
9890ad
 
9890ad
   if (ret_len) *ret_len = pos;
9890ad
   
9890ad
-  return retbuf;
9890ad
+  return outbuf;
9890ad
 }
9890ad
 
9890ad
 size_t length_base64(char* src,size_t ssize)
9890ad
diff --git a/src/db.c b/src/db.c
9890ad
index 858240d..62c4faa 100644
9890ad
--- a/src/db.c
9890ad
+++ b/src/db.c
9890ad
@@ -664,13 +664,15 @@ db_line* db_char2line(char** ss,int db){
9890ad
 
9890ad
 time_t base64totime_t(char* s){
9890ad
   
9890ad
+  if(strcmp(s,"0")==0){
9890ad
+      return 0;
9890ad
+  }
9890ad
   byte* b=decode_base64(s,strlen(s),NULL);
9890ad
   char* endp;
9890ad
   
9890ad
-  if (b==NULL||strcmp(s,"0")==0) {
9890ad
+  if (b==NULL) {
9890ad
     
9890ad
     /* Should we print error here? */
9890ad
-    free(b);
9890ad
     
9890ad
     return 0;
9890ad
   } else {