From 66499780b21f6240d2a8c67f0dfb6af52131c174 Mon Sep 17 00:00:00 2001 From: Yu Zhang Date: Fri, 16 Jun 2023 11:47:19 -0700 Subject: [PATCH] Fix error case memory bug in GetHostName() (#11544) Summary: Fix the error handling in `GetHostName` for non EFAULT, non EINVAL error. Current handling will cause stack overflow when non null-terminated c style string is in `name`, e.g. ENAMETOOLONG, when the `name` buffer is not big enough and the host name is truncated. Pull Request resolved: https://github.com/facebook/rocksdb/pull/11544 Test Plan: ``` COMPILE_WITH_ASAN=1 make all check ``` Reviewed By: pdillinger Differential Revision: D46775799 Pulled By: jowlyzhang fbshipit-source-id: e0fc9400c50fe38bc1fd888b4fea5fe8706165bf --- env/env_posix.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/env/env_posix.cc b/env/env_posix.cc index 400b8ac6e..ae2f90360 100644 --- a/env/env_posix.cc +++ b/env/env_posix.cc @@ -330,12 +330,16 @@ class PosixEnv : public CompositeEnv { } Status GetHostName(char* name, uint64_t len) override { - int ret = gethostname(name, static_cast(len)); + const size_t max_len = static_cast(len); + int ret = gethostname(name, max_len); if (ret < 0) { if (errno == EFAULT || errno == EINVAL) { return Status::InvalidArgument(errnoStr(errno).c_str()); + } else if (errno == ENAMETOOLONG) { + return IOError("GetHostName", std::string(name, strnlen(name, max_len)), + errno); } else { - return IOError("GetHostName", name, errno); + return IOError("GetHostName", "", errno); } } return Status::OK();