Skip to contentSkip to conversation

common: events: be more strict when placing the \0 character

PATCH In review Arturo Borrero Gonzalez 2 participants 3 messages Jul 14, 2014 – Jul 17, 2014 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 common: events: be more strict when placing the \0 character

Previous to this patch, the code path can potentially print an empty buffer with the \0 at the end of the buffer.

Be more strict and place the \0 character in the first position if the buffer is empty.

---
 src/common.c |   10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
src/common.c +6 -4
index 1b600f1..c81a1d8 100644
@@ -114,9 +114,10 @@ int nft_event_header_snprintf(char *buf, size_t size, uint32_t type,
 int nft_event_header_fprintf(FILE *fp, uint32_t type, uint32_t flags)
 {
 	char buf[64]; /* enough for the maximum string length above */
+	int ret;
 
-	nft_event_header_snprintf(buf, sizeof(buf), type, flags);
-	buf[sizeof(buf) - 1] = '\0';
+	ret = nft_event_header_snprintf(buf, sizeof(buf), type, flags);
+	buf[ret] = '\0';
 
 	return fprintf(fp, "%s", buf);
 }
@@ -140,9 +141,10 @@ int nft_event_footer_snprintf(char *buf, size_t size, uint32_t type,
 int nft_event_footer_fprintf(FILE *fp, uint32_t type, uint32_t flags)
 {
 	char buf[32]; /* enough for the maximum string length above */
+	int ret;
 
-	nft_event_footer_snprintf(buf, sizeof(buf), type, flags);
-	buf[sizeof(buf) - 1] = '\0';
+	ret = nft_event_footer_snprintf(buf, sizeof(buf), type, flags);
+	buf[ret] = '\0';
 
 	return fprintf(fp, "%s", buf);
 }
#1 lore raw
Quoted 16 lines · Arturo Borrero Gonzalez

Previous to this patch, the code path can potentially print an empty buffer with the \0 at the end of the buffer.

Be more strict and place the \0 character in the first position if the buffer is empty.

---
 src/common.c |   10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
src/common.c +0 -0
index 1b600f1..c81a1d8 100644

src/common.c int nft_event_header_snprintf(char *buf, size_t size, uint32_t type, · line 120

Quoted 9 lines
src/common.c +3 -2
@@ -114,9 +114,10 @@ int nft_event_header_snprintf(char *buf, size_t size, uint32_t type,
 int nft_event_header_fprintf(FILE *fp, uint32_t type, uint32_t flags)
 {
 	char buf[64]; /* enough for the maximum string length above */
+	int ret;
 
-	nft_event_header_snprintf(buf, sizeof(buf), type, flags);
-	buf[sizeof(buf) - 1] = '\0';
+	ret = nft_event_header_snprintf(buf, sizeof(buf), type, flags);
+	buf[ret] = '\0';

ret can be 64 at worst case, then you have ret[64] would be an off-by-one memory access (out of bound).

BTW, could you review all_snprintf functions? We should retain the snprintf semantics, ie. always nul-terminate strings if

offset < buffer_size

Otherwise, return the string without the nul-termination.

#2 lore raw
Quoted 7 lines · Pablo Neira Ayuso

BTW, could you review all_snprintf functions? We should retain the snprintf semantics, ie. always nul-terminate strings if

offset < buffer_size

Otherwise, return the string without the nul-termination.

Sure, thanks.

Signature · 5 lines
Arturo Borrero González
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html