Unverified Commit 29babe5e by Donghwa Jeong Committed by Christian Brauner

secure coding: #2 strcpy => strlcpy

parent c9651be4
...@@ -51,6 +51,10 @@ ...@@ -51,6 +51,10 @@
#include "parse.h" #include "parse.h"
#include "utils.h" #include "utils.h"
#ifndef HAVE_STRLCPY
#include "include/strlcpy.h"
#endif
#define usernic_debug_stream(stream, format, ...) \ #define usernic_debug_stream(stream, format, ...) \
do { \ do { \
fprintf(stream, "%s: %d: %s: " format, __FILE__, __LINE__, \ fprintf(stream, "%s: %d: %s: " format, __FILE__, __LINE__, \
...@@ -829,9 +833,11 @@ static bool create_db_dir(char *fnam) ...@@ -829,9 +833,11 @@ static bool create_db_dir(char *fnam)
{ {
int ret; int ret;
char *p; char *p;
size_t len;
p = alloca(strlen(fnam) + 1); len = strlen(fnam);
strcpy(p, fnam); p = alloca(len + 1);
(void)strlcpy(p, fnam, len + 1);
fnam = p; fnam = p;
p = p + 1; p = p + 1;
......
...@@ -65,6 +65,10 @@ ...@@ -65,6 +65,10 @@
#include <sys/personality.h> #include <sys/personality.h>
#endif #endif
#ifndef HAVE_STRLCPY
#include "include/strlcpy.h"
#endif
lxc_log_define(lxc_confile, lxc); lxc_log_define(lxc_confile, lxc);
#define lxc_config_define(name) \ #define lxc_config_define(name) \
...@@ -2181,7 +2185,7 @@ static int set_config_uts_name(const char *key, const char *value, ...@@ -2181,7 +2185,7 @@ static int set_config_uts_name(const char *key, const char *value,
return -1; return -1;
} }
strcpy(utsname->nodename, value); (void)strlcpy(utsname->nodename, value, sizeof(utsname->nodename));
free(lxc_conf->utsname); free(lxc_conf->utsname);
lxc_conf->utsname = utsname; lxc_conf->utsname = utsname;
......
...@@ -26,6 +26,10 @@ ...@@ -26,6 +26,10 @@
#include "initutils.h" #include "initutils.h"
#include "log.h" #include "log.h"
#ifndef HAVE_STRLCPY
#include "include/strlcpy.h"
#endif
lxc_log_define(lxc_initutils, lxc); lxc_log_define(lxc_initutils, lxc);
static char *copy_global_config_value(char *p) static char *copy_global_config_value(char *p)
...@@ -35,14 +39,17 @@ static char *copy_global_config_value(char *p) ...@@ -35,14 +39,17 @@ static char *copy_global_config_value(char *p)
if (len < 1) if (len < 1)
return NULL; return NULL;
if (p[len-1] == '\n') { if (p[len-1] == '\n') {
p[len-1] = '\0'; p[len-1] = '\0';
len--; len--;
} }
retbuf = malloc(len+1);
retbuf = malloc(len + 1);
if (!retbuf) if (!retbuf)
return NULL; return NULL;
strcpy(retbuf, p);
(void)strlcpy(retbuf, p, len + 1);
return retbuf; return retbuf;
} }
...@@ -355,7 +362,7 @@ int setproctitle(char *title) ...@@ -355,7 +362,7 @@ int setproctitle(char *title)
ret = prctl(PR_SET_MM, PR_SET_MM_MAP, (long) &prctl_map, sizeof(prctl_map), 0); ret = prctl(PR_SET_MM, PR_SET_MM_MAP, (long) &prctl_map, sizeof(prctl_map), 0);
if (ret == 0) if (ret == 0)
strcpy((char*)arg_start, title); (void)strlcpy((char*)arg_start, title, len);
else else
INFO("setting cmdline failed - %s", strerror(errno)); INFO("setting cmdline failed - %s", strerror(errno));
......
...@@ -59,6 +59,10 @@ ...@@ -59,6 +59,10 @@
#include "utils.h" #include "utils.h"
#ifndef HAVE_STRLCPY
#include "include/strlcpy.h"
#endif
#define pam_cgfs_debug_stream(stream, format, ...) \ #define pam_cgfs_debug_stream(stream, format, ...) \
do { \ do { \
fprintf(stream, "%s: %d: %s: " format, __FILE__, __LINE__, \ fprintf(stream, "%s: %d: %s: " format, __FILE__, __LINE__, \
...@@ -1626,7 +1630,8 @@ static char *string_join(const char *sep, const char **parts, bool use_as_prefix ...@@ -1626,7 +1630,8 @@ static char *string_join(const char *sep, const char **parts, bool use_as_prefix
return NULL; return NULL;
if (use_as_prefix) if (use_as_prefix)
strcpy(result, sep); (void)strlcpy(result, sep, (result_len + 1) * sizeof(char));
for (p = (char **)parts; *p; p++) { for (p = (char **)parts; *p; p++) {
if (p > (char **)parts) if (p > (char **)parts)
strcat(result, sep); strcat(result, sep);
......
...@@ -37,6 +37,10 @@ ...@@ -37,6 +37,10 @@
#include "storage_utils.h" #include "storage_utils.h"
#include "utils.h" #include "utils.h"
#ifndef HAVE_STRLCPY
#include "include/strlcpy.h"
#endif
lxc_log_define(nbd, lxc); lxc_log_define(nbd, lxc);
struct nbd_attach_data { struct nbd_attach_data {
...@@ -53,10 +57,14 @@ static bool wait_for_partition(const char *path); ...@@ -53,10 +57,14 @@ static bool wait_for_partition(const char *path);
bool attach_nbd(char *src, struct lxc_conf *conf) 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; 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 */ /* if path is followed by a partition, drop that for now */
p = strchr(orig, ':'); p = strchr(orig, ':');
if (p) if (p)
......
...@@ -34,6 +34,10 @@ ...@@ -34,6 +34,10 @@
#include "storage_utils.h" #include "storage_utils.h"
#include "utils.h" #include "utils.h"
#ifndef HAVE_STRLCPY
#include "include/strlcpy.h"
#endif
lxc_log_define(rbd, lxc); lxc_log_define(rbd, lxc);
struct rbd_args { struct rbd_args {
...@@ -193,6 +197,7 @@ int rbd_destroy(struct lxc_storage *orig) ...@@ -193,6 +197,7 @@ int rbd_destroy(struct lxc_storage *orig)
char *rbdfullname; char *rbdfullname;
char cmd_output[MAXPATHLEN]; char cmd_output[MAXPATHLEN];
struct rbd_args args = {0}; struct rbd_args args = {0};
size_t len;
src = lxc_storage_get_path(orig->src, orig->type); src = lxc_storage_get_path(orig->src, orig->type);
if (file_exists(src)) { if (file_exists(src)) {
...@@ -206,9 +211,11 @@ int rbd_destroy(struct lxc_storage *orig) ...@@ -206,9 +211,11 @@ int rbd_destroy(struct lxc_storage *orig)
} }
} }
rbdfullname = alloca(strlen(src) - 8); len = strlen(src);
strcpy(rbdfullname, &src[9]); rbdfullname = alloca(len - 8);
(void)strlcpy(rbdfullname, &src[9], len - 8);
args.rbd_name = rbdfullname; args.rbd_name = rbdfullname;
ret = run_command(cmd_output, sizeof(cmd_output), ret = run_command(cmd_output, sizeof(cmd_output),
rbd_delete_wrapper, (void *)&args); rbd_delete_wrapper, (void *)&args);
if (ret < 0) { if (ret < 0) {
......
...@@ -60,6 +60,10 @@ ...@@ -60,6 +60,10 @@
#include "utils.h" #include "utils.h"
#include "zfs.h" #include "zfs.h"
#ifndef HAVE_STRLCPY
#include "include/strlcpy.h"
#endif
#ifndef BLKGETSIZE64 #ifndef BLKGETSIZE64
#define BLKGETSIZE64 _IOR(0x12, 114, size_t) #define BLKGETSIZE64 _IOR(0x12, 114, size_t)
#endif #endif
...@@ -564,9 +568,12 @@ struct lxc_storage *storage_create(const char *dest, const char *type, ...@@ -564,9 +568,12 @@ struct lxc_storage *storage_create(const char *dest, const char *type,
if (strchr(type, ',')) { if (strchr(type, ',')) {
char *dup, *token; char *dup, *token;
char *saveptr = NULL; 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; for (token = strtok_r(dup, ",", &saveptr); token;
token = strtok_r(NULL, ",", &saveptr)) { token = strtok_r(NULL, ",", &saveptr)) {
bdev = do_storage_create(dest, token, cname, specs); bdev = do_storage_create(dest, token, cname, specs);
......
...@@ -48,6 +48,10 @@ ...@@ -48,6 +48,10 @@
#include "arguments.h" #include "arguments.h"
#include "tool_utils.h" #include "tool_utils.h"
#ifndef HAVE_STRLCPY
#include "include/strlcpy.h"
#endif
int lxc_fill_elevated_privileges(char *flaglist, int *flags) int lxc_fill_elevated_privileges(char *flaglist, int *flags)
{ {
char *token, *saveptr = NULL; char *token, *saveptr = NULL;
...@@ -422,13 +426,16 @@ char **lxc_string_split(const char *string, char _sep) ...@@ -422,13 +426,16 @@ char **lxc_string_split(const char *string, char _sep)
char **tmp = NULL, **result = NULL; char **tmp = NULL, **result = NULL;
size_t result_capacity = 0; size_t result_capacity = 0;
size_t result_count = 0; size_t result_count = 0;
size_t len;
int r, saved_errno; int r, saved_errno;
if (!string) if (!string)
return calloc(1, sizeof(char *)); return calloc(1, sizeof(char *));
str = alloca(strlen(string) + 1); len = strlen(string);
strcpy(str, string); str = alloca(len + 1);
(void)strlcpy(str, string, len + 1);
for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) { for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) {
r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16); r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16);
if (r < 0) if (r < 0)
...@@ -506,7 +513,8 @@ char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix) ...@@ -506,7 +513,8 @@ char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix)
return NULL; return NULL;
if (use_as_prefix) if (use_as_prefix)
strcpy(result, sep); (void)strlcpy(result, sep, result_len + 1);
for (p = (char **)parts; *p; p++) { for (p = (char **)parts; *p; p++) {
if (p > (char **)parts) if (p > (char **)parts)
strcat(result, sep); strcat(result, sep);
...@@ -868,12 +876,15 @@ char **lxc_string_split_and_trim(const char *string, char _sep) ...@@ -868,12 +876,15 @@ char **lxc_string_split_and_trim(const char *string, char _sep)
size_t result_count = 0; size_t result_count = 0;
int r, saved_errno; int r, saved_errno;
size_t i = 0; size_t i = 0;
size_t len;
if (!string) if (!string)
return calloc(1, sizeof(char *)); return calloc(1, sizeof(char *));
str = alloca(strlen(string)+1); len = strlen(string);
strcpy(str, string); str = alloca(len + 1);
(void)strlcpy(str, string, len + 1);
for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) { for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) {
while (token[0] == ' ' || token[0] == '\t') while (token[0] == ' ' || token[0] == '\t')
token++; token++;
......
...@@ -51,6 +51,10 @@ ...@@ -51,6 +51,10 @@
#include "parse.h" #include "parse.h"
#include "utils.h" #include "utils.h"
#ifndef HAVE_STRLCPY
#include "include/strlcpy.h"
#endif
#ifndef O_PATH #ifndef O_PATH
#define O_PATH 010000000 #define O_PATH 010000000
#endif #endif
...@@ -641,7 +645,8 @@ char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix) ...@@ -641,7 +645,8 @@ char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix)
return NULL; return NULL;
if (use_as_prefix) if (use_as_prefix)
strcpy(result, sep); (void)strlcpy(result, sep, result_len + 1);
for (p = (char **)parts; *p; p++) { for (p = (char **)parts; *p; p++) {
if (p > (char **)parts) if (p > (char **)parts)
strcat(result, sep); strcat(result, sep);
...@@ -758,12 +763,15 @@ bool lxc_string_in_list(const char *needle, const char *haystack, char _sep) ...@@ -758,12 +763,15 @@ bool lxc_string_in_list(const char *needle, const char *haystack, char _sep)
{ {
char *token, *str, *saveptr = NULL; char *token, *str, *saveptr = NULL;
char sep[2] = { _sep, '\0' }; char sep[2] = { _sep, '\0' };
size_t len;
if (!haystack || !needle) if (!haystack || !needle)
return 0; return 0;
str = alloca(strlen(haystack)+1); len = strlen(haystack);
strcpy(str, haystack); str = alloca(len + 1);
(void)strlcpy(str, haystack, len + 1);
for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) { for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) {
if (strcmp(needle, token) == 0) if (strcmp(needle, token) == 0)
return 1; return 1;
...@@ -780,12 +788,15 @@ char **lxc_string_split(const char *string, char _sep) ...@@ -780,12 +788,15 @@ char **lxc_string_split(const char *string, char _sep)
size_t result_capacity = 0; size_t result_capacity = 0;
size_t result_count = 0; size_t result_count = 0;
int r, saved_errno; int r, saved_errno;
size_t len;
if (!string) if (!string)
return calloc(1, sizeof(char *)); return calloc(1, sizeof(char *));
str = alloca(strlen(string) + 1); len = strlen(string);
strcpy(str, string); str = alloca(len + 1);
(void)strlcpy(str, string, len + 1);
for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) { for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) {
r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16); r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16);
if (r < 0) if (r < 0)
...@@ -889,12 +900,15 @@ char **lxc_string_split_and_trim(const char *string, char _sep) ...@@ -889,12 +900,15 @@ char **lxc_string_split_and_trim(const char *string, char _sep)
size_t result_count = 0; size_t result_count = 0;
int r, saved_errno; int r, saved_errno;
size_t i = 0; size_t i = 0;
size_t len;
if (!string) if (!string)
return calloc(1, sizeof(char *)); return calloc(1, sizeof(char *));
str = alloca(strlen(string)+1); len = strlen(string);
strcpy(str, string); str = alloca(len + 1);
(void)strlcpy(str, string, len + 1);
for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) { for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) {
while (token[0] == ' ' || token[0] == '\t') while (token[0] == ' ' || token[0] == '\t')
token++; token++;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment