Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp5037331pxu; Thu, 10 Dec 2020 11:17:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJxDQmasnZz+U/y/Ho2vCrnQewOOguEyLC3hXlGa3CYEjbYaQNJR5vOVJmcJIzcW65CfquHp X-Received: by 2002:a05:6402:1b1e:: with SMTP id by30mr8250568edb.75.1607627829273; Thu, 10 Dec 2020 11:17:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607627829; cv=none; d=google.com; s=arc-20160816; b=uVmFQ4oMsKBR01ViMwzmXNUsFsAupJmdZ/KLynZk1Nyk+5JVXU8IqRGoH/LY3Up8Nz 1nrufbCDk6uH27HnYG4i/FjAWLLhG21JNQQxosq6ypN6Jzu1QoB01zw7QVbaoFbSQdpk O7D62Nao9UDxnqnsv93AgSkWLf+rC771mdnwf9m+dSsuImFuQi58E6PHhNK91d8GzXsD fwzKwoDtvK2A8ofMPffN6ejbLFkZotbnOx/mBadyuowYgB7YnpkjaHEGK4GlBXIz7x4w Xq8iYldZmwgc75ZtYb4yd8PhC/ybY2LS+/M8bNWYKagk02mofsWKQ8BuMG9i3ekNkx13 nBGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:autocrypt:from :references:cc:to:subject:dkim-signature; bh=i/rwupQvQkYiokDwNb0ikWR1Xxc0O7H0JsJy333WoH4=; b=aA2cuW3mW5pH78z5CXkC0o4/eq/GoB1ov6zK1qJBeaTkQmTRTDCQm0Kc97lyolbjux HZR+sYeQndQC0f4WLkbvAPthqLW05LzhsIkURzkRerxDf3fV7dLFRh5qbnGis1DdCwP7 XyrzANpFFqWv/LxkCf3ykFR+jouIKZ2zecpUpCX4dAkGDZKFXTXlB3uUKYO0/vFhf3oG 6wyZhysf8k1ZZYW7Dkm70EQbmfBYkH3k+bYw2BGCObxDvqJ8lyud6v7B3PKkdA/Lx1aJ DTXB/wGXIyvWSCF+VIIbJ3/KI8NrrHKCcacjhf+2uFo7rBNCutAIQxYSDmTDH2H04qAV mZgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="OTtSR5G/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l7si3619475edv.409.2020.12.10.11.16.46; Thu, 10 Dec 2020 11:17:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="OTtSR5G/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2393401AbgLJTNb (ORCPT + 99 others); Thu, 10 Dec 2020 14:13:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390917AbgLJTNE (ORCPT ); Thu, 10 Dec 2020 14:13:04 -0500 Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8EBB8C061794 for ; Thu, 10 Dec 2020 11:12:24 -0800 (PST) Received: by mail-qt1-x843.google.com with SMTP id 7so4580150qtp.1 for ; Thu, 10 Dec 2020 11:12:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=subject:to:cc:references:from:autocrypt:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=i/rwupQvQkYiokDwNb0ikWR1Xxc0O7H0JsJy333WoH4=; b=OTtSR5G/9/kciSl+snmBLJQ9DP7IX9WofQtpXWNGgk0cMzostaxi8DeKv8+Fn1kIDt 8IS41tRh2eR3mk118ordBVemK27vG7EiG+Dh/1uv4mSceM5nB0JI/yz7gt3ua564gsUO tAAkDLZESXBQH08GmT1EUWMKcmquQ2ZcCYVeK8Dkg0e6vfUpFg4G5DPdETaH1aRuyiYX +RX/E3XbvsAG/GYDbNpiG/Z/+bI2vSjbQvRioYwxZZqLsckT8WsMnptPww8C7CrGy2xM 0SR35fP01eggY30RL5x8H5g3Lz7TpsZY6EsPpJtmtlt/9Jnkz9U81i+FElxNDJnhXp2J wOfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=i/rwupQvQkYiokDwNb0ikWR1Xxc0O7H0JsJy333WoH4=; b=EL0EYSLJTMf9MdseFaZ5pN7mfrmM3aQWWYi99Pv2Mp0lT0HxrRaUXxXE7ZoxGhG3EX dU9udafXmHJ648/UIqVG6elXNrHMOB9awVUHPzcZMR5gM/V4k1pNwurLeBgfBWEvwGoy 0eR2Hk1TkHGMjdleTWT4MsLfIsWJTerUwziccDc3Msafy0OOLtMvY0eiQmOTjoqZfShJ nMfocGD3X+98CN+TtFB1mHTMMCUWyrj2x728Kuvq4fODZlBeAPnQM8zXP+PsDr4NiVUo yE/Ve4Xx3Tei8fDjmdL1C2XR491yhSpuMU7ezCTPhOG2uoT+1tmoTZT5Djlv+gAg7b2K Ks9A== X-Gm-Message-State: AOAM532JhKmIYQM1610SmPIei44i74xVO459BTN31Htuk0xP5QlM+uQa YOLglgyGXnTtuI8RysQxFSywXg== X-Received: by 2002:ac8:2a8f:: with SMTP id b15mr2239663qta.33.1607627543439; Thu, 10 Dec 2020 11:12:23 -0800 (PST) Received: from [192.168.1.4] ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id i68sm1431916qkc.82.2020.12.10.11.12.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 10 Dec 2020 11:12:22 -0800 (PST) Subject: Re: [PATCH v3 2/2] aarch64: Use mmap to add PROT_BTI instead of mprotect [BZ #26831] To: Szabolcs Nagy , libc-alpha@sourceware.org Cc: Mark Rutland , kernel-hardening@lists.openwall.com, Catalin Marinas , linux-kernel@vger.kernel.org, Jeremy Linton , Mark Brown , Topi Miettinen , Will Deacon , linux-arm-kernel@lists.infradead.org References: <7e800caa0485fec67b21ebab1e27bb23b2f0d971.1606898457.git.szabolcs.nagy@arm.com> From: Adhemerval Zanella Autocrypt: addr=adhemerval.zanella@linaro.org; prefer-encrypt=mutual; keydata= mQINBFcVGkoBEADiQU2x/cBBmAVf5C2d1xgz6zCnlCefbqaflUBw4hB/bEME40QsrVzWZ5Nq 8kxkEczZzAOKkkvv4pRVLlLn/zDtFXhlcvQRJ3yFMGqzBjofucOrmdYkOGo0uCaoJKPT186L NWp53SACXguFJpnw4ODI64ziInzXQs/rUJqrFoVIlrPDmNv/LUv1OVPKz20ETjgfpg8MNwG6 iMizMefCl+RbtXbIEZ3TE/IaDT/jcOirjv96lBKrc/pAL0h/O71Kwbbp43fimW80GhjiaN2y WGByepnkAVP7FyNarhdDpJhoDmUk9yfwNuIuESaCQtfd3vgKKuo6grcKZ8bHy7IXX1XJj2X/ BgRVhVgMHAnDPFIkXtP+SiarkUaLjGzCz7XkUn4XAGDskBNfbizFqYUQCaL2FdbW3DeZqNIa nSzKAZK7Dm9+0VVSRZXP89w71Y7JUV56xL/PlOE+YKKFdEw+gQjQi0e+DZILAtFjJLoCrkEX w4LluMhYX/X8XP6/C3xW0yOZhvHYyn72sV4yJ1uyc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gGr1bnFv74/1vbKtjeXLCcRKk3Ulw0bY1OoDxWQr86T2fZGJ/HIZuVVBf3+ gaYJF92GXFynHnea14nFFuFgOni0Mi1zDxYH/8yGGBXvo14KWd8JOW0NJPaCDFJkdS5hu0VY 7vJwKcyHJGxsCLU+Et0mryX8qZwqibJIzu7kUJQdQDljbRPDFd/xmGUFCQiQAncSilYOcxNU EMVCXPAQTteqkvA+gNqSaK1NM9tY0eQ4iJpo+aoX8HAcn4sZzt2pfUB9vQMTBJ2d4+m/qO6+ cFTAceXmIoFsN8+gFN3i8Is3u12u8xGudcBPvpoy4OoG Message-ID: Date: Thu, 10 Dec 2020 16:12:18 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <7e800caa0485fec67b21ebab1e27bb23b2f0d971.1606898457.git.szabolcs.nagy@arm.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 02/12/2020 05:55, Szabolcs Nagy via Libc-alpha wrote: > Re-mmap executable segments if possible instead of using mprotect > to add PROT_BTI. This allows using BTI protection with security > policies that prevent mprotect with PROT_EXEC. > > If the fd of the ELF module is not available because it was kernel > mapped then mprotect is used and failures are ignored. To protect > the main executable even when mprotect is filtered the linux kernel > will have to be changed to add PROT_BTI to it. > > The delayed failure reporting is mainly needed because currently > _dl_process_gnu_properties does not propagate failures such that > the required cleanups happen. Using the link_map_machine struct for > error propagation is not ideal, but this seemed to be the least > intrusive solution. > > Fixes bug 26831. LGTM, thanks. Reviewed-by: Adhemerval Zanella > --- > v3: > - split the last patch in two. > - pushed to nsz/btifix-v3 branch. > > sysdeps/aarch64/dl-bti.c | 54 ++++++++++++++++++++++++++------------- > sysdeps/aarch64/dl-prop.h | 8 +++++- > sysdeps/aarch64/linkmap.h | 2 +- > 3 files changed, 44 insertions(+), 20 deletions(-) > > diff --git a/sysdeps/aarch64/dl-bti.c b/sysdeps/aarch64/dl-bti.c > index 67d63c8a73..ff26c98ccf 100644 > --- a/sysdeps/aarch64/dl-bti.c > +++ b/sysdeps/aarch64/dl-bti.c > @@ -19,9 +19,17 @@ > #include > #include > #include > +#include > > -static void > -enable_bti (struct link_map *map, const char *program) > +/* See elf/dl-load.h. */ > +#ifndef MAP_COPY > +# define MAP_COPY (MAP_PRIVATE | MAP_DENYWRITE) > +#endif > + > +/* Enable BTI protection for MAP. */ > + > +void > +_dl_bti_protect (struct link_map *map, int fd) > { > const size_t pagesz = GLRO(dl_pagesize); > const ElfW(Phdr) *phdr; > @@ -41,19 +49,31 @@ enable_bti (struct link_map *map, const char *program) > if (phdr->p_flags & PF_W) > prot |= PROT_WRITE; > > - if (__mprotect (start, len, prot) < 0) > - { > - if (program) > - _dl_fatal_printf ("%s: mprotect failed to turn on BTI\n", > - map->l_name); > - else > - _dl_signal_error (errno, map->l_name, "dlopen", > - N_("mprotect failed to turn on BTI")); > - } > + if (fd == -1) > + /* Ignore failures for kernel mapped binaries. */ > + __mprotect (start, len, prot); > + else > + map->l_mach.bti_fail = __mmap (start, len, prot, > + MAP_FIXED|MAP_COPY|MAP_FILE, > + fd, off) == MAP_FAILED; > } > } > OK. I am not very found of ignore the mprotect failure here, but it has been discussed in multiple threads that we need kernel support to proper handle it. > -/* Enable BTI for MAP and its dependencies. */ > + > +static void > +bti_failed (struct link_map *l, const char *program) > +{ > + if (program) No implicit checks. > + _dl_fatal_printf ("%s: %s: failed to turn on BTI protection\n", > + program, l->l_name); > + else > + /* Note: the errno value is not available any more. */ > + _dl_signal_error (0, l->l_name, "dlopen", > + N_("failed to turn on BTI protection")); > +} > + > + > +/* Report BTI protection failures for MAP and its dependencies. */ > Ok. > void > _dl_bti_check (struct link_map *map, const char *program) > @@ -61,16 +81,14 @@ _dl_bti_check (struct link_map *map, const char *program) > if (!GLRO(dl_aarch64_cpu_features).bti) > return; > > - if (map->l_mach.bti) > - enable_bti (map, program); > + if (map->l_mach.bti_fail) > + bti_failed (map, program); > > unsigned int i = map->l_searchlist.r_nlist; > while (i-- > 0) > { > struct link_map *l = map->l_initfini[i]; > - if (l->l_init_called) > - continue; > - if (l->l_mach.bti) > - enable_bti (l, program); > + if (l->l_mach.bti_fail) > + bti_failed (l, program); > } > } Ok. > diff --git a/sysdeps/aarch64/dl-prop.h b/sysdeps/aarch64/dl-prop.h > index 2016d1472e..e926e54984 100644 > --- a/sysdeps/aarch64/dl-prop.h > +++ b/sysdeps/aarch64/dl-prop.h > @@ -19,6 +19,8 @@ > #ifndef _DL_PROP_H > #define _DL_PROP_H > > +extern void _dl_bti_protect (struct link_map *, int) attribute_hidden; > + > extern void _dl_bti_check (struct link_map *, const char *) > attribute_hidden; > > @@ -43,6 +45,10 @@ static inline int > _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type, > uint32_t datasz, void *data) > { > + if (!GLRO(dl_aarch64_cpu_features).bti) > + /* Skip note processing. */ > + return 0; > + > if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) > { > /* Stop if the property note is ill-formed. */ > @@ -51,7 +57,7 @@ _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type, > > unsigned int feature_1 = *(unsigned int *) data; > if (feature_1 & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) > - l->l_mach.bti = true; > + _dl_bti_protect (l, fd); > > /* Stop if we processed the property note. */ > return 0; Ok. > diff --git a/sysdeps/aarch64/linkmap.h b/sysdeps/aarch64/linkmap.h > index 847a03ace2..b3f7663b07 100644 > --- a/sysdeps/aarch64/linkmap.h > +++ b/sysdeps/aarch64/linkmap.h > @@ -22,5 +22,5 @@ struct link_map_machine > { > ElfW(Addr) plt; /* Address of .plt */ > void *tlsdesc_table; /* Address of TLS descriptor hash table. */ > - bool bti; /* Branch Target Identification is enabled. */ > + bool bti_fail; /* Failed to enable Branch Target Identification. */ > }; > Ok.