openwrt / odhcpd

This repository is a mirror of https://git.openwrt.org/?p=project/odhcpd.git. Pull requests will be accepted which will be merged in odhcpd.git
GNU General Public License v2.0
163 stars 99 forks source link

When odhcpd receives a DHCPv6 client request it calls external /usr/bin/basename binary #214

Open Konstantin-Glukhov opened 9 months ago

Konstantin-Glukhov commented 9 months ago

When odhcpd receives a DHCPv6 client request it calls basename() function. Somehow it invokes the external /usr/bin/basename with incorrect arguments. Is it a linkage problem with libc.so on arm hardware?

# odhcpd
odhcpd[536]: Sending RS to wan6
odhcpd[536]: Got a RA on wan6
odhcpd[536]: Forward a RA on lan
odhcpd[536]: DHCPV6 SOLICIT IA_NA from XXX on lan: no addresses available
basename: missing operand
Try 'basename --help' for more information.
odhcpd[536]: DHCPV6 SOLICIT IA_NA from XXX on lan: no addresses available
odhcpd[536]: Sending RS to wan6
odhcpd[536]: Got a RA on wan6
odhcpd[536]: Forward a RA on lan
^C
# which basename
/usr/bin/basename
# ls -l /usr/bin/basename
lrwxrwxrwx    1 root     root          31 Feb 14 06:23 /usr/bin/basename -> /usr/libexec/basename-coreutils*

I wrote a simple test function and it works as expected:

cat t.c
#include <stdio.h>
#include <libgen.h>

int main() {
    const char *path = "/path/to/some/file.txt";

    char *base = basename((char *)path);

    printf("Base name: %s\n", base);

    return 0;
}
arm-openwrt-linux-gcc t.c -o t
file t
t: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-armhf.so.1, with debug_info, not stripped
scp t router:/tmp

Run on router:

/tmp/t
Base name: file.txt

I also checked all of the dynamic libraries loaded by odhcpd, and the only one that has basename() in it is libc.so:

ldd /usr/sbin/odhcpd
        /lib/ld-musl-armhf.so.1 (0xb6f56000)
        libubox.so.20230523 => /lib/libubox.so.20230523 (0xb6f37000)
        libuci.so => /lib/libuci.so (0xb6f2d000)
        libnl-tiny.so.1 => /usr/lib/libnl-tiny.so.1 (0xb6f25000)
        libubus.so.20230605 => /lib/libubus.so.20230605 (0xb6f1f000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb6f12000)
        libc.so => /lib/ld-musl-armhf.so.1 (0xb6f56000)

What causes odhcpd to invoke the external program?

manageryzy commented 3 weeks ago

same issue for me, I'm musl - 1.2.4-4

manageryzy commented 3 weeks ago

I found out the root casuse: https://github.com/openwrt/odhcpd/blob/d8118f6e76e5519881f9a37137c3a06b3cb60fd2/src/dhcpv6-ia.c#L583 called /usr/sbin/odhcpd-update

which refered /lib/functions/procd.sh