From 5eaf165ecd46b3081aa884e395047a3a480154c7 Mon Sep 17 00:00:00 2001 From: Ivan Max Date: Mon, 1 Jan 2024 13:00:09 +0000 Subject: [PATCH] gpkg/glibc: corrections (#173) ldd fix for x86_64 fix mprotect.c ignore system call 1008 and mbind --- gpkg/glibc/aarch64-arch-syscall.h.patch | 8 --- gpkg/glibc/arm-arch-syscall.h.patch | 8 --- gpkg/glibc/build.sh | 13 +++- gpkg/glibc/dl-load.c.patch | 21 ++++++ gpkg/glibc/i386-arch-syscall.h.patch | 8 --- gpkg/glibc/ignore-syscall.h | 17 +++++ .../{Makefile.patch => linux-Makefile.patch} | 0 gpkg/glibc/misc-Makefile.patch | 10 +++ gpkg/glibc/misc-Versions.patch | 11 +++ gpkg/glibc/mprotect.c | 66 +++++++++--------- gpkg/glibc/syscall.S.patch | 68 +++++++++++++++++++ gpkg/glibc/syscall.c.patch | 42 ++++++++++++ gpkg/glibc/unistd.h.patch | 10 +++ gpkg/glibc/x86_64-arch-syscall.h.patch | 8 --- 14 files changed, 221 insertions(+), 69 deletions(-) create mode 100644 gpkg/glibc/dl-load.c.patch create mode 100644 gpkg/glibc/ignore-syscall.h rename gpkg/glibc/{Makefile.patch => linux-Makefile.patch} (100%) create mode 100644 gpkg/glibc/misc-Makefile.patch create mode 100644 gpkg/glibc/misc-Versions.patch create mode 100644 gpkg/glibc/syscall.S.patch create mode 100644 gpkg/glibc/syscall.c.patch create mode 100644 gpkg/glibc/unistd.h.patch diff --git a/gpkg/glibc/aarch64-arch-syscall.h.patch b/gpkg/glibc/aarch64-arch-syscall.h.patch index 0afa2501e..58e08d64f 100644 --- a/gpkg/glibc/aarch64-arch-syscall.h.patch +++ b/gpkg/glibc/aarch64-arch-syscall.h.patch @@ -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 diff --git a/gpkg/glibc/arm-arch-syscall.h.patch b/gpkg/glibc/arm-arch-syscall.h.patch index f2261daa2..c1c828414 100644 --- a/gpkg/glibc/arm-arch-syscall.h.patch +++ b/gpkg/glibc/arm-arch-syscall.h.patch @@ -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 diff --git a/gpkg/glibc/build.sh b/gpkg/glibc/build.sh index 114bc457f..d416b478f 100644 --- a/gpkg/glibc/build.sh +++ b/gpkg/glibc/build.sh @@ -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" @@ -16,11 +16,20 @@ 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/x86_64/configure* + + if [ "$TERMUX_ARCH" = "i686" ]; then + mv ${TERMUX_PKG_SRCDIR}/sysdeps/unix/sysv/linux/i386/syscall.S \ + ${TERMUX_PKG_SRCDIR}/sysdeps/unix/sysv/linux/i386/syscallS.S + else + mv ${TERMUX_PKG_SRCDIR}/sysdeps/unix/sysv/linux/${TERMUX_ARCH}/syscall.S \ + ${TERMUX_PKG_SRCDIR}/sysdeps/unix/sysv/linux/${TERMUX_ARCH}/syscallS.S + fi 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/ diff --git a/gpkg/glibc/dl-load.c.patch b/gpkg/glibc/dl-load.c.patch new file mode 100644 index 000000000..548ea6416 --- /dev/null +++ b/gpkg/glibc/dl-load.c.patch @@ -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 (); diff --git a/gpkg/glibc/i386-arch-syscall.h.patch b/gpkg/glibc/i386-arch-syscall.h.patch index eb98cd5c0..3d108be19 100644 --- a/gpkg/glibc/i386-arch-syscall.h.patch +++ b/gpkg/glibc/i386-arch-syscall.h.patch @@ -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 diff --git a/gpkg/glibc/ignore-syscall.h b/gpkg/glibc/ignore-syscall.h new file mode 100644 index 000000000..c5d369b8d --- /dev/null +++ b/gpkg/glibc/ignore-syscall.h @@ -0,0 +1,17 @@ +#ifndef _IGNORE_SYSCALL +#define _IGNORE_SYSCALL + +#include + +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])) + +#endif //_IGNORE_SYSCALL diff --git a/gpkg/glibc/Makefile.patch b/gpkg/glibc/linux-Makefile.patch similarity index 100% rename from gpkg/glibc/Makefile.patch rename to gpkg/glibc/linux-Makefile.patch diff --git a/gpkg/glibc/misc-Makefile.patch b/gpkg/glibc/misc-Makefile.patch new file mode 100644 index 000000000..4ace1ff43 --- /dev/null +++ b/gpkg/glibc/misc-Makefile.patch @@ -0,0 +1,10 @@ +--- glibc-2.38/misc/Makefile 2023-07-31 20:54:16.000000000 +0300 ++++ glibc-2.38/misc/Makefile.patch 2023-12-26 00:08:33.550537534 +0300 +@@ -192,6 +192,7 @@ + swapon \ + sync \ + syncfs \ ++ syscallS \ + syscall \ + syslog \ + truncate \ diff --git a/gpkg/glibc/misc-Versions.patch b/gpkg/glibc/misc-Versions.patch new file mode 100644 index 000000000..ee7978646 --- /dev/null +++ b/gpkg/glibc/misc-Versions.patch @@ -0,0 +1,11 @@ +--- glibc-2.38/misc/Versions 2023-07-31 20:54:16.000000000 +0300 ++++ glibc-2.38/misc/Versions.patch 2024-01-01 13:56:26.514498144 +0300 +@@ -71,7 +71,7 @@ + # s* + sbrk; select; setdomainname; setfsent; sethostent; sethostid; sethostname; + setlogmask; setmntent; setregid; setreuid; setttyent; setusershell; sstk; +- stty; sync; syscall; syslog; ++ stty; sync; syscallS; syscall; syslog; + + # t* + tdelete; tfind; truncate; tsearch; ttyslot; twalk; diff --git a/gpkg/glibc/mprotect.c b/gpkg/glibc/mprotect.c index a17b6aebb..187b03ddb 100644 --- a/gpkg/glibc/mprotect.c +++ b/gpkg/glibc/mprotect.c @@ -17,7 +17,7 @@ #include #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"... @@ -42,42 +42,36 @@ 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 @@ -85,18 +79,20 @@ static int __is_mmaped(void *addr) { 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 diff --git a/gpkg/glibc/syscall.S.patch b/gpkg/glibc/syscall.S.patch new file mode 100644 index 000000000..9956f34d0 --- /dev/null +++ b/gpkg/glibc/syscall.S.patch @@ -0,0 +1,68 @@ +--- glibc-2.38/sysdeps/unix/sysv/linux/aarch64/syscall.S 2023-07-31 20:54:16.000000000 +0300 ++++ glibc-2.38/sysdeps/unix/sysv/linux/aarch64/syscall.S.patch 2023-12-25 22:43:44.138402265 +0300 +@@ -26,7 +26,7 @@ + For kernel entry we need to move the system call nr to x8 then + load the remaining arguments to register. */ + +-ENTRY (syscall) ++ENTRY (syscallS) + uxtw x8, w0 + mov x0, x1 + mov x1, x2 +@@ -41,4 +41,4 @@ + RET + 1: + b SYSCALL_ERROR +-PSEUDO_END (syscall) ++PSEUDO_END (syscallS) +--- glibc-2.38/sysdeps/unix/sysv/linux/arm/syscall.S 2023-07-31 20:54:16.000000000 +0300 ++++ glibc-2.38/sysdeps/unix/sysv/linux/arm/syscall.S.patch 2023-12-25 22:45:38.988402221 +0300 +@@ -21,7 +21,7 @@ + implement syscall(). It won't work reliably with 64-bit arguments + (but that is true on many modern platforms). */ + +-ENTRY (syscall) ++ENTRY (syscallS) + mov ip, sp + push {r4, r5, r6, r7} + cfi_adjust_cfa_offset (16) +@@ -45,4 +45,4 @@ + it cc + RETINSTR(cc, lr) + b PLTJMP(syscall_error) +-PSEUDO_END (syscall) ++PSEUDO_END (syscallS) +--- glibc-2.38/sysdeps/unix/sysv/linux/x86_64/syscall.S 2023-07-31 20:54:16.000000000 +0300 ++++ glibc-2.38/sysdeps/unix/sysv/linux/x86_64/syscall.S.patch 2023-12-25 23:00:38.685827592 +0300 +@@ -26,7 +26,7 @@ + + + .text +-ENTRY (syscall) ++ENTRY (syscallS) + movq %rdi, %rax /* Syscall number -> rax. */ + movq %rsi, %rdi /* shift arg1 - arg5. */ + movq %rdx, %rsi +@@ -39,4 +39,4 @@ + jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */ + ret /* Return to caller. */ + +-PSEUDO_END (syscall) ++PSEUDO_END (syscallS) +--- glibc-2.38/sysdeps/unix/sysv/linux/i386/syscall.S 2023-07-31 20:54:16.000000000 +0300 ++++ glibc-2.38/sysdeps/unix/sysv/linux/i386/syscall.S.patch 2023-12-25 23:01:40.765827568 +0300 +@@ -21,7 +21,7 @@ + more information about the value -4095 used below.*/ + + .text +-ENTRY (syscall) ++ENTRY (syscallS) + + PUSHARGS_6 /* Save register contents. */ + _DOARGS_6(44) /* Load arguments. */ +@@ -32,4 +32,4 @@ + jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */ + ret /* Return to caller. */ + +-PSEUDO_END (syscall) ++PSEUDO_END (syscallS) diff --git a/gpkg/glibc/syscall.c.patch b/gpkg/glibc/syscall.c.patch new file mode 100644 index 000000000..03c6eada9 --- /dev/null +++ b/gpkg/glibc/syscall.c.patch @@ -0,0 +1,42 @@ +--- glibc-2.38/sysdeps/unix/sysv/linux/syscall.c 2023-07-31 20:54:16.000000000 +0300 ++++ glibc-2.38/sysdeps/unix/sysv/linux/syscall.c.patch 2024-01-01 13:12:45.120508427 +0300 +@@ -18,26 +18,26 @@ + + #include + #include ++#include + + long int + syscall (long int number, ...) + { ++ for (int i=0; i