lkml.org 
[lkml]   [2020]   [Aug]   [13]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
Subject[PATCH] bootconfig: Fix off-by-one in xbc_node_compose_key_after()
From: Steven Rostedt (VMware) <rostedt@goodmis.org>

While reviewing some patches for bootconfig, I noticed the following
code in xbc_node_compose_key_after():

ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node),
depth ? "." : "");
if (ret < 0)
return ret;
if (ret > size) {
size = 0;
} else {
size -= ret;
buf += ret;
}

But snprintf() returns the number of bytes that would be written, not
the number of bytes that are written (ignoring the nul terminator).
This means that if the number of non null bytes written were to equal
size, then the nul byte, which snprintf() always adds, will overwrite
that last byte.

ret = snprintf(buf, 5, "hello");
printf("buf = '%s'\n", buf);
printf("ret = %d\n", ret);

produces:

buf = 'hell'
ret = 5

The string was truncated without ret being greater than 5.
Test (ret >= size) for overwrite.

Cc: stable@vger.kernel.org
Fixes: 76db5a27a827c ("bootconfig: Add Extra Boot Config support")
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
diff --git a/lib/bootconfig.c b/lib/bootconfig.c
index a5f701161f6b..42735f2b8860 100644
--- a/lib/bootconfig.c
+++ b/lib/bootconfig.c
@@ -248,7 +248,7 @@ int __init xbc_node_compose_key_after(struct xbc_node *root,
depth ? "." : "");
if (ret < 0)
return ret;
- if (ret > size) {
+ if (ret >= size) {
size = 0;
} else {
size -= ret;
\
 
 \ /
  Last update: 2020-08-14 00:31    [W:0.034 / U:1.352 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site