Skip to contentSkip to conversation

rt-tests: deadline: Fix segmentation faults

PATCH In review Kurt Kanzenbach 3 participants 12 messages Apr 4, 2019 – Apr 5, 2019

lore
Share & tools
lore
#0 lore raw

cover rt-tests: deadline: Fix segmentation faults

Hi,

the deadline test binaries may crash under certain circumstances.

That seems to be caused by stack buffer overflows. While here, I've noticed that some memory wasn't free'd. So, added that as well.

Thanks,
Kurt

Kurt Kanzenbach (4):
rt-tests: cyclicdeadline: fix segmentation fault on close
rt-tests: cyclicdeadline: add missing free calls
rt-tests: deadline_tests: fix stack buffer flow
rt-tests: deadline_test: add missing frees

 src/sched_deadline/cyclicdeadline.c | 4 +++-
 src/sched_deadline/deadline_test.c  | 7 ++++---
 2 files changed, 7 insertions(+), 4 deletions(-)
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/4 rt-tests: cyclicdeadline: fix segmentation fault on close

The current code generates a segmentation fault in the last free() call.

  $ sudo ./cyclicdeadline
  Using all CPUS
  /sys/kernel/debug/sched_features: Success
  interval: 600:1000
    Tested at 5us of 600us
  deadline thread 2963
  thread[2963] runtime=600us deadline=1000us
  main thread 2962
  fail 2 0
  T: 0 ( 2963) I:1000 C:   1268 Min:      7 Act:   55 Avg:   56 Max:     256
  [1]    2961 segmentation fault  sudo ./cyclicdeadline

This is caused by a buffer overflow in setup_ftrace_marker(). The appended string is 21 not 14 characters wide. Fix it by using strlen() like the other function do.

---
 src/sched_deadline/cyclicdeadline.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/sched_deadline/cyclicdeadline.c +1 -1
index 08460107c464..303b5e96647a 100644
@@ -283,7 +283,7 @@ static void setup_ftrace_marker(void)
 {
 	struct stat st;
 	const char *debugfs = find_debugfs();
-	char files[strlen(debugfs) + 14];
+	char files[strlen(debugfs) + strlen("/tracing/trace_marker") + 1];
 	int ret;
 
 	if (strlen(debugfs) == 0)
Signature · 1 line
2.11.0
#2 lore raw

Re: [PATCH 1/4] rt-tests: cyclicdeadline: fix segmentation fault on close

Quoted 40 lines · Kurt Kanzenbach

The current code generates a segmentation fault in the last free() call.

  $ sudo ./cyclicdeadline
  Using all CPUS
  /sys/kernel/debug/sched_features: Success
  interval: 600:1000
    Tested at 5us of 600us
  deadline thread 2963
  thread[2963] runtime=600us deadline=1000us
  main thread 2962
  fail 2 0
  T: 0 ( 2963) I:1000 C:   1268 Min:      7 Act:   55 Avg:   56 Max:     256
  [1]    2961 segmentation fault  sudo ./cyclicdeadline

This is caused by a buffer overflow in setup_ftrace_marker(). The appended string is 21 not 14 characters wide. Fix it by using strlen() like the other function do.

---
 src/sched_deadline/cyclicdeadline.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/sched_deadline/cyclicdeadline.c +1 -1
index 08460107c464..303b5e96647a 100644
@@ -283,7 +283,7 @@ static void setup_ftrace_marker(void)
 {
 	struct stat st;
 	const char *debugfs = find_debugfs();
-	char files[strlen(debugfs) + 14];
+	char files[strlen(debugfs) + strlen("/tracing/trace_marker") + 1];
 	int ret;
 
 	if (strlen(debugfs) == 0)
Signature · 1 line
2.11.0
#3 lore raw

PATCH 2/4 rt-tests: cyclicdeadline: add missing free calls

In reply to Kurt Kanzenbach

The threads and thread data structures are allocated using calloc(). Free them as well.

---
 src/sched_deadline/cyclicdeadline.c | 2 ++
 1 file changed, 2 insertions(+)
src/sched_deadline/cyclicdeadline.c +2 -0
index 303b5e96647a..f4dd26a2dba2 100644
@@ -1261,6 +1261,8 @@ int main (int argc, char **argv)
 		}
 	}
 
+	free(thread);
+	free(sched_data);
 	free(setcpu_buf);
 	return 0;
 }
Signature · 1 line
2.11.0
#4 lore raw

Re: [PATCH 2/4] rt-tests: cyclicdeadline: add missing free calls

Quoted 25 lines · Kurt Kanzenbach

The threads and thread data structures are allocated using calloc(). Free them as well.

---
 src/sched_deadline/cyclicdeadline.c | 2 ++
 1 file changed, 2 insertions(+)
src/sched_deadline/cyclicdeadline.c +2 -0
index 303b5e96647a..f4dd26a2dba2 100644
@@ -1261,6 +1261,8 @@ int main (int argc, char **argv)
 		}
 	}
 
+	free(thread);
+	free(sched_data);
 	free(setcpu_buf);
 	return 0;
 }
Signature · 1 line
2.11.0
#5 lore raw

PATCH 3/4 rt-tests: deadline_tests: fix stack buffer flow

In reply to Kurt Kanzenbach

The appended string is actually longer than 14 characters. Use strlen() to compute the correct length like the other functions do.

---
 src/sched_deadline/deadline_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/sched_deadline/deadline_test.c +1 -1
