Skip to contentSkip to conversation

tools/intel_gpu_frequency: prevent SEGFAULT w/o root

PATCH In review Kostiantyn Naddur 2 participants 3 messages Nov 28, 2025 – Dec 1, 2025

lore
Share & tools
lore
#0 lore raw

cover tools/intel_gpu_frequency: prevent SEGFAULT w/o root

Dear Maintainers,

This patch addresses a core dump in intel_gpu_frequency that occurs when the tool is executed without root privileges and the write modes for frequency files are not correctly specified.

While debugging, printf("%s %s\n", freq_info->path, freq_info->mode) in initialize_freq_info() made it clear that the write mode logic was allowing invalid access patterns. To fix the issue without breaking existing read-only usage, this patch introduces verify_write_cond(). The helper checks both the program’s privilege level and the requested write mode, updating the "min" and "max" modes to "rb+" when needed. This prevents crashes while preserving expected behavior for read-only operations.

The issue is tracked here:

Feedback is welcome.

Thanks,
Kostiantyn Naddur

Kostiantyn Naddur (1):
tools/intel_gpu_frequency: prevent failure w/o root by properly
specifying the write mode

 tools/intel_gpu_frequency.c | 38 +++++++++++++++++++++++++++++--------
 1 file changed, 30 insertions(+), 8 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH 1/1 tools/intel_gpu_frequency: prevent failure w/o root by properly specifying the write mode

Running intel_gpu_frequency without properly specified write modes leads to core dump.

printf("%s %s\n", freq_info->path, freq_info->mode) was used in initialize_freq_info() to understand what was going on.

To fix core dump while keeping the workability of read-only functions, the verify_write_cond() was added, which checks whether the program is running with root and what write mode the functions are using, and then changes the "min" and "max" modes to "rb+".

---
 tools/intel_gpu_frequency.c | 38 +++++++++++++++++++++++++++++--------
 1 file changed, 30 insertions(+), 8 deletions(-)
tools/intel_gpu_frequency.c +30 -8
index 8c3c91313..8f5719265 100644
@@ -24,6 +24,7 @@
 #include <assert.h>
 #include <getopt.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <time.h>
 #include <unistd.h>
 
@@ -52,10 +53,10 @@ struct freq_info {
 };
 
 static struct freq_info info[] = {
-	{ "cur", "r"  },
-	{ "min", "rb+" },
+	{ "cur", "r" },
+	{ "min", "r" },
 	{ "RP1", "r" },
-	{ "max", "rb+" },
+	{ "max", "r" },
 	{ "RP0", "r" },
 	{ "RPn", "r" }
 };
@@ -117,6 +118,21 @@ static int get_frequency(struct freq_info *freq_info)
 	return val;
 }
 
+static void verify_write_cond(bool write)
+{
+	if (getuid() != 0) {
+		fprintf(stderr, "Failed to initialize! (Permission denied)\n");
+		exit(EXIT_FAILURE);
+	}
+
+	if (write == true) {
+		if (strcmp(info[1].mode, "r") == 0)
+			info[1].mode = "rb+";
+		if (strcmp(info[3].mode, "r") == 0)
+			info[3].mode = "rb+";
+	}
+}
+
 static void __attribute__((noreturn))
 usage(const char *prog, int err)
 {
@@ -225,6 +241,7 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			}
 
 			write = true;
+			verify_write_cond(write);
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
 			sscanf(optarg, "%d", &new_freq[MAX]);
@@ -240,6 +257,7 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			}
 
 			write = true;
+			verify_write_cond(write);
 
 			if (!strncmp("min=", optarg, 4)) {
 				act_upon[MIN] = true;
@@ -259,35 +277,39 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 					"FIXME: Warning efficient frequency information is incorrect.\n");
 				exit(EXIT_FAILURE);
 			}
+			write = true;
+			verify_write_cond(write);
 			tmp = get_frequency(&info[EFF]);
 			new_freq[MIN] = tmp;
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
 			break;
 		case 'i': /* mIn */
+			write = true;
+			verify_write_cond(write);
 			tmp = get_frequency(&info[RPn]);
 			new_freq[MIN] = tmp;
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
 			break;
 		case 'm': /* max */
+			write = true;
+			verify_write_cond(write);
 			tmp = get_frequency(&info[RP0]);
 			new_freq[MIN] = tmp;
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
 			break;
 		case 'd': /* defaults */
+			write = true;
+			verify_write_cond(write);
 			new_freq[MIN] = get_frequency(&info[RPn]);
 			new_freq[MAX] = get_frequency(&info[RP0]);
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
 			break;
 		default:
 			fprintf(stderr, "Error: unknown option\n");
