Skip to contentSkip to conversation

tst_module: Add separate declarations of helpers for new tests framework

PATCH New Viresh Kumar 2 participants 11 messages Dec 17, 2020 – Jan 6, 2021 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 tst_module: Add separate declarations of helpers for new tests framework

The tests using the new test framework must not use the headers from the old one, fix that by declaring the helpers in tst_module.h. To achieve that rename the original routines in tst_module.c with an underscore and use static inline wrappers to call them.

Also update the delete_module tests to use the new header.

---
V2:
- New patch.

 include/old/old_module.h                      | 27 ++++++++++++---
 include/tst_module.h                          | 33 +++++++++++++++++++
 lib/tst_module.c                              |  8 ++---
 .../syscalls/delete_module/delete_module01.c  |  6 ++--
 .../syscalls/delete_module/delete_module03.c  | 12 +++----
 5 files changed, 68 insertions(+), 18 deletions(-)
 create mode 100644 include/tst_module.h
include/old/old_module.h +22 -5
index c50efec76244..1af7d1f68027 100644
@@ -34,6 +34,14 @@
 #ifndef TST_MODULE
 #define TST_MODULE
 
+void tst_module_exist_(void (cleanup_fn)(void), const char *mod_name,
+					 char **mod_path);
+
+void tst_module_load_(void (cleanup_fn)(void), const char *mod_name,
+					char *const argv[]);
+
+void tst_module_unload_(void (cleanup_fn)(void), const char *mod_name);
+
 /*
  * Check module existence.
  *
@@ -44,8 +52,11 @@
  *
  * In case of failure, test'll call cleanup_fn and exit with TCONF return value.
  */
-void tst_module_exist(void (cleanup_fn)(void), const char *mod_name,
-	char **mod_path);
+static inline void tst_module_exist(void (cleanup_fn)(void),
+				    const char *mod_name, char **mod_path)
+{
+	tst_module_exist_(cleanup_fn, mod_name, mod_path);
+}
 
 /*
  * Load a module using insmod program.
@@ -58,8 +69,11 @@ void tst_module_exist(void (cleanup_fn)(void), const char *mod_name,
  * In case of insmod failure, test will call cleanup_fn and exit with TBROK
  * return value.
  */
-void tst_module_load(void (cleanup_fn)(void),
-	const char *mod_name, char *const argv[]);
+static inline void tst_module_load(void (cleanup_fn)(void),
+				   const char *mod_name, char *const argv[])
+{
+	tst_module_load_(cleanup_fn, mod_name, argv);
+}
 
 /*
  * Unload a module using rmmod program. In case of failure, test will call
@@ -67,6 +81,9 @@ void tst_module_load(void (cleanup_fn)(void),
  *
  * @mod_name: can be module name or module's file name.
  */
-void tst_module_unload(void (cleanup_fn)(void), const char *mod_name);
+static inline void tst_module_unload(void (cleanup_fn)(void), const char *mod_name)
+{
+	tst_module_unload_(cleanup_fn, mod_name);
+}
 
 #endif /* TST_MODULE */
include/tst_module.h new file +33 -0
new file mode 100644
index 000000000000..637e85c0bf2f
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
+ * Alexey Kodanev <[email protected]>
+ */
+
+#ifndef TST_MODULE_H
+#define TST_MODULE_H
+
+void tst_module_exist_(void (cleanup_fn)(void), const char *mod_name,
+					 char **mod_path);
+
+static inline void tst_module_exist(const char *mod_name, char **mod_path)
+{
+	tst_module_exist_(NULL, mod_name, mod_path);
+}
+
+void tst_module_load_(void (cleanup_fn)(void), const char *mod_name,
+					char *const argv[]);
+
+static inline void tst_module_load(const char *mod_name, char *const argv[])
+{
+	tst_module_load_(NULL, mod_name, argv);
+}
+
+void tst_module_unload_(void (cleanup_fn)(void), const char *mod_name);
+
+static inline void tst_module_unload(const char *mod_name)
+{
+	tst_module_unload_(NULL, mod_name);
+}
+
+#endif /* TST_MODULE_H */
lib/tst_module.c +4 -4
index eda61872fe01..9bd443623616 100644
@@ -28,7 +28,7 @@
 #include "ltp_priv.h"
 #include "old_module.h"
 
