Skip to content

Commit

Permalink
gpkg/glibc: corrections
Browse files Browse the repository at this point in the history
ldd fix for x86_64
fix mprotect.c
ignore system call 1008 and mbind
  • Loading branch information
Maxython committed Dec 25, 2023
1 parent 9506b01 commit 31cb34b
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 69 deletions.
8 changes: 0 additions & 8 deletions gpkg/glibc/aarch64-arch-syscall.h.patch
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@
#define __NR_get_mempolicy 236
#define __NR_get_robust_list 100
#define __NR_getcpu 168
@@ -124,7 +119,6 @@
#define __NR_lseek 62
#define __NR_lsetxattr 6
#define __NR_madvise 233
-#define __NR_mbind 235
#define __NR_membarrier 283
#define __NR_memfd_create 279
#define __NR_memfd_secret 447
@@ -168,7 +162,6 @@
#define __NR_personality 92
#define __NR_pidfd_getfd 438
Expand Down
8 changes: 0 additions & 8 deletions gpkg/glibc/arm-arch-syscall.h.patch
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@
#define __NR_getpid 20
#define __NR_getppid 64
#define __NR_getpriority 96
@@ -168,7 +158,6 @@
#define __NR_lstat 107
#define __NR_lstat64 196
#define __NR_madvise 220
-#define __NR_mbind 319
#define __NR_membarrier 389
#define __NR_memfd_create 385
#define __NR_migrate_pages 400
@@ -220,7 +209,6 @@
#define __NR_personality 136
#define __NR_pidfd_getfd 438
Expand Down
6 changes: 4 additions & 2 deletions gpkg/glibc/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ TERMUX_PKG_DESCRIPTION="GNU C Library"
TERMUX_PKG_LICENSE="GPL-3.0, LGPL-3.0"
TERMUX_PKG_MAINTAINER="@termux-pacman"
TERMUX_PKG_VERSION=2.38
TERMUX_PKG_REVISION=10
TERMUX_PKG_REVISION=11
TERMUX_PKG_SRCURL=https://ftp.gnu.org/gnu/libc/glibc-$TERMUX_PKG_VERSION.tar.xz
TERMUX_PKG_SHA256=fb82998998b2b29965467bc1b69d152e9c307d2cf301c9eafb4555b770ef3fd2
TERMUX_PKG_DEPENDS="linux-api-headers-glibc"
Expand All @@ -16,11 +16,13 @@ termux_step_pre_configure() {
termux_error_exit "Compilation is only possible based on glibc"
fi

for i in shmem-android.h mprotect.c; do
for i in shmem-android.h mprotect.c ignore-syscall.h; do
install -Dm644 "${TERMUX_PKG_BUILDER_DIR}/${i}" "${TERMUX_PKG_SRCDIR}/sysdeps/unix/sysv/linux/${i}"
done

rm ${TERMUX_PKG_SRCDIR}/sysdeps/unix/sysv/linux/*/clone3.S
rm ${TERMUX_PKG_SRCDIR}/sysdeps/unix/sysv/linux/*/syscall.S
rm ${TERMUX_PKG_SRCDIR}/sysdeps/unix/sysv/linux/x86_64/configure*

for i in android_passwd_group.h android_passwd_group.c android_system_user_ids.h; do
cp ${TERMUX_PKG_BUILDER_DIR}/${i} ${TERMUX_PKG_SRCDIR}/nss/
Expand Down
14 changes: 14 additions & 0 deletions gpkg/glibc/dl-execstack.c.dis
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--- src/sysdeps/unix/sysv/linux/dl-execstack.c 2023-07-31 20:54:16.000000000 +0300
+++ src/sysdeps/unix/sysv/linux/dl-execstack.c.patch 2023-12-25 10:12:20.634704179 +0300
@@ -35,9 +35,10 @@
/* This gives us the highest/lowest page that needs to be changed. */
uintptr_t page = ((uintptr_t) *stack_endp
& -(intptr_t) GLRO(dl_pagesize));
+ void *addr = mmap((void*)page, GLRO(dl_pagesize), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
int result = 0;

- if (__builtin_expect (__mprotect ((void *) page, GLRO(dl_pagesize),
+ if (__builtin_expect (__mprotect (addr, GLRO(dl_pagesize),
__stack_prot) == 0, 1))
goto return_success;
result = errno;
13 changes: 13 additions & 0 deletions gpkg/glibc/dl-load.c.dis
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--- src/elf/dl-load.c 2023-07-31 20:54:16.000000000 +0300
+++ src/elf/dl-load.c.patch 2023-12-24 17:48:46.238884735 +0300
@@ -90,9 +90,7 @@


int __stack_prot attribute_hidden attribute_relro
-#if _STACK_GROWS_DOWN && defined PROT_GROWSDOWN
- = PROT_GROWSDOWN;
-#elif _STACK_GROWS_UP && defined PROT_GROWSUP
+#if _STACK_GROWS_UP && defined PROT_GROWSUP
= PROT_GROWSUP;
#else
= 0;
21 changes: 21 additions & 0 deletions gpkg/glibc/dl-load.c.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--- src/elf/dl-load.c 2023-07-31 20:54:16.000000000 +0300
+++ src/elf/dl-load.c.patch 2023-12-25 17:28:25.688056890 +0300
@@ -1330,15 +1330,15 @@
errstring = N_("cannot change memory protections");
goto lose_errno;
}
- __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
+ __stack_prot |= PROT_READ|PROT_WRITE;
__mprotect ((void *) p, s, PROT_READ);
}
else
- __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
+ __stack_prot |= PROT_READ|PROT_WRITE;
}
else
#endif
- __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC;
+ __stack_prot |= PROT_READ|PROT_WRITE;

#ifdef check_consistency
check_consistency ();
8 changes: 0 additions & 8 deletions gpkg/glibc/i386-arch-syscall.h.patch
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@
#define __NR_getpid 20
#define __NR_getpmsg 188
#define __NR_getppid 64
@@ -181,7 +172,6 @@
#define __NR_lstat 107
#define __NR_lstat64 196
#define __NR_madvise 219
-#define __NR_mbind 274
#define __NR_membarrier 375
#define __NR_memfd_create 356
#define __NR_memfd_secret 447
@@ -239,7 +229,6 @@
#define __NR_personality 136
#define __NR_pidfd_getfd 438
Expand Down
24 changes: 24 additions & 0 deletions gpkg/glibc/ignore-syscall.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef _IGNORE_SYSCALL
#define _IGNORE_SYSCALL

#include <arch-syscall.h>

struct IdSyscalls {
int id;
};

static struct IdSyscalls ListIgnoreSyscall[] = {
{ __NR_mbind },
{ 1008 }, // for some reason used in julia
};

#define count_ignore_syscall (sizeof(ListIgnoreSyscall) / sizeof(ListIgnoreSyscall[0]))

static int is_ignore_syscall(int id) {
for (int i=0; i<count_ignore_syscall; ++i)
if (ListIgnoreSyscall[i].id == id)
return 1;
return 0;
}

#endif //_IGNORE_SYSCALL
66 changes: 31 additions & 35 deletions gpkg/glibc/mprotect.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <not-cancel.h>

#ifdef SHARED
# define GLOBAL_READ_SIZE 45
# define GLOBAL_READ_SIZE 1024

/* - String To Unsigned Long Int
There are two chairs - one is called "strtoul", the other "stuli"...
Expand All @@ -42,61 +42,57 @@ unsigned long int __stuli(char *value) {

static int __is_mmaped(void *addr) {
char buff[GLOBAL_READ_SIZE];
char *buff2;
char maddr[GLOBAL_READ_SIZE*2];
memset(maddr, 0, sizeof(maddr));

int skip = 0;
int res = 1;

char *buff2 = "";
char *cont = "";
int res = 0;
int strlc, strlb;
memset(buff, 0, sizeof(buff));
int map = __open_nocancel("/proc/self/maps", O_RDONLY|O_CLOEXEC);
if (map >= 0) {
while (__read_nocancel(map, buff, GLOBAL_READ_SIZE) == GLOBAL_READ_SIZE) {
while (__read_nocancel(map, buff, GLOBAL_READ_SIZE) > 0) {
buff[GLOBAL_READ_SIZE] = '\0';
if (skip == 1)
if ((buff2=strrchr(buff, '\n')) != NULL)
memcpy(maddr, &buff2[1], strlen(&buff2[1])+1);
else
continue;
if (strrchr(maddr, '-') == NULL) {
if (skip == 1) {
skip = 0;
continue;
}
__strncat(maddr, buff, GLOBAL_READ_SIZE);
strlc = strlen(cont);
if (strlc > 0) {
buff2 = malloc(sizeof(char)*strlc);
memcpy(buff2, cont, strlc);
}
if (__stuli(strtok(maddr, "-")) == (unsigned long int)addr) {
res = 0;
cont = malloc(sizeof(char)*(strlc+strlen(buff)));
memset(cont, 0, sizeof(cont));
strlb = strlen(buff2);
if (strlb > 0)
memcpy(cont, buff2, strlb);
__strncat(cont, buff, GLOBAL_READ_SIZE);
memset(buff, 0, sizeof(buff));
}
char *saveptr;
while ((buff2 = strtok_r(cont, "\n", &cont)))
if (__stuli(strtok_r(buff2, "-", &saveptr)) == (unsigned long int)addr) {
res = 1;
break;
}
memset(maddr, 0, sizeof(maddr));
if ((buff2=strrchr(buff, '\n')) != NULL)
memcpy(maddr, &buff2[1], strlen(&buff2[1])+1);
else
skip = 1;
}
__close_nocancel_nostatus(map);
}

__close_nocancel_nostatus(map);
return res;
}
#endif

int __mprotect(void *addr, size_t len, int prot) {
int res = INLINE_SYSCALL_CALL(mprotect, addr, len, prot);
#ifdef SHARED
if (res == -1 && errno == EACCES && prot & PROT_EXEC && __is_mmaped(addr) == 1) {
if (res == -1 && errno == EACCES && prot & PROT_EXEC && !__is_mmaped(addr)) {
size_t saddr = strlen(addr)+1;
void *caddr;
int mmap_flags = MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED;
if (prot & PROT_GROWSDOWN)
mmap_flags |= MAP_GROWSDOWN;
if (saddr > 1) {
caddr = malloc(saddr);
memcpy(caddr, addr, saddr);
}
mmap(addr, len, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
if (saddr > 1) {
free(addr);
addr = mmap(addr, len, PROT_READ|PROT_WRITE|PROT_EXEC, mmap_flags, -1, 0);
if (saddr > 1)
memcpy(addr, caddr, saddr);
free(caddr);
}
return INLINE_SYSCALL_CALL(mprotect, addr, len, prot);
}
#endif
Expand Down
17 changes: 17 additions & 0 deletions gpkg/glibc/syscall.c.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--- src/sysdeps/unix/sysv/linux/syscall.c 2023-07-31 20:54:16.000000000 +0300
+++ src/sysdeps/unix/sysv/linux/syscall.c.patch 2023-12-25 08:47:12.612090672 +0300
@@ -18,10 +18,14 @@

#include <stdarg.h>
#include <sysdep.h>
+#include <ignore-syscall.h>

long int
syscall (long int number, ...)
{
+ if (is_ignore_syscall(number))
+ return 0;
+
va_list args;

va_start (args, number);
8 changes: 0 additions & 8 deletions gpkg/glibc/x86_64-arch-syscall.h.patch
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@
#define __NR_getpid 39
#define __NR_getpmsg 181
#define __NR_getppid 110
@@ -152,7 +144,6 @@
#define __NR_lsetxattr 189
#define __NR_lstat 6
#define __NR_madvise 28
-#define __NR_mbind 237
#define __NR_membarrier 324
#define __NR_memfd_create 319
#define __NR_memfd_secret 447
@@ -201,7 +192,6 @@
#define __NR_personality 135
#define __NR_pidfd_getfd 438
Expand Down

0 comments on commit 31cb34b

Please sign in to comment.