Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp654549rdh; Sun, 24 Sep 2023 06:19:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFdoz9zUaIhbAmsUvj2R0DDOQuOHHcr82dFKe/5p2Yla3w//YIClJn0ChO2Qs9/BkDyItCj X-Received: by 2002:a05:6808:1452:b0:3ae:18c0:dd8e with SMTP id x18-20020a056808145200b003ae18c0dd8emr8149180oiv.27.1695561553759; Sun, 24 Sep 2023 06:19:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695561553; cv=none; d=google.com; s=arc-20160816; b=t43hEqd1TRybehibd49t6uig0TVK+9BOYsah/2Xu8m1vStnZeyLaeLjWNcW6wyP5Ih HuczZrsd72qNZBM/iZLSljeGZrLxp+55/kbEuyHzpueUMNtsEXWtw6Tg3MJmBkj8Hb4V fCzWqo1BKyiUdo3RRT/zHhPyd+8apUO7XjFPCUacsPC9eh/9/fVTqqTVY6Apxnrz3EcU WXt6oMFSTc7eFwZbhADO+BJJ6+U1WCKNPtiHtn5TKQpRw15R4mGqdcDs28wopbfVOri9 6v1IVhsEOGHWfhP7tjmKbfxtFpR8t06lJstpN70hvJ98eFT1eChbIvue+AgH3kbvF0pE Z+ZQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=wm/Q9vJ2tas3K3rcjLYa2PyWbgbTw72GxteCLrujtIU=; fh=J5iHzZLtoblasp7/xMmxwL9qWaol2dI/5Vhx8uk1IVg=; b=PPAwJmZbT/6SoL+P+6FMrgK43bcYGoJBuLtiRC3oH32OVa0qVl9ZCkhyg04szFzeeX u6hDR7vCIJGhVJrO519zhVIhuQxJrmB3zz9dSlJNz1aw02KtmsphJLuWZigSnQGPLEVW yR4O+rhl1mOqW0O4iw0Mko0zFJt7Y1fuux1+k4PmAuJBGnspZNjpurmCZEmQLBXX219o B46rpdCVBw2G0o2LnWRblekhH615UqidbXmL9hy6AXPVu+3nLdoHwWbuLlfQ7HX7hU/P p+fmMgYSZR9ovy0nPc9ziW8Hj0PU+4BQhkfaS+8VrreEpUxWfuzOeCJi+afHZ7NKh+uy nE2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r9WbdCsY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id q22-20020a056a00151600b0068a5877bfaesi8160166pfu.382.2023.09.24.06.19.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Sep 2023 06:19:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r9WbdCsY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id DD63680BE0BE; Sun, 24 Sep 2023 06:17:39 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230176AbjIXNRY (ORCPT + 99 others); Sun, 24 Sep 2023 09:17:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230091AbjIXNQx (ORCPT ); Sun, 24 Sep 2023 09:16:53 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC2DC1712; Sun, 24 Sep 2023 06:16:13 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A2A5C433D9; Sun, 24 Sep 2023 13:16:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695561370; bh=x/nzVIl2gyab2szhiXSwlJxDiAJS0+cM22APpBMLpWw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r9WbdCsYRg/r3dpMBafKadBK/68e5SzBABFciUk01lM7paNHf/SI0JAICisPUwpUI bjdxL32SIrAeoLZyjTPF5hMTiFSbYJ8iRkgdl8SAV0quMwvvPqHzZ5/Gjz+jI31meI 3xlHf13cq4EeJMHB+FSOk5OshPmTO5J4Z7s7S/L8mZLmDP+H+6KbO1KO8nRlVRVNAh ZU8T4ABxiJj13EVB2So9tTCjwwmGVxqw6ZZwJB6Clm1Z8gN8XcfuSlhwWbMHWdCh8n BE3jRx7sWDhRjVfQZoOMW943fLQKYodw35MJMABIjpaWbBTS/XmtGVEcKtqupFVHr5 cD9hwtkUHaqvQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: "Steven Rostedt (Google)" , Masami Hiramatsu , Mark Rutland , Tze-nan Wu , Sasha Levin , matthias.bgg@gmail.com, linux-trace-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org Subject: [PATCH AUTOSEL 6.5 17/41] ring-buffer: Do not attempt to read past "commit" Date: Sun, 24 Sep 2023 09:15:05 -0400 Message-Id: <20230924131529.1275335-17-sashal@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230924131529.1275335-1-sashal@kernel.org> References: <20230924131529.1275335-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.5.5 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Sun, 24 Sep 2023 06:17:40 -0700 (PDT) From: "Steven Rostedt (Google)" [ Upstream commit 95a404bd60af6c4d9d8db01ad14fe8957ece31ca ] When iterating over the ring buffer while the ring buffer is active, the writer can corrupt the reader. There's barriers to help detect this and handle it, but that code missed the case where the last event was at the very end of the page and has only 4 bytes left. The checks to detect the corruption by the writer to reads needs to see the length of the event. If the length in the first 4 bytes is zero then the length is stored in the second 4 bytes. But if the writer is in the process of updating that code, there's a small window where the length in the first 4 bytes could be zero even though the length is only 4 bytes. That will cause rb_event_length() to read the next 4 bytes which could happen to be off the allocated page. To protect against this, fail immediately if the next event pointer is less than 8 bytes from the end of the commit (last byte of data), as all events must be a minimum of 8 bytes anyway. Link: https://lore.kernel.org/all/20230905141245.26470-1-Tze-nan.Wu@mediatek.com/ Link: https://lore.kernel.org/linux-trace-kernel/20230907122820.0899019c@gandalf.local.home Cc: Masami Hiramatsu Cc: Mark Rutland Reported-by: Tze-nan Wu Signed-off-by: Steven Rostedt (Google) Signed-off-by: Sasha Levin --- kernel/trace/ring_buffer.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 1267e1016ab5c..53b73b85cf737 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -2398,6 +2398,11 @@ rb_iter_head_event(struct ring_buffer_iter *iter) */ commit = rb_page_commit(iter_head_page); smp_rmb(); + + /* An event needs to be at least 8 bytes in size */ + if (iter->head > commit - 8) + goto reset; + event = __rb_page_index(iter_head_page, iter->head); length = rb_event_length(event); -- 2.40.1