Skip to contentSkip to conversation

cve-2017-17052: tolerate ENOMEM during test

PATCH In review Li Wang via ltp 3 participants 6 messages Jan 29 – Jan 30

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cve-2017-17052: tolerate ENOMEM during test

As each iteration of mmap_thread() grabs a fresh 16 MiB MAP_POPULATE region and never releases it. As the loop runs, those regions accumulate consuming both virtual address space and committed physical memory right away instead of lazily.

Easily mmap() fails with ENOMEM on smaller/limit RAM resource system.

Error Log:

cve-2017-17052.c:48: TBROK: mmap((nil),16777216,PROT_READ(1),32802,-1,0) failed: ENOMEM (12) tst_test.c:479: TINFO: Child process reported TBROK killing the test tst_test.c:1909: TINFO: Killed the leftover descendant processes

Consider there is no practical upper bound on this allocation pattern, so setting .test.min_mem_avail may not helps. Here we just tolerate ENOMEM during the mmap_thread() looping.

---
 testcases/cve/cve-2017-17052.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
testcases/cve/cve-2017-17052.c +8 -1
index 700eb782e..61032c197 100644
@@ -44,9 +44,16 @@ static void cleanup(void)
 
 static void *mmap_thread(void *arg)
 {
+	void *ptr;
+
 	for (;;) {
-		SAFE_MMAP(NULL, 0x1000000, PROT_READ,
+		ptr = mmap(NULL, 0x1000000, PROT_READ,
 				MAP_POPULATE|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+
+		if ((ptr == MAP_FAILED) && (errno == ENOMEM)) {
+			usleep(1000);
+			continue;
+		}
 	}
 
 	return arg;
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw
Quoted 32 lines · Li Wang via ltp

As each iteration of mmap_thread() grabs a fresh 16 MiB MAP_POPULATE region and never releases it. As the loop runs, those regions accumulate consuming both virtual address space and committed physical memory right away instead of lazily.

Easily mmap() fails with ENOMEM on smaller/limit RAM resource system.

Error Log:

cve-2017-17052.c:48: TBROK: mmap((nil),16777216,PROT_READ(1),32802,-1,0) failed: ENOMEM (12) tst_test.c:479: TINFO: Child process reported TBROK killing the test tst_test.c:1909: TINFO: Killed the leftover descendant processes

Consider there is no practical upper bound on this allocation pattern, so setting .test.min_mem_avail may not helps. Here we just tolerate ENOMEM during the mmap_thread() looping.

---
 testcases/cve/cve-2017-17052.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
testcases/cve/cve-2017-17052.c +1 -0
index 700eb782e..61032c197 100644
@@ -44,9 +44,16 @@ static void cleanup(void)
 
 static void *mmap_thread(void *arg)
 {
+       void *ptr;

testcases/cve/cve-2017-17052.c static void cleanup(void) · line 56

Quoted 10 lines
testcases/cve/cve-2017-17052.c +7 -1
+
        for (;;) {
-               SAFE_MMAP(NULL, 0x1000000, PROT_READ,
+               ptr = mmap(NULL, 0x1000000, PROT_READ,
                                MAP_POPULATE|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+
+               if ((ptr == MAP_FAILED) && (errno == ENOMEM)) {
+                       usleep(1000);
+                       continue;
+               }

Oops, I forgot to handle the other failures, so the patch should be:

testcases/cve/cve-2017-17052.c +11 -1
@@ -44,9 +44,19 @@ static void cleanup(void)
 
 static void *mmap_thread(void *arg)
 {
+       void *ptr;
+
        for (;;) {
-               SAFE_MMAP(NULL, 0x1000000, PROT_READ,
+               ptr = mmap(NULL, 0x1000000, PROT_READ,
                                MAP_POPULATE|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+
+               if (ptr == MAP_FAILED) {
+                       if (errno == ENOMEM) {
+                               usleep(1000);
+                               continue;
+                       }
+                       tst_brk(TBROK | TTERRNO, "Unexpected mmap() error");
+               }
        }
 
        return arg;
Signature · 6 lines
Regards,
Li Wang


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

Hi Li,

[ Cc others as it's a pre-release ]

Quoted 59 lines · Li Wang via ltp
Quoted 32 lines

As each iteration of mmap_thread() grabs a fresh 16 MiB MAP_POPULATE region and never releases it. As the loop runs, those regions accumulate consuming both virtual address space and committed physical memory right away instead of lazily.

Easily mmap() fails with ENOMEM on smaller/limit RAM resource system.

Error Log:

cve-2017-17052.c:48: TBROK: mmap((nil),16777216,PROT_READ(1),32802,-1,0) failed: ENOMEM (12) tst_test.c:479: TINFO: Child process reported TBROK killing the test tst_test.c:1909: TINFO: Killed the leftover descendant processes

Consider there is no practical upper bound on this allocation pattern, so setting .test.min_mem_avail may not helps. Here we just tolerate ENOMEM during the mmap_thread() looping.

---
 testcases/cve/cve-2017-17052.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
testcases/cve/cve-2017-17052.c +1 -0
index 700eb782e..61032c197 100644
@@ -44,9 +44,16 @@ static void cleanup(void)
 
 static void *mmap_thread(void *arg)
 {
+       void *ptr;

testcases/cve/cve-2017-17052.c static void cleanup(void) · line 56

Quoted 10 lines
testcases/cve/cve-2017-17052.c +7 -1
+
        for (;;) {
-               SAFE_MMAP(NULL, 0x1000000, PROT_READ,
+               ptr = mmap(NULL, 0x1000000, PROT_READ,
                                MAP_POPULATE|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+
+               if ((ptr == MAP_FAILED) && (errno == ENOMEM)) {
+                       usleep(1000);
+                       continue;
+               }

Oops, I forgot to handle the other failures, so the patch should be:

testcases/cve/cve-2017-17052.c +4 -1
@@ -44,9 +44,19 @@ static void cleanup(void)
 
 static void *mmap_thread(void *arg)
 {
+       void *ptr;
+
        for (;;) {
-               SAFE_MMAP(NULL, 0x1000000, PROT_READ,
+               ptr = mmap(NULL, 0x1000000, PROT_READ,
                                MAP_POPULATE|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+

testcases/cve/cve-2017-17052.c static void cleanup(void) · line 62

Quoted 10 lines
testcases/cve/cve-2017-17052.c +7 -0
+               if (ptr == MAP_FAILED) {
+                       if (errno == ENOMEM) {
+                               usleep(1000);
+                               continue;
+                       }
+                       tst_brk(TBROK | TTERRNO, "Unexpected mmap() error");
+               }
        }
 
        return arg;

Patched version LGTM.

Out of curiosity how much RAM is not enough for this? I tried 400 MB + eating other RAM but was not able to trigger.

Kind regards,
Petr

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

Out of curiosity how much RAM is not enough for this? I tried 400 MB + eating other RAM but was not able to trigger.

It occurs (but rarely) in our CI jobs, and is hard to reproduce manually.

But if you limit resources in a cgroup or prolong the looping time for a while, that would be possible reproduce the fail.

Signature · 6 lines
Regards,
Li Wang


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by
#4 lore raw
In reply to Li Wang via ltp

Hi!

testcases/cve/cve-2017-17052.c static void cleanup(void) · line 58

Quoted 11 lines
        for (;;) {
testcases/cve/cve-2017-17052.c +8 -1
-               SAFE_MMAP(NULL, 0x1000000, PROT_READ,
+               ptr = mmap(NULL, 0x1000000, PROT_READ,
                                MAP_POPULATE|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
+
+               if (ptr == MAP_FAILED) {
+                       if (errno == ENOMEM) {
+                               usleep(1000);
+                               continue;
+                       }
+                       tst_brk(TBROK | TTERRNO, "Unexpected mmap() error");

Maybe this part would be more readable with an else branch:

			if (errno == ENOMEM)
				usleep(1000);
			else
				tst_brk(TBROK | TTERRNO, "mmap() failed");

Otherwise:

Quoted 14 lines

+ }

        }

        return arg;
Signature · 6 lines
Regards,
Li Wang


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw
Quoted 13 lines · Cyril Hrubis

Hi!

testcases/cve/cve-2017-17052.c static void cleanup(void)

Quoted 4 lines
        for (;;) {
testcases/cve/cve-2017-17052.c +1 -1
-               SAFE_MMAP(NULL, 0x1000000, PROT_READ,
+               ptr = mmap(NULL, 0x1000000, PROT_READ,
                                MAP_POPULATE|MAP_ANONYMOUS|MAP_PRIVATE,

-1, 0);

testcases/cve/cve-2017-17052.c static void cleanup(void)

Quoted 7 lines
testcases/cve/cve-2017-17052.c +7 -0
+
+               if (ptr == MAP_FAILED) {
+                       if (errno == ENOMEM) {
+                               usleep(1000);
+                               continue;
+                       }
+                       tst_brk(TBROK | TTERRNO, "Unexpected mmap()
Quoted 9 lines

error");

Maybe this part would be more readable with an else branch:

                        if (errno == ENOMEM)
                                usleep(1000);
                        else
                                tst_brk(TBROK | TTERRNO, "mmap() failed");

Good suggestion, patch merged like this. Thanks!

Signature · 5 lines
Regards,
Li Wang

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