Skip to contentSkip to conversation

tracing: Fix infinite loop in tracing_read_pipe on overflowed print_trace_line

PATCH In review Yang Jihong 2 participants 3 messages Nov 29, 2022 – Dec 12, 2022 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 tracing: Fix infinite loop in tracing_read_pipe on overflowed print_trace_line

print_trace_line may overflow seq_file buffer. If the event is not consumed, the while loop keeps peeking this event, causing a infinite loop.

---
 kernel/trace/trace.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
kernel/trace/trace.c +14 -1
index a7fe0e115272..de7c99f4b539 100644
@@ -6787,7 +6787,20 @@ tracing_read_pipe(struct file *filp, char __user *ubuf,
 
 		ret = print_trace_line(iter);
 		if (ret == TRACE_TYPE_PARTIAL_LINE) {
-			/* don't print partial lines */
+			/*
+			 * If one print_trace_line() fills entire trace_seq in one shot,
+			 * trace_seq_to_user() will returns -EBUSY because save_len == 0,
+			 * In this case, we need to consume it, otherwise, loop will peek
+			 * this event next time, resulting in an infinite loop.
+			 */
+			if (save_len == 0) {
+				iter->seq.full = 0;
+				trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n");
+				trace_consume(iter);
+				break;
+			}
+
+			/* In other cases, don't print partial lines */
 			iter->seq.seq.len = save_len;
 			break;
 		}
Signature · 1 line
2.30.GIT
#1 lore raw

Hello,

PING.

Are there any other problems with this patch?

Thanks,
Yang.

Quoted 35 lines · Yang Jihong

print_trace_line may overflow seq_file buffer. If the event is not consumed, the while loop keeps peeking this event, causing a infinite loop.

---
  kernel/trace/trace.c | 15 ++++++++++++++-
  1 file changed, 14 insertions(+), 1 deletion(-)
kernel/trace/trace.c +14 -1
index a7fe0e115272..de7c99f4b539 100644
@@ -6787,7 +6787,20 @@ tracing_read_pipe(struct file *filp, char __user *ubuf,
 
  		ret = print_trace_line(iter);
  		if (ret == TRACE_TYPE_PARTIAL_LINE) {
-			/* don't print partial lines */
+			/*
+			 * If one print_trace_line() fills entire trace_seq in one shot,
+			 * trace_seq_to_user() will returns -EBUSY because save_len == 0,
+			 * In this case, we need to consume it, otherwise, loop will peek
+			 * this event next time, resulting in an infinite loop.
+			 */
+			if (save_len == 0) {
+				iter->seq.full = 0;
+				trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n");
+				trace_consume(iter);
+				break;
+			}
+
+			/* In other cases, don't print partial lines */
  			iter->seq.seq.len = save_len;
  			break;
  		}
maintainer
#2 lore raw
Quoted 5 lines · On Mon, 12 Dec 2022 10:57:08 +0800 Yang Jihong <[email protected]> wrote:

Hello,

PING.

Are there any other problems with this patch?

Thanks for the ping, I'll take a look at it. It somehow got lost in the noise of other patches :-/

-- Steve