Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1961129imu; Sat, 22 Dec 2018 09:03:24 -0800 (PST) X-Google-Smtp-Source: AFSGD/VVg3wcA0h3+yviNMMmXkO071WIlrCwFBrl4xPvb0gHWd3NF9IETPWOpCzKMlJZMz82GfkA X-Received: by 2002:a62:b24a:: with SMTP id x71mr7273921pfe.148.1545498204259; Sat, 22 Dec 2018 09:03:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545498204; cv=none; d=google.com; s=arc-20160816; b=1IUcpuxeYRcAMsfq6OvoHVt+EKQ74C9pWTRk/7Z3QgkfNKHfXBD6PCq1uk5MziIY0J M8OzVdUEoajUjyN52sbYjJvPHZwQAEUS4qMEkqxysP08VvbbI8+ma8AS7UaxG0fut+cc tLgTor3BMEN9k8VD9vviqmSuJ5BrQpar5JneELeqyWfPRDHKZRdWc/EGSk3UwgaXWL/6 z60PN9P8Hkxoj0UMl+QzL8+wOcU1RccZbVU8MYa/ymtgHMfcVAlJC+ycTHx9EnkAdlm9 +YwjySrA49hcqoyQKsUGCcJSmug8A6R+XKFYLSUIXWNAIS8MSDc7iia1StjExS25hIfm +YFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=K0cEd50nZUVEkzjOzC/s/uIvzDgfy0uY9tzji/RbJzE=; b=e5HZu6yg+uzXJ+1ggdsA+8mtrm01Ery21et+HSFBE1ugOmrEtwxT1Vk9hHwYeC31N+ CSwBlxctz/kfQFUirOIm//dr5ocmzJll6IOQe3FF72GJTjZXBdGx+/0Fpa5SriLSWn4w Fr0zQZ6FzD/xzQMVZyGlUerdbJDwJHtOiTeycG1Qxk/Nd/z0vUP/52DyUyndyfUmTzSP 8buftZs50xTJEzlzJpX6S+d6xt/5nKyBxZsQ/DILTsrvxArv6LH184oIlniP1TWmvYT2 Qvhh5DFt8Zep2tZzUUE6/mBkIP7nmSaJ7CjaZZwVzMkTuj8ibT1JEH3Le14JqDozY/7p UsbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BYpMR85O; 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=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 27si23340600pgp.135.2018.12.22.09.03.09; Sat, 22 Dec 2018 09:03:24 -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=@linaro.org header.s=google header.b=BYpMR85O; 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=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390156AbeLURZ4 (ORCPT + 99 others); Fri, 21 Dec 2018 12:25:56 -0500 Received: from mail-it1-f195.google.com ([209.85.166.195]:54415 "EHLO mail-it1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731522AbeLURZ4 (ORCPT ); Fri, 21 Dec 2018 12:25:56 -0500 Received: by mail-it1-f195.google.com with SMTP id i145so7905297ita.4 for ; Fri, 21 Dec 2018 09:25:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=K0cEd50nZUVEkzjOzC/s/uIvzDgfy0uY9tzji/RbJzE=; b=BYpMR85Osp/mg5VSCCEDH8v8uY5255k3mLtbb/Ot2RjMPC+r5B1kQBGsO6+ox0/8sA ttxEYhAF+ush3/ssvADiO5smboMyRyv53xWbmBeAa9CHjWZnFLgudqS1ms7po5iR1I8H QL2Faewfg0zDLigvnzaY5eezeMi2rQn3VIZLU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=K0cEd50nZUVEkzjOzC/s/uIvzDgfy0uY9tzji/RbJzE=; b=h6Hgj3/MQozr5eO6jst9mswt80y67+qDMFfOrHUGhW2NrRM/f+ysx0xGFMkLYeP2eO 8bIXAi5IsyN562C3qxrFk8CeyOtpjrrhj9Z0QQmy9ww/Gg2b8nJCZ+Gflr7R/GYohedM aNrjevzuCwmsaWXkDvUGKNZRDe6zEi0rZSLspMN57gA+1o73F7QBaxDbhZjIglELWxwM RddqDm/D0fsp6h638RMg6aHuJarJfGCUcxsDMtkg4TtLfr4X+o0CNAM8FgS78XGCxhEd YHDwZ5+9Jfdcaj5QNxCjhlRMOaZYbli0mFzJHhqeQHWzfP40Rv0uGKnxjs+J/plsckj3 hkFQ== X-Gm-Message-State: AA+aEWa+EFpXmXp6dRIK9TPgjCiGOfdm52eOsKO4SBFm6GuV8cqzthFF d1twVYAD/ZW5IE7FLMaJfz2CzYjq3eLI5PjJFDoJag== X-Received: by 2002:a24:710:: with SMTP id f16mr2225282itf.121.1545413154724; Fri, 21 Dec 2018 09:25:54 -0800 (PST) MIME-Version: 1.0 References: <20181212000354.31955-1-rick.p.edgecombe@intel.com> <20181212000354.31955-2-rick.p.edgecombe@intel.com> In-Reply-To: From: Ard Biesheuvel Date: Fri, 21 Dec 2018 18:25:43 +0100 Message-ID: Subject: Re: [PATCH v2 1/4] vmalloc: New flags for safe vfree on special perms To: Andy Lutomirski Cc: Rick Edgecombe , Andrew Morton , Will Deacon , Linux-MM , LKML , Kernel Hardening , "Naveen N . Rao" , Anil S Keshavamurthy , "David S. Miller" , Masami Hiramatsu , Steven Rostedt , Ingo Molnar , Alexei Starovoitov , Daniel Borkmann , Jessica Yu , Nadav Amit , Network Development , Jann Horn , Kristen Carlson Accardi , Dave Hansen , "Dock, Deneen T" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 21 Dec 2018 at 18:12, Andy Lutomirski wrote: > > > On Dec 21, 2018, at 9:39 AM, Ard Biesheuvel = wrote: > > > >> On Wed, 12 Dec 2018 at 03:20, Andy Lutomirski wrote: > >> > >> On Tue, Dec 11, 2018 at 4:12 PM Rick Edgecombe > >> wrote: > >>> > >>> This adds two new flags VM_IMMEDIATE_UNMAP and VM_HAS_SPECIAL_PERMS, = for > >>> enabling vfree operations to immediately clear executable TLB entries= to freed > >>> pages, and handle freeing memory with special permissions. > >>> > >>> In order to support vfree being called on memory that might be RO, th= e vfree > >>> deferred list node is moved to a kmalloc allocated struct, from where= it is > >>> today, reusing the allocation being freed. > >>> > >>> arch_vunmap is a new __weak function that implements the actual unmap= ping and > >>> resetting of the direct map permissions. It can be overridden by more= efficient > >>> architecture specific implementations. > >>> > >>> For the default implementation, it uses architecture agnostic methods= which are > >>> equivalent to what most usages do before calling vfree. So now it is = just > >>> centralized here. > >>> > >>> This implementation derives from two sketches from Dave Hansen and An= dy > >>> Lutomirski. > >>> > >>> Suggested-by: Dave Hansen > >>> Suggested-by: Andy Lutomirski > >>> Suggested-by: Will Deacon > >>> Signed-off-by: Rick Edgecombe > >>> --- > >>> include/linux/vmalloc.h | 2 ++ > >>> mm/vmalloc.c | 73 +++++++++++++++++++++++++++++++++++++---= - > >>> 2 files changed, 69 insertions(+), 6 deletions(-) > >>> > >>> diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h > >>> index 398e9c95cd61..872bcde17aca 100644 > >>> --- a/include/linux/vmalloc.h > >>> +++ b/include/linux/vmalloc.h > >>> @@ -21,6 +21,8 @@ struct notifier_block; /* in notifie= r.h */ > >>> #define VM_UNINITIALIZED 0x00000020 /* vm_struct is not fu= lly initialized */ > >>> #define VM_NO_GUARD 0x00000040 /* don't add guard pag= e */ > >>> #define VM_KASAN 0x00000080 /* has allocated kasan= shadow memory */ > >>> +#define VM_IMMEDIATE_UNMAP 0x00000200 /* flush before relea= sing pages */ > >>> +#define VM_HAS_SPECIAL_PERMS 0x00000400 /* may be freed with = special perms */ > >>> /* bits [20..32] reserved for arch specific ioremap internals */ > >>> > >>> /* > >>> diff --git a/mm/vmalloc.c b/mm/vmalloc.c > >>> index 97d4b25d0373..02b284d2245a 100644 > >>> --- a/mm/vmalloc.c > >>> +++ b/mm/vmalloc.c > >>> @@ -18,6 +18,7 @@ > >>> #include > >>> #include > >>> #include > >>> +#include > >>> #include > >>> #include > >>> #include > >>> @@ -38,6 +39,11 @@ > >>> > >>> #include "internal.h" > >>> > >>> +struct vfree_work { > >>> + struct llist_node node; > >>> + void *addr; > >>> +}; > >>> + > >>> struct vfree_deferred { > >>> struct llist_head list; > >>> struct work_struct wq; > >>> @@ -50,9 +56,13 @@ static void free_work(struct work_struct *w) > >>> { > >>> struct vfree_deferred *p =3D container_of(w, struct vfree_defe= rred, wq); > >>> struct llist_node *t, *llnode; > >>> + struct vfree_work *cur; > >>> > >>> - llist_for_each_safe(llnode, t, llist_del_all(&p->list)) > >>> - __vunmap((void *)llnode, 1); > >>> + llist_for_each_safe(llnode, t, llist_del_all(&p->list)) { > >>> + cur =3D container_of(llnode, struct vfree_work, node)= ; > >>> + __vunmap(cur->addr, 1); > >>> + kfree(cur); > >>> + } > >>> } > >>> > >>> /*** Page table manipulation functions ***/ > >>> @@ -1494,6 +1504,48 @@ struct vm_struct *remove_vm_area(const void *a= ddr) > >>> return NULL; > >>> } > >>> > >>> +/* > >>> + * This function handles unmapping and resetting the direct map as e= fficiently > >>> + * as it can with cross arch functions. The three categories of arch= itectures > >>> + * are: > >>> + * 1. Architectures with no set_memory implementations and no dire= ct map > >>> + * permissions. > >>> + * 2. Architectures with set_memory implementations but no direct = map > >>> + * permissions > >>> + * 3. Architectures with set_memory implementations and direct map= permissions > >>> + */ > >>> +void __weak arch_vunmap(struct vm_struct *area, int deallocate_pages= ) > >> > >> My general preference is to avoid __weak functions -- they don't > >> optimize well. Instead, I prefer either: > >> > >> #ifndef arch_vunmap > >> void arch_vunmap(...); > >> #endif > >> > >> or > >> > >> #ifdef CONFIG_HAVE_ARCH_VUNMAP > >> ... > >> #endif > >> > >> > >>> +{ > >>> + unsigned long addr =3D (unsigned long)area->addr; > >>> + int immediate =3D area->flags & VM_IMMEDIATE_UNMAP; > >>> + int special =3D area->flags & VM_HAS_SPECIAL_PERMS; > >>> + > >>> + /* > >>> + * In case of 2 and 3, use this general way of resetting the = permissions > >>> + * on the directmap. Do NX before RW, in case of X, so there = is no W^X > >>> + * violation window. > >>> + * > >>> + * For case 1 these will be noops. > >>> + */ > >>> + if (immediate) > >>> + set_memory_nx(addr, area->nr_pages); > >>> + if (deallocate_pages && special) > >>> + set_memory_rw(addr, area->nr_pages); > >> > >> Can you elaborate on the intent here? VM_IMMEDIATE_UNMAP means "I > >> want that alias gone before any deallocation happens". > >> VM_HAS_SPECIAL_PERMS means "I mucked with the direct map -- fix it for > >> me, please". deallocate means "this was vfree -- please free the > >> pages". I'm not convinced that all the various combinations make > >> sense. Do we really need both flags? > >> > >> (VM_IMMEDIATE_UNMAP is a bit of a lie, since, if in_interrupt(), it's > >> not immediate.) > >> > >> If we do keep both flags, maybe some restructuring would make sense, > >> like this, perhaps. Sorry about horrible whitespace damage. > >> > >> if (special) { > >> /* VM_HAS_SPECIAL_PERMS makes little sense without deallocate_pages. = */ > >> WARN_ON_ONCE(!deallocate_pages); > >> > >> if (immediate) { > >> /* It's possible that the vmap alias is X and we're about to make > >> the direct map RW. To avoid a window where executable memory is > >> writable, first mark the vmap alias NX. This is silly, since we're > >> about to *unmap* it, but this is the best we can do if all we have to > >> work with is the set_memory_abc() APIs. Architectures should override > >> this whole function to get better behavior. */ > > > > So can't we fix this first? Assuming that architectures that bother to > > implement them will not have executable mappings in the linear region, > > all we'd need is set_linear_range_ro/rw() routines that default to > > doing nothing, and encapsulate the existing code for x86 and arm64. > > That way, we can handle do things in the proper order, i.e., release > > the vmalloc mapping (without caring about the permissions), restore > > the linear alias attributes, and finally release the pages. > > Seems reasonable, except that I think it should be > set_linear_range_not_present() and set_linear_range_rw(), for three > reasons: > > 1. It=E2=80=99s not at all clear to me that we need to keep the linear ma= pping > around for modules. > I'm pretty sure hibernate on arm64 will have to be fixed, since it expects to be able to read all valid pages via the linear map. But we can fix that. > 2. At least on x86, the obvious algorithm to do the free operation > with a single flush requires it. Someone should probably confirm that > arm=E2=80=99s TLB works the same way, i.e. that no flush is needed when > changing from not-present (or whatever ARM calls it) to RW. > Good point. ARM is similar in this regard, although we'll probably clear the access flag rather than unmap the page entirely (which is treated the same way in terms of required TLB management) > 3. Anyone playing with XPFO wants this facility anyway. In fact, with > this change, Rick=E2=80=99s series will more or less implement XPFO for > vmalloc memory :) > > Does that seem reasonable to you? Absolutely.