Skip to contentSkip to conversation

provide a unified way to parse /proc/cmdline

PATCH In review Li Wang 2 participants 6 messages Mar 17, 2024 – Mar 18, 2024 Library code

lore
Share & tools
lore

Li Wang

author
#0 lore raw

v3 cover provide a unified way to parse /proc/cmdline

v2 --> v3
* add params.found feild
* correct sentece errors
* replace the fgetc() by fgets()

Li Wang (3):
kconfig: add funtion to parse /proc/cmdline
init_module: To handle kernel module signature enforcement
stack_clash: make use of tst_kcmdline_parse

 include/tst_kconfig.h                         | 29 ++++++++
 lib/newlib_tests/test_kconfig03.c             | 40 +++++++++++
 lib/tst_kconfig.c                             | 66 +++++++++++++++++++
 testcases/cve/stack_clash.c                   | 14 ++--
 .../syscalls/delete_module/delete_module01.c  |  8 +++
 .../syscalls/delete_module/delete_module03.c  |  8 +++
 .../syscalls/finit_module/finit_module01.c    | 16 ++++-
 .../syscalls/finit_module/finit_module02.c    | 38 +++++++----
 .../syscalls/init_module/init_module01.c      | 14 ++++
 .../syscalls/init_module/init_module02.c      | 20 +++++-
 10 files changed, 229 insertions(+), 24 deletions(-)
 create mode 100644 lib/newlib_tests/test_kconfig03.c
Signature · 5 lines
2.40.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#1 lore raw

PATCH v3 1/3 kconfig: add funtion to parse /proc/cmdline

A new structure tst_kcmdline_var is defined to hold a command-line argument's key and a fixed-size value. Furthermore, function tst_kcmdline_parse is added to the corresponding .c file, which reads from /proc/cmdline, parses the command-line arguments, and populates the tst_kcmdline_var array with the obtained key-value pairs, ensuring safe file operations and buffer size checks.

---
 include/tst_kconfig.h             | 29 ++++++++++++++
 lib/newlib_tests/test_kconfig03.c | 40 +++++++++++++++++++
 lib/tst_kconfig.c                 | 66 +++++++++++++++++++++++++++++++
 3 files changed, 135 insertions(+)
 create mode 100644 lib/newlib_tests/test_kconfig03.c
include/tst_kconfig.h +29 -0
index 8b24a8380..bbb899784 100644
@@ -6,6 +6,8 @@
 #ifndef TST_KCONFIG_H__
 #define TST_KCONFIG_H__
 
+#include <stdbool.h>
+
 /**
  * Initialization helper macro for struct tst_kconfig_var. Requires <string.h>
  */
@@ -64,4 +66,31 @@ void tst_kconfig_read(struct tst_kconfig_var vars[], size_t vars_len);
  */
 int tst_kconfig_check(const char *const kconfigs[]);
 
+/**
+ * Macro to initialize a tst_kcmdline_var structure with a specified parameter
+ * name and an empty value. This is useful for setting up an array of parameter
+ * structures before parsing the actual command-line arguments.
+ */
+#define TST_KCMDLINE_INIT(paraname) { \
+	.key = paraname, \
+	.value = "", \
+	.found = false \
+}
+
+/**
+ * Structure for storing command-line parameter key and its corresponding value
+ */
+struct tst_kcmdline_var {
+	const char *key;
+	char value[128];
+	bool found;
+};
+
+/**
+ * Parses command-line parameters from /proc/cmdline and stores them in params array.
+ * params: The array of tst_kcmdline_var structures to be filled with parsed key-value pairs.
+ * params_len: The length of the params array, indicating how many parameters to parse.
+ */
+void tst_kcmdline_parse(struct tst_kcmdline_var params[], size_t params_len);
+
 #endif	/* TST_KCONFIG_H__ */
