Blame SOURCES/libgcrypt-1.10.0-fips-integrity2.patch

e35e9c
From 3c8b6c4a9cad59c5e1db5706f6774a3141b60210 Mon Sep 17 00:00:00 2001
e35e9c
From: NIIBE Yutaka <gniibe@fsij.org>
e35e9c
Date: Thu, 17 Feb 2022 10:28:05 +0900
e35e9c
Subject: [PATCH] fips: Fix gen-note-integrity.sh script not to use cmp
e35e9c
 utility.
e35e9c
e35e9c
* src/gen-note-integrity.sh: Simplify detecting 32-bit machine
e35e9c
or 64-bit machine.
e35e9c
e35e9c
--
e35e9c
e35e9c
GnuPG-bug-id: 5835
e35e9c
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
e35e9c
---
e35e9c
 src/gen-note-integrity.sh | 8 ++++----
e35e9c
 1 file changed, 4 insertions(+), 4 deletions(-)
e35e9c
e35e9c
diff --git a/src/gen-note-integrity.sh b/src/gen-note-integrity.sh
e35e9c
index 969fdca6..878d7095 100755
e35e9c
--- a/src/gen-note-integrity.sh
e35e9c
+++ b/src/gen-note-integrity.sh
e35e9c
@@ -73,9 +73,9 @@ FILE=.libs/libgcrypt.so
e35e9c
 #
e35e9c
 # Fixup the ELF header to clean up section information
e35e9c
 #
e35e9c
-printf '%b' '\002' > 2.bin
e35e9c
-dd ibs=1 skip=4 count=1 if=$FILE status=none > class-byte.bin
e35e9c
-if cmp class-byte.bin 2.bin; then
e35e9c
+BYTE002=$(printf '%b' '\002')
e35e9c
+CLASS_BYTE=$(dd ibs=1 skip=4 count=1 if=$FILE status=none)
e35e9c
+if test "$CLASS_BYTE" = "$BYTE002"; then
e35e9c
     CLASS=64
e35e9c
     HEADER_SIZE=64
e35e9c
 else
e35e9c
@@ -112,4 +112,4 @@ END { print offset}")
e35e9c
  dd ibs=1 skip=$HEADER_SIZE count=$OFFSET if=$FILE status=none) \
e35e9c
  | ./hmac256 --stdkey --binary
e35e9c
 
e35e9c
-rm -f 2.bin class-byte.bin header-fixed.bin
e35e9c
+rm -f header-fixed.bin
e35e9c
-- 
e35e9c
2.39.1
e35e9c
e35e9c
e35e9c
From 052c5ef4cea56772b7015e36f231fa0bcbf91410 Mon Sep 17 00:00:00 2001
e35e9c
From: NIIBE Yutaka <gniibe@fsij.org>
e35e9c
Date: Thu, 17 Feb 2022 11:21:35 +0900
e35e9c
Subject: [PATCH] fips: Clarify what to be hashed for the integrity check.
e35e9c
e35e9c
* src/fips.c (get_file_offset): Compute the maximum offset
e35e9c
of segments.
e35e9c
* src/gen-note-integrity.sh: Likewise.
e35e9c
e35e9c
--
e35e9c
e35e9c
The result is same (in current format of ELF program).
e35e9c
Semantics is more clear.  It hashes:
e35e9c
e35e9c
  - From the start of shared library file,
e35e9c
  - fixed up the ELF header to exclude link-time information,
e35e9c
  - up to the last segment.
e35e9c
e35e9c
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
e35e9c
---
e35e9c
 src/fips.c                | 20 +++++++++-----------
e35e9c
 src/gen-note-integrity.sh | 20 ++++++++++++++------
e35e9c
 2 files changed, 23 insertions(+), 17 deletions(-)
e35e9c
e35e9c
diff --git a/src/fips.c b/src/fips.c
e35e9c
index d798d577..89f8204b 100644
e35e9c
--- a/src/fips.c
e35e9c
+++ b/src/fips.c
e35e9c
@@ -595,7 +595,7 @@ run_random_selftests (void)
e35e9c
 
e35e9c
 /*
e35e9c
  * In the ELF file opened as FP, fill the ELF header to the pointer
e35e9c
- * EHDR_P, determine the offset of last loadable segment in R_OFFSET.
e35e9c
+ * EHDR_P, determine the maximum offset of segments in R_OFFSET.
e35e9c
  * Also, find the section which contains the hmac value and return it
e35e9c
  * in HMAC.  Rewinds FP to the beginning on success.
e35e9c
  */
