bpfloader: Sanitize BPF prog and map pin paths

Replace periods with underscores in prog/map pin paths to align with
libbpf's sanitize_pin_path() behavior. The BPF filesystem (bpffs)
disallows periods in filenames, and this change prevents pinning
errors for maps with names containing them, such as .rodata sections.

Bug: 419635765
Bug: 438637519
Test: manual, load maps names containing . from ag/35064705
Change-Id: I55dd4caf38c3ccc39bccd6ea670b647c330d6793
Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
This commit is contained in:
Kalesh Singh 2025-08-15 11:24:42 -07:00
commit d49152733c

View file

@ -529,7 +529,12 @@ fn libbpf_worker(file_desc: &BpfFileDesc) -> Result<(), anyhow::Error> {
}
let pinpath_str = bpffs_path.clone() + "map_" + filename + "_" + &name;
let pinpath = Path::new(&pinpath_str);
// bpffs disallows periods in path names, so replace them with underscores
// to align with libbpf's sanitize_pin_path() behavior.
let pinpath_sanitized_str = pinpath_str.replace('.', "_");
let pinpath = Path::new(&pinpath_sanitized_str);
debug!("Pinning: {}", pinpath.display());
map.pin(pinpath).map_err(|e| anyhow!("Failed to pin map {name}: {e}"))?;
fs::set_permissions(pinpath, Permissions::from_mode(map_desc.perms as _)).map_err(
@ -574,7 +579,12 @@ fn libbpf_worker(file_desc: &BpfFileDesc) -> Result<(), anyhow::Error> {
continue;
}
let pinpath_str = bpffs_path.clone() + "prog_" + filename + "_" + &name;
let pinpath = Path::new(&pinpath_str);
// bpffs disallows periods in path names, so replace them with underscores
// to align with libbpf's sanitize_pin_path() behavior.
let pinpath_sanitized_str = pinpath_str.replace('.', "_");
let pinpath = Path::new(&pinpath_sanitized_str);
debug!("Pinning: {}", pinpath.display());
prog.pin(pinpath).map_err(|e| anyhow!("Failed to pin prog {name}: {e}"))?;
fs::set_permissions(pinpath, Permissions::from_mode(PERM_UGR as _)).map_err(