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

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