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 A0F61C47258 for ; Thu, 18 Jan 2024 00:29:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 31C136B009B; Wed, 17 Jan 2024 19:29:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2CCCA6B009D; Wed, 17 Jan 2024 19:29:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1BBD86B009E; Wed, 17 Jan 2024 19:29:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0BFCB6B009B for ; Wed, 17 Jan 2024 19:29:44 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DEE0E160575 for ; Thu, 18 Jan 2024 00:29:43 +0000 (UTC) X-FDA: 81690548646.04.AA60626 Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) by imf11.hostedemail.com (Postfix) with ESMTP id 503634000E for ; Thu, 18 Jan 2024 00:29:42 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=KQrcH173; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of surenb@google.com designates 209.85.128.180 as permitted sender) smtp.mailfrom=surenb@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705537782; 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=0GbF8lWg9biRY3x9FtIXLv154tdCpudzNKri0Og9WYs=; b=ZgRzjX+InTGYUHsKVHTvrr+QVDorH4P1kBSFZmUuqUKGcW0Tb4XvEuQE3xJZX3hfeN7qSQ DPrA+9Kp8OIEl6QeElgu+cqfGAfflhxM1UrXfn4BMtJrp1L3zCOH5vJLxxU18D2WevRL9j 04HbrwfAjwjQZBFtB8Fk8hJN+U2DDFs= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=KQrcH173; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of surenb@google.com designates 209.85.128.180 as permitted sender) smtp.mailfrom=surenb@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705537782; a=rsa-sha256; cv=none; b=wQj5qAiI8Xh6uDS8WGDzhpjNURKeRPaedL+BqFzIPfxw6rkbtoN3YhQJYl8ksQXtaL2wzR C+rWZHWMu+uHkXZxr8njxw5PVT2NW/aKdmew+RmBe8vRj2Yrm3g4I0klGDyfvDGM1P1fYm 4M5+ilg2CvvDtLULQy2xiBbcQNNfHBc= Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-5fc2e997804so53060917b3.3 for ; Wed, 17 Jan 2024 16:29:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705537781; x=1706142581; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=0GbF8lWg9biRY3x9FtIXLv154tdCpudzNKri0Og9WYs=; b=KQrcH173YNzPY1gCVva/ZWajHhKNKuaDZQ7xIMawfOndIeKzC0l3n6EeUDyHlfis+0 LTthNQKw8ePkCFF988WfcGyVNrmXutSUNAH4T/OsF3hvR8q31VP3LzpWJO3t9XRKsX2e DLFlRZWSEkYl99dogD2wk35xaebVOz3JGmdXDGQhfLIrPQ/O2XRIjlg/bNrwclg0toGi fumAbel4JLMIYXs3mYOj61qGynR28jHJ6pd3wn/zd6cgI0uUoIMhzPcASg6QWdch88PE AJQAayi81aW+3OhoM4cEnuEKRJ6ooo6vXaZ5PWDztmJvVRNiEsjE5PIYQG0hXx5znTe8 yTww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705537781; x=1706142581; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0GbF8lWg9biRY3x9FtIXLv154tdCpudzNKri0Og9WYs=; b=Kh5aDfDpfaQPUr9Q3bgJ+PwmBlOccWt5gksDm3YcHC4KbXOnHPke1ihCD3jiXk4Ypg qrwmZdPpxv3KzFwAieuRWH+7QFfz1dpJov8UOs3CoN3N6fiB6ErB85Aturr3ft61eH7n QJ72T4jOXWyhndwt+mbaqwDWTMrhPFIjWw08FdAgMdLakjBmEjKHHaDgFWvhvGZCILhI wiadUoqjdpGqC1zI5DMxl9t5CX1mSlYn5YHC3gXYKjnbWmU4SDMvuoV1BX6We78fFw+W IlfNO0giTrSvjNbsfRBwTKizWAVJRBTzsaMDvBuhLNHr/8p0ivlXm4dNLWDCZL+sWh9e yCIg== X-Gm-Message-State: AOJu0Yxbl81zacqco6NdszhdEgvNLbNirykjMCw66gqr1+Lum92qasBU oRilh3XAnwwW18FLn9fgzmVLl2pfYla+vohfLVBcGyAfnxQZGCLrzQdanVo0EMOnFmlkbGV+3zi QcXdZEeka7cLZMP4b2pQNWR3RkxkCNHSSQVnH X-Google-Smtp-Source: AGHT+IHoLXuA8c9/5bG5YB6abwFwfyTloeuGrrgIFPJuTccOwX142oUtKwWJNAXj+AD9v5Ms3/0z4WMcarpc6HGNSs8= X-Received: by 2002:a0d:dfc1:0:b0:5f9:6f9d:4ae1 with SMTP id i184-20020a0ddfc1000000b005f96f9d4ae1mr49373ywe.81.1705537781228; Wed, 17 Jan 2024 16:29:41 -0800 (PST) MIME-Version: 1.0 References: <20220809142457.4751229f@imladris.surriel.com> <3193bf5b-4e22-412f-8c5b-68574942d9bc@kernel.org> <202401170925.015D300A@keescook> In-Reply-To: From: Suren Baghdasaryan Date: Wed, 17 Jan 2024 16:29:30 -0800 Message-ID: Subject: Re: [PATCH v2] mm: align larger anonymous mappings on THP boundaries To: Yang Shi Cc: Kees Cook , 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Stat-Signature: cwob9ctgw3fpebt8wac5m9zqf1jz6pbk X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 503634000E X-HE-Tag: 1705537782-212180 X-HE-Meta: U2FsdGVkX19X1f8MegNxZEqb/ioXPnGKzOcyfatd72itKIApbjfH4lycTqTZujcoOHh7j8OBm6cdJ1Hn7JD99qegQZfVgyUGFP9cU8BWsROYzT4s+u/hzTyT9VqjbKLlaVBqoBNu2mr8VffCu6rd9Y5MoaqGD14Lj3ZWKobgE5wlHOUn4V+18cugOgRkwD1SqIXNVofYvwYp78iphIbKsdYjrWs3FCRdD6MeM1dWZ0PfgFJpMcTDZtsW4EtTWmUJg5E+ipcBHocT//tUHgPLJ04jhfQAFMgcQIUk9MsvB7C8PZyuW/cQpMlQa04dQ/8R0J1rBs39IfeJTqTPv5FGgGbnUBWTF2/8dKLxc4cJBkpG0eCxmUAsUyC702HF3TTjMNGW5u4xnr3DLXhNGSkYFi6xk7PUvB+AzLj8u7Rgk/Lp7aceSjF7PXUoQjo1bHBGBaxjG0I3Ns/P3elWKkFgjBWiyDKexOZcdXfYDG65x/qPS6WXo6WwQh7CC+0ITD9UVETLbBZRInso+Aktlqr1GxoY6VrrcMM2F/Gg5e2sqCc6gn0/rOLn6w2kHlw4nLiQTkLSUmRu5JkO+FvNQASnbkHRHhjeopSxKiDO/P++4fnIvNV4647Ch4OpQ1Ujdn4v1YU6bGVrjFMZWW5Fu548IbYPDQyIrV0s36iE/7lJohnnt/LLH0Cc9/ShX8DM/GwoDQH1sP5K9zfN4deGGCFx08G09burbeIu5dLTjTLsImaUCncbEDBt2GYrEC3MuZqya+oTpqpniH5i1KI60xt91HOE0txIe91RTGVckVK58wlfeRka7GHVCC4xwB+XhsTbsnBLBegNSg5wmGc3WMHE/t8nN8sF/MpMaqQxifTY8ToMAQAaxFVJNuzyK8/dbEqNsOK+yUNvsUb0tY8IiNq0dVNDSAHWE0pwGyVOHSmRoMu6BZ5YE7g1ArBN+NUMXrP35hXIudD+qiCQSgF1psj 9Il2DWgg L/5Ja 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 Wed, Jan 17, 2024 at 4:13=E2=80=AFPM Yang Shi wrot= e: > > On Wed, Jan 17, 2024 at 4:02=E2=80=AFPM Suren Baghdasaryan wrote: > > > > On Wed, Jan 17, 2024 at 3:32=E2=80=AFPM Yang Shi = wrote: > > > > > > On Wed, Jan 17, 2024 at 9:40=E2=80=AFAM Kees Cook wrote: > > > > > > > > On Tue, Jan 16, 2024 at 02:30:36PM -0800, Suren Baghdasaryan wrote: > > > > > On Tue, Jan 16, 2024 at 2:25=E2=80=AFPM Yang Shi wrote: > > > > > > > > > > > > On Tue, Jan 16, 2024 at 1:58=E2=80=AFPM Suren Baghdasaryan wrote: > > > > > > > > > > > > > > On Tue, Jan 16, 2024 at 12:56=E2=80=AFPM Yang Shi wrote: > > > > > > > > > > > > > > > > On Tue, Jan 16, 2024 at 11:16=E2=80=AFAM Suren Baghdasaryan= wrote: > > > > > > > > > > > > > > > > > > On Tue, Jan 16, 2024 at 4:09=E2=80=AFAM 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 bounda= ries by > > > > > > > > > > >> going through thp_get_unmapped_area if THPs are enab= led for > > > > > > > > > > >> the current process. > > > > > > > > > > >> > > > > > > > > > > >> With this patch, larger anonymous mappings are now T= HP aligned. > > > > > > > > > > >> When a malloc library allocates a 2MB or larger aren= a, that > > > > > > > > > > >> arena can now be mapped with THPs right from the sta= rt, which > > > > > > > > > > >> can result in better TLB hit rates and execution tim= e. > > > > > > > > > > > > > > > > > > > > > > This appears to break 32bit processes on x86_64 (at l= east). In > > > > > > > > > > > particular, 32bit kernel or firefox builds in our bui= ld system. > > > > > > > > > > > > > > > > > > > > > > Reverting this on top of 6.7 makes it work again. > > > > > > > > > > > > > > > > > > > > > > Downstream report: > > > > > > > > > > > https://bugzilla.suse.com/show_bug.cgi?id=3D1218841 > > > > > > > > > > > > > > > > > > > > > > So running: > > > > > > > > > > > pahole -J --btf_gen_floats -j --lang_exclude=3Drust > > > > > > > > > > > --skip_encoding_btf_inconsistent_proto --btf_gen_opti= mized .tmp_vmlinux.btf > > > > > > > > > > > > > > > > > > > > > > crashes or errors out with some random errors: > > > > > > > > > > > [182671] STRUCT idr's field 'idr_next' offset=3D128 b= it_size=3D0 type=3D181346 > > > > > > > > > > > Error emitting field > > > > > > > > > > > > > > > > > > > > > > strace shows mmap() fails with ENOMEM right before th= e errors: > > > > > > > > > > > 1223 mmap2(NULL, 5783552, PROT_READ|PROT_WRITE, > > > > > > > > > > > MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 > > > > > > > > > > > ... > > > > > > > > > > > 1223 <... mmap2 resumed>) =3D -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 *fi= le, unsigned long > > > > > > > > > > addr, unsigned long len, > > > > > > > > > > */ > > > > > > > > > > pgoff =3D 0; > > > > > > > > > > get_area =3D shmem_get_unmapped_area; > > > > > > > > > > - } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPA= GE)) { > > > > > > > > > > + } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPA= GE) && > > > > > > > > > > !in_32bit_syscall()) { > > > > > > > > > > /* Ensures that larger anonymous mappi= ngs are THP > > > > > > > > > > aligned. */ > > > > > > > > > > get_area =3D thp_get_unmapped_area; > > > > > > > > > > } > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > thp_get_unmapped_area() does not take care of the legac= y stuff... > > > > > > > > > > > > > > > > > > This change also affects the entropy of allocations. With= this patch > > > > > > > > > Android test [1] started failing and it requires only 8 b= its 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 definit= ely 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 ran= domization. > > > > > > > The test failure was reported while running kernel_virt_x86_6= 4 target > > > > > > > (Android emulator on x86), so it does impact 64bit targets. > > > > > > > > > > > > IIUC this breaks the "expectation" for randomized addresses ret= urned > > > > > > 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 outweig= hs > > > > > > security could opt this feature out. However I'm wondering whet= her > > > > > > Android should implement a general address randomization mechan= ism > > > > > > 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 Kee= s 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/randomi= ze_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 w= ant > > > > 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 A= SLR > > > > sysctls in some way so nothing lying about the ASLR entropy. > > > > > > Thanks for the explanation. IIUC the randomiza_va_space and > > > mmap_rnd_bits randomize the mmap_base and start_brk for each exec() > > > call. So the heap allocation is randomized. But it seems the formula > > > doesn't take into account huge page. ARM64 adjusts the mmap_rnd_bits > > > based on page size. > > > > > > I did a simple test, which conceptually does: > > > 1. call mmap to allocate 8M heap > > > 2. print out the allocated address > > > 3. run the program 1000 times (launch/exit/re-launch) > > > 4. check how many unique addresses > > > > > > With the default config on my arm64 VM (mmap_rnd_bits is 18), I saw > > > 134 unique addresses. Without the patch, I saw 945 unique addresses. > > > So I think the test could replicate what your test does. > > > > > > When I increased the mmap_rnd_bits to 24, I saw 988 unique addresses > > > with the patch. x86_64 should have 28 bits by default, it should > > > randomize the address quite well. I don't know why you still saw this= , > > > or you have a different setting for mmap_rnd_bits? > > > > I checked the configuration on our test harness where the test failed: > > Thanks, Suren. > > > > > /proc/sys/vm/mmap_rnd_bits =3D 32 > > It is surprising 32 bits still fail. 24 bits on arm64 works for me. Or > the compat bits is used? Hmm. Let me verify to exclude that possibility. > > > /proc/sys/vm/mmap_rnd_compat_bits =3D 16 > > > > The failure logs are: > > > > 10-20 14:37:52.123 7029 7029 V AslrMallocTest: 7 bits of entropy for > > allocation size 8388608 (minimum 8) > > 10-20 14:37:52.123 7029 7029 E AslrMallocTest: insufficient entropy > > for malloc(8388608) > > > > which come from here: > > https://cs.android.com/android/platform/superproject/main/+/main:cts/te= sts/aslr/src/AslrMallocTest.cpp;l=3D127 > > So, the allocation size for which this test failed was 2^23. > > The patch just tries to align >=3D 2M allocations. It looks like your > test allocates 256 bytes, 64K and 8M. So just 8M is impacted. Correct. > > > > > > > > > I'm wondering whether we should take into account huge page alignment > > > for mmap_rnd_bits. And I think this is a huge page common problem, we > > > have file mapping huge page aligned as well. > > > > > > 32 bit is easy, I think I can just make thp_get_unmapped_area() a > > > no-op on 32 bit system. > > > > > > > > > > > -Kees > > > > > > > > -- > > > > Kees Cook