if (endAddr-startAddr != pageSize) { snprintf(debugMsg, sizeof(debugMsg), "[!] FATAL: range must be exactly 1 page large (%ld bytes).", pageSize); debugLog(debugMsg); return(1); }
/* attempt attach, wait for process to stop after being attached (there's a brief period of time where the pid won't exist in the pool) */
snprintf(debugMsg, sizeof(debugMsg), "PTRACE_DETACH call failed; errno is %d; exiting...", errno);
debugLog(debugMsg);
}
exit(1);
}
/* write the PEEKDATA into page[] */
page[offset++] = buf;
}
/* detach; print problem if there is one */
if (ptrace(PTRACE_DETACH, pid, NULL, NULL))
{
snprintf(debugMsg, sizeof(debugMsg), "PTRACE_DETACH call failed; errno is %d", errno);
debugLog(debugMsg);
}
/* hash the page, convert binary hash to hex string (easier to deal with/verify if ASCII armored; this may change later for speed) */
unsigned char hashBin[SHA1HashSize];
char hashString[41];
SHA1 ( (const unsigned char *)&page, // cast page as an unsigned char (binary) and send to SHA1 for hashing
pageSize, // page will be pageSize long
(unsigned char *)&hashBin ); // store it in hashBin
int i; for (i = 0; i < 20; i++) { sprintf(hashString+(i*2), "%02x", hashBin[i]); }
hashString[40] = '\0'; // make sure it's NULL terminated
/* connect to DB; wait up to 5 seconds for lock */
sqlite3 *db;
char *errMsg = 0;
int resultCode = sqlite3_open("/etc/trecc/trecc.sqlite", &db);
sqlite3_busy_timeout(db, 5000);
if (resultCode) {
snprintf(debugMsg, sizeof(debugMsg), "can't open database: %s; exiting...", sqlite3_errmsg(db)); debugLog(debugMsg);
sqlite3_close(db); exit(1);
}
/* remove all measurements matching the PID, imageID, startAddr & endAddr we're measuring (there should only be 1) */
if (!trustedMeasurements) {
snprintf(query, sizeof(query), "DELETE FROM measurements WHERE imageID=%d AND pid=%d AND startAddr='0x%lx' AND endAddr='0x%lx'", imageID, pid, startAddr, endAddr);