Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp2928420ybi; Sun, 26 May 2019 10:35:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqwqaa13h86JHmQyJsBkFF8c4xJqwWAYu93xPud+edvZPGaPwcrc30Sd+/6c3MkB1g6Rii7O X-Received: by 2002:a17:90a:372a:: with SMTP id u39mr25626403pjb.19.1558892141965; Sun, 26 May 2019 10:35:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558892141; cv=none; d=google.com; s=arc-20160816; b=04q6cRUY38qtWEKn1r8cDchQjyI8GijLzBF1VSYCg10K4ZwZ55e+hQymm4BLcRc7F/ y3y+xFPLf6yyNMnOGZ7gkx0hxgs3yAmtaXKi2ZKCA0oIvRRy3LMxjDT/1ObBK+vg6Z+4 oQXDfoQxmhQfHtUJArSHOTMWe6ORRctzy5MiceeCdoScjc5TaQD1orjveza9vDJXLPGn sMyexEH4YetlYOoVH9DHl/giMjG7SU4uSPah6jPp4eqh8RcorBkv9ND7+jqDd4BHTNo8 mWEBfs7pv9IFCuN0FEo8b92DCXiggXv/yst/fMJa1u+/xyOUoorcDAyWabOjSY3K9pZv +LEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date; bh=cNwt8jxrbWcE5noyG1OUv2kNpkiSO+S3XqdYDpukk7o=; b=ZSMY2vgTkNPUw6Y68IIw947ZN2YV6lLnoCnbLqGlXzK4Dls38LNpnLMnaRyIgOkIy+ QhQnp/xxtgT8+gzBEz15ZBeO4yaLmbewPNHW8g2A9BhVEdZukNua0yrX99EyVN8yFmPf eCnBBqk19fZ+Welbr89Y5VWM67WA3jnnR2hxfhGsrHQmdcOzAuLbSvzOojHnFBUymqa+ y5ZObeT10owTjWaL3GVuSjbf8yPxQ5nQmRL82BtZFyVyH/HmjJ9pBT5vSPfpAlbPr8+A c2hiWws0lfQY1upZFyRQi5Lwy0fsI9XWy2uGZAtTWkSoA313Q9Zhu3lFgoolTnLYiJV4 B9ZA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p91si15874329plb.165.2019.05.26.10.35.16; Sun, 26 May 2019 10:35:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727996AbfEZRdi (ORCPT + 99 others); Sun, 26 May 2019 13:33:38 -0400 Received: from Galois.linutronix.de ([146.0.238.70]:46270 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727926AbfEZRdi (ORCPT ); Sun, 26 May 2019 13:33:38 -0400 Received: from bigeasy by Galois.linutronix.de with local (Exim 4.80) (envelope-from ) id 1hUx1S-00024Z-LB; Sun, 26 May 2019 19:33:26 +0200 Date: Sun, 26 May 2019 19:33:25 +0200 From: Sebastian Andrzej Siewior To: Hugh Dickins , x86@kernel.org Cc: Andrew Morton , Mike Rapoport , Andrea Arcangeli , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Borislav Petkov , Pavel Machek , Dave Hansen Subject: [PATCH] x86/fpu: Use fault_in_pages_writeable() for pre-faulting Message-ID: <20190526173325.lpt5qtg7c6rnbql5@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hugh Dickins Since commit d9c9ce34ed5c8 ("x86/fpu: Fault-in user stack if copy_fpstate_to_sigframe() fails") we use get_user_pages_unlocked() to pre-faulting user's memory if a write generates a page fault while the handler is disabled. This works in general and uncovered a bug as reported by Mike Rapoport. It has been pointed out that this function may be fragile and a simple pre-fault as in fault_in_pages_writeable() would be a better solution. Better as in taste and simplicity: That write (as performed by the alternative function) performs exactly the same faulting of memory that we had before. This was suggested by Hugh Dickins and Andrew Morton. Use fault_in_pages_writeable() for pre-faulting of user's stack. Suggested-by: Andrew Morton Signed-off-by: Hugh Dickins Link: https://lkml.kernel.org/r/alpine.LSU.2.11.1905251033230.1112@eggly.anvils [bigeasy: patch description] Signed-off-by: Sebastian Andrzej Siewior --- arch/x86/kernel/fpu/signal.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c index 5a8d118bc423e..060d6188b4533 100644 --- a/arch/x86/kernel/fpu/signal.c +++ b/arch/x86/kernel/fpu/signal.c @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -189,15 +190,7 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) fpregs_unlock(); if (ret) { - int aligned_size; - int nr_pages; - - aligned_size = offset_in_page(buf_fx) + fpu_user_xstate_size; - nr_pages = DIV_ROUND_UP(aligned_size, PAGE_SIZE); - - ret = get_user_pages_unlocked((unsigned long)buf_fx, nr_pages, - NULL, FOLL_WRITE); - if (ret == nr_pages) + if (!fault_in_pages_writeable(buf_fx, fpu_user_xstate_size)) goto retry; return -EFAULT; } -- 2.20.1