From 1b0f6fd558ee372b4d44c97de05e1c50bf913c50 Mon Sep 17 00:00:00 2001 From: mehrdadn Date: Mon, 13 Apr 2020 09:30:01 -0700 Subject: [PATCH] Check AF_UNIX path length (#7951) --- python/ray/node.py | 7 +++++++ python/ray/tests/test_tempfile.py | 6 ++++++ src/ray/util/util.cc | 3 +++ 3 files changed, 16 insertions(+) diff --git a/python/ray/node.py b/python/ray/node.py index 8a50ac7c3..884c3f220 100644 --- a/python/ray/node.py +++ b/python/ray/node.py @@ -394,6 +394,7 @@ class Node: socket_path (string): the socket file to prepare. """ result = socket_path + is_mac = sys.platform.startswith("darwin") if sys.platform == "win32": if socket_path is None: result = "tcp://{}:{}".format(self._localhost, @@ -407,6 +408,12 @@ class Node: raise RuntimeError( "Socket file {} exists!".format(socket_path)) try_to_create_directory(os.path.dirname(socket_path)) + + # Check socket path length to make sure it's short enough + maxlen = (104 if is_mac else 108) - 1 # sockaddr_un->sun_path + if len(result.split("://", 1)[-1].encode("utf-8")) > maxlen: + raise OSError("AF_UNIX path length cannot exceed " + "{} bytes: {!r}".format(maxlen, result)) return result def start_reaper_process(self): diff --git a/python/ray/tests/test_tempfile.py b/python/ray/tests/test_tempfile.py index 0e174fd23..13554632f 100644 --- a/python/ray/tests/test_tempfile.py +++ b/python/ray/tests/test_tempfile.py @@ -72,6 +72,12 @@ def test_tempdir_commandline(): ignore_errors=True) +def test_tempdir_long_path(): + temp_dir = os.path.join(ray.utils.get_user_temp_dir(), "z" * 108) + with pytest.raises(OSError): + ray.init(temp_dir=temp_dir) # path should be too long + + def test_raylet_socket_name(shutdown_only): ray.init( raylet_socket_name=os.path.join(ray.utils.get_user_temp_dir(), diff --git a/src/ray/util/util.cc b/src/ray/util/util.cc index f94a24dc2..df49304b5 100644 --- a/src/ray/util/util.cc +++ b/src/ray/util/util.cc @@ -100,6 +100,9 @@ ParseUrlEndpoint(const std::string &endpoint, int default_port) { } if (scheme == "unix://") { #ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS + size_t maxlen = sizeof(sockaddr_un().sun_path) / sizeof(*sockaddr_un().sun_path) - 1; + RAY_CHECK(address.size() <= maxlen) + << "AF_UNIX path length cannot exceed " << maxlen << " bytes: " << address; result = boost::asio::local::stream_protocol::endpoint(address); #else RAY_LOG(FATAL) << "UNIX-domain socket endpoints are not supported: " << endpoint;