]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blobdiff - src/coloredstderr.c
Prevent crash in execve() if env is NULL
[coloredstderr/coloredstderr.git] / src / coloredstderr.c
index 44726899e4fb7590345d0890410cc6ee57abf3ca..9161fc34f3deff694dba52eb07923fdde19bc4c2 100644 (file)
@@ -2,7 +2,7 @@
  * Hook output functions (like printf(3)) with LD_PRELOAD to color stderr (or
  * other file descriptors).
  *
- * Copyright (C) 2013  Simon Ruderich
+ * Copyright (C) 2013-2018  Simon Ruderich
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 #include <config.h>
 
+#include "compiler.h"
+
 /* Must be loaded before the following headers. */
 #include "ldpreload.h"
 
+/* Disable assert()s if not compiled with --enable-debug. */
+#ifndef DEBUG
+# define NDEBUG
+#endif
+
+#ifndef TLS
+# define TLS
+#endif
+
+#include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
+#include <sys/stat.h>
 #include <unistd.h>
 
+#ifdef HAVE_ERR_H
+# include <err.h>
+#endif
 #ifdef HAVE_ERROR_H
 # include <error.h>
 #endif
+#ifdef HAVE_STRUCT__IO_FILE__FILENO
+# include <libio.h>
+#endif
 
-/* Conflicting declaration in glibc. */
-#undef fwrite_unlocked
+/* The following functions may be macros. Undefine them or they cause build
+ * failures when used in our hook macros below. */
+
+/* In glibc, real fwrite_unlocked() is called in macro. */
+#ifdef HAVE_FWRITE_UNLOCKED
+# undef fwrite_unlocked
+#endif
+/* In Clang when compiling with hardening flags (fortify) on Debian Wheezy. */
+#undef printf
+#undef fprintf
+/* On FreeBSD (9.1), __swbuf() is used instead of these macros. */
+#ifdef HAVE___SWBUF
+# undef putc
+# undef putc_unlocked
+# undef putchar
+# undef putchar_unlocked
+#endif
 
 
 /* Used by various functions, including debug(). */
@@ -48,10 +82,19 @@ static size_t (*real_fwrite)(void const *, size_t, size_t, FILE *);
 static int initialized;
 /* Force hooked writes even when not writing to a tty. Used for tests. */
 static int force_write_to_non_tty;
+/* Was ENV_NAME_FDS found and used when init_from_environment() was called?
+ * This is not true if the process set it manually after initialization. */
+static int used_fds_set_by_user;
+/* Was any of our handle_*_pre()/handle_*_post() functions called recursively?
+ * If so don't print the pre/post string for the recursive calls. This is
+ * necessary on some systems (e.g. FreeBSD 9.1) which call multiple hooked
+ * functions while printing a string (e.g. a FILE * and a fd hook function is
+ * called). This is not thread-safe if TLS is not available. */
+static TLS int handle_recursive;
 
 
 #include "constants.h"
-#ifdef DEBUG
+#ifdef WARNING
 # include "debug.h"
 #endif
 
@@ -60,34 +103,32 @@ static int force_write_to_non_tty;
 
 
 
