linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Takashi Iwai <tiwai@suse.de>
Cc: linux-iio@vger.kernel.org
Subject: Re: [PATCH 1/2] iio: core: Use scnprintf() for avoiding potential buffer overflow
Date: Sun, 15 Mar 2020 09:54:46 +0000	[thread overview]
Message-ID: <20200315095446.4f2aa451@archlinux> (raw)
In-Reply-To: <20200311074325.7922-2-tiwai@suse.de>

On Wed, 11 Mar 2020 08:43:24 +0100
Takashi Iwai <tiwai@suse.de> 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().

Hmm. This is definitely in the somewhat theoretical bug category, but
given it can be called to print a list of values that is defined
in a driver, fair enough - it's good protection.

I'm not going to rush this in given we don't have any drivers
that are known to run into this.  Hence I've queued it up for
the togreg branch of iio.git targeting the merge window rather than
as a fix.

Thanks,

Jonathan


> 
> Signed-off-by: Takashi Iwai <tiwai@suse.de>
> ---
>  drivers/iio/industrialio-core.c | 34 +++++++++++++++++-----------------
>  1 file changed, 17 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index 65ff0d067018..197006b5d5c2 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -559,46 +559,46 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
>  
>  	switch (type) {
>  	case IIO_VAL_INT:
> -		return snprintf(buf, len, "%d", vals[0]);
> +		return scnprintf(buf, len, "%d", vals[0]);
>  	case IIO_VAL_INT_PLUS_MICRO_DB:
>  		scale_db = true;
>  		/* fall through */
>  	case IIO_VAL_INT_PLUS_MICRO:
>  		if (vals[1] < 0)
> -			return snprintf(buf, len, "-%d.%06u%s", abs(vals[0]),
> +			return scnprintf(buf, len, "-%d.%06u%s", abs(vals[0]),
>  					-vals[1], scale_db ? " dB" : "");
>  		else
> -			return snprintf(buf, len, "%d.%06u%s", vals[0], vals[1],
> +			return scnprintf(buf, len, "%d.%06u%s", vals[0], vals[1],
>  					scale_db ? " dB" : "");
>  	case IIO_VAL_INT_PLUS_NANO:
>  		if (vals[1] < 0)
> -			return snprintf(buf, len, "-%d.%09u", abs(vals[0]),
> +			return scnprintf(buf, len, "-%d.%09u", abs(vals[0]),
>  					-vals[1]);
>  		else
> -			return snprintf(buf, len, "%d.%09u", vals[0], vals[1]);
> +			return scnprintf(buf, len, "%d.%09u", vals[0], vals[1]);
>  	case IIO_VAL_FRACTIONAL:
>  		tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
>  		tmp1 = vals[1];
>  		tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
> -		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
> +		return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
>  	case IIO_VAL_FRACTIONAL_LOG2:
>  		tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
>  		tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
> -		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
> +		return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
>  	case IIO_VAL_INT_MULTIPLE:
>  	{
>  		int i;
>  		int l = 0;
>  
>  		for (i = 0; i < size; ++i) {
> -			l += snprintf(&buf[l], len - l, "%d ", vals[i]);
> +			l += scnprintf(&buf[l], len - l, "%d ", vals[i]);
>  			if (l >= len)
>  				break;
>  		}
>  		return l;
>  	}
>  	case IIO_VAL_CHAR:
> -		return snprintf(buf, len, "%c", (char)vals[0]);
> +		return scnprintf(buf, len, "%c", (char)vals[0]);
>  	default:
>  		return 0;
>  	}
> @@ -669,10 +669,10 @@ static ssize_t iio_format_avail_list(char *buf, const int *vals,
>  			if (len >= PAGE_SIZE)
>  				return -EFBIG;
>  			if (i < length - 1)
> -				len += snprintf(buf + len, PAGE_SIZE - len,
> +				len += scnprintf(buf + len, PAGE_SIZE - len,
>  						" ");
>  			else
> -				len += snprintf(buf + len, PAGE_SIZE - len,
> +				len += scnprintf(buf + len, PAGE_SIZE - len,
>  						"\n");
>  			if (len >= PAGE_SIZE)
>  				return -EFBIG;
> @@ -685,10 +685,10 @@ static ssize_t iio_format_avail_list(char *buf, const int *vals,
>  			if (len >= PAGE_SIZE)
>  				return -EFBIG;
>  			if (i < length / 2 - 1)
> -				len += snprintf(buf + len, PAGE_SIZE - len,
> +				len += scnprintf(buf + len, PAGE_SIZE - len,
>  						" ");
>  			else
> -				len += snprintf(buf + len, PAGE_SIZE - len,
> +				len += scnprintf(buf + len, PAGE_SIZE - len,
>  						"\n");
>  			if (len >= PAGE_SIZE)
>  				return -EFBIG;
> @@ -712,10 +712,10 @@ static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
>  			if (len >= PAGE_SIZE)
>  				return -EFBIG;
>  			if (i < 2)
> -				len += snprintf(buf + len, PAGE_SIZE - len,
> +				len += scnprintf(buf + len, PAGE_SIZE - len,
>  						" ");
>  			else
> -				len += snprintf(buf + len, PAGE_SIZE - len,
> +				len += scnprintf(buf + len, PAGE_SIZE - len,
>  						"]\n");
>  			if (len >= PAGE_SIZE)
>  				return -EFBIG;
> @@ -728,10 +728,10 @@ static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
>  			if (len >= PAGE_SIZE)
>  				return -EFBIG;
>  			if (i < 2)
> -				len += snprintf(buf + len, PAGE_SIZE - len,
> +				len += scnprintf(buf + len, PAGE_SIZE - len,
>  						" ");
>  			else
> -				len += snprintf(buf + len, PAGE_SIZE - len,
> +				len += scnprintf(buf + len, PAGE_SIZE - len,
>  						"]\n");
>  			if (len >= PAGE_SIZE)
>  				return -EFBIG;


  reply	other threads:[~2020-03-15  9:54 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-11  7:43 [PATCH 0/2] iio: Use scnprintf() for avoiding potential buffer overflow Takashi Iwai
2020-03-11  7:43 ` [PATCH 1/2] iio: core: " Takashi Iwai
2020-03-15  9:54   ` Jonathan Cameron [this message]
2020-03-15 10:17     ` Takashi Iwai
2020-03-11  7:43 ` [PATCH 2/2] iio: tsl2772: " Takashi Iwai
2020-03-15  9:58   ` Jonathan Cameron
2020-03-15 10:25     ` Takashi Iwai
2020-03-15 10:33     ` Brian Masney
2020-03-15 13:10       ` Jonathan Cameron
2020-03-16  8:04         ` Takashi Iwai
2020-03-16 11:50           ` Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200315095446.4f2aa451@archlinux \
    --to=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=tiwai@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).