]> granicus.if.org Git - libnl/commitdiff
route: fix strncpy() warning from coverity about unterminated string
authorThomas Haller <thaller@redhat.com>
Thu, 8 Aug 2019 08:16:54 +0000 (10:16 +0200)
committerThomas Haller <thaller@redhat.com>
Thu, 8 Aug 2019 15:01:21 +0000 (17:01 +0200)
Coverity says:

  Error: BUFFER_SIZE_WARNING (CWE-120): [#def1]
  libnl-3.4.0/lib/route/cls/ematch/text.c:94: buffer_size_warning: Calling strncpy with a maximum size argument of 16 bytes on destination array "t->cfg.algo" of size 16 bytes might leave the destination string unterminated.
  #   92|    struct text_data *t = rtnl_ematch_data(e);
  #   93|
  #   94|->  strncpy(t->cfg.algo, algo, sizeof(t->cfg.algo));
  #   95|   }
  #   96|

  Error: BUFFER_SIZE_WARNING (CWE-120): [#def11]
  libnl-3.4.0/lib/xfrm/sa.c:1192: buffer_size_warning: Calling strncpy with a maximum size argument of 64 bytes on destination array "auth->alg_name" of size 64 bytes might leave the destination string unterminated.
  # 1190|    }
  # 1191|
  # 1192|->  strncpy(auth->alg_name, tmpl->auth->alg_name, sizeof(auth->alg_name));
  # 1193|    auth->alg_key_len = tmpl->auth->alg_key_len;
  # 1194|    memcpy(auth->alg_key, tmpl->auth->alg_key, (tmpl->auth->alg_key_len + 7) / 8);

lib/route/cls/ematch/text.c
lib/xfrm/sa.c

index b14c4abb92a7ff4806f08f74c28dd53b70e6b0d7..12a1e747b48a138ee9fd4c1854a88e6e2f3ece77 100644 (file)
@@ -92,6 +92,7 @@ void rtnl_ematch_text_set_algo(struct rtnl_ematch *e, const char *algo)
        struct text_data *t = rtnl_ematch_data(e);
 
        strncpy(t->cfg.algo, algo, sizeof(t->cfg.algo));
+       t->cfg.algo[sizeof(t->cfg.algo) - 1] = '\0';
 }
 
 char *rtnl_ematch_text_get_algo(struct rtnl_ematch *e)
index 995df9fd976999ce9099c59ec9d5bb28fa392ce8..15a3661a9699ec36d703274b8d6d16411ae318d8 100644 (file)
@@ -1190,6 +1190,7 @@ static int build_xfrm_sa_message(struct xfrmnl_sa *tmpl, int cmd, int flags, str
                        }
 
                        strncpy(auth->alg_name, tmpl->auth->alg_name, sizeof(auth->alg_name));
+                       auth->alg_name[sizeof(auth->alg_name) - 1] = '\0';
                        auth->alg_key_len = tmpl->auth->alg_key_len;
                        memcpy(auth->alg_key, tmpl->auth->alg_key, (tmpl->auth->alg_key_len + 7) / 8);
                        if (nla_put(msg, XFRMA_ALG_AUTH, len, auth) < 0) {