aboutsummaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/rpm/rpm/debugedit-segv.patch
blob: 585cf12fd8463befafff43462c3a930adf3a512b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
During the recalculation of the buildid, it's necessary to change the word
back to the original endian.  However, if we do this in-place, we've also
affected the headers that we're also working on.  The side effect of this is
we can no longer rely on 'sh_type' as it may have been changed.

This patch ensures that any time we translate the loaded data to the machine
format, we only do it in a backup copy and never the original copy.

Note: in all other places a backup copy was used, just not buildid processing.

Also the process (...) function was modified to verify the data is not
NULL as well.  This is an extra check and is not strictly necessary.

Upstream-Status: Pending

Signed-off-by: Mark Hatle <mark.hatle@windriver.com>

Index: rpm/tools/debugedit.c
===================================================================
--- rpm.orig/tools/debugedit.c
+++ rpm/tools/debugedit.c
@@ -1403,7 +1403,8 @@ static inline void process (hashFunction
 		const void *data, size_t size)
 {
     memchunk chunk = { .data = (void *) data, .size = size };
-    hashFunctionContextUpdateMC (ctx, &chunk);
+    if (data != NULL && size != 0)
+      hashFunctionContextUpdateMC (ctx, &chunk);
 }
 
 /* Compute a fresh build ID bit-string from the editted file contents.  */
@@ -1456,14 +1457,16 @@ handle_build_id (DSO *dso, Elf_Data *bui
       GElf_Ehdr ehdr;
       GElf_Phdr phdr;
       GElf_Shdr shdr;
-    } u;
-    Elf_Data x = { .d_version = EV_CURRENT, .d_buf = &u };
-
-    x.d_type = ELF_T_EHDR;
-    x.d_size = sizeof u.ehdr;
-    u.ehdr = dso->ehdr;
-    u.ehdr.e_phoff = u.ehdr.e_shoff = 0;
-    if (elf64_xlatetom (&x, &x, dso->ehdr.e_ident[EI_DATA]) == NULL)
+    } u1, u2;
+    Elf_Data src  = { .d_version = EV_CURRENT, .d_buf = &u1 };
+    Elf_Data dest = { .d_version = EV_CURRENT, .d_buf = &u2 };
+
+    src.d_type = ELF_T_EHDR;
+    src.d_size = sizeof u1.ehdr;
+    dest.d_size = sizeof u2.ehdr;
+    u1.ehdr = dso->ehdr;
+    u1.ehdr.e_phoff = u1.ehdr.e_shoff = 0;
+    if (elf64_xlatetom (&dest, &src, dso->ehdr.e_ident[EI_DATA]) == NULL)
       {
       bad:
 	fprintf (stderr, "Failed to compute header checksum: %s\n",
@@ -1471,29 +1474,31 @@ handle_build_id (DSO *dso, Elf_Data *bui
 	exit (1);
       }
 
-    x.d_type = ELF_T_PHDR;
-    x.d_size = sizeof u.phdr;
+    src.d_type = ELF_T_PHDR;
+    src.d_size = sizeof u1.phdr;
+    dest.d_size = sizeof u2.phdr;
     for (i = 0; i < dso->ehdr.e_phnum; ++i)
       {
-	if (gelf_getphdr (dso->elf, i, &u.phdr) == NULL)
+	if (gelf_getphdr (dso->elf, i, &u1.phdr) == NULL)
 	  goto bad;
-	if (elf64_xlatetom (&x, &x, dso->ehdr.e_ident[EI_DATA]) == NULL)
+	if (elf64_xlatetom (&dest, &src, dso->ehdr.e_ident[EI_DATA]) == NULL)
 	  goto bad;
-	process (&ctx, x.d_buf, x.d_size);
+	process (&ctx, dest.d_buf, dest.d_size);
       }
 
-    x.d_type = ELF_T_SHDR;
-    x.d_size = sizeof u.shdr;
+    src.d_type = ELF_T_SHDR;
+    src.d_size = sizeof u1.shdr;
+    dest.d_size = sizeof u2.shdr;
     for (i = 0; i < dso->ehdr.e_shnum; ++i)
       if (dso->scn[i] != NULL)
 	{
-	  u.shdr = dso->shdr[i];
-	  u.shdr.sh_offset = 0;
-	  if (elf64_xlatetom (&x, &x, dso->ehdr.e_ident[EI_DATA]) == NULL)
+	  u1.shdr = dso->shdr[i];
+	  u1.shdr.sh_offset = 0;
+	  if (elf64_xlatetom (&dest, &src, dso->ehdr.e_ident[EI_DATA]) == NULL)
 	    goto bad;
-	  process (&ctx, x.d_buf, x.d_size);
+	  process (&ctx, dest.d_buf, dest.d_size);
 
-	  if (u.shdr.sh_type != SHT_NOBITS)
+	  if (u1.shdr.sh_type != SHT_NOBITS)
 	    {
 	      Elf_Data *d = elf_rawdata (dso->scn[i], NULL);
 	      if (d == NULL)