Skip to content

Commit e01eda8

Browse files
author
marc
committed
libroach: disable core file when encryption is requested.
Set core size soft/max limits to 0 when encryption-at-rest is enabled. This spits out a loud warning on stdout (similar to warning about lack of AES instruction set support) when the `(set|get)rlimit` calls fail, or when running on Windows. Release note (enterprise change): disable core dumps when enabling encryption
1 parent e30595d commit e01eda8

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

c-deps/libroach/ccl/crypto_utils.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
#include <cryptopp/hex.h>
1212
#include <cryptopp/osrng.h>
1313
#include <cryptopp/sha.h>
14+
#include "../fmt.h"
15+
16+
#ifndef _WIN32
17+
#include <sys/resource.h>
18+
#include <sys/time.h>
19+
#include <sys/types.h>
20+
#endif
1421

1522
std::string HexString(const std::string& s) {
1623
std::string value;
@@ -61,3 +68,32 @@ rocksdb_utils::BlockCipher* NewAESEncryptCipher(const enginepbccl::SecretKey* ke
6168
}
6269

6370
bool UsesAESNI() { return CryptoPP::UsesAESNI(); }
71+
72+
rocksdb::Status DisableCoreFile() {
73+
#ifdef _WIN32
74+
return rocksdb::Status::NotSupported("preventing crash reports is not supported on Windows");
75+
#else
76+
// We can't use prlimit on OSX. Use setrlimit and getrlimit instead.
77+
rlimit lim;
78+
int ret = getrlimit(RLIMIT_CORE, &lim);
79+
if (ret != 0) {
80+
return rocksdb::Status::NotSupported(
81+
fmt::StringPrintf("failed to get core size rlimit: %s", strerror(errno)));
82+
}
83+
84+
if (lim.rlim_cur == 0 && lim.rlim_max == 0) {
85+
return rocksdb::Status::OK();
86+
}
87+
88+
rlimit new_lim = {0, 0};
89+
ret = setrlimit(RLIMIT_CORE, &new_lim);
90+
if (ret != 0) {
91+
return rocksdb::Status::NotSupported(
92+
fmt::StringPrintf("failed to set core size rlimit: %s", strerror(errno)));
93+
}
94+
95+
std::cerr << "changed maximum core size limit to soft=" << new_lim.rlim_cur
96+
<< " hard=" << new_lim.rlim_max << std::endl;
97+
return rocksdb::Status::OK();
98+
#endif
99+
}

c-deps/libroach/ccl/crypto_utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#pragma once
1010

11+
#include <rocksdb/status.h>
1112
#include <string>
1213
#include "../rocksdbutils/env_encryption.h"
1314
#include "ccl/storageccl/engineccl/enginepbccl/key_registry.pb.h"
@@ -32,3 +33,7 @@ rocksdb_utils::BlockCipher* NewAESEncryptCipher(const enginepbccl::SecretKey* ke
3233

3334
// Returns true if CryptoPP is using AES-NI.
3435
bool UsesAESNI();
36+
37+
// DisableCoreFile sets the maximum size of a core file to 0. Returns success
38+
// if successfully called.
39+
rocksdb::Status DisableCoreFile();

c-deps/libroach/ccl/crypto_utils_test.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,27 @@
1010
#include "crypto_utils.h"
1111

1212
TEST(CryptoUtils, HasAESNI) { EXPECT_TRUE(UsesAESNI()); }
13+
14+
#ifdef _WIN32
15+
16+
TEST(CryptoUtils, DisableCoreDumps) {
17+
EXPECT_ERR(DisableCoreFile(), ".* not supported on Windows");
18+
}
19+
20+
#else
21+
22+
#include <sys/resource.h>
23+
#include <sys/time.h>
24+
#include <sys/types.h>
25+
26+
TEST(CryptoUtils, DisableCoreDumps) {
27+
rlimit lim = {1 << 10, 2 << 10};
28+
ASSERT_EQ(0, setrlimit(RLIMIT_CORE, &lim));
29+
30+
EXPECT_OK(DisableCoreFile());
31+
ASSERT_EQ(0, getrlimit(RLIMIT_CORE, &lim));
32+
EXPECT_EQ(0, lim.rlim_cur);
33+
EXPECT_EQ(0, lim.rlim_max);
34+
}
35+
36+
#endif

c-deps/libroach/ccl/db.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ rocksdb::Status DBOpenHook(std::shared_ptr<rocksdb::Logger> info_log, const std:
126126
<< std::endl;
127127
}
128128

129+
// Attempt to disable core dumps.
130+
auto status = DisableCoreFile();
131+
if (!status.ok()) {
132+
// Shout loudly on standard out.
133+
std::cout << std::endl
134+
<< "*** WARNING ***" << std::endl
135+
<< "Encryption requested, but could not disable core dumps" << std::endl
136+
<< "Keys will be leaks in code dumps!" << std::endl
137+
<< std::endl;
138+
}
139+
129140
// The Go code sets the "file_registry" storage version if we specified encryption flags,
130141
// but let's double check anyway.
131142
if (!db_opts.use_file_registry) {
@@ -147,7 +158,7 @@ rocksdb::Status DBOpenHook(std::shared_ptr<rocksdb::Logger> info_log, const std:
147158
// NOTE: FileKeyManager uses the default env as the MemEnv can never have pre-populated files.
148159
FileKeyManager* store_key_manager = new FileKeyManager(
149160
rocksdb::Env::Default(), opts.key_files().current_key(), opts.key_files().old_key());
150-
rocksdb::Status status = store_key_manager->LoadKeys();
161+
status = store_key_manager->LoadKeys();
151162
if (!status.ok()) {
152163
delete store_key_manager;
153164
return status;

0 commit comments

Comments
 (0)