Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2504979ybb; Mon, 30 Mar 2020 07:28:14 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsghGLbdsFt1UtfJtlmoGOXjnjWlhkRevUnYHcJUcEYOix+ZxR8NPgJv61KbhjmxA0hARJx X-Received: by 2002:aca:f254:: with SMTP id q81mr7771414oih.12.1585578494346; Mon, 30 Mar 2020 07:28:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585578494; cv=none; d=google.com; s=arc-20160816; b=h9tGV0EOoGQFV2lIFKAXf9Gfrn5EmjTpy+MMVEAp4OatyUk/nh18F2ctPmeQbodeYd bzniGhQzVvoy2rXdVdZnH+UZq6NRdbHmgnRR5HmO00rxvPz9nRenVZbkfQev+IRmx7h9 owEkqZ3DKerV7EyIq4IiqZAJJGzhEwOWy72ZuTW4ZVkFMTex0Vo3YQ0gBxZjNvmh1gTb vgVcYzyXHNXBIFaxkecaNpPVzmvyun7P16uxmkHbfGZuDEjD/s14s0LCVHeW0FgDYVs9 BqaAxHAvht3jrXulkZVyJ5Q6pW4uPR6ne0eJvLzTfALsuTutu1z4g9PWT4qDGYig6tX7 EtUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=/JqkS5X7283s35wDGYZ8zY0lp3qTEkxpFY2sfArhUNA=; b=B/RNWiBU+cwcESTlsOJy6jrDc722f0AV1PlAEsBo6jwC/YcwPneQw/S3dw4gnU2ZWN PE2JWRc8vkUFEOX3cFPt5oXOZy/wRMw0piPDgKH5Zvr3IZnt+oVVChVaga+NqoyY3rvK PC+ChNo9mVqbdSjZEiWvdiiE0Copc7CItaGa37LpNN8hJlrnoDS6McTjIj/v5dVrT0ia gz7qJZ141wPjRCyWo9aisTM8iDYn6/GMkD/2yuWNSFH/ae0+k7jNhVc77MYjOYL88AlW DWxW+PLuaT+AsuScW+MGjUszNYdEGP6ifm5IxQ2XRO+kMV1jcmv9sLRdeYZx/E+jPNkL BScA== 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 y6si5892843ooj.61.2020.03.30.07.28.00; Mon, 30 Mar 2020 07:28:14 -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 S1727905AbgC3OJD (ORCPT + 99 others); Mon, 30 Mar 2020 10:09:03 -0400 Received: from mx2.suse.de ([195.135.220.15]:42908 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725268AbgC3OJC (ORCPT ); Mon, 30 Mar 2020 10:09:02 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 25303AC5F; Mon, 30 Mar 2020 14:09:01 +0000 (UTC) From: Takashi Iwai To: Zhang Rui , Daniel Lezcano Cc: Amit Kucheria , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] thermal: Add a sanity check for invalid state at stats update Date: Mon, 30 Mar 2020 16:08:59 +0200 Message-Id: <20200330140859.12535-1-tiwai@suse.de> X-Mailer: git-send-email 2.16.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The thermal sysfs handler keeps the statistics table with the fixed size that was determined from the initial max_states() call, and the table entry is updated at each sysfs cur_state write call. And, when the driver's set_cur_state() ops accepts the value given from user-space, the thermal sysfs core blindly applies it to the statistics table entry, which may overflow and cause an Oops. Although it's rather a bug in the driver's ops implementations, we shouldn't crash but rather give a proper warning instead. This patch adds a sanity check for avoiding such an OOB access and warns with a stack trace to show the suspicious device in question. Signed-off-by: Takashi Iwai --- We've hit some crash by stress tests, and this patch at least works around the crash itself. While the actual bug fix of the buggy driver is still being investigated, I submit the hardening in the core side at first. drivers/thermal/thermal_sysfs.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c index aa99edb4dff7..a23c4e701d63 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -772,6 +772,11 @@ void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev, spin_lock(&stats->lock); + if (dev_WARN_ONCE(&cdev->device, new_state >= stats->max_states, + "new state %ld exceeds max_state %ld", + new_state, stats->max_states)) + goto unlock; + if (stats->state == new_state) goto unlock; -- 2.16.4