Skip to content

Commit

Permalink
fix: not properly switching mount ns
Browse files Browse the repository at this point in the history
This commit corrects mount namespace code.
  • Loading branch information
ThePedroo committed Aug 15, 2024
1 parent e79a695 commit d51acdd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 39 deletions.
1 change: 0 additions & 1 deletion module/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ androidComponents.onVariants { variant ->
val privKey = kf.generatePrivate(privKeySpec);
val sig = Signature.getInstance("ed25519")
fun File.sha(realFile: File? = null) {
val path = this.path.replace("\\", "/")
sig.update(this.name.toByteArray())
sig.update(0) // null-terminated string
val real = realFile ?: this
Expand Down
6 changes: 5 additions & 1 deletion zygiskd/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ int main(int argc, char *argv[]) {
}
}

switch_mount_namespace((pid_t)1);
if (switch_mount_namespace((pid_t)1) == false) {
LOGE("Failed to switch mount namespace\n");

return 1;
}
root_impls_setup();
zygiskd_start();

Expand Down
63 changes: 27 additions & 36 deletions zygiskd/src/utils.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
Expand All @@ -15,42 +16,28 @@

#include "utils.h"

void switch_mount_namespace(pid_t pid) {
char current_path[PATH_MAX];
if (getcwd(current_path, PATH_MAX) == NULL) {
/* TODO: Improve error messages */
LOGE("getcwd: %s\n", strerror(errno));

return;
}

/* INFO: We will NEVER achieve PATH_MAX value, but this is for ensurance. */
bool switch_mount_namespace(pid_t pid) {
char path[PATH_MAX];
snprintf(path, PATH_MAX, "/proc/%d/ns/mnt", pid);
snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid);

FILE *mnt_ns = fopen(path, "r");
if (mnt_ns == NULL) {
/* TODO: Improve error messages */
LOGE("fopen: %s\n", strerror(errno));
int nsfd = open(path, O_RDONLY | O_CLOEXEC);
if (nsfd == -1) {
LOGE("Failed to open nsfd: %s\n", strerror(errno));

return;
return false;
}

if (setns(fileno(mnt_ns), 0) == -1) {
/* TODO: Improve error messages */
LOGE("setns: %s\n", strerror(errno));
if (setns(nsfd, CLONE_NEWNS) == -1) {
LOGE("Failed to setns: %s\n", strerror(errno));

return;
}
close(nsfd);

fclose(mnt_ns);
return false;
}

if (chdir(current_path) == -1) {
/* TODO: Improve error messages */
LOGE("chdir: %s\n", strerror(errno));
close(nsfd);

return;
}
return true;
}

int __system_property_get(const char *, char *);
Expand All @@ -63,35 +50,39 @@ void set_socket_create_context(const char *context) {
char path[PATH_MAX];
snprintf(path, PATH_MAX, "/proc/thread-self/attr/sockcreate");

FILE *sockcreate = fopen(path, "w");
if (sockcreate == NULL) {
LOGE("fopen: %s\n", strerror(errno));
int sockcreate = open(path, O_CLOEXEC);
if (sockcreate == -1) {
LOGE("Failed to open sockcreate: %s\n", strerror(errno));
errno = 0;

return;
}

if (fwrite(context, 1, strlen(context), sockcreate) != strlen(context)) {
if (write(context, 1, strlen(context), sockcreate) != strlen(context)) {
LOGE("fwrite: %s\n", strerror(errno));
errno = 0;

return;
}

fclose(sockcreate);
close(sockcreate);
}

static void get_current_attr(char *output) {
char path[PATH_MAX];
snprintf(path, PATH_MAX, "/proc/self/attr/current");

FILE *current = fopen(path, "r");
if (current == NULL) {
LOGE("fopen: %s\n", strerror(errno));
int current = open(path, O_RDONLY | O_CLOEXEC);
if (current == -1) {
LOGE("Failed to open current: %s\n", strerror(errno));
errno = 0;

return;
}

if (fgets(output, PATH_MAX, current) == NULL) {
if (fgets(output, PATH_MAX, fileno(current)) == NULL) {
LOGE("fgets: %s\n", strerror(errno));
errno = 0;

return;
}
Expand Down
2 changes: 1 addition & 1 deletion zygiskd/src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
__android_log_print(ANDROID_LOG_INFO , lp_select("zygiskd32", "zygiskd64"), __VA_ARGS__); \
printf(__VA_ARGS__)

void switch_mount_namespace(pid_t pid);
bool switch_mount_namespace(pid_t pid);

void get_property(const char *name, char *output);

Expand Down

0 comments on commit d51acdd

Please sign in to comment.