Skip to content

blockstorage: XOR blocksdir *.dat files#28052

Merged
achow101 merged 3 commits intobitcoin:masterfrom
maflcko:2306-fs_stuff-
Aug 5, 2024
Merged

blockstorage: XOR blocksdir *.dat files#28052
achow101 merged 3 commits intobitcoin:masterfrom
maflcko:2306-fs_stuff-

Conversation

@maflcko
Copy link
Member

@maflcko maflcko commented Jul 8, 2023

Currently the *.dat files in the blocksdir store the data received from remote peers as-is. This may be problematic when a program other than Bitcoin Core tries to interpret them by accident. For example, an anti-virus program or other program may scan them and move them into quarantine, or delete them, or corrupt them. This may cause Bitcoin Core to fail a reorg, or fail to reply to block requests (via P2P, RPC, REST, ...).

Fix this, similar to #6650, by rolling a random XOR pattern over the dat files when writing or reading them.

Obviously this can only protect against programs that accidentally and unintentionally are trying to mess with the dat files. Any program that intentionally wants to mess with the dat files can still trivially do so.

The XOR pattern is only applied when the blocksdir is freshly created, and there is an option to disable it (on creation), so that people can disable it, if needed.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jul 8, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK TheCharlatan, hodlinator, achow101
Concept ACK jamesob, sipa, dergoegge, darosior, kristapsk
Stale ACK pablomartin4btc, willcl-ark

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #28687 (C++20 std::views::reverse by stickies-v)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@maflcko maflcko changed the title XOR blocksdir *.dat files blockstorage: XOR blocksdir *.dat files Jul 8, 2023
@jamesob
Copy link
Contributor

jamesob commented Jul 8, 2023

Concept ACK. Certainly worth doing.

@sipa sipa marked this pull request as ready for review July 8, 2023 17:27
@sipa
Copy link
Member

sipa commented Jul 8, 2023

Oops, I clicked wrong.

I just meant to Concept ACK.

@maflcko maflcko marked this pull request as draft July 9, 2023 05:50
@luke-jr
Copy link
Member

luke-jr commented Jul 9, 2023

AFAIK there is software which reads these files intentionally. IMO they can be expected to adapt, but having the option to disable it seems like a good idea in case they aren't ready right away.

@sedited
Copy link
Contributor

sedited commented Jul 10, 2023

Strong Concept ACK.

@maflcko
Copy link
Member Author

maflcko commented Jul 10, 2023

Thanks for the feedback so far. I've kept the option to disable, but made it a simple bool instead of allowing the user to pick the XOR-key.

If someone is interested, a new commit can be added to upgrade the linearize scripts to read the XOR-key.

@dergoegge
Copy link
Member

Concept ACK

@maflcko
Copy link
Member Author

maflcko commented Aug 1, 2023

Rebased on the latest ##28060

fanquake added a commit to bitcoin-core/gui that referenced this pull request Aug 1, 2023
fa633aa streams: Teach AutoFile how to XOR (MarcoFalke)
000019e Add AutoFile::detail_fread member function (MarcoFalke)
fa7724b refactor: Modernize AutoFile (MarcoFalke)
fa8d227 doc: Remove comments that just repeat what the code does (MarcoFalke)
fafe2ca refactor: Remove redundant file check from AutoFile shift operators (MarcoFalke)
9999a49 Extract util::Xor, Add key_offset option, Add bench (MarcoFalke)

Pull request description:

  This allows `AutoFile` to roll an XOR pattern while reading or writing to the underlying file.

  This is needed for bitcoin/bitcoin#28052, but can also be used in any other place.

  Also, there are tests, so I've split this up from the larger pull to make review easier, hopefully.

ACKs for top commit:
  Crypt-iQ:
    crACK fa633aa
  willcl-ark:
    Lightly tested ACK fa633aa
  jamesob:
    reACK fa633aa ([`jamesob/ackr/28060.4.MarcoFalke.util_add_xorfile`](https://github.com/jamesob/bitcoin/tree/ackr/28060.4.MarcoFalke.util_add_xorfile))

Tree-SHA512: 6d66cad0a089a096d3f95e4f2b28bce80b349d4b76f53d09dc9a0bea4fc1b7c0652724469c37971ba27728c7d46398a4c1d289c252af4c0f83bb2fcbc6f8e90b
@DrahtBot DrahtBot removed the CI failed label Aug 1, 2023
Copy link
Contributor

@hodlinator hodlinator left a comment

Choose a reason for hiding this comment

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

ACK fa895c7

git range-diff fa9d4aa9a1a8df6343f615572479db9c8e26b19d~3 fa9d4aa9a1a8df6343f615572479db9c8e26b19d fa895c72832f9555b52d5bb1dba1093f73de3136

Confirmed only thing that really changed was:

  • Merging with mainly 7aa8994 - " refactor: Add FlatFileSeq member variables in BlockManager "
  • Correcting CAutoFile -> AutoFile in commit message
  • Comment typo in InitBlocksdirXorKey
  • LogInfo format string re-jiggling.

Passed make check & test_runner.py.


It would be nice to get to the bottom with why

LogInfo("Using obfuscation key for %s/*.dat files: '%s'\n", fs::PathToString(opts.blocks_dir), HexStr(xor_key));

was worse than

LogInfo("Using obfuscation key for blocksdir *.dat files (%s): '%s'\n", fs::PathToString(opts.blocks_dir), HexStr(xor_key));

and fix test/lint/run-lint-format-strings.py in the longer term.

@maflcko maflcko mentioned this pull request Jul 30, 2024
@achow101
Copy link
Member

achow101 commented Aug 5, 2024

ACK fa895c7

@maflcko
Copy link
Member Author

maflcko commented Aug 7, 2024

Follow-up ideas for this pull requests are:

@hodlinator
Copy link
Contributor

Is there some label or something that can assist people in discovering this kind of follow-up work on closed/merged PRs?

@maflcko
Copy link
Member Author

maflcko commented Aug 7, 2024

Is there some label or something that can assist people in discovering this kind of follow-up work on closed/merged PRs?

I've moved it to an issue, see #30599

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.