Skip to contentSkip to conversation

SysV IPC bug reproducer

PATCH Superseded Andrea Cervesato 4 participants 11 messages Feb 26, 2024 – Mar 4, 2024 Library code

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover SysV IPC bug reproducer

From: Andrea Cervesato <[email protected]>

This patch series provides a bug reproducer for SysV IPC, which has been written by Michal Hocko.
It also includes the SAFE_MPROTECT macro, useful for many other tests as well.

Andrea Cervesato (2):
Add SAFE_MPROTECT() macro
Add shmat04 SysV IPC bug reproducer

 include/tst_safe_macros.h                     |   5 +
 lib/safe_macros.c                             |  18 +++
 runtest/syscalls                              |   1 +
 runtest/syscalls-ipc                          |   1 +
 .../kernel/syscalls/ipc/shmat/.gitignore      |   1 +
 testcases/kernel/syscalls/ipc/shmat/shmat04.c | 115 ++++++++++++++++++
 6 files changed, 141 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ipc/shmat/shmat04.c
Signature · 5 lines
2.35.3


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

PATCH v2 1/2 Add SAFE_MPROTECT() macro

From: Andrea Cervesato <[email protected]>

---
 include/tst_safe_macros.h |  5 +++++
 lib/safe_macros.c         | 18 ++++++++++++++++++
 2 files changed, 23 insertions(+)
include/tst_safe_macros.h +5 -0
index f2ce8919b..bd401f094 100644
@@ -625,6 +625,11 @@ int safe_munlock(const char *file, const int lineno, const char *addr,
 	size_t len);
 #define SAFE_MUNLOCK(addr, len) safe_munlock(__FILE__, __LINE__, (addr), (len))
 
+int safe_mprotect(const char *file, const int lineno, const char *addr,
+	size_t len, int prot);
+#define SAFE_MPROTECT(addr, len, prot) \
+	safe_mprotect(__FILE__, __LINE__, (addr), (len), (prot))
+
 int safe_mincore(const char *file, const int lineno, void *start,
 	size_t length, unsigned char *vec);
 #define SAFE_MINCORE(start, length, vec) \
lib/safe_macros.c +18 -0
index 951e1b064..69ca3eabc 100644
@@ -1317,6 +1317,24 @@ int safe_munlock(const char *file, const int lineno, const void *addr,
 	return rval;
 }
 
