Skip to contentSkip to conversation

Add tst_secureboot_enabled() helper function

PATCH Superseded Martin Doucha 4 participants 16 messages Nov 9, 2020 – Nov 13, 2020

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 Add tst_secureboot_enabled() helper function

Also check for SecureBoot status in tst_lockdown_enabled() if the lockdown sysfile is not available/readable

---

Changes since v1:
- check whether machine is in EFI mode first

Changes since v2:
- move tst_secureboot_enabled() code to a separate header file
- move EFIVAR_CFLAGS and EFIVAR_LIBS out of global CFLAGS and LDLIBS

 configure.ac             |  1 +
 include/mk/config.mk.in  |  2 ++
 include/tst_lockdown.h   |  4 +++
 include/tst_secureboot.h | 53 ++++++++++++++++++++++++++++++++++++++++
 m4/ltp-libefivar.m4      |  9 +++++++
 5 files changed, 69 insertions(+)
 create mode 100644 include/tst_secureboot.h
 create mode 100644 m4/ltp-libefivar.m4
configure.ac +1 -0
index 03e4e09c9..d9ca5ad38 100644
@@ -296,6 +296,7 @@ LTP_CHECK_CAPABILITY_SUPPORT
 LTP_CHECK_CC_WARN_OLDSTYLE
 LTP_CHECK_CLONE_SUPPORTS_7_ARGS
 LTP_CHECK_CRYPTO
+LTP_CHECK_EFIVAR
 LTP_CHECK_FORTIFY_SOURCE
 LTP_CHECK_KERNEL_DEVEL
 LTP_CHECK_KEYUTILS_SUPPORT
include/mk/config.mk.in +2 -0
index 427608a17..45105cdab 100644
@@ -45,6 +45,8 @@ KEYUTILS_LIBS		:= @KEYUTILS_LIBS@
 HAVE_FTS_H		:= @HAVE_FTS_H@
 LIBMNL_LIBS		:= @LIBMNL_LIBS@
 LIBMNL_CFLAGS		:= @LIBMNL_CFLAGS@
+EFIVAR_CFLAGS		:= @EFIVAR_CFLAGS@
+EFIVAR_LIBS		:= @EFIVAR_LIBS@
 
 prefix			:= @prefix@
 
include/tst_lockdown.h +4 -0
index 78eaeccea..1c60151a9 100644
@@ -5,6 +5,10 @@
 
 #define PATH_LOCKDOWN	"/sys/kernel/security/lockdown"
 
+/*
+ * Note: Also check tst_secureboot_enabled(). It may indicate integrity
+ * lockdown even if tst_lockdown_enabled() returns 0.
+ */
 int tst_lockdown_enabled(void);
 
 #endif /* TST_LOCKDOWN_H */
include/tst_secureboot.h new file +53 -0

53 lines not on this page · raw message

m4/ltp-libefivar.m4 new file +9 -0
new file mode 100644
index 000000000..0a2750701
@@ -0,0 +1,9 @@
+dnl SPDX-License-Identifier: GPL-2.0-or-later
+dnl Copyright (c) 2020 SUSE LLC <[email protected]>
+
+AC_DEFUN([LTP_CHECK_EFIVAR], [
+	dnl efivar library and headers
+	PKG_CHECK_MODULES([EFIVAR], [efivar], [
+		AC_DEFINE([HAVE_EFIVAR], [1], [Define to 1 if you have libefivar library and headers])
+	], [have_efivar=no])
+])
Signature · 1 line
2.28.0
#1 lore raw

PATCH v3 2/2 syscalls/iopl, ioperm: Check for SecureBoot lockdown

SecureBoot implies integrity lockdown even if tst_lockdown_enabled() cannot check lockdown status directly. Udpate skip condition in ioperm() and iopl() tests.

---

Changes since v2:
- new patch

 testcases/kernel/syscalls/ioperm/Makefile   | 3 +++
 testcases/kernel/syscalls/ioperm/ioperm01.c | 3 ++-
 testcases/kernel/syscalls/ioperm/ioperm02.c | 5 +++++
 testcases/kernel/syscalls/iopl/Makefile     | 3 +++
 testcases/kernel/syscalls/iopl/iopl01.c     | 3 ++-
 testcases/kernel/syscalls/iopl/iopl02.c     | 6 ++++++
 6 files changed, 21 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/ioperm/Makefile +3 -0
