redmine

Added "is_protected()" function to malloc.c

... ... @@ -156,6 +156,8 @@ filesz:1M\n\
#define DEFAULT_CAPS_INHERIT CI_EMPTY
#define DEFAULT_PIVOT_MODE (PW_OFF)
#define DEVZERO "/dev/zero"
// How long to wait on highloaded locks before fallback to mutexes
// See: doc/devel/thread-splitting/highload-locks/clsync-graph-comma.odc
// But optimal value can be very different on different systems
... ...
... ... @@ -2190,6 +2190,8 @@ int main(int argc, char *argv[]) {
ncpus = sysconf(_SC_NPROCESSORS_ONLN); // Get number of available logical CPUs
memory_init();
{
struct passwd *pwd = getpwnam(DEFAULT_USER);
ctx_p->uid = (pwd != NULL) ? pwd->pw_uid : DEFAULT_UID;
... ...
... ... @@ -22,10 +22,22 @@
#ifdef CAPABILITIES_SUPPORT
# include <unistd.h>
# include <sys/mman.h>
# ifdef SECCOMP_SUPPORT
# include <sys/stat.h>
# include <fcntl.h>
# endif
#endif
#include "malloc.h"
#include "error.h"
#include "configuration.h"
#ifdef CAPABILITIES_SUPPORT
long pagesize;
# ifdef SECCOMP_SUPPORT
int devzero_fd;
# endif
#endif
void *xmalloc(size_t size) {
#ifdef _DEBUG
... ... @@ -82,7 +94,6 @@ void *xrealloc(void *oldptr, size_t size) {
#ifdef CAPABILITIES_SUPPORT
void *malloc_align(size_t size) {
long pagesize = sysconf(_SC_PAGE_SIZE);
size_t total_size;
void *ret;
# ifdef _DEBUG
... ... @@ -92,9 +103,6 @@ void *malloc_align(size_t size) {
size++; // Just in case
# endif
if (pagesize == -1)
critical("(%li): Got error from sysconf(_SC_PAGE_SIZE)");
total_size = size;
# ifdef PARANOID
total_size += pagesize-1;
... ... @@ -141,5 +149,39 @@ char *strdup_protect(const char *src, int prot) {
return dst;
}
# ifdef SECCOMP_SUPPORT
int is_protected(void *addr) {
char *_addr = addr, t;
int is_protected;
t = *_addr;
is_protected = (read(devzero_fd, addr, 1) == -1);
if (!is_protected)
*_addr = t;
return is_protected;
}
# endif
#endif
int memory_init() {
#ifdef CAPABILITIES_SUPPORT
pagesize = sysconf(_SC_PAGE_SIZE);
if (pagesize == -1)
critical("Got error from sysconf(_SC_PAGE_SIZE)");
# ifdef SECCOMP_SUPPORT
devzero_fd = open(DEVZERO, O_RDONLY);
if (devzero_fd == -1)
critical("Got error while open(\""DEVZERO"\", O_RDONLY)");
# endif
#endif
return 0;
}
... ...
... ... @@ -26,5 +26,10 @@ extern void *xrealloc(void *oldptr, size_t size);
extern void *malloc_align(size_t size);
extern void *calloc_align(size_t nmemb, size_t size);
extern char *strdup_protect(const char *src, int prot);
# ifdef SECCOMP_SUPPORT
extern int is_protected(void *addr);
# endif
#endif
extern int memory_init();
... ...