From patchwork Mon Nov 11 06:27:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 2009407 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=RoDYG4vL; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=LCmRtxst; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xn03369sZz1xyB for ; Mon, 11 Nov 2024 17:27:43 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=6rxsmZtwDE9hveMIBPCsasEv3gNTZZ8QeBClxURXh1g=; b=RoDYG4vLg7oQ21uyFY8DbU91wZ 0lgXzJvAWc3V12WFpn74BSvT88DPnE1CdtbKRSmt1XRsbsOwN7JrKKcQ3HhnK/25gBPdmMGStIasy QIQFTUtdRUoQrTIRjl4OAHV/bieLBCI0MZCoU7WvcF7+R9Fe6Z40NnHFF0kjGxkdl1rul7by4em9c O5uOM2wR6INdWiTLJtNtMw3XFjpVgkZthzfleX6PdY47vR9NkjKtPTsklCPoKGgiDi1UewjQ+TaIL qXLzXDQj5UMCl4cAWjLjczCmyWsvmHawtpfNNrBI9OrpBUnlYGBR2TzoiOS+R6pT4kxfeHJsUfFfN gRFEgG4w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tANtm-0000000GU5P-0hKe; Mon, 11 Nov 2024 06:27:42 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tANtk-0000000GU51-0Ol9 for linux-um@lists.infradead.org; Mon, 11 Nov 2024 06:27:41 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-20cf3e36a76so42567975ad.0 for ; Sun, 10 Nov 2024 22:27:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731306459; x=1731911259; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=6rxsmZtwDE9hveMIBPCsasEv3gNTZZ8QeBClxURXh1g=; b=LCmRtxst+CEMrTbO/Gk1FijQk2OZbyhCAJmPZrP/yzuKfQjiIZNsb7Wo6SM/M45LVP k4GmEOq8ZPTLIov1wzpRnaDjV4vxeqkGio+/IVGHazMPagFVc/QyAsPcUm4XnPxPkQaj InE2KqcxJjf5G9GGkN00de7dPGzyKSWRd0CghezCFg1AVndgAh8jPkA5pLbiNM4L5gSL I1766CuYt0q0ke0quqgHUw6ZbMI+NHVv7XU9EwTmVhuTNPelNafNyZypbajU2dyUZApq j83XtXQeEL7yiKavMIjjhb2KltKIPq/pJJEY1+J5yCWs8tJ7Rb0jztBUfR/fBuv/BCvp P8Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731306459; x=1731911259; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6rxsmZtwDE9hveMIBPCsasEv3gNTZZ8QeBClxURXh1g=; b=S5mmEjCThoOzoLt87ylHdlqJtVU8L4zfZCaq9BCkBBGOnVoHdkeGr1kqdQ1/CE6QmA Q1d2xq3KAi4qq03TkDTPAZ7eieCmlExUmBT9/D2Cx3DpRsdbYwE/OXgwNxhdAzOZb/eJ DYKgojaa4NhU1eIHwrFC9Quijlw7Trq0R79RNtra7c8meyn2qQl24TOAvU6lWp2Dus0y 95dadog72rSaaCbBX4E9+DavhwoFU5Gc/e3iDHI3aTdEVJvT5Kx2NebX8QU8PsQaA/eY jS66w5Ru7Pr2hMcdyvoHNtKfCYZrVMvLQXSjxsIXSOT8WEARlkaP5bg7vRoQa09kUGae Uucw== X-Gm-Message-State: AOJu0YyOjDfPGFU4sDXn5AaWZTwyzRE5455GvoAlWZtZrc0CTnzvxZ5V 39Wrcef7Y/LS+PYdNW4VDO5pZtVho+6one6rnqAtw+qs7qaySfQE X-Google-Smtp-Source: AGHT+IHwpmnY7dnXQcsSzHRDD73tGXzcdssS5VVzqSGyOahZ7QhXBU0+7Fxo/FbpiQKc5P/v4ljTRA== X-Received: by 2002:a17:903:245:b0:20b:5645:d860 with SMTP id d9443c01a7336-211835bf105mr168324925ad.36.1731306458982; Sun, 10 Nov 2024 22:27:38 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21177dc836bsm68741545ad.13.2024.11.10.22.27.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 22:27:38 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id E5712DBA914; Mon, 11 Nov 2024 15:27:36 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com, Alexander Viro , Christian Brauner , Jan Kara , Eric Biederman , Kees Cook , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH v2 01/13] fs: binfmt_elf_efpic: add architecture hook elf_arch_finalize_exec Date: Mon, 11 Nov 2024 15:27:01 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241110_222740_158905_BEC599CF X-CRM114-Status: GOOD ( 11.76 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: FDPIC ELF loader adds an architecture hook at the end of loading binaries to finalize the mapped memory before moving toward exec function. The hook is used by UML under !MMU when translating syscall/ [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:636 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime(at)gmail.com] X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org FDPIC ELF loader adds an architecture hook at the end of loading binaries to finalize the mapped memory before moving toward exec function. The hook is used by UML under !MMU when translating syscall/sysenter instructions before calling execve. Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Cc: Eric Biederman Cc: Kees Cook Cc: linux-fsdevel@vger.kernel.org Cc: linux-mm@kvack.org Signed-off-by: Hajime Tazaki --- fs/binfmt_elf_fdpic.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c index 4fe5bb9f1b1f..ab16fdf475b0 100644 --- a/fs/binfmt_elf_fdpic.c +++ b/fs/binfmt_elf_fdpic.c @@ -175,6 +175,12 @@ static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params, return 0; } +int __weak elf_arch_finalize_exec(struct elf_fdpic_params *exec_params, + struct elf_fdpic_params *interp_params) +{ + return 0; +} + /*****************************************************************************/ /* * load an fdpic binary into various bits of memory @@ -457,6 +463,10 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm) dynaddr); #endif + retval = elf_arch_finalize_exec(&exec_params, &interp_params); + if (retval) + goto error; + finalize_exec(bprm); /* everything is now ready... get the userspace context ready to roll */ entryaddr = interp_params.entry_addr ?: exec_params.entry_addr; From patchwork Mon Nov 11 06:27:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 2009408 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=cCI6GjdT; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=OMBvK0V7; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xn03G4HD5z1xyB for ; Mon, 11 Nov 2024 17:27:54 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=xOVZBEb1qaBKyIOwwQleK43822fSZbq/jfSKIeFdX6E=; b=cCI6GjdTdyj7Vr/6Bn6AIwkeR1 pNWhkjyJpr4Ot/AtjkZQC5ZhQ0hGABQteiPpr2YfDy7o9VwL6iUvKNLZWH63gmt36ZOGIXjy/t3JE JB5M436FaEKiNwOvjnhbTxABbhPR65/j1ZfRYOIWbB11Twzp+39Nq/RGi6xhPi8F3fI3T2CeeG3JA RjYIOvQ4mQKVD93EQcx4j471s8BDhNXByXMJPZn3HOJX0O72vjAc7E/grXpvs8XfQWvj7jrHmp5OR leZ2bpcDKpsfYVr5lZ1B7Kyi6I+/SkgO94GASUV2gYdVBWSavouPMsxToG/IRgTtttG+Ozu2gQL4m KBaJWEbA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tANtw-0000000GU8Q-3aKy; Mon, 11 Nov 2024 06:27:52 +0000 Received: from mail-pg1-x52d.google.com ([2607:f8b0:4864:20::52d]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tANtu-0000000GU7Y-07wi for linux-um@lists.infradead.org; Mon, 11 Nov 2024 06:27:51 +0000 Received: by mail-pg1-x52d.google.com with SMTP id 41be03b00d2f7-7ea68af2f62so3187445a12.3 for ; Sun, 10 Nov 2024 22:27:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731306469; x=1731911269; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xOVZBEb1qaBKyIOwwQleK43822fSZbq/jfSKIeFdX6E=; b=OMBvK0V7w1sOJkNKnpQeTwftz8+q4NMLU3ulNL8EptT+iNq8/6KE10L4ed01KR+AUa B6hjEb13eYj3LZGVHMrEN6J0QHxrP06su0gQL9lLEJUGoFRPAHR67yabsuO5Z741F6KF Jyxlo9nLQVjvxR4usiqYudn48P+SGyCO9MC4ZPqfjm1Jnl0Fdy2cnIZtMixsUJ5XH74e gfAMCnzcnkW98biH7Pzj2R0U4/nFPIyeRTwFInemLGjcdOdW0d8SvfYztWpeiadAQ0aV chYNzoV+bQ/B1GTLa87Ry44kqxnIKeqZxgjGsS0mtlgbpXlTPPqcdrCr5p/JdICqx4RR ab2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731306469; x=1731911269; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xOVZBEb1qaBKyIOwwQleK43822fSZbq/jfSKIeFdX6E=; b=F5UeJ7x7ZTOhw7GzgsqG7fwjvm2fslAB61X+SEa8Dn6HLbcglIXhHeqBsMER3dCKDE q1PtqnvusNWc3x74s/aAZYD17wnn5EZe9H2xuTEsyr221eG34YpIyx0wF9FuQFNdylc5 IM8NYomiDGiFvJOWcfLJyrI5DGqKEo7AbyEtGGEG1MUMW/jll/o/HD4rkm8Opre6I0+K x5XL4LXrmwGixVqeWH2kU8BtvHVcYgWJKeFjqpgaZdw5D5pbr3z3xoeKZA7oBCwsIUIM mYaYuH6eE1l2Se1c7YkVXUIhDo0u5urbeb2KhfIF6avPJeYmmB9lJ1eddPOwFhzPi0rE x5Kg== X-Gm-Message-State: AOJu0YzvY3GuznXX10tqFPfVaGuO1qyWznKL0kmBGLJ17mX5MevShhzh wuExP0GjXdZ4bUumKhnNBH35D/ln/QGtCrqf6cruGN3nhPb+FwY9 X-Google-Smtp-Source: AGHT+IHr4mrXQCJG6ehz4R575ji8XJjfRwnS/MkIAJ3E4bPBw/vNBMDgKXMb0Ui2eNQcCUFveGlOmw== X-Received: by 2002:a05:6a20:4324:b0:1db:f06e:f666 with SMTP id adf61e73a8af0-1dc22b91c5emr16383347637.41.1731306469013; Sun, 10 Nov 2024 22:27:49 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5f8f5d3sm7819534a91.27.2024.11.10.22.27.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 22:27:48 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id 43199DBA916; Mon, 11 Nov 2024 15:27:46 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com, Eric Biederman , Kees Cook , Alexander Viro , Christian Brauner , Jan Kara , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Subject: [RFC PATCH v2 02/13] x86/um: nommu: elf loader for fdpic Date: Mon, 11 Nov 2024 15:27:02 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241110_222750_092316_96E8136C X-CRM114-Status: GOOD ( 17.84 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: As UML supports CONFIG_MMU=n case, it has to use an alternate ELF loader, FDPIC ELF loader. In this commit, we added necessary definitions in the arch, as UML has not been used so far. It also updates [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:52d listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime(at)gmail.com] X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org As UML supports CONFIG_MMU=n case, it has to use an alternate ELF loader, FDPIC ELF loader. In this commit, we added necessary definitions in the arch, as UML has not been used so far. It also updates Kconfig file to use BINFMT_ELF_FDPIC under !MMU environment. Cc: Eric Biederman Cc: Kees Cook Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Cc: linux-mm@kvack.org Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Hajime Tazaki Signed-off-by: Ricardo Koller --- arch/um/include/asm/Kbuild | 1 + arch/um/include/asm/mmu.h | 5 +++++ arch/um/include/asm/ptrace-generic.h | 6 ++++++ arch/x86/um/asm/elf.h | 8 ++++++-- arch/x86/um/asm/module.h | 24 ------------------------ fs/Kconfig.binfmt | 2 +- 6 files changed, 19 insertions(+), 27 deletions(-) delete mode 100644 arch/x86/um/asm/module.h diff --git a/arch/um/include/asm/Kbuild b/arch/um/include/asm/Kbuild index 18f902da8e99..cf8260fdcfe5 100644 --- a/arch/um/include/asm/Kbuild +++ b/arch/um/include/asm/Kbuild @@ -14,6 +14,7 @@ generic-y += irq_work.h generic-y += kdebug.h generic-y += mcs_spinlock.h generic-y += mmiowb.h +generic-y += module.h generic-y += module.lds.h generic-y += param.h generic-y += parport.h diff --git a/arch/um/include/asm/mmu.h b/arch/um/include/asm/mmu.h index a3eaca41ff61..01422b761aa0 100644 --- a/arch/um/include/asm/mmu.h +++ b/arch/um/include/asm/mmu.h @@ -14,6 +14,11 @@ typedef struct mm_context { /* Address range in need of a TLB sync */ unsigned long sync_tlb_range_from; unsigned long sync_tlb_range_to; + +#ifdef CONFIG_BINFMT_ELF_FDPIC + unsigned long exec_fdpic_loadmap; + unsigned long interp_fdpic_loadmap; +#endif } mm_context_t; #endif diff --git a/arch/um/include/asm/ptrace-generic.h b/arch/um/include/asm/ptrace-generic.h index 4696f24d1492..4ff844bcb1cd 100644 --- a/arch/um/include/asm/ptrace-generic.h +++ b/arch/um/include/asm/ptrace-generic.h @@ -29,6 +29,12 @@ struct pt_regs { #define PTRACE_OLDSETOPTIONS 21 +#ifdef CONFIG_BINFMT_ELF_FDPIC +#define PTRACE_GETFDPIC 31 +#define PTRACE_GETFDPIC_EXEC 0 +#define PTRACE_GETFDPIC_INTERP 1 +#endif + struct task_struct; extern long subarch_ptrace(struct task_struct *child, long request, diff --git a/arch/x86/um/asm/elf.h b/arch/x86/um/asm/elf.h index 62ed5d68a978..33f69f1eac10 100644 --- a/arch/x86/um/asm/elf.h +++ b/arch/x86/um/asm/elf.h @@ -9,6 +9,7 @@ #include #define CORE_DUMP_USE_REGSET +#define ELF_FDPIC_CORE_EFLAGS 0 #ifdef CONFIG_X86_32 @@ -190,8 +191,11 @@ extern int arch_setup_additional_pages(struct linux_binprm *bprm, extern unsigned long um_vdso_addr; #define AT_SYSINFO_EHDR 33 -#define ARCH_DLINFO NEW_AUX_ENT(AT_SYSINFO_EHDR, um_vdso_addr) - +#define ARCH_DLINFO \ +do { \ + NEW_AUX_ENT(AT_SYSINFO_EHDR, um_vdso_addr); \ + NEW_AUX_ENT(AT_MINSIGSTKSZ, 0); \ +} while (0) #endif typedef unsigned long elf_greg_t; diff --git a/arch/x86/um/asm/module.h b/arch/x86/um/asm/module.h deleted file mode 100644 index a3b061d66082..000000000000 --- a/arch/x86/um/asm/module.h +++ /dev/null @@ -1,24 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef __UM_MODULE_H -#define __UM_MODULE_H - -/* UML is simple */ -struct mod_arch_specific -{ -}; - -#ifdef CONFIG_X86_32 - -#define Elf_Shdr Elf32_Shdr -#define Elf_Sym Elf32_Sym -#define Elf_Ehdr Elf32_Ehdr - -#else - -#define Elf_Shdr Elf64_Shdr -#define Elf_Sym Elf64_Sym -#define Elf_Ehdr Elf64_Ehdr - -#endif - -#endif diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt index bd2f530e5740..419ba0282806 100644 --- a/fs/Kconfig.binfmt +++ b/fs/Kconfig.binfmt @@ -58,7 +58,7 @@ config ARCH_USE_GNU_PROPERTY config BINFMT_ELF_FDPIC bool "Kernel support for FDPIC ELF binaries" default y if !BINFMT_ELF - depends on ARM || ((M68K || RISCV || SUPERH || XTENSA) && !MMU) + depends on ARM || ((M68K || RISCV || SUPERH || UML || XTENSA) && !MMU) select ELFCORE help ELF FDPIC binaries are based on ELF, but allow the individual load From patchwork Mon Nov 11 06:27:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 2009409 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=0nDAdllK; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=cAO3SVqT; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xn03T2dkRz1xyB for ; Mon, 11 Nov 2024 17:28:05 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=8TsHBqRXEhsW1k+vYQNTlDumwxnEt2nZhxPupGXZdpc=; b=0nDAdllK7hsiygOUYnWq4wLO0O Bo1B3mhnxPgpSHh3EpxiOT5wiOqPc7ybOKTUwEajfYiBTI0eq//wO4/AT9wSptro2lloRhCeaT3TX HvTssenBoVvBz+SIRTcxQkkYhuLiA7wthBNmchH8xQqIwZsZwZPf4804/40tGLgXppUogjJMWnsPl 9Z47MivkP0F2S/hHXc6WzIiIooeOXFV0VUnPAmWm9gdBGImZAp4CYHPTr3IJ3klaDS8m4j0MqKBLm XVrlIIkdkycOo7QcSifUci5NaFS7dvM9ef6CeYdouSPI80r4Gx5MttaXRWqeM5esteqeuqDAZp5DY eRrMVKKQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tANu7-0000000GUBc-2p44; Mon, 11 Nov 2024 06:28:03 +0000 Received: from mail-oi1-x233.google.com ([2607:f8b0:4864:20::233]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tANu5-0000000GUAx-0XXg for linux-um@lists.infradead.org; Mon, 11 Nov 2024 06:28:02 +0000 Received: by mail-oi1-x233.google.com with SMTP id 5614622812f47-3e602994635so2585318b6e.0 for ; Sun, 10 Nov 2024 22:28:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731306480; x=1731911280; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8TsHBqRXEhsW1k+vYQNTlDumwxnEt2nZhxPupGXZdpc=; b=cAO3SVqTUAJSs46qhZLoRiijtwUm3bFoqvntuG6c10XqjHc7mpFrD0metrAsutIGod to4SrO3eubbRBe1xhpS+p1wM9eX8WMCv0bLql8bSPbGfL/bVJyksDvQEFul2mH2vbV8h 9AdFF6ZICvQtNjPxvbqAF0kwqpq7wUGZ4ABYsLFKIsUlYJEJDWnfxajfsNZaZMV4T/Tl uE6xAgT3pGsRBEbBnA5Utr9Cou7T8Wy77nogsge81QtGJv8pE3bQsbF7GG+TzfrDOvsB YI0b/KD90aHoqokr6Gq4lIvtATHbpSQuwUlIcsJXphl/dLdP4eRXAuzfQFhrHYawdqC+ gDgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731306480; x=1731911280; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8TsHBqRXEhsW1k+vYQNTlDumwxnEt2nZhxPupGXZdpc=; b=Ix8YxF2q9QyFUW5EJ7xmPCIFyDm8V2+NiClg25/NfgD+BYN/b9Q5J0OkgcVoZuueiv 3ym+3GBwwsxBYiADGtZ8dVs7cGnDLFlgL6v1j/IiGI8IO6oNby6QXdPlHAlxnIkVBeEi v5Q1pkzOGyHUK15GuNEpixsaS2+OAdCDr5tu3y597I0tU1quBy0onXEg/tASQsjzFO4U /0Ug0NGM8Ukm0EUJZjFdm3anolKKYkDBwyfBra9PvSguCQ7ULQsjDLg45z0u2MQn4rw9 kI5VQKOWY3RsdPZA6fyaJySpofVlpVGgHCrko+VV1nanjrhMVAxCvGJMSMZzo8/nhxrI OlXA== X-Gm-Message-State: AOJu0YxCFBiz09to3I6erboMrBWiJI64L3AMbIRcGJ7b6rgD9/8Z47rb Oc+ugKndFKP3Sy5rg9LYv380dsbvXJi1Dwz4/APmVSjB6vn/w37H X-Google-Smtp-Source: AGHT+IGd5Kw6h+GNTSlF3dPV88QlwHoyg1PqqT2JJXTbCM6sWZy1EexrUr5XE7bBfRlBCrVYkWLOxA== X-Received: by 2002:a05:6808:15a6:b0:3e6:6273:657d with SMTP id 5614622812f47-3e7946c80a6mr9807322b6e.26.1731306479829; Sun, 10 Nov 2024 22:27:59 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7f41f65a271sm7784800a12.71.2024.11.10.22.27.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 22:27:59 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id 46DF9DBA918; Mon, 11 Nov 2024 15:27:57 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com Subject: [RFC PATCH v2 03/13] um: nommu: memory handling Date: Mon, 11 Nov 2024 15:27:03 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241110_222801_196221_44D5B372 X-CRM114-Status: GOOD ( 20.75 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This commit adds memory operations on UML under !MMU environment. Some part of the original UML code relying on CONFIG_MMU are excluded from compilation when !CONFIG_MMU. Additionally, generic functions such as uaccess, futex, memcpy/strnlen/strncpy can be used as u [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:233 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime(at)gmail.com] X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This commit adds memory operations on UML under !MMU environment. Some part of the original UML code relying on CONFIG_MMU are excluded from compilation when !CONFIG_MMU. Additionally, generic functions such as uaccess, futex, memcpy/strnlen/strncpy can be used as user- and kernel-space share the address space in !CONFIG_MMU mode. Signed-off-by: Hajime Tazaki Signed-off-by: Ricardo Koller --- arch/um/include/asm/futex.h | 4 ++++ arch/um/include/asm/mmu.h | 3 +++ arch/um/include/asm/mmu_context.h | 13 +++++++++++-- arch/um/include/asm/tlbflush.h | 22 ++++++++++++++++++++++ arch/um/include/asm/uaccess.h | 7 ++++--- arch/um/include/shared/os.h | 6 ++++++ arch/um/kernel/Makefile | 3 ++- arch/um/kernel/mem.c | 12 +++++++++++- arch/um/kernel/physmem.c | 6 ++++++ arch/um/kernel/skas/Makefile | 4 ++-- arch/um/kernel/trap.c | 4 ++++ arch/um/os-Linux/Makefile | 1 + arch/um/os-Linux/process.c | 4 ++-- 13 files changed, 78 insertions(+), 11 deletions(-) diff --git a/arch/um/include/asm/futex.h b/arch/um/include/asm/futex.h index 780aa6bfc050..89a8ac0b6963 100644 --- a/arch/um/include/asm/futex.h +++ b/arch/um/include/asm/futex.h @@ -8,7 +8,11 @@ int arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr); +#ifdef CONFIG_MMU int futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, u32 oldval, u32 newval); +#else +#include +#endif #endif diff --git a/arch/um/include/asm/mmu.h b/arch/um/include/asm/mmu.h index 01422b761aa0..d4087f9499e2 100644 --- a/arch/um/include/asm/mmu.h +++ b/arch/um/include/asm/mmu.h @@ -15,10 +15,13 @@ typedef struct mm_context { unsigned long sync_tlb_range_from; unsigned long sync_tlb_range_to; +#ifndef CONFIG_MMU + unsigned long end_brk; #ifdef CONFIG_BINFMT_ELF_FDPIC unsigned long exec_fdpic_loadmap; unsigned long interp_fdpic_loadmap; #endif +#endif /* !CONFIG_MMU */ } mm_context_t; #endif diff --git a/arch/um/include/asm/mmu_context.h b/arch/um/include/asm/mmu_context.h index 23dcc914d44e..da287e8c86b3 100644 --- a/arch/um/include/asm/mmu_context.h +++ b/arch/um/include/asm/mmu_context.h @@ -37,10 +37,19 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, } #define init_new_context init_new_context -extern int init_new_context(struct task_struct *task, struct mm_struct *mm); - #define destroy_context destroy_context +#ifdef CONFIG_MMU +extern int init_new_context(struct task_struct *task, struct mm_struct *mm); extern void destroy_context(struct mm_struct *mm); +#else +static inline int init_new_context(struct task_struct *task, struct mm_struct *mm) +{ + return 0; +} +static inline void destroy_context(struct mm_struct *mm) +{ +} +#endif #include diff --git a/arch/um/include/asm/tlbflush.h b/arch/um/include/asm/tlbflush.h index 13a3009942be..9157f71695c6 100644 --- a/arch/um/include/asm/tlbflush.h +++ b/arch/um/include/asm/tlbflush.h @@ -30,6 +30,7 @@ * - flush_tlb_kernel_range(start, end) flushes a range of kernel pages */ +#ifdef CONFIG_MMU extern int um_tlb_sync(struct mm_struct *mm); extern void flush_tlb_all(void); @@ -55,5 +56,26 @@ static inline void flush_tlb_kernel_range(unsigned long start, /* Kernel needs to be synced immediately */ um_tlb_sync(&init_mm); } +#else +static inline int um_tlb_sync(struct mm_struct *mm) +{ + return 0; +} + +static inline void flush_tlb_page(struct vm_area_struct *vma, + unsigned long address) +{ +} + +static inline void flush_tlb_range(struct vm_area_struct *vma, + unsigned long start, unsigned long end) +{ +} + +static inline void flush_tlb_kernel_range(unsigned long start, + unsigned long end) +{ +} +#endif #endif diff --git a/arch/um/include/asm/uaccess.h b/arch/um/include/asm/uaccess.h index 1d4b6bbc1b65..9bfee12cb6b7 100644 --- a/arch/um/include/asm/uaccess.h +++ b/arch/um/include/asm/uaccess.h @@ -22,6 +22,7 @@ #define __addr_range_nowrap(addr, size) \ ((unsigned long) (addr) <= ((unsigned long) (addr) + (size))) +#ifdef CONFIG_MMU extern unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n); extern unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n); extern unsigned long __clear_user(void __user *mem, unsigned long len); @@ -33,9 +34,6 @@ static inline int __access_ok(const void __user *ptr, unsigned long size); #define INLINE_COPY_FROM_USER #define INLINE_COPY_TO_USER - -#include - static inline int __access_ok(const void __user *ptr, unsigned long size) { unsigned long addr = (unsigned long)ptr; @@ -43,6 +41,9 @@ static inline int __access_ok(const void __user *ptr, unsigned long size) (__under_task_size(addr, size) || __access_ok_vsyscall(addr, size)); } +#endif + +#include /* no pagefaults for kernel addresses in um */ #define __get_kernel_nofault(dst, src, type, err_label) \ diff --git a/arch/um/include/shared/os.h b/arch/um/include/shared/os.h index 5babad8c5f75..6874be0c38a8 100644 --- a/arch/um/include/shared/os.h +++ b/arch/um/include/shared/os.h @@ -195,7 +195,13 @@ extern void get_host_cpu_features( extern int create_mem_file(unsigned long long len); /* tlb.c */ +#ifdef CONFIG_MMU extern void report_enomem(void); +#else +static inline void report_enomem(void) +{ +} +#endif /* process.c */ extern void os_alarm_process(int pid); diff --git a/arch/um/kernel/Makefile b/arch/um/kernel/Makefile index f8567b933ffa..b41e9bcabbe3 100644 --- a/arch/um/kernel/Makefile +++ b/arch/um/kernel/Makefile @@ -16,9 +16,10 @@ extra-y := vmlinux.lds obj-y = config.o exec.o exitcode.o irq.o ksyms.o mem.o \ physmem.o process.o ptrace.o reboot.o sigio.o \ - signal.o sysrq.o time.o tlb.o trap.o \ + signal.o sysrq.o time.o trap.o \ um_arch.o umid.o maccess.o kmsg_dump.o capflags.o skas/ obj-y += load_file.o +obj-$(CONFIG_MMU) += tlb.o obj-$(CONFIG_BLK_DEV_INITRD) += initrd.o obj-$(CONFIG_GPROF) += gprof_syms.o diff --git a/arch/um/kernel/mem.c b/arch/um/kernel/mem.c index 53248ed04771..b674017d9871 100644 --- a/arch/um/kernel/mem.c +++ b/arch/um/kernel/mem.c @@ -64,7 +64,8 @@ void __init mem_init(void) * to be turned on. */ brk_end = (unsigned long) UML_ROUND_UP(sbrk(0)); - map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0); + map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, + !IS_ENABLED(CONFIG_MMU)); memblock_free((void *)brk_end, uml_reserved - brk_end); uml_reserved = brk_end; @@ -78,6 +79,7 @@ void __init mem_init(void) * Create a page table and place a pointer to it in a middle page * directory entry. */ +#ifdef CONFIG_MMU static void __init one_page_table_init(pmd_t *pmd) { if (pmd_none(*pmd)) { @@ -149,6 +151,12 @@ static void __init fixrange_init(unsigned long start, unsigned long end, j = 0; } } +#else +static void __init fixrange_init(unsigned long start, unsigned long end, + pgd_t *pgd_base) +{ +} +#endif static void __init fixaddr_user_init( void) { @@ -230,6 +238,7 @@ void *uml_kmalloc(int size, int flags) return kmalloc(size, flags); } +#ifdef CONFIG_MMU static const pgprot_t protection_map[16] = { [VM_NONE] = PAGE_NONE, [VM_READ] = PAGE_READONLY, @@ -249,3 +258,4 @@ static const pgprot_t protection_map[16] = { [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED }; DECLARE_VM_GET_PAGE_PROT +#endif diff --git a/arch/um/kernel/physmem.c b/arch/um/kernel/physmem.c index a74f17b033c4..f55d46dbe173 100644 --- a/arch/um/kernel/physmem.c +++ b/arch/um/kernel/physmem.c @@ -84,7 +84,11 @@ void __init setup_physmem(unsigned long start, unsigned long reserve_end, exit(1); } +#ifdef CONFIG_MMU physmem_fd = create_mem_file(len); +#else + physmem_fd = -1; +#endif err = os_map_memory((void *) reserve_end, physmem_fd, reserve, map_size, 1, 1, 1); @@ -95,12 +99,14 @@ void __init setup_physmem(unsigned long start, unsigned long reserve_end, exit(1); } +#ifdef CONFIG_MMU /* * Special kludge - This page will be mapped in to userspace processes * from physmem_fd, so it needs to be written out there. */ os_seek_file(physmem_fd, __pa(__syscall_stub_start)); os_write_file(physmem_fd, __syscall_stub_start, PAGE_SIZE); +#endif memblock_add(__pa(start), len); memblock_reserve(__pa(start), reserve); diff --git a/arch/um/kernel/skas/Makefile b/arch/um/kernel/skas/Makefile index 3384be42691f..64d7ba803b1a 100644 --- a/arch/um/kernel/skas/Makefile +++ b/arch/um/kernel/skas/Makefile @@ -3,8 +3,8 @@ # Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) # -obj-y := stub.o mmu.o process.o syscall.o uaccess.o \ - stub_exe_embed.o +obj-y := stub.o process.o stub_exe_embed.o +obj-$(CONFIG_MMU) += mmu.o syscall.o uaccess.o # Stub executable diff --git a/arch/um/kernel/trap.c b/arch/um/kernel/trap.c index cdaee3e94273..a7519b3de4bf 100644 --- a/arch/um/kernel/trap.c +++ b/arch/um/kernel/trap.c @@ -24,6 +24,7 @@ int handle_page_fault(unsigned long address, unsigned long ip, int is_write, int is_user, int *code_out) { +#ifdef CONFIG_MMU struct mm_struct *mm = current->mm; struct vm_area_struct *vma; pmd_t *pmd; @@ -129,6 +130,9 @@ int handle_page_fault(unsigned long address, unsigned long ip, goto out_nosemaphore; pagefault_out_of_memory(); return 0; +#else + return -EFAULT; +#endif } static void show_segv_info(struct uml_pt_regs *regs) diff --git a/arch/um/os-Linux/Makefile b/arch/um/os-Linux/Makefile index 049dfa5bc9c6..20ff8d5971db 100644 --- a/arch/um/os-Linux/Makefile +++ b/arch/um/os-Linux/Makefile @@ -21,3 +21,4 @@ USER_OBJS := $(user-objs-y) elf_aux.o execvp.o file.o helper.o irq.o \ tty.o umid.o util.o include $(srctree)/arch/um/scripts/Makefile.rules +CFLAGS_process.o=-g -O0 diff --git a/arch/um/os-Linux/process.c b/arch/um/os-Linux/process.c index 9f086f939420..ef1a2f0aa06a 100644 --- a/arch/um/os-Linux/process.c +++ b/arch/um/os-Linux/process.c @@ -63,8 +63,8 @@ int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len, prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) | (x ? PROT_EXEC : 0); - loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED, - fd, off); + loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED | + (!IS_ENABLED(CONFIG_MMU) ? MAP_ANONYMOUS : 0), fd, off); if (loc == MAP_FAILED) return -errno; return 0; From patchwork Mon Nov 11 06:27:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 2009410 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=fJ+W4AJ8; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=M5bgbaGi; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xn03Y685Kz1xyB for ; Mon, 11 Nov 2024 17:28:09 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=h+MQ8LBXw5PDwKxMHEr82155zLkpbExofGKGEuLjFDA=; b=fJ+W4AJ8rSgwOLzHAfXkq7APez 9KEnbYj2udNrJfJj5Jg3c3S1YxO+S+KgsTxtlslM4hPf3vnp7CLN9F5e3GcYy1de6p5Q+pWXvAgVb /OcR5awAgI/vQ1SN82HGHk6qR3CmLChxh7QxPgBA5u0RjIauQf3TT+IMa63CniZ5feWt77igY4tf8 3A+49Xc0NIWZF5B74imtRISwzBpZJJTZzCkYKfOc44ZTmRWj9c4NROkKEWpInBsOYhoiFai5nLwg9 Cm/ZgB9A5blf4WE/+Ote0XcYLCjtAl6TfTa9r+MkEYFGMO6uX0bO6mr9pPTOlQil2s26FdOOuQ5ej RyJg3kCw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tANuC-0000000GUCa-0qla; Mon, 11 Nov 2024 06:28:08 +0000 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tANu9-0000000GUC1-1jF4 for linux-um@lists.infradead.org; Mon, 11 Nov 2024 06:28:06 +0000 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-720e94d36c8so4424917b3a.1 for ; Sun, 10 Nov 2024 22:28:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731306484; x=1731911284; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=h+MQ8LBXw5PDwKxMHEr82155zLkpbExofGKGEuLjFDA=; b=M5bgbaGiSWBCKNBSoeha2CTY5f16pijQXdV/bm1Z4G/7kpCO0iMSBv2zhh4gxid8Gy yHSTN3I15c1qPLS4eyJEaj5hQ269R4c2/klKs3AqeQkKPsW1uJmEEfLTr026OyRcF2S8 cXKAOb1SGCwdoFAcBULdWYLs4g8EO0RiQGfqlepfYyT4GU803LRgvzzBL2x/5awbqSqk kziKg1ir/iNtpm1xvDRKUvBeLLEfqDMnnXm9oyaivQwqiOKUMs//nyoSVGHPOX52jx+Z 1KgZZ5x4CsEY1hkUjQO25AkBkUKCx3C0eb+XqkVe8xxiqH1NxXbweza4VAUGvOduE1Ho BJnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731306484; x=1731911284; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=h+MQ8LBXw5PDwKxMHEr82155zLkpbExofGKGEuLjFDA=; b=OL93gbnK+lKNH0wShJnr04FZY0qpvfviAqAQfiKOjw01tSzvx7WUocbKyX5p15kdps WLSV4qcg8G1Kj6Ppxn6rNNfGRqSRV0ZFn2wIJ596DV7Yh6n6ZKqpEMdAAHt7SmPWMEaS 7SVrBDGwLuRUZ/9Leij43ajyaCcNEXAkVKq0uJ6nWejxvlHImUIhPlFBdCLlbPqdidaT DUsPgt9giD3XHJdsbGj8MgwlaQR82IpO4m+qOSQh4/x4tLID9qPIjaLjxogWXNU3lbh6 WM/dFQ1WUmNwxNknD75ZbseEEljR+9Y3Z82HY3WwcGYfOl4dXBhyu95rA1I460slMk7Z AGuw== X-Gm-Message-State: AOJu0Yxa97/08wnpZ8jwxT3Cm1ZiO9gB/P9HyWsrZ5cVlFEwkgIls/ZX ImBd7XtFs2Y639ymnvrehYjCg0vvOH5gKV1hlqHopZzYYPWnORLN X-Google-Smtp-Source: AGHT+IH6zGwWwtO1PBvl6k5+HYdx3OYEFfDs7V+DGavgoUmFx683YIwE67DreBe3Ghq2AaSrJtyEiA== X-Received: by 2002:a05:6a21:3391:b0:1d3:418a:e42 with SMTP id adf61e73a8af0-1dc23321c26mr17047533637.10.1731306484360; Sun, 10 Nov 2024 22:28:04 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-724079a404csm8533066b3a.126.2024.11.10.22.28.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 22:28:03 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id 6893FDBA91A; Mon, 11 Nov 2024 15:28:02 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com Subject: [RFC PATCH v2 04/13] x86/um: nommu: syscall handling Date: Mon, 11 Nov 2024 15:27:04 +0900 Message-ID: <3ddee75eda49da14d9584e6be683e4db7fabd478.1731290567.git.thehajime@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241110_222805_477494_A3B05422 X-CRM114-Status: GOOD ( 19.92 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This commit introduces an entry point of syscall interface for !MMU mode. It uses an entry function, __kernel_vsyscall, a kernel-wide global symbol accessible from any locations. Although it isn't in the scope of this commit, it can be also exposed via vdso image which is directly accessible from userspace. A standard library (i.e., libc) can utilize this entry point to implem [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:432 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime(at)gmail.com] X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This commit introduces an entry point of syscall interface for !MMU mode. It uses an entry function, __kernel_vsyscall, a kernel-wide global symbol accessible from any locations. Although it isn't in the scope of this commit, it can be also exposed via vdso image which is directly accessible from userspace. A standard library (i.e., libc) can utilize this entry point to implement syscall wrapper; we can also use this by hooking syscall for unmodified userspace applications/libraries, which will be implemented in the subsequent commit. This only supports 64-bit mode of x86 architecture. Signed-off-by: Hajime Tazaki Signed-off-by: Ricardo Koller --- arch/x86/um/do_syscall_64.c | 37 +++++++++++ arch/x86/um/entry_64.S | 87 +++++++++++++++++++++++++ arch/x86/um/shared/sysdep/syscalls_64.h | 6 ++ 3 files changed, 130 insertions(+) create mode 100644 arch/x86/um/do_syscall_64.c create mode 100644 arch/x86/um/entry_64.S diff --git a/arch/x86/um/do_syscall_64.c b/arch/x86/um/do_syscall_64.c new file mode 100644 index 000000000000..a1189ddb2b50 --- /dev/null +++ b/arch/x86/um/do_syscall_64.c @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include + +__visible void do_syscall_64(struct pt_regs *regs) +{ + int syscall; + + syscall = PT_SYSCALL_NR(regs->regs.gp); + UPT_SYSCALL_NR(®s->regs) = syscall; + + pr_debug("syscall(%d) (current=%lx) (fn=%lx)\n", + syscall, (unsigned long)current, + (unsigned long)sys_call_table[syscall]); + + if (likely(syscall < NR_syscalls)) { + PT_REGS_SET_SYSCALL_RETURN(regs, + EXECUTE_SYSCALL(syscall, regs)); + } + + pr_debug("syscall(%d) --> %lx\n", syscall, + regs->regs.gp[HOST_AX]); + + PT_REGS_SYSCALL_RET(regs) = regs->regs.gp[HOST_AX]; + + /* force do_signal() --> is_syscall() */ + set_thread_flag(TIF_SIGPENDING); + interrupt_end(); + + /* execve succeeded */ + if (syscall == __NR_execve && regs->regs.gp[HOST_AX] == 0) + userspace(¤t->thread.regs.regs); +} diff --git a/arch/x86/um/entry_64.S b/arch/x86/um/entry_64.S new file mode 100644 index 000000000000..022a8122690b --- /dev/null +++ b/arch/x86/um/entry_64.S @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#include + +#include +#include +#include + +#include "../entry/calling.h" + +#ifdef CONFIG_SMP +#error need to stash these variables somewhere else +#endif + +#define UM_GLOBAL_VAR(x) .data; .align 8; .globl x; x:; .long 0 + +UM_GLOBAL_VAR(current_top_of_stack) +UM_GLOBAL_VAR(current_ptregs) + +.code64 +.section .entry.text, "ax" + +.align 8 +#undef ENTRY +#define ENTRY(x) .text; .globl x; .type x,%function; x: +#undef END +#define END(x) .size x, . - x + +/* + * %rcx has the return address (we set it like that in zpoline trampoline). + * + * Registers on entry: + * rax system call number + * rcx return address + * rdi arg0 + * rsi arg1 + * rdx arg2 + * r10 arg3 + * r8 arg4 + * r9 arg5 + * + * (note: we are allowed to mess with r11: r11 is callee-clobbered + * register in C ABI) + */ +ENTRY(__kernel_vsyscall) + + movq %rsp, %r11 + + /* Point rsp to the top of the ptregs array, so we can + just fill it with a bunch of push'es. */ + movq current_ptregs, %rsp + + /* 8 bytes * 20 registers (plus 8 for the push) */ + addq $168, %rsp + + /* Construct struct pt_regs on stack */ + pushq $0 /* pt_regs->ss (index 20) */ + pushq %r11 /* pt_regs->sp */ + pushfq /* pt_regs->flags */ + pushq $0 /* pt_regs->cs */ + pushq %rcx /* pt_regs->ip */ + pushq %rax /* pt_regs->orig_ax */ + + PUSH_AND_CLEAR_REGS rax=$-ENOSYS + + mov %rsp, %rdi + + /* + * Switch to current top of stack, so "current->" points + * to the right task. + */ + movq current_top_of_stack, %rsp + + call do_syscall_64 + + movq current_ptregs, %rsp + + POP_REGS + + addq $8, %rsp /* skip orig_ax */ + addq $8, %rsp /* skip ip */ + addq $8, %rsp /* skip cs */ + addq $8, %rsp /* skip flags */ + popq %rsp + + ret + +END(__kernel_vsyscall) diff --git a/arch/x86/um/shared/sysdep/syscalls_64.h b/arch/x86/um/shared/sysdep/syscalls_64.h index b6b997225841..f3a4fd76673f 100644 --- a/arch/x86/um/shared/sysdep/syscalls_64.h +++ b/arch/x86/um/shared/sysdep/syscalls_64.h @@ -25,4 +25,10 @@ extern syscall_handler_t *sys_call_table[]; extern syscall_handler_t sys_modify_ldt; extern syscall_handler_t sys_arch_prctl; +#ifndef CONFIG_MMU +__visible void do_syscall_64(struct pt_regs *regs); +extern long __kernel_vsyscall(int64_t a0, int64_t a1, int64_t a2, int64_t a3, + int64_t a4, int64_t a5, int64_t a6); +#endif + #endif From patchwork Mon Nov 11 06:27:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 2009411 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=NTNgfUuu; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=VzC47ee0; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xn03h2HX9z1xyB for ; Mon, 11 Nov 2024 17:28:16 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=A308RrEr7E36b4DSV1XsgRK9lOwiuRrBRlfF8Ce2kbE=; b=NTNgfUuu/9KxEQ1Bx1ArOK63+D yoDq/EcwjEgkk1/1AznuDQ5hxrOkQIYR7jTCAzQk+QWJ39UW1iXt6ciaDyR1sP6Dk1JgUUMknFcV5 2kxbXXDHCHyOWe7HfU/EaaxE13cU1Arx8rC64vsRDqjJtRO/P0d/bmPOXmJqNc7IRQHuLTe80e7RT SNODHU54A2pERepjjyKOgLHR8wkB6a6c1AQFir3J2A3vxzuHvhKK/tgJBwsJWuQZvawRSbR3iBXIo 6XLkx+M0YVnxMO8s3tDb/0a1fmO41aZhexu5GAp057QwMLWpp/KrI837+8+pvP+cJVa1gqnKi1cG1 5Vsz8lhw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tANuI-0000000GUDv-2x7e; Mon, 11 Nov 2024 06:28:14 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tANuF-0000000GUDQ-3aaw for linux-um@lists.infradead.org; Mon, 11 Nov 2024 06:28:13 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-21145812538so38536385ad.0 for ; Sun, 10 Nov 2024 22:28:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731306491; x=1731911291; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=A308RrEr7E36b4DSV1XsgRK9lOwiuRrBRlfF8Ce2kbE=; b=VzC47ee0Peoj68MV00eiHy2VWb0D3k+qs0BwxM3xRPA4TvcfmwMMqMKW1sX0KJ86lz rtBMRdq74hlWKZh8eCW0KTW2/YcQarhJWUJoLrJi4HXERifVxiyFsw7hleCCn44DA1tH L4LRwdt+c8wF8wAFnHKdk7lWA1zo2lW4NjBQuubhunh/Bxm0AgHBiPlwM9WCL0nDinSI PyMQUltJ9xU3KBXJ14/aQACx5hfKYG1z1ufoXJNMLxX73yqr6ybo0Hjyg4e5IpzrV8Su h0LpIGt+vAjAyQ6z3Pg07h+Bb2+Sw7IWZZCqZQ+ZogRGWRHLPsHzbKRm/Cxid9N+iE0Q Dseg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731306491; x=1731911291; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=A308RrEr7E36b4DSV1XsgRK9lOwiuRrBRlfF8Ce2kbE=; b=HeVxt27SLp7MJ9qr0lgTVW768ZgMin3h+GdfjOFXO99Ohroq4AJt7lreeDWt5ZpBot ci0uGpEkxVWLNmOKu3p7WgkWnPvlpHiO+2IkHis/XVbRCDiImasRglKBPrY3jAaUylhO 7iGVNu2PxpnuLaGjpdeIoOfQrFI/WSpQB1gcHKd8O0WEM7z42OKkeU7UfVZALDW81bS3 0ugD5Z+LpnmrgU+Ov+9y2TmntGMqLthlo+kRPfcb6BLS6D3lL8p+haG6f8SjfD5ZiHQK +srjxa4JHtCfpLhVk70Q7KNVWZKHoeSFZ6+ZLdtW1+FNkdU4r5WyqeIs0ZZgsgga0TrU OQSA== X-Gm-Message-State: AOJu0YyPe+NStYnBK6qj4xfY8T31xQbpf310prllyi5qQC5ClLEKDnPN Qgri3BZWz+7naGFyLeOKsLQq0MvZis7xvBBR8IJwbHMl4XRog98h X-Google-Smtp-Source: AGHT+IG8p7Jy5jq1kPUBPc5ZGDqOwo9/uiIxIcNjEis/R5ZfrRlCdeB+nt/wY0OVtQxNXYb1aUkY6g== X-Received: by 2002:a17:903:2347:b0:20c:5e86:9b68 with SMTP id d9443c01a7336-21183c43299mr161859665ad.4.1731306490767; Sun, 10 Nov 2024 22:28:10 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21177dc8c29sm68891505ad.25.2024.11.10.22.28.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 22:28:10 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id 2ACF8DBA91C; Mon, 11 Nov 2024 15:28:08 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com Subject: [RFC PATCH v2 05/13] x86/um: nommu: syscall translation by zpoline Date: Mon, 11 Nov 2024 15:27:05 +0900 Message-ID: <99114f684db0bc3bbd789323c80211f18f347454.1731290567.git.thehajime@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241110_222811_924353_A29AF137 X-CRM114-Status: GOOD ( 27.28 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This commit adds a mechanism to hook syscalls for unmodified userspace programs used under UML in !MMU mode. The mechanism, called zpoline, translates syscall/sysenter instructions with `call *%rax`, [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:629 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime(at)gmail.com] X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This commit adds a mechanism to hook syscalls for unmodified userspace programs used under UML in !MMU mode. The mechanism, called zpoline, translates syscall/sysenter instructions with `call *%rax`, which can be processed by a trampoline code also installed upon an initcall during boot. The translation is triggered by elf_arch_finalize_exec(), an arch hook introduced by another commit. All syscalls issued by userspace thus redirected to a specific function, __kernel_vsyscall, introduced as a syscall entry point for !MMU UML. This totally changes the code path to hook syscall with ptrace(2) used by MMU-full UML. Signed-off-by: Hajime Tazaki --- arch/x86/um/asm/elf.h | 3 + arch/x86/um/zpoline.c | 223 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 226 insertions(+) create mode 100644 arch/x86/um/zpoline.c diff --git a/arch/x86/um/asm/elf.h b/arch/x86/um/asm/elf.h index 33f69f1eac10..6f5977ff0d21 100644 --- a/arch/x86/um/asm/elf.h +++ b/arch/x86/um/asm/elf.h @@ -188,6 +188,9 @@ do { \ struct linux_binprm; extern int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp); +struct elf_fdpic_params; +extern int elf_arch_finalize_exec(struct elf_fdpic_params *exec_params, + struct elf_fdpic_params *interp_params); extern unsigned long um_vdso_addr; #define AT_SYSINFO_EHDR 33 diff --git a/arch/x86/um/zpoline.c b/arch/x86/um/zpoline.c new file mode 100644 index 000000000000..97f5345ab314 --- /dev/null +++ b/arch/x86/um/zpoline.c @@ -0,0 +1,223 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * zpoline.c + * + * Replace syscall/sysenter instructions to `call *%rax` to hook syscalls. + * + */ +//#define DEBUG +#include +#include +#include +#include +#include +#include +#include + +/* start of trampoline code area */ +static char *__zpoline_start; + +static int __zpoline_translate_syscalls(struct elf_fdpic_params *params) +{ + int count = 0, loop; + struct insn insn; + unsigned long addr; + struct elf_fdpic_loadseg *seg; + struct elf_phdr *phdr; + struct elfhdr *ehdr = (struct elfhdr *)params->elfhdr_addr; + + if (!ehdr) + return 0; + + seg = params->loadmap->segs; + phdr = params->phdrs; + for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) { + if (phdr->p_type != PT_LOAD) + continue; + addr = seg->addr; + /* skip translation of trampoline code */ + if (addr <= (unsigned long)(&__zpoline_start[0] + 0x1000 + 0x0100)) { + pr_warn("%lx: address is in the range of trampoline", addr); + return -EINVAL; + } + + /* translate only segment with Executable flag */ + if (!(phdr->p_flags & PF_X)) { + seg++; + continue; + } + + pr_debug("translation 0x%lx-0x%llx", addr, + seg->addr + seg->p_memsz); + /* now ready to translate */ + while (addr < (seg->addr + seg->p_memsz)) { + insn_init(&insn, (void *)addr, MAX_INSN_SIZE, 1); + insn_get_length(&insn); + + insn_get_opcode(&insn); + + switch (insn.opcode.bytes[0]) { + case 0xf: + switch (insn.opcode.bytes[1]) { + case 0x05: /* syscall */ + case 0x34: /* sysenter */ + pr_debug("%lx: found syscall/sysenter", addr); + *(char *)addr = 0xff; // callq + *((char *)addr + 1) = 0xd0; // *%rax + count++; + break; + } + default: + break; + } + + addr += insn.length; + if (insn.length == 0) { + pr_debug("%lx: length zero with byte %x. skip ?", + addr, insn.opcode.bytes[0]); + addr += 1; + } + } + seg++; + } + return count; +} + +/** + * elf_arch_finalize_exec() - architecture hook to translate syscall/sysenter + * + * translate syscall/sysenter instruction upon loading ELF binary file + * on execve(2)&co syscall. + * + * suppose we have those instructions: + * + * mov $sysnr, %rax + * syscall 0f 05 + * + * this will translate it with: + * + * mov $sysnr, %rax (<= untouched) + * call *(%rax) ff d0 + * + * this will finally called hook function guided by trampoline code installed + * at setup_zpoline_trampoline(). + * + * @exec_params: ELF meta data for executable file + * @interp_params: ELF meta data for the interpreter file + */ +int elf_arch_finalize_exec(struct elf_fdpic_params *exec_params, + struct elf_fdpic_params *interp_params) +{ + int err = 0, count = 0; + struct mm_struct *mm = current->mm; + + if (down_write_killable(&mm->mmap_lock)) + return -EINTR; + + /* translate for the executable */ + err = __zpoline_translate_syscalls(exec_params); + if (err < 0) { + pr_info("zpoline: xlate error %d", err); + goto out; + } + count += err; + pr_debug("zpoline: rewritten (exec) %d syscalls\n", count); + + /* translate for the interpreter */ + err = __zpoline_translate_syscalls(interp_params); + if (err < 0) { + pr_info("zpoline: xlate error %d", err); + goto out; + } + count += err; + + err = 0; + pr_debug("zpoline: rewritten (exec+interp) %d syscalls\n", count); + +out: + up_write(&mm->mmap_lock); + return err; +} + +/** + * setup_zpoline_trampoline() - install trampoline code for zpoline + * + * setup trampoline code for syscall hooks + * + * the trampoline code guides to call hooked function, __kernel_vsyscall + * in this case, via nop slides at the memory address zero (thus, zpoline). + * + * loaded binary by exec(2) is translated to call the function. + */ +static int __init setup_zpoline_trampoline(void) +{ + int i, ret; + int ptr; + + /* zpoline: map area of trampoline code started from addr 0x0 */ + __zpoline_start = 0x0; + + ret = os_map_memory((void *) 0, -1, 0, PAGE_SIZE, 1, 1, 1); + if (ret) + panic("map failed\n NOTE: /proc/sys/vm/mmap_min_addr should be set 0\n"); + + /* fill nop instructions until the trampoline code */ + for (i = 0; i < NR_syscalls; i++) + __zpoline_start[i] = 0x90; + + /* optimization to skip old syscalls */ + /* short jmp */ + __zpoline_start[214 /* __NR_epoll_ctl_old */] = 0xeb; + /* range of a short jmp : -128 ~ +127 */ + __zpoline_start[215 /* __NR_epoll_wait_old */] = 127; + + /** + * FIXME: shift red zone area to properly handle the case + */ + + /** + * put code for jumping to __kernel_vsyscall. + * + * here we embed the following code. + * + * movabs [$addr],%r11 + * jmpq *%r11 + * + */ + ptr = NR_syscalls; + /* 49 bb [64-bit addr (8-byte)] movabs [64-bit addr (8-byte)],%r11 */ + __zpoline_start[ptr++] = 0x49; + __zpoline_start[ptr++] = 0xbb; + __zpoline_start[ptr++] = ((uint64_t) __kernel_vsyscall >> (8 * 0)); + __zpoline_start[ptr++] = ((uint64_t) __kernel_vsyscall >> (8 * 1)); + __zpoline_start[ptr++] = ((uint64_t) __kernel_vsyscall >> (8 * 2)); + __zpoline_start[ptr++] = ((uint64_t) __kernel_vsyscall >> (8 * 3)); + __zpoline_start[ptr++] = ((uint64_t) __kernel_vsyscall >> (8 * 4)); + __zpoline_start[ptr++] = ((uint64_t) __kernel_vsyscall >> (8 * 5)); + __zpoline_start[ptr++] = ((uint64_t) __kernel_vsyscall >> (8 * 6)); + __zpoline_start[ptr++] = ((uint64_t) __kernel_vsyscall >> (8 * 7)); + + /* + * pretending to be syscall instruction by putting return + * address in %rcx. + */ + /* 48 8b 0c 24 mov (%rsp),%rcx */ + __zpoline_start[ptr++] = 0x48; + __zpoline_start[ptr++] = 0x8b; + __zpoline_start[ptr++] = 0x0c; + __zpoline_start[ptr++] = 0x24; + + /* 41 ff e3 jmp *%r11 */ + __zpoline_start[ptr++] = 0x41; + __zpoline_start[ptr++] = 0xff; + __zpoline_start[ptr++] = 0xe3; + + /* permission: XOM (PROT_EXEC only) */ + ret = os_protect_memory(0, PAGE_SIZE, 0, 0, 1); + if (ret) + panic("failed: can't configure permission on trampoline code"); + + pr_info("zpoline: setting up trampoline code done\n"); + return 0; +} +arch_initcall(setup_zpoline_trampoline); From patchwork Mon Nov 11 06:27:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 2009412 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=i4RzPUph; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=hQ7NPgIP; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xn03q0Q41z1xyB for ; Mon, 11 Nov 2024 17:28:23 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=LDAmKXGbtDJjiaikFG+gdRryv6+QLSbmKiwO1FENW/U=; b=i4RzPUphZ0c7XZ+gwK/bjT5k2Q SxffvriTw8vLlyvw6CLBIcOpfiFFRjXb4AXN4WaNBlDr2ItmyZoY1TApOtTVmCaJWvDBPjnzqd8lD LrwHsljfhO3x4Vt7Zzz/QFAnBVMJt0Op+raFp9BqyS68VSNgEAbnt8UXgO31qGieo3MBPbXkJ/6Lf W6FYUIwdtTxFQiza1cYZrJW+UwVyxFhr2zQk2UV2FDbVC2s7JZ2s+nkvYl1hboLlvBtlP4WiK4gWg pr/1ttbWOLwRzGv0+aybiqN7ZdSQ6XzVwHjprKDx1nF11yj6Lg7NThG5rxpkcr3zdDq98qbGiTNPy W2KQn5tw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tANuP-0000000GUFE-1vlq; Mon, 11 Nov 2024 06:28:21 +0000 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tANuN-0000000GUEl-1QDW for linux-um@lists.infradead.org; Mon, 11 Nov 2024 06:28:20 +0000 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-71e5a62031aso3206454b3a.1 for ; Sun, 10 Nov 2024 22:28:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731306498; x=1731911298; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LDAmKXGbtDJjiaikFG+gdRryv6+QLSbmKiwO1FENW/U=; b=hQ7NPgIPsTbvjJHj/lI6VwofRgg7dF7LYtIaoB7KOTXX6PlswUvOPJ3m8BYBLKpKdJ TPBrHkyy4bCoqbd6YxFVviZ++u9fV1qJPCKFcddt3bymy0kFIuHVE7PjBi3lyHBkayFe MdrWM6qKnbESMuXZOy11gTseixqX4ub1U5V4988YH4QkvO5JE59blu5dgcX/kKGEF2Xp 0PVB6tM3Ao0anQFr12CZdmzAh8x/2bhA2oSWkw76DW3TQIr8SYIJk4lACKz0obigtSYC 9TFMCEtLduynd80M7oxPwCTKwp5MN9Jnt1dJqrIse8QcGPABrK4L6HMtiBeYJM43ZFFt 6/Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731306498; x=1731911298; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LDAmKXGbtDJjiaikFG+gdRryv6+QLSbmKiwO1FENW/U=; b=q+v+F55D/0TnOWA+kO2yDqQJPaIfAZ9PJSLc8FNOx7yKv/CFy+RrKoYqJw30LlQNdm 8s/lw3LR+eehSjrHBRgkT7CYWHPL0RR45UFwzhk/h424+NZKmaWQcU/dio4YqMMLufL9 yHoPy5vrhyKGBwLqFTy3s16edh/102eLMlEmzyXvdcpx+OAzx5tHDHEgEtuAS39kb+uX SPa8Q64+c0w3/MVMCM5tc71C1IoKHkx2nw6VLTGiJ5rz5i+Ci12TyyDtQ3YWYPGcIy81 LqwgHRzfdqWAPdtpW33z9L19F0TolmSakL/LlM8WaLrNSJFCpAO3MS205aZRG40fYJQ/ TqYg== X-Gm-Message-State: AOJu0YxgFIXCfR/c/dszoFjiXTEfqslwjBFKyo9kzIbzzD4+SCA92Hzw lz8oVJ38kOXmZ9FqY63V3xgPtsmXHLy8J9Yij1coj4dahxxGGjxcw44dnw== X-Google-Smtp-Source: AGHT+IFAA3HxMKVVGu2ErJm7CjX9/6A24s2Q/3wZkwNmTRxvGNJ6v45JVy0ryMV/gb9tk8u6Y22xSQ== X-Received: by 2002:a05:6a00:21cc:b0:71e:3b51:e850 with SMTP id d2e1a72fcca58-724132788c8mr14958538b3a.2.1731306498328; Sun, 10 Nov 2024 22:28:18 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72407a187f4sm8516951b3a.144.2024.11.10.22.28.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 22:28:17 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id C794DDBA91E; Mon, 11 Nov 2024 15:28:15 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com Subject: [RFC PATCH v2 06/13] um: nommu: prevent host syscalls from userspace by seccomp filter Date: Mon, 11 Nov 2024 15:27:06 +0900 Message-ID: <7da099d239edb0ca4ef4a5c8c6a5155e0f1b0c4c.1731290567.git.thehajime@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241110_222819_406143_BEB4028E X-CRM114-Status: GOOD ( 14.36 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: As syscall translation done by zpoline assumes that there are no direct syscalls issued by userspace code, but there would be possibly issued by 1) dlopen-ed code containing syscall instructions, or 2 [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:434 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime(at)gmail.com] X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org As syscall translation done by zpoline assumes that there are no direct syscalls issued by userspace code, but there would be possibly issued by 1) dlopen-ed code containing syscall instructions, or 2) JIT-generated code. This commit add a seccomp filter to prevent such syscalls from userspace code. Signed-off-by: Hajime Tazaki --- arch/um/include/shared/os.h | 3 ++ arch/um/kernel/um_arch.c | 4 ++ arch/um/os-Linux/process.c | 76 +++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/arch/um/include/shared/os.h b/arch/um/include/shared/os.h index 6874be0c38a8..5a6722f254d5 100644 --- a/arch/um/include/shared/os.h +++ b/arch/um/include/shared/os.h @@ -220,6 +220,9 @@ extern int os_unmap_memory(void *addr, int len); extern int os_drop_memory(void *addr, int length); extern int can_drop_memory(void); extern int os_mincore(void *addr, unsigned long len); +#ifndef CONFIG_MMU +extern int os_setup_seccomp(void); +#endif void os_set_pdeathsig(void); diff --git a/arch/um/kernel/um_arch.c b/arch/um/kernel/um_arch.c index ec17576ce9fc..694e428ddf35 100644 --- a/arch/um/kernel/um_arch.c +++ b/arch/um/kernel/um_arch.c @@ -433,6 +433,10 @@ void __init setup_arch(char **cmdline_p) add_bootloader_randomness(rng_seed, sizeof(rng_seed)); memzero_explicit(rng_seed, sizeof(rng_seed)); } + +#ifndef CONFIG_MMU + os_setup_seccomp(); +#endif } void __init arch_cpu_finalize_init(void) diff --git a/arch/um/os-Linux/process.c b/arch/um/os-Linux/process.c index ef1a2f0aa06a..ed3d99301dc8 100644 --- a/arch/um/os-Linux/process.c +++ b/arch/um/os-Linux/process.c @@ -17,7 +17,11 @@ #include #include #include +#include #include +#include +#include +#include void os_alarm_process(int pid) { @@ -209,3 +213,75 @@ void os_set_pdeathsig(void) { prctl(PR_SET_PDEATHSIG, SIGKILL); } + +#ifndef CONFIG_MMU +int os_setup_seccomp(void) +{ + int err; + unsigned long __userspace_start = uml_reserved, + __userspace_end = high_physmem; + + struct sock_filter filter[] = { + /* if (IP_high > __userspace_end) allow; */ + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, + offsetof(struct seccomp_data, instruction_pointer) + 4), + BPF_JUMP(BPF_JMP + BPF_JGT + BPF_K, __userspace_end >> 32, + /*true-skip=*/0, /*false-skip=*/1), + BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), + + /* if (IP_high == __userspace_end && IP_low >= __userspace_end) allow; */ + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, + offsetof(struct seccomp_data, instruction_pointer) + 4), + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __userspace_end >> 32, + /*true-skip=*/0, /*false-skip=*/3), + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, + offsetof(struct seccomp_data, instruction_pointer)), + BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, __userspace_end, + /*true-skip=*/0, /*false-skip=*/1), + BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), + + /* if (IP_high < __userspace_start) allow; */ + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, + offsetof(struct seccomp_data, instruction_pointer) + 4), + BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, __userspace_start >> 32, + /*true-skip=*/1, /*false-skip=*/0), + BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), + + /* if (IP_high == __userspace_start && IP_low < __userspace_start) allow; */ + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, + offsetof(struct seccomp_data, instruction_pointer) + 4), + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __userspace_start >> 32, + /*true-skip=*/0, /*false-skip=*/3), + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, + offsetof(struct seccomp_data, instruction_pointer)), + BPF_JUMP(BPF_JMP + BPF_JGE + BPF_K, __userspace_start, + /*true-skip=*/1, /*false-skip=*/0), + BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), + + /* other address; trap */ + BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_TRAP), + }; + struct sock_fprog prog = { + .len = ARRAY_SIZE(filter), + .filter = filter, + }; + + err = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); + if (err) + os_warn("PR_SET_NO_NEW_PRIVS (err=%d, ernro=%d)\n", + err, errno); + + err = syscall(SYS_seccomp, SECCOMP_SET_MODE_FILTER, + SECCOMP_FILTER_FLAG_TSYNC, &prog); + if (err) { + os_warn("SECCOMP_SET_MODE_FILTER (err=%d, ernro=%d)\n", + err, errno); + exit(-1); + } + + os_info("seccomp: filter syscalls in the range: 0x%lx-0x%lx\n", + __userspace_start, __userspace_end); + + return 0; +} +#endif From patchwork Mon Nov 11 06:27:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 2009413 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=g6Fo1JRw; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=QXKrnmmW; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xn03y47kPz1xyB for ; Mon, 11 Nov 2024 17:28:30 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=meRC4zEq37TyfhrYFoYjU8XELa8Mik0M7wCNHRqMtao=; b=g6Fo1JRw0HEDjrjy6aYPmF7+j0 bPYS+HUwV+Br7hFe7137ZL9eAP6GzV6xTAlIgIWR3W1/FV4PVoe+5VD6jgjYbsAckUfrokiHV0kju MLCxJEZ//BIvoxFQmVHsNFGCfcAQLe9O9lcWlGAMna3/vzm+eumW5XAPGrtNOrnuDdtfvahHRqiD9 RETSS09t/z6WBj/EeBNAPX+iFdWyEcJkOX40zHnWExV0W42cTNbJRwdYA3tRgHzmOT5FDA/5k16YS oaV4HMMHeJuVzuK1alv0lVCz5ddv+np5zOzPga56PCPLa1ql7aNENjc7EbACgh5VhPm6n/oltPtLU YTIupJ9g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tANuW-0000000GUGP-3zvy; Mon, 11 Nov 2024 06:28:28 +0000 Received: from mail-ot1-x330.google.com ([2607:f8b0:4864:20::330]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tANuT-0000000GUFt-2UQx for linux-um@lists.infradead.org; Mon, 11 Nov 2024 06:28:27 +0000 Received: by mail-ot1-x330.google.com with SMTP id 46e09a7af769-7180c7a4e02so2342029a34.0 for ; Sun, 10 Nov 2024 22:28:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731306505; x=1731911305; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=meRC4zEq37TyfhrYFoYjU8XELa8Mik0M7wCNHRqMtao=; b=QXKrnmmW8Q6Z+4qjnr+tE9mE5gNAMwfiRwxT6gXBtQotgrR7XlBXXBushPOH9JBUe+ jJCRsScgf9f155gB9ENt26q8Aco+E/TN8cAv60gR2GjfEQLiKnGqoevISWC/kFoLrswp 3QbZ7xbuwyp3XnPXPh4Vido6mAPW1P+aMIIv7ginr7wQtFeW+ewQ3Jt1/hMknRUI+fZN w3OXAaulFbw91IKl4Zcq+HFu7UZYC2/2vXpp3zXhZQOIcRCjFuSXtzp/gBfdhidVT0/B VZHlhMFx5X2yM98xyqYWTlAJ52Y+n+FJ/Q3pyM+6CPXI57NWkzIg5IejCABUehpXtU1S 1nTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731306505; x=1731911305; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=meRC4zEq37TyfhrYFoYjU8XELa8Mik0M7wCNHRqMtao=; b=GcHmxuNZ0VBkmZDmh/KlyULPNjO84JrtPYMt8A9ReuoQvzkZwWXNsO9z463772EPxW OxVz2uAozaA2oLu6XAZgNFL61jSAPeSJJ6FHabHZz32JLlWQ06yBF/aBRuu3+JNmZ6lR fuKTlT/aWbm+v330G8qr773RHxPGfXkPH3785ivIP2GA/Qca3qP82hQzwMdOkL2WTrQb RTXllelGPT7PiFw2upnQeBXDj6gSRKkojkxMvk6JYXSdl1pyXaxRU3oO+GqFrVrds1ko 7T3fdFTmnY46SBaoNWSFJueTLC+8yzNv5n+x3GyAe4Xq02JIKtkRUv0eONAQouoRQW5n 67uA== X-Gm-Message-State: AOJu0YwB4CP2tBOD9ykwbgfDPeuMpngnoOTyZNIbg9iJTi3TO9cELcFJ L/hmB6Pq+0/pxLAwqAbL5GGLNvSyDqyQcUVzXTViOtppA9L/WG+HjskqVg== X-Google-Smtp-Source: AGHT+IFLWxCd/FMsBpjE11NSml9TLL9peHSbfnNhDxS/95o6zyeoYcyqG1EnAyAzazUCGpz9ZJKOHg== X-Received: by 2002:a05:6830:2a87:b0:718:ad8a:e2ce with SMTP id 46e09a7af769-71a1c24a724mr8805877a34.17.1731306504611; Sun, 10 Nov 2024 22:28:24 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7f41f5c26a6sm7845568a12.30.2024.11.10.22.28.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 22:28:23 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id 0B399DBA920; Mon, 11 Nov 2024 15:28:22 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com Subject: [RFC PATCH v2 07/13] x86/um: nommu: process/thread handling Date: Mon, 11 Nov 2024 15:27:07 +0900 Message-ID: <3c850be9ec2a50b1de7d42b0649f254fe8472527.1731290567.git.thehajime@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241110_222825_662662_10EA8832 X-CRM114-Status: GOOD ( 22.82 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Since ptrace facility isn't used under !MMU of UML, there is different code path to invoke processes/threads; on an entry to the syscall interface, the stack pointer should be manipulated to handle vf [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:330 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime(at)gmail.com] X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Since ptrace facility isn't used under !MMU of UML, there is different code path to invoke processes/threads; on an entry to the syscall interface, the stack pointer should be manipulated to handle vfork(2) return address, no external process is used, and need to properly configure some of registers (fs segment register for TLS, etc) on every context switch, etc. Signals aren't delivered in non-ptrace syscall entry/leave so, we also need to handle pending signal by ourselves. Signed-off-by: Hajime Tazaki Signed-off-by: Ricardo Koller --- arch/um/kernel/process.c | 33 +++++++++++++++++++++++++++++- arch/um/os-Linux/process.c | 6 ++++++ arch/um/os-Linux/skas/process.c | 4 ++++ arch/x86/um/asm/processor.h | 12 +++++++++++ arch/x86/um/do_syscall_64.c | 36 +++++++++++++++++++++++++++++++++ arch/x86/um/entry_64.S | 21 +++++++++++++++++++ arch/x86/um/syscalls_64.c | 12 +++++++++++ 7 files changed, 123 insertions(+), 1 deletion(-) diff --git a/arch/um/kernel/process.c b/arch/um/kernel/process.c index 56e7e525fc91..b3708dceb731 100644 --- a/arch/um/kernel/process.c +++ b/arch/um/kernel/process.c @@ -116,13 +116,17 @@ void new_thread_handler(void) * callback returns only if the kernel thread execs a process */ fn(arg); +#ifndef CONFIG_MMU + arch_switch_to(current); +#endif userspace(¤t->thread.regs.regs); } /* Called magically, see new_thread_handler above */ static void fork_handler(void) { - schedule_tail(current->thread.prev_sched); + if (current->thread.prev_sched) + schedule_tail(current->thread.prev_sched); /* * XXX: if interrupt_end() calls schedule, this call to @@ -133,6 +137,33 @@ static void fork_handler(void) current->thread.prev_sched = NULL; +#ifndef CONFIG_MMU + /* + * child of vfork(2) comes here. + * clone(2) also enters here but doesn't need to advance the %rsp. + * + * This fork can only come from libc's vfork, which + * does this: + * popq %%rdx; + * call *%rax; // zpoline => __kernel_vsyscall + * pushq %%rdx; + * %rcx stores the return address which is stored + * at pt_regs[HOST_IP] at the moment. As child returns + * via userspace() with a jmp instruction (while parent + * does via ret instruction in __kernel_vsyscall), we + * need to pop (advance) the pushed address by "call" + * though, so this is what this next line does. + * + * As a result of vfork return in child, stack contents + * is overwritten by child (by pushq in vfork), which + * makes the parent puzzled after child returns. + * + * thus the contents should be restored before vfork/parent + * returns. this is done in do_syscall_64(). + */ + if (current->thread.regs.regs.gp[HOST_ORIG_AX] == __NR_vfork) + current->thread.regs.regs.gp[REGS_SP_INDEX] += 8; +#endif userspace(¤t->thread.regs.regs); } diff --git a/arch/um/os-Linux/process.c b/arch/um/os-Linux/process.c index ed3d99301dc8..5acf6d41a4c2 100644 --- a/arch/um/os-Linux/process.c +++ b/arch/um/os-Linux/process.c @@ -25,7 +25,10 @@ void os_alarm_process(int pid) { +/* !CONFIG_MMU doesn't send alarm signal to other processes */ +#ifdef CONFIG_MMU kill(pid, SIGALRM); +#endif } void os_kill_process(int pid, int reap_child) @@ -42,11 +45,14 @@ void os_kill_process(int pid, int reap_child) void os_kill_ptraced_process(int pid, int reap_child) { +/* !CONFIG_MMU doesn't have ptraced process */ +#ifdef CONFIG_MMU kill(pid, SIGKILL); ptrace(PTRACE_KILL, pid); ptrace(PTRACE_CONT, pid); if (reap_child) CATCH_EINTR(waitpid(pid, NULL, __WALL)); +#endif } /* Don't use the glibc version, which caches the result in TLS. It misses some diff --git a/arch/um/os-Linux/skas/process.c b/arch/um/os-Linux/skas/process.c index f683cfc9e51a..291136008431 100644 --- a/arch/um/os-Linux/skas/process.c +++ b/arch/um/os-Linux/skas/process.c @@ -144,6 +144,7 @@ void wait_stub_done(int pid) extern unsigned long current_stub_stack(void); +#ifdef CONFIG_MMU static void get_skas_faultinfo(int pid, struct faultinfo *fi) { int err; @@ -176,6 +177,7 @@ static void handle_trap(int pid, struct uml_pt_regs *regs) handle_syscall(regs); } +#endif extern char __syscall_stub_start[]; @@ -389,6 +391,7 @@ int start_userspace(unsigned long stub_stack) } int unscheduled_userspace_iterations; +#ifdef CONFIG_MMU extern unsigned long tt_extra_sched_jiffies; void userspace(struct uml_pt_regs *regs) @@ -550,6 +553,7 @@ void userspace(struct uml_pt_regs *regs) } } } +#endif /* UML_CONFIG_MMU */ void new_thread(void *stack, jmp_buf *buf, void (*handler)(void)) { diff --git a/arch/x86/um/asm/processor.h b/arch/x86/um/asm/processor.h index 478710384b34..d88d7d9d5c18 100644 --- a/arch/x86/um/asm/processor.h +++ b/arch/x86/um/asm/processor.h @@ -38,6 +38,18 @@ static __always_inline void cpu_relax(void) #define task_pt_regs(t) (&(t)->thread.regs) +#ifndef CONFIG_MMU +#define task_top_of_stack(task) \ +({ \ + unsigned long __ptr = (unsigned long)task->stack; \ + __ptr += THREAD_SIZE; \ + __ptr; \ +}) + +extern long current_top_of_stack; +extern long current_ptregs; +#endif + #include #endif diff --git a/arch/x86/um/do_syscall_64.c b/arch/x86/um/do_syscall_64.c index a1189ddb2b50..203bacc4cb3c 100644 --- a/arch/x86/um/do_syscall_64.c +++ b/arch/x86/um/do_syscall_64.c @@ -1,14 +1,43 @@ // SPDX-License-Identifier: GPL-2.0 +//#define DEBUG 1 #include #include #include #include #include +/* + * save/restore the return address stored in the stack, as the child overwrites + * the contents after returning to userspace (i.e., by push %rdx). + * + * see the detail in fork_handler(). + */ +static void *vfork_save_stack(void) +{ + unsigned char *stack_copy; + + stack_copy = kzalloc(8, GFP_KERNEL); + if (!stack_copy) + return NULL; + + memcpy(stack_copy, + (void *)current->thread.regs.regs.gp[HOST_SP], 8); + + return stack_copy; +} + +static void vfork_restore_stack(void *stack_copy) +{ + WARN_ON_ONCE(!stack_copy); + memcpy((void *)current->thread.regs.regs.gp[HOST_SP], + stack_copy, 8); +} + __visible void do_syscall_64(struct pt_regs *regs) { int syscall; + unsigned char *stack_copy = NULL; syscall = PT_SYSCALL_NR(regs->regs.gp); UPT_SYSCALL_NR(®s->regs) = syscall; @@ -17,6 +46,9 @@ __visible void do_syscall_64(struct pt_regs *regs) syscall, (unsigned long)current, (unsigned long)sys_call_table[syscall]); + if (syscall == __NR_vfork) + stack_copy = vfork_save_stack(); + if (likely(syscall < NR_syscalls)) { PT_REGS_SET_SYSCALL_RETURN(regs, EXECUTE_SYSCALL(syscall, regs)); @@ -34,4 +66,8 @@ __visible void do_syscall_64(struct pt_regs *regs) /* execve succeeded */ if (syscall == __NR_execve && regs->regs.gp[HOST_AX] == 0) userspace(¤t->thread.regs.regs); + + /* only parents of vfork restores the contents of stack */ + if (syscall == __NR_vfork && regs->regs.gp[HOST_AX] > 0) + vfork_restore_stack(stack_copy); } diff --git a/arch/x86/um/entry_64.S b/arch/x86/um/entry_64.S index 022a8122690b..32f5002e2eb0 100644 --- a/arch/x86/um/entry_64.S +++ b/arch/x86/um/entry_64.S @@ -85,3 +85,24 @@ ENTRY(__kernel_vsyscall) ret END(__kernel_vsyscall) + +// void userspace(struct uml_pt_regs *regs) +ENTRY(userspace) + /* align the stack for x86_64 ABI */ + and $-0x10, %rsp + /* Handle any immediate reschedules or signals */ + call interrupt_end + + movq current_ptregs, %rsp + + POP_REGS + + addq $8, %rsp /* skip orig_ax */ + popq %r11 /* pt_regs->ip */ + addq $8, %rsp /* skip cs */ + addq $8, %rsp /* skip flags */ + popq %rsp + + jmp *%r11 + +END(userspace) diff --git a/arch/x86/um/syscalls_64.c b/arch/x86/um/syscalls_64.c index 6a00a28c9cca..edb17fc73e07 100644 --- a/arch/x86/um/syscalls_64.c +++ b/arch/x86/um/syscalls_64.c @@ -51,6 +51,18 @@ void arch_switch_to(struct task_struct *to) * Nothing needs to be done on x86_64. * The FS_BASE/GS_BASE registers are saved in the ptrace register set. */ +#ifndef CONFIG_MMU + current_top_of_stack = task_top_of_stack(to); + current_ptregs = (long)task_pt_regs(to); + + if ((to->thread.regs.regs.gp[FS_BASE / sizeof(unsigned long)] == 0) || + (to->mm == NULL)) + return; + + /* this changes the FS on every context switch */ + arch_prctl(to, ARCH_SET_FS, + (void __user *) to->thread.regs.regs.gp[FS_BASE / sizeof(unsigned long)]); +#endif } SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len, From patchwork Mon Nov 11 06:27:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 2009414 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=q7KhXbye; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=aWa6rniL; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xn0450JD1z1xyB for ; Mon, 11 Nov 2024 17:28:37 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=pN5CuG6WF+TQ5+fRPMrqHbPBVi3cfYGsR4fmOv8Ey5Y=; b=q7KhXbyeNHq46L28CjPSR8vzH5 8e18rbsHk+dXUwPopJ3soW7jPFOG1IVxubDQlDiMO4LSSXolAP8bWrgVOep5Qa+OZ/VbJmL/Qqwr+ +u5FPDelyYa+++kIGTfkNw8tTgEfyzn7dV2CvapxCXiF4Ivb2ojatJeKDUnZ446qMO9/nDhsSMwg8 jPKt6tvposusnCQVMk8HXl3/Z7w9CLqteGzQmhMbmwWk2cB0iZRLA9D8Jv3q4LHCfwhhb5ZubXHiX 3nPTNHdzZAy+ne7PWO7oozLEK1H5FLteoie6mqdpVYKRR9mYal0gZssFoeQD2IQ5IDDFYq+FTVYVd xs3mxdpA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tANuc-0000000GUHh-1xrA; Mon, 11 Nov 2024 06:28:34 +0000 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tANua-0000000GUH3-0ItG for linux-um@lists.infradead.org; Mon, 11 Nov 2024 06:28:33 +0000 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-720be27db27so3306359b3a.2 for ; Sun, 10 Nov 2024 22:28:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731306511; x=1731911311; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pN5CuG6WF+TQ5+fRPMrqHbPBVi3cfYGsR4fmOv8Ey5Y=; b=aWa6rniLWvaw47hdDrNM0SJxRZ3g3XKHrXsnc2kteRrON905JoeC39bmRPs150lDl5 zREzxYjJkrdRX5kV2kO1tpW+Db3oWcxbVpDYyKfawASKga6geztw3h/9JB2oNGgFSgEN ltbbhqgMXatUz+vIHUeW6bKWljdJtGDUNunwQy/JR64uyep6LijRaUKEJYsVtfgxsvfK B0eeZ7zpqWV/OmYq+5G3D3z1utE1/+32zuYJrOi7e1VQryTYch6Km7Kgb/vDSda+9jNy iNLo33BHghltvQhPnx1Z7aPpMxxYzD/4iLEqh3rL6UD+iWWPP5NtM/EoCSbRQsedMW0t 9XiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731306511; x=1731911311; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pN5CuG6WF+TQ5+fRPMrqHbPBVi3cfYGsR4fmOv8Ey5Y=; b=GWT65w+e71AeK91nMGIOAflOS1dEaxt6Hl69q7y37wABUCbJ4X9OVfep4aUmVIGU/H 6LxFxRdmUUCU0TnY/gsYcsrY6YE16K6fKM1AoxthH2rqG7OKK9HLzXEPyMksrH/aStbc /Y6nwyZ2A7azy6A1yIInQQuSZ46URp5u28PjDbWl2rO3K1SyCZDqYreG/95p9yer6bJe XMGXmslN6IWr++w+osDf7dp+pKjKWPiTX5vKDgT/UWg7zdIQl31NMMWO+PtixzRh6KWa DfAVirZQbVjemtUY3tueElMKb8uKPFwfYtkB0fvnVVH4yf/6aYplwmiLU/jacAVy608i C6kw== X-Gm-Message-State: AOJu0YwpLylbK0bb93c7JO2qvUJX83lipZmX0INu2sRKwP2oW9f65slS 2SSCKJc/wuGxvPsymG4ZNVzndGJL+qI8zgrtuTHEI2PFwy0RpkSs1lgidA== X-Google-Smtp-Source: AGHT+IHIE/l9dDw1fdeL3b0/UPQXxNz7NqKNUPhpYAcMTt/x0bA1w8jw4aORSXBxWUAjHqBdzyMUiQ== X-Received: by 2002:a05:6a00:851:b0:71e:3b8f:926 with SMTP id d2e1a72fcca58-724133a169cmr16411712b3a.22.1731306511200; Sun, 10 Nov 2024 22:28:31 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-724079a4177sm8267587b3a.99.2024.11.10.22.28.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 22:28:30 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id B4B16DBA922; Mon, 11 Nov 2024 15:28:28 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com Subject: [RFC PATCH v2 08/13] um: nommu: configure fs register on host syscall invocation Date: Mon, 11 Nov 2024 15:27:08 +0900 Message-ID: <894d17f7924e7d31bfd5d6595ee84158f7411e47.1731290567.git.thehajime@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241110_222832_142001_C5E59481 X-CRM114-Status: GOOD ( 29.77 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: As userspace on UML/!MMU also need to configure %fs register when it is running to correctly access thread structure, host syscalls implemented in os-Linux drivers may be puzzled when they are called. [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:42e listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime(at)gmail.com] X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org As userspace on UML/!MMU also need to configure %fs register when it is running to correctly access thread structure, host syscalls implemented in os-Linux drivers may be puzzled when they are called. Thus it has to configure %fs register via arch_prctl(SET_FS) on every host syscalls. Signed-off-by: Hajime Tazaki Signed-off-by: Ricardo Koller --- arch/um/include/shared/os.h | 5 ++++ arch/um/os-Linux/Makefile | 4 +-- arch/um/os-Linux/cpu.c | 50 ++++++++++++++++++++++++++++++++ arch/um/os-Linux/internal.h | 5 ++++ arch/um/os-Linux/main.c | 5 ++++ arch/um/os-Linux/process.c | 8 ++++++ arch/um/os-Linux/start_up.c | 3 ++ arch/x86/um/do_syscall_64.c | 35 +++++++++++++++++++++++ arch/x86/um/syscalls_64.c | 57 +++++++++++++++++++++++++++++++++++++ 9 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 arch/um/os-Linux/cpu.c diff --git a/arch/um/include/shared/os.h b/arch/um/include/shared/os.h index 5a6722f254d5..69a7854f5f87 100644 --- a/arch/um/include/shared/os.h +++ b/arch/um/include/shared/os.h @@ -136,6 +136,9 @@ static inline struct openflags of_cloexec(struct openflags flags) return flags; } +/* cpu.c */ +extern int host_has_fsgsbase; + /* file.c */ extern int os_stat_file(const char *file_name, struct uml_stat *buf); extern int os_stat_fd(const int fd, struct uml_stat *buf); @@ -221,6 +224,8 @@ extern int os_drop_memory(void *addr, int length); extern int can_drop_memory(void); extern int os_mincore(void *addr, unsigned long len); #ifndef CONFIG_MMU +extern long long host_fs; +extern int os_arch_prctl(int pid, int option, unsigned long *arg); extern int os_setup_seccomp(void); #endif diff --git a/arch/um/os-Linux/Makefile b/arch/um/os-Linux/Makefile index 20ff8d5971db..af7c5f4373bc 100644 --- a/arch/um/os-Linux/Makefile +++ b/arch/um/os-Linux/Makefile @@ -8,7 +8,7 @@ KCOV_INSTRUMENT := n obj-y = execvp.o file.o helper.o irq.o main.o mem.o process.o \ registers.o sigio.o signal.o start_up.o time.o tty.o \ - umid.o user_syms.o util.o drivers/ skas/ + umid.o user_syms.o util.o cpu.o drivers/ skas/ CFLAGS_signal.o += -Wframe-larger-than=4096 @@ -18,7 +18,7 @@ obj-$(CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA) += elf_aux.o USER_OBJS := $(user-objs-y) elf_aux.o execvp.o file.o helper.o irq.o \ main.o mem.o process.o registers.o sigio.o signal.o start_up.o time.o \ - tty.o umid.o util.o + tty.o umid.o util.o cpu.o include $(srctree)/arch/um/scripts/Makefile.rules CFLAGS_process.o=-g -O0 diff --git a/arch/um/os-Linux/cpu.c b/arch/um/os-Linux/cpu.c new file mode 100644 index 000000000000..49b6d8b4d65d --- /dev/null +++ b/arch/um/os-Linux/cpu.c @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include "internal.h" + +int host_has_fsgsbase; +/* those definitions can be pulled from os.h but if we include this + * it shows conflicts of jmp_buf definitions in longjmp.h (UM) and + * host one. thus we declared here instead. + */ +void os_info(const char *fmt, ...); +void os_warn(const char *fmt, ...); + +/** + * get_host_cpu_features() return true with X86_FEATURE_FSGSBASE even + * if the kernel is older and disabled using fsgsbase instruction. + * thus detection is based on whether SIGILL is raised or not. + */ +static jmp_buf jmpbuf; +static void sigill(int sig, siginfo_t *si, void *ctx_void) +{ + siglongjmp(jmpbuf, 1); +} + +void __init check_fsgsbase(void) +{ + unsigned long fsbase; + struct sigaction sa; + + /* Probe FSGSBASE */ + memset(&sa, 0, sizeof(sa)); + sa.sa_sigaction = sigill; + sa.sa_flags = SA_SIGINFO | SA_RESETHAND; + sigemptyset(&sa.sa_mask); + if (sigaction(SIGILL, &sa, 0)) + os_warn("sigaction"); + + os_info("Checking FSGSBASE instructions..."); + if (sigsetjmp(jmpbuf, 0) == 0) { + asm volatile("rdfsbase %0" : "=r" (fsbase) :: "memory"); + host_has_fsgsbase = 1; + os_info("OK\n"); + } else { + host_has_fsgsbase = 0; + os_info("disabled\n"); + } +} diff --git a/arch/um/os-Linux/internal.h b/arch/um/os-Linux/internal.h index 317fca190c2b..60220b8b8843 100644 --- a/arch/um/os-Linux/internal.h +++ b/arch/um/os-Linux/internal.h @@ -2,6 +2,11 @@ #ifndef __UM_OS_LINUX_INTERNAL_H #define __UM_OS_LINUX_INTERNAL_H +/* + * cpu.c + */ +void check_fsgsbase(void); + /* * elf_aux.c */ diff --git a/arch/um/os-Linux/main.c b/arch/um/os-Linux/main.c index 0afcdeb8995b..aecf63d3db79 100644 --- a/arch/um/os-Linux/main.c +++ b/arch/um/os-Linux/main.c @@ -17,6 +17,7 @@ #include #include #include +#include /* XXX This should get the constants from libc */ #include "internal.h" #define PGD_BOUND (4 * 1024 * 1024) @@ -158,6 +159,10 @@ int __init main(int argc, char **argv, char **envp) change_sig(SIGPIPE, 0); ret = linux_main(argc, argv, envp); +#ifndef CONFIG_MMU + os_arch_prctl(0, ARCH_SET_FS, (void *)host_fs); +#endif + /* * Disable SIGPROF - I have no idea why libc doesn't do this or turn * off the profiling time, but UML dies with a SIGPROF just before diff --git a/arch/um/os-Linux/process.c b/arch/um/os-Linux/process.c index 5acf6d41a4c2..5a3b09096f92 100644 --- a/arch/um/os-Linux/process.c +++ b/arch/um/os-Linux/process.c @@ -221,6 +221,14 @@ void os_set_pdeathsig(void) } #ifndef CONFIG_MMU +#include +#include /* For SYS_xxx definitions */ + +int os_arch_prctl(int pid, int option, unsigned long *arg2) +{ + return syscall(SYS_arch_prctl, option, arg2); +} + int os_setup_seccomp(void) { int err; diff --git a/arch/um/os-Linux/start_up.c b/arch/um/os-Linux/start_up.c index 93fc82c01aba..88164893cbec 100644 --- a/arch/um/os-Linux/start_up.c +++ b/arch/um/os-Linux/start_up.c @@ -293,6 +293,9 @@ void __init os_early_checks(void) */ check_tmpexec(); + /* probe fsgsbase instruction */ + check_fsgsbase(); + pid = start_ptraced_child(); if (init_pid_registers(pid)) fatal("Failed to initialize default registers"); diff --git a/arch/x86/um/do_syscall_64.c b/arch/x86/um/do_syscall_64.c index 203bacc4cb3c..75326acc931b 100644 --- a/arch/x86/um/do_syscall_64.c +++ b/arch/x86/um/do_syscall_64.c @@ -3,6 +3,8 @@ //#define DEBUG 1 #include #include +#include +#include #include #include #include @@ -34,6 +36,31 @@ static void vfork_restore_stack(void *stack_copy) stack_copy, 8); } +static int os_x86_arch_prctl(int pid, int option, unsigned long *arg2) +{ + if (host_has_fsgsbase) { + switch (option) { + case ARCH_SET_FS: + wrfsbase(*arg2); + break; + case ARCH_SET_GS: + wrgsbase(*arg2); + break; + case ARCH_GET_FS: + *arg2 = rdfsbase(); + break; + case ARCH_GET_GS: + *arg2 = rdgsbase(); + break; + } + return 0; + } else { + return os_arch_prctl(pid, option, arg2); + } + + return 0; +} + __visible void do_syscall_64(struct pt_regs *regs) { int syscall; @@ -49,6 +76,9 @@ __visible void do_syscall_64(struct pt_regs *regs) if (syscall == __NR_vfork) stack_copy = vfork_save_stack(); + /* set fs register to the original host one */ + os_x86_arch_prctl(0, ARCH_SET_FS, (void *)host_fs); + if (likely(syscall < NR_syscalls)) { PT_REGS_SET_SYSCALL_RETURN(regs, EXECUTE_SYSCALL(syscall, regs)); @@ -63,6 +93,11 @@ __visible void do_syscall_64(struct pt_regs *regs) set_thread_flag(TIF_SIGPENDING); interrupt_end(); + /* restore back fs register to userspace configured one */ + os_x86_arch_prctl(0, ARCH_SET_FS, + (void *)(current->thread.regs.regs.gp[FS_BASE + / sizeof(unsigned long)])); + /* execve succeeded */ if (syscall == __NR_execve && regs->regs.gp[HOST_AX] == 0) userspace(¤t->thread.regs.regs); diff --git a/arch/x86/um/syscalls_64.c b/arch/x86/um/syscalls_64.c index edb17fc73e07..d56df936a2d7 100644 --- a/arch/x86/um/syscalls_64.c +++ b/arch/x86/um/syscalls_64.c @@ -12,11 +12,26 @@ #include /* XXX This should get the constants from libc */ #include #include +#include +#include + +#ifndef CONFIG_MMU +/* + * The guest libc can change FS, which confuses the host libc. + * In fact, changing FS directly is not supported (check + * man arch_prctl). So, whenever we make a host syscall, + * we should be changing FS to the original FS (not the + * one set by the guest libc). This original FS is stored + * in host_fs. + */ +long long host_fs = -1; +#endif long arch_prctl(struct task_struct *task, int option, unsigned long __user *arg2) { long ret = -EINVAL; +#ifdef CONFIG_MMU switch (option) { case ARCH_SET_FS: @@ -38,6 +53,48 @@ long arch_prctl(struct task_struct *task, int option, } return ret; +#else + + unsigned long *ptr = arg2, tmp; + + switch (option) { + case ARCH_SET_FS: + if (host_fs == -1) + os_arch_prctl(0, ARCH_GET_FS, (void *)&host_fs); + ret = 0; + break; + case ARCH_SET_GS: + ret = 0; + break; + case ARCH_GET_FS: + case ARCH_GET_GS: + ptr = &tmp; + break; + } + + ret = os_arch_prctl(0, option, ptr); + if (ret) + return ret; + + switch (option) { + case ARCH_SET_FS: + current->thread.regs.regs.gp[FS_BASE / sizeof(unsigned long)] = + (unsigned long) arg2; + break; + case ARCH_SET_GS: + current->thread.regs.regs.gp[GS_BASE / sizeof(unsigned long)] = + (unsigned long) arg2; + break; + case ARCH_GET_FS: + ret = put_user(current->thread.regs.regs.gp[FS_BASE / sizeof(unsigned long)], arg2); + break; + case ARCH_GET_GS: + ret = put_user(current->thread.regs.regs.gp[GS_BASE / sizeof(unsigned long)], arg2); + break; + } + + return ret; +#endif } SYSCALL_DEFINE2(arch_prctl, int, option, unsigned long, arg2) From patchwork Mon Nov 11 06:27:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 2009415 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=TSB5J/5h; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=M7NCg25h; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xn04C5wkcz1xyB for ; Mon, 11 Nov 2024 17:28:43 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=4z/4pzvJnEqsoNvP0yTw36MEBQUM24HSeydPm0quFZo=; b=TSB5J/5hcEzxKBx06vUYt31WEm gLmccAmmTL3PhG6uaSV4trV1v9XF4w7vsnRwDb/nG+U0olHkJeG1I5ixq61tE4jACUxgBu0/bzMIo 2OWe0e2tCUlsrspSOZ2xPZ4BUANgjHO0DXXbRpGh2Wt3PwuIjrLJWIs3+uXVTlK5Rc1Z6eDM8wsJv +itp6NVtOIo7OfHBJlwjcyPlbSbZF9BHasmgfZ20oLz7KzQMCfGys0FialfP7p8BznQF5zhltfVtt V1ed0gPwimw50tJlSmi7WAGkQ3IAk++m19gZZ/3NUwHDpV4/VaIj6wICKipH+S1MkYIpUWr2iy7UB ndVocY7A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tANuk-0000000GUIw-0y3I; Mon, 11 Nov 2024 06:28:42 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tANuh-0000000GUIY-3Jks for linux-um@lists.infradead.org; Mon, 11 Nov 2024 06:28:41 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-2110a622d76so37024865ad.3 for ; Sun, 10 Nov 2024 22:28:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731306519; x=1731911319; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4z/4pzvJnEqsoNvP0yTw36MEBQUM24HSeydPm0quFZo=; b=M7NCg25hKbMY34Xkm5riFEGwn0Qv/Q6cfVJ2BP8puHc4ViM/z/UvN1+dn6uo0kOLp2 wxi6Yp8LYrctQp/tY8V8+Dig8rNDJJImxUY2w9Mpw+xvHI4lPzdvNtZ06RDNo1jyzKXw rYt7fo8sVW0SjL240RnmZcqhwvrkHnLz2kf4Z7gFTuLkY+VvOvI79YPlKn/m29m1DBz7 8cncE1hKXR3/hHudtwFkEZZ1iOkZMdQ/sk1KIBG0rDM2ez8UZVPMIKupKl774J/RKyMn /mTNEG1cXHMh107qTqeYgme5n/Jv/G+G8Cn82RzF+C/0/vyqNTDI84VCF6IWZU3qBYzL No5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731306519; x=1731911319; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4z/4pzvJnEqsoNvP0yTw36MEBQUM24HSeydPm0quFZo=; b=I+Kx4GRNP8iTbfAlwQ26m9+Ohf0qB39+TG18jLZa3sNwwgLE08It3c5cugeezcOdWi XzE+pnQ5uBTv86OtCxYTN0GfLdbsDDCLE2G8BgZaC7BGDWJJPunGvDPzbMrCAKRtPDM8 TR+vTCqToyMZSuuCd3JerUisktyCDphDQ7CsDpPCgIdhAdDDuqNXHmGQi+plQKHOVEMU 7Skcj2jd5Mtord2/CZwAvDkkM+lXfGSYUjiotc4cfss4l3TfhAJaKLIVfe0M96Kz5WRZ 6BNCChDuDukGHweZEgYVTR0tinNknn3WHdQEDnbtxqPZ1FDf0IEU5YBa8qqXbBqyBvtC J6mQ== X-Gm-Message-State: AOJu0YxVckitGapgSxEeWscV+56VuSP+V6HC7CHO3aio3AC8kxFCKt2R REEzgjotqV7HcraIqO9J6G88dyP77CESF8V+4MIrRBLXOs6v+erU X-Google-Smtp-Source: AGHT+IFHQ9X7/TrAAs6WcCIqiWxX7A1Ig4cRMY+7UcKdQp/I9O9Vw7sY8/AkJSseFlp1roqehXanmg== X-Received: by 2002:a17:902:f606:b0:20c:5cb1:ddf0 with SMTP id d9443c01a7336-21183c435fdmr159639215ad.10.1731306518872; Sun, 10 Nov 2024 22:28:38 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21177e58117sm69108265ad.172.2024.11.10.22.28.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 22:28:38 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id 3CF88DBA924; Mon, 11 Nov 2024 15:28:36 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com Subject: [RFC PATCH v2 09/13] x86/um/vdso: nommu: vdso memory update Date: Mon, 11 Nov 2024 15:27:09 +0900 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241110_222839_852243_5988422A X-CRM114-Status: GOOD ( 12.49 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: On !MMU mode, the address of vdso is accessible from userspace. This commit implements the entry point by pointing a block of page address. This commit also add memory permission configuration of vdso page to be executable. Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:633 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime(at)gmail.com] X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org On !MMU mode, the address of vdso is accessible from userspace. This commit implements the entry point by pointing a block of page address. This commit also add memory permission configuration of vdso page to be executable. Signed-off-by: Hajime Tazaki Signed-off-by: Ricardo Koller --- arch/x86/um/vdso/um_vdso.c | 20 ++++++++++++++++++++ arch/x86/um/vdso/vma.c | 14 ++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/arch/x86/um/vdso/um_vdso.c b/arch/x86/um/vdso/um_vdso.c index cbae2584124f..eff3e6641a0e 100644 --- a/arch/x86/um/vdso/um_vdso.c +++ b/arch/x86/um/vdso/um_vdso.c @@ -23,10 +23,17 @@ int __vdso_clock_gettime(clockid_t clock, struct __kernel_old_timespec *ts) { long ret; +#ifdef CONFIG_MMU asm("syscall" : "=a" (ret) : "0" (__NR_clock_gettime), "D" (clock), "S" (ts) : "rcx", "r11", "memory"); +#else + asm("call *%1" + : "=a" (ret) + : "0" ((unsigned long)__NR_clock_gettime), "D" (clock), "S" (ts) + : "rcx", "r11", "memory"); +#endif return ret; } @@ -37,10 +44,17 @@ int __vdso_gettimeofday(struct __kernel_old_timeval *tv, struct timezone *tz) { long ret; +#ifdef CONFIG_MMU asm("syscall" : "=a" (ret) : "0" (__NR_gettimeofday), "D" (tv), "S" (tz) : "rcx", "r11", "memory"); +#else + asm("call *%1" + : "=a" (ret) + : "0" ((unsigned long)__NR_gettimeofday), "D" (tv), "S" (tz) + : "rcx", "r11", "memory"); +#endif return ret; } @@ -51,9 +65,15 @@ __kernel_old_time_t __vdso_time(__kernel_old_time_t *t) { long secs; +#ifdef CONFIG_MMU asm volatile("syscall" : "=a" (secs) : "0" (__NR_time), "D" (t) : "cc", "r11", "cx", "memory"); +#else + asm("call *%1" + : "=a" (secs) + : "0" ((unsigned long)__NR_time), "D" (t) : "cc", "r11", "cx", "memory"); +#endif return secs; } diff --git a/arch/x86/um/vdso/vma.c b/arch/x86/um/vdso/vma.c index f238f7b33cdd..83c861e2a815 100644 --- a/arch/x86/um/vdso/vma.c +++ b/arch/x86/um/vdso/vma.c @@ -9,6 +9,7 @@ #include #include #include +#include static unsigned int __read_mostly vdso_enabled = 1; unsigned long um_vdso_addr; @@ -24,7 +25,9 @@ static int __init init_vdso(void) BUG_ON(vdso_end - vdso_start > PAGE_SIZE); +#ifdef CONFIG_MMU um_vdso_addr = task_size - PAGE_SIZE; +#endif vdsop = kmalloc(sizeof(struct page *), GFP_KERNEL); if (!vdsop) @@ -40,6 +43,15 @@ static int __init init_vdso(void) copy_page(page_address(um_vdso), vdso_start); *vdsop = um_vdso; +#ifndef CONFIG_MMU + /* this is fine with NOMMU as everything is accessible */ + um_vdso_addr = (unsigned long)page_address(um_vdso); + os_protect_memory((void *)um_vdso_addr, vdso_end - vdso_start, 1, 1, 1); + pr_debug("vdso_start=%lx um_vdso_addr=%lx pg_um_vdso=%lx", + (unsigned long)vdso_start, um_vdso_addr, + (unsigned long)page_address(um_vdso)); +#endif + return 0; oom: @@ -50,6 +62,7 @@ static int __init init_vdso(void) } subsys_initcall(init_vdso); +#ifdef CONFIG_MMU int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) { struct vm_area_struct *vma; @@ -74,3 +87,4 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) return IS_ERR(vma) ? PTR_ERR(vma) : 0; } +#endif From patchwork Mon Nov 11 06:27:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 2009416 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=PaMNMvR1; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Gc2Rq9ri; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xn04J1y9Bz1xyB for ; Mon, 11 Nov 2024 17:28:48 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=P9W8sLSZVRt/hdna/XYup1SpEEQT3PS8wgsso2fTjHQ=; b=PaMNMvR1ptJpdsDrEiYTEpgZId OBQpFwhDPQ97YWk17+d6xLijCtYK2rtRVAeoRbBs/UyBX8boA56BLlBdSjA0jJHxKbAdpv0Zf632a qwORTuCP4hqEdxApGmzewtL59XRZcNrDgvMpgX0mZJ8VUJE2xd4ZB1T+G51q+IH6bVmj6DlMLkMW5 HwCDvrOcOkTRWFrMVeX0XP243jYeuIU/ajfFy2MZfnO7qqbyQyO1sQhEuIkisWLST7z4Heb4f0Voz ve7vBUUXBA0AYm232cEvr0tcSRgCiO+qh+yN82i546KII5a90kCL67hu11EGfmgSrM1Qa7R9sxd3h nVr+ESqA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tANuo-0000000GUKH-2kdy; Mon, 11 Nov 2024 06:28:46 +0000 Received: from mail-ot1-x335.google.com ([2607:f8b0:4864:20::335]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tANum-0000000GUJk-2Svb for linux-um@lists.infradead.org; Mon, 11 Nov 2024 06:28:45 +0000 Received: by mail-ot1-x335.google.com with SMTP id 46e09a7af769-7181b86a749so1696758a34.3 for ; Sun, 10 Nov 2024 22:28:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731306524; x=1731911324; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=P9W8sLSZVRt/hdna/XYup1SpEEQT3PS8wgsso2fTjHQ=; b=Gc2Rq9ri8H0KQKIpa1JYl3RKfYtm2QgxbqkgpMuzHLnDl6Zpel3E66nPsAUrAarr5G tWFxnoUtZ12+Gr+OyAp8uBFlZHyZWyawPTHQKVf/ag8Gbfixyzo8VYIEw60ng0FYCmDq zV+mkabFjfvevPh6J5cfRxvzJ/U7Ujm6Ops0se9eR7ZDb1xY/mxnCG9xAot+cd+U7uBJ QnXPHyETCAxo2i8c8HSGCs8z2yCTunYdJSSIFbwv658b1f6gOhckUUUbDLduTv7ek0g9 2Nu/A0blrgYs022FGEJPqkiR5FmhUQZLZCEmdMkBdpjQYtjtxdO9K2Bl1vBK7TCtWZVo 4U2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731306524; x=1731911324; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=P9W8sLSZVRt/hdna/XYup1SpEEQT3PS8wgsso2fTjHQ=; b=eAN59hsJlPSWWl3gXPq1Kq09HfMpfM76FvoV+z/fq32G4CFnTLeBOZLhw1zZhMjxzc YNpCjbkwyFUGory6SerzQTj1ZIiyND3GUvtj4h/0MH7zKJa3dkS9o58EHFfUDn8QxVJY 9x0CGOnSsooNr8XOYWJxTB4w6lK3i/6n+27CO9haApynEz+RQvVOWRwAoNaG2OnRcA9j IPmCumAbuo7G+U/srKWJ3r9zRgaJBEwv/O12LSUmcuWpOUU69O9H1t6iNJZEl1y6fZ// 0SRXt16SINSFdxz/dyaEshUwShh39p1m9JcMAoa52VRVtzmpDfnOYz/gF6YUVo5+zOAU YGgg== X-Gm-Message-State: AOJu0YySDIL0gF1tQuro3e6m3lNet+XNX4xoaHSkxwZl9w/MtSYAiGF2 AWjVeRuq9fiEsysr9veUsb6aNdv/+4qVRxHh6O3OdspQ7e0Zj8qg X-Google-Smtp-Source: AGHT+IGKB1lL6Ad17VRVApUJFc/Lgy9c2q0hsMuXNYHTpZUPb6caqXDCrgXYN/3oOxehePQpxivzcg== X-Received: by 2002:a05:6830:620f:b0:710:ea11:3d35 with SMTP id 46e09a7af769-71a1c221985mr9828249a34.15.1731306523622; Sun, 10 Nov 2024 22:28:43 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7f41f65fd7asm7722516a12.83.2024.11.10.22.28.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 22:28:42 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id ECFB3DBA926; Mon, 11 Nov 2024 15:28:40 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com Subject: [RFC PATCH v2 10/13] x86/um: nommu: signal handling Date: Mon, 11 Nov 2024 15:27:10 +0900 Message-ID: <5a769da2dcc8e7f9b89fbdbc4bccd0b8a1660309.1731290567.git.thehajime@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241110_222844_652056_9BB57CA0 X-CRM114-Status: GOOD ( 20.10 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This commit updates the behavior of signal handling under !MMU environment. 1) the stack preparation for the signal handlers and 2) restoration of stack after rt_sigreturn(2) syscall. Those are needed [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:335 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime(at)gmail.com] X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This commit updates the behavior of signal handling under !MMU environment. 1) the stack preparation for the signal handlers and 2) restoration of stack after rt_sigreturn(2) syscall. Those are needed as the stack usage on vfork(2) syscall is different. It also adds the follow up routine for SIGSEGV as a signal delivery runs in the same stack frame while we have to avoid endless SIGSEGV. Signed-off-by: Hajime Tazaki --- arch/um/include/shared/kern_util.h | 3 +++ arch/um/kernel/trap.c | 10 ++++++++ arch/um/os-Linux/signal.c | 18 ++++++++++++++- arch/x86/um/signal.c | 37 +++++++++++++++++++++++++++++- 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/arch/um/include/shared/kern_util.h b/arch/um/include/shared/kern_util.h index f21dc8517538..bcc8d28279ae 100644 --- a/arch/um/include/shared/kern_util.h +++ b/arch/um/include/shared/kern_util.h @@ -62,6 +62,9 @@ extern int singlestepping(void); extern void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs); extern void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs); extern void fatal_sigsegv(void) __attribute__ ((noreturn)); +#ifndef CONFIG_MMU +extern void sigsegv_post_routine(void); +#endif void um_idle_sleep(void); diff --git a/arch/um/kernel/trap.c b/arch/um/kernel/trap.c index a7519b3de4bf..b9b54e777894 100644 --- a/arch/um/kernel/trap.c +++ b/arch/um/kernel/trap.c @@ -174,6 +174,16 @@ void fatal_sigsegv(void) os_dump_core(); } +#ifndef CONFIG_MMU +void sigsegv_post_routine(void) +{ + change_sig(SIGIO, 1); + change_sig(SIGALRM, 1); + change_sig(SIGWINCH, 1); + userspace(¤t->thread.regs.regs); +} +#endif + /** * segv_handler() - the SIGSEGV handler * @sig: the signal number diff --git a/arch/um/os-Linux/signal.c b/arch/um/os-Linux/signal.c index 52852018a3ad..a06622415d8f 100644 --- a/arch/um/os-Linux/signal.c +++ b/arch/um/os-Linux/signal.c @@ -36,7 +36,15 @@ static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc) struct uml_pt_regs r; int save_errno = errno; - r.is_user = 0; +#ifndef CONFIG_MMU + memset(&r, 0, sizeof(r)); + /* mark is_user=1 when the IP is from userspace code. */ + if (mc && (REGS_IP(mc->gregs) > uml_reserved + && REGS_IP(mc->gregs) < high_physmem)) + r.is_user = 1; + else +#endif + r.is_user = 0; if (sig == SIGSEGV) { /* For segfaults, we want the data from the sigcontext. */ get_regs_from_mc(&r, mc); @@ -191,6 +199,7 @@ static void hard_handler(int sig, siginfo_t *si, void *p) ucontext_t *uc = p; mcontext_t *mc = &uc->uc_mcontext; unsigned long pending = 1UL << sig; + int is_segv = 0; do { int nested, bail; @@ -214,6 +223,7 @@ static void hard_handler(int sig, siginfo_t *si, void *p) while ((sig = ffs(pending)) != 0){ sig--; + is_segv = (sig == SIGSEGV) ? 1 : 0; pending &= ~(1 << sig); (*handlers[sig])(sig, (struct siginfo *)si, mc); } @@ -227,6 +237,12 @@ static void hard_handler(int sig, siginfo_t *si, void *p) if (!nested) pending = from_irq_stack(nested); } while (pending); + +#ifndef CONFIG_MMU + /* if there is SIGSEGV notified, let the userspace run w/ __noreturn */ + if (is_segv) + sigsegv_post_routine(); +#endif } void set_handler(int sig) diff --git a/arch/x86/um/signal.c b/arch/x86/um/signal.c index 75087e85b6fd..b7365c75a967 100644 --- a/arch/x86/um/signal.c +++ b/arch/x86/um/signal.c @@ -371,6 +371,13 @@ int setup_signal_stack_si(unsigned long stack_top, struct ksignal *ksig, round_down(stack_top - sizeof(struct rt_sigframe), 16); /* Add required space for math frame */ +#ifndef CONFIG_MMU + /* + * the sig_frame on !MMU needs be aligned for SSE as + * the frame is used as-is. + */ + math_size = round_down(math_size, 16); +#endif frame = (struct rt_sigframe __user *)((unsigned long)frame - math_size); /* Subtract 128 for a red zone and 8 for proper alignment */ @@ -417,6 +424,18 @@ int setup_signal_stack_si(unsigned long stack_top, struct ksignal *ksig, /* could use a vstub here */ return err; +#ifndef CONFIG_MMU + /* + * we need to push handler address at top of stack, as + * __kernel_vsyscall, called after this returns with ret with + * stack contents, thus push the handler here. + */ + frame = (struct rt_sigframe __user *) ((unsigned long) frame - + sizeof(unsigned long)); + err |= __put_user((unsigned long)ksig->ka.sa.sa_handler, + (unsigned long *)frame); +#endif + if (err) return err; @@ -442,9 +461,25 @@ SYSCALL_DEFINE0(rt_sigreturn) unsigned long sp = PT_REGS_SP(¤t->thread.regs); struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(sp - sizeof(long)); - struct ucontext __user *uc = &frame->uc; + struct ucontext __user *uc; sigset_t set; +#ifndef CONFIG_MMU + /** + * we enter here with: + * + * __restore_rt: + * mov $15, %rax + * call *%rax (translated from syscall) + * + * (code is from musl libc) + * so, stack needs to be popped of "call"ed address before + * looking at rt_sigframe. + */ + frame = (struct rt_sigframe __user *)((unsigned long)frame + sizeof(long)); +#endif + uc = &frame->uc; + if (copy_from_user(&set, &uc->uc_sigmask, sizeof(set))) goto segfault; From patchwork Mon Nov 11 06:27:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 2009417 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=cZvUj91O; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=anfpWmOI; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xn04N5kVnz1xyB for ; Mon, 11 Nov 2024 17:28:52 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=coSQp1/Gwn4CiglYWsw06eTGbQvY9qYl2cHooFvAJNc=; b=cZvUj91OLPCT3Enuk/fdL1nekp odXMIWSBWHKLNEfDGkOMbEiaHNvdm/pnHeGdNW9BAmK0vwqIQsYg4k+Y3vHx0eyoiLHVbeiINbZHw B1BFl349Ca2ZenyRbWHBtaWkD4YUCr3Azksyrpp5WJkhKTC9bww8+1XXWXUxLDjnqw2pTc9IyAz02 1tYfMWT7bEkMO+oA7x2MoWVcm7KsYgWdfS882Z5Bo9aZFYQS7yTwalmgWLbnOgUyKNs5Xa+Bv4XLF +hbcNxKU1RuR4r3HJxFOdvTQclrKwvFlPIk4VDGnVm+ShzqsOLN0BUbA+RFwxuOA5R2BvJIHWVunL hxWYixiQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tANut-0000000GULQ-0oWv; Mon, 11 Nov 2024 06:28:51 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tANuq-0000000GUKr-2y07 for linux-um@lists.infradead.org; Mon, 11 Nov 2024 06:28:50 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-20cbcd71012so48250145ad.3 for ; Sun, 10 Nov 2024 22:28:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731306528; x=1731911328; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=coSQp1/Gwn4CiglYWsw06eTGbQvY9qYl2cHooFvAJNc=; b=anfpWmOIjiiZVoB3nq17c0lvxZ7YIywzXBH1l/zmMKt3kD0cCQYCnoQzKDyyrKz6eL 6JPkIgc5vwVMQNJhNoTp+y0k4LTrEuNomSlvuEZdHHmuMZT+RehPg2M6EIDpGB5xdGG6 th5Rry8QGVMgMV8rBq9aAyVl4oaye4YhIzrZGSC2tfCwN2GqqBQPWtIHRWqjw21rplzy B+f58sd7rpvadnDuP41cgalijQAZcWVG6phruqVDwC3fH4gTtLHKobowmDJU3BfU84yZ AimJfjRP3beTLqQASYXjYBQ8oo00yrGV/j9xYf2Eh3v8TaFogyAtgmN/sMT4uAJXkrwF s6Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731306528; x=1731911328; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=coSQp1/Gwn4CiglYWsw06eTGbQvY9qYl2cHooFvAJNc=; b=uvVEWE+NBPhHxC7a1i2YcI7g+MwbFRRFkgJnWXVlJkdNGDQJgl778F8GsvaOdXqRWF 9dr5qGbqegz63L56OhxQU66pkJ7jrfCgpEoisEh1g7iQ/QNklnBmAPsZpwUG0lLgT8D/ h/rAkRDhnyL2W69zhER6LwFQhtpbQ7L9BfzjJ+jTf+XvT0sczs77MwdXchDvXbt4caqx kVyqbrYt5i6Qz0ltlmZvbEpUKd/svQIxd9b0U5bPDYrYUO+Ghk1WuRzCm/qA0bYSYFFD XRmE6PLg6zSeezerO6+lfprDX381yXW68NTWKWINeL5sCofOuQX/OCfFUKROhGO0b6q6 yfNw== X-Gm-Message-State: AOJu0Yxt79BDOvfN9xzjB0SGNN9zafkp2r7mSzzfaPzxgSz4B8APXL3j UwHkQgupoDdqdRT1hu6vzA8iUzk8uyF9xGpudaAI9yDuXopfGoM5 X-Google-Smtp-Source: AGHT+IEfYK6/Xq2Z6VB7yF7YwLvt5svDLDSTtZo7R0WcR9bNAjDIvANCHL6N55S+z/ihwnlKf2Mw1g== X-Received: by 2002:a17:903:244c:b0:20e:57c8:6abd with SMTP id d9443c01a7336-211835c090dmr174859265ad.52.1731306527695; Sun, 10 Nov 2024 22:28:47 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21177e59df0sm69010355ad.189.2024.11.10.22.28.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 22:28:47 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id C7E4DDBA928; Mon, 11 Nov 2024 15:28:45 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com Subject: [RFC PATCH v2 11/13] um: change machine name for uname output Date: Mon, 11 Nov 2024 15:27:11 +0900 Message-ID: <13ed8d91dee15907fc71c66f259d884db3def4cc.1731290567.git.thehajime@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241110_222848_767587_CFD188F6 X-CRM114-Status: GOOD ( 10.20 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This commit tries to display MMU/!MMU mode from the output of uname(2) so that users can distinguish which mode of UML is running right now. Signed-off-by: Hajime Tazaki --- arch/um/Makefile | 6 ++++++ arch/um/os-Linux/util.c | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:62e listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime(at)gmail.com] X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This commit tries to display MMU/!MMU mode from the output of uname(2) so that users can distinguish which mode of UML is running right now. Signed-off-by: Hajime Tazaki --- arch/um/Makefile | 6 ++++++ arch/um/os-Linux/util.c | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/arch/um/Makefile b/arch/um/Makefile index 1d36a613aad8..e0cfa3a9eae4 100644 --- a/arch/um/Makefile +++ b/arch/um/Makefile @@ -151,6 +151,12 @@ export CFLAGS_vmlinux := $(LINK-y) $(LINK_WRAPS) $(LD_FLAGS_CMDLINE) $(CC_FLAGS_ CLEAN_FILES += linux x.i gmon.out MRPROPER_FILES += $(HOST_DIR)/include/generated +ifeq ($(CONFIG_MMU),y) +UTS_MACHINE := "um" +else +UTS_MACHINE := "um\(nommu\)" +endif + archclean: @find . \( -name '*.bb' -o -name '*.bbg' -o -name '*.da' \ -o -name '*.gcov' \) -type f -print | xargs rm -f diff --git a/arch/um/os-Linux/util.c b/arch/um/os-Linux/util.c index 4193e04d7e4a..20421e9f0f77 100644 --- a/arch/um/os-Linux/util.c +++ b/arch/um/os-Linux/util.c @@ -65,7 +65,8 @@ void setup_machinename(char *machine_out) } # endif #endif - strcpy(machine_out, host.machine); + strcat(machine_out, "/"); + strcat(machine_out, host.machine); } void setup_hostinfo(char *buf, int len) From patchwork Mon Nov 11 06:27:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 2009418 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=sOT05A3s; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Sx2TQv4u; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xn04V1vS6z1xyB for ; Mon, 11 Nov 2024 17:28:58 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Tp5Cqi0yHYmB2aJoxGy0TuFARNJ6AQlSVSOfXif1f1k=; b=sOT05A3slwis4uE76fUclVbD+m dthItKF+nNet/cJsH/N2y+Qc+p1LbJZ6/rpPoaAZ9UlP8gp+PjVe0zEoeaDzCTYNn5LnOG0M56fmW xiYFGb/LrkZD0O0PZOyLIcBTHhIjDw1/hSFMGOpOkoYuWEztrstswJzzPOAyzMBOfUiRqx/NtdQ7b kOoJ3idmEfyGwBsct9Ux+ZMM0mXo2aQPvGX0dV2+H55j/NP1iL2Dm9tu72KzR7JYFzEmgrzwkpcHn uu3h0qgIYjvEfkG85Dd8rr1AuzlJXh5kj34RJ9ox8m6d9pO9qa85yKfBwz40gzncskdFBVh6Fz2DN S+pqqlgg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tANuy-0000000GUMe-2iYU; Mon, 11 Nov 2024 06:28:56 +0000 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tANuv-0000000GUM4-3c7e for linux-um@lists.infradead.org; Mon, 11 Nov 2024 06:28:55 +0000 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-71ec997ad06so3352374b3a.3 for ; Sun, 10 Nov 2024 22:28:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731306533; x=1731911333; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Tp5Cqi0yHYmB2aJoxGy0TuFARNJ6AQlSVSOfXif1f1k=; b=Sx2TQv4uYgC09twjReOOTFSt4EY41cyaBuRBDkHQEqY+STMho+HrmAmrjav0WLJT61 s08iwf3W93kz0c7i9emnn3OvjiZwBjlNvExpeFj6B1LiCrup39qEvC/v5dismy3+EYW8 hBzEUc+FXSeYlFkxozwZVs1GpnvqvXW8S4j6NVaflD0jrI4N1UCigmFj5MeqNmZ+rZ92 PHLM97HpAYgq6ux6SWgqxIZ2YVJr5buy/cgg1HIykSjTj+LtFEIPfvX9AO6n2JMW9ijT z4wDnn1AGe1mnrT9CiItzdBg0UDWjZz3N+4uspCBKgoevaVMoXoDM5ndFAOcx4qJ113U 50Lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731306533; x=1731911333; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Tp5Cqi0yHYmB2aJoxGy0TuFARNJ6AQlSVSOfXif1f1k=; b=mdanQguD9FM/vO8jvP0mLoShl2TaTDlQ2bU724Od5UpgEZp5pANM90D0+cAT9vowwM ctxv3PDcRorfHid5mrydu+mPhZ7W9IaFLKsTMPKvLB01pP4N0DFrjKWVYmPSUUFg9ZU8 N0nlzoMS6Z5RTYXyk0R9MBNRCgxdrUY4sGknEpq/vnzK6T2Muw7lz+L6nsX0h3gbKv99 s9gjQnJhRQ5txqqgIAqOvgCS4DmWo/+nRhKxLj2napp7o5jYrxuksHsXNWAEBjjKloMH RSx4Nn/FOB+iBE0G4++jMXEZSLLywk7kluodsYm2AW5zRQtMd/gdgP21dQ9tn/SdZALL 10Sg== X-Gm-Message-State: AOJu0YwDAaxrR8I60eOrzT4bBpRCQkac2ivEHsatPP1R7IsjMLLrnrwR AR1KD0fLGR3LzDpvJ4z5RfqiyBIm5ktp8eU/LthYfFkrpAD9XX9O X-Google-Smtp-Source: AGHT+IE6BJzh4ChUkLOUVblIOo7ibr4J0yOxVHpeN5BW2R8C3kfNcIKNYA8u80Gq7FvyRLvXbuc3UQ== X-Received: by 2002:a05:6a00:21cc:b0:71e:587d:f268 with SMTP id d2e1a72fcca58-724132788e0mr15820601b3a.4.1731306532800; Sun, 10 Nov 2024 22:28:52 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7f4424adcd7sm4035888a12.69.2024.11.10.22.28.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 22:28:52 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id 45EFDDBA92A; Mon, 11 Nov 2024 15:28:50 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com Subject: [RFC PATCH v2 12/13] um: nommu: add documentation of nommu UML Date: Mon, 11 Nov 2024 15:27:12 +0900 Message-ID: <23a7331dee1536925b940a7857ca891203a240ef.1731290567.git.thehajime@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241110_222853_930929_1DB6DAEF X-CRM114-Status: GOOD ( 25.51 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This commit adds an initial documentation for !MMU mode of UML. Signed-off-by: Hajime Tazaki --- Documentation/virt/uml/nommu-uml.rst | 221 +++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 Documentation/virt/uml/nommu-uml.rst Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:435 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime(at)gmail.com] X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This commit adds an initial documentation for !MMU mode of UML. Signed-off-by: Hajime Tazaki --- Documentation/virt/uml/nommu-uml.rst | 221 +++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 Documentation/virt/uml/nommu-uml.rst diff --git a/Documentation/virt/uml/nommu-uml.rst b/Documentation/virt/uml/nommu-uml.rst new file mode 100644 index 000000000000..9172918be137 --- /dev/null +++ b/Documentation/virt/uml/nommu-uml.rst @@ -0,0 +1,221 @@ +.. SPDX-License-Identifier: GPL-2.0 + +UML has been built with CONFIG_MMU since day 0. The patchset +introduces the nommu mode on UML in a different angle from what Linux +Kernel Library tried. + +.. contents:: :local: + +What is it for ? +================ + +- Alleviate syscall hook overhead implemented with ptrace(2) +- To exercises nommu code over UML (and over KUnit) +- Less dependency to host facilities + + +How it works ? +============== + +To illustrate how this feature works, the below shows how syscalls are +called under nommu/UML environment. + +- boot kernel, setup zpoline trampoline code (detailed later) at address 0x0 +- (userspace starts) +- calls vfork/execve syscalls +- during execve, more specifically during load_elf_fdpic_binary() + function, kernel translates `syscall/sysenter` instructions with `call + *%rax`, which usually point to address 0 to NR_syscalls (around + 512), where trampoline code was installed during startup. +- when syscalls are issued by userspace, it jumps to `*%rax`, slides + until `nop` instructions end, and jump to hooked function, + `__kernel_vsyscall`, which is an entrypoint for syscall under nommu + UML environment. +- call handler function in sys_call_table[] and follow how UML syscall + works. +- return to userspace + + +What are the differences from MMU-full UML ? +============================================ + +The current nommu implementation adds 3 different functions which +MMU-full UML doesn't have: + +- kernel address space can directly be accessible from userspace + - so, uaccess() always returns 1 + - generic implementation of memcpy/strcpy/futex is also used +- alternate syscall entrypoint without ptrace +- translation of syscall/sysenter instructions to a trampoline code + and syscall hooks + +With those modifications, it allows us to use unmodified userspace +binaries with nommu UML. + + +History +======= + +This feature was originally introduced by Ricardo Koller at Open +Source Summit NA 2020, then integrated with the syscall translation +functionality with the clean up to the original code. + +Building and run +================ + +``` +% make ARCH=um x86_64_nommu_defconfig +% make ARCH=um +``` + +will build UML with CONFIG_MMU=n applied. + +Kunit tests can run with the following command: + +``` +% ./tools/testing/kunit/kunit.py run --kconfig_add CONFIG_MMU=n +``` + +To run a typical Linux distribution, we need nommu-aware userspace. +We can use a stock version of Alpine Linux with nommu-built version of +busybox and musl-libc. + + +Preparing root filesystem +========================= + +nommu UML requires to use a specific standard library which is aware +of nommu kernel. We have tested custom-build musl-libc and busybox, +both of which have built-in support for nommu kernels. + +There are no available Linux distributions for nommu under x86_64 +architecture, so we need to prepare our own image for the root +filesystem. We use Alpine Linux as a base distribution and replace +busybox and musl-libc on top of that. The following are the step to +prepare the filesystem for the quick start. + +``` + container_id=$(docker create ghcr.io/thehajime/alpine:3.20.3-um-nommu) + docker start $container_id + docker wait $container_id + docker export $container_id > alpine.tar + docker rm $container_id + + mnt=$(mktemp -d) + dd if=/dev/zero of=alpine.ext4 bs=1 count=0 seek=1G + sudo chmod og+wr "alpine.ext4" + yes 2>/dev/null | mkfs.ext4 "alpine.ext4" || true + sudo mount "alpine.ext4" $mnt + sudo tar -xf alpine.tar -C $mnt + sudo umount $mnt +``` + +This will create a file image, `alpine.ext4`, which contains busybox +and musl with nommu build on the Alpine Linux root filesystem. The +file can be specified to the argument `ubd0=` to the UML command line. + +``` + ./vmlinux eth0=tuntap,tap100,0e:fd:0:0:0:1,172.17.0.1 ubd0=./alpine.ext4 rw mem=1024m loglevel=8 init=/sbin/init +``` + +We plan to upstream apk packages for busybox and musl so that we can +follow the proper procedure to set up the root filesystem. + + +Quick start with docker +======================= + +There is a docker image that you can quickly start with a simple step. + +``` + docker run -it -v /dev/shm:/dev/shm --rm ghcr.io/thehajime/alpine:3.20.3-um-nommu +``` + +This will launch a UML instance with an pre-configured root filesystem. + +Benchmark +========= + +The below shows an example of performance measurement conducted with +lmbench and (self-crafted) getpid benchmark (with v6.12-rc2 uml/next +tree). + +### lmbench (usec) + +||native|um|um-nommu| +|--|--|--|--| +|select-10 |0.5644|31.0917|0.2743| +|select-100 |2.3869|31.4651|1.1472| +|select-1000 |20.4004|36.4966|9.7533| +|syscall |0.1733|25.9904|0.1053| +|read |0.3438|27.4873|0.1451| +|write |0.2862|25.8794|0.1361| +|stat |1.9250|37.5072|0.4532| +|open/close |3.8961|65.1736|0.7665| +|fork+sh |1173.8889|5404.5000|20577.0000| +|fork+execve |535.2105|2179.2000|4716.3333| + +### do_getpid bench (nsec) + +||native|um|um-nommu| +|--|--|--|--| +|getpid | 172 | 25602 | 103| + + +Limitations +=========== + +generic nommu limitations +------------------------- +Since this port is a kernel of nommu architecture so, the +implementation inherits the characteristics of other nommu kernels +(riscv, arm, etc), described below. + +- vfork(2) should be used instead of fork(2) +- ELF loader only loads PIE (position independent executable) binaries +- processes share the address space among others +- mmap(2) offers a subset of functionalities (e.g., unsupported + MMAP_FIXED) + +Thus, we have limited options to userspace programs. We have tested +Alpine Linux with musl-libc, which has a support nommu kernel. + +access to mmap_min_addr +---------------------- +As the mechanism of syscall translations relies on an ability to +write/read memory address zero (0x0), we need to configure host kernel +with the following command: + +``` +% sh -c "echo 0 > /proc/sys/vm/mmap_min_addr" +``` + +supported architecture +---------------------- +The current implementation of nommu UML only works on x86_64 SUBARCH. +We have not tested with 32-bit environment. + +target of syscall translation +----------------------------- +The syscall translation only applies to the executable and interpreter +of ELF binary files which are processed by execve(2) syscall for the +moment: other libraries such as linked library and dlopen-ed one +aren't translated; we may be able to trigger the translation by +LD_PRELOAD. JIT compiler generated code is also generated after execve +thus, it is not currently translated. + +Note that with musl-libc in Alpine Linux which we've been tested, most +of syscalls are implemented in the interpreter file +(ld-musl-x86_64.so) and calling syscall/sysenter instructions from the +linked/loaded libraries might be rare. But it is definitely possible +so, a workaround with LD_PRELOAD is effective. + + +Further readings about NOMMU UML +================================ + +- NOMMU UML (original code by Ricardo Koller) +https://static.sched.com/hosted_files/ossna2020/ec/kollerr_linux_um_nommu.pdf + +- zpoline: syscall translation mechanism +https://www.usenix.org/conference/atc23/presentation/yasukata From patchwork Mon Nov 11 06:27:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 2009419 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=0i0FO9so; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=djZOGxgf; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xn04Z6lP1z1xyB for ; Mon, 11 Nov 2024 17:29:02 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=88f6/3StlK+B2n/V04PkoiIStLbFWa6jUrm9PgjB1nk=; b=0i0FO9so1F/KQEPoEFhOdjwugd IgxaNnBvFxJW5vvwu7EwIPKDgrDKWcGwOlURtUAV0C2lvp/9dJMPgD+qo4UFyYynocXCMQiYcreY+ N1jqDSbczLIu3gA/clpGncfZGU9jFbC8bRlNUnkzG+pZSYsQiD15pwgEW2/z8QZKICibsSZ6Y7knk eKFQ+g8pJfpjjgLQpLuufO0TSEf0cYIBe9v43EMgBi6ht1rZYRH6TBObfD5u8WILgwRdPG+Tuj43q ICIfZeFPGVmof2y9PsZLZX4jFz+HPuFZTv2idFL513+9pEzS+rgH3jog50dWd69tDSCMR6ARZYcwa gdUMvadQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tANv3-0000000GUNk-1S1i; Mon, 11 Nov 2024 06:29:01 +0000 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tANv0-0000000GUN4-27bU for linux-um@lists.infradead.org; Mon, 11 Nov 2024 06:28:59 +0000 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-720e94d36c8so4425407b3a.1 for ; Sun, 10 Nov 2024 22:28:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731306537; x=1731911337; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=88f6/3StlK+B2n/V04PkoiIStLbFWa6jUrm9PgjB1nk=; b=djZOGxgfFIlcfcHOmR0ygtFcTBWx6hIhGfs0OHxVd7iYDbgU8qeJFZ65/s+ldI8cip FskXKLAhmnXIfBiiU4aG0aoorXesjJacELKfJ2iY5UKxh+BkS7ZtxJns4P151lT+9luP e8auI8G/R0chtQeTnUfx4ry38vIsyY6m37opvubYFDarFHG29HHaqt8ePMpmPeL9+TpS Fk1xvrA5VMVWrrUz9AuoNDvnfVjrNrZXTC5AOPerQeGypuRZeyGlVv/3gwfICrd/Bohk dE61kv7BaYTajMzUrJSIqxmP5Jih7KsQFl++HUlX0zrz7Q8bgS9c3Se3imuOjRNMGEcv y2Wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731306537; x=1731911337; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=88f6/3StlK+B2n/V04PkoiIStLbFWa6jUrm9PgjB1nk=; b=On5FMOAV32tdfYAj8b/e8zrk687CIQuCIKs31zjE8bLDCgztbY6G8bGv5e2nvsiyF8 H2EXOSBEqohxDBfuJnUXrEDjOvfrTpc12HlmKC+GWelGQv4NTvMS2STfhRVMvGIO5O29 vsmwOweReuvQybI7Sj62grwnlBLKXDjN3kOBmrXfF1n+dLHzB3pM1mnFjabJJXYL7Koj qCBNsrK70lk27+PcZ92HbqeNglQQzAzN4BoAD+oVWxEp/afefu/eKPlYQpr5c6r2oimd qanVOt1AN3HuDUhkbY9WSvj4Qlays8c+ziyThlalH3g6BlDRQG4W5WLa8bAfGOKC5ErI j86w== X-Gm-Message-State: AOJu0YyG0c1W0mU6NdF6Vw2KK6w4AcL1ok+SsucvOfd+ug98ao4jHju+ wvIM6R8Lma5cUf5FK7IH2a13xewsTWkv3d9KzOqM08OKQaLFlH9V X-Google-Smtp-Source: AGHT+IEOfVoxFaJ4zhHD+TEqr4EbtC2/3lVnsJg2jPWEIWsqpj/mbQt/NxnYEXXBtk7Q2qf26GLT+A== X-Received: by 2002:a05:6a00:1701:b0:71e:6a57:7288 with SMTP id d2e1a72fcca58-72413f80258mr18286267b3a.5.1731306537319; Sun, 10 Nov 2024 22:28:57 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72407a4f566sm8251211b3a.171.2024.11.10.22.28.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 22:28:56 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id CFD8CDBA92C; Mon, 11 Nov 2024 15:28:54 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com Subject: [RFC PATCH v2 13/13] um: nommu: plug nommu code into build system Date: Mon, 11 Nov 2024 15:27:13 +0900 Message-ID: <460c900f1bafb3fc239c6f874f9e971d727ba736.1731290567.git.thehajime@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241110_222858_570246_5C39C29B X-CRM114-Status: GOOD ( 13.68 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Add nommu kernel for um build. defconfig is also provided. Signed-off-by: Hajime Tazaki Signed-off-by: Ricardo Koller --- arch/um/Kconfig | 14 +++++- arch/um/configs/x86_64_nommu_defconfig | 64 ++++++++++++++++++++++++++ arch/x86/um/Makefile | 18 ++++++++ [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:42b listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime(at)gmail.com] X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Add nommu kernel for um build. defconfig is also provided. Signed-off-by: Hajime Tazaki Signed-off-by: Ricardo Koller --- arch/um/Kconfig | 14 +++++- arch/um/configs/x86_64_nommu_defconfig | 64 ++++++++++++++++++++++++++ arch/x86/um/Makefile | 18 ++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 arch/um/configs/x86_64_nommu_defconfig diff --git a/arch/um/Kconfig b/arch/um/Kconfig index a9876bdb5bf9..81897c496711 100644 --- a/arch/um/Kconfig +++ b/arch/um/Kconfig @@ -30,13 +30,16 @@ config UML select ARCH_SUPPORTS_LTO_CLANG_THIN select TRACE_IRQFLAGS_SUPPORT select TTY # Needed for line.c - select HAVE_ARCH_VMAP_STACK + select HAVE_ARCH_VMAP_STACK if MMU select HAVE_RUST select ARCH_HAS_UBSAN select HAVE_ARCH_TRACEHOOK + select UACCESS_MEMCPY if !MMU + select GENERIC_STRNLEN_USER if !MMU + select GENERIC_STRNCPY_FROM_USER if !MMU config MMU - bool + bool "MMU-based Paged Memory Management Support" if 64BIT default y config UML_DMA_EMULATION @@ -189,8 +192,15 @@ config MAGIC_SYSRQ The keys are documented in . Don't say Y unless you really know what this hack does. +config ARCH_FORCE_MAX_ORDER + int "Order of maximal physically contiguous allocations" if EXPERT + default "10" if MMU + default "16" if !MMU + config KERNEL_STACK_ORDER int "Kernel stack size order" + default 3 if !MMU + range 3 10 if !MMU default 2 if 64BIT range 2 10 if 64BIT default 1 if !64BIT diff --git a/arch/um/configs/x86_64_nommu_defconfig b/arch/um/configs/x86_64_nommu_defconfig new file mode 100644 index 000000000000..c2e0fb546987 --- /dev/null +++ b/arch/um/configs/x86_64_nommu_defconfig @@ -0,0 +1,64 @@ +CONFIG_SYSVIPC=y +CONFIG_POSIX_MQUEUE=y +CONFIG_NO_HZ=y +CONFIG_HIGH_RES_TIMERS=y +CONFIG_BSD_PROCESS_ACCT=y +CONFIG_IKCONFIG=y +CONFIG_IKCONFIG_PROC=y +CONFIG_LOG_BUF_SHIFT=14 +CONFIG_CGROUPS=y +CONFIG_BLK_CGROUP=y +CONFIG_CGROUP_SCHED=y +CONFIG_CGROUP_DEVICE=y +CONFIG_CGROUP_CPUACCT=y +# CONFIG_PID_NS is not set +CONFIG_CC_OPTIMIZE_FOR_SIZE=y +# CONFIG_MMU is not set +CONFIG_HOSTFS=y +CONFIG_MAGIC_SYSRQ=y +CONFIG_SSL=y +CONFIG_NULL_CHAN=y +CONFIG_PORT_CHAN=y +CONFIG_PTY_CHAN=y +CONFIG_TTY_CHAN=y +CONFIG_CON_CHAN="pts" +CONFIG_SSL_CHAN="pts" +CONFIG_UML_SOUND=m +CONFIG_UML_NET=y +CONFIG_UML_NET_ETHERTAP=y +CONFIG_UML_NET_TUNTAP=y +CONFIG_UML_NET_SLIP=y +CONFIG_UML_NET_DAEMON=y +CONFIG_UML_NET_MCAST=y +CONFIG_UML_NET_SLIRP=y +CONFIG_MODULES=y +CONFIG_MODULE_UNLOAD=y +CONFIG_IOSCHED_BFQ=m +CONFIG_BINFMT_MISC=m +CONFIG_NET=y +CONFIG_PACKET=y +CONFIG_UNIX=y +CONFIG_INET=y +CONFIG_DEVTMPFS=y +CONFIG_DEVTMPFS_MOUNT=y +CONFIG_BLK_DEV_UBD=y +CONFIG_BLK_DEV_LOOP=m +CONFIG_BLK_DEV_NBD=m +CONFIG_DUMMY=m +CONFIG_TUN=m +CONFIG_PPP=m +CONFIG_SLIP=m +CONFIG_LEGACY_PTY_COUNT=32 +CONFIG_UML_RANDOM=y +CONFIG_SOUND=m +CONFIG_EXT4_FS=y +CONFIG_REISERFS_FS=y +CONFIG_QUOTA=y +CONFIG_AUTOFS_FS=m +CONFIG_ISO9660_FS=m +CONFIG_JOLIET=y +CONFIG_NLS=y +CONFIG_DEBUG_KERNEL=y +CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y +CONFIG_FRAME_WARN=1024 +CONFIG_IPV6=y diff --git a/arch/x86/um/Makefile b/arch/x86/um/Makefile index b42c31cd2390..0513c4ad0130 100644 --- a/arch/x86/um/Makefile +++ b/arch/x86/um/Makefile @@ -32,6 +32,24 @@ obj-y += syscalls_64.o vdso/ subarch-y = ../lib/csum-partial_64.o ../lib/memcpy_64.o \ ../lib/memmove_64.o ../lib/memset_64.o + +# used by zpoline.c to translate syscall/sysenter instructions +# note: only in x86_64 w/ !CONFIG_MMU +ifneq ($(CONFIG_MMU),y) +inat_tables_script = $(srctree)/arch/x86/tools/gen-insn-attr-x86.awk +inat_tables_maps = $(srctree)/arch/x86/lib/x86-opcode-map.txt +quiet_cmd_inat_tables = GEN $@ + cmd_inat_tables = $(AWK) -f $(inat_tables_script) $(inat_tables_maps) > $@ +$(obj)/inat-tables.c: $(inat_tables_script) $(inat_tables_maps) + $(call cmd,inat_tables) +targets += inat-tables.c +$(obj)/../lib/inat.o: $(obj)/inat-tables.c +subarch-y += ../lib/insn.o ../lib/inat.o + + +obj-y += do_syscall_$(BITS).o entry_$(BITS).o zpoline.o +endif + endif subarch-$(CONFIG_MODULES) += ../kernel/module.o