diff mbox

[1/2] libmultipath: print.c: make sure lines are 0-terminated

Message ID 20170517135439.25935-2-mwilck@suse.com (mailing list archive)
State Not Applicable, archived
Delegated to: christophe varoqui
Headers show

Commit Message

Martin Wilck May 17, 2017, 1:54 p.m. UTC
Under certain conditions, the output of "multipath -ll" or
"multipathd show topology", or the multipathd logs, may
contain garbage characters. Fix that by making sure that
the strings are always properly zero-terminated.

Note 1: The way this is coded, the previously written
character is overwritten by the newline. That behavior is
unchanged by this patch. I didn't want to fuzz with the
carefully crafted print.c code more than necessary at
this point.

Note 2: The condition (c <= line + len - 1) is equivalent to
(TAIL >= 0). It should always hold the way ENDLINE is called
after PRINT and PAD in print.c, and is only added as an
additional safeguard, e.g. against future code changes.
---
 libmultipath/print.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

Comments

Johannes Thumshirn May 17, 2017, 2:07 p.m. UTC | #1
Hi Martin,

On 05/17/2017 03:54 PM, Martin Wilck wrote:
> Under certain conditions, the output of "multipath -ll" or
> "multipathd show topology", or the multipathd logs, may
> contain garbage characters. Fix that by making sure that
> the strings are always properly zero-terminated.
> 
> Note 1: The way this is coded, the previously written
> character is overwritten by the newline. That behavior is
> unchanged by this patch. I didn't want to fuzz with the
> carefully crafted print.c code more than necessary at
> this point.
> 
> Note 2: The condition (c <= line + len - 1) is equivalent to
> (TAIL >= 0). It should always hold the way ENDLINE is called
> after PRINT and PAD in print.c, and is only added as an
> additional safeguard, e.g. against future code changes.

^^ Missing S-o-b
Bart Van Assche May 17, 2017, 2:17 p.m. UTC | #2
On Wed, 2017-05-17 at 15:54 +0200, Martin Wilck wrote:
>  #define ENDLINE \
> -		if (c > line) \
> -			line[c - line - 1] = '\n'
> +		if (c > line) {				\
> +			if (c <= line + len - 1) {	\
> +				*(c - 1) = '\n';	\
> +				*c = '\0';		\
> +			} else				\
> +				line[len - 1] = '\0';	\
> +		}

Hello Martin,

Please convert ENDLINE from a macro into a function. Functions are
easier to maintain than macros.

Thanks,

Bart.

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel
diff mbox

Patch

diff --git a/libmultipath/print.c b/libmultipath/print.c
index 7c2a1588..3a07fcf5 100644
--- a/libmultipath/print.c
+++ b/libmultipath/print.c
@@ -40,8 +40,14 @@  do { \
 } while (0)
 
 #define ENDLINE \
-		if (c > line) \
-			line[c - line - 1] = '\n'
+		if (c > line) {				\
+			if (c <= line + len - 1) {	\
+				*(c - 1) = '\n';	\
+				*c = '\0';		\
+			} else				\
+				line[len - 1] = '\0';	\
+		}
+
 #define PRINT(var, size, format, args...) \
 do { \
 	fwd = snprintf(var, size, format, ##args); \