From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754468AbdGNOHE (ORCPT ); Fri, 14 Jul 2017 10:07:04 -0400 Received: from bh-25.webhostbox.net ([208.91.199.152]:55447 "EHLO bh-25.webhostbox.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754426AbdGNOHB (ORCPT ); Fri, 14 Jul 2017 10:07:01 -0400 Subject: Re: [PATCH 15/22] hwmon: applesmc: fix format string overflow To: Arnd Bergmann , linux-kernel@vger.kernel.org, Henrik Rydberg , Jean Delvare Cc: Greg Kroah-Hartman , Linus Torvalds , akpm@linux-foundation.org, netdev@vger.kernel.org, "David S . Miller" , "James E . J . Bottomley" , "Martin K . Petersen" , linux-scsi@vger.kernel.org, x86@kernel.org, linux-hwmon@vger.kernel.org References: <20170714120720.906842-1-arnd@arndb.de> <20170714120720.906842-16-arnd@arndb.de> From: Guenter Roeck Message-ID: Date: Fri, 14 Jul 2017 07:06:54 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <20170714120720.906842-16-arnd@arndb.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated_sender: linux@roeck-us.net X-OutGoing-Spam-Status: No, score=-1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - bh-25.webhostbox.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - roeck-us.net X-Get-Message-Sender-Via: bh-25.webhostbox.net: authenticated_id: linux@roeck-us.net X-Authenticated-Sender: bh-25.webhostbox.net: linux@roeck-us.net X-Source: X-Source-Args: X-Source-Dir: Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 07/14/2017 05:07 AM, Arnd Bergmann wrote: > gcc-7 warns that the key might exceed five bytes for lage index > values: > > drivers/hwmon/applesmc.c: In function 'applesmc_show_fan_position': > drivers/hwmon/applesmc.c:906:18: error: '%d' directive writing between 1 and 5 bytes into a region of size 4 [-Werror=format-overflow=] > sprintf(newkey, FAN_ID_FMT, to_index(attr)); > ^~~~~~~ > drivers/hwmon/applesmc.c:906:18: note: directive argument in the range [0, 65535] > drivers/hwmon/applesmc.c:906:2: note: 'sprintf' output between 5 and 9 bytes into a destination of size 5 > > As the key is required to be four characters plus trailing zero, > we know that the index has to be small here. I'm using snprintf() > to avoid the warning. This would truncate the string instead of > overflowing. > > Signed-off-by: Arnd Bergmann I submitted a more comprehensive patch a couple of days ago. There are other similar sprintf() calls in the driver which gcc doesn't report. Guenter > --- > drivers/hwmon/applesmc.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c > index 0af7fd311979..515163b9a89f 100644 > --- a/drivers/hwmon/applesmc.c > +++ b/drivers/hwmon/applesmc.c > @@ -903,7 +903,7 @@ static ssize_t applesmc_show_fan_position(struct device *dev, > char newkey[5]; > u8 buffer[17]; > > - sprintf(newkey, FAN_ID_FMT, to_index(attr)); > + snprintf(newkey, sizeof(newkey), FAN_ID_FMT, to_index(attr)); > > ret = applesmc_read_key(newkey, buffer, 16); > buffer[16] = 0; >