tools/symbols: fix oversized names and token expansion
v2 cover tools/symbols: fix oversized names and token expansion
Hi all,
This series fixes two buffer overflows in Xen's symbol handling. Thanks to Jan for the review of v1.
Patch 1 makes KSYM_NAME_LEN include the terminating NUL, following Linux's convention. The runtime lookup buffers stay at 128 bytes. The token expansion buffer grows to 129 bytes to hold the type, a 127-byte name and the NUL.
Patch 2 reports and skips names longer than 127 bytes in read_symbol(), including the filename prefix added to local SYSV symbols. This avoids generating a table that can overflow the runtime lookup buffers. A long absolute source path caused this overflow during the boot self-tests on an Orange Pi 5 debug build. Human-readable --xensyms maps keep names longer than this limit.
Changes in v2:
- Reorder the patches so the name-length check uses the final KSYM_NAME_LEN convention from the start.
- Patch 1 (v1 patch 2): make KSYM_NAME_LEN include the NUL and update its users. The token buffer is KSYM_NAME_LEN + 1, with the extra byte for the type, as discussed with Jan.
- Patch 2 (v1 patch 1): report and skip oversized names instead of failing the build, and move the check to read_symbol(), as suggested by Jan.
Testing:
- Built the host generator with GCC 15.2.0 and tested it under ASan/UBSan.
- Checked BSD and SYSV input at the 127/128-byte boundary, --xensyms, --sort-by-name and --error-dup.
- Reproduced the token overflow with 64 identical 127-byte names of type T; the fixed generator processes the same input without errors.
Best regards,
Mykola
Mykola Kvach (2):
symbols: make KSYM_NAME_LEN include the trailing NUL tools/symbols: skip names exceeding the runtime limit
xen/arch/x86/platform_hypercall.c | 2 +- xen/common/symbols.c | 6 +++--- xen/common/vsprintf.c | 2 +- xen/include/xen/symbols.h | 3 ++- xen/tools/symbols.c | 11 ++++++++++- 5 files changed, 17 insertions(+), 7 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/2 symbols: make KSYM_NAME_LEN include the trailing NUL
write_src() expands compression tokens into a 128-byte buffer. A token can contain the type byte and a valid 127-byte symbol name, leaving no space for the NUL written by expand_symbol().
Make KSYM_NAME_LEN include the terminating NUL, following Linux commit 9281acea6a36 ("kallsyms: make KSYM_NAME_LEN include space for trailing '\0'"). Set it to 128 and adjust the runtime users to keep their buffers at 128 bytes.
write_src() keeps an extra byte for the type, so its buffer grows to the required 129 bytes without changing the buffer declaration.
livepatch.c passes KSYM_NAME_LEN to strlcpy(), which now preserves up to 127 characters instead of 126.
With --all-symbols and 64 identical 127-byte names of type T, ASan reports a stack-buffer-overflow. The fixed tool passes this case under ASan/UBSan.
---
Changes in v2:
- Previously patch 2; move it before the name-length check so that check uses the final KSYM_NAME_LEN convention from the start.
- Make KSYM_NAME_LEN include the trailing NUL, following Linux (Jan).
- Set KSYM_NAME_LEN to 128 and update the runtime users, preserving their 128-byte buffers.
- Use KSYM_NAME_LEN + 1 for the token buffer, keeping it at 129 bytes.
- Drop the claim that the overflow only occurs with very small symbol tables.
xen/arch/x86/platform_hypercall.c | 2 +- xen/common/symbols.c | 6 +++--- xen/common/vsprintf.c | 2 +- xen/include/xen/symbols.h | 3 ++- xen/tools/symbols.c | 3 ++- 5 files changed, 9 insertions(+), 7 deletions(-)
xen/arch/x86/platform_hypercall.c +1 -1
@@ -892,7 +892,7 @@ ret_t do_platform_op(case XENPF_get_symbol: { -static char name[KSYM_NAME_LEN +1 ];/* protected by xenpf_lock */ +static char name[KSYM_NAME_LEN ];/* protected by xenpf_lock */ XEN_GUEST_HANDLE (char ) nameh;uint32_t namelen, copylen;unsigned long addr;
xen/common/symbols.c +3 -3
@@ -113,7 +113,7 @@ const char *symbols_lookup(unsigned long addr,unsigned long symbol_end =0 ;const struct virtual_region *region; - namebuf[KSYM_NAME_LEN ] =0 ; + namebuf[KSYM_NAME_LEN -1 ] =0 ; namebuf[0 ] =0 ; region =find_text_region (addr);
@@ -247,7 +247,7 @@ int xensyms_read(uint32_t *symnum, char *type,unsigned long symbols_lookup_by_name (const char *symname) { -char name[KSYM_NAME_LEN +1 ]; +char name[KSYM_NAME_LEN ];#ifdef CONFIG_FAST_SYMBOL_LOOKUP unsigned long low, high;#else
@@ -299,7 +299,7 @@ unsigned long symbols_lookup_by_name(const char *symname)static void __init test_lookup (unsigned long addr,const char *expected) { -char buf[KSYM_NAME_LEN +1 ]; +char buf[KSYM_NAME_LEN ];const char *name, *symname;unsigned long size, offs;
xen/common/vsprintf.c +1 -1
@@ -397,7 +397,7 @@ static char *pointer(char *str, const char *end, const char **fmt_ptr,case 'S' :/* Symbol name unconditionally with offset and size */ {unsigned long sym_size, sym_offset; -char namebuf[KSYM_NAME_LEN +1 ]; +char namebuf[KSYM_NAME_LEN ];/* Advance parents fmt string, as we have consumed 's' or 'S' */ ++*fmt_ptr;
xen/include/xen/symbols.h +2 -1
@@ -3,7 +3,8 @@#include <xen/types.h> -#define KSYM_NAME_LEN 127 +/* Longest symbol name, including the terminating NUL. */ +#define KSYM_NAME_LEN 128/* * Typedef for the callback functions that symbols_lookup
xen/tools/symbols.c +2 -1
@@ -33,7 +33,7 @@#include <stdbool.h> #include <ctype.h> -#define KSYM_NAME_LEN 127 +#define KSYM_NAME_LEN 128struct sym_entry {
@@ -338,6 +338,7 @@ static void write_src(void)unsigned int i, k, off, ends;unsigned int best_idx[256 ];unsigned int *markers; +/* Type byte, then the longest name with its terminating NUL. */ char buf[KSYM_NAME_LEN +1 ];if (map_only) {
Signature · 1 line
2.53.0
PATCH v2 2/2 tools/symbols: skip names exceeding the runtime limit
In reply to Mykola KvachThe runtime lookup buffers hold KSYM_NAME_LEN bytes (128), but the generator accepts longer names. SYSV input prefixes local symbols with their filename, so an absolute assembly source path can make a name too long even when the symbol itself is short.
On an Orange Pi 5 debug build this crashed Xen at boot: during the test_symbols() constructor, symbols_lookup_by_name() expanded the 145-byte name ending in mmu/head.S#identity_mapping_removed into a 128-byte buffer, overwriting the saved frame pointer and return address.
Report and skip names of KSYM_NAME_LEN bytes or more, as Linux does. Check the complete name in read_symbol(), after adding the filename prefix. Keep --xensyms maps complete: they are not loaded into Xen.
---
Changes in v2:
- Previously patch 1; move it after the KSYM_NAME_LEN conversion.
- Report and skip oversized names instead of failing the build (Jan).
- Move the check from build_initial_tok_table() to read_symbol(), after adding the filename prefix (Jan).
- Use the NUL-inclusive KSYM_NAME_LEN in the check and diagnostic.
xen/tools/symbols.c | 8 ++++++++ 1 file changed, 8 insertions(+)
xen/tools/symbols.c +8 -0
@@ -178,6 +178,14 @@ static int read_symbol(FILE *in, struct sym_entry *s) *sym++ ='#' ; }strcpy (sym, str); +/* Only the table loaded into Xen is limited, not --xensyms maps. */ +if (!map_only && s->len -1 >=KSYM_NAME_LEN ) { +fprintf (stderr, +"Ignoring symbol '%s': too long (%u bytes, maximum %u)\n" , +SYMBOL_NAME (s), s->len -1 ,KSYM_NAME_LEN -1 ); +free (s->sym); + goto skip_tail; + }if (sort_by_name || map_only) s->orig_symbol =strdup (SYMBOL_NAME (s)); s->type = stype;/* As s->sym[0] may end up mangled. */
Signature · 1 line
2.53.0