runtime bounds check

master
Avril 5 years ago
parent 76817a13d7
commit 3f568538bf
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -155,12 +155,24 @@ struct DirTree
} }
}; };
constexpr uint64_t max_sz()
{
uint64_t sz =0;
for(int i=0;i<DATA_COUNT;i++)
sz+=DATA_LENGTHS[i];
return sz;
}
int main(int argc,char** argv) int main(int argc,char** argv)
{ {
auto path = fs::temp_directory_path() / get_uuid(); auto path = fs::temp_directory_path() / get_uuid();
vector<string> vecargs; vector<string> vecargs;
auto args = arg_strings(argc,argv, vecargs); auto args = arg_strings(argc,argv, vecargs);
const auto max_size = max_sz();
static_assert(max_size == sizeof(DATA));
if(!verify_hash()) if(!verify_hash())
{ {
cerr << "Error: Bad message hash" << endl; cerr << "Error: Bad message hash" << endl;
@ -172,20 +184,31 @@ int main(int argc,char** argv)
#endif #endif
DirTree tree(path); DirTree tree(path);
uint64_t sz=0;
for(int i=0;i<DATA_COUNT;i++) for(int i=0;i<DATA_COUNT;i++)
{ {
auto data = get_data(i); auto data = get_data(i);
auto data_len = DATA_LENGTHS[i];
if ((sz+=data_len) > sizeof(DATA))
{
#ifndef SILENT
cerr << "Failed: Invalid sizes (" << sz << " is larger than " << sizeof(DATA) << ")" << endl;
#endif
return 1;
}
#ifndef SILENT #ifndef SILENT
cout << " <- " << DATA_NAMES[i] << " (" << DATA_LENGTHS[i] << ")" << flush; cout << " <- " << DATA_NAMES[i] << " (" << data_len << ")" << flush;
#endif #endif
if(!verify_hash(i, data, DATA_LENGTHS[i])) { if(!verify_hash(i, data, data_len)) {
#ifndef SILENT #ifndef SILENT
cout << " FAILED: Invalid hash" << endl; cout << " FAILED: Invalid hash" << endl;
cerr << "Aborting." << endl; cerr << "Aborting." << endl;
#endif #endif
return 1; return 1;
} else { } else {
write_to_file(&tree / DATA_NAMES[i], data, DATA_LENGTHS[i]); write_to_file(&tree / DATA_NAMES[i], data, data_len);
#ifndef SILENT #ifndef SILENT
cout << " OK" << endl; cout << " OK" << endl;
#endif #endif

@ -11,7 +11,7 @@ $SCRIPTPATH/generator -o "$SCRIPTPATH/file.h" "$@" || exit 1
echo "Compiling binary..." echo "Compiling binary..."
g++ -I$SCRIPTPATH/include/ -w --std=c++20 "$SCRIPTPATH/main.cpp" "$SCRIPTPATH/sha256_literal/sha256.cpp" -o sfexec g++ -I$SCRIPTPATH/include/ -w --std=c++17 "$SCRIPTPATH/main.cpp" "$SCRIPTPATH/sha256_literal/sha256.cpp" -o sfexec
rm "$SCRIPTPATH/file.h" rm "$SCRIPTPATH/file.h"

Loading…
Cancel
Save