]> 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 7525346e906dc83ce5788a9cf145dbe6b8676101..1319025a1ba21758f376ce7678b1cbe0efa6d0db 100644 (file)
@@ -31,6 +31,7 @@
 #include <grp.h>
 #include <inttypes.h>
 #include <libgen.h>
+#include <pthread.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include "parse.h"
 #include "utils.h"
 
+#ifndef HAVE_STRLCPY
+#include "include/strlcpy.h"
+#endif
+
 #ifndef O_PATH
 #define O_PATH      010000000
 #endif
@@ -84,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;
@@ -495,7 +497,7 @@ struct lxc_popen_FILE *lxc_popen(const char *command)
                if (ret < 0)
                        _exit(EXIT_FAILURE);
 
-               ret = sigprocmask(SIG_UNBLOCK, &mask, NULL);
+               ret = pthread_sigmask(SIG_UNBLOCK, &mask, NULL);
                if (ret < 0)
                        _exit(EXIT_FAILURE);
 
@@ -643,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);
@@ -760,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;
@@ -782,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)
@@ -891,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++;
@@ -1813,17 +1825,16 @@ int lxc_count_file_lines(const char *fn)
 
 /* Check whether a signal is blocked by a process. */
 /* /proc/pid-to-str/status\0 = (5 + 21 + 7 + 1) */
-#define __PROC_STATUS_LEN (5 + (LXC_NUMSTRLEN64) + 7 + 1)
-bool task_blocking_signal(pid_t pid, int signal)
+#define __PROC_STATUS_LEN (6 + (LXC_NUMSTRLEN64) + 7 + 1)
+bool task_blocks_signal(pid_t pid, int signal)
 {
-       bool bret = false;
-       char *line = NULL;
-       long unsigned int sigblk = 0;
-       size_t n = 0;
        int ret;
-       FILE *f;
-
        char status[__PROC_STATUS_LEN];
+       FILE *f;
+       uint64_t sigblk = 0, one = 1;
+       size_t n = 0;
+       bool bret = false;
+       char *line = NULL;
 
        ret = snprintf(status, __PROC_STATUS_LEN, "/proc/%d/status", pid);
        if (ret < 0 || ret >= __PROC_STATUS_LEN)
@@ -1834,14 +1845,20 @@ bool task_blocking_signal(pid_t pid, int signal)
                return bret;
 
        while (getline(&line, &n, f) != -1) {
-               if (strncmp(line, "SigBlk:\t", 8))
+               char *numstr;
+
+               if (strncmp(line, "SigBlk:", 7))
                        continue;
 
-               if (sscanf(line + 8, "%lx", &sigblk) != 1)
+               numstr = lxc_trim_whitespace_in_place(line + 7);
+               ret = lxc_safe_uint64(numstr, &sigblk, 16);
+               if (ret < 0)
                        goto out;
+
+               break;
        }
 
-       if (sigblk & (1LU << (signal - 1)))
+       if (sigblk & (one << (signal - 1)))
                bret = true;
 
 out:
@@ -1958,7 +1975,7 @@ int lxc_safe_ulong(const char *numstr, unsigned long *converted)
        return 0;
 }
 
-int lxc_safe_uint64(const char *numstr, uint64_t *converted)
+int lxc_safe_uint64(const char *numstr, uint64_t *converted, int base)
 {
        char *err = NULL;
        uint64_t u;
@@ -1970,7 +1987,7 @@ int lxc_safe_uint64(const char *numstr, uint64_t *converted)
                return -EINVAL;
 
        errno = 0;
-       u = strtoull(numstr, &err, 0);
+       u = strtoull(numstr, &err, base);
        if (errno == ERANGE && u == ULLONG_MAX)
                return -ERANGE;
 
@@ -2085,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;
 
@@ -2233,7 +2247,7 @@ int run_command(char *buf, size_t buf_size, int (*child_fn)(void *), void *args)
        int ret, fret, pipefd[2];
        ssize_t bytes;
 
-       /* Make sure our callers do not receive unitialized memory. */
+       /* Make sure our callers do not receive uninitialized memory. */
        if (buf_size > 0 && buf)
                buf[0] = '\0';
 
@@ -2411,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;