Skip to content

Commit 2a67b02

Browse files
committed
Match maximum command line size for startup tracing.
Fix too small buffer for the startup property not accounting for the prefix. Bug: 131893397 This is a cherry-pick of f6d221eeed66c975fb05589a171fe4a05c0d35f7. Change-Id: Iad2398bc09c61b1144430bcbef58e5a832b85b0a Merged-In: Iad2398bc09c61b1144430bcbef58e5a832b85b0a
1 parent 2a24f6b commit 2a67b02

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

libc/bionic/malloc_heapprofd.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,23 @@ static void MaybeInstallInitHeapprofdHook(int) {
143143
}
144144
}
145145

146+
constexpr char kHeapprofdProgramPropertyPrefix[] = "heapprofd.enable.";
147+
constexpr size_t kHeapprofdProgramPropertyPrefixSize = sizeof(kHeapprofdProgramPropertyPrefix) - 1;
148+
constexpr size_t kMaxCmdlineSize = 512;
149+
146150
static bool GetHeapprofdProgramProperty(char* data, size_t size) {
147-
constexpr char prefix[] = "heapprofd.enable.";
148-
// - 1 to skip nullbyte, which we will write later.
149-
constexpr size_t prefix_size = sizeof(prefix) - 1;
150-
if (size < prefix_size) {
151+
if (size < kHeapprofdProgramPropertyPrefixSize) {
151152
error_log("%s: Overflow constructing heapprofd property", getprogname());
152153
return false;
153154
}
154-
memcpy(data, prefix, prefix_size);
155+
memcpy(data, kHeapprofdProgramPropertyPrefix, kHeapprofdProgramPropertyPrefixSize);
155156

156157
int fd = open("/proc/self/cmdline", O_RDONLY | O_CLOEXEC);
157158
if (fd == -1) {
158159
error_log("%s: Failed to open /proc/self/cmdline", getprogname());
159160
return false;
160161
}
161-
char cmdline[128];
162+
char cmdline[kMaxCmdlineSize];
162163
ssize_t rd = read(fd, cmdline, sizeof(cmdline) - 1);
163164
close(fd);
164165
if (rd == -1) {
@@ -167,7 +168,7 @@ static bool GetHeapprofdProgramProperty(char* data, size_t size) {
167168
}
168169
cmdline[rd] = '\0';
169170
char* first_arg = static_cast<char*>(memchr(cmdline, '\0', rd));
170-
if (first_arg == nullptr || first_arg == cmdline + size - 1) {
171+
if (first_arg == nullptr) {
171172
error_log("%s: Overflow reading cmdline", getprogname());
172173
return false;
173174
}
@@ -192,12 +193,12 @@ static bool GetHeapprofdProgramProperty(char* data, size_t size) {
192193
}
193194

194195
size_t name_size = static_cast<size_t>(first_arg - start);
195-
if (name_size >= size - prefix_size) {
196+
if (name_size >= size - kHeapprofdProgramPropertyPrefixSize) {
196197
error_log("%s: overflow constructing heapprofd property.", getprogname());
197198
return false;
198199
}
199200
// + 1 to also copy the trailing null byte.
200-
memcpy(data + prefix_size, start, name_size + 1);
201+
memcpy(data + kHeapprofdProgramPropertyPrefixSize, start, name_size + 1);
201202
return true;
202203
}
203204

@@ -213,7 +214,7 @@ bool HeapprofdShouldLoad() {
213214
return true;
214215
}
215216

216-
char program_property[128];
217+
char program_property[kHeapprofdProgramPropertyPrefixSize + kMaxCmdlineSize];
217218
if (!GetHeapprofdProgramProperty(program_property,
218219
sizeof(program_property))) {
219220
return false;

0 commit comments

Comments
 (0)