1*4882a593SmuzhiyunFrom cb8326de54ad7a56658b0dc8efb7da5e71684a7c Mon Sep 17 00:00:00 2001
2*4882a593SmuzhiyunFrom: Pablo Galindo <pablogsal@gmail.com>
3*4882a593SmuzhiyunDate: Tue, 22 Sep 2020 01:33:47 +0100
4*4882a593SmuzhiyunSubject: [PATCH] Use sh_offset instead of sh_addr when checking already
5*4882a593Smuzhiyun replaced libs
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunWhen checking for already replaced libs, the check against the size must
8*4882a593Smuzhiyunbe done using the section header offset, not the section file address.
9*4882a593SmuzhiyunThis was not crashing in many situations because normally sh_address and
10*4882a593Smuzhiyunsh_offset have the same value but these two may differ and using the
11*4882a593Smuzhiyunsh_address value instead can cause library corruption in these
12*4882a593Smuzhiyunsituations.
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunFetch from: https://github.com/NixOS/patchelf/commit/83aa89addf8757e2d63aa73222f2fa9bc6d7321a
15*4882a593Smuzhiyun
16*4882a593SmuzhiyunBackported to v0.9
17*4882a593Smuzhiyun
18*4882a593SmuzhiyunSigned-off-by: Conrad Ratschan <conrad.ratschan@rockwellcollins.com>
19*4882a593Smuzhiyun---
20*4882a593Smuzhiyun src/patchelf.cc | 2 +-
21*4882a593Smuzhiyun 1 file changed, 1 insertion(+), 1 deletion(-)
22*4882a593Smuzhiyun
23*4882a593Smuzhiyundiff --git a/src/patchelf.cc b/src/patchelf.cc
24*4882a593Smuzhiyunindex 4676157..c025ae2 100644
25*4882a593Smuzhiyun--- a/src/patchelf.cc
26*4882a593Smuzhiyun+++ b/src/patchelf.cc
27*4882a593Smuzhiyun@@ -666,7 +666,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
28*4882a593Smuzhiyun     /* Some sections may already be replaced so account for that */
29*4882a593Smuzhiyun     unsigned int i = 1;
30*4882a593Smuzhiyun     Elf_Addr pht_size = sizeof(Elf_Ehdr) + (phdrs.size() + 1)*sizeof(Elf_Phdr);
31*4882a593Smuzhiyun-    while( shdrs[i].sh_addr <= pht_size && i < rdi(hdr->e_shnum) ) {
32*4882a593Smuzhiyun+    while( shdrs[i].sh_offset <= pht_size && i < rdi(hdr->e_shnum) ) {
33*4882a593Smuzhiyun         if (not haveReplacedSection(getSectionName(shdrs[i])))
34*4882a593Smuzhiyun             replaceSection(getSectionName(shdrs[i]), shdrs[i].sh_size);
35*4882a593Smuzhiyun         i++;
36*4882a593Smuzhiyun--
37*4882a593Smuzhiyun2.17.1
38*4882a593Smuzhiyun
39