Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
unix: fix format string vulnerability in freebsd.c
Browse files Browse the repository at this point in the history
uv_set_process_title() was susceptible to a format string vulnerability:

  $ node -e 'process.title = Array(42).join("%s")'
  Segmentation fault: 11 (core dumped)

The fix is trivial - call setproctitle("%s", s) instead of setproctitle(s) -
but valgrind complains loudly about reads from and writes to uninitialized
memory in libc. It's not a libuv bug because the test case below triggers the
same warnings:

  #include <stdio.h>
  #include <stdlib.h>
  #include <sys/types.h>
  #include <unistd.h>

  int main(void)
  {
    setproctitle("%s", "test");
    return 0;
  }

That's why this commit replaces setproctitle() with sysctl(KERN_PROC_ARGS).
  • Loading branch information
bnoordhuis committed Jul 13, 2012
1 parent a87abc7 commit a9f6f06
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
16 changes: 15 additions & 1 deletion src/unix/freebsd.c
Expand Up @@ -139,9 +139,23 @@ char** uv_setup_args(int argc, char** argv) {


uv_err_t uv_set_process_title(const char* title) {
int oid[4];

if (process_title) free(process_title);
process_title = strdup(title);
setproctitle(title);

oid[0] = CTL_KERN;
oid[1] = KERN_PROC;
oid[2] = KERN_PROC_ARGS;
oid[3] = getpid();

sysctl(oid,
ARRAY_SIZE(oid),
NULL,
NULL,
process_title,
strlen(process_title) + 1);

return uv_ok_;
}

Expand Down
13 changes: 10 additions & 3 deletions test/test-process-title.c
Expand Up @@ -23,20 +23,27 @@
#include "task.h"
#include <string.h>

TEST_IMPL(process_title) {

static void set_title(const char* title) {
char buffer[512];
uv_err_t err;

err = uv_get_process_title(buffer, sizeof(buffer));
ASSERT(UV_OK == err.code);

err = uv_set_process_title("new title");
err = uv_set_process_title(title);
ASSERT(UV_OK == err.code);

err = uv_get_process_title(buffer, sizeof(buffer));
ASSERT(UV_OK == err.code);

ASSERT(strcmp(buffer, "new title") == 0);
ASSERT(strcmp(buffer, title) == 0);
}


TEST_IMPL(process_title) {
/* Check for format string vulnerabilities. */
set_title("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s");
set_title("new title");
return 0;
}

0 comments on commit a9f6f06

Please sign in to comment.