From 4e2ecdb92083e862a723e73c240879aec64ca070 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Fri, 17 Jun 2022 17:02:44 +0800 Subject: [PATCH] Fix env overflow Fix #5989 --- native/jni/su/su_daemon.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/native/jni/su/su_daemon.cpp b/native/jni/su/su_daemon.cpp index 0aa186986..87e31fcc4 100644 --- a/native/jni/su/su_daemon.cpp +++ b/native/jni/su/su_daemon.cpp @@ -415,14 +415,13 @@ void su_daemon_handler(int client, const sock_cred *cred) { if (realpath(path, cwd)) chdir(cwd); snprintf(path, sizeof(path), "/proc/%d/environ", ctx.pid); - char buf[4096] = { 0 }; - int fd = xopen(path, O_RDONLY); - read(fd, buf, sizeof(buf)); - close(fd); + auto env = full_read(path); clearenv(); - for (size_t pos = 0; buf[pos];) { - putenv(buf + pos); - pos += strlen(buf + pos) + 1; + for (size_t pos = 0; pos < env.size(); ++pos) { + putenv(env.data() + pos); + pos = env.find_first_of('\0', pos); + if (pos == std::string::npos) + break; } if (!ctx.req.keepenv) { struct passwd *pw;