Skip to contentSkip to conversation

tests/keys: skip KEYCTL_DH_COMPUTE when unavailable

PATCH Superseded Pablo Alessandro Santos Hugen 2 participants 5 messages Oct 18 – Oct 21 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/keys: skip KEYCTL_DH_COMPUTE when unavailable

Since 32f07cc40c9b ("crypto: dh - disallow plain "dh" usage in FIPS mode") landed in linux 5.18, plain "dh" is disallowed in FIPS mode per SP800-56Arev3. As noted in the commit:

Quoted 6 lines

This change will effectively render the keyctl(KEYCTL_DH_COMPUTE) syscall unusable in FIPS mode, but it has been brought up that this might even be a good thing ([1])."

[1] https://lore.kernel.org/r/[email protected]

keyctl(KEYCTL_DH_COMPUTE) returns ENOENT in FIPS mode.

So, this patch treat ENOENT and ENOSYS as non-fatal in keyctl.c (because the operation is unavailable, not denied by policy). SELinux denials (EACCES) still exit 9. Skip test_key_no_read_t in FIPS mode since KEYCTL_DH_COMPUTE is the only operation requiring key { read }.

Fixes 2d7aad8a1f8c ("selinux-testsuite: Add keys tests")
---
 tests/keys/keyctl.c       | 15 ++++++++++-----
 tests/keys/request_keys.c | 15 ++++++++++-----
 tests/keys/test           | 21 +++++++++++++++------
 3 files changed, 35 insertions(+), 16 deletions(-)
tests/keys/keyctl.c +10 -5
index e13c655..261c00a 100644
@@ -142,12 +142,17 @@ int main(int argc, char *argv[])
 	/* Requires key { create read write } */
 	compute = keyctl(KEYCTL_DH_COMPUTE, &params, NULL, 0, 0);
 	if (compute < 0) {
-		fprintf(stderr, "Failed KEYCTL_DH_COMPUTE: %s\n",
-			strerror(errno));
-		exit(9);
-	}
-	if (verbose)
+		if (errno == ENOENT || errno == ENOSYS) {
+			if (verbose)
+				printf("KEYCTL_DH_COMPUTE not available, skipping\n");
+		} else {
+			fprintf(stderr, "Failed KEYCTL_DH_COMPUTE: %s\n",
+				strerror(errno));
+			exit(9);
+		}
+	} else if (verbose) {
 		printf("KEYCTL_DH_COMPUTE key ID size: %d\n", compute);
+	}
 
 	/* Requires key { write link } */
 	link = keyctl(KEYCTL_LINK, base, KEY_SPEC_PROCESS_KEYRING);
tests/keys/request_keys.c +10 -5
index 43b20d9..7cf7cd0 100644
@@ -127,12 +127,17 @@ int main(int argc, char **argv)
 	/* Requires key { read write } */
 	compute = keyctl(KEYCTL_DH_COMPUTE, &params, NULL, 0, 0);
 	if (compute < 0) {
-		fprintf(stderr, "Failed KEYCTL_DH_COMPUTE: %s\n",
-			strerror(errno));
-		exit(8);
-	}
-	if (verbose)
+		if (errno == ENOENT || errno == ENOSYS) {
+			if (verbose)
+				printf("\tKEYCTL_DH_COMPUTE not available, skipping\n");
+		} else {
+			fprintf(stderr, "Failed KEYCTL_DH_COMPUTE: %s\n",
+				strerror(errno));
+			exit(8);
+		}
+	} else if (verbose) {
 		printf("\tKEYCTL_DH_COMPUTE key ID size: %d\n", compute);
+	}
 
 	exit(0);
 }
tests/keys/test +15 -6
index 061bc62..9e50633 100755
@@ -29,6 +29,9 @@ BEGIN {
         $test_link_53 = 1;
     }
 
