Skip to content

Conversation

@achow101
Copy link
Member

@achow101 achow101 commented Aug 9, 2019

QApplication takes the command line arguments and parses them itself for some built in command line arguments that it has. We don't want any of those built in arguments, so instead give it dummy arguments.

To test, you can use the -reverse option. Without this patch, everything will appear right-to-left; things that were on the left side will be on the right and everything is right aligned.

After this patch, -reverse will now give a startup error since we do not support this argument.

QApplication takes the command line arguments and parses them itself
for some built in command line arguments that it has. We don't want
any of those built in arguments, so instead give it dummy arguments.
@promag
Copy link
Contributor

promag commented Aug 9, 2019

We don't want any of those built in arguments

Care to explain why?

@achow101
Copy link
Member Author

achow101 commented Aug 9, 2019

Care to explain why?

They can do unexpected things and cause issues. Qt can also just add more arguments that do other stuff. We have a larger attack surface by allowing this.

@fanquake
Copy link
Member

@theuni might want to comment here, as he was asking about if the changes in #16386 would remove the ability to add qt-specific runtime args to bitcoin-qt ?.

@fanquake fanquake removed the Tests label Aug 10, 2019
BitcoinApplication::BitcoinApplication(interfaces::Node& node, int &argc, char **argv):
QApplication(argc, argv),
static int qt_argc = 1;
static const char* qt_argv = "bitcoin-qt";
Copy link
Member

@laanwj laanwj Aug 10, 2019

Choose a reason for hiding this comment

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

  • these can be inside the function scope
  • if you remove the const here, you don't need to const_cast below (which is dangerous, as now this will be put in a .rodata section and Qt might try to write it)

Copy link
Contributor

Choose a reason for hiding this comment

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

these can be inside the function scope

What function? Constructor?

Copy link
Contributor

@promag promag Aug 10, 2019

Choose a reason for hiding this comment

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

My suggestion is to make the constructor private and add a static BitcoinApplication::create() where you could declare a temporary static argc and argv and the app.

Copy link
Member

Choose a reason for hiding this comment

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

Please note:

Warning: The data referred to by argc and argv must stay valid for the entire lifetime of the QCoreApplication object.

Copy link
Member Author

Choose a reason for hiding this comment

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

* if you remove the `const` here, you don't need to `const_cast` below (which is dangerous, as now this will be put in a `.rodata` section and Qt might try to write it)

GCC complains if it is non const.

warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]

Copy link
Member Author

@achow101 achow101 Aug 10, 2019

Choose a reason for hiding this comment

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

My suggestion is to make the constructor private and add a static BitcoinApplication::create() where you could declare a temporary static argc and argv and the app.

That requires a copy constructor, and QApplication does not have one. Qt explicitly does not want things to be copied.

Copy link
Member

Choose a reason for hiding this comment

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

GCC complains if it is non const.

Could be:

Suggested change
static const char* qt_argv = "bitcoin-qt";
static char qt_arg[] = "bitcoin-qt";
static char* qt_argv = qt_arg;

?

Copy link
Member

Choose a reason for hiding this comment

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

That requires a copy constructor, and QApplication does not have one. Qt explicitly does not want things to be copied.

Eh, whatever you do, please don't copy around QApplication objects, even if you could. It's a singleton.
It's also definitely not necessary to solve this.

@laanwj
Copy link
Member

laanwj commented Aug 10, 2019

Concept ACK

Copy link
Member

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

BitcoinApplication::BitcoinApplication(interfaces::Node& node, int &argc, char **argv):
QApplication(argc, argv),
static int qt_argc = 1;
static const char* qt_argv = "bitcoin-qt";
Copy link
Member

Choose a reason for hiding this comment

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

Please note:

Warning: The data referred to by argc and argv must stay valid for the entire lifetime of the QCoreApplication object.

@laanwj
Copy link
Member

laanwj commented Aug 10, 2019

@hebasto that's why the variables are static, it holds just as true for static variables within a function scope

@fanquake
Copy link
Member

Concept ACK

@practicalswift
Copy link
Contributor

Concept ACK: smaller attack surface is better.

