public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
* Why does _dl_protect_relro align the end address down?
@ 2023-01-16 21:26 Joel Molin
  2023-01-17  7:57 ` Florian Weimer
  0 siblings, 1 reply; 7+ messages in thread
From: Joel Molin @ 2023-01-16 21:26 UTC (permalink / raw)
  To: libc-help

[-- 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)
    {
...
```

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-01-17 15:56 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-16 21:26 Why does _dl_protect_relro align the end address down? Joel Molin
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

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).