Skip to contentSkip to conversation

Fix patch creation with GCC 6.1+

PATCH Superseded Ross Lagerwall 4 participants 8 messages Nov 24, 2016 – Nov 25, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix patch creation with GCC 6.1+

GCC 6.1+ fixed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=192 which means that .rodata.str* sections are now split by function. We could probably be smarter about including just the sections we need, but for now, include all .rodata.str* sections as is done for previous versions of GCC.

This manifests itself as symbol error. E.g.:
(XEN) Unknown symbol: .LC0

---
 create-diff-object.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
create-diff-object.c +38 -1
index 69bcd88..b0d1348 100644
@@ -1184,6 +1184,43 @@ static void kpatch_process_special_sections(struct kpatch_elf *kelf)
 	}
 }
 
+/* Returns true if s is a string of only numbers with length > 0. */
+static int isnumber(const char *s)
+{
+	do {
+		if (!*s || !isdigit(*s))
+			return 0;
+	} while (*++s);
+
+	return 1;
+}
+
+/*
+ * String sections are always included even if unchanged.
+ * The format is either:
+ * .rodata.<func>.str1.[0-9]+ (new in GCC 6.1.0)
+ * or .rodata.str1.[0-9]+ (older versions of GCC)
+ * For the new format we could be smarter and only include the needed
+ * strings sections.
+ */
+static int should_include_str_section(const char *name)
+{
+	const char *s;
+
+	if (strncmp(name, ".rodata.", 8))
+		return 0;
+
+	/* Check if name matches ".rodata.str1.[0-9]+" */
+	if (!strncmp(name, ".rodata.str1.", 13))
+		return isnumber(name + 13);
+
+	/* Check if name matches ".rodata.<func>.str1.[0-9]+" */
+	s = strstr(name, ".str1.");
+	if (!s)
+		return 0;
+	return isnumber(s + 6);
+}
+
 static void kpatch_include_standard_elements(struct kpatch_elf *kelf)
 {
 	struct section *sec;
@@ -1193,7 +1230,7 @@ static void kpatch_include_standard_elements(struct kpatch_elf *kelf)
 		if (!strcmp(sec->name, ".shstrtab") ||
 		    !strcmp(sec->name, ".strtab") ||
 		    !strcmp(sec->name, ".symtab") ||
-		    !strncmp(sec->name, ".rodata.str1.", 13)) {
+		    should_include_str_section(sec->name)) {
 			sec->include = 1;
 			if (sec->secsym)
 				sec->secsym->include = 1;
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw
Quoted 6 lines · Ross Lagerwall

GCC 6.1+ fixed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=192 which means that .rodata.str* sections are now split by function. We could probably be smarter about including just the sections we need, but for now, include all .rodata.str* sections as is done for previous versions of GCC.

Here you say .str*

But the code only does this for .str1.*

Did you mean to make it.more generic for say .rodata.*.str[0-9].*

?

Quoted 67 lines

This manifests itself as symbol error. E.g.:
(XEN) Unknown symbol: .LC0

---
create-diff-object.c | 39 ++++++++++++++++++++++++++++++++++++++-

1 file changed, 38 insertions(+), 1 deletion(-)
create-diff-object.c +37 -0
index 69bcd88..b0d1348 100644
@@ -1184,6 +1184,43 @@ static void
 kpatch_process_special_sections(struct kpatch_elf *kelf)
 	}
 }
 
