From patchwork Fri Dec 16 20:30:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 34083 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp1183899wrn; Fri, 16 Dec 2022 12:36:28 -0800 (PST) X-Google-Smtp-Source: AA0mqf6lyvhLJJzVOOZzjAWI1dGeTPM9LP02/boyY0xL8aUlQ3zlBBtihjyeOtv68xL6Vun79uwW X-Received: by 2002:a05:6402:1f8e:b0:45c:835c:eab6 with SMTP id c14-20020a0564021f8e00b0045c835ceab6mr31474537edc.36.1671222988208; Fri, 16 Dec 2022 12:36:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671222988; cv=none; d=google.com; s=arc-20160816; b=tIbZufY8PdOG7y2mXO1M5N4IVLWOb8y5C6FDdjdvB8jUFIXYcY2whFKxn7G8xmGGsY 5rjDWmVBM9LXn/YxWMvWOKgp+NSvlRroXPW4SADc6GCNE9Azc8wFajlIdqsB9W9G018T xn/kGWObXTkB0jUW198KsZ2/YLXOo66VszKLmgeEJ2qYlr3XxbffcdhOIKeLwh2yrY78 rzvBcdxNgPg++Gfgb065ckYTrc4DimuZzrbUmotIrxGQmKS5aLRGWvs5kaE5Lz+v/ulv Poov/6WPeBCJvC1Fc4JZmRh99D1dOPzCN1wuNp3DWvyO80FkqEcSpbxdBl8XhmtfcPQ7 Nuvw== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=rhdvcMuCK8cVd24YqXpIq4X0YsKEOwsG/ikZkxQ8Me8=; b=IhlgY9Eqabhytg6E1mMCMuvKmz+snVikbvmneZUQlkFPy61mNEOJI5+RCKPf9YCce8 aoYiub5N8eyz2MhdWOo/ULPX0vdm2OXojPWqUgkVKt9CbipwqkfNUT0NHeInQRvYvzdq uFqsULVD5OUCK2gSi/u38d76j9fKF1Hcnztf+vQgXF4z5b2D3HxyUdQX43Wn6/I+gdFk XWNujUTjcQAfbzKGRK4qj4T/YUULqf6aLKVrli1Y4Cjl64CdTxOSLuiFBapz10o8238A QsuB8/S4TVaa7NSS1mMOUm6qRUx3T2BpxhM30l9r1Xcd3pgV/vYRYO6dLSbHgzip8tLt /+iQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Td09ZtE0; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b11-20020a056402278b00b004619867344csi4170212ede.0.2022.12.16.12.36.05; Fri, 16 Dec 2022 12:36:28 -0800 (PST) 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=@chromium.org header.s=google header.b=Td09ZtE0; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231741AbiLPUaf (ORCPT + 99 others); Fri, 16 Dec 2022 15:30:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231515AbiLPUad (ORCPT ); Fri, 16 Dec 2022 15:30:33 -0500 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 300723EAC7 for ; Fri, 16 Dec 2022 12:30:32 -0800 (PST) Received: by mail-pl1-x62d.google.com with SMTP id d3so3396416plr.10 for ; Fri, 16 Dec 2022 12:30:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=rhdvcMuCK8cVd24YqXpIq4X0YsKEOwsG/ikZkxQ8Me8=; b=Td09ZtE0IX5Fvm5IdQS1h9XVZyhi2weGB+7yVfa+EFDjgIFxxqIc4OAqCKlJ5ruqyX XNdCAR9FhchU3lupE56ZkZu2lHS9qCrUOAgQ2mmpo7ZbPwgKW01wGuQe0mVi6I3ha7Rt XU1UFK91y4FzgunLMUSG7yQB9W+9rQCHddMg0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rhdvcMuCK8cVd24YqXpIq4X0YsKEOwsG/ikZkxQ8Me8=; b=yMyG/1GmKCquZobHfhbqJ58IQFpsfVF2eB28ndi/AQx/apDKixnEeguY81DO4Wlran 3tok5/MCYBiIQ/2WMlrjf2CtS43nKDpnRWKrBfsNO7bo2zAAyHcpMeIUcjXFpGNJsrP0 X5Hs2uHMt8zhEXqSa3KDDBWKiyoM49yBy0w/tEHe+zPb0cswO5uCGMM3hH495/2RYlko ZLS3D7V0o//9607tQ76VIKg8BwTq/Zog+tUlUGumW7IdVLxguVkMR9t7Ki5fr8Moeo1t eWJy2VrZ2TJif5HIZ02SyyBKVBerPLPczj8IhalnFwD4OruBZ1WL8YSGd1nct/jhOfY8 7ftA== X-Gm-Message-State: ANoB5plGVHbZ5LsJmL36M2llwBExDaYMGC56MNmAiOe5w3606KujcwBq 1OKatbiif7q/ViIN5nf1aYv0PQ== X-Received: by 2002:a17:90a:4f4b:b0:219:15b0:3da3 with SMTP id w11-20020a17090a4f4b00b0021915b03da3mr33713864pjl.37.1671222631618; Fri, 16 Dec 2022 12:30:31 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id p15-20020a17090a348f00b0021952b5e9bcsm5170108pjb.53.2022.12.16.12.30.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Dec 2022 12:30:31 -0800 (PST) From: Kees Cook To: Peter Zijlstra Cc: Kees Cook , "Eric W. Biederman" , Jann Horn , Arnd Bergmann , Petr Mladek , Andrew Morton , Luis Chamberlain , Marco Elver , tangmeng , Sebastian Andrzej Siewior , Tiezhu Yang , Stafford Horne , "Guilherme G. Piccoli" , John Ogness , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH] exit: Use READ_ONCE() for all oops/warn limit reads Date: Fri, 16 Dec 2022 12:30:28 -0800 Message-Id: <20221216203024.never.640-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2639; h=from:subject:message-id; bh=QFDv42gSw8RrL/kT/bLCpkDTyQ5e1mjGxtt244QW9V0=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjnNVkYwcCuX3+lRp4U1y4bVoF7qP2EdUuq4JMMDD2 0AFmIzyJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY5zVZAAKCRCJcvTf3G3AJlNcD/ 0QvyfFKYGmpjzAId3QIjrakY8NxwFTZRZWL6SjPD22tFgR90XCdBibLo6mbOWrszOVuhsYlDrc2/Tn jxZqKMuAVbU8ybDSpXio897z7ooUsCJe6RX3IKjAJ0M0ToVvnsY9o5k1VleYRZJH0FsIN+LQrda8Yh qClm/+UwCGCJV+mYCiLGqB9F/Bs2oLxUgdfOk0PBpNAewEVz46tTlLj/me8beXXtABSMY595LjRDVr zgUb5LaPaYYIGwksz33bGSsHRI+lyI05RWuG+DGLz5xHfTQtlz/zONesrX0Q8pAiqkVkoSz1qjXJEF h85OXdmGjwEQxQG+JXTySbrhnbsRg3kOTjHuiJKs93On3G2M9KFSC2vAWhENdi/zPTnGR8iyNg8UrU NmHvOIeTKSkRGu2Sr6zkhwWhAa76SqO+YGU4AtuFA7UGK2Xwuaq3YmQTvZENJyBojoRoley2Oc4n/4 Phu3zm03tPfE7mTAfowjhuKKvhEBOF54gnyP3sIrZ0dLcQ9aP1sbb2KpDaPA4pWWEQAah2OCjELhh6 ke4JhhJ/QoXAZ9w+dpjLUgniEDtnZSPvfMnDhWAn1RBfj06+om5o4RcdrjiOqcwxnyVQkDhzVk2vBq 8f2bsA4MpSbk+OhcluHRTvpxuVCD5apQLNDcCcoNYoIR5HI8bTiOUt2OXrQA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752404316152074341?= X-GMAIL-MSGID: =?utf-8?q?1752404316152074341?= Use a temporary variable to take full advantage of READ_ONCE() behavior. Without this, the report (and even the test) might be out of sync with the initial test. Reported-by: Peter Zijlstra Link: https://lore.kernel.org/lkml/Y5x7GXeluFmZ8E0E@hirez.programming.kicks-ass.net Fixes: 9fc9e278a5c0 ("panic: Introduce warn_limit") Fixes: d4ccd54d28d3 ("exit: Put an upper limit on how often we can oops") Cc: "Eric W. Biederman" Cc: Jann Horn Cc: Arnd Bergmann Cc: Petr Mladek Cc: Andrew Morton Cc: Luis Chamberlain Cc: Marco Elver Cc: tangmeng Cc: Sebastian Andrzej Siewior Cc: Tiezhu Yang Signed-off-by: Kees Cook Acked-by: Peter Zijlstra (Intel) --- kernel/exit.c | 6 ++++-- kernel/panic.c | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/kernel/exit.c b/kernel/exit.c index deffb8e4b1b2..15dc2ec80c46 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -931,6 +931,7 @@ void __noreturn make_task_dead(int signr) * Then do everything else. */ struct task_struct *tsk = current; + unsigned int limit; if (unlikely(in_interrupt())) panic("Aiee, killing interrupt handler!"); @@ -954,8 +955,9 @@ void __noreturn make_task_dead(int signr) * To make sure this can't happen, place an upper bound on how often the * kernel may oops without panic(). */ - if (atomic_inc_return(&oops_count) >= READ_ONCE(oops_limit) && oops_limit) - panic("Oopsed too often (kernel.oops_limit is %d)", oops_limit); + limit = READ_ONCE(oops_limit); + if (atomic_inc_return(&oops_count) >= limit && limit) + panic("Oopsed too often (kernel.oops_limit is %d)", limit); /* * We're taking recursive faults here in make_task_dead. Safest is to just diff --git a/kernel/panic.c b/kernel/panic.c index 54deb743b2d5..7834c9854e02 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -231,12 +231,15 @@ static void panic_print_sys_info(bool console_flush) void check_panic_on_warn(const char *origin) { + unsigned int limit; + if (panic_on_warn) panic("%s: panic_on_warn set ...\n", origin); - if (atomic_inc_return(&warn_count) >= READ_ONCE(warn_limit) && warn_limit) + limit = READ_ONCE(warn_limit); + if (atomic_inc_return(&warn_count) >= limit && limit) panic("%s: system warned too often (kernel.warn_limit is %d)", - origin, warn_limit); + origin, limit); } /**