zram: fix bug storing backing_dev
A message that is not in the archive (sent off-list or not yet received): [email protected]
Minchan Kim
Acked-byQuoted 9 lines · Peter Kalauskas
The call to strlcpy in
backing_dev_storeis incorrect. It should take the size of the destination buffer instead of the size of the source buffer. Additionally, ignore the newline character (\n) when reading the newfile_namebuffer. This makes it possible to set thebacking_devas follows:echo
/dev/sdX>/sys/block/zram0/backing_devSigned-off-by Peter Kalauskas <[email protected]>
Quoted 32 lines
--- drivers/block/zram/zram_drv.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)drivers/block/zram/zram_drv.c +6
-1@@ -336,6 +336,7 @@ static ssize_t backing_dev_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { char *file_name; +size_t sz;struct file *backing_dev =NULL ;struct inode *inode;struct address_space *mapping;@@ -356,7 +357,11 @@ static ssize_t backing_dev_store(struct device *dev, goto out; } -strlcpy (file_name, buf, len); +strlcpy (file_name, buf,PATH_MAX ); +/* ignore trailing newline */ + sz =strlen (file_name); +if (sz >0 && file_name[sz -1 ] =='\n' ) + file_name[sz -1 ] =0x00 ; backing_dev =filp_open (file_name,O_RDWR |O_LARGEFILE ,0 );if (IS_ERR (backing_dev)) {Signature · 1 line
2.18.0.597.ga71716f1ad-goog
Sergey Senozhatsky
Reviewed-byQuoted 8 lines · Minchan Kim
Quoted 8 lines
The call to strlcpy in
backing_dev_storeis incorrect. It should take the size of the destination buffer instead of the size of the source buffer. Additionally, ignore the newline character (\n) when reading the newfile_namebuffer. This makes it possible to set thebacking_devas follows:echo
/dev/sdX>/sys/block/zram0/backing_dev
Quoted 7 lines
Quoted 1 line
Signed-off-by Peter Kalauskas <[email protected]>Acked-by Minchan Kim <[email protected]>Cc Andrew Morton <[email protected]>Cc Sergey Senozhatsky <[email protected]>CC LKML <[email protected]>Cc <[email protected]> [4.14+]
Thanks for Cc-ing Minchan.
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +0
-1- strlcpy(file_name, buf, len);
This is quite interesting. The reason it worked before was the fact that strlcpy() copies 'len - 1' bytes, which is strlen(buf) - 1 in our case, so it accidentally didn't copy the trailing new line symbol. Which also means that "echo -n /dev/sdX" most likely was broken.
-ss
Quoted 19 lines · On Mon, 13 Aug 2018 16:38:25 +0900 Sergey Senozhatsky…
Quoted 8 lines · Minchan Kim
Quoted 8 lines
The call to strlcpy in
backing_dev_storeis incorrect. It should take the size of the destination buffer instead of the size of the source buffer. Additionally, ignore the newline character (\n) when reading the newfile_namebuffer. This makes it possible to set thebacking_devas follows:echo
/dev/sdX>/sys/block/zram0/backing_devQuoted 7 lines
Quoted 1 line
Signed-off-by Peter Kalauskas <[email protected]>Acked-by Minchan Kim <[email protected]>Cc Andrew Morton <[email protected]>Cc Sergey Senozhatsky <[email protected]>CC LKML <[email protected]>Cc <[email protected]> [4.14+]Thanks for Cc-ing Minchan.
Quoted 9 lines
Reviewed-by Sergey Senozhatsky <[email protected]>Quoted 1 line
Review
Quoted 1 line
(quoted diff) +0
-1- strlcpy(file_name, buf, len);This is quite interesting. The reason it worked before was the fact that
strlcpy()copies 'len - 1' bytes, which is strlen(buf) - 1 in our case, so it accidentally didn't copy the trailing new line symbol. Which also means that "echo -n/dev/sdX" most likely was broken.
I can't find the original email on lkml for some reason, but I recreated the patch.
The changelog doesn't describe the end-user impact of the bug, which is very desirable when tagging a patch for -stable backporting. Can we have that paragraph please?
The implementation might be able to use strim() somehow.
Quoted 11 lines · Andrew Morton
Quoted 8 lines
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +0
-1- strlcpy(file_name, buf, len);This is quite interesting. The reason it worked before was the fact that
strlcpy()copies 'len - 1' bytes, which is strlen(buf) - 1 in our case, so it accidentally didn't copy the trailing new line symbol. Which also means that "echo -n/dev/sdX" most likely was broken.I can't find the original email on lkml for some reason, but I recreated the patch.
Neither can I.
Quoted 3 lines
The changelog doesn't describe the end-user impact of the bug, which is very desirable when tagging a patch for -stable backporting. Can we have that paragraph please?
The problem is that strlcpy() copies as many bytes as the source string has, not as many bytes as destination string can fit.
IOW:
char dst[100]; char src[1000]; ... strlcpy(dst, src, strlen(src));
where it should do
strlcpy(dst, src, strlen(dst));
Quoted 1 line
The implementation might be able to use
strim()somehow.
strim() trims white-spaces. What we have here is a trailing new line symbol, which echo appends to the string it writes to the kernel [echo -n switch disables it]. So we receive a "/dev/name\n" device name from sysfs, which we unsuccessfully try to open(). To make it all work we need to remove that trailing new line.
A side note,
There is sysfs_strcmp(), which takes care of that "user space may append a new line to the string" case, I wonder if we should finally have sysfs_strcpy(), which would not copy the trailing new line. I think this "if string[sz - 1] == '\n' then string[sz - 1] == 0x00" is quite common.
-ss
A bunch of corrections
Quoted 2 lines · Sergey Senozhatsky
The problem is that
strlcpy()copies as many bytes as the source string has, not as many bytes as destination string can fit.
I mean - strlcpy() expects that the 3rd argument will be sizeof(dst), but we passed the wrong argument strlen(src), so we used strlcpy() like if it was strncpy(). The difference between strncpy() and strlcpy() is in the 3rd argument.
Quoted 10 lines
IOW:
char dst[100]; char src[1000]; ... strlcpy(dst, src, strlen(src));where it should do
strlcpy(dst, src, strlen(dst));
^^^ sizeof()
Quoted 2 lines
A side note,
There issysfs_strcmp(), which takes care of that "user space may append
^^^ sysfs_streq() -ss
Quoted 3 lines · On Thu, 16 Aug 2018 10:48:35 +0900 Sergey Senozhatsky…
Quoted 1 line
The implementation might be able to use
strim()somehow.
strim()trims white-spaces.
Which includes \n.
Quoted 11 lines
What we have here is a trailing new line symbol,
which echo appends to the string it writes to the kernel [echo -n switch disables it]. So we receive a "/dev/name\n" device name from sysfs, which we unsuccessfully try toopen(). To make it all work we need to remove that trailing new line.A side note,
There issysfs_strcmp(), which takes care of that "user space may append a new line to the string" case, I wonder if we should finally havesysfs_strcpy(), which would not copy the trailing new line. I think this "if string[sz - 1] == '\n' then string[sz - 1] ==0x00" is quite common.
Sure, some additional well-chosen helpers here would be good. There's a LOT of code which does basically-the-same-thing with sysfs input. And a lot of it misses things, such as leading whitespace. Passing all this through helpers would provide consistency as well as code-size reductions, improved reviewability, etc.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 3 lines · Andrew Morton
The changelog doesn't describe the end-user impact of the bug, which is very desirable when tagging a patch for -stable backporting. Can we have that paragraph please?
The end-user impact is that echoing to writeback_dev would not cause it to change. Would the following statement suffice?
Without this change, it is not possible to change writeback_dev unless the tool used to do so does not include a newline character. For example, 'echo -n' or 'printf' would work, but 'echo' would not work.
I can also include Sergey's description of the bug if that would be helpful.
Quoted 10 lines · Sergey Senozhatsky
Quoted 9 lines · Andrew Morton
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
-
strlcpy(file_name, buf, len);This is quite interesting. The reason it worked before was the fact that
strlcpy()copies 'len - 1' bytes, which is strlen(buf) - 1 in our case, so it accidentally didn't copy the trailing new line symbol. Which also means that "echo -n/dev/sdX" most likely was broken.
Quoted 5 lines
Quoted 2 lines
I can't find the original email on lkml for some reason, but I recreated the patch.
Neither can I.
Sorry, I sent it to Minchan directly. I'll send PATCH v2 to lkml, if v2 is needed.
Quoted 8 lines · Andrew Morton
Quoted 3 lines · On Thu, 16 Aug 2018 10:48:35 +0900 Sergey Senozhatsky…
Quoted 1 line
The implementation might be able to use
strim()somehow.
strim()trims white-spaces.Which includes \n.
The if statement is already used by comp_algorithm_store in zram_drv.c, so perhaps it should remain the if statement for consistency. What do you think, should I use strim or the if statement?
A message that is not in the archive (sent off-list or not yet received): 20180822004504.GB2218@jagdpanzerIV
Quoted 5 lines · Sergey Senozhatsky
Hi,
Did you intend to remove lkml mailing list from Cc?
No, that was an accident. I resent it so it's on the record.
I will submit a v2 patch with an updated commit message then.