]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
secure coding: #2 strcpy => strlcpy
authorDonghwa Jeong <dh48.jeong@samsung.com>
Mon, 18 Jun 2018 08:18:14 +0000 (17:18 +0900)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 10 Dec 2018 13:26:46 +0000 (14:26 +0100)
Signed-off-by: Donghwa Jeong <dh48.jeong@samsung.com>
src/lxc/initutils.c
src/lxc/lxc_user_nic.c
src/lxc/storage/nbd.c
src/lxc/storage/storage.c
src/lxc/utils.c

index 56926fb5fcef0724145b7685b6d9dc909e1b7aa7..b95bffdfe95a51f8ac6380513b77aa1e204f84ef 100644 (file)
 #include "initutils.h"
 #include "log.h"
 
+#ifndef HAVE_STRLCPY
+#include "include/strlcpy.h"
+#endif
+
 lxc_log_define(lxc_initutils, lxc);
 
 static char *copy_global_config_value(char *p)
@@ -35,14 +39,17 @@ static char *copy_global_config_value(char *p)
 
        if (len < 1)
                return NULL;
+
        if (p[len-1] == '\n') {
                p[len-1] = '\0';
                len--;
        }
-       retbuf = malloc(len+1);
+
+       retbuf = malloc(len + 1);
        if (!retbuf)
                return NULL;
-       strcpy(retbuf, p);
+
+       (void)strlcpy(retbuf, p, len + 1);
        return retbuf;
 }
 
@@ -355,7 +362,7 @@ int setproctitle(char *title)
 
        ret = prctl(PR_SET_MM, PR_SET_MM_MAP, (long) &prctl_map, sizeof(prctl_map), 0);
        if (ret == 0)
-               strcpy((char*)arg_start, title);
+               (void)strlcpy((char*)arg_start, title, len);
        else
                INFO("setting cmdline failed - %s", strerror(errno));
 
index d79b02976eb2efdd8e5f5b7d4a2b1fc8f01de40a..803eae38b27b5be4525f3f85b57913e4382b0054 100644 (file)
 #include "network.h"
 #include "utils.h"
 
+#ifndef HAVE_STRLCPY
+#include "include/strlcpy.h"
+#endif
+
 #define usernic_debug_stream(stream, format, ...)                              \
        do {                                                                   \
                fprintf(stream, "%s: %d: %s: " format, __FILE__, __LINE__,     \
@@ -828,9 +832,11 @@ static bool create_db_dir(char *fnam)
 {
        int ret;
        char *p;
+       size_t len;
 
-       p = alloca(strlen(fnam) + 1);
-       strcpy(p, fnam);
+       len = strlen(fnam);
+       p = alloca(len + 1);
+       (void)strlcpy(p, fnam, len + 1);
        fnam = p;
        p = p + 1;
 
index c9dacf14e92abcc5629ed279787d06f5c42979f4..494be355a886c90518cf5503d251198f15906af7 100644 (file)
 #include "storage_utils.h"
 #include "utils.h"
 
+#ifndef HAVE_STRLCPY
+#include "include/strlcpy.h"
+#endif
+
 lxc_log_define(nbd, lxc);
 
 struct nbd_attach_data {
@@ -53,10 +57,14 @@ static bool wait_for_partition(const char *path);
 
 bool attach_nbd(char *src, struct lxc_conf *conf)
 {
-       char *orig = alloca(strlen(src)+1), *p, path[50];
+       char *orig, *p, path[50];
        int i = 0;
+       size_t len;
+
+       len = strlen(src);
+       orig = alloca(len + 1);
+       (void)strlcpy(orig, src, len + 1);
 
-       strcpy(orig, src);
        /* if path is followed by a partition, drop that for now */
        p = strchr(orig, ':');
        if (p)
index 6ecb6b8ca1695e222ebea1a11c181af0c1072f8d..4be68d2ae5b5c8de3806ae27a3272045e4f54102 100644 (file)
 #include "utils.h"
 #include "zfs.h"
 
+#ifndef HAVE_STRLCPY
+#include "include/strlcpy.h"
+#endif
+
 #ifndef BLKGETSIZE64
 #define BLKGETSIZE64 _IOR(0x12, 114, size_t)
 #endif
@@ -510,9 +514,12 @@ struct lxc_storage *storage_create(const char *dest, const char *type,
        if (strchr(type, ',')) {
                char *dup, *token;
                char *saveptr = NULL;
+               size_t len;
+
+               len = strlen(type);
+               dup = alloca(len + 1);
+               (void)strlcpy(dup, type, len + 1);
 
-               dup = alloca(strlen(type) + 1);
-               strcpy(dup, type);
                for (token = strtok_r(dup, ",", &saveptr); token;
                     token = strtok_r(NULL, ",", &saveptr)) {
                        bdev = do_storage_create(dest, token, cname, specs);
index 2e4934f848f1516a5ec6239b6f80849ffa572c4f..ccd7469ec9415a0e8b31db505d1dbf897a4207f5 100644 (file)
 #include "parse.h"
 #include "utils.h"
 
+#ifndef HAVE_STRLCPY
+#include "include/strlcpy.h"
+#endif
+
 #ifndef O_PATH
 #define O_PATH      010000000
 #endif
@@ -641,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);
@@ -758,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;
@@ -780,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)
@@ -821,12 +832,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++;