All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mtd: nftl: Remove VLA usage
@ 2018-04-23 20:35 Kees Cook
  2018-04-29  9:16 ` Boris Brezillon
  0 siblings, 1 reply; 4+ messages in thread
From: Kees Cook @ 2018-04-23 20:35 UTC (permalink / raw)
  To: Boris Brezillon
  Cc: linux-kernel, David Woodhouse, Brian Norris, Marek Vasut,
	Richard Weinberger, linux-mtd

On the quest to remove all VLAs from the kernel[1] this changes the
check_free_sectors() routine to use the same stack buffer for both
data byte checks (SECTORSIZE) and oob byte checks (oobsize). Since
these regions aren't needed at the same time, they don't need to be
consecutively allocated. Additionally, while it's possible for oobsize
to be large, it is unlikely to be larger than the actual SECTORSIZE. As
such, remove the VLA, adjust offsets and add a sanity check to make sure
we never get a pathological oobsize.

[1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 drivers/mtd/inftlmount.c | 11 ++++++++---
 drivers/mtd/nftlmount.c  | 11 ++++++++---
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/drivers/mtd/inftlmount.c b/drivers/mtd/inftlmount.c
index aab4f68bd36f..9cdae7f0fc2e 100644
--- a/drivers/mtd/inftlmount.c
+++ b/drivers/mtd/inftlmount.c
@@ -334,7 +334,7 @@ static int memcmpb(void *a, int c, int n)
 static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
 	int len, int check_oob)
 {
-	u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
+	u8 buf[SECTORSIZE];
 	struct mtd_info *mtd = inftl->mbd.mtd;
 	size_t retlen;
 	int i;
@@ -346,10 +346,15 @@ static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
 			return -1;
 
 		if (check_oob) {
+			if (mtd->oobsize > sizeof(buf)) {
+				pr_warn("MTD oobsize > SECTORSIZE: %d\n",
+					mtd->oobsize);
+				return -1;
+			}
 			if(inftl_read_oob(mtd, address, mtd->oobsize,
-					  &retlen, &buf[SECTORSIZE]) < 0)
+					  &retlen, buf) < 0)
 				return -1;
-			if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
+			if (memcmpb(buf, 0xff, mtd->oobsize) != 0)
 				return -1;
 		}
 		address += SECTORSIZE;
diff --git a/drivers/mtd/nftlmount.c b/drivers/mtd/nftlmount.c
index a6fbfa4e5799..e6eba7f3fdf5 100644
--- a/drivers/mtd/nftlmount.c
+++ b/drivers/mtd/nftlmount.c
@@ -272,7 +272,7 @@ static int memcmpb(void *a, int c, int n)
 static int check_free_sectors(struct NFTLrecord *nftl, unsigned int address, int len,
 			      int check_oob)
 {
-	u8 buf[SECTORSIZE + nftl->mbd.mtd->oobsize];
+	u8 buf[SECTORSIZE];
 	struct mtd_info *mtd = nftl->mbd.mtd;
 	size_t retlen;
 	int i;
@@ -284,10 +284,15 @@ static int check_free_sectors(struct NFTLrecord *nftl, unsigned int address, int
 			return -1;
 
 		if (check_oob) {
+			if (mtd->oobsize > sizeof(buf)) {
+				pr_warn("MTD oobsize > SECTORSIZE: %d\n",
+					mtd->oobsize);
+				return -1;
+			}
 			if(nftl_read_oob(mtd, address, mtd->oobsize,
-					 &retlen, &buf[SECTORSIZE]) < 0)
+					 &retlen, buf) < 0)
 				return -1;
-			if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
+			if (memcmpb(buf, 0xff, mtd->oobsize) != 0)
 				return -1;
 		}
 		address += SECTORSIZE;
-- 
2.7.4


-- 
Kees Cook
Pixel Security

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] mtd: nftl: Remove VLA usage
  2018-04-23 20:35 [PATCH] mtd: nftl: Remove VLA usage Kees Cook
@ 2018-04-29  9:16 ` Boris Brezillon
  2018-04-29 14:31   ` Kees Cook
  0 siblings, 1 reply; 4+ messages in thread
From: Boris Brezillon @ 2018-04-29  9:16 UTC (permalink / raw)
  To: Kees Cook
  Cc: linux-kernel, David Woodhouse, Brian Norris, Marek Vasut,
	Richard Weinberger, linux-mtd

Hi Kees,

On Mon, 23 Apr 2018 13:35:00 -0700
Kees Cook <keescook@chromium.org> wrote:

> On the quest to remove all VLAs from the kernel[1] this changes the
> check_free_sectors() routine to use the same stack buffer for both
> data byte checks (SECTORSIZE) and oob byte checks (oobsize). Since
> these regions aren't needed at the same time, they don't need to be
> consecutively allocated. Additionally, while it's possible for oobsize
> to be large, it is unlikely to be larger than the actual SECTORSIZE. As
> such, remove the VLA, adjust offsets and add a sanity check to make sure
> we never get a pathological oobsize.
> 
> [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
> 
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  drivers/mtd/inftlmount.c | 11 ++++++++---
>  drivers/mtd/nftlmount.c  | 11 ++++++++---
>  2 files changed, 16 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/mtd/inftlmount.c b/drivers/mtd/inftlmount.c
> index aab4f68bd36f..9cdae7f0fc2e 100644
> --- a/drivers/mtd/inftlmount.c
> +++ b/drivers/mtd/inftlmount.c
> @@ -334,7 +334,7 @@ static int memcmpb(void *a, int c, int n)
>  static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
>  	int len, int check_oob)
>  {
> -	u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
> +	u8 buf[SECTORSIZE];

Could we instead move to dynamic allocation. I mean, SECTORSIZE is 512
bytes, so only with this function we consume 1/16 of the stack. Not to
mention that some MTD drivers might want to do DMA on buffer passed by
the MTD user, and if the buffer is on the stack they'll have to use a
bounce buffer instead.

Regards,

Boris

>  	struct mtd_info *mtd = inftl->mbd.mtd;
>  	size_t retlen;
>  	int i;
> @@ -346,10 +346,15 @@ static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
>  			return -1;
>  
>  		if (check_oob) {
> +			if (mtd->oobsize > sizeof(buf)) {
> +				pr_warn("MTD oobsize > SECTORSIZE: %d\n",
> +					mtd->oobsize);
> +				return -1;
> +			}
>  			if(inftl_read_oob(mtd, address, mtd->oobsize,
> -					  &retlen, &buf[SECTORSIZE]) < 0)
> +					  &retlen, buf) < 0)
>  				return -1;
> -			if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
> +			if (memcmpb(buf, 0xff, mtd->oobsize) != 0)
>  				return -1;
>  		}
>  		address += SECTORSIZE;
> diff --git a/drivers/mtd/nftlmount.c b/drivers/mtd/nftlmount.c
> index a6fbfa4e5799..e6eba7f3fdf5 100644
> --- a/drivers/mtd/nftlmount.c
> +++ b/drivers/mtd/nftlmount.c
> @@ -272,7 +272,7 @@ static int memcmpb(void *a, int c, int n)
>  static int check_free_sectors(struct NFTLrecord *nftl, unsigned int address, int len,
>  			      int check_oob)
>  {
> -	u8 buf[SECTORSIZE + nftl->mbd.mtd->oobsize];
> +	u8 buf[SECTORSIZE];
>  	struct mtd_info *mtd = nftl->mbd.mtd;
>  	size_t retlen;
>  	int i;
> @@ -284,10 +284,15 @@ static int check_free_sectors(struct NFTLrecord *nftl, unsigned int address, int
>  			return -1;
>  
>  		if (check_oob) {
> +			if (mtd->oobsize > sizeof(buf)) {
> +				pr_warn("MTD oobsize > SECTORSIZE: %d\n",
> +					mtd->oobsize);
> +				return -1;
> +			}
>  			if(nftl_read_oob(mtd, address, mtd->oobsize,
> -					 &retlen, &buf[SECTORSIZE]) < 0)
> +					 &retlen, buf) < 0)
>  				return -1;
> -			if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
> +			if (memcmpb(buf, 0xff, mtd->oobsize) != 0)
>  				return -1;
>  		}
>  		address += SECTORSIZE;

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mtd: nftl: Remove VLA usage
  2018-04-29  9:16 ` Boris Brezillon
