diff options
Diffstat (limited to 'kms/server')
-rw-r--r-- | kms/server/kms_server.c | 127 |
1 files changed, 25 insertions, 102 deletions
diff --git a/kms/server/kms_server.c b/kms/server/kms_server.c index 2677134..070875b 100644 --- a/kms/server/kms_server.c +++ b/kms/server/kms_server.c @@ -8,6 +8,7 @@ #include <string.h> #include <errno.h> #include <stdlib.h> +#include <locale.h> #include <unistd.h> #include <limits.h> @@ -25,7 +26,6 @@ typedef struct { int drmfd; - drmModePlaneResPtr planes; } gsr_drm; typedef struct { @@ -208,7 +208,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 +289,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 +356,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 +399,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; @@ -419,87 +434,13 @@ static double clock_get_monotonic_seconds(void) { 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"); @@ -532,17 +473,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; @@ -577,11 +507,6 @@ int main(int argc, char **argv) { goto done; } - // if(!is_remote_peer_program_gpu_screen_recorder(socket_fd)) { - // res = 3; - // goto done; - // } - for(;;) { gsr_kms_request request; request.version = 0; @@ -642,7 +567,7 @@ int main(int argc, char **argv) { 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 +606,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) |