Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp8846356imu; Tue, 4 Dec 2018 15:28:32 -0800 (PST) X-Google-Smtp-Source: AFSGD/Wwh0Ui3YdIiVYwnyY915TwWUBV4fcBOpmAW9vbJU3qGrPEYKHhqLBRjHQldMiy70qsMiDD X-Received: by 2002:a17:902:7044:: with SMTP id h4mr21703614plt.35.1543966112256; Tue, 04 Dec 2018 15:28:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543966112; cv=none; d=google.com; s=arc-20160816; b=IL/EzyC2O5b09xDGqDlnL0EtskoxDNz2bUc8/38x6j2v8XXVNPk4eL49CEd/mfJ42v e8vBm5RdoOnnpFA2N/scI33ihMTz/tZLCxWksWPDAIMHnoEtMehQKZRDR3i36Ab7m0Z3 mTgzygMPooSgtrtRe1eBra6DSkbCiyRtH4Vyu0a2M61tRneisLlHiPz1coSbw0lYpXas gP618AzUNwkEQUWq+/irRpMWVpyo50qr4rvayqbzv4AQhfLOssDtWKGzLSKZgIi/Q8Bs ZzJclPJ0mbraF5bjW6gscBiCqF5zf+MTJdl5JSJxbSrdRROGPb//U/eQxO4pm4Iwxs8e 5/Jw== 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; bh=k4gRrwf0vwBfra6ZEDTewPtpNHK5kwzpNqTQwIAEL2Q=; b=b1RX1vI+iTzVT3a9KCBMc2neNX26gm9T+TxE+qVLh4oDPqIo2wPRuw0Q2HLvbFhoJW YSdfIYo50uRIni/R2g7bCuM8ihzilxT4fAOb8q0+TZErourIzGcP0/EPNGS22QRqzZKC hS2xfIFV0Ort4F2Rct7L7DE7FvYmt2VFMC9BJq+R1v160WuG6j4S5gs4Fc6X/hFnbPrF 7UnpNWwczVwCdmennCnUQqLdHwIQRps9IWm3TCfv1PUlqv1VCxey8z3+/EoWHxSUIDrm O8sVETeNtpWxgE8vNEW2uijQzCI5uJ3im9RR/HbM1t17MV1hTXbCqHW19h3PD8+PIW46 9dlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amacapital-net.20150623.gappssmtp.com header.s=20150623 header.b=2M99FNvR; 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 l12si20320082plc.0.2018.12.04.15.28.16; Tue, 04 Dec 2018 15:28:32 -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=@amacapital-net.20150623.gappssmtp.com header.s=20150623 header.b=2M99FNvR; 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 S1726369AbeLDX1j (ORCPT + 99 others); Tue, 4 Dec 2018 18:27:39 -0500 Received: from mail-pg1-f193.google.com ([209.85.215.193]:41871 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725904AbeLDX1j (ORCPT ); Tue, 4 Dec 2018 18:27:39 -0500 Received: by mail-pg1-f193.google.com with SMTP id 70so8080295pgh.8 for ; Tue, 04 Dec 2018 15:27:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amacapital-net.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=k4gRrwf0vwBfra6ZEDTewPtpNHK5kwzpNqTQwIAEL2Q=; b=2M99FNvRaGGgZI1h3vp7GB2gd23fTl8jYf4soLuY1MGKWwr3pz0ZVVhLfFXQ4FnoQu QM2mQtuvzp5G7vdzb57yjxGD2RPXO82//r7ezuZkHwySXR9kL65gg2fzIsKprBUeKbhF 2+mbdArVxne11lu8S1bM04Xw0AOgOhmwYr5bGcrNXWDxVHfa2rjLztK6aaivy+wUhWGA gvMLR+D/n235o0fP951cgmf6Ajnf9ZBBJAoMqk/CjsWTzV0+FNtJpdp1CuRF+aNdc01U K+zGwsWQVgTRdtFDNUAOEOH30EtOtE83qMiDIBygSf2bULOvKKPqc4cabW0JL0g0UlmT P0nA== 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=k4gRrwf0vwBfra6ZEDTewPtpNHK5kwzpNqTQwIAEL2Q=; b=COcX7QfnWD2I7936GjjUuWbTsEWzdm2g9DFpbI4XXmeeGFv3FS5ootGM4aigVdo2XS qyEjjQw8CuGKgMCZrnVGswid1I041JCEonFDkjUbzjdpuDofL4OXbBPpi2PpS2TtntfF 8KUvTxfaBulzhqtSIEI+6FrMBZ9k6C3hgJUakAFZ4J015E2rRGMBTba6Wzt67V0noUi7 Dr3J9N1FGKsYwONIMDf0ewERR2t0ESRDB9sfAbtdewoSLX923hK3abEN51xCY8bAgTS9 ISIotqRvrdlcCKJ6PnZbko1PhPxWXTA/eLSFJO7wLvv7HwOUJO7FgjCPGdJhQWIYy5K1 RrKQ== X-Gm-Message-State: AA+aEWZeBdMJwZVbj8mmjbGsiNlTT0aMWZ1U6eDJJ+iwHjiN/VGenDkd cmVvpHTOSwx9QhEJdv7IE+4M3A== X-Received: by 2002:a63:3546:: with SMTP id c67mr18807821pga.284.1543966058294; Tue, 04 Dec 2018 15:27:38 -0800 (PST) Received: from ?IPv6:2600:1010:b05a:3e8f:cd65:4915:ea8d:fbab? ([2600:1010:b05a:3e8f:cd65:4915:ea8d:fbab]) by smtp.gmail.com with ESMTPSA id m67sm31736273pfb.25.2018.12.04.15.27.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Dec 2018 15:27:36 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (1.0) Subject: Re: [PATCH 1/2] vmalloc: New flag for flush before releasing pages From: Andy Lutomirski X-Mailer: iPhone Mail (16B92) In-Reply-To: <20CC2F71-308D-42E2-8C54-F64D7CC3863F@gmail.com> Date: Tue, 4 Dec 2018 15:27:35 -0800 Cc: Andy Lutomirski , 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 , jeyu@kernel.org, Network Development , Ard Biesheuvel , Jann Horn , Kristen Carlson Accardi , Dave Hansen , "Dock, Deneen T" , Peter Zijlstra Content-Transfer-Encoding: quoted-printable Message-Id: References: <20181128000754.18056-1-rick.p.edgecombe@intel.com> <20181128000754.18056-2-rick.p.edgecombe@intel.com> <4883FED1-D0EC-41B0-A90F-1A697756D41D@gmail.com> <08141F66-F3E6-4CC5-AF91-1ED5F101A54C@gmail.com> <20CC2F71-308D-42E2-8C54-F64D7CC3863F@gmail.com> To: Nadav Amit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Dec 4, 2018, at 2:48 PM, Nadav Amit wrote: >> On Dec 4, 2018, at 11:48 AM, Andy Lutomirski wrote: >>=20 >> On Tue, Dec 4, 2018 at 11:45 AM Nadav Amit wrote: >>>> On Dec 4, 2018, at 10:56 AM, Andy Lutomirski wrote: >>>>=20 >>>> On Mon, Dec 3, 2018 at 5:43 PM Nadav Amit wrote:= >>>>>> On Nov 27, 2018, at 4:07 PM, Rick Edgecombe wrote: >>>>>>=20 >>>>>> Since vfree will lazily flush the TLB, but not lazily free the underl= ying pages, >>>>>> it often leaves stale TLB entries to freed pages that could get re-us= ed. This is >>>>>> undesirable for cases where the memory being freed has special permis= sions such >>>>>> as executable. >>>>>=20 >>>>> So I am trying to finish my patch-set for preventing transient W+X map= pings >>>>> from taking space, by handling kprobes & ftrace that I missed (thanks a= gain for >>>>> pointing it out). >>>>>=20 >>>>> But all of the sudden, I don=E2=80=99t understand why we have the prob= lem that this >>>>> (your) patch-set deals with at all. We already change the mappings to m= ake >>>>> the memory writable before freeing the memory, so why can=E2=80=99t we= make it >>>>> non-executable at the same time? Actually, why do we make the module m= emory, >>>>> including its data executable before freeing it??? >>>>=20 >>>> All the code you're looking at is IMO a very awkward and possibly >>>> incorrect of doing what's actually necessary: putting the direct map >>>> the way it wants to be. >>>>=20 >>>> Can't we shove this entirely mess into vunmap? Have a flag (as part >>>> of vmalloc like in Rick's patch or as a flag passed to a vfree variant >>>> directly) that makes the vunmap code that frees the underlying pages >>>> also reset their permissions? >>>>=20 >>>> Right now, we muck with set_memory_rw() and set_memory_nx(), which >>>> both have very awkward (and inconsistent with each other!) semantics >>>> when called on vmalloc memory. And they have their own flushes, which >>>> is inefficient. Maybe the right solution is for vunmap to remove the >>>> vmap area PTEs, call into a function like set_memory_rw() that resets >>>> the direct maps to their default permissions *without* flushing, and >>>> then to do a single flush for everything. Or, even better, to cause >>>> the change_page_attr code to do the flush and also to flush the vmap >>>> area all at once so that very small free operations can flush single >>>> pages instead of flushing globally. >>>=20 >>> Thanks for the explanation. I read it just after I realized that indeed t= he >>> whole purpose of this code is to get cpa_process_alias() >>> update the corresponding direct mapping. >>>=20 >>> This thing (pageattr.c) indeed seems over-engineered and very unintuitiv= e. >>> Right now I have a list of patch-sets that I owe, so I don=E2=80=99t hav= e the time >>> to deal with it. >>>=20 >>> But, I still think that disable_ro_nx() should not call set_memory_x(). >>> IIUC, this breaks W+X of the direct-mapping which correspond with the mo= dule >>> memory. Does it ever stop being W+X?? I=E2=80=99ll have another look. >>=20 >> Dunno. I did once chase down a bug where some memory got freed while >> it was still read-only, and the results were hilarious and hard to >> debug, since the explosion happened long after the buggy code >> finished. >=20 > This piece of code causes me pain and misery. >=20 > So, it turns out that the direct map is *not* changed if you just change > the NX-bit. See change_page_attr_set_clr(): >=20 > /* No alias checking for _NX bit modifications */ > checkalias =3D (pgprot_val(mask_set) | pgprot_val(mask_clr)) !=3D _= PAGE_NX; >=20 > How many levels of abstraction are broken in the way? What would happen > if somebody tries to change the NX-bit and some other bit in the PTE? > Luckily, I don=E2=80=99t think someone does=E2=80=A6 at least for now. >=20 > So, again, I think the change I proposed makes sense. nios2 does not have > set_memory_x() and it will not be affected. >=20 > [ I can add a comment, although I don=E2=80=99t have know if nios2 has an N= X bit, > and I don=E2=80=99t find any code that defines PTEs. Actually where is pte= _present() > of nios2 being defined? Whatever. ] >=20 At least rename the function, then. The last thing we need is for disable_ro= _nx to *enable* NX.=