Linux block layer
 help / color / mirror / Atom feed
From: Miko Larsson <mikoxyzzz@gmail.com>
To: minchan@kernel.org, ngupta@vflare.org, senozhatsky@chromium.org,
	axboe@kernel.dk, linux-kernel@vger.kernel.org,
	linux-block@vger.kernel.org
Cc: Miko Larsson <mikoxyzzz@gmail.com>, hch@infradead.org
Subject: [PATCH v2 2/2] zram: zram_drv: replace 'strlcpy()'
Date: Fri, 17 Dec 2021 07:32:24 +0100	[thread overview]
Message-ID: <20211217063224.3474-3-mikoxyzzz@gmail.com> (raw)
In-Reply-To: <20211217063224.3474-1-mikoxyzzz@gmail.com>

'strlcpy()' shouldn't be used, and should be replaced with safer
alternatives. Cristoph Hellwig suggested [1] that 'kmemdup_nul()' should
be used in two cases instead of 'strscpy()', and that a regular
'strcpy()' should be used in the third case. [2][3]

[1] https://lore.kernel.org/all/YbsRlDYT2BfgrXRX@infradead.org/
[2] https://lore.kernel.org/all/Ybt8wY3U2ETjQijH@infradead.org/
[3] https://lore.kernel.org/all/Ybt8554NZpscKx2K@infradead.org/

Signed-off-by: Miko Larsson <mikoxyzzz@gmail.com>
---
 drivers/block/zram/zram_drv.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 4de6fe13edaf..c7c751e6ca2e 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -482,7 +482,7 @@ static ssize_t backing_dev_store(struct device *dev,
 	int err;
 	struct zram *zram = dev_to_zram(dev);
 
-	file_name = kmalloc(PATH_MAX, GFP_KERNEL);
+	file_name = kmemdup_nul(buf, PATH_MAX, GFP_KERNEL);
 	if (!file_name)
 		return -ENOMEM;
 
@@ -493,7 +493,6 @@ static ssize_t backing_dev_store(struct device *dev,
 		goto out;
 	}
 
-	strlcpy(file_name, buf, PATH_MAX);
 	/* ignore trailing newline */
 	sz = strlen(file_name);
 	if (sz > 0 && file_name[sz - 1] == '\n')
@@ -1024,10 +1023,10 @@ static ssize_t comp_algorithm_store(struct device *dev,
 		struct device_attribute *attr, const char *buf, size_t len)
 {
 	struct zram *zram = dev_to_zram(dev);
-	char compressor[ARRAY_SIZE(zram->compressor)];
+	char *compressor;
 	size_t sz;
 
-	strlcpy(compressor, buf, sizeof(compressor));
+	compressor = kmemdup_nul(buf, sizeof(zram->compressor), GFP_KERNEL);
 	/* ignore trailing newline */
 	sz = strlen(compressor);
 	if (sz > 0 && compressor[sz - 1] == '\n')
@@ -1981,7 +1980,7 @@ static int zram_add(void)
 	if (ret)
 		goto out_cleanup_disk;
 
-	strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
+	strcpy(zram->compressor, default_compressor);
 
 	zram_debugfs_register(zram);
 	pr_info("Added device: %s\n", zram->disk->disk_name);
-- 
2.34.1


  parent reply	other threads:[~2021-12-17  6:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-17  6:32 [PATCH v2 0/2] Fix some formatting problems Miko Larsson
2021-12-17  6:32 ` [PATCH v2 1/2] zram: zram_drv: add SPDX license identifiers Miko Larsson
2021-12-21  8:58   ` Christoph Hellwig
2021-12-21  9:33   ` Joe Perches
2021-12-24 13:13     ` Miko Larsson
2021-12-24 16:33       ` Joe Perches
2021-12-17  6:32 ` Miko Larsson [this message]
2021-12-21  8:59   ` [PATCH v2 2/2] zram: zram_drv: replace 'strlcpy()' Christoph Hellwig

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=20211217063224.3474-3-mikoxyzzz@gmail.com \
    --to=mikoxyzzz@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=hch@infradead.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=minchan@kernel.org \
    --cc=ngupta@vflare.org \
    --cc=senozhatsky@chromium.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