Skip to content

Commit 6e08e5c

Browse files
committed
Merge bitcoin/bitcoin#17127: util: Set safe permissions for data directory and wallets/ subdir
c9ba4f9 test: Add test for file system permissions (Hennadii Stepanov) 581f16e Apply default umask in `SetupEnvironment()` (Hennadii Stepanov) 8a6219e Remove `-sysperms` option (Hennadii Stepanov) Pull request description: On master (1e7564e) docs say: ``` $ ./src/bitcoind -help | grep -A 3 sysperms -sysperms Create new files with system default permissions, instead of umask 077 (only effective with disabled wallet functionality) ``` Basing on that, one could expect that running `bitcoind` first time will create data directory and `wallets/` subdirectory with safe 0700 permissions. But that is not the case: ``` $ stat .bitcoin | grep id Access: (0775/drwxrwxr-x) Uid: ( 1000/ hebasto) Gid: ( 1000/ hebasto) $ stat .bitcoin/wallets | grep id Access: (0775/drwxrwxr-x) Uid: ( 1000/ hebasto) Gid: ( 1000/ hebasto) ``` Both directories, in fact, are created with system default permissions. With this PR: ``` $ stat .bitcoin/wallets | grep id Access: (0700/drwx------) Uid: ( 1000/ hebasto) Gid: ( 1000/ hebasto) $ stat .bitcoin/wallets | grep id Access: (0700/drwx------) Uid: ( 1000/ hebasto) Gid: ( 1000/ hebasto) ``` --- This PR: - is alternative to bitcoin/bitcoin#13389 - fixes bitcoin/bitcoin#15902 - fixes bitcoin/bitcoin#22595 - closes bitcoin/bitcoin#13371 - reverts bitcoin/bitcoin#4286 Changes in behavior: removed `-sysperms` command-line argument / configure option. The related discussions are here: - bitcoin/bitcoin#13389 (comment) - bitcoin/bitcoin#13389 (comment) - bitcoin/bitcoin#13389 (comment) If users rely on non-default access permissions, they could use `chmod`. ACKs for top commit: john-moffett: ACK c9ba4f9 willcl-ark: ACK c9ba4f9 Tree-SHA512: 96c745339e6bd0e4d7bf65daf9a721e2e1945b2b0ab74ca0f66576d0dc358b5de8eb8cdb89fe2160f3b19c39d2798bb8b291784316085dc73a27102d3415bd57
2 parents 5a80086 + c9ba4f9 commit 6e08e5c

File tree

9 files changed

+57
-15
lines changed

9 files changed

+57
-15
lines changed

doc/init.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ NOTE: When using the systemd .service file, the creation of the aforementioned
7070
directories and the setting of their permissions is automatically handled by
7171
systemd. Directories are given a permission of 710, giving the bitcoin group
7272
access to files under it _if_ the files themselves give permission to the
73-
bitcoin group to do so (e.g. when `-sysperms` is specified). This does not allow
73+
bitcoin group to do so. This does not allow
7474
for the listing of files under the directory.
7575

7676
NOTE: It is not currently possible to override `datadir` in