lib/newlib_tests/test_kconfig03.c new file +40 -0
new file mode 100644
index 000000000..937c2b29c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 Li Wang <[email protected]>
+ */
+
+#include "tst_test.h"
+#include "tst_kconfig.h"
+
+static struct tst_kcmdline_var params[] = {
+	TST_KCMDLINE_INIT("BOOT_IMAGE"),
+	TST_KCMDLINE_INIT("root"),
+	TST_KCMDLINE_INIT("foo")
+};
+
+static void do_test(void)
+{
+	int i, N;
+
+	N = (int) ARRAY_SIZE(params);
+
+	tst_kcmdline_parse(params, N);
+
+	for (i = 0; i < N; i++) {
+		if (params[i].found) {
+			if (params[i].value)
+				tst_res(TPASS, "params[%d] = {%s, %s}", i, params[i].key, params[i].value);
+			else
+				tst_res(TPASS, "params[%d] = {%s, no value}", i, params[i].key);
+		} else {
+			if (!strcmp(params[i].key, "foo"))
+				tst_res(TPASS, "%s is not found in /proc/cmdline", params[i].key);
+			else
+				tst_res(TFAIL, "%s is not found in /proc/cmdline", params[i].key);
+		}
+	}
+}
+
+static struct tst_test test = {
+	.test_all = do_test,
+};
lib/tst_kconfig.c +66 -0

75 lines not on this page · raw message

Signature · 5 lines
2.40.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v3 1/3
#2 lore raw

Re: [LTP] [PATCH v3 1/3] kconfig: add funtion to parse /proc/cmdline

Hi Li,

Quoted 6 lines

A new structure tst_kcmdline_var is defined to hold a command-line argument's key and a fixed-size value. Furthermore, function tst_kcmdline_parse is added to the corresponding .c file, which reads from /proc/cmdline, parses the command-line arguments, and populates the tst_kcmdline_var array with the obtained key-value pairs, ensuring safe file operations and buffer size checks.

Nice. Just compiler warning and formatting nits below (just fix them before merge). Thanks a lot for adding a test.

Quoted 13 lines
---
 include/tst_kconfig.h             | 29 ++++++++++++++
 lib/newlib_tests/test_kconfig03.c | 40 +++++++++++++++++++
 lib/tst_kconfig.c                 | 66 +++++++++++++++++++++++++++++++
 3 files changed, 135 insertions(+)
 create mode 100644 lib/newlib_tests/test_kconfig03.c
include/tst_kconfig.h +0 -0
index 8b24a8380..bbb899784 100644

include/tst_kconfig.h line 12

Quoted 7 lines
include/tst_kconfig.h +2 -0
@@ -6,6 +6,8 @@
 #ifndef TST_KCONFIG_H__
 #define TST_KCONFIG_H__
 
+#include <stdbool.h>
+
 /**
  * Initialization helper macro for struct tst_kconfig_var. Requires <string.h>

very nic: missing dot at the end.

Quoted 8 lines
include/tst_kconfig.h +3 -0
  */
@@ -64,4 +66,31 @@ void tst_kconfig_read(struct tst_kconfig_var vars[], size_t vars_len);
  */
 int tst_kconfig_check(const char *const kconfigs[]);
 
+/**
+ * Macro to initialize a tst_kcmdline_var structure with a specified parameter
+ * name and an empty value. This is useful for setting up an array of parameter

include/tst_kconfig.h void tst_kconfig_read(struct tst_kconfig_var vars[], size_t vars_len); · line 81

Quoted 10 lines
include/tst_kconfig.h +10 -0
+ * structures before parsing the actual command-line arguments.
+ */
+#define TST_KCMDLINE_INIT(paraname) { \
+	.key = paraname, \
+	.value = "", \
+	.found = false \
+}
+
+/**
+ * Structure for storing command-line parameter key and its corresponding value

and here.

Quoted 36 lines
include/tst_kconfig.h +14 -0
+ */
+struct tst_kcmdline_var {
+	const char *key;
+	char value[128];
+	bool found;
+};
+
+/**
+ * Parses command-line parameters from /proc/cmdline and stores them in params array.
+ * params: The array of tst_kcmdline_var structures to be filled with parsed key-value pairs.
+ * params_len: The length of the params array, indicating how many parameters to parse.
+ */
+void tst_kcmdline_parse(struct tst_kcmdline_var params[], size_t params_len);
+
 #endif	/* TST_KCONFIG_H__ */
lib/newlib_tests/test_kconfig03.c new file +15 -0
new file mode 100644
index 000000000..937c2b29c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 Li Wang <[email protected]>
+ */
+
+#include "tst_test.h"
+#include "tst_kconfig.h"
+
+static struct tst_kcmdline_var params[] = {
+	TST_KCMDLINE_INIT("BOOT_IMAGE"),
+	TST_KCMDLINE_INIT("root"),
+	TST_KCMDLINE_INIT("foo")
+};
+
+static void do_test(void)

lib/newlib_tests/test_kconfig03.c line 25

Quoted 10 lines
lib/newlib_tests/test_kconfig03.c new file +10 -0
+{
+	int i, N;
+
+	N = (int) ARRAY_SIZE(params);
+
+	tst_kcmdline_parse(params, N);
+
+	for (i = 0; i < N; i++) {
+		if (params[i].found) {
+			if (params[i].value)

test_kconfig03.c:25:29: warning: the comparison will always evaluate as ‘true’ for the address of ‘value’ will never be NULL [-Waddress]
25 | if (params[i].value)
| ^~~~~~
In file included from test_kconfig03.c:7:
../../include/tst_kconfig.h:85:14: note: ‘value’ declared here
85 | char value[128];

It must be:
if (params[i].value[0] != '\0')

Or just without else at all:

	for (i = 0; i < N; i++) {
		if (params[i].found) {
			tst_res(TPASS, "params[%d] = {%s, %s}", i, params[i].key, params[i].value);
		} else {
			if (!strcmp(params[i].key, "foo"))
				tst_res(TPASS, "%s is not found in /proc/cmdline", params[i].key);
			else
				tst_res(TFAIL, "%s is not found in /proc/cmdline", params[i].key);
		}

Kind regards,
Petr

Quoted 11 lines
lib/newlib_tests/test_kconfig03.c new file +11 -0
+				tst_res(TPASS, "params[%d] = {%s, %s}", i, params[i].key, params[i].value);
+			else
+				tst_res(TPASS, "params[%d] = {%s, no value}", i, params[i].key);
+		} else {
+			if (!strcmp(params[i].key, "foo"))
+				tst_res(TPASS, "%s is not found in /proc/cmdline", params[i].key);
+			else
+				tst_res(TFAIL, "%s is not found in /proc/cmdline", params[i].key);
+		}
+	}
+}

...

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#3 lore raw

Re: [LTP] [PATCH v3 1/3] kconfig: add funtion to parse /proc/cmdline

Quoted 126 lines · Petr Vorel

Hi Li,

Quoted 6 lines

A new structure tst_kcmdline_var is defined to hold a command-line argument's key and a fixed-size value. Furthermore, function tst_kcmdline_parse is added to the corresponding .c file, which reads from /proc/cmdline, parses the command-line arguments, and populates the tst_kcmdline_var array with the obtained key-value pairs, ensuring safe file operations and buffer size checks.

Nice. Just compiler warning and formatting nits below (just fix them before merge).
Thanks a lot for adding a test.

Quoted 13 lines
---
 include/tst_kconfig.h             | 29 ++++++++++++++
 lib/newlib_tests/test_kconfig03.c | 40 +++++++++++++++++++
 lib/tst_kconfig.c                 | 66 +++++++++++++++++++++++++++++++
 3 files changed, 135 insertions(+)
 create mode 100644 lib/newlib_tests/test_kconfig03.c
include/tst_kconfig.h +0 -0
index 8b24a8380..bbb899784 100644

include/tst_kconfig.h line 12

Quoted 7 lines
include/tst_kconfig.h +2 -0
@@ -6,6 +6,8 @@
 #ifndef TST_KCONFIG_H__
 #define TST_KCONFIG_H__
 
+#include <stdbool.h>
+
 /**
  * Initialization helper macro for struct tst_kconfig_var. Requires

<string.h>
very nic: missing dot at the end.

include/tst_kconfig.h void tst_kconfig_read(struct tst_kconfig_var vars[],

Quoted 2 lines
include/tst_kconfig.h +0 -0
  */
@@ -64,4 +66,31 @@ void tst_kconfig_read(struct tst_kconfig_var vars[],

size_t vars_len);

include/tst_kconfig.h void tst_kconfig_read(struct tst_kconfig_var vars[], size_t vars_len);

Quoted 5 lines
include/tst_kconfig.h +2 -0
  */
 int tst_kconfig_check(const char *const kconfigs[]);
 
+/**
+ * Macro to initialize a tst_kcmdline_var structure with a specified

parameter

include/tst_kconfig.h void tst_kconfig_read(struct tst_kconfig_var vars[], · line 71

Quoted 1 line
include/tst_kconfig.h +1 -0
+ * name and an empty value. This is useful for setting up an array of

parameter

include/tst_kconfig.h void tst_kconfig_read(struct tst_kconfig_var vars[], size_t vars_len);

Quoted 10 lines
include/tst_kconfig.h +10 -0
+ * structures before parsing the actual command-line arguments.
+ */
+#define TST_KCMDLINE_INIT(paraname) { \
+     .key = paraname, \
+     .value = "", \
+     .found = false \
+}
+
+/**
+ * Structure for storing command-line parameter key and its

corresponding value
and here.

include/tst_kconfig.h void tst_kconfig_read(struct tst_kconfig_var vars[], size_t vars_len);

Quoted 9 lines
include/tst_kconfig.h +9 -0
+ */
+struct tst_kcmdline_var {
+     const char *key;
+     char value[128];
+     bool found;
+};
+
+/**
+ * Parses command-line parameters from /proc/cmdline and stores them in

params array.

include/tst_kconfig.h void tst_kconfig_read(struct tst_kconfig_var vars[], · line 91

Quoted 1 line
include/tst_kconfig.h +1 -0
+ * params: The array of tst_kcmdline_var structures to be filled with

parsed key-value pairs.

include/tst_kconfig.h void tst_kconfig_read(struct tst_kconfig_var vars[], · line 92

Quoted 1 line
include/tst_kconfig.h +1 -0
+ * params_len: The length of the params array, indicating how many

parameters to parse.

include/tst_kconfig.h void tst_kconfig_read(struct tst_kconfig_var vars[], · line 94

Quoted 2 lines
include/tst_kconfig.h +2 -0
+ */
+void tst_kcmdline_parse(struct tst_kcmdline_var params[], size_t

params_len);

