Handle the case when KernelShark is started as Root
v2 cover Handle the case when KernelShark is started as Root
A patch set implementing the improvements suggested here:
https://bugzilla.kernel.org/show_bug.cgi?id=204475
Plus a minor improvements of the error messages printed when something goes wrong withe the "Record" dialog.
v2 changes:
- Correcting typos in patch 1/5
- Fixing a bug in patch 2/5. trace-cmd now gets correctly formatted list of command line arguments.
- Patches 4/5 and 5/5 are new.
Yordan Karadzhov (VMware) (5):
kernel-shark: Show warning message when running as Root
kernel-shark: Don't use pkexec when running as Root
kernel-shark: Use standart error message in KsMainWindow::_record()
kernel-shark: Optimize the error messages when "Record" fails to start
kernel-shark: Better formatting of the error messages from "Record"
kernel-shark/src/KsMainWindow.cpp | 90 +++++++++++++++++++++++-------- kernel-shark/src/KsMainWindow.hpp | 4 ++ 2 files changed, 71 insertions(+), 23 deletions(-)
Signature · 1 line
2.20.1
PATCH v2 1/5 kernel-shark: Show warning message when running as Root
Running the KernelShark GUI with Root privileges is not recommended due to security reasons. The user will be allowed to continue on its own risk.
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=204475
--- kernel-shark/src/KsMainWindow.cpp | 28 ++++++++++++++++++++++++++++ kernel-shark/src/KsMainWindow.hpp | 2 ++ 2 files changed, 30 insertions(+)
kernel-shark/src/KsMainWindow.cpp +28 -0
@@ -76,6 +76,9 @@ KsMainWindow::KsMainWindow(QWidget *parent) _createMenus(); _initCapture(); + if (geteuid() == 0) + _rootWarning(); + _splitter.addWidget(&_graph); _splitter.addWidget(&_view); setCentralWidget(&_splitter);
@@ -1271,3 +1274,28 @@ void KsMainWindow::_deselectB() _mState.updateLabels(); _graph.glPtr()->model()->update(); } + +void KsMainWindow::_rootWarning() +{ + QString cbFlag("noRootWarn"); + + if (_settings.value(cbFlag).toBool()) + return; + + QMessageBox warn; + warn.setText("KernelShark will have Root privileges."); + warn.setInformativeText("Continue on your own risk."); + warn.setIcon(QMessageBox::Warning); + warn.setStandardButtons(QMessageBox::Close); + + QCheckBox cb("Don't show this message again."); + + auto lamCbChec = [&] (int state) { + if (state) + _settings.setValue(cbFlag, true); + }; + + connect(&cb, &QCheckBox::stateChanged, lamCbChec); + warn.setCheckBox(&cb); + warn.exec(); +}
kernel-shark/src/KsMainWindow.hpp +2 -0
@@ -238,6 +238,8 @@ private: void _deselectB(); + void _rootWarning(); + void _updateFilterMenu(); void _filterSyncCBoxUpdate(kshark_context *kshark_ctx);
Signature · 1 line
2.20.1
Re: [PATCH v2 1/5] kernel-shark: Show warning message when running as Root
kernel-shark/src/KsMainWindow.cpp void KsMainWindow::_deselectB() · line 1287
Quoted 2 lines · On Mon, 26 Aug 2019 18:58:07 +0300 "Yordan Karadzhov (VMware)"…
kernel-shark/src/KsMainWindow.cpp +2
-0+ warn.setText("KernelShark will have Root privileges."); + warn.setInformativeText("Continue on your own risk.");
This wasn't updated.
From: https://lore.kernel.org/linux-trace-devel/[email protected]/
Quoted 8 lines
kernel-shark/src/KsMainWindow.cpp void KsMainWindow::_deselectB()
Quoted 3 lines
kernel-shark/src/KsMainWindow.cpp +3
-0+ QMessageBox warn; + warn.setText("KernelShark will have Root privileges."); + warn.setInformativeText("Continue on your one risk.");Probably better to say:
"KernelShark is running with Root privileges."
"Continue at your own risk."
I could update the patch if you like, instead of sending out another series. Let me finish the rest of the series.
-- Steve
Re: [PATCH v2 1/5] kernel-shark: Show warning message when running as Root
Quoted 16 lines · On Tue, 27 Aug 2019 18:22:53 -0400 Steven Rostedt <[email protected]> wrote:
kernel-shark/src/KsMainWindow.cpp void KsMainWindow::_deselectB() · line 1287
Quoted 2 lines · On Mon, 26 Aug 2019 18:58:07 +0300 "Yordan Karadzhov (VMware)"…
kernel-shark/src/KsMainWindow.cpp +2
-0+ warn.setText("KernelShark will have Root privileges."); + warn.setInformativeText("Continue on your own risk.");This wasn't updated.
From: https://lore.kernel.org/linux-trace-devel/[email protected]/
Quoted 6 lines
kernel-shark/src/KsMainWindow.cpp void KsMainWindow::_deselectB()
Quoted 3 lines
kernel-shark/src/KsMainWindow.cpp +3
-0+ QMessageBox warn; + warn.setText("KernelShark will have Root privileges."); + warn.setInformativeText("Continue on your one risk.");Probably better to say:
Quoted 6 lines
Quoted 2 lines
"KernelShark is running with Root privileges."
"Continue at your own risk."I could update the patch if you like, instead of sending out another series. Let me finish the rest of the series.
We decided in our meeting that you were going to send a v3, right? Just want to make sure (too many things to keep track of ;-)
Thanks!
-- Steve
Re: [PATCH v2 1/5] kernel-shark: Show warning message when running as Root
Quoted 22 lines · Steven Rostedt
Quoted 19 lines · On Tue, 27 Aug 2019 18:22:53 -0400 Steven Rostedt <[email protected]> wrote:
kernel-shark/src/KsMainWindow.cpp void KsMainWindow::_deselectB() · line 1287
Quoted 2 lines · On Mon, 26 Aug 2019 18:58:07 +0300 "Yordan Karadzhov (VMware)"…
kernel-shark/src/KsMainWindow.cpp +2
-0+ warn.setText("KernelShark will have Root privileges."); + warn.setInformativeText("Continue on your own risk.");This wasn't updated.
From: https://lore.kernel.org/linux-trace-devel/[email protected]/
Quoted 8 lines
kernel-shark/src/KsMainWindow.cpp void KsMainWindow::_deselectB()
Quoted 3 lines
kernel-shark/src/KsMainWindow.cpp +3
-0+ QMessageBox warn; + warn.setText("KernelShark will have Root privileges."); + warn.setInformativeText("Continue on your one risk.");Probably better to say:
"KernelShark is running with Root privileges."
"Continue at your own risk."
Quoted 6 lines
Quoted 3 lines
I could update the patch if you like, instead of sending out another series. Let me finish the rest of the series.
We decided in our meeting that you were going to send a v3, right? Just want to make sure (too many things to keep track of ;-)
Yes, I am sending v3 right now.
Y.
Quoted 5 lines
Thanks!
-- Steve
PATCH v2 2/5 kernel-shark: Don't use pkexec when running as Root
In reply to Yordan Karadzhov (VMware)If KernelShark GUI has been started as Root we do not need to use "pkexec" when starting the Record dialog. Note that the actual place where "pkexec" gets used is in the script "kshark-su-record".
--- kernel-shark/src/KsMainWindow.cpp | 47 +++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 15 deletions(-)
kernel-shark/src/KsMainWindow.cpp +32 -15
@@ -883,23 +883,26 @@ void KsMainWindow::_pluginAdd() void KsMainWindow::_record() { -#ifndef DO_AS_ROOT + bool canDoAsRoot(false); - QErrorMessage *em = new QErrorMessage(this); - QString message; - - message = "Record is currently not supported."; - message += " Install \"pkexec\" and then do:<br>"; - message += " cd build <br> sudo ./cmake_uninstall.sh <br>"; - message += " ./cmake_clean.sh <br> cmake .. <br> make <br>"; - message += " sudo make install"; +#ifdef DO_AS_ROOT + canDoAsRoot = true; +#endif - em->showMessage(message); - qCritical() << "ERROR: " << message; + if (geteuid() && !canDoAsRoot) { + QErrorMessage *em = new QErrorMessage(this); + QString message; - return; + message = "Record is currently not supported."; + message += " Install \"pkexec\" and then do:<br>"; + message += " cd build <br> sudo ./cmake_uninstall.sh <br>"; + message += " ./cmake_clean.sh <br> cmake .. <br> make <br>"; + message += " sudo make install"; -#endif + em->showMessage(message); + qCritical() << "ERROR: " << message; + return; + } _capture.start(); }
@@ -1134,9 +1137,24 @@ void KsMainWindow::loadSession(const QString &fileName) void KsMainWindow::_initCapture() { + bool canDoAsRoot(false); + #ifdef DO_AS_ROOT + canDoAsRoot = true; +#endif + + if (geteuid() && !canDoAsRoot) + return; - _capture.setProgram("kshark-su-record"); + if (geteuid()) { + _capture.setProgram("kshark-su-record"); + } else { + QStringList argv; + + _capture.setProgram("kshark-record"); + argv << QString("-o") << QDir::homePath() + "/trace.dat"; + _capture.setArguments(argv); + } connect(&_capture, &QProcess::started, this, &KsMainWindow::_captureStarted);
@@ -1155,7 +1173,6 @@ void KsMainWindow::_initCapture() connect(&_captureLocalServer, &QLocalServer::newConnection, this, &KsMainWindow::_readSocket); -#endif } void KsMainWindow::_captureStarted()
Signature · 1 line
2.20.1
PATCH v2 3/5 kernel-shark: Use standart error message in KsMainWindow::_record()
In reply to Yordan Karadzhov (VMware)The error message is printed using the method KsMainWindow::_error(). The message itself remains unchanged. If we want to change the message, this can be done in another patch.
--- kernel-shark/src/KsMainWindow.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
kernel-shark/src/KsMainWindow.cpp +1 -3
@@ -890,7 +890,6 @@ void KsMainWindow::_record() #endif if (geteuid() && !canDoAsRoot) { - QErrorMessage *em = new QErrorMessage(this); QString message; message = "Record is currently not supported.";
@@ -899,8 +898,7 @@ void KsMainWindow::_record() message += " ./cmake_clean.sh <br> cmake .. <br> make <br>"; message += " sudo make install"; - em->showMessage(message); - qCritical() << "ERROR: " << message; + _error(message, "recordCantStart", false, false); return; }
Signature · 1 line
2.20.1
PATCH v2 4/5 kernel-shark: Optimize the error messages when "Record" fails to start
In reply to Yordan Karadzhov (VMware)Removing duplicate code.
--- kernel-shark/src/KsMainWindow.cpp | 14 +++++++------- kernel-shark/src/KsMainWindow.hpp | 2 ++ 2 files changed, 9 insertions(+), 7 deletions(-)
kernel-shark/src/KsMainWindow.cpp +7 -7
@@ -1199,18 +1199,18 @@ void KsMainWindow::_captureFinished(int ret, QProcess::ExitStatus st) return; } - if (ret != 0 && st == QProcess::NormalExit) { - QString message = "Capture process failed:<br>"; - - message += capture->errorString(); - message += capture->readAllStandardError(); - _error(message, "captureFinishedErr", false, false); - } + if (ret != 0 && st == QProcess::NormalExit) + _captureErrorMessage(capture); } void KsMainWindow::_captureError(QProcess::ProcessError error) { QProcess *capture = (QProcess *)sender(); + _captureErrorMessage(capture); +} + +void KsMainWindow::_captureErrorMessage(QProcess *capture) +{ QString message = "Capture process failed:<br>"; message += capture->errorString();
kernel-shark/src/KsMainWindow.hpp +2 -0
@@ -215,6 +215,8 @@ private: void _captureError(QProcess::ProcessError error); + void _captureErrorMessage(QProcess *capture); + void _readSocket(); void _splitterMoved(int pos, int index);
Signature · 1 line
2.20.1
PATCH v2 5/5 kernel-shark: Better formatting of the error messages from "Record"
In reply to Yordan Karadzhov (VMware)Make the message easier to read and understand by separating the QProcess's error and the Standard error.
--- kernel-shark/src/KsMainWindow.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel-shark/src/KsMainWindow.cpp +2 -1
@@ -1211,9 +1211,10 @@ void KsMainWindow::_captureError(QProcess::ProcessError error) void KsMainWindow::_captureErrorMessage(QProcess *capture) { - QString message = "Capture process failed:<br>"; + QString message = "Capture process failed: "; message += capture->errorString(); + message += "<br>Standard Error :"; message += capture->readAllStandardError(); _error(message, "captureFinishedErr", false, false); }
Signature · 1 line
2.20.1
Re: [PATCH v2 5/5] kernel-shark: Better formatting of the error messages from "Record"
Quoted 13 lines · On Mon, 26 Aug 2019 18:58:11 +0300 "Yordan Karadzhov (VMware)"…
Make the message easier to read and understand by separating the QProcess's error and the Standard error.
Signed-off-by Yordan Karadzhov (VMware) <[email protected]>--- kernel-shark/src/KsMainWindow.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)kernel-shark/src/KsMainWindow.cpp +0
-0
kernel-shark/src/KsMainWindow.cpp void KsMainWindow::_captureError(QProcess::ProcessError error) · line 1217
Quoted 8 lines
kernel-shark/src/KsMainWindow.cpp +2
-1@@ -1211,9 +1211,10 @@ void KsMainWindow::_captureError(QProcess::ProcessError error) void KsMainWindow::_captureErrorMessage(QProcess *capture) { - QString message = "Capture process failed:<br>"; + QString message = "Capture process failed: "; message += capture->errorString(); + message += "<br>Standard Error :";
I changed the above to: "<br>Standard Error: ", as I added:
echo "this is an error" >&2
exit 1
to kshark-su-record and the current patch produces:
ERROR: Capture process failed: Unknown error
Standard Error :this is an error
Where I think the below looks better:
ERROR: Capture process failed: Unknown error
Standard Error: this is an error
-- Steve
Quoted 3 lines
kernel-shark/src/KsMainWindow.cpp +0
-0message += capture->readAllStandardError(); _error(message, "captureFinishedErr", false, false); }