From 4e41da6a86418e4e2a9241b42301a1abe38e7372 Mon Sep 17 00:00:00 2001 From: Quentin JEROME Date: Wed, 19 Apr 2023 10:41:09 +0200 Subject: [PATCH] =?UTF-8?q?Fixed=20BTF=C2=A0linkage=20of=20memset=20and=20?= =?UTF-8?q?memcpy=20to=20static?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Quentin JEROME --- aya-obj/src/btf/btf.rs | 103 +++++++++++++++++++++++++++++++++++------ 1 file changed, 88 insertions(+), 15 deletions(-) diff --git a/aya-obj/src/btf/btf.rs b/aya-obj/src/btf/btf.rs index ac915bfd..ec0bedd4 100644 --- a/aya-obj/src/btf/btf.rs +++ b/aya-obj/src/btf/btf.rs @@ -535,24 +535,44 @@ impl Btf { let enum_type = BtfType::Enum(Enum::new(ty.name_offset, members)); types.types[i] = enum_type; } + // Sanitize FUNC - BtfType::Func(ty) if !features.btf_func => { - debug!("{}: not supported. replacing with TYPEDEF", kind); - let typedef_type = BtfType::Typedef(Typedef::new(ty.name_offset, ty.btf_type)); - types.types[i] = typedef_type; - } - // Sanitize BTF_FUNC_GLOBAL - BtfType::Func(ty) if !features.btf_func_global => { - let mut fixed_ty = ty.clone(); - if ty.linkage() == FuncLinkage::Global { - debug!( - "{}: BTF_FUNC_GLOBAL not supported. replacing with BTF_FUNC_STATIC", - kind - ); - fixed_ty.set_linkage(FuncLinkage::Static); + BtfType::Func(ty) => { + let name = self.string_at(ty.name_offset)?; + // Sanitize FUNC + if !features.btf_func { + debug!("{}: not supported. replacing with TYPEDEF", kind); + let typedef_type = + BtfType::Typedef(Typedef::new(ty.name_offset, ty.btf_type)); + types.types[i] = typedef_type; + } else if !features.btf_func_global || name == "memset" || name == "memcpy" { + // Sanitize BTF_FUNC_GLOBAL and memset, memcpy + let mut fixed_ty = ty.clone(); + if ty.linkage() == FuncLinkage::Global { + debug!( + "{}: BTF_FUNC_GLOBAL not supported. replacing with BTF_FUNC_STATIC", + kind + ); + fixed_ty.set_linkage(FuncLinkage::Static); + } + types.types[i] = BtfType::Func(fixed_ty); } - types.types[i] = BtfType::Func(fixed_ty); + + // Sanitize BTF_FUNC_GLOBAL and memset, memcpy + /*let name = self.string_at(ty.name_offset)?; + if !features.btf_func_global || name == "memset" || name == "memcpy" { + let mut fixed_ty = ty.clone(); + if ty.linkage() == FuncLinkage::Global { + debug!( + "{}: BTF_FUNC_GLOBAL not supported. replacing with BTF_FUNC_STATIC", + kind + ); + fixed_ty.set_linkage(FuncLinkage::Static); + } + types.types[i] = BtfType::Func(fixed_ty); + }*/ } + // Sanitize FLOAT BtfType::Float(ty) if !features.btf_float => { debug!("{}: not supported. replacing with STRUCT", kind); @@ -1350,6 +1370,59 @@ mod tests { Btf::parse(&raw, Endianness::default()).unwrap(); } + #[test] + fn test_sanitize_memset_memcpy() { + let mut btf = Btf::new(); + let name_offset = btf.add_string("int".to_string()); + let int_type_id = btf.add_type(BtfType::Int(Int::new( + name_offset, + 4, + IntEncoding::Signed, + 0, + ))); + + let params = vec![ + BtfParam { + name_offset: btf.add_string("a".to_string()), + btf_type: int_type_id, + }, + BtfParam { + name_offset: btf.add_string("b".to_string()), + btf_type: int_type_id, + }, + ]; + let func_proto_type_id = + btf.add_type(BtfType::FuncProto(FuncProto::new(params, int_type_id))); + + ["memset", "memcpy"].iter().for_each(|fname| { + let func_name_offset = btf.add_string(fname.to_string()); + let func_type_id = btf.add_type(BtfType::Func(Func::new( + func_name_offset, + func_proto_type_id, + FuncLinkage::Global, + ))); + + let features = BtfFeatures { + btf_func: true, + btf_func_global: true, // to force function name check + ..Default::default() + }; + + btf.fixup_and_sanitize(&HashMap::new(), &HashMap::new(), &features) + .unwrap(); + + if let BtfType::Func(fixed) = btf.type_by_id(func_type_id).unwrap() { + assert!(fixed.linkage() == FuncLinkage::Static); + } else { + panic!("not a func") + } + + // Ensure we can convert to bytes and back again + let raw = btf.to_bytes(); + Btf::parse(&raw, Endianness::default()).unwrap(); + }); + } + #[test] fn test_sanitize_float() { let mut btf = Btf::new();