Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp1461089pxu; Thu, 8 Oct 2020 12:02:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwk4jrFZPpiKEge8DXlJPxS1EPK+ocRYb0PTTV2YAvCbqqfqUtNt/uWv0TXKR/7WnnEGQ91 X-Received: by 2002:a50:b745:: with SMTP id g63mr10645873ede.181.1602183720705; Thu, 08 Oct 2020 12:02:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602183720; cv=none; d=google.com; s=arc-20160816; b=NSuXty00XzN6q0bkGPDjAvR6BIUoAyzpeKFWNakQAjEIFU2r4RZ7fnO/70tPU564gs yfTp2IFMIGKO77vw0/2xDkx24ksQ8giDK+QWNObSBk1/h9LVHlMk/o+NIyEqnv0/YHLM UsgeXAkMOYGdKLFRcpcomE7i1cUjOJI8AjPTv6CsqP2L0T8bopc1CbfyVYxhZlXGY7fu Wj808d05AA7aX6yePMkIawSrHjwy1x9V9a91yxLCkxTtXlP6yc1VOJTxTSKtb7Esp/em P8vMkFgt0vNyNR7QNZCzf168FnKX9wbiUYBsuaQc8hzrGTAi/PHFXur4hmZxpcpA6wou MwOA== 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:from:references :cc:to:subject:dkim-signature; bh=iY2DdIaqED/GpphAr9g0IhgVV6qVeDZXTm/GMY0em2M=; b=h8iFMch9Y28/CpqFhZcVktUhH64G2h60Na+vFITXFxPBwoK7ln/2S4WMTpxdyBF428 3d2jI+nBqiI/G8ZZt+o4LZ+vlx3Bd+IBqGBmMQtHpluERqOSRRZpDiUiXf/zEtx5MLUE DBf9K4vVm1lSEigJUuQZdDpPJ94LesOk/06w6/bShS2RToQ+hFfmZ6wxxMXNfwwoEgVq rAhjaUd4ZNcq/D+piO8izYgqhgelMc5Tg8PvQkpPsEFHrXDIqLaFLTs+VdL2DNJbtmPr 0EUwJ83Ulj7yJfbX6fIRHvDRoewuoibdrGd0bMP/sOrJR6BJ5nTaOI7WbT8fN7L+7qw+ HODQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=UMSSEWHZ; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gy23si4382778ejb.483.2020.10.08.12.01.35; Thu, 08 Oct 2020 12:02:00 -0700 (PDT) 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=@gmail.com header.s=20161025 header.b=UMSSEWHZ; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731181AbgJHSKU (ORCPT + 99 others); Thu, 8 Oct 2020 14:10:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731152AbgJHSKU (ORCPT ); Thu, 8 Oct 2020 14:10:20 -0400 Received: from mail-lf1-x144.google.com (mail-lf1-x144.google.com [IPv6:2a00:1450:4864:20::144]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E58C4C061755; Thu, 8 Oct 2020 11:10:19 -0700 (PDT) Received: by mail-lf1-x144.google.com with SMTP id j22so2044788lfe.10; Thu, 08 Oct 2020 11:10:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=iY2DdIaqED/GpphAr9g0IhgVV6qVeDZXTm/GMY0em2M=; b=UMSSEWHZ4rqm9CloQbxNH8hdOC45yMRgYYgviO0VCaUv9HxSl6z7uM7z7mP9GxsK6F Ku25gKThBsih72le/NZ24sYhySq3bJ91DuboCIxRQWcv4EKufcwzmt5UC3BIDYTtsMnA GibUtNb7OEF/k5+aU9EJMObfgl0dy20A3mLr2XYKmucKeLLM+XHyE5JtUdkAsfqPuf5L 6E/pQwniXPtza/99MTrO+iHAK9oGHfTfA9ofM22OIcK8oGkvx4jpOtnV7Sg65n74xmLR oMyZvA0GJxMHXVk23ZFzg+vOD5Inh6AwniPwaPGAzySdJ/0bWGcSWx5xsekpRNA2ZXc4 B+Yw== 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:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=iY2DdIaqED/GpphAr9g0IhgVV6qVeDZXTm/GMY0em2M=; b=rq1zIvPoBU90Tm3iobWurLGO9owVbdbNNTY9iFnNLno90jBlI7wmzVc1LNi9N2S6iX 8BxevG7EKGfYYWtwPe5fcRqPPFAwIVtxl3Mou/wTAYWNqo+QRoDv+mh/bApOcTj2obHb MCS+jP+p9L7snfahV6KHe33uemuyag++eGLCih97qwlnZGNSmSXbvSfyglHmN7GdSjJP UZxb4qu14UwUHpI/VGkoA6BSxmJpDc7OfpxgOUH1sSVxYX0lZEk1+ViiLjVUEVfyh/If tmWlfDWXhP3kH0NIza1XJ9i6cU8V9nBwlvR4Sgh8R5l7ZLO6Pd+pCkTXq3ecKl6sns4J ewqw== X-Gm-Message-State: AOAM530SviaM2z3zrv9WU3AJ1xOpYr570+BbCBmgGteogk1aQvMsXLd1 in7hTJIh969p6tsyUjpRY4JJzEhR/2k= X-Received: by 2002:ac2:58d8:: with SMTP id u24mr1181223lfo.415.1602180618002; Thu, 08 Oct 2020 11:10:18 -0700 (PDT) Received: from [192.168.1.112] (88-114-211-119.elisa-laajakaista.fi. [88.114.211.119]) by smtp.gmail.com with ESMTPSA id s16sm1076712ljj.35.2020.10.08.11.10.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 08 Oct 2020 11:10:17 -0700 (PDT) Subject: Re: [PATCH RESEND v2] mm: Optional full ASLR for mmap() and mremap() To: Jann Horn Cc: linux-hardening@vger.kernel.org, Andrew Morton , Linux-MM , kernel list References: <20201008165408.38228-1-toiwoton@gmail.com> From: Topi Miettinen Message-ID: <3413d0c8-17c7-fbae-e5fa-74a918e61239@gmail.com> Date: Thu, 8 Oct 2020 21:10:12 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 8.10.2020 20.13, Jann Horn wrote: > On Thu, Oct 8, 2020 at 6:54 PM Topi Miettinen wrote: >> Writing a new value of 3 to /proc/sys/kernel/randomize_va_space >> enables full randomization of memory mappings created with mmap(NULL, >> ...). With 2, the base of the VMA used for such mappings is random, >> but the mappings are created in predictable places within the VMA and >> in sequential order. With 3, new VMAs are created to fully randomize >> the mappings. Also mremap(..., MREMAP_MAYMOVE) will move the mappings >> even if not necessary. > [...] >> + if ((flags & MREMAP_MAYMOVE) && randomize_va_space >= 3) { >> + /* >> + * Caller is happy with a different address, so let's >> + * move even if not necessary! >> + */ >> + new_addr = arch_mmap_rnd(); >> + >> + ret = mremap_to(addr, old_len, new_addr, new_len, >> + &locked, flags, &uf, &uf_unmap_early, >> + &uf_unmap); >> + goto out; >> + } > > You just pick a random number as the address, and try to place the > mapping there? Won't this fail if e.g. the old address range overlaps > with the new one, causing mremap_to() to bail out at "if (addr + > old_len > new_addr && new_addr + new_len > addr)"? Thanks for the review. I think overlap would be OK in this case and the check should be skipped. > Also, on Linux, the main program stack is (currently) an expanding > memory mapping that starts out being something like a couple hundred > kilobytes in size. If you allocate memory too close to the main > program stack, and someone then recurses deep enough to need more > memory, the program will crash. It sounds like your patch will > randomly make such programs crash. Right, especially on 32 bit systems this could be a real problem. I have limited the stack for tasks in the whole system to 2MB without problems (most use only 128kB) and on 48 bit virtual address systems the collision to 2MB area could be roughly 1/2^(48-21) which is a very small number. But perhaps this should be still be avoided by not picking an address too close to bottom of stack, say 64MB to be sure. It may also make this more useful also for 32 bit systems but overall I'm not so optimistic due to increased fragmentation. > Also, what's your strategy in general with regards to collisions with > existing mappings? Is your intention to just fall back to the classic > algorithm in that case? Maybe a different address could be tried (but not infinitely, say 5 times) and then fall back to classics. This would not be good for the ASLR but I haven't seen mremap() to be used much in my tests. > You may want to consider whether it would be better to store > information about free memory per subtree in the VMA tree, together > with the maximum gap size that is already stored in each node, and > then walk down the tree randomly, with the randomness weighted by free > memory in the subtrees, but ignoring subtrees whose gaps are too > small. And for expanding stacks, it might be a good idea for other > reasons as well (locking consistency) to refactor them such that the > size in the VMA tree corresponds to the maximum expansion of the stack > (and if an allocation is about to fail, shrink such stack mappings). This would reduce the randomization which I want to avoid. I think the extra overhead should be OK: if this is unacceptable for a workload or system constraints, don't use mode '3' but '2'. Instead of single global sysctl, this could be implemented as a new personality (or make this model the default and add a compatibility personality with no or less randomization), so it could be applied only for some tasks but not all. -Topi