Received: by 2002:a05:6512:3d0e:0:0:0:0 with SMTP id d14csp15830lfv; Tue, 12 Apr 2022 15:22:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJ19Q+tGKul34InGbETFntgEhH+hdxmTfa9tQ1FkcyGaQ+fnRL82BkOE6atDZKZL697CDA X-Received: by 2002:a65:400d:0:b0:39d:2213:ca72 with SMTP id f13-20020a65400d000000b0039d2213ca72mr10693799pgp.332.1649802132602; Tue, 12 Apr 2022 15:22:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649802132; cv=none; d=google.com; s=arc-20160816; b=d3NlfU3EPKsnynv/TvGlwZH/wZND2Z9tfP3jChT5ObbV3S3xTcz5sTmvkude1ufAKo CJZV2/S5sJhJX+MKDmiKPySjt6aRhSlxPaJnbJYL2lMzsH/fHJVd+Q2Qq503oRyuSISd TQzZOD5jalHMxLcmF2BYgEyOEaGGT40mAj/P7dPlyMyNoljrRz0nCx1KJTslMJUEvgx9 niV5H+5XQsmVxCNXNkrcivm/hpBThbN8OM1YByG6G//sSBhSohQz9M99MvUCTLA5khcJ 0Etv61DEmJlg5qGg7yI4D11XqCQ7nrw0Qg2A2/prsWkxosnDYtOnxV7y5oaTP6xUaVxn +Jpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3FBXUJRmycW1VMCEhoyCZP3+DQFjtSTq4cd039vhiwo=; b=klcGwMAFEMpwo6GpiScsc4zrX9Y3AG6xiCDYqBSNU/8NmCu0gtenWQsG4b58L8bmp8 UOVPn1FAiURb5Amia0x8kAEIFGD1q+2O3/e6qu9l0kgUjCpKWw2HKPaQmPxhMKbkBs+m 32j7Pe20cajk/60BlO9A8IjI0hbruwAZOytbvFgx8wkil0HtAnsg5h7PXxZOyr8xEJnw pcV9B7h30kzKcpXzdbgn6NI1yghjjr2lzQRiU+5lhC5qOgsRGNJqzF5bLEi2eI0+IvN+ pGzC8jL85LMm4rJVM96P8Qn+PhnHJrDsLOW4x0gcHMVxzlSIIYa66HOA+M09YQXQx5KP +NGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iNPNGVDs; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id r8-20020a635d08000000b003816043f160si3884598pgb.853.2022.04.12.15.21.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 15:22:12 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iNPNGVDs; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F3AA7176651; Tue, 12 Apr 2022 14:02:39 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1385462AbiDLIvw (ORCPT + 99 others); Tue, 12 Apr 2022 04:51:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358746AbiDLHmK (ORCPT ); Tue, 12 Apr 2022 03:42:10 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A97EBFA; Tue, 12 Apr 2022 00:19:04 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C75E06178D; Tue, 12 Apr 2022 07:19:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D498EC385A1; Tue, 12 Apr 2022 07:19:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649747943; bh=2jdeLQ93hezcJuVqD+oM3RT2cCSavnSxmemMyJ13KnY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iNPNGVDs2yoVXn0CKd2i7FAWoBZGtCcc4un/fBqWmKDy8mkW1NkjfXHPjxXc5uuZv Z9ABW76gISixNGaA/+mewUJ1LyGkED6n+yqoGcTxXJSJTB8bFFTBVbOzDXDtwqCHaW 9XJmDCWSjTV1h5D0u/UK/ZfmWtafkY9ioXMc4poI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, James Clark , Denis Nikitin , Jiri Olsa , Alexander Shishkin , Alexey Budankov , Namhyung Kim , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 5.17 262/343] perf session: Remap buf if there is no space for event Date: Tue, 12 Apr 2022 08:31:20 +0200 Message-Id: <20220412062958.886965492@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220412062951.095765152@linuxfoundation.org> References: <20220412062951.095765152@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 From: Denis Nikitin [ Upstream commit bc21e74d4775f883ae1f542c1f1dc7205b15d925 ] If a perf event doesn't fit into remaining buffer space return NULL to remap buf and fetch the event again. Keep the logic to error out on inadequate input from fuzzing. This fixes perf failing on ChromeOS (with 32b userspace): $ perf report -v -i perf.data ... prefetch_event: head=0x1fffff8 event->header_size=0x30, mmap_size=0x2000000: fuzzed or compressed perf.data? Error: failed to process sample Fixes: 57fc032ad643ffd0 ("perf session: Avoid infinite loop when seeing invalid header.size") Reviewed-by: James Clark Signed-off-by: Denis Nikitin Acked-by: Jiri Olsa Cc: Alexander Shishkin Cc: Alexey Budankov Cc: Namhyung Kim Link: https://lore.kernel.org/r/20220330031130.2152327-1-denik@chromium.org Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/perf/util/session.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 498b05708db5..245dc70d1882 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -2084,6 +2084,7 @@ prefetch_event(char *buf, u64 head, size_t mmap_size, bool needs_swap, union perf_event *error) { union perf_event *event; + u16 event_size; /* * Ensure we have enough space remaining to read @@ -2096,15 +2097,23 @@ prefetch_event(char *buf, u64 head, size_t mmap_size, if (needs_swap) perf_event_header__bswap(&event->header); - if (head + event->header.size <= mmap_size) + event_size = event->header.size; + if (head + event_size <= mmap_size) return event; /* We're not fetching the event so swap back again */ if (needs_swap) perf_event_header__bswap(&event->header); - pr_debug("%s: head=%#" PRIx64 " event->header_size=%#x, mmap_size=%#zx:" - " fuzzed or compressed perf.data?\n",__func__, head, event->header.size, mmap_size); + /* Check if the event fits into the next mmapped buf. */ + if (event_size <= mmap_size - head % page_size) { + /* Remap buf and fetch again. */ + return NULL; + } + + /* Invalid input. Event size should never exceed mmap_size. */ + pr_debug("%s: head=%#" PRIx64 " event->header.size=%#x, mmap_size=%#zx:" + " fuzzed or compressed perf.data?\n", __func__, head, event_size, mmap_size); return error; } -- 2.35.1