Skip to contentSkip to conversation

zram: Fix buffer size passed to strlcpy()

PATCH In review Matthias Kaehlcke 3 participants 4 messages Jul 28, 2017 – Aug 2, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 zram: Fix buffer size passed to strlcpy()

comp_algorithm_store() passes the size of the source buffer to strlcpy() instead of the destination buffer size, fix this.

---
 drivers/block/zram/zram_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/zram/zram_drv.c +1 -1
index 856d5dc02451..7d2ddffad361 100644
@@ -327,7 +327,7 @@ static ssize_t comp_algorithm_store(struct device *dev,
 		return -EBUSY;
 	}
 
-	strlcpy(zram->compressor, compressor, sizeof(compressor));
+	strlcpy(zram->compressor, compressor, sizeof(zram->compressor));
 	up_write(&zram->init_lock);
 	return len;
 }
Signature · 1 line
2.14.0.rc0.400.g1c36432dff-goog
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 2 lines · Matthias Kaehlcke

comp_algorithm_store() passes the size of the source buffer to strlcpy() instead of the destination buffer size, fix this.

This was introduced in commit 415403be37e2 ("zram: use crypto api to check alg availability"), but probably don't need a "Fixes" since there's not really a bug (see below)

Quoted 10 lines
---
 drivers/block/zram/zram_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/zram/zram_drv.c +0 -0
index 856d5dc02451..7d2ddffad361 100644

drivers/block/zram/zram_drv.c static ssize_t comp_algorithm_store(struct device *dev, · line 330

Quoted 5 lines
drivers/block/zram/zram_drv.c +1 -1
@@ -327,7 +327,7 @@ static ssize_t comp_algorithm_store(struct device *dev,
                return -EBUSY;
        }
 
-       strlcpy(zram->compressor, compressor, sizeof(compressor));
+       strlcpy(zram->compressor, compressor, sizeof(zram->compressor));

As far as I can tell the two sizes are identical. In struct zram:

char compressor[CRYPTO_MAX_ALG_NAME];

Locally here:

char compressor[CRYPTO_MAX_ALG_NAME];

...so there is no bug per say unless there's a hidden "#undef". ...but your change does make it a little clearer, plus if someone ever changed one of these arrays it would be safer. Thus:

I suppose another option would be to define the local array based on the size of the structure. AKA locally in the function:

  char compressor[ARRAY_SIZE(zram->compressor)];

...if you did that you could replace the strlcpy() below with a simple strcpy() since you'd be guaranteed that there's be enough space. ...but I'm probably overthinking it too much. ;-P

-Doug

maintainer
#2 lore raw

Hi Doug,

Quoted 41 lines · Doug Anderson

Hi,

Quoted 2 lines · Matthias Kaehlcke

comp_algorithm_store() passes the size of the source buffer to strlcpy() instead of the destination buffer size, fix this.

This was introduced in commit 415403be37e2 ("zram: use crypto api to check alg availability"), but probably don't need a "Fixes" since there's not really a bug (see below)

Quoted 10 lines
---
 drivers/block/zram/zram_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/zram/zram_drv.c +0 -0
index 856d5dc02451..7d2ddffad361 100644

drivers/block/zram/zram_drv.c static ssize_t comp_algorithm_store(struct device *dev, · line 330

Quoted 5 lines
drivers/block/zram/zram_drv.c +1 -1
@@ -327,7 +327,7 @@ static ssize_t comp_algorithm_store(struct device *dev,
                return -EBUSY;
        }
 
-       strlcpy(zram->compressor, compressor, sizeof(compressor));
+       strlcpy(zram->compressor, compressor, sizeof(zram->compressor));

As far as I can tell the two sizes are identical. In struct zram:

char compressor[CRYPTO_MAX_ALG_NAME];

Locally here:

char compressor[CRYPTO_MAX_ALG_NAME];

...so there is no bug per say unless there's a hidden "#undef". ...but your change does make it a little clearer, plus if someone ever changed one of these arrays it would be safer. Thus:

Quoted 8 lines

I suppose another option would be to define the local array based on the size of the structure. AKA locally in the function:

  char compressor[ARRAY_SIZE(zram->compressor)];

...if you did that you could replace the strlcpy() below with a simple strcpy() since you'd be guaranteed that there's be enough space. ...but I'm probably overthinking it too much. ;-P

First of all, Thanks for the patch, Matthias. You are correct and you patch doesn't have any problem. However, I think Doug's suggestion looks better. Could you mind resending?

Thanks.

#3 lore raw

El Thu, Aug 03, 2017 at 08:44:37AM +0900 Minchan Kim ha dit:

Quoted 49 lines

Hi Doug,

Quoted 46 lines · Doug Anderson

Hi,

Quoted 2 lines · Matthias Kaehlcke

comp_algorithm_store() passes the size of the source buffer to strlcpy() instead of the destination buffer size, fix this.

This was introduced in commit 415403be37e2 ("zram: use crypto api to check alg availability"), but probably don't need a "Fixes" since there's not really a bug (see below)

Quoted 10 lines
---
 drivers/block/zram/zram_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/zram/zram_drv.c +0 -0
index 856d5dc02451..7d2ddffad361 100644

drivers/block/zram/zram_drv.c static ssize_t comp_algorithm_store(struct device *dev, · line 330

Quoted 5 lines
drivers/block/zram/zram_drv.c +1 -1
@@ -327,7 +327,7 @@ static ssize_t comp_algorithm_store(struct device *dev,
                return -EBUSY;
        }
 
-       strlcpy(zram->compressor, compressor, sizeof(compressor));
+       strlcpy(zram->compressor, compressor, sizeof(zram->compressor));

As far as I can tell the two sizes are identical. In struct zram:

char compressor[CRYPTO_MAX_ALG_NAME];

Locally here:

char compressor[CRYPTO_MAX_ALG_NAME];

...so there is no bug per say unless there's a hidden "#undef". ...but your change does make it a little clearer, plus if someone ever changed one of these arrays it would be safer. Thus:

I suppose another option would be to define the local array based on the size of the structure. AKA locally in the function:

  char compressor[ARRAY_SIZE(zram->compressor)];
Quoted 7 lines
Quoted 3 lines

...if you did that you could replace the strlcpy() below with a simple strcpy() since you'd be guaranteed that there's be enough space. ...but I'm probably overthinking it too much. ;-P

First of all, Thanks for the patch, Matthias. You are correct and you patch doesn't have any problem. However, I think Doug's suggestion looks better. Could you mind resending?

Sure, I can rework the patch.