libpci: sysfs: Fix segmentation fault by including libgen.h
Subsystems (2)
PATCH RESEND 1/1 libpci: sysfs: Fix segmentation fault by including libgen.h
On a musl-based system (Alpine-linux) the basename(3) function is not defined by including string.h with _GNU_SOURCE defined. However basename(3) could be defined by including libgen.h.
On musl this is a problem than can lead to a segmentation fault, as I have experienced. This issue is caused by basename(3) function being implicitly declared and thus having, implicitly, a return type of int. Which in my case caused an erroneous sign extension of a pointer leading to a segmentation fault.
Adding an include for libgen.h sound to me like a proper solution. Also by doing so the _GNU_SOURCE defined is no longer needed.
You can find below details on the issue, on alpine linux (x86_64) running:
$ lspci -s 00:01.0 -v
00:01.0 Host bridge: Advanced Micro Devices, Inc. [AMD] Family 17h-19h PCIe Dummy Host Bridge (rev 01)
Segmentation faultAfter debugging, the fault is indirectly caused by this compilation warning:
sysfs.c: In function 'sysfs_fill_info':
sysfs.c:457:53: warning: implicit declaration of function 'basename' [-Wimplicit-function-declaration]
457 | pci_set_property(d, PCI_FILL_IOMMU_GROUP, basename(group_link));
| ^~~~~~~~
sysfs.c:457:53: warning: passing argument 3 of 'pci_set_property' makes pointer from integer without a cast [-Wint-conversion]
457 | pci_set_property(d, PCI_FILL_IOMMU_GROUP, basename(group_link));
| ^~~~~~~~~~~~~~~~~~~~ int
Here is the relevant assembly, dump from gdb:
0x7ffff7f4f072 call *0x5db8(%rip) # call to basename
0x7ffff7f4f078 mov %rbx,%rdi
0x7ffff7f4f07b mov $0x4000,%esi # PCI_FILL_IOMM_GROUP
0x7ffff7f4f080 movslq %eax,%rdx # return value of basename is signed extended from 32bit (eax) to 64bit (rdx)
0x7ffff7f4f083 call 0x7ffff7f4831b # call to pci_set_propertyAnd how the address becomes invalid:
(gdb) x/s 0x7ffff7ffed20 # argument of basename
0x7ffff7ffed20: "/sys/kernel/iommu_groups/0"
(gdb) x/s 0x7ffff7ffed39 # result from basename
0x7ffff7ffed39: "0"
(gdb) x/s 0xfffffffff7ffed39 # after sign extension
0xfffffffff7ffed39: <error: Cannot access memory at address 0xfffffffff7ffed39>--- lib/sysfs.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
lib/sysfs.c +1 -2
@@ -9,11 +9,10 @@* SPDX-License-Identifier: GPL-2.0-or-later */ -#define _GNU_SOURCE -#include <stdio.h> #include <stdlib.h> #include <string.h> +#include <libgen.h> #include <stdarg.h> #include <unistd.h> #include <errno.h>
Signature · 1 line
2.44.0
Hello!
Quoted 4 lines
On a musl-based system (Alpine-linux) the basename(3) function is not defined by including
string.hwith_GNU_SOURCEdefined. However basename(3) could be defined by includinglibgen.h.
Quoted 8 lines
On musl this is a problem than can lead to a segmentation fault, as I have experienced. This issue is caused by basename(3) function being implicitly declared and thus having, implicitly, a return type of int. Which in my case caused an erroneous sign extension of a pointer leading to a segmentation fault.
Adding an include for
libgen.hsound to me like a proper solution. Also by doing so the_GNU_SOURCEdefined is no longer needed.
It should be fixed by commit 89cb2ae87236604b0e8ededd0fd7d9425c2d8cb6.
Could you please check if it works for you?
Have a nice fortnight
Signature · 3 lines
Martin `MJ' Mareš <[email protected]> http://mj.ucw.cz/ United Computer Wizards, Prague, Czech Republic, Europe, Earth, Universe Sic transit gloria Monday!
Quoted 12 lines · Martin Mareš
Hello!
Quoted 10 lines
On a musl-based system (Alpine-linux) the basename(3) function is not defined by including
string.hwith_GNU_SOURCEdefined. However basename(3) could be defined by includinglibgen.h.On musl this is a problem than can lead to a segmentation fault, as I have experienced. This issue is caused by basename(3) function being implicitly declared and thus having, implicitly, a return type of int. Which in my case caused an erroneous sign extension of a pointer leading to a segmentation fault.
Quoted 6 lines
Quoted 2 lines
Adding an include for
libgen.hsound to me like a proper solution. Also by doing so the_GNU_SOURCEdefined is no longer needed.It should be fixed by commit
89cb2ae87236604b0e8ededd0fd7d9425c2d8cb6.Could you please check if it works for you?
Yes, it does work.
Quoted 12 lines · Martin Mareš
Hello!
Quoted 10 lines
On a musl-based system (Alpine-linux) the basename(3) function is not defined by including
string.hwith_GNU_SOURCEdefined. However basename(3) could be defined by includinglibgen.h.On musl this is a problem than can lead to a segmentation fault, as I have experienced. This issue is caused by basename(3) function being implicitly declared and thus having, implicitly, a return type of int. Which in my case caused an erroneous sign extension of a pointer leading to a segmentation fault.
Quoted 6 lines
Quoted 2 lines
Adding an include for
libgen.hsound to me like a proper solution. Also by doing so the_GNU_SOURCEdefined is no longer needed.It should be fixed by commit
89cb2ae87236604b0e8ededd0fd7d9425c2d8cb6.Could you please check if it works for you?
nitpick / for the record:
it previously compiled with musl, gcc (13) only generated a warning, but it should nolonger compile with gcc 14, as the warning is now an error.