From fa97aeb1f5236e8278a2c3adcea9c016a2a86e20 Mon Sep 17 00:00:00 2001 From: Filip Schauer Date: Tue, 19 Nov 2024 16:56:09 +0100 Subject: [PATCH] fetch-answer: encode unsafe characters in partition label Ensure potentially unsafe characters in the partition label are encoded, preventing the installer from failing to find the answer partition when the label contains whitespaces or !"$%&'()*,/;<>?[\]^`{|}~ The encoding is done according to `blkid_encode_string` [0] in the blkid util, which is used by `/lib/udev/rules.d/60-persistent-storage.rules` to create a symlink under `/dev/disk/by-label/`. For example: "ANSWER PART" is encoded to "ANSWER\x20PART" [0] https://github.com/util-linux/util-linux/blob/master/libblkid/src/encode.c Signed-off-by: Filip Schauer Tested-by: Christoph Heiss --- .../src/fetch_plugins/partition.rs | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/proxmox-fetch-answer/src/fetch_plugins/partition.rs b/proxmox-fetch-answer/src/fetch_plugins/partition.rs index 131f422..0f1fa7b 100644 --- a/proxmox-fetch-answer/src/fetch_plugins/partition.rs +++ b/proxmox-fetch-answer/src/fetch_plugins/partition.rs @@ -41,6 +41,19 @@ fn path_exists_logged(file_name: &str, search_path: &str) -> Option { } } +fn encode_partlabel(input: &str) -> String { + input + .chars() + .map(|c| { + if (' '..='~').contains(&c) && !(c.is_ascii_alphanumeric() || "#+-.:=@_".contains(c)) { + format!("\\x{:02x}", c as u32) + } else { + c.to_string() + } + }) + .collect() +} + /// Searches for the exact case, upper and finally lower case existence of the partlabel in the /// search_path, in that order. /// @@ -52,19 +65,22 @@ fn path_exists_logged(file_name: &str, search_path: &str) -> Option { /// * `partlabel_source` - Partition Label, used for matching, in the exact, upper and lower case /// * `search_path` - Path where to search for the partition label fn scan_partlabels(partlabel: &str, search_path: &str) -> Result { - if let Some(path) = path_exists_logged(partlabel, search_path) { + let partlabel_enc = encode_partlabel(partlabel); + if let Some(path) = path_exists_logged(&partlabel_enc, search_path) { info!("Found partition with label '{partlabel}'"); return Ok(path); } let partlabel_upper_case = partlabel.to_uppercase(); - if let Some(path) = path_exists_logged(&partlabel_upper_case, search_path) { + let partlabel_upper_case_enc = encode_partlabel(&partlabel_upper_case); + if let Some(path) = path_exists_logged(&partlabel_upper_case_enc, search_path) { info!("Found partition with label '{partlabel_upper_case}'"); return Ok(path); } let partlabel_lower_case = partlabel.to_lowercase(); - if let Some(path) = path_exists_logged(&partlabel_lower_case, search_path) { + let partlabel_lower_case_enc = encode_partlabel(&partlabel_lower_case); + if let Some(path) = path_exists_logged(&partlabel_lower_case_enc, search_path) { info!("Found partition with label '{partlabel_lower_case}'"); return Ok(path); }