@@ -301,7 +323,6 @@ done:
 
 int main(int argc, char *argv[])
 {
-
 	bool write, fail, targets[MAX+1] = {false};
 	int i, fd, try = 1, set_freq[MAX+1] = {0};
 
@@ -334,6 +355,7 @@ again:
 			continue;
 
 		if (write) {
+			verify_write_cond(write);
 			set_frequency(&info[i], set_freq[i]);
 			if (get_frequency(&info[i]) != set_freq[i])
 				fail = true;
Signature · 1 line
2.52.0
#2 lore raw

Re: [PATH i-g-t v1 1/1] tools/intel_gpu_frequency: prevent failure w/o root by properly specifying the write mode

Hi Kostiantyn,

Quoted 5 lines · Kostiantyn Naddur

Running intel_gpu_frequency without properly specified write modes leads to core dump.

printf("%s %s\n", freq_info->path, freq_info->mode) was used in initialize_freq_info() to understand what was going on.

This should be below after '---' line, it should not be a part of description of commit. Or you could add it cover letter about how you debugged it.

Quoted 5 lines

To fix core dump while keeping the workability of read-only functions, the verify_write_cond() was added, which checks whether the program is running with root and what write mode the functions are using, and then changes the "min" and "max" modes to "rb+".

Description should try to write why and how, but 'how' part should not be C code in english. For pointers look into links in CONTRIBUTING.md, especially 'Sending patches'
https://drm.pages.freedesktop.org/igt-gpu-tools/CONTRIBUTING/#sending-patches

Quoted 2 lines

We usally use 'Closes:' tag, fixes for a commits.

Quoted 33 lines
---
 tools/intel_gpu_frequency.c | 38 +++++++++++++++++++++++++++++--------
 1 file changed, 30 insertions(+), 8 deletions(-)
tools/intel_gpu_frequency.c +4 -3
index 8c3c91313..8f5719265 100644
@@ -24,6 +24,7 @@
 #include <assert.h>
 #include <getopt.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <time.h>
 #include <unistd.h>
 
@@ -52,10 +53,10 @@ struct freq_info {
 };
 
 static struct freq_info info[] = {
-	{ "cur", "r"  },
-	{ "min", "rb+" },
+	{ "cur", "r" },
+	{ "min", "r" },
 	{ "RP1", "r" },
-	{ "max", "rb+" },
+	{ "max", "r" },
 	{ "RP0", "r" },
 	{ "RPn", "r" }
 };

tools/intel_gpu_frequency.c static int get_frequency(struct freq_info *freq_info) · line 126

Quoted 9 lines
tools/intel_gpu_frequency.c +6 -0
@@ -117,6 +118,21 @@ static int get_frequency(struct freq_info *freq_info)
 	return val;
 }
 
+static void verify_write_cond(bool write)
+{
+	if (getuid() != 0) {
+		fprintf(stderr, "Failed to initialize! (Permission denied)\n");
+		exit(EXIT_FAILURE);
+	}

The problem with this check is that a non-root user could still access those files when he/she is in a group. So imho better solution was to open and check for error. There is already a proposed solution to this issue, see my series

https://patchwork.freedesktop.org/series/157834/
intel_gpu_frequency: Allow printing frequencies by regular user

especially patch 3/3,
[i-g-t,v3,3/3] tools/intel_gpu_frequency: Allow printing by regular user

Regards,
Kamil

PS. what is still left todo is to replace all asserts with proper error reporting.

Quoted 91 lines
tools/intel_gpu_frequency.c +20 -5
+
+	if (write == true) {
+		if (strcmp(info[1].mode, "r") == 0)
+			info[1].mode = "rb+";
+		if (strcmp(info[3].mode, "r") == 0)
+			info[3].mode = "rb+";
+	}
+}
+
 static void __attribute__((noreturn))
 usage(const char *prog, int err)
 {
@@ -225,6 +241,7 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			}
 
 			write = true;
+			verify_write_cond(write);
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
 			sscanf(optarg, "%d", &new_freq[MAX]);
@@ -240,6 +257,7 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			}
 
 			write = true;
+			verify_write_cond(write);
 
 			if (!strncmp("min=", optarg, 4)) {
 				act_upon[MIN] = true;
@@ -259,35 +277,39 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 					"FIXME: Warning efficient frequency information is incorrect.\n");
 				exit(EXIT_FAILURE);
 			}
+			write = true;
+			verify_write_cond(write);
 			tmp = get_frequency(&info[EFF]);
 			new_freq[MIN] = tmp;
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
 			break;
 		case 'i': /* mIn */
+			write = true;
+			verify_write_cond(write);
 			tmp = get_frequency(&info[RPn]);
 			new_freq[MIN] = tmp;
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
 			break;
 		case 'm': /* max */
+			write = true;
+			verify_write_cond(write);
 			tmp = get_frequency(&info[RP0]);
 			new_freq[MIN] = tmp;
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
 			break;
 		case 'd': /* defaults */
+			write = true;
+			verify_write_cond(write);
 			new_freq[MIN] = get_frequency(&info[RPn]);
 			new_freq[MAX] = get_frequency(&info[RP0]);
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
 			break;
 		default:
 			fprintf(stderr, "Error: unknown option\n");
@@ -301,7 +323,6 @@ done:
 
 int main(int argc, char *argv[])
 {
-
 	bool write, fail, targets[MAX+1] = {false};
 	int i, fd, try = 1, set_freq[MAX+1] = {0};
 
@@ -334,6 +355,7 @@ again:
 			continue;
 
 		if (write) {
+			verify_write_cond(write);
 			set_frequency(&info[i], set_freq[i]);
 			if (get_frequency(&info[i]) != set_freq[i])
 				fail = true;
Signature · 1 line
2.52.0