Skip to content

Conversation

@laanwj
Copy link
Member

@laanwj laanwj commented Aug 25, 2017

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 #11129. As such, this is an alternative to that PR.

@maflcko
Copy link
Member

maflcko commented Aug 25, 2017 via email

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to use fs::unique_path.

Edit: Concept ACK!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Append the suffix in GenerateAuthCookie() and there is no need to call GetAuthCookieFile() twice.

Copy link
Member Author

@laanwj laanwj Aug 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, well, find a fool proof and portable way to append to a fs::path (without adding path components!) and get back me. After browsing stack overflow and boost documentation for a while, I deemed it easier and safer to just do it where the filename is constructed initially.

Edit: oh, and if you want to hear something else funny, calling path::replace_extension will replace .cookie with .tmp. ARg.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also saw all that, but with GetDataDir() / fs::unique_path() none of ths is really necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove forward declaration?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it only used after the declaration? To minimize diff I didn't want to move the function around and had some compile issues, but that might have been for another reason, will try again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we always put the documentation above the declaration, but whatever, no need to change it back now.

@laanwj
Copy link
Member Author

laanwj commented Aug 25, 2017

Another option is to use fs::unique_path.

I wasn't sure that would work in all cases - using a temporary file would potentially move over filesystem boundaries, right? Cross-filesystem move is not atomic IIRC.

@promag
Copy link
Contributor

promag commented Aug 25, 2017

I meant GetDataDir() / fs::unique_path().

@laanwj
Copy link
Member Author

laanwj commented Aug 25, 2017

I meant GetDataDir() / fs::unique_path().

It's possible to place the cookie file outside of the data directory with an option. Also my gut feeling says it's better to create files with deterministic names in the data directory, what if e.g. boost accidentally generates a name used as a wallet name :) I don't see any advantages.

@promag
Copy link
Contributor

promag commented Aug 25, 2017

what if e.g. boost accidentally generates a name used as a wallet name :)

I see you take serious precautions 😄

In the documentation there is http://www.boost.org/doc/libs/1_53_0/libs/filesystem/doc/reference.html#path-concatenation.

@laanwj laanwj force-pushed the 2017_08_atomic_cookie branch from 43e7dcb to e413f1d Compare August 25, 2017 13:31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not tested but here says:

A common misconception is that calling close will necessarily perform an implicit fsync.

From the docs there is no explicit mention to flush, only:

Any pending output sequence is written to the file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other words, call file.flush() before close?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're having another misconception there: fsync is not necessary for other processes to see the result! Only to make sure that the data hits disk, in case of crashes etc. Which is not relevant here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's ask @theuni just in case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the only concern is that libc may buffer the write as long as the file is open, in which case a flush would be useful to make it visible to the OS (and thus other processes). However, as you're closing the file anyway, I don't believe that matters.

@laanwj
Copy link
Member Author

laanwj commented Aug 25, 2017

http://www.boost.org/doc/libs/1_53_0/libs/filesystem/doc/reference.html#path-concatenation.

Yes, but as I understoof, there is difference between boost_filesystem versions there. I don't really have a lot of time to dive into that, and I don't want this to come back with some strange compile error, and this works so...

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.
@laanwj laanwj force-pushed the 2017_08_atomic_cookie branch from e413f1d to 82dd719 Compare August 25, 2017 13:38
@maflcko maflcko added this to the 0.15.1 milestone Aug 25, 2017
Copy link
Contributor

@gmaxwell gmaxwell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

@meshcollider
Copy link
Contributor

utACK 82dd719

I don't want this to come back with some strange compile error, and this works so...

Agreed, this seems fine to me

@maflcko maflcko requested a review from theuni August 27, 2017 13:34
@sipa
Copy link
Member

sipa commented Aug 27, 2017

utACK 82dd719

@laanwj laanwj merged commit 82dd719 into bitcoin:master Aug 28, 2017
laanwj added a commit that referenced this pull request Aug 28, 2017
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 #11129. As such, this is an alternative to that PR.

Tree-SHA512: 47fcc1ed2ff3d8fed4b7441e4939f29cc99b57b7a035673c3b55a124a2e49c8a904637a6ff700dd13a184be8c0255707d74781f8e626314916418954e2467e03
maflcko pushed a commit to maflcko/bitcoin-core that referenced this pull request Oct 3, 2017
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.

Github-Pull: bitcoin#11131
Rebased-From: 82dd719
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Aug 30, 2019
Summary:
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 #11129. As such, this is an alternative to that PR.

Tree-SHA512: 47fcc1ed2ff3d8fed4b7441e4939f29cc99b57b7a035673c3b55a124a2e49c8a904637a6ff700dd13a184be8c0255707d74781f8e626314916418954e2467e03

Backport of Core PR11131
bitcoin/bitcoin#11131

Test Plan:
  make check
  ./bitcoind
Verify `.cookie` exists ine `.bitcoin/<dir>/.cookie`

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D3940
codablock pushed a commit to codablock/dash that referenced this pull request Sep 25, 2019
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
jonspock pushed a commit to jonspock/devault that referenced this pull request Dec 8, 2019
Summary:
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 #11129. As such, this is an alternative to that PR.

Tree-SHA512: 47fcc1ed2ff3d8fed4b7441e4939f29cc99b57b7a035673c3b55a124a2e49c8a904637a6ff700dd13a184be8c0255707d74781f8e626314916418954e2467e03

Backport of Core PR11131
bitcoin/bitcoin#11131

Test Plan:
  make check
  ./bitcoind
Verify `.cookie` exists ine `.bitcoin/<dir>/.cookie`

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D3940
jonspock pushed a commit to jonspock/devault that referenced this pull request Dec 8, 2019
Summary:
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 #11129. As such, this is an alternative to that PR.

Tree-SHA512: 47fcc1ed2ff3d8fed4b7441e4939f29cc99b57b7a035673c3b55a124a2e49c8a904637a6ff700dd13a184be8c0255707d74781f8e626314916418954e2467e03

Backport of Core PR11131
bitcoin/bitcoin#11131

Test Plan:
  make check
  ./bitcoind
Verify `.cookie` exists ine `.bitcoin/<dir>/.cookie`

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D3940
jonspock pushed a commit to jonspock/devault that referenced this pull request Dec 8, 2019
Summary:
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 #11129. As such, this is an alternative to that PR.

Tree-SHA512: 47fcc1ed2ff3d8fed4b7441e4939f29cc99b57b7a035673c3b55a124a2e49c8a904637a6ff700dd13a184be8c0255707d74781f8e626314916418954e2467e03

Backport of Core PR11131
bitcoin/bitcoin#11131

Test Plan:
  make check
  ./bitcoind
Verify `.cookie` exists ine `.bitcoin/<dir>/.cookie`

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D3940
proteanx pushed a commit to devaultcrypto/devault that referenced this pull request Dec 12, 2019
Summary:
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 #11129. As such, this is an alternative to that PR.

Tree-SHA512: 47fcc1ed2ff3d8fed4b7441e4939f29cc99b57b7a035673c3b55a124a2e49c8a904637a6ff700dd13a184be8c0255707d74781f8e626314916418954e2467e03

Backport of Core PR11131
bitcoin/bitcoin#11131

Test Plan:
  make check
  ./bitcoind
Verify `.cookie` exists ine `.bitcoin/<dir>/.cookie`

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D3940
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants