Squashed commit of the following:

commit 3007df0d153ade6d328321dad8f88d63869159c8
Merge: 7557851 cd14d68
Author: Carl Pearson <pearson@illinois.edu>
Date:   Wed Sep 25 09:59:54 2019 -0500

    Merge branch 'master' into feature/aslr

commit 7557851508f0e1c2d75244267c5f78fb3d1ca303
Author: Carl Pearson <pearson@illinois.edu>
Date:   Tue Sep 24 07:50:15 2019 -0500

    wish list

commit 0bbbac31f354c16ae1942dbfe4683a66ec260498
Author: Carl Pearson <pearson@illinois.edu>
Date:   Tue Sep 24 07:49:36 2019 -0500

    ASLR documentation

commit f1ae37e057792696a739e30ecdbd09e071b8a7d4
Author: Carl Pearson <pearson@illinois.edu>
Date:   Tue Sep 24 07:45:54 2019 -0500

    add ASLR interface
This commit is contained in:
Carl Pearson
2019-09-25 10:03:32 -05:00
parent cd14d68c47
commit 3a86aef546
5 changed files with 157 additions and 2 deletions

View File

@@ -16,6 +16,7 @@ CPU/GPU Performance control library for benchmarking on Linux, x86, POWER, and N
- [x] Flush addresses from cache (amd64, POWER)
- [x] CUDA not required (GPU functions will not be compiled)
- [x] Flush file system caches (linux)
- [x] Disable ASLR (linux)
## Contributors
* [Carl Pearson](https://cwpearson.github.io)
@@ -85,6 +86,20 @@ See [examples/gpu_monitor.cu](examples/gpu_monitor.cu)
* `void Monitor::pause()`: pause the monitor thread
* `void Monitor::resume()`: resume the monitor thread
### Disable ASLR
`perfect` can disable ASLR
See [tools/no_aslr.cpp](tools/no_aslr.cpp)
```c++
#include "perfect/aslr.hpp"
```
* `Result disable_aslr()`: disable ASLR
* `Result get_aslr(AslrState &state)`: save the current ASLR state
* `Result set_aslr(const AslrState &state)`: set a previously-saved ASLR state
### Flush file system caches
`perfect` can drop various filesystem caches
@@ -192,7 +207,6 @@ See [examples/cpu_cache.cpp](examples/cpu_cache.cpp).
- [ ] only monitor certain GPUs
- [ ] hyperthreading interface
- [ ] ASLR interface
- [ ] process priority interface
- [ ] A wrapper utility
- [ ] disable hyperthreading

40
include/perfect/aslr.hpp Normal file
View File

@@ -0,0 +1,40 @@
#pragma once
#include <cerrno>
#include <iostream>
#ifdef __linux__
#include "detail/os/linux.hpp"
#endif
#include "init.hpp"
#include "result.hpp"
namespace perfect {
struct AslrState {
#ifdef __linux__
unsigned long persona;
#else
#error "unsupported platform"
#endif
};
Result get_aslr(AslrState &state) {
int persona;
PERFECT_SUCCESS_OR_RETURN(detail::get_personality(persona));
state.persona = persona;
return Result::SUCCESS;
}
Result set_aslr(const AslrState &state) {
return detail::set_personality(state.persona);
}
Result disable_aslr() {
int persona;
PERFECT_SUCCESS_OR_RETURN(detail::get_personality(persona));
persona |= ADDR_NO_RANDOMIZE;
return detail::set_personality(persona);
}
} // namespace perfect

View File

@@ -12,6 +12,7 @@
#include <sched.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/personality.h>
#include "perfect/result.hpp"
@@ -88,4 +89,24 @@ size_t cache_linesize() {
#endif
}
namespace detail {
Result get_personality(int &persona) {
int ret = personality(0xffffffff);
if (-1 == ret) {
return Result::UNKNOWN;
} else {
persona = ret;
}
return Result::SUCCESS;
}
Result set_personality(const int persona) {
int ret = personality(persona);
if (-1 == ret) {
return Result::UNKNOWN;
}
return Result::SUCCESS;
}
}
} // namespace perfect

View File

@@ -39,3 +39,6 @@ target_link_libraries(enable-turbo perfect)
add_executable(sync-drop-caches sync_drop_caches.cpp)
target_link_libraries(sync-drop-caches perfect)
add_executable(no-aslr no_aslr.cpp)
target_link_libraries(no-aslr perfect)

77
tools/no_aslr.cpp Normal file
View File

@@ -0,0 +1,77 @@
#include <iostream>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cstring>
#include <vector>
#include "perfect/aslr.hpp"
using namespace perfect;
int main(int argc, char **argv) {
using namespace perfect;
PERFECT(init());
pid_t pid;
int status;
pid = fork();
if (pid == -1) {
// pid == -1 means error occured
std::cerr << "can't fork, error occured\n";
exit(EXIT_FAILURE);
} else if (pid == 0) {
// in the child process
// skip the first argument, which is this program
std::vector<char*> args;
for (int i = 1; i < argc; ++i) {
args.push_back(argv[i]);
}
assert(args.size() > 0);
args.push_back(nullptr);
PERFECT(disable_aslr());
// the execv() only return if error occured.
// The return value is -1
return execvp(args[0], args.data());
} else {
// parent process
if (waitpid(pid, &status, 0) > 0) {
if (WIFEXITED(status) && !WEXITSTATUS(status)) {
// success
exit(status);
}
else if (WIFEXITED(status) && WEXITSTATUS(status)) {
if (WEXITSTATUS(status) == 127) {
// execv failed
std::cerr << "execv failed\n";
exit(status);
} else {
std::cerr << "program terminated normally, but returned a non-zero status\n";
exit(status);
}
} else {
printf("program didn't terminate normally\n");
exit(status);
}
} else {
// waitpid() failed
printf("waitpid() failed\n");
exit(EXIT_FAILURE);
}
exit(0);
}
return 0;
}