+int safe_mprotect(const char *file, const int lineno, const void *addr,
+	size_t len, int prot)
+{
+	int rval;
+
+	rval = mprotect(addr, len, prot);
+
+	if (rval == -1) {
+		tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
+			"mprotect() failed");
+	} else if (rval) {
+		tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
+			"Invalid mprotect() return value %d", rval);
+	}
+
+	return rval;
+}
+
 int safe_mincore(const char *file, const int lineno, void *start,
 	size_t length, unsigned char *vec)
 {
Signature · 5 lines
2.35.3


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

Re: [LTP] [PATCH v2 1/2] Add SAFE_MPROTECT() macro

In reply to Andrea Cervesato

Hi Andrea,

Quoted 5 lines
---
 include/tst_safe_macros.h |  5 +++++
 lib/safe_macros.c         | 18 ++++++++++++++++++
 2 files changed, 23 insertions(+)

Although Cyril acked this I wonder why we are adding it into the old API. Shouldn't it be added only into tst_safe_macros.c?

Kind regards,
Petr

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

Re: [LTP] [PATCH v2 1/2] Add SAFE_MPROTECT() macro

Hi!

Quoted 8 lines
Quoted 5 lines
---
 include/tst_safe_macros.h |  5 +++++
 lib/safe_macros.c         | 18 ++++++++++++++++++
 2 files changed, 23 insertions(+)

Although Cyril acked this I wonder why we are adding it into the old API. Shouldn't it be added only into tst_safe_macros.c?

To be honest I do not care that much as long as we do not expose the functions into the headers for old API. We will have to convert the safe_macros.c with coccinelle once all tests are converted and one more function in there does not really matter that much.

Signature · 5 lines
Cyril Hrubis
[email protected]

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

Re: [LTP] [PATCH v2 1/2] Add SAFE_MPROTECT() macro

Quoted 7 lines

Hi!

Quoted 5 lines
Quoted 5 lines
---
 include/tst_safe_macros.h |  5 +++++
 lib/safe_macros.c         | 18 ++++++++++++++++++
 2 files changed, 23 insertions(+)
Quoted 7 lines
Quoted 2 lines

Although Cyril acked this I wonder why we are adding it into the old API. Shouldn't it be added only into tst_safe_macros.c?

To be honest I do not care that much as long as we do not expose the functions into the headers for old API. We will have to convert the safe_macros.c with coccinelle once all tests are converted and one more function in there does not really matter that much.

OTOH we have tst_* file, moving it there (+ remove 'm' from functions) would be trivial (and less confusing). Sure, finally coccinelle will be used.

Kind regards,
Petr

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

PATCH v2 2/2 Add shmat04 SysV IPC bug reproducer

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

This is an LTP port of a SysV bug reproducer provided by Michal Hocko.

When debugging issues with a workload using SysV shmem, Michal Hocko has come up with a reproducer that shows how a series of mprotect() operations can result in an elevated shm_nattch and thus leak of the resource.

---
Simplified the algorithm
Usage of SAFE_MPROTECT
More TINFO messages

 runtest/syscalls                              |   1 +
 runtest/syscalls-ipc                          |   1 +
 .../kernel/syscalls/ipc/shmat/.gitignore      |   1 +
 testcases/kernel/syscalls/ipc/shmat/shmat04.c | 115 ++++++++++++++++++
 4 files changed, 118 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ipc/shmat/shmat04.c
runtest/syscalls +1 -0
index 7794f1465..cc0144379 100644
@@ -1445,6 +1445,7 @@ setxattr03 setxattr03
 shmat01 shmat01
 shmat02 shmat02
 shmat03 shmat03
+shmat04 shmat04
 
 shmctl01 shmctl01
 shmctl02 shmctl02
runtest/syscalls-ipc +1 -0
index df41140a7..9860394de 100644
@@ -49,6 +49,7 @@ semop03 semop03
 
 shmat01 shmat01
 shmat02 shmat02
+shmat04 shmat04
 
 shmctl01 shmctl01
 shmctl02 shmctl02
testcases/kernel/syscalls/ipc/shmat/.gitignore +1 -0
index 2b972f8f2..5600b2742 100644
@@ -1,3 +1,4 @@
 /shmat01
 /shmat02
 /shmat03
+/shmat04
testcases/kernel/syscalls/ipc/shmat/shmat04.c new file +115 -0

115 lines not on this page · raw message

Signature · 5 lines
2.35.3


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

Re: [LTP] [PATCH v2 2/2] Add shmat04 SysV IPC bug reproducer

Hi!

Quoted 9 lines
testcases/kernel/syscalls/ipc/shmat/shmat04.c new file +9 -0
+static void change_access(void *addr, int size, int prot)
+{
+	switch (prot) {
+	case PROT_NONE:
+		tst_res(TINFO, "Disable memory access. addr: %p - size: %d",
+			addr, size);
+		break;
+	case PROT_WRITE:
+		tst_res(TINFO, "Enable write memory access. addr: %p - size: %d",

testcases/kernel/syscalls/ipc/shmat/shmat04.c line 51

Quoted 10 lines
testcases/kernel/syscalls/ipc/shmat/shmat04.c new file +10 -0
+			addr, size);
+		break;
+	default:
+		tst_res(TINFO, "Change memory access. addr: %p - size: %d",
+			addr, size);
+		break;
+	}
+
+	SAFE_MPROTECT(addr, size, prot);
+}

Hmm, it's kind of ugly how we wrap the macro here like that...

What about we instead add debugging messages to all the SAFE_MACROS()?

Given that we added TDEBUG flag recently we can do soemthing as:

	tst_res_(TDEBUG, file, lineno, "mprotect(%p, %d, %s)",
	         addr, size, prot_to_str(prot));

To the SAFE_MPROTECT() and get the verbose output for free with verbose flag passed to the test.

We can do that with all SAFE_MACROS() then we do not have to print most of the messages in this test...

Quoted 30 lines
testcases/kernel/syscalls/ipc/shmat/shmat04.c new file +30 -0
+
+static void run(void)
+{
+	struct shmid_ds shmid_ds;
+	void *sh_mem;
+
+	segment_id = SAFE_SHMGET(
+		key_id,
+		segment_size,
+		IPC_CREAT | IPC_EXCL | 0600);
+
+	sh_mem = SAFE_SHMAT(segment_id, NULL, 0);
+
+	tst_res(TINFO, "Attached at %p. key: %d - size: %lu",
+		sh_mem, segment_id, segment_size);
+
+	SAFE_SHMCTL(segment_id, IPC_STAT, &shmid_ds);
+
+	tst_res(TINFO, "Number of attaches: %lu", shmid_ds.shm_nattch);
+
+	change_access(sh_mem + page_size, page_size, PROT_NONE);
+	change_access(sh_mem, 2 * page_size, PROT_WRITE);
+
+	SAFE_SHMCTL(segment_id, IPC_STAT, &shmid_ds);
+
+	tst_res(TINFO, "Number of attaches: %lu", shmid_ds.shm_nattch);
+	tst_res(TINFO, "Delete attached memory");
+
+	SAFE_SHMDT(sh_mem);
+	SAFE_SHMCTL(segment_id, IPC_STAT, &shmid_ds);

testcases/kernel/syscalls/ipc/shmat/shmat04.c line 91

Quoted 10 lines
testcases/kernel/syscalls/ipc/shmat/shmat04.c new file +10 -0
+
+	tst_res(TINFO, "Number of attaches: %lu", shmid_ds.shm_nattch);
+
+	SAFE_SHMCTL(segment_id, IPC_RMID, NULL);
+	segment_id = -1;
+
+	if (shmid_ds.shm_nattch)
+		tst_res(TFAIL, "The system is affected by the SysV IPC bug");
+	else
+		tst_res(TPASS, "Test passed");

These messages are not really that useful, we can as well do:

TST_EXP_EQ_LU(shmid_ds.shm_nattach, 0);

That will provide better message than "PASS: Test passed"

Quoted 30 lines
testcases/kernel/syscalls/ipc/shmat/shmat04.c new file +24 -0
+}
+
+static void setup(void)
+{
+	key_id = GETIPCKEY();
+	page_size = getpagesize();
+
+	tst_res(TINFO, "Key id: %d", key_id);
+	tst_res(TINFO, "Page size: %d", page_size);
+
+	segment_size = 3 * page_size;
+}
+
+static void cleanup(void)
+{
+	if (segment_id != -1)
+		SAFE_SHMCTL(segment_id, IPC_RMID, NULL);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+};
Signature · 5 lines
2.35.3


-- 
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
#8 lore raw

Re: [LTP] [PATCH v2 2/2] Add shmat04 SysV IPC bug reproducer

Hi!

Quoted 26 lines · Cyril Hrubis

Hi!

Quoted 9 lines
testcases/kernel/syscalls/ipc/shmat/shmat04.c new file +9 -0
+static void change_access(void *addr, int size, int prot)
+{
+	switch (prot) {
+	case PROT_NONE:
+		tst_res(TINFO, "Disable memory access. addr: %p - size: %d",
+			addr, size);
+		break;
+	case PROT_WRITE:
+		tst_res(TINFO, "Enable write memory access. addr: %p - size: %d",

testcases/kernel/syscalls/ipc/shmat/shmat04.c line 51

Quoted 10 lines
testcases/kernel/syscalls/ipc/shmat/shmat04.c new file +10 -0
+			addr, size);
+		break;
+	default:
+		tst_res(TINFO, "Change memory access. addr: %p - size: %d",
+			addr, size);
+		break;
+	}
+
+	SAFE_MPROTECT(addr, size, prot);
+}

Hmm, it's kind of ugly how we wrap the macro here like that...

What about we instead add debugging messages to all the SAFE_MACROS()?

Given that we added TDEBUG flag recently we can do soemthing as:

Quoted 8 lines
	tst_res_(TDEBUG, file, lineno, "mprotect(%p, %d, %s)",
	         addr, size, prot_to_str(prot));

To the SAFE_MPROTECT() and get the verbose output for free with verbose flag passed to the test.

We can do that with all SAFE_MACROS() then we do not have to print most of the messages in this test...

Is this comment related with the previous patch of the set?

Quoted 74 lines
Quoted 30 lines
testcases/kernel/syscalls/ipc/shmat/shmat04.c new file +30 -0
+
+static void run(void)
+{
+	struct shmid_ds shmid_ds;
+	void *sh_mem;
+
+	segment_id = SAFE_SHMGET(
+		key_id,
+		segment_size,
+		IPC_CREAT | IPC_EXCL | 0600);
+
+	sh_mem = SAFE_SHMAT(segment_id, NULL, 0);
+
+	tst_res(TINFO, "Attached at %p. key: %d - size: %lu",
+		sh_mem, segment_id, segment_size);
+
+	SAFE_SHMCTL(segment_id, IPC_STAT, &shmid_ds);
+
+	tst_res(TINFO, "Number of attaches: %lu", shmid_ds.shm_nattch);
+
+	change_access(sh_mem + page_size, page_size, PROT_NONE);
+	change_access(sh_mem, 2 * page_size, PROT_WRITE);
+
+	SAFE_SHMCTL(segment_id, IPC_STAT, &shmid_ds);
+
+	tst_res(TINFO, "Number of attaches: %lu", shmid_ds.shm_nattch);
+	tst_res(TINFO, "Delete attached memory");
+
+	SAFE_SHMDT(sh_mem);
+	SAFE_SHMCTL(segment_id, IPC_STAT, &shmid_ds);

testcases/kernel/syscalls/ipc/shmat/shmat04.c line 91

Quoted 10 lines
testcases/kernel/syscalls/ipc/shmat/shmat04.c new file +10 -0
+
+	tst_res(TINFO, "Number of attaches: %lu", shmid_ds.shm_nattch);
+
+	SAFE_SHMCTL(segment_id, IPC_RMID, NULL);
+	segment_id = -1;
+
+	if (shmid_ds.shm_nattch)
+		tst_res(TFAIL, "The system is affected by the SysV IPC bug");
+	else
+		tst_res(TPASS, "Test passed");

These messages are not really that useful, we can as well do:

TST_EXP_EQ_LU(shmid_ds.shm_nattach, 0);

That will provide better message than "PASS: Test passed"

Quoted 28 lines
testcases/kernel/syscalls/ipc/shmat/shmat04.c new file +24 -0
+}
+
+static void setup(void)
+{
+	key_id = GETIPCKEY();
+	page_size = getpagesize();
+
+	tst_res(TINFO, "Key id: %d", key_id);
+	tst_res(TINFO, "Page size: %d", page_size);
+
+	segment_size = 3 * page_size;
+}
+
+static void cleanup(void)
+{
+	if (segment_id != -1)
+		SAFE_SHMCTL(segment_id, IPC_RMID, NULL);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+};
Signature · 1 line
2.35.3
Quoted 2 lines
Quoted 2 lines
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp

Andrea

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

Re: [LTP] [PATCH v2 2/2] Add shmat04 SysV IPC bug reproducer

Hi!

Quoted 9 lines
Quoted 9 lines

Hmm, it's kind of ugly how we wrap the macro here like that...

What about we instead add debugging messages to all the SAFE_MACROS()?

Given that we added TDEBUG flag recently we can do soemthing as:

	tst_res_(TDEBUG, file, lineno, "mprotect(%p, %d, %s)",
	         addr, size, prot_to_str(prot));
Quoted 6 lines
Quoted 5 lines

To the SAFE_MPROTECT() and get the verbose output for free with verbose flag passed to the test.

We can do that with all SAFE_MACROS() then we do not have to print most of the messages in this test...

Is this comment related with the previous patch of the set?

Not at all, I'm just complaining that we are adding debuging print to the test itself when it would be much cleaner to put it into the test library instead.

Signature · 5 lines
Cyril Hrubis
[email protected]

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

Re: [LTP] [PATCH v2 2/2] Add shmat04 SysV IPC bug reproducer

Quoted 13 lines

Hi!

Quoted 11 lines
Quoted 11 lines

Hmm, it's kind of ugly how we wrap the macro here like that...

What about we instead add debugging messages to all the SAFE_MACROS()?

Given that we added TDEBUG flag recently we can do soemthing as:

	tst_res_(TDEBUG, file, lineno, "mprotect(%p, %d, %s)",
	         addr, size, prot_to_str(prot));

To the SAFE_MPROTECT() and get the verbose output for free with verbose flag passed to the test.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

We can do that with all SAFE_MACROS() then we do not have to print most of the messages in this test...

Is this comment related with the previous patch of the set?

Not at all, I'm just complaining that we are adding debuging print to the test itself when it would be much cleaner to put it into the test library instead.

+1, setting patchset as changes requested, please send v3.

Kind regards,
Petr

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