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

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