Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
os: handle 256 character hostnames
Browse files Browse the repository at this point in the history
Fix a (rather academic) buffer overflow. MAXHOSTNAMELEN is 256 on most
platforms, which means the buffer wasn't big enough to hold the
trailing nul byte on a system with a maximum length hostname.
  • Loading branch information
bnoordhuis committed Apr 15, 2013
1 parent 78c5de5 commit afbadde
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/node_os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
#endif

#ifdef __POSIX__
# include <unistd.h> // gethostname, sysconf
# include <netdb.h> // MAXHOSTNAMELEN on Solaris.
# include <unistd.h> // gethostname, sysconf
# include <sys/param.h> // MAXHOSTNAMELEN on Linux and the BSDs.
# include <sys/utsname.h>
#endif

Expand All @@ -51,18 +53,17 @@ static Handle<Value> GetEndianness(const Arguments& args) {

static Handle<Value> GetHostname(const Arguments& args) {
HandleScope scope;
char s[255];
int r = gethostname(s, 255);
char buf[MAXHOSTNAMELEN + 1];

if (r < 0) {
if (gethostname(buf, sizeof(buf))) {
#ifdef __POSIX__
return ThrowException(ErrnoException(errno, "gethostname"));
#else // __MINGW32__
return ThrowException(ErrnoException(WSAGetLastError(), "gethostname"));
#endif // __MINGW32__
}

return scope.Close(String::New(s));
return scope.Close(String::New(buf));
}

static Handle<Value> GetOSType(const Arguments& args) {
Expand Down

0 comments on commit afbadde

Please sign in to comment.