Skip to contentSkip to conversation

Handle the case when KernelShark is started as Root

PATCH New Yordan Karadzhov (VMware) 1 participant 6 messages Sep 20, 2019

lore
Share & tools
lore
#0 lore raw

v3 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.

v3 changes:
- Polishing the text of the warning/error messages in patches 1/5 and 5/5. The modifications have been suggested by Steven.

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
#1 lore raw

PATCH v3 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
index 6439265..904d682 100644
@@ -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 is running with Root privileges.");
+	warn.setInformativeText("Continue at 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
index 62e66a0..4a7b8ab 100644
@@ -238,6 +238,8 @@ private:
 
 	void _deselectB();
 
+	void _rootWarning();
+
 	void _updateFilterMenu();
 
 	void _filterSyncCBoxUpdate(kshark_context *kshark_ctx);
Signature · 1 line
2.20.1
#2 lore raw

PATCH v3 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
index 904d682..c220afb 100644
@@ -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
#3 lore raw

PATCH v3 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
index c220afb..e24b0f5 100644
@@ -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
#4 lore raw

PATCH v3 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
index e24b0f5..311aa87 100644
@@ -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
index 4a7b8ab..eef4f96 100644
@@ -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
#5 lore raw

PATCH v3 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
index 311aa87..a583f5f 100644
@@ -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