Skip to content

util: Fix UB in SetStdinEcho when ENOTTY#34597

Merged
fanquake merged 2 commits intobitcoin:masterfrom
maflcko:2602-less-ub-stdin
Feb 27, 2026
Merged

util: Fix UB in SetStdinEcho when ENOTTY#34597
fanquake merged 2 commits intobitcoin:masterfrom
maflcko:2602-less-ub-stdin

Conversation

@maflcko
Copy link
Member

@maflcko maflcko commented Feb 16, 2026

The call to tcgetattr may fail with ENOTTY, leaving the struct possibly uninitialized (UB).

Fix this UB by returning early when isatty fails, or when tcgetattr fails. (Same for Windows)

This can be tested by a command that fails valgrind before the change and passes after:

echo 'pipe' | valgrind --quiet ./bld-cmake/bin/bitcoin-cli -stdinrpcpass uptime

@DrahtBot
Copy link
Contributor

DrahtBot commented Feb 16, 2026

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

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK achow101, sedited, l0rinc

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #34589 (ci: Temporarily use clang in valgrind tasks by maflcko)
  • #34004 (Implementation of SwiftSync by rustaceanrob)

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.

Copy link
Contributor

@l0rinc l0rinc 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

Does this enable the removal of

implicit-integer-sign-change:SetStdinEcho
?

@maflcko
Copy link
Member Author

maflcko commented Feb 16, 2026

No, that is just a harmless warning. I guess I can refactor it a bit while touching this...

@maflcko
Copy link
Member Author

maflcko commented Feb 16, 2026

Pushed that as well. Needs compilation with the integer sanitizer (-DSANITIZERS=integer, ref: https://godbolt.org/z/YGjWa89vs) and can be tested via:

UBSAN_OPTIONS="suppressions=$(pwd)/test/sanitizer_suppressions/ubsan:print_stacktrace=1:halt_on_error=1:report_error_type=1" ./bld-cmake/bin/bitcoin-cli -stdinrpcpass uptime

// https://stackoverflow.com/questions/1413445/reading-a-password-from-stdcin
void SetStdinEcho(bool enable)
{
if (!StdinTerminal()) {
Copy link
Member

Choose a reason for hiding this comment

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

Since this is now checked here, should failure below log an error? (Maybe visibly so the user knows to expect echo?)

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think an error can happen, so this seems a bit too much effort for basically dead/untested code

Copy link
Member Author

Choose a reason for hiding this comment

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

actually, done with a simple fputs and a string literal for each case

}
if (!enable) {
tty.c_lflag &= ~ECHO;
tty.c_lflag &= static_cast<decltype(tty.c_lflag)>(~ECHO);
Copy link
Member

Choose a reason for hiding this comment

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

Probably should do the case before the bitwise negation?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think it matters here, because everything will be compiled down to the same executable anyway and this is just to silence a harmless ubsan warning.

MarcoFalke added 2 commits February 18, 2026 21:28
…r warning

This refactor does not change any behavior, except for the integer
sanitizer warning.

Can be tested via:

UBSAN_OPTIONS="suppressions=$(pwd)/test/sanitizer_suppressions/ubsan:print_stacktrace=1:halt_on_error=1:report_error_type=1" ./bld-cmake/bin/bitcoin-cli -stdinrpcpass uptime
@achow101
Copy link
Member

ACK fa6af85

@DrahtBot DrahtBot requested a review from l0rinc February 23, 2026 23:33
@maflcko maflcko requested a review from luke-jr February 24, 2026 10:09
Copy link
Contributor

@sedited sedited left a comment

Choose a reason for hiding this comment

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

ACK fa6af85

Was thinking why we don't just throw on these errors, but I think printing something to stderr is the correct thing to do, and introducing exceptions was discussed in the original PR: #13716 (comment) .

@maflcko
Copy link
Member Author

maflcko commented Feb 26, 2026

Was thinking why we don't just throw on these errors, but I think printing something to stderr is the correct thing to do, and introducing exceptions was discussed in the original PR: #13716 (comment) .

I think that was referring to the test/lint/lint-locale-dependence.py exception. But yeah, printing to stderr seems sufficient and aborting the whole program may not be needed.

@l0rinc
Copy link
Contributor

l0rinc commented Feb 27, 2026

lightly tested code review ACK fa6af85

Thanks for fixing these and removing the suppressions - they always make me nervous.

@fanquake fanquake merged commit 05cd3b0 into bitcoin:master Feb 27, 2026
26 checks passed
@maflcko maflcko deleted the 2602-less-ub-stdin branch February 27, 2026 11:10
fanquake pushed a commit to fanquake/bitcoin that referenced this pull request Feb 27, 2026
fanquake pushed a commit to fanquake/bitcoin that referenced this pull request Feb 27, 2026
…r warning

This refactor does not change any behavior, except for the integer
sanitizer warning.

Can be tested via:

UBSAN_OPTIONS="suppressions=$(pwd)/test/sanitizer_suppressions/ubsan:print_stacktrace=1:halt_on_error=1:report_error_type=1" ./bld-cmake/bin/bitcoin-cli -stdinrpcpass uptime

Github-Pull: bitcoin#34597
Rebased-From: fa6af85
@fanquake fanquake mentioned this pull request Feb 27, 2026
@fanquake
Copy link
Member

Backported to 30.x in #34689.

fanquake pushed a commit to fanquake/bitcoin that referenced this pull request Mar 9, 2026
fanquake pushed a commit to fanquake/bitcoin that referenced this pull request Mar 9, 2026
…r warning

This refactor does not change any behavior, except for the integer
sanitizer warning.

Can be tested via:

UBSAN_OPTIONS="suppressions=$(pwd)/test/sanitizer_suppressions/ubsan:print_stacktrace=1:halt_on_error=1:report_error_type=1" ./bld-cmake/bin/bitcoin-cli -stdinrpcpass uptime

Github-Pull: bitcoin#34597
Rebased-From: fa6af85
fanquake added a commit that referenced this pull request Mar 11, 2026
49a777d doc: update release notes for v30.x (fanquake)
0f9e08f doc: update build guides pre v31 (fanquake)
597ac36 doc: Fix `fee` field in `getblock` RPC result (nervana21)
47ed306 depends: Allow building Qt packages after interruption (Hennadii Stepanov)
d221d1c psbt: validate pubkeys in MuSig2 pubnonce/partial sig deserialization (tboy1337)
e1210ac doc: Improve dependencies.md IPC documentation (Ryan Ofsky)
c17a5cd test: Add missing timeout_factor to zmq socket (MarcoFalke)
3042509 netif: fix compilation warning in QueryDefaultGatewayImpl() (MarcoFalke)
475a5b0 refactor: Use static_cast<decltype(...)> to suppress integer sanitizer warning (MarcoFalke)
7220ee3 util: Fix UB in SetStdinEcho when ENOTTY (MarcoFalke)

Pull request description:

  Backports:
  * #34093
  * #34219
  * #34597
  * #34690
  * #34702
  * #34706
  * #34713
  * #34789

ACKs for top commit:
  marcofleon:
    ACK 49a777d

Tree-SHA512: b4ce54860b7306b22de75bb093ad574110875253e4ea3ca96a736809c8291dea1144a617c8791f36618d8e367022709ba5cf84ca0e450ef6d76394ab80f22e2f
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.

7 participants