>Synopsis:      getaddrinfo() is not thread-safe in 6.8
>Category:      system
>Environment:
        System      : OpenBSD 6.8
        Details     : OpenBSD 6.8 (GENERIC.MP) #1: Tue Nov  3 09:06:04 MST 2020
                        
[email protected]:/usr/src/sys/arch/amd64/compile/GENERIC.MP

        Architecture: OpenBSD.amd64
        Machine     : amd64
>Description:
Hi, getaddrinfo() crashes when multiple threads run getaddrinfo()
concurrently. This didn't happen in 6.7.
It looks like asr_ctx which is supposed to be thread-local according to
_asr_use_resolver(), is actually static / shared between threads.
>How-To-Repeat:

Compile this code (gcc a.c -pthread), and run. It will segfault in
several seconds. Happens with both gcc (4.2.1) and egcc (8.4.0).

#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>

#define NUM_THREADS 5

static void do_lookup(const char *host)
{
        int s;
        struct addrinfo hints;
        struct addrinfo *result;

        memset(&hints, 0, sizeof(hints));
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = AI_ADDRCONFIG;
        hints.ai_protocol = IPPROTO_TCP;

        s = getaddrinfo(host, NULL, &hints, &result);
        if (s != 0) {
                fprintf(stderr, "Lookup error for %s: %s\n", host, 
gai_strerror(s));
        } else {
                freeaddrinfo(result);
        }
}

static void *
do_things(void *arg)
{
        (void) arg;
        do_lookup("ipv4.google.com");
        do_lookup("ipv6.google.com");
        do_lookup("google.com");
        do_lookup("heise.de");
        return NULL;
}

int main()
{
        pthread_t threads[NUM_THREADS];
        int i;
        int s;

        for (;;) {
                do_things(NULL);
                for (i = 0; i < NUM_THREADS; i++) {
                        s = pthread_create(&threads[i], NULL, do_things, NULL);
                        if (s != 0)
                                fprintf(stderr, "Error creating thread");
                }
                for (i = 0; i < NUM_THREADS; i++) {
                        pthread_join(threads[i], NULL);
                }
        }
        return 0;
}

The reproducing code written by psychon, CCed, while debugging the crash
of a more complicated software

>Fix:
        


dmesg:
OpenBSD 6.8 (GENERIC.MP) #1: Tue Nov  3 09:06:04 MST 2020

