]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/utils.c
secure coding: #2 strcpy => strlcpy
[mirror_lxc.git] / src / lxc / utils.c
index cd5edd6ab6a631348d06c9b704a5fcb3a6cbb532..1319025a1ba21758f376ce7678b1cbe0efa6d0db 100644 (file)
 #include "parse.h"
 #include "utils.h"
 
+#ifndef HAVE_STRLCPY
+#include "include/strlcpy.h"
+#endif
+
 #ifndef O_PATH
 #define O_PATH      010000000
 #endif
@@ -85,9 +89,6 @@ static int _recursive_rmdir(const char *dirname, dev_t pdev,
                struct stat mystat;
                int rc;
 
-               if (!direntp)
-                       break;
-
                if (!strcmp(direntp->d_name, ".") ||
                    !strcmp(direntp->d_name, ".."))
                        continue;
@@ -644,7 +645,8 @@ char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix)
                return NULL;
 
        if (use_as_prefix)
-               strcpy(result, sep);
+               (void)strlcpy(result, sep, result_len + 1);
+
        for (p = (char **)parts; *p; p++) {
                if (p > (char **)parts)
                        strcat(result, sep);
@@ -761,12 +763,15 @@ bool lxc_string_in_list(const char *needle, const char *haystack, char _sep)
 {
        char *token, *str, *saveptr = NULL;
        char sep[2] = { _sep, '\0' };
+       size_t len;
 
        if (!haystack || !needle)
                return 0;
 
-       str = alloca(strlen(haystack)+1);
-       strcpy(str, haystack);
+       len = strlen(haystack);
+       str = alloca(len + 1);
+       (void)strlcpy(str, haystack, len + 1);
+
        for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) {
                if (strcmp(needle, token) == 0)
                        return 1;
@@ -783,12 +788,15 @@ char **lxc_string_split(const char *string, char _sep)
        size_t result_capacity = 0;
        size_t result_count = 0;
        int r, saved_errno;
+       size_t len;
 
        if (!string)
                return calloc(1, sizeof(char *));
 
-       str = alloca(strlen(string) + 1);
-       strcpy(str, string);
+       len = strlen(string);
+       str = alloca(len + 1);
+       (void)strlcpy(str, string, len + 1);
+
        for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) {
                r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16);
                if (r < 0)
@@ -892,12 +900,15 @@ char **lxc_string_split_and_trim(const char *string, char _sep)
        size_t result_count = 0;
        int r, saved_errno;
        size_t i = 0;
+       size_t len;
 
        if (!string)
                return calloc(1, sizeof(char *));
 
-       str = alloca(strlen(string)+1);
-       strcpy(str, string);
+       len = strlen(string);
+       str = alloca(len + 1);
+       (void)strlcpy(str, string, len + 1);
+
        for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) {
                while (token[0] == ' ' || token[0] == '\t')
                        token++;
@@ -2091,9 +2102,6 @@ static int lxc_get_unused_loop_dev_legacy(char *loop_name)
                return -1;
 
        while ((dp = readdir(dir))) {
-               if (!dp)
-                       break;
-
                if (strncmp(dp->d_name, "loop", 4) != 0)
                        continue;
 
@@ -2417,8 +2425,11 @@ bool lxc_nic_exists(char *nic)
 int lxc_make_tmpfile(char *template, bool rm)
 {
        int fd, ret;
+       mode_t msk;
 
+       msk = umask(0022);
        fd = mkstemp(template);
+       umask(msk);
        if (fd < 0)
                return -1;