e35e9c
@@ -624,24 +624,22 @@ get_file_offset (FILE *fp, ElfW (Ehdr) *ehdr_p,
e35e9c
   if (fseek (fp, ehdr_p->e_phoff, SEEK_SET) != 0)
e35e9c
     return gpg_error_from_syserror ();
e35e9c
 
e35e9c
-  /* Iterate over the program headers, determine the last loadable
e35e9c
-     segment.  */
e35e9c
+  /* Iterate over the program headers, determine the last offset of
e35e9c
+     segments.  */
e35e9c
   for (i = 0; i < ehdr_p->e_phnum; i++)
e35e9c
     {
e35e9c
+      unsigned long off;
e35e9c
+
e35e9c
       if (fread (&phdr, sizeof (phdr), 1, fp) != 1)
e35e9c
         return gpg_error_from_syserror ();
e35e9c
 
e35e9c
-      if (phdr.p_type == PT_PHDR)
e35e9c
-        continue;
e35e9c
-
e35e9c
-      if (phdr.p_type != PT_LOAD)
e35e9c
-        break;
e35e9c
-
e35e9c
-      off_segment = phdr.p_offset + phdr.p_filesz;
e35e9c
+      off = phdr.p_offset + phdr.p_filesz;
e35e9c
+      if (off_segment < off)
e35e9c
+        off_segment = off;
e35e9c
     }
e35e9c
 
e35e9c
   if (!off_segment)
e35e9c
-    /* The segment not found in the file */
e35e9c
+    /* No segment found in the file */
e35e9c
     return gpg_error (GPG_ERR_INV_OBJ);
e35e9c
 
e35e9c
   /* The section header entry size should match the size of the shdr struct */
e35e9c
diff --git a/src/gen-note-integrity.sh b/src/gen-note-integrity.sh
e35e9c
index 878d7095..50071bf5 100755
e35e9c
--- a/src/gen-note-integrity.sh
e35e9c
+++ b/src/gen-note-integrity.sh
e35e9c
@@ -95,21 +95,29 @@ else
e35e9c
     dd ibs=1         count=6  if=/dev/zero status=none
e35e9c
 fi > header-fixed.bin
e35e9c
 
e35e9c
-# Compute the end of loadable segment.
e35e9c
+#
e35e9c
+# Compute the end of segments, and emit the COUNT to read
e35e9c
+# (For each segment in program headers, calculate the offset
e35e9c
+#  and select the maximum)
e35e9c
 #
e35e9c
 # This require computation in hexadecimal, and GNU awk needs
e35e9c
 # --non-decimal-data option
e35e9c
 #
e35e9c
-OFFSET=$($READELF --wide --program-headers $FILE | \
e35e9c
-         $AWK $AWK_OPTION "/^  LOAD/ { offset=\$2+\$5-$HEADER_SIZE }\
e35e9c
-END { print offset}")
e35e9c
+COUNT=$($READELF --wide --program-headers $FILE | \
e35e9c
+         $AWK $AWK_OPTION \
e35e9c
+"BEGIN { max_offset=0 }
e35e9c
+/^\$/ { if (program_headers_start) program_headers_end=1 }
e35e9c
+(program_headers_start && !program_headers_end) { offset = \$2 + \$5 }
e35e9c
+(max_offset < offset) { max_offset = offset }
e35e9c
+/^  Type/ { program_headers_start=1 }
e35e9c
+END { print max_offset- $HEADER_SIZE }")
e35e9c
 
e35e9c
 #
e35e9c
-# Feed the header fixed and loadable segments to HMAC256
e35e9c
+# Feed the header fixed and all segments to HMAC256
e35e9c
 # to generate hmac hash of the FILE
e35e9c
 #
e35e9c
 (cat header-fixed.bin; \
e35e9c
- dd ibs=1 skip=$HEADER_SIZE count=$OFFSET if=$FILE status=none) \
e35e9c
+ dd ibs=1 skip=$HEADER_SIZE count=$COUNT if=$FILE status=none) \
e35e9c
  | ./hmac256 --stdkey --binary
e35e9c
 
e35e9c
 rm -f header-fixed.bin
e35e9c
-- 
e35e9c
2.39.1
e35e9c
e35e9c
e35e9c
From 3fd3bb31597f80c76a94ea62e42d58d796beabf1 Mon Sep 17 00:00:00 2001
e35e9c
From: Jakub Jelen <jjelen@redhat.com>
e35e9c
Date: Mon, 20 Feb 2023 16:16:01 +0100
e35e9c
Subject: [PATCH] fips: Check return value from ftell
e35e9c
e35e9c
* src/fips.c (get_file_offset): Check return value of ftell to be able
e35e9c
  to detect errors.
e35e9c
--
e35e9c
e35e9c
Originally reported by coverity.
e35e9c
e35e9c
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
e35e9c
---
e35e9c
 src/fips.c | 2 ++
e35e9c
 1 file changed, 2 insertions(+)
e35e9c
e35e9c
diff --git a/src/fips.c b/src/fips.c
e35e9c
index 272aabae..0d89b6da 100644
e35e9c
--- a/src/fips.c
e35e9c
+++ b/src/fips.c
e35e9c
@@ -681,6 +681,8 @@ get_file_offset (FILE *fp, ElfW (Ehdr) *ehdr_p,
e35e9c
         return gpg_error_from_syserror ();
e35e9c
 
e35e9c
       off = ftell (fp);
e35e9c
+      if (off < 0)
e35e9c
+        return gpg_error_from_syserror ();
e35e9c
       if (shdr.sh_type == SHT_NOTE && shdr.sh_flags == 0 && shdr.sh_size == 48)
e35e9c
         {
e35e9c
           const char header_of_the_note[] = {
e35e9c
-- 
e35e9c
2.39.2
e35e9c