Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D23D0C10DCE for ; Sun, 15 Mar 2020 10:25:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9881C20578 for ; Sun, 15 Mar 2020 10:25:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728209AbgCOKZO (ORCPT ); Sun, 15 Mar 2020 06:25:14 -0400 Received: from mx2.suse.de ([195.135.220.15]:41692 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728141AbgCOKZN (ORCPT ); Sun, 15 Mar 2020 06:25:13 -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 1B92CAD48; Sun, 15 Mar 2020 10:25:12 +0000 (UTC) Date: Sun, 15 Mar 2020 11:25:11 +0100 Message-ID: From: Takashi Iwai To: Jonathan Cameron Cc: linux-iio@vger.kernel.org, Brian Masney Subject: Re: [PATCH 2/2] iio: tsl2772: Use scnprintf() for avoiding potential buffer overflow In-Reply-To: <20200315095834.76f50454@archlinux> References: <20200311074325.7922-1-tiwai@suse.de> <20200311074325.7922-3-tiwai@suse.de> <20200315095834.76f50454@archlinux> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/25.3 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org On Sun, 15 Mar 2020 10:58:34 +0100, Jonathan Cameron wrote: > > On Wed, 11 Mar 2020 08:43:25 +0100 > Takashi Iwai wrote: > > > Since snprintf() returns the would-be-output size instead of the > > actual output size, the succeeding calls may go beyond the given > > buffer limit. Fix it by replacing with scnprintf(). > > > > Signed-off-by: Takashi Iwai > > This one is printing a short well defined list of values. No way they go > anywhere near the smallest possible PAGE_SIZE buffer that it's printing > into. > > Which is handy given the remaining space isn't adjusted as we add items > to the string. Hence even with scnprintf it would overflow. Well, the issue handled here isn't whether the string is truncated. Rather whether the pointer passed to the function go beyond the boundary. In short, the call pattern like pos += snprintf(buf, limit - pos, ...); is wrong per design of snprintf(), unless you check the return value at each time. snprintf() doesn't return the actually truncated length but the length that would be. So, concatenating with snprintf() can go beyond the limit. Meanwhile, simply replacing it with scnprintf() avoids the wrong pointer increment effectively, as it returns the actual output size, so it'd return zero if it's all truncated. Practically seen, the current code should "work", but it's a wrong usage of snprintf(), as found in many other places. I've already submitted the patches to each subsystem to cover at least the concatenating cases like the above, and this is one of them. thanks, Takashi > > Brian, can you take a look at this when you get a moment? > > Thanks, > > Jonathan > > > --- > > drivers/iio/light/tsl2772.c | 4 ++-- > > 1 file changed, 2 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/iio/light/tsl2772.c b/drivers/iio/light/tsl2772.c > > index be37fcbd4654..44a0b56a558c 100644 > > --- a/drivers/iio/light/tsl2772.c > > +++ b/drivers/iio/light/tsl2772.c > > @@ -986,7 +986,7 @@ static ssize_t in_illuminance0_lux_table_show(struct device *dev, > > int offset = 0; > > > > while (i < TSL2772_MAX_LUX_TABLE_SIZE) { > > - offset += snprintf(buf + offset, PAGE_SIZE, "%u,%u,", > > + offset += scnprintf(buf + offset, PAGE_SIZE, "%u,%u,", > > chip->tsl2772_device_lux[i].ch0, > > chip->tsl2772_device_lux[i].ch1); > > if (chip->tsl2772_device_lux[i].ch0 == 0) { > > @@ -1000,7 +1000,7 @@ static ssize_t in_illuminance0_lux_table_show(struct device *dev, > > i++; > > } > > > > - offset += snprintf(buf + offset, PAGE_SIZE, "\n"); > > + offset += scnprintf(buf + offset, PAGE_SIZE, "\n"); > > return offset; > > } > > >