Received: by 2002:ac0:8845:0:0:0:0:0 with SMTP id g63csp531496img; Tue, 26 Feb 2019 04:27:15 -0800 (PST) X-Google-Smtp-Source: AHgI3IZRTwj07TE4vTal/Rl9/3ZZCORnzZhX0AXUPZwRt24b316HzThrvJ6iQ+JwOiSPVA9KrY9o X-Received: by 2002:a63:d49:: with SMTP id 9mr24108614pgn.27.1551184035014; Tue, 26 Feb 2019 04:27:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551184035; cv=none; d=google.com; s=arc-20160816; b=ob+1iuG4e1gdGv8dTTo0HkUPfKXYRrjugfjAXeGxA5EPWt362VNtvV/G7bCUgoJy/Q YGANXoNQXLSFxsDMw/oDlC7XtJ2WKWDFyBEhQ/Q+w+OERf8o72MeiMq3rb8WUw7HOZql ycxLXhGMiINcS1KOSX+V3SfQfXekhZINOeSRTgGsTug4MhF565QplNJuGhcUPEkEMBaD 91JL2Jnxh0CqfNPTpLgkwzlBGD24UbTLyEMoUSh1iCCeDPvEIY8X6E7BoMD41SixOtfW 8jCMvQzf5EXqLNdmwg8MgloiFvSSWBWggaE5Ss2izKVn9aiLLZuo/8j8PORptSmcWBe9 sMRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=g0z4xfU/ehMJe5u6skLGpNE2SI8MAv2SOH+mmbbIQJo=; b=MrTCrpLMXKIc+17ECLhWfQvVxAI3nHI9909Y81LZFiwUukOSWnQ0T1XN+atp+rGHRw qBDaaVhtOWjv53K516kHZHOXWJNJmsTHqIbyKMovfKkrMbYbIG/9So7P9T7qfhi8Qhoc mDPZ9ziZ0QfjphY63FDpR8h2F1v0rdAy2lw6sA9Dd56+GzHOneHyIN0beEleUlmXdukR JKPOQgMRg4r1Tz9YpNYPZsCP08hr+tl+njrdtPRbe7iuSWPtjpwtGt+SxponsFx0Xq1L es0KD9pTioO4R7guIp0DEgiuv9SJCYCDZ8fErMNslHY03twSQeWko/4ha5GPbUUbmrnW tq2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=WM8MSCGg; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 11si11798730pgd.272.2019.02.26.04.26.59; Tue, 26 Feb 2019 04:27:14 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=default header.b=WM8MSCGg; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726934AbfBZMYq (ORCPT + 99 others); Tue, 26 Feb 2019 07:24:46 -0500 Received: from mail.kernel.org ([198.145.29.99]:56598 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726063AbfBZMYq (ORCPT ); Tue, 26 Feb 2019 07:24:46 -0500 Received: from devbox (NE2965lan1.rev.em-net.ne.jp [210.141.244.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CDBFC2087C; Tue, 26 Feb 2019 12:24:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1551183885; bh=ETwGHflMYhX4AQ2bXQHYtui6Isayas8cvHYGcvjg3Ds=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=WM8MSCGgOcf2gOc2GUDbfdZrNCmMV3LonQOXn3USPpEoitQTkHicd8surY8w71eQ+ ZIea4Iylap2dNtXmd859BuX4AEbNdgZliMGBZtyZR1QoxhbgEvMnDatJmYu05ppPud lJXeE23Ay7Y147Co45AWp8ZJxE02aF4wTAofCcvU= Date: Tue, 26 Feb 2019 21:24:40 +0900 From: Masami Hiramatsu To: Masami Hiramatsu Cc: Linus Torvalds , Peter Zijlstra , Steven Rostedt , Linux List Kernel Mailing , Andy Lutomirski , Ingo Molnar , Andrew Morton , Changbin Du , Jann Horn , Kees Cook , Andy Lutomirski , Alexei Starovoitov , Nadav Amit Subject: Re: [RFC PATCH 2/4] uaccess: Add non-pagefault user-space read functions Message-Id: <20190226212440.c59f0a3fd5a9133cf980e137@kernel.org> In-Reply-To: <20190226131605.fa3969d542c6b13ed86e06f0@kernel.org> References: <155110348217.21156.3874419272673328527.stgit@devbox> <155110354092.21156.13871336589042178985.stgit@devbox> <20190225150603.GE32494@hirez.programming.kicks-ass.net> <20190226131605.fa3969d542c6b13ed86e06f0@kernel.org> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 26 Feb 2019 13:16:05 +0900 Masami Hiramatsu wrote: > Hi Linus, > > On Mon, 25 Feb 2019 09:00:57 -0800 > Linus Torvalds wrote: > > > On Mon, Feb 25, 2019 at 7:06 AM Peter Zijlstra wrote: > > > > > > Would something like so work for people? > > > > Looks reasonable to me. > > > > > Why not keep it simple: > > > > > > mm_segment_t old_fs = get_fs(); > > > > > > set_fs(USER_DS); > > > ret = __strncpy...(); > > > set_fs(old_fd); > > > > > > return ret; > > > > So none of this code looks sane. First odd, there's no real reason to > > use __get_user(). The thing should never be used. It does the whole > > stac/clac for every byte. > > Ah, I got it. I just followed the commit bd28b14591b9 ("x86: remove more > uaccess_32.h complexity") as same as strnlen_from_unsafe(). No special > reason. > > > > > In the copy_from_user() case, I suggested re-doing it as one common > > routine without the set_fs() dance for the "already there" case to > > simplify error handling. Here it doesn't do that. > > > > But honestly, I think for the strncpy case, we could just do > > > > long strncpy_from_unsafe_user(char *dst, const void __user *src, long count) > > { > > long ret; > > mm_segment_t old_fs = get_fs(); > > > > set_fs(USER_DS); > > pagefault_disable(); > > ret = strncpy_from_user(dst, src, count); > > pagefault_enable(); > > set_fs(old_fs); > > return ret; > > } > > > > and be done with it. Efficient and simple. > > Yes, it looks good to me :) > > > > > Note: the above will *only* work for actual user addresses, because > > strncpy_from_user() does that proper range check. > > I think we can reuse do_strncpy_from_user() for strncpy_from_unsafe(). > (so maybe we should move it from mm/maccess.c to lib/strncpy_from_user.c?) Ah, no, since lib/strncpy_from_user.c depends on CONFIG_GENERIC_STRNCPY_FROM_USER which is not selected on a half of supported architectures. Hmm, can we make strncpy_from_user() an __weak function and build that always? > As Kees pointed out, I think it is a good chance to sort the behavior of > these strXcpy APIs to match their names. I meant, at first rename strncpy_from_unsafe() to strscpy_from_unsafe() and change the behavior (returning the length of copied string, exclude NULL). Thank you, -- Masami Hiramatsu