diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d745b11..ebf22e3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,58 @@ What changed in each version of Cortex Terminal, newest first. The release workf of a GitHub release from the section of its version here. Older versions are described on the [GitHub releases](https://github.com/ssfuisu/cortex/releases) page. +## 1.29.3 (2026-12-05) + +### Fixed + +- **The desktop stayed black.** Ubuntu 26.04's GTK loads every icon and picture through glycin, + which first asks bubblewrap (`bwrap`) for a sandbox. Android gives apps no namespaces, and bwrap + failed with "Can't read /proc/sys/kernel/overflowuid: Permission denied", an answer glycin did not + know: it took the sandbox for available, no icon could be loaded, and XFCE's session, panel and + desktop ended at once ("Bail out! … Failed to load …image-missing.svg: Loader process exited"). + Cortex's `bwrap` now answers with bubblewrap's own message for a system without user namespaces, + and glycin loads images without the sandbox. The desktop, its panel and programs (Firefox, the + terminal, Mousepad, Thunar) start again. +- **Character set conversions other than UTF-8 failed** in every C program: glibc loads its + converters (`/usr/lib//gconv`) with its own internal calls, which the hook does not see. iconv + said "conversion from `ISO-8859-1' is not supported", GTK could not take text from the desktop's + clipboard ("Error converting selection from STRING"), and vim, git and PHP's iconv failed with + Latin-1 or Windows-1254 (Turkish) text. The hook now sets `GCONV_PATH` to the rootfs's converters of + its own architecture. +- **uptime, vmstat, ps, top and Node's `os.cpus()`**: Android keeps `/proc/uptime`, `/proc/stat`, + `/proc/loadavg`, `/proc/vmstat` and most of `/proc/sys` from apps. `uptime` failed ("Cannot get + system uptime"), `vmstat` too ("Unable to create vmstat structure"), `ps` said "Unable to get system + boot time" and showed wrong start times, `top` showed no CPU use, and `os.cpus()` was empty in + Node, so build tools that start one worker per processor started none. Where the kernel refuses + them, the hook now makes these files from what an app may ask for (the clocks, `sysinfo()`, the + number of processors, `uname()`), as it did for `/proc/version`. What each processor did is not + known to an app: it is counted as idle. +- **Node's `os.networkInterfaces()` threw "Permission denied"**, and with it development servers + that print their network address (Vite, webpack's, Next.js); Python's `socket.if_nameindex()` and + `hostname -I` failed too. glibc's `getifaddrs()` and `if_nameindex()` bind a netlink socket and ask + for the links, which Android 11+ refuses apps. Where they fail, the hook asks the way Android's own + libc does: the addresses (which apps may read) and each interface's name and flags. Hardware + addresses stay hidden, as Android hides them from apps. +- **Names added to `/etc/hosts` were never found, and service names were unknown**: glibc reads + `/etc/hosts`, `/etc/resolv.conf` and `/etc/services` from inside itself, where the hook does not + reach, so it read Android's `/etc` (no resolv.conf, no services). `getservbyname("https")` failed, + `getaddrinfo(host, "ssh")` was refused or gave port 80, and `gethostbyname()` failed for every name + ("Host name lookup failure" in Python's `socket.gethostbyname_ex()`, `getent hosts`, Perl, PHP). + `getaddrinfo()` now looks in Cortex's `/etc/hosts` first and takes service names from its + `/etc/services`; `getservbyname()`, `getservbyport()`, `getprotobyname()` (Python's + `socket.getprotobyname("tcp")` failed too) and the `gethostbyname()` family answer from them, + Cortex's `/etc/protocols` and the DNS fallback where glibc finds nothing. +- **`chsh` called every shell invalid**, `/bin/bash` too, and refused a new one to a user: + `getusershell()` read Android's `/etc/shells` (there is none) and gave glibc's built-in two. It + reads Cortex's now. + +### Tests + +- The emulator test checks iconv (also with an empty environment), bwrap's answer and the made + files of /proc, the network interfaces, /etc/hosts and /etc/services. +- The package test starts and asks MariaDB as root (`sudo mariadb`), its administrator on Ubuntu: as + the app's user it was refused ("Access denied for user 'cortex'"), as on any Ubuntu. + ## 1.29.2 (2026-12-04) ### Fixed diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 988b9478..f2f8cabc 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -16,8 +16,8 @@ android { // privacy warnings. Writable rootfs binaries (app_data_file) execute via libcortex-exec.so // in nativeLibraryDir (apk_data_file), which holds execute_no_trans under targetSdk >= 29. targetSdk = 35 - versionCode = 12902 - versionName = "1.29.2" + versionCode = 12903 + versionName = "1.29.3" // UbuntuOnDeviceTest (app/src/androidTest), on Firebase Test Lab testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/assets/scripts/bwrap.sh b/app/src/main/assets/scripts/bwrap.sh new file mode 100644 index 00000000..02036dc9 --- /dev/null +++ b/app/src/main/assets/scripts/bwrap.sh @@ -0,0 +1,14 @@ +#!/bin/sh +# bwrap (bubblewrap) in Cortex: Android gives an app no namespaces, so a sandbox of bubblewrap's +# cannot be made here. The real one failed with "Can't read /proc/sys/kernel/overflowuid: Permission +# denied", which its users do not know: GTK's image loader (glycin) then took the sandbox for +# available and every icon and picture failed to load, which ended XFCE's session, panel and desktop +# at once (a black desktop). This answers with bubblewrap's own message for a system without user +# namespaces, which glycin, Flatpak and the others recognize and do without the sandbox. +case "${1:-}" in + --version|--help|--usage) + if [ -x /usr/bin/bwrap ]; then exec /usr/bin/bwrap "$@"; fi + ;; +esac +echo "bwrap: No permissions to create a new namespace, likely because the kernel does not allow non-privileged user namespaces." >&2 +exit 1 diff --git a/app/src/main/cpp/cortex-hook.c b/app/src/main/cpp/cortex-hook.c index 7c5d30f8..5a911b31 100644 --- a/app/src/main/cpp/cortex-hook.c +++ b/app/src/main/cpp/cortex-hook.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -100,6 +101,7 @@ static void *cortex_next(void *own, const char *name) { #include "hook/exec.c" #include "hook/net.c" #include "hook/sockets.c" +#include "hook/ifaddrs.c" #include "hook/sysv_shm.c" #include "hook/memfd_locks.c" #include "hook/termios.c" diff --git a/app/src/main/cpp/hook/files.c b/app/src/main/cpp/hook/files.c index 11b7cde1..fa55485d 100644 --- a/app/src/main/cpp/hook/files.c +++ b/app/src/main/cpp/hook/files.c @@ -5,74 +5,237 @@ #ifndef CORTEX_HOST_TEST -// Android's SELinux denies untrusted_app access to /proc/version (u:object_r:proc_version:s0). -// Programs like LibreOffice (oosplash) check stat("/proc/version") on startup and fail with -// "ERROR: /proc not mounted" when kernel denies it. Fall back to synthetic uname-backed metadata -// and content only if kernel refuses access. +// Files of /proc that Android's SELinux policy keeps from apps (untrusted_app may not read +// proc_version, proc_stat, proc_loadavg, proc_uptime, most of /proc/sys): where the kernel refuses +// one, its content is made from what an app may ask for (uname(), sysinfo(), the clocks, the +// number of processors) and handed out as a file of its own (a memfd). Programs that need them +// failed: LibreOffice's oosplash ("/proc not mounted" without /proc/version), uptime ("Cannot get +// system uptime"), ps ("Unable to get system boot time", wrong START times), vmstat ("Unable to +// create vmstat structure"), top (no CPU use), Node's os.cpus() (empty: a build tool that starts +// one worker per processor started none), bubblewrap (/proc/sys/kernel/overflowuid). A file the +// kernel lets the app read is read as it is. +enum { + SYNTH_PROC_NONE = 0, + SYNTH_PROC_VERSION, + SYNTH_PROC_UPTIME, + SYNTH_PROC_LOADAVG, + SYNTH_PROC_STAT, + SYNTH_PROC_VMSTAT, + SYNTH_PROC_OVERFLOWUID, + SYNTH_PROC_OVERFLOWGID, + SYNTH_PROC_PID_MAX, + SYNTH_PROC_NGROUPS_MAX, + SYNTH_PROC_CAP_LAST_CAP, + SYNTH_PROC_HOSTNAME, + SYNTH_PROC_OSRELEASE, + SYNTH_PROC_FILE_MAX, + SYNTH_PROC_NR_OPEN, +}; + +static int synth_proc_kind(const char *path) { + if (!path || strncmp(path, "/proc/", 6) != 0) return SYNTH_PROC_NONE; + const char *p = path + 6; + if (strcmp(p, "version") == 0) return SYNTH_PROC_VERSION; + if (strcmp(p, "uptime") == 0) return SYNTH_PROC_UPTIME; + if (strcmp(p, "loadavg") == 0) return SYNTH_PROC_LOADAVG; + if (strcmp(p, "stat") == 0) return SYNTH_PROC_STAT; + if (strcmp(p, "vmstat") == 0) return SYNTH_PROC_VMSTAT; + if (strncmp(p, "sys/kernel/", 11) == 0) { + p += 11; + if (strcmp(p, "overflowuid") == 0) return SYNTH_PROC_OVERFLOWUID; + if (strcmp(p, "overflowgid") == 0) return SYNTH_PROC_OVERFLOWGID; + if (strcmp(p, "pid_max") == 0) return SYNTH_PROC_PID_MAX; + if (strcmp(p, "ngroups_max") == 0) return SYNTH_PROC_NGROUPS_MAX; + if (strcmp(p, "cap_last_cap") == 0) return SYNTH_PROC_CAP_LAST_CAP; + if (strcmp(p, "hostname") == 0) return SYNTH_PROC_HOSTNAME; + if (strcmp(p, "osrelease") == 0) return SYNTH_PROC_OSRELEASE; + return SYNTH_PROC_NONE; + } + if (strcmp(p, "sys/fs/file-max") == 0) return SYNTH_PROC_FILE_MAX; + if (strcmp(p, "sys/fs/nr_open") == 0) return SYNTH_PROC_NR_OPEN; + return SYNTH_PROC_NONE; +} + static int is_proc_version_path(const char *path) { - return path && strcmp(path, "/proc/version") == 0; + return synth_proc_kind(path) != SYNTH_PROC_NONE; +} + +// The highest capability of the running kernel: CAP_CHECKPOINT_RESTORE (40) since 5.9, CAP_BPF +// (39) in 5.8, CAP_PERFMON (38) in 5.8's first release candidates, CAP_AUDIT_READ (37) before +static int synth_cap_last_cap(const char *release) { + int major = 0, minor = 0; + if (sscanf(release, "%d.%d", &major, &minor) != 2) return 40; + if (major > 5 || (major == 5 && minor >= 9)) return 40; + if (major == 5 && minor == 8) return 39; + return 37; } -static size_t format_proc_version_line(char *buf, size_t cap) { +// The content of a synthetic file into buf; its length (0: not one of them) +static size_t synth_proc_content(int kind, char *buf, size_t cap) { struct utsname u; + struct sysinfo si; + struct timespec ts; + int n = 0; memset(&u, 0, sizeof(u)); - if (uname(&u) != 0) { - int n = snprintf(buf, cap, "Linux version 6.1.0-android (cortex@localhost) (gcc) #1 SMP PREEMPT\n"); - return (n > 0 && (size_t)n < cap) ? (size_t)n : (cap > 0 ? cap - 1 : 0); + memset(&si, 0, sizeof(si)); + switch (kind) { + case SYNTH_PROC_VERSION: + if (uname(&u) != 0) { + n = snprintf(buf, cap, "Linux version 6.1.0-android (cortex@localhost) (gcc) #1 SMP PREEMPT\n"); + } else { + n = snprintf(buf, cap, "Linux version %s (cortex@localhost) (gcc) %s\n", + u.release[0] ? u.release : "6.1.0-android", u.version[0] ? u.version : "#1 SMP PREEMPT"); + } + break; + case SYNTH_PROC_UPTIME: + case SYNTH_PROC_STAT: { + if (clock_gettime(CLOCK_BOOTTIME, &ts) != 0) memset(&ts, 0, sizeof(ts)); + long ncpu = sysconf(_SC_NPROCESSORS_CONF); + if (ncpu < 1) ncpu = 1; + if (ncpu > 1024) ncpu = 1024; + unsigned long long centis = (unsigned long long)ts.tv_sec * 100ULL + (unsigned long long)ts.tv_nsec / 10000000ULL; + if (kind == SYNTH_PROC_UPTIME) { + // the idle time of all processors is not known to an app: given as all of it, like + // the "cpu" lines of /proc/stat below + unsigned long long idle = centis * (unsigned long long)ncpu; + n = snprintf(buf, cap, "%llu.%02llu %llu.%02llu\n", centis / 100, centis % 100, idle / 100, idle % 100); + break; + } + // Times in USER_HZ (1/100 s). What each processor did is not known to an app: all of its + // time is counted as idle (top shows 0 % used rather than nothing at all) + size_t len = 0; + int k = snprintf(buf, cap, "cpu 0 0 0 %llu 0 0 0 0 0 0\n", centis * (unsigned long long)ncpu); + if (k < 0 || (size_t)k >= cap) return 0; + len = (size_t)k; + for (long i = 0; i < ncpu; i++) { + k = snprintf(buf + len, cap - len, "cpu%ld 0 0 0 %llu 0 0 0 0 0 0\n", i, centis); + if (k < 0 || (size_t)k >= cap - len) return 0; + len += (size_t)k; + } + (void)sysinfo(&si); + struct timespec now; + if (clock_gettime(CLOCK_REALTIME, &now) != 0) memset(&now, 0, sizeof(now)); + long long btime = (long long)now.tv_sec - (long long)ts.tv_sec; + k = snprintf(buf + len, cap - len, + "intr 0\nctxt 0\nbtime %lld\nprocesses %u\nprocs_running 1\nprocs_blocked 0\nsoftirq 0 0 0 0 0 0 0 0 0 0 0\n", + btime, (unsigned)si.procs); + if (k < 0 || (size_t)k >= cap - len) return 0; + return len + (size_t)k; + } + case SYNTH_PROC_LOADAVG: { + (void)sysinfo(&si); + double scale = (double)(1 << SI_LOAD_SHIFT); + n = snprintf(buf, cap, "%.2f %.2f %.2f 1/%u %d\n", si.loads[0] / scale, si.loads[1] / scale, + si.loads[2] / scale, si.procs ? (unsigned)si.procs : 1u, (int)getpid()); + break; + } + case SYNTH_PROC_VMSTAT: { + // free memory as sysinfo() has it; the counters of paging and swapping are not known to an app + (void)sysinfo(&si); + long page = sysconf(_SC_PAGESIZE); + if (page <= 0) page = 4096; + unsigned long long unit = si.mem_unit ? si.mem_unit : 1; + n = snprintf(buf, cap, + "nr_free_pages %llu\nnr_dirty 0\nnr_writeback 0\npgpgin 0\npgpgout 0\npswpin 0\npswpout 0\n" + "pgalloc_normal 0\npgfree 0\npgfault 0\npgmajfault 0\n", + (unsigned long long)si.freeram * unit / (unsigned long long)page); + break; + } + case SYNTH_PROC_OVERFLOWUID: + case SYNTH_PROC_OVERFLOWGID: + n = snprintf(buf, cap, "65534\n"); + break; + case SYNTH_PROC_PID_MAX: + n = snprintf(buf, cap, "4194304\n"); // the most a 64-bit kernel allows: no pid is above it + break; + case SYNTH_PROC_NGROUPS_MAX: + n = snprintf(buf, cap, "65536\n"); + break; + case SYNTH_PROC_CAP_LAST_CAP: + n = snprintf(buf, cap, "%d\n", uname(&u) == 0 ? synth_cap_last_cap(u.release) : 40); + break; + case SYNTH_PROC_HOSTNAME: + n = snprintf(buf, cap, "%s\n", uname(&u) == 0 && u.nodename[0] ? u.nodename : "localhost"); + break; + case SYNTH_PROC_OSRELEASE: + n = snprintf(buf, cap, "%s\n", uname(&u) == 0 ? u.release : "6.1.0-android"); + break; + case SYNTH_PROC_FILE_MAX: + n = snprintf(buf, cap, "9223372036854775807\n"); + break; + case SYNTH_PROC_NR_OPEN: + n = snprintf(buf, cap, "1048576\n"); + break; + default: + return 0; } - int n = snprintf(buf, cap, "Linux version %s (cortex@localhost) (gcc) %s\n", - u.release[0] ? u.release : "6.1.0-android", - u.version[0] ? u.version : "#1 SMP PREEMPT"); if (n <= 0) return 0; return ((size_t)n < cap) ? (size_t)n : cap - 1; } -static void fill_synthetic_proc_version_stat(struct stat *st) { +#define SYNTH_PROC_CAP 65536 + +static size_t synth_proc_size(const char *path) { + char *buf = malloc(SYNTH_PROC_CAP); + if (!buf) return 0; + size_t len = synth_proc_content(synth_proc_kind(path), buf, SYNTH_PROC_CAP); + free(buf); + return len; +} + +// As the kernel shows files of /proc: size 0 (their content is made when read); /proc/version kept +// its length, which programs never relied on either way +static void fill_synthetic_proc_version_stat(const char *path, struct stat *st) { if (!st) return; - char line[384]; - size_t len = format_proc_version_line(line, sizeof(line)); + int kind = synth_proc_kind(path); memset(st, 0, sizeof(*st)); - st->st_mode = S_IFREG | 0444; + st->st_mode = S_IFREG | ((kind >= SYNTH_PROC_OVERFLOWUID) ? 0644 : 0444); st->st_nlink = 1; - st->st_uid = 0; - st->st_gid = 0; - st->st_ino = 0x70726f63UL; + st->st_ino = 0x70726f63UL + (unsigned long)kind; st->st_blksize = 4096; - st->st_size = (off_t)len; + st->st_size = kind == SYNTH_PROC_VERSION ? (off_t)synth_proc_size(path) : 0; } -static void fill_synthetic_proc_version_statx(struct statx *sx) { +static void fill_synthetic_proc_version_statx(const char *path, struct statx *sx) { if (!sx) return; - char line[384]; - size_t len = format_proc_version_line(line, sizeof(line)); + struct stat st; + fill_synthetic_proc_version_stat(path, &st); memset(sx, 0, sizeof(*sx)); sx->stx_mask = STATX_BASIC_STATS; sx->stx_blksize = 4096; sx->stx_nlink = 1; - sx->stx_uid = 0; - sx->stx_gid = 0; - sx->stx_mode = S_IFREG | 0444; - sx->stx_ino = 0x70726f63ULL; - sx->stx_size = (uint64_t)len; + sx->stx_mode = (uint16_t)st.st_mode; + sx->stx_ino = (uint64_t)st.st_ino; + sx->stx_size = (uint64_t)st.st_size; } static int is_proc_version_fallback_err(int err) { return err == EACCES || err == EPERM || err == ENOENT; } -static int open_synthetic_proc_version(int flags) { +static int open_synthetic_proc_version(const char *path, int flags) { if ((flags & O_ACCMODE) != O_RDONLY) { errno = EACCES; return -1; } - char line[384]; - size_t len = format_proc_version_line(line, sizeof(line)); + char *buf = malloc(SYNTH_PROC_CAP); + if (!buf) { + errno = ENOMEM; + return -1; + } + size_t len = synth_proc_content(synth_proc_kind(path), buf, SYNTH_PROC_CAP); + if (len == 0) { + free(buf); + errno = EACCES; + return -1; + } int fd = -1; #ifdef __NR_memfd_create - fd = (int)syscall(__NR_memfd_create, "proc_version", MFD_CLOEXEC); + fd = (int)syscall(__NR_memfd_create, "proc", MFD_CLOEXEC); if (fd >= 0) { - ssize_t w = write(fd, line, len); + ssize_t w = write(fd, buf, len); if (w == (ssize_t)len && lseek(fd, 0, SEEK_SET) == 0) { + free(buf); if ((flags & O_CLOEXEC) == 0) fcntl(fd, F_SETFD, 0); return fd; } @@ -80,12 +243,16 @@ static int open_synthetic_proc_version(int flags) { } #endif int pfd[2]; - if (pipe2(pfd, (flags & O_CLOEXEC) ? O_CLOEXEC : 0) == 0) { - ssize_t w = write(pfd[1], line, len); + if (len <= 4096 && pipe2(pfd, (flags & O_CLOEXEC) ? O_CLOEXEC : 0) == 0) { + ssize_t w = write(pfd[1], buf, len); syscall(SYS_close, pfd[1]); - if (w == (ssize_t)len) return pfd[0]; + if (w == (ssize_t)len) { + free(buf); + return pfd[0]; + } syscall(SYS_close, pfd[0]); } + free(buf); errno = EACCES; return -1; } @@ -263,7 +430,7 @@ int open(const char *pathname, int flags, ...) { int fd = orig_open(target, flags); if (fd < 0 && errno == EACCES) fd = open_as_root_after_refusal(AT_FDCWD, target, flags, 0, 0); if (fd < 0 && is_proc_version_fallback_err(errno) && is_proc_version_path(pathname)) { - fd = open_synthetic_proc_version(flags); + fd = open_synthetic_proc_version(pathname, flags); is_home = 0; } if (fd >= 0) home_fd_set(fd, is_home); @@ -305,7 +472,7 @@ int openat(int dirfd, const char *pathname, int flags, ...) { int fd = orig_openat(dirfd, target, flags); if (fd < 0 && errno == EACCES) fd = open_as_root_after_refusal(dirfd, target, flags, 0, 0); if (fd < 0 && is_proc_version_fallback_err(errno) && is_proc_version_path(pathname)) { - fd = open_synthetic_proc_version(flags); + fd = open_synthetic_proc_version(pathname, flags); is_home = 0; } if (fd >= 0) home_fd_set(fd, is_home); @@ -384,7 +551,7 @@ int openat2(int dirfd, const char *pathname, cortex_open_how_arg_t how, size_t u int ret = orig_openat2(dirfd, target, how, usize); if (ret < 0 && how && (how->flags & O_ACCMODE) == O_RDONLY && is_proc_version_fallback_err(errno) && is_proc_version_path(pathname)) { - return open_synthetic_proc_version((int)(how->flags & 0xffffffffu)); + return open_synthetic_proc_version(pathname, (int)(how->flags & 0xffffffffu)); } if (ret >= 0 || errno != ENOSYS) return ret; } @@ -392,7 +559,7 @@ int openat2(int dirfd, const char *pathname, cortex_open_how_arg_t how, size_t u long sret = call_orig_syscall(__NR_openat2, (unsigned long)dirfd, (unsigned long)target, (unsigned long)how, (unsigned long)usize, 0, 0); if (sret < 0 && how && (how->flags & O_ACCMODE) == O_RDONLY && is_proc_version_fallback_err(errno) && is_proc_version_path(pathname)) { - return open_synthetic_proc_version((int)(how->flags & 0xffffffffu)); + return open_synthetic_proc_version(pathname, (int)(how->flags & 0xffffffffu)); } if (sret >= 0 || errno != ENOSYS) { return (int)sret; @@ -474,7 +641,7 @@ FILE *fopen(const char *pathname, const char *mode) { if (mode && (mode[0] != 'r' || strchr(mode, '+'))) { errno = EACCES; } else { - int pfd = open_synthetic_proc_version(O_RDONLY | ((mode && strchr(mode, 'e')) ? O_CLOEXEC : 0)); + int pfd = open_synthetic_proc_version(pathname, O_RDONLY | ((mode && strchr(mode, 'e')) ? O_CLOEXEC : 0)); if (pfd >= 0) { f = fdopen(pfd, mode ? mode : "r"); if (!f) close(pfd); @@ -695,7 +862,7 @@ int stat(const char *pathname, struct stat *statbuf) { if (retry) ret = orig_stat(retry, statbuf); } if (ret != 0 && is_proc_version_fallback_err(errno) && is_proc_version_path(pathname)) { - fill_synthetic_proc_version_stat(statbuf); + fill_synthetic_proc_version_stat(pathname, statbuf); errno = 0; return 0; } @@ -825,7 +992,7 @@ int lstat(const char *pathname, struct stat *statbuf) { link_size_as_read(AT_FDCWD, pathname, target, statbuf); } if (ret != 0 && is_proc_version_fallback_err(errno) && is_proc_version_path(pathname)) { - fill_synthetic_proc_version_stat(statbuf); + fill_synthetic_proc_version_stat(pathname, statbuf); errno = 0; return 0; } @@ -853,7 +1020,7 @@ int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags) { link_size_as_read(dirfd, pathname, target, statbuf); } if (ret != 0 && is_proc_version_fallback_err(errno) && is_proc_version_path(pathname)) { - fill_synthetic_proc_version_stat(statbuf); + fill_synthetic_proc_version_stat(pathname, statbuf); errno = 0; return 0; } @@ -872,7 +1039,7 @@ int fstat(int fd, struct stat *statbuf) { static int maybe_proc_version_stat(int ret, const char *pathname, void *statbuf) { if (ret != 0 && is_proc_version_fallback_err(errno) && is_proc_version_path(pathname)) { - fill_synthetic_proc_version_stat((struct stat *)statbuf); + fill_synthetic_proc_version_stat(pathname, (struct stat *)statbuf); return 0; } return ret; @@ -945,7 +1112,7 @@ int statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct const char *target = (pathname && pathname[0] == '/') ? rewrite_path(pathname, buf, sizeof(buf)) : pathname; int ret = orig_statx ? orig_statx(dirfd, target, flags, mask, statxbuf) : -1; if (ret != 0 && is_proc_version_fallback_err(errno) && is_proc_version_path(pathname)) { - fill_synthetic_proc_version_statx(statxbuf); + fill_synthetic_proc_version_statx(pathname, statxbuf); errno = 0; return 0; } diff --git a/app/src/main/cpp/hook/identity.c b/app/src/main/cpp/hook/identity.c index 1481b0bf..5664e15d 100644 --- a/app/src/main/cpp/hook/identity.c +++ b/app/src/main/cpp/hook/identity.c @@ -924,3 +924,84 @@ int CORTEX_NSS_SYM(getpwnam_r)(const char *name, struct passwd *pwd, char *buf, if (result) *result = NULL; return (err == ERANGE) ? ERANGE : 0; } + +#ifndef CORTEX_HOST_TEST +// getusershell(): glibc reads /etc/shells from inside itself, and Android has none, so it gave its +// built-in two (/bin/sh, /bin/csh): chsh called every shell invalid (/bin/bash too) and refused it to +// a user. The list comes from Cortex's /etc/shells instead, as glibc reads it: one path per line, +// '#' and blank lines skipped; glibc's two when the file is missing. +static pthread_mutex_t g_shells_mutex = PTHREAD_MUTEX_INITIALIZER; +static char **g_shells; +static size_t g_shells_count; +static size_t g_shells_next; + +static void cx_free_shells(void) { + for (size_t i = 0; i < g_shells_count; i++) free(g_shells[i]); + free(g_shells); + g_shells = NULL; + g_shells_count = 0; + g_shells_next = 0; +} + +static void cx_load_shells(void) { + cx_free_shells(); + init_cortex_hook(); + char path[PATH_MAX]; + FILE *f = NULL; + if (g_cortex_root[0] && snprintf(path, sizeof(path), "%s/etc/shells", g_cortex_root) < (int)sizeof(path)) { + f = fopen(path, "re"); + } + size_t cap = 0; + if (f) { + char line[PATH_MAX]; + while (fgets(line, sizeof(line), f)) { + char *p = line; + while (*p == ' ' || *p == '\t') p++; + if (*p != '/') continue; + char *e = p; + while (*e && *e != '\n' && *e != '\r' && *e != ' ' && *e != '\t' && *e != '#') e++; + *e = '\0'; + if (g_shells_count == cap) { + size_t ncap = cap ? cap * 2 : 16; + char **n = realloc(g_shells, ncap * sizeof(char *)); + if (!n) break; + g_shells = n; + cap = ncap; + } + char *s = strdup(p); + if (!s) break; + g_shells[g_shells_count++] = s; + } + fclose(f); + } + if (g_shells_count == 0) { + static const char *const builtin[] = { "/bin/sh", "/bin/csh" }; + g_shells = calloc(2, sizeof(char *)); + if (!g_shells) return; + for (size_t i = 0; i < 2; i++) { + g_shells[i] = strdup(builtin[i]); + if (g_shells[i]) g_shells_count = i + 1; + } + } +} + +char *getusershell(void) { + pthread_mutex_lock(&g_shells_mutex); + if (!g_shells) cx_load_shells(); + char *r = g_shells_next < g_shells_count ? g_shells[g_shells_next++] : NULL; + pthread_mutex_unlock(&g_shells_mutex); + return r; +} + +void setusershell(void) { + pthread_mutex_lock(&g_shells_mutex); + cx_load_shells(); + pthread_mutex_unlock(&g_shells_mutex); +} + +void endusershell(void) { + pthread_mutex_lock(&g_shells_mutex); + cx_free_shells(); + pthread_mutex_unlock(&g_shells_mutex); +} +#endif /* !CORTEX_HOST_TEST */ diff --git a/app/src/main/cpp/hook/ifaddrs.c b/app/src/main/cpp/hook/ifaddrs.c new file mode 100644 index 00000000..4d5f4a82 --- /dev/null +++ b/app/src/main/cpp/hook/ifaddrs.c @@ -0,0 +1,338 @@ +// Part of libcortex-hook.so, included by cortex-hook.c (one translation unit, so the +// parts share their static functions and are built with the same command as before). +// +// The network interfaces and their addresses: getifaddrs() and if_nameindex(). glibc asks the kernel +// for them over a netlink socket that it first bind()s and then asks for the links (RTM_GETLINK): +// Android 11 and newer refuse an app both ("Permission denied"). So Node's os.networkInterfaces() +// threw (and with it the development servers that print their addresses: Vite, webpack's, Next.js), +// Python's socket.if_nameindex(), `hostname -I` and Go's net.Interfaces() failed. What an app may +// still ask for, the way Android's own libc does: the addresses (RTM_GETADDR, without bind()) and, +// for each interface, its name, flags and index (ioctl). An interface's hardware address is not given +// to apps: its AF_PACKET entry has none, as on Android. + +#ifndef CORTEX_HOST_TEST + +#include +#include +#include +#include +#include +#include +#include + +struct cx_ifaddr_rec { + struct ifaddrs ifa; + struct sockaddr_storage addr; + struct sockaddr_storage mask; + struct sockaddr_storage brd; + struct rtnl_link_stats stats; + char name[IF_NAMESIZE]; +}; + +struct cx_addr_info { + int family; + int index; + int prefix; + int has_brd; + unsigned char addr[16]; + unsigned char brd[16]; +}; + +#define CX_IFADDR_MAX 256 + +// The addresses of all interfaces (RTM_GETADDR), at most max; their number, or -1 +static int cx_netlink_addrs(struct cx_addr_info *out, int max) { + int s = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); + if (s < 0) return -1; + struct { + struct nlmsghdr h; + struct ifaddrmsg m; + } req; + memset(&req, 0, sizeof(req)); + req.h.nlmsg_len = sizeof(req); + req.h.nlmsg_type = RTM_GETADDR; + req.h.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; + req.h.nlmsg_seq = 1; + req.m.ifa_family = AF_UNSPEC; + if (send(s, &req, sizeof(req), 0) < 0) { + int e = errno; + close(s); + errno = e; + return -1; + } + int count = 0, done = 0, failed = 0; + char *buf = malloc(32768); + if (!buf) { + close(s); + errno = ENOMEM; + return -1; + } + while (!done) { + ssize_t n = recv(s, buf, 32768, 0); + if (n < 0 && errno == EINTR) continue; + if (n <= 0) { + failed = 1; + break; + } + for (struct nlmsghdr *h = (struct nlmsghdr *)buf; NLMSG_OK(h, (size_t)n); h = NLMSG_NEXT(h, n)) { + if (h->nlmsg_type == NLMSG_DONE) { + done = 1; + break; + } + if (h->nlmsg_type == NLMSG_ERROR) { + struct nlmsgerr *e = (struct nlmsgerr *)NLMSG_DATA(h); + errno = e->error < 0 ? -e->error : EIO; + failed = 1; + done = 1; + break; + } + if (h->nlmsg_type != RTM_NEWADDR || count >= max) continue; + struct ifaddrmsg *m = (struct ifaddrmsg *)NLMSG_DATA(h); + if (m->ifa_family != AF_INET && m->ifa_family != AF_INET6) continue; + int alen = m->ifa_family == AF_INET ? 4 : 16; + struct cx_addr_info *a = &out[count]; + memset(a, 0, sizeof(*a)); + a->family = m->ifa_family; + a->index = (int)m->ifa_index; + a->prefix = m->ifa_prefixlen; + int have_local = 0, have_addr = 0; + unsigned char local[16], address[16]; + int rlen = (int)IFA_PAYLOAD(h); + for (struct rtattr *r = IFA_RTA(m); RTA_OK(r, rlen); r = RTA_NEXT(r, rlen)) { + if ((int)RTA_PAYLOAD(r) < alen) continue; + if (r->rta_type == IFA_LOCAL) { + memcpy(local, RTA_DATA(r), (size_t)alen); + have_local = 1; + } else if (r->rta_type == IFA_ADDRESS) { + memcpy(address, RTA_DATA(r), (size_t)alen); + have_addr = 1; + } else if (r->rta_type == IFA_BROADCAST && m->ifa_family == AF_INET) { + memcpy(a->brd, RTA_DATA(r), 4); + a->has_brd = 1; + } + } + // IFA_LOCAL is the interface's own address (IFA_ADDRESS is the peer's on a point-to-point link) + if (have_local) memcpy(a->addr, local, (size_t)alen); + else if (have_addr) memcpy(a->addr, address, (size_t)alen); + else continue; + count++; + } + } + free(buf); + int e = errno; + close(s); + if (failed) { + errno = e; + return -1; + } + return count; +} + +static void cx_fill_sockaddr(struct sockaddr_storage *ss, int family, const unsigned char *bytes, int index) { + memset(ss, 0, sizeof(*ss)); + if (family == AF_INET) { + struct sockaddr_in *sin = (struct sockaddr_in *)ss; + sin->sin_family = AF_INET; + memcpy(&sin->sin_addr, bytes, 4); + } else { + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ss; + sin6->sin6_family = AF_INET6; + memcpy(&sin6->sin6_addr, bytes, 16); + // link-local addresses name their interface, as glibc's do + if (bytes[0] == 0xfe && (bytes[1] & 0xc0) == 0x80) sin6->sin6_scope_id = (uint32_t)index; + } +} + +static void cx_fill_mask(struct sockaddr_storage *ss, int family, int prefix) { + unsigned char m[16]; + int len = family == AF_INET ? 4 : 16; + memset(m, 0, sizeof(m)); + for (int i = 0; i < len; i++) { + int bits = prefix - i * 8; + m[i] = bits >= 8 ? 0xff : (bits <= 0 ? 0 : (unsigned char)(0xff << (8 - bits))); + } + cx_fill_sockaddr(ss, family, m, 0); +} + +// Name and flags of the interface with this index; 0, or -1 +static int cx_if_name_flags(int sock, int index, char *name, unsigned int *flags) { + struct ifreq r; + memset(&r, 0, sizeof(r)); + r.ifr_ifindex = index; + if (ioctl(sock, SIOCGIFNAME, &r) != 0) return -1; + memcpy(name, r.ifr_name, IF_NAMESIZE); + name[IF_NAMESIZE - 1] = '\0'; + *flags = 0; + if (ioctl(sock, SIOCGIFFLAGS, &r) == 0) *flags = (unsigned short)r.ifr_flags; + return 0; +} + +// The interfaces that have an address, in the order of their first one (glibc lists by index) +static int cx_collect_indexes(const struct cx_addr_info *addrs, int n, int *idx, int max) { + int k = 0; + for (int i = 0; i < n; i++) { + int seen = 0; + for (int j = 0; j < k; j++) { + if (idx[j] == addrs[i].index) seen = 1; + } + if (!seen && k < max) idx[k++] = addrs[i].index; + } + // sorted by index + for (int i = 1; i < k; i++) { + int v = idx[i], j = i - 1; + while (j >= 0 && idx[j] > v) { + idx[j + 1] = idx[j]; + j--; + } + idx[j + 1] = v; + } + return k; +} + +static int cortex_getifaddrs_fallback(struct ifaddrs **ifap) { + struct cx_addr_info *addrs = malloc(sizeof(struct cx_addr_info) * CX_IFADDR_MAX); + if (!addrs) { + errno = ENOMEM; + return -1; + } + int n = cx_netlink_addrs(addrs, CX_IFADDR_MAX); + if (n < 0) { + int e = errno; + free(addrs); + errno = e; + return -1; + } + int idx[CX_IFADDR_MAX]; + int nif = cx_collect_indexes(addrs, n, idx, CX_IFADDR_MAX); + int sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); + // One block, the entries first, as glibc's: its own freeifaddrs() frees it + size_t total = (size_t)(nif + n); + struct cx_ifaddr_rec *recs = calloc(total ? total : 1, sizeof(struct cx_ifaddr_rec)); + if (!recs) { + if (sock >= 0) close(sock); + free(addrs); + errno = ENOMEM; + return -1; + } + size_t used = 0; + // AF_PACKET: one per interface (glibc's come first), then the addresses + for (int i = 0; i < nif; i++) { + struct cx_ifaddr_rec *r = &recs[used]; + unsigned int flags = 0; + if (sock < 0 || cx_if_name_flags(sock, idx[i], r->name, &flags) != 0) { + snprintf(r->name, sizeof(r->name), "if%d", idx[i]); + } + struct sockaddr_ll *ll = (struct sockaddr_ll *)&r->addr; + ll->sll_family = AF_PACKET; + ll->sll_ifindex = idx[i]; + ll->sll_hatype = (flags & IFF_LOOPBACK) ? 772 /* ARPHRD_LOOPBACK */ : 1 /* ARPHRD_ETHER */; + ll->sll_halen = 6; + r->ifa.ifa_name = r->name; + r->ifa.ifa_flags = flags; + r->ifa.ifa_addr = (struct sockaddr *)&r->addr; + r->ifa.ifa_data = &r->stats; + used++; + } + for (int i = 0; i < n; i++) { + struct cx_ifaddr_rec *r = &recs[used]; + const struct cx_addr_info *a = &addrs[i]; + const char *name = NULL; + unsigned int flags = 0; + for (int j = 0; j < nif; j++) { + if (idx[j] == a->index) { + name = recs[j].name; + flags = recs[j].ifa.ifa_flags; + } + } + snprintf(r->name, sizeof(r->name), "%s", name ? name : "?"); + cx_fill_sockaddr(&r->addr, a->family, a->addr, a->index); + cx_fill_mask(&r->mask, a->family, a->prefix); + r->ifa.ifa_name = r->name; + r->ifa.ifa_flags = flags; + r->ifa.ifa_addr = (struct sockaddr *)&r->addr; + r->ifa.ifa_netmask = (struct sockaddr *)&r->mask; + if (a->has_brd && (flags & IFF_BROADCAST)) { + cx_fill_sockaddr(&r->brd, a->family, a->brd, a->index); + r->ifa.ifa_broadaddr = (struct sockaddr *)&r->brd; + } + used++; + } + for (size_t i = 0; i + 1 < used; i++) recs[i].ifa.ifa_next = &recs[i + 1].ifa; + if (sock >= 0) close(sock); + free(addrs); + if (used == 0) { + free(recs); + *ifap = NULL; + return 0; + } + *ifap = &recs[0].ifa; + return 0; +} + +int getifaddrs(struct ifaddrs **ifap) { + static int (*orig)(struct ifaddrs **) = NULL; + if (!orig) orig = (int (*)(struct ifaddrs **))dlsym(RTLD_NEXT, "getifaddrs"); + if (!ifap) { + errno = EFAULT; + return -1; + } + int saved = errno; + if (orig && orig(ifap) == 0) return 0; + int e = errno; + if (e != EACCES && e != EPERM && orig) return -1; + if (cortex_getifaddrs_fallback(ifap) != 0) { + errno = e; + return -1; + } + errno = saved; + return 0; +} + +struct if_nameindex *if_nameindex(void) { + static struct if_nameindex *(*orig)(void) = NULL; + if (!orig) orig = (struct if_nameindex *(*)(void))dlsym(RTLD_NEXT, "if_nameindex"); + int saved = errno; + struct if_nameindex *res = orig ? orig() : NULL; + if (res) return res; + int e = errno; + if (e != EACCES && e != EPERM && orig) return NULL; + // The interfaces with an address (an app is not told of the others), as glibc builds the list: + // each name malloc()ed, the array ended by {0, NULL}; its if_freenameindex() frees them + struct cx_addr_info *addrs = malloc(sizeof(struct cx_addr_info) * CX_IFADDR_MAX); + if (!addrs) { + errno = ENOMEM; + return NULL; + } + int n = cx_netlink_addrs(addrs, CX_IFADDR_MAX); + if (n < 0) { + free(addrs); + errno = e; + return NULL; + } + int idx[CX_IFADDR_MAX]; + int nif = cx_collect_indexes(addrs, n, idx, CX_IFADDR_MAX); + free(addrs); + res = calloc((size_t)nif + 1, sizeof(struct if_nameindex)); + int sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); + if (!res || sock < 0) { + free(res); + if (sock >= 0) close(sock); + errno = ENOBUFS; + return NULL; + } + int k = 0; + for (int i = 0; i < nif; i++) { + char name[IF_NAMESIZE]; + unsigned int flags; + if (cx_if_name_flags(sock, idx[i], name, &flags) != 0) continue; + res[k].if_index = (unsigned int)idx[i]; + res[k].if_name = strdup(name); + if (!res[k].if_name) break; + k++; + } + close(sock); + errno = saved; + return res; +} + +#endif /* !CORTEX_HOST_TEST */ diff --git a/app/src/main/cpp/hook/net.c b/app/src/main/cpp/hook/net.c index bf201ea9..872c786d 100644 --- a/app/src/main/cpp/hook/net.c +++ b/app/src/main/cpp/hook/net.c @@ -805,6 +805,336 @@ static int synthesize_fallback_addrinfo(const char *node, const char *service, return EAI_NONAME; } +// Cortex's /etc/services. glibc reads /etc/services from inside itself, and Android has none: every +// service name was unknown (getservbyname("https") failed, getaddrinfo(host, "ssh") was refused or, +// through the fallback above, gave port 80). Where glibc finds nothing they are answered from the +// rootfs's file. The line for `name` (or for `port`, in host order, when name is NULL) and `proto` +// (any when NULL): its fields into ret, its strings into buf. 0, ERANGE (buf too small) or ENOENT. +static int cx_fill_servent(const char *name, int port, const char *proto, struct servent *ret, char *buf, size_t buflen) { + if (!ret || !buf || !g_cortex_root[0]) return ENOENT; + char path[PATH_MAX]; + if (snprintf(path, sizeof(path), "%s/etc/services", g_cortex_root) >= (int)sizeof(path)) return ENOENT; + FILE *f = fopen(path, "re"); + if (!f) return ENOENT; + char line[1024]; + int rc = ENOENT; + while (fgets(line, sizeof(line), f)) { + char *hash = strchr(line, '#'); + if (hash) *hash = '\0'; + char *save = NULL; + char *sname = strtok_r(line, " \t\r\n", &save); + char *pp = sname ? strtok_r(NULL, " \t\r\n", &save) : NULL; + if (!pp) continue; + char *slash = strchr(pp, '/'); + if (!slash) continue; + *slash = '\0'; + char *sproto = slash + 1; + char *end = NULL; + long sport = strtol(pp, &end, 10); + if (!end || *end || sport <= 0 || sport > 65535) continue; + if (proto && strcmp(proto, sproto) != 0) continue; + char *aliases[32]; + int na = 0; + int match = name ? strcmp(sname, name) == 0 : sport == port; + for (char *a = strtok_r(NULL, " \t\r\n", &save); a && na < 31; a = strtok_r(NULL, " \t\r\n", &save)) { + aliases[na++] = a; + if (name && strcmp(a, name) == 0) match = 1; + } + if (!match) continue; + // buf: the alias pointers, then the strings + size_t pad = (sizeof(char *) - ((uintptr_t)buf % sizeof(char *))) % sizeof(char *); + size_t need = pad + sizeof(char *) * (size_t)(na + 1) + strlen(sname) + 1 + strlen(sproto) + 1; + for (int i = 0; i < na; i++) need += strlen(aliases[i]) + 1; + if (need > buflen) { + rc = ERANGE; + break; + } + char **list = (char **)(void *)(buf + pad); + char *p = (char *)(list + na + 1); + ret->s_name = p; + p = stpcpy(p, sname) + 1; + ret->s_proto = p; + p = stpcpy(p, sproto) + 1; + for (int i = 0; i < na; i++) { + list[i] = p; + p = stpcpy(p, aliases[i]) + 1; + } + list[na] = NULL; + ret->s_aliases = list; + ret->s_port = (int)htons((uint16_t)sport); + rc = 0; + break; + } + fclose(f); + return rc; +} + +#define CX_SERVENT_BUF 4096 + +struct servent *getservbyname(const char *name, const char *proto) { + static struct servent *(*orig)(const char *, const char *) = NULL; + if (!orig) orig = (struct servent *(*)(const char *, const char *))dlsym(RTLD_NEXT, "getservbyname"); + struct servent *r = orig ? orig(name, proto) : NULL; + if (r || !name) return r; + static __thread struct servent se; + static __thread char buf[CX_SERVENT_BUF]; + init_cortex_hook(); + return cx_fill_servent(name, 0, proto, &se, buf, sizeof(buf)) == 0 ? &se : NULL; +} + +struct servent *getservbyport(int port, const char *proto) { + static struct servent *(*orig)(int, const char *) = NULL; + if (!orig) orig = (struct servent *(*)(int, const char *))dlsym(RTLD_NEXT, "getservbyport"); + struct servent *r = orig ? orig(port, proto) : NULL; + if (r) return r; + static __thread struct servent se; + static __thread char buf[CX_SERVENT_BUF]; + init_cortex_hook(); + return cx_fill_servent(NULL, ntohs((uint16_t)port), proto, &se, buf, sizeof(buf)) == 0 ? &se : NULL; +} + +int getservbyname_r(const char *name, const char *proto, struct servent *ret, char *buf, size_t buflen, struct servent **result) { + static int (*orig)(const char *, const char *, struct servent *, char *, size_t, struct servent **) = NULL; + if (!orig) orig = (int (*)(const char *, const char *, struct servent *, char *, size_t, struct servent **))dlsym(RTLD_NEXT, "getservbyname_r"); + int rc = orig ? orig(name, proto, ret, buf, buflen, result) : ENOENT; + if ((rc == 0 && result && *result) || rc == ERANGE || !name || !result) return rc; + init_cortex_hook(); + int fill = cx_fill_servent(name, 0, proto, ret, buf, buflen); + if (fill == 0) { + *result = ret; + return 0; + } + *result = NULL; + return fill == ERANGE ? ERANGE : rc; +} + +int getservbyport_r(int port, const char *proto, struct servent *ret, char *buf, size_t buflen, struct servent **result) { + static int (*orig)(int, const char *, struct servent *, char *, size_t, struct servent **) = NULL; + if (!orig) orig = (int (*)(int, const char *, struct servent *, char *, size_t, struct servent **))dlsym(RTLD_NEXT, "getservbyport_r"); + int rc = orig ? orig(port, proto, ret, buf, buflen, result) : ENOENT; + if ((rc == 0 && result && *result) || rc == ERANGE || !result) return rc; + init_cortex_hook(); + int fill = cx_fill_servent(NULL, ntohs((uint16_t)port), proto, ret, buf, buflen); + if (fill == 0) { + *result = ret; + return 0; + } + *result = NULL; + return fill == ERANGE ? ERANGE : rc; +} + +// Cortex's /etc/protocols, for the same reason as /etc/services: Android has none, so +// getprotobyname("tcp") failed (Python's socket.getprotobyname(), PHP's, getent protocols). +// The line for `name` (or for `number` when name is NULL): 0, ERANGE or ENOENT. +static int cx_fill_protoent(const char *name, int number, struct protoent *ret, char *buf, size_t buflen) { + if (!ret || !buf || !g_cortex_root[0]) return ENOENT; + char path[PATH_MAX]; + if (snprintf(path, sizeof(path), "%s/etc/protocols", g_cortex_root) >= (int)sizeof(path)) return ENOENT; + FILE *f = fopen(path, "re"); + if (!f) return ENOENT; + char line[1024]; + int rc = ENOENT; + while (fgets(line, sizeof(line), f)) { + char *hash = strchr(line, '#'); + if (hash) *hash = '\0'; + char *save = NULL; + char *pname = strtok_r(line, " \t\r\n", &save); + char *num = pname ? strtok_r(NULL, " \t\r\n", &save) : NULL; + if (!num) continue; + char *end = NULL; + long pnum = strtol(num, &end, 10); + if (!end || *end || pnum < 0 || pnum > 255) continue; + char *aliases[32]; + int na = 0; + int match = name ? strcmp(pname, name) == 0 : pnum == number; + for (char *a = strtok_r(NULL, " \t\r\n", &save); a && na < 31; a = strtok_r(NULL, " \t\r\n", &save)) { + aliases[na++] = a; + if (name && strcmp(a, name) == 0) match = 1; + } + if (!match) continue; + size_t pad = (sizeof(char *) - ((uintptr_t)buf % sizeof(char *))) % sizeof(char *); + size_t need = pad + sizeof(char *) * (size_t)(na + 1) + strlen(pname) + 1; + for (int i = 0; i < na; i++) need += strlen(aliases[i]) + 1; + if (need > buflen) { + rc = ERANGE; + break; + } + char **list = (char **)(void *)(buf + pad); + char *p = (char *)(list + na + 1); + ret->p_name = p; + p = stpcpy(p, pname) + 1; + for (int i = 0; i < na; i++) { + list[i] = p; + p = stpcpy(p, aliases[i]) + 1; + } + list[na] = NULL; + ret->p_aliases = list; + ret->p_proto = (int)pnum; + rc = 0; + break; + } + fclose(f); + return rc; +} + +struct protoent *getprotobyname(const char *name) { + static struct protoent *(*orig)(const char *) = NULL; + if (!orig) orig = (struct protoent *(*)(const char *))dlsym(RTLD_NEXT, "getprotobyname"); + struct protoent *r = orig ? orig(name) : NULL; + if (r || !name) return r; + static __thread struct protoent pe; + static __thread char buf[CX_SERVENT_BUF]; + init_cortex_hook(); + return cx_fill_protoent(name, 0, &pe, buf, sizeof(buf)) == 0 ? &pe : NULL; +} + +struct protoent *getprotobynumber(int proto) { + static struct protoent *(*orig)(int) = NULL; + if (!orig) orig = (struct protoent *(*)(int))dlsym(RTLD_NEXT, "getprotobynumber"); + struct protoent *r = orig ? orig(proto) : NULL; + if (r) return r; + static __thread struct protoent pe; + static __thread char buf[CX_SERVENT_BUF]; + init_cortex_hook(); + return cx_fill_protoent(NULL, proto, &pe, buf, sizeof(buf)) == 0 ? &pe : NULL; +} + +int getprotobyname_r(const char *name, struct protoent *ret, char *buf, size_t buflen, struct protoent **result) { + static int (*orig)(const char *, struct protoent *, char *, size_t, struct protoent **) = NULL; + if (!orig) orig = (int (*)(const char *, struct protoent *, char *, size_t, struct protoent **))dlsym(RTLD_NEXT, "getprotobyname_r"); + int rc = orig ? orig(name, ret, buf, buflen, result) : ENOENT; + if ((rc == 0 && result && *result) || rc == ERANGE || !name || !result) return rc; + init_cortex_hook(); + int fill = cx_fill_protoent(name, 0, ret, buf, buflen); + if (fill == 0) { + *result = ret; + return 0; + } + *result = NULL; + return fill == ERANGE ? ERANGE : rc; +} + +int getprotobynumber_r(int proto, struct protoent *ret, char *buf, size_t buflen, struct protoent **result) { + static int (*orig)(int, struct protoent *, char *, size_t, struct protoent **) = NULL; + if (!orig) orig = (int (*)(int, struct protoent *, char *, size_t, struct protoent **))dlsym(RTLD_NEXT, "getprotobynumber_r"); + int rc = orig ? orig(proto, ret, buf, buflen, result) : ENOENT; + if ((rc == 0 && result && *result) || rc == ERANGE || !result) return rc; + init_cortex_hook(); + int fill = cx_fill_protoent(NULL, proto, ret, buf, buflen); + if (fill == 0) { + *result = ret; + return 0; + } + *result = NULL; + return fill == ERANGE ? ERANGE : rc; +} + +// The port of a service name for getaddrinfo(): from Cortex's /etc/services, for the protocol of the +// socket type (tcp, then udp, for any). -1 when unknown. +static int rootfs_service_port(const char *service, int socktype) { + struct servent se; + char buf[CX_SERVENT_BUF]; + const char *protos[2] = { socktype == SOCK_DGRAM ? "udp" : "tcp", socktype == 0 ? "udp" : NULL }; + for (int i = 0; i < 2 && protos[i]; i++) { + if (cx_fill_servent(service, 0, protos[i], &se, buf, sizeof(buf)) == 0) return ntohs((uint16_t)se.s_port); + } + return -1; +} + +// Cortex's /etc/hosts. glibc reads Android's (it opens /etc/hosts from inside itself, where the hook +// does not reach), so names added to the rootfs's were never found. The addresses of `name`, in +// the order of the file, at most max: each as text into out[i]; *canon gets the line's first name. +#define CX_HOSTS_MAX 16 +static int rootfs_hosts_lookup(const char *name, char out[][INET6_ADDRSTRLEN], int max, char *canon, size_t canon_size) { + if (!name || !*name || !g_cortex_root[0]) return 0; + char path[PATH_MAX]; + if (snprintf(path, sizeof(path), "%s/etc/hosts", g_cortex_root) >= (int)sizeof(path)) return 0; + int fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd < 0) return 0; + size_t cap = 1 << 20, len = 0; // a hosts file larger than 1 MiB is read up to there + char *text = malloc(cap + 1); + if (!text) { + close(fd); + return 0; + } + for (;;) { + ssize_t r = read(fd, text + len, cap - len); + if (r < 0 && errno == EINTR) continue; + if (r <= 0) break; + len += (size_t)r; + if (len == cap) break; + } + close(fd); + text[len] = '\0'; + int n = 0; + char *save_line = NULL; + for (char *line = strtok_r(text, "\n", &save_line); line && n < max; line = strtok_r(NULL, "\n", &save_line)) { + char *hash = strchr(line, '#'); + if (hash) *hash = '\0'; + char *save_tok = NULL; + char *addr = strtok_r(line, " \t\r", &save_tok); + if (!addr) continue; + unsigned char bin[16]; + if (inet_pton(AF_INET, addr, bin) != 1 && inet_pton(AF_INET6, addr, bin) != 1) continue; + const char *first = NULL; + int match = 0; + for (char *h = strtok_r(NULL, " \t\r", &save_tok); h; h = strtok_r(NULL, " \t\r", &save_tok)) { + if (!first) first = h; + if (strcasecmp(h, name) == 0) match = 1; + } + if (!match) continue; + if (n == 0 && canon && canon_size) snprintf(canon, canon_size, "%s", first); + snprintf(out[n++], INET6_ADDRSTRLEN, "%s", addr); + } + free(text); + return n; +} + +// getaddrinfo() for a name of Cortex's /etc/hosts: glibc's own answer for each of its addresses +// (as numbers, so the socket types, ports and errors are glibc's), joined. EAI_NONAME when the file +// does not name it or no address fits the hints. +static int rootfs_hosts_addrinfo(int (*orig)(const char *, const char *, const struct addrinfo *, struct addrinfo **), + const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) { + char addrs[CX_HOSTS_MAX][INET6_ADDRSTRLEN]; + char canon[256] = ""; + int n = rootfs_hosts_lookup(node, addrs, CX_HOSTS_MAX, canon, sizeof(canon)); + if (n == 0 || !orig) return EAI_NONAME; + struct addrinfo h; + memcpy(&h, hints, sizeof(h)); + h.ai_flags |= AI_NUMERICHOST; + struct addrinfo *head = NULL, *tail = NULL; + int last = EAI_NONAME; + for (int i = 0; i < n; i++) { + struct addrinfo *part = NULL; + last = orig(addrs[i], service, &h, &part); + if (last != 0 || !part) { + if (last == EAI_SERVICE || last == EAI_SOCKTYPE || last == EAI_BADFLAGS) break; + continue; + } + if (!head) { + head = part; + if ((hints->ai_flags & AI_CANONNAME) && part->ai_canonname && canon[0]) { + char *c = strdup(canon); + if (c) { + free(part->ai_canonname); // glibc's freeaddrinfo() frees it with free() + part->ai_canonname = c; + } + } + } else { + if (part->ai_canonname) { + free(part->ai_canonname); + part->ai_canonname = NULL; + } + tail->ai_next = part; + } + tail = part; + while (tail->ai_next) tail = tail->ai_next; + } + if (!head) return last == 0 ? EAI_NONAME : last; + *res = head; + return 0; +} + int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) { @@ -824,6 +1154,33 @@ int getaddrinfo(const char *node, const char *service, mod_hints.ai_flags = 0; } + // A service name as its number from Cortex's /etc/services (glibc would look in Android's /etc, + // which has none) + char port_text[8]; + if (service && *service && !(mod_hints.ai_flags & AI_NUMERICSERV)) { + char *end = NULL; + (void)strtol(service, &end, 10); + if (end && *end) { + init_cortex_hook(); + int port = rootfs_service_port(service, mod_hints.ai_socktype); + if (port > 0) { + snprintf(port_text, sizeof(port_text), "%d", port); + service = port_text; + } + } + } + + // Cortex's /etc/hosts first, as nsswitch.conf's "files dns". localhost is left to glibc, which + // finds it in Android's /etc/hosts as before (127.0.0.1 only: servers that listen on 127.0.0.1 + // are still reached through it). + if (node && res && !(mod_hints.ai_flags & AI_NUMERICHOST) && strcasecmp(node, "localhost") != 0) { + unsigned char bin[16]; + if (inet_pton(AF_INET, node, bin) != 1 && inet_pton(AF_INET6, node, bin) != 1) { + init_cortex_hook(); + if (rootfs_hosts_addrinfo(orig_getaddrinfo, node, service, &mod_hints, res) == 0) return 0; + } + } + int ret = orig_getaddrinfo ? orig_getaddrinfo(node, service, &mod_hints, res) : EAI_FAIL; // A numeric-only lookup must never turn into a DNS query. if ((ret != 0 || !res || !*res) && node && !(hints && (hints->ai_flags & AI_NUMERICHOST))) { @@ -962,42 +1319,96 @@ int res_search(const char *dname, int class, int type, return orig_res_search ? orig_res_search(dname, class, type, answer, anslen) : -1; } -static struct hostent s_fallback_hostent; -static char *s_fallback_aliases[1] = { NULL }; -static in_addr_t s_fallback_addr1; -static in_addr_t s_fallback_addr2; -static char *s_fallback_addr_list[3] = { NULL, NULL, NULL }; - -static struct hostent *get_fallback_hostent(const char *name) { - if (!name) return NULL; - if (strstr(name, "ubuntu.com") != NULL) { - inet_pton(AF_INET, "91.189.91.103", &s_fallback_addr1); - inet_pton(AF_INET, "91.189.92.21", &s_fallback_addr2); - } else if (strstr(name, "debian.org") != NULL) { - inet_pton(AF_INET, "151.101.130.132", &s_fallback_addr1); - inet_pton(AF_INET, "151.101.2.132", &s_fallback_addr2); - } else { - return NULL; +// gethostbyname() and its kin. glibc reads /etc/resolv.conf and /etc/hosts from inside itself, +// where the hook does not reach: it reads Android's /etc (no resolv.conf there, so it asks +// 127.0.0.1, where nothing answers), and these failed for every name ("Host name lookup failure" +// from Python's socket.gethostbyname_ex(), getent hosts, Perl's and PHP's gethostbyname()). Where +// glibc finds nothing they answer from getaddrinfo() above, which knows Cortex's /etc/hosts and +// asks the network's DNS servers. +#define CX_HOSTENT_MAX_ADDRS 16 + +// Fills `ret` for `name` in the family `af` with buf as its storage. 0, ERANGE (buf too small), or +// ENOENT with *herr set (no address) +static int cx_fill_hostent(const char *name, int af, struct hostent *ret, char *buf, size_t buflen, int *herr) { + if (!name || !ret || !buf || (af != AF_INET && af != AF_INET6)) { + if (herr) *herr = HOST_NOT_FOUND; + return ENOENT; + } + struct addrinfo hints, *res = NULL; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = af; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_CANONNAME; + int gai = getaddrinfo(name, NULL, &hints, &res); + if (gai != 0 || !res) { + if (herr) *herr = gai == EAI_AGAIN ? TRY_AGAIN : HOST_NOT_FOUND; + return ENOENT; + } + size_t alen = af == AF_INET ? 4 : 16; + unsigned char addrs[CX_HOSTENT_MAX_ADDRS][16]; + int n = 0; + for (struct addrinfo *ai = res; ai && n < CX_HOSTENT_MAX_ADDRS; ai = ai->ai_next) { + if (ai->ai_family != af || !ai->ai_addr) continue; + const void *src = af == AF_INET ? (const void *)&((struct sockaddr_in *)ai->ai_addr)->sin_addr + : (const void *)&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr; + int dup = 0; + for (int i = 0; i < n; i++) { + if (memcmp(addrs[i], src, alen) == 0) dup = 1; + } + if (!dup) memcpy(addrs[n++], src, alen); } - s_fallback_addr_list[0] = (char *)&s_fallback_addr1; - s_fallback_addr_list[1] = (char *)&s_fallback_addr2; - s_fallback_addr_list[2] = NULL; + const char *canon = (res->ai_canonname && res->ai_canonname[0]) ? res->ai_canonname : name; + size_t canon_len = strlen(canon) + 1; + // buf: the aliases (none) and address pointers, the addresses, the name + size_t ptrs = sizeof(char *) * (size_t)(1 + n + 1); + size_t pad = (sizeof(char *) - ((uintptr_t)buf % sizeof(char *))) % sizeof(char *); + size_t need = pad + ptrs + alen * (size_t)n + canon_len; + if (n == 0) { + freeaddrinfo(res); + if (herr) *herr = NO_DATA; + return ENOENT; + } + if (buflen < need) { + freeaddrinfo(res); + if (herr) *herr = NETDB_INTERNAL; + return ERANGE; + } + char **aliases = (char **)(void *)(buf + pad); + char **list = aliases + 1; + char *data = (char *)(list + n + 1); + aliases[0] = NULL; + for (int i = 0; i < n; i++) { + memcpy(data + alen * (size_t)i, addrs[i], alen); + list[i] = data + alen * (size_t)i; + } + list[n] = NULL; + char *hname = data + alen * (size_t)n; + memcpy(hname, canon, canon_len); + freeaddrinfo(res); + ret->h_name = hname; + ret->h_aliases = aliases; + ret->h_addrtype = af; + ret->h_length = (int)alen; + ret->h_addr_list = list; + return 0; +} - s_fallback_hostent.h_name = (char *)name; - s_fallback_hostent.h_aliases = s_fallback_aliases; - s_fallback_hostent.h_addrtype = AF_INET; - s_fallback_hostent.h_length = sizeof(in_addr_t); - s_fallback_hostent.h_addr_list = s_fallback_addr_list; - return &s_fallback_hostent; +static struct hostent *cx_static_hostent(const char *name, int af) { + static __thread struct hostent he; + static __thread char buf[sizeof(char *) * (CX_HOSTENT_MAX_ADDRS + 3) + 16 * CX_HOSTENT_MAX_ADDRS + 1100]; + int herr = HOST_NOT_FOUND; + if (!name || strlen(name) > 1024 || cx_fill_hostent(name, af, &he, buf, sizeof(buf), &herr) != 0) return NULL; + return &he; } struct hostent *gethostbyname(const char *name) { static struct hostent *(*orig_gethostbyname)(const char *) = NULL; if (!orig_gethostbyname) orig_gethostbyname = (struct hostent *(*)(const char *))dlsym(RTLD_NEXT, "gethostbyname"); struct hostent *ret = orig_gethostbyname ? orig_gethostbyname(name) : NULL; - if (!ret && name && (strstr(name, "ubuntu.com") || strstr(name, "debian.org"))) { - return get_fallback_hostent(name); - } + if (ret || !name) return ret; + int saved_herr = h_errno; + ret = cx_static_hostent(name, AF_INET); + if (!ret) h_errno = saved_herr; return ret; } @@ -1005,10 +1416,55 @@ struct hostent *gethostbyname2(const char *name, int af) { static struct hostent *(*orig_gethostbyname2)(const char *, int) = NULL; if (!orig_gethostbyname2) orig_gethostbyname2 = (struct hostent *(*)(const char *, int))dlsym(RTLD_NEXT, "gethostbyname2"); struct hostent *ret = orig_gethostbyname2 ? orig_gethostbyname2(name, af) : NULL; - if (!ret && (af == AF_INET || af == AF_UNSPEC) && name && (strstr(name, "ubuntu.com") || strstr(name, "debian.org"))) { - return get_fallback_hostent(name); - } + if (ret || !name) return ret; + int saved_herr = h_errno; + ret = cx_static_hostent(name, af); + if (!ret) h_errno = saved_herr; return ret; } +int gethostbyname2_r(const char *name, int af, struct hostent *ret, char *buf, size_t buflen, + struct hostent **result, int *h_errnop) { + static int (*orig)(const char *, int, struct hostent *, char *, size_t, struct hostent **, int *) = NULL; + if (!orig) orig = (int (*)(const char *, int, struct hostent *, char *, size_t, struct hostent **, int *))dlsym(RTLD_NEXT, "gethostbyname2_r"); + int rc = orig ? orig(name, af, ret, buf, buflen, result, h_errnop) : ENOENT; + if ((rc == 0 && result && *result) || rc == ERANGE || !name || !result) return rc; + int herr = HOST_NOT_FOUND; + int fill = cx_fill_hostent(name, af, ret, buf, buflen, &herr); + if (fill == 0) { + *result = ret; + if (h_errnop) *h_errnop = 0; + return 0; + } + if (fill == ERANGE) { + *result = NULL; + if (h_errnop) *h_errnop = NETDB_INTERNAL; + errno = ERANGE; + return ERANGE; + } + return rc; +} + +int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, + struct hostent **result, int *h_errnop) { + static int (*orig)(const char *, struct hostent *, char *, size_t, struct hostent **, int *) = NULL; + if (!orig) orig = (int (*)(const char *, struct hostent *, char *, size_t, struct hostent **, int *))dlsym(RTLD_NEXT, "gethostbyname_r"); + int rc = orig ? orig(name, ret, buf, buflen, result, h_errnop) : ENOENT; + if ((rc == 0 && result && *result) || rc == ERANGE || !name || !result) return rc; + int herr = HOST_NOT_FOUND; + int fill = cx_fill_hostent(name, AF_INET, ret, buf, buflen, &herr); + if (fill == 0) { + *result = ret; + if (h_errnop) *h_errnop = 0; + return 0; + } + if (fill == ERANGE) { + *result = NULL; + if (h_errnop) *h_errnop = NETDB_INTERNAL; + errno = ERANGE; + return ERANGE; + } + return rc; +} + #endif /* !CORTEX_HOST_TEST */ diff --git a/app/src/main/cpp/hook/setup.c b/app/src/main/cpp/hook/setup.c index 44871a0f..ecaaa4f3 100644 --- a/app/src/main/cpp/hook/setup.c +++ b/app/src/main/cpp/hook/setup.c @@ -68,6 +68,37 @@ static void init_cortex_hook(void) { cortex_libc_setenv("CURL_CA_BUNDLE", curl_buf, 1); } + // glibc loads its character set converters (gconv: ISO-8859-1, CP1254, UTF-16, ...) from + // /usr/lib//gconv with its own internal open() and dlopen(), which no hook sees: there + // is no such directory on Android, and every conversion but UTF-8's failed (iconv + // "conversion from `ISO-8859-1' is not supported", GTK "Error converting selection from + // STRING", vim and git with Latin-1 files). GCONV_PATH names the rootfs's, of this process's + // architecture: one inherited from a program of the other (armhf from arm64) is replaced, a + // directory of the user's own is kept. +#if defined(__aarch64__) + static const char gconv_rel[] = "/usr/lib/aarch64-linux-gnu/gconv"; +#elif defined(__arm__) + static const char gconv_rel[] = "/usr/lib/arm-linux-gnueabihf/gconv"; +#elif defined(__x86_64__) + static const char gconv_rel[] = "/usr/lib/x86_64-linux-gnu/gconv"; +#else + static const char gconv_rel[] = ""; +#endif + if (gconv_rel[0] != '\0') { + char gconv_buf[PATH_MAX]; + snprintf(gconv_buf, sizeof(gconv_buf), "%s%s", g_cortex_root, gconv_rel); + const char *curr_gconv = cortex_libc_getenv("GCONV_PATH"); + int set = !curr_gconv || curr_gconv[0] == '\0'; + if (!set && strcmp(curr_gconv, gconv_buf) != 0) { + // ours, for another architecture: "/usr/lib//gconv" + size_t rl = strlen(g_cortex_root), cl = strlen(curr_gconv); + set = strncmp(curr_gconv, g_cortex_root, rl) == 0 && strncmp(curr_gconv + rl, "/usr/lib/", 9) == 0 && + cl > rl + 9 + 6 && strcmp(curr_gconv + cl - 6, "/gconv") == 0 && + memchr(curr_gconv + rl + 9, '/', cl - rl - 9 - 6) == NULL; + } + if (set) cortex_libc_setenv("GCONV_PATH", gconv_buf, 1); + } + const char *curr_godebug = cortex_libc_getenv("GODEBUG"); if (!curr_godebug || curr_godebug[0] == '\0') { cortex_libc_setenv("GODEBUG", "netdns=cgo", 0); diff --git a/app/src/main/java/org/cortex/terminal/runtime/BootstrapManager.kt b/app/src/main/java/org/cortex/terminal/runtime/BootstrapManager.kt index 34243b72..8d9acdf2 100644 --- a/app/src/main/java/org/cortex/terminal/runtime/BootstrapManager.kt +++ b/app/src/main/java/org/cortex/terminal/runtime/BootstrapManager.kt @@ -12,7 +12,7 @@ import java.util.zip.GZIPInputStream object BootstrapManager { private const val TAG = "BootstrapManager" - const val CURRENT_BOOTSTRAP_VERSION = 12902 + const val CURRENT_BOOTSTRAP_VERSION = 12903 fun initializeFileSystem(context: Context) { val root = Environment.getCortexRoot(context) diff --git a/app/src/main/java/org/cortex/terminal/runtime/ShellScriptsInstaller.kt b/app/src/main/java/org/cortex/terminal/runtime/ShellScriptsInstaller.kt index 2ff0a6d0..c38d8ee8 100644 --- a/app/src/main/java/org/cortex/terminal/runtime/ShellScriptsInstaller.kt +++ b/app/src/main/java/org/cortex/terminal/runtime/ShellScriptsInstaller.kt @@ -322,6 +322,7 @@ object ShellScriptsInstaller { "notify-send" to "notify-send.sh", "cortex-sshd" to "cortex-sshd.sh", "cortex-desktop" to "cortex-desktop.sh", + "bwrap" to "bwrap.sh", "termux-clipboard-get" to "termux-clipboard.sh", "termux-clipboard-set" to "termux-clipboard.sh" ) diff --git a/fastlane/metadata/android/en-US/changelogs/12903.txt b/fastlane/metadata/android/en-US/changelogs/12903.txt new file mode 100644 index 00000000..5531db51 --- /dev/null +++ b/fastlane/metadata/android/en-US/changelogs/12903.txt @@ -0,0 +1,5 @@ +- The desktop is no longer black: icons, panel and programs load again +- Character set conversions (Latin-1, Windows-1254, UTF-16) work in iconv, vim, git, PHP and the clipboard +- uptime, vmstat, ps, top and Node os.cpus() work (Android hides parts of /proc) +- Node os.networkInterfaces() and hostname -I work again (Vite, Next.js dev servers) +- /etc/hosts entries, service names (https, ssh) and gethostbyname work diff --git a/tests/e2e/compat.sh b/tests/e2e/compat.sh index 25cb0e50..356ddcd6 100644 --- a/tests/e2e/compat.sh +++ b/tests/e2e/compat.sh @@ -127,8 +127,9 @@ package redis "redis-server" "PONG" \ 'redis-server --port 6390 --daemonize yes --save "" >/dev/null && sleep 2 && redis-cli -p 6390 ping; redis-cli -p 6390 shutdown nosave >/dev/null 2>&1' package nginx "nginx-light curl" "hello from nginx" \ 'mkdir -p ng && echo "hello from nginx" > ng/index.html && printf "daemon on; pid %s/ng/pid; error_log %s/ng/err; events {} http { access_log off; server { listen 8088; root %s/ng; } }\n" "$PWD" "$PWD" "$PWD" > ng/conf && nginx -c "$PWD/ng/conf" && sleep 1 && curl -s http://127.0.0.1:8088/; nginx -c "$PWD/ng/conf" -s stop' +# The administrator of a new MariaDB is the system's root (unix_socket), as on Ubuntu: sudo mariadb package mariadb "mariadb-server" "42" \ - '(mariadbd-safe --no-watch >/dev/null 2>&1 &); for i in $(seq 1 90); do mariadb -N -e "select 1" >/dev/null 2>&1 && break; sleep 1; done; mariadb -N -e "create database t; use t; create table x(a int); insert into x values (41); select a+1 from x"; s=$?; mariadb-admin shutdown >/dev/null 2>&1; exit $s' + '(sudo mariadbd-safe --no-watch >/dev/null 2>&1 &); for i in $(seq 1 90); do sudo mariadb -N -e "select 1" >/dev/null 2>&1 && break; sleep 1; done; sudo mariadb -N -e "create database t; use t; create table x(a int); insert into x values (41); select a+1 from x"; s=$?; sudo mariadb-admin shutdown >/dev/null 2>&1; exit $s' package postgresql-client "postgresql-client" "psql (PostgreSQL)" 'psql --version' # As on Ubuntu: the package's cluster, started as a service, and psql as the postgres user. It installs, # but the server refuses to run as root and Cortex's root is only pretended (pg_createcluster stops at diff --git a/tests/e2e/in-terminal.sh b/tests/e2e/in-terminal.sh index 0e957959..3285a059 100644 --- a/tests/e2e/in-terminal.sh +++ b/tests/e2e/in-terminal.sh @@ -130,6 +130,19 @@ expect users-runuser "nobody 65534:65534 root" bash -c 'u=$(sudo runuser -u nobo expect users-no-fakeroot "cortex yes root" bash -c 'cortex-api no-fakeroot bash -c "echo \$(id -un) \$([ \$(id -u) -ne 0 ] && echo yes) \$(sudo id -un)"' check users-chown bash -c 'f=$(mktemp) && sudo chown nobody:nogroup "$f" && [ "$(stat -c %U:%G "$f")" = nobody:nogroup ]; s=$?; rm -f "$f"; exit $s' check sha256sum bash -c 'echo hi | sha256sum | grep -q ^98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4' +# glibc's character set converters (gconv modules, loaded by glibc itself: GCONV_PATH), also in a +# program started with an empty environment +check iconv bash -c '[ "$(printf "caf\xe9" | iconv -f ISO-8859-1 -t UTF-8)" = "café" ] && [ "$(printf "\xfd\xf0" | env -i /usr/bin/iconv -f CP1254 -t UTF-8)" = "ığ" ]' +# bubblewrap answers that there are no namespaces in words GTK's image loader (glycin) knows: else it +# took the sandbox for available, no icon loaded and XFCE's session ended (a black desktop) +check bwrap-no-namespaces bash -c '! bwrap --unshare-all --die-with-parent /usr/bin/true 2>/tmp/bwrap.err && grep -q "No permissions to create a new namespace" /tmp/bwrap.err' +# Files of /proc that Android keeps from apps, made by the hook where the kernel refuses them +check proc-files bash -c 'read -r up _ < /proc/uptime && [ "${up%%.*}" -gt 0 ] && grep -q "^btime [0-9]" /proc/stat && [ "$(grep -c "^cpu[0-9]" /proc/stat)" -ge 1 ] && read -r l1 _ < /proc/loadavg && [ -n "$l1" ] && [ "$(cat /proc/sys/kernel/overflowuid)" = 65534 ] && { ! command -v uptime >/dev/null || uptime | grep -q "load average"; }' +# The network interfaces and addresses (glibc's getifaddrs() asks in ways Android refuses apps) +check network-interfaces bash -c 'hostname -I | grep -qE "[0-9a-f]" && { ! command -v python3 >/dev/null || python3 -c "import socket; assert socket.if_nameindex()"; }' +# Cortex's /etc/hosts, /etc/services and /etc/protocols (glibc reads Android's /etc from inside itself; +# the last two come with netbase, which the bootstrap does not have, as Ubuntu's base image does not) +check etc-hosts-services bash -c 'echo "10.11.12.13 cortex-e2e.test" | sudo tee -a /etc/hosts >/dev/null || { echo "cannot write /etc/hosts"; exit 1; }; getent ahosts cortex-e2e.test | grep -q 10.11.12.13 || { echo "getent ahosts: $(getent ahosts cortex-e2e.test)"; r=1; }; getent hosts cortex-e2e.test | grep -q 10.11.12.13 || { echo "getent hosts: $(getent hosts cortex-e2e.test)"; r=1; }; sudo sed -i "/cortex-e2e.test/d" /etc/hosts; [ -z "$r" ] || exit 1; if [ -f /etc/services ]; then getent services https | grep -q 443/tcp || { echo "getent services https: $(getent services https)"; exit 1; }; fi; if [ -f /etc/protocols ]; then getent protocols tcp | grep -qw 6 || { echo "getent protocols tcp: $(getent protocols tcp)"; exit 1; }; fi; if [ -f /etc/services ] && command -v python3 >/dev/null; then python3 -c "import socket; assert socket.getservbyname(\"ssh\") == 22; assert socket.getaddrinfo(\"127.0.0.1\", \"ssh\")[0][4][1] == 22"; fi' check shared-storage bash -c 'echo hi > "$0/written-by-ubuntu.txt" && [ "$(cat "$0/written-by-ubuntu.txt")" = hi ]' "$OUT" # skip NAME WHY: a check left out diff --git a/tests/scripts/test_shell_scripts.sh b/tests/scripts/test_shell_scripts.sh index e402d2cb..e427fddf 100644 --- a/tests/scripts/test_shell_scripts.sh +++ b/tests/scripts/test_shell_scripts.sh @@ -27,7 +27,7 @@ cleanup() { trap cleanup EXIT echo "=== 0. Checking LF line endings and syntax ===" -for script_name in reload.sh service.sh systemctl.sh systemd-sysusers.sh su.sh cortex-shell.sh cortex-backup.sh cortex-api.sh cortex-audio.sh notify-send.sh apt-autoupdate.sh cortex-sshd.sh cortex-desktop.sh termux-clipboard.sh; do +for script_name in reload.sh service.sh systemctl.sh systemd-sysusers.sh su.sh cortex-shell.sh cortex-backup.sh cortex-api.sh cortex-audio.sh notify-send.sh apt-autoupdate.sh cortex-sshd.sh cortex-desktop.sh termux-clipboard.sh bwrap.sh; do script_path="$ASSETS_SCRIPTS/$script_name" if LC_ALL=C grep -q $'\r' "$script_path"; then fail "$script_name contains CRLF line endings"