Skip to contentSkip to conversation

Add tst_get_hugepage_size() to LTP library

PATCH Superseded Martin Doucha 2 participants 6 messages Jul 1, 2020 – Jul 3, 2020 Library code

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 Add tst_get_hugepage_size() to LTP library

---

No changes since v1.

 include/tst_hugepage.h | 5 +++++
 lib/tst_hugepage.c     | 9 +++++++++
 2 files changed, 14 insertions(+)
include/tst_hugepage.h +5 -0
index 60c03c5c5..e08a2daa2 100644
@@ -12,6 +12,11 @@
 extern char *nr_opt; /* -s num   Set the number of the been allocated hugepages */
 extern char *Hopt;   /* -H /..   Location of hugetlbfs, i.e.  -H /var/hugetlbfs */
 
+/*
+ * Get the default hugepage size. Returns 0 if hugepages are not supported.
+ */
+size_t tst_get_hugepage_size(void);
+
 /*
  * Try the best to request a specified number of huge pages from system,
  * it will store the reserved hpage number in tst_hugepages.
lib/tst_hugepage.c +9 -0
index f2bf5d20e..3a8dcca90 100644
@@ -12,6 +12,15 @@ unsigned long tst_hugepages;
 char *nr_opt;
 char *Hopt;
 
+size_t tst_get_hugepage_size(void)
+{
+	if (access(PATH_HUGEPAGES, F_OK)) {
+		return 0;
+	}
+
+	return SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
+}
+
 unsigned long tst_request_hugepages(unsigned long hpages)
 {
 	unsigned long val, max_hpages;
Signature · 1 line
2.26.2
#1 lore raw

PATCH v2 2/2 Add test for CVE 2017-1000405

Fixes #316

---

Changes since v1:
- include lapi/mmap.h
- TCONF if madvise(MADV_HUGEPAGE) fails with EINVAL

 runtest/cve                       |   1 +
 runtest/mm                        |   1 +
 testcases/kernel/mem/.gitignore   |   1 +
 testcases/kernel/mem/thp/Makefile |   1 +
 testcases/kernel/mem/thp/thp04.c  | 160 ++++++++++++++++++++++++++++++
 5 files changed, 164 insertions(+)
 create mode 100644 testcases/kernel/mem/thp/thp04.c
runtest/cve +1 -0
index a3a25dbe1..fdb455af1 100644
@@ -43,6 +43,7 @@ cve-2017-18075 pcrypt_aead01
 cve-2017-1000111 setsockopt07
 cve-2017-1000112 setsockopt05
 cve-2017-1000380 snd_timer01
+cve-2017-1000405 thp04
 cve-2018-5803 sctp_big_chunk
 cve-2018-7566 snd_seq01
 cve-2018-8897 ptrace09
runtest/mm +1 -0
index 612a4d066..4701a14bd 100644
@@ -86,6 +86,7 @@ swapping01 swapping01 -i 5
 thp01 thp01 -I 120
 thp02 thp02
 thp03 thp03
+thp04 thp04
 
 vma01 vma01
 vma02 vma02
testcases/kernel/mem/.gitignore +1 -0
index ce21ca70f..b95ada109 100644
@@ -65,6 +65,7 @@
 /thp/thp01
 /thp/thp02
 /thp/thp03
+/thp/thp04
 /tunable/max_map_count
 /tunable/min_free_kbytes
 /tunable/overcommit_memory
testcases/kernel/mem/thp/Makefile +1 -0
index 867dcf089..89abdc7cd 100644
@@ -18,6 +18,7 @@
 #
 
 top_srcdir		?= ../../../..
+thp04:			LDLIBS += -lrt
 
 include $(top_srcdir)/include/mk/testcases.mk
 include $(top_srcdir)/testcases/kernel/mem/include/libmem.mk
testcases/kernel/mem/thp/thp04.c new file +160 -0

160 lines not on this page · raw message

Signature · 1 line
2.26.2
#2 lore raw

PATCH v2 2/2 Add test for CVE 2017-1000405

Martin Doucha <[email protected]> wrote:

...

testcases/kernel/mem/thp/thp04.c line 39

Quoted 11 lines
testcases/kernel/mem/thp/thp04.c new file +6 -0
+static void *alloc_zero_page(void *baseaddr)
+{
+       int i;
+       void *ret;
+
+       /* Find aligned chunk of address space. MAP_HUGETLB doesn't work.

*/