+    # KEYCTL_DH_COMPUTE is disabled in FIPS mode
+    chomp( $fips = `cat /proc/sys/crypto/fips_enabled 2>/dev/null` );
+    $skip_dh = ( $fips eq "1" ) ? 1 : 0;
     plan tests => $test_count;
 }
 
@@ -51,8 +54,11 @@ ok( $result >> 8 eq 6 );
 $result = system "runcon -t test_key_no_view_t $basedir/keyctl $v 2>&1";
 ok( $result >> 8 eq 8 );
 
-$result = system "runcon -t test_key_no_read_t $basedir/keyctl $v 2>&1";
-ok( $result >> 8 eq 9 );
+SKIP: {
+    skip( "KEYCTL_DH_COMPUTE unavailable in FIPS mode", 1 ) if $skip_dh;
+    $result = system "runcon -t test_key_no_read_t $basedir/keyctl $v 2>&1";
+    ok( $result >> 8 eq 9 );
+}
 
 $result = system "runcon -t test_key_no_link_t $basedir/keyctl $v 2>&1";
 if ($test_link_53) {
@@ -101,9 +107,12 @@ $result = system(
 );
 ok( $result >> 8 eq 6 );
 
-$result = system(
-"runcon -t test_keyring_service_t $basedir/keyring_service $v test_request_keys_no_read_t $basedir/request_keys 2>&1"
-);
-ok( $result >> 8 eq 8 );
+SKIP: {
+    skip( "KEYCTL_DH_COMPUTE unavailable in FIPS mode", 1 ) if $skip_dh;
+    $result = system(
+    "runcon -t test_keyring_service_t $basedir/keyring_service $v test_request_keys_no_read_t $basedir/request_keys 2>&1"
+    );
+    ok( $result >> 8 eq 8 );
+}
 
 exit;
Signature · 1 line
2.56.0
maintainer
#1 lore raw
Quoted 33 lines · Pablo Alessandro Santos Hugen

Since 32f07cc40c9b ("crypto: dh - disallow plain "dh" usage in FIPS mode") landed in linux 5.18, plain "dh" is disallowed in FIPS mode per SP800-56Arev3. As noted in the commit:

Quoted 6 lines

This change will effectively render the keyctl(KEYCTL_DH_COMPUTE) syscall unusable in FIPS mode, but it has been brought up that this might even be a good thing ([1])."

[1] https://lore.kernel.org/r/[email protected]

keyctl(KEYCTL_DH_COMPUTE) returns ENOENT in FIPS mode.

So, this patch treat ENOENT and ENOSYS as non-fatal in keyctl.c (because the operation is unavailable, not denied by policy). SELinux denials (EACCES) still exit 9. Skip test_key_no_read_t in FIPS mode since KEYCTL_DH_COMPUTE is the only operation requiring key { read }.

Fixes 2d7aad8a1f8c ("selinux-testsuite: Add keys tests")
---
 tests/keys/keyctl.c       | 15 ++++++++++-----
 tests/keys/request_keys.c | 15 ++++++++++-----
 tests/keys/test           | 21 +++++++++++++++------
 3 files changed, 35 insertions(+), 16 deletions(-)
tests/keys/keyctl.c +0 -0
index e13c655..261c00a 100644

tests/keys/keyctl.c int main(int argc, char *argv[]) · line 145

Quoted 9 lines
tests/keys/keyctl.c +1 -5
@@ -142,12 +142,17 @@ int main(int argc, char *argv[])
        /* Requires key { create read write } */
        compute = keyctl(KEYCTL_DH_COMPUTE, &params, NULL, 0, 0);
        if (compute < 0) {
-               fprintf(stderr, "Failed KEYCTL_DH_COMPUTE: %s\n",
-                       strerror(errno));
-               exit(9);
-       }
-       if (verbose)
+               if (errno == ENOENT || errno == ENOSYS) {

KEYCTL_DH_COMPUTE never returns ENOSYS; with !CONFIG_KEY_DH_OPERATIONS the stub in security/keys/internal.h returns -EOPNOTSUPP, and the sys_keyctl default case is also -EOPNOTSUPP. If you want to cover kernels built with DH support, this should be EOPNOTSUPP instead (both files).

Quoted 5 lines
tests/keys/request_keys.c +0 -0
index 43b20d9..7cf7cd0 100644

tests/keys/request_keys.c int main(int argc, char **argv) · line 130

Quoted 9 lines
tests/keys/request_keys.c +1 -5
@@ -127,12 +127,17 @@ int main(int argc, char **argv)
        /* Requires key { read write } */
        compute = keyctl(KEYCTL_DH_COMPUTE, &params, NULL, 0, 0);
        if (compute < 0) {
-               fprintf(stderr, "Failed KEYCTL_DH_COMPUTE: %s\n",
-                       strerror(errno));
-               exit(8);
-       }
-       if (verbose)
+               if (errno == ENOENT || errno == ENOSYS) {

Same comment as above.

Quoted 5 lines
tests/keys/test +0 -0
index 061bc62..9e50633 100755

tests/keys/test ok( $result >> 8 eq 6 ); · line 61

Quoted 10 lines
tests/keys/test +5 -2
@@ -51,8 +54,11 @@ ok( $result >> 8 eq 6 );
 $result = system "runcon -t test_key_no_view_t $basedir/keyctl $v 2>&1";
 ok( $result >> 8 eq 8 );
 
-$result = system "runcon -t test_key_no_read_t $basedir/keyctl $v 2>&1";
-ok( $result >> 8 eq 9 );
+SKIP: {
+    skip( "KEYCTL_DH_COMPUTE unavailable in FIPS mode", 1 ) if $skip_dh;
+    $result = system "runcon -t test_key_no_read_t $basedir/keyctl $v 2>&1";
+    ok( $result >> 8 eq 9 );
+}

This SKIP block and the one below shouldn't be necessary. In __keyctl_dh_compute() the three dh_data_from_key() calls each do lookup_user_key(id, 0, KEY_NEED_READ) and run before crypto_alloc_kpp("dh"). key_task_permission() for KEY_NEED_READ ends with security_key_permission() as the final arbiter, so for test_key_no_read_t the SELinux denial fires there, lookup_user_key() returns -EACCES, and dh_data_from_key() squashes it to -ENOKEY. ENOKEY isn't ENOENT or ENOSYS, so with just the C-side change the no_read_t runs still exit 9 / 8 under FIPS exactly as they do today; the FIPS-mode ENOENT is never reached when read is denied.

tests/keys/test $result = system( · line 116

Quoted 11 lines
tests/keys/test +7 -4
-$result = system(
-"runcon -t test_keyring_service_t $basedir/keyring_service $v test_request_keys_no_read_t $basedir/request_keys 2>&1"
-);
-ok( $result >> 8 eq 8 );
+SKIP: {
+    skip( "KEYCTL_DH_COMPUTE unavailable in FIPS mode", 1 ) if $skip_dh;
+    $result = system(
+    "runcon -t test_keyring_service_t $basedir/keyring_service $v test_request_keys_no_read_t $basedir/request_keys 2>&1"
+    );
+    ok( $result >> 8 eq 8 );
+}

See above comment.

#3 lore raw

PATCH v2 1/1 tests/keys: skip KEYCTL_DH_COMPUTE when unavailable

In reply to Pablo Alessandro Santos Hugen

Since 32f07cc40c9b ("crypto: dh - disallow plain "dh" usage in FIPS mode") landed in linux 5.18, plain "dh" is disallowed in FIPS mode per SP800-56Arev3. keyctl(KEYCTL_DH_COMPUTE) returns ENOENT in FIPS mode.

Treat ENOENT and EOPNOTSUPP as non-fatal in keyctl.c and request_keys.c (the operation is unavailable, not denied by policy). SELinux denials (EACCES) still exit 9/8.

Fixes 2d7aad8a1f8c ("selinux-testsuite: Add keys tests")

---
Changes in v2 (Stephen Smalley):
- Replace ENOSYS with EOPNOTSUPP — KEYCTL_DH_COMPUTE never returns ENOSYS; the !CONFIG_KEY_DH_OPERATIONS stub returns EOPNOTSUPP
- Drop SKIP blocks in Perl test — SELinux denial fires before DH computation (EACCES -> ENOKEY), so no_read_t tests still pass under FIPS

 tests/keys/keyctl.c       | 15 ++++++++++-----
 tests/keys/request_keys.c | 15 ++++++++++-----
 2 files changed, 20 insertions(+), 10 deletions(-)
tests/keys/keyctl.c +10 -5
index e13c655..5e98f11 100644
@@ -142,12 +142,17 @@ int main(int argc, char *argv[])
 	/* Requires key { create read write } */
 	compute = keyctl(KEYCTL_DH_COMPUTE, &params, NULL, 0, 0);
 	if (compute < 0) {
-		fprintf(stderr, "Failed KEYCTL_DH_COMPUTE: %s\n",
-			strerror(errno));
-		exit(9);
-	}
-	if (verbose)
+		if (errno == ENOENT || errno == EOPNOTSUPP) {
+			if (verbose)
+				printf("KEYCTL_DH_COMPUTE not available, skipping\n");
+		} else {
+			fprintf(stderr, "Failed KEYCTL_DH_COMPUTE: %s\n",
+				strerror(errno));
+			exit(9);
+		}
+	} else if (verbose) {
 		printf("KEYCTL_DH_COMPUTE key ID size: %d\n", compute);
+	}
 
 	/* Requires key { write link } */
 	link = keyctl(KEYCTL_LINK, base, KEY_SPEC_PROCESS_KEYRING);
tests/keys/request_keys.c +10 -5
index 43b20d9..291a522 100644
@@ -127,12 +127,17 @@ int main(int argc, char **argv)
 	/* Requires key { read write } */
 	compute = keyctl(KEYCTL_DH_COMPUTE, &params, NULL, 0, 0);
 	if (compute < 0) {
-		fprintf(stderr, "Failed KEYCTL_DH_COMPUTE: %s\n",
-			strerror(errno));
-		exit(8);
-	}
-	if (verbose)
+		if (errno == ENOENT || errno == EOPNOTSUPP) {
+			if (verbose)
+				printf("\tKEYCTL_DH_COMPUTE not available, skipping\n");
+		} else {
+			fprintf(stderr, "Failed KEYCTL_DH_COMPUTE: %s\n",
+				strerror(errno));
+			exit(8);
+		}
+	} else if (verbose) {
 		printf("\tKEYCTL_DH_COMPUTE key ID size: %d\n", compute);
+	}
 
 	exit(0);
 }
Signature · 1 line
2.56.0
maintainer Acked-by on v2 1/1
#4 lore raw
Quoted 6 lines · Pablo Alessandro Santos Hugen

Since 32f07cc40c9b ("crypto: dh - disallow plain "dh" usage in FIPS mode") landed in linux 5.18, plain "dh" is disallowed in FIPS mode per SP800-56Arev3. keyctl(KEYCTL_DH_COMPUTE) returns ENOENT in FIPS mode.

Quoted 6 lines

Treat ENOENT and EOPNOTSUPP as non-fatal in keyctl.c and request_keys.c (the operation is unavailable, not denied by policy). SELinux denials (EACCES) still exit 9/8.

Fixes 2d7aad8a1f8c ("selinux-testsuite: Add keys tests")

Note that this will skip this testing on FIPS-enabled hosts so it won't be exercised if your testing infrastructure always enables FIPS; that's ok with me but noting for the record.