Skip to content

Commit

Permalink
openlog(): keep the ident string in a safe place
Browse files Browse the repository at this point in the history
Notes from the openlog(3) man page:

"The argument ident in the call of openlog() is probably stored as-is.
Thus, if the string it points to is changed, syslog() may start prepending
the changed string, and if the string it points to ceases to exist, the
results are undefined. Most portable is to use a string constant."
  • Loading branch information
melor committed Dec 28, 2011
1 parent d23573d commit 2bb717f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .npmignore
Expand Up @@ -13,3 +13,5 @@ node_modules/
lib/posix/*~
test/unit/*~
.*~
.*
*.gz
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -22,6 +22,7 @@ Other extension modules that provide POSIX/Unix/Linux/BSD functionality:
* chroot(), daemonization http://search.npmjs.org/#/daemon-tools
* iconv() http://search.npmjs.org/#/iconv
* mmap() http://search.npmjs.org/#/mmap
* PAM authentication, flock() and mkstemp() http://search.npmjs.org/#/unixlib

## Roadmap

Expand Down
9 changes: 8 additions & 1 deletion src/posix.cc
Expand Up @@ -365,6 +365,11 @@ static Handle<Value> node_setreuid(const Arguments& args) {
return Undefined();
}

// openlog() first argument (const char* ident) is not guaranteed to be
// copied within the openlog() call so we need to keep it in a safe location
static const size_t MAX_SYSLOG_IDENT=100;
static char syslog_ident[MAX_SYSLOG_IDENT+1] = {0};

static Handle<Value> node_openlog(const Arguments& args) {
HandleScope scope;

Expand All @@ -373,11 +378,13 @@ static Handle<Value> node_openlog(const Arguments& args) {
}

String::Utf8Value ident(args[0]->ToString());
strncpy(syslog_ident, *ident, MAX_SYSLOG_IDENT);
syslog_ident[MAX_SYSLOG_IDENT] = 0;
if(!args[1]->IsNumber() || !args[2]->IsNumber()) {
return EXCEPTION("openlog: invalid argument values");
}
// note: openlog does not ever fail, no return value
openlog(*ident, args[1]->Int32Value(), args[2]->Int32Value());
openlog(syslog_ident, args[1]->Int32Value(), args[2]->Int32Value());

return Undefined();
}
Expand Down

0 comments on commit 2bb717f

Please sign in to comment.