Received: by 2002:ac0:8845:0:0:0:0:0 with SMTP id g63csp930535img; Thu, 28 Feb 2019 10:05:58 -0800 (PST) X-Google-Smtp-Source: APXvYqz13hsM5WKaXV4ovS/VX1AMJ3LEKkoyktqB+2uXGNlsdivMFr2o7RsRWxgBPvNIapMSOut2 X-Received: by 2002:a63:6a48:: with SMTP id f69mr409255pgc.7.1551377157963; Thu, 28 Feb 2019 10:05:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551377157; cv=none; d=google.com; s=arc-20160816; b=qlpWLIiS6rbwX4ruLyNgCO2m9w/qiijx23OJt5ryJNxHT7SHo7DuOqBaH7UHH/w2VL qzVKZcQZ2R+eodPpjxg+qqHswkx6/nBkCyRywugm5SSEzregmYErx0UI9sA1n3m4CiEM S/viK8N7FYN3ScRC1Sz+R7VDLrUZ9XXsQTQnCBcoc5HMLp8CxmvbJfPvEtIGu/c4Mu1p C0fBzgNtbWKm+cVGnG5Cx5ZyKtyzJ38Fw26iy16ulTW1jXT/o9TI/hk++9DTnl80AQmo wfxlgfmCrqfv3chFGLZ4jyRkidTkYpNvFUVvnsk5q4fRanIu7T74vFZ/KAU5eiUGBlHW Nzpg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=M0FN6TW26O7XNRSq2d1Bv/w/n85UbidHuY1oKNoERHY=; b=z1VaJN4hvI6L8MxrAMXIoNhbDZx+Y9T84leJrB6nueBBmNaR57bTreC4xJF8JfukTD 7rwvEA0pwthd9vwIWJ3FZ70wEYCMvXXAqTxvmMgWl3zDMNg23rFO160YieAyEPlhX1EC 76nUFg1u0HQny303nTVOgIVHehOhFC7QQmwoxqLtgulifow7g5BUnqL01YSTF5RU8iDM C8Ntv1FHYiWQmm3KJ+Xaf9f5hpuy6qNzQGtSkuJWPANrCVc9yuGPazpheuQWGU5LbSCR 8oioTIOdRxVl66Z4DAMDJ3lXopNT/OlkmLaY9qUEx6v0PU3vw7YmMFB+LG6iPLLPa/HH v3QQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=jjd2xmyv; 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 t62si17743114pgd.369.2019.02.28.10.04.55; Thu, 28 Feb 2019 10:05:57 -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=jjd2xmyv; 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 S1732165AbfB1QDV (ORCPT + 99 others); Thu, 28 Feb 2019 11:03:21 -0500 Received: from mail.kernel.org ([198.145.29.99]:49314 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729880AbfB1QDU (ORCPT ); Thu, 28 Feb 2019 11:03:20 -0500 Received: from localhost.localdomain (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 77CAE2184A; Thu, 28 Feb 2019 16:03:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1551369799; bh=6HIg3cCfp80ZWIMRQ0sAPRGGN0ciKotjFxY4qEk/hP8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jjd2xmyv89Nz7F27mQqwazjWf8ihvr4wMg73Ep19t0RUfTa76P+MClTcbZZDLC0zr FWTCnql5wp3PacdCCNWYJLc6C6oV0Qn8VQmUZzSrijPWuP6VPsrsWWH8PHNPxoAj06 YBj8eAIKOs1N++Z7PvXqPpSVuEr5v7tH03RgzYUg= From: Masami Hiramatsu To: Steven Rostedt , Linus Torvalds , Shuah Khan Cc: mhiramat@kernel.org, linux-kernel@vger.kernel.org, Andy Lutomirski , Ingo Molnar , Andrew Morton , Changbin Du , Jann Horn , Kees Cook , Andy Lutomirski , Alexei Starovoitov , Nadav Amit , Peter Zijlstra , Joel Fernandes , yhs@fb.com Subject: [PATCH v5 1/6] uaccess: Add user_access_ok() Date: Fri, 1 Mar 2019 01:02:55 +0900 Message-Id: <155136977556.2968.5821499627398165570.stgit@devbox> X-Mailer: git-send-email 2.13.6 In-Reply-To: <155136974478.2968.3105123100519786079.stgit@devbox> References: <155136974478.2968.3105123100519786079.stgit@devbox> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Zijlstra Add user_access_ok() macro which ensures current context is user context, or explicitly do set_fs(USER_DS). This function is very much like access_ok(), except it (may) have different context validation. In general we must be very careful when using this. Signed-off-by: Peter Zijlstra Signed-off-by: Masami Hiramatsu --- arch/x86/include/asm/uaccess.h | 8 +++++++- include/linux/uaccess.h | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h index 780f2b42c8ef..3125d129d3b6 100644 --- a/arch/x86/include/asm/uaccess.h +++ b/arch/x86/include/asm/uaccess.h @@ -92,12 +92,18 @@ static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, un * checks that the pointer is in the user space range - after calling * this function, memory access functions may still return -EFAULT. */ -#define access_ok(addr, size) \ +#define access_ok(addr, size) \ ({ \ WARN_ON_IN_IRQ(); \ likely(!__range_not_ok(addr, size, user_addr_max())); \ }) +#define user_access_ok(addr, size) \ +({ \ + WARN_ON_ONCE(!segment_eq(get_fs(), USER_DS)); \ + likely(!__range_not_ok(addr, size, user_addr_max())); \ +}) + /* * These are the main single-value transfer routines. They automatically * use the right size if we just have the right pointer type. diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h index 37b226e8df13..bf762689658b 100644 --- a/include/linux/uaccess.h +++ b/include/linux/uaccess.h @@ -10,6 +10,24 @@ #include +/** + * user_access_ok: Checks if a user space pointer is valid + * @addr: User space pointer to start of block to check + * @size: Size of block to check + * + * Context: User context or explicit set_fs(USER_DS). + * + * This function is very much like access_ok(), except it (may) have different + * context validation. In general we must be very careful when using this. + */ +#ifndef user_access_ok +#define user_access_ok(addr, size) \ +({ \ + WARN_ON_ONCE(!segment_eq(get_fs(), USER_DS)); \ + access_ok(addr, size); \ +}) +#endif + /* * Architectures should provide two primitives (raw_copy_{to,from}_user()) * and get rid of their private instances of copy_{to,from}_user() and