06b5c95
From db835f8c40e83c6392e69ffc7f2cc500f7682dd4 Mon Sep 17 00:00:00 2001
06b5c95
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com>
06b5c95
Date: Fri, 3 Sep 2021 19:23:20 +0200
06b5c95
Subject: [PATCH 10/15] Fix coverity detected issues in option.c
06b5c95
06b5c95
Error: STRING_OVERFLOW (CWE-120): [#def99]
06b5c95
dnsmasq-2.86test7/src/option.c:801: fixed_size_dest: You might overrun the 100-character fixed-size string "buff" by copying "usage[i].arg" without checking the length.
06b5c95
#  799|         if (usage[i].arg)
06b5c95
#  800|   	{
06b5c95
#  801|-> 	  strcpy(buff, usage[i].arg);
06b5c95
#  802|   	  for (j = 0; tab[j].handle; j++)
06b5c95
#  803|   	    if (tab[j].handle == *(usage[i].arg))
06b5c95
06b5c95
Error: CLANG_WARNING: [#def100]
06b5c95
dnsmasq-2.86test7/src/option.c:962:3: warning[deadcode.DeadStores]: Value stored to 'domain' is never read
06b5c95
#  960|       }
06b5c95
#  961|
06b5c95
#  962|->   domain += sprintf(domain, "in-addr.arpa");
06b5c95
#  963|
06b5c95
#  964|     return 1;
06b5c95
06b5c95
Error: CLANG_WARNING: [#def101]
06b5c95
dnsmasq-2.86test7/src/option.c:981:3: warning[deadcode.DeadStores]: Value stored to 'domain' is never read
06b5c95
#  979|         domain += sprintf(domain, "%.1x.", (i>>2) & 1 ? dig & 15 : dig >> 4);
06b5c95
#  980|       }
06b5c95
#  981|->   domain += sprintf(domain, "ip6.arpa");
06b5c95
#  982|
06b5c95
#  983|     return 1;
06b5c95
06b5c95
Error: RESOURCE_LEAK (CWE-772): [#def102] [important]
06b5c95
dnsmasq-2.86test7/src/option.c:1809: alloc_fn: Storage is returned from allocation function "opt_malloc".
06b5c95
dnsmasq-2.86test7/src/option.c:1809: var_assign: Assigning: "path" = storage returned from "opt_malloc(strlen(directory) + len + 2UL)".
06b5c95
dnsmasq-2.86test7/src/option.c:1810: noescape: Resource "path" is not freed or pointed-to in "strcpy". [Note: The source code implementation of the function has been overridden by a builtin model.]
06b5c95
dnsmasq-2.86test7/src/option.c:1811: noescape: Resource "path" is not freed or pointed-to in "strcat". [Note: The source code implementation of the function has been overridden by a builtin model.]
06b5c95
dnsmasq-2.86test7/src/option.c:1812: noescape: Resource "path" is not freed or pointed-to in "strcat". [Note: The source code implementation of the function has been overridden by a builtin model.]
06b5c95
dnsmasq-2.86test7/src/option.c:1815: noescape: Resource "path" is not freed or pointed-to in "stat".
06b5c95
dnsmasq-2.86test7/src/option.c:1809: overwrite_var: Overwriting "path" in "path = opt_malloc(strlen(directory) + len + 2UL)" leaks the storage that "path" points to.
06b5c95
# 1807|   	      continue;
06b5c95
# 1808|
06b5c95
# 1809|-> 	    path = opt_malloc(strlen(directory) + len + 2);
06b5c95
# 1810|   	    strcpy(path, directory);
06b5c95
# 1811|   	    strcat(path, "/");
06b5c95
06b5c95
Error: RESOURCE_LEAK (CWE-772): [#def103] [important]
06b5c95
dnsmasq-2.86test7/src/option.c:1809: alloc_fn: Storage is returned from allocation function "opt_malloc".
06b5c95
dnsmasq-2.86test7/src/option.c:1809: var_assign: Assigning: "path" = storage returned from "opt_malloc(strlen(directory) + len + 2UL)".
06b5c95
dnsmasq-2.86test7/src/option.c:1810: noescape: Resource "path" is not freed or pointed-to in "strcpy". [Note: The source code implementation of the function has been overridden by a builtin model.]
06b5c95
dnsmasq-2.86test7/src/option.c:1811: noescape: Resource "path" is not freed or pointed-to in "strcat". [Note: The source code implementation of the function has been overridden by a builtin model.]
06b5c95
dnsmasq-2.86test7/src/option.c:1812: noescape: Resource "path" is not freed or pointed-to in "strcat". [Note: The source code implementation of the function has been overridden by a builtin model.]
06b5c95
dnsmasq-2.86test7/src/option.c:1815: noescape: Resource "path" is not freed or pointed-to in "stat".
06b5c95
dnsmasq-2.86test7/src/option.c:1858: leaked_storage: Variable "path" going out of scope leaks the storage it points to.
06b5c95
# 1856|   	    free(files);
06b5c95
# 1857|   	  }
06b5c95
# 1858|-> 	break;
06b5c95
# 1859|         }
06b5c95
# 1860|
06b5c95
06b5c95
Error: RESOURCE_LEAK (CWE-772): [#def104] [important]
06b5c95
dnsmasq-2.86test7/src/option.c:1996: alloc_fn: Storage is returned from allocation function "canonicalise_opt".
06b5c95
dnsmasq-2.86test7/src/option.c:1996: var_assign: Assigning: "name" = storage returned from "canonicalise_opt(arg)".
06b5c95
dnsmasq-2.86test7/src/option.c:1998: leaked_storage: Variable "name" going out of scope leaks the storage it points to.
06b5c95
# 1996|   	if (!(name = canonicalise_opt(arg)) ||
06b5c95
# 1997|   	    (comma && !(target = canonicalise_opt(comma))))
06b5c95
# 1998|-> 	  ret_err(_("bad MX name"));
06b5c95
# 1999|
06b5c95
# 2000|   	new = opt_malloc(sizeof(struct mx_srv_record));
06b5c95
06b5c95
Error: RESOURCE_LEAK (CWE-772): [#def106] [important]
06b5c95
dnsmasq-2.86test7/src/option.c:3477: alloc_fn: Storage is returned from allocation function "opt_malloc".
06b5c95
dnsmasq-2.86test7/src/option.c:3477: var_assign: Assigning: "new" = storage returned from "opt_malloc(96UL)".
06b5c95
dnsmasq-2.86test7/src/option.c:3618: leaked_storage: Variable "new" going out of scope leaks the storage it points to.
06b5c95
# 3616|   		      sprintf(errstr, _("duplicate dhcp-host IP address %s"),
06b5c95
# 3617|   			      daemon->addrbuff);
06b5c95
# 3618|-> 		      return 0;
06b5c95
# 3619|   		    }
06b5c95
# 3620|   	      }
06b5c95
06b5c95
Error: RESOURCE_LEAK (CWE-772): [#def108] [important]
06b5c95
dnsmasq-2.86test7/src/option.c:3781: alloc_fn: Storage is returned from allocation function "opt_malloc".
06b5c95
dnsmasq-2.86test7/src/option.c:3781: var_assign: Assigning: "new" = storage returned from "opt_malloc(32UL)".
06b5c95
dnsmasq-2.86test7/src/option.c:3786: leaked_storage: Variable "new" going out of scope leaks the storage it points to.
06b5c95
# 3784|
06b5c95
# 3785|   	if (!(comma = split(arg)) || (len = strlen(comma)) == 0)
06b5c95
# 3786|-> 	  ret_err(gen_err);
06b5c95
# 3787|
06b5c95
# 3788|   	new->wildcard = 0;
06b5c95
06b5c95
Error: RESOURCE_LEAK (CWE-772): [#def109] [important]
06b5c95
dnsmasq-2.86test7/src/option.c:3921: alloc_fn: Storage is returned from allocation function "opt_malloc".
06b5c95
dnsmasq-2.86test7/src/option.c:3921: var_assign: Assigning: "new" = storage returned from "opt_malloc(56UL)".
06b5c95
dnsmasq-2.86test7/src/option.c:3994: leaked_storage: Variable "new" going out of scope leaks the storage it points to.
06b5c95
# 3992|   	   }
06b5c95
# 3993|
06b5c95
# 3994|-> 	 ret_err(gen_err);
06b5c95
# 3995|          }
06b5c95
# 3996|
06b5c95
06b5c95
Error: CLANG_WARNING: [#def111]
06b5c95
dnsmasq-2.86test7/src/option.c:4693:25: warning[deadcode.DeadStores]: Value stored to 'tmp' during its initialization is never read
06b5c95
# 4691|   		if (!canon)
06b5c95
# 4692|                     {
06b5c95
# 4693|-> 		    struct name_list *tmp = new->names, *next;
06b5c95
# 4694|   		    for (tmp = new->names; tmp; tmp = next)
06b5c95
# 4695|
06b5c95
---
06b5c95
 src/option.c | 33 +++++++++++++++++++++------------
06b5c95
 1 file changed, 21 insertions(+), 12 deletions(-)
06b5c95
06b5c95
diff --git a/src/option.c b/src/option.c
06b5c95
index ffce9fc..11655fd 100644
06b5c95
--- a/src/option.c
06b5c95
+++ b/src/option.c
06b5c95
@@ -798,7 +798,7 @@ static void do_usage(void)
06b5c95
 	     
06b5c95
       if (usage[i].arg)
06b5c95
 	{
06b5c95
-	  strcpy(buff, usage[i].arg);
06b5c95
+	  safe_strncpy(buff, usage[i].arg, sizeof(buff));
06b5c95
 	  for (j = 0; tab[j].handle; j++)
06b5c95
 	    if (tab[j].handle == *(usage[i].arg))
06b5c95
 	      sprintf(buff, "%d", tab[j].val);
06b5c95
@@ -959,7 +959,7 @@ static int domain_rev4(char *domain, struct in_addr addr, int msize)
06b5c95
       return 0;
06b5c95
     }
06b5c95
   
06b5c95
-  domain += sprintf(domain, "in-addr.arpa");
06b5c95
+  sprintf(domain, "in-addr.arpa");
06b5c95
   
06b5c95
   return 1;
06b5c95
 }
06b5c95
@@ -978,7 +978,7 @@ static int domain_rev6(char *domain, struct in6_addr *addr, int msize)
06b5c95
       int dig = ((unsigned char *)addr)[i>>3];
06b5c95
       domain += sprintf(domain, "%.1x.", (i>>2) & 1 ? dig & 15 : dig >> 4);
06b5c95
     }
06b5c95
-  domain += sprintf(domain, "ip6.arpa");
06b5c95
+  sprintf(domain, "ip6.arpa");
06b5c95
   
06b5c95
   return 1;
06b5c95
 }
06b5c95
@@ -1829,6 +1829,8 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
06b5c95
 		new->next = li;
06b5c95
 		*up = new;
06b5c95
 	      }
06b5c95
+	    else
06b5c95
+	      free(path);
06b5c95
 
06b5c95
 	  }
06b5c95
 
06b5c95
@@ -1995,7 +1997,11 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
06b5c95
 	
06b5c95
 	if (!(name = canonicalise_opt(arg)) || 
06b5c95
 	    (comma && !(target = canonicalise_opt(comma))))
06b5c95
-	  ret_err(_("bad MX name"));
06b5c95
+	  {
06b5c95
+	    free(name);
06b5c95
+	    free(target);
06b5c95
+	    ret_err(_("bad MX name"));
06b5c95
+	  }
06b5c95
 	
06b5c95
 	new = opt_malloc(sizeof(struct mx_srv_record));
06b5c95
 	new->next = daemon->mxnames;
06b5c95
@@ -3616,6 +3622,7 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
06b5c95
 		      inet_ntop(AF_INET, &in, daemon->addrbuff, ADDRSTRLEN);
06b5c95
 		      sprintf(errstr, _("duplicate dhcp-host IP address %s"),
06b5c95
 			      daemon->addrbuff);
06b5c95
+		      dhcp_config_free(new);
06b5c95
 		      return 0;
06b5c95
 		    }	      
06b5c95
 	      }
06b5c95
@@ -3779,16 +3786,16 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
06b5c95
 
06b5c95
     case LOPT_NAME_MATCH: /* --dhcp-name-match */
06b5c95
       {
06b5c95
-	struct dhcp_match_name *new = opt_malloc(sizeof(struct dhcp_match_name));
06b5c95
-	struct dhcp_netid *id = opt_malloc(sizeof(struct dhcp_netid));
06b5c95
+	struct dhcp_match_name *new;
06b5c95
 	ssize_t len;
06b5c95
 	
06b5c95
 	if (!(comma = split(arg)) || (len = strlen(comma)) == 0)
06b5c95
 	  ret_err(gen_err);
06b5c95
 
06b5c95
+	new = opt_malloc(sizeof(struct dhcp_match_name));
06b5c95
 	new->wildcard = 0;
06b5c95
-	new->netid = id;
06b5c95
-	id->net = opt_string_alloc(set_prefix(arg));
06b5c95
+	new->netid = opt_malloc(sizeof(struct dhcp_netid));
06b5c95
+	new->netid->net = opt_string_alloc(set_prefix(arg));
06b5c95
 
06b5c95
 	if (comma[len-1] == '*')
06b5c95
 	  {
06b5c95
@@ -3992,6 +3999,8 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
06b5c95
 	       }
06b5c95
 	   }
06b5c95
 	 
06b5c95
+	 dhcp_netid_free(new->netid);
06b5c95
+	 free(new);
06b5c95
 	 ret_err(gen_err);
06b5c95
        }
06b5c95
 	 
06b5c95
@@ -4367,7 +4376,7 @@ err:
06b5c95
     case LOPT_CNAME: /* --cname */
06b5c95
       {
06b5c95
 	struct cname *new;
06b5c95
-	char *alias, *target, *last, *pen;
06b5c95
+	char *alias, *target=NULL, *last, *pen;
06b5c95
 	int ttl = -1;
06b5c95
 
06b5c95
 	for (last = pen = NULL, comma = arg; comma; comma = split(comma))
06b5c95
@@ -4382,13 +4391,13 @@ err:
06b5c95
 	if (pen != arg && atoi_check(last, &ttl))
06b5c95
 	  last = pen;
06b5c95
 	  	
06b5c95
-    	target = canonicalise_opt(last);
06b5c95
-
06b5c95
 	while (arg != last)
06b5c95
 	  {
06b5c95
 	    int arglen = strlen(arg);
06b5c95
 	    alias = canonicalise_opt(arg);
06b5c95
 
06b5c95
+	    if (!target)
06b5c95
+	      target = canonicalise_opt(last);
06b5c95
 	    if (!alias || !target)
06b5c95
 	      {
06b5c95
 		free(target);
06b5c95
@@ -4691,7 +4700,7 @@ err:
06b5c95
 		struct name_list *nl;
06b5c95
 		if (!canon)
06b5c95
                   {
06b5c95
-		    struct name_list *tmp = new->names, *next;
06b5c95
+		    struct name_list *tmp, *next;
06b5c95
 		    for (tmp = new->names; tmp; tmp = next)
06b5c95
 		      {
06b5c95
 			next = tmp->next;
06b5c95
-- 
06b5c95
2.31.1
06b5c95