From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by sourceware.org (Postfix) with ESMTPS id E87C93857409 for ; Wed, 11 May 2022 18:17:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E87C93857409 Received: by mail-pj1-x102c.google.com with SMTP id j10-20020a17090a94ca00b001dd2131159aso5705151pjw.0 for ; Wed, 11 May 2022 11:17:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=TuF/471O1Vor2a5AuduCVoz3L9wJUGGkZ0wxQPnxOtY=; b=WDkYKELLYcFJB01R+EQqRQTL8qO/OZ+H9n2wFPaG6qGSZiqKkDVTyRhFV6LTxnl9X0 2WHiIOWXCEg4GErh9Af5/OUVn2RSUOHTv0QYj0pjtvlGUIwgalxrsQEDj7ywwgvkGkP6 NXeOjxLLpRoa6+Y2hKITpnbXtjSDeZzhWWfK47JofsMQcZNBTSqRMy/3NTQGIqX2OyKX /go2IWKpsLIjkXDwm3iEGtydzjmdJJc+IIuQ1pP8G3vY74RLzuoqrEs/m9HY2ntw/9Ng ouch8vySLIvEqMpCDUiEGJ25mWcjpdCU4z/HiABX3XObVo43HEImaYbywJR7jr7Qvbwn DYXA== X-Gm-Message-State: AOAM530fTzzotcJk07xIuPbvFaYW9DGfoD30LGIFxnT3dkeF2iZJCBcl 9n88/Gae6qe5KprGgICrhhaIcAq+UN8HQw== X-Google-Smtp-Source: ABdhPJyqhqdZzib7wGtnXlHiaRg02cmaVvkKs1qHABOn/DI4b3CRrOW4awZWgkDmuS9hSX2/lCTBqQ== X-Received: by 2002:a17:90b:35cb:b0:1dc:7905:c4c1 with SMTP id nb11-20020a17090b35cb00b001dc7905c4c1mr6709744pjb.95.1652293027650; Wed, 11 May 2022 11:17:07 -0700 (PDT) Received: from google.com ([2620:15c:2ce:200:3228:6d2d:ebc8:7bc1]) by smtp.gmail.com with ESMTPSA id j21-20020a62e915000000b005104c6d7941sm2034407pfh.31.2022.05.11.11.17.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 11:17:07 -0700 (PDT) Date: Wed, 11 May 2022 11:17:04 -0700 From: Fangrui Song To: Florian Weimer Cc: "H.J. Lu" , libc-alpha@sourceware.org, binutils@sourceware.org Subject: Re: PT_GNU_RELRO is somewhat broken Message-ID: <20220511181704.y4pldvlqnbix3p53@google.com> References: <871qx0dmz5.fsf@oldenburg.str.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-19.8 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 May 2022 18:17:11 -0000 On 2022-05-11, H.J. Lu via Libc-alpha wrote: >On Wed, May 11, 2022 at 9:59 AM Florian Weimer via Libc-alpha > wrote: >> >> PT_GNU_RELRO is supposed to identify a region in the process image which >> has to be flipped to PROT_READ (only) permission after relocation >> (“Read-Only after RELocation”). >> >> glibc has this code in the dynamic loader in elf/dl-reloc.c: >> >> | 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) >> | { >> | static const char errstring[] = N_("\ >> | cannot apply additional memory protection after relocation"); >> | _dl_signal_error (errno, l->l_name, NULL, errstring); >> | } >> | } >> >> I assume the intent is to conservatively apply the largest possible >> RELRO region given GLRO(dl_pagesize), the run-time page size reported by >> the kernel. If the binary is built to a smaller page size (to save disk >> space), glibc can still load it, but apply only some RELRO protection. >> But _dl_relocate_object has a bug: to be conservative, it would have to >> use ALGIN_UP for the start (lower) address of the range. >> >> But it turns out we can't make this change without incurring a loss of >> hardening: BFD ld does not align the start address to a page boundary. >> For example, /bin/true in Fedora 35 x86-64 has this: >> >> | $ readelf -l /bin/true >> | >> | Elf file type is DYN (Position-Independent Executable file) >> | Entry point 0x1960 >> | There are 13 program headers, starting at offset 64 >> | >> | Program Headers: >> | Type Offset VirtAddr PhysAddr >> | FileSiz MemSiz Flags Align >> | PHDR 0x0000000000000040 0x0000000000000040 0x0000000000000040 >> | 0x00000000000002d8 0x00000000000002d8 R 0x8 >> | INTERP 0x0000000000000318 0x0000000000000318 0x0000000000000318 >> | 0x000000000000001c 0x000000000000001c R 0x1 >> | [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2] >> | LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000 >> | 0x0000000000000ff8 0x0000000000000ff8 R 0x1000 >> | LOAD 0x0000000000001000 0x0000000000001000 0x0000000000001000 >> | 0x00000000000029a1 0x00000000000029a1 R E 0x1000 >> | LOAD 0x0000000000004000 0x0000000000004000 0x0000000000004000 >> | 0x0000000000000d38 0x0000000000000d38 R 0x1000 >> | LOAD 0x0000000000005c78 0x0000000000006c78 0x0000000000006c78 >> | 0x0000000000000390 0x00000000000003a0 RW 0x1000 >> | DYNAMIC 0x0000000000005c90 0x0000000000006c90 0x0000000000006c90 >> | 0x00000000000001f0 0x00000000000001f0 RW 0x8 >> | NOTE 0x0000000000000338 0x0000000000000338 0x0000000000000338 >> | 0x0000000000000050 0x0000000000000050 R 0x8 >> | NOTE 0x0000000000000388 0x0000000000000388 0x0000000000000388 >> | 0x0000000000000044 0x0000000000000044 R 0x4 >> | GNU_PROPERTY 0x0000000000000338 0x0000000000000338 0x0000000000000338 >> | 0x0000000000000050 0x0000000000000050 R 0x8 >> | GNU_EH_FRAME 0x00000000000049c4 0x00000000000049c4 0x00000000000049c4 >> | 0x000000000000007c 0x000000000000007c R 0x4 >> | GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000 >> | 0x0000000000000000 0x0000000000000000 RW 0x10 >> | GNU_RELRO 0x0000000000005c78 0x0000000000006c78 0x0000000000006c78 >> | 0x0000000000000388 0x0000000000000388 R 0x1 >> | […] >> >> The virtual address for PT_GNU_RELRO is 0x388, which is definitely not >> aligned to a 4K page. (0x388 + 0x6c78 == 0x7000, so at least the end >> address is aligned.) In practice, this seems to work because the RELRO >> area seems to be at the start of the RW LOAD segment, so we can safely >> flip the slack space at the start of the page to RO. It still looks >> like a major wart to me, though. > >After relocation, we change the end of the RO segment (aligned down from >the beginning of the RELRO area) to the end of the RELRO segment to RO. >Since the end of the RELRO segment must be aligned to the page size, >ALIGN_DOWN on the end of the RELRO segment doesn't lose any protection. > >> Any suggestions what should we do to fix this properly, mainly for >> targets that have varying page size in practice? > >The end of the RELRO segment should be aligned to the maximum page >size. > PT_GNU_RELRO is designed/implemented this way: * there can be at most one PT_GNU_RELRO * p_vaddr(PT_GNU_RELRO) = p_vaddr(first RW PT_LOAD); https://sourceware.org/binutils/docs/ld/Builtin-Functions.html DATA_SEGMENT_RELRO_END is designed this way * p_vaddr(PT_GNU_RELRO) + p_memsz(PT_GNU_RELRO) is aligned by common-page-size. comon page size is chosen probably because of less waste If the proposal is to align p_vaddr(PT_GNU_RELRO) + p_memsz(PT_GNU_RELRO) to max page size, that will penalize the size of many max-page-size>4096 ports with the current GNU ld section/segment layout. See https://sourceware.org/bugzilla/show_bug.cgi?id=24490 and https://sourceware.org/bugzilla/show_bug.cgi?id=23704 for GNU ld's -z separate-code complaints. Note: ld.lld used (before 9.0.0) to place PT_GNU_RELRO in the middle of the RW PT_LOAD. I changed it to the start in https://reviews.llvm.org/D58892 With the new scheme, it doesn't really matter whether p_vaddr(PT_GNU_RELRO) + p_memsz(PT_GNU_RELRO) is aligned to max-page-size or common-page-size: the file size does not change.