1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <sys/capability.h>
#define KMS_SERVER_PROXY_FILEPATH "/var/lib/flatpak/app/com.dec05eba.gpu_screen_recorder/current/active/files/bin/kms-server-proxy"
#define GSR_KMS_SERVER_FILEPATH "/var/lib/flatpak/app/com.dec05eba.gpu_screen_recorder/current/active/files/bin/gsr-kms-server"
#define GSR_GLOBAL_HOTKEYS_FILEPATH "/var/lib/flatpak/app/com.dec05eba.gpu_screen_recorder/current/active/files/bin/gsr-global-hotkeys"
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 */
strncpy(symlinked_path, filepath, sizeof(symlinked_path));
} else if(bytes_written == -1) {
return false;
} else {
symlinked_path[bytes_written] = '\0';
}
if(!realpath(symlinked_path, buffer))
return false;
return true;
}
static bool file_has_sys_admin_capability(const char *filepath) {
cap_t cap = cap_get_file(filepath);
if(!cap)
return false;
cap_flag_value_t res = CAP_CLEAR;
cap_get_flag(cap, CAP_SYS_ADMIN, CAP_PERMITTED, &res);
bool cap_set = res == CAP_SET;
cap_free(cap);
return cap_set;
}
static bool file_set_capabilities(const char *filepath, const cap_value_t *caps_to_set, int num_caps_to_set) {
cap_t cap = cap_get_file(filepath);
if(!cap && errno != ENODATA)
return false;
if(!cap) {
cap = cap_init();
if(!cap)
return false;
}
int res = 0;
res |= cap_set_flag(cap, CAP_EFFECTIVE, num_caps_to_set, caps_to_set, CAP_SET);
res |= cap_set_flag(cap, CAP_PERMITTED, num_caps_to_set, caps_to_set, CAP_SET);
/*cap_set_flag(cap, CAP_INHERITABLE, num_caps_to_set, caps_to_set, CAP_SET);*/
res |= cap_set_file(filepath, cap);
cap_free(cap);
return res == 0;
}
static bool file_set_permissions(const char *filepath, unsigned int permissions) {
return chmod(filepath, permissions) == 0;
}
static bool create_local_kms_server_proxy_directory(const char *home) {
char path[PATH_MAX];
int err;
const char *paths[] = { ".local", ".local/share", ".local/share/gpu-screen-recorder", NULL };
for(size_t i = 0; paths[i]; ++i) {
const char *path_part = paths[i];
snprintf(path, sizeof(path), "%s/%s", home, path_part);
err = mkdir(path, S_IRWXU);
if(err == -1 && errno != EEXIST)
return false;
}
return true;
}
static bool copy_file_atomic(const char *source_path, const char *dest_path) {
int in_fd = -1;
int out_fd = -1;
bool res = false;
char tmp_filepath[PATH_MAX];
snprintf(tmp_filepath, sizeof(tmp_filepath), "%s.tmp", dest_path);
in_fd = open(source_path, O_RDONLY);
if(in_fd == -1)
goto done;
struct stat st;
if(fstat(in_fd, &st) == -1)
goto done;
out_fd = open(tmp_filepath, O_RDWR | O_CREAT | O_TRUNC, 0755);
if(out_fd == -1)
goto done;
if(sendfile(out_fd, in_fd, NULL, st.st_size) != st.st_size)
goto done;
res = true;
done:
if(in_fd)
close(in_fd);
if(out_fd)
close(out_fd);
if(res)
res = rename(tmp_filepath, dest_path) == 0;
return res;
}
static void usage(void) {
fprintf(stderr, "usage alt.1: kms-server-proxy <initial_socket_path> <card_path> <user_homepath>\n");
fprintf(stderr, "usage alt.2: kms-server-proxy setup-gsr-ui [user_homepath]\n");
fprintf(stderr, "usage alt.3: kms-server-proxy launch-gsr-global-hotkeys <user_homepath> [args...]\n");
exit(1);
}
static bool set_kms_server_proxy_permissions_and_capabilities(const char *kms_server_proxy_filepath) {
/* owner: read/write/execute, group: read/execute, public: read/execute */
if(!file_set_permissions(kms_server_proxy_filepath, 0755)) {
fprintf(stderr, "Error: failed to set kms-server-proxy permissions\n");
return false;
}
if(!file_set_capabilities(kms_server_proxy_filepath, (const cap_value_t[]){ CAP_SYS_ADMIN, CAP_SETFCAP, CAP_SETUID }, 3)) {
fprintf(stderr, "Error: failed to set kms-server-proxy capabilities\n");
return false;
}
return true;
}
/* |gsr_global_hotkeys_local_filepath| can be NULL */
static bool setup_local_files(const char *user_homepath, const char *kms_server_proxy_local_filepath, const char *gsr_kms_server_local_filepath, const char *gsr_global_hotkeys_local_filepath) {
if(!create_local_kms_server_proxy_directory(user_homepath)) {
fprintf(stderr, "Error: failed to create ~/.local/share/gpu-screen-recorder directory\n");
return false;
}
if(!copy_file_atomic(KMS_SERVER_PROXY_FILEPATH, kms_server_proxy_local_filepath)) {
fprintf(stderr, "Error: failed to copy kms-server-proxy to %s\n", kms_server_proxy_local_filepath);
return false;
}
if(!copy_file_atomic(GSR_KMS_SERVER_FILEPATH, gsr_kms_server_local_filepath)) {
fprintf(stderr, "Error: failed to copy gsr-kms-server to %s\n", gsr_kms_server_local_filepath);
return false;
}
if(gsr_global_hotkeys_local_filepath) {
if(!copy_file_atomic(GSR_GLOBAL_HOTKEYS_FILEPATH, gsr_global_hotkeys_local_filepath)) {
fprintf(stderr, "Error: failed to copy gsr-global-hotkeys to %s\n", gsr_global_hotkeys_local_filepath);
return false;
}
}
return true;
}
static int setup_gsr_ui(const char *user_homepath) {
char self_path[PATH_MAX];
if(!readlink_realpath("/proc/self/exe", self_path)) {
fprintf(stderr, "failed to resolve /proc/self/exe\n");
return 1;
}
if(!user_homepath) {
user_homepath = getenv("HOME");
if(!user_homepath)
user_homepath = "/tmp";
}
char kms_server_proxy_local_filepath[PATH_MAX];
/* Update kms-server-proxy-N to kms-server-proxy-N+1 on update (update that needs to run before this program launches itself) */
snprintf(kms_server_proxy_local_filepath, sizeof(kms_server_proxy_local_filepath), "%s/.local/share/gpu-screen-recorder/kms-server-proxy-2", user_homepath);
char gsr_kms_server_local_filepath[PATH_MAX];
snprintf(gsr_kms_server_local_filepath, sizeof(gsr_kms_server_local_filepath), "%s/.local/share/gpu-screen-recorder/gsr-kms-server", user_homepath);
char gsr_global_hotkeys_local_filepath[PATH_MAX];
snprintf(gsr_global_hotkeys_local_filepath, sizeof(gsr_global_hotkeys_local_filepath), "%s/.local/share/gpu-screen-recorder/gsr-global-hotkeys", user_homepath);
if(geteuid() == 0) { /* is current user root? */
if(!set_kms_server_proxy_permissions_and_capabilities(kms_server_proxy_local_filepath))
return 1;
return 0;
} else {
if(!setup_local_files(user_homepath, kms_server_proxy_local_filepath, gsr_kms_server_local_filepath, gsr_global_hotkeys_local_filepath))
return 1;
const char *args[] = { "pkexec", kms_server_proxy_local_filepath, "setup-gsr-ui", user_homepath, NULL };
return execvp(args[0], (char *const*)args);
}
}
static int launch_gsr_kms_server(const char *initial_socket_path, const char *card_path, const char *user_homepath) {
char self_path[PATH_MAX];
if(!readlink_realpath("/proc/self/exe", self_path)) {
fprintf(stderr, "failed to resolve /proc/self/exe\n");
return 1;
}
char kms_server_proxy_local_filepath[PATH_MAX];
/* Update kms-server-proxy-N to kms-server-proxy-N+1 on update (update that needs to run before this program launches itself) */
snprintf(kms_server_proxy_local_filepath, sizeof(kms_server_proxy_local_filepath), "%s/.local/share/gpu-screen-recorder/kms-server-proxy-2", user_homepath);
char gsr_kms_server_local_filepath[PATH_MAX];
snprintf(gsr_kms_server_local_filepath, sizeof(gsr_kms_server_local_filepath), "%s/.local/share/gpu-screen-recorder/gsr-kms-server", user_homepath);
if(file_has_sys_admin_capability(kms_server_proxy_local_filepath)) {
/* Need to resolve kms_server_proxy_local_filepath because /home can be a symlink to another location */
char kms_server_proxy_local_filepath_full[PATH_MAX];
if(!readlink_realpath(kms_server_proxy_local_filepath, kms_server_proxy_local_filepath_full)) {
fprintf(stderr, "failed to resolve %s\n", kms_server_proxy_local_filepath);
return 1;
}
/*
Run cached ~/.local/share/gpu-screen-recorder/kms-server-proxy which has sys admin capability.
The one in flatpak location gets capabilities overwritten on flatpak update.
*/
if(strcmp(self_path, kms_server_proxy_local_filepath_full) != 0) {
const char *args[] = { kms_server_proxy_local_filepath_full, initial_socket_path, card_path, user_homepath, NULL };
return execv(args[0], (char *const*)args);
}
const char *args[] = { "pkexec", gsr_kms_server_local_filepath, initial_socket_path, card_path, NULL };
return execvp(args[0], (char *const*)args);
} else if(geteuid() == 0) { /* is current user root? */
if(!set_kms_server_proxy_permissions_and_capabilities(kms_server_proxy_local_filepath))
return 1;
const char *args[] = { gsr_kms_server_local_filepath, initial_socket_path, card_path, NULL };
return execv(args[0], (char *const*)args);
} else {
if(!setup_local_files(user_homepath, kms_server_proxy_local_filepath, gsr_kms_server_local_filepath, NULL))
return 1;
const char *args[] = { "pkexec", kms_server_proxy_local_filepath, initial_socket_path, card_path, user_homepath, NULL };
return execvp(args[0], (char *const*)args);
}
}
static int launch_gsr_global_hotkeys(char **argv) {
char self_path[PATH_MAX];
if(!readlink_realpath("/proc/self/exe", self_path)) {
fprintf(stderr, "failed to resolve /proc/self/exe\n");
return 1;
}
const char *user_homepath = argv[2];
char kms_server_proxy_local_filepath[PATH_MAX];
/* Update kms-server-proxy-N to kms-server-proxy-N+1 on update (update that needs to run before this program launches itself) */
snprintf(kms_server_proxy_local_filepath, sizeof(kms_server_proxy_local_filepath), "%s/.local/share/gpu-screen-recorder/kms-server-proxy-2", user_homepath);
char gsr_global_hotkeys_local_filepath[PATH_MAX];
snprintf(gsr_global_hotkeys_local_filepath, sizeof(gsr_global_hotkeys_local_filepath), "%s/.local/share/gpu-screen-recorder/gsr-global-hotkeys", user_homepath);
if(!file_has_sys_admin_capability(kms_server_proxy_local_filepath)) {
fprintf(stderr, "Error: kms-server-proxy is missing cap sys admin capability\n");
return 1;
}
/* Need to resolve kms_server_proxy_local_filepath because /home can be a symlink to another location */
char kms_server_proxy_local_filepath_full[PATH_MAX];
if(!readlink_realpath(kms_server_proxy_local_filepath, kms_server_proxy_local_filepath_full)) {
fprintf(stderr, "failed to resolve %s\n", kms_server_proxy_local_filepath);
return 1;
}
/*
Run cached ~/.local/share/gpu-screen-recorder/kms-server-proxy which has sys admin capability.
The one in flatpak location gets capabilities overwritten on flatpak update.
*/
if(strcmp(self_path, kms_server_proxy_local_filepath_full) != 0) {
argv[0] = kms_server_proxy_local_filepath_full;
return execv(argv[0], argv);
}
if(setuid(0) == -1) {
fprintf(stderr, "Error: failed to switch to root user to launch gsr-global-hotkeys\n");
return 1;
}
argv[2] = gsr_global_hotkeys_local_filepath;
return execv(argv[2], argv + 2);
}
int main(int argc, char **argv) {
if(argc < 2)
usage();
if(strcmp(argv[1], "setup-gsr-ui") == 0) {
const char *user_homepath = argc == 3 ? argv[2] : NULL;
return setup_gsr_ui(user_homepath);
} else if(strcmp(argv[1], "launch-gsr-global-hotkeys") == 0) {
if(argc >= 3) {
return launch_gsr_global_hotkeys(argv);
} else {
fprintf(stderr, "Error: missing user_homepath argument to launch-gsr-global-hotkeys\n");
usage();
}
} else if(argc == 4) {
return launch_gsr_kms_server(argv[1], argv[2], argv[3]);
} else {
fprintf(stderr, "error: invalid option \"%s\".\n", argv[1]);
usage();
}
}
|