public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Topi Miettinen <toiwoton@gmail.com>
To: Siddhesh Poyarekar <siddhesh@gotplt.org>, libc-alpha@sourceware.org
Subject: Re: Randomize offset between program segments?
Date: Wed, 21 Oct 2020 13:44:25 +0300	[thread overview]
Message-ID: <c6599480-e8ee-0782-086f-e7d0750c8b97@gmail.com> (raw)
In-Reply-To: <c2e87d0b-3a0f-dc2c-0771-cadcf15dda5e@gotplt.org>

On 21.10.2020 12.54, Siddhesh Poyarekar wrote:
> On 10/21/20 3:04 PM, Topi Miettinen wrote:
>> Why can't the dynamic linker calculate the offset?
>>
> 
> It can calculate, but to be able to patch the pc-relative load
> instructions it will need the executable section to also be writable and
> is a really bad idea.

Agreed, I didn't consider that.

> The alternative (which is what PIC does for global variables) is to have
> a GOT-like indirection, where instead of the single pc-relative load,
> the compiler emits a load from that table and a subsequent load from the
> address in GOT.  Here, patching by the dynamic linker is safe since the
> offset table is rw, but you will have doubled the number of instructions
> needed to access your data.

Also size of GOT will increase. Otherwise this seems a better approach.

> Hence the question: how much benefit does this provide on top of what is
> achieved by randomizing the base address and does it justify doubling
> the number of instructions to access static variables?

I don't know. What would be the method to quantify such benefits? This 
applies to a specific case where the attacker is able to determine an 
address in one segment but needs to find an address in another segment 
in order to win, and without ASLR the offset between the addresses would 
be always known by the attacker (for example, because the distro and the 
version for the program or library is known). Without ASLR, chance of 
winning is 100%. With ASLR, this could be related to number of bits in 
randomization. In the 32 bit offset case this would be 20 bits (assuming 
12 bits page size), so the chances of guessing would be 2^-20 and brute 
forcing the offset would be expected to take 2^20/2 attempts.

For the large model, ASLR could use 44 - 12 = 32 bits, so numbers would 
be 2^-32 and 2^32/2.

-mcmodel=large increases number of instructions by a factor of 5, so 
doubling would still be an improvement:
0000000000000000 <setter>:
    0:   48 8d 05 f9 ff ff ff    lea    -0x7(%rip),%rax        # 0 <setter>
    7:   49 bb 00 00 00 00 00    movabs $0x0,%r11
    e:   00 00 00
   11:   4c 01 d8                add    %r11,%rax
   14:   48 ba 00 00 00 00 00    movabs $0x0,%rdx
   1b:   00 00 00
   1e:   48 89 3c 02             mov    %rdi,(%rdx,%rax,1)
   22:   c3                      retq

Normal model, extern variable should be similar to GOT access:
0000000000000000 <setter>:
    0:   48 8b 05 00 00 00 00    mov    0x0(%rip),%rax        # 7 
<setter+0x7>
    7:   48 89 38                mov    %rdi,(%rax)
    a:   c3                      retq

Normal model, static variable:
0000000000000000 <setter>:
    0:   48 89 3d 00 00 00 00    mov    %rdi,0x0(%rip)        # 7 
<setter+0x7>
    7:   c3                      retq

But I suppose the extra memory access in GOT version is worse for 
performance than the extra instructions which don't access memory in 
-mcmodel=large.

-Topi

  reply	other threads:[~2020-10-21 10:44 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-19 15:31 Topi Miettinen
2020-10-21  2:21 ` Siddhesh Poyarekar
2020-10-21  7:40   ` Topi Miettinen
2020-10-21  9:14     ` Siddhesh Poyarekar
2020-10-21  9:34       ` Topi Miettinen
2020-10-21  9:54         ` Siddhesh Poyarekar
2020-10-21 10:44           ` Topi Miettinen [this message]
2020-10-29  8:04             ` Siddhesh Poyarekar
2020-10-30 15:37               ` Topi Miettinen
2020-10-21  5:33 ` Florian Weimer
2020-10-21  8:02   ` Topi Miettinen
2020-10-21  8:06     ` Florian Weimer
2020-10-21  9:28       ` Topi Miettinen

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=c6599480-e8ee-0782-086f-e7d0750c8b97@gmail.com \
    --to=toiwoton@gmail.com \
    --cc=libc-alpha@sourceware.org \
    --cc=siddhesh@gotplt.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).