Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3866131ybb; Mon, 23 Mar 2020 09:07:06 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsrq4/k/svv7ozWElXHu3BxqqGqOj1LmziTDs0sBVLllgWZKn95nzWaQ2r1a3qpIylMaK7B X-Received: by 2002:a4a:940e:: with SMTP id h14mr603351ooi.26.1584979625946; Mon, 23 Mar 2020 09:07:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584979625; cv=none; d=google.com; s=arc-20160816; b=dgA7GevPrSGbl3e1OdBGUCN5mTS6asiR6je9OMigbLgF/yxu166FT2W/xYBQaKfK1B 9SBUMogY7iZ0rA4jrQdB5qoBM3ucQMwKSzoYFShQTVhBO0MppVaqyXojWQeNeaZ3Lsxd XwjhLF+M9CephemQXDSJYyo4YWhLCKRsieykzfFoTo1AyrvrEVLxoBK/JDdiET3d2Xm7 KakLBMWoQfHBoH5HO9A958Dih7lPhetUf/eBAOXqKr8G53H0O7gtA+fvciYKpWsF1Ylx 52P25UDGn4klvGluxDRPmuDR3ejVkCYKNzNcYGYcd3frHb6sWTPIBwTUXhMvfq3hTfBx T9KA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:organization:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:ironport-sdr:ironport-sdr; bh=NZS9LQRElX+kp0yNWd+X1qgrSXit45PN2YoR/xezHY4=; b=LXBBLGJ5jR/k86EbvAN7hpo6xUbzFER/43iJHl1kPWAsKLq+BAjhfXK8MBZa6MIXtY ZFjSQwIVwWVbfvSLK1PBn5VBBUU9Zke60CCRAFkWfMNoGsuSAaSfiadQIPHsi4v1KGd/ QjHJvOmBppY5gfVy96lrstfiusx7kzJv67BGgWABKxxevaQBM4cKQ8FTBPySTAAmQWL1 5hG1Ep2M9TrSOV5/xqD4YInl449kbppwJYMlZc9EeawHTKp0i/myNZu+JQxxrTks7Shq MRHlcmZ05xH0o+kE5P2cisYfaP0EuigLR26BcNiU6bu2yc9Gy7LFKfGOKiXh/IKANvwc 3q3Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e14si1522568otf.226.2020.03.23.09.06.51; Mon, 23 Mar 2020 09:07:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727491AbgCWQFe (ORCPT + 99 others); Mon, 23 Mar 2020 12:05:34 -0400 Received: from mga07.intel.com ([134.134.136.100]:46754 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727479AbgCWQFe (ORCPT ); Mon, 23 Mar 2020 12:05:34 -0400 IronPort-SDR: t/H1DBiayEHmqAkdDEAoeAGIG4Ez1Aax5SU24Fn9IRHigGJ0q7+WqNXxwASdxg6cxohpjejQun +NBivTErJfBA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Mar 2020 09:05:33 -0700 IronPort-SDR: 7O/Nrp0ZmN2eUWqYaSNVfMn+7z3XKsjRcZ2D6zOChIXsFrixHltSjKSGQKeMNLmc6hj0/jdKmS lWbogD1KkJLA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,297,1580803200"; d="scan'208";a="246241169" Received: from smile.fi.intel.com (HELO smile) ([10.237.68.40]) by orsmga003.jf.intel.com with ESMTP; 23 Mar 2020 09:05:30 -0700 Received: from andy by smile with local (Exim 4.93) (envelope-from ) id 1jGPa0-00CHXi-RE; Mon, 23 Mar 2020 18:05:32 +0200 Date: Mon, 23 Mar 2020 18:05:32 +0200 From: 'Andy Shevchenko' To: David Laight Cc: Rohit Sarkar , linux-iio , Linux Kernel Mailing List , Jonathan Cameron , "dragos.bogdan@analog.com" , Lars-Peter Clausen , Michael Hennerich , Stefan Popa , Hartmut Knaack , Peter Meerwald Subject: Re: [PATCH] iio: gyro: adis16136: use scnprintf instead of snprintf Message-ID: <20200323160532.GS1922688@smile.fi.intel.com> References: <5e723666.1c69fb81.3545b.79c3@mx.google.com> <20200322002542.GA2826015@smile.fi.intel.com> <5e77017a.1c69fb81.dc341.8ab9@mx.google.com> <81821e142e3c48febb7ab357dd200992@AcuMS.aculab.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <81821e142e3c48febb7ab357dd200992@AcuMS.aculab.com> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Mar 23, 2020 at 03:04:23PM +0000, David Laight wrote: > From: Andy Shevchenko > > Sent: 22 March 2020 10:27 > > On Sun, Mar 22, 2020 at 8:11 AM Rohit Sarkar wrote: > > > > > > On Sun, Mar 22, 2020 at 02:25:42AM +0200, Andy Shevchenko wrote: > > > > On Wed, Mar 18, 2020 at 08:25:22PM +0530, Rohit Sarkar wrote: > > > > > scnprintf returns the actual number of bytes written into the buffer as > > > > > opposed to snprintf which returns the number of bytes that would have > > > > > been written if the buffer was big enough. Using the output of snprintf > > > > > may lead to difficult to detect bugs. > > > > > > > > Nice. Have you investigate the code? > > > > > > > > > @@ -96,7 +96,7 @@ static ssize_t adis16136_show_serial(struct file *file, > > > > > if (ret) > > > > > return ret; > > > > > > > > > > - len = snprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2, > > > > > + len = scnprintf(buf, sizeof(buf), "%.4x%.4x%.4x-%.4x\n", lot1, lot2, > > > > > lot3, serial); > > > > > > > > > > return simple_read_from_buffer(userbuf, count, ppos, buf, len); > > > > > > > > The buffer size is 20, the pattern size I count to 19. Do you think snprintf() > > > > can fail? > > > That might be the case, but IMO using scnprintf can be considered as a > > > best practice. There is no overhead with this change and further if the > > > pattern is changed by someone in the future they might overlook the > > > buffersize > > > > If we cut the string above we will give wrong information to the user space. > > I think scnprintf() change is a noise and does not improve the situation anyhow. > > If, for any reason, any of the values are large the user will get > corrupt data. > But you don't want to leak random kernel memory to the user. How? Kernel already got crashed at this point. > > So while you may be able to prove that this particular snprintf() > can't overflow, in general checking it requires knowledge of the code. Here it's still a noise. > With scnprintf() you know nothing odd will happen. ...and quite likely hide a lot of issues. Really any "micro" / "small" correction / optimization to be very carefully thought through. > FWIW I suspect the 'standard' return value from snprintf() comes > from the return value of the original user-space implementations > which faked-up a FILE structure on stack and just silently discarded > the output bytes that wouldn't fit in the buffer (they'd usually > by flushed to a real file). > The original sprintf() just specified a very big length so the > flush would never be requested. -- With Best Regards, Andy Shevchenko