From 6c7c716bec3b3302c2212c9273c33f9640de8206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gr=C3=B6ber?= Date: Wed, 24 Jun 2020 15:29:59 +0200 Subject: conntrack: Replace strncpy with snprintf to improve null byte handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We currently use strncpy in a bunch of places which has this weird quirk where it doesn't write a terminating null byte if the input string is >= the max length. To mitigate this we write a null byte to the last character manually. While this works it is easy to forget. Instead we should just be using snprintf which has more sensible behaviour as it always writes a null byte even when truncating the string. Signed-off-by: Daniel Gröber Signed-off-by: Pablo Neira Ayuso --- src/conntrack/setter.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/conntrack/setter.c') diff --git a/src/conntrack/setter.c b/src/conntrack/setter.c index 7b96936..3a293b0 100644 --- a/src/conntrack/setter.c +++ b/src/conntrack/setter.c @@ -389,8 +389,7 @@ set_attr_repl_off_aft(struct nf_conntrack *ct, const void *value, size_t len) static void set_attr_helper_name(struct nf_conntrack *ct, const void *value, size_t len) { - strncpy(ct->helper_name, value, NFCT_HELPER_NAME_MAX); - ct->helper_name[NFCT_HELPER_NAME_MAX-1] = '\0'; + snprintf(ct->helper_name, NFCT_HELPER_NAME_MAX, "%s", (char *)value); } static void -- cgit v1.2.3