src/i2p.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ void Session::GenerateAndSavePrivateKey(const Sock& sock)
336336
{
337337
DestGenerate(sock);
338338

339-
// umask is set to 077 in init.cpp, which is ok (unless -sysperms is given)
339+
// umask is set to 0077 in util/system.cpp, which is ok.
340340
if (!WriteBinaryFile(m_private_key_file,
341341
std::string(m_private_key.begin(), m_private_key.end()))) {
342342
throw std::runtime_error(

src/init.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,6 @@ void SetupServerArgs(ArgsManager& argsman)
458458
#if HAVE_SYSTEM
459459
argsman.AddArg("-startupnotify=<cmd>", "Execute command on startup.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
460460
argsman.AddArg("-shutdownnotify=<cmd>", "Execute command immediately before beginning shutdown. The need for shutdown may be urgent, so be careful not to delay it long (if the command doesn't require interaction with the server, consider having it fork into the background).", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
461-
#endif
462-
#ifndef WIN32
463-
argsman.AddArg("-sysperms", "Create new files with system default permissions, instead of umask 077 (only effective with disabled wallet functionality)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
464-
#else
465-
hidden_args.emplace_back("-sysperms");
466461
#endif
467462
argsman.AddArg("-txindex", strprintf("Maintain a full transaction index, used by the getrawtransaction rpc call (default: %u)", DEFAULT_TXINDEX), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
468463
argsman.AddArg("-blockfilterindex=<type>",
@@ -821,10 +816,6 @@ bool AppInitBasicSetup(const ArgsManager& args)
821816
}
822817

823818
#ifndef WIN32
824-
if (!args.GetBoolArg("-sysperms", false)) {
825-
umask(077);
826-
}
827-
828819
// Clean shutdown on SIGTERM
829820
registerSignalHandler(SIGTERM, HandleSIGTERM);
830821
registerSignalHandler(SIGINT, HandleSIGTERM);

src/rpc/request.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ bool GenerateAuthCookie(std::string *cookie_out)
8686
std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd);
8787

8888
/** the umask determines what permissions are used to create this file -
89-
* these are set to 077 in init.cpp unless overridden with -sysperms.
89+
* these are set to 0077 in util/system.cpp.
9090
*/
9191
std::ofstream file;
9292
fs::path filepath_tmp = GetAuthCookieFile(true);

src/util/system.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,11 @@ void SetupEnvironment()
13601360
SetConsoleCP(CP_UTF8);
13611361
SetConsoleOutputCP(CP_UTF8);
13621362
#endif
1363+
1364+
#ifndef WIN32
1365+
constexpr mode_t private_umask = 0077;
1366+
umask(private_umask);
1367+
#endif
13631368
}
13641369

13651370
bool SetupNetworking()

src/wallet/init.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,6 @@ bool WalletInit::ParameterInteraction() const
122122
return InitError(Untranslated("-zapwallettxes has been removed. If you are attempting to remove a stuck transaction from your wallet, please use abandontransaction instead."));
123123
}
124124

125-
if (gArgs.GetBoolArg("-sysperms", false))
126-
return InitError(Untranslated("-sysperms is not allowed in combination with enabled wallet functionality"));
127-
128125
return true;
129126
}
130127

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2022 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test file system permissions for POSIX platforms.
6+
"""
7+
8+
import os
9+
import stat
10+
11+
from test_framework.test_framework import BitcoinTestFramework
12+
13+
14+
class PosixFsPermissionsTest(BitcoinTestFramework):
15+
def set_test_params(self):
16+
self.setup_clean_chain = True
17+
self.num_nodes = 1
18+
19+
def skip_test_if_missing_module(self):
20+
self.skip_if_platform_not_posix()
21+
22+
def check_directory_permissions(self, dir):
23+
mode = os.lstat(dir).st_mode
24+
self.log.info(f"{stat.filemode(mode)} {dir}")
25+
assert mode == (stat.S_IFDIR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
26+
27+
def check_file_permissions(self, file):
28+
mode = os.lstat(file).st_mode
29+
self.log.info(f"{stat.filemode(mode)} {file}")
30+
assert mode == (stat.S_IFREG | stat.S_IRUSR | stat.S_IWUSR)
31+
32+
def run_test(self):
33+
self.stop_node(0)
34+
datadir = os.path.join(self.nodes[0].datadir, self.chain)
35+
self.check_directory_permissions(datadir)
36+
walletsdir = os.path.join(datadir, "wallets")
37+
self.check_directory_permissions(walletsdir)
38+
debuglog = os.path.join(datadir, "debug.log")
39+
self.check_file_permissions(debuglog)
40+
41+
42+
if __name__ == '__main__':
43+
PosixFsPermissionsTest().main()

test/functional/test_framework/test_framework.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,11 @@ def skip_if_platform_not_linux(self):
880880
if platform.system() != "Linux":
881881
raise SkipTest("not on a Linux system")
882882

883+
def skip_if_platform_not_posix(self):
884+
"""Skip the running test if we are not on a POSIX platform"""
885+
if os.name != 'posix':
886+
raise SkipTest("not on a POSIX system")
887+
883888
def skip_if_no_bitcoind_zmq(self):
884889
"""Skip the running test if bitcoind has not been compiled with zmq support."""
885890
if not self.is_zmq_compiled():

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
'p2p_addrv2_relay.py',
212212
'p2p_compactblocks_hb.py',
213213
'p2p_disconnect_ban.py',
214+
'feature_posix_fs_permissions.py',
214215
'rpc_decodescript.py',
215216
'rpc_blockchain.py',
216217
'rpc_deprecated.py',

0 commit comments

Comments
 (0)