From ad55d141ebd0922be310b66c8e4c0e81e94521d1 Mon Sep 17 00:00:00 2001 From: shugo Date: Fri, 25 Jan 2013 08:15:26 +0000 Subject: * ext/socket/raddrinfo.c (rsock_unixpath_len, init_unix_addrinfo), ext/socket/unixsocket.c (unixsock_connect_internal, rsock_init_unixsock): calculate the correct address length of an abstract socket. Without this fix, sizeof(struct sockaddr_un) is specified as the length of an abstract socket for bind(2) or connect(2), so the address of the socket is filled with extra NUL characters. See unix(7) for details. * ext/socket/lib/socket.rb (unix_server_socket): don't access the file system if the platform is Linux and path starts with NUL, which means that the socket is an abstract socket. * test/socket/test_unix.rb: related test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38939 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 16 ++++++++++++++++ ext/socket/lib/socket.rb | 26 +++++++++++++++++++------- ext/socket/raddrinfo.c | 21 ++++++++++++++++++++- ext/socket/rubysocket.h | 1 + ext/socket/unixsocket.c | 8 ++++++-- test/socket/test_unix.rb | 30 ++++++++++++++++++++++++++++++ 6 files changed, 92 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3eba9ee33f..07520339ed 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +Fri Jan 25 16:47:31 2013 Shugo Maeda + + * ext/socket/raddrinfo.c (rsock_unixpath_len, init_unix_addrinfo), + ext/socket/unixsocket.c (unixsock_connect_internal, + rsock_init_unixsock): calculate the correct address length of + an abstract socket. Without this fix, sizeof(struct sockaddr_un) + is specified as the length of an abstract socket for bind(2) or + connect(2), so the address of the socket is filled with extra NUL + characters. See unix(7) for details. + + * ext/socket/lib/socket.rb (unix_server_socket): don't access the + file system if the platform is Linux and path starts with NUL, + which means that the socket is an abstract socket. + + * test/socket/test_unix.rb: related test. + Fri Jan 25 13:02:27 2013 Eric Hodel * lib/drb/drb.rb: Updated documentation based on patch from Vincent diff --git a/ext/socket/lib/socket.rb b/ext/socket/lib/socket.rb index 10ab13b953..981b937329 100644 --- a/ext/socket/lib/socket.rb +++ b/ext/socket/lib/socket.rb @@ -791,12 +791,14 @@ class Socket < BasicSocket # } # def self.unix_server_socket(path) - begin - st = File.lstat(path) - rescue Errno::ENOENT - end - if st && st.socket? && st.owned? - File.unlink path + if !unix_socket_abstract_name?(path) + begin + st = File.lstat(path) + rescue Errno::ENOENT + end + if st && st.socket? && st.owned? + File.unlink path + end end s = Addrinfo.unix(path).listen if block_given? @@ -804,13 +806,23 @@ class Socket < BasicSocket yield s ensure s.close if !s.closed? - File.unlink path + if !unix_socket_abstract_name?(path) + File.unlink path + end end else s end end + class << self + private + + def unix_socket_abstract_name?(path) + /linux/ =~ RUBY_PLATFORM && /\A\0/ =~ path + end + end + # creates a UNIX socket server on _path_. # It calls the block for each socket accepted. # diff --git a/ext/socket/raddrinfo.c b/ext/socket/raddrinfo.c index 6a5129efa8..fdc3d4e61c 100644 --- a/ext/socket/raddrinfo.c +++ b/ext/socket/raddrinfo.c @@ -441,6 +441,23 @@ rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len) return rb_assoc_new(rb_str_new2("AF_UNIX"), rsock_unixpath_str(sockaddr, len)); } + +socklen_t +rsock_unixpath_len(VALUE path) +{ +#ifdef __linux__ + if (RSTRING_PTR(path)[0] == '\0') { + /* abstract namespace; see unix(7) for details. */ + return (socklen_t) offsetof(struct sockaddr_un, sun_path) + + RSTRING_LEN(path); + } + else { +#endif + return (socklen_t) sizeof(struct sockaddr_un); +#ifdef __linux__ + } +#endif +} #endif struct hostent_arg { @@ -769,6 +786,7 @@ static void init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path, int socktype) { struct sockaddr_un un; + socklen_t len; StringValue(path); @@ -782,7 +800,8 @@ init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path, int socktype) un.sun_family = AF_UNIX; memcpy((void*)&un.sun_path, RSTRING_PTR(path), RSTRING_LEN(path)); - init_addrinfo(rai, (struct sockaddr *)&un, (socklen_t)sizeof(un), + len = rsock_unixpath_len(path); + init_addrinfo(rai, (struct sockaddr *)&un, len, PF_UNIX, socktype, 0, Qnil, Qnil); } #endif diff --git a/ext/socket/rubysocket.h b/ext/socket/rubysocket.h index 33cb51a5c9..c27b164188 100644 --- a/ext/socket/rubysocket.h +++ b/ext/socket/rubysocket.h @@ -240,6 +240,7 @@ int rsock_revlookup_flag(VALUE revlookup, int *norevlookup); #ifdef HAVE_SYS_UN_H VALUE rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len); VALUE rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len); +socklen_t rsock_unixpath_len(VALUE path); #endif int rsock_socket(int domain, int type, int proto); diff --git a/ext/socket/unixsocket.c b/ext/socket/unixsocket.c index c8557cb4c9..e7693356b5 100644 --- a/ext/socket/unixsocket.c +++ b/ext/socket/unixsocket.c @@ -13,6 +13,7 @@ #ifdef HAVE_SYS_UN_H struct unixsock_arg { struct sockaddr_un *sockaddr; + socklen_t sockaddrlen; int fd; }; @@ -21,13 +22,14 @@ unixsock_connect_internal(VALUE a) { struct unixsock_arg *arg = (struct unixsock_arg *)a; return (VALUE)rsock_connect(arg->fd, (struct sockaddr*)arg->sockaddr, - (socklen_t)sizeof(*arg->sockaddr), 0); + arg->sockaddrlen, 0); } VALUE rsock_init_unixsock(VALUE sock, VALUE path, int server) { struct sockaddr_un sockaddr; + socklen_t sockaddrlen; int fd, status; rb_io_t *fptr; @@ -44,14 +46,16 @@ rsock_init_unixsock(VALUE sock, VALUE path, int server) RSTRING_LEN(path), (int)sizeof(sockaddr.sun_path)); } memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path)); + sockaddrlen = rsock_unixpath_len(path); if (server) { - status = bind(fd, (struct sockaddr*)&sockaddr, (socklen_t)sizeof(sockaddr)); + status = bind(fd, (struct sockaddr*)&sockaddr, sockaddrlen); } else { int prot; struct unixsock_arg arg; arg.sockaddr = &sockaddr; + arg.sockaddrlen = sockaddrlen; arg.fd = fd; status = (int)rb_protect(unixsock_connect_internal, (VALUE)&arg, &prot); if (prot) { diff --git a/test/socket/test_unix.rb b/test/socket/test_unix.rb index 1c5395e380..a44ef2de38 100644 --- a/test/socket/test_unix.rb +++ b/test/socket/test_unix.rb @@ -531,4 +531,34 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase } end + def test_abstract_unix_server + return if /linux/ !~ RUBY_PLATFORM + name = "\0ruby-test_unix" + s0 = nil + UNIXServer.open(name) {|s| + assert_equal(name, s.local_address.unix_path) + s0 = s + UNIXSocket.open(name) {|c| + sock = s.accept + assert_equal(name, c.remote_address.unix_path) + } + } + assert(s0.closed?) + end + + def test_abstract_unix_server_socket + return if /linux/ !~ RUBY_PLATFORM + name = "\0ruby-test_unix" + s0 = nil + Socket.unix_server_socket(name) {|s| + assert_equal(name, s.local_address.unix_path) + s0 = s + Socket.unix(name) {|c| + sock, = s.accept + assert_equal(name, c.remote_address.unix_path) + } + } + assert(s0.closed?) + end + end if defined?(UNIXSocket) && /cygwin/ !~ RUBY_PLATFORM -- cgit v1.2.3