Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp2618181lqz; Wed, 3 Apr 2024 03:46:03 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVfJlvykZFCyMrM9Y5mLNueYsQjLX9pk94eG3Pq9xYupr+e6hStMYvmnW2tESuoxgSqt6pnEoynCb37Gm7yn/+lDT8ScVJ+LNu6M8WSig== X-Google-Smtp-Source: AGHT+IFuMS/zC/G0XueCmrWX0clHHwOc7pNdPdcAbG6ZCKcGWzufpwtL8u8waeJMzJmMeDOeUuaa X-Received: by 2002:a05:6871:26c2:b0:22a:ae35:4bd2 with SMTP id zb2-20020a05687126c200b0022aae354bd2mr12250147oab.21.1712141162821; Wed, 03 Apr 2024 03:46:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712141162; cv=pass; d=google.com; s=arc-20160816; b=irKMlkd8PuLns1YXtAasHyShm6eLhvuAV5xDJuCHlBis9NbBmaSVNFMRYdJw4RH00s JlRTnfI//jqQfkkwh7QfrdbcYvyPV6njMEDhkQ4HZAgIPN3Eh+w8WdXcg+Q1vIDZhcdW 9uLE5C/qZnsXs4Q1WYB3LNkHB24jhUNz8pdfnO5GOGAz/DAhXojB9dgUbUad8iRpphcR JBAndlWQ0PJpZF6aYkls5nrX8bwKgvDwOwfEFZ2vHO0OUauKM2il8/wPaxloxaS3IeoQ 44vv/AlYnaBR0UJeny1TUlSOe1L75BJIObE8el7C/K8mcAIkyyO3KPiY9ARangBYMPNS t/wg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=DM3MruV8xgndrXUKi0XabWvgomlzzo6Hs77lj1LmAyw=; fh=ycB1t+V4/9GXCDjQuPGTcrcT8oy3WDo2mWMRnoW4pOc=; b=eJa7OdbzSYHkGgtKo+GWEp/55b4FjgTGpN27RLqCcQnviRKI3wi4au3JCZGGmF/ZZM a+SuM7M2CHxSxDcoLZsUR7FhdYZ4LvvQM7uKk1zwOA5OGIvKiekuczTYm+OJKB3yi6/W EQaodTLlzWPEMV57hNMja3W5Y+UtD4u5A7Nc+cm/MYa35dbJna16vMnH6UPhD9PL01xm npaKNpxn9DbyJBVdUTE9/DQiS6yzes/IkxDBNweSI0hUkGooE9EaDZk4ZQZxmKcRoIYs 5dymbueKq9c6FHdFbGG0QwR72ubnyE3ahTz/8Atd5lbGDdox8LrLczv8GldGz4afJmbf 7T/g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=fuWJz5xB; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-129515-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-129515-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id q7-20020a056a00084700b006eb43bc0fbesi6049029pfk.51.2024.04.03.03.46.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 03:46:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-129515-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=fuWJz5xB; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-129515-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-129515-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 8E4FEB298B1 for ; Wed, 3 Apr 2024 10:14:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 19C3113A89F; Wed, 3 Apr 2024 10:12:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="fuWJz5xB" Received: from mail-yb1-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) (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 5F49B13AA38 for ; Wed, 3 Apr 2024 10:12:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712139148; cv=none; b=Wtf5iBWSFNur8xDRak8wfrzn5uBJoZfL1uoSckrcsz2IXyJUQ+bWsl7TMHeASgIU5+4DLZBRnM1TmbQOJ+ePMdLwygq2W97SmvR2wKGVLLJ6NKRe4+KtMx98KLyZC/Bpmio8MQ8ux/1KZUr35YUq25Vvpv7cbfFyUIjq/SxRBK4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712139148; c=relaxed/simple; bh=dBoosYSLCKiN/Z4tyIZ+kREWT4a37+GEzlZscyrSPgk=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=itRHQz3jk98PScGsPCI9io/8e6XT2a3l1DfEpfwMEaRt7u8nVNNhW+OCLTNuz+7G/TB28JgfKSOyFArlxUeXobz2m2UdU2anXGHi1s4ENUsnieawQ4TKrFrDheLbQpP4aZaHeESiDCi43oEOQnzo0rzbNj1/vB8J4XSCV+AgHmE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com; spf=pass smtp.mailfrom=sifive.com; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b=fuWJz5xB; arc=none smtp.client-ip=209.85.219.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-yb1-f180.google.com with SMTP id 3f1490d57ef6-ddaebc9d6c9so5806560276.0 for ; Wed, 03 Apr 2024 03:12:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1712139145; x=1712743945; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=DM3MruV8xgndrXUKi0XabWvgomlzzo6Hs77lj1LmAyw=; b=fuWJz5xB0p863eutso/dFfDCAkzwbeB9T7ULudM1DTnf1BOgxXO67/yi3OohxhG3lm n4QBhfc6w2M9dHCsy+w+hA2angVuU77O4b6SxwI3VXe1qJjDzjJ1HU38tgzjS+N3JhaN 0JwViKYjyu5R2jCgUOAV5KxLdov1ZvtFzb2HBDkiz3YGboK/b85q5UFp9z+CrHWARQ6M F5gunApzeXa6VZr/OL5GK5dN5hWizn9ujJRVO6/q950EjDZVbbtKGHVhjjIp8lbPI/0b Myw6Uodx/fuSeUmWhWo5mohF4QVYAR2bRis5dy/Z14tia32sbhjsOVQTAVmBg+CyXIb9 U4ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712139145; x=1712743945; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DM3MruV8xgndrXUKi0XabWvgomlzzo6Hs77lj1LmAyw=; b=LZY/QpiVfwh0DFkFYGTbBptkpNtVuIf8sUoLYKkDRDKPtLzZpEp5PtepxPKE0wPaH2 WLhTclsp6FLN+B7oVJBoDA6YHAEaHCJ6zg72+e1ajJN6xmF1X3t2W02B6Vu7KD/ZQEdY rXV8lh/fo0Efzj/oj6989S+qE9e5jI0I0Mgk5cnVx0M22mvYRx8e0z3TjaX4YUvoteiB Nk4rjCKT0z6Zsljdn/+MrtAkamBOnaRjtbOrx1g5MHuuKnOfd5dex8tOIrYtnpuJeiFz t8PllM0W8k0SzjXdzAeMRKXD78Tm+lNaEhTkQhdcWJRMew3BWv6hUvUao6ULMyvjcmPw 44Ew== X-Forwarded-Encrypted: i=1; AJvYcCXO71S8pQDZJ5wdJAoiQzyX/cp7/h/3wH/iCdx792L3PBXiN5YP8NIfOXFyhWgd7nqDbCTo/IWaUWN5EU/Hk0/tox/9PcJs3TY0pnhn X-Gm-Message-State: AOJu0YwS7b5jl4m/TXxMpmas7zYhzlZ0r9XJxyarKdKwN0ezoP5KwooF EX6xcXxVGINIHAGzQsg0KAFrahCWSybsB+oxPvxLZvJJkBt3BIkwofP2GINV8HQ7qi1k42CaBxp NJVNetUt9vdDPuaGaLYmg4zhA7Yb2w4zzMB3/Tw== X-Received: by 2002:a25:870e:0:b0:dd1:3909:bdd with SMTP id a14-20020a25870e000000b00dd139090bddmr11067625ybl.65.1712139145217; Wed, 03 Apr 2024 03:12:25 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240403072638.567446-1-bjorn@kernel.org> In-Reply-To: <20240403072638.567446-1-bjorn@kernel.org> From: Andy Chiu Date: Wed, 3 Apr 2024 18:12:14 +0800 Message-ID: Subject: Re: [PATCH] riscv: Fix vector state restore in rt_sigreturn() To: =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org, =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Conor Dooley , Heiko Stuebner , Vincent Chen , Ben Dooks , Greentime Hu , Haorong Lu , Jerry Shih , Nick Knight , linux-kernel@vger.kernel.org, Vineet Gupta , Charlie Jenkins , Vineet Gupta Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Apr 3, 2024 at 3:27=E2=80=AFPM Bj=C3=B6rn T=C3=B6pel wrote: > > From: Bj=C3=B6rn T=C3=B6pel > > The RISC-V Vector specification states in "Appendix D: Calling > Convention for Vector State" [1] that "Executing a system call causes > all caller-saved vector registers (v0-v31, vl, vtype) and vstart to > become unspecified.". In the RISC-V kernel this is called "discarding > the vstate". > > Returning from a signal handler via the rt_sigreturn() syscall, vector > discard is also performed. However, this is not an issue since the > vector state should be restored from the sigcontext, and therefore not > care about the vector discard. > > The "live state" is the actual vector register in the running context, > and the "vstate" is the vector state of the task. A dirty live state, > means that the vstate and live state are not in synch. > > When vectorized user_from_copy() was introduced, an bug sneaked in at > the restoration code, related to the discard of the live state. > > An example when this go wrong: > > 1. A userland application is executing vector code > 2. The application receives a signal, and the signal handler is > entered. > 3. The application returns from the signal handler, using the > rt_sigreturn() syscall. > 4. The live vector state is discarded upon entering the > rt_sigreturn(), and the live state is marked as "dirty", indicating > that the live state need to be synchronized with the current > vstate. > 5. rt_sigreturn() restores the vstate, except the Vector registers, > from the sigcontext > 6. rt_sigreturn() restores the Vector registers, from the sigcontext, > and now the vectorized user_from_copy() is used. The dirty live > state from the discard is saved to the vstate, making the vstate > corrupt. > 7. rt_sigreturn() returns to the application, which crashes due to > corrupted vstate. > > Note that the vectorized user_from_copy() is invoked depending on the > value of CONFIG_RISCV_ISA_V_UCOPY_THRESHOLD. Default is 768, which > means that vlen has to be larger than 128b for this bug to trigger. > > The fix is simply to mark the live state as non-dirty/clean prior > performing the vstate restore. > > Link: https://github.com/riscv/riscv-isa-manual/releases/download/riscv-i= sa-release-8abdb41-2024-03-26/unpriv-isa-asciidoc.pdf # [1] > Reported-by: Charlie Jenkins > Reported-by: Vineet Gupta > Fixes: c2a658d41924 ("riscv: lib: vectorize copy_to_user/copy_from_user") > Signed-off-by: Bj=C3=B6rn T=C3=B6pel Thanks for the findings! Reviewed-by: Andy Chiu > --- > arch/riscv/kernel/signal.c | 15 ++++++++------- > 1 file changed, 8 insertions(+), 7 deletions(-) > > diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c > index 501e66debf69..5a2edd7f027e 100644 > --- a/arch/riscv/kernel/signal.c > +++ b/arch/riscv/kernel/signal.c > @@ -119,6 +119,13 @@ static long __restore_v_state(struct pt_regs *regs, = void __user *sc_vec) > struct __sc_riscv_v_state __user *state =3D sc_vec; > void __user *datap; > > + /* > + * Mark the vstate as clean prior performing the actual copy, > + * to avoid getting the vstate incorrectly clobbered by the > + * discarded vector state. > + */ > + riscv_v_vstate_set_restore(current, regs); > + > /* Copy everything of __sc_riscv_v_state except datap. */ > err =3D __copy_from_user(¤t->thread.vstate, &state->v_state= , > offsetof(struct __riscv_v_ext_state, datap= )); > @@ -133,13 +140,7 @@ static long __restore_v_state(struct pt_regs *regs, = void __user *sc_vec) > * Copy the whole vector content from user space datap. Use > * copy_from_user to prevent information leak. > */ > - err =3D copy_from_user(current->thread.vstate.datap, datap, riscv= _v_vsize); > - if (unlikely(err)) > - return err; > - > - riscv_v_vstate_set_restore(current, regs); > - > - return err; > + return copy_from_user(current->thread.vstate.datap, datap, riscv_= v_vsize); > } > #else > #define save_v_state(task, regs) (0) > > base-commit: 7115ff4a8bfed3b9294bad2e111744e6abeadf1a > -- > 2.40.1 >