12
12
13
13
static const char testfile [] = "/tmp/test_progs_fs_kfuncs" ;
14
14
15
- static void test_xattr ( void )
15
+ static void test_get_xattr ( const char * name , const char * value , bool allow_access )
16
16
{
17
17
struct test_get_xattr * skel = NULL ;
18
18
int fd = -1 , err ;
@@ -25,7 +25,7 @@ static void test_xattr(void)
25
25
close (fd );
26
26
fd = -1 ;
27
27
28
- err = setxattr (testfile , "user.kfuncs" , "hello" , sizeof ( "hello" ) , 0 );
28
+ err = setxattr (testfile , name , value , strlen ( value ) + 1 , 0 );
29
29
if (err && errno == EOPNOTSUPP ) {
30
30
printf ("%s:SKIP:local fs doesn't support xattr (%d)\n"
31
31
"To run this test, make sure /tmp filesystem supports xattr.\n" ,
@@ -48,16 +48,23 @@ static void test_xattr(void)
48
48
goto out ;
49
49
50
50
fd = open (testfile , O_RDONLY , 0644 );
51
+
51
52
if (!ASSERT_GE (fd , 0 , "open_file" ))
52
53
goto out ;
53
54
54
- ASSERT_EQ (skel -> bss -> found_xattr_from_file , 1 , "found_xattr_from_file" );
55
-
56
55
/* Trigger security_inode_getxattr */
57
- err = getxattr (testfile , "user.kfuncs" , v , sizeof (v ));
58
- ASSERT_EQ (err , -1 , "getxattr_return" );
59
- ASSERT_EQ (errno , EINVAL , "getxattr_errno" );
60
- ASSERT_EQ (skel -> bss -> found_xattr_from_dentry , 1 , "found_xattr_from_dentry" );
56
+ err = getxattr (testfile , name , v , sizeof (v ));
57
+
58
+ if (allow_access ) {
59
+ ASSERT_EQ (err , -1 , "getxattr_return" );
60
+ ASSERT_EQ (errno , EINVAL , "getxattr_errno" );
61
+ ASSERT_EQ (skel -> bss -> found_xattr_from_file , 1 , "found_xattr_from_file" );
62
+ ASSERT_EQ (skel -> bss -> found_xattr_from_dentry , 1 , "found_xattr_from_dentry" );
63
+ } else {
64
+ ASSERT_EQ (err , strlen (value ) + 1 , "getxattr_return" );
65
+ ASSERT_EQ (skel -> bss -> found_xattr_from_file , 0 , "found_xattr_from_file" );
66
+ ASSERT_EQ (skel -> bss -> found_xattr_from_dentry , 0 , "found_xattr_from_dentry" );
67
+ }
61
68
62
69
out :
63
70
close (fd );
@@ -141,8 +148,18 @@ static void test_fsverity(void)
141
148
142
149
void test_fs_kfuncs (void )
143
150
{
144
- if (test__start_subtest ("xattr" ))
145
- test_xattr ();
151
+ /* Matches xattr_names in progs/test_get_xattr.c */
152
+ if (test__start_subtest ("user_xattr" ))
153
+ test_get_xattr ("user.kfuncs" , "hello" , true);
154
+
155
+ if (test__start_subtest ("security_bpf_xattr" ))
156
+ test_get_xattr ("security.bpf.xxx" , "hello" , true);
157
+
158
+ if (test__start_subtest ("security_bpf_xattr_error" ))
159
+ test_get_xattr ("security.bpf" , "hello" , false);
160
+
161
+ if (test__start_subtest ("security_selinux_xattr_error" ))
162
+ test_get_xattr ("security.selinux" , "hello" , false);
146
163
147
164
if (test__start_subtest ("fsverity" ))
148
165
test_fsverity ();
0 commit comments