index b213c206559c..4826557d0624 100644
@@ -435,7 +435,7 @@ static void setup_ftrace_marker(void)
 {
 	struct stat st;
 	const char *debugfs = find_debugfs();
-	char files[strlen(debugfs) + 14];
+	char files[strlen(debugfs) + strlen("/tracing/trace_marker") + 1];
 	int ret;
 
 	if (strlen(debugfs) == 0)
Signature · 1 line
2.11.0
#6 lore raw

Re: [PATCH 3/4] rt-tests: deadline_tests: fix stack buffer flow

Quoted 25 lines · Kurt Kanzenbach

The appended string is actually longer than 14 characters. Use strlen() to compute the correct length like the other functions do.

---
 src/sched_deadline/deadline_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/sched_deadline/deadline_test.c +1 -1
index b213c206559c..4826557d0624 100644
@@ -435,7 +435,7 @@ static void setup_ftrace_marker(void)
 {
 	struct stat st;
 	const char *debugfs = find_debugfs();
-	char files[strlen(debugfs) + 14];
+	char files[strlen(debugfs) + strlen("/tracing/trace_marker") + 1];
 	int ret;
 
 	if (strlen(debugfs) == 0)
Signature · 1 line
2.11.0
#7 lore raw

PATCH 4/4 rt-tests: deadline_test: add missing frees

In reply to Kurt Kanzenbach

The threads and thread data structures are allocated using calloc(). But never free'd. So, free them.

While here, remove the if statement for setcpu_buf. Free() is NULL safe.

---
 src/sched_deadline/deadline_test.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
src/sched_deadline/deadline_test.c +3 -2
index 4826557d0624..69e6fd18b73b 100644
@@ -2092,8 +2092,9 @@ int main (int argc, char **argv)
 		printf("\n");
 	}
 
-	if (!setcpu_buf)
-		free(setcpu_buf);
+	free(thread);
+	free(sched_data);
+	free(setcpu_buf);
 
 	return 0;
 }
Signature · 1 line
2.11.0
#8 lore raw

Re: [PATCH 4/4] rt-tests: deadline_test: add missing frees

Quoted 28 lines · Kurt Kanzenbach

The threads and thread data structures are allocated using calloc(). But never free'd. So, free them.

While here, remove the if statement for setcpu_buf. Free() is NULL safe.

---
 src/sched_deadline/deadline_test.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
src/sched_deadline/deadline_test.c +3 -2
index 4826557d0624..69e6fd18b73b 100644
@@ -2092,8 +2092,9 @@ int main (int argc, char **argv)
 		printf("\n");
 	}
 
-	if (!setcpu_buf)
-		free(setcpu_buf);
+	free(thread);
+	free(sched_data);
+	free(setcpu_buf);
 
 	return 0;
 }
Signature · 1 line
2.11.0

Does this remove your own if statement that I didn't even apply yet? :)

Maybe, you want to regenerate this patch without that?

Thanks

John

#9 lore raw

Re: [PATCH 4/4] rt-tests: deadline_test: add missing frees

Hi,

Quoted 28 lines · John Kacur
Quoted 24 lines · Kurt Kanzenbach

The threads and thread data structures are allocated using calloc(). But never free'd. So, free them.

While here, remove the if statement for setcpu_buf. Free() is NULL safe.

---
 src/sched_deadline/deadline_test.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
src/sched_deadline/deadline_test.c +3 -2
index 4826557d0624..69e6fd18b73b 100644
@@ -2092,8 +2092,9 @@ int main (int argc, char **argv)
 		printf("\n");
 	}
 
-	if (!setcpu_buf)
-		free(setcpu_buf);
+	free(thread);
+	free(sched_data);
+	free(setcpu_buf);
 
Quoted 7 lines
Quoted 4 lines
src/sched_deadline/deadline_test.c +0 -1
 	return 0;
 }
--

2.11.0

Does this remove your own if statement that I didn't even apply yet? :)

No. That's a different binary. My patch was made against cyclicdeadline.

Thanks,
Kurt

#10 lore raw

Re: [PATCH 4/4] rt-tests: deadline_test: add missing frees

Quoted 39 lines · Kurt Kanzenbach

Hi,

Quoted 28 lines · John Kacur
Quoted 24 lines · Kurt Kanzenbach

The threads and thread data structures are allocated using calloc(). But never free'd. So, free them.

While here, remove the if statement for setcpu_buf. Free() is NULL safe.

---
 src/sched_deadline/deadline_test.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
src/sched_deadline/deadline_test.c +3 -2
index 4826557d0624..69e6fd18b73b 100644
@@ -2092,8 +2092,9 @@ int main (int argc, char **argv)
 		printf("\n");
 	}
 
-	if (!setcpu_buf)
-		free(setcpu_buf);
+	free(thread);
+	free(sched_data);
+	free(setcpu_buf);
 
Quoted 7 lines
Quoted 4 lines
src/sched_deadline/deadline_test.c +0 -1
 	return 0;
 }
--

2.11.0

Does this remove your own if statement that I didn't even apply yet? :)

Quoted 4 lines

No. That's a different binary. My patch was made against cyclicdeadline.

Thanks,
Kurt

Ok, got it, thanks!

John

#11 lore raw

Re: [PATCH 4/4] rt-tests: deadline_test: add missing frees

In reply to Kurt Kanzenbach
Quoted 30 lines · Kurt Kanzenbach

The threads and thread data structures are allocated using calloc(). But never free'd. So, free them.

While here, remove the if statement for setcpu_buf. Free() is NULL safe.

---
 src/sched_deadline/deadline_test.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
src/sched_deadline/deadline_test.c +3 -2
index 4826557d0624..69e6fd18b73b 100644
@@ -2092,8 +2092,9 @@ int main (int argc, char **argv)
 		printf("\n");
 	}
 
-	if (!setcpu_buf)
-		free(setcpu_buf);
+	free(thread);
+	free(sched_data);
+	free(setcpu_buf);
 
 	return 0;
 }
Signature · 1 line
2.11.0