Blame SOURCES/openscap-1.3.5-memory-PR_1627.patch

c342a4
From 5eea79eaf426ac3e51a09d3f3fe72c2b385abc89 Mon Sep 17 00:00:00 2001
c342a4
From: =?UTF-8?q?Jan=20=C4=8Cern=C3=BD?= <jcerny@redhat.com>
c342a4
Date: Tue, 10 Nov 2020 11:16:00 +0100
c342a4
Subject: [PATCH] Fix memory allocation
c342a4
c342a4
We can't assume that size of a structure is a sum of sizes of its
c342a4
members because padding and alignment can be involved. In fact,
c342a4
we need to allocate more bytes for the structure than the
c342a4
sum of sizes of its members.
c342a4
c342a4
The wrong assumption caused invalid writes and invalid reads
c342a4
which can be discovered by valgrind. Moreover, when run with
c342a4
MALLOC_CHECK_ environment variable set to non-zero value, the
c342a4
program aborted.
c342a4
c342a4
The memory issue happened only when NDEBUG is defined, eg. when cmake
c342a4
-DCMAKE_BUILD_TYPE=RelWithDebInfo or Release, it doesn't happen if cmake
c342a4
-DCMAKE_BUILD_TYPE=Debug which we usually use in Jenkins CI. This is
c342a4
most likely because in debug mode the struct SEXP contains 2 additional
c342a4
members which are the magic canaries and therefore is bigger.
c342a4
c342a4
This commit wants to fix the problem by 2 step allocation in which
c342a4
first the size of the struct SEXP_val_lblk is used and then the
c342a4
array of SEXPs is allocated separately.
c342a4
c342a4
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1891770
c342a4
---
c342a4
 src/OVAL/probes/SEAP/_sexp-value.h |  2 +-
c342a4
 src/OVAL/probes/SEAP/sexp-value.c  | 12 ++++++------
c342a4
 2 files changed, 7 insertions(+), 7 deletions(-)
c342a4
c342a4
diff --git a/src/OVAL/probes/SEAP/_sexp-value.h b/src/OVAL/probes/SEAP/_sexp-value.h
c342a4
index 426cd2c3d..e66777ef9 100644
c342a4
--- a/src/OVAL/probes/SEAP/_sexp-value.h
c342a4
+++ b/src/OVAL/probes/SEAP/_sexp-value.h
c342a4
@@ -94,7 +94,7 @@ struct SEXP_val_lblk {
c342a4
         uintptr_t nxsz;
c342a4
         uint16_t  real;
c342a4
         uint16_t  refs;
c342a4
-        SEXP_t    memb[];
c342a4
+	SEXP_t *memb;
c342a4
 };
c342a4
 
c342a4
 size_t    SEXP_rawval_list_length (struct SEXP_val_list *list);
c342a4
diff --git a/src/OVAL/probes/SEAP/sexp-value.c b/src/OVAL/probes/SEAP/sexp-value.c
c342a4
index a11cbc70c..b8b3ed609 100644
c342a4
--- a/src/OVAL/probes/SEAP/sexp-value.c
c342a4
+++ b/src/OVAL/probes/SEAP/sexp-value.c
c342a4
@@ -106,10 +106,8 @@ uintptr_t SEXP_rawval_lblk_new (uint8_t sz)
c342a4
 {
c342a4
         _A(sz < 16);
c342a4
 
c342a4
-	struct SEXP_val_lblk *lblk = oscap_aligned_malloc(
c342a4
-		sizeof(uintptr_t) + (2 * sizeof(uint16_t)) + (sizeof(SEXP_t) * (1 << sz)),
c342a4
-		SEXP_LBLK_ALIGN
c342a4
-	);
c342a4
+	struct SEXP_val_lblk *lblk = malloc(sizeof(struct SEXP_val_lblk));
c342a4
+	lblk->memb = malloc(sizeof(SEXP_t) * (1 << sz));
c342a4
 
c342a4
         lblk->nxsz = ((uintptr_t)(NULL) & SEXP_LBLKP_MASK) | ((uintptr_t)sz & SEXP_LBLKS_MASK);
c342a4
         lblk->refs = 1;
c342a4
@@ -519,7 +517,8 @@ void SEXP_rawval_lblk_free (uintptr_t lblkp, void (*func) (SEXP_t *))
c342a4
                         func (lblk->memb + lblk->real);
c342a4
                 }
c342a4
 
c342a4
-		oscap_aligned_free(lblk);
c342a4
+		free(lblk->memb);
c342a4
+		free(lblk);
c342a4
 
c342a4
                 if (next != NULL)
c342a4
                         SEXP_rawval_lblk_free ((uintptr_t)next, func);
c342a4
@@ -540,7 +539,8 @@ void SEXP_rawval_lblk_free1 (uintptr_t lblkp, void (*func) (SEXP_t *))
c342a4
                         func (lblk->memb + lblk->real);
c342a4
                 }
c342a4
 
c342a4
-		oscap_aligned_free(lblk);
c342a4
+		free(lblk->memb);
c342a4
+		free(lblk);
c342a4
         }
c342a4
 
c342a4
         return;
c342a4
-- 
c342a4
2.26.2
c342a4