public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Joel Molin <joelmolin@gmail.com>
To: libc-help@sourceware.org
Subject: Why does _dl_protect_relro align the end address down?
Date: Mon, 16 Jan 2023 13:26:28 -0800	[thread overview]
Message-ID: <CACXmTyJ7qtzNVBGBOn5bgsvV0VQZEquiP+5tMqAdxXxp3H48xg@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1911 bytes --]

Hi all,

I'm wondering if there's a reason why _dl_protect_relro aligns the end
address down, and whether it wouldn't be a pure improvement to align it up?

I've copied the relevant part of it below for reference (at
569cfcc6bf35c28112ca8d7112e9eb4a22bed5b8), but the effect of it is:
 - when start and end are on the same page, nothing is protected.
 - if end is not on a page boundary, parts of the region intended for
protection is left unprotected.

Notably this behavior is entirely silent, making it easy to believe that
you have hardened your executable when in fact you have not. This seems
very brittle.

I have no real world issue. I was writing a linker for fun, and was putting
my relro segment at the start of a page, it seemed the straightforward
thing to do. GNU ld 2.39.0 instead puts the relro segment at the end of a
page (I'm not familiar enough to know whether this is a cause, effect, or
unrelated).

I did however do some looking around and found
https://bugs.launchpad.net/ubuntu/+source/binutils/+bug/1412553, so it
seems like this has caused problems for projects with users.

Since mprotect will protect entire pages anyway, wouldn't it make a lot
more sense here to say `end = ALIGN_UP(...)`? Apart from feeling more
intuitive, it also seems like it would avoid page size inconsistencies
between runtime and link editing like the one above.

I couldn't really find any documentation about this that spelled out the
"align at end" requirement, but I'm happy to be proven wrong.

Happy to provide more examples if desired.

Thanks,
Joel

```
void
_dl_protect_relro (struct link_map *l)
{
  ElfW(Addr) start = ALIGN_DOWN((l->l_addr
+ l->l_relro_addr),
GLRO(dl_pagesize));
  ElfW(Addr) end = ALIGN_DOWN((l->l_addr
      + l->l_relro_addr
      + l->l_relro_size),
     GLRO(dl_pagesize));
  if (start != end
      && __mprotect ((void *) start, end - start, PROT_READ) < 0)
    {
...
```

             reply	other threads:[~2023-01-16 21:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-16 21:26 Joel Molin [this message]
2023-01-17  7:57 ` Florian Weimer
2023-01-17 14:11   ` Adhemerval Zanella Netto
2023-01-17 14:16     ` Florian Weimer
2023-01-17 14:30       ` Adhemerval Zanella Netto
2023-01-17 15:48         ` H.J. Lu
2023-01-17 15:56           ` Joel Molin

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=CACXmTyJ7qtzNVBGBOn5bgsvV0VQZEquiP+5tMqAdxXxp3H48xg@mail.gmail.com \
    --to=joelmolin@gmail.com \
    --cc=libc-help@sourceware.org \
    /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).