Skip to contentSkip to conversation

getcpu: Add testcase for EFAULT

PATCH Superseded Ma Xinjian via ltp 3 participants 6 messages Jul 17, 2024 – Aug 1, 2024

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 getcpu: Add testcase for EFAULT

Add a testcase with the arguments point to an invalid address.

---
 runtest/syscalls                            |  1 +
 testcases/kernel/syscalls/getcpu/.gitignore |  1 +
 testcases/kernel/syscalls/getcpu/getcpu02.c | 31 +++++++++++++++++++++
 3 files changed, 33 insertions(+)
 create mode 100644 testcases/kernel/syscalls/getcpu/getcpu02.c
runtest/syscalls +1 -0
index 136fd03fa..31922bc9f 100644
@@ -440,6 +440,7 @@ futimesat01 futimesat01
 getcontext01 getcontext01
 
 getcpu01 getcpu01
+getcpu02 getcpu02
 
 getcwd01 getcwd01
 getcwd02 getcwd02
testcases/kernel/syscalls/getcpu/.gitignore +1 -0
index 31fec5d35..cd3022bbb 100644
@@ -1 +1,2 @@
 /getcpu01
+/getcpu02
testcases/kernel/syscalls/getcpu/getcpu02.c new file +31 -0
new file mode 100644
index 000000000..0857a0598
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Copyright (c) Linux Test Project, 2024
+ * Author: Ma Xinjian <[email protected]>
+ *
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that getcpu(2) fails with
+ *
+ * - EFAULT arguments point outside the calling process's address
+ *          space.
+ */
+
+#define _GNU_SOURCE
+#include "tst_test.h"
+#include "lapi/sched.h"
+
+static void run(void) {
+        unsigned int cpu_id, node_id = 0;
+
+        TST_EXP_FAIL(getcpu(tst_get_bad_addr(NULL), &node_id), EFAULT);
+        TST_EXP_FAIL(getcpu(&cpu_id, tst_get_bad_addr(NULL)), EFAULT);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
Signature · 5 lines
2.18.1


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

Hi!
Please run make check and fix all the errors.

Signature · 5 lines
Cyril Hrubis
[email protected]

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

PATCH v2 1/1 getcpu: Add testcase for EFAULT

Add a testcase with the arguments point to an invalid address.

---
 runtest/syscalls                            |  1 +
 testcases/kernel/syscalls/getcpu/.gitignore |  1 +
 testcases/kernel/syscalls/getcpu/getcpu02.c | 32 +++++++++++++++++++++
 3 files changed, 34 insertions(+)
 create mode 100644 testcases/kernel/syscalls/getcpu/getcpu02.c
runtest/syscalls +1 -0
index 136fd03fa..31922bc9f 100644
@@ -440,6 +440,7 @@ futimesat01 futimesat01
 getcontext01 getcontext01
 
 getcpu01 getcpu01
+getcpu02 getcpu02
 
 getcwd01 getcwd01
 getcwd02 getcwd02
testcases/kernel/syscalls/getcpu/.gitignore +1 -0
index 31fec5d35..cd3022bbb 100644
@@ -1 +1,2 @@
 /getcpu01
+/getcpu02
testcases/kernel/syscalls/getcpu/getcpu02.c new file +32 -0
new file mode 100644
index 000000000..81a8dd871
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Copyright (c) Linux Test Project, 2024
+ * Author: Ma Xinjian <[email protected]>
+ *
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that getcpu(2) fails with
+ *
+ * - EFAULT arguments point outside the calling process's address
+ *          space.
+ */
+
+#define _GNU_SOURCE
+#include "tst_test.h"
+#include "lapi/sched.h"
+
+static void run(void)
+{
+	unsigned int cpu_id, node_id = 0;
+
+	TST_EXP_FAIL(getcpu(tst_get_bad_addr(NULL), &node_id), EFAULT);
+	TST_EXP_FAIL(getcpu(&cpu_id, tst_get_bad_addr(NULL)), EFAULT);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
Signature · 5 lines
2.18.1


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

Hi Ma, Cyril,

...

testcases/kernel/syscalls/getcpu/getcpu02.c line 26

Quoted 5 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +5 -0
+static void run(void)
+{
+	unsigned int cpu_id, node_id = 0;
+
+	TST_EXP_FAIL(getcpu(tst_get_bad_addr(NULL), &node_id), EFAULT);

I'm not sure why, but I get SIGSEGV due tst_get_bad_addr(NULL) on various kernels (SLES 5.14.21, Tumbleweed 6.5.1, 6.10, Debian 6.9, ...).

But the test works on SLES 4.4.180.

Kind regards,
Petr

Quoted 6 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +6 -0
+	TST_EXP_FAIL(getcpu(&cpu_id, tst_get_bad_addr(NULL)), EFAULT);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+};
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Hi!

Quoted 9 lines

testcases/kernel/syscalls/getcpu/getcpu02.c line 26

Quoted 5 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +5 -0
+static void run(void)
+{
+	unsigned int cpu_id, node_id = 0;
+
+	TST_EXP_FAIL(getcpu(tst_get_bad_addr(NULL), &node_id), EFAULT);

I'm not sure why, but I get SIGSEGV due tst_get_bad_addr(NULL) on various kernels (SLES 5.14.21, Tumbleweed 6.5.1, 6.10, Debian 6.9, ...).

But the test works on SLES 4.4.180.

If you are getting SIGSEGV that means that the address is used in userspace. Looking at man getcpu() it suggests that on some architectures it may be implemented as VDSO, which would explain it.

So I suppose that the easies solution here would be to run the test in a child process and accepting SIGSEGV as a correct outcome as well.

Signature · 5 lines
Cyril Hrubis
[email protected]

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

Hi!

Quoted 7 lines

testcases/kernel/syscalls/getcpu/getcpu02.c line 26

Quoted 5 lines
testcases/kernel/syscalls/getcpu/getcpu02.c new file +5 -0
+static void run(void)
+{
+	unsigned int cpu_id, node_id = 0;
+
+	TST_EXP_FAIL(getcpu(tst_get_bad_addr(NULL), &node_id), EFAULT);

I'm not sure why, but I get SIGSEGV due tst_get_bad_addr(NULL) on various kernels (SLES 5.14.21, Tumbleweed 6.5.1, 6.10, Debian 6.9, ...).

Quoted 8 lines
Quoted 1 line

But the test works on SLES 4.4.180.

If you are getting SIGSEGV that means that the address is used in userspace. Looking at man getcpu() it suggests that on some architectures it may be implemented as VDSO, which would explain it.

So I suppose that the easies solution here would be to run the test in a child process and accepting SIGSEGV as a correct outcome as well.

Thanks, Cyril.

Ma Xinjian, could you please send another version?

You can get inspiration in:
testcases/kernel/syscalls/setrlimit/setrlimit05.c

Or in others:

$ git grep -l .forks_child $(git grep -l SIGSEGV)

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp