Received: by 2002:a05:7412:2a91:b0:fc:a2b0:25d7 with SMTP id u17csp513235rdh; Wed, 14 Feb 2024 03:57:57 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWlNgwaVCpSAcEbcbPfeU6h5NWx5aIXCzvW3CLpUJ/KMaO1kMkCb5PUT2RuQX/JqWlui2QUYBHxomPW4t/NzRHUgYW+/ystGmH3V662xA== X-Google-Smtp-Source: AGHT+IFElTQSdLoLoQmkMcVdzPr9mL4i7mGCcRvnLv0hUN7qYGY8VBZYkeJhvwEjafVX5AZDPnIq X-Received: by 2002:a17:90a:34c5:b0:296:a464:ee3a with SMTP id m5-20020a17090a34c500b00296a464ee3amr2254641pjf.29.1707911877238; Wed, 14 Feb 2024 03:57:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707911877; cv=pass; d=google.com; s=arc-20160816; b=jpoyQDZpV8Tj5rcvV9tDFzXoo3QWe4UXHo53dgWD3+i0WH4m3m6CLm4RL+mMEfUCMg 2QvYqvi+xoFH/uEkEiQyxl+EPt50nfwPEZtSWrNcL7SAyJaDZQppkxEG823rIQfV0yJl +SnUH1Nhi7CJHSxSx80JNBl0O0H+xN+hPDrhl5OYE7lCdQuR9QOdSL6VxcRFKkAseIrn 2MmCIadCVFs3eW8Lb4EYVgiZ9EekVSOam0iGa4lNIFFZSgmV7COn2paOoMb1dwCQVMpQ FM41Ioum4F5Tv9nvjqmLEukBdeILIUER+VNC/CtIxAf/iG5T309WcKHs9wIH/gHMkTCk lHaA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=0VLO9XCCRrOyvGlH5NhYWuh2+3Y4ipxYRLoEzFH+z+0=; fh=Bpt3/IKdNfcoJH2w8U/V46gkW6SmN4JcP9ibWeWvRd0=; b=iR97Js7guCcHkn3kTEPdFusuUTbCYPZE6R0O5IlNaAk+VwBhc1lVpycCAbmz52wmAN L0EbzNiFE4r4q08vybdKj6V5CPfyooffAf+MTNddZwoyyHQUcK/SFX/hH24ahnzwrEf/ zqDg+ocLqZsEVXKnVYUO9UCkd91CWemNgHn9ON+53i1JvKBSHy0AxALzzsGSenKZdhYs q/FnLGsFtTIbPF2BPvBjrie2nne3BrkkncSb2zqOTSTsr9+F6E3i248Sy0lquzWP0iLy kvNa/27agAiau57I8VodkrM3oxA195BthWW2cSKx/crz0RtgtIzVUeeTGO2JMWntudh1 IpmQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-65139-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65139-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id bf3-20020a17090b0b0300b002955c8628ecsi1080029pjb.31.2024.02.14.03.57.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 03:57:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65139-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-65139-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65139-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 644D7B2A156 for ; Wed, 14 Feb 2024 11:36:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 495DA1B7E2; Wed, 14 Feb 2024 11:36:16 +0000 (UTC) Received: from frasgout12.his.huawei.com (frasgout12.his.huawei.com [14.137.139.154]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 966771B5AA; Wed, 14 Feb 2024 11:36:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=14.137.139.154 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707910575; cv=none; b=Z4xw6xzdYi1TKIJtGkqW+cGLLEne7/D+OYPoIiJk9A1Rq+sIDEKqPJJHLajwCYtPJPwEyNDhpJbC6HPObwOtr40qXY85mpBOmZ1wwXLLKGJTMIDb3vrVdf4IbxaPAvLI+olwSOrIhSspZaCchTKh3vi9F5JuPy/hpUVU16J5tr0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707910575; c=relaxed/simple; bh=BhUpmPPCX9aFET6nbzIg0SSMZ201vmY12lSaJWzujcQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Gp9xTl8yvutEr8RaMNMsuiZqs9IIZGCEG5BAOSmcYvOUYdJQmXMHzWLRfzLPDoi9cAuGfoyQcsA0eKQYErmk0Sm0vwR22C7VdoB3grJ3HRdI3b0b4C2gk5amrE/eEFxKMq4hpC2ef+C0uUkfIeRKdArDNcCPyNmnvIlitqN3V+I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=14.137.139.154 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.18.186.29]) by frasgout12.his.huawei.com (SkyGuard) with ESMTP id 4TZbHz5GM5z9yLnN; Wed, 14 Feb 2024 19:17:03 +0800 (CST) Received: from mail02.huawei.com (unknown [7.182.16.47]) by mail.maildlp.com (Postfix) with ESMTP id A0B5B1400DA; Wed, 14 Feb 2024 19:36:10 +0800 (CST) Received: from huaweicloud.com (unknown [10.45.156.69]) by APP1 (Coremail) with SMTP id LxC2BwAHshp7pcxlDJx9Ag--.51624S4; Wed, 14 Feb 2024 12:36:10 +0100 (CET) From: Petr Tesarik To: Jonathan Corbet , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org (maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)), "H. Peter Anvin" , Andy Lutomirski , Oleg Nesterov , Peter Zijlstra , Xin Li , Arnd Bergmann , Andrew Morton , Rick Edgecombe , Kees Cook , "Masami Hiramatsu (Google)" , Pengfei Xu , Josh Poimboeuf , Ze Gao , "Kirill A. Shutemov" , Kai Huang , David Woodhouse , Brian Gerst , Jason Gunthorpe , Joerg Roedel , "Mike Rapoport (IBM)" , Tina Zhang , Jacob Pan , linux-doc@vger.kernel.org (open list:DOCUMENTATION), linux-kernel@vger.kernel.org (open list) Cc: Roberto Sassu , petr@tesarici.cz, Petr Tesarik Subject: [PATCH v1 2/8] sbm: x86: execute target function on sandbox mode stack Date: Wed, 14 Feb 2024 12:35:10 +0100 Message-Id: <20240214113516.2307-3-petrtesarik@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240214113516.2307-1-petrtesarik@huaweicloud.com> References: <20240214113516.2307-1-petrtesarik@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:LxC2BwAHshp7pcxlDJx9Ag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F0xvEx4A2jsIEc7CjxVAFwI0_Gr1j6F4UJbIY CTnIWIevJa73UjIFyTuYvjfUn3kuUUUUU X-CM-SenderInfo: hshw23xhvd2x3n6k3tpzhluzxrxghudrp/ From: Petr Tesarik Allocate and map a separate stack for sandbox mode in arch_sbm_init(). Switch to this stack in arch_sbm_exec(). Store the address of the stack as arch-specific state. On X86_64, RSP is never used to locate thread-specific data, so it is safe to change its value. If the sandbox is preempted by an interrupt, RSP is saved by switch_to() and restored when the sandbox task is scheduled again. The original kernel stack pointer is restored when the sandbox function returns. Since the stack switch mechanism is implemented only for 64-bit, make CONFIG_HAVE_ARCH_SBM depend on X86_64 for now. Leave it under "config X86", because it would be possible to implement a 32-bit variant. Signed-off-by: Petr Tesarik --- arch/x86/Kconfig | 2 +- arch/x86/include/asm/sbm.h | 2 ++ arch/x86/kernel/sbm/Makefile | 6 ++++++ arch/x86/kernel/sbm/call_64.S | 40 +++++++++++++++++++++++++++++++++++ arch/x86/kernel/sbm/core.c | 17 ++++++++++++++- 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 arch/x86/kernel/sbm/call_64.S diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 41fa4ab84c15..090d46c7ee7c 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -188,7 +188,7 @@ config X86 select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPAT select HAVE_ARCH_COMPAT_MMAP_BASES if MMU && COMPAT select HAVE_ARCH_PREL32_RELOCATIONS - select HAVE_ARCH_SBM + select HAVE_ARCH_SBM if X86_64 select HAVE_ARCH_SECCOMP_FILTER select HAVE_ARCH_THREAD_STRUCT_WHITELIST select HAVE_ARCH_STACKLEAK diff --git a/arch/x86/include/asm/sbm.h b/arch/x86/include/asm/sbm.h index 01c8d357550b..ed214c17af06 100644 --- a/arch/x86/include/asm/sbm.h +++ b/arch/x86/include/asm/sbm.h @@ -16,12 +16,14 @@ /** * struct x86_sbm_state - Run-time state of the environment. * @pgd: Sandbox mode page global directory. + * @stack: Sandbox mode stack. * * One instance of this union is allocated for each sandbox and stored as SBM * instance private data. */ struct x86_sbm_state { pgd_t *pgd; + unsigned long stack; }; #endif /* defined(CONFIG_HAVE_ARCH_SBM) && defined(CONFIG_SANDBOX_MODE) */ diff --git a/arch/x86/kernel/sbm/Makefile b/arch/x86/kernel/sbm/Makefile index 92d368b526cd..62c3e85c14a4 100644 --- a/arch/x86/kernel/sbm/Makefile +++ b/arch/x86/kernel/sbm/Makefile @@ -8,3 +8,9 @@ # obj-y := core.o + +### +# 64 bit specific files +ifeq ($(CONFIG_X86_64),y) + obj-y += call_64.o +endif diff --git a/arch/x86/kernel/sbm/call_64.S b/arch/x86/kernel/sbm/call_64.S new file mode 100644 index 000000000000..245d0dddce73 --- /dev/null +++ b/arch/x86/kernel/sbm/call_64.S @@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2023-2024 Huawei Technologies Duesseldorf GmbH + * + * Author: Petr Tesarik + * + * SandBox Mode (SBM) low-level x86_64 assembly. + */ + +#include +#include + +.code64 +.section .entry.text, "ax" + +/* + * arguments: + * rdi .. SBM state (kernel address) + * rsi .. func + * rdx .. args + * rcx .. top of sandbox stack + */ +SYM_FUNC_START(x86_sbm_exec) + /* + * Set up the sandbox stack: + * 1. Store the old stack pointer at the top of the sandbox stack, + * where various unwinders can find it and link back to the + * kernel stack. + */ + sub $8, %rcx + mov %rsp, (%rcx) + mov %rcx, %rsp + + mov %rdx, %rdi /* args */ + CALL_NOSPEC rsi + + pop %rsp + + RET +SYM_FUNC_END(x86_sbm_exec) diff --git a/arch/x86/kernel/sbm/core.c b/arch/x86/kernel/sbm/core.c index b775e3b387b1..de6986801148 100644 --- a/arch/x86/kernel/sbm/core.c +++ b/arch/x86/kernel/sbm/core.c @@ -17,6 +17,9 @@ #define GFP_SBM_PGTABLE (GFP_KERNEL | __GFP_ZERO) #define PGD_ORDER get_order(sizeof(pgd_t) * PTRS_PER_PGD) +asmlinkage int x86_sbm_exec(struct x86_sbm_state *state, sbm_func func, + void *args, unsigned long sbm_tos); + static inline phys_addr_t page_to_ptval(struct page *page) { return PFN_PHYS(page_to_pfn(page)) | _PAGE_TABLE; @@ -182,6 +185,15 @@ int arch_sbm_init(struct sbm *sbm) if (err) return err; + state->stack = __get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER); + if (!state->stack) + return -ENOMEM; + + err = map_range(state, state->stack, state->stack + THREAD_SIZE, + PAGE_SHARED); + if (err) + return err; + return 0; } @@ -238,11 +250,14 @@ void arch_sbm_destroy(struct sbm *sbm) free_pgd(state->pgd); free_pages((unsigned long)state->pgd, PGD_ORDER); } + free_pages(state->stack, THREAD_SIZE_ORDER); free_page((unsigned long)state); sbm->private = NULL; } int arch_sbm_exec(struct sbm *sbm, sbm_func func, void *args) { - return func(args); + struct x86_sbm_state *state = sbm->private; + + return x86_sbm_exec(state, func, args, state->stack + THREAD_SIZE); } -- 2.34.1