aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2024-05-09 16:17:38 +0200
committerdec05eba <dec05eba@protonmail.com>2024-05-09 16:17:38 +0200
commitf1eb8934930d0bb5e4f64ce24ca9971f0655c04f (patch)
treed1f21120dcca846d21b2a3031bee22f174693c56
parent447b5d0f0ba9ea34b90acd8d6e23005f15f1c242 (diff)
Proper strncpy null termination
-rw-r--r--kms/client/kms_client.c4
-rw-r--r--kms/server/kms_server.c4
-rw-r--r--src/utils.c13
3 files changed, 15 insertions, 6 deletions
diff --git a/kms/client/kms_client.c b/kms/client/kms_client.c
index 57c6ccf..1c8f634 100644
--- a/kms/client/kms_client.c
+++ b/kms/client/kms_client.c
@@ -127,7 +127,7 @@ static bool create_socket_path(char *output_path, size_t output_path_size) {
return true;
}
-static void strncpy_safe(char *dst, const char *src, int len) {
+static void string_copy(char *dst, const char *src, int len) {
int src_len = strlen(src);
int min_len = src_len;
if(len - 1 < min_len)
@@ -232,7 +232,7 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) {
}
local_addr.sun_family = AF_UNIX;
- strncpy_safe(local_addr.sun_path, self->initial_socket_path, sizeof(local_addr.sun_path));
+ string_copy(local_addr.sun_path, self->initial_socket_path, sizeof(local_addr.sun_path));
const mode_t prev_mask = umask(0000);
const int bind_res = bind(self->initial_socket_fd, (struct sockaddr*)&local_addr, sizeof(local_addr.sun_family) + strlen(local_addr.sun_path));
diff --git a/kms/server/kms_server.c b/kms/server/kms_server.c
index 09deaab..2eaa1ed 100644
--- a/kms/server/kms_server.c
+++ b/kms/server/kms_server.c
@@ -379,7 +379,7 @@ static double clock_get_monotonic_seconds(void) {
return (double)ts.tv_sec + (double)ts.tv_nsec * 0.000000001;
}
-static void strncpy_safe(char *dst, const char *src, int len) {
+static void string_copy(char *dst, const char *src, int len) {
int src_len = strlen(src);
int min_len = src_len;
if(len - 1 < min_len)
@@ -444,7 +444,7 @@ int main(int argc, char **argv) {
while(clock_get_monotonic_seconds() - start_time < connect_timeout_sec) {
struct sockaddr_un remote_addr = {0};
remote_addr.sun_family = AF_UNIX;
- strncpy_safe(remote_addr.sun_path, domain_socket_path, sizeof(remote_addr.sun_path));
+ string_copy(remote_addr.sun_path, domain_socket_path, sizeof(remote_addr.sun_path));
// TODO: Check if parent disconnected
if(connect(socket_fd, (struct sockaddr*)&remote_addr, sizeof(remote_addr.sun_family) + strlen(remote_addr.sun_path)) == -1) {
if(errno == ECONNREFUSED || errno == ENOENT) {
diff --git a/src/utils.c b/src/utils.c
index 7dd1890..40d652e 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -414,9 +414,18 @@ static bool try_card_has_valid_plane(const char *card_path) {
return false;
}
+static void string_copy(char *dst, const char *src, int len) {
+ int src_len = strlen(src);
+ int min_len = src_len;
+ if(len - 1 < min_len)
+ min_len = len - 1;
+ memcpy(dst, src, min_len);
+ dst[min_len] = '\0';
+}
+
bool gsr_get_valid_card_path(gsr_egl *egl, char *output) {
if(egl->dri_card_path) {
- strncpy(output, egl->dri_card_path, 127);
+ string_copy(output, egl->dri_card_path, 127);
return try_card_has_valid_plane(output);
}
@@ -435,7 +444,7 @@ bool gsr_card_path_get_render_path(const char *card_path, char *render_path) {
char *render_path_tmp = drmGetRenderDeviceNameFromFd(fd);
if(render_path_tmp) {
- strncpy(render_path, render_path_tmp, 127);
+ string_copy(render_path, render_path_tmp, 127);
free(render_path_tmp);
close(fd);
return true;