Review

Quoted 3 lines
include/tst_kconfig.h +1 -0
+
 #endif       /* TST_KCONFIG_H__ */
(quoted diff) +0 -0

b/lib/newlib_tests/test_kconfig03.c

Quoted 20 lines

new file mode 100644
index 000000000..937c2b29c

lib/newlib_tests/test_kconfig03.c new file +15 -0
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 Li Wang <[email protected]>
+ */
+
+#include "tst_test.h"
+#include "tst_kconfig.h"
+
+static struct tst_kcmdline_var params[] = {
+     TST_KCMDLINE_INIT("BOOT_IMAGE"),
+     TST_KCMDLINE_INIT("root"),
+     TST_KCMDLINE_INIT("foo")
+};
+
+static void do_test(void)

lib/newlib_tests/test_kconfig03.c line 25

Quoted 10 lines
lib/newlib_tests/test_kconfig03.c new file +10 -0
+{
+     int i, N;
+
+     N = (int) ARRAY_SIZE(params);
+
+     tst_kcmdline_parse(params, N);
+
+     for (i = 0; i < N; i++) {
+             if (params[i].found) {
+                     if (params[i].value)

test_kconfig03.c:25:29: warning: the comparison will always evaluate as ‘true’ for the address of ‘value’ will never be NULL [-Waddress]
25 | if (params[i].value)
| ^~~~~~
In file included from test_kconfig03.c:7:
../../include/tst_kconfig.h:85:14: note: ‘value’ declared here
85 | char value[128];

It must be:
if (params[i].value[0] != '\0')

Or just without else at all:

Quoted 13 lines
        for (i = 0; i < N; i++) {
                if (params[i].found) {
                        tst_res(TPASS, "params[%d] = {%s, %s}", i,
params[i].key, params[i].value);
                } else {
                        if (!strcmp(params[i].key, "foo"))
                                tst_res(TPASS, "%s is not found in
/proc/cmdline", params[i].key);
                        else
                                tst_res(TFAIL, "%s is not found in
/proc/cmdline", params[i].key);
                }

Looks good, Patch applied with your suggestion. Thanks!

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#4 lore raw

PATCH v3 2/3 init_module: To handle kernel module signature enforcement

In reply to Li Wang

The patch modifies init_module syscall test cases to account for kernel module signature enforcement. It adds parsing for the 'module.sig_enforce' parameter and adjusts test expectations based on whether signature enforcement is enabled, using new conditional logic.

If enforcement is active, tests expect an EKEYREJECTED error; otherwise, they proceed as normal.

---
 .../syscalls/delete_module/delete_module01.c  |  8 ++++
 .../syscalls/delete_module/delete_module03.c  |  8 ++++
 .../syscalls/finit_module/finit_module01.c    | 16 +++++++-
 .../syscalls/finit_module/finit_module02.c    | 38 ++++++++++++-------
 .../syscalls/init_module/init_module01.c      | 14 +++++++
 .../syscalls/init_module/init_module02.c      | 20 +++++++++-
 6 files changed, 89 insertions(+), 15 deletions(-)
testcases/kernel/syscalls/delete_module/delete_module01.c +8 -0
index 90d8b5289..11c4eae58 100644
@@ -14,8 +14,10 @@
  * Install dummy_del_mod.ko and delete it with delete_module(2).
  */
 
+#include <stdlib.h>
 #include "tst_test.h"
 #include "tst_module.h"
+#include "tst_kconfig.h"
 #include "lapi/syscalls.h"
 
 #define MODULE_NAME	"dummy_del_mod"
@@ -25,6 +27,12 @@ static int module_loaded;
 
 static void do_delete_module(void)
 {
+	struct tst_kcmdline_var params = TST_KCMDLINE_INIT("module.sig_enforce");
+
+	tst_kcmdline_parse(&params, 1);
+	if (atoi(params.value) == 1)
+		tst_brk(TCONF, "module signature is enforced, skip test");
+
 	if (!module_loaded) {
 		tst_module_load(MODULE_NAME_KO, NULL);
 		module_loaded = 1;
testcases/kernel/syscalls/delete_module/delete_module03.c +8 -0
index 7e92fc2af..fdf0c293d 100644
@@ -12,9 +12,11 @@
  * if tried to remove a module while other modules depend on this module.
  */
 
+#include <stdlib.h>
 #include <errno.h>
 #include "tst_test.h"
 #include "tst_module.h"
+#include "tst_kconfig.h"
 #include "lapi/syscalls.h"
 
 #define DUMMY_MOD		"dummy_del_mod"
@@ -50,6 +52,12 @@ static void do_delete_module(void)
 
 static void setup(void)
 {
+	struct tst_kcmdline_var params = TST_KCMDLINE_INIT("module.sig_enforce");
+
+	tst_kcmdline_parse(&params, 1);
+	if (atoi(params.value) == 1)
+		tst_brk(TCONF, "module signature is enforced, skip test");
+
 	/* Load first kernel module */
 	tst_module_load(DUMMY_MOD_KO, NULL);
 	dummy_mod_loaded = 1;
testcases/kernel/syscalls/finit_module/finit_module01.c +15 -1
index 1929c30fa..1546d4fb7 100644
@@ -13,18 +13,26 @@
  * Inserts a simple module after opening and mmaping the module file.
  */
 
+#include <stdlib.h>
 #include <errno.h>
 #include "lapi/init_module.h"
 #include "tst_module.h"
+#include "tst_kconfig.h"
 
 #define MODULE_NAME	"finit_module.ko"
 
-static int fd;
+static int fd, sig_enforce;
 
 static char *mod_path;
 
 static void setup(void)
 {
+	struct tst_kcmdline_var params = TST_KCMDLINE_INIT("module.sig_enforce");
+
+	tst_kcmdline_parse(&params, 1);
+	if (params.found)
+		sig_enforce = atoi(params.value);
+
 	tst_module_exists(MODULE_NAME, &mod_path);
 
 	fd = SAFE_OPEN(mod_path, O_RDONLY|O_CLOEXEC);
@@ -32,6 +40,12 @@ static void setup(void)
 
 static void run(void)
 {
+	if (sig_enforce == 1) {
+		tst_res(TINFO, "module signature is enforced");
+		TST_EXP_FAIL(finit_module(fd, "status=valid", 0), EKEYREJECTED);
+		return;
+	}
+
 	TST_EXP_PASS(finit_module(fd, "status=valid", 0));
 	if (!TST_PASS)
 		return;
testcases/kernel/syscalls/finit_module/finit_module02.c +25 -13
index 223d9b388..2fe70af4d 100644
@@ -14,9 +14,11 @@
  */
 
 #include <linux/capability.h>
+#include <stdlib.h>
 #include <errno.h>
 #include "lapi/init_module.h"
 #include "tst_module.h"
+#include "tst_kconfig.h"
 #include "tst_capability.h"
 
 #define MODULE_NAME	"finit_module.ko"
@@ -25,7 +27,7 @@
 static char *mod_path;
 
 static int fd, fd_zero, fd_invalid = -1, fd_dir;
-static int kernel_lockdown, secure_boot;
+static int kernel_lockdown, secure_boot, sig_enforce;
 
 static struct tst_cap cap_req = TST_CAP(TST_CAP_REQ, CAP_SYS_MODULE);
 static struct tst_cap cap_drop = TST_CAP(TST_CAP_DROP, CAP_SYS_MODULE);
@@ -59,27 +61,27 @@ static void dir_setup(struct tcase *tc)
 }
 
 static struct tcase tcases[] = {
-	{"invalid-fd", &fd_invalid, "", O_RDONLY | O_CLOEXEC, 0, 0, 0, 0,
-		bad_fd_setup},
+	{"invalid-fd", &fd_invalid, "", O_RDONLY | O_CLOEXEC, 0, 0, 0, 0, bad_fd_setup},
 	{"zero-fd", &fd_zero, "", O_RDONLY | O_CLOEXEC, 0, 0, EINVAL, 0, NULL},
 	{"null-param", &fd, NULL, O_RDONLY | O_CLOEXEC, 0, 0, EFAULT, 1, NULL},
-	{"invalid-param", &fd, "status=invalid", O_RDONLY | O_CLOEXEC, 0, 0,
-		EINVAL, 1, NULL},
-	{"invalid-flags", &fd, "", O_RDONLY | O_CLOEXEC, -1, 0, EINVAL, 0,
-		NULL},
+	{"invalid-param", &fd, "status=invalid", O_RDONLY | O_CLOEXEC, 0, 0, EINVAL, 1, NULL},
+	{"invalid-flags", &fd, "", O_RDONLY | O_CLOEXEC, -1, 0, EINVAL, 0, NULL},
 	{"no-perm", &fd, "", O_RDONLY | O_CLOEXEC, 0, 1, EPERM, 0, NULL},
-	{"module-exists", &fd, "", O_RDONLY | O_CLOEXEC, 0, 0, EEXIST, 1,
-		NULL},
-	{"file-not-readable", &fd, "", O_WRONLY | O_CLOEXEC, 0, 0, EBADF, 0,
-		NULL},
-	{"file-readwrite", &fd, "", O_RDWR | O_CLOEXEC, 0, 0, ETXTBSY, 0,
-		NULL},
+	{"module-exists", &fd, "", O_RDONLY | O_CLOEXEC, 0, 0, EEXIST, 1, NULL},
+	{"module-unsigned", &fd, "", O_RDONLY | O_CLOEXEC, 0, 0, EKEYREJECTED, 1, NULL},
+	{"file-not-readable", &fd, "", O_WRONLY | O_CLOEXEC, 0, 0, EBADF, 0, NULL},
+	{"file-readwrite", &fd, "", O_RDWR | O_CLOEXEC, 0, 0, ETXTBSY, 0, NULL},
 	{"directory", &fd_dir, "", O_RDONLY | O_CLOEXEC, 0, 0, 0, 0, dir_setup},
 };
 
 static void setup(void)
 {
 	unsigned long int i;
+	struct tst_kcmdline_var params = TST_KCMDLINE_INIT("module.sig_enforce");
+
+	tst_kcmdline_parse(&params, 1);
+	if (params.found)
+		sig_enforce = atoi(params.value);
 
 	tst_module_exists(MODULE_NAME, &mod_path);
 
@@ -109,6 +111,16 @@ static void run(unsigned int n)
 		return;
 	}
 
+	if ((sig_enforce == 1) && (tc->exp_errno != EKEYREJECTED)) {
+		tst_res(TCONF, "module signature is enforced, skipping %s", tc->name);
+		return;
+	}
+
+	if ((sig_enforce != 1) && (tc->exp_errno == EKEYREJECTED)) {
+		tst_res(TCONF, "module signature is not enforced, skipping %s", tc->name);
+		return;
+	}
+
 	fd = SAFE_OPEN(mod_path, tc->open_flags);
 
 	if (tc->cap)
testcases/kernel/syscalls/init_module/init_module01.c +14 -0
index 26ff0b93b..27a1fc627 100644
@@ -13,18 +13,26 @@
  * Inserts a simple module after opening and mmaping the module file.
  */
 
+#include <stdlib.h>
 #include <errno.h>
 #include "lapi/init_module.h"
 #include "tst_module.h"
+#include "tst_kconfig.h"
 
 #define MODULE_NAME	"init_module.ko"
 
 static struct stat sb;
 static void *buf;
+static int sig_enforce;
 
 static void setup(void)
 {
 	int fd;
+	struct tst_kcmdline_var params = TST_KCMDLINE_INIT("module.sig_enforce");
+
+	tst_kcmdline_parse(&params, 1);
+	if (params.found)
+		sig_enforce = atoi(params.value);
 
 	tst_module_exists(MODULE_NAME, NULL);
 
@@ -36,6 +44,12 @@ static void setup(void)
 
 static void run(void)
 {
+	if (sig_enforce == 1) {
+		tst_res(TINFO, "module signature is enforced");
+		TST_EXP_FAIL(init_module(buf, sb.st_size, "status=valid"), EKEYREJECTED);
+		return;
+	}
+
 	TST_EXP_PASS(init_module(buf, sb.st_size, "status=valid"));
 	if (!TST_PASS)
 		return;
testcases/kernel/syscalls/init_module/init_module02.c +19 -1
index e6730e21c..68d9ff16b 100644
@@ -14,15 +14,17 @@
  */
 
 #include <linux/capability.h>
+#include <stdlib.h>
 #include <errno.h>
 #include "lapi/init_module.h"
+#include "tst_kconfig.h"
 #include "tst_module.h"
 #include "tst_capability.h"
 
 #define MODULE_NAME	"init_module.ko"
 
 static unsigned long size, zero_size;
-static int kernel_lockdown, secure_boot;
+static int kernel_lockdown, secure_boot, sig_enforce;
 static void *buf, *faulty_buf, *null_buf;
 
 static struct tst_cap cap_req = TST_CAP(TST_CAP_REQ, CAP_SYS_MODULE);
@@ -44,12 +46,18 @@ static struct tcase {
 	{"invalid_param", &buf, &size, "status=invalid", 0, 1, EINVAL},
 	{"no-perm", &buf, &size, "", 1, 0, EPERM},
 	{"module-exists", &buf, &size, "", 0, 1, EEXIST},
+	{"module-unsigned", &buf, &size, "", 0, 1, EKEYREJECTED},
 };
 
 static void setup(void)
 {
 	struct stat sb;
 	int fd;
+	struct tst_kcmdline_var params = TST_KCMDLINE_INIT("module.sig_enforce");
+
+	tst_kcmdline_parse(&params, 1);
+	if (params.found)
+		sig_enforce = atoi(params.value);
 
 	tst_module_exists(MODULE_NAME, NULL);
 
@@ -73,6 +81,16 @@ static void run(unsigned int n)
 		return;
 	}
 
+	if ((sig_enforce == 1) && (tc->exp_errno != EKEYREJECTED)) {
+		tst_res(TCONF, "module signature is enforced, skipping %s", tc->name);
+		return;
+	}
+
+	if ((sig_enforce != 1) && (tc->exp_errno == EKEYREJECTED)) {
+		tst_res(TCONF, "module signature is not enforced, skipping %s", tc->name);
+		return;
+	}
+
 	if (tc->cap)
 		tst_cap_action(&cap_drop);
 
Signature · 5 lines
2.40.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#5 lore raw

PATCH v3 3/3 stack_clash: make use of tst_kcmdline_parse

In reply to Li Wang
---
 testcases/cve/stack_clash.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
testcases/cve/stack_clash.c +5 -9
index 56b970a1b..8cc2c20b5 100644
@@ -44,6 +44,7 @@
 #include <stdlib.h>
 
 #include "tst_test.h"
+#include "tst_kconfig.h"
 #include "tst_safe_stdio.h"
 #include "lapi/mmap.h"
 
@@ -272,19 +273,14 @@ void do_child(void)
 
 void setup(void)
 {
-	char buf[4096], *p;
-
 	page_size = sysconf(_SC_PAGESIZE);
 	page_mask = ~(page_size - 1);
 
-	buf[4095] = '\0';
-	SAFE_FILE_SCANF("/proc/cmdline", "%4095[^\n]", buf);
+	struct tst_kcmdline_var params = TST_KCMDLINE_INIT("stack_guard_gap");
+	tst_kcmdline_parse(&params, 1);
 
-	if ((p = strstr(buf, "stack_guard_gap=")) != NULL) {
-		if (sscanf(p, "stack_guard_gap=%ld", &GAP_PAGES) != 1) {
-			tst_brk(TBROK | TERRNO, "sscanf");
-			return;
-		}
+	if (params.found) {
+		GAP_PAGES= atol(params.value);
 		tst_res(TINFO, "stack_guard_gap = %ld", GAP_PAGES);
 	}
 
Signature · 5 lines
2.40.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp