Received: by 10.223.185.116 with SMTP id b49csp6428595wrg; Wed, 28 Feb 2018 09:14:29 -0800 (PST) X-Google-Smtp-Source: AH8x227cMzvruJC2aAkDK2whuSZYWSUCXKdtH0Q5JXN+nma/45iGAp4Vx9nBmj1wl2zzUcAOO6Fy X-Received: by 10.99.112.20 with SMTP id l20mr15102302pgc.412.1519838069611; Wed, 28 Feb 2018 09:14:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519838069; cv=none; d=google.com; s=arc-20160816; b=nel9MjYwT5w2OnKkZFPmbcya9W2oU4zDBcSl7AH3vFLL+dRxq5xvMqbkk56HvLUqK5 deCCF5iBY8rmdPTEd5bE/OuWqqhnlP47IvgjZfMrWZPzhX+vZSmW332VkmBJQVOlHwCA bcRV0ekmskeXy1oR3EslWRuZBu0BsQYUNgxIWtxSyrhIEhwoAAHQqh27N4uSCkW8dXwB +8St1yAzvgFhm1zvIY/ynPvVqGgKUpH0PNNUmXXvgcZJQpcgjm2/lwMJ3/0GF3keOggw NnWBASsEoOPM4F9rYXblT/VLSNaVkWab1eMbHfANAGXV1irpCOn7F0hW30z39cXdvuIc JKxg== 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=ZapZwXxUBCAF2rGOx9l0tXAvPjZUIaPXb5FxqCpZK+M=; b=ULpP+6tK6LPfXcCGt8HFQlEKOTbfKi37Gwua5rodymfqxKt7N/NsWT2eoL7vEFzsW/ PrAWm5yKOUyQjTvzLxrZr3Et0taAYbJNQ9FcOfVc44E+i4yswjbxNRPydmOpa3XG8KpP yw+c1LmJyKAYD462zwcQHfTAHI8ix/bGer8YfFAmA0ISfYv5t8Qed4da2ZTdrOmHRvBC VONWIvZo1vUZOK1p/mHXu2wLz6XVIlKlIbYfW5FtOVKhmXwG3bdPx/t2N9F/G7/AARaM wfsoqMiEmg2+NOBtBIZlNIcIVVuONYOuLczH3u8SSpMCvOT/4Rf38e9kMT8RWSsStFHn 9nVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=jQiAnod1; 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 33-v6si1484191pls.710.2018.02.28.09.14.14; Wed, 28 Feb 2018 09:14:29 -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=jQiAnod1; 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 S933183AbeB1RNH (ORCPT + 99 others); Wed, 28 Feb 2018 12:13:07 -0500 Received: from mail-lf0-f65.google.com ([209.85.215.65]:37562 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752578AbeB1RNF (ORCPT ); Wed, 28 Feb 2018 12:13:05 -0500 Received: by mail-lf0-f65.google.com with SMTP id y19so4626166lfd.4 for ; Wed, 28 Feb 2018 09:13:04 -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=ZapZwXxUBCAF2rGOx9l0tXAvPjZUIaPXb5FxqCpZK+M=; b=jQiAnod1en2Ebvl7BBV/OJ86Emb9sqxlg1RYPjbP6ObgE1LfH5i6/g3kCJRiKi1N8L dcPVBV8jxGkEZlPRtgKM92luriaIoNYyuqZKenHJQTog3ZtmsDqgWQP503ODoxzc0Woy OWTmFJxKIlEY4vSbwuhr3LqxkLfICKMjBCH4dGu8icKZiXYauaNc9E9CygDWK47jjs40 kLSSw7VuMY9xYtMamufj0ZOdHcZ+kSrwjxXUTBZ2/8OpHIsPgqPaJwURLwmCJIMoAiY3 mwZQLnFSCMvFcuT9b8Vv/Ycy0x9SF2OqvC8koseo6Udl5AWwyqetPjW/xVUthaFo7dp5 AdiA== 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=ZapZwXxUBCAF2rGOx9l0tXAvPjZUIaPXb5FxqCpZK+M=; b=MTYX8SnlJYmaxIt71TH2zPH1umdRajVJFMMtlLWkY8pDP8MeLjG/rRR3AnRvCiSfeH +aeobJ4hjRNoHOGtiZr32GDwCXLX0GWY/azehjirX6KMrieW/vjLRDOtm79JpImTQUZA iBYy/bGlWc7vzY/ExaFgTZPqS2Ku4ppGHYbsQgtmsgKVGHT+43cHQjgSK4pmsJe81kQ5 +95EYW4s0RMUiSc0VbKTHAhj7X4qyMhFbqe6i/fh5wOETclB98Vylo1zU9doyRnXs3cB UAfFVU0fn+HKmv93g7lktExLF3odJizQauRCqlgwPze1aNmyxCkE+I9d2z7d3bIyx4Rh tqPQ== X-Gm-Message-State: APf1xPCoarTBbRPmRQ1i+IEc9L0FA8dTBHVzQY4R96wWb9xqphoorcz/ my7Pspcy73779Vo1VlsginQ= X-Received: by 10.46.34.70 with SMTP id i67mr13151265lji.143.1519837983398; Wed, 28 Feb 2018 09:13:03 -0800 (PST) Received: from [10.0.36.41] ([31.44.93.2]) by smtp.gmail.com with ESMTPSA id x23sm459385ljc.33.2018.02.28.09.13.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 28 Feb 2018 09:13:01 -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: Wed, 28 Feb 2018 20:13:00 +0300 Cc: Andrew Morton , Dan Williams , Michal Hocko , "Kirill A. Shutemov" , Jan Kara , Jerome Glisse , Hugh Dickins , Matthew Wilcox , Helge Deller , Andrea Arcangeli , Oleg Nesterov , Linux-MM , LKML , Kernel Hardening Content-Transfer-Encoding: quoted-printable Message-Id: <55C92196-5398-4C19-B7A7-6C122CD78F32@gmail.com> References: <20180227131338.3699-1-blackzert@gmail.com> To: Kees Cook 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 Kees, Thanks for your time spent on that! > On 27 Feb 2018, at 23:52, Kees Cook wrote: >=20 > I'd like more details on the threat model here; if it's just a matter > of .so loading order, I wonder if load order randomization would get a > comparable level of uncertainty without the memory fragmentation, > like: > https://android-review.googlesource.com/c/platform/bionic/+/178130/2 > If glibc, for example, could do this too, it would go a long way to > improving things. Obviously, it's not as extreme as loading stuff all > over the place, but it seems like the effect for an attack would be > similar. The search _area_ remains small, but the ordering wouldn't be > deterministic any more. >=20 I=E2=80=99m afraid library order randomization wouldn=E2=80=99t help = much, there are several=20 cases described in chapter 2 here:=20 http://www.openwall.com/lists/oss-security/2018/02/27/5 when it is possible to bypass ASLR.=20 I=E2=80=99m agree library randomizaiton is a good improvement but after = my patch I think not much valuable. On my GitHub = https://github.com/blackzert/aslur=20 I provided tests and will make them 'all in one=E2=80=99 chain later. > It would be worth spelling out the "not recommended" bit some more > too: this fragments the mmap space, which has some serious issues on > smaller address spaces if you get into a situation where you cannot > allocate a hole large enough between the other allocations. >=20 I=E2=80=99m agree, that's the point. >> vm_unmapped_area(struct vm_unmapped_area_info *info) >> { >> + if (current->flags & PF_RANDOMIZE) >> + return unmapped_area_random(info); >=20 > I think this will need a larger knob -- doing this by default is > likely to break stuff, I'd imagine? Bikeshedding: I'm not sure if this > should be setting "3" for /proc/sys/kernel/randomize_va_space, or a > separate one like /proc/sys/mm/randomize_mmap_allocation. I will improve it like you said. It looks like a better option. >> + // first lets find right border with unmapped_area_topdown >=20 > Nit: kernel comments are /* */. (It's a good idea to run patches > through scripts/checkpatch.pl first.) >=20 Sorry, I will fix it. Thanks! >> + if (!rb_parent(prev)) >> + return -ENOMEM; >> + vma =3D rb_entry(rb_parent(prev), >> + struct vm_area_struct, vm_rb); >> + if (prev =3D=3D vma->vm_rb.rb_right) { >> + gap_start =3D vma->vm_prev ? >> + vm_end_gap(vma->vm_prev) : 0; >> + goto check_current_down; >> + } >> + } >> + } >=20 > Everything from here up is identical to the existing > unmapped_area_topdown(), yes? This likely needs to be refactored > instead of copy/pasted, and adjust to handle both unmapped_area() and > unmapped_area_topdown(). >=20 This part also keeps =E2=80=98right_vma' as a border. If it is ok, that = combined version will return vma struct, I=E2=80=99ll do it. >> + /* Go back up the rbtree to find next candidate node = */ >> + while (true) { >> + struct rb_node *prev =3D &vma->vm_rb; >> + >> + if (!rb_parent(prev)) >> + BUG(); // this should not happen >> + vma =3D rb_entry(rb_parent(prev), >> + struct vm_area_struct, vm_rb); >> + if (prev =3D=3D vma->vm_rb.rb_left) { >> + gap_start =3D = vm_end_gap(vma->vm_prev); >> + gap_end =3D vm_start_gap(vma); >> + if (vma =3D=3D right_vma) >=20 > mm/mmap.c: In function =E2=80=98unmapped_area_random=E2=80=99: > mm/mmap.c:1939:8: warning: =E2=80=98vma=E2=80=99 may be used = uninitialized in this > function [-Wmaybe-uninitialized] > if (vma =3D=3D right_vma) > ^ Thanks, fixed! >> + break; >> + goto check_current_up; >> + } >> + } >> + } >=20 > What are the two phases here? Could this second one get collapsed into > the first? >=20 Let me explain.=20 1. we use current implementation to get larger address. Remember it as=20= =E2=80=98right_vma=E2=80=99. 2. we walk tree from mm->mmap what is lowest vma. 3. we check if current vma gap satisfies length and low/high constrains 4. if so, we call random() to decide if we choose it. This how we = randomly choos e vma and gap 5. we walk tree from lowest vma to highest and ignore subtrees with less = gap.=20 we do it until reach =E2=80=98right_vma=E2=80=99 Once we found gap, we may randomly choose address inside it. >> + addr =3D get_random_long() % ((high - low) >> PAGE_SHIFT); >> + addr =3D low + (addr << PAGE_SHIFT); >> + return addr; >>=20 >=20 > How large are the gaps intended to be? Looking at the gaps on > something like Xorg they differ a lot. Sorry, I can=E2=80=99t get clue. What's the context? You tried patch or = whats the case? Thanks, Ilya