Received: by 10.223.185.116 with SMTP id b49csp2976380wrg; Mon, 5 Mar 2018 11:48:48 -0800 (PST) X-Google-Smtp-Source: AG47ELuV7WPerO0Ba+jBFuQKS8IQ+oOSBt/B42jZo/e2z6hDSCeWbs7w8/q27oEbQ/hCyBmFoAgR X-Received: by 10.101.101.5 with SMTP id x5mr12910607pgv.195.1520279327920; Mon, 05 Mar 2018 11:48:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520279327; cv=none; d=google.com; s=arc-20160816; b=b7pj/Xx1fSmeG5oqucuogs+l88j2H60pbD4FajcANnXTrOc1I7C5vo9KCFwdtEm+aI Oe4LWMlMxLZKx+ksNEaTeMNv5bEz1Usgjqd4rpXOkHbAqmxZEAv9nUECo5xyeFo4+VYB c0h1JKESb1bi0UTISt0sQfMHFcqzBk2R7OeJfTl3X2mkRvwCEEpxwg0KkkKAy2YN3/Z7 nde26BtVhl0NHY9qF8BEbGLFoaFWPe67aC70KJFDNiF2rBtXjOMwfhyofVfRfMCQiZ3B i5Bjc6kASsfVPFMzLris75jn2BRt0BvDjYBR2ri7fKWA/n1UGytIMiAgT4VPnJgEvLhd BsBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature :arc-authentication-results; bh=NUGs0g03mUoRfdZHTgoxNpSdN2ILTucSwNe5pIE5zY0=; b=fGRe4C5M/ltSOyb/YAce3Ry97RW6fC9ktwNveUl9CU0gmbKZ5FSTs00EsABrLIFCLs zSCXm+KsfvGR0mSqJE7vzhg4loMt+VIfFDtAWGkiS/WAJKUjqZts0kaS90NX4cQ7TyTy KXPLnGcMSzRoYBxpOjq2JnW4SHB6ie2vzn10ZoBdVBxUJAc0cJiUzO8dg+RuGtxt+PiJ 0kQlnrRMyWu9Dg/XYDBj2+UEvG5XCc1s4K0MrE1raaTonBiigz7RlF91qQe6FaeZLh3w c0LwLy/JDYbajKVh3q9F7B7RD3FsiqP3Mqy/udcVBcUK9Ev1AlbGsC2CiTxSmObOSuQh mO+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=ug7jKrfl; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 30-v6si9651593pld.724.2018.03.05.11.48.33; Mon, 05 Mar 2018 11:48:47 -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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=ug7jKrfl; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752759AbeCETrm (ORCPT + 99 others); Mon, 5 Mar 2018 14:47:42 -0500 Received: from bombadil.infradead.org ([198.137.202.133]:34624 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751764AbeCETrk (ORCPT ); Mon, 5 Mar 2018 14:47:40 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Transfer-Encoding :Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=NUGs0g03mUoRfdZHTgoxNpSdN2ILTucSwNe5pIE5zY0=; b=ug7jKrfldr0SETgBEYvfxh7CdL O8RwOSwToO6Kv17XRHNv9y8Whvh+pNHkfW+v1Aw/6oHrCEeFffLQzsP1CSEHNwHD/eAgLQx33lyHt ZgPa0Qgq4sf7wIZYtMyyhr4tmQTOCy6yK3rVEnf0Ce16YLA856g33aPiLsQrBuuJwqU8DZrMIC1yv J5zSbY0TTISL3wH2+fdf2L2DQlwqlKOColfVHhPJNQfQynvSb0347Afqn+uYcntRTc/RjMwJkbD1+ GdzekGhm40T2ItlNqJWlqE5IBsJwf9yfEmgSbLx2ftlUVQKVl2BOsr7mNgmjixXlxTyuVHDet94KB OX+coC0w==; Received: from willy by bombadil.infradead.org with local (Exim 4.89 #1 (Red Hat Linux)) id 1esw52-0005oL-Fq; Mon, 05 Mar 2018 19:47:28 +0000 Date: Mon, 5 Mar 2018 11:47:28 -0800 From: Matthew Wilcox To: Ilya Smith Cc: Daniel Micay , 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 Subject: Re: [RFC PATCH] Randomization of address chosen by mmap. Message-ID: <20180305194728.GB10418@bombadil.infradead.org> References: <55C92196-5398-4C19-B7A7-6C122CD78F32@gmail.com> <20180228183349.GA16336@bombadil.infradead.org> <2CF957C6-53F2-4B00-920F-245BEF3CA1F6@gmail.com> <20180304034704.GB20725@bombadil.infradead.org> <20180304205614.GC23816@bombadil.infradead.org> <7FA6631B-951F-42F4-A7BF-8E5BB734D709@gmail.com> <20180305162343.GA8230@bombadil.infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.9.2 (2017-12-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Mar 05, 2018 at 10:27:32PM +0300, Ilya Smith wrote: > > On 5 Mar 2018, at 19:23, Matthew Wilcox wrote: > > On Mon, Mar 05, 2018 at 04:09:31PM +0300, Ilya Smith wrote: > >> I’m analysing that approach and see much more problems: > >> - each time you call mmap like this, you still increase count of vmas as my > >> patch did > > > > Umm ... yes, each time you call mmap, you get a VMA. I'm not sure why > > that's a problem with my patch. I was trying to solve the problem Daniel > > pointed out, that mapping a guard region after each mmap cost twice as > > many VMAs, and it solves that problem. > > > The issue was in VMAs count as Daniel mentioned. > The more count, the harder walk tree. I think this is fine The performance problem Daniel was mentioning with your patch was not with the number of VMAs but with the scattering of addresses across the page table tree. > >> - the entropy you provide is like 16 bit, that is really not so hard to brute > > > > It's 16 bits per mapping. I think that'll make enough attacks harder > > to be worthwhile. > > Well yes, its ok, sorry. I just would like to have 32 bit entropy maximum some day :) We could put 32 bits of padding into the prot argument on 64-bit systems (and obviously you need a 64-bit address space to use that many bits). The thing is that you can't then put anything else into those pages (without using MAP_FIXED). > >> - if you unmap/remap one page inside region, field vma_guard will show head > >> or tail pages for vma, not both; kernel don’t know how to handle it > > > > There are no head pages. The guard pages are only placed after the real end. > > Ok, we have MG where G = vm_guard, right? so when you do vm_split, > you may come to situation - m1g1m2G, how to handle it? I mean when M is > split with only one page inside this region. How to handle it? I thought I covered that in my earlier email. Using one letter per page, and a five-page mapping with two guard pages: MMMMMGG. Now unmap the fourth page, and the VMA gets split into two. You get: MMMGMGG. > > I can't agree with that. The user has plenty of opportunities to get > > randomness; from /dev/random is the easiest, but you could also do timing > > attacks on your own cachelines, for example. > > I think the usual case to use randomization for any mmap or not use it at all > for whole process. So here I think would be nice to have some variable > changeable with sysctl (root only) and ioctl (for greedy processes). I think this functionality can just as well live inside libc as in the kernel. > Well, let me summary: > My approach chose random gap inside gap range with following strings: > > + addr = get_random_long() % ((high - low) >> PAGE_SHIFT); > + addr = low + (addr << PAGE_SHIFT); > > Could be improved limiting maximum possible entropy in this shift. > To prevent situation when attacker may massage allocations and > predict chosen address, I randomly choose memory region. I’m still > like my idea, but not going to push it anymore, since you have yours now. > > Your idea just provide random non-mappable and non-accessable offset > from best-fit region. This consumes memory (1GB gap if random value > is 0xffff). But it works and should work faster and should resolve the issue. umm ... 64k * 4k is a 256MB gap, not 1GB. And it consumes address space, not memory. > My point was that current implementation need to be changed and you > have your own approach for that. :) > Lets keep mine in the mind till better times (or worse?) ;) > Will you finish your approach and upstream it? I'm just putting it out there for discussion. If people think this is the right approach, then I'm happy to finish it off. If the consensus is that we should randomly pick addresses instead, I'm happy if your approach gets merged.