EnricoMi commented on code in PR #46626: URL: https://github.com/apache/arrow/pull/46626#discussion_r2123438439
########## cpp/src/arrow/util/secure_string.cc: ########## @@ -0,0 +1,169 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#define __STDC_WANT_LIB_EXT1__ 1 +#include <string.h> +#include <utility> + +#if defined(ARROW_USE_OPENSSL) +# include <openssl/crypto.h> +# include <openssl/opensslv.h> +#endif + +#include "arrow/util/windows_compatibility.h" +#if defined(_WIN32) +# include <windows.h> +#endif + +#include "arrow/util/secure_string.h" +#include "arrow/util/span.h" + +namespace arrow::util { + +SecureString::SecureString(SecureString&& secret) noexcept + : secret_(std::move(secret.secret_)) { + secret.Dispose(); +} + +SecureString::SecureString(std::string&& secret) noexcept : secret_(std::move(secret)) { + SecureClear(&secret); +} + +SecureString::SecureString(size_t n, char c) noexcept : secret_(n, c) {} + +SecureString& SecureString::operator=(SecureString&& secret) noexcept { + if (this == &secret) { + // self-assignment + return *this; + } + Dispose(); + secret_ = std::move(secret.secret_); + secret.Dispose(); + return *this; +} + +SecureString& SecureString::operator=(const SecureString& secret) { + if (this == &secret) { + // self-assignment + return *this; + } + Dispose(); + secret_ = secret.secret_; + return *this; +} + +SecureString& SecureString::operator=(std::string&& secret) noexcept { + Dispose(); + // std::string implementation may distinguish between local string (a very short string) + // and non-local (longer) strings. The former stores the string in a local buffer, the + // latter stores a pointer to allocated memory that stores the string. + // + // If secret is a local string, copies local buffer, resets size to 0 + // - requires secure cleaning the local buffer + // If secret is longer, moves the pointer to secret_, resets to 0, uses local buffer + // - does not require cleaning anything + secret_ = std::move(secret); Review Comment: @pitrou This requires the `std::string` implementation to move (reuse) the string memory (not the local buffer) of long non-local strings. If the implementation would copy the string memory and the free the memory, we would leak the secret. Is it safe to assume `secret_ = std::move(secret)` reuses the non-local memory in all implementations? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org