mailing list of musl libc
 help / color / mirror / code / Atom feed
From: "Alex Xu (Hello71)" <alex_y_xu@yahoo.ca>
To: musl@lists.openwall.com
Cc: "Alex Xu (Hello71)" <alex_y_xu@yahoo.ca>
Subject: [musl] [PATCH] before __dls2b, call mprotect with __syscall
Date: Tue, 29 Mar 2022 22:11:48 -0400	[thread overview]
Message-ID: <20220330021148.520601-1-alex_y_xu@yahoo.ca> (raw)
In-Reply-To: <20220330021148.520601-1-alex_y_xu.ref@yahoo.ca>

if LTO is enabled, gcc hoists the call to ___errno_location outside the
loop even though the access to errno is gated behind head != &ldso
because ___errno_location is marked __attribute__((const)). this causes
the program to crash because TLS is not yet initialized when called from
__dls2. this is also possible if LTO is not enabled; even though gcc 11
doesn't do it, it is still wrong to use errno here.

since the start and end are already aligned, we can simply call
__syscall instead of using global errno.

Fixes: e13a2b8953ef ("implement PT_GNU_RELRO support")
---

I think this doesn't necessarily need to go in the next release, just
sending it out for review.

 ldso/dynlink.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/ldso/dynlink.c b/ldso/dynlink.c
index 5b9c8be4..b5a8f1f2 100644
--- a/ldso/dynlink.c
+++ b/ldso/dynlink.c
@@ -1356,12 +1356,14 @@ static void reloc_all(struct dso *p)
 		do_relocs(p, laddr(p, dyn[DT_REL]), dyn[DT_RELSZ], 2);
 		do_relocs(p, laddr(p, dyn[DT_RELA]), dyn[DT_RELASZ], 3);
 
-		if (head != &ldso && p->relro_start != p->relro_end &&
-		    mprotect(laddr(p, p->relro_start), p->relro_end-p->relro_start, PROT_READ)
-		    && errno != ENOSYS) {
-			error("Error relocating %s: RELRO protection failed: %m",
-				p->name);
-			if (runtime) longjmp(*rtld_fail, 1);
+		if (head != &ldso && p->relro_start != p->relro_end) {
+			long ret = __syscall(SYS_mprotect, laddr(p, p->relro_start),
+				p->relro_end-p->relro_start, PROT_READ);
+			if (ret != 0 && ret != -ENOSYS) {
+				error("Error relocating %s: RELRO protection failed: %m",
+					p->name);
+				if (runtime) longjmp(*rtld_fail, 1);
+			}
 		}
 
 		p->relocated = 1;
-- 
2.35.1


       reply	other threads:[~2022-03-30  2:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20220330021148.520601-1-alex_y_xu.ref@yahoo.ca>
2022-03-30  2:11 ` Alex Xu (Hello71) [this message]
2022-07-02 20:16   ` Rich Felker

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=20220330021148.520601-1-alex_y_xu@yahoo.ca \
    --to=alex_y_xu@yahoo.ca \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).