From nobody Mon Dec 15 09:46:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1601676400; cv=none; d=zohomail.com; s=zohoarc; b=gxCH8YN9kWblSmJ5+UNi5NPWfxkJfNLTVoRglH+RmXHcNufXGlcbt8L4+rm28NDRxRw29qlHnO5nSQoZNZLEqobv98R2x3Dg4dGa4M6eDkX8LST/8B9np97cggBNWBAEGUJYBTDcheufU9O/pBT7D1WaGgFn1/ZbvrGPpL0JG7o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601676400; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=UmXRKUbKgXmhfW+77FLjDFzejAZSQt8OzgtpBP6qGqY=; b=RnOyT4ARs/+PHLGQfSmx156Z+wus35dCyrSbOVm9D9SPECGZ+QumZxNGAnIeSiKBHSjWolkY6teWgosNn5J7vVKXTKMJDNtpPDLNuvZQOBn5TMWp35mKxwubE86cE7yLtDKQ5XH5WuhRZjoRSO0fPhkqkWo8xbc2XTBUviPbwuo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 160167640080283.6027226607282; Fri, 2 Oct 2020 15:06:40 -0700 (PDT) Received: from localhost ([::1]:41936 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kOTCJ-0001pr-Fa for importer@patchew.org; Fri, 02 Oct 2020 18:06:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35068) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kOT64-0002lC-LQ for qemu-devel@nongnu.org; Fri, 02 Oct 2020 18:00:12 -0400 Received: from mail-ot1-x344.google.com ([2607:f8b0:4864:20::344]:45134) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kOT62-0007Po-18 for qemu-devel@nongnu.org; Fri, 02 Oct 2020 18:00:12 -0400 Received: by mail-ot1-x344.google.com with SMTP id g96so2861603otb.12 for ; Fri, 02 Oct 2020 15:00:09 -0700 (PDT) Received: from localhost.localdomain (fixed-187-189-51-144.totalplay.net. [187.189.51.144]) by smtp.gmail.com with ESMTPSA id k10sm684268ots.60.2020.10.02.15.00.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Oct 2020 15:00:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UmXRKUbKgXmhfW+77FLjDFzejAZSQt8OzgtpBP6qGqY=; b=BP9MX9OcYQH2u/z17O9RrczsPMQ4PpIEB8DQmSSOx1zKmX/5WSpt/Ojh4Y3NO7nB63 33789GG/tht7XCnfIGeByLbuMdrT0jiwA2xd00qJOXP2VQUAXV7xKu9++1L3v2JdnfnE BPDkUWSorIPaPcYfTEJKlTNuM9gMcekeOl2C81xfdF4GBjCfqnzx2mQsWMognZHs9XwS OnPkQbzqYhla/23ucPpCmhpTGeVY2NUfrqop1MWtEImjf0sdleiQcYcC6XKVpivs8agz YDUNJ3VR+QHNKJygy857y4Yvt0omcIFPZyXeigtJh6LLOJxkgrfjcMv86zT+MhrFEDSm fZ3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UmXRKUbKgXmhfW+77FLjDFzejAZSQt8OzgtpBP6qGqY=; b=oZ8FXl8qEq0TYQjYQrCCLulSpViGFKWYsXBSixhPQixsdmIDCPXab2MrzoHlDKLbo9 zJGLKo29wdrKo5ybmt+X+RRFT8xn8SdHSLfp7TQJtEMfQ+NfOayCBvRTQqI0bjB+ouw/ iVNejvU6P5YwCUbRvfVd/mV5GGiAnkFVtBDiX0aVkj3iGlaqvnh8bi91MVDpSzT393bW x45rlRX5EkMjZ1Z7lXfN/y9LX9gX0qwLU3RANG2b2LNF/hDW8yVk7puq6zZg7QXZXdIJ G1nVzFwwe2qHiPTYM+Gx7tZXiJ7rphoctgUe35wSYLMnzrrmnbP8j47+oUy81V18RkiP nVcg== X-Gm-Message-State: AOAM532vfJB5yY+M6p2Ip79pjbwUp6pxL+rCd1K5k4lsT9mstm7pvJNl H1P/FFFi4N2G7YUuRonMeGijcZ4MRey5Dzgq X-Google-Smtp-Source: ABdhPJxWQWgHqeHeLP9AZ7TlKphAE28bQei1LUV6m367BQpAYasGUBryOfDcMJHgROyhEPcNsD9ZpA== X-Received: by 2002:a05:6830:1ac8:: with SMTP id r8mr3071904otc.70.1601676008387; Fri, 02 Oct 2020 15:00:08 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v10 7/8] linux-user/elfload: Parse NT_GNU_PROPERTY_TYPE_0 notes Date: Fri, 2 Oct 2020 16:59:54 -0500 Message-Id: <20201002215955.254866-8-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201002215955.254866-1-richard.henderson@linaro.org> References: <20201002215955.254866-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::344; envelope-from=richard.henderson@linaro.org; helo=mail-ot1-x344.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, qemu-arm@nongnu.org, alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" For aarch64, this includes the GNU_PROPERTY_AARCH64_FEATURE_1_BTI bit, which indicates that the image should be mapped with guarded pages. Signed-off-by: Richard Henderson --- v9: Only map the startup executable with BTI; anything else must be handled by the interpreter. v10: Split out preparatory patches (pmm). --- linux-user/qemu.h | 4 +++ linux-user/elfload.c | 68 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 941ca99722..534753ca12 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -61,6 +61,10 @@ struct image_info { abi_ulong interpreter_loadmap_addr; abi_ulong interpreter_pt_dynamic_addr; struct image_info *other_info; + + /* For target-specific processing of NT_GNU_PROPERTY_TYPE_0. */ + uint32_t note_flags; + #ifdef TARGET_MIPS int fp_abi; int interp_fp_abi; diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 6b422990ff..3c6cbd35c3 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2391,7 +2391,7 @@ static void load_elf_image(const char *image_name, in= t image_fd, struct elfhdr *ehdr =3D (struct elfhdr *)bprm_buf; struct elf_phdr *phdr; abi_ulong load_addr, load_bias, loaddr, hiaddr, error; - int i, retval; + int i, retval, prot_exec; const char *errmsg; =20 /* First of all, some simple consistency checks */ @@ -2467,6 +2467,50 @@ static void load_elf_image(const char *image_name, i= nt image_fd, goto exit_errmsg; } *pinterp_name =3D interp_name; + } else if (eppnt->p_type =3D=3D PT_GNU_PROPERTY) { + /* Process NT_GNU_PROPERTY_TYPE_0. */ + const uint32_t gnu0_magic =3D const_le32('G' | 'N' << 8 | 'U' = << 16); + uint32_t note[7]; + + /* + * The note contents are 7 words, but depending on LP64 vs ILP= 32 + * there may be an 8th padding word at the end. Check for and + * read the minimum size. Further checks below will validate + * that the sizes of everything involved are as we expect. + */ + if (eppnt->p_filesz < sizeof(note)) { + continue; + } + if (eppnt->p_offset + eppnt->p_filesz <=3D BPRM_BUF_SIZE) { + memcpy(note, bprm_buf + eppnt->p_offset, sizeof(note)); + } else { + retval =3D pread(image_fd, note, sizeof(note), eppnt->p_of= fset); + if (retval !=3D sizeof(note)) { + goto exit_perror; + } + } +#ifdef BSWAP_NEEDED + for (i =3D 0; i < ARRAY_SIZE(note); ++i) { + bswap32s(note + i); + } +#endif + /* + * Check that this is a NT_GNU_PROPERTY_TYPE_0 note. + * Again, descsz includes padding. Full size validation + * awaits checking the final payload. + */ + if (note[0] !=3D 4 || /* namesz */ + note[1] < 12 || /* descsz */ + note[2] !=3D NT_GNU_PROPERTY_TYPE_0 || /* type */ + note[3] !=3D gnu0_magic) { /* name */ + continue; + } +#ifdef TARGET_AARCH64 + if (note[4] =3D=3D GNU_PROPERTY_AARCH64_FEATURE_1_AND && + note[5] =3D=3D 4) { + info->note_flags =3D note[6]; + } +#endif /* TARGET_AARCH64 */ } } =20 @@ -2555,6 +2599,26 @@ static void load_elf_image(const char *image_name, i= nt image_fd, info->brk =3D 0; info->elf_flags =3D ehdr->e_flags; =20 + prot_exec =3D PROT_EXEC; +#ifdef TARGET_AARCH64 + /* + * If the BTI feature is present, this indicates that the executable + * pages of the startup binary should be mapped with PROT_BTI, so that + * branch targets are enforced. + * + * The startup binary is either the interpreter or the static executab= le. + * The interpreter is responsible for all pages of a dynamic executabl= e. + * + * Elf notes are backward compatible to older cpus. + * Do not enable BTI unless it is supported. + */ + if ((info->note_flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) + && (pinterp_name =3D=3D NULL || *pinterp_name =3D=3D 0) + && cpu_isar_feature(aa64_bti, ARM_CPU(thread_cpu))) { + prot_exec |=3D TARGET_PROT_BTI; + } +#endif + for (i =3D 0; i < ehdr->e_phnum; i++) { struct elf_phdr *eppnt =3D phdr + i; if (eppnt->p_type =3D=3D PT_LOAD) { @@ -2568,7 +2632,7 @@ static void load_elf_image(const char *image_name, in= t image_fd, elf_prot |=3D PROT_WRITE; } if (eppnt->p_flags & PF_X) { - elf_prot |=3D PROT_EXEC; + elf_prot |=3D prot_exec; } =20 vaddr =3D load_bias + eppnt->p_vaddr; --=20 2.25.1