-/* Should the "action" handler be invoked for this file descriptor? */
-static int check_handle_fd(int fd) {
-    /* Load state from environment. Only necessary once per process. */
-    if (!initialized) {
-        init_from_environment();
-    }
+/* See hookmacros.h for the decision if a function call is colored. */
 
-    /* tracked_fds_find() is most likely faster than calling isatty(),
-     * therefore check if we are tracking this file descriptor first. */
-    if (!tracked_fds_find(fd)) {
-        return 0;
-    }
 
-    /* Never touch anything not going to a terminal - unless we are explicitly
-     * asked to do so. */
-    if (!force_write_to_non_tty && !isatty(fd)) {
-        return 0;
-    }
+/* Prevent inlining into hook functions because it may increase the number of
+ * spilled registers unnecessarily. As it's not called very often accept the
+ * additional call. */
+static int isatty_noinline(int fd) noinline;
+static int isatty_noinline(int fd) {
+    assert(fd >= 0);
+
+    int saved_errno = errno;
+    int result = isatty(fd);
+    errno = saved_errno;
 
-    return 1;
+    return result;
 }
 
+
 static void dup_fd(int oldfd, int newfd) {
 #ifdef DEBUG
     debug("%3d -> %3d\t\t\t[%d]\n", oldfd, newfd, getpid());
 #endif
 
-    if (!initialized) {
+    assert(oldfd >= 0 && newfd >= 0);
+
+    if (unlikely(!initialized)) {
         init_from_environment();
     }
 
@@ -109,7 +150,9 @@ static void close_fd(int fd) {
     debug("%3d ->   .\t\t\t[%d]\n", fd, getpid());
 #endif
 
-    if (!initialized) {
+    assert(fd >= 0);
+
+    if (unlikely(!initialized)) {
         init_from_environment();
     }
 
@@ -119,14 +162,14 @@ static void close_fd(int fd) {
 
 /* "Action" handlers called when a file descriptor is matched. */
 
-static char *pre_string;
+static char const *pre_string;
 static size_t pre_string_size;
-static char *post_string;
+static char const *post_string;
 static size_t post_string_size;
 
 /* Load alternative pre/post strings from the environment if available, fall
  * back to default values. */
-inline static void init_pre_post_string() {
+static void init_pre_post_string(void) {
     pre_string = getenv(ENV_NAME_PRE_STRING);
     if (!pre_string) {
         pre_string = DEFAULT_PRE_STRING;
@@ -140,38 +183,69 @@ inline static void init_pre_post_string() {
     post_string_size = strlen(post_string);
 }
 
-static void handle_fd_pre(int fd, int action) {
-    (void)action;
+/* Don't inline any of the pre/post functions. Keep the hook function as small
+ * as possible for speed reasons. */
+static void handle_fd_pre(int fd) noinline;
+static void handle_fd_post(int fd) noinline;
+static void handle_file_pre(FILE *stream) noinline;
+static void handle_file_post(FILE *stream) noinline;
+
+static void handle_fd_pre(int fd) {
+    if (handle_recursive++ > 0) {
+        return;
+    }
+
+    int saved_errno = errno;
 
-    if (!pre_string || !post_string) {
+    if (unlikely(!pre_string)) {
         init_pre_post_string();
     }
 
     DLSYM_FUNCTION(real_write, "write");
     real_write(fd, pre_string, pre_string_size);
+
+    errno = saved_errno;
 }
-static void handle_fd_post(int fd, int action) {
-    (void)action;
+static void handle_fd_post(int fd) {
+    if (--handle_recursive > 0) {
+        return;
+    }
+
+    int saved_errno = errno;
 
     /* write() already loaded above in handle_fd_pre(). */
     real_write(fd, post_string, post_string_size);
+
+    errno = saved_errno;
 }
 
-static void handle_file_pre(FILE *stream, int action) {
-    (void)action;
+static void handle_file_pre(FILE *stream) {
+    if (handle_recursive++ > 0) {
+        return;
+    }
+
+    int saved_errno = errno;
 
-    if (!pre_string || !post_string) {
+    if (unlikely(!pre_string)) {
         init_pre_post_string();
     }
 
     DLSYM_FUNCTION(real_fwrite, "fwrite");
     real_fwrite(pre_string, pre_string_size, 1, stream);
+
+    errno = saved_errno;
 }
-static void handle_file_post(FILE *stream, int action) {
-    (void)action;
+static void handle_file_post(FILE *stream) {
+    if (--handle_recursive > 0) {
+        return;
+    }
+
+    int saved_errno = errno;
 
     /* fwrite() already loaded above in handle_file_pre(). */
     real_fwrite(post_string, post_string_size, 1, stream);
+
+    errno = saved_errno;
 }
 
 
@@ -190,6 +264,14 @@ HOOK_FILE2(int, fputc, stream,
            int, c, FILE *, stream)
 HOOK_FILE2(int, putc, stream,
            int, c, FILE *, stream)
+/* The glibc uses a macro for putc() which expands to _IO_putc(). However
+ * sometimes the raw putc() is used as well, not sure why. Make sure to hook
+ * it too. */
+#ifdef putc
+# undef putc
+HOOK_FILE2(int, putc, stream,
+           int, c, FILE *, stream)
+#endif
 HOOK_FILE1(int, putchar, stdout,
            int, c)
 HOOK_FILE1(int, puts, stdout,
@@ -215,28 +297,76 @@ HOOK_FILE4(int, __vfprintf_chk, stream,
            FILE *, stream, int, flag, char const *, format, va_list, ap)
 
 /* unlocked_stdio(3), only functions from above are hooked */
+#ifdef HAVE_FWRITE_UNLOCKED
 HOOK_FILE4(size_t, fwrite_unlocked, stream,
            void const *, ptr, size_t, size, size_t, nmemb, FILE *, stream)
+#endif
+#ifdef HAVE_FPUTS_UNLOCKED
 HOOK_FILE2(int, fputs_unlocked, stream,
            char const *, s, FILE *, stream)
+#endif
+#ifdef HAVE_FPUTC_UNLOCKED
 HOOK_FILE2(int, fputc_unlocked, stream,
            int, c, FILE *, stream)
+#endif
 HOOK_FILE2(int, putc_unlocked, stream,
            int, c, FILE *, stream)
 HOOK_FILE1(int, putchar_unlocked, stdout,
            int, c)
-HOOK_FILE1(int, puts_unlocked, stdout,
-           char const *, s)
+/* glibc defines (_IO_)putc_unlocked() to a macro which either updates the
+ * output buffer or calls __overflow(). As this code is inlined we can't
+ * handle the first case, but if __overflow() is called we can color that
+ * part. As writes to stderr are never buffered, __overflow() is always called
+ * and everything works fine. This is only a problem if stdout is dupped to
+ * stderr (which shouldn't be the case too often). */
+#if defined(HAVE_STRUCT__IO_FILE__FILENO) && defined(HAVE___OVERFLOW)
+/* _IO_FILE is glibc's representation of FILE. */
+HOOK_FILE2(int, __overflow, f, _IO_FILE *, f, int, ch)
+#endif
+/* Same for FreeBSD's libc. However it's more aggressive: The inline writing
+ * and __swbuf() are also used for normal output (e.g. putc()). Writing to
+ * stderr is still fine; it always calls __swbuf() as stderr is always
+ * unbuffered. */
+#ifdef HAVE___SWBUF
+HOOK_FILE2(int, __swbuf, f, int, c, FILE *, f)
+#endif
 
 /* perror(3) */
 HOOK_VOID1(void, perror, STDERR_FILENO,
            char const *, s)
 
-/* error(3) */
+/* err(3), non standard BSD extension */
+#ifdef HAVE_ERR_H
+HOOK_VAR_VOID2(void, err, STDERR_FILENO, verr,
+               int, eval, char const *, fmt)
+HOOK_VAR_VOID2(void, errx, STDERR_FILENO, verrx,
+               int, eval, char const *, fmt)
+HOOK_VAR_VOID1(void, warn, STDERR_FILENO, vwarn,
+               char const *, fmt)
+HOOK_VAR_VOID1(void, warnx, STDERR_FILENO, vwarnx,
+               char const *, fmt)
+HOOK_FUNC_SIMPLE3(void, verr, int, eval, const char *, fmt, va_list, args) {
+    /* Can't use verr() directly as it terminates the process which prevents
+     * the post string from being printed. */
+    vwarn(fmt, args);
+    exit(eval);
+}
+HOOK_FUNC_SIMPLE3(void, verrx, int, eval, const char *, fmt, va_list, args) {
+    /* See verr(). */
+    vwarnx(fmt, args);
+    exit(eval);
+}
+HOOK_VOID2(void, vwarn, STDERR_FILENO,
+           char const *, fmt, va_list, args)
+HOOK_VOID2(void, vwarnx, STDERR_FILENO,
+           char const *, fmt, va_list, args)
+#endif
+
+/* error(3), non-standard GNU extension */
 #ifdef HAVE_ERROR_H
-static void  error_vararg(int status, int errnum,
-                   char const *filename, unsigned int linenum,
-                   char const *format, va_list ap) {
+static void error_vararg(int status, int errnum,
+                         char const *filename, unsigned int linenum,
+                         char const *format, va_list ap) {
     static char const *last_filename;
     static unsigned int last_linenum;
 
@@ -245,7 +375,7 @@ static void  error_vararg(int status, int errnum,
     if (error_one_per_line
             && filename != NULL && linenum != 0
             && filename == last_filename && linenum == last_linenum) {
-        return;
+        goto out;
     }
     last_filename = filename;
     last_linenum  = linenum;
@@ -278,6 +408,7 @@ static void  error_vararg(int status, int errnum,
 
     fprintf(stderr, "\n");
 
+out:
     if (status != 0) {
         exit(status);
     }
@@ -303,50 +434,44 @@ void error(int status, int errnum, char const *format, ...) {
 
 /* Hook functions which duplicate file descriptors to track them. */
 
-static int (*real_dup)(int);
-static int (*real_dup2)(int, int);
-static int (*real_dup3)(int, int, int);
-int dup(int oldfd) {
+/* int dup(int) */
+HOOK_FUNC_DEF1(int, dup, int, oldfd) {
     int newfd;
 
     DLSYM_FUNCTION(real_dup, "dup");
 
     newfd = real_dup(oldfd);
-    if (newfd != -1) {
-        int saved_errno = errno;
+    if (newfd > -1) {
         dup_fd(oldfd, newfd);
-        errno = saved_errno;
     }
 
     return newfd;
 }
-int dup2(int oldfd, int newfd) {
+/* int dup2(int, int) */
+HOOK_FUNC_DEF2(int, dup2, int, oldfd, int, newfd) {
     DLSYM_FUNCTION(real_dup2, "dup2");
 
     newfd = real_dup2(oldfd, newfd);
-    if (newfd != -1) {
-        int saved_errno = errno;
+    if (newfd > -1) {
         dup_fd(oldfd, newfd);
-        errno = saved_errno;
     }
 
     return newfd;
 }
-int dup3(int oldfd, int newfd, int flags) {
+/* int dup3(int, int, int) */
+HOOK_FUNC_DEF3(int, dup3, int, oldfd, int, newfd, int, flags) {
     DLSYM_FUNCTION(real_dup3, "dup3");
 
     newfd = real_dup3(oldfd, newfd, flags);
-    if (newfd != -1) {
-        int saved_errno = errno;
+    if (newfd > -1) {
         dup_fd(oldfd, newfd);
-        errno = saved_errno;
     }
 
     return newfd;
 }
 
-static int (*real_fcntl)(int, int, ...);
-int fcntl(int fd, int cmd, ...) {
+/* int fcntl(int, int, ...) */
+HOOK_FUNC_VAR_DEF2(int, fcntl, int, fd, int, cmd /*, ... */) {
     int result;
     va_list ap;
 
@@ -370,27 +495,31 @@ int fcntl(int fd, int cmd, ...) {
     va_end(ap);
 
     /* We only care about duping fds. */
-    if (cmd == F_DUPFD && result != -1) {
-        int saved_errno = errno;
+    if (cmd == F_DUPFD && result > -1) {
         dup_fd(fd, result);
-        errno = saved_errno;
     }
 
     return result;
 }
 
-static int (*real_close)(int);
-int close(int fd) {
+/* int close(int) */
+HOOK_FUNC_DEF1(int, close, int, fd) {
     DLSYM_FUNCTION(real_close, "close");
 
-    close_fd(fd);
+    if (fd >= 0) {
+        close_fd(fd);
+    }
     return real_close(fd);
 }
-static int (*real_fclose)(FILE *);
-int fclose(FILE *fp) {
+/* int fclose(FILE *) */
+HOOK_FUNC_DEF1(int, fclose, FILE *, fp) {
+    int fd;
+
     DLSYM_FUNCTION(real_fclose, "fclose");
 
-    close_fd(fileno(fp));
+    if (fp != NULL && (fd = fileno(fp)) >= 0) {
+        close_fd(fd);
+    }
     return real_fclose(fp);
 }
 
@@ -405,11 +534,12 @@ pid_t vfork(void) {
      * isn't allowed to perform any memory-modifications before the exec()
      * (except the pid_t result variable of vfork()).
      *
-     * As some programs don't adhere to the standard (e.g. the "child" closes
-     * or dups a descriptor before the exec()) and this breaks our tracking of
-     * file descriptors (e.g. it gets closed in the parent as well), we just
-     * fork() instead. This is in compliance with the POSIX standard and as
-     * most systems use copy-on-write anyway not a performance issue. */
+     * As we have to store changes to the file descriptors in memory (e.g. the
+     * "child" closes or dups a descriptor before the exec()) and this
+     * modifies the parent as well due to the semantics of vfork() - thus
+     * breaking the requirements of vfork(), we just use fork instead(). This
+     * is in compliance with the POSIX standard and as most systems use
+     * copy-on-write anyway not a performance issue. */
     return fork();
 }
 #endif
@@ -417,34 +547,28 @@ pid_t vfork(void) {
 
 /* Hook execve() and the other exec*() functions. Some shells use exec*() with
  * a custom environment which doesn't necessarily contain our updates to
- * ENV_NAME_FDS. It's also faster to update the environment only when
- * necessary, right before the exec() to pass it to the new process. */
+ * ENV_NAME_PRIVATE_FDS. It's also faster to update the environment only when
+ * necessary, right before the exec(), to pass it to the new program. */
 
-static int (*real_execve)(char const *filename, char * const argv[], char * const env[]);
-int execve(char const *filename, char * const argv[], char * const env[]) {
+/* int execve(char const *, char * const [], char * const []) */
+HOOK_FUNC_DEF3(int, execve, char const *, filename, char * const *, argv, char * const *, env) {
     DLSYM_FUNCTION(real_execve, "execve");
 
-    int found = 0;
-    size_t index = 0;
+    char * const fake_env[] = {NULL};
+    if (env == NULL) {
+        env = fake_env;
+    }
 
-    /* Count arguments and search for existing ENV_NAME_FDS environment
-     * variable. */
+    /* Count environment variables. */
     size_t count = 0;
     char * const *x = env;
-    while (*x) {
-        if (!strncmp(*x, ENV_NAME_FDS "=", strlen(ENV_NAME_FDS) + 1)) {
-            found = 1;
-            index = count;
-        }
-
-        x++;
+    while (*x++) {
         count++;
     }
     /* Terminating NULL. */
     count++;
 
     char *env_copy[count + 1 /* space for our new entry if necessary */];
-    memcpy(env_copy, env, count * sizeof(char *));
 
     /* Make sure the information from the environment is loaded. We can't just
      * do nothing (like update_environment()) because the caller might pass a
@@ -453,17 +577,40 @@ int execve(char const *filename, char * const argv[], char * const env[]) {
         init_from_environment();
     }
 
-    char fds_env[strlen(ENV_NAME_FDS) + 1 + update_environment_buffer_size()];
-    strcpy(fds_env, ENV_NAME_FDS "=");
-    update_environment_buffer(fds_env + strlen(ENV_NAME_FDS) + 1);
+    char fds_env[strlen(ENV_NAME_PRIVATE_FDS)
+                 + 1 + update_environment_buffer_size()];
+    strcpy(fds_env, ENV_NAME_PRIVATE_FDS "=");
+    update_environment_buffer(fds_env + strlen(ENV_NAME_PRIVATE_FDS) + 1);
 
-    if (found) {
-        env_copy[index] = fds_env;
-    } else {
-        /* If the process removed ENV_NAME_FDS from the environment, re-add
-         * it. */
-        env_copy[count-1] = fds_env;
-        env_copy[count] = NULL;
+    int found = 0;
+    char **x_copy = env_copy;
+
+    /* Copy the environment manually; allows skipping elements. */
+    x = env;
+    while ((*x_copy = *x)) {
+        /* Remove ENV_NAME_FDS if we've already used its value. The new
+         * program must use the updated list from ENV_NAME_PRIVATE_FDS. */
+        if (used_fds_set_by_user
+                && !strncmp(*x, ENV_NAME_FDS "=", strlen(ENV_NAME_FDS) + 1)) {
+            x++;
+            continue;
+        /* Update ENV_NAME_PRIVATE_FDS. */
+        } else if (!strncmp(*x, ENV_NAME_PRIVATE_FDS "=",
+                            strlen(ENV_NAME_PRIVATE_FDS) + 1)) {
+            *x_copy = fds_env;
+            found = 1;
+        }
+
+        x++;
+        x_copy++;
+    }
+    /* The loop "condition" NULL-terminates env_copy. */
+
+    if (!found) {
+        /* If the process removed ENV_NAME_PRIVATE_FDS from the environment,
+         * re-add it. */
+        *x_copy++ = fds_env;
+        *x_copy++ = NULL;
     }
 
     return real_execve(filename, argv, env_copy);
@@ -476,14 +623,14 @@ int execve(char const *filename, char * const argv[], char * const env[]) {
     /* Count arguments. */ \
     size_t count = 1; /* arg */ \
     va_start(ap, arg); \
-    while (va_arg(ap, const char *)) { \
+    while (va_arg(ap, char *)) { \
         count++; \
     } \
     va_end(ap); \
     \
     /* Copy varargs. */ \
     char *args[count + 1 /* terminating NULL */]; \
-    args[0] = (char *)arg; \
+    args[0] = (char *)arg; /* there's no other way around the cast */ \
     \
     size_t i = 1; \
     va_start(ap, arg); \
@@ -500,38 +647,56 @@ int execve(char const *filename, char * const argv[], char * const env[]) {
 int execl(char const *path, char const *arg, ...) {
     EXECL_COPY_VARARGS(args);
 
-    update_environment();
+    /* execv() updates the environment. */
     return execv(path, args);
 }
-
 int execlp(char const *file, char const *arg, ...) {
     EXECL_COPY_VARARGS(args);
 
-    update_environment();
+    /* execvp() updates the environment. */
     return execvp(file, args);
 }
-
 int execle(char const *path, char const *arg, ... /*, char * const envp[] */) {
+    char * const *envp;
+
     EXECL_COPY_VARARGS_START(args);
     /* Get envp[] located after arguments. */
-    char * const *envp = va_arg(ap, char * const *);
+    envp = va_arg(ap, char * const *);
     EXECL_COPY_VARARGS_END(args);
 
+    /* execve() updates the environment. */
     return execve(path, args, envp);
 }
 
-static int (*real_execv)(char const *path, char * const argv[]);
-int execv(char const *path, char * const argv[]) {
+/* int execv(char const *, char * const []) */
+HOOK_FUNC_DEF2(int, execv, char const *, path, char * const *, argv) {
     DLSYM_FUNCTION(real_execv, "execv");
 
     update_environment();
     return real_execv(path, argv);
 }
 
-static int (*real_execvp)(char const *path, char * const argv[]);
-int execvp(char const *path, char * const argv[]) {
+/* int execvp(char const *, char * const []) */
+HOOK_FUNC_DEF2(int, execvp, char const *, file, char * const *, argv) {
     DLSYM_FUNCTION(real_execvp, "execvp");
 
     update_environment();
-    return real_execvp(path, argv);
+    return real_execvp(file, argv);
+}
+
+#ifdef HAVE_EXECVPE
+extern char **environ;
+int execvpe(char const *file, char * const argv[], char * const envp[]) {
+    int result;
+    char **old_environ = environ;
+
+    /* Fake the environment so we can reuse execvp(). */
+    environ = (char **)envp;
+
+    /* execvp() updates the environment. */
+    result = execvp(file, argv);
+
+    environ = old_environ;
+    return result;
 }
+#endif