RootShell: properly use errormessages

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-03-09 01:36:01 +08:00
parent 1839730663
commit 840c7ea560
4 changed files with 71 additions and 33 deletions

View File

@ -13,6 +13,7 @@ import androidx.annotation.Nullable;
import com.wireguard.android.Application; import com.wireguard.android.Application;
import com.wireguard.android.R; import com.wireguard.android.R;
import com.wireguard.android.backend.BackendException; import com.wireguard.android.backend.BackendException;
import com.wireguard.android.util.RootShell.RootShellException;
import com.wireguard.config.BadConfigException; import com.wireguard.config.BadConfigException;
import com.wireguard.config.BadConfigException.Location; import com.wireguard.config.BadConfigException.Location;
import com.wireguard.config.InetEndpoint; import com.wireguard.config.InetEndpoint;
@ -49,6 +50,14 @@ public final class ErrorMessages {
BackendException.Reason.TUN_CREATION_ERROR, R.string.tun_create_error, BackendException.Reason.TUN_CREATION_ERROR, R.string.tun_create_error,
BackendException.Reason.GO_ACTIVATION_ERROR_CODE, R.string.tunnel_on_error BackendException.Reason.GO_ACTIVATION_ERROR_CODE, R.string.tunnel_on_error
)); ));
private static final Map<RootShellException.Reason, Integer> RSE_REASON_MAP = new EnumMap<>(Maps.of(
RootShellException.Reason.NO_ROOT_ACCESS, R.string.error_root,
RootShellException.Reason.SHELL_MARKER_COUNT_ERROR, R.string.shell_marker_count_error,
RootShellException.Reason.SHELL_EXIT_STATUS_READ_ERROR, R.string.shell_exit_status_read_error,
RootShellException.Reason.SHELL_START_ERROR, R.string.shell_start_error,
RootShellException.Reason.CREATE_BIN_DIR_ERROR, R.string.create_bin_dir_error,
RootShellException.Reason.CREATE_TEMP_DIR_ERROR, R.string.create_temp_dir_error
));
private static final Map<Format, Integer> KFE_FORMAT_MAP = new EnumMap<>(Maps.of( private static final Map<Format, Integer> KFE_FORMAT_MAP = new EnumMap<>(Maps.of(
Format.BASE64, R.string.key_length_explanation_base64, Format.BASE64, R.string.key_length_explanation_base64,
Format.BINARY, R.string.key_length_explanation_binary, Format.BINARY, R.string.key_length_explanation_binary,
@ -89,6 +98,9 @@ public final class ErrorMessages {
} else if (rootCause instanceof BackendException) { } else if (rootCause instanceof BackendException) {
final BackendException be = (BackendException) rootCause; final BackendException be = (BackendException) rootCause;
message = resources.getString(BE_REASON_MAP.get(be.getReason()), be.getFormat()); message = resources.getString(BE_REASON_MAP.get(be.getReason()), be.getFormat());
} else if (rootCause instanceof RootShellException) {
final RootShellException rse = (RootShellException) rootCause;
message = resources.getString(RSE_REASON_MAP.get(rse.getReason()), rse.getFormat());
} else if (rootCause.getMessage() != null) { } else if (rootCause.getMessage() != null) {
message = rootCause.getMessage(); message = rootCause.getMessage();
} else { } else {
@ -135,7 +147,8 @@ public final class ErrorMessages {
private static Throwable rootCause(final Throwable throwable) { private static Throwable rootCause(final Throwable throwable) {
Throwable cause = throwable; Throwable cause = throwable;
while (cause.getCause() != null) { while (cause.getCause() != null) {
if (cause instanceof BadConfigException || cause instanceof BackendException) if (cause instanceof BadConfigException || cause instanceof BackendException ||
cause instanceof RootShellException)
break; break;
final Throwable nextCause = cause.getCause(); final Throwable nextCause = cause.getCause();
if (nextCause instanceof RemoteException) if (nextCause instanceof RemoteException)

View File

@ -10,7 +10,7 @@ import android.system.OsConstants;
import android.util.Base64; import android.util.Base64;
import com.wireguard.android.Application; import com.wireguard.android.Application;
import com.wireguard.android.util.RootShell.NoRootException; import com.wireguard.android.util.RootShell.RootShellException;
import net.i2p.crypto.eddsa.EdDSAEngine; import net.i2p.crypto.eddsa.EdDSAEngine;
import net.i2p.crypto.eddsa.EdDSAPublicKey; import net.i2p.crypto.eddsa.EdDSAPublicKey;
@ -55,7 +55,7 @@ public class ModuleLoader {
return moduleDir.exists() && moduleDir.isDirectory(); return moduleDir.exists() && moduleDir.isDirectory();
} }
public void loadModule() throws IOException, NoRootException { public void loadModule() throws IOException, RootShellException {
Application.getRootShell().run(null, String.format("insmod \"%s/wireguard-$(sha256sum /proc/version|cut -d ' ' -f 1).ko\"", moduleDir.getAbsolutePath())); Application.getRootShell().run(null, String.format("insmod \"%s/wireguard-$(sha256sum /proc/version|cut -d ' ' -f 1).ko\"", moduleDir.getAbsolutePath()));
} }
@ -122,7 +122,7 @@ public class ModuleLoader {
return hashes; return hashes;
} }
public Integer download() throws IOException, NoRootException, NoSuchAlgorithmException { public Integer download() throws IOException, RootShellException, NoSuchAlgorithmException {
final List<String> output = new ArrayList<>(); final List<String> output = new ArrayList<>();
Application.getRootShell().run(output, "sha256sum /proc/version|cut -d ' ' -f 1"); Application.getRootShell().run(output, "sha256sum /proc/version|cut -d ' ' -f 1");
if (output.size() != 1 || output.get(0).length() != 64) if (output.size() != 1 || output.get(0).length() != 64)

View File

@ -10,11 +10,10 @@ import androidx.annotation.Nullable;
import android.util.Log; import android.util.Log;
import com.wireguard.android.BuildConfig; import com.wireguard.android.BuildConfig;
import com.wireguard.android.R; import com.wireguard.android.util.RootShell.RootShellException.Reason;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
@ -30,8 +29,6 @@ public class RootShell {
private static final String SU = "su"; private static final String SU = "su";
private static final String TAG = "WireGuard/" + RootShell.class.getSimpleName(); private static final String TAG = "WireGuard/" + RootShell.class.getSimpleName();
private final Context context;
private final String deviceNotRootedMessage;
private final File localBinaryDir; private final File localBinaryDir;
private final File localTemporaryDir; private final File localTemporaryDir;
private final Object lock = new Object(); private final Object lock = new Object();
@ -42,12 +39,10 @@ public class RootShell {
@Nullable private BufferedReader stdout; @Nullable private BufferedReader stdout;
public RootShell(final Context context) { public RootShell(final Context context) {
deviceNotRootedMessage = context.getString(R.string.error_root);
localBinaryDir = new File(context.getCodeCacheDir(), "bin"); localBinaryDir = new File(context.getCodeCacheDir(), "bin");
localTemporaryDir = new File(context.getCacheDir(), "tmp"); localTemporaryDir = new File(context.getCacheDir(), "tmp");
preamble = String.format("export CALLING_PACKAGE=%s PATH=\"%s:$PATH\" TMPDIR='%s'; id -u\n", preamble = String.format("export CALLING_PACKAGE=%s PATH=\"%s:$PATH\" TMPDIR='%s'; id -u\n",
BuildConfig.APPLICATION_ID, localBinaryDir, localTemporaryDir); BuildConfig.APPLICATION_ID, localBinaryDir, localTemporaryDir);
this.context = context;
} }
private static boolean isExecutableInPath(final String name) { private static boolean isExecutableInPath(final String name) {
@ -83,7 +78,7 @@ public class RootShell {
* @return The exit value of the command. * @return The exit value of the command.
*/ */
public int run(@Nullable final Collection<String> output, final String command) public int run(@Nullable final Collection<String> output, final String command)
throws IOException, NoRootException { throws IOException, RootShellException {
synchronized (lock) { synchronized (lock) {
/* Start inside synchronized block to prevent a concurrent call to stop(). */ /* Start inside synchronized block to prevent a concurrent call to stop(). */
start(); start();
@ -122,24 +117,24 @@ public class RootShell {
} }
} }
if (markersSeen != 4) if (markersSeen != 4)
throw new IOException(context.getString(R.string.shell_marker_count_error, markersSeen)); throw new RootShellException(Reason.SHELL_MARKER_COUNT_ERROR, markersSeen);
if (errnoStdout != errnoStderr) if (errnoStdout != errnoStderr)
throw new IOException(context.getString(R.string.shell_exit_status_read_error)); throw new RootShellException(Reason.SHELL_EXIT_STATUS_READ_ERROR);
Log.v(TAG, "exit: " + errnoStdout); Log.v(TAG, "exit: " + errnoStdout);
return errnoStdout; return errnoStdout;
} }
} }
public void start() throws IOException, NoRootException { public void start() throws IOException, RootShellException {
if (!isExecutableInPath(SU)) if (!isExecutableInPath(SU))
throw new NoRootException(deviceNotRootedMessage); throw new RootShellException(Reason.NO_ROOT_ACCESS);
synchronized (lock) { synchronized (lock) {
if (isRunning()) if (isRunning())
return; return;
if (!localBinaryDir.isDirectory() && !localBinaryDir.mkdirs()) if (!localBinaryDir.isDirectory() && !localBinaryDir.mkdirs())
throw new FileNotFoundException(context.getString(R.string.create_bin_dir_error)); throw new RootShellException(Reason.CREATE_BIN_DIR_ERROR);
if (!localTemporaryDir.isDirectory() && !localTemporaryDir.mkdirs()) if (!localTemporaryDir.isDirectory() && !localTemporaryDir.mkdirs())
throw new FileNotFoundException(context.getString(R.string.create_temp_dir_error)); throw new RootShellException(Reason.CREATE_TEMP_DIR_ERROR);
try { try {
final ProcessBuilder builder = new ProcessBuilder().command(SU); final ProcessBuilder builder = new ProcessBuilder().command(SU);
builder.environment().put("LC_ALL", "C"); builder.environment().put("LC_ALL", "C");
@ -147,7 +142,9 @@ public class RootShell {
process = builder.start(); process = builder.start();
} catch (final IOException e) { } catch (final IOException e) {
// A failure at this stage means the device isn't rooted. // A failure at this stage means the device isn't rooted.
throw new NoRootException(deviceNotRootedMessage, e); final RootShellException rse = new RootShellException(Reason.NO_ROOT_ACCESS);
rse.initCause(e);
throw rse;
} }
stdin = new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8); stdin = new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8);
stdout = new BufferedReader(new InputStreamReader(process.getInputStream(), stdout = new BufferedReader(new InputStreamReader(process.getInputStream(),
@ -160,18 +157,18 @@ public class RootShell {
final String uid = stdout.readLine(); final String uid = stdout.readLine();
if (!"0".equals(uid)) { if (!"0".equals(uid)) {
Log.w(TAG, "Root check did not return correct UID: " + uid); Log.w(TAG, "Root check did not return correct UID: " + uid);
throw new NoRootException(deviceNotRootedMessage); throw new RootShellException(Reason.NO_ROOT_ACCESS);
} }
if (!isRunning()) { if (!isRunning()) {
String line; String line;
while ((line = stderr.readLine()) != null) { while ((line = stderr.readLine()) != null) {
Log.w(TAG, "Root check returned an error: " + line); Log.w(TAG, "Root check returned an error: " + line);
if (line.contains("Permission denied")) if (line.contains("Permission denied"))
throw new NoRootException(deviceNotRootedMessage); throw new RootShellException(Reason.NO_ROOT_ACCESS);
} }
throw new IOException(context.getString(R.string.shell_start_error, process.exitValue())); throw new RootShellException(Reason.SHELL_START_ERROR, process.exitValue());
} }
} catch (final IOException | NoRootException e) { } catch (final IOException | RootShellException e) {
stop(); stop();
throw e; throw e;
} }
@ -187,13 +184,29 @@ public class RootShell {
} }
} }
public static class NoRootException extends Exception { public static class RootShellException extends Exception {
public NoRootException(final String message, final Throwable cause) { public enum Reason {
super(message, cause); NO_ROOT_ACCESS,
SHELL_MARKER_COUNT_ERROR,
SHELL_EXIT_STATUS_READ_ERROR,
SHELL_START_ERROR,
CREATE_BIN_DIR_ERROR,
CREATE_TEMP_DIR_ERROR
} }
private final Reason reason;
public NoRootException(final String message) { private final Object[] format;
super(message); public RootShellException(final Reason reason, final Object ...format) {
this.reason = reason;
this.format = format;
}
public boolean isIORelated() {
return reason != Reason.NO_ROOT_ACCESS;
}
public Reason getReason() {
return reason;
}
public Object[] getFormat() {
return format;
} }
} }
} }

View File

@ -13,7 +13,7 @@ import android.util.Log;
import com.wireguard.android.Application; import com.wireguard.android.Application;
import com.wireguard.android.BuildConfig; import com.wireguard.android.BuildConfig;
import com.wireguard.android.R; import com.wireguard.android.R;
import com.wireguard.android.util.RootShell.NoRootException; import com.wireguard.android.util.RootShell.RootShellException;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -63,7 +63,7 @@ public final class ToolsInstaller {
return null; return null;
} }
public int areInstalled() throws NoRootException { public int areInstalled() throws RootShellException {
if (INSTALL_DIR == null) if (INSTALL_DIR == null)
return ERROR; return ERROR;
final StringBuilder script = new StringBuilder(); final StringBuilder script = new StringBuilder();
@ -81,6 +81,10 @@ public final class ToolsInstaller {
return willInstallAsMagiskModule() ? NO | MAGISK : NO | SYSTEM; return willInstallAsMagiskModule() ? NO | MAGISK : NO | SYSTEM;
} catch (final IOException ignored) { } catch (final IOException ignored) {
return ERROR; return ERROR;
} catch (final RootShellException e) {
if (e.isIORelated())
return ERROR;
throw e;
} }
} }
@ -102,11 +106,11 @@ public final class ToolsInstaller {
} }
} }
public int install() throws NoRootException, IOException { public int install() throws RootShellException, IOException {
return willInstallAsMagiskModule() ? installMagisk() : installSystem(); return willInstallAsMagiskModule() ? installMagisk() : installSystem();
} }
private int installMagisk() throws NoRootException, IOException { private int installMagisk() throws RootShellException, IOException {
extract(); extract();
final StringBuilder script = new StringBuilder("set -ex; "); final StringBuilder script = new StringBuilder("set -ex; ");
@ -125,10 +129,14 @@ public final class ToolsInstaller {
return Application.getRootShell().run(null, script.toString()) == 0 ? YES | MAGISK : ERROR; return Application.getRootShell().run(null, script.toString()) == 0 ? YES | MAGISK : ERROR;
} catch (final IOException ignored) { } catch (final IOException ignored) {
return ERROR; return ERROR;
} catch (final RootShellException e) {
if (e.isIORelated())
return ERROR;
throw e;
} }
} }
private int installSystem() throws NoRootException, IOException { private int installSystem() throws RootShellException, IOException {
if (INSTALL_DIR == null) if (INSTALL_DIR == null)
return OsConstants.ENOENT; return OsConstants.ENOENT;
extract(); extract();
@ -143,6 +151,10 @@ public final class ToolsInstaller {
return Application.getRootShell().run(null, script.toString()) == 0 ? YES | SYSTEM : ERROR; return Application.getRootShell().run(null, script.toString()) == 0 ? YES | SYSTEM : ERROR;
} catch (final IOException ignored) { } catch (final IOException ignored) {
return ERROR; return ERROR;
} catch (final RootShellException e) {
if (e.isIORelated())
return ERROR;
throw e;
} }
} }