From: liuzhensong <liuzhensong@loongson.cn>
To: Xi Ruoyao <xry111@xry111.site>, binutils@sourceware.org
Cc: Lulu Cheng <chenglulu@loongson.cn>, Wang Xuerui <i@xen0n.name>,
Chenghua Xu <xuchenghua@loongson.cn>,
mengqinggang@loongson.cn
Subject: Re: [PATCH 1/2] LoongArch: Avoid heap-buffer-overflow in loongarch_elf_relocate_section
Date: Wed, 14 Sep 2022 16:57:33 +0800 [thread overview]
Message-ID: <b4d228b5-2742-a75f-432b-431feb815c14@loongson.cn> (raw)
In-Reply-To: <20220913154414.554861-2-xry111@xry111.site>
[-- Attachment #1: Type: text/plain, Size: 3868 bytes --]
在 2022/9/13 下午11:44, Xi Ruoyao 写道:
> If a and b are different sections, we cannot access something in b with
> "a->contents + (offset from a)" because "a->contents" and "b->contents"
> are heap buffers allocated separately, not slices of a large buffer.
>
> The issue was found during an attempt to add static-pie support to the
> toolchain with ASAN.
Can you provide compile parameters?
> ---
> bfd/elfnn-loongarch.c | 18 ++++++++----------
> 1 file changed, 8 insertions(+), 10 deletions(-)
>
> diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
> index ed42b8b6770..4b408b1db72 100644
> --- a/bfd/elfnn-loongarch.c
> +++ b/bfd/elfnn-loongarch.c
> @@ -3128,6 +3128,7 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
> unresolved_reloc = false;
> BFD_ASSERT (rel->r_addend == 0);
>
> + asection *my_got = got;
> bfd_vma got_off = 0;
> if (h != NULL)
> {
> @@ -3145,17 +3146,14 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
> {
> idx = (h->plt.offset - PLT_HEADER_SIZE)
> / PLT_ENTRY_SIZE;
> - got_off = sec_addr (htab->elf.sgotplt)
> - + GOTPLT_HEADER_SIZE
> - + (idx * GOT_ENTRY_SIZE)
> - - sec_addr (htab->elf.sgot);
> + my_got = htab->elf.sgotplt;
> + got_off = GOTPLT_HEADER_SIZE + idx * GOT_ENTRY_SIZE;
> }
> else
> {
> idx = h->plt.offset / PLT_ENTRY_SIZE;
> - got_off = sec_addr (htab->elf.sgotplt)
> - + (idx * GOT_ENTRY_SIZE)
> - - sec_addr (htab->elf.sgot);
> + my_got = htab->elf.sgotplt;
> + got_off = idx * GOT_ENTRY_SIZE;
> }
> }
>
> @@ -3172,7 +3170,7 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
> && SYMBOL_REFERENCES_LOCAL (info, h))
> {
> Elf_Internal_Rela rela;
> - rela.r_offset = sec_addr (got) + got_off;
> + rela.r_offset = sec_addr (my_got) + got_off;
> rela.r_info = ELFNN_R_INFO (0, R_LARCH_RELATIVE);
> rela.r_addend = relocation;
> loongarch_elf_append_rela (output_bfd,
> @@ -3202,9 +3200,9 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
> }
> }
>
> - bfd_put_NN (output_bfd, relocation, got->contents + got_off);
> + bfd_put_NN (output_bfd, relocation, my_got->contents + got_off);
>
> - relocation = got_off + sec_addr (got);
> + relocation = got_off + sec_addr (my_got);
> }
>
> if (r_type == R_LARCH_GOT_PC_HI20)
This may be the reason of overflow.
Shouldn't write to got table when using hidden ifunc.
diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index ed42b8b6770..9278faa91aa 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -3179,6 +3179,7 @@ loongarch_elf_relocate_section (bfd *output_bfd,
struct bfd_link_info *info,
htab->elf.srelgot, &rela);
}
h->got.offset |= 1;
+ bfd_put_NN (output_bfd, relocation, got->contents
+ got_off);
}
}
else
@@ -3200,10 +3201,9 @@ loongarch_elf_relocate_section (bfd *output_bfd,
struct bfd_link_info *info,
}
local_got_offsets[r_symndx] |= 1;
}
+ bfd_put_NN (output_bfd, relocation, got->contents +
got_off);
}
- bfd_put_NN (output_bfd, relocation, got->contents + got_off);
-
relocation = got_off + sec_addr (got);
}
next prev parent reply other threads:[~2022-09-14 8:57 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-13 15:44 [PATCH 0/2] LoongArch: Fix two bugs breaking IFUNC in static PIE Xi Ruoyao
2022-09-13 15:44 ` [PATCH 1/2] LoongArch: Avoid heap-buffer-overflow in loongarch_elf_relocate_section Xi Ruoyao
2022-09-14 8:57 ` liuzhensong [this message]
2022-09-14 10:15 ` Xi Ruoyao
2022-09-14 11:15 ` Xi Ruoyao
2022-09-15 1:47 ` liuzhensong
2022-09-15 2:56 ` Xi Ruoyao
2022-09-15 3:54 ` liuzhensong
2022-09-15 6:13 ` Xi Ruoyao
2022-09-13 15:44 ` [PATCH 2/2] LoongArch: Fix R_LARCH_IRELATIVE insertion after elf_link_sort_relocs Xi Ruoyao
2022-09-15 13:03 ` liuzhensong
2022-09-16 20:11 ` Xi Ruoyao
2022-09-18 9:58 ` Xi Ruoyao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b4d228b5-2742-a75f-432b-431feb815c14@loongson.cn \
--to=liuzhensong@loongson.cn \
--cc=binutils@sourceware.org \
--cc=chenglulu@loongson.cn \
--cc=i@xen0n.name \
--cc=mengqinggang@loongson.cn \
--cc=xry111@xry111.site \
--cc=xuchenghua@loongson.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).