Skip to contentSkip to conversation

lib: add function to check for kernel lockdown

PATCH New Erico Nunes 3 participants 8 messages Jul 28, 2020 – Jul 30, 2020 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 lib: add function to check for kernel lockdown

Some syscalls are not available if the kernel is booted using the 'lockdown' feature. That can cause some tests to report fail, showing a message like:

Lockdown: iopl01: iopl is restricted; see man kernel_lockdown.7

This patch adds a function that can be used by tests to check for this case, so it can be handled accordingly.

---
v2:
- just return the lockdown status so tests can handle as needed, instead of just always skipping the test.
- handle fgets return value to avoid compiler warning (can't return any sensible value so I just ported what I have seen in other similar cases).

---
 include/tst_lockdown.h |  8 ++++++++
 include/tst_test.h     |  1 +
 lib/tst_lockdown.c     | 31 +++++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+)
 create mode 100644 include/tst_lockdown.h
 create mode 100644 lib/tst_lockdown.c
include/tst_lockdown.h new file +8 -0
new file mode 100644
index 000000000..383026b1e
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#ifndef TST_LOCKDOWN_H
+#define TST_LOCKDOWN_H
+
+int tst_lockdown_enabled(void);
+
+#endif /* TST_LOCKDOWN_H */
include/tst_test.h +1 -0
index b84f7b9dd..b02de4597 100644
@@ -40,6 +40,7 @@
 #include "tst_hugepage.h"
 #include "tst_assert.h"
 #include "tst_cgroup.h"
+#include "tst_lockdown.h"
 
 /*
  * Reports testcase result.
lib/tst_lockdown.c new file +31 -0
new file mode 100644
index 000000000..024047aae
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#define TST_NO_DEFAULT_MAIN
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mount.h>
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+#include "tst_safe_stdio.h"
+#include "tst_lockdown.h"
+
+int tst_lockdown_enabled(void)
+{
+	char line[BUFSIZ];
+	const char *lockdown_path = "/sys/kernel/security/lockdown";
+	FILE *file;
+
+	if (access("/sys/kernel/security/lockdown", F_OK) != 0) {
+		tst_res(TINFO, "Unable to determine system lockdown state\n");
+		return 0;
+	}
+
+	file = SAFE_FOPEN(lockdown_path, "r");
+	if (!fgets(line, sizeof(line), file))
+		tst_brk(TBROK | TERRNO, "fgets %s", lockdown_path);
+	SAFE_FCLOSE(file);
+
+	return (strstr(line, "[none]") == NULL);
+}
Signature · 1 line
2.26.2
#1 lore raw

PATCH v2 2/3 ioperm01: skip test if kernel is locked down

ioperm is restricted under kernel lockdown.

---
v2:
- update to skip the test here rather than relying on the helper function to skip.

---
 testcases/kernel/syscalls/ioperm/ioperm01.c | 4 ++++
 1 file changed, 4 insertions(+)
testcases/kernel/syscalls/ioperm/ioperm01.c +4 -0
index 4c5c0e6ea..c25baa678 100644
@@ -42,6 +42,10 @@ static void verify_ioperm(void)
 
 static void setup(void)
 {
+	/* ioperm is restricted under kernel lockdown. */
+	if (tst_lockdown_enabled())
+		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 ioperm
Signature · 1 line
2.26.2
#2 lore raw

PATCH v2 3/3 iopl01: skip test if kernel is locked down

In reply to Erico Nunes

iopl is restricted under kernel lockdown.

---
v2:
- update to skip the test here rather than relying on the helper function to skip.

---
 testcases/kernel/syscalls/iopl/iopl01.c | 9 +++++++++
 1 file changed, 9 insertions(+)
testcases/kernel/syscalls/iopl/iopl01.c +9 -0
index edf586cd1..e5ec1147f 100644
@@ -42,6 +42,14 @@ static void verify_iopl(void)
 	}
 }
 
+static void setup(void)
+{
+	/* iopl is restricted under kernel lockdown. */
+	if (tst_lockdown_enabled())
+		tst_brk(TCONF, "Kernel is locked down, skip this test");
+
+}
+
 static void cleanup(void)
 {
 	/*
@@ -54,6 +62,7 @@ static void cleanup(void)
 static struct tst_test test = {
 	.test_all = verify_iopl,
 	.needs_root = 1,
+	.setup = setup,
 	.cleanup = cleanup,
 };
 
Signature · 1 line
2.26.2
#3 lore raw

PATCH v2 1/3 lib: add function to check for kernel lockdown

In reply to Erico Nunes

Thanks Erico for patch V2.

Quoted 13 lines · Erico Nunes

Some syscalls are not available if the kernel is booted using the 'lockdown' feature. That can cause some tests to report fail, showing a message like:

Lockdown: iopl01: iopl is restricted; see man kernel_lockdown.7

This patch adds a function that can be used by tests to check for this case, so it can be handled accordingly.

---
...

lib/tst_lockdown.c line 17

Quoted 4 lines
lib/tst_lockdown.c new file +4 -0
+int tst_lockdown_enabled(void)
+{
+       char line[BUFSIZ];
+       const char *lockdown_path = "/sys/kernel/security/lockdown";

I prefer to add a macro definition in the header file instead of this ^.

    #define PATH_LOCKDOWN  "/sys/kernel/security/lockdown"

Considering some distribution's LSM feature has not aligned with the mainline kernel, so I think this method is enough to detect the lockdown status at currently, if some new changes happening then we can help improve the function as well.

Anyway, the whole patchset looks good, if nobody has objection I will help merge it one day later.

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

PATCH v2 1/3 lib: add function to check for kernel lockdown

Quoted 3 lines · Cyril Hrubis

Looks good to me as well.

Pushed.

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

PATCH v2 1/3 lib: add function to check for kernel lockdown

In reply to Li Wang
Quoted 24 lines · Li Wang

Thanks Erico for patch V2.

On Wed, Jul 29, 2020 at 12:23 AM Erico Nunes <[email protected] <mailto:[email protected]>> wrote:

Some syscalls are not available if the kernel is booted using the 'lockdown' feature. That can cause some tests to report fail, showing a message like:

? Lockdown: iopl01: iopl is restricted; see man kernel_lockdown.7

This patch adds a function that can be used by tests to check for this case, so it can be handled accordingly.

<mailto:[email protected]>>
---
...
+int tst_lockdown_enabled(void)
+{
+? ? ? ?char line[BUFSIZ];
+? ? ? ?const char *lockdown_path = "/sys/kernel/security/lockdown";

Quoted 2 lines

I prefer to add a macro definition in the header file instead of this ^. ? ? #define PATH_LOCKDOWN? "/sys/kernel/security/lockdown"

I'm ok with that, do you want me to submit another version like this or can you change while applying?

Thank you

Erico

#7 lore raw

PATCH v2 1/3 lib: add function to check for kernel lockdown

Quoted 6 lines
Quoted 2 lines

I prefer to add a macro definition in the header file instead of this ^.

    #define PATH_LOCKDOWN  "/sys/kernel/security/lockdown"

I'm ok with that, do you want me to submit another version like this or can you change while applying?

No needed for a new version, I can help do that.

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200729/0a6edb69/attachment.htm>