Skip to content

Commit 86f01a9

Browse files
committed
improve: use lsetxattr instead of chcon via system; fix: not closing socket_fd in error paths
This commit makes ReZygisk use `lsetxattr` directly to set SELinux content instead of starting a shell for that. It also makes "socket_fd" be closed in the error paths, avoiding a fd leak in case it fails.
1 parent 44b6d90 commit 86f01a9

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

zygiskd/src/utils.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <sys/un.h>
1111
#include <sys/sysmacros.h>
1212
#include <sys/mount.h>
13+
#include <sys/xattr.h>
1314

1415
#include <unistd.h>
1516
#include <linux/limits.h>
@@ -121,9 +122,9 @@ void unix_datagram_sendto(const char *restrict path, const void *restrict buf, s
121122

122123
set_socket_create_context(current_attr);
123124

124-
struct sockaddr_un addr;
125-
addr.sun_family = AF_UNIX;
126-
125+
struct sockaddr_un addr = {
126+
.sun_family = AF_UNIX
127+
};
127128
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
128129

129130
int socket_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
@@ -136,12 +137,16 @@ void unix_datagram_sendto(const char *restrict path, const void *restrict buf, s
136137
if (connect(socket_fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
137138
LOGE("connect: %s\n", strerror(errno));
138139

140+
close(socket_fd);
141+
139142
return;
140143
}
141144

142145
if (sendto(socket_fd, buf, len, 0, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
143146
LOGE("sendto: %s\n", strerror(errno));
144147

148+
close(socket_fd);
149+
145150
return;
146151
}
147152

@@ -151,10 +156,7 @@ void unix_datagram_sendto(const char *restrict path, const void *restrict buf, s
151156
}
152157

153158
int chcon(const char *restrict path, const char *context) {
154-
char command[PATH_MAX];
155-
snprintf(command, PATH_MAX, "chcon %s %s", context, path);
156-
157-
return system(command);
159+
return lsetxattr(path, "security.selinux", context, strlen(context) + 1, 0);
158160
}
159161

160162
int unix_listener_from_path(const char *restrict path) {
@@ -179,20 +181,23 @@ int unix_listener_from_path(const char *restrict path) {
179181
if (bind(socket_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
180182
LOGE("bind: %s\n", strerror(errno));
181183

184+
close(socket_fd);
185+
182186
return -1;
183187
}
184188

185189
if (listen(socket_fd, 2) == -1) {
186190
LOGE("listen: %s\n", strerror(errno));
187191

192+
close(socket_fd);
193+
188194
return -1;
189195
}
190196

191-
if (chcon(path, "u:object_r:zygisk_file:s0") == -1) {
192-
LOGE("chcon: %s\n", strerror(errno));
197+
LOGI("socket listening on %s (fd=%d)", path, socket_fd);
193198

194-
return -1;
195-
}
199+
if (chcon(path, "u:object_r:zygisk_file:s0") == -1)
200+
LOGW("chcon (non-fatal): %s\n", strerror(errno));
196201

197202
return socket_fd;
198203
}

0 commit comments

Comments
 (0)