Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3936420yba; Tue, 23 Apr 2019 12:04:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqwPnTFpeQerd7vSQ66LQCdJJw33MLfoUuRMO0ZyM1y8zuNKu3oQH2OR0n6OahflaPTVsski X-Received: by 2002:a62:cfc4:: with SMTP id b187mr28471123pfg.130.1556046253993; Tue, 23 Apr 2019 12:04:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556046253; cv=none; d=google.com; s=arc-20160816; b=CribfOjONtMvobvqgHCmxFr+m4PgpSitxp32rawh0pQZiBJ0yPRXiGLzdoTLmKyoiy 6XSe9bIVVCe5XRu6aN5CN6ZEZbpqvCj5AqohC/+bKA5/W2/x/1GqydofylQAueVjhA2L /jwKfR7iJTyKD5IJIV5RqCvC5+tJs0zueusjHOut1UJr9W4rjeYyB6qzgsry1MtwKomw HWKSPSXs6o3AftHxYh9l1eBNrk/gxcbMK7Pn1uGZsRBiJpZaIEei9npMN0Am5cP5XTJw i/yO2ioWnmP006RtJeerkJelkDVazvYnYqyJHJ0jGvdN5qQyzR/kuP9hQL15/17LcSSx 5gEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=qfXy8+0qHmN8/L3YDNa3xi4FBFvK73vT67UVRpGiWKU=; b=Y5sbI/PmkHadk0HeMFPu9r5spQ0yAtGFImPoXnGR4p48ZHU19sCsfX0wayEY8MPmvk JxQKrCN0/tsuhwkBw7LncjcEZSetjAYYpw7j+RjBNw0u8CXtgxs4sK6DAsWHY+cqxw5K L2CDXv60AdZXcpty3v2lIstlgbWdbOcP51aG0H36sNMI6FZQNjAEwhohMMneQGhMYM0g ZlNVsCzXi+f75k1tH2VmxUFb2lDTncgHDPxm21DM/W194CgW2xIh3Uifw9T1GWuvwLru lpueELm640thj3RzaGgGR1nPXOwKHNenuCLQ7oTIE0FUtdGH+5S6yTYOcUmRpzopmHNP dJOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=MS7hMqRE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u4si15656813pga.245.2019.04.23.12.03.58; Tue, 23 Apr 2019 12:04:13 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=MS7hMqRE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726758AbfDWTCb (ORCPT + 99 others); Tue, 23 Apr 2019 15:02:31 -0400 Received: from mail-ot1-f68.google.com ([209.85.210.68]:34215 "EHLO mail-ot1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725945AbfDWTCa (ORCPT ); Tue, 23 Apr 2019 15:02:30 -0400 Received: by mail-ot1-f68.google.com with SMTP id h2so3373783otm.1 for ; Tue, 23 Apr 2019 12:02:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=qfXy8+0qHmN8/L3YDNa3xi4FBFvK73vT67UVRpGiWKU=; b=MS7hMqREmsPGWn+aQRIoeY+ZNGYMaYZB3oJTqkWWEbPmNRQeG3IafiO/lXspsz+Tkc EofF7uWJtjsh7pyQYUzRlLA+UZqLqz+JeuDd4zhT49oLgGg3u3MjV4+FydVZ5SZHUosq BQFfZln0Dcb5YbdTEhj79sCXGgG/bwpVZVfjifTMC+06pOoQFMGZ31zUHVRz3xW3iKEe x59YG4yBcSSHaLyyExiNeb3l/4b9mPibBwHQfvMyoUiJXYLa+k4wI2c3mtpATO8eLeiK alpbb89bkvI49Q1wTc5Ah+QOKXJuV4mLkseSS2iYbqp8VEj8fvekfYnnpzZzZDmNyFr8 AbKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=qfXy8+0qHmN8/L3YDNa3xi4FBFvK73vT67UVRpGiWKU=; b=h5xQUO6DqFphcOvgkEut1wJ5ByPJwImzEAfhjednecrTFBp0++1emUMpt4ks6lK4LZ HcLEfXktUtE75PDozV/WLBGWtaxFM//MrDEu93s1tgfT4S2Mw6l6AhRzZzC4evS599Ta KO98nIRfIf/s9ytDGxJnnatmeR+iWkGHy8vmnSJzP9+PPz7djzkP60RnysL17VnVDyqi AKkcnrlTtlRyHEdrg8L0jqb5DkvigmFnYLZ8uuYvVURjK8S6kTPs1cKYB7RFrh95AFuy 4zOYZM1AwK2nK0XRL9NP2WVc+no4X+PeDP3VbLRm7oYTQ7sZUMfs57SvaxVpCr4H39z6 +zog== X-Gm-Message-State: APjAAAWfkd5jusUdEjpdSPlxUqZ8sUYlfoKriwoWWhfZohRZcpOAfWjF bXc44EzCYoLhSZsGH2+6N/g9WKry1mlZLtu6KgHvQg== X-Received: by 2002:a9d:694c:: with SMTP id p12mr16793422oto.242.1556046149596; Tue, 23 Apr 2019 12:02:29 -0700 (PDT) MIME-Version: 1.0 References: <20190423181210.GA2443@beast> In-Reply-To: <20190423181210.GA2443@beast> From: Jann Horn Date: Tue, 23 Apr 2019 21:02:03 +0200 Message-ID: Subject: Re: [PATCH] binfmt_elf: Update READ_IMPLIES_EXEC logic for modern CPUs To: Kees Cook Cc: Andrew Morton , Hector Marco-Gisbert , Jason Gunthorpe , kernel list , "the arch/x86 maintainers" , Thomas Gleixner , Andy Lutomirski , Kernel Hardening , Mark Rutland , linux-arm-kernel@lists.infradead.org, musl@lists.openwall.com, Linux API Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org +linux-api, +musl On Tue, Apr 23, 2019 at 8:12 PM Kees Cook wrote: > The READ_IMPLIES_EXEC work-around was designed for old CPUs lacking NX > (to have the visible permission flags on memory regions reflect reality: > they are all executable), and for old toolchains that lacked the ELF > PT_GNU_STACK marking (under the assumption than toolchains that couldn't > even specify memory protection flags may have it wrong for all memory > regions). > > This logic is sensible, but was implemented in a way that equated having > a PT_GNU_STACK marked executable as being as "broken" as lacking the > PT_GNU_STACK marking entirely. This is not a reasonable assumption > for CPUs that have had NX support from the start (or very close to > the start). This confusion has led to situations where modern 64-bit > programs with explicitly marked executable stack are forced into the > READ_IMPLIES_EXEC state when no such thing is needed. (And leads to > unexpected failures when mmap()ing regions of device driver memory that > wish to disallow VM_EXEC[1].) > > To fix this, elf_read_implies_exec() is adjusted on arm64 (where NX has > always existed and all toolchains include PT_GNU_STACK), and x86 is > adjusted to handle this combination of possible outcomes: > > CPU: | lacks NX | has NX, ia32 | has NX, x86_64 | > ELF: | | | | > ------------------------------|------------------|------------------| > missing GNU_STACK | needs RIE | needs RIE | no RIE | > GNU_STACK == RWX | needs RIE | no RIE: stack X | no RIE: stack X | > GNU_STACK == RW | needs RIE | no RIE: stack NX | no RIE: stack NX | > > This has the effect of making binfmt_elf's EXSTACK_DEFAULT actually take > on the correct architecture default of being non-executable on arm64 and > x86_64, and being executable on ia32. It's probably worth going a bit more into detail in this description on how libraries typically allocate thread stacks. It looks like glibc will be fine; before commit 54ee14b3882 (https://sourceware.org/git/?p=glibc.git;a=blobdiff;f=nptl/allocatestack.c;h=dc501650b8629eda4502f2016016f09106cfb526;hp=6ada1fe1381de104153c0627e27f09fe5ad02caa;hb=54ee14b3882;hpb=16a76cd23ce9d3924fa192395e730423e3dc8b36), thread stacks were always RWX, and since then, from what I can tell, thread stacks were executable depending on the executable's ELF headers (as parsed by glibc). But e.g. musl's __pthread_create() seems to hardcode PROT_READ|PROT_WRITE, which I think would mean that if someone built a multithreaded program with nested functions and linked with musl, that program would stop working? Or maybe I'm just reading the code wrong. Then again, I'm not sure whether anyone actually uses nested functions...