From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0BC3E1DED42 for ; Sat, 27 Dec 2025 21:29:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766870944; cv=none; b=fg/ii02AspCZ3RqdHeX77Trptsefia4ZV4MZaSLK64kryOZux8y6g1oS1Ora49Nl1ZrmibVzSmfy3czkPsSbsr8ZvkBOlbi2GY/WWtQKY73cRAZkGdK125wDrrp8WYqG/oEBo/Wfk9N4m+CtdEHoyxi053tZK7XzouX4+tPolw0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766870944; c=relaxed/simple; bh=ZgPetopRj0vXEil5WBO8VRXOx673wGcUwy1VGZGiAVU=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Q/UBWxlwjbLYYdcT7/eSd6oxteSUwzw6meLrL1fg+9bPaHr7RX22NX/W6EOA/DSgLeQf0Mv2h7che4rpNCbAkTtG4YESsFzyUy1f77aPgPagneJ23MHxiu9gwlbD/cj2r8mtWD1ZTSs/vHj0eB+Buc8jrgnAyxpQy+Zu9nAQcuI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=c6DFem7R; arc=none smtp.client-ip=209.85.221.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="c6DFem7R" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-430f9ffd4e8so4189393f8f.0 for ; Sat, 27 Dec 2025 13:29:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766870941; x=1767475741; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=pHxGUXHdDKjBEJt6gdAHmJtT/TC6JT7WBnoFRnVfOAg=; b=c6DFem7Rl+0LB7cEahLAbcXKqOYKls/duryxiciUbgPXip2qALJb2KbS7FzNEI7sob AyllJxhmMKdcRkNISi+/MIhytoW0tfrCFZkew+zW0bbeDiLehYWz3BDPLRMmic7ACaHt 6Tn5RihkMdiP9/pz/qn6GlhPKdpMjFta0Z2+8g5UeoI/L1GjDVCKrCt6HqPEHSehyVyo IU415Z/fNH9N1FdP+zPEO9T5Qu9dU0oWTRnDdwjSV57K44l2Cw8wRnX/FiGQsKwXvOhi q4ZaCJVN5Bq583DXqBkPCqDZ6o9NTjEqeePlLMrGeJ6enkF0DHWD3r/RBUp50monC3Zz 9U5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766870941; x=1767475741; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=pHxGUXHdDKjBEJt6gdAHmJtT/TC6JT7WBnoFRnVfOAg=; b=ljsd/T5QcUHfmgsF+qx0VgqzTymmRi5tElrokUDWCVj8VYGzvMiiUijHfFvT50gTcv d/DLqPTQW1oh5YkocaQ0sxJw/rBUXPYb/UXaLxxXHpdgjaXKL10KCFr2TADDLJa2jZJ8 FlAP7lv2cFnG7TG4+xcVc1gSxMzfprUuyghYkCKIvNvsBlnevuO1WCU40HdHJNddCQ4X VRNhkUpTU5s98z7ucVkvWfw5n9JdXRLVrsyss6NFA9XrxyJfxupTIiLkMfzusgXfoPWl pktG8v8klA3FhR7A/9v8CXE8Z/wNFLS1/I2NR1oNrXsh1XW9iGPIBfiwVn37nZ/WMP3c FOXQ== X-Forwarded-Encrypted: i=1; AJvYcCVJhVBYLl3SHrVMiF0Hc79nyixZhR/+kj6dUPpjcVsSHgZRsOqPdlEFZPUBbZ5lYubok2UQDtECmGDKdxU=@vger.kernel.org X-Gm-Message-State: AOJu0Ywfpr7kvGIgAMsgehqpSnz2Aied83ZOCGp0SuZX2kXZvHUzm1bc 7vbgFGawGqTvEMGuaWlvQe1q6U8uz7wBw+u8Oq8qu3KVgLamvxsJGhAU X-Gm-Gg: AY/fxX6fvn91gUgvGnpiB/WV127p8SjtxtmIDzIAGAC2yspUfPUDbzCu8wAgdIFGna0 QFHaD017SqFFDZeiJJ8rXcM1jUyK2FM3WFLYcHvCPKibfxqfFHoUFuJTvNbGreS5OjqubTziShU cg+tYmvjjTeSnmXZlbzms4m+L+pZ4Sn5mu55Cd2zPJOfQhtDCKzd5PaalcF8tfmpfjtSSUsNy+T CW53pjC078/d6iDMM/qy7GIpcOtAqXig5/xxj6bEMiv9mIs+pbk9luDBwgV7cYPbPUazRRHS7jZ FpkEDH5ZBxSUfSMnX5mEhw8HqyYPT7REfD3rM3iW83P+kdRBsWW33jsS/FSWa7MCItNXBV9xt0A clVwjUbBBIoaOeKkI4g7/UydL+qe1rFcXQYw2jlei+qUevTh9WdR9M4YlBTpgOXCnqEW92anpT5 0HcFhP5CeeqczGzGMPomEITK1dE6RU46yf8hn2POHcborml0NzdSJ/KLYa8Mntdg== X-Google-Smtp-Source: AGHT+IEp3AixBs6DuHhs/EFr8jwXwRGuKnRwB1g0/0NqpwjTisNWrX9O8srAllOgZCHhiOvgQWCqAg== X-Received: by 2002:a05:6000:2586:b0:430:fdc8:8bd6 with SMTP id ffacd0b85a97d-4324e437897mr33047121f8f.31.1766870941136; Sat, 27 Dec 2025 13:29:01 -0800 (PST) Received: from pumpkin (host-2-103-239-165.as13285.net. [2.103.239.165]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4324ea227casm53178291f8f.15.2025.12.27.13.29.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Dec 2025 13:29:00 -0800 (PST) Date: Sat, 27 Dec 2025 21:28:59 +0000 From: David Laight To: Deepak Gupta Cc: Lukas Gerlach , linux-riscv@lists.infradead.org, palmer@dabbelt.com, pjw@kernel.org, aou@eecs.berkeley.edu, alex@ghiti.fr, linux-kernel@vger.kernel.org, daniel.weber@cispa.de, michael.schwarz@cispa.de, marton.bognar@kuleuven.be, jo.vanbulck@kuleuven.be Subject: Re: [PATCH 1/2] riscv: Use pointer masking to limit uaccess speculation Message-ID: <20251227212859.3a83d65e@pumpkin> In-Reply-To: References: <20251218191332.35849-1-lukas.gerlach@cispa.de> <20251218191332.35849-2-lukas.gerlach@cispa.de> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 19 Dec 2025 16:44:11 -0800 Deepak Gupta wrote: > On Thu, Dec 18, 2025 at 08:13:31PM +0100, Lukas Gerlach wrote: > >Similarly to x86 and arm64, mitigate speculation past an access_ok() > >check by masking the pointer before use. > > > >On RISC-V, user addresses have the MSB clear while kernel addresses > >have the MSB set. The uaccess_mask_ptr() function clears the MSB, > >ensuring any kernel pointer becomes invalid and will fault, while > >valid user pointers remain unchanged. This prevents speculative > >access to kernel memory via user copy functions. > > > >The masking is applied to __get_user, __put_user, raw_copy_from_user, > >raw_copy_to_user, clear_user, and the unsafe_* variants. > > > >Signed-off-by: Lukas Gerlach > >--- > > arch/riscv/include/asm/uaccess.h | 41 +++++++++++++++++++++++++------- > > 1 file changed, 32 insertions(+), 9 deletions(-) > > > >diff --git a/arch/riscv/include/asm/uaccess.h b/arch/riscv/include/asm/uaccess.h > >index 36bba6720c26..ceee1d62ff9b 100644 > >--- a/arch/riscv/include/asm/uaccess.h > >+++ b/arch/riscv/include/asm/uaccess.h > >@@ -74,6 +74,23 @@ static inline unsigned long __untagged_addr_remote(struct mm_struct *mm, unsigne > > #define __typefits(x, type, not) \ > > __builtin_choose_expr(sizeof(x) <= sizeof(type), (unsigned type)0, not) > > > >+/* > >+ * Sanitize a uaccess pointer such that it cannot reach any kernel address. > >+ * > >+ * On RISC-V, virtual addresses are sign-extended from the top implemented bit. > >+ * User addresses have the MSB clear; kernel addresses have the MSB set. > >+ * Clearing the MSB ensures any kernel pointer becomes non-canonical and will > >+ * fault, while valid user pointers remain unchanged. > >+ */ > >+#define uaccess_mask_ptr(ptr) ((__typeof__(ptr))__uaccess_mask_ptr(ptr)) > >+static inline void __user *__uaccess_mask_ptr(const void __user *ptr) > >+{ > >+ unsigned long val = (unsigned long)ptr; > >+ > >+ val = (val << 1) >> 1; > >+ return (void __user *)val; > > This is only clearing b63 which is what we don't need here. It is also entirely the wrong operation. A kernel address needs converting into an address that is guaranteed to fault - not a user address that might be valid. You need a guard page of address space between valid user addresses and valid kernel addresses that is guaranteed to not be used. Typically this is the top page of the user address space. All kernel addresses need converting to the base of the guard page using ALU operations (to avoid speculative accesses). So: ptr = min(ptr, guard_page); easiest done (as in x86-64) with a compare and conditional move. The ppc version is more complex because there isn't a usable conditional move instruction. I think this would work for x86-32: offset = ptr + -guard_page; mask = sbb(x,x); // ~0u if the add set the carry flag. ptr -= offset & mask; You need to find something that works for riscv. > > You should be clearing b47 (if bit indexing starts at 0) on Sv48 and b56 > on Sv57 system. > > Anything above b47/b56 isn't going to be used anyways in indexing into > page tables and will be ignored if pointer masking is enabled at S. Gah more broken hardware... Ignoring the high address bits doesn't work and is really a bad idea. Arm did it as well. Trying to do 'user pointer masking' for uaccess validation is a PITA if you have to allow for non-zero values in the high bits it makes life even more complicated. And 'pointer masking' is so broken unless it is done at the page level and enforced by the hardware. What it does is make random/invalid addresses much more likely to be valid. An interpreter can use them internally, but they are so slow software masking (following the validation) shouldn't be a real issue. David