Received: by 10.223.185.116 with SMTP id b49csp711950wrg; Sat, 3 Mar 2018 06:00:11 -0800 (PST) X-Google-Smtp-Source: AG47ELs0bktX1Kgb2Viib4urUtPen+O/qU8eLTfF/Hxb0aHQL5Lc+C5k86ldDuxIklMUeE47fkf2 X-Received: by 10.99.175.8 with SMTP id w8mr7229515pge.390.1520085611598; Sat, 03 Mar 2018 06:00:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520085611; cv=none; d=google.com; s=arc-20160816; b=bzxPOTz/DzC0EVo6GyUh6uB5qhdgoDRYy4j40wXF8edQtKeynEvgQcoI50l1vNYMOn ipNTXNOTmaO8N4afWiuHG4kVABu++DW9GPxlUUHQ6kagAJG+xRR5VvYzsSekanJOqEQ0 u+mhO/g4BVRs4ot0MbvHFX5AepSntxdWCKLdGSB5Dwi2hTe8xj0SMWFcDYfnJDcF009T 9Af91xBeS0hGLIdZSRJhzndynbABCm/+8+zeF1X4u+B5VXIQ09J/u18JNA6dqfzxpYNb vCQiOU166NTUMR2lu6TnFYw35tyEnjcahPl4qz14CZMeRq2o1TwVg5DAMde8745KJUaR 8M+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature:arc-authentication-results; bh=96DGP6ZbXyck9pxHya3Acq1KJi9dsOu4NVoGIgPibA8=; b=DszvsmsxwHWgva6AwKUwoEPaas2U8caSvxXKZh0z1X7SNFvDCvqIEDxZSapXpsEE3z jxOQWs9q8QO3tO5uxKBj/ReUliQVcruWU8x67azMV1iVFncByqOAKJ9tR/gDZMA5KsiD /Yte3oWKHQc7pK7g8bMu1lSnIS55qs13+ct+VNrGbOHwFz1LlGu2xkTT+Z3hHtN8zpwR X1w/yWIMyl3Fy4t3my8Ep9SC6I4DBiT0/Zk1uN98QBsRDAEGVeevWG5ICrK0dxKO33y9 A1PG+Zgl1P6DIXDRLW3Q5xwPLmZQ67s3Y7pu7ksawvtecTaNLMaKb1DYEyer0vMHaD5i eOXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=LfcFa0ms; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id a15si6719410pff.139.2018.03.03.05.59.57; Sat, 03 Mar 2018 06:00:11 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=LfcFa0ms; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1752082AbeCCN6q (ORCPT + 99 others); Sat, 3 Mar 2018 08:58:46 -0500 Received: from mail-lf0-f52.google.com ([209.85.215.52]:42150 "EHLO mail-lf0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751989AbeCCN6o (ORCPT ); Sat, 3 Mar 2018 08:58:44 -0500 Received: by mail-lf0-f52.google.com with SMTP id t204so17106240lff.9 for ; Sat, 03 Mar 2018 05:58:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=96DGP6ZbXyck9pxHya3Acq1KJi9dsOu4NVoGIgPibA8=; b=LfcFa0ms0LCJQERavVj9MSX0EDo8Io+43IpREtod11i4miuaVPLA6rbXDsPV2WVU1z zEWaxHu5CMspCwXzxx0GEcUwIjsDkWaVluaYTw90CwWnAa/D73sWKDBSoHIqH70oUFL8 0t5uxebxi3GGF97mwQxKXle2ZiAKo+YuGyO7+E3XLcZtq0ehhKltAUZsIGjaZFQk/Tvf DacIBOKaVAInwtge9pbAoqgH/pN68WuPup2UI/JvGHBfGu3eGbwhAWHcvp5K/WQDvezN AgLSPAUjoWc3MXOAgTm1lhaLtBNt/qp4QgkLpFByQBkulXeR+7qkpqrJh2h9kLt5bW8Z H90g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=96DGP6ZbXyck9pxHya3Acq1KJi9dsOu4NVoGIgPibA8=; b=rkR2KSLHqGHmrT4/xxg23YIOLw08mONQe1FWscNfxaUUtU9V/VSZtiyyKPho4Ad+Tb k81AUxCvZlIowZL8X48iO5XGMwLkemmv11iV5vu/f6VBk75DHwBImJ5XEGdIWmmVKTC3 iP38F14ssFujtP0ZLsCgjuSMPM27AlZ+2UP5H/cIiE30Cr2oY2pfITA2I9Hluvf6Ugjf Jb2x9DDQ65CPrAkdaFgu9fFsrfpx5jPsCH5P8uKYbpopAbSWsbZE6W/8ykjosATot7Q0 rCnoWNBb+aLP3m490hhTSgQT/5PJolL/QifKu1ZplXhR/sFt6v/ElxJkxe9PTT2qUvXm kVIA== X-Gm-Message-State: AElRT7GHe3RaYx66+rny6L7od/I4M6JFjQ53tsj+xoynRtTVtgaYXNBu 1+p6w0n/SrLNP7vffRTsWGI= X-Received: by 10.25.40.212 with SMTP id o203mr5930819lfo.103.1520085523265; Sat, 03 Mar 2018 05:58:43 -0800 (PST) Received: from [192.168.1.3] (broadband-188-255-70-164.moscow.rt.ru. [188.255.70.164]) by smtp.gmail.com with ESMTPSA id 11sm1777426ljr.27.2018.03.03.05.58.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 03 Mar 2018 05:58:41 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 11.2 \(3445.5.20\)) Subject: Re: [RFC PATCH] Randomization of address chosen by mmap. From: Ilya Smith In-Reply-To: Date: Sat, 3 Mar 2018 16:58:40 +0300 Cc: Matthew Wilcox , Kees Cook , Andrew Morton , Dan Williams , Michal Hocko , "Kirill A. Shutemov" , Jan Kara , Jerome Glisse , Hugh Dickins , Helge Deller , Andrea Arcangeli , Oleg Nesterov , Linux-MM , LKML , Kernel Hardening Content-Transfer-Encoding: quoted-printable Message-Id: <2CF957C6-53F2-4B00-920F-245BEF3CA1F6@gmail.com> References: <20180227131338.3699-1-blackzert@gmail.com> <55C92196-5398-4C19-B7A7-6C122CD78F32@gmail.com> <20180228183349.GA16336@bombadil.infradead.org> To: Daniel Micay X-Mailer: Apple Mail (2.3445.5.20) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello Daniel, thanks for sharing you experience! > On 1 Mar 2018, at 00:02, Daniel Micay wrote: >=20 > I don't think it makes sense for the kernel to attempt mitigations to > hide libraries. The best way to do that is in userspace, by having the > linker reserve a large PROT_NONE region for mapping libraries (both at > initialization and for dlopen) including a random gap to act as a > separate ASLR base. Why this is the best and what is the limit of this large region? Let=E2=80=99s think out of the box. What you say here means you made a separate memory region for libraries = without=20 changing kernel. But the basic idea - you have a separate region for = libraries=20 only. Probably you also want to have separate regions for any thread = stack, for=20 mmaped files, shared memory, etc. This allows to protect memory regions = of=20 different types from each other. It is impossible to implement this = without=20 keeping the whole memory map. This map should be secure from any leak = attack to=20 prevent ASLR bypass. The only one way to do it is to implement it in the = kernel=20 and provide different syscalls like uselib or allocstack, etc. This one = is=20 really hard in current kernel implementation. My approach was to hide memory regions from attacker and from each = other. > If an attacker has library addresses, it's hard to > see much point in hiding the other libraries from them. In some cases attacker has only one leak for whole attack. And we should = do the best to make even this leak useless. > It does make > sense to keep them from knowing the location of any executable code if > they leak non-library addresses. An isolated library region + gap is a > feature we implemented in CopperheadOS and it works well, although we > haven't ported it to Android 7.x or 8.x. This one interesting to know and I would like to try to attack it, but = it's out of the scope of current conversation. > I don't think the kernel can > bring much / anything to the table for it. It's inherently the > responsibility of libc to randomize the lower bits for secondary > stacks too. I think any bit of secondary stack should be randomized to provide = attacker as less information as we can. > Fine-grained randomized mmap isn't going to be used if it causes > unpredictable levels of fragmentation or has a high / unpredictable > performance cost. Lets pretend any chosen address is pure random and always satisfies = request. At=20 some time we failed to mmap new chunk with size N. What does this means? = This=20 means that all chunks with size of N are occupied and we even can=E2=80=99= t find place=20 between them. Now lets count already allocated memory. Let=E2=80=99s = pretend on all of=20 these occupied chunks lies one page minimum. So count of these pages is=20= TASK_SIZE / N. Total bytes already allocated is PASGE_SIZE * TASK_SIZE / = N. Now=20 we can calculate. TASK_SIZE is 2^48 bytes. PAGE_SIZE 4096. If N is 1MB,=20= allocated memory minimum 1125899906842624, that is very big number. Ok. = is N is=20 256 MB, we already consumed 4TB of memory. And this one is still ok. if = N is=20 1GB we allocated 1GB and it looks like a problem. If we allocated 1GB of = memory=20 we can=E2=80=99t mmap chunk size of 1GB. Sounds scary, but this is = absolutely bad case=20 when we consume 1 page on 1GB chunk. In reality this number would be = much=20 bigger and random according this patch. Here lets stop and think - if we know that application going to consume = memory.=20 The question here would be can we protect it? Attacker will know he has = a good=20 probability to guess address with read permissions. In this case ASLR = may not=20 work at all. For such applications we can turn off address randomization = or=20 decrease entropy level since it any way will not help much. Would be good to know whats the performance costs you can see here. Can you please tell? > I don't think it makes sense to approach it > aggressively in a way that people can't use. The OpenBSD randomized > mmap is a fairly conservative implementation to avoid causing > excessive fragmentation. I think they do a bit more than adding random > gaps by switching between different 'pivots' but that isn't very high > benefit. The main benefit is having random bits of unmapped space all > over the heap when combined with their hardened allocator which > heavily uses small mmap mappings and has a fair bit of malloc-level > randomization (it's a bitmap / hash table based slab allocator using > 4k regions with a page span cache and we use a port of it to Android > with added hardening features but we're missing the fine-grained mmap > rand it's meant to have underneath what it does itself). >=20 So you think OpenBSD implementation even better? It seems like you like = it after all. > The default vm.max_map_count =3D 65530 is also a major problem for = doing > fine-grained mmap randomization of any kind and there's the 32-bit > reference count overflow issue on high memory machines with > max_map_count * pid_max which isn't resolved yet. I=E2=80=99ve read a thread about it. This one is what should be fixed = anyway. Thanks, Ilya