Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp35270200rwd; Mon, 10 Jul 2023 05:15:17 -0700 (PDT) X-Google-Smtp-Source: APBJJlG71tMZXoNOTQi41nvd8nQ1JE05GbffhJlS/EH7Pv/fURonD1rjQi8sUVmuvt990ffF68x0 X-Received: by 2002:a2e:9250:0:b0:2b6:fe3c:c3c1 with SMTP id v16-20020a2e9250000000b002b6fe3cc3c1mr10184793ljg.4.1688991317058; Mon, 10 Jul 2023 05:15:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688991317; cv=none; d=google.com; s=arc-20160816; b=V35AvV+v5ez0Pnmpeld02Odg3V7TP22Vmij7a5pqHb8Fj//EKW0HBjO0PbeoOLhqiY 9lhlHY+nT1JVNFNdP3WUtoOwZDcylukZJz3+chzY/8rUOmqVxnJmnmeBQ42pl0fdlITQ 4EmTBEPC4OYkh2mv1i1TPHVU7ZjW1uG9Tmq60WZ3+UxxPj5MbU9NWVhrB69D19Vlondn dJFZaJ1LiBIvDFGOYfQW6/NiUxQTLeNsM88cgEFz/CwkXKC+wPI+LOtlY/i6c6KVtJO2 QUC2R/cThdKKObDLcDDNOtpQeiq9xB5x0f7EfB4jmJ2PENdWNDm6mxeSdQWrhzXhAE9p cDBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=7a3aObqALZ0RGVJNPU87pW+rmJD5s/3iTVbIQ9L7MVY=; fh=Pu4049EnaK/FIarmotUCTkK2RRDxgUnVuZXv1CZygFE=; b=CriL7Ek3WEURpaODw8RD1OsETNKrAsXH1uwOD37lOUIdlKwA9MSRi+QA7yxrjZtxit f/GajY4+0rA7wJfbhho+KhXJYRhptEi46db/5CsDSN2MR5jFkUjk4VIIhebukfWWG4eY p/gTnLnsioNIKbYENuIHNPUN3KA+p8XXC0ZdsX1ArJY9N5vbndZWmMQh4jGdfuemxU7k 0aAR+z/XE9x85jxlp4YzwCzlaqO1qUYgQ/FTzEhx99lm6X6Zd9Z4Wqtcs5QYd/qYddfn PT3JFh3Z99GNQ48HhhHjgZRWUsulC67Obr6vsV9YpAxUKiAl6YE1uCbtQLhgupjI4tWA hxvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=jK+jMf+l; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p12-20020a1709060dcc00b009928b4e3b9esi8765305eji.313.2023.07.10.05.14.53; Mon, 10 Jul 2023 05:15:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=jK+jMf+l; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230389AbjGJMBB (ORCPT + 99 others); Mon, 10 Jul 2023 08:01:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229663AbjGJMBA (ORCPT ); Mon, 10 Jul 2023 08:01:00 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63C4AD1; Mon, 10 Jul 2023 05:00:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=7a3aObqALZ0RGVJNPU87pW+rmJD5s/3iTVbIQ9L7MVY=; b=jK+jMf+l74JDzsvgKRmBF4nw95 wxZTqNU1gg/WKprlF0LO8D2en3yOA61Kshjq/DxDa+/kqZ8nFhKca0gXFfKQ9qfbo3n54C4MiC+T8 T+QNDSKZM+ucpPDLOZKvE4Wlu59PAFYBtGhMoaFzuK+hRQAa5dP8UxK+rVrEbFgt+4iAYDthzC1bs SogNMxPbjzWK3WpN02v+NQIvlaG+N8WLW6/HjEjuC2psnZ7/fUnr6xR8ijWA9XaGX2czu7u6g2WAD Uzc/C+ixvg5d7maGag0HUi3Zjetl5ab3nfcGsNdvPTGTr7597PFKqsPoIfJuekshDJoV42NZSZz7Z 2pLsxtPg==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1qIpZ6-00EZpt-FH; Mon, 10 Jul 2023 12:00:28 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 87F0630017D; Mon, 10 Jul 2023 14:00:26 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 6B79B2B3B25E1; Mon, 10 Jul 2023 14:00:26 +0200 (CEST) Date: Mon, 10 Jul 2023 14:00:26 +0200 From: Peter Zijlstra To: Shuai Xue Cc: alexander.shishkin@linux.intel.com, james.clark@arm.com, leo.yan@linaro.org, mingo@redhat.com, baolin.wang@linux.alibaba.com, acme@kernel.org, mark.rutland@arm.com, jolsa@kernel.org, namhyung@kernel.org, irogers@google.com, adrian.hunter@intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Subject: Re: [Patch v2] perf/core: Bail out early if the request AUX area is out of bound Message-ID: <20230710120026.GA3034907@hirez.programming.kicks-ass.net> References: <20230613123211.58393-1-xueshuai@linux.alibaba.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230613123211.58393-1-xueshuai@linux.alibaba.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 13, 2023 at 08:32:11PM +0800, Shuai Xue wrote: > kernel/events/ring_buffer.c | 13 +++++++++++++ > tools/perf/Documentation/perf-record.txt | 3 ++- > 2 files changed, 15 insertions(+), 1 deletion(-) > > diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c > index a0433f37b024..e514aaba9d42 100644 > --- a/kernel/events/ring_buffer.c > +++ b/kernel/events/ring_buffer.c > @@ -673,6 +673,7 @@ int rb_alloc_aux(struct perf_buffer *rb, struct perf_event *event, > bool overwrite = !(flags & RING_BUFFER_WRITABLE); > int node = (event->cpu == -1) ? -1 : cpu_to_node(event->cpu); > int ret = -ENOMEM, max_order; > + size_t bytes; > > if (!has_aux(event)) > return -EOPNOTSUPP; > @@ -699,6 +700,18 @@ int rb_alloc_aux(struct perf_buffer *rb, struct perf_event *event, > watermark = 0; > } > > + /* > + * 'rb->aux_pages' allocated by kcalloc() is a pointer array which is > + * used to maintains AUX trace pages. The allocated page for this array > + * is physically contiguous (and virtually contiguous) with an order of > + * 0..MAX_ORDER. If the size of pointer array crosses the limitation set > + * by MAX_ORDER, it reveals a WARNING. > + * > + * So bail out early if the request AUX area is out of bound. > + */ > + if (check_mul_overflow(nr_pages, sizeof(void *), &bytes) || > + get_order(bytes) > MAX_ORDER) > + return -EINVAL; This is all quite horrific :/ What's wrong with something simple: /* Can't allocate more than MAX_ORDER */ if (get_order((unsigned long)nr_pages * sizeof(void*)) > MAX_ORDER) return -EINVAL; If you're on 32bit then nr_pages should never be big enough to overflow, fundamentally you'll only have 32-PAGE_SHIFT bits in nr_pages. > rb->aux_pages = kcalloc_node(nr_pages, sizeof(void *), GFP_KERNEL, > node);