firstboot: validate keymap entry

As described in #30940, systemd-firstboot currently does not perform
any validation on keymap entry, allowing nonexistent keymaps to be
written to /etc/vconsole.conf. This commit adds validation checks
based on those already performed on locale entry, preventing invalid
keymaps from being set.

Closes #30940

m
This commit is contained in:
Eric Daigle
2024-02-08 23:09:34 -08:00
committed by Frantisek Sumsal
parent 3588c510d3
commit 321a8c595e

View File

@@ -458,6 +458,20 @@ static int process_locale(int rfd) {
return 1;
}
static bool keymap_exists_bool(const char *name) {
return keymap_exists(name) > 0;
}
static typeof(&keymap_is_valid) determine_keymap_validity_func(int rfd) {
int r;
r = dir_fd_is_root(rfd);
if (r < 0)
log_debug_errno(r, "Unable to determine if operating on host root directory, assuming we are: %m");
return r != 0 ? keymap_exists_bool : keymap_is_valid;
}
static int prompt_keymap(int rfd) {
_cleanup_strv_free_ char **kmaps = NULL;
int r;
@@ -489,7 +503,7 @@ static int prompt_keymap(int rfd) {
print_welcome(rfd);
return prompt_loop("Please enter system keymap name or number",
kmaps, 60, keymap_is_valid, &arg_keymap);
kmaps, 60, determine_keymap_validity_func(rfd), &arg_keymap);
}
static int process_keymap(int rfd) {
@@ -1695,6 +1709,8 @@ static int run(int argc, char *argv[]) {
/* We check these conditions here instead of in parse_argv() so that we can take the root directory
* into account. */
if (arg_keymap && !determine_keymap_validity_func(rfd)(arg_keymap))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Keymap %s is not installed.", arg_keymap);
if (arg_locale && !locale_is_ok(rfd, arg_locale))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale);
if (arg_locale_messages && !locale_is_ok(rfd, arg_locale_messages))