-void tst_module_exists(void (cleanup_fn)(void),
+void tst_module_exists_(void (cleanup_fn)(void),
 	const char *mod_name, char **mod_path)
 {
 	/* check current working directory */
@@ -77,11 +77,11 @@ void tst_module_exists(void (cleanup_fn)(void),
 		free(buf);
 }
 
-void tst_module_load(void (cleanup_fn)(void),
+void tst_module_load_(void (cleanup_fn)(void),
 	const char *mod_name, char *const argv[])
 {
 	char *mod_path = NULL;
-	tst_module_exists(cleanup_fn, mod_name, &mod_path);
+	tst_module_exists_(cleanup_fn, mod_name, &mod_path);
 
 	const int offset = 2; /* command name & module path */
 	int size = 0;
@@ -101,7 +101,7 @@ void tst_module_load(void (cleanup_fn)(void),
 	free(mod_path);
 }
 
-void tst_module_unload(void (cleanup_fn)(void), const char *mod_name)
+void tst_module_unload_(void (cleanup_fn)(void), const char *mod_name)
 {
 	int i, rc;
 
testcases/kernel/syscalls/delete_module/delete_module01.c +3 -3
index e5cb53cc9ec3..8fb559f0c703 100644
@@ -14,9 +14,9 @@
  */
 
 #include <errno.h>
-#include "old_module.h"
 #include "lapi/syscalls.h"
 #include "tst_test.h"
+#include "tst_module.h"
 
 #define MODULE_NAME	"dummy_del_mod"
 #define MODULE_NAME_KO	"dummy_del_mod.ko"
@@ -26,7 +26,7 @@ static int module_loaded;
 static void do_delete_module(void)
 {
 	if (module_loaded == 0) {
-		tst_module_load(NULL, MODULE_NAME_KO, NULL);
+		tst_module_load(MODULE_NAME_KO, NULL);
 		module_loaded = 1;
 	}
 
@@ -44,7 +44,7 @@ static void do_delete_module(void)
 static void cleanup(void)
 {
 	if (module_loaded == 1)
-		tst_module_unload(NULL, MODULE_NAME_KO);
+		tst_module_unload(MODULE_NAME_KO);
 }
 
 static struct tst_test test = {
testcases/kernel/syscalls/delete_module/delete_module03.c +6 -6
index 8bd51be07dc2..7178e8ef13b0 100644
@@ -14,7 +14,7 @@
 
 #include <errno.h>
 #include "tst_test.h"
-#include "old_module.h"
+#include "tst_module.h"
 #include "lapi/syscalls.h"
 
 #define DUMMY_MOD		"dummy_del_mod"
@@ -43,7 +43,7 @@ static void do_delete_module(void)
 		 * insmod DUMMY_MOD_KO again in case running
 		 * with -i option
 		 */
-		tst_module_load(NULL, DUMMY_MOD_KO, NULL);
+		tst_module_load(DUMMY_MOD_KO, NULL);
 		dummy_mod_loaded = 1;
 	}
 }
@@ -51,11 +51,11 @@ static void do_delete_module(void)
 static void setup(void)
 {
 	/* Load first kernel module */
-	tst_module_load(NULL, DUMMY_MOD_KO, NULL);
+	tst_module_load(DUMMY_MOD_KO, NULL);
 	dummy_mod_loaded = 1;
 
 	/* Load dependant kernel module */
-	tst_module_load(NULL, DUMMY_MOD_DEP_KO, NULL);
+	tst_module_load(DUMMY_MOD_DEP_KO, NULL);
 	dummy_mod_dep_loaded = 1;
 }
 
@@ -63,11 +63,11 @@ static void cleanup(void)
 {
 	/* Unload dependent kernel module */
 	if (dummy_mod_dep_loaded == 1)
-		tst_module_unload(NULL, DUMMY_MOD_DEP_KO);
+		tst_module_unload(DUMMY_MOD_DEP_KO);
 
 	/* Unload first kernel module */
 	if (dummy_mod_loaded == 1)
-		tst_module_unload(NULL, DUMMY_MOD_KO);
+		tst_module_unload(DUMMY_MOD_KO);
 }
 
 static struct tst_test test = {
Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
#1 lore raw

PATCH v2 2/3 syscalls: init_module: Add tests

This patch adds success and failure tests for init_module() syscall.

---
V2:
- Remove unwanted header
- Use TST_EXP_*() macros
- Always provide definition of init_module().
- Rename module.

 include/lapi/init_module.h                    | 19 ++++
 runtest/syscalls                              |  3 +
 .../kernel/syscalls/init_module/.gitignore    |  3 +
 .../kernel/syscalls/init_module/Makefile      | 21 ++++
 .../kernel/syscalls/init_module/init_module.c | 37 +++++++
 .../syscalls/init_module/init_module01.c      | 54 ++++++++++
 .../syscalls/init_module/init_module02.c      | 99 +++++++++++++++++++
 7 files changed, 236 insertions(+)
 create mode 100644 include/lapi/init_module.h
 create mode 100644 testcases/kernel/syscalls/init_module/.gitignore
 create mode 100644 testcases/kernel/syscalls/init_module/Makefile
 create mode 100644 testcases/kernel/syscalls/init_module/init_module.c
 create mode 100644 testcases/kernel/syscalls/init_module/init_module01.c
 create mode 100644 testcases/kernel/syscalls/init_module/init_module02.c
include/lapi/init_module.h new file +19 -0
new file mode 100644
index 000000000000..65ff70356c60
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Linaro Limited. All rights reserved.
+ * Author: Viresh Kumar <[email protected]>
+ */
+
+#ifndef INIT_MODULE_H__
+#define INIT_MODULE_H__
+
+#include "config.h"
+#include "lapi/syscalls.h"
+#include "tst_test.h"
+
+static inline int init_module(void *module_image, unsigned long len,
+			      const char *param_values)
+{
+	return tst_syscall(__NR_init_module, module_image, len, param_values);
+}
+#endif /* INIT_MODULE_H__ */
runtest/syscalls +3 -0
index 9c328697b4a3..28174dddd716 100644
@@ -517,6 +517,9 @@ getxattr03 getxattr03
 getxattr04 getxattr04
 getxattr05 getxattr05
 
+init_module01 init_module01
+init_module02 init_module02
+
 #Needs tty device.
 #ioctl01 ioctl01 -D /dev/tty0
 #ioctl02 ioctl02 -D /dev/tty0
testcases/kernel/syscalls/init_module/.gitignore new file +3 -0
new file mode 100644
index 000000000000..b4d11e958544
@@ -0,0 +1,3 @@
+/init_module01
+/init_module02
+/*.ko
testcases/kernel/syscalls/init_module/Makefile new file +21 -0
new file mode 100644
index 000000000000..ebdca67d401d
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+ifneq ($(KERNELRELEASE),)
+
+obj-m := init_module.o
+
+else
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+REQ_VERSION_MAJOR	:= 2
+REQ_VERSION_PATCH	:= 6
+
+MAKE_TARGETS		:= init_module01 init_module02 init_module.ko
+
+include $(top_srcdir)/include/mk/module.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
+
+endif
testcases/kernel/syscalls/init_module/init_module.c new file +37 -0
new file mode 100644
index 000000000000..78d03b899a7e
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Viresh Kumar <[email protected]>
+ *
+ * Dummy test module.
+ *
+ * The module accepts a single argument named "status" and it fails
+ * initialization if the status is set to "invalid".
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/proc_fs.h>
+#include <linux/kernel.h>
+
+static char status[20];
+module_param_string(status, status, 20, 0444);
+
+static int dummy_init(void)
+{
+	struct proc_dir_entry *proc_dummy;
+
+	if (!strcmp(status, "invalid"))
+		return -EINVAL;
+
+	proc_dummy = proc_mkdir("dummy", 0);
+	return 0;
+}
+module_init(dummy_init);
+
+static void dummy_exit(void)
+{
+	remove_proc_entry("dummy", 0);
+}
+module_exit(dummy_exit);
+
+MODULE_LICENSE("GPL");
testcases/kernel/syscalls/init_module/init_module01.c new file +54 -0

54 lines not on this page · raw message

testcases/kernel/syscalls/init_module/init_module02.c new file +99 -0

99 lines not on this page · raw message

Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
#2 lore raw

PATCH v2 2/3 syscalls: init_module: Add tests

Hi!
Pushed with two small fixes, thanks.

* Added dummy call to tst_module_exists() to the setup()s which causes the test to exit with TCONF if the module wasn't build for example when kernel devel package wasn't installed

* Cleaned up a bit the init_module02.c run() function

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

PATCH v2 3/3 syscalls: finit_module: Add tests

In reply to Viresh Kumar

This patch adds success and failure tests for finit_module() syscall.

---
V2:
- Remove unwanted header
- Use TST_EXP_*() macros
- Rename module.

 include/lapi/init_module.h                    | 16 ++++
 runtest/syscalls                              |  3 +
 .../kernel/syscalls/finit_module/.gitignore   |  3 +
 .../kernel/syscalls/finit_module/Makefile     | 21 +++++
 .../syscalls/finit_module/finit_module.c      | 37 ++++++++
 .../syscalls/finit_module/finit_module01.c    | 49 +++++++++++
 .../syscalls/finit_module/finit_module02.c    | 84 +++++++++++++++++++
 7 files changed, 213 insertions(+)
 create mode 100644 testcases/kernel/syscalls/finit_module/.gitignore
 create mode 100644 testcases/kernel/syscalls/finit_module/Makefile
 create mode 100644 testcases/kernel/syscalls/finit_module/finit_module.c
 create mode 100644 testcases/kernel/syscalls/finit_module/finit_module01.c
 create mode 100644 testcases/kernel/syscalls/finit_module/finit_module02.c
include/lapi/init_module.h +16 -0
index 65ff70356c60..9e79e11b8557 100644
@@ -16,4 +16,20 @@ static inline int init_module(void *module_image, unsigned long len,
 {
 	return tst_syscall(__NR_init_module, module_image, len, param_values);
 }
+
+static inline int finit_module(int fd, const char *param_values, int flags)
+{
+	return tst_syscall(__NR_finit_module, fd, param_values, flags);
+}
+
+void finit_module_supported_by_kernel(void)
+{
+       if ((tst_kvercmp(3, 8, 0)) < 0) {
+               /* Check if the syscall is backported on an older kernel */
+               TEST(syscall(__NR_finit_module, 0, "", 0));
+               if (TST_RET == -1 && TST_ERR == ENOSYS)
+                       tst_brk(TCONF, "Test not supported on kernel version < v3.8");
+       }
+}
+
 #endif /* INIT_MODULE_H__ */
runtest/syscalls +3 -0
index 28174dddd716..961545e73834 100644
@@ -327,6 +327,9 @@ fgetxattr01 fgetxattr01
 fgetxattr02 fgetxattr02
 fgetxattr03 fgetxattr03
 
+finit_module01 finit_module01
+finit_module02 finit_module02
+
 flistxattr01 flistxattr01
 flistxattr02 flistxattr02
 flistxattr03 flistxattr03
testcases/kernel/syscalls/finit_module/.gitignore new file +3 -0
new file mode 100644
index 000000000000..5fcafdb3736d
@@ -0,0 +1,3 @@
+/finit_module01
+/finit_module02
+/*.ko
testcases/kernel/syscalls/finit_module/Makefile new file +21 -0
new file mode 100644
index 000000000000..a529695d23cc
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+ifneq ($(KERNELRELEASE),)
+
+obj-m := finit_module.o
+
+else
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+REQ_VERSION_MAJOR	:= 3
+REQ_VERSION_PATCH	:= 8
+
+MAKE_TARGETS		:= finit_module01 finit_module02 finit_module.ko
+
+include $(top_srcdir)/include/mk/module.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
+
+endif
testcases/kernel/syscalls/finit_module/finit_module.c new file +37 -0
new file mode 100644
index 000000000000..78d03b899a7e
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Viresh Kumar <[email protected]>
+ *
+ * Dummy test module.
+ *
+ * The module accepts a single argument named "status" and it fails
+ * initialization if the status is set to "invalid".
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/proc_fs.h>
+#include <linux/kernel.h>
+
+static char status[20];
+module_param_string(status, status, 20, 0444);
+
+static int dummy_init(void)
+{
+	struct proc_dir_entry *proc_dummy;
+
+	if (!strcmp(status, "invalid"))
+		return -EINVAL;
+
+	proc_dummy = proc_mkdir("dummy", 0);
+	return 0;
+}
+module_init(dummy_init);
+
+static void dummy_exit(void)
+{
+	remove_proc_entry("dummy", 0);
+}
+module_exit(dummy_exit);
+
+MODULE_LICENSE("GPL");
testcases/kernel/syscalls/finit_module/finit_module01.c new file +49 -0

49 lines not on this page · raw message

testcases/kernel/syscalls/finit_module/finit_module02.c new file +84 -0

84 lines not on this page · raw message

Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
#4 lore raw

PATCH v2 3/3 syscalls: finit_module: Add tests

Hi!
This finit_module02 fails for me:

tst_test.c:1261: TINFO: Timeout per run is 0h 05m 00s
finit_module02.c:66: TPASS: finit_module(*tc->fd, tc->param, tc->flags): EINVAL (22) finit_module02.c:66: TFAIL: finit_module(*tc->fd, tc->param, tc->flags) expected ENOEXEC: EBADF (9) finit_module02.c:66: TPASS: finit_module(*tc->fd, tc->param, tc->flags): EFAULT (14) finit_module02.c:66: TPASS: finit_module(*tc->fd, tc->param, tc->flags): EINVAL (22) finit_module02.c:66: TPASS: finit_module(*tc->fd, tc->param, tc->flags): EINVAL (22) tst_capability.c:29: TINFO: Dropping CAP_SYS_MODULE(16)
finit_module02.c:66: TPASS: finit_module(*tc->fd, tc->param, tc->flags): EPERM (1) tst_capability.c:41: TINFO: Permitting CAP_SYS_MODULE(16)
finit_module02.c:59: TPASS: finit_module(*tc->fd, tc->param, tc->flags) passed finit_module02.c:63: TPASS: finit_module(*tc->fd, tc->param, tc->flags): EEXIST (17) finit_module02.c:66: TFAIL: finit_module(*tc->fd, tc->param, tc->flags) expected EBADF: ETXTBSY (26)

Linux 5.9.12

Also it looks to me like EBADF is more reasonable error for fd set to -1 also ETXTBSY sounds more reasonable for a file opened for writing.

I guess that someone cleaned up the kernel implementation but forget to update the manual pages?

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

PATCH v2 3/3 syscalls: finit_module: Add tests

Quoted 16 lines · Cyril Hrubis

Hi!
This finit_module02 fails for me:

tst_test.c:1261: TINFO: Timeout per run is 0h 05m 00s
finit_module02.c:66: TPASS: finit_module(*tc->fd, tc->param, tc->flags): EINVAL (22) finit_module02.c:66: TFAIL: finit_module(*tc->fd, tc->param, tc->flags) expected ENOEXEC: EBADF (9) finit_module02.c:66: TPASS: finit_module(*tc->fd, tc->param, tc->flags): EFAULT (14) finit_module02.c:66: TPASS: finit_module(*tc->fd, tc->param, tc->flags): EINVAL (22) finit_module02.c:66: TPASS: finit_module(*tc->fd, tc->param, tc->flags): EINVAL (22) tst_capability.c:29: TINFO: Dropping CAP_SYS_MODULE(16)
finit_module02.c:66: TPASS: finit_module(*tc->fd, tc->param, tc->flags): EPERM (1) tst_capability.c:41: TINFO: Permitting CAP_SYS_MODULE(16)
finit_module02.c:59: TPASS: finit_module(*tc->fd, tc->param, tc->flags) passed finit_module02.c:63: TPASS: finit_module(*tc->fd, tc->param, tc->flags): EEXIST (17) finit_module02.c:66: TFAIL: finit_module(*tc->fd, tc->param, tc->flags) expected EBADF: ETXTBSY (26)

Quoted 7 lines

Linux 5.9.12

Also it looks to me like EBADF is more reasonable error for fd set to -1 also ETXTBSY sounds more reasonable for a file opened for writing.

I guess that someone cleaned up the kernel implementation but forget to update the manual pages?

Merged in v4.6 and I am on 4.4 :(

commit a1db74209483a24c861c848b4bb79a4d945ef6fa

Author: Mimi Zohar <[email protected]>
Date:   Wed Dec 30 07:35:30 2015 -0500

module: replace copy_module_from_fd with kernel version

Replace copy_module_from_fd() with kernel_read_file_from_fd().

Although none of the upstreamed LSMs define a kernel_module_from_file hook, IMA is called, based on policy, to prevent unsigned kernel modules from being loaded by the original kernel module syscall and to measure/appraise signed kernel modules.

The security function security_kernel_module_from_file() was called prior to reading a kernel module. Preventing unsigned kernel modules from being loaded by the original kernel module syscall remains on the pre-read kernel_read_file() security hook. Instead of reading the kernel module twice, once for measuring/appraising and again for loading the kernel module, the signature validation is moved to the kernel_post_read_file() security hook.

This patch removes the security_kernel_module_from_file() hook and security call.

Signature · 1 line
viresh
#6 lore raw

PATCH v3 3/3 syscalls: finit_module: Add tests

In reply to Viresh Kumar

This patch adds success and failure tests for finit_module() syscall.

---
 include/lapi/init_module.h                    | 16 ++++
 runtest/syscalls                              |  3 +
 .../kernel/syscalls/finit_module/.gitignore   |  3 +
 .../kernel/syscalls/finit_module/Makefile     | 21 +++++
 .../syscalls/finit_module/finit_module.c      | 37 ++++++++
 .../syscalls/finit_module/finit_module01.c    | 49 ++++++++++
 .../syscalls/finit_module/finit_module02.c    | 89 +++++++++++++++++++
 7 files changed, 218 insertions(+)
 create mode 100644 testcases/kernel/syscalls/finit_module/.gitignore
 create mode 100644 testcases/kernel/syscalls/finit_module/Makefile
 create mode 100644 testcases/kernel/syscalls/finit_module/finit_module.c
 create mode 100644 testcases/kernel/syscalls/finit_module/finit_module01.c
 create mode 100644 testcases/kernel/syscalls/finit_module/finit_module02.c
include/lapi/init_module.h +16 -0
index 65ff70356c60..9e79e11b8557 100644
@@ -16,4 +16,20 @@ static inline int init_module(void *module_image, unsigned long len,
 {
 	return tst_syscall(__NR_init_module, module_image, len, param_values);
 }
+
+static inline int finit_module(int fd, const char *param_values, int flags)
+{
+	return tst_syscall(__NR_finit_module, fd, param_values, flags);
+}
+
+void finit_module_supported_by_kernel(void)
+{
+       if ((tst_kvercmp(3, 8, 0)) < 0) {
+               /* Check if the syscall is backported on an older kernel */
+               TEST(syscall(__NR_finit_module, 0, "", 0));
+               if (TST_RET == -1 && TST_ERR == ENOSYS)
+                       tst_brk(TCONF, "Test not supported on kernel version < v3.8");
+       }
+}
+
 #endif /* INIT_MODULE_H__ */
runtest/syscalls +3 -0
index 28174dddd716..961545e73834 100644
@@ -327,6 +327,9 @@ fgetxattr01 fgetxattr01
 fgetxattr02 fgetxattr02
 fgetxattr03 fgetxattr03
 
+finit_module01 finit_module01
+finit_module02 finit_module02
+
 flistxattr01 flistxattr01
 flistxattr02 flistxattr02
 flistxattr03 flistxattr03
testcases/kernel/syscalls/finit_module/.gitignore new file +3 -0
new file mode 100644
index 000000000000..5fcafdb3736d
@@ -0,0 +1,3 @@
+/finit_module01
+/finit_module02
+/*.ko
testcases/kernel/syscalls/finit_module/Makefile new file +21 -0
new file mode 100644
index 000000000000..a529695d23cc
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+ifneq ($(KERNELRELEASE),)
+
+obj-m := finit_module.o
+
+else
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+REQ_VERSION_MAJOR	:= 3
+REQ_VERSION_PATCH	:= 8
+
+MAKE_TARGETS		:= finit_module01 finit_module02 finit_module.ko
+
+include $(top_srcdir)/include/mk/module.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
+
+endif
testcases/kernel/syscalls/finit_module/finit_module.c new file +37 -0
new file mode 100644
index 000000000000..78d03b899a7e
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Viresh Kumar <[email protected]>
+ *
+ * Dummy test module.
+ *
+ * The module accepts a single argument named "status" and it fails
+ * initialization if the status is set to "invalid".
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/proc_fs.h>
+#include <linux/kernel.h>
+
+static char status[20];
+module_param_string(status, status, 20, 0444);
+
+static int dummy_init(void)
+{
+	struct proc_dir_entry *proc_dummy;
+
+	if (!strcmp(status, "invalid"))
+		return -EINVAL;
+
+	proc_dummy = proc_mkdir("dummy", 0);
+	return 0;
+}
+module_init(dummy_init);
+
+static void dummy_exit(void)
+{
+	remove_proc_entry("dummy", 0);
+}
+module_exit(dummy_exit);
+
+MODULE_LICENSE("GPL");
testcases/kernel/syscalls/finit_module/finit_module01.c new file +49 -0

49 lines not on this page · raw message

testcases/kernel/syscalls/finit_module/finit_module02.c new file +89 -0

89 lines not on this page · raw message

Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
#7 lore raw

PATCH v3 3/3 syscalls: finit_module: Add tests

Hi!

testcases/kernel/syscalls/finit_module/finit_module02.c line 54

Quoted 7 lines
testcases/kernel/syscalls/finit_module/finit_module02.c new file +7 -0
+static void setup(void)
+{
+	finit_module_supported_by_kernel();
+
+	/* Kernel returned ENOEXEC for fd = -1 earlier */
+	if ((tst_kvercmp(4, 6, 0)) < 0)
+		tcases[0].exp_errno = ENOEXEC;

There are plenty of ways how to avoid hackery like that.

What about we defined a setup function in the test structure and then loop over the testcases in the test setup() and execute all per-test setup functions we have.

The we can keep the errno set to zero in the static structure and initialize it in the per-testcase setup as:

static void bad_fd_setup(struct tcase *tc)

{
	if (tst_kvercmp(4, 6, 0)) < 0)
		tc->errno = ENOEXEC;
	else
		tc->errno = EBADFD;
}

Also I think that the last test needs to handle ETXTBSY in the same way and we may possibly add more tests since I guess that most of the errors that apply to execve() would apply here as well. What happpens for example if we pass a fd that points to a directory?

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

PATCH v4 3/3 syscalls: finit_module: Add tests

In reply to Viresh Kumar

This patch adds success and failure tests for finit_module() syscall.

---
V4:
- Add fix_errno() callback in tcase in finit_module02.c and use it for 3 of the tests.
- Add test where fd is a directory.

 include/lapi/init_module.h                    |  16 +++
 runtest/syscalls                              |   3 +
 .../kernel/syscalls/finit_module/.gitignore   |   3 +
 .../kernel/syscalls/finit_module/Makefile     |  21 +++
 .../syscalls/finit_module/finit_module.c      |  37 +++++
 .../syscalls/finit_module/finit_module01.c    |  49 +++++++
 .../syscalls/finit_module/finit_module02.c    | 131 ++++++++++++++++++
 7 files changed, 260 insertions(+)
 create mode 100644 testcases/kernel/syscalls/finit_module/.gitignore
 create mode 100644 testcases/kernel/syscalls/finit_module/Makefile
 create mode 100644 testcases/kernel/syscalls/finit_module/finit_module.c
 create mode 100644 testcases/kernel/syscalls/finit_module/finit_module01.c
 create mode 100644 testcases/kernel/syscalls/finit_module/finit_module02.c
include/lapi/init_module.h +16 -0
index 65ff70356c60..9e79e11b8557 100644
@@ -16,4 +16,20 @@ static inline int init_module(void *module_image, unsigned long len,
 {
 	return tst_syscall(__NR_init_module, module_image, len, param_values);
 }
+
+static inline int finit_module(int fd, const char *param_values, int flags)
+{
+	return tst_syscall(__NR_finit_module, fd, param_values, flags);
+}
+
+void finit_module_supported_by_kernel(void)
+{
+       if ((tst_kvercmp(3, 8, 0)) < 0) {
+               /* Check if the syscall is backported on an older kernel */
+               TEST(syscall(__NR_finit_module, 0, "", 0));
+               if (TST_RET == -1 && TST_ERR == ENOSYS)
+                       tst_brk(TCONF, "Test not supported on kernel version < v3.8");
+       }
+}
+
 #endif /* INIT_MODULE_H__ */
runtest/syscalls +3 -0
index 28174dddd716..961545e73834 100644
@@ -327,6 +327,9 @@ fgetxattr01 fgetxattr01
 fgetxattr02 fgetxattr02
 fgetxattr03 fgetxattr03
 
+finit_module01 finit_module01
+finit_module02 finit_module02
+
 flistxattr01 flistxattr01
 flistxattr02 flistxattr02
 flistxattr03 flistxattr03
testcases/kernel/syscalls/finit_module/.gitignore new file +3 -0
new file mode 100644
index 000000000000..5fcafdb3736d
@@ -0,0 +1,3 @@
+/finit_module01
+/finit_module02
+/*.ko
testcases/kernel/syscalls/finit_module/Makefile new file +21 -0
new file mode 100644
index 000000000000..a529695d23cc
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+ifneq ($(KERNELRELEASE),)
+
+obj-m := finit_module.o
+
+else
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+REQ_VERSION_MAJOR	:= 3
+REQ_VERSION_PATCH	:= 8
+
+MAKE_TARGETS		:= finit_module01 finit_module02 finit_module.ko
+
+include $(top_srcdir)/include/mk/module.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
+
+endif
testcases/kernel/syscalls/finit_module/finit_module.c new file +37 -0
new file mode 100644
index 000000000000..78d03b899a7e
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Viresh Kumar <[email protected]>
+ *
+ * Dummy test module.
+ *
+ * The module accepts a single argument named "status" and it fails
+ * initialization if the status is set to "invalid".
+ */
+
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/proc_fs.h>
+#include <linux/kernel.h>
+
+static char status[20];
+module_param_string(status, status, 20, 0444);
+
+static int dummy_init(void)
+{
+	struct proc_dir_entry *proc_dummy;
+
+	if (!strcmp(status, "invalid"))
+		return -EINVAL;
+
+	proc_dummy = proc_mkdir("dummy", 0);
+	return 0;
+}
+module_init(dummy_init);
+
+static void dummy_exit(void)
+{
+	remove_proc_entry("dummy", 0);
+}
+module_exit(dummy_exit);
+
+MODULE_LICENSE("GPL");
testcases/kernel/syscalls/finit_module/finit_module01.c new file +49 -0

49 lines not on this page · raw message

testcases/kernel/syscalls/finit_module/finit_module02.c new file +131 -0

131 lines not on this page · raw message

Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
#10 lore raw

PATCH v2 1/3 tst_module: Add separate declarations of helpers for new tests framework

In reply to Viresh Kumar

Hi!
Pushed with a small fix, thanks.

It turned out that there was a typo in the old_module.h header, the function tst_module_exist() never existed and it's supposed to be tst_module_exists(). So I've renamed all instances accordingly.

Signature · 2 lines
Cyril Hrubis
[email protected]