Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp7669687ybn; Mon, 30 Sep 2019 18:16:28 -0700 (PDT) X-Google-Smtp-Source: APXvYqyeX482CaRAKAEm/9/QFSiYLzGsMGox+OM0O9PpvAlA43W6Rp9ElKhiJ24XWCMoDu6r0+p+ X-Received: by 2002:a50:f0d4:: with SMTP id a20mr23426587edm.149.1569892588137; Mon, 30 Sep 2019 18:16:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569892588; cv=none; d=google.com; s=arc-20160816; b=B4sCWYGPPZoRou1zVcDVKb0+1uh9TCWs/eYx9G/OAgOso5/5JxkM1GvufnkpDE4CIq HgC3XwXeY925D3bf+XYr5mdbibSJxFM1Tke0USoRKX42Q+tkCbnNPxFWloPEzjpRsV8j pOoz0Uw2cEXwygGnK1aF3SseNQHw9Brjv0n47ACAMqRvIFWKGI6c7WKUfXwRGzcw9mO0 QydcFUGxg4HsrOKhn4QcatdLa9ogcENkQzeZAIx11DneY0P/VZqJwWzUW87+FRs3fSXH WA01xq616bVAIi730tEeu6PzFV18VsaK3z98bhqZPi46hL/iCQtDzfXwqCDORNcemJbp dqaA== 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=taAk5X+Criy0KMkrPjTsnK4mEFE1pg7m3dlE4yVCDSM=; b=YZh9+dtVcb+Fjo9HlMzoS84PLqNr3VV75YmDRVzFC0cmdG0M8E7/hZLJt8ALVE07Ks QBgXisYWdWIlXmkcIVO7BfoO+AgHeyV3ngW/qCaSkTTc/Wy3rYupCmxGKJ6mu8Nghw89 3czj5BMteNR/NVHuJR6S9lwGoqdC32NcgYEYMBDzbRa/+JPh9mO+iXrIha23dEpYOuDI iYGTvYwZ7o+dh2E1c7nB3hlq58pncdU8/Llwe7nGH7xF2HzyGyxhrYCVATEQQpZySQFo MHApFfetEOaVQKKS4CpbOKSKIEHts3WRtY7ZBJMzqpZtVJH4rQrXhfKa8dXsdEfefS5S CF2A== 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 e48si8549915eda.130.2019.09.30.18.16.02; Mon, 30 Sep 2019 18:16:28 -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 S1730797AbfJABMr (ORCPT + 99 others); Mon, 30 Sep 2019 21:12:47 -0400 Received: from mx2.mailbox.org ([80.241.60.215]:40258 "EHLO mx2.mailbox.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726590AbfJABMr (ORCPT ); Mon, 30 Sep 2019 21:12:47 -0400 Received: from smtp2.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:105:465:1:1:0]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id 54365A221F; Tue, 1 Oct 2019 03:12:44 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.240]) by spamfilter01.heinlein-hosting.de (spamfilter01.heinlein-hosting.de [80.241.56.115]) (amavisd-new, port 10030) with ESMTP id eZZ-U1uKCHAI; Tue, 1 Oct 2019 03:12:41 +0200 (CEST) From: Aleksa Sarai To: Ingo Molnar , Peter Zijlstra , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Christian Brauner , Kees Cook Cc: Aleksa Sarai , Rasmus Villemoes , Al Viro , Linus Torvalds , libc-alpha@sourceware.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 4/4] perf_event_open: switch to copy_struct_from_user() Date: Tue, 1 Oct 2019 11:10:55 +1000 Message-Id: <20191001011055.19283-5-cyphar@cyphar.com> In-Reply-To: <20191001011055.19283-1-cyphar@cyphar.com> References: <20191001011055.19283-1-cyphar@cyphar.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 The change is very straightforward, and helps unify the syscall interface for struct-from-userspace syscalls. Reviewed-by: Kees Cook Signed-off-by: Aleksa Sarai --- kernel/events/core.c | 47 +++++++++----------------------------------- 1 file changed, 9 insertions(+), 38 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 4655adbbae10..3f0cb82e4fbc 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -10586,55 +10586,26 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr, u32 size; int ret; - if (!access_ok(uattr, PERF_ATTR_SIZE_VER0)) - return -EFAULT; - - /* - * zero the full structure, so that a short copy will be nice. - */ + /* Zero the full structure, so that a short copy will be nice. */ memset(attr, 0, sizeof(*attr)); ret = get_user(size, &uattr->size); if (ret) return ret; - if (size > PAGE_SIZE) /* silly large */ - goto err_size; - - if (!size) /* abi compat */ + /* ABI compatibility quirk: */ + if (!size) size = PERF_ATTR_SIZE_VER0; - - if (size < PERF_ATTR_SIZE_VER0) + if (size < PERF_ATTR_SIZE_VER0 || size > PAGE_SIZE) goto err_size; - /* - * If we're handed a bigger struct than we know of, - * ensure all the unknown bits are 0 - i.e. new - * user-space does not rely on any kernel feature - * extensions we dont know about yet. - */ - if (size > sizeof(*attr)) { - unsigned char __user *addr; - unsigned char __user *end; - unsigned char val; - - addr = (void __user *)uattr + sizeof(*attr); - end = (void __user *)uattr + size; - - for (; addr < end; addr++) { - ret = get_user(val, addr); - if (ret) - return ret; - if (val) - goto err_size; - } - size = sizeof(*attr); + ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size); + if (ret) { + if (ret == -E2BIG) + goto err_size; + return ret; } - ret = copy_from_user(attr, uattr, size); - if (ret) - return -EFAULT; - attr->size = size; if (attr->__reserved_1) -- 2.23.0