fix: segmentation fault in write

This commit fixes the segmentation fault when trying to write to /proc/.../sockcreate.
This commit is contained in:
ThePedroo
2024-08-15 21:09:44 -03:00
parent 2ab6542a9a
commit 19d2a1758e

View File

@@ -50,22 +50,20 @@ void set_socket_create_context(const char *context) {
char path[PATH_MAX]; char path[PATH_MAX];
snprintf(path, PATH_MAX, "/proc/thread-self/attr/sockcreate"); snprintf(path, PATH_MAX, "/proc/thread-self/attr/sockcreate");
int sockcreate = open(path, O_CLOEXEC); FILE *sockcreate = fopen(path, "w");
if (sockcreate == -1) { if (sockcreate == NULL) {
LOGE("Failed to open sockcreate: %s\n", strerror(errno)); LOGE("Failed to open /proc/thread-self/attr/sockcreate: %s\n", strerror(errno));
errno = 0;
return; return;
} }
if (write(sockcreate, context, strlen(context)) != (ssize_t)strlen(context)) { if (fwrite(context, 1, strlen(context), sockcreate) != strlen(context)) {
LOGE("fwrite: %s\n", strerror(errno)); LOGE("Failed to write to /proc/thread-self/attr/sockcreate: %s\n", strerror(errno));
errno = 0;
return; return;
} }
close(sockcreate); fclose(sockcreate);
} }
static void get_current_attr(char *output) { static void get_current_attr(char *output) {
@@ -74,15 +72,13 @@ static void get_current_attr(char *output) {
FILE *current = fopen(path, "r"); FILE *current = fopen(path, "r");
if (current == NULL) { if (current == NULL) {
LOGE("Failed to open current: %s\n", strerror(errno)); LOGE("fopen: %s\n", strerror(errno));
errno = 0;
return; return;
} }
if (fgets(output, PATH_MAX, current) == NULL) { if (fgets(output, PATH_MAX, current) == NULL) {
LOGE("fgets: %s\n", strerror(errno)); LOGE("fgets: %s\n", strerror(errno));
errno = 0;
return; return;
} }