Skip to content

Commit

Permalink
Merge bitcoin#11131: rpc: Write authcookie atomically
Browse files Browse the repository at this point in the history
82dd719 rpc: Write authcookie atomically (Wladimir J. van der Laan)

Pull request description:

  Use POSIX rename atomicity at the `bitcoind` side to create a working
  cookie atomically:

  - Write `.cookie.tmp`, close file
  - Rename `.cookie.tmp` to `.cookie`

  This avoids clients reading invalid/partial cookies as in bitcoin#11129. As such, this is an alternative to that PR.

Tree-SHA512: 47fcc1ed2ff3d8fed4b7441e4939f29cc99b57b7a035673c3b55a124a2e49c8a904637a6ff700dd13a184be8c0255707d74781f8e626314916418954e2467e03
  • Loading branch information
laanwj authored and PastaPastaPasta committed Sep 23, 2019
1 parent 221495d commit 900c6ae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
21 changes: 16 additions & 5 deletions src/rpc/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ static const std::string COOKIEAUTH_USER = "__cookie__";
/** Default name for auth cookie file */
static const std::string COOKIEAUTH_FILE = ".cookie";

fs::path GetAuthCookieFile()
/** Get name of RPC authentication cookie file */
static fs::path GetAuthCookieFile(bool temp=false)
{
fs::path path(gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE));
std::string arg = gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE);
if (temp) {
arg += ".tmp";
}
fs::path path(arg);
if (!path.is_complete()) path = GetDataDir() / path;
return path;
}
Expand All @@ -86,14 +91,20 @@ bool GenerateAuthCookie(std::string *cookie_out)
* these are set to 077 in init.cpp unless overridden with -sysperms.
*/
std::ofstream file;
fs::path filepath = GetAuthCookieFile();
file.open(filepath.string().c_str());
fs::path filepath_tmp = GetAuthCookieFile(true);
file.open(filepath_tmp.string().c_str());
if (!file.is_open()) {
LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath.string());
LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath_tmp.string());
return false;
}
file << cookie;
file.close();

fs::path filepath = GetAuthCookieFile(false);
if (!RenameOver(filepath_tmp, filepath)) {
LogPrintf("Unable to rename cookie authentication file %s to %s\n", filepath_tmp.string(), filepath.string());
return false;
}
LogPrintf("Generated RPC authentication cookie %s\n", filepath.string());

if (cookie_out)
Expand Down
2 changes: 0 additions & 2 deletions src/rpc/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const Un
std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id);
UniValue JSONRPCError(int code, const std::string& message);

/** Get name of RPC authentication cookie file */
fs::path GetAuthCookieFile();
/** Generate a new RPC authentication cookie and write it to disk */
bool GenerateAuthCookie(std::string *cookie_out);
/** Read the RPC authentication cookie from disk */
Expand Down

0 comments on commit 900c6ae

Please sign in to comment.