diff --git a/CHANGELOG.md b/CHANGELOG.md index 023c1de..f2880c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added + +- **Android Shell carries the SSH client** — `ssh` and `ssh-keygen` (the same + self-contained binaries the SSH feature ships) are now part of the device + shell's toolkit, so they run as ordinary commands in the shell. They come + from assets already in the APK, so there is nothing to download. + ## [v2.6.0] ### Added diff --git a/app/src/main/java/com/redtermapp/util/DeviceToolkit.kt b/app/src/main/java/com/redtermapp/util/DeviceToolkit.kt index 913c950..aecbbd0 100644 --- a/app/src/main/java/com/redtermapp/util/DeviceToolkit.kt +++ b/app/src/main/java/com/redtermapp/util/DeviceToolkit.kt @@ -4,8 +4,9 @@ import android.content.Context import java.io.File /** - * The optional toolkit behind the device shell: a dynamic busybox and a - * dynamic bash, per ABI, extracted into app storage. + * The optional toolkit behind the device shell: a dynamic busybox, a + * dynamic bash, and the app's own SSH client (ssh, ssh-keygen), per ABI, + * extracted into app storage. * * The device shell works without it — `/system/bin/sh` is always there. * The toolkit is what upgrades that to bash with awk, find, ps and the @@ -15,10 +16,12 @@ import java.io.File * which is the same shape as sftp and scp: optional binaries that * extract best-effort and can never fail the launch. * - * The binaries are dynamic bionic executables, never static: a static + * busybox and bash are dynamic bionic executables, never static: a static * NDK build aborts on start with the underaligned-TLS error, and the * build that produces them is checked for both properties before it is - * allowed into the assets. + * allowed into the assets. The SSH client is different: it is the app's + * own self-contained binary, shipped under `assets/ssh/` and copied in + * unchanged, so it needs no libraries of its own. */ object DeviceToolkit { @@ -27,11 +30,25 @@ object DeviceToolkit { * v2: the extraction now sets the exec bit on the binaries (v1 shipped * them 0644 and proot refused them); an existing v1 stamp must not * short-circuit the re-extraction. + * v3: the toolkit now also carries the app's own SSH client (ssh and + * ssh-keygen, extracted from the same assets the SSH feature uses), so + * the shell can reach it; a v2 stamp must re-extract to gain them. */ - const val TOOLKIT_VERSION = 2 + const val TOOLKIT_VERSION = 3 private const val STAMP_NAME = "version" + /** + * The SSH client the shell carries: the very binaries the SSH feature + * ships under `assets/ssh//`. They land in the toolkit directory, + * which the launcher already puts on PATH, so `ssh` and `ssh-keygen` + * become ordinary shell commands. Extracted best-effort (see [extract]): + * they are a bonus, and the toolkit still provides the shell without + * them. The list mirrors [SshClient.REQUIRED_PROGRAMS] so the shell and + * the SSH feature cannot drift apart. + */ + val SSH_TOOLS = listOf("ssh", "ssh-keygen") + /** * The two ABI vocabularies disagree, and the failure is a missing file: * assets use the build-openssh names, Build.SUPPORTED_ABIS the NDK @@ -86,9 +103,9 @@ object DeviceToolkit { } } - /** Copies the two binaries for [arch] out of the assets into [into], and - * links the busybox applets next to it so the extra tools are usable by - * name. + /** Copies the toolkit binaries (busybox, bash) and the app's SSH client + * for [arch] out of the assets into [into], and links the busybox + * applets next to it so the extra tools are usable by name. * * `busybox` is a single mult-call binary: `awk`, `find`, `ps` and the * rest are applets it dispatches on `argv[0]`. Shipping only the binary @@ -113,6 +130,22 @@ object DeviceToolkit { // the line the ssh install has and the toolkit build once lacked. bin.setExecutable(true, true) } + // The app's own SSH client, a bonus tool for the shell: copied in + // best-effort from the assets the SSH feature already ships, so its + // absence never sinks a toolkit that busybox and bash still provide. + // ssh and ssh-keygen are self-contained, so a plain copy is all they + // need; they run under proot's loader exactly like the rest. + for (name in SSH_TOOLS) { + val bin = File(into, name) + try { + context.assets.open("ssh/$assetArch/$name").use { input -> + bin.outputStream().use { output -> input.copyTo(output) } + } + bin.setExecutable(true, true) + } catch (e: Exception) { + AppLog.w(context, "device-toolkit", "optional ssh tool $name unavailable: ${e.message}") + } + } linkApplets(context, into) File(into, STAMP_NAME).writeText("${assetArch}:$TOOLKIT_VERSION") AppLog.i(context, "device-toolkit", "installed $assetArch into ${into.absolutePath}") diff --git a/app/src/test/java/com/redtermapp/util/DeviceToolkitTest.kt b/app/src/test/java/com/redtermapp/util/DeviceToolkitTest.kt index 344cb28..18c4e14 100644 --- a/app/src/test/java/com/redtermapp/util/DeviceToolkitTest.kt +++ b/app/src/test/java/com/redtermapp/util/DeviceToolkitTest.kt @@ -36,6 +36,15 @@ class DeviceToolkitTest { assertEquals(null, DeviceToolkit.assetArch(null)) } + @Test + fun `the shell carries exactly the required ssh client`() { + // The device shell's ssh tools must be the same set the SSH feature + // needs: if the two drifted, the shell would offer an ssh the rest of + // the app does not (or miss one it does). + assertEquals(SshClient.REQUIRED_PROGRAMS, DeviceToolkit.SSH_TOOLS) + assertTrue(DeviceToolkit.SSH_TOOLS.containsAll(listOf("ssh", "ssh-keygen"))) + } + @Test fun `the launcher runs proot to exec the toolkit bash`() { val script = DeviceToolkit.launcherScript( diff --git a/native/ssh_compat.c b/native/ssh_compat.c index e038118..cd2f3e6 100644 --- a/native/ssh_compat.c +++ b/native/ssh_compat.c @@ -15,16 +15,24 @@ * over the shared libc, so defining these here keeps every passwd lookup in the * process out of bionic's NSS. * - * The client only ever runs as the root user that proot maps the whole session - * to, in a rootfs that contains exactly one account, so a fixed struct is - * equivalent to reading /etc/passwd. A lookup for any other user correctly - * returns NULL, because no such user exists here. + * Two callers need an entry. The SSH client in its rootfs runs under -0, so + * it is uid 0 and its one account is root (home /root). The same binary in + * the device shell runs under the app's own uid, without -0; there ssh and + * ssh-keygen call getpwuid(getuid()) at startup and abort with "No user + * exists for uid N" before a single option is read if that lookup returns + * NULL. So a lookup for uid 0 returns the fixed root entry, and a lookup for + * any other uid returns a synthesised entry for that uid with its home taken + * from $HOME (the device shell points that at the app's writable storage). + * Neither reads /etc/passwd; the executable's own definitions are what + * bionic's NSS would have resolved to anyway. */ #include #include #include +#include #include #include +#include /* The volatile pointer keeps the compiler from optimising the wipe away. */ static void *(*volatile redterm_memset)(void *, int, size_t) = memset; @@ -40,36 +48,33 @@ explicit_bzero(void *p, size_t n) #define REDTERM_SHELL "/bin/sh" static int -redterm_passwd(struct passwd *pw, char *buf, size_t buflen) +redterm_passwd(struct passwd *pw, char *buf, size_t buflen, + uid_t uid, const char *name, const char *home) { - size_t need; - - if (pw == NULL) - return 0; - /* - * Room for the strings plus the alignment the caller's buffer is - * unlikely to provide. bionic's own version is allowed to fail when the - * buffer is too small, so keep that behaviour. + * The four strings (name, passwd "x", home, shell) plus their NULs, + * laid out contiguously in buf. bionic's own version is allowed to + * fail when the buffer is too small, so keep that behaviour. */ - need = sizeof("root") + sizeof(REDTERM_HOME) + sizeof(REDTERM_SHELL) + - sizeof("root") + 5 * sizeof(char *); - if (buf == NULL || buflen < need) + size_t need = strlen(name) + 1 + 2 + strlen(home) + 1 + + strlen(REDTERM_SHELL) + 1; + + if (pw == NULL || buf == NULL || buflen < need) return 0; memset(buf, 0, need); pw->pw_name = buf; - (void)strlcpy(buf, "root", buflen); - pw->pw_passwd = buf + strlen("root") + 1; + (void)strlcpy(pw->pw_name, name, buflen); + pw->pw_passwd = buf + strlen(name) + 1; (void)strlcpy(pw->pw_passwd, "x", buflen - (size_t)(pw->pw_passwd - buf)); pw->pw_gecos = pw->pw_passwd; pw->pw_dir = pw->pw_passwd + 2; - (void)strlcpy(pw->pw_dir, REDTERM_HOME, buflen - (size_t)(pw->pw_dir - buf)); - pw->pw_shell = pw->pw_dir + sizeof(REDTERM_HOME); + (void)strlcpy(pw->pw_dir, home, buflen - (size_t)(pw->pw_dir - buf)); + pw->pw_shell = pw->pw_dir + strlen(home) + 1; (void)strlcpy(pw->pw_shell, REDTERM_SHELL, buflen - (size_t)(pw->pw_shell - buf)); - pw->pw_uid = 0; - pw->pw_gid = 0; + pw->pw_uid = uid; + pw->pw_gid = uid; return 1; } @@ -77,12 +82,27 @@ int getpwuid_r(uid_t uid, struct passwd *pw, char *buf, size_t buflen, struct passwd **result) { + const char *name, *home; + if (result == NULL) return EINVAL; *result = NULL; - if (uid != 0) - return 0; /* no such user in this rootfs */ - if (!redterm_passwd(pw, buf, buflen)) + if (uid == 0) { + /* The one account the SSH rootfs carries. */ + name = "root"; + home = REDTERM_HOME; + } else { + /* + * The app's own uid, when the device shell runs these binaries + * without -0. Home is $HOME (the launcher points it at the + * app's writable storage); "." when it is unset, so the entry + * always has a home the caller can resolve. + */ + const char *h = getenv("HOME"); + name = "android"; + home = (h != NULL && *h != '\0') ? h : "."; + } + if (!redterm_passwd(pw, buf, buflen, uid, name, home)) return ERANGE; *result = pw; return 0; @@ -92,12 +112,29 @@ int getpwnam_r(const char *name, struct passwd *pw, char *buf, size_t buflen, struct passwd **result) { + const char *h; + if (result == NULL) return EINVAL; *result = NULL; - if (name == NULL || strcmp(name, "root") != 0) - return 0; /* no such user in this rootfs */ - if (!redterm_passwd(pw, buf, buflen)) + if (name == NULL) + return 0; + if (strcmp(name, "root") == 0) { + /* The one account the SSH rootfs carries. */ + if (!redterm_passwd(pw, buf, buflen, 0, "root", REDTERM_HOME)) + return ERANGE; + *result = pw; + return 0; + } + /* + * Any other name the caller asks for is, in this private environment, + * the process's own user. Resolve it to the current uid, with home + * from $HOME (or /root), so a lookup never aborts the session with a + * missing-user error the way the original did for the app uid. + */ + h = getenv("HOME"); + if (!redterm_passwd(pw, buf, buflen, getuid(), name, + (h != NULL && *h != '\0') ? h : REDTERM_HOME)) return ERANGE; *result = pw; return 0;