Read auxv from proc rather than the stack

This commit is contained in:
Asher Baker 2019-01-12 01:33:10 +00:00
parent 3582376f35
commit 2d4ffc703b
2 changed files with 62 additions and 44 deletions

View File

@ -490,29 +490,38 @@ class UploadThread: public IThread
std::string vdsoOutputPath = ""; std::string vdsoOutputPath = "";
if (debugFile == "linux-gate.so") { if (debugFile == "linux-gate.so") {
FILE *auxvFile = fopen("/proc/self/auxv", "rb");
if (auxvFile) {
char vdsoOutputPathBuffer[512]; char vdsoOutputPathBuffer[512];
g_pSM->BuildPath(Path_SM, vdsoOutputPathBuffer, sizeof(vdsoOutputPathBuffer), "data/dumps/linux-gate.so"); g_pSM->BuildPath(Path_SM, vdsoOutputPathBuffer, sizeof(vdsoOutputPathBuffer), "data/dumps/linux-gate.so");
vdsoOutputPath = vdsoOutputPathBuffer; vdsoOutputPath = vdsoOutputPathBuffer;
int auxvStart = 0;
while (environ[auxvStart++] != nullptr); while (!feof(auxvFile)) {
struct { int auxvEntryId = 0;
int id; fread(&auxvEntryId, sizeof(auxvEntryId), 1, auxvFile);
void *value; long auxvEntryValue = 0;
} *auxvEntry = (decltype(auxvEntry))&environ[auxvStart]; fread(&auxvEntryValue, sizeof(auxvEntryValue), 1, auxvFile);
for (int auxvIndex = 0; true; ++auxvIndex) {
if (auxvEntry[auxvIndex].id == 0) break; if (auxvEntryId == 0) break;
if (auxvEntry[auxvIndex].id != 33) continue; // AT_SYSINFO_EHDR if (auxvEntryId != 33) continue; // AT_SYSINFO_EHDR
Elf32_Ehdr *vdsoHdr = (Elf32_Ehdr *)auxvEntry[auxvIndex].value;
Elf32_Ehdr *vdsoHdr = (Elf32_Ehdr *)auxvEntryValue;
auto vdsoSize = vdsoHdr->e_shoff + (vdsoHdr->e_shentsize * vdsoHdr->e_shnum); auto vdsoSize = vdsoHdr->e_shoff + (vdsoHdr->e_shentsize * vdsoHdr->e_shnum);
void *vdsoBuffer = malloc(vdsoSize); void *vdsoBuffer = malloc(vdsoSize);
memcpy(vdsoBuffer, vdsoHdr, vdsoSize); memcpy(vdsoBuffer, vdsoHdr, vdsoSize);
FILE *vdsoFile = fopen(vdsoOutputPath.c_str(), "wb"); FILE *vdsoFile = fopen(vdsoOutputPath.c_str(), "wb");
if (vdsoFile) { if (vdsoFile) {
fwrite(vdsoBuffer, 1, vdsoSize, vdsoFile); fwrite(vdsoBuffer, 1, vdsoSize, vdsoFile);
fclose(vdsoFile); fclose(vdsoFile);
debugFile = vdsoOutputPath; debugFile = vdsoOutputPath;
} }
free(vdsoBuffer); free(vdsoBuffer);
break;
}
fclose(auxvFile);
} }
} }

View File

@ -240,30 +240,39 @@ int main(int argc, char *argv[])
std::string vdsoOutputPath = ""; std::string vdsoOutputPath = "";
if (debugFile == "linux-gate.so") { if (debugFile == "linux-gate.so") {
FILE *auxvFile = fopen("/proc/self/auxv", "rb");
if (auxvFile) {
auto workingDir = getcwd(nullptr, 0); auto workingDir = getcwd(nullptr, 0);
vdsoOutputPath = workingDir + std::string("/linux-gate.so"); vdsoOutputPath = workingDir + std::string("/linux-gate.so");
int auxvStart = 0; free(workingDir);
while (environ[auxvStart++] != nullptr);
struct { while (!feof(auxvFile)) {
int id; int auxvEntryId = 0;
void *value; fread(&auxvEntryId, sizeof(auxvEntryId), 1, auxvFile);
} *auxvEntry = (decltype(auxvEntry))&environ[auxvStart]; long auxvEntryValue = 0;
for (int auxvIndex = 0; true; ++auxvIndex) { fread(&auxvEntryValue, sizeof(auxvEntryValue), 1, auxvFile);
if (auxvEntry[auxvIndex].id == 0) break;
if (auxvEntry[auxvIndex].id != 33) continue; // AT_SYSINFO_EHDR if (auxvEntryId == 0) break;
Elf32_Ehdr *vdsoHdr = (Elf32_Ehdr *)auxvEntry[auxvIndex].value; if (auxvEntryId != 33) continue; // AT_SYSINFO_EHDR
Elf32_Ehdr *vdsoHdr = (Elf32_Ehdr *)auxvEntryValue;
auto vdsoSize = vdsoHdr->e_shoff + (vdsoHdr->e_shentsize * vdsoHdr->e_shnum); auto vdsoSize = vdsoHdr->e_shoff + (vdsoHdr->e_shentsize * vdsoHdr->e_shnum);
void *vdsoBuffer = malloc(vdsoSize); void *vdsoBuffer = malloc(vdsoSize);
memcpy(vdsoBuffer, vdsoHdr, vdsoSize); memcpy(vdsoBuffer, vdsoHdr, vdsoSize);
FILE *vdsoFile = fopen(vdsoOutputPath.c_str(), "wb"); FILE *vdsoFile = fopen(vdsoOutputPath.c_str(), "wb");
if (vdsoFile) { if (vdsoFile) {
fwrite(vdsoBuffer, 1, vdsoSize, vdsoFile); fwrite(vdsoBuffer, 1, vdsoSize, vdsoFile);
fclose(vdsoFile); fclose(vdsoFile);
debugFile = vdsoOutputPath; debugFile = vdsoOutputPath;
} }
free(vdsoBuffer); free(vdsoBuffer);
break;
}
fclose(auxvFile);
} }
free(workingDir);
} }
if (debugFile[0] != '/') { if (debugFile[0] != '/') {