Received: by 2002:a17:90a:37e8:0:0:0:0 with SMTP id v95csp7143482pjb; Wed, 16 Oct 2019 00:46:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqwVbmksKwR6VrK9dfbqoZkK14efoPfbWkuHcOKgQZdnOnu/gddCy65WD6r+12uYO7jAgmaT X-Received: by 2002:a17:906:4748:: with SMTP id j8mr39104830ejs.210.1571212006499; Wed, 16 Oct 2019 00:46:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571212006; cv=none; d=google.com; s=arc-20160816; b=kONKElqysTQvTtPy81kXScWROb3KAnGvLgKP2wiKAYgPk2z1Ysft0FfwmH2fgCja3V AcgXym5iAol6CK86dq9cuJ9Ad4ZY9hDwNYVsMaFlJgCD+Pb8wcfyiXsE7eRFEPqpBJr8 iMGnr34DQucyLxNxwZL+SKL9cc0cLTEQNHdBleA9sGD6w6KhFeCILbFJ8vuOqvWuIkil usKvRs1lj8L487RMPz6jRIyPcQGSuh++h5Y9DOvYz6972slsqWLpM28n7LlH4tavOo9K fMmryamOlPpa5HNnXf4cHCy/vPIEA1hfgDUV3+D2c63zp43nJHK8WypkKM9wOkxADxdf ObPQ== 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:date:subject:cc:to:from; bh=zPXTL//nYyzbdWxaudWC2AAZ9xp0+fJhl8UEC+mWuQg=; b=AyVgn8jYtK3tofuwnCwG2r4MuQ3KDm4O9eL/FblQ7pUHfrFISGlQeXf1XCLwOGqZeg AgAxx3LQk8dlJHSc1h3NO5iAlkZb1C2o2dMRtHW1WYIoU8B8J/DQfPPijcYPZ5lKEe6p riCpaXd7xJsP0Uy1U452YP0svHQ03VQqcM6Lzk3jd8hfZ4hRxCfZggEZd4299DrtWkK0 b5snwejz5cFm5VFQqdywFUa8JKL4wVpHEfVkWlR/3yYKGDLHr+4ZsphwFY/xzomctdxo w/OWkv0qq6KR/ErQFoKlsXKAuzrgGhHivGXELX7T9m31HZfX4Uakm5kYOv7zdMfQsImC jjgA== 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 qh16si15292857ejb.349.2019.10.16.00.46.23; Wed, 16 Oct 2019 00:46:46 -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 S2390349AbfJPAlx (ORCPT + 99 others); Tue, 15 Oct 2019 20:41:53 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:53354 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390300AbfJPAlt (ORCPT ); Tue, 15 Oct 2019 20:41:49 -0400 Received: from [213.220.153.21] (helo=localhost.localdomain) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1iKXNr-00065g-01; Wed, 16 Oct 2019 00:41:47 +0000 From: Christian Brauner To: christian.brauner@ubuntu.com Cc: ast@kernel.org, bpf@vger.kernel.org, daniel@iogearbox.net, kafai@fb.com, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, songliubraving@fb.com, yhs@fb.com, Aleksa Sarai Subject: [PATCH v2 1/3] bpf: use check_zeroed_user() in bpf_check_uarg_tail_zero() Date: Wed, 16 Oct 2019 02:41:36 +0200 Message-Id: <20191016004138.24845-2-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191016004138.24845-1-christian.brauner@ubuntu.com> References: <20191009160907.10981-1-christian.brauner@ubuntu.com> <20191016004138.24845-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In v5.4-rc2 we added a new helper (cf. [1]) check_zeroed_user() which does what bpf_check_uarg_tail_zero() is doing generically. We're slowly switching such codepaths over to use check_zeroed_user() instead of using their own hand-rolled version. [1]: f5a1a536fa14 ("lib: introduce copy_struct_from_user() helper") Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: bpf@vger.kernel.org Acked-by: Aleksa Sarai Signed-off-by: Christian Brauner --- /* v1 */ Link: https://lore.kernel.org/r/20191009160907.10981-2-christian.brauner@ubuntu.com /* v2 */ - Alexei Starovoitov : - Add a comment in bpf_check_uarg_tail_zero() to clarify that copy_struct_from_user() should be used whenever possible instead. --- kernel/bpf/syscall.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 82eabd4e38ad..b289ae747cae 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -58,35 +58,31 @@ static const struct bpf_map_ops * const bpf_map_types[] = { * There is a ToCToU between this function call and the following * copy_from_user() call. However, this is not a concern since this function is * meant to be a future-proofing of bits. + * + * Note, instead of using bpf_check_uarg_tail_zero() followed by + * copy_from_user() use the dedicated copy_struct_from_user() helper which + * performs both tasks whenever possible. */ int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size, size_t actual_size) { - unsigned char __user *addr; - unsigned char __user *end; - unsigned char val; + size_t size = min(expected_size, actual_size); + size_t rest = max(expected_size, actual_size) - size; int err; if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ return -E2BIG; - if (unlikely(!access_ok(uaddr, actual_size))) - return -EFAULT; - if (actual_size <= expected_size) return 0; - addr = uaddr + expected_size; - end = uaddr + actual_size; + err = check_zeroed_user(uaddr + expected_size, rest); + if (err < 0) + return err; - for (; addr < end; addr++) { - err = get_user(val, addr); - if (err) - return err; - if (val) - return -E2BIG; - } + if (err) + return -E2BIG; return 0; } -- 2.23.0