make --with-selinux work with libtcc, too

This commit is contained in:
Henry Kroll III 2010-04-24 03:28:54 -07:00
parent a4ed587f61
commit 2ab42855cb
3 changed files with 35 additions and 34 deletions

View File

@ -1038,7 +1038,12 @@ LIBTCCAPI void tcc_delete(TCCState *s1)
dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths); dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
tcc_free(s1->tcc_lib_path); tcc_free(s1->tcc_lib_path);
#ifdef HAVE_SELINUX
munmap (s1->write_mem, s1->mem_size);
munmap (s1->runtime_mem, s1->mem_size);
#else
tcc_free(s1->runtime_mem); tcc_free(s1->runtime_mem);
#endif
tcc_free(s1); tcc_free(s1);
} }

4
tcc.h
View File

@ -521,6 +521,10 @@ struct TCCState {
/* for tcc_relocate */ /* for tcc_relocate */
int runtime_added; int runtime_added;
void *runtime_mem; void *runtime_mem;
#ifdef HAVE_SELINUX
void *write_mem;
unsigned long mem_size;
#endif
struct InlineFunc **inline_fns; struct InlineFunc **inline_fns;
int nb_inline_fns; int nb_inline_fns;

View File

@ -37,12 +37,33 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr);
int tcc_relocate(TCCState *s1) int tcc_relocate(TCCState *s1)
{ {
int ret; int ret;
#ifdef HAVE_SELINUX
char tmpfname[] = "/tmp/.tccrunXXXXXX";
int fd = mkstemp (tmpfname);
unlink (tmpfname); ftruncate (fd, 1000);
if ((ret= tcc_relocate_ex(s1,NULL)) < 0)return -1;
s1->mem_size=ret;
/* Use mmap instead of malloc for Selinux */
s1->write_mem = mmap (NULL, ret, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0);
if(s1->write_mem == MAP_FAILED){
error("/tmp not writeable");
return -1;
}
s1->runtime_mem = mmap (NULL, ret, PROT_READ|PROT_EXEC,
MAP_SHARED, fd, 0);
if(s1->runtime_mem == MAP_FAILED){
error("/tmp not executable");
return -1;
}
ret = tcc_relocate_ex(s1, s1->write_mem);
#else
ret = tcc_relocate_ex(s1, NULL); ret = tcc_relocate_ex(s1, NULL);
if (-1 != ret) { if (-1 != ret) {
s1->runtime_mem = tcc_malloc(ret); s1->runtime_mem = tcc_malloc(ret);
ret = tcc_relocate_ex(s1, s1->runtime_mem); ret = tcc_relocate_ex(s1, s1->runtime_mem);
} }
#endif
return ret; return ret;
} }
@ -50,34 +71,10 @@ int tcc_relocate(TCCState *s1)
int tcc_run(TCCState *s1, int argc, char **argv) int tcc_run(TCCState *s1, int argc, char **argv)
{ {
int (*prog_main)(int, char **); int (*prog_main)(int, char **);
int ret;
#ifdef HAVE_SELINUX
int rret;
void *ptr,*writep;
char tmpfname[] = "/tmp/.tccrunXXXXXX";
int fd = mkstemp (tmpfname);
unlink (tmpfname);
ftruncate (fd, 1000);
if ((rret= tcc_relocate_ex(s1,NULL)) < 0)
return -1;
/* Use mmap instead of malloc for Selinux */
writep = mmap (NULL, rret, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0);
if(writep == MAP_FAILED){
error("/tmp not writeable");
return -1;
}
ptr = mmap (NULL, rret, PROT_READ|PROT_EXEC,
MAP_SHARED, fd, 0);
if(ptr == MAP_FAILED){
error("/tmp not executable");
return -1;
}
tcc_relocate_ex(s1, writep);
#else
if (tcc_relocate(s1) < 0) if (tcc_relocate(s1) < 0)
return -1; return -1;
#endif
prog_main = tcc_get_symbol_err(s1, "main"); prog_main = tcc_get_symbol_err(s1, "main");
#ifdef CONFIG_TCC_BACKTRACE #ifdef CONFIG_TCC_BACKTRACE
@ -89,6 +86,7 @@ int tcc_run(TCCState *s1, int argc, char **argv)
if (s1->do_bounds_check) { if (s1->do_bounds_check) {
void (*bound_init)(void); void (*bound_init)(void);
void (*bound_exit)(void); void (*bound_exit)(void);
int ret;
/* set error function */ /* set error function */
rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg"); rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg");
rt_prog_main = prog_main; rt_prog_main = prog_main;
@ -108,13 +106,7 @@ int tcc_run(TCCState *s1, int argc, char **argv)
if (p) *p = 0; if (p) *p = 0;
} }
#endif #endif
ret=(*prog_main)(argc, argv); return (*prog_main)(argc, argv);
#ifdef HAVE_SELINUX
munmap (writep, rret);
munmap (ptr, rret);
#endif
return ret;
} }