[connection] Added support to validate netmask address. 32/147732/2 accepted/tizen/unified/20170913.071044 submit/tizen/20170911.125730
authorNiraj Kumar Goit <niraj.g@samsung.com>
Tue, 5 Sep 2017 10:32:14 +0000 (16:02 +0530)
committerNiraj Kumar Goit <niraj.g@samsung.com>
Tue, 5 Sep 2017 14:15:43 +0000 (19:45 +0530)
Change-Id: I901cba2c331e19eb11ada4f4ca5d709b3ce3d53d
Signed-off-by: Niraj Kumar Goit <niraj.g@samsung.com>
src/connection_profile.c

index 7f54484..de4c037 100755 (executable)
@@ -121,13 +121,18 @@ static char* __profile_get_ethernet_proxy(void)
 }
 //LCOV_EXCL_STOP
 
-static int __profile_convert_netmask_to_prefix_len(const char *netmask)
+static unsigned char __profile_convert_netmask_to_prefix_len(const char *netmask)
 {
        if (netmask == NULL)
                return 0;
 
        in_addr_t mask = inet_network(netmask);
-       int prefix_len = 0;
+       in_addr_t host = ~mask;
+       unsigned char prefix_len = 0;
+
+       /* a valid netmask must be 2^n - 1 */
+       if ((host & (host + 1)) != 0)
+               return -1;
 
        for (; mask; mask <<= 1)
                ++prefix_len;
@@ -970,6 +975,10 @@ EXPORT_API int connection_profile_set_subnet_mask(connection_profile_h profile,
                return CONNECTION_ERROR_INVALID_PARAMETER;
 
        net_info->PrefixLen = __profile_convert_netmask_to_prefix_len(subnet_mask);
+       if (net_info->PrefixLen <= 0 || net_info->PrefixLen > 31) {
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid Prefix length: %d", net_info->PrefixLen);
+               return CONNECTION_ERROR_INVALID_PARAMETER;
+       }
 
        return CONNECTION_ERROR_NONE;
 }