Feature request: bitcoin-wallet tool: don’t modify files unless requested #15608

issue harding openend this issue on March 15, 2019
  1. harding commented at 2:26 pm on March 15, 2019: contributor

    When only getting info for a wallet with the bitcoin-wallet tool, I was surprised to find it changing the file modification time (but happy that it didn’t also seem to modify the file contents):

     0$ stat --format=%Y /tmp/test.dat ; sha256sum /tmp/test.dat
     11552658778
     28d932caabee0dc8d42c7ea362fb685325bd23f217557135282279f5b14233eec  /tmp/test.dat
     3
     4$ bitcoin/src/bitcoin-wallet -wallet=/tmp/test.dat info
     5Wallet info
     6[...]
     7
     8$ stat --format=%Y /tmp/test.dat ; sha256sum /tmp/test.dat
     91552658867
    108d932caabee0dc8d42c7ea362fb685325bd23f217557135282279f5b14233eec  /tmp/test.dat
    

    Setting the file permissions to read-only also surprisingly causes the tool to fail:

    0$ chmod 400 /tmp/test.dat 
    1$ bitcoin/src/bitcoin-wallet -wallet=/tmp/test.dat info
    2Error loading /tmp/test.dat. Is wallet being used by another process?
    

    It also fails on a read-only filesystem even if the permission bits are set to writable:

    0$ chmod 600 /tmp/test.dat 
    1$ sudo mount -t tmpfs -o size=10000000 tmpfs /mnt
    2$ sudo cp -a /tmp/test.dat /mnt
    3$ sudo mount -o remount,ro /mnt
    4$ bitcoin/src/bitcoin-wallet -wallet=/mnt/test.dat info
    5Error loading /mnt/test.dat. Is wallet being used by other process?
    6$ ls -lh /mnt/test.dat 
    7-rw------- 1 user user 1.4M Mar 15 10:07 /mnt/test.dat
    

    I’d expect the tool to open files read-only when it doesn’t need to make any modifications in order to ensure it doesn’t make any modifications. Additionally, I’d expect the tool to be able to examine backup files that have been safeguarded by making them read-only in some way.

    See also IRC discussion, including @laanwj mentioning a DB_RDONLY flag.

  2. harding commented at 2:31 pm on March 15, 2019: contributor

    Nitpick: I also just noticed, looking over my pastes above, that the error messages for read-only-permissions and read-only-filesystem differ slightly for some reason:

    0Error loading /tmp/test.dat. Is wallet being used by another process?
    1Error loading /mnt/test.dat. Is wallet being used by other process?
    
  3. fanquake added the label Utils/log/libs on Mar 15, 2019
  4. jonatack commented at 3:30 pm on March 15, 2019: member

    Reproduced.

     0$ stat --format=%Y /home/jon/.bitcoin/wallets/wallet.dat ; shasum /home/jon/.bitcoin/wallets/wallet.dat
     11552663315
     292a68e8a779c223cc5a66b075a9c8cba7a515627  /home/jon/.bitcoin/wallets/wallet.dat
     3
     4$ src/bitcoin-wallet -wallet=/home/jon/.bitcoin/wallets/wallet.dat info
     5Wallet info
     6[...]
     7
     8$ stat --format=%Y /home/jon/.bitcoin/wallets/wallet.dat ; shasum /home/jon/.bitcoin/wallets/wallet.dat
     91552663564
    1092a68e8a779c223cc5a66b075a9c8cba7a515627  /home/jon/.bitcoin/wallets/wallet.dat
    11
    12$ chmod 400 /home/jon/.bitcoin/wallets/wallet.dat
    13$ src/bitcoin-wallet -wallet=/home/jon/.bitcoin/wallets/wallet.dat info
    14Error loading /home/jon/.bitcoin/wallets/wallet.dat. Is wallet being used by another process?
    
  5. laanwj commented at 3:32 pm on March 15, 2019: member

    I’ve done some preliminary investigation here. To make it work on a read-only filesystem there are multiple issues, at least:

    1. Creation of the wallets directory (even if it clearly already exists!)
    0$ src/bitcoin-wallet -regtest -wallet=wallet.dat info
    1terminate called after throwing an instance of 'boost::filesystem::filesystem_error'
    2  what():  boost::filesystem::create_directory: Permission denied: "/.../regtest/wallets/database"
    
    1. Creation of the lock file .walletlock
    2. Opening of the database itself in rw mode

    Opening the database itself read-only is easier. I’ve found that replacing nFlags in BerkeleyBatch::BerkeleyBatch in wallet/db.cpp with just DB_RDONLY is enough to open a database file that has no write permissions for the current user. It succesfully executes the info command.

    I suppose to implement this, there will need to be a flag, per bitcoin-tool command, whether to open the wallet in read-only or read-write mode. In the first case it needs to bypass (1) and (2) as well.

  6. jonatack referenced this in commit 112ea2a4ba on Mar 25, 2019
  7. jonatack referenced this in commit c5ebe5f0c5 on Mar 25, 2019
  8. jonatack referenced this in commit ec601487ff on Mar 25, 2019
  9. jonatack commented at 2:14 pm on March 25, 2019: member

    First attempt at a test to reproduce. I haven’t figured out yet how to test src/bitcoin-wallet info so used node.getwalletinfo() as a proxy. Apologies if this doesn’t add value; pointers or guidance welcome. Here is the test output:

     0$ test/functional/wallet_readonly.py 
     12019-03-25T13:59:56 (INFO): Initializing test directory /tmp/bitcoin_func_test_j360767k
     22019-03-25T13:59:57 (INFO): Fetch wallet file timestamp before getwalletinfo
     32019-03-25T13:59:57 (INFO): Wallet file timestamp before getwalletinfo: 1553522397.0744224
     42019-03-25T13:59:57 (INFO): Call getwalletinfo
     52019-03-25T13:59:57 (INFO): Fetch wallet file timestamp after getwalletinfo
     62019-03-25T13:59:57 (INFO): Wallet file timestamp after getwalletinfo: 1553522397.1064227
     72019-03-25T13:59:57 (INFO): Assert wallet file timestamp increased
     82019-03-25T13:59:57 (INFO): Stopping nodes
     92019-03-25T13:59:57 (INFO): Cleaning up /tmp/bitcoin_func_test_j360767k on exit
    102019-03-25T13:59:57 (INFO): Tests successful
    
  10. MarcoFalke commented at 3:23 pm on March 25, 2019: member
    @jonatack There is a ./test/functional/tool_wallet.py that you could modify
  11. jonatack referenced this in commit 2d259bf31d on Mar 27, 2019
  12. jonatack referenced this in commit 42a4a28fdb on Mar 28, 2019
  13. jonatack referenced this in commit 6d9b033e3d on Mar 28, 2019
  14. jonatack referenced this in commit 3e2668f7e6 on Mar 30, 2019
  15. jonatack referenced this in commit 7abd4550d7 on Apr 3, 2019
  16. jonatack referenced this in commit 96e69a2ace on Apr 5, 2019
  17. jonatack referenced this in commit 1b286d39e3 on Jul 2, 2019
  18. jonatack referenced this in commit cb47121f70 on Jul 2, 2019
  19. jonatack referenced this in commit d3363211ae on Jul 2, 2019
  20. jonatack referenced this in commit 497af0c8b2 on Jul 3, 2019
  21. jonatack referenced this in commit 7195fa792f on Jul 8, 2019
  22. laanwj referenced this in commit b5fa2319d8 on Jul 8, 2019
  23. sidhujag referenced this in commit a60f2ac4bc on Jul 9, 2019
  24. sidhujag referenced this in commit 3eef7b22aa on Jul 9, 2019
  25. HashUnlimited referenced this in commit 6126ba09e3 on Aug 30, 2019
  26. barrystyle referenced this in commit 0c0a4e70fe on Nov 11, 2019
  27. jonatack referenced this in commit 7d457fafcf on May 17, 2020
  28. monstrobishi referenced this in commit 52e54f3b82 on Sep 6, 2020
  29. monstrobishi referenced this in commit 387d1f4494 on Sep 6, 2020
  30. jasonbcox referenced this in commit 2030c4ba65 on Oct 1, 2020
  31. vijaydasmp referenced this in commit 7b692e00b9 on Dec 6, 2021
  32. vijaydasmp referenced this in commit 8a1097d724 on Dec 10, 2021
  33. vijaydasmp referenced this in commit 0087a7924a on Dec 13, 2021
  34. vijaydasmp referenced this in commit 1849b8e10d on Dec 13, 2021
  35. vijaydasmp referenced this in commit 3d8be2d355 on Dec 15, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-09-29 01:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me