diff mbox series

[v1,3/3] syscalls/mallinfo03: Add an overflow test when setting 2G size

Message ID 1611654925-8994-3-git-send-email-xuyang2018.jy@cn.fujitsu.com
State Changes Requested
Headers show
Series [v1,1/3] syscalls/mallinfo01: Add a basic test for mallinfo | expand

Commit Message

Yang Xu Jan. 26, 2021, 9:55 a.m. UTC
Since these members of mallinfo struct use int data type, it will overflow
when allocating 2G size. mallinfo() is deprecated and we should use mallinfo2()
in the future.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/mallinfo/.gitignore |  1 +
 .../kernel/syscalls/mallinfo/mallinfo03.c     | 62 +++++++++++++++++++
 3 files changed, 64 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo03.c

Comments

Cyril Hrubis Jan. 28, 2021, 3:42 p.m. UTC | #1
Hi!
> +void test_mallinfo(void)
> +{
> +	struct mallinfo info;
> +	char *buf;
> +	size_t size = 2UL * 1024UL * 1024UL * 1024UL;
> +
> +	buf = SAFE_MALLOC(size);

If nothing else use of SAFE_MALLOC() here is wrong. The call may
potentionally fail and return NULL since we are passing overly large
value there.

For example it will fail if memory overcommit is disabled and there is
not enough free memory.

So we should, at least, use malloc() instead and skip the test if NULL
was returned.

> +	info = mallinfo();
> +	if (info.hblkhd < 0) {
> +		print_mallinfo("Test malloc 2G", &info);
> +		tst_res(TFAIL, "The member of struct mallinfo overflow, we should use mallinfo2");
> +	} else {
> +		/*We will never get here*/
> +		tst_res(TPASS, "The member of struct mallinfo doesn't overflow");
> +	}
> +	free(buf);
> +}
> +
> +static struct tst_test test = {
> +	.test_all = test_mallinfo,
> +};
> +
> +#else
> +TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
> +#endif
> -- 
> 2.23.0
> 
> 
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp
Yang Xu Feb. 3, 2021, 9:49 a.m. UTC | #2
Hi Cyril
> Hi!
>> +void test_mallinfo(void)
>> +{
>> +	struct mallinfo info;
>> +	char *buf;
>> +	size_t size = 2UL * 1024UL * 1024UL * 1024UL;
>> +
>> +	buf = SAFE_MALLOC(size);
>
> If nothing else use of SAFE_MALLOC() here is wrong. The call may
> potentionally fail and return NULL since we are passing overly large
> value there.
>
> For example it will fail if memory overcommit is disabled and there is
> not enough free memory.
>
> So we should, at least, use malloc() instead and skip the test if NULL
> was returned.
>
Agree. Will do it in v2.
>> +	info = mallinfo();
>> +	if (info.hblkhd<  0) {
>> +		print_mallinfo("Test malloc 2G",&info);
>> +		tst_res(TFAIL, "The member of struct mallinfo overflow, we should use mallinfo2");
>> +	} else {
>> +		/*We will never get here*/
>> +		tst_res(TPASS, "The member of struct mallinfo doesn't overflow");
>> +	}
>> +	free(buf);
>> +}
>> +
>> +static struct tst_test test = {
>> +	.test_all = test_mallinfo,
>> +};
>> +
>> +#else
>> +TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
>> +#endif
>> --
>> 2.23.0
>>
>>
>>
>>
>> --
>> Mailing list info: https://lists.linux.it/listinfo/ltp
>
Li Wang Feb. 4, 2021, 8:54 a.m. UTC | #3
Hi Xu,


