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

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