121925
From 7fb67f8be47706af6380cd251a2666d6969aa6f1 Mon Sep 17 00:00:00 2001
121925
From: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
121925
Date: Wed, 28 Mar 2018 14:36:23 +0000
121925
Subject: [PATCH] merge revision(s) 62991,63000:
121925
121925
	unixsocket.c: check NUL bytes
121925
121925
	* ext/socket/unixsocket.c (rsock_init_unixsock): check NUL bytes.
121925
	  https://hackerone.com/reports/302997
121925
121925
	unixsocket.c: abstract namespace
121925
121925
	* ext/socket/unixsocket.c (unixsock_path_value): fix r62991 for
121925
	  Linux abstract namespace.
121925
121925
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@63018 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
121925
---
121925
 ext/socket/unixsocket.c  | 24 +++++++++++++++++++++++-
121925
 test/socket/test_unix.rb | 10 ++++++++++
121925
 2 files changed, 33 insertions(+), 1 deletion(-)
121925
121925
diff --git a/ext/socket/unixsocket.c b/ext/socket/unixsocket.c
121925
index 75da9c1a78..6d68138af4 100644
121925
--- a/ext/socket/unixsocket.c
121925
+++ b/ext/socket/unixsocket.c
121925
@@ -25,6 +25,28 @@ unixsock_connect_internal(VALUE a)
121925
 			        arg->sockaddrlen, 0);
121925
 }
121925
 
121925
+static VALUE
121925
+unixsock_path_value(VALUE path)
121925
+{
121925
+#ifdef __linux__
121925
+#define TO_STR_FOR_LINUX_ABSTRACT_NAMESPACE 0
121925
+
121925
+    VALUE name = path;
121925
+#if TO_STR_FOR_LINUX_ABSTRACT_NAMESPACE
121925
+    const int isstr = !NIL_P(name = rb_check_string_type(name));
121925
+#else
121925
+    const int isstr = RB_TYPE_P(name, T_STRING);
121925
+#endif
121925
+    if (isstr) {
121925
+        if (RSTRING_LEN(name) == 0 || RSTRING_PTR(name)[0] == '\0') {
121925
+            rb_check_safe_obj(name);
121925
+            return name;             /* ignore encoding */
121925
+        }
121925
+    }
121925
+#endif
121925
+    return rb_get_path(path);
121925
+}
121925
+
121925
 VALUE
121925
 rsock_init_unixsock(VALUE sock, VALUE path, int server)
121925
 {
121925
@@ -33,7 +55,7 @@ rsock_init_unixsock(VALUE sock, VALUE path, int server)
121925
     int fd, status;
121925
     rb_io_t *fptr;
121925
 
121925
-    SafeStringValue(path);
121925
+    path = unixsock_path_value(path);
121925
     fd = rsock_socket(AF_UNIX, SOCK_STREAM, 0);
121925
     if (fd < 0) {
121925
 	rb_sys_fail("socket(2)");
121925
diff --git a/test/socket/test_unix.rb b/test/socket/test_unix.rb
121925
index eae236a60b..aef23141b8 100644
121925
--- a/test/socket/test_unix.rb
121925
+++ b/test/socket/test_unix.rb
121925
@@ -253,6 +253,16 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase
121925
     File.unlink path if path && File.socket?(path)
121925
   end
121925
 
121925
+  def test_open_nul_byte
121925
+    tmpfile = Tempfile.new("s")
121925
+    path = tmpfile.path
121925
+    tmpfile.close(true)
121925
+    assert_raise(ArgumentError) {UNIXServer.open(path+"\0")}
121925
+    assert_raise(ArgumentError) {UNIXSocket.open(path+"\0")}
121925
+  ensure
121925
+    File.unlink path if path && File.socket?(path)
121925
+  end
121925
+
121925
   def test_addr
121925
     bound_unix_socket(UNIXServer) {|serv, path|
121925
       c = UNIXSocket.new(path)
121925
-- 
121925
2.17.1
121925