From 3dee2eb486c7205222453adf2eb90a4147b732c5 Mon Sep 17 00:00:00 2001 From: tareksander <57038324+tareksander@users.noreply.github.com> Date: Wed, 8 Dec 2021 10:29:13 +0100 Subject: [PATCH] Changed: Allow connections from root o sockets. --- .../src/main/cpp/local-filesystem-socket.cpp | 1 + .../shared/shell/LocalFilesystemSocket.java | 24 +++++++++++-------- .../shared/shell/LocalSocketListener.java | 1 - 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/termux-shared/src/main/cpp/local-filesystem-socket.cpp b/termux-shared/src/main/cpp/local-filesystem-socket.cpp index eb0ca32b..4c5bc3e1 100644 --- a/termux-shared/src/main/cpp/local-filesystem-socket.cpp +++ b/termux-shared/src/main/cpp/local-filesystem-socket.cpp @@ -188,6 +188,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_termux_shared_shell_LocalFilesystemSocket_getpeeruid(JNIEnv *env, jclass clazz, jint fd) { struct ucred cred = {}; + cred.uid = 1; // initialize uid to 1 here because I'm paranoid and a failed getsockopt that somehow doesn't report as failed would report the uid of root socklen_t len = sizeof(cred); if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) == -1) { return -1; diff --git a/termux-shared/src/main/java/com/termux/shared/shell/LocalFilesystemSocket.java b/termux-shared/src/main/java/com/termux/shared/shell/LocalFilesystemSocket.java index 3a4db47c..951e668c 100644 --- a/termux-shared/src/main/java/com/termux/shared/shell/LocalFilesystemSocket.java +++ b/termux-shared/src/main/java/com/termux/shared/shell/LocalFilesystemSocket.java @@ -209,16 +209,20 @@ public class LocalFilesystemSocket if (backlog <= 0) { throw new IllegalArgumentException("Backlog has to be at least 1"); } - if (path == null) { - throw new IllegalArgumentException("path cannot be null"); + if (path == null || path.length() == 0) { + throw new IllegalArgumentException("path cannot be null or empty"); } this.path = path; - File f = new File(path); - File parent = f.getParentFile(); - if (parent != null) { - parent.mkdirs(); + if (path.getBytes(StandardCharsets.UTF_8)[0] != 0) { + // not a socket in the abstract linux namespace, make sure the path is accessible and clear + File f = new File(path); + File parent = f.getParentFile(); + if (parent != null) { + parent.mkdirs(); + } + f.delete(); } - f.delete(); + fd = createserversocket(path.getBytes(StandardCharsets.UTF_8), backlog); if (fd == -1) { throw new IOException("Could not create UNIX server socket at \""+path+"\""); @@ -245,9 +249,9 @@ public class LocalFilesystemSocket c = -1; continue; } - - if (peeruid == app.getApplicationInfo().uid) { - // if the peer has the same uid, allow the connection + + // if the peer has the same uid or is root, allow the connection + if (peeruid == app.getApplicationInfo().uid || peeruid == 0) { break; } else { Logger.logWarn("LocalFilesystemSocket.ServerSocket", "WARNING: An app with the uid of "+peeruid+" tried to connect to the socket at \""+path+"\", closing connection."); diff --git a/termux-shared/src/main/java/com/termux/shared/shell/LocalSocketListener.java b/termux-shared/src/main/java/com/termux/shared/shell/LocalSocketListener.java index 551562f2..19322bf3 100644 --- a/termux-shared/src/main/java/com/termux/shared/shell/LocalSocketListener.java +++ b/termux-shared/src/main/java/com/termux/shared/shell/LocalSocketListener.java @@ -77,7 +77,6 @@ public class LocalSocketListener while ((c = r.read()) > 0) { b.append((char) c); } - Logger.logDebug(LOG_TAG, b.toString()); String outString; String errString; int ret;