index 044619fb8..8624e2c99 100644
@@ -5,4 +5,7 @@ top_srcdir		?= ../../../..
 
 include $(top_srcdir)/include/mk/testcases.mk
 
+CFLAGS			+= $(EFIVAR_CFLAGS)
+LDLIBS			+= $(EFIVAR_LIBS)
+
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/ioperm/ioperm01.c +2 -1
index fc5754be9..01f83aefe 100644
@@ -15,6 +15,7 @@
 #include <unistd.h>
 
 #include "tst_test.h"
+#include "tst_secureboot.h"
 
 #if defined __i386__ || defined(__x86_64__)
 #include <sys/io.h>
@@ -43,7 +44,7 @@ static void verify_ioperm(void)
 static void setup(void)
 {
 	/* ioperm() is restricted under kernel lockdown. */
-	if (tst_lockdown_enabled())
+	if (tst_lockdown_enabled() || tst_secureboot_enabled() > 0)
 		tst_brk(TCONF, "Kernel is locked down, skip this test");
 
 	/*
testcases/kernel/syscalls/ioperm/ioperm02.c +5 -0
index 1808191bf..129ca265c 100644
@@ -22,6 +22,7 @@
 #include <pwd.h>
 #include "tst_test.h"
 #include "tst_safe_macros.h"
+#include "tst_secureboot.h"
 
 #if defined __i386__ || defined(__x86_64__)
 #include <sys/io.h>
@@ -45,6 +46,10 @@ static struct tcase_t {
 
 static void setup(void)
 {
+	/* ioperm() is restricted under kernel lockdown. */
+	if (tst_lockdown_enabled() || tst_secureboot_enabled() > 0)
+		tst_brk(TCONF, "Kernel is locked down, skip this test");
+
 	/*
 	 * The value of IO_BITMAP_BITS (include/asm-i386/processor.h) changed
 	 * from kernel 2.6.8 to permit 16-bits (65536) ioperm
testcases/kernel/syscalls/iopl/Makefile +3 -0
index 044619fb8..8624e2c99 100644
@@ -5,4 +5,7 @@ top_srcdir		?= ../../../..
 
 include $(top_srcdir)/include/mk/testcases.mk
 
+CFLAGS			+= $(EFIVAR_CFLAGS)
+LDLIBS			+= $(EFIVAR_LIBS)
+
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/iopl/iopl01.c +2 -1
index dcf2cc406..60fc529e8 100644
@@ -18,6 +18,7 @@
 #include <unistd.h>
 
 #include "tst_test.h"
+#include "tst_secureboot.h"
 
 #if defined __i386__ || defined(__x86_64__)
 #include <sys/io.h>
@@ -45,7 +46,7 @@ static void verify_iopl(void)
 static void setup(void)
 {
 	/* iopl() is restricted under kernel lockdown. */
-	if (tst_lockdown_enabled())
+	if (tst_lockdown_enabled() || tst_secureboot_enabled() > 0)
 		tst_brk(TCONF, "Kernel is locked down, skip this test");
 }
 
testcases/kernel/syscalls/iopl/iopl02.c +6 -0
index 6a817cf2d..f27cfd098 100644
@@ -21,6 +21,7 @@
 #include <pwd.h>
 #include "tst_test.h"
 #include "tst_safe_macros.h"
+#include "tst_secureboot.h"
 
 #if defined __i386__ || defined(__x86_64__)
 #include <sys/io.h>
@@ -52,6 +53,11 @@ static void verify_iopl(unsigned int i)
 static void setup(void)
 {
 	struct passwd *pw;
+
+	/* ioperm() is restricted under kernel lockdown. */
+	if (tst_lockdown_enabled() || tst_secureboot_enabled() > 0)
+		tst_brk(TCONF, "Kernel is locked down, skip this test");
+
 	pw = SAFE_GETPWNAM("nobody");
 	SAFE_SETEUID(pw->pw_uid);
 }
Signature · 1 line
2.28.0
#2 lore raw

PATCH v3 2/2 syscalls/iopl, ioperm: Check for SecureBoot lockdown

Review

Quoted 7 lines · Martin Doucha

...

include $(top_srcdir)/include/mk/testcases.mk

(quoted diff) +2 -0
+CFLAGS                 += $(EFIVAR_CFLAGS)
+LDLIBS                 += $(EFIVAR_LIBS)

Where can we get the value of these two variables? Shouldn't we add AC_SUBST() in the m4 file?

