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 BBBC2C4725D for ; Thu, 18 Jan 2024 01:34:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 237DD6B007B; Wed, 17 Jan 2024 20:34:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1E72D6B007D; Wed, 17 Jan 2024 20:34:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0898C6B0081; Wed, 17 Jan 2024 20:34:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id EDAEA6B007B for ; Wed, 17 Jan 2024 20:34:35 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id BE547401A1 for ; Thu, 18 Jan 2024 01:34:35 +0000 (UTC) X-FDA: 81690712110.18.FD3DC21 Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) by imf19.hostedemail.com (Postfix) with ESMTP id F174F1A0009 for ; Thu, 18 Jan 2024 01:34:33 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=X1j7+X9T; spf=pass (imf19.hostedemail.com: domain of surenb@google.com designates 209.85.128.182 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705541674; a=rsa-sha256; cv=none; b=2+4qoOEQ+fHzow2cOPN950Lmj779xvwVvPOLEKmiPemrzbyb6oJAWVcRNgPMWmfuLVsp// ZSwwohIsVU6KzhODYu20NfbyQ2VqaJqslwN3CK8BqiYK2MRKJOPu8mlNvl850MefaJoJyq touzoq0D9DX5mt8+FYYLcsHPEfq20NM= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=X1j7+X9T; spf=pass (imf19.hostedemail.com: domain of surenb@google.com designates 209.85.128.182 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705541674; 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=CTt1bjaKAIwqL4Q5JoMaVHxUmIps0QDcEIJnYMzsbOg=; b=0PrfAHBwGoUHyIH2LeTLmkP8dCj5kPF2Fz2KdfoMLrhK1kDuh/H5rNqvmN+G+ZdwEAbvaY hgK14BaRiMYFjKB4kcdDmmyc0XzJpC3bJpWZYwZBz1R504rKXOsJyQC5WU16WuZ0N02XqT gfYSCInCDgwm1JX7jKhAn4cFCtJYvfA= Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-5ff6d3504d5so10900667b3.3 for ; Wed, 17 Jan 2024 17:34:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705541673; x=1706146473; 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=CTt1bjaKAIwqL4Q5JoMaVHxUmIps0QDcEIJnYMzsbOg=; b=X1j7+X9TRuLXNPaxulMn6kxA+Q+Tv5IMy83zLOMr7liK+xxeQgJezUIRcfdf/mhspl 2/vyYNIAmnQrsC2/eFcuYFEftiOMDhf5OCH8MDJwTBLQWdVSkxCgneKialdlqYB/5+Gc 6+FuhBZpT8DRW+C0sL+zyYig5FkWBQmLy6UfneJi0GkCUb9UlpYvnC+i8Eg45+BCf+BA NdXdWdKLKj0edzn8U8zA4JcQPSlCdHMnFWXvwbY3P3GrCH8rQVbA48Bv6V7GV28eUZTv tT7SmMq0K5FUt3isfMyHGTnI8Z7O54rBgcHZBQSeOESO0AFJdsLq2Cyk6EPyYJVUQ91R xoMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705541673; x=1706146473; 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=CTt1bjaKAIwqL4Q5JoMaVHxUmIps0QDcEIJnYMzsbOg=; b=LeGH6umosWETrSvD9GyqdBxFfXetdVzZFF0IuBpVwdimux81MwJpv0YgmP9K0wkBJY QiyL/8+9jzwTTnIrzXbzukRpASPZxa9Q/w9wlkjueI5/VBv+Lhwvdi3029D+QElUDWyT eJC/b2qoHj8B22S8t917afB/XUQF5VSpIvek9ddCYVM16leAIAKqJGW6wnuSG/uzzoy4 ugVSkfIjT5WrjNjqwEGQ6XuexKiRt173j9IhCU+djRc33hCOKCY27USbI/1/Wofj9Exs quuC+XszCC2y35xUUFjZGnAK+ez0khrntxFp+uu3d4UGOWHrLM9WKriZvkrOP5UMQgXo MGKQ== X-Gm-Message-State: AOJu0YwPSqtRIJNqk3vxNQYfONigDDu+y2My8Bm8sh4qVZkTEJ1jC1rq HgvOvryoJkk25slDk/N/p90hAfmY8HyJzveqsWV0D1WGDYHzilt5rlK0GcHIWl3PGpm8Fdqze8Q H3Jw3Fo3V6w3kvpgCnQX6XQj3tIQseh4b+M/L X-Google-Smtp-Source: AGHT+IGe/hI4XX9yWMva0w0KkLnGBUrB82IA2UYEtldpo0mepPXCTVkCoMSie9V+25xRsGLWhMC0j8RkvUi3m+tbTE4= X-Received: by 2002:a81:47c3:0:b0:5f5:63ee:6adf with SMTP id u186-20020a8147c3000000b005f563ee6adfmr126075ywa.93.1705541672800; Wed, 17 Jan 2024 17:34:32 -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 17:34:20 -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-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: F174F1A0009 X-Stat-Signature: 85xaeurd1pfkb1ka37gasnes1uameno9 X-Rspam-User: X-HE-Tag: 1705541673-351264 X-HE-Meta: U2FsdGVkX1/huweEjnlraRhjrwjsgf3CdAf0+2PCAa18r+WlRvyq/u956se4D2ZcnChL0U9ehwnDOVoRiPvvsHNFfuuAufLsZH6Ub0Zzdjmo3Us1iazCpZMoBpvnQJ/RLu/LZGDJR62P0oYS3jhix8XYmEX9viGh8F4HAAshcDXXM7nf0Av+DQppA+wCWVhvmnoOdk5JEYtITcfSfowPePzASt1+L5opK1/K30b8LpESOM9IhuhWaePMJJ+dfOtJAfogGtkmDJjQMDLp/D4UWkRAq/mw80zSpQ80q118efEsou8F5cRmp/8WFFptUWqeHGJd2VZTvpT4BRTIQxcW7BZn5iUfSovRDlKUHt/SHlgx3qGKot8heJ5jkNlcWuWiec2YV51Jr6/5RKzivOKhw7DFhV1J6bSqvA+d1TmwybuzVfRtk/KmuPP+PE/0uCmaLYjSVLRZfetvQaCSiPD7bkE4R3gwJVb0pzMi+dqJsAfQTAF8PHuOYXFNGtWsYungegOXtg2gqgN/U5LuLIVT+R78tI7nYaQzHP8upArOTqt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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:29=E2=80=AFPM Suren Baghdasaryan wrote: > > On Wed, Jan 17, 2024 at 4:13=E2=80=AFPM Yang Shi wr= ote: > > > > 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 wrot= e: > > > > > > 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 Baghdasary= an 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 boun= daries by > > > > > > > > > > > >> going through thp_get_unmapped_area if THPs are en= abled for > > > > > > > > > > > >> the current process. > > > > > > > > > > > >> > > > > > > > > > > > >> With this patch, larger anonymous mappings are now= THP aligned. > > > > > > > > > > > >> When a malloc library allocates a 2MB or larger ar= ena, that > > > > > > > > > > > >> arena can now be mapped with THPs right from the s= tart, which > > > > > > > > > > > >> can result in better TLB hit rates and execution t= ime. > > > > > > > > > > > > > > > > > > > > > > > > This appears to break 32bit processes on x86_64 (at= least). In > > > > > > > > > > > > particular, 32bit kernel or firefox builds in our b= uild system. > > > > > > > > > > > > > > > > > > > > > > > > Reverting this on top of 6.7 makes it work again. > > > > > > > > > > > > > > > > > > > > > > > > Downstream report: > > > > > > > > > > > > https://bugzilla.suse.com/show_bug.cgi?id=3D12188= 41 > > > > > > > > > > > > > > > > > > > > > > > > So running: > > > > > > > > > > > > pahole -J --btf_gen_floats -j --lang_exclude=3Drust > > > > > > > > > > > > --skip_encoding_btf_inconsistent_proto --btf_gen_op= timized .tmp_vmlinux.btf > > > > > > > > > > > > > > > > > > > > > > > > crashes or errors out with some random errors: > > > > > > > > > > > > [182671] STRUCT idr's field 'idr_next' offset=3D128= bit_size=3D0 type=3D181346 > > > > > > > > > > > > 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>) =3D -1 ENOM= EM (Cannot allocate > > > > > > > > > > > > memory) > > > > > > > > > > > > > > > > > > > > > > > > Note the .tmp_vmlinux.btf above can be arbitrary, b= ut 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 ba= nd-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 =3D 0; > > > > > > > > > > > get_area =3D shmem_get_unmapped_area= ; > > > > > > > > > > > - } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE= PAGE)) { > > > > > > > > > > > + } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE= PAGE) && > > > > > > > > > > > !in_32bit_syscall()) { > > > > > > > > > > > /* Ensures that larger anonymous map= pings are THP > > > > > > > > > > > aligned. */ > > > > > > > > > > > get_area =3D thp_get_unmapped_area; > > > > > > > > > > > } > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > thp_get_unmapped_area() does not take care of the leg= acy stuff... > > > > > > > > > > > > > > > > > > > > This change also affects the entropy of allocations. Wi= th 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 wa= s necessary for > > > > > > > > > > 32 bit ARM targets with low RAM at the time. It's defin= itely not > > > > > > > > > > acceptable for 64 bit targets. > > > > > > > > > > > > > > > > > > Thanks for the report. Is it 32 bit only or 64 bit is als= o impacted? > > > > > > > > > If I understand the code correctly, it expects the addres= s allocated > > > > > > > > > by malloc() is kind of randomized, right? > > > > > > > > > > > > > > > > Yes, correct, the test expects a certain level of address r= andomization. > > > > > > > > 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 r= eturned > > > > > > > by malloc(), but it doesn't break any real Android applicatio= n, 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 outwe= ighs > > > > > > > security could opt this feature out. However I'm wondering wh= ether > > > > > > > Android should implement a general address randomization mech= anism > > > > > > > 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 K= ees 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/rando= mize_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-b= it > > > > > 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. > > > > > > > > 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 formul= a > > > > doesn't take into account huge page. ARM64 adjusts the mmap_rnd_bit= s > > > > 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 addresse= s > > > > 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 th= is, > > > > 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. Aha! You are correct, the test is using compat syscalls and your suggestion at https://lore.kernel.org/all/CAHbLzkoL6sCDciHqVMJga288853CHdOT= a5thOPQ9SHKSqjGGPQ@mail.gmail.com/ seems to fix it. I started a complete set of presubmit tests at https://android-review.googlesource.com/c/kernel/common/+/2916065 and will report the results tomorrow morning but I expect them to pass now. Thanks, Suren. > > > > > > /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 fo= r > > > 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/= tests/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 alignme= nt > > > > 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