Received: by 2002:a17:90a:37e8:0:0:0:0 with SMTP id v95csp7144761pjb; Wed, 16 Oct 2019 00:48:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqyI64RGRxHYXuaLvWNzQmXKfaUl/eI1DQpbn92vSy/F4rSyLLvH4mPuMpJX9GqAA5KsRo5y X-Received: by 2002:a17:906:3016:: with SMTP id 22mr37535634ejz.227.1571212093037; Wed, 16 Oct 2019 00:48:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571212093; cv=none; d=google.com; s=arc-20160816; b=aXlYKiClLUCtYehCNPxNN0Wms/d7T9DnZIptwiAZNGnIFrm9UVns8TrIHmk1vMHQ6Z ajh1VlVQyqZrhSmMPbKB6ck0pzHEy7gyMEzpcQzKlaRQap71lIyDeQvtXXq+KJhu5w7S k4KD1JUZs/TTpL5/bA4UT0Mk2LCIR9519pMCQqdyRiAfEHJA4xuWBUsWH5WfAcBzGnx0 KVMRpIsFER1UbTZmtKSVEpvHDf3JAa2wrriMUU5v51cejNk1gEXLul+UqdZgiH+Xje+T WthufwvdqX4HHp/jnBsmBf3hWTRoVxo6rn+YKqCe9NtBq2Rc79+mJa4kwIMHoO1pwn8v JsHw== 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=rY9kFSOPhstYSObLmkONtfozGMWOJcqXffo6wC4bDWc=; b=dmGemA+WS2MiVAumJeZ2UZPxFRfK3W2NHQXb6LrByXYuafVFPXjDiJTP7hx3cnjKnO W3bgNECyya/WFp/o2yC0PTHlVrIArRUEycF2xOStRk+HEOjtSVrELCcqZho2MRIJ28JK nggON+vWlK3MGcu+xGSvfW85sH0iE5iwE0eigASepGdJd7Mqwj9WqCUeC7WvlKf0Yszn SlHJqch2VraA3XMEcDRZH/fOwaDJdssKlE7kVYaUKrQZajmwT+C+0sBjgxhUdD7o4KWD kvNGjzreyMz3g7HZBmEH7NTDH9m6yj1l2SjM6G/4QNeBeWuzFmx2IJqHYRjgnOY9JoBS sJUg== 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 j38si17148036ede.374.2019.10.16.00.47.49; Wed, 16 Oct 2019 00:48:13 -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 S2390365AbfJPAmD (ORCPT + 99 others); Tue, 15 Oct 2019 20:42:03 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:53360 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390304AbfJPAlu (ORCPT ); Tue, 15 Oct 2019 20:41:50 -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-Qz; 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 3/3] bpf: use copy_struct_from_user() in bpf() syscall Date: Wed, 16 Oct 2019 02:41:38 +0200 Message-Id: <20191016004138.24845-4-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]) copy_struct_from_user(). This helper is intended for all codepaths that copy structs from userspace that are versioned by size. The bpf() syscall does exactly what copy_struct_from_user() is doing. Note that copy_struct_from_user() is calling min() already. So technically, the min_t() call could go. But the size is used further below so leave it. [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-4-christian.brauner@ubuntu.com /* v2 */ - Alexei Starovoitov : - remove unneeded initialization --- kernel/bpf/syscall.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 45524089e15d..5db9887a8f4c 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2817,20 +2817,17 @@ static int bpf_task_fd_query(const union bpf_attr *attr, SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) { - union bpf_attr attr = {}; + union bpf_attr attr; int err; if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN)) return -EPERM; - err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); - if (err) - return err; size = min_t(u32, size, sizeof(attr)); - /* copy attributes from user space, may be less than sizeof(bpf_attr) */ - if (copy_from_user(&attr, uattr, size) != 0) - return -EFAULT; + err = copy_struct_from_user(&attr, sizeof(attr), uattr, size); + if (err) + return err; err = security_bpf(cmd, &attr, size); if (err < 0) -- 2.23.0