[email protected]:/usr/src/sys/arch/amd64/compile/GENERIC.MP
real mem = 2130550784 (2031MB)
avail mem = 2050998272 (1955MB)
random: good seed from bootblocks
mpath0 at root
scsibus0 at mpath0: 256 targets
mainbus0 at root
bios0 at mainbus0: SMBIOS rev. 2.8 @ 0xf58a0 (10 entries)
bios0: vendor SeaBIOS version "1.12.0-1" date 04/01/2014
bios0: QEMU Standard PC (i440FX + PIIX, 1996)
acpi0 at bios0: ACPI 1.0
acpi0: sleep states S3 S4 S5
acpi0: tables DSDT FACP APIC HPET
acpi0: wakeup devices
acpitimer0 at acpi0: 3579545 Hz, 24 bits
acpimadt0 at acpi0 addr 0xfee00000: PC-AT compat
cpu0 at mainbus0: apid 0 (boot processor)
cpu0: QEMU Virtual CPU version 2.5+, 3793.36 MHz, 06-06-03
cpu0:
FPU,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,SSE3,CX16,x2APIC,HV,NXE,LONG,LAHF
cpu0: 64KB 64b/line 2-way I-cache, 64KB 64b/line 2-way D-cache, 512KB
64b/line 16-way L2 cache
cpu0: ITLB 255 4KB entries direct-mapped, 255 4MB entries direct-mapped
cpu0: DTLB 255 4KB entries direct-mapped, 255 4MB entries direct-mapped
cpu0: smt 0, core 0, package 0
mtrr: Pentium Pro MTRR support, 8 var ranges, 88 fixed ranges
cpu0: apic clock running at 1000MHz
cpu1 at mainbus0: apid 1 (application processor)
cpu1: QEMU Virtual CPU version 2.5+, 3792.99 MHz, 06-06-03
cpu1:
FPU,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,SSE3,CX16,x2APIC,HV,NXE,LONG,LAHF
cpu1: 64KB 64b/line 2-way I-cache, 64KB 64b/line 2-way D-cache, 512KB
64b/line 16-way L2 cache
cpu1: ITLB 255 4KB entries direct-mapped, 255 4MB entries direct-mapped
cpu1: DTLB 255 4KB entries direct-mapped, 255 4MB entries direct-mapped
cpu1: smt 0, core 0, package 1
ioapic0 at mainbus0: apid 0 pa 0xfec00000, version 11, 24 pins
acpihpet0 at acpi0: 100000000 Hz
acpiprt0 at acpi0: bus 0 (PCI0)
"ACPI0006" at acpi0 not configured
acpipci0 at acpi0 PCI0
acpicmos0 at acpi0
"PNP0A06" at acpi0 not configured
"PNP0A06" at acpi0 not configured
"PNP0A06" at acpi0 not configured
"QEMU0002" at acpi0 not configured
"ACPI0010" at acpi0 not configured
acpicpu0 at acpi0: C1(@1 halt!)
acpicpu1 at acpi0: C1(@1 halt!)
pvbus0 at mainbus0: KVM, Hyper-V 6.1
pvclock0 at pvbus0
hyperv0 at pvbus0: not functional
pci0 at mainbus0 bus 0
pchb0 at pci0 dev 0 function 0 "Intel 82441FX" rev 0x02
pcib0 at pci0 dev 1 function 0 "Intel 82371SB ISA" rev 0x00
pciide0 at pci0 dev 1 function 1 "Intel 82371SB IDE" rev 0x00: DMA,
channel 0 wired to compatibility, channel 1 wired to compatibility
pciide0: channel 0 disabled (no drives)
atapiscsi0 at pciide0 channel 1 drive 1
scsibus1 at atapiscsi0: 2 targets
cd0 at scsibus1 targ 0 lun 0: <QEMU, QEMU DVD-ROM, 2.5+> removable
cd0(pciide0:1:1): using PIO mode 4, DMA mode 2
uhci0 at pci0 dev 1 function 2 "Intel 82371SB USB" rev 0x01: apic 0 int 11
piixpm0 at pci0 dev 1 function 3 "Intel 82371AB Power" rev 0x03: apic 0
int 9
iic0 at piixpm0
vga1 at pci0 dev 2 function 0 "Cirrus Logic CL-GD5446" rev 0x00
wsdisplay0 at vga1 mux 1: console (80x25, vt100 emulation)
wsdisplay0: screen 1-5 added (80x25, vt100 emulation)
virtio0 at pci0 dev 3 function 0 "Qumranet Virtio Network" rev 0x00
vio0 at virtio0: address 00:16:bc:79:b7:f6
virtio0: msix per-VQ
virtio1 at pci0 dev 4 function 0 "Qumranet Virtio SCSI" rev 0x00
vioscsi0 at virtio1: qsize 128
scsibus2 at vioscsi0: 255 targets
sd0 at scsibus2 targ 0 lun 0: <BUYVM, SLAB, 2.5+>
serial.BUYVM_SLAB_VOLUME-4047
sd0: 524288MB, 512 bytes/sector, 1073741824 sectors, thin
virtio1: msix shared
virtio2 at pci0 dev 5 function 0 "Qumranet Virtio Storage" rev 0x00
vioblk0 at virtio2
scsibus3 at vioblk0: 1 targets
sd1 at scsibus3 targ 0 lun 0: <VirtIO, Block Device, >
sd1: 40960MB, 512 bytes/sector, 83886080 sectors
virtio2: msix shared
virtio3 at pci0 dev 6 function 0 "Qumranet Virtio Memory Balloon" rev 0x00
viomb0 at virtio3
virtio3: apic 0 int 10
isa0 at pcib0
isadma0 at isa0
fdc0 at isa0 port 0x3f0/6 irq 6 drq 2
pckbc0 at isa0 port 0x60/5 irq 1 irq 12
pckbd0 at pckbc0 (kbd slot)
wskbd0 at pckbd0: console keyboard, using wsdisplay0
pms0 at pckbc0 (aux slot)
wsmouse0 at pms0 mux 0
pcppi0 at isa0 port 0x61
spkr0 at pcppi0
usb0 at uhci0: USB revision 1.0
uhub0 at usb0 configuration 1 interface 0 "Intel UHCI root hub" rev
1.00/1.00 addr 1
uhidev0 at uhub0 port 1 configuration 1 interface 0 "QEMU QEMU USB
Tablet" rev 2.00/0.00 addr 2
uhidev0: iclass 3/0
ums0 at uhidev0: 3 buttons, Z dir
wsmouse1 at ums0 mux 0
vscsi0 at root
scsibus4 at vscsi0: 256 targets
softraid0 at root
scsibus5 at softraid0: 256 targets
root on sd1a (37deb3c1c30cbfbf.a) swap on sd1b dump on sd1b
vio0: DAD detected duplicate IPv6 address 2605:6400:10::: NS in/out=0/1,
NA in=1
vio0: DAD complete for 2605:6400:10:: - duplicate found
vio0: manual intervention required
nd6_na_input: duplicate IP6 address 2605:6400:10::
nd6_na_input: duplicate IP6 address 2605:6400:10::
nd6_na_input: duplicate IP6 address 2605:6400:10::
nd6_na_input: duplicate IP6 address 2605:6400:10::
fd0 at fdc0 drive 1: density unknown

usbdevs:
usbdevs: no USB controllers found

-- 
Best regards,
Alexey "DarthGandalf" Sokolov

Reply via email to