Skip to contentSkip to conversation

backlight-helper: fixes and improvements

PATCH In review Hans de Goede 2 participants 6 messages Feb 17, 2014

lore
Share & tools
lore
#0 lore raw

cover backlight-helper: fixes and improvements

Hi All,

I've tried the backlight code as merged with X running as a regular user + pkexec, and they work as advertised.

When reviewing the changes I noticed some minor and not so minor (security!) issues, which this patch series address.

Thanks & Regards,

Hans

#1 lore raw

PATCH 1/4 backlight: Explain better why we support both pkexec and suid root for the helper

Update the comment about trying suid-root first with some explanations of why pkexec may be preferable in some cases.

---
 src/backlight.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
src/backlight.c +4 -2
index 70c6559..0e63ba5 100644
@@ -270,8 +270,10 @@ static int __backlight_helper_init(struct backlight *b, char *iface)
 	int use_pkexec = 0;
 	int fds[2];
 
-	/* If system policy is to disallow setuid helpers,
-	 * we fallback to invoking PolicyKit. However, as pkexec
+	/*
+	 * Some systems may prefer using PolicyKit's pkexec over
+	 * making the helper suid root, since the suid option will allow
+	 * anyone to control the backlight.  However, as pkexec
 	 * is quite troublesome and not universally available, we
 	 * still try the old fashioned and simple method first.
 	 * Either way, we have to trust that it is our backlight-helper
Signature · 1 line
1.8.5.3
#2 lore raw

PATCH 2/4 backlight: Use System instead of system when checking for pkexec

In reply to Hans de Goede

Event though we've failed to open the backlight normally, we may still be running under a suid-root xserver, so use the servers build in System instead of system so as to properly drop root rights.

---
 src/backlight.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/backlight.c +2 -1
index 0e63ba5..518d756 100644
@@ -41,6 +41,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <xf86.h>
 
 #include "backlight.h"
 #include "fd.h"
@@ -283,7 +284,7 @@ static int __backlight_helper_init(struct backlight *b, char *iface)
 		return 0;
 
 	if ((st.st_mode & (S_IFREG | S_ISUID | S_IXUSR)) != (S_IFREG | S_ISUID | S_IXUSR)) {
-		if (system("pkexec --version"))
+		if (System("pkexec --version"))
 			return 0;
 
 		use_pkexec = 1;
Signature · 1 line
1.8.5.3
#3 lore raw

PATCH 3/4 backlight: Drop rights before executing pkexec

In reply to Hans de Goede

Event though we've failed to open the backlight normally, we may still be running under a suid-root xserver, so drop any elevated rights before executing what we hope will be pkxec.

---
 src/backlight.c | 4 ++++
 1 file changed, 4 insertions(+)
src/backlight.c +4 -0
index 518d756..dc26307 100644
@@ -295,6 +295,10 @@ static int __backlight_helper_init(struct backlight *b, char *iface)
 
 	switch ((b->pid = fork())) {
 	case 0:
+		if (setgid(getgid()) != 0)
+			_exit(127);
+		if (setuid(getuid()) != 0)
+			_exit(127);
 		close(fds[1]);
 		dup2(fds[0], 0);
 		close(fds[0]);
Signature · 1 line
1.8.5.3
#4 lore raw

PATCH 4/4 backlight-helper: Simplify reading the level from stdin

In reply to Hans de Goede

Since the helper is a standalone app, the usual xserver rules of not using stdio because of signal handling don't apply.

And since the helper does run with elevated rights, it is important to keep the code KISS so that it can be audited easily.

This commit replaces the hard to read "raw" read loop with a much simpler loop using fgets, improving readability of the code.

---
 tools/backlight_helper.c | 29 +++++++----------------------
 1 file changed, 7 insertions(+), 22 deletions(-)
tools/backlight_helper.c +7 -22
index fc16fce..11abebc 100644
@@ -9,7 +9,7 @@
 int main(int argc, char *argv[])
 {
 	struct stat st;
-	char buf[1024], *b = buf;
+	char buf[1024];
 	int len, fd;
 
 	if (argc != 2) {
@@ -24,27 +24,12 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
-	while ((len = read(0, b, sizeof(buf) - (b - buf) - 1)) > 0) {
-		len += b - buf;
-		buf[len] = '\0';
-
-		b = buf;
-		do {
-			char *end = strchr(b, '\n');
-			if (end == NULL)
-				break;
-
-			++end;
-			if (write(fd, b, end - b) != end - b) {
-				fprintf(stderr, "Failed to update backlight interface '%s'\n", argv[1]);
-				return 2;
-			}
-
-			b = end;
-		} while (1);
-
-		memmove(buf, b, len = buf + len - b);
-		b = buf + len;
+	while (fgets(buf, sizeof(buf), stdin)) {
+		len = strlen(buf);
+		if (write(fd, buf, len) != len) {
+			fprintf(stderr, "Failed to update backlight interface '%s'\n", argv[1]);
+			return 2;
+		}
 	}
 
 	return 0;
Signature · 1 line
1.8.5.3
#5 lore raw

Re: [PATCH 4/4] backlight-helper: Simplify reading the level from stdin

Quoted 8 lines · Hans de Goede

Since the helper is a standalone app, the usual xserver rules of not using stdio because of signal handling don't apply.

And since the helper does run with elevated rights, it is important to keep the code KISS so that it can be audited easily.

This commit replaces the hard to read "raw" read loop with a much simpler loop using fgets, improving readability of the code.

Ok, the simplicity of the final code won me over.

Thanks for the patches, pushed.
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre