Skip to contentSkip to conversation

Handle the case when KernelShark is started as Root

PATCH Superseded Yordan Karadzhov (VMware) 2 participants 8 messages Aug 9, 2019 1 message not in the archive

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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

Yordan Karadzhov (VMware) (3):
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/src/KsMainWindow.cpp | 73 ++++++++++++++++++++++++-------
 kernel-shark/src/KsMainWindow.hpp |  2 +
 2 files changed, 60 insertions(+), 15 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/3 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..2560bf8 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("noRootWorn");
+
+	if (_settings.value(cbFlag).toBool())
+		return;
+
+	QMessageBox warn;
+	warn.setText("KernelShark will have Root privileges.");
+	warn.setInformativeText("Continue on your one 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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw

Re: [PATCH 1/3] kernel-shark: Show warning message when running as Root

Hey Mike,
Very well spotted, thanks!

It will be great if you can test the patches on your machine.

cheers,
Yordan

Quoted 83 lines · Mike Auty

Hiya,

I can't really review the rest of the code, my main language is python, but I spotted a couple of minor typos that might be worth fixing? I didn't want to post them to everyone since they're fairly trivial. 5:S

"noRootWorn" might want to "noRootWarn" "your one risk." might want to be "your own risk."

Thanks for putting the patches together so quickly! 5:)

Mike 5:)

Quoted 68 lines · Yordan Karadzhov (VMware)

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..2560bf8 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("noRootWorn");
+
+	if (_settings.value(cbFlag).toBool())
+		return;
+
+	QMessageBox warn;
+	warn.setText("KernelShark will have Root privileges.");
+	warn.setInformativeText("Continue on your one 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);
#4 lore raw

Re: [PATCH 1/3] kernel-shark: Show warning message when running as Root

In reply to Yordan Karadzhov (VMware)
Quoted 26 lines · On Fri, 9 Aug 2019 11:06:21 +0300 "Yordan Karadzhov (VMware)"…

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 +3 -0
index 6439265..2560bf8 100644
@@ -76,6 +76,9 @@ KsMainWindow::KsMainWindow(QWidget *parent)
 	_createMenus();
 	_initCapture();
 
+	if (geteuid() == 0)
+		_rootWarning();
+
 	_splitter.addWidget(&_graph);
 	_splitter.addWidget(&_view);
 	setCentralWidget(&_splitter);

kernel-shark/src/KsMainWindow.cpp void KsMainWindow::_deselectB() · line 1280

Quoted 7 lines
kernel-shark/src/KsMainWindow.cpp +4 -0
@@ -1271,3 +1274,28 @@ void KsMainWindow::_deselectB()
 	_mState.updateLabels();
 	_graph.glPtr()->model()->update();
 }
+
+void KsMainWindow::_rootWarning()
+{
+	QString cbFlag("noRootWorn");

Mike already pointed this out (Thanks Mike!)

kernel-shark/src/KsMainWindow.cpp void KsMainWindow::_deselectB() · line 1287

Quoted 7 lines
kernel-shark/src/KsMainWindow.cpp +7 -0
+
+	if (_settings.value(cbFlag).toBool())
+		return;
+
+	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."

-- Steve

Quoted 27 lines
kernel-shark/src/KsMainWindow.cpp +14 -0
+	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);
#5 lore raw

PATCH 2/3 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 2560bf8..e9c6d54 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();
+		_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
#6 lore raw

Re: [PATCH 2/3] kernel-shark: Don't use pkexec when running as Root

Quoted 14 lines · On Fri, 9 Aug 2019 11:06:22 +0300 "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 +0 -0
index 2560bf8..e9c6d54 100644

kernel-shark/src/KsMainWindow.cpp void KsMainWindow::_pluginAdd() · line 886

Quoted 5 lines
kernel-shark/src/KsMainWindow.cpp +1 -1
@@ -883,23 +883,26 @@ void KsMainWindow::_pluginAdd()
 
 void KsMainWindow::_record()
 {
-#ifndef DO_AS_ROOT
+	bool canDoAsRoot(false);

Is this the C++ way of doing:

bool canDoAsRoot = false; ?

kernel-shark/src/KsMainWindow.cpp void KsMainWindow::_pluginAdd() · line 888

Quoted 10 lines
kernel-shark/src/KsMainWindow.cpp +1 -8
-	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

BTW, I think we should rename DO_AS_ROOT to "HAS_PKEXEC" as that is much more descriptive of what that macro means.

Quoted 25 lines
kernel-shark/src/KsMainWindow.cpp +14 -4
+	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();
 }

kernel-shark/src/KsMainWindow.cpp void KsMainWindow::loadSession(const QString &fileName) · line 1144

Quoted 8 lines
kernel-shark/src/KsMainWindow.cpp +4 -0
@@ -1134,9 +1137,24 @@ void KsMainWindow::loadSession(const QString &fileName)
 
 void KsMainWindow::_initCapture()
 {
+	bool canDoAsRoot(false);
+
 #ifdef DO_AS_ROOT
+	canDoAsRoot = true;
+#endif

As we are repeating this, why not just do at the top of the file:

#ifdef HAS_PKEXEC

# define has_pkexec 1
#else
# define has_pkexec 0
#endif

And remove the deplicate logic.

kernel-shark/src/KsMainWindow.cpp void KsMainWindow::loadSession(const QString &fileName) · line 1149

Quoted 6 lines
kernel-shark/src/KsMainWindow.cpp +4 -1
+
+	if (geteuid() && !canDoAsRoot)
+		return;
 
-	_capture.setProgram("kshark-su-record");
+	if (geteuid()) {

Also, "geteuid()" is a system call. It causes a call to the kernel each time. We should cache that in a local variable instead, and use that.

	uid_t euid = geteuid();

Then use "euid" for other locations. But its OK to calculated it in the function itself. That is, don't use the value from a different function, as we don't want to worry about adding commands that change the euid later.

-- Steve

Quoted 19 lines
kernel-shark/src/KsMainWindow.cpp +8 -1
+		_capture.setProgram("kshark-su-record");
+	} else {
+		QStringList argv;
+
+		_capture.setProgram("kshark-record");
+		argv << QString("-o ") + QDir::homePath();
+		_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()
#7 lore raw

PATCH 3/3 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 e9c6d54..b462ded 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
#8 lore raw

Re: [PATCH 3/3] kernel-shark: Use standart error message in KsMainWindow::_record()

On Fri, 9 Aug 2019 11:06:23 +0300
"Yordan Karadzhov (VMware)" <[email protected]> wrote:

Typo in subject "standard".

Other than that, it looks good.

-- Steve

Quoted 31 lines

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 e9c6d54..b462ded 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;
 	}