> +void test_mallinfo(void)
> +{
> +       struct mallinfo info;
> +       char *buf;
> +       size_t size = 2UL * 1024UL * 1024UL * 1024UL;
> +
> +       buf = SAFE_MALLOC(size);
> +       info = mallinfo();
> +       if (info.hblkhd < 0) {
> +               print_mallinfo("Test malloc 2G", &info);
> +               tst_res(TFAIL, "The member of struct mallinfo overflow, we
> should use mallinfo2");
>

TPASS?


> +       } else {
> +               /*We will never get here*/
> +               tst_res(TPASS, "The member of struct mallinfo doesn't
> overflow");
>

TFAIL?



> +       }
> +       free(buf);
> +}
>
Yang Xu Feb. 4, 2021, 10:01 a.m. UTC | #4
Hi Li
> Hi Xu,
>
>     +void test_mallinfo(void)
>     +{
>     + struct mallinfo info;
>     + char *buf;
>     + size_t size = 2UL * 1024UL * 1024UL * 1024UL;
>     +
>     + buf = SAFE_MALLOC(size);
>     + info = mallinfo();
>     + if (info.hblkhd < 0) {
>     + print_mallinfo("Test malloc 2G", &info);
>     + tst_res(TFAIL, "The member of struct mallinfo overflow, we should
>     use mallinfo2");
>
> TPASS?
>
>     + } else {
>     + /*We will never get here*/
>     + tst_res(TPASS, "The member of struct mallinfo doesn't overflow");
>
>
> TFAIL?
>
>     + }
>     + free(buf);
>     +}

It is a "always" fail test.  We should use mallinfo2 in the future. I 
guess failure may attract user's attention than pass.

>
>
>
> --
> Regards,
> Li Wang
Li Wang Feb. 4, 2021, 1:56 p.m. UTC | #5
> >     + tst_res(TFAIL, "The member of struct mallinfo overflow, we should
> >     use mallinfo2");
> >
> > TPASS?
> >
> >     + } else {
> >     + /*We will never get here*/
> >     + tst_res(TPASS, "The member of struct mallinfo doesn't overflow");
> >
> >
> > TFAIL?
> >
> >     + }
> >     + free(buf);
> >     +}
>
> It is a "always" fail test.  We should use mallinfo2 in the future. I
> guess failure may attract user's attention than pass.
>

Sorry, I don't understand the intention here. I got failures like below,
allow it to report FAIL each time in CI/CD system?

# ./mallinfo03
tst_test.c:1263: TINFO: Timeout per run is 0h 05m 00s
tst_mallinfo.c:15: TINFO: Test malloc 2G...
tst_mallinfo.c:17: TINFO: arena: 135168
tst_mallinfo.c:18: TINFO: ordblks: 1
tst_mallinfo.c:19: TINFO: smblks: 0
tst_mallinfo.c:20: TINFO: hblks: 1
tst_mallinfo.c:21: TINFO: hblkhd: -2147479552
tst_mallinfo.c:22: TINFO: usmblks: 0
tst_mallinfo.c:23: TINFO: fsmblks: 0
tst_mallinfo.c:24: TINFO: uordblks: 848
tst_mallinfo.c:25: TINFO: fordblks: 134320
tst_mallinfo.c:26: TINFO: keepcost: 134320
mallinfo03.c:36: TFAIL: The member of struct mallinfo overflow, we should
use mallinfo2

Summary:
passed   0
failed   1
broken   0
skipped  0
warnings 0
diff mbox series

Patch

diff --git a/runtest/syscalls b/runtest/syscalls
index 74ad999e8..8fe123f8c 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -684,6 +684,7 @@  lstat02_64 lstat02_64
 
 mallinfo01 mallinfo01
 mallinfo02 mallinfo02
+mallinfo03 mallinfo03
 
 mallopt01 mallopt01
 
diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore b/testcases/kernel/syscalls/mallinfo/.gitignore
index 678ac277e..30c315cf2 100644
--- a/testcases/kernel/syscalls/mallinfo/.gitignore
+++ b/testcases/kernel/syscalls/mallinfo/.gitignore
@@ -1,2 +1,3 @@ 
 /mallinfo01
 /mallinfo02
+/mallinfo03
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo03.c b/testcases/kernel/syscalls/mallinfo/mallinfo03.c
new file mode 100644
index 000000000..e8dc35e42
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo03.c
@@ -0,0 +1,62 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test. Test the member of struct mallinfo
+ * whether overflow when setting 2G size. mallinfo() is deprecated
+ * since the type used for the fields is too small. We should use
+ * mallinfo2() and it was added since glibc 2.33.
+\*/
+
+#include <malloc.h>
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+
+#ifdef HAVE_MALLINFO
+static void print_mallinfo(const char *msg, struct mallinfo *m)
+{
+	tst_res(TINFO, "%s...", msg);
+#define P(f) tst_res(TINFO, "%s: %d", #f, m->f)
+	P(arena);
+	P(ordblks);
+	P(smblks);
+	P(hblks);
+	P(hblkhd);
+	P(usmblks);
+	P(fsmblks);
+	P(uordblks);
+	P(fordblks);
+	P(keepcost);
+}
+
+void test_mallinfo(void)
+{
+	struct mallinfo info;
+	char *buf;
+	size_t size = 2UL * 1024UL * 1024UL * 1024UL;
+
+	buf = SAFE_MALLOC(size);
+	info = mallinfo();
+	if (info.hblkhd < 0) {
+		print_mallinfo("Test malloc 2G", &info);
+		tst_res(TFAIL, "The member of struct mallinfo overflow, we should use mallinfo2");
+	} else {
+		/*We will never get here*/
+		tst_res(TPASS, "The member of struct mallinfo doesn't overflow");
+	}
+	free(buf);
+}
+
+static struct tst_test test = {
+	.test_all = test_mallinfo,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif