From d764e2293786a3c3228952ab13a391996ee099c1 Mon Sep 17 00:00:00 2001 From: David Disseldorp Date: Wed, 13 Nov 2024 07:56:40 +0100 Subject: [PATCH 1/4] lkl: fix vfio_pci warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Calling snprintf() with overlapping source and destination buffers is undefined behavior, as per C/POSIX specs. Avoid this by adding a second buffer, and also eliminate a compiler warning from similar readlink() source-is-dest logic: lib/vfio_pci.c: In function ‘vfio_pci_add’: lib/vfio_pci.c:80:28: warning: passing argument 2 to ‘restrict’-qualified parameter aliases with argument 1 [-Wrestrict] 80 | i = readlink(path, path, sizeof(path)); | ~~~~ ^~~~ Signed-off-by: David Disseldorp --- tools/lkl/lib/vfio_pci.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/lkl/lib/vfio_pci.c b/tools/lkl/lib/vfio_pci.c index 172e0175e6826d..7f6d4af0399fc3 100644 --- a/tools/lkl/lib/vfio_pci.c +++ b/tools/lkl/lib/vfio_pci.c @@ -43,7 +43,7 @@ static struct lkl_pci_dev *vfio_pci_add(const char *name, void *kernel_ram, unsigned long ram_size) { struct lkl_pci_dev *dev; - char path[128]; + char path[128], link[128]; int segn, busn, devn, funcn; int i; int container_fd = 0, group_fd = 0; @@ -77,12 +77,12 @@ static struct lkl_pci_dev *vfio_pci_add(const char *name, void *kernel_ram, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/iommu_group", segn, busn, devn, funcn); - i = readlink(path, path, sizeof(path)); + i = readlink(path, link, sizeof(link)); if (i < 0) goto error; - path[i] = '\0'; - snprintf(path, sizeof(path), "/dev/vfio%s", strrchr(path, '/')); + link[i] = '\0'; + snprintf(path, sizeof(path), "/dev/vfio%s", strrchr(link, '/')); group_fd = open(path, O_RDWR); if (group_fd < 0) From 6e382bda575783e1e7235f77620c94dd6dbabe57 Mon Sep 17 00:00:00 2001 From: David Disseldorp Date: Thu, 14 Nov 2024 01:25:24 +0100 Subject: [PATCH 2/4] lkl: minor vfio_pci error handling improvements readlink() can fill up to sizeof(link), in which case the nulterm would overflow. Fix this and avoid assuming that the link carries a '/'. Signed-off-by: David Disseldorp --- tools/lkl/lib/vfio_pci.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tools/lkl/lib/vfio_pci.c b/tools/lkl/lib/vfio_pci.c index 7f6d4af0399fc3..1d76793420ae18 100644 --- a/tools/lkl/lib/vfio_pci.c +++ b/tools/lkl/lib/vfio_pci.c @@ -43,7 +43,7 @@ static struct lkl_pci_dev *vfio_pci_add(const char *name, void *kernel_ram, unsigned long ram_size) { struct lkl_pci_dev *dev; - char path[128], link[128]; + char path[128], link[128], *l; int segn, busn, devn, funcn; int i; int container_fd = 0, group_fd = 0; @@ -77,12 +77,16 @@ static struct lkl_pci_dev *vfio_pci_add(const char *name, void *kernel_ram, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/iommu_group", segn, busn, devn, funcn); - i = readlink(path, link, sizeof(link)); + i = readlink(path, link, sizeof(link) - 1); if (i < 0) goto error; link[i] = '\0'; - snprintf(path, sizeof(path), "/dev/vfio%s", strrchr(link, '/')); + l = strrchr(link, '/'); + if (l == NULL) + goto error; + + snprintf(path, sizeof(path), "/dev/vfio%s", l); group_fd = open(path, O_RDWR); if (group_fd < 0) From 19a12600d842d05a5fe6e9d240930989ad762d86 Mon Sep 17 00:00:00 2001 From: David Disseldorp Date: Wed, 13 Nov 2024 04:22:40 +0100 Subject: [PATCH 3/4] lkl: silence compiler warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit arch/lkl/kernel/misc.c: In function ‘wrong_size_cmpxchg’: arch/lkl/kernel/misc.c:18:16: warning: function declared ‘noreturn’ has a ‘return’ statement 18 | return 0; Drop the return after panic(). Signed-off-by: David Disseldorp --- arch/lkl/kernel/misc.c | 1 - 1 file changed, 1 deletion(-) diff --git a/arch/lkl/kernel/misc.c b/arch/lkl/kernel/misc.c index 5071b2990c8adb..ecbe447a3a3cb5 100644 --- a/arch/lkl/kernel/misc.c +++ b/arch/lkl/kernel/misc.c @@ -15,7 +15,6 @@ void __generic_xchg_called_with_bad_pointer(void) unsigned long wrong_size_cmpxchg(volatile void *ptr) { panic("%s shouldn't be executed\n", __func__); - return 0; } #ifdef CONFIG_PROC_FS From b1339686dbc0866caf49c0fd57b70fbceb543680 Mon Sep 17 00:00:00 2001 From: David Disseldorp Date: Thu, 14 Nov 2024 04:04:36 +0100 Subject: [PATCH 4/4] lkl: silence bootmem compiler warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit gcc-14 complains about the type mismatch: arch/lkl/mm/bootmem.c: In function ‘bootmem_init’: arch/lkl/mm/bootmem.c:39:35: error: passing argument 1 of ‘virt_to_pfn’ makes pointer from integer without a cast [-Wint-conversion] 39 | max_low_pfn = virt_to_pfn(memory_end); | ^~~~~~~~~~ | | | long unsigned int ... ./include/asm-generic/page.h:77:53: note: expected ‘const void *’ but argument is of type ‘long unsigned int’ 77 | static inline unsigned long virt_to_pfn(const void *kaddr) | ~~~~~~~~~~~~^~~~~ Add a cast to silence the warning. Drop some unnecessary casts for _memory_start and empty_zero_page, which can both be void *. Signed-off-by: David Disseldorp --- arch/lkl/mm/bootmem.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/arch/lkl/mm/bootmem.c b/arch/lkl/mm/bootmem.c index 94854355c8b0bf..bef66ec7d65b35 100644 --- a/arch/lkl/mm/bootmem.c +++ b/arch/lkl/mm/bootmem.c @@ -4,7 +4,8 @@ #include unsigned long memory_start, memory_end; -static unsigned long _memory_start, mem_size; +static void *_memory_start; +static unsigned long mem_size; void *empty_zero_page; @@ -16,12 +17,12 @@ void __init bootmem_init(unsigned long mem_sz) if (lkl_ops->page_alloc) { mem_size = PAGE_ALIGN(mem_size); - _memory_start = (unsigned long)lkl_ops->page_alloc(mem_size); + _memory_start = lkl_ops->page_alloc(mem_size); } else { - _memory_start = (unsigned long)lkl_ops->mem_alloc(mem_size); + _memory_start = lkl_ops->mem_alloc(mem_size); } - memory_start = _memory_start; + memory_start = (unsigned long)_memory_start; BUG_ON(!memory_start); memory_end = memory_start + mem_size; @@ -36,12 +37,12 @@ void __init bootmem_init(unsigned long mem_sz) * Give all the memory to the bootmap allocator, tell it to put the * boot mem_map at the start of memory. */ - max_low_pfn = virt_to_pfn(memory_end); - min_low_pfn = virt_to_pfn(memory_start); + max_low_pfn = virt_to_pfn((void *)memory_end); + min_low_pfn = virt_to_pfn((void *)memory_start); memblock_add(memory_start, mem_size); empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE); - memset((void *)empty_zero_page, 0, PAGE_SIZE); + memset(empty_zero_page, 0, PAGE_SIZE); zones_max_pfn[ZONE_NORMAL] = max_low_pfn; free_area_init(zones_max_pfn); @@ -66,7 +67,7 @@ void free_initmem(void) void free_mem(void) { if (lkl_ops->page_free) - lkl_ops->page_free((void *)_memory_start, mem_size); + lkl_ops->page_free(_memory_start, mem_size); else - lkl_ops->mem_free((void *)_memory_start); + lkl_ops->mem_free(_memory_start); }