@ 2018-04-29 14:31   ` Kees Cook
  2018-04-29 15:58     ` Boris Brezillon
  0 siblings, 1 reply; 4+ messages in thread
From: Kees Cook @ 2018-04-29 14:31 UTC (permalink / raw)
  To: Boris Brezillon
  Cc: LKML, David Woodhouse, Brian Norris, Marek Vasut,
	Richard Weinberger, Linux mtd

On Sun, Apr 29, 2018 at 2:16 AM, Boris Brezillon
<boris.brezillon@bootlin.com> wrote:
> Hi Kees,
>
> On Mon, 23 Apr 2018 13:35:00 -0700
> Kees Cook <keescook@chromium.org> wrote:
>
>> On the quest to remove all VLAs from the kernel[1] this changes the
>> check_free_sectors() routine to use the same stack buffer for both
>> data byte checks (SECTORSIZE) and oob byte checks (oobsize). Since
>> these regions aren't needed at the same time, they don't need to be
>> consecutively allocated. Additionally, while it's possible for oobsize
>> to be large, it is unlikely to be larger than the actual SECTORSIZE. As
>> such, remove the VLA, adjust offsets and add a sanity check to make sure
>> we never get a pathological oobsize.
>>
>> [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
>>
>> Signed-off-by: Kees Cook <keescook@chromium.org>
>> ---
>>  drivers/mtd/inftlmount.c | 11 ++++++++---
>>  drivers/mtd/nftlmount.c  | 11 ++++++++---
>>  2 files changed, 16 insertions(+), 6 deletions(-)
>>
>> diff --git a/drivers/mtd/inftlmount.c b/drivers/mtd/inftlmount.c
>> index aab4f68bd36f..9cdae7f0fc2e 100644
>> --- a/drivers/mtd/inftlmount.c
>> +++ b/drivers/mtd/inftlmount.c
>> @@ -334,7 +334,7 @@ static int memcmpb(void *a, int c, int n)
>>  static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
>>       int len, int check_oob)
>>  {
>> -     u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
>> +     u8 buf[SECTORSIZE];
>
> Could we instead move to dynamic allocation. I mean, SECTORSIZE is 512
> bytes, so only with this function we consume 1/16 of the stack. Not to
> mention that some MTD drivers might want to do DMA on buffer passed by
> the MTD user, and if the buffer is on the stack they'll have to use a
> bounce buffer instead.

Sure! I can rework it to do that. Is GFP_KERNEL okay for that, or does
it need something else?

-Kees

-- 
Kees Cook
Pixel Security

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mtd: nftl: Remove VLA usage
  2018-04-29 14:31   ` Kees Cook
@ 2018-04-29 15:58     ` Boris Brezillon
  0 siblings, 0 replies; 4+ messages in thread
From: Boris Brezillon @ 2018-04-29 15:58 UTC (permalink / raw)
  To: Kees Cook
  Cc: LKML, David Woodhouse, Brian Norris, Marek Vasut,
	Richard Weinberger, Linux mtd

On Sun, 29 Apr 2018 07:31:15 -0700
Kees Cook <keescook@chromium.org> wrote:

> On Sun, Apr 29, 2018 at 2:16 AM, Boris Brezillon
> <boris.brezillon@bootlin.com> wrote:
> > Hi Kees,
> >
> > On Mon, 23 Apr 2018 13:35:00 -0700
> > Kees Cook <keescook@chromium.org> wrote:
> >  
> >> On the quest to remove all VLAs from the kernel[1] this changes the
> >> check_free_sectors() routine to use the same stack buffer for both
> >> data byte checks (SECTORSIZE) and oob byte checks (oobsize). Since
> >> these regions aren't needed at the same time, they don't need to be
> >> consecutively allocated. Additionally, while it's possible for oobsize
> >> to be large, it is unlikely to be larger than the actual SECTORSIZE. As
> >> such, remove the VLA, adjust offsets and add a sanity check to make sure
> >> we never get a pathological oobsize.
> >>
> >> [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
> >>
> >> Signed-off-by: Kees Cook <keescook@chromium.org>
> >> ---
> >>  drivers/mtd/inftlmount.c | 11 ++++++++---
> >>  drivers/mtd/nftlmount.c  | 11 ++++++++---
> >>  2 files changed, 16 insertions(+), 6 deletions(-)
> >>
> >> diff --git a/drivers/mtd/inftlmount.c b/drivers/mtd/inftlmount.c
> >> index aab4f68bd36f..9cdae7f0fc2e 100644
> >> --- a/drivers/mtd/inftlmount.c
> >> +++ b/drivers/mtd/inftlmount.c
> >> @@ -334,7 +334,7 @@ static int memcmpb(void *a, int c, int n)
> >>  static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
> >>       int len, int check_oob)
> >>  {
> >> -     u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
> >> +     u8 buf[SECTORSIZE];  
> >
> > Could we instead move to dynamic allocation. I mean, SECTORSIZE is 512
> > bytes, so only with this function we consume 1/16 of the stack. Not to
> > mention that some MTD drivers might want to do DMA on buffer passed by
> > the MTD user, and if the buffer is on the stack they'll have to use a
> > bounce buffer instead.  
> 
> Sure! I can rework it to do that. Is GFP_KERNEL okay for that, or does
> it need something else?

Just had a quick look, and I think GFP_KERNEL is good.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-04-29 15:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-23 20:35 [PATCH] mtd: nftl: Remove VLA usage Kees Cook
2018-04-29  9:16 ` Boris Brezillon
2018-04-29 14:31   ` Kees Cook
2018-04-29 15:58     ` Boris Brezillon

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.