Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp7974122rdb; Thu, 4 Jan 2024 14:03:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IGT4CqdTDtx0kCPJXoS4jiGmVCcNRDyFkjy2r4Bx7FK6GIHIDyAkvnKCkNwlTKbncZt1+Hf X-Received: by 2002:a17:906:d14d:b0:a28:7f93:56e7 with SMTP id br13-20020a170906d14d00b00a287f9356e7mr351330ejb.173.1704405839671; Thu, 04 Jan 2024 14:03:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704405839; cv=none; d=google.com; s=arc-20160816; b=bbOIrPhd3oHvZWv1HBeviDxmvp6dY8wwP7bhloXxXuxtl6xofRRCikOkNTPT9XRGPf sCC9WMRVGIM/bGlFMOSbaVi/lekkg8+R6glzspI/5n5gheJkh+80DbtGWX0+XZJZ6QkP k3lZIYQlBvkPwXB0+W50AyqRoeATJZGZPerWyvBCQmVlXdJ6dHktNDfR5DzOIkl0y+FH U/oUYvghhBJiDQc+iJm+eQTmdq25WYcR9ysvD5yEJrGy+f4WoLnmvNx+c+lDgfNlDs1v DSTGF0U5akCS5mN6PnRQSWAZy+vuEInQlmRGbRsjXkmLle0yYCAI8FZn/1IM8COE2GKl 1Nvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=gXXy4XaMx73PQXHAjdjEv87MNa8ZHKFz0AC7Uv3YPaw=; fh=WRV0kXYUzbJrUXoN2KHFppUuf9jeqWrbcazm29c50Fk=; b=jVrzX5unwEb57+V6TgQPyYSroFyXI9qW9AVJxzaQ9C3VB9f2b0zdxSGE3tV3Xj80pa WzB3o/7LN4zMwnSHZe+ZJKEMEOy/MHzSBHIDb8EgSUfPJqe59HVQaT8Cyt4i7oQB6B61 BHmU0lqqYMn71TQO8+JSUPNVDymq8eDJ6tuqQHmvwgBVmBD/cB37vN/sFOIte1DDD5Fc hLHU0lLpQRJ6vjmOtQ9OKFKH6WS2GK4tdludw3exDhXLteymBFIGpOuqE4znBqtwcCvb KiivqY4x2XvLwDmLC6ZwcQt+E45wzTA4hhaEhy8k+fJbKvSB6IPhWccKliH+Q990Rfvb QHHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Oy1rGAQ9; spf=pass (google.com: domain of linux-kernel+bounces-17270-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17270-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id bh20-20020a170906a0d400b00a26f5e38a09si84683ejb.892.2024.01.04.14.03.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 14:03:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-17270-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Oy1rGAQ9; spf=pass (google.com: domain of linux-kernel+bounces-17270-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17270-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 446971F21602 for ; Thu, 4 Jan 2024 22:03:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 47DBB2C854; Thu, 4 Jan 2024 22:03:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Oy1rGAQ9" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D6CF32C84F; Thu, 4 Jan 2024 22:03:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-3368b9bbeb4so850654f8f.2; Thu, 04 Jan 2024 14:03:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704405826; x=1705010626; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=gXXy4XaMx73PQXHAjdjEv87MNa8ZHKFz0AC7Uv3YPaw=; b=Oy1rGAQ9x5vGN+PTD56sTjjKl5Qtaphvt2i6xQ+KZybN59gs+P8g+ph9qClImdqTWU N9mljp8oeXG2I9DN9lsmgJ8oRx7AnP7dl0OAfqRupOcEFRjp2Vsc5Bb///fz4Uu5ePsY pLZKhrS/fcKFPIO8XOK85m+EB6u8DT3Z/x4uHSFxEPh8MA4V81Xgs7YITxwbyjL4ah1i qYf8XFyKwKibTfVurlGuJiAiddz10HRz6IaXiz5C5uRGQUE0EgUaWoFDT8dBnyzdWNAp Yk2CtEtcy0EIbjlg2bt6OvNZmXn9gNB+XwU8XT3TTq/IGKtJdIpS58XN/8MxP+s9UxLM XpUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704405826; x=1705010626; 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=gXXy4XaMx73PQXHAjdjEv87MNa8ZHKFz0AC7Uv3YPaw=; b=JXKadd+tDWaBCHs+9foIhIQ7y227Xu2yz5GZw+vg35g11pZMOBPZvx6tiNyGLDUSMk QQf+AiVwtLEk9QeXzg4bLvV8uqaerVisk2Xl6CGPhDFfl1Z4jwfrF4oLp30wh2PpZcUX RNG8poBehpe3VF2rMwfXTS2IyuXg6pHIygvZvKDu5L9EH/mMOoTzEQn6ZX8wB/SY5sFG qS6f98olB61peZ/QlbSYJUYFJah3x6mk6xWEFC3yeDb0vjYDFwGtPA7Wy0q2IL1Hkwc7 997g3QMiLzzDDZ22M6vV2gfAXdGkwH+mGdA+7GKcf6yKeyVa5MHqRvMoLs6NYJoJ7bhJ SGeg== X-Gm-Message-State: AOJu0Yzo4pfKh70OIuyaUqykyH3NlfXVBNCu+1+/hcFxeVa6P+7qeDcm XLYHoiHetJVtJLYDS6crCak= X-Received: by 2002:a5d:6b12:0:b0:336:62f7:720f with SMTP id v18-20020a5d6b12000000b0033662f7720fmr299156wrw.160.1704405825822; Thu, 04 Jan 2024 14:03:45 -0800 (PST) Received: from localhost.localdomain (host-80-116-159-187.pool80116.interbusiness.it. [80.116.159.187]) by smtp.googlemail.com with ESMTPSA id e18-20020adfe7d2000000b003367a5b6b69sm164286wrn.106.2024.01.04.14.03.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 14:03:45 -0800 (PST) From: Christian Marangi To: MyungJoo Ham , Kyungmin Park , Chanwoo Choi , Christian Marangi , Jonghwa Lee , linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org Cc: stable@vger.kernel.org Subject: [RESEND PATCH 1/2] PM / devfreq: Fix buffer overflow in trans_stat_show Date: Thu, 4 Jan 2024 22:55:14 +0100 Message-ID: <20240104215521.10772-1-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Fix buffer overflow in trans_stat_show(). Convert simple snprintf to the more secure scnprintf with size of PAGE_SIZE. Add condition checking if we are exceeding PAGE_SIZE and exit early from loop. Also add at the end a warning that we exceeded PAGE_SIZE and that stats is disabled. Return -EFBIG in the case where we don't have enough space to write the full transition table. Also document in the ABI that this function can return -EFBIG error. Cc: stable@vger.kernel.org Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218041 Fixes: e552bbaf5b98 ("PM / devfreq: Add sysfs node for representing frequency transition information.") Signed-off-by: Christian Marangi --- Documentation/ABI/testing/sysfs-class-devfreq | 3 + drivers/devfreq/devfreq.c | 57 +++++++++++++------ 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-class-devfreq b/Documentation/ABI/testing/sysfs-class-devfreq index 5e6b74f30406..1e7e0bb4c14e 100644 --- a/Documentation/ABI/testing/sysfs-class-devfreq +++ b/Documentation/ABI/testing/sysfs-class-devfreq @@ -52,6 +52,9 @@ Description: echo 0 > /sys/class/devfreq/.../trans_stat + If the transition table is bigger than PAGE_SIZE, reading + this will return an -EFBIG error. + What: /sys/class/devfreq/.../available_frequencies Date: October 2012 Contact: Nishanth Menon diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c index 63347a5ae599..8459512d9b07 100644 --- a/drivers/devfreq/devfreq.c +++ b/drivers/devfreq/devfreq.c @@ -1688,7 +1688,7 @@ static ssize_t trans_stat_show(struct device *dev, struct device_attribute *attr, char *buf) { struct devfreq *df = to_devfreq(dev); - ssize_t len; + ssize_t len = 0; int i, j; unsigned int max_state; @@ -1697,7 +1697,7 @@ static ssize_t trans_stat_show(struct device *dev, max_state = df->max_state; if (max_state == 0) - return sprintf(buf, "Not Supported.\n"); + return scnprintf(buf, PAGE_SIZE, "Not Supported.\n"); mutex_lock(&df->lock); if (!df->stop_polling && @@ -1707,31 +1707,52 @@ static ssize_t trans_stat_show(struct device *dev, } mutex_unlock(&df->lock); - len = sprintf(buf, " From : To\n"); - len += sprintf(buf + len, " :"); - for (i = 0; i < max_state; i++) - len += sprintf(buf + len, "%10lu", - df->freq_table[i]); + len += scnprintf(buf + len, PAGE_SIZE - len, " From : To\n"); + len += scnprintf(buf + len, PAGE_SIZE - len, " :"); + for (i = 0; i < max_state; i++) { + if (len >= PAGE_SIZE - 1) + break; + len += scnprintf(buf + len, PAGE_SIZE - len, "%10lu", + df->freq_table[i]); + } + if (len >= PAGE_SIZE - 1) + return PAGE_SIZE - 1; - len += sprintf(buf + len, " time(ms)\n"); + len += scnprintf(buf + len, PAGE_SIZE - len, " time(ms)\n"); for (i = 0; i < max_state; i++) { + if (len >= PAGE_SIZE - 1) + break; if (df->freq_table[i] == df->previous_freq) - len += sprintf(buf + len, "*"); + len += scnprintf(buf + len, PAGE_SIZE - len, "*"); else - len += sprintf(buf + len, " "); + len += scnprintf(buf + len, PAGE_SIZE - len, " "); + if (len >= PAGE_SIZE - 1) + break; + + len += scnprintf(buf + len, PAGE_SIZE - len, "%10lu:", + df->freq_table[i]); + for (j = 0; j < max_state; j++) { + if (len >= PAGE_SIZE - 1) + break; + len += scnprintf(buf + len, PAGE_SIZE - len, "%10u", + df->stats.trans_table[(i * max_state) + j]); + } + if (len >= PAGE_SIZE - 1) + break; + len += scnprintf(buf + len, PAGE_SIZE - len, "%10llu\n", (u64) + jiffies64_to_msecs(df->stats.time_in_state[i])); + } - len += sprintf(buf + len, "%10lu:", df->freq_table[i]); - for (j = 0; j < max_state; j++) - len += sprintf(buf + len, "%10u", - df->stats.trans_table[(i * max_state) + j]); + if (len < PAGE_SIZE - 1) + len += scnprintf(buf + len, PAGE_SIZE - len, "Total transition : %u\n", + df->stats.total_trans); - len += sprintf(buf + len, "%10llu\n", (u64) - jiffies64_to_msecs(df->stats.time_in_state[i])); + if (len >= PAGE_SIZE - 1) { + pr_warn_once("devfreq transition table exceeds PAGE_SIZE. Disabling\n"); + return -EFBIG; } - len += sprintf(buf + len, "Total transition : %u\n", - df->stats.total_trans); return len; } -- 2.43.0