Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2918416rwb; Mon, 15 Aug 2022 14:03:06 -0700 (PDT) X-Google-Smtp-Source: AA6agR7MOQKmnxZAeVAcSQMm2TRlzJRbYE1VE6k82tVmCxVFIRPFj76035j+yecv1urEDHJp9cK/ X-Received: by 2002:a05:6402:270c:b0:43d:efd3:883e with SMTP id y12-20020a056402270c00b0043defd3883emr16691135edd.221.1660597385909; Mon, 15 Aug 2022 14:03:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660597385; cv=none; d=google.com; s=arc-20160816; b=SxTvOR0BB4JvrfjHfGbRx2lZJ1+phnjy8/8nSSKpHXgpFq4/CZ1JdiHr5wtRLycpXG vpF4T5RFMrO86x/4AQ8sfbD7UcqeASO47z4k+tbMWmoRHF12h/dXBGHUyz/trXcMMzXS Y56Vzne/+YGhDmcYUO+FxKk5q7X861rppHYWtME/utENX+DJR+NNm6xjaW2J3GfhkyYZ xuBGBAOJzyt1ptkI9layGhyqG4xW0ys2UCP3KEmR5vY96q/4VGzPJZ2v8ydx9BivkpbM dg0i54LuJ6rXGdqP/GrjPgJYsRnEwscM7uiWHBKdlGO4G7wNi1VOomn8KQBCZANykxT8 WoQw== 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=7IvKoJp6UTKRxgk4YVEHVcNRtgEnkLDIHp8OcsyAxok=; b=e6b59toY/SicVYDuX0A+CQAAKT8Z5XPNgpBq9BLa00NfzChmlIgAtF+15nDkGnYj4m njO/DRJ5US1gDr1UcRXhYmEelAkOQzJPMDWnwqeHFvcMCUp+/FJ00szJJCxn6KHi++vK WR/KxK1pDqGSlxLrmwYi13pwfow8uhsVZNIMWsLVCrmTfMWlpau0qxHEZ4RMZQY1y7LU gpB1d+b+nw3tqnXG5r7mSKJyUB47Lpb01wWUhuDK1fZ2zGxEK/8Tz4zpTQmQpMBuFUe2 6/FR0TkKUDC2pa4aNPtxctQEr5rERnz5WcsbKiq7WmNGcS/RNXVBOKTfAlGAtt/TkbOE /UTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RKT5w103; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e11-20020a1709062c0b00b0073159d94151si6800454ejh.499.2022.08.15.14.02.38; Mon, 15 Aug 2022 14:03:05 -0700 (PDT) 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=@linuxfoundation.org header.s=korg header.b=RKT5w103; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233551AbiHOTox (ORCPT + 99 others); Mon, 15 Aug 2022 15:44:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344815AbiHOTlf (ORCPT ); Mon, 15 Aug 2022 15:41:35 -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 62615419BB; Mon, 15 Aug 2022 11:47:34 -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 6C6C4611EC; Mon, 15 Aug 2022 18:47:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 75D7EC433D6; Mon, 15 Aug 2022 18:47:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660589252; bh=xIKOzEbAHX9r+GH4VZhr1OOjANvkwjV7THn/VhsTL20=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RKT5w103BdBDpExVnAgPm2iaoU/qLGTdb3VAgvvtIzonL4hCqYC/58b4a+l+Xo5tR 6F5E2MTfxrxj7jDiGeUut5yZfwabwDFxC5jhUvy5rWzdyytdHqbm8yA2+JIUU9ocVC 5hl6DkAIo3osUNNvSMWFXS3G0Rm/0jnOpM6HVQFk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Florian Fainelli , "Rafael J. Wysocki" , Sasha Levin Subject: [PATCH 5.15 659/779] tools/thermal: Fix possible path truncations Date: Mon, 15 Aug 2022 20:05:03 +0200 Message-Id: <20220815180405.532688469@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 From: Florian Fainelli [ Upstream commit 6c58cf40e3a1d2f47c09d3489857e9476316788a ] A build with -D_FORTIFY_SOURCE=2 enabled will produce the following warnings: sysfs.c:63:30: warning: '%s' directive output may be truncated writing up to 255 bytes into a region of size between 0 and 255 [-Wformat-truncation=] snprintf(filepath, 256, "%s/%s", path, filename); ^~ Bump up the buffer to PATH_MAX which is the limit and account for all of the possible NUL and separators that could lead to exceeding the allocated buffer sizes. Fixes: 94f69966faf8 ("tools/thermal: Introduce tmon, a tool for thermal subsystem") Signed-off-by: Florian Fainelli Signed-off-by: Rafael J. Wysocki Signed-off-by: Sasha Levin --- tools/thermal/tmon/sysfs.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/tools/thermal/tmon/sysfs.c b/tools/thermal/tmon/sysfs.c index b00b1bfd9d8e..cb1108bc9249 100644 --- a/tools/thermal/tmon/sysfs.c +++ b/tools/thermal/tmon/sysfs.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -33,9 +34,9 @@ int sysfs_set_ulong(char *path, char *filename, unsigned long val) { FILE *fd; int ret = -1; - char filepath[256]; + char filepath[PATH_MAX + 2]; /* NUL and '/' */ - snprintf(filepath, 256, "%s/%s", path, filename); + snprintf(filepath, sizeof(filepath), "%s/%s", path, filename); fd = fopen(filepath, "w"); if (!fd) { @@ -57,9 +58,9 @@ static int sysfs_get_ulong(char *path, char *filename, unsigned long *p_ulong) { FILE *fd; int ret = -1; - char filepath[256]; + char filepath[PATH_MAX + 2]; /* NUL and '/' */ - snprintf(filepath, 256, "%s/%s", path, filename); + snprintf(filepath, sizeof(filepath), "%s/%s", path, filename); fd = fopen(filepath, "r"); if (!fd) { @@ -76,9 +77,9 @@ static int sysfs_get_string(char *path, char *filename, char *str) { FILE *fd; int ret = -1; - char filepath[256]; + char filepath[PATH_MAX + 2]; /* NUL and '/' */ - snprintf(filepath, 256, "%s/%s", path, filename); + snprintf(filepath, sizeof(filepath), "%s/%s", path, filename); fd = fopen(filepath, "r"); if (!fd) { @@ -199,8 +200,8 @@ static int find_tzone_cdev(struct dirent *nl, char *tz_name, { unsigned long trip_instance = 0; char cdev_name_linked[256]; - char cdev_name[256]; - char cdev_trip_name[256]; + char cdev_name[PATH_MAX]; + char cdev_trip_name[PATH_MAX]; int cdev_id; if (nl->d_type == DT_LNK) { @@ -213,7 +214,8 @@ static int find_tzone_cdev(struct dirent *nl, char *tz_name, return -EINVAL; } /* find the link to real cooling device record binding */ - snprintf(cdev_name, 256, "%s/%s", tz_name, nl->d_name); + snprintf(cdev_name, sizeof(cdev_name) - 2, "%s/%s", + tz_name, nl->d_name); memset(cdev_name_linked, 0, sizeof(cdev_name_linked)); if (readlink(cdev_name, cdev_name_linked, sizeof(cdev_name_linked) - 1) != -1) { @@ -226,8 +228,8 @@ static int find_tzone_cdev(struct dirent *nl, char *tz_name, /* find the trip point in which the cdev is binded to * in this tzone */ - snprintf(cdev_trip_name, 256, "%s%s", nl->d_name, - "_trip_point"); + snprintf(cdev_trip_name, sizeof(cdev_trip_name) - 1, + "%s%s", nl->d_name, "_trip_point"); sysfs_get_ulong(tz_name, cdev_trip_name, &trip_instance); /* validate trip point range, e.g. trip could return -1 -- 2.35.1