Received: by 2002:a05:7412:8d23:b0:f7:29d7:fb05 with SMTP id bj35csp182733rdb; Sat, 16 Dec 2023 05:02:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IGi2HJGZ+1LqNkK/QkUcGIAoYrIpddDmB3gFaClGbWdKAWP1eBvwHBaIqgnFRIhgSlZj24v X-Received: by 2002:a05:6402:2152:b0:54c:7994:ac02 with SMTP id bq18-20020a056402215200b0054c7994ac02mr7732844edb.33.1702731779407; Sat, 16 Dec 2023 05:02:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702731779; cv=none; d=google.com; s=arc-20160816; b=LHyK0fHZYZVLRVShz70WLtmB95XBNNoRBgz8frAJ6ADtFSEzPvz+y7cKHvm6F43b09 qkm9pzWtRXCvwsMENiDc0yvMspBYQYa1YT189Qfsywva2VhqJncK1yfbo3hHayUMWs7a 7vbZxiZ06tayL8b3Q8U4kcF8XoGPg8UNQ+ww78QzHEwAChb7La6U/IXKUP3hU0P3DP7m F0cxDrp38VCwDNNP7udPJ7arAHAh79RkI/MUzw6LFTbhbWE47Aco+pV9SLK934gePKv2 G5E27HyNrPDYRMRdJIPcBjKZ1OH2+EyACqap/U/wQccjKHMBbbuxVB6VE3crxnq8nSyL +wgw== 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=Fa9yeiiOVaCIdEktKvlcN06YmNgKcMNBNVzgkivBQlM=; fh=/kmYZi0URd93NFxYVLlfLY7FRSbH2YFUg0I3NW39kBI=; b=s11taOLVVcCibeSkndsHTxG5N81e3zpA1YeKgzx9Z47G5y+OG9BWutljlyCdsZlxvk PgvJLgi27s30mLc2bV9glz9b1GXtCUopl+IorzJvl376sB6DVXHcf27ek2lD+wzVaeQ1 yDHsLLq9cmTOF0/VEDFOUnzcN9QxY4jdthsaDqif+KzWKSGSKlGIol/uVE/JX2NorMMT 3PzIBHmWhjqSwV6n3qpn3Ly68fRB77FUuYbFARSefH2BgyB/PcK0sC5R31o+XMYrvsMD BN23W7e7IuihXLuSTNkMg1Nj/u5gzqNvLzIpwOvFPAR+Bw3udvps/7BKGKvZup/xsgeb Z0dQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=dUmPu60U; spf=pass (google.com: domain of linux-kernel+bounces-2193-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2193-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id v25-20020a50a459000000b0054ce2f143bfsi8277856edb.498.2023.12.16.05.02.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 05:02:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2193-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=@wanadoo.fr header.s=t20230301 header.b=dUmPu60U; spf=pass (google.com: domain of linux-kernel+bounces-2193-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2193-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr 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 2C9C51F23BA5 for ; Sat, 16 Dec 2023 13:02:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 64D38288AE; Sat, 16 Dec 2023 13:02:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=wanadoo.fr header.i=@wanadoo.fr header.b="dUmPu60U" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.smtpout.orange.fr (smtp-30.smtpout.orange.fr [80.12.242.30]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2406830351 for ; Sat, 16 Dec 2023 13:02:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=wanadoo.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=wanadoo.fr Received: from pop-os.home ([92.140.202.140]) by smtp.orange.fr with ESMTPA id EUC8rL1Hz45wtEUC8rBRUA; Sat, 16 Dec 2023 13:55:05 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1702731305; bh=Fa9yeiiOVaCIdEktKvlcN06YmNgKcMNBNVzgkivBQlM=; h=From:To:Cc:Subject:Date; b=dUmPu60UhSREhpVkcpZzDVQ689zSfTU3NlsfqjtfvupSWZEOVLUDCpxKto0CSkdCS 5Nnz1O4jZqfqJyS7xA65N7xp1/kmUYljw3x+tt45Q+MXE5cg9EsqdE9dgDSxhtWDXR 5ppLEBsLOB+DvYlZOVH7klV4W/NwDXWvYLa0by1VEYun4K9rtAN1nBgZwQPFtY2Y8Q 5RExd/d6mlgz104tZhfylYj2bgUpalB5Vc4RRgn1wUZ7H3/1NZYhjrRX5EdgAR2Ejd fw1JpwvUDvlszECy2petjXjIxYPQY4hb9TJES0C/R4AbPDK6PRLg1/a0xsKLhE9R3H X/1OmkPCMQwqA== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sat, 16 Dec 2023 13:55:05 +0100 X-ME-IP: 92.140.202.140 From: Christophe JAILLET To: Tejun Heo , Josef Bacik , Jens Axboe , Shaohua Li Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , cgroups@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH] blk-throttle: Fix some potential string truncation in tg_prfill_limit() Date: Sat, 16 Dec 2023 13:54:56 +0100 Message-Id: <0461f1d69c84cf5a98ae57012856dace757d319e.1702731206.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When compiled with W=1, we get: block/blk-throttle.c: In function ‘tg_prfill_limit’: block/blk-throttle.c:1539:74: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation=] 1539 | snprintf(idle_time, sizeof(idle_time), " idle=%lu", | ^ block/blk-throttle.c:1539:25: note: ‘snprintf’ output between 8 and 27 bytes into a destination of size 26 1539 | snprintf(idle_time, sizeof(idle_time), " idle=%lu", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1540 | tg->idletime_threshold_conf); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ block/blk-throttle.c:1546:43: error: ‘%lu’ directive output may be truncated writing between 1 and 20 bytes into a region of size 17 [-Werror=format-truncation=] 1546 | " latency=%lu", tg->latency_target_conf); | ^~~ block/blk-throttle.c:1546:33: note: directive argument in the range [0, 18446744073709551614] 1546 | " latency=%lu", tg->latency_target_conf); | ^~~~~~~~~~~~~~ block/blk-throttle.c:1545:25: note: ‘snprintf’ output between 11 and 30 bytes into a destination of size 26 1545 | snprintf(latency_time, sizeof(latency_time), | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1546 | " latency=%lu", tg->latency_target_conf); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In order to fix it, remove all the intermediate buffers and write directly into the 'sf' seq_file. Fixes: ada75b6e5b2a ("blk-throttle: add interface to configure idle time threshold") Fixes: ec80991d6fc2 ("blk-throttle: add interface for per-cgroup target latency") Signed-off-by: Christophe JAILLET --- block/blk-throttle.c | 52 +++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/block/blk-throttle.c b/block/blk-throttle.c index 16f5766620a4..470a8a4ed68e 100644 --- a/block/blk-throttle.c +++ b/block/blk-throttle.c @@ -1494,11 +1494,8 @@ static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd, { struct throtl_grp *tg = pd_to_tg(pd); const char *dname = blkg_dev_name(pd->blkg); - char bufs[4][21] = { "max", "max", "max", "max" }; u64 bps_dft; unsigned int iops_dft; - char idle_time[26] = ""; - char latency_time[26] = ""; if (!dname) return 0; @@ -1520,35 +1517,46 @@ static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd, tg->latency_target_conf == DFL_LATENCY_TARGET))) return 0; + seq_printf(sf, "%s", dname); + + seq_puts(sf, " rbps="); if (tg->bps_conf[READ][off] != U64_MAX) - snprintf(bufs[0], sizeof(bufs[0]), "%llu", - tg->bps_conf[READ][off]); + seq_printf(sf, "%llu", tg->bps_conf[READ][off]); + else + seq_puts(sf, "max"); + + seq_puts(sf, " wbps="); if (tg->bps_conf[WRITE][off] != U64_MAX) - snprintf(bufs[1], sizeof(bufs[1]), "%llu", - tg->bps_conf[WRITE][off]); + seq_printf(sf, "%llu", tg->bps_conf[WRITE][off]); + else + seq_puts(sf, "max"); + + seq_puts(sf, " riops="); if (tg->iops_conf[READ][off] != UINT_MAX) - snprintf(bufs[2], sizeof(bufs[2]), "%u", - tg->iops_conf[READ][off]); + seq_printf(sf, "%u", tg->iops_conf[READ][off]); + else + seq_puts(sf, "max"); + + seq_puts(sf, " wiops="); if (tg->iops_conf[WRITE][off] != UINT_MAX) - snprintf(bufs[3], sizeof(bufs[3]), "%u", - tg->iops_conf[WRITE][off]); + seq_printf(sf, "%u", tg->iops_conf[WRITE][off]); + else + seq_puts(sf, "max"); + if (off == LIMIT_LOW) { - if (tg->idletime_threshold_conf == ULONG_MAX) - strcpy(idle_time, " idle=max"); + seq_puts(sf, " idle="); + if (tg->idletime_threshold_conf != ULONG_MAX) + seq_printf(sf, "%lu", tg->idletime_threshold_conf); else - snprintf(idle_time, sizeof(idle_time), " idle=%lu", - tg->idletime_threshold_conf); + seq_puts(sf, "max"); - if (tg->latency_target_conf == ULONG_MAX) - strcpy(latency_time, " latency=max"); + seq_puts(sf, " latency="); + if (tg->latency_target_conf != ULONG_MAX) + seq_printf(sf, "%lu", tg->latency_target_conf); else - snprintf(latency_time, sizeof(latency_time), - " latency=%lu", tg->latency_target_conf); + seq_puts(sf, "max"); } - seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s%s%s\n", - dname, bufs[0], bufs[1], bufs[2], bufs[3], idle_time, - latency_time); return 0; } -- 2.34.1