linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Willy Tarreau <w@1wt.eu>
Cc: Silvio Cesare <silvio.cesare@gmail.com>,
	LKML <linux-kernel@vger.kernel.org>,
	Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>,
	Liam Girdwood <liam.r.girdwood@linux.intel.com>,
	Jie Yang <yang.jie@linux.intel.com>,
	Dan Carpenter <dan.carpenter@oracle.com>,
	Will Deacon <will.deacon@arm.com>, Greg KH <greg@kroah.com>
Subject: Re: [PATCH 6/8] ASoC: intel: skylake: change snprintf to scnprintf for possible overflow
Date: Mon, 14 Jan 2019 17:12:32 -0800	[thread overview]
Message-ID: <CAGXu5jL=gSQdv4B70QwQ9cvTM590ivNJWYpc9Dk025r9mzhzbA@mail.gmail.com> (raw)
In-Reply-To: <20190112152844.26550-6-w@1wt.eu>

On Sat, Jan 12, 2019 at 7:28 AM Willy Tarreau <w@1wt.eu> wrote:
>
> From: Silvio Cesare <silvio.cesare@gmail.com>
>
> Change snprintf to scnprintf. There are generally two cases where using
> snprintf causes problems.
>
> 1) Uses of size += snprintf(buf, SIZE - size, fmt, ...)
> In this case, if snprintf would have written more characters than what the
> buffer size (SIZE) is, then size will end up larger than SIZE. In later
> uses of snprintf, SIZE - size will result in a negative number, leading
> to problems. Note that size might already be too large by using
> size = snprintf before the code reaches a case of size += snprintf.
>
> 2) If size is ultimately used as a length parameter for a copy back to user
> space, then it will potentially allow for a buffer overflow and information
> disclosure when size is greater than SIZE. When the size is used to index
> the buffer directly, we can have memory corruption. This also means when
> size = snprintf... is used, it may also cause problems since size may become
> large.  Copying to userspace is mitigated by the HARDENED_USERCOPY kernel
> configuration.
>
> The solution to these issues is to use scnprintf which returns the number of
> characters actually written to the buffer, so the size variable will never
> exceed SIZE.
>
> Signed-off-by: Silvio Cesare <silvio.cesare@gmail.com>
> Cc: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
> Cc: Liam Girdwood <liam.r.girdwood@linux.intel.com>
> Cc: Jie Yang <yang.jie@linux.intel.com>
> Cc: Dan Carpenter <dan.carpenter@oracle.com>
> Cc: Kees Cook <keescook@chromium.org>
> Cc: Will Deacon <will.deacon@arm.com>
> Cc: Greg KH <greg@kroah.com>
> Signed-off-by: Willy Tarreau <w@1wt.eu>

This should get a Cc: stable, IMO.

Reviewed-by: Kees Cook <keescook@chromium.org>

-Kees