@jonasschnelli
Copy link
Contributor

Concept ACK.
Codewise, I agree with @laanwj that casting const away and passing it to QApplication seems unfortunate.

@promag
Copy link
Contributor

promag commented Aug 10, 2019

How about this

diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp
index 5ce4f3c19..f38235b15 100644
--- a/src/qt/bitcoin.cpp
+++ b/src/qt/bitcoin.cpp
@@ -169,11 +169,8 @@ void BitcoinCore::shutdown()
     }
 }

-static int qt_argc = 1;
-static const char* qt_argv = "bitcoin-qt";
-
 BitcoinApplication::BitcoinApplication(interfaces::Node& node):
-    QApplication(qt_argc, const_cast<char **>(&qt_argv)),
+    QApplication(argc, const_cast<char **>(&argv)),
     coreThread(nullptr),
     m_node(node),
     optionsModel(nullptr),
diff --git a/src/qt/bitcoin.h b/src/qt/bitcoin.h
index 3869193a3..d8b7f3b36 100644
--- a/src/qt/bitcoin.h
+++ b/src/qt/bitcoin.h
@@ -55,6 +55,10 @@ private:
 class BitcoinApplication: public QApplication
 {
     Q_OBJECT
+
+    // Some wild comment here...
+    int argc{1};
+    const char* argv{"bitcoin-qt"};
 public:
     explicit BitcoinApplication(interfaces::Node& node);
     ~BitcoinApplication();

@achow101
Copy link
Member Author

achow101 commented Aug 10, 2019

How about this

Results in a segfault.


FWIW, making the argv const and then casting it is how QCoreApplication does it internally when you say argc is 0. The only reason that I did not choose to just do QApplication(0, 0) is because it results in the warning QSettings::value: Empty key passed being printed a couple of times (at least for me using KDE, this might actually just be a KDE thing).

@fanquake fanquake added this to the 0.19.0 milestone Aug 12, 2019
@laanwj
Copy link
Member

laanwj commented Aug 14, 2019

@achow101 But why add the const at all if you're going to cast it away?

Edit: oh, because of the string constant. I get it, the string is read-only, which is fine, Qt is not going to change the contents of arguments, but the array needs to be writable as it might remove arguments.

@laanwj
Copy link
Member

laanwj commented Aug 14, 2019

ACK a2714a5

@hebasto
Copy link
Member

hebasto commented Aug 14, 2019

Technically, there is a solution without const.

@achow101
Copy link
Member Author

Technically, there is a solution without const.

I'm going to leave it as is. I feel that doing the same thing that Qt does internally is safe and there's no reason that we should not follow suit.

@hebasto
Copy link
Member

hebasto commented Aug 14, 2019

FWIW, making the argv const and then casting it is how QCoreApplication does it internally when you say argc is 0.

Indeed.

ACK a2714a5

Copy link
Member

@fanquake fanquake left a comment

Choose a reason for hiding this comment

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

ACK a2714a5 - Have tested that arguments like -reverse are no longer being passed through and result in an error.

@fanquake fanquake merged commit a2714a5 into bitcoin:master Aug 15, 2019
fanquake added a commit that referenced this pull request Aug 15, 2019
a2714a5 Give QApplication dummy arguments (Andrew Chow)

Pull request description:

  QApplication takes the command line arguments and parses them itself for some [built in command line arguments](https://doc.qt.io/qt-5/qapplication.html#QApplication) that it has. We don't want any of those built in arguments, so instead give it dummy arguments.

  To test, you can use the `-reverse` option. Without this patch, everything will appear right-to-left; things that were on the left side will be on the right and everything is right aligned.

  After this patch, `-reverse` will now give a startup error since we do not support this argument.

ACKs for top commit:
  laanwj:
    ACK a2714a5
  hebasto:
    ACK a2714a5
  fanquake:
    ACK a2714a5 - Have tested that arguments like `-reverse` are no longer being passed through and result in an error.

Tree-SHA512: 983bd948ca6999f895b6662b58c37e33af7ed61fdd600c6b4623febb87ec06a92c66e3b3300783530110cc711902793ef82d751d7f563696c4c3a8416b2b1f51
@fanquake
Copy link
Member

Added for backport in #16617.

@fanquake fanquake mentioned this pull request Aug 15, 2019
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 17, 2019
…ation

a2714a5 Give QApplication dummy arguments (Andrew Chow)

Pull request description:

  QApplication takes the command line arguments and parses them itself for some [built in command line arguments](https://doc.qt.io/qt-5/qapplication.html#QApplication) that it has. We don't want any of those built in arguments, so instead give it dummy arguments.

  To test, you can use the `-reverse` option. Without this patch, everything will appear right-to-left; things that were on the left side will be on the right and everything is right aligned.

  After this patch, `-reverse` will now give a startup error since we do not support this argument.

ACKs for top commit:
  laanwj:
    ACK a2714a5
  hebasto:
    ACK a2714a5
  fanquake:
    ACK a2714a5 - Have tested that arguments like `-reverse` are no longer being passed through and result in an error.

Tree-SHA512: 983bd948ca6999f895b6662b58c37e33af7ed61fdd600c6b4623febb87ec06a92c66e3b3300783530110cc711902793ef82d751d7f563696c4c3a8416b2b1f51
@laanwj
Copy link
Member

laanwj commented Sep 12, 2019

What about QT_QPA_PLATFORM or one of the other environment variables?

the non-specific "larger attack surface" one cited in comments

We don't want to be too specific about this yet.

fanquake pushed a commit to fanquake/bitcoin that referenced this pull request Sep 23, 2019
QApplication takes the command line arguments and parses them itself
for some built in command line arguments that it has. We don't want
any of those built in arguments, so instead give it dummy arguments.

Github-Pull: bitcoin#16578
Rebased-From: a2714a5
@Sjors
Copy link
Member

Sjors commented Oct 1, 2019

I made an issue about the test_bitcoin-qt -platform cocoa: #17013. I see @ryanofsky already spotted it. A QT_QPA_PLATFORM environment variable works for me.

@Sjors
Copy link
Member

Sjors commented Oct 2, 2019

This also breaks the URI handler on macOS, but only when opening from the command line, not when opening from a browser. See #17025.

laanwj added a commit that referenced this pull request Nov 25, 2019
0b18ea6 util: Filter control characters out of log messages (Wladimir J. van der Laan)
ac30fc4 build: Factor out qt translations from build system (Wladimir J. van der Laan)
3b8af5f build: update boost macros to latest upstream (fanquake)
b12defc Test that joinpsbts randomly shuffles the inputs (Andrew Chow)
eb07d22 Shuffle inputs and outputs after joining psbts (Andrew Chow)
1175410 addrdb: Remove temporary files created in SerializeFileDB. Fixes non-determinism in unit tests. (practicalswift)
c52dd12 Handle the result of posix_fallocate system call (Luca Venturini)
f792b25 torcontrol: Use the default/standard network port for Tor hidden services, even if the internal port is set differently (Luke Dashjr)
9fe8d28 Bugfix: QA: Run tests with UPnP disabled (Luke Dashjr)
1d12e52 Add vertical spacer (Josu Goñi)
d764141 depends: add patch to common dependencies (fanquake)
56815e9 Give QApplication dummy arguments (Andrew Chow)
9d389d0 util: No translation of `Bitcoin Core` in the copyright (MarcoFalke)
87908e9 scripted-diff: Avoid passing PACKAGE_NAME for translation (MarcoFalke)
a44e18f build: Stop translating PACKAGE_NAME (MarcoFalke)
7bd8f4e rpc: Fix getblocktemplate CLI example (#16594) (Emil Engler)
1cc06a1 doc: Fix typos in COPYRIGHT (Chuf)

Pull request description:

  Backports some commits to the `0.18` branch:

  * #16596 - rpc: Fix getblocktemplate CLI example
  * #16615 - doc: Fix typos in COPYRIGHT
  * #16291 - gui: Stop translating PACKAGE_NAME (without the `make translate` commit)
  * #16578 - Do not pass in command line arguments to QApplication
  * #16051 - depends: add patch to common dependencies
  * #16090 - Add vertical spacer
  * #15651 - torcontrol: Use the default/standard network port for Tor hidden services, even if the internal port is set differently
  * #15650 - Handle the result of posix_fallocate system call
  * #16646 - Bugfix: QA: Run tests with UPnP disabled
  * #16212 - addrdb: Remove temporary files created in SerializeFileDB. Fixes non-determinism in unit tests.
  * #16512 - rpc: Shuffle inputs and outputs after joining psbts
  * #16870 - build: update boost macros to latest upstream for improved error reporting
  * #16982 - build: Factor out qt translations from build system
  * #17095 - util: Filter control characters out of log messages

ACKs for top commit:
  laanwj:
    ACK 0b18ea6

Tree-SHA512: 37f0e5afc20975f4d1506e8662eda2ae0125f2f424a852818b5af2c3b8db78fc1c365b83571aa80ca63c885ca314302190b891a50ff3851fda9b9238455a5627
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 2, 2020
Summary:
a2714a5c69f0b0506689af04c3e785f71ee0915d Give QApplication dummy arguments (Andrew Chow)

Pull request description:

  QApplication takes the command line arguments and parses them itself for some [built in command line arguments](https://doc.qt.io/qt-5/qapplication.html#QApplication) that it has. We don't want any of those built in arguments, so instead give it dummy arguments.

  To test, you can use the `-reverse` option. Without this patch, everything will appear right-to-left; things that were on the left side will be on the right and everything is right aligned.

  After this patch, `-reverse` will now give a startup error since we do not support this argument.

---

Backport of Core [[bitcoin/bitcoin#16578 | PR16578]]

Test Plan:
  ninja all check check-functional

Check that `bitcoin-qt -reverse -regtest` gives an error with this patch and reverses the GUI without i.t

Reviewers: #bitcoin_abc, jasonbcox

Reviewed By: #bitcoin_abc, jasonbcox

Subscribers: jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D7747
@achow101
Copy link
Member Author

achow101 commented Feb 1, 2021

Disclosure of the vulnerability this PR fixes: https://achow101.com/2021/02/0.18-uri-vuln

A possible followup now that this is public would be to explicitly allow safe Qt arguments instead of flatly disabling all of them.

@practicalswift
Copy link
Contributor

@achow101 Very nice find and thanks for sharing! Has this one been assigned a CVE yet?

@hebasto
Copy link
Member

hebasto commented Mar 30, 2021

Has this one been assigned a CVE yet?

CVE-2021-3401

DeckerSU added a commit to DeckerSU/KomodoOcean that referenced this pull request Jun 26, 2021
- bitcoin/bitcoin#16578
- https://achow101.com/2021/02/0.18-uri-vuln (CVE-2021-3401)
- https://www.twitch.tv/videos/897351419

p.s. about explicit constructor for KomodoApplication class,
according C++ Standart 12.3.1 "a default constructor
may be an explicit constructor", so, let it be here as is (with
explicit keyword).
Munkybooty pushed a commit to Munkybooty/dash that referenced this pull request Nov 30, 2021
…ation

a2714a5 Give QApplication dummy arguments (Andrew Chow)

Pull request description:

  QApplication takes the command line arguments and parses them itself for some [built in command line arguments](https://doc.qt.io/qt-5/qapplication.html#QApplication) that it has. We don't want any of those built in arguments, so instead give it dummy arguments.

  To test, you can use the `-reverse` option. Without this patch, everything will appear right-to-left; things that were on the left side will be on the right and everything is right aligned.

  After this patch, `-reverse` will now give a startup error since we do not support this argument.

ACKs for top commit:
  laanwj:
    ACK a2714a5
  hebasto:
    ACK a2714a5
  fanquake:
    ACK a2714a5 - Have tested that arguments like `-reverse` are no longer being passed through and result in an error.

Tree-SHA512: 983bd948ca6999f895b6662b58c37e33af7ed61fdd600c6b4623febb87ec06a92c66e3b3300783530110cc711902793ef82d751d7f563696c4c3a8416b2b1f51
hans-schmidt pushed a commit to RavenProject/Ravencoin that referenced this pull request Feb 9, 2022
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.