From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57F27C47258 for ; Wed, 17 Jan 2024 17:40:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CBBE86B007E; Wed, 17 Jan 2024 12:40:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C6B6E6B0080; Wed, 17 Jan 2024 12:40:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B32D56B0081; Wed, 17 Jan 2024 12:40:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id A24BF6B007E for ; Wed, 17 Jan 2024 12:40:19 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2990380CD9 for ; Wed, 17 Jan 2024 17:40:19 +0000 (UTC) X-FDA: 81689516958.08.D80D190 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf19.hostedemail.com (Postfix) with ESMTP id 374491A001F for ; Wed, 17 Jan 2024 17:40:16 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=jSJ9Eh6Q; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf19.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.177 as permitted sender) smtp.mailfrom=keescook@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705513216; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HI6bavhqQV1VGnP6OfS5XotwfVqJiEyBGy8aO+1AKAU=; b=whgcSTV9eS5A5ip2u5uW1UdNl2thzLKtlRc7EX4vMlk6svr3iwSbyHdyIr4MQQuc0rHI3v TPQ6AHGbj7LQLdpIWI87ImTnp62q7L51ssllRW91ydO98auAP8FSHYOwCf+iG1fkbDTHli y3luW9NCCK1ar3aeYLCSecV8fQim/Cg= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=jSJ9Eh6Q; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf19.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.177 as permitted sender) smtp.mailfrom=keescook@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705513216; a=rsa-sha256; cv=none; b=hN2MqnEIYlgVty8pYexGJLfGjpweLpL2waQf7HNUnAEcMJpiZaD2vmLKkYHpBnjKgXevD7 LgQp/LnZ43WTeU7EdKNr7YLpwJOw2P+ebQak3kZvuTYtpd2Czmf+DjpaKF3d2OQeF8CgXK S2JLpUcFIRmmugoHwXmxZkFtgdk9nKQ= Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1d427518d52so83859445ad.0 for ; Wed, 17 Jan 2024 09:40:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1705513215; x=1706118015; darn=kvack.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=HI6bavhqQV1VGnP6OfS5XotwfVqJiEyBGy8aO+1AKAU=; b=jSJ9Eh6QOIW9Mc3qacxwntKFXDuQCyeFRfSIhlAvD+TRjMQxwxfX3DEBHNXTyPPtff 67IozocRB7qRgPxCuL+WTu8PHnnmYL3tKQLkcftQy8Aa0mLvhgZTlfaYHLgl0s3wdFzq 7aOT2zaSpgEwGoLxWIrDQzpPdj0iDq3txZ/z8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705513215; x=1706118015; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=HI6bavhqQV1VGnP6OfS5XotwfVqJiEyBGy8aO+1AKAU=; b=BAhGSZ5gGS6IxBBELkMTvu727bbRGZNoXm8zTRUMUqKFMdbu2UaSXvq/1XxpJEnQX0 J2x6mZan8K51gYfpUY5QvCt14vwPmucd9X5vHu67llc/U+YDlEz+UVrZxL7bU2FbvqdY PwUrVNnmIe9hadIn2yWoD/VMv5auz9XYun5I1GRVuyOjY0xjL+PIf0MlbYVI3Ds+7LaM 0N4YZILGB6gL9DBUR1Q86HPPJZliolm6FSU+qy4xJOcpaoq+qcPM5Qf8jEeE7Xe1tgPt nmMBpbbmHvcCThevjRKYj15l6WIQ2FvFdZVA9l3PEjWaeQ5xXd5Zoi4lhUkrlc+UmSCN CBLw== X-Gm-Message-State: AOJu0Yx94Y9J3PyO1QKcYfptlXfI+U+sU+jwVpOk/MLismU5MAX1evgB WaN2mLCBU3Mcujz8QIh9PS2VHCEJOZy7 X-Google-Smtp-Source: AGHT+IH52fdJN93YoHDB5xj/lRsRVa+gmw/PCGVpP5CZggqbnNav6Zj6tGn0Ox6nArT7Tv1Myn7gxQ== X-Received: by 2002:a17:902:e808:b0:1d5:e949:bc43 with SMTP id u8-20020a170902e80800b001d5e949bc43mr1730139plg.32.1705513215006; Wed, 17 Jan 2024 09:40:15 -0800 (PST) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id t2-20020a1709028c8200b001d5d58216bdsm4615406plo.164.2024.01.17.09.40.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jan 2024 09:40:14 -0800 (PST) Date: Wed, 17 Jan 2024 09:40:13 -0800 From: Kees Cook To: Suren Baghdasaryan Cc: Yang Shi , Jeffrey Vander Stoep , Jiri Slaby , Rik van Riel , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@fb.com, Matthew Wilcox , Christoph Lameter , linux-hardening@vger.kernel.org Subject: Re: [PATCH v2] mm: align larger anonymous mappings on THP boundaries Message-ID: <202401170925.015D300A@keescook> References: <20220809142457.4751229f@imladris.surriel.com> <3193bf5b-4e22-412f-8c5b-68574942d9bc@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 374491A001F X-Stat-Signature: 9apyadyohum7aucb4pwct79nkdgk7sjn X-HE-Tag: 1705513216-375735 X-HE-Meta: U2FsdGVkX1/Ysf7D3r4e0VEY1KyVKp/M4ZE4BNhWiYpWLDMKDnDDuu/4PXCTu4XBMm7mzHwZLa0dBErIGKZ9bl/2Vg5a22Shsxa+ZKy8yU/nwKHGr06M7hiqVTp1tUwPSgOV6dylWN3sgZBH19WcFloN3nfFlLP21dYEpDTOeU4Sm6ggXfvinOrmuLehwpwz5NKRbIWg+DjrgTg/XEPHLFxMAyBIBKNUwAT0JubtXhWf0ZeGjLJZvwcX9ShhbrG4td5C6TNOCX/Z1JyIxIsqFKRlJNpyZ190s8tVOZRjD4jfM1mm/4SElUxsqNahK57s2GkXUjrEOwDBiBHGNKq0LqXHjHhauJi3McyVWeETNvRbKXVaXME4r0fQTNvg2RKMOjGO/mikAei2F7cd8WhPGZw6+q6r4mX7YYsaHmetgDEfOLdcPTfxu5Z6z66A+eRVYm9WmlJhgW0RLnp2CXHoFvbsGHWyj5EpypYouE0L0pDxxav0e6lrRozro6tgoIea1lpyoLb+v2KzzdxjOrP6UxbJ3LkSOrblzTPfXNoSqE1YkwV450NarAe3C/QBVwBWqkmOgNVQyIsM0ewjdsU9MSRyYmwh2WIqwK9o/ZBY4bEW5PAD5cwMfPFmEBPN/mP7HVo3fmULg7HuLFArYUI/asULhZRk3gSwLQApF+ynubnuioLvrb6MYfDa+bzyHkqaAWNZp64s3lB13fjrofh7cYn4fegSEiEwvnv0CLKLLMfDOJLBw6oohy6XxDB8LDomQETndJsZ0EZ+LFHY+1tWi2DddqjhLKh2Fp+LoMvUH15RLH0pBheUxcmFJEvGgg0FC1sh6kbtQ95UkxRlgmdLrY/s0YkLZS/kAzxaHVbsaXwc5yWiNuoLzARMF2kS0g6nDIswZ+6XBRIurEqHoWV+IPqJnzmNx/jfHpOknpZdaG7wWeNZGH52aAmR/76EUkdZAPGJmi0I9RE9DW/m2e8 SVgvmXyZ Mmf7f7a/jtx8yhsDxNs2irqKyhS9wUTeToTbu6yIXp4tk826YPI2C4Zm4zDqKIAQJCtWhqBb3eaq/RkArqKHdBnyv7Njf9dSu1JtbXibDciidGQRfFjsK/HvG0G7j/N2j1DQnPzldjJHozJY6rHFemoJSfk9NFQoZT5OUuv5SLHNl9Fyt/ScT7U1ZEXGFNEx0hNYmYMc6ZBfhM9WvYhj5A9TO7sc92L8qcvUnaF9YXdJE+GDuUVhxyAUyIlpFDz73zj1QwaZtRDgKFLbClAQspFzQxjchL4EY+PaOcJez8uiPR+E5tOY0tfOnBBn8kvR87ZGatfLS86kTGxpte2sKD6xF9J/PorqossY/4+5ksQ9FS78qER8Jmd7YrPL1Y/iz9/VQDX2mLiB/g/YU58yqMI/Pv5rDOArb9Y2bUGXpl/aP8elrOsxit1keGFo0MDI16lbNTY8Jib4a8dD5H6JU1owmQi5y20EN8dOoEM8g9o2JJdd6hCOqdOac7kAaQxOSDNxF5WSKMVpbppYQ60cYoCdroeixRbj7DhfYw8N7miWnmBxpcCsSdRwwA9hBl6vEonDTAaUXdW/x0yU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Tue, Jan 16, 2024 at 02:30:36PM -0800, Suren Baghdasaryan wrote: > On Tue, Jan 16, 2024 at 2:25 PM Yang Shi wrote: > > > > On Tue, Jan 16, 2024 at 1:58 PM Suren Baghdasaryan wrote: > > > > > > On Tue, Jan 16, 2024 at 12:56 PM Yang Shi wrote: > > > > > > > > On Tue, Jan 16, 2024 at 11:16 AM Suren Baghdasaryan wrote: > > > > > > > > > > On Tue, Jan 16, 2024 at 4:09 AM Jiri Slaby wrote: > > > > > > > > > > > > On 16. 01. 24, 12:53, Jiri Slaby wrote: > > > > > > > Hi, > > > > > > > > > > > > > > On 09. 08. 22, 20:24, Rik van Riel wrote: > > > > > > >> Align larger anonymous memory mappings on THP boundaries by > > > > > > >> going through thp_get_unmapped_area if THPs are enabled for > > > > > > >> the current process. > > > > > > >> > > > > > > >> With this patch, larger anonymous mappings are now THP aligned. > > > > > > >> When a malloc library allocates a 2MB or larger arena, that > > > > > > >> arena can now be mapped with THPs right from the start, which > > > > > > >> can result in better TLB hit rates and execution time. > > > > > > > > > > > > > > This appears to break 32bit processes on x86_64 (at least). In > > > > > > > particular, 32bit kernel or firefox builds in our build system. > > > > > > > > > > > > > > Reverting this on top of 6.7 makes it work again. > > > > > > > > > > > > > > Downstream report: > > > > > > > https://bugzilla.suse.com/show_bug.cgi?id=1218841 > > > > > > > > > > > > > > So running: > > > > > > > pahole -J --btf_gen_floats -j --lang_exclude=rust > > > > > > > --skip_encoding_btf_inconsistent_proto --btf_gen_optimized .tmp_vmlinux.btf > > > > > > > > > > > > > > crashes or errors out with some random errors: > > > > > > > [182671] STRUCT idr's field 'idr_next' offset=128 bit_size=0 type=181346 > > > > > > > Error emitting field > > > > > > > > > > > > > > strace shows mmap() fails with ENOMEM right before the errors: > > > > > > > 1223 mmap2(NULL, 5783552, PROT_READ|PROT_WRITE, > > > > > > > MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 > > > > > > > ... > > > > > > > 1223 <... mmap2 resumed>) = -1 ENOMEM (Cannot allocate > > > > > > > memory) > > > > > > > > > > > > > > Note the .tmp_vmlinux.btf above can be arbitrary, but likely large > > > > > > > enough. For reference, one is available at: > > > > > > > https://decibel.fi.muni.cz/~xslaby/n/btf > > > > > > > > > > > > > > Any ideas? > > > > > > > > > > > > This works around the problem, of course (but is a band-aid, not a fix): > > > > > > > > > > > > --- a/mm/mmap.c > > > > > > +++ b/mm/mmap.c > > > > > > @@ -1829,7 +1829,7 @@ get_unmapped_area(struct file *file, unsigned long > > > > > > addr, unsigned long len, > > > > > > */ > > > > > > pgoff = 0; > > > > > > get_area = shmem_get_unmapped_area; > > > > > > - } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { > > > > > > + } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && > > > > > > !in_32bit_syscall()) { > > > > > > /* Ensures that larger anonymous mappings are THP > > > > > > aligned. */ > > > > > > get_area = thp_get_unmapped_area; > > > > > > } > > > > > > > > > > > > > > > > > > thp_get_unmapped_area() does not take care of the legacy stuff... > > > > > > > > > > This change also affects the entropy of allocations. With this patch > > > > > Android test [1] started failing and it requires only 8 bits of > > > > > entropy. The feedback from our security team: > > > > > > > > > > 8 bits of entropy is already embarrassingly low, but was necessary for > > > > > 32 bit ARM targets with low RAM at the time. It's definitely not > > > > > acceptable for 64 bit targets. > > > > > > > > Thanks for the report. Is it 32 bit only or 64 bit is also impacted? > > > > If I understand the code correctly, it expects the address allocated > > > > by malloc() is kind of randomized, right? > > > > > > Yes, correct, the test expects a certain level of address randomization. > > > The test failure was reported while running kernel_virt_x86_64 target > > > (Android emulator on x86), so it does impact 64bit targets. > > > > IIUC this breaks the "expectation" for randomized addresses returned > > by malloc(), but it doesn't break any real Android application, right? > > So this is a security concern instead of a real regression. > > How is making a system move vulnerabile not a real regression? > > > > > I think we can make this opt-in with a knob. Anyone who outweighs > > security could opt this feature out. However I'm wondering whether > > Android should implement a general address randomization mechanism > > instead of depending on "luck" if you do care about it. > > This is not depending on luck. This is checking for possible > vulnerabilities in the system. > I admit I'm not a security expert, so I'm looping in Jeff and Kees to chime in. Hi! Just to chime in, but reduction in ASLR entropy is absolutely a regression. This is userspace visible (via /proc/sys/kernel/randomize_va_space, /proc/sys/vm/mmap_rnd*_bits) with expectations that it work as advertised. So, while 32-bit might be already ASLR-weak, we don't want to make things super bad nor break ASLR in compat mode under 64-bit systems. Having an opt-in sounds reasonable, but we need to wire it to the ASLR sysctls in some way so nothing lying about the ASLR entropy. -Kees -- Kees Cook