>
> ---
>  sound/soc/intel/skylake/skl-debug.c | 28 ++++++++++++++--------------
>  1 file changed, 14 insertions(+), 14 deletions(-)
>
> diff --git a/sound/soc/intel/skylake/skl-debug.c b/sound/soc/intel/skylake/skl-debug.c
> index 5d7ac2ee7a3c..bb28db734fb7 100644
> --- a/sound/soc/intel/skylake/skl-debug.c
> +++ b/sound/soc/intel/skylake/skl-debug.c
> @@ -43,7 +43,7 @@ static ssize_t skl_print_pins(struct skl_module_pin *m_pin, char *buf,
>         ssize_t ret = 0;
>
>         for (i = 0; i < max_pin; i++)
> -               ret += snprintf(buf + size, MOD_BUF - size,
> +               ret += scnprintf(buf + size, MOD_BUF - size,
>                                 "%s %d\n\tModule %d\n\tInstance %d\n\t"
>                                 "In-used %s\n\tType %s\n"
>                                 "\tState %d\n\tIndex %d\n",
> @@ -59,7 +59,7 @@ static ssize_t skl_print_pins(struct skl_module_pin *m_pin, char *buf,
>  static ssize_t skl_print_fmt(struct skl_module_fmt *fmt, char *buf,
>                                         ssize_t size, bool direction)
>  {
> -       return snprintf(buf + size, MOD_BUF - size,
> +       return scnprintf(buf + size, MOD_BUF - size,
>                         "%s\n\tCh %d\n\tFreq %d\n\tBit depth %d\n\t"
>                         "Valid bit depth %d\n\tCh config %#x\n\tInterleaving %d\n\t"
>                         "Sample Type %d\n\tCh Map %#x\n",
> @@ -81,16 +81,16 @@ static ssize_t module_read(struct file *file, char __user *user_buf,
>         if (!buf)
>                 return -ENOMEM;
>
> -       ret = snprintf(buf, MOD_BUF, "Module:\n\tUUID %pUL\n\tModule id %d\n"
> +       ret = scnprintf(buf, MOD_BUF, "Module:\n\tUUID %pUL\n\tModule id %d\n"
>                         "\tInstance id %d\n\tPvt_id %d\n", mconfig->guid,
>                         mconfig->id.module_id, mconfig->id.instance_id,
>                         mconfig->id.pvt_id);
>
> -       ret += snprintf(buf + ret, MOD_BUF - ret,
> +       ret += scnprintf(buf + ret, MOD_BUF - ret,
>                         "Resources:\n\tMCPS %#x\n\tIBS %#x\n\tOBS %#x\t\n",
>                         mconfig->mcps, mconfig->ibs, mconfig->obs);
>
> -       ret += snprintf(buf + ret, MOD_BUF - ret,
> +       ret += scnprintf(buf + ret, MOD_BUF - ret,
>                         "Module data:\n\tCore %d\n\tIn queue %d\n\t"
>                         "Out queue %d\n\tType %s\n",
>                         mconfig->core_id, mconfig->max_in_queue,
> @@ -100,38 +100,38 @@ static ssize_t module_read(struct file *file, char __user *user_buf,
>         ret += skl_print_fmt(mconfig->in_fmt, buf, ret, true);
>         ret += skl_print_fmt(mconfig->out_fmt, buf, ret, false);
>
> -       ret += snprintf(buf + ret, MOD_BUF - ret,
> +       ret += scnprintf(buf + ret, MOD_BUF - ret,
>                         "Fixup:\n\tParams %#x\n\tConverter %#x\n",
>                         mconfig->params_fixup, mconfig->converter);
>
> -       ret += snprintf(buf + ret, MOD_BUF - ret,
> +       ret += scnprintf(buf + ret, MOD_BUF - ret,
>                         "Module Gateway:\n\tType %#x\n\tVbus %#x\n\tHW conn %#x\n\tSlot %#x\n",
>                         mconfig->dev_type, mconfig->vbus_id,
>                         mconfig->hw_conn_type, mconfig->time_slot);
>
> -       ret += snprintf(buf + ret, MOD_BUF - ret,
> +       ret += scnprintf(buf + ret, MOD_BUF - ret,
>                         "Pipeline:\n\tID %d\n\tPriority %d\n\tConn Type %d\n\t"
>                         "Pages %#x\n", mconfig->pipe->ppl_id,
>                         mconfig->pipe->pipe_priority, mconfig->pipe->conn_type,
>                         mconfig->pipe->memory_pages);
>
> -       ret += snprintf(buf + ret, MOD_BUF - ret,
> +       ret += scnprintf(buf + ret, MOD_BUF - ret,
>                         "\tParams:\n\t\tHost DMA %d\n\t\tLink DMA %d\n",
>                         mconfig->pipe->p_params->host_dma_id,
>                         mconfig->pipe->p_params->link_dma_id);
>
> -       ret += snprintf(buf + ret, MOD_BUF - ret,
> +       ret += scnprintf(buf + ret, MOD_BUF - ret,
>                         "\tPCM params:\n\t\tCh %d\n\t\tFreq %d\n\t\tFormat %d\n",
>                         mconfig->pipe->p_params->ch,
>                         mconfig->pipe->p_params->s_freq,
>                         mconfig->pipe->p_params->s_fmt);
>
> -       ret += snprintf(buf + ret, MOD_BUF - ret,
> +       ret += scnprintf(buf + ret, MOD_BUF - ret,
>                         "\tLink %#x\n\tStream %#x\n",
>                         mconfig->pipe->p_params->linktype,
>                         mconfig->pipe->p_params->stream);
>
> -       ret += snprintf(buf + ret, MOD_BUF - ret,
> +       ret += scnprintf(buf + ret, MOD_BUF - ret,
>                         "\tState %d\n\tPassthru %s\n",
>                         mconfig->pipe->state,
>                         mconfig->pipe->passthru ? "true" : "false");
> @@ -141,7 +141,7 @@ static ssize_t module_read(struct file *file, char __user *user_buf,
>         ret += skl_print_pins(mconfig->m_out_pin, buf,
>                         mconfig->max_out_queue, ret, false);
>
> -       ret += snprintf(buf + ret, MOD_BUF - ret,
> +       ret += scnprintf(buf + ret, MOD_BUF - ret,
>                         "Other:\n\tDomain %d\n\tHomogeneous Input %s\n\t"
>                         "Homogeneous Output %s\n\tIn Queue Mask %d\n\t"
>                         "Out Queue Mask %d\n\tDMA ID %d\n\tMem Pages %d\n\t"
> @@ -199,7 +199,7 @@ static ssize_t fw_softreg_read(struct file *file, char __user *user_buf,
>                 __iowrite32_copy(d->fw_read_buff, fw_reg_addr, w0_stat_sz >> 2);
>
>         for (offset = 0; offset < FW_REG_SIZE; offset += 16) {
> -               ret += snprintf(tmp + ret, FW_REG_BUF - ret, "%#.4x: ", offset);
> +               ret += scnprintf(tmp + ret, FW_REG_BUF - ret, "%#.4x: ", offset);
>                 hex_dump_to_buffer(d->fw_read_buff + offset, 16, 16, 4,
>                                    tmp + ret, FW_REG_BUF - ret, 0);
>                 ret += strlen(tmp + ret);
> --
> 2.19.2
>


-- 
Kees Cook

  reply	other threads:[~2019-01-15  1:12 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-12 15:28 [PATCH 1/8] lkdtm: change snprintf to scnprintf for possible overflow Willy Tarreau
2019-01-12 15:28 ` [PATCH 2/8] libertas: " Willy Tarreau
2019-01-15  1:09   ` Kees Cook
2019-01-15  5:55   ` Kalle Valo
2019-01-15 20:35     ` Willy Tarreau
2019-01-16 16:40       ` Kalle Valo
2019-01-16 17:02         ` Willy Tarreau
2019-01-12 15:28 ` [PATCH 3/8] ocfs2: " Willy Tarreau
2019-01-15  1:14   ` Kees Cook
2019-01-12 15:28 ` [PATCH 4/8] ASoC: " Willy Tarreau
2019-01-15  1:13   ` Kees Cook
2019-01-15  1:25   ` Nicolin Chen
2019-01-15  3:18     ` Willy Tarreau
2019-01-12 15:28 ` [PATCH 5/8] scsi: lpfc: " Willy Tarreau
2019-01-15  1:15   ` Kees Cook
2019-01-15 22:41     ` James Smart
2019-03-20 17:39       ` Greg KH
2019-03-20 20:27         ` James Smart
2019-03-21  0:41         ` James Smart
2019-01-12 15:28 ` [PATCH 6/8] ASoC: intel: skylake: " Willy Tarreau
2019-01-15  1:12   ` Kees Cook [this message]
2019-01-16 18:41   ` Kees Cook
2019-01-16 19:35     ` Pierre-Louis Bossart
2019-01-16 19:51       ` Kees Cook
2019-01-12 15:28 ` [PATCH 7/8] ASoC: dapm: " Willy Tarreau
2019-01-14 14:56   ` Mark Brown
2019-01-15  3:16     ` Willy Tarreau
2019-01-15 15:44       ` Mark Brown
2019-01-15 15:55         ` Willy Tarreau
2019-01-12 15:28 ` [PATCH 8/8] spi: dw: " Willy Tarreau
2019-01-15  1:09   ` Kees Cook
2019-01-15  1:02 ` [PATCH 1/8] lkdtm: " Kees Cook
2019-01-15  1:07   ` Kees Cook
2019-01-15  3:12   ` Willy Tarreau
2019-01-15 20:47 ` Kees Cook
2019-01-18 13:06   ` Greg KH

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='CAGXu5jL=gSQdv4B70QwQ9cvTM590ivNJWYpc9Dk025r9mzhzbA@mail.gmail.com' \
    --to=keescook@chromium.org \
    --cc=dan.carpenter@oracle.com \
    --cc=greg@kroah.com \
    --cc=liam.r.girdwood@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pierre-louis.bossart@linux.intel.com \
    --cc=silvio.cesare@gmail.com \
    --cc=w@1wt.eu \
    --cc=will.deacon@arm.com \
    --cc=yang.jie@linux.intel.com \
    /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).