@@ -19,8 +19,6 @@ use cap_std_ext::cap_std::fs::{Metadata, MetadataExt};
1919#[ cfg( feature = "install" ) ]
2020use cap_std_ext:: dirext:: CapStdExtDirExt ;
2121use fn_error_context:: context;
22- #[ cfg( feature = "install" ) ]
23- use gvariant:: { aligned_bytes:: TryAsAligned , Marker , Structure } ;
2422use ostree_ext:: gio;
2523use ostree_ext:: ostree;
2624use rustix:: fd:: AsFd ;
@@ -177,12 +175,12 @@ pub(crate) fn selinux_set_permissive(permissive: bool) -> Result<()> {
177175#[ cfg( feature = "install" ) ]
178176/// Check if the ostree-formatted extended attributes include a security.selinux value.
179177pub ( crate ) fn xattrs_have_selinux ( xattrs : & ostree:: glib:: Variant ) -> bool {
180- let v = xattrs. data_as_bytes ( ) ;
181- let v = v . try_as_aligned ( ) . unwrap ( ) ;
182- let v = gvariant :: gv! ( "a(ayay)" ) . cast ( v ) ;
183- for xattr in v . iter ( ) {
184- let k = xattr . to_tuple ( ) . 0 ;
185- if k == SELINUX_XATTR {
178+ let n = xattrs. n_children ( ) ;
179+ for i in 0 ..n {
180+ let child = xattrs . child_value ( i ) ;
181+ let key = child . child_value ( 0 ) ;
182+ let key = key . data_as_bytes ( ) ;
183+ if key == SELINUX_XATTR {
186184 return true ;
187185 }
188186 }
@@ -419,3 +417,19 @@ where
419417 f ( w)
420418 } )
421419}
420+
421+ #[ cfg( test) ]
422+ mod tests {
423+ use super :: * ;
424+ use gio:: glib:: Variant ;
425+
426+ #[ test]
427+ fn test_selinux_xattr ( ) {
428+ let notfound: & [ & [ ( & [ u8 ] , & [ u8 ] ) ] ] = & [ & [ ] , & [ ( b"foo" , b"bar" ) ] ] ;
429+ for case in notfound {
430+ assert ! ( !xattrs_have_selinux( & Variant :: from( case) ) ) ;
431+ }
432+ let found: & [ ( & [ u8 ] , & [ u8 ] ) ] = & [ ( b"foo" , b"bar" ) , ( SELINUX_XATTR , b"foo_t" ) ] ;
433+ assert ! ( xattrs_have_selinux( & Variant :: from( found) ) ) ;
434+ }
435+ }
0 commit comments