[Lldb-commits] [lldb] [lldb][Linux] Parse, but don't store "comm" from /proc/stat file (PR #100387)

David Spickett via lldb-commits lldb-commits at lists.llvm.org
Wed Jul 24 07:26:41 PDT 2024


https://github.com/DavidSpickett created https://github.com/llvm/llvm-project/pull/100387

As reported in https://github.com/llvm/llvm-project/issues/89710, the %s code used for `comm` could and probably does, overflow the buffer. Likely we haven't seen it cause problems because the following data is overwritten right afterwards.

Also scanf isn't a great choice here as this `comm` can include many characters that might trip up %s.

We don't actually use `comm`, so parse but don't store it so we're not overflowing anything.

>From 0e65e0e28cc4a20fbe91d3a7a76b7a0ed671e0cc Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Wed, 24 Jul 2024 14:02:47 +0000
Subject: [PATCH] [lldb][Linux] Parse, but don't store "comm" from /proc/stat
 file

As reported in https://github.com/llvm/llvm-project/issues/89710, the %s
code used for `comm` could and probably does, overflow the buffer.
Likely we haven't seen it cause problems because the following data
is overwritten right afterwards.

Also scanf isn't a great choice here as this `comm` can include many characters
that might trip up %s.

We don't actually use `comm`, so parse but don't store it so we're
not overflowing anything.
---
 lldb/source/Host/linux/Host.cpp | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/lldb/source/Host/linux/Host.cpp b/lldb/source/Host/linux/Host.cpp
index 5545f9ef4d70e..0bc736d90ea76 100644
--- a/lldb/source/Host/linux/Host.cpp
+++ b/lldb/source/Host/linux/Host.cpp
@@ -51,11 +51,9 @@ enum class ProcessState {
   Zombie,
 };
 
-constexpr int task_comm_len = 16;
-
 struct StatFields {
   ::pid_t pid = LLDB_INVALID_PROCESS_ID;
-  char comm[task_comm_len];
+  // comm
   char state;
   ::pid_t ppid = LLDB_INVALID_PROCESS_ID;
   ::pid_t pgrp = LLDB_INVALID_PROCESS_ID;
@@ -100,8 +98,8 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo,
   StatFields stat_fields;
   if (sscanf(
           Rest.data(),
-          "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld",
-          &stat_fields.pid, stat_fields.comm, &stat_fields.state,
+          "%d %*s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld",
+          &stat_fields.pid, /* comm, */ &stat_fields.state,
           &stat_fields.ppid, &stat_fields.pgrp, &stat_fields.session,
           &stat_fields.tty_nr, &stat_fields.tpgid, &stat_fields.flags,
           &stat_fields.minflt, &stat_fields.cminflt, &stat_fields.majflt,



More information about the lldb-commits mailing list