testcases/kernel/mem/thp/thp04.c new file +3 -0
+       for (i = 0; i < 16; i++, baseaddr += thp_size) {
+               ret = mmap(baseaddr, thp_size, PROT_READ,
+                       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

I'm wondering why here request the READ ONLY memory, shouldn't we write something into the write_thp loopingly then?

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200702/373aa7d0/attachment.htm>
#3 lore raw

PATCH v2 2/2 Add test for CVE 2017-1000405

Quoted 16 lines · Li Wang

Martin Doucha <[email protected] <mailto:[email protected]>> wrote:

    ...
    +static void *alloc_zero_page(void *baseaddr)
    +{
    +? ? ? ?int i;
    +? ? ? ?void *ret;
    +
    +? ? ? ?/* Find aligned chunk of address space. MAP_HUGETLB doesn't
    work. */
    +? ? ? ?for (i = 0; i < 16; i++, baseaddr += thp_size) {
    +? ? ? ? ? ? ? ?ret = mmap(baseaddr, thp_size, PROT_READ,
    +? ? ? ? ? ? ? ? ? ? ? ?MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Quoted 2 lines

I'm wondering why here request the READ ONLY memory, shouldn't we write something into the write_thp loopingly then?

We do write into it. Except we do it through /proc/self/mem file descriptor. I didn't try with PROT_WRITE but presume that mapping the memory as read-only is important for confusing the kernel into thinking that the address space doesn't need to be copied on write.

Nevertheless, I've tested the reproducer on a vulnerable kernel and it works reliably.

Signature · 7 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic
#4 lore raw

PATCH v2 2/2 Add test for CVE 2017-1000405

Quoted 20 lines · Martin Doucha
Quoted 16 lines · Li Wang

Martin Doucha <[email protected] <mailto:[email protected]>> wrote:

...
+static void *alloc_zero_page(void *baseaddr)
+{
+ int i;
+ void *ret;
+
+ /* Find aligned chunk of address space. MAP_HUGETLB doesn't
work. */
+ for (i = 0; i < 16; i++, baseaddr += thp_size) {
+ ret = mmap(baseaddr, thp_size, PROT_READ,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

Quoted 2 lines

I'm wondering why here request the READ ONLY memory, shouldn't we write something into the write_thp loopingly then?

Quoted 8 lines

We do write into it. Except we do it through /proc/self/mem file descriptor. I didn't try with PROT_WRITE but presume that mapping the memory as read-only is important for confusing the kernel into thinking that the address space doesn't need to be copied on write.

Nevertheless, I've tested the reproducer on a vulnerable kernel and it works reliably.

Thanks for the explanation, I helped merge the patchset v3.

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200703/a1f14e85/attachment-0001.htm>
#5 lore raw

PATCH v2 1/2 Add tst_get_hugepage_size() to LTP library

In reply to Martin Doucha
Quoted 2 lines · Martin Doucha
Quoted 32 lines

---

No changes since v1.

 include/tst_hugepage.h | 5 +++++
 lib/tst_hugepage.c     | 9 +++++++++
 2 files changed, 14 insertions(+)
include/tst_hugepage.h +0 -0
index 60c03c5c5..e08a2daa2 100644
@@ -12,6 +12,11 @@
 extern char *nr_opt; /* -s num   Set the number of the been allocated

hugepages */
extern char *Hopt; /* -H /.. Location of hugetlbfs, i.e. -H
/var/hugetlbfs */

+/*
+ * Get the default hugepage size. Returns 0 if hugepages are not supported.
+ */

include/tst_hugepage.h +2 -0
+size_t tst_get_hugepage_size(void);
+
 /*
  * Try the best to request a specified number of huge pages from system,
  * it will store the reserved hpage number in tst_hugepages.
lib/tst_hugepage.c +0 -0
index f2bf5d20e..3a8dcca90 100644

lib/tst_hugepage.c unsigned long tst_hugepages; · line 18

Quoted 7 lines
lib/tst_hugepage.c +3 -0
@@ -12,6 +12,15 @@ unsigned long tst_hugepages;
 char *nr_opt;
 char *Hopt;
 
+size_t tst_get_hugepage_size(void)
+{
+       if (access(PATH_HUGEPAGES, F_OK)) {
 

The braces here is redundant. Someone who merging the patch can help clean this.

Quoted 17 lines
lib/tst_hugepage.c +6 -0
+               return 0;
+       }
+
+       return SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
+}
+
 unsigned long tst_request_hugepages(unsigned long hpages)
 {
        unsigned long val, max_hpages;

--
2.26.2

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

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200702/c2b85b7a/attachment.htm>