fix: fd leak and out-of-bounds access in exec_command

This commit fixes both fd leak and out-of-bounds access in the "exec_command" function, which can happen when execution of the command fails, leading to crashes on Magisk-rooted devices, and possibly APatch-rooted devices too.
This commit is contained in:
ThePedroo
2024-11-08 17:23:33 -03:00
parent 8892eca4a7
commit 47d46e305c

View File

@@ -347,6 +347,9 @@ bool exec_command(char *restrict buf, size_t len, const char *restrict file, cha
if ((pid = fork()) == -1) {
LOGE("fork: %s\n", strerror(errno));
close(link[0]);
close(link[1]);
return false;
}
@@ -356,13 +359,20 @@ bool exec_command(char *restrict buf, size_t len, const char *restrict file, cha
close(link[1]);
execv(file, argv);
LOGE("execv failed: %s\n", strerror(errno));
_exit(1);
} else {
close(link[1]);
int nbytes = read(link[0], buf, len);
buf[nbytes - 1] = '\0';
if (nbytes > 0) buf[nbytes - 1] = '\0';
/* INFO: If something went wrong, at least we must ensure it is NULL-terminated */
else buf[0] = '\0';
wait(NULL);
close(link[0]);
}
return true;