Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp10677870imu; Thu, 6 Dec 2018 05:10:52 -0800 (PST) X-Google-Smtp-Source: AFSGD/WPY0pMfmb/EidaSpDhzC6ZDcccIhaqWbX/0TG6nkuzuDY01f6NGUS6NXcuZeolBlJRUU5o X-Received: by 2002:a63:101d:: with SMTP id f29mr24290840pgl.38.1544101852463; Thu, 06 Dec 2018 05:10:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544101852; cv=none; d=google.com; s=arc-20160816; b=wW0+2vxFNinmX+RVD+gBWdlvUQrPB8r2azY7SaODEcAChEOc1HNSz4EM2P/7dEqsFm xp91vbq363vFmUwppEovOz30v7IFg259J8aKW9lqhJBcFO8INKa2TJQw1GubrD4GtoFC xUKQASvybfqBLfwWCzC8cKYj20pKVzeX005pfd/cE7p+CLQKdD5Bxo58sm7XFkRvzOMB 5p0HnROGD6yhqZfk8fxCm5o0u9GvKksmkZze5XUl25kdCli7Q2qub68JFIemeO1sbbw1 2baralP1kzNPWawTvvfOEgg0JJ2TZwi47fGyqe2vg5AOHVG8eDfiM4oXrWhjXpkv9gFJ 38dw== 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:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=QwVkOSgXbQMOKiknSAOqqNuLccGEGGsQAXL+x1qPF6A=; b=vPbJcRatWlyNrUG4ja2PG9mBgK4n+ovGAT1e2yA3C1EMQAbnl/umbflC1oOOqjoQO7 1HTdhC6+3tngnSt6FVwuPwIv6DEWvTe6P0O9JifqYq0CpxBQvO7lsWhVBjUIVpSZFH1i ksXdevoSVwR436FRFLUoQfZ8fnJbLpfyBetZ1qVbtPjI6Ur9b4KvDWyRvHsDdzhVw62k j7zW+ax61hifpHfuHRUlYTbZyCLAbbRBstMNrsIj9mvK/Lxq+nC7R2xjc473ESNtYQFN OQmSk3h+9Z+VBOkSWqTppFF0GLzwjz+gepSJpI6mWBZYnSY+eevlsGfkBjpxf03YeabE qKPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1A89uC6A; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o7si252641pgg.118.2018.12.06.05.10.29; Thu, 06 Dec 2018 05:10:52 -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=@kernel.org header.s=default header.b=1A89uC6A; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729560AbeLFNJ2 (ORCPT + 99 others); Thu, 6 Dec 2018 08:09:28 -0500 Received: from mail.kernel.org ([198.145.29.99]:45390 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729369AbeLFNJ2 (ORCPT ); Thu, 6 Dec 2018 08:09:28 -0500 Received: from devnote (NE2965lan1.rev.em-net.ne.jp [210.141.244.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6F33120838; Thu, 6 Dec 2018 13:09:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544101767; bh=f1bDdZtr9VDioZCEqAt/67HERLJ0WUZkqRvMMmrNW1o=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=1A89uC6A8NndbXNSvzeYdVf8ddPOA/wq6ZGM1zzExAPJ8kR5EtVj3/D5xySCWJttj 2wqJj34wrSoGQxPo7VgyyFms+2mkYWLxiVPGL4fvVmdob+0bAyL474RRv7R4HZZ47Z dpdUAMyX5wSHOKvGrYfS8xHwOl2wsQijmSIg+9bs= Date: Thu, 6 Dec 2018 22:09:23 +0900 From: Masami Hiramatsu To: Nadav Amit Cc: Ingo Molnar , , , "H. Peter Anvin" , Thomas Gleixner , Borislav Petkov , Andy Lutomirski , Nadav Amit , Dave Hansen , Peter Zijlstra , , , , Masami Hiramatsu Subject: Re: [PATCH v7 09/14] x86/kprobes: Instruction pages initialization enhancements Message-Id: <20181206220923.20e5b0593fa1a8038eb710fd@kernel.org> In-Reply-To: <20181205013408.47725-10-namit@vmware.com> References: <20181205013408.47725-1-namit@vmware.com> <20181205013408.47725-10-namit@vmware.com> X-Mailer: Sylpheed 3.5.0 (GTK+ 2.24.30; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 4 Dec 2018 17:34:03 -0800 Nadav Amit wrote: > This patch is a preparatory patch for a following patch that makes > module allocated pages non-executable. The patch sets the page as > executable after allocation. > > In the future, we may get better protection of executables. For example, > by using hypercalls to request the hypervisor to protect VM executable > pages from modifications using nested page-tables. This would allow > us to ensure the executable has not changed between allocation and > its write-protection. Sounds interesting! > > While at it, do some small cleanup of what appears to be unnecessary > masking. Looks good to me. Acked-by: Masami Hiramatsu Thanks! > > Cc: Masami Hiramatsu > Signed-off-by: Nadav Amit > --- > arch/x86/kernel/kprobes/core.c | 24 ++++++++++++++++++++---- > 1 file changed, 20 insertions(+), 4 deletions(-) > > diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c > index c33b06f5faa4..ca0118d3b3e8 100644 > --- a/arch/x86/kernel/kprobes/core.c > +++ b/arch/x86/kernel/kprobes/core.c > @@ -431,8 +431,20 @@ void *alloc_insn_page(void) > void *page; > > page = module_alloc(PAGE_SIZE); > - if (page) > - set_memory_ro((unsigned long)page & PAGE_MASK, 1); > + if (page == NULL) > + return NULL; > + > + /* > + * First make the page read-only, and then only then make it executable > + * to prevent it from being W+X in between. > + */ > + set_memory_ro((unsigned long)page, 1); > + > + /* > + * TODO: Once additional kernel code protection mechanisms are set, ensure > + * that the page was not maliciously altered and it is still zeroed. > + */ > + set_memory_x((unsigned long)page, 1); > > return page; > } > @@ -440,8 +452,12 @@ void *alloc_insn_page(void) > /* Recover page to RW mode before releasing it */ > void free_insn_page(void *page) > { > - set_memory_nx((unsigned long)page & PAGE_MASK, 1); > - set_memory_rw((unsigned long)page & PAGE_MASK, 1); > + /* > + * First make the page non-executable, and then only then make it > + * writable to prevent it from being W+X in between. > + */ > + set_memory_nx((unsigned long)page, 1); > + set_memory_rw((unsigned long)page, 1); > module_memfree(page); > } > > -- > 2.17.1 > -- Masami Hiramatsu