diff options
-rw-r--r-- | libsandbox/canonicalize.c | 4 | ||||
-rw-r--r-- | libsandbox/libsandbox.c | 36 | ||||
-rw-r--r-- | libsandbox/memory.c | 18 | ||||
-rw-r--r-- | libsbutil/Makefile.am | 1 | ||||
-rw-r--r-- | libsbutil/include/rcscripts/rcutil.h | 2 | ||||
-rw-r--r-- | libsbutil/include/rcscripts/util/debug.h | 25 | ||||
-rw-r--r-- | libsbutil/include/rcscripts/util/str_list.h | 2 | ||||
-rw-r--r-- | libsbutil/include/rcscripts/util/string.h | 3 | ||||
-rw-r--r-- | libsbutil/sb_memory.c | 88 | ||||
-rw-r--r-- | libsbutil/sbutil.h | 19 | ||||
-rw-r--r-- | libsbutil/src/config.c | 16 | ||||
-rw-r--r-- | libsbutil/src/debug.c | 92 | ||||
-rw-r--r-- | libsbutil/src/dynbuf.c | 14 | ||||
-rw-r--r-- | libsbutil/src/string.c | 26 | ||||
-rw-r--r-- | src/environ.c | 12 | ||||
-rw-r--r-- | src/sandbox.c | 3 |
16 files changed, 127 insertions, 234 deletions
diff --git a/libsandbox/canonicalize.c b/libsandbox/canonicalize.c index e0eec98..b88c46f 100644 --- a/libsandbox/canonicalize.c +++ b/libsandbox/canonicalize.c @@ -76,8 +76,6 @@ erealpath(const char *name, char *resolved) if (resolved == NULL) { rpath = xmalloc(path_max); - if (rpath == NULL) - return NULL; } else rpath = resolved; rpath_limit = rpath + path_max; @@ -133,8 +131,6 @@ erealpath(const char *name, char *resolved) else new_size += path_max; new_rpath = (char *) xrealloc(rpath, new_size); - if (new_rpath == NULL) - goto error; rpath = new_rpath; rpath_limit = rpath + new_size; diff --git a/libsandbox/libsandbox.c b/libsandbox/libsandbox.c index 3910296..4932806 100644 --- a/libsandbox/libsandbox.c +++ b/libsandbox/libsandbox.c @@ -195,8 +195,6 @@ static char *resolve_path(const char *path, int follow_link) save_errno(); filtered_path = xmalloc(SB_PATH_MAX * sizeof(char)); - if (NULL == filtered_path) - return NULL; if (0 == follow_link) { if (-1 == canonicalize(path, filtered_path)) { @@ -328,10 +326,6 @@ static char *getcmdline(void) } proc_data = rc_dynbuf_new(); - if (NULL == proc_data) { - SB_EERROR("ISE ", "Could not allocate dynamic buffer!\n"); - return NULL; - } fd = sb_open(PROC_SELF_CMDLINE, O_RDONLY, 0); if (fd < 0) { @@ -526,11 +520,7 @@ static void init_env_entries(char ***prefixes_array, int *prefixes_num, const ch /* num_delimiters might be 0, and we need 2 entries at least */ pfx_array = xmalloc(((num_delimiters * 2) + 2) * sizeof(char *)); - if (NULL == pfx_array) - goto error; - buffer = rc_strndup(prefixes_env, prefixes_env_length); - if (NULL == buffer) - goto error; + buffer = strdup(prefixes_env); buffer_ptr = buffer; #ifdef HAVE_STRTOK_R @@ -549,17 +539,13 @@ static void init_env_entries(char ***prefixes_array, int *prefixes_num, const ch /* Now add the realpath if it exists and * are not a duplicate */ rpath = xmalloc(SB_PATH_MAX * sizeof(char)); - if (NULL != rpath) { - pfx_item = realpath(*(&(pfx_item) - 1), rpath); - if ((NULL != pfx_item) && - (0 != strcmp(*(&(pfx_item) - 1), pfx_item))) { - pfx_num++; - } else { - free(rpath); - pfx_item = NULL; - } + pfx_item = realpath(*(&(pfx_item) - 1), rpath); + if ((NULL != pfx_item) && + (0 != strcmp(*(&(pfx_item) - 1), pfx_item))) { + pfx_num++; } else { - goto error; + free(rpath); + pfx_item = NULL; } } @@ -575,10 +561,6 @@ static void init_env_entries(char ***prefixes_array, int *prefixes_num, const ch done: errno = old_errno; return; - -error: - SB_EERROR("ISE ", "Unrecoverable error!\n"); - abort(); } static int check_prefixes(char **prefixes, int num_prefixes, const char *path) @@ -917,10 +899,6 @@ int before_syscall(int dirfd, int sb_nr, const char *func, const char *file) if (0 == sb_init) { init_context(&sbcontext); cached_env_vars = xcalloc(4, sizeof(char *)); - if (NULL == cached_env_vars) { - SB_EERROR("ISE ", "Unrecoverable error!\n"); - abort(); - } sb_init = 1; } diff --git a/libsandbox/memory.c b/libsandbox/memory.c index cd208dd..d6f4b00 100644 --- a/libsandbox/memory.c +++ b/libsandbox/memory.c @@ -44,13 +44,7 @@ void free(void *ptr) void *calloc(size_t nmemb, size_t size) { - void *ret; - size_t malloc_size = nmemb * size; - ret = malloc(malloc_size); /* dont care about overflow */ - if (ret == NULL) - return NULL; - memset(ret, 0x00, malloc_size); - return ret; + return xzalloc(nmemb * size); /* dont care about overflow */ } void *realloc(void *ptr, size_t size) @@ -59,16 +53,14 @@ void *realloc(void *ptr, size_t size) size_t old_malloc_size; if (ptr == NULL) - return malloc(size); + return xmalloc(size); if (size == 0) { free(ptr); return ptr; } old_malloc_size = SB_MALLOC_TO_SIZE(ptr); - ret = malloc(size); - if (ret == NULL) - return NULL; + ret = xmalloc(size); memcpy(ret, ptr, MIN(size, old_malloc_size)); free(ptr); return ret; @@ -83,8 +75,6 @@ char *strdup(const char *s) return NULL; len = strlen(s); - ret = malloc(len + 1); - if (ret == NULL) - return NULL; + ret = xmalloc(len + 1); return memcpy(ret, s, len + 1); } diff --git a/libsbutil/Makefile.am b/libsbutil/Makefile.am index df2e10f..9de8b0a 100644 --- a/libsbutil/Makefile.am +++ b/libsbutil/Makefile.am @@ -25,6 +25,7 @@ libsbutil_la_SOURCES = \ sb_write.c \ sb_close.c \ sb_printf.c \ + sb_memory.c \ include/rcscripts/rcutil.h \ include/rcscripts/util/str_list.h \ include/rcscripts/util/debug.h \ diff --git a/libsbutil/include/rcscripts/rcutil.h b/libsbutil/include/rcscripts/rcutil.h index 895a141..248b84a 100644 --- a/libsbutil/include/rcscripts/rcutil.h +++ b/libsbutil/include/rcscripts/rcutil.h @@ -18,4 +18,6 @@ #include "rcscripts/util/str_list.h" /* used by libsandbox/execve wrapper */ +#include "sbutil.h" + #endif /* __RCUTIL_H__ */ diff --git a/libsbutil/include/rcscripts/util/debug.h b/libsbutil/include/rcscripts/util/debug.h index a9f3e7b..8bc2371 100644 --- a/libsbutil/include/rcscripts/util/debug.h +++ b/libsbutil/include/rcscripts/util/debug.h @@ -14,10 +14,6 @@ #include <errno.h> #include <stdio.h> -#define save_errno() int old_errno = errno; -#define restore_errno() errno = old_errno; -#define saved_errno old_errno - void rc_log_domain (const char *new_domain); void @@ -115,25 +111,4 @@ bool __check_arg_fp (FILE * fp, const char *file, const char *func, size_t line) #define check_arg_fp(_fp) \ __check_arg_fp (_fp, __FILE__, __func__, __LINE__) -/* - * Various memory allocation functions and macro's. - * They set errno to ENOMEM and print debug info. - */ - -void *__xcalloc (size_t nmemb, size_t size, const char *file, const char *func, size_t line); -void *__xmalloc (size_t size, const char *file, const char *func, size_t line); -void *__xrealloc (void *ptr, size_t size, const char *file, const char *func, size_t line); - -#define xcalloc(_nmemb, _size) \ - __xcalloc (_nmemb, _size, __FILE__, __func__, __LINE__) -#define xmalloc(_size) \ - __xmalloc (_size, __FILE__, __func__, __LINE__) -#define xrealloc(_ptr, _size) \ - __xrealloc (_ptr, _size, __FILE__, __func__, __LINE__) - -char *__xstrndup (const char *str, size_t size, const char *file, const char *func, size_t line); - -#define xstrndup(_str, _size) \ - __xstrndup (_str, _size, __FILE__, __func__, __LINE__) - #endif /* __RC_DEBUG_H__ */ diff --git a/libsbutil/include/rcscripts/util/str_list.h b/libsbutil/include/rcscripts/util/str_list.h index 068f902..ac287db 100644 --- a/libsbutil/include/rcscripts/util/str_list.h +++ b/libsbutil/include/rcscripts/util/str_list.h @@ -64,7 +64,7 @@ goto _error; \ } \ _string_list = _tmp_p; \ - _tmp_str = xstrndup (_item, strlen (_item)); \ + _tmp_str = xstrdup (_item); \ if (NULL == _tmp_str) \ { \ goto _error; \ diff --git a/libsbutil/include/rcscripts/util/string.h b/libsbutil/include/rcscripts/util/string.h index 982d9a7..e7c1b2d 100644 --- a/libsbutil/include/rcscripts/util/string.h +++ b/libsbutil/include/rcscripts/util/string.h @@ -15,7 +15,4 @@ * with the malloc() call. */ char *rc_strcatpaths (const char *pathname1, const char *pathname2); -/* Compat functions for GNU extensions */ -char *rc_strndup (const char *str, size_t size); - #endif /* __RC_STRING_H__ */ diff --git a/libsbutil/sb_memory.c b/libsbutil/sb_memory.c new file mode 100644 index 0000000..23d74af --- /dev/null +++ b/libsbutil/sb_memory.c @@ -0,0 +1,88 @@ +/* + * debug.c + * + * Simle debugging/logging macro's and functions. + * + * Copyright 1999-2008 Gentoo Foundation + * Copyright 2004-2007 Martin Schlemmer <azarah@nosferatu.za.org> + * Licensed under the GPL-2 + */ + +#include "headers.h" +#include "sbutil.h" + +void * +__xcalloc(size_t nmemb, size_t size, const char *file, const char *func, size_t line) +{ + void *ret = calloc(nmemb, size); + + if (ret == NULL) { + SB_EERROR("calloc()", " %s:%s():%zu: calloc(%zu, %zu) failed: %s\n", + file, func, line, nmemb, size, strerror(errno)); + abort(); + } + + return ret; +} + +void * +__xmalloc(size_t size, const char *file, const char *func, size_t line) +{ + void *ret = malloc(size); + + if (ret == NULL) { + SB_EERROR("malloc()", " %s:%s():%zu: malloc(%zu) failed: %s\n", + file, func, line, size, strerror(errno)); + abort(); + } + + return ret; +} + +void * +__xzalloc(size_t size /*, const char *file, const char *func, size_t line */) +{ + return memset(xmalloc(size), 0x00, size); +} + +void * +__xrealloc(void *ptr, size_t size, const char *file, const char *func, size_t line) +{ + void *ret = realloc(ptr, size); + + if (ret == NULL) { + SB_EERROR("realloc()", " %s:%s():%zu: realloc(%p, %zu) failed: %s\n", + file, func, line, ptr, size, strerror(errno)); + abort(); + } + + return ret; +} + +char * +__xstrdup(const char *str, const char *file, const char *func, size_t line) +{ + char *ret = strdup(str); + + if (ret == NULL) { + SB_EERROR("strdup()", " %s:%s():%zu: strdup(%p) failed: %s\n", + file, func, line, str, strerror(errno)); + abort(); + } + + return ret; +} + +char * +__xstrndup(const char *str, size_t size, const char *file, const char *func, size_t line) +{ + char *ret = strndup(str, size); + + if (ret == NULL) { + SB_EERROR("strndup()", " %s:%s():%zu: strndup(%p, %zu) failed: %s\n", + file, func, line, str, size, strerror(errno)); + abort(); + } + + return ret; +} diff --git a/libsbutil/sbutil.h b/libsbutil/sbutil.h index 6863288..9707215 100644 --- a/libsbutil/sbutil.h +++ b/libsbutil/sbutil.h @@ -110,4 +110,23 @@ __attribute__((__format__(__printf__, 3, 4))) void sb_efunc(const char *color, c goto _error; \ } while (0) +/* Memory functions */ +void *__xcalloc(size_t nmemb, size_t size, const char *file, const char *func, size_t line); +void *__xmalloc(size_t size, const char *file, const char *func, size_t line); +void *__xzalloc(size_t size /*, const char *file, const char *func, size_t line */); +void *__xrealloc(void *ptr, size_t size, const char *file, const char *func, size_t line); +char *__xstrdup(const char *str, const char *file, const char *func, size_t line); +char *__xstrndup(const char *str, size_t size, const char *file, const char *func, size_t line); +#define xcalloc(_nmemb, _size) __xcalloc(_nmemb, _size, __FILE__, __func__, __LINE__) +#define xmalloc(_size) __xmalloc(_size, __FILE__, __func__, __LINE__) +#define xzalloc(_size) __xzalloc(_size /*, __FILE__, __func__, __LINE__ */) +#define xrealloc(_ptr, _size) __xrealloc(_ptr, _size, __FILE__, __func__, __LINE__) +#define xstrdup(_str) __xstrdup(_str, __FILE__, __func__, __LINE__) +#define xstrndup(_str, _size) __xstrndup(_str, _size, __FILE__, __func__, __LINE__) + +/* errno helpers */ +#define save_errno() int old_errno = errno; +#define restore_errno() errno = old_errno; +#define saved_errno old_errno + #endif /* __SBUTIL_H__ */ diff --git a/libsbutil/src/config.c b/libsbutil/src/config.c index b0284bf..29e333f 100644 --- a/libsbutil/src/config.c +++ b/libsbutil/src/config.c @@ -100,26 +100,12 @@ rc_get_cnf_entry (const char *pathname, const char *entry, const char *sep) if (NULL != value) free (value); - value = xstrndup (token, strlen (token)); - if (NULL == value) - { - rc_dynbuf_free (dynbuf); - free (buf); - - return NULL; - } + value = xstrdup (token); } else { value = xrealloc (value, strlen(value) + strlen(token) + strlen(sep) + 1); - if (NULL == value) - { - rc_dynbuf_free (dynbuf); - free (buf); - - return NULL; - } snprintf(value + strlen(value), strlen(token) + strlen(sep) + 1, "%s%s", sep, token); } diff --git a/libsbutil/src/debug.c b/libsbutil/src/debug.c index a8c8790..4806e51 100644 --- a/libsbutil/src/debug.c +++ b/libsbutil/src/debug.c @@ -57,17 +57,7 @@ debug_message (const char *file, const char *func, int line, int length; length = strlen (log_domain) + strlen ("(): ") + 1; - /* Do not use xmalloc() here, else we may have recursive issues */ - format_str = malloc (length); - if (NULL == format_str) - { - fprintf (stderr, "(%s) error: in %s, function %s(), line %i:\n", - log_domain, __FILE__, __func__, __LINE__); - fprintf (stderr, "(%s) Failed to allocate buffer!\n", - log_domain); - abort (); - } - + format_str = xmalloc (length); snprintf (format_str, length, "(%s) ", log_domain); va_start (arg, format); @@ -219,83 +209,3 @@ __check_arg_fp (FILE *fp, const char *file, const char *func, size_t line) return true; } - -void * -__xcalloc(size_t nmemb, size_t size, const char *file, - const char *func, size_t line) -{ - void *new_ptr; - - new_ptr = calloc (nmemb, size); - if (NULL == new_ptr) - { - /* Set errno in case specific malloc() implementation does not */ - rc_errno_set (ENOMEM); - - debug_message (file, func, line, "Failed to allocate buffer!\n"); - - return NULL; - } - - return new_ptr; -} - -void * -__xmalloc (size_t size, const char *file, const char *func, size_t line) -{ - void *new_ptr; - - new_ptr = malloc (size); - if (NULL == new_ptr) - { - /* Set errno in case specific malloc() implementation does not */ - rc_errno_set (ENOMEM); - - debug_message (file, func, line, "Failed to allocate buffer!\n"); - - return NULL; - } - - return new_ptr; -} - -void * -__xrealloc (void *ptr, size_t size, const char *file, - const char *func, size_t line) -{ - void *new_ptr; - - new_ptr = realloc (ptr, size); - if (NULL == new_ptr) - { - /* Set errno in case specific realloc() implementation does not */ - rc_errno_set (ENOMEM); - - debug_message (file, func, line, "Failed to reallocate buffer!\n"); - - return NULL; - } - - return new_ptr; -} - -char * -__xstrndup (const char *str, size_t size, const char *file, - const char *func, size_t line) -{ - char *new_ptr; - - new_ptr = rc_strndup (str, size); - if (NULL == new_ptr) - { - /* Set errno in case specific realloc() implementation does not */ - rc_errno_set (ENOMEM); - - debug_message (file, func, line, - "Failed to duplicate string via rc_strndup() !\n"); - - return NULL; - } - - return new_ptr; -} diff --git a/libsbutil/src/dynbuf.c b/libsbutil/src/dynbuf.c index 3bd021b..604d85e 100644 --- a/libsbutil/src/dynbuf.c +++ b/libsbutil/src/dynbuf.c @@ -21,15 +21,7 @@ rc_dynbuf_new (void) rc_dynbuf_t *dynbuf = NULL; dynbuf = xmalloc (sizeof (rc_dynbuf_t)); - if (NULL == dynbuf) - return NULL; - dynbuf->data = xmalloc (DYNAMIC_BUFFER_SIZE); - if (NULL == dynbuf->data) - { - free (dynbuf); - return NULL; - } dynbuf->length = DYNAMIC_BUFFER_SIZE; dynbuf->rd_index = 0; @@ -45,8 +37,6 @@ rc_dynbuf_new_mmap_file (const char *name) rc_dynbuf_t *dynbuf = NULL; dynbuf = xmalloc (sizeof (rc_dynbuf_t)); - if (NULL == dynbuf) - return NULL; if (-1 == rc_file_map (name, &dynbuf->data, &dynbuf->length)) { @@ -90,8 +80,6 @@ rc_dynbuf_reallocate (rc_dynbuf_t *dynbuf, size_t needed) len = dynbuf->length + DYNAMIC_BUFFER_SIZE; new_ptr = xrealloc (dynbuf->data, len); - if (NULL == new_ptr) - return NULL; dynbuf->data = new_ptr; dynbuf->length = len; @@ -393,8 +381,6 @@ rc_dynbuf_read_line (rc_dynbuf_t *dynbuf) { buf = xstrndup ((dynbuf->data + dynbuf->rd_index), (count - dynbuf->rd_index)); - if (NULL == buf) - return NULL; dynbuf->rd_index = count; diff --git a/libsbutil/src/string.c b/libsbutil/src/string.c index 71e7213..eec6f85 100644 --- a/libsbutil/src/string.c +++ b/libsbutil/src/string.c @@ -24,8 +24,6 @@ rc_strcatpaths (const char *pathname1, const char *pathname2) lenght = strlen (pathname1) + strlen (pathname2) + 2; /* lenght + '\0' */ new_path = xmalloc (lenght); - if (NULL == new_path) - return NULL; snprintf (new_path, lenght, "%s%s%s", pathname1, (pathname1[strlen (pathname1) - 1] != '/') ? "/" : "", @@ -33,27 +31,3 @@ rc_strcatpaths (const char *pathname1, const char *pathname2) return new_path; } - -char * -rc_strndup (const char *str, size_t size) -{ - char *new_str = NULL; - size_t len; - - /* We cannot check if its a valid string here, as it might - * not be '\0' terminated ... */ - if (!check_arg_ptr (str)) - return NULL; - - /* Check lenght of str without breaching the size limit */ - for (len = 0; (len < size) && ('\0' != str[len]); len++); - - new_str = xmalloc (len + 1); - if (NULL == new_str) - return NULL; - - /* Make sure our string is NULL terminated */ - new_str[len] = '\0'; - - return (char *) memcpy (new_str, str, len); -} diff --git a/src/environ.c b/src/environ.c index c723c1b..98dc9b2 100644 --- a/src/environ.c +++ b/src/environ.c @@ -226,8 +226,6 @@ static int sb_setenv(char ***envp, const char *name, const char *val) /* strlen(name) + strlen(val) + '=' + '\0' */ tmp_string = xmalloc((strlen(name) + strlen(val) + 2) * sizeof(char)); - if (NULL == tmp_string) - goto error; snprintf(tmp_string, strlen(name) + strlen(val) + 2, "%s=%s", name, val); @@ -276,17 +274,11 @@ char **setup_environ(struct sandbox_info_t *sandbox_info, bool interactive) ld_preload_envvar = xcalloc(strlen(orig_ld_preload_envvar) + strlen(sandbox_info->sandbox_lib) + 2, sizeof(char)); - if (NULL == ld_preload_envvar) - return NULL; snprintf(ld_preload_envvar, strlen(orig_ld_preload_envvar) + strlen(sandbox_info->sandbox_lib) + 2, "%s %s", sandbox_info->sandbox_lib, orig_ld_preload_envvar); - } else { - ld_preload_envvar = rc_strndup(sandbox_info->sandbox_lib, - strlen(sandbox_info->sandbox_lib)); - if (NULL == ld_preload_envvar) - return NULL; - } + } else + ld_preload_envvar = xstrdup(sandbox_info->sandbox_lib); /* Do not unset this, as strange things might happen */ /* unsetenv(ENV_LD_PRELOAD); */ diff --git a/src/sandbox.c b/src/sandbox.c index 2417363..1b8fd59 100644 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -106,12 +106,11 @@ int print_sandbox_log(char *sandbox_log) return 0; } - buffer = xmalloc((len + 1) * sizeof(char)); + buffer = xzalloc((len + 1) * sizeof(char)); if (NULL == buffer) { perror("sandbox: Could not allocate buffer for Log file"); return 0; } - memset(buffer, 0, len + 1); if (-1 == sb_read(sandbox_log_file, buffer, len)) { perror("sandbox: Could read Log file"); return 0; |