Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp1897432pxv; Fri, 2 Jul 2021 15:31:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwlWMt4PT0HGBxj1suhVo6EfTubgR3edTytP9y2BLsuHmkyYhEEO7vdxBiaDzoRC5qPcSjR X-Received: by 2002:a50:8e16:: with SMTP id 22mr1973372edw.369.1625265066349; Fri, 02 Jul 2021 15:31:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625265066; cv=none; d=google.com; s=arc-20160816; b=X8+4Y3ORssNEX1w5vzwQGti1CK97qK0RWcL/5+AbrDmxjkhb7wqlM4bYnqNpr01Cwy ezvVIjDG5WSRGkUofe1Um3Gfxsm8H77fd5pFy2N89zE8DTflwN4bnirEvEqEFyMBPp0C y3mUIOk2r0s+ckNuAZoqjwFf8TTCbRw4cTR/RsHY2J0hMUrdzmxHQ1ix9p+ce7eE3vJA m59/PLa4bIZBXwmWYXrueA9k/sSDjsEuU5Ia2NbRZ5RrRpUG6yY3cjmleM1g3SGxM7S0 qYEN5yZdQMIBRkYerCL/DAp+DdbYE1ZbImTph7iokF7og1rNqk7Y6IE7Xj+2THyo0LPV pHMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=TM6cjgF2amzNsWvXOB7KJYtX5jxhorxbWHwc1I9Rrtw=; b=YT6ugu3INukkr1wyfAaWwWJQzRiv0Hmjp/RuqqObpQxpHaktu3h5baTalreCXFYXBk U42aVQ4JVlfTSCiiEeaUbKy0aKaT12/vwHgx72dlMEULn1zttp8vcUsQmgEJkn6FYrK1 B0jfqZ4CjmVDiXGbgaK9aXjoSFwnvc8JNmYP8dsEq6HwbWjLE5H2xlgM/A/dUq+DzkIB BnVmR+MvdPxCfPbaH2wYd0ZhtC6pTCW3gccjHQoBQU4DI2seWJqShM+9Eg7V2Auc7q8r 3H8icEfTBUf1FTj1WfhvNCNUdUfMDEK+ZY0RZoUi4Rs+2dRY7RhLyFvmGRW6jDgOPCug XNhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=SXpkEDNH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x16si5027780edd.240.2021.07.02.15.30.43; Fri, 02 Jul 2021 15:31:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=SXpkEDNH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230035AbhGBW1a (ORCPT + 99 others); Fri, 2 Jul 2021 18:27:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229917AbhGBW1a (ORCPT ); Fri, 2 Jul 2021 18:27:30 -0400 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B8F9C061765; Fri, 2 Jul 2021 15:24:56 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id a127so10362279pfa.10; Fri, 02 Jul 2021 15:24:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=TM6cjgF2amzNsWvXOB7KJYtX5jxhorxbWHwc1I9Rrtw=; b=SXpkEDNH/iGEKPGIGJQ+P9nX3dWAhgZii4Bd3EV5wTQhaQPy619uoJUzR6cjZbC18W DaWUT90O5ug/ThjjYGZKrEQ5FbNLyJsR99Llg4VFauOUfA8FctMxLzDMaeiFX4k+DDym Z7oH2IT72uD/htMxeHNaKWlYWzSQdjPg0za7exx5Qe55/paMiOhm3cHD0oNHcSmM8kIk B5HQVntDF5bVhJ/QwwDOQdthYHEJdbmqTbWIzwG5e6pX3/ClZz7sNwujTuCOAt5o6mSu 3hwonR3hx7FE8Dz6w8K4fyghbz1zIkA+Bh/zoEI3JcIe7pFRESe6MGZq5gfOfVAiUV4t LBzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=TM6cjgF2amzNsWvXOB7KJYtX5jxhorxbWHwc1I9Rrtw=; b=aXECF90TfqgQEhCHOo4fBSBsuogaVNHYJsbmjgfl+B0eYGbEzzLtbh55qCuSgo9oJ9 1tiHAZZsonpCKqYg7sxJmwDoBHEfTAdhND/eaQ+J80CuNwZzI0hHg4RW4weJm6+HevWB Ok+fKGZmoXbt5/fOqzubJSVwGWfyzktYdtczsAgFNUXvMH8tngqax/zY2hFum6Wl+Pak h3G9twp8VZSkaDW3NcVOb9kMJ4lDazm4YUnB4Hlp5SUvzp1fFsoqOfw4oUA8M/uIUiYv jzMh+7r2xScpcwXshbXihW5zNBJGWPgjZEqFh3jOclwavPEAFwMR6x9p7Rd995tXU1is tICg== X-Gm-Message-State: AOAM533VpQE41DIXsG9s054/wILKwVBF6+cl2a6s2GB2R9rtwGLdpfih PCdZPQ7ERv39SrhgWh+LBzE= X-Received: by 2002:aa7:9727:0:b029:304:3644:2771 with SMTP id k7-20020aa797270000b029030436442771mr1682109pfg.73.1625264695987; Fri, 02 Jul 2021 15:24:55 -0700 (PDT) Received: from gmail.com ([2601:600:8500:5f14:d627:c51e:516e:a105]) by smtp.gmail.com with ESMTPSA id y4sm5083312pfc.15.2021.07.02.15.24.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jul 2021 15:24:55 -0700 (PDT) Date: Fri, 2 Jul 2021 15:21:22 -0700 From: Andrei Vagin To: Peter Zijlstra Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-um@lists.infradead.org, criu@openvz.org, avagin@google.com, Andrew Morton , Andy Lutomirski , Anton Ivanov , Christian Brauner , Dmitry Safonov <0x7f454c46@gmail.com>, Ingo Molnar , Jeff Dike , Mike Rapoport , Michael Kerrisk , Oleg Nesterov , Richard Weinberger , Thomas Gleixner Subject: Re: [PATCH 2/4] arch/x86: implement the process_vm_exec syscall Message-ID: References: <20210414055217.543246-1-avagin@gmail.com> <20210414055217.543246-3-avagin@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=koi8-r Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 02, 2021 at 10:51:13AM +0200, Peter Zijlstra wrote: > > I'm terrified of all of this... > > On Tue, Apr 13, 2021 at 10:52:15PM -0700, Andrei Vagin wrote: > > > +long swap_vm_exec_context(struct sigcontext __user *uctx) > > +{ > > + struct sigcontext ctx = {}; > > + sigset_t set = {}; > > + > > + > > + if (copy_from_user(&ctx, uctx, CONTEXT_COPY_SIZE)) > > + return -EFAULT; > > + /* A floating point state is managed from user-space. */ > > + if (ctx.fpstate != 0) > > + return -EINVAL; Here, we check that ctx doesn't have an FPU state. > > + if (!user_access_begin(uctx, sizeof(*uctx))) > > + return -EFAULT; > > + unsafe_put_sigcontext(uctx, NULL, current_pt_regs(), (&set), Efault); > > + user_access_end(); > > But here you save the sigcontext without FPU state. > > > + > > + if (__restore_sigcontext(current_pt_regs(), &ctx, 0)) > > + goto badframe; > > And here you restore sigcontext, *with* FPU state. At which point your > FPU state is irrecoverably lost. process_vm_exec doesn't change a process FPU state. Unlike signals, here we can control it from a user-space. A process can set an FPU state before process_vm_exec and then retore its FPU state after the call. This version of patches has a bug that I fixed in my tree when I implemented the user-space part for gVisor. I didn't take into account that restore_sigcontext(ctx) clears a process fpu state if ctx->fpstate is zero. I moved fpu__restore_sig out from __restore_sigcontext to fix this issue: https://github.com/avagin/linux-task-diag/commit/55b7194d00ff > > Also, I'm not at all convinced this can ever do the right thing when the > tasks don't agree on what the FPU state is. I suppose in the best case > the save will EFAULT. > > > + > > + return 0; > > +Efault: > > + user_access_end(); > > +badframe: > > + signal_fault(current_pt_regs(), uctx, "swap_vm_exec_context"); > > + return -EFAULT; > > +}