From 19d2a1758e9747c4718868bf67cd9786e77029a5 Mon Sep 17 00:00:00 2001 From: ThePedroo Date: Thu, 15 Aug 2024 21:09:44 -0300 Subject: [PATCH] fix: segmentation fault in write This commit fixes the segmentation fault when trying to write to /proc/.../sockcreate. --- zygiskd/src/utils.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/zygiskd/src/utils.c b/zygiskd/src/utils.c index 5527f6d..d5f9cec 100644 --- a/zygiskd/src/utils.c +++ b/zygiskd/src/utils.c @@ -50,22 +50,20 @@ void set_socket_create_context(const char *context) { char path[PATH_MAX]; snprintf(path, PATH_MAX, "/proc/thread-self/attr/sockcreate"); - int sockcreate = open(path, O_CLOEXEC); - if (sockcreate == -1) { - LOGE("Failed to open sockcreate: %s\n", strerror(errno)); - errno = 0; + FILE *sockcreate = fopen(path, "w"); + if (sockcreate == NULL) { + LOGE("Failed to open /proc/thread-self/attr/sockcreate: %s\n", strerror(errno)); return; } - if (write(sockcreate, context, strlen(context)) != (ssize_t)strlen(context)) { - LOGE("fwrite: %s\n", strerror(errno)); - errno = 0; + if (fwrite(context, 1, strlen(context), sockcreate) != strlen(context)) { + LOGE("Failed to write to /proc/thread-self/attr/sockcreate: %s\n", strerror(errno)); return; } - close(sockcreate); + fclose(sockcreate); } static void get_current_attr(char *output) { @@ -74,15 +72,13 @@ static void get_current_attr(char *output) { FILE *current = fopen(path, "r"); if (current == NULL) { - LOGE("Failed to open current: %s\n", strerror(errno)); - errno = 0; + LOGE("fopen: %s\n", strerror(errno)); return; } if (fgets(output, PATH_MAX, current) == NULL) { LOGE("fgets: %s\n", strerror(errno)); - errno = 0; return; }