Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp941952imu; Wed, 28 Nov 2018 01:58:30 -0800 (PST) X-Google-Smtp-Source: AFSGD/XtHq4q0UBPk/Xh/Jz4QBgySNVH8I0REmhlqXxzgUk2FHfV0o61dAERMa7TCqCs7g2nid3k X-Received: by 2002:a63:4342:: with SMTP id q63mr32769845pga.63.1543399110187; Wed, 28 Nov 2018 01:58:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543399110; cv=none; d=google.com; s=arc-20160816; b=Kq0T7dI+7TQ7E2heFByVijYQeuf0/H5LvfbBSrq/x9PpFGClarf6s92YeCGeLk4sx+ 3o13DRmi/VkGrH9m6NoGaJikiCAOH0mjP0vcmC0wFp9fRqY58Sv3bM53NsxW1pTVws1U tbRrNu/o2qj5tPZhrgq1qYIdOkrheYIx8YVJwS8pas+9T3gkjiSjE98GVFJZ3fnG/G/X uyhYj/2Fu3d3WWL3Nclh24teyUKyT9qDAOtXzy9smhytmEKKGT1elp7qKzkQKb/iy4Yk 7CTHkEDvbWnbr1LEp6y6fD0Kgu29EWexeS68dvzqZEPDULuHVJ/nGrJqpwr3qK5b5NwC 0sHQ== 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; bh=bWM0VEXJKBpZOnGdt7f8e3tfmI8q7vKb9RZN2VGHieI=; b=AP5YUe0vKu/Wmd9CslHC8EExT+b3A+ChUPI/UfxBiTYyBEsCbEVTyfyjCm6WKSV8qc wQDjubDPH7ZBZs0PolkRs7ImXJv1LN6qgblRHAhBTkIJP5gaXK6melPCUOfyXUUL5hYd 1zGUnTQkzYuvFV98aQxgwnrlS/TakssewEfQdX5JXHJ2YtlA3atQhQcpE0zSH4ygAmVy 9Gx7nJ+WSzfxhf5UqDOwJVsVKV1bKp1yJmrBOAyrdnc8XbsvLpIztkfUK/FMszkFR87L EOQDZKHGYcwFjBsLwHizuQkyBt7YwhV6hs9oGRnfMClPx2rHU+P/AXGi/0VjVCHk1Ksa CbsA== ARC-Authentication-Results: i=1; mx.google.com; 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 u6si7027400plz.220.2018.11.28.01.58.15; Wed, 28 Nov 2018 01:58:30 -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; 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 S1728134AbeK1U6W (ORCPT + 99 others); Wed, 28 Nov 2018 15:58:22 -0500 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:33266 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727541AbeK1U6W (ORCPT ); Wed, 28 Nov 2018 15:58:22 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 84B393610; Wed, 28 Nov 2018 01:57:17 -0800 (PST) Received: from edgewater-inn.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 527303F5A0; Wed, 28 Nov 2018 01:57:17 -0800 (PST) Received: by edgewater-inn.cambridge.arm.com (Postfix, from userid 1000) id 699C61AE0B6A; Wed, 28 Nov 2018 09:57:35 +0000 (GMT) Date: Wed, 28 Nov 2018 09:57:35 +0000 From: Will Deacon To: Nadav Amit Cc: Rick Edgecombe , Andrew Morton , Andy Lutomirski , linux-mm , LKML , Kernel Hardening , naveen.n.rao@linux.vnet.ibm.com, anil.s.keshavamurthy@intel.com, David Miller , Masami Hiramatsu , rostedt@goodmis.org, mingo@redhat.com, ast@kernel.org, daniel@iogearbox.net, jeyu@kernel.org, netdev@vger.kernel.org, ard.biesheuvel@linaro.org, jannh@google.com, kristen@linux.intel.com, dave.hansen@intel.com, deneen.t.dock@intel.com Subject: Re: [PATCH 0/2] =?utf-8?B?RG9u4oCZ?= =?utf-8?Q?t?= leave executable TLB entries to freed pages Message-ID: <20181128095734.GA23467@arm.com> References: <20181128000754.18056-1-rick.p.edgecombe@intel.com> <449E6648-5599-476D-8136-EE570101F930@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <449E6648-5599-476D-8136-EE570101F930@gmail.com> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 27, 2018 at 05:21:08PM -0800, Nadav Amit wrote: > > On Nov 27, 2018, at 5:06 PM, Nadav Amit wrote: > > > >> On Nov 27, 2018, at 4:07 PM, Rick Edgecombe wrote: > >> > >> Sometimes when memory is freed via the module subsystem, an executable > >> permissioned TLB entry can remain to a freed page. If the page is re-used to > >> back an address that will receive data from userspace, it can result in user > >> data being mapped as executable in the kernel. The root of this behavior is > >> vfree lazily flushing the TLB, but not lazily freeing the underlying pages. > >> > >> There are sort of three categories of this which show up across modules, bpf, > >> kprobes and ftrace: > >> > >> 1. When executable memory is touched and then immediatly freed > >> > >> This shows up in a couple error conditions in the module loader and BPF JIT > >> compiler. > > > > Interesting! > > > > Note that this may cause conflict with "x86: avoid W^X being broken during > > modules loading”, which I recently submitted. > > I actually have not looked on the vmalloc() code too much recent, but it > seems … strange: > > void vm_unmap_aliases(void) > { > > ... > mutex_lock(&vmap_purge_lock); > purge_fragmented_blocks_allcpus(); > if (!__purge_vmap_area_lazy(start, end) && flush) > flush_tlb_kernel_range(start, end); > mutex_unlock(&vmap_purge_lock); > } > > Since __purge_vmap_area_lazy() releases the memory, it seems there is a time > window between the release of the region and the TLB flush, in which the > area can be allocated for another purpose. This can result in a > (theoretical) correctness issue. No? If __purge_vmap_area_lazy() returns false, then it hasn't freed the memory, so we only invalidate the TLB if 'flush' is true in that case. If __purge_vmap_area_lazy() returns true instead, then it takes care of the TLB invalidation before the freeing. Will