lib: add function to check for kernel lockdown
PATCH 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 tests can be skipped rather than reporting a test failure.
--- include/tst_lockdown.h | 8 ++++++++ include/tst_test.h | 1 + lib/tst_lockdown.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 include/tst_lockdown.h create mode 100644 lib/tst_lockdown.c
include/tst_lockdown.h new file +8 -0
@@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef TST_LOCKDOWN_H +#define TST_LOCKDOWN_H + +void tst_lockdown_skip (void ); + +#endif /* TST_LOCKDOWN_H */
include/tst_test.h +1 -0
@@ -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 +28 -0
@@ -0,0 +1,28 @@ +// 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" + +void tst_lockdown_skip (void ) +{ +char line[BUFSIZ ]; +FILE *file; + +if (access ("/sys/kernel/security/lockdown" ,F_OK ) !=0 ) +return ; + + file =SAFE_FOPEN ("/sys/kernel/security/lockdown" ,"r" ); +fgets (line,sizeof (line), file); +SAFE_FCLOSE (file); + +if (strstr (line,"[none]" ) ==NULL ) +tst_brk (TCONF ,"Kernel is locked down, skip this test." ); +}
Signature · 1 line
2.26.2
PATCH 2/3 ioperm01: skip test if kernel is locked down
ioperm is restricted under kernel lockdown.
--- testcases/kernel/syscalls/ioperm/ioperm01.c | 3 +++ 1 file changed, 3 insertions(+)
testcases/kernel/syscalls/ioperm/ioperm01.c +3 -0
@@ -42,6 +42,9 @@ static void verify_ioperm(void)static void setup (void ) { +/* ioperm is restricted under kernel lockdown. */ +tst_lockdown_skip (); +/* * 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
PATCH 2/3 ioperm01: skip test if kernel is locked down
Hi!
Quoted 12 lines
ioperm is restricted under kernel lockdown.
Signed-off-by Erico Nunes <[email protected]>--- testcases/kernel/syscalls/ioperm/ioperm01.c | 3 +++ 1 file changed, 3 insertions(+)testcases/kernel/syscalls/ioperm/ioperm01.c +0
-0
testcases/kernel/syscalls/ioperm/ioperm01.c static void verify_ioperm(void) · line 50
Quoted 9 lines
testcases/kernel/syscalls/ioperm/ioperm01.c +3
-0@@ -42,6 +42,9 @@ static void verify_ioperm(void)static void setup (void ) { +/* ioperm is restricted under kernel lockdown. */ +tst_lockdown_skip (); +/* * The value of IO_BITMAP_BITS (include/asm-i386/processor.h) changed * from kernel 2.6.8 to permit 16-bits ioperm
This looks good, however shouldn't we as well add a third ioperm test that checks that the call fails for root when lockdown is enabled?
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 2/3 ioperm01: skip test if kernel is locked down
Quoted 14 lines · Cyril Hrubis
Hi!
Quoted 13 lines
ioperm is restricted under kernel lockdown.
Signed-off-by Erico Nunes <[email protected]>--- testcases/kernel/syscalls/ioperm/ioperm01.c | 3 +++ 1 file changed, 3 insertions(+)testcases/kernel/syscalls/ioperm/ioperm01.c +0
-0@@ -42,6 +42,9 @@ static void verify_ioperm(void)
Quoted 12 lines
testcases/kernel/syscalls/ioperm/ioperm01.c static void verify_ioperm(void) · line 50
Quoted 8 lines
testcases/kernel/syscalls/ioperm/ioperm01.c +3
-0static void setup (void ) { +/* ioperm is restricted under kernel lockdown. */ +tst_lockdown_skip (); +/* * The value of IO_BITMAP_BITS (include/asm-i386/processor.h) changed * from kernel 2.6.8 to permit 16-bits ioperm This looks good, however shouldn't we as well add a third ioperm test that checks that the call fails for root when lockdown is enabled?
Good point. I think it is a good idea, but can be done in a separate patchset.
There are many other things that can be covered together with that by new tests considering the recent lockdown feature (enum lockdown_reason seems to provide a good list and starting point).
Erico
PATCH 3/3 iopl01: skip test if kernel is locked down
In reply to Erico Nunesiopl is restricted under kernel lockdown.
--- testcases/kernel/syscalls/iopl/iopl01.c | 7 +++++++ 1 file changed, 7 insertions(+)
testcases/kernel/syscalls/iopl/iopl01.c +7 -0
@@ -42,6 +42,12 @@ static void verify_iopl(void) } } +static void setup (void ) +{ +/* iopl is restricted under kernel lockdown. */ +tst_lockdown_skip (); +} +static void cleanup (void ) {/*
@@ -54,6 +60,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
PATCH 3/3 iopl01: skip test if kernel is locked down
Hi!
Quoted 25 lines
iopl is restricted under kernel lockdown.
Signed-off-by Erico Nunes <[email protected]>--- testcases/kernel/syscalls/iopl/iopl01.c | 7 +++++++ 1 file changed, 7 insertions(+)testcases/kernel/syscalls/iopl/iopl01.c +6
-0@@ -42,6 +42,12 @@ static void verify_iopl(void) } } +static void setup (void ) +{ +/* iopl is restricted under kernel lockdown. */ +tst_lockdown_skip (); +} +static void cleanup (void ) {/*
testcases/kernel/syscalls/iopl/iopl01.c static void cleanup(void) · line 65
Quoted 6 lines
testcases/kernel/syscalls/iopl/iopl01.c +1
-0@@ -54,6 +60,7 @@ static void cleanup(void)static struct tst_test test = { .test_all = verify_iopl, .needs_root =1 , + .setup = setup, .cleanup = cleanup, };
Here as well, shouldn't we add iopl03?
Or at least change the library so that we have a function that returns if kernel is locked or not so that someone can write such test.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/3 lib: add function to check for kernel lockdown
In reply to Erico NunesHi Erico,
Thanks for working on this fix. Comments as below:
Quoted 55 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 tests can be skipped rather than reporting a test failure.
Signed-off-by Erico Nunes <[email protected]>--- include/tst_lockdown.h | 8 ++++++++ include/tst_test.h | 1 + lib/tst_lockdown.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 include/tst_lockdown.h create mode 100644 lib/tst_lockdown.cinclude/tst_lockdown.h new file +8
-0@@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef TST_LOCKDOWN_H +#define TST_LOCKDOWN_H + +void tst_lockdown_skip (void ); + +#endif /* TST_LOCKDOWN_H */ include/tst_test.h +1
-0@@ -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 +4
-0@@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#define TST_NO_DEFAULT_MAIN +
lib/tst_lockdown.c line 14
Quoted 10 lines
lib/tst_lockdown.c new file +10
-0+#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" + +void tst_lockdown_skip (void )
Maybe renaming the function to tst_lockdown_enabled() is better? Then we can return 1 if confirm kernel under lockdown mode otherwise 0.
+{
lib/tst_lockdown.c line 19
Quoted 5 lines
lib/tst_lockdown.c new file +4
-0+char line[BUFSIZ ]; +FILE *file; + +if (access ("/sys/kernel/security/lockdown" ,F_OK ) !=0 )
After thinking over, I guess it's not enough to only check /sys/../lockdown file. Seems we need to consider the situation that system without supporting this file?
i.e.
Test on RHEL8 (no /sys/../lockdown file) with kernel parameter "lockdown"
and got the restriction error too.
# cat /proc/cmdline BOOT_IMAGE=(hd0,msdos1)/vmlinuz-4.18.0-226.el8.x86_64 root=/dev/mapper/rhel_bootp--73--3--209-root ro console=ttyS0,115200 ... lockdown
# ll /sys/kernel/security/lockdown ls: cannot access '/sys/kernel/security/lockdown': No such file or directory
# ./iopl01 ... iopl01.c:37: FAIL: iopl() failed for level 1, errno=1 : EPERM: EPERM (1) iopl01.c:37: FAIL: iopl() failed for level 2, errno=1 : EPERM: EPERM (1)
Quoted 17 lines
lib/tst_lockdown.c new file +9
-1+return ; + + file =SAFE_FOPEN ("/sys/kernel/security/lockdown" ,"r" ); +fgets (line,sizeof (line), file); +SAFE_FCLOSE (file); + +if (strstr (line,"[none]" ) ==NULL ) +tst_brk (TCONF ,"Kernel is locked down, skip this test." ); +} --2.26.2
--
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Regards, Li Wang -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.linux.it/pipermail/ltp/attachments/20200721/c9de85e0/attachment.htm>
PATCH 1/3 lib: add function to check for kernel lockdown
Thanks for the review. I'll address other comments soon, just an initial note below:
Quoted 18 lines · Li Wang
Maybe renaming the function to
tst_lockdown_enabled()is better? Then we can return?1 if confirm kernel under lockdown mode otherwise 0.+{ +? ? ? ?char line[BUFSIZ]; +? ? ? ?FILE *file; + +? ? ? ?if (access("/sys/kernel/security/lockdown", F_OK) != 0)After thinking over, I guess it's not enough to only check
/sys/../lockdownfile. Seems we need to consider the situation that system without supporting this file??i.e.?
? Test on RHEL8 (no/sys/../lockdownfile) with kernel parameter "lockdown" and got the restriction error too.
Quoted 7 lines
# cat /proc/cmdline? BOOT_IMAGE=(hd0,msdos1)/vmlinuz-4.18.0-226.el8.x86_64 root=/dev/mapper/rhel_bootp--73--3--209-root ro console=ttyS0,115200 ...?lockdown ? ?? # ll /sys/kernel/security/lockdown ls: cannot access '/sys/kernel/security/lockdown': No such file or directory
To my understanding, the parameter to enable lockdown through kenrel parameters is "lockdown={integrity|confidentiality}", not just "lockdown", at least on upstream kernels:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=aefcf2f4b58155d27340ba5f9ddbe9513da8286d
If /sys/kernel/security/lockdown doesn't exist, I'm not sure there is much we can do easily, or that is worth doing now. I think it is ok to fall back and fail like it has been happening since the feature was merged upstream.
I can't see a tweak that would enable the feature but not the sysfs file in the kernel source. Maybe that kernel only had partial support?
Erico
PATCH 1/3 lib: add function to check for kernel lockdown
Erico,
Quoted 5 lines · Erico Nunes
...
Quoted 2 lines
Maybe renaming the function to
tst_lockdown_enabled()is better? Then we can return 1 if confirm kernel under lockdown mode otherwise 0.
How do you think about this suggestion? ^^
Another reason to name it as tst_lockdown_enabled() is, we can give more flexible
to test case, because not all tests need a simple skip in lockdown mode(in future).
i.e.
if (tst_lockdown_enabled()) {
// skip or not,
// do what they wanted in this mode
}
Quoted 13 lines
After thinking over, I guess it's not enough to only check
Quoted 12 lines
/sys/../lockdownfile. Seems we need to consider the situation that system without supporting this file?i.e.
Test on RHEL8 (no/sys/../lockdownfile) with kernel parameter
"lockdown" and got the restriction error too.# cat /proc/cmdline BOOT_IMAGE=(hd0,msdos1)/vmlinuz-4.18.0-226.el8.x86_64 root=/dev/mapper/rhel_bootp--73--3--209-root ro console=ttyS0,115200 ... lockdown
Quoted 8 lines
Quoted 2 lines
# ll /sys/kernel/security/lockdown ls: cannot access '/sys/kernel/security/lockdown': No such file ordirectory
To my understanding, the parameter to enable lockdown through kenrel parameters is "lockdown={integrity|confidentiality}", not just "lockdown", at least on upstream kernels:
Good to know this.
Quoted 11 lines
If
/sys/kernel/security/lockdowndoesn't exist, I'm not sure there is much we can do easily, or that is worth doing now. I think it is ok to fall back and fail like it has been happening since the feature was merged upstream.
Yes, it looks a bit tricky.
Quoted 3 lines
I can't see a tweak that would enable the feature but not the sysfs file in the kernel source. Maybe that kernel only had partial support?
Seems you're right, there are many differences between mainline-kernel and some distros in lockdown code. The reason that some distribution (i.e RHEL, Ubuntu) partly customizes the LSM feature, it does not support lockdown features completely so far.
But one point we're sure is that the /sys/kernel/../lockdown file was introduced from kernel-v5.4.
So maybe we could simply do detect the /sys/kernel/../loackdown file as your patch,
but adding an extra warning print when test failed on older than kernel-v5.4.
Or, if others can provide a better way I'd happy to hear.
Signature · 5 lines
Regards, Li Wang -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.linux.it/pipermail/ltp/attachments/20200721/88b05af2/attachment.htm>
PATCH 1/3 lib: add function to check for kernel lockdown
Quoted 17 lines · Li Wang
On Tue, Jul 21, 2020 at 4:57 PM Erico Nunes <[email protected] <mailto:[email protected]>> wrote:
...
> Maybe renaming the function to
tst_lockdown_enabled()is better? Then we
> can return?1 if confirm kernel under lockdown mode otherwise 0.How do you think about this suggestion? ^^
Another reason?to name it as
tst_lockdown_enabled()is, we can give more flexible
to test case, because?not all tests need a simple skip in lockdown mode(in future).
Quoted 5 lines
i.e.
if (tst_lockdown_enabled()) {
? ?// skip or not,
? ?// do what they wanted in this mode
}
I like this suggestion, I'll send v2 with this.
Quoted 18 lines
If
/sys/kernel/security/lockdowndoesn't exist, I'm not sure there is much we can do easily, or that is worth doing now. I think it is ok to fall back and fail like it has been happening since the feature was merged upstream.Yes, it looks a bit tricky.
?I can't see a tweak that would enable the feature but not the sysfs file in the kernel source. Maybe that kernel only had partial support?
Seems you're right, there are many differences between mainline-kernel and some distros in lockdown code. The reason that some distribution (i.e RHEL, Ubuntu) partly customizes the LSM feature,?it does not support lockdown features completely so far.
Quoted 7 lines
But one point we're sure is that the
/sys/kernel/../lockdownfile was introduced from kernel-v5.4.So maybe we could simply do detect the
/sys/kernel/../loackdownfile as your patch,
but adding an extra warning print when test failed on older than kernel-v5.4.
I like the idea of the warning. The only thing to consider is that the warning would also show up on all old kernels that don't even support lockdown and then don't have the file. So would you suggest this message to be something like a tst_res(TWARN, ...) or TINFO or some other less noisy way?
I also thought about limiting to some kernel version but that wouldn't work with distribution kernels like RHEL which have an earlier version number but also have the feature...
Erico
PATCH 1/3 lib: add function to check for kernel lockdown
Hi!
Quoted 10 lines
Quoted 4 lines
So maybe we could simply do detect the
/sys/kernel/../loackdownfile as your patch,
but adding an extra warning print when test failed on older than kernel-v5.4.I like the idea of the warning. The only thing to consider is that the warning would also show up on all old kernels that don't even support lockdown and then don't have the file. So would you suggest this message to be something like a
tst_res(TWARN, ...)or TINFO or some other less noisy way?
TWARN will cause the test to exit with non-zero status, which will probably show up as a failure in some environments, so I would go for TINFO.
Quoted 3 lines
I also thought about limiting to some kernel version but that wouldn't work with distribution kernels like RHEL which have an earlier version number but also have the feature...
We also have an interface to match different kernel versions per distribution, have a look at tst_kern_exv structure in inotify04.c testcase.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/3 lib: add function to check for kernel lockdown
In reply to Erico NunesQuoted 7 lines · Erico Nunes
...
Quoted 4 lines
So maybe we could simply do detect the
/sys/kernel/../loackdownfile as your patch,
but adding an extra warning print when test failed on older than kernel-v5.4.I like the idea of the warning. The only thing to consider is that the
warning would also show up on all old kernels that don't even support
lockdown and then don't have the file. So would you suggest this message
Quoted 3 lines
to be something like a
tst_res(TWARN, ...)or TINFO or some other less noisy way?
Thanks, but I did not suggest to show the warning in any system without the lockdown file. I mean if test getting FAIL on the no 'lockdown' file system, then could consider throwing a warning as a hint.
And this could be achieved in the test case but not the library.
For indicating the 'lockdown' file exist or no-exist, the 'TINFO' is enough.
Signature · 5 lines
Regards, Li Wang -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.linux.it/pipermail/ltp/attachments/20200723/2a96b60d/attachment-0001.htm>
PATCH 1/3 lib: add function to check for kernel lockdown
In reply to Erico NunesHi!
Quoted 63 lines
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 tests can be skipped rather than reporting a test failure.
Signed-off-by Erico Nunes <[email protected]>--- include/tst_lockdown.h | 8 ++++++++ include/tst_test.h | 1 + lib/tst_lockdown.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 include/tst_lockdown.h create mode 100644 lib/tst_lockdown.cinclude/tst_lockdown.h new file +8
-0@@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifndef TST_LOCKDOWN_H +#define TST_LOCKDOWN_H + +void tst_lockdown_skip (void ); + +#endif /* TST_LOCKDOWN_H */ include/tst_test.h +1
-0@@ -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 +13
-0@@ -0,0 +1,28 @@ +// 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" +
lib/tst_lockdown.c line 23
Quoted 10 lines
lib/tst_lockdown.c new file +10
-0+void tst_lockdown_skip (void ) +{ +char line[BUFSIZ ]; +FILE *file; + +if (access ("/sys/kernel/security/lockdown" ,F_OK ) !=0 ) +return ; + + file =SAFE_FOPEN ("/sys/kernel/security/lockdown" ,"r" ); +fgets (line,sizeof (line), file);
The compiler complains that we haven't checked the return value here I guess that we can silence it with:
if (!fgets(line, sizeof(line), file) return;
Quoted 11 lines
lib/tst_lockdown.c new file +5
-0+SAFE_FCLOSE (file); + +if (strstr (line,"[none]" ) ==NULL ) +tst_brk (TCONF ,"Kernel is locked down, skip this test." ); +}Signature · 5 lines
2.26.2 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/3 lib: add function to check for kernel lockdown
Quoted 14 lines · Cyril Hrubis
lib/tst_lockdown.c line 23
Quoted 10 lines
lib/tst_lockdown.c new file +10
-0+void tst_lockdown_skip (void ) +{ +char line[BUFSIZ ]; +FILE *file; + +if (access ("/sys/kernel/security/lockdown" ,F_OK ) !=0 ) +return ; + + file =SAFE_FOPEN ("/sys/kernel/security/lockdown" ,"r" ); +fgets (line,sizeof (line), file);The compiler complains that we haven't checked the return value here I guess that we can silence it with:
Quoted 3 lines
if (!fgets(line, sizeof(line), file) return;
Thanks, will fix in v2.