Skip to content

Commit c7eb0e8

Browse files
committed
rust: dynamically add libpython*.so.1.0 to allowed libraries list
This is more accurate. This may catch version mismatch someday.
1 parent f163c2e commit c7eb0e8

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

src/main.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ const ELF_ALLOWED_LIBRARIES: &[&str] = &[
5454
"libpthread.so.0",
5555
"librt.so.1",
5656
"libutil.so.1",
57-
// Our set.
58-
"libpython3.8.so.1.0",
59-
"libpython3.8d.so.1.0",
60-
"libpython3.9.so.1.0",
61-
"libpython3.9d.so.1.0",
62-
"libpython3.10.so.1.0",
63-
"libpython3.10d.so.1.0",
6457
];
6558

6659
const PE_ALLOWED_LIBRARIES: &[&str] = &[
@@ -384,6 +377,7 @@ fn allowed_dylibs_for_triple(triple: &str) -> Vec<MachOAllowedDylib> {
384377

385378
fn validate_elf(
386379
target_triple: &str,
380+
python_major_minor: &str,
387381
path: &Path,
388382
elf: &goblin::elf::Elf,
389383
bytes: &[u8],
@@ -413,13 +407,19 @@ fn validate_elf(
413407
));
414408
}
415409

416-
let mut allowed_libraries = ELF_ALLOWED_LIBRARIES.to_vec();
410+
let mut allowed_libraries = ELF_ALLOWED_LIBRARIES
411+
.iter()
412+
.map(|x| x.to_string())
413+
.collect::<Vec<_>>();
417414
if let Some(extra) = ELF_ALLOWED_LIBRARIES_BY_TRIPLE.get(target_triple) {
418-
allowed_libraries.extend(extra.iter());
415+
allowed_libraries.extend(extra.iter().map(|x| x.to_string()));
419416
}
420417

418+
allowed_libraries.push(format!("libpython{}.so.1.0", python_major_minor));
419+
allowed_libraries.push(format!("libpython{}d.so.1.0", python_major_minor));
420+
421421
for lib in &elf.libraries {
422-
if !allowed_libraries.contains(lib) {
422+
if !allowed_libraries.contains(&lib.to_string()) {
423423
errors.push(format!("{} loads illegal library {}", path.display(), lib));
424424
}
425425
}
@@ -578,6 +578,11 @@ fn validate_distribution(dist_path: &Path) -> Result<Vec<String>> {
578578
let mut seen_dylibs = BTreeSet::new();
579579
let mut seen_paths = BTreeSet::new();
580580

581+
let dist_filename = dist_path
582+
.file_name()
583+
.expect("unable to obtain filename")
584+
.to_string_lossy();
585+
581586
let fh = std::fs::File::open(&dist_path)
582587
.with_context(|| format!("unable to open {}", dist_path.display()))?;
583588

@@ -595,6 +600,16 @@ fn validate_distribution(dist_path: &Path) -> Result<Vec<String>> {
595600
)
596601
})?;
597602

603+
let python_major_minor = if dist_filename.starts_with("cpython-3.8.") {
604+
"3.8"
605+
} else if dist_filename.starts_with("cpython-3.9.") {
606+
"3.9"
607+
} else if dist_filename.starts_with("cpython-3.10.") {
608+
"3.10"
609+
} else {
610+
return Err(anyhow!("could not parse Python version from filename"));
611+
};
612+
598613
let reader = std::io::BufReader::new(fh);
599614
let dctx = zstd::stream::Decoder::new(reader)?;
600615
let mut tf = tar::Archive::new(dctx);
@@ -611,7 +626,13 @@ fn validate_distribution(dist_path: &Path) -> Result<Vec<String>> {
611626
if let Ok(object) = goblin::Object::parse(&data) {
612627
match object {
613628
goblin::Object::Elf(elf) => {
614-
errors.extend(validate_elf(triple, path.as_ref(), &elf, &data)?);
629+
errors.extend(validate_elf(
630+
triple,
631+
python_major_minor,
632+
path.as_ref(),
633+
&elf,
634+
&data,
635+
)?);
615636
}
616637
goblin::Object::Mach(mach) => match mach {
617638
goblin::mach::Mach::Binary(macho) => {

0 commit comments

Comments
 (0)