mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Arnd Bergmann <arnd@arndb.de>,
	Jiri Prchal <jiri.prchal@aksignal.cz>,
	Ralph Siemsen <ralph.siemsen@linaro.org>,
	Mark Brown <broonie@kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	linux-hardening@vger.kernel.org
Subject: Re: [PATCH] eeprom: at25: Restore missing allocation
Date: Wed, 12 Jan 2022 12:41:27 -0800	[thread overview]
Message-ID: <202201121240.C9A4613D36@keescook> (raw)
In-Reply-To: <CAHp75VdqK7h63fz-cPaQ2MGaVdaR2f1Fb5kKCZidUG3RwLsAVA@mail.gmail.com>

On Sat, Jan 08, 2022 at 01:36:12PM +0200, Andy Shevchenko wrote:
> On Sat, Jan 8, 2022 at 1:01 PM Kees Cook <keescook@chromium.org> wrote:
> >
> > Building under CONFIG_FORTIFY_SOURCE=y and -Warray-bounds complained about
> > strncpy() being used against an empty object. It turns out this was due to
> > the at25 allocation going missing during a conflict resolution. Restore
> > this, and while we're here take the opportunity to do another strncpy()
> > replacement, since it's use is deprecated[1].
> >
> > Seen as:
> >
> > In function 'strncpy',
> >     inlined from 'at25_fw_to_chip.constprop' at drivers/misc/eeprom/at25.c:312:2:
> > ./include/linux/fortify-string.h:48:33: warning: '__builtin_strncpy' offset [0, 9] is out of the bounds [0, 0] [-Warray-bounds]
> >    48 | #define __underlying_strncpy    __builtin_strncpy
> >       |                                 ^
> > ./include/linux/fortify-string.h:59:16: note: in expansion of macro '__underlying_strncpy'
> >    59 |         return __underlying_strncpy(p, q, size);
> >       |                ^~~~~~~~~~~~~~~~~~~~
> > In function 'strncpy',
> >     inlined from 'at25_fram_to_chip' at drivers/misc/eeprom/at25.c:373:2,
> >     inlined from 'at25_probe' at drivers/misc/eeprom/at25.c:453:10:
> > ./include/linux/fortify-string.h:48:33: warning: '__builtin_strncpy' offset [0, 9] is out of the bounds [0, 0] [-Warray-bounds]
> >    48 | #define __underlying_strncpy    __builtin_strncpy
> >       |                                 ^
> > ./include/linux/fortify-string.h:59:16: note: in expansion of macro '__underlying_strncpy'
> >    59 |         return __underlying_strncpy(p, q, size);
> >       |                ^~~~~~~~~~~~~~~~~~~~
> >
> > [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
> 
> Thanks!
> With or without the below comment being addressed
> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>

Thanks!

> 
> > Fixes: af40d16042d6 ("Merge v5.15-rc5 into char-misc-next")
> > Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > Cc: Arnd Bergmann <arnd@arndb.de>
> > Cc: Jiri Prchal <jiri.prchal@aksignal.cz>
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> > ---
> >  drivers/misc/eeprom/at25.c | 8 ++++++--
> >  1 file changed, 6 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/misc/eeprom/at25.c b/drivers/misc/eeprom/at25.c
> > index c3305bdda69c..1a19fa5728c8 100644
> > --- a/drivers/misc/eeprom/at25.c
> > +++ b/drivers/misc/eeprom/at25.c
> > @@ -309,7 +309,7 @@ static int at25_fw_to_chip(struct device *dev, struct spi_eeprom *chip)
> >         u32 val;
> >         int err;
> >
> > -       strncpy(chip->name, "at25", sizeof(chip->name));
> > +       strscpy(chip->name, "at25", sizeof(chip->name));
> >
> >         err = device_property_read_u32(dev, "size", &val);
> >         if (err)
> > @@ -370,7 +370,7 @@ static int at25_fram_to_chip(struct device *dev, struct spi_eeprom *chip)
> >         u8 id[FM25_ID_LEN];
> >         int i;
> >
> > -       strncpy(chip->name, "fm25", sizeof(chip->name));
> > +       strscpy(chip->name, "fm25", sizeof(chip->name));
> >
> >         /* Get ID of chip */
> >         fm25_aux_read(at25, id, FM25_RDID, FM25_ID_LEN);
> > @@ -440,6 +440,10 @@ static int at25_probe(struct spi_device *spi)
> >                 return -ENXIO;
> >         }
> >
> > +       at25 = devm_kzalloc(&spi->dev, sizeof(struct at25_data), GFP_KERNEL);
> 
> I would use sizeof(*at25) but I think you restored the exact context.

Yeah, I just restore the chunk exactly as it was.

Greg, should I send a v2 with this adjusted?

-Kees

> 
> > +       if (!at25)
> > +               return -ENOMEM;
> > +
> >         mutex_init(&at25->lock);
> >         at25->spi = spi;
> >         spi_set_drvdata(spi, at25);
> > --
> > 2.30.2
> >
> 
> 
> -- 
> With Best Regards,
> Andy Shevchenko

-- 
Kees Cook

      reply	other threads:[~2022-01-12 20:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-07 23:24 Kees Cook
2022-01-08 11:36 ` Andy Shevchenko
2022-01-12 20:41   ` Kees Cook [this message]

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=202201121240.C9A4613D36@keescook \
    --to=keescook@chromium.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=andy.shevchenko@gmail.com \
    --cc=arnd@arndb.de \
    --cc=broonie@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jiri.prchal@aksignal.cz \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ralph.siemsen@linaro.org \
    /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

Powered by JetHome