-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#1096) Detect AppScope library state in the application
- check the state of application if was unscoped/scoped/loaded - provide a dummy mapping for the loaded state Closes #1096
- Loading branch information
1 parent
0e44b30
commit ab8d128
Showing
14 changed files
with
302 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include "libstate.h" | ||
#include <fcntl.h> | ||
#include "scopestdlib.h" | ||
|
||
static void *loadedStrMap = NULL; | ||
#define LOADED_MAP_SIZE (4096) | ||
|
||
/* | ||
* AppScope state describes the process state in context of the AppScope library. | ||
* We recognize two states: | ||
* - Scoped - when all the functions are funchooked | ||
* - AppScope library is loaded - when execve family functions are funchooked | ||
* | ||
* The distinguish between the AppScope states is based on presence of the "loaded mapping". | ||
* | ||
* Example loaded mapping view in /proc/<PID>/maps: | ||
* 7fa2b4734000-7fa2b4735000 rw-s 00000000 103:07 23856559 /tmp/scope_loaded.2142471 (deleted) | ||
*/ | ||
|
||
/* | ||
* libstateLoaded sets the loaded state for the current process by: | ||
* - creating a temporary file `/tmp/scope_loaded.<pid>`. | ||
* - the file is mapped into the process memory as "loaded mapping" | ||
* - the file is removed (unlinked) | ||
*/ | ||
bool | ||
libstateLoaded(pid_t pid) { | ||
char path[PATH_MAX] = {0}; | ||
bool res = FALSE; | ||
/* | ||
* Switching to "loaded" AppScope state: | ||
* We are done if mapping is present. | ||
*/ | ||
if (loadedStrMap) { | ||
return TRUE; | ||
} | ||
|
||
if (scope_snprintf(path, sizeof(path), "/tmp/scope_loaded.%d", pid) < 0) { | ||
return res; | ||
} | ||
|
||
int outFd = scope_open(path, O_RDWR | O_CREAT, 0664); | ||
if (outFd == -1) { | ||
return res; | ||
} | ||
|
||
if (scope_ftruncate(outFd, LOADED_MAP_SIZE) != 0) { | ||
goto close_file; | ||
} | ||
|
||
void* dest = scope_mmap(NULL, LOADED_MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, outFd, 0); | ||
if (dest == MAP_FAILED) { | ||
goto close_file; | ||
} | ||
|
||
loadedStrMap = dest; | ||
|
||
res = TRUE; | ||
|
||
close_file: | ||
scope_close(outFd); | ||
if (scope_unlink(path) != 0 ) { | ||
return FALSE; | ||
} | ||
return res; | ||
} | ||
|
||
/* | ||
* libstateScoped sets the scoped state for the current process by unmap the "loaded mapping" | ||
*/ | ||
bool | ||
libstateScoped(void) { | ||
/* | ||
* Switching to "scoped" AppScope state: | ||
* We remove "loaded mapping" if present. | ||
*/ | ||
if (loadedStrMap != NULL ) { | ||
scope_munmap(loadedStrMap, LOADED_MAP_SIZE); | ||
loadedStrMap = NULL; | ||
} | ||
return TRUE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
#ifndef __LIBSTATE_H__ | ||
#define __LIBSTATE_H__ | ||
|
||
#include "scopetypes.h" | ||
|
||
bool libstateLoaded(pid_t); | ||
bool libstateScoped(void); | ||
|
||
#endif // __LIBSTATE_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.