+/* Returns true if s is a string of only numbers with length > 0. */
+static int isnumber(const char *s)
+{
+	do {
+		if (!*s || !isdigit(*s))
+			return 0;
+	} while (*++s);
+
+	return 1;
+}
+
+/*
+ * String sections are always included even if unchanged.
+ * The format is either:
+ * .rodata.<func>.str1.[0-9]+ (new in GCC 6.1.0)
+ * or .rodata.str1.[0-9]+ (older versions of GCC)
+ * For the new format we could be smarter and only include the needed
+ * strings sections.
+ */
+static int should_include_str_section(const char *name)
+{
+	const char *s;
+
+	if (strncmp(name, ".rodata.", 8))
+		return 0;
+
+	/* Check if name matches ".rodata.str1.[0-9]+" */
+	if (!strncmp(name, ".rodata.str1.", 13))
+		return isnumber(name + 13);
+
+	/* Check if name matches ".rodata.<func>.str1.[0-9]+" */
+	s = strstr(name, ".str1.");
+	if (!s)
+		return 0;
+	return isnumber(s + 6);
+}
+
 static void kpatch_include_standard_elements(struct kpatch_elf *kelf)
 {
	struct section *sec;
create-diff-object.c +1 -1
@@ -1193,7 +1230,7 @@ static void
 kpatch_include_standard_elements(struct kpatch_elf *kelf)
 		if (!strcmp(sec->name, ".shstrtab") ||
 		    !strcmp(sec->name, ".strtab") ||
 		    !strcmp(sec->name, ".symtab") ||
-		    !strncmp(sec->name, ".rodata.str1.", 13)) {
+		    should_include_str_section(sec->name)) {
 			sec->include = 1;
 			if (sec->secsym)
Quoted 1 line
				sec->secsym->include = 1;

Thanks!

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw
Quoted 8 lines · Konrad Rzeszutek Wilk
Quoted 6 lines · Ross Lagerwall

GCC 6.1+ fixed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=192 which means that .rodata.str* sections are now split by function. We could probably be smarter about including just the sections we need, but for now, include all .rodata.str* sections as is done for previous versions of GCC.

Quoted 8 lines

Here you say .str*

But the code only does this for .str1.*

Did you mean to make it.more generic for say .rodata.*.str[0-9].*

?

The code is what I intended. I tweaked the commit message slightly:

GCC 6.1+ fixed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=192 which means that .rodata.str1.[0-9]+ sections are now split by function. We could probably be smarter about including just the sections we need, but for now, simply include the string sections for all functions as is done for previous versions of GCC.

Signature · 6 lines
Ross Lagerwall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#3 lore raw
In reply to Ross Lagerwall
Quoted 8 lines · Ross Lagerwall

GCC 6.1+ fixed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=192 which means that .rodata.str* sections are now split by function. We could probably be smarter about including just the sections we need, but for now, include all .rodata.str* sections as is done for previous versions of GCC.

This manifests itself as symbol error. E.g.:
(XEN) Unknown symbol: .LC0

There may be a problem with this patch. I built livepatch-build-tools (from the xenbits git repo) with this and the other patch posted yesterday and successfully built and applied xsa191 to xsa193 (cumulatively) to xen-4.8.0-rc6, but the computer freezes if I try to apply xsa194 (cumulatively or on its own). This was the only patch I tried which got the Unknown symbol: .LC3 message ie.
(XEN) livepatch_elf.c:295: livepatch: xsa194: Unknown symbol: .LC3 so this may be related to the crash.

Michael Young

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw
Quoted 10 lines · M A Young
Quoted 8 lines · Ross Lagerwall

GCC 6.1+ fixed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=192 which means that .rodata.str* sections are now split by function. We could probably be smarter about including just the sections we need, but for now, include all .rodata.str* sections as is done for previous versions of GCC.

This manifests itself as symbol error. E.g.:
(XEN) Unknown symbol: .LC0

Quoted 8 lines

There may be a problem with this patch. I built livepatch-build-tools (from the xenbits git repo) with this and the other patch posted yesterday and successfully built and applied xsa191 to xsa193 (cumulatively) to xen-4.8.0-rc6, but the computer freezes if I try to apply xsa194 (cumulatively or on its own). This was the only patch I tried which got the Unknown symbol: .LC3 message ie.
(XEN) livepatch_elf.c:295: livepatch: xsa194: Unknown symbol: .LC3 so this may be related to the crash.

XSA-194 is a toolstack patch. It isn't applicable to livepatch.

There is one copy of the vulnerable code in Xen, but it is only used to construct dom0 and discarded along with all the other __init code.

Such a livepatch should be rejected by Xen...

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#7 lore raw
In reply to M A Young
Quoted 11 lines · M A Young
Quoted 8 lines · Ross Lagerwall

GCC 6.1+ fixed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=192 which means that .rodata.str* sections are now split by function. We could probably be smarter about including just the sections we need, but for now, include all .rodata.str* sections as is done for previous versions of GCC.

This manifests itself as symbol error. E.g.:
(XEN) Unknown symbol: .LC0

Quoted 8 lines

There may be a problem with this patch. I built livepatch-build-tools (from the xenbits git repo) with this and the other patch posted yesterday and successfully built and applied xsa191 to xsa193 (cumulatively) to xen-4.8.0-rc6, but the computer freezes if I try to apply xsa194 (cumulatively or on its own). This was the only patch I tried which got the Unknown symbol: .LC3 message ie.
(XEN) livepatch_elf.c:295: livepatch: xsa194: Unknown symbol: .LC3 so this may be related to the crash.

Anything on the serial console? Can you use the crash functionality? Pls make sure to boot with 'loglvl=all'. Also you may want to build it with 'debug=y' so the livepatch debug errors show up too.

Thanks!

Quoted 2 lines

Michael Young

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#5 lore raw
In reply to Andrew Cooper
Quoted 20 lines · Andrew Cooper
Quoted 10 lines · M A Young
Quoted 8 lines · Ross Lagerwall

GCC 6.1+ fixed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=192 which means that .rodata.str* sections are now split by function. We could probably be smarter about including just the sections we need, but for now, include all .rodata.str* sections as is done for previous versions of GCC.

This manifests itself as symbol error. E.g.:
(XEN) Unknown symbol: .LC0

Quoted 8 lines

There may be a problem with this patch. I built livepatch-build-tools (from the xenbits git repo) with this and the other patch posted yesterday and successfully built and applied xsa191 to xsa193 (cumulatively) to xen-4.8.0-rc6, but the computer freezes if I try to apply xsa194 (cumulatively or on its own). This was the only patch I tried which got the Unknown symbol: .LC3 message ie.
(XEN) livepatch_elf.c:295: livepatch: xsa194: Unknown symbol: .LC3 so this may be related to the crash.

Quoted 7 lines

XSA-194 is a toolstack patch. It isn't applicable to livepatch.

There is one copy of the vulnerable code in Xen, but it is only used to construct dom0 and discarded along with all the other __init code.

Such a livepatch should be rejected by Xen...

elf_init() is not marked __init, so it is included in the live patch.

Signature · 6 lines
Ross Lagerwall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#6 lore raw
Quoted 25 lines · Ross Lagerwall
Quoted 24 lines · Andrew Cooper
Quoted 11 lines · M A Young
Quoted 9 lines · Ross Lagerwall

GCC 6.1+ fixed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=192 which means that .rodata.str* sections are now split by function. We could probably be smarter about including just the sections we need, but for now, include all .rodata.str* sections as is done for previous versions
of GCC.

This manifests itself as symbol error. E.g.:
(XEN) Unknown symbol: .LC0

Quoted 9 lines

There may be a problem with this patch. I built livepatch-build-tools (from the xenbits git repo) with this and the other patch posted yesterday
and successfully built and applied xsa191 to xsa193 (cumulatively) to xen-4.8.0-rc6, but the computer freezes if I try to apply xsa194 (cumulatively or on its own). This was the only patch I tried which got the Unknown symbol: .LC3 message ie.
(XEN) livepatch_elf.c:295: livepatch: xsa194: Unknown symbol: .LC3 so this may be related to the crash.

XSA-194 is a toolstack patch. It isn't applicable to livepatch.

Quoted 7 lines
Quoted 5 lines

There is one copy of the vulnerable code in Xen, but it is only used to construct dom0 and discarded along with all the other __init code.

Such a livepatch should be rejected by Xen...

elf_init() is not marked __init, so it is included in the live patch.

Well that's unfortunate, as it does genuinely live in .init. The entirety of libelf is objcpy'd

libelf.o: libelf-temp.o Makefile

        $(OBJCOPY) $(foreach s,$(SECTIONS),--rename-section

.$(s)=.init.$(s)) $< $@

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel