From nobody Tue Nov 16 23:42:12 2021 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 68C381850915; Tue, 16 Nov 2021 23:42:24 +0000 (UTC) (envelope-from koobs.freebsd@gmail.com) Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1D4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Hv2fS2C4Tz3m8J; Tue, 16 Nov 2021 23:42:24 +0000 (UTC) (envelope-from koobs.freebsd@gmail.com) Received: by mail-pj1-x102d.google.com with SMTP id np6-20020a17090b4c4600b001a90b011e06so838303pjb.5; Tue, 16 Nov 2021 15:42:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:message-id:date:mime-version:user-agent:reply-to:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=Dm/7OsTRlmcI7rUbuNE0Gdqzz96IDDDhUVuaJy8OMFw=; b=J/HCjnDhMBjbx5BpKGT+EhCCBG+UaczU5JEtICBMgHqn5JDWe+TfWCu6H4/lMsWSoo CYr2LGhuJl+PSYQUVzwe6RoxI5IB5tMKd2rDwbXAxtywZfl/TBvRHHZirvfJ3CEy574T 8suevcGl64OTu619xGk1do4pHwO22ekuusTjdHVayy+BW/UTP8ysQqsqqQKpeNhjy8qR Vs8RPM3rmBANCJ4pg5eInI7TputKKNsIfTiXMEPnxTfdsbKV4nfnmHkAS8VsDwKON/vX e7oHYBgOcUJWidlaV+Lpswo6PduvBjSoPEqhM3dpgmd9t2Q/S6r0Iz+lVXf8Bl+P0zE0 tlWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:message-id:date:mime-version:user-agent :reply-to:subject:content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=Dm/7OsTRlmcI7rUbuNE0Gdqzz96IDDDhUVuaJy8OMFw=; b=6hfI0sWazzorWAT2vD+6trmY26vVG+zOs5Jgw2j8XW9kNc5nv7/Zu/d84Yimpsmkmq 9fQLMfX8ydeNb1+g6o2cZrDrKgbDyz7jxBixZap4wfiJm3Qca/yrtPQwc24bBOpo0mA5 FiHrrtM8ggkjelTbYcL0xH/1BAzxtXt3jg1pQWdvykFWN1bBo5cviwVvoTA4+g/6pUsK gDUhoBPhmOq/YwcMw5odcH7X6g3ToKgAAYdpHsmVZq0dqJMg/blaUiwO4LEW2EcnmlbY U4AUcP+RaJZi6P/pGUskwB0RSDIBaO4fWfTLFmy32Jm3AEQt89wnXV1lInXNWxzdI3ct +0DA== X-Gm-Message-State: AOAM53027XmmAh59P4mm1fNitiIEI7CT6FbhiSzb2FD/DaYM5+m1y7Ix 5HXfNGsxV0BwY66B6BkqDTIMWix8bgM= X-Google-Smtp-Source: ABdhPJxQxfNDCS40miFHKNYg7op2ONXrJ7s/6sQKmtAOmC/6OJqJSAV76xJ86rrTziduL5G0RN4j7A== X-Received: by 2002:a17:90a:e005:: with SMTP id u5mr3793557pjy.17.1637106136947; Tue, 16 Nov 2021 15:42:16 -0800 (PST) Received: from ?IPV6:2403:5807:1b:1:6c9e:b489:476f:91c0? (2403-5807-1b-1-6c9e-b489-476f-91c0.ip6.aussiebb.net. [2403:5807:1b:1:6c9e:b489:476f:91c0]) by smtp.gmail.com with ESMTPSA id o9sm10298093pfh.37.2021.11.16.15.42.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 16 Nov 2021 15:42:16 -0800 (PST) Sender: Kubilay Kocak Message-ID: Date: Wed, 17 Nov 2021 10:42:12 +1100 List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Thunderbird/96.0a1 Reply-To: koobs@FreeBSD.org Subject: Re: git: b014e0f15bc7 - main - Enable ASLR by default for 64-bit executables Content-Language: en-US To: Marcin Wojtas , src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org References: <202111162226.1AGMQg00099240@gitrepo.freebsd.org> From: Kubilay Kocak In-Reply-To: <202111162226.1AGMQg00099240@gitrepo.freebsd.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 4Hv2fS2C4Tz3m8J X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; TAGGED_FROM(0.00)[] X-ThisMailContainsUnwantedMimeParts: N On 17/11/2021 9:26 am, Marcin Wojtas wrote: > The branch main has been updated by mw: > > URL: https://cgit.FreeBSD.org/src/commit/?id=b014e0f15bc73d80ef49b64fd1f8c29f469467cb > > commit b014e0f15bc73d80ef49b64fd1f8c29f469467cb > Author: Marcin Wojtas > AuthorDate: 2021-10-24 14:53:06 +0000 > Commit: Marcin Wojtas > CommitDate: 2021-11-16 22:16:09 +0000 > > Enable ASLR by default for 64-bit executables > > Address Space Layout Randomization (ASLR) is an exploit mitigation > technique implemented in the majority of modern operating systems. > It involves randomly positioning the base address of an executable > and the position of libraries, heap, and stack, in a process's address > space. Although over the years ASLR proved to not guarantee full OS > security on its own, this mechanism can make exploitation more difficult. > > Tests on the tier 1 64-bit architectures demonstrated that the ASLR is > stable and does not result in noticeable performance degradation, > therefore it should be safe to enable this mechanism by default. > Moreover its effectiveness is increased for PIE (Position Independent > Executable) binaries. Thanks to commit 9a227a2fd642 ("Enable PIE by > default on 64-bit architectures"), building from src is not necessary > to have PIE binaries. It is enough to control usage of ASLR in the > OS solely by setting the appropriate sysctls. > > This patch toggles the kernel settings to use address map randomization > for PIE & non-PIE 64-bit binaries. It also disables SBRK, in order > to allow utilization of the bss grow region for mappings. The latter > has no effect if ASLR is disabled, so apply it to all architectures. > > As for the drawbacks, a consequence of using the ASLR is more > significant VM fragmentation, hence the issues may be encountered > in the systems with a limited address space in high memory consumption > cases, such as buildworld. As a result, although the tests on 32-bit > architectures with ASLR enabled were mostly on par with what was > observed on 64-bit ones, the defaults for the former are not changed > at this time. Also, for the sake of safety keep the feature disabled > for 32-bit executables on 64-bit machines, too. > > The committed change affects the overall OS operation, so the > following should be taken into consideration: > * Address space fragmentation. > * A changed ABI due to modified layout of address space. > * More complicated debugging due to: > * Non-reproducible address space layout between runs. > * Some debuggers automatically disable ASLR for spawned processes, > making target's environment different between debug and > non-debug runs. > > In order to confirm/rule-out the dependency of any encountered issue > on ASLR it is strongly advised to re-run the test with the feature > disabled - it can be done by setting the following sysctls > in the /etc/sysctl.conf file: > kern.elf64.aslr.enable=0 > kern.elf64.aslr.pie_enable=0 > > Co-developed by: Dawid Gorecki > Reviewed by: emaste, kib > Obtained from: Semihalf > Sponsored by: Stormshield > MFC after: 1 month > Differential revision: https://reviews.freebsd.org/D27666 > --- > sys/kern/imgact_elf.c | 20 +++++++++++++++++--- > 1 file changed, 17 insertions(+), 3 deletions(-) > > diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c > index 898f0f66a532..38ad61d8720b 100644 > --- a/sys/kern/imgact_elf.c > +++ b/sys/kern/imgact_elf.c > @@ -161,19 +161,33 @@ SYSCTL_NODE(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, aslr, > ""); > #define ASLR_NODE_OID __CONCAT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), _aslr) > > -static int __elfN(aslr_enabled) = 0; > +/* > + * While for 64-bit machines ASLR works properly, there are > + * still some problems when using 32-bit architectures. For this > + * reason ASLR is only enabled by default when running native > + * 64-bit non-PIE executables. > + */ > +static int __elfN(aslr_enabled) = __ELF_WORD_SIZE == 64; > SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, enable, CTLFLAG_RWTUN, > &__elfN(aslr_enabled), 0, > __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) > ": enable address map randomization"); > > -static int __elfN(pie_aslr_enabled) = 0; > +/* > + * Enable ASLR only for 64-bit PIE binaries by default. > + */ > +static int __elfN(pie_aslr_enabled) = __ELF_WORD_SIZE == 64; > SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, pie_enable, CTLFLAG_RWTUN, > &__elfN(pie_aslr_enabled), 0, > __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) > ": enable address map randomization for PIE binaries"); The current description seems ambiguous with respect to the added comment. If the sysctl (=1) applies ASLR "only" for PIE binaries, where the =0 (sysctl disabled) case applies it unconditionally, a better description might be: "Enable address map randomization only for PIE binaries" What is the actual/correct behaviour of the control? Might aslr_enabled_pie_only also be a better OID name? Perhaps not worth the churn, but long term it would be great if OID names reflected what they are/do, rather than what they're not/don't do. > -static int __elfN(aslr_honor_sbrk) = 1; > +/* > + * Sbrk is now deprecated and it can be assumed, that in most > + * cases it will not be used anyway. This setting is valid only > + * for the ASLR enabled and allows for utilizing the bss grow region. > + */ > +static int __elfN(aslr_honor_sbrk) = 0; > SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, honor_sbrk, CTLFLAG_RW, > &__elfN(aslr_honor_sbrk), 0, > __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": assume sbrk is used"); > Can we add (DEPRECATED) to the control description, and/or otherwise mark the control as deprecated if the sysctl framework supports an attribute marking them as such?