Quoted 11 lines
testcases/kernel/syscalls/ioperm/ioperm02.c +1 -0
@@ -22,6 +22,7 @@
 #include <pwd.h>
 #include "tst_test.h"
 #include "tst_safe_macros.h"
+#include "tst_secureboot.h"
 
 #if defined __i386__ || defined(__x86_64__)
 #include <sys/io.h>

testcases/kernel/syscalls/ioperm/ioperm02.c static struct tcase_t { · line 52

Quoted 7 lines
testcases/kernel/syscalls/ioperm/ioperm02.c +3 -0
@@ -45,6 +46,10 @@ static struct tcase_t {
 
 static void setup(void)
 {
+       /* ioperm() is restricted under kernel lockdown. */
+       if (tst_lockdown_enabled() || tst_secureboot_enabled() > 0)
+               tst_brk(TCONF, "Kernel is locked down, skip this test");
 

The ioperm02 is an error test for ioperm(), it doesn't matter without the lockdown/secure-boot status. Better to remove this from setup().

iopl02 as well.

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20201110/bdfdaaa4/attachment.htm>
#3 lore raw

PATCH v3 2/2 syscalls/iopl, ioperm: Check for SecureBoot lockdown

Hi!

Quoted 10 lines

Review

Quoted 7 lines

...

include $(top_srcdir)/include/mk/testcases.mk

(quoted diff) +2 -0
+CFLAGS                 += $(EFIVAR_CFLAGS)
+LDLIBS                 += $(EFIVAR_LIBS)

Where can we get the value of these two variables? Shouldn't we add AC_SUBST() in the m4 file?

These are exported by the PKG_CHECK_MODULES() pkgconfig autotools macro.

Quoted 22 lines
Quoted 11 lines
testcases/kernel/syscalls/ioperm/ioperm02.c +1 -0
@@ -22,6 +22,7 @@
 #include <pwd.h>
 #include "tst_test.h"
 #include "tst_safe_macros.h"
+#include "tst_secureboot.h"
 
 #if defined __i386__ || defined(__x86_64__)
 #include <sys/io.h>

testcases/kernel/syscalls/ioperm/ioperm02.c static struct tcase_t { · line 52

Quoted 7 lines
testcases/kernel/syscalls/ioperm/ioperm02.c +3 -0
@@ -45,6 +46,10 @@ static struct tcase_t {
 
 static void setup(void)
 {
+       /* ioperm() is restricted under kernel lockdown. */
+       if (tst_lockdown_enabled() || tst_secureboot_enabled() > 0)
+               tst_brk(TCONF, "Kernel is locked down, skip this test");
 

The ioperm02 is an error test for ioperm(), it doesn't matter without the lockdown/secure-boot status. Better to remove this from setup().

Quoted 1 line

iopl02 as well.

Actually I think that this is correct, since there is no imposed order on the checks in kernel, so we may not get the errors we expect to get.

What we are actually missing are tests that iopl() and ioperm() does fail with EPERM when either of lockdown or secureboot are enabled.

Signature · 2 lines
Cyril Hrubis
[email protected]
#4 lore raw

PATCH v3 2/2 syscalls/iopl, ioperm: Check for SecureBoot lockdown

Quoted 5 lines · Cyril Hrubis

Hi!

Quoted 4 lines
Quoted 4 lines

...

include $(top_srcdir)/include/mk/testcases.mk

Quoted 9 lines
Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+CFLAGS                 += $(EFIVAR_CFLAGS)
+LDLIBS                 += $(EFIVAR_LIBS)

Where can we get the value of these two variables? Shouldn't we add AC_SUBST() in the m4 file?

These are exported by the PKG_CHECK_MODULES() pkgconfig autotools macro.

Good to know this.

Quoted 24 lines
Quoted 13 lines
Quoted 13 lines
testcases/kernel/syscalls/ioperm/ioperm02.c +1 -0
@@ -22,6 +22,7 @@
 #include <pwd.h>
 #include "tst_test.h"
 #include "tst_safe_macros.h"
+#include "tst_secureboot.h"
 
 #if defined __i386__ || defined(__x86_64__)
 #include <sys/io.h>
@@ -45,6 +46,10 @@ static struct tcase_t {
 
Quoted 5 lines

testcases/kernel/syscalls/ioperm/ioperm02.c static struct tcase_t {

Quoted 5 lines
testcases/kernel/syscalls/ioperm/ioperm02.c +3 -0
 static void setup(void)
 {
+       /* ioperm() is restricted under kernel lockdown. */
+       if (tst_lockdown_enabled() || tst_secureboot_enabled() > 0)
+               tst_brk(TCONF, "Kernel is locked down, skip this

test");

Quoted 5 lines
Quoted 1 line

The ioperm02 is an error test for ioperm(), it doesn't matter without the lockdown/secure-boot status. Better to remove this from setup().

Quoted 9 lines
Quoted 1 line

iopl02 as well.

Actually I think that this is correct, since there is no imposed order on the checks in kernel, so we may not get the errors we expect to get.

What we are actually missing are tests that iopl() and ioperm() does fail with EPERM when either of lockdown or secureboot are enabled.

I remember they(ioperm02, iopl02) works well with secure-boot enabled/disabled.
(I did that test when reviewing Erico's tst_lockdown.c patch)

Okay, but I agree that it's safer to add this check because it may change in the newer kernel someday.

Feel free to add:

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20201110/23fbd2f2/attachment.htm>
#5 lore raw

PATCH v3 2/2 syscalls/iopl, ioperm: Check for SecureBoot lockdown

In reply to Cyril Hrubis

Hi,

...

Quoted 10 lines
Quoted 8 lines

Review

Quoted 4 lines

include $(top_srcdir)/include/mk/testcases.mk

(quoted diff) +2 -0
+CFLAGS                 += $(EFIVAR_CFLAGS)
+LDLIBS                 += $(EFIVAR_LIBS)

Where can we get the value of these two variables? Shouldn't we add AC_SUBST() in the m4 file?

These are exported by the PKG_CHECK_MODULES() pkgconfig autotools macro.

FYI: I added a fix for old pkg-config (< 0.24) into m4/ltp-tirpc.m4 (the first m4 file which started to use pkg-config)
https://autotools.io/pkgconfig/pkg_check_modules.html

But 0.24 is probably old enough (2010; 0.23 was released 2008), thus we should probably remove it.

Kind regards,
Petr

#6 lore raw

PATCH v3 2/2 syscalls/iopl, ioperm: Check for SecureBoot lockdown

In reply to Martin Doucha

Hi,

Quoted 3 lines

SecureBoot implies integrity lockdown even if tst_lockdown_enabled() cannot check lockdown status directly. Udpate skip condition in ioperm() and iopl() tests.

...

testcases/kernel/syscalls/ioperm/ioperm02.c static struct tcase_t { · line 52

Quoted 4 lines
testcases/kernel/syscalls/ioperm/ioperm02.c +4 -0
+	/* ioperm() is restricted under kernel lockdown. */
+	if (tst_lockdown_enabled() || tst_secureboot_enabled() > 0)
+		tst_brk(TCONF, "Kernel is locked down, skip this test");
+

I wonder if this could be interesting for docparse to have this functionality exposed as a struct tst_test flag:
.tst_lockdown_restricted = 1

Kind regards,
Petr

#7 lore raw

PATCH v3 2/2 syscalls/iopl, ioperm: Check for SecureBoot lockdown

Hi!

Quoted 3 lines

I wonder if this could be interesting for docparse to have this functionality exposed as a struct tst_test flag:
.tst_lockdown_restricted = 1

Well if we happen to have more than two testcases in the future it would make sense to move the checks to the library like this instead.

Signature · 2 lines
Cyril Hrubis
[email protected]
#9 lore raw

PATCH v3 1/2 Add tst_secureboot_enabled() helper function

Quoted 7 lines · Petr Vorel
Quoted 2 lines

Also check for SecureBoot status in tst_lockdown_enabled() if the lockdown sysfile is not available/readable

Kind regards,
Petr

Ah, yes, I guess that part of commit message should be dropped. Sorry about that.

Signature · 7 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic
#10 lore raw

PATCH v3 1/2 Add tst_secureboot_enabled() helper function

Quoted 11 lines · Petr Vorel
Quoted 7 lines
Quoted 2 lines

Also check for SecureBoot status in tst_lockdown_enabled() if the lockdown sysfile is not available/readable

Kind regards,
Petr

Ah, yes, I guess that part of commit message should be dropped. Sorry about that.

np, maintainer will drop that.

Kind regards,
Petr

#11 lore raw

PATCH v3 1/2 Add tst_secureboot_enabled() helper function

In reply to Martin Doucha

Hi!
I've looked into the library and what it actually does in this case is that it opens a sysfs file and reads a few bytes from there. I guess that we can even avoid linking the library in this case, since we just want to know a value of the single bit in the SecureBoot file.

The full path is:

/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c

The SecureBoot is the name of the variable and the hex numbers represends the global GUID.

Now on my system with secure boot disabled the content of the file looks like:

cat /sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c |xxd
00000000: 0600 0000 00 .....

The first four bytes are attributes, we can ingore them and the last byte is the data byte, which tells us if secure boot is enabled or not.

So it may be as well easier to:

* Check if that file exists

* Read five bytes and return the last one

Signature · 2 lines
Cyril Hrubis
[email protected]
#12 lore raw

PATCH v3 1/2 Add tst_secureboot_enabled() helper function

Quoted 9 lines · Cyril Hrubis

Hi!
I've looked into the library and what it actually does in this case is that it opens a sysfs file and reads a few bytes from there. I guess that we can even avoid linking the library in this case, since we just want to know a value of the single bit in the SecureBoot file.

The full path is:

/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c

Yes, we could read the sysfile directly. But do we want to deal with potential compatibility issues and test breakage if the UEFI vars API changes in the future? The binary format of those sysfiles is controlled by the UEFI Forum, not by kernel devs. The efivars library is available on basically all modern distros and we most likely won't do any SecureBoot tests on distros that don't have it.

Signature · 7 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic
#13 lore raw

PATCH v3 1/2 Add tst_secureboot_enabled() helper function

Hi,

Quoted 9 lines · Cyril Hrubis
Quoted 7 lines

Hi!
I've looked into the library and what it actually does in this case is that it opens a sysfs file and reads a few bytes from there. I guess that we can even avoid linking the library in this case, since we just want to know a value of the single bit in the SecureBoot file.

The full path is:

Quoted 8 lines
Quoted 1 line

/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c

Yes, we could read the sysfile directly. But do we want to deal with potential compatibility issues and test breakage if the UEFI vars API changes in the future? The binary format of those sysfiles is controlled by the UEFI Forum, not by kernel devs. The efivars library is available on basically all modern distros and we most likely won't do any SecureBoot tests on distros that don't have it.

I also don't see a big deal to use the efivars library.

Kind regards,
Petr

#14 lore raw

PATCH v3 1/2 Add tst_secureboot_enabled() helper function

Quoted 14 lines · Petr Vorel

Hi,

Quoted 10 lines · Cyril Hrubis
Quoted 9 lines

Hi!
I've looked into the library and what it actually does in this case is that it opens a sysfs file and reads a few bytes from there. I guess that we can even avoid linking the library in this case, since we just want to know a value of the single bit in the SecureBoot file.

The full path is:

/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c

Quoted 9 lines
Quoted 6 lines

Yes, we could read the sysfile directly. But do we want to deal with potential compatibility issues and test breakage if the UEFI vars API changes in the future? The binary format of those sysfiles is controlled by the UEFI Forum, not by kernel devs. The efivars library is available on basically all modern distros and we most likely won't do any SecureBoot tests on distros that don't have it.

I also don't see a big deal to use the efivars library.

That's true. I have no objection to the patchset.

But we always try to avoid the LTP dependency on other libraries, in this point, I agree with Cyril.

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20201113/f273b39a/attachment.htm>
#15 lore raw

PATCH v3 1/2 Add tst_secureboot_enabled() helper function

In reply to Martin Doucha

Hi!

Quoted 7 lines
Quoted 7 lines

I've looked into the library and what it actually does in this case is that it opens a sysfs file and reads a few bytes from there. I guess that we can even avoid linking the library in this case, since we just want to know a value of the single bit in the SecureBoot file.

The full path is:

Quoted 8 lines
Quoted 1 line

/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c

Yes, we could read the sysfile directly. But do we want to deal with potential compatibility issues and test breakage if the UEFI vars API changes in the future? The binary format of those sysfiles is controlled by the UEFI Forum, not by kernel devs. The efivars library is available on basically all modern distros and we most likely won't do any SecureBoot tests on distros that don't have it.

I do not see how the code that uses the library is actually better. If the format changes you will need a newer UEFI library that will presumbly handle the difference. Which is even worse than hardcoding the stuff in LTP because the UEFI library has to be updated by a distribution.

In that case patching the code in LTP will be faster and work everywhere and not only on distributions that are fast enough to update packages.

Signature · 2 lines
Cyril Hrubis
[email protected]