diff options
Diffstat (limited to 'kms')
-rw-r--r-- | kms/client/kms_client.c | 159 | ||||
-rw-r--r-- | kms/client/kms_client.h | 3 | ||||
-rw-r--r-- | kms/kms_shared.h | 1 | ||||
-rw-r--r-- | kms/server/kms_server.c | 212 |
4 files changed, 47 insertions, 328 deletions
diff --git a/kms/client/kms_client.c b/kms/client/kms_client.c index 018b25e..5e3ce08 100644 --- a/kms/client/kms_client.c +++ b/kms/client/kms_client.c @@ -1,5 +1,4 @@ #include "kms_client.h" -#include "../../include/utils.h" #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -9,17 +8,13 @@ #include <stdbool.h> #include <fcntl.h> #include <sys/socket.h> -#include <sys/un.h> #include <sys/wait.h> -#include <poll.h> -#include <sys/stat.h> #include <sys/capability.h> #define GSR_SOCKET_PAIR_LOCAL 0 #define GSR_SOCKET_PAIR_REMOTE 1 static void cleanup_socket(gsr_kms_client *self, bool kill_server); -static int gsr_kms_client_replace_connection(gsr_kms_client *self); static void close_fds(gsr_kms_response *response) { for(int i = 0; i < response->num_items; ++i) { @@ -117,21 +112,6 @@ static int recv_msg_from_server(int server_pid, int server_fd, gsr_kms_response return res; } -/* We have to use $HOME because in flatpak there is no simple path that is accessible, read and write, that multiple flatpak instances can access */ -static bool create_socket_path(char *output_path, size_t output_path_size) { - const char *home = getenv("HOME"); - if(!home) - home = "/tmp"; - - char random_characters[11]; - random_characters[10] = '\0'; - if(!generate_random_characters_standard_alphabet(random_characters, 10)) - return false; - - snprintf(output_path, output_path_size, "%s/.gsr-kms-socket-%s", home, random_characters); - return true; -} - static bool readlink_realpath(const char *filepath, char *buffer) { char symlinked_path[PATH_MAX]; ssize_t bytes_written = readlink(filepath, symlinked_path, sizeof(symlinked_path) - 1); @@ -206,18 +186,8 @@ static bool find_program_in_path(const char *program_name, char *filepath, int f int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) { int result = -1; self->kms_server_pid = -1; - self->initial_socket_fd = -1; - self->initial_client_fd = -1; - self->initial_socket_path[0] = '\0'; self->socket_pair[0] = -1; self->socket_pair[1] = -1; - struct sockaddr_un local_addr = {0}; - struct sockaddr_un remote_addr = {0}; - - if(!create_socket_path(self->initial_socket_path, sizeof(self->initial_socket_path))) { - fprintf(stderr, "gsr error: gsr_kms_client_init: failed to create path to kms socket\n"); - return -1; - } char server_filepath[PATH_MAX]; if(!readlink_realpath("/proc/self/exe", server_filepath)) { @@ -239,7 +209,7 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) { } } - fprintf(stderr, "gsr info: gsr_kms_client_init: setting up connection to %s\n", server_filepath); + fprintf(stderr, "gsr info: gsr_kms_client_init: launching gsr-kms-server at %s\n", server_filepath); const bool inside_flatpak = getenv("FLATPAK_ID") != NULL; const char *home = getenv("HOME"); @@ -274,90 +244,34 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) { goto err; } - self->initial_socket_fd = socket(AF_UNIX, SOCK_STREAM, 0); - if(self->initial_socket_fd == -1) { - fprintf(stderr, "gsr error: gsr_kms_client_init: socket failed, error: %s\n", strerror(errno)); - goto err; - } - - local_addr.sun_family = AF_UNIX; - snprintf(local_addr.sun_path, sizeof(local_addr.sun_path), "%s", (const char*)self->initial_socket_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)); - umask(prev_mask); - - if(bind_res == -1) { - fprintf(stderr, "gsr error: gsr_kms_client_init: failed to bind socket, error: %s\n", strerror(errno)); - goto err; - } - - if(listen(self->initial_socket_fd, 1) == -1) { - fprintf(stderr, "gsr error: gsr_kms_client_init: failed to listen on socket, error: %s\n", strerror(errno)); - goto err; - } - pid_t pid = fork(); if(pid == -1) { fprintf(stderr, "gsr error: gsr_kms_client_init: fork failed, error: %s\n", strerror(errno)); goto err; } else if(pid == 0) { /* child */ + char socket_pair_remote_str[32]; + snprintf(socket_pair_remote_str, sizeof(socket_pair_remote_str), "%d", self->socket_pair[GSR_SOCKET_PAIR_REMOTE]); + if(inside_flatpak) { - const char *args[] = { "flatpak-spawn", "--host", "/var/lib/flatpak/app/com.dec05eba.gpu_screen_recorder/current/active/files/bin/kms-server-proxy", self->initial_socket_path, card_path, home, NULL }; + char forward_fd_arg[128]; + snprintf(forward_fd_arg, sizeof(forward_fd_arg), "--forward-fd=%s", socket_pair_remote_str); + + const char *args[] = { "flatpak-spawn", "--host", forward_fd_arg, "/var/lib/flatpak/app/com.dec05eba.gpu_screen_recorder/current/active/files/bin/kms-server-proxy", socket_pair_remote_str, card_path, home, NULL }; execvp(args[0], (char *const*)args); } else if(has_perm) { - const char *args[] = { server_filepath, self->initial_socket_path, card_path, NULL }; + const char *args[] = { server_filepath, socket_pair_remote_str, card_path, NULL }; execvp(args[0], (char *const*)args); } else { - const char *args[] = { "pkexec", server_filepath, self->initial_socket_path, card_path, NULL }; + const char *args[] = { "pkexec", server_filepath, socket_pair_remote_str, card_path, NULL }; execvp(args[0], (char *const*)args); } - fprintf(stderr, "gsr error: gsr_kms_client_init: execvp failed, error: %s\n", strerror(errno)); + + fprintf(stderr, "gsr error: gsr_kms_client_init: failed to launch \"gsr-kms-server\", error: %s\n", strerror(errno)); _exit(127); } else { /* parent */ self->kms_server_pid = pid; } - fprintf(stderr, "gsr info: gsr_kms_client_init: waiting for server to connect\n"); - struct pollfd poll_fd = { - .fd = self->initial_socket_fd, - .events = POLLIN, - .revents = 0 - }; - for(;;) { - int poll_res = poll(&poll_fd, 1, 100); - if(poll_res > 0 && (poll_fd.revents & POLLIN)) { - socklen_t sock_len = 0; - self->initial_client_fd = accept(self->initial_socket_fd, (struct sockaddr*)&remote_addr, &sock_len); - if(self->initial_client_fd == -1) { - fprintf(stderr, "gsr error: gsr_kms_client_init: accept failed on socket, error: %s\n", strerror(errno)); - goto err; - } - break; - } else { - int status = 0; - int wait_result = waitpid(self->kms_server_pid, &status, WNOHANG); - if(wait_result != 0) { - int exit_code = -1; - if(WIFEXITED(status)) - exit_code = WEXITSTATUS(status); - fprintf(stderr, "gsr error: gsr_kms_client_init: kms server died or never started, exit code: %d\n", exit_code); - self->kms_server_pid = -1; - if(exit_code != 0) - result = exit_code; - goto err; - } - } - } - fprintf(stderr, "gsr info: gsr_kms_client_init: server connected\n"); - - fprintf(stderr, "gsr info: replacing file-backed unix domain socket with socketpair\n"); - if(gsr_kms_client_replace_connection(self) != 0) - goto err; - - cleanup_socket(self, false); - fprintf(stderr, "gsr info: using socketpair\n"); - return 0; err: @@ -366,16 +280,6 @@ int gsr_kms_client_init(gsr_kms_client *self, const char *card_path) { } void cleanup_socket(gsr_kms_client *self, bool kill_server) { - if(self->initial_client_fd > 0) { - close(self->initial_client_fd); - self->initial_client_fd = -1; - } - - if(self->initial_socket_fd > 0) { - close(self->initial_socket_fd); - self->initial_socket_fd = -1; - } - if(kill_server) { for(int i = 0; i < 2; ++i) { if(self->socket_pair[i] > 0) { @@ -387,54 +291,17 @@ void cleanup_socket(gsr_kms_client *self, bool kill_server) { if(kill_server && self->kms_server_pid > 0) { kill(self->kms_server_pid, SIGKILL); + // TODO: //int status; //waitpid(self->kms_server_pid, &status, 0); self->kms_server_pid = -1; } - - if(self->initial_socket_path[0] != '\0') { - remove(self->initial_socket_path); - self->initial_socket_path[0] = '\0'; - } } void gsr_kms_client_deinit(gsr_kms_client *self) { cleanup_socket(self, true); } -int gsr_kms_client_replace_connection(gsr_kms_client *self) { - gsr_kms_response response; - response.version = 0; - response.result = KMS_RESULT_FAILED_TO_SEND; - response.err_msg[0] = '\0'; - - gsr_kms_request request; - request.version = GSR_KMS_PROTOCOL_VERSION; - request.type = KMS_REQUEST_TYPE_REPLACE_CONNECTION; - request.new_connection_fd = self->socket_pair[GSR_SOCKET_PAIR_REMOTE]; - if(send_msg_to_server(self->initial_client_fd, &request) == -1) { - fprintf(stderr, "gsr error: gsr_kms_client_replace_connection: failed to send request message to server\n"); - return -1; - } - - const int recv_res = recv_msg_from_server(self->kms_server_pid, self->socket_pair[GSR_SOCKET_PAIR_LOCAL], &response); - if(recv_res == 0) { - fprintf(stderr, "gsr warning: gsr_kms_client_replace_connection: kms server shut down\n"); - return -1; - } else if(recv_res == -1) { - fprintf(stderr, "gsr error: gsr_kms_client_replace_connection: failed to receive response\n"); - return -1; - } - - if(response.version != GSR_KMS_PROTOCOL_VERSION) { - fprintf(stderr, "gsr error: gsr_kms_client_replace_connection: expected gsr-kms-server protocol version to be %u, but it's %u. please reinstall gpu screen recorder\n", GSR_KMS_PROTOCOL_VERSION, response.version); - /*close_fds(response);*/ - return -1; - } - - return 0; -} - int gsr_kms_client_get_kms(gsr_kms_client *self, gsr_kms_response *response) { response->version = 0; response->result = KMS_RESULT_FAILED_TO_SEND; diff --git a/kms/client/kms_client.h b/kms/client/kms_client.h index 2d18848..59168b7 100644 --- a/kms/client/kms_client.h +++ b/kms/client/kms_client.h @@ -9,9 +9,6 @@ typedef struct gsr_kms_client gsr_kms_client; struct gsr_kms_client { pid_t kms_server_pid; - int initial_socket_fd; - int initial_client_fd; - char initial_socket_path[PATH_MAX]; int socket_pair[2]; }; diff --git a/kms/kms_shared.h b/kms/kms_shared.h index 2dbb655..ab848b5 100644 --- a/kms/kms_shared.h +++ b/kms/kms_shared.h @@ -15,7 +15,6 @@ typedef struct gsr_kms_response_item gsr_kms_response_item; typedef struct gsr_kms_response gsr_kms_response; typedef enum { - KMS_REQUEST_TYPE_REPLACE_CONNECTION, KMS_REQUEST_TYPE_GET_KMS } gsr_kms_request_type; diff --git a/kms/server/kms_server.c b/kms/server/kms_server.c index 2677134..b844526 100644 --- a/kms/server/kms_server.c +++ b/kms/server/kms_server.c @@ -6,15 +6,14 @@ #include <stdio.h> #include <string.h> -#include <errno.h> #include <stdlib.h> +#include <errno.h> +#include <locale.h> #include <unistd.h> #include <limits.h> #include <fcntl.h> #include <sys/socket.h> -#include <sys/un.h> -#include <time.h> #include <xf86drm.h> #include <xf86drmMode.h> @@ -25,7 +24,6 @@ typedef struct { int drmfd; - drmModePlaneResPtr planes; } gsr_drm; typedef struct { @@ -208,7 +206,7 @@ static uint32_t plane_get_properties(int drmfd, uint32_t plane_id, int *x, int * return property_mask; } -/* Returns 0 if not found */ +/* Returns NULL if not found */ static const connector_crtc_pair* get_connector_pair_by_crtc_id(const connector_to_crtc_map *c2crtc_map, uint32_t crtc_id) { for(int i = 0; i < c2crtc_map->num_maps; ++i) { if(c2crtc_map->maps[i].crtc_id == crtc_id) @@ -289,21 +287,31 @@ static int drm_prime_handles_to_fds(gsr_drm *drm, drmModeFB2Ptr drmfb, int *fb_f return GSR_KMS_MAX_DMA_BUFS; } -static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response, connector_to_crtc_map *c2crtc_map) { +static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response) { int result = -1; response->result = KMS_RESULT_OK; response->err_msg[0] = '\0'; response->num_items = 0; - for(uint32_t i = 0; i < drm->planes->count_planes && response->num_items < GSR_KMS_MAX_ITEMS; ++i) { + connector_to_crtc_map c2crtc_map; + c2crtc_map.num_maps = 0; + map_crtc_to_connector_ids(drm, &c2crtc_map); + + drmModePlaneResPtr planes = drmModeGetPlaneResources(drm->drmfd); + if(!planes) { + fprintf(stderr, "kms server error: failed to get plane resources, error: %s\n", strerror(errno)); + goto done; + } + + for(uint32_t i = 0; i < planes->count_planes && response->num_items < GSR_KMS_MAX_ITEMS; ++i) { drmModePlanePtr plane = NULL; drmModeFB2Ptr drmfb = NULL; - plane = drmModeGetPlane(drm->drmfd, drm->planes->planes[i]); + plane = drmModeGetPlane(drm->drmfd, planes->planes[i]); if(!plane) { response->result = KMS_RESULT_FAILED_TO_GET_PLANE; - snprintf(response->err_msg, sizeof(response->err_msg), "failed to get drm plane with id %u, error: %s\n", drm->planes->planes[i], strerror(errno)); + snprintf(response->err_msg, sizeof(response->err_msg), "failed to get drm plane with id %u, error: %s\n", planes->planes[i], strerror(errno)); fprintf(stderr, "kms server error: %s\n", response->err_msg); goto next; } @@ -346,7 +354,7 @@ static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response, connector_to_crt const int item_index = response->num_items; - const connector_crtc_pair *crtc_pair = get_connector_pair_by_crtc_id(c2crtc_map, plane->crtc_id); + const connector_crtc_pair *crtc_pair = get_connector_pair_by_crtc_id(&c2crtc_map, plane->crtc_id); if(crtc_pair && crtc_pair->hdr_metadata_blob_id) { response->items[item_index].has_hdr_metadata = get_hdr_metadata(drm->drmfd, crtc_pair->hdr_metadata_blob_id, &response->items[item_index].hdr_metadata); } else { @@ -389,6 +397,11 @@ static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response, connector_to_crt drmModeFreePlane(plane); } + done: + + if(planes) + drmModeFreePlaneResources(planes); + if(response->num_items > 0) response->result = KMS_RESULT_OK; @@ -411,110 +424,28 @@ static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response, connector_to_crt return result; } -static double clock_get_monotonic_seconds(void) { - struct timespec ts; - ts.tv_sec = 0; - ts.tv_nsec = 0; - clock_gettime(CLOCK_MONOTONIC, &ts); - return (double)ts.tv_sec + (double)ts.tv_nsec * 0.000000001; -} - -// static bool readlink_realpath(const char *filepath, char *buffer) { -// char symlinked_path[PATH_MAX]; -// ssize_t bytes_written = readlink(filepath, symlinked_path, sizeof(symlinked_path) - 1); -// if(bytes_written == -1 && errno == EINVAL) { -// /* Not a symlink */ -// snprintf(symlinked_path, sizeof(symlinked_path), "%s", filepath); -// } else if(bytes_written == -1) { -// return false; -// } else { -// symlinked_path[bytes_written] = '\0'; -// } - -// if(!realpath(symlinked_path, buffer)) -// return false; - -// return true; -// } - -// static void file_get_directory(char *filepath) { -// char *end = strrchr(filepath, '/'); -// if(end == NULL) -// filepath[0] = '\0'; -// else -// *end = '\0'; -// } - -// static bool string_ends_with(const char *str, const char *ends_with) { -// const int len = strlen(str); -// const int ends_with_len = strlen(ends_with); -// return len >= ends_with_len && memcmp(str + len - ends_with_len, ends_with, ends_with_len) == 0; -// } - -// This is not foolproof, but the assumption is that gsr-kms-server and gpu-screen-recorder are installed in the same directory -// in a location that only the root user can write to (usually /usr/bin or /usr/local/bin) and if the client runs from that location -// and is called gpu-screen-recorder then gsr-kms-server can only be used by a malicious program if the malicious program -// had root access, to modify that program install directory. -// static bool is_remote_peer_program_gpu_screen_recorder(int socket_fd) { -// // TODO: Use SO_PEERPIDFD on kernel >= 6.5 to avoid a race condition in the /proc/<pid> check -// struct ucred cred; -// socklen_t ucred_len = sizeof(cred); -// if(getsockopt(socket_fd, SOL_SOCKET, SO_PEERCRED, &cred, &ucred_len) == -1) { -// fprintf(stderr, "kms server error: failed to get peer credentials, error: %s\n", strerror(errno)); -// return false; -// } - -// char self_directory[PATH_MAX]; -// if(!readlink_realpath("/proc/self/exe", self_directory)) { -// fprintf(stderr, "kms server error: failed to resolve /proc/self/exe\n"); -// return false; -// } -// file_get_directory(self_directory); - -// char peer_directory[PATH_MAX]; -// char peer_exe_path[PATH_MAX]; -// snprintf(peer_exe_path, sizeof(peer_exe_path), "/proc/%d/exe", (int)cred.pid); -// if(!readlink_realpath(peer_exe_path, peer_directory)) { -// fprintf(stderr, "kms server error: failed to resolve /proc/self/exe\n"); -// return false; -// } - -// if(!string_ends_with(peer_directory, "/gpu-screen-recorder")) { -// fprintf(stderr, "kms server error: only gpu-screen-recorder can use gsr-kms-server. client program location is %s\n", peer_directory); -// return false; -// } - -// file_get_directory(peer_directory); - -// if(strcmp(self_directory, peer_directory) != 0) { -// fprintf(stderr, "kms server error: the client program is in directory %s but only programs in %s can run gsr-kms-server\n", peer_directory, self_directory); -// return false; -// } - -// return true; -// } - int main(int argc, char **argv) { + setlocale(LC_ALL, "C"); // Sigh... stupid C + int res = 0; int socket_fd = 0; gsr_drm drm; drm.drmfd = 0; - drm.planes = NULL; if(argc != 3) { - fprintf(stderr, "usage: gsr-kms-server <domain_socket_path> <card_path>\n"); + fprintf(stderr, "usage: gsr-kms-server <socket_fd> <card_path>\n"); return 1; } - const char *domain_socket_path = argv[1]; - socket_fd = socket(AF_UNIX, SOCK_STREAM, 0); - if(socket_fd == -1) { - fprintf(stderr, "kms server error: failed to create socket, error: %s\n", strerror(errno)); - return 2; - } - + const char *socket_fd_str = argv[1]; const char *card_path = argv[2]; + socket_fd = atoi(socket_fd_str); + if(socket_fd <= 0) { + fprintf(stderr, "kms server error: received invalid socket fd for argument 1, expected a number got \"%s\"\n", socket_fd_str); + return 1; + } + drm.drmfd = open(card_path, O_RDONLY); if(drm.drmfd < 0) { fprintf(stderr, "kms server error: failed to open %s, error: %s", card_path, strerror(errno)); @@ -532,56 +463,6 @@ int main(int argc, char **argv) { fprintf(stderr, "kms server warning: drmSetClientCap DRM_CLIENT_CAP_ATOMIC failed, error: %s. The wrong monitor may be captured as a result\n", strerror(errno)); } - drm.planes = drmModeGetPlaneResources(drm.drmfd); - if(!drm.planes) { - fprintf(stderr, "kms server error: failed to get plane resources, error: %s\n", strerror(errno)); - res = 2; - goto done; - } - - connector_to_crtc_map c2crtc_map; - c2crtc_map.num_maps = 0; - map_crtc_to_connector_ids(&drm, &c2crtc_map); - - fprintf(stderr, "kms server info: connecting to the client\n"); - bool connected = false; - const double connect_timeout_sec = 5.0; - const double start_time = clock_get_monotonic_seconds(); - while(clock_get_monotonic_seconds() - start_time < connect_timeout_sec) { - struct sockaddr_un remote_addr = {0}; - remote_addr.sun_family = AF_UNIX; - snprintf(remote_addr.sun_path, sizeof(remote_addr.sun_path), "%s", domain_socket_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) { - goto next; - } else if(errno == EISCONN) { - connected = true; - break; - } - - fprintf(stderr, "kms server error: connect failed, error: %s (%d)\n", strerror(errno), errno); - res = 2; - goto done; - } - - next: - usleep(30 * 1000); // 30 milliseconds - } - - if(connected) { - fprintf(stderr, "kms server info: connected to the client\n"); - } else { - fprintf(stderr, "kms server error: failed to connect to the client in %f seconds\n", connect_timeout_sec); - res = 2; - goto done; - } - - // if(!is_remote_peer_program_gpu_screen_recorder(socket_fd)) { - // res = 3; - // goto done; - // } - for(;;) { gsr_kms_request request; request.version = 0; @@ -614,35 +495,12 @@ int main(int argc, char **argv) { } switch(request.type) { - case KMS_REQUEST_TYPE_REPLACE_CONNECTION: { - gsr_kms_response response; - response.version = GSR_KMS_PROTOCOL_VERSION; - response.num_items = 0; - - if(request.new_connection_fd > 0) { - if(socket_fd > 0) - close(socket_fd); - socket_fd = request.new_connection_fd; - - response.result = KMS_RESULT_OK; - if(send_msg_to_client(socket_fd, &response) == -1) - fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_REPLACE_CONNECTION request\n"); - } else { - response.result = KMS_RESULT_INVALID_REQUEST; - snprintf(response.err_msg, sizeof(response.err_msg), "received invalid connection fd"); - fprintf(stderr, "kms server error: %s\n", response.err_msg); - if(send_msg_to_client(socket_fd, &response) == -1) - fprintf(stderr, "kms server error: failed to respond to client request\n"); - } - - break; - } case KMS_REQUEST_TYPE_GET_KMS: { gsr_kms_response response; response.version = GSR_KMS_PROTOCOL_VERSION; response.num_items = 0; - if(kms_get_fb(&drm, &response, &c2crtc_map) == 0) { + if(kms_get_fb(&drm, &response) == 0) { if(send_msg_to_client(socket_fd, &response) == -1) fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_GET_KMS request\n"); } else { @@ -681,8 +539,6 @@ int main(int argc, char **argv) { } done: - if(drm.planes) - drmModeFreePlaneResources(drm.planes); if(drm.drmfd > 0) close(drm.drmfd); if(socket_fd > 0) |