DragonFlyBSD/src 12b64b8sys/netinet if_ether.c

if_ether: Reject invalid ARP packets to avoid kernel memory leaking

The ARP ingress parser arpintr() validates ar_hrd and ar_pro but never
validates ar_hln (hardware address length) or ar_pln (protocol address
length). The ARP reply builder in_arpreply() then uses these
attacker-controlled byte values as memcpy lengths when copying from
kernel-internal buffers that hold only 6 bytes (IF_LLADDR) or 4 bytes
(&taddr on stack). The over-read bytes fill the ARP reply mbuf, which is
transmitted back to the attacker. A single crafted ARP request from any
host on the same L2 segment leaks up to ~249 bytes of kernel heap and
~251 bytes of kernel stack.

Fix the bug by rejecting ARP packets with invalid ar_hln or ar_pln.

This patch is derived from FreeBSD:
- https://github.com/freebsd/freebsd-src/commit/09d3f8953e47a33d298a13b77eed55f54ae5247b
- https://github.com/freebsd/freebsd-src/commit/414676ba31117f4ddda936d0035398eb7dfdfa34
- https://github.com/freebsd/freebsd-src/commit/deb6bda6e373d7d01f43d5cc2d724dcecbae2039


    [3 lines not shown]
DeltaFile
+22-0sys/netinet/if_ether.c
+22-01 files

DragonFlyBSD/src 0b709f6sys/vm device_pager.c

vm: fix cdev pager object lifecycle and allocation race

cdev_pager_allocate() previously called cdev_pg_ctor before looking up
the object.  Repeated allocations therefore called a non-idempotent
constructor multiple times for one allocated vm_object, while the
destructor ran only once.

In addition, cdev_pager_allocate() could race with other threads
allocating an object.

This commit fixes both problems.  Reserve a newly allocated object in
the pager list with a NULL ops pointer while its constructor runs
outside dev_pager_mtx.  Concurrent lookup and allocation callers wait
for construction to finish.  Publish the ops pointer and wake them on
success; remove and mark the object dead before waking them on failure.
Use ops rather than dev as the construction sentinel because DragonFly
has valid NULL-handle pager users.

Derived-from: FreeBSD (commit e93404065177d6c909cd64bf7d74fe0d8df35edf)
GitHub-PR: https://github.com/DragonFlyBSD/DragonFlyBSD/pull/49
DeltaFile
+92-34sys/vm/device_pager.c
+92-341 files

DragonFlyBSD/src 67824a9sys/vm device_pager.c

vm: Clean up cdev_pager_allocate() a bit

* Introduce 'pindex' variable to clean up the code.
* Add KASSERT() to ensure the object type matches (obtained from FreeBSD)
DeltaFile
+7-4sys/vm/device_pager.c
+7-41 files

DragonFlyBSD/src 2c4f9absys/netinet tcp_input.c

tcp: Accept a RST whose sequence number is exactly RCV.NXT

An incoming RST is validated against a window anchored on last_ack_sent:
[last_ack_sent, last_ack_sent + rcv_wnd].  When the receiver has
delayed-ACKed data (rcv_nxt > last_ack_sent) and its receive window has
shrunk below that gap (rcv_nxt - last_ack_sent > rcv_wnd), a RST at
rcv_nxt, sits beyond the right edge last_ack_sent + rcv_wnd and is
silently dropped.  The caused the connection to stay ESTABLISHED
(half-open), leaving the application to time out and close.

Accept a RST whose sequence number is exactly rcv_nxt, matching the
long-deployed OpenBSD [1] and NetBSD [2] behaviour.

This does not weaken existing RST acceptance: DragonFly already accepts
any in-window RST (RFC 793, no challenge ACK), so an off-path attacker
gains nothing -- the change only stops legitimate RSTs at rcv_nxt from
being dropped.  The new behavior also conforms to RFC 793 and RFC 5961
(section 3.2).


    [6 lines not shown]
DeltaFile
+3-2sys/netinet/tcp_input.c
+3-21 files

DragonFlyBSD/src 4bb22ffsys/netinet if_ether.c

if_ether: Reject invalid ARP packets to avoid kernel memory leaking

The ARP ingress parser arpintr() validates ar_hrd and ar_pro but never
validates ar_hln (hardware address length) or ar_pln (protocol address
length). The ARP reply builder in_arpreply() then uses these
attacker-controlled byte values as memcpy lengths when copying from
kernel-internal buffers that hold only 6 bytes (IF_LLADDR) or 4 bytes
(&taddr on stack). The over-read bytes fill the ARP reply mbuf, which is
transmitted back to the attacker. A single crafted ARP request from any
host on the same L2 segment leaks up to ~249 bytes of kernel heap and
~251 bytes of kernel stack.

Fix the bug by rejecting ARP packets with invalid ar_hln or ar_pln.

This patch is derived from FreeBSD:
- https://github.com/freebsd/freebsd-src/commit/09d3f8953e47a33d298a13b77eed55f54ae5247b
- https://github.com/freebsd/freebsd-src/commit/414676ba31117f4ddda936d0035398eb7dfdfa34
- https://github.com/freebsd/freebsd-src/commit/deb6bda6e373d7d01f43d5cc2d724dcecbae2039


    [3 lines not shown]
DeltaFile
+22-0sys/netinet/if_ether.c
+22-01 files

DragonFlyBSD/src 7119b8asys/vm device_pager.c

vm: fix cdev pager object lifecycle and allocation race

cdev_pager_allocate() previously called cdev_pg_ctor before looking up
the object.  Repeated allocations therefore called a non-idempotent
constructor multiple times for one allocated vm_object, while the
destructor ran only once.

In addition, cdev_pager_allocate() could race with other threads
allocating an object.

This commit fixes both problems.  Reserve a newly allocated object in
the pager list with a NULL ops pointer while its constructor runs
outside dev_pager_mtx.  Concurrent lookup and allocation callers wait
for construction to finish.  Publish the ops pointer and wake them on
success; remove and mark the object dead before waking them on failure.
Use ops rather than dev as the construction sentinel because DragonFly
has valid NULL-handle pager users.

Derived-from: FreeBSD (commit e93404065177d6c909cd64bf7d74fe0d8df35edf)
GitHub-PR: https://github.com/DragonFlyBSD/DragonFlyBSD/pull/49
DeltaFile
+92-34sys/vm/device_pager.c
+92-341 files

DragonFlyBSD/src fb61005tools/regression/sockets/rst_rcvnxt_window_drop Makefile rst_rcvnxt_window_drop.c

regression: Add TCP RST receive window test

Note: root is required it it uses BPF to observe the client's final data
segment and a raw IPv4 socket to inject the RST at exactly RCV.NXT.

Before the fix in the last commit:
```
$ ./rst_rcvnxt_window_drop
net.inet.tcp.rfc1323: 1 -> 0
net.inet.tcp.delayed_ack: 1 -> 1
net.inet.tcp.recvbuf_auto: 1 -> 0
FAIL: connection still ESTABLISHED (RST at rcv_nxt was dropped), expected ECONNRESET
net.inet.tcp.rfc1323: 0 -> 1
net.inet.tcp.delayed_ack: 1 -> 1
net.inet.tcp.recvbuf_auto: 0 -> 1
```

After the fix:
```

    [12 lines not shown]
DeltaFile
+491-0tools/regression/sockets/rst_rcvnxt_window_drop/rst_rcvnxt_window_drop.c
+5-0tools/regression/sockets/rst_rcvnxt_window_drop/Makefile
+496-02 files

DragonFlyBSD/src f4823e0sys/netinet tcp_input.c

tcp: Accept a RST whose sequence number is exactly RCV.NXT

An incoming RST is validated against a window anchored on last_ack_sent:
[last_ack_sent, last_ack_sent + rcv_wnd].  When the receiver has
delayed-ACKed data (rcv_nxt > last_ack_sent) and its receive window has
shrunk below that gap (rcv_nxt - last_ack_sent > rcv_wnd), a RST at
rcv_nxt, sits beyond the right edge last_ack_sent + rcv_wnd and is
silently dropped.  The caused the connection to stay ESTABLISHED
(half-open), leaving the application to time out and close.

Accept a RST whose sequence number is exactly rcv_nxt, matching the
long-deployed OpenBSD [1] and NetBSD [2] behaviour.

This does not weaken existing RST acceptance: DragonFly already accepts
any in-window RST (RFC 793, no challenge ACK), so an off-path attacker
gains nothing -- the change only stops legitimate RSTs at rcv_nxt from
being dropped.  The new behavior also conforms to RFC 793 and RFC 5961
(section 3.2).


    [6 lines not shown]
DeltaFile
+3-2sys/netinet/tcp_input.c
+3-21 files

DragonFlyBSD/src b4510a6lib/libc/stdlib nmalloc.c

libc - Remove optimization that breaks malloc_usable_size() use cases

* The malloc implementation had an optimization meant to aid realloc()s
  whereby unused portions of large memory blocks could be munmap()d.

  However, lots of code uses the space returned by malloc_usable_size()
  without calling realloc() to notify libc that additional space is being
  used.  Leading to segmentation faults.

* Original proposed solution to make malloc_usable_size() aware of a
  prior munmap optimization does not completely fix the problem as excess
  space may be unmapped after such calls as well as before.

* Removing the optimization and letting the pager deal with any actually-dead
  excess space is the only option.  So that is what we do.

Reported-by: Several people
DeltaFile
+38-2lib/libc/stdlib/nmalloc.c
+38-21 files

DragonFlyBSD/src 6856d6bshare/mk bsd.crunchgen.mk

bsd.crunchgen.mk: Minor cleanups
DeltaFile
+13-15share/mk/bsd.crunchgen.mk
+13-151 files

DragonFlyBSD/src 52ff205initrd/rescue Makefile, share/mk bsd.crunchgen.mk

bsd.crunchgen.mk: Add CRUNCH_HOSTPROGS_${P} and fix awk in rescue

The usr.bin/awk consists of two subdirs: (1) 'awk' the actual program;
(2) 'maketab' a host program to help generate additional source.
Previously, the initrd/rescue/Makefile pointed the awk source to
'usr.bin/awk', and that caused the following warning:

```
crunchgen: rescue.conf: awk: warning: could not find any .o files
```

And pointing the source to 'usr.bin/awk/awk' couldn't fix it because the
required 'maketab' program would be unavailable.

To fix the problem as well as to extend the crunchgen framework, add the
CRUNCH_HOSTPROGS_${P} variable to specify the host programs required by
the build.  Implement the rules and specify the dependencies to build
the host programs.


    [2 lines not shown]
DeltaFile
+20-0share/mk/bsd.crunchgen.mk
+3-0initrd/rescue/Makefile
+23-02 files

DragonFlyBSD/src 250a8b4. Makefile_upgrade.inc, initrd Makefile

initrd: Merge rescue.libcrypto into rescue

After the last commit fixed the 'crc32' symbol conflict between
libhammer and libz, the merge of rescue.libcrypto and rescue only had
one conflict symbol: tilde_expand() from libprivate_ssh and
libprivate_edit.  Work around this symbol conflict by setting
libprivate_ssh an internal library for ssh/scp.

This merge reduces the rescue binaries size by ~1.1MB, from 12.1MB
(rescue 5.3MB + rescue.libcrypto 6.8MB) to 11MB.
DeltaFile
+106-53initrd/rescue/Makefile
+0-80initrd/rescue.libcrypto/Makefile
+1-1initrd/Makefile
+1-0Makefile_upgrade.inc
+108-1344 files

DragonFlyBSD/src 8cb3695lib/libhammer Makefile

libhammer(3): Exclude crc32.c and icrc32.c from sys/libkern

The user hammer(8) already includes these sources from sys/libkern, so
don't need to provide them in this library. More importantly, this
avoids the 'crc32' symbol conflict with libz.
DeltaFile
+4-7lib/libhammer/Makefile
+4-71 files

DragonFlyBSD/src addc09einitrd/rescue Makefile, initrd/rescue.libcrypto Makefile

crunchgen(1): Improve the handling of internal libraries

Previously, the `libs_int` and `special lib_int` commands specified the
internal libraries by their full paths, following the way of handling of
the shared libraries by `libs_so` and `special lib_so`. However, this
was actually a mistake because the internal libraries require building.
So it was lucky/hacky that rescue/rescue.libcrypto specified the
internal libraries with a combination of source directory and the
library name, e.g.,

```
CRUNCH_INTLIB_grep= ${CRUNCH_PATH_grep}/grep/libgreputils/libgreputils.a
CRUNCH_INTLIB_telnet= ${.CURDIR}/../../lib/libtelnet/libtelnet.a
```

To actually properly support internal libraries, we must explicitly the
source directories, as well as support build options.  To this end,
change the `libs_int` and `special lib_int` commands to only specify the
libraries names, and then extend the `special srcdir` to explicitly the

    [10 lines not shown]
DeltaFile
+153-51usr.bin/crunch/crunchgen/crunchgen.c
+69-18usr.bin/crunch/crunchgen/crunchgen.1
+40-28share/mk/bsd.crunchgen.mk
+2-1initrd/rescue/Makefile
+2-1initrd/rescue.libcrypto/Makefile
+266-995 files

DragonFlyBSD/src cc3bf8ashare/mk bsd.crunchgen.mk

bsd.crunchgen.mk: Minor cleanups
DeltaFile
+13-15share/mk/bsd.crunchgen.mk
+13-151 files

DragonFlyBSD/src 6e9501binitrd/rescue Makefile, share/mk bsd.crunchgen.mk

bsd.crunchgen.mk: Add CRUNCH_HOSTPROGS_${P} and fix awk in rescue

The usr.bin/awk consists of two subdirs: (1) 'awk' the actual program;
(2) 'maketab' a host program to help generate additional source.
Previously, the initrd/rescue/Makefile pointed the awk source to
'usr.bin/awk', and that caused the following warning:

```
crunchgen: rescue.conf: awk: warning: could not find any .o files
```

And pointing the source to 'usr.bin/awk/awk' couldn't fix it because the
required 'maketab' program would be unavailable.

To fix the problem as well as to extend the crunchgen framework, add the
CRUNCH_HOSTPROGS_${P} variable to specify the host programs required by
the build.  Implement the rules and specify the dependencies to build
the host programs.


    [2 lines not shown]
DeltaFile
+20-0share/mk/bsd.crunchgen.mk
+3-0initrd/rescue/Makefile
+23-02 files

DragonFlyBSD/src 8410ea0usr.bin/crunch/crunchgen crunchgen.c, usr.bin/crunch/crunchide crunchide.c

crunchgen(1): Remove the unneeded '-dc' linker flag

crunchide(1) does not hide symbols by making them local for many years.
We've also been using '-fno-common' compiler flag for a long time. So
the '-dc' linker flag is obsolete. Just remove it.

Obtained-from: FreeBSD (https://reviews.freebsd.org/D34215)
DeltaFile
+2-4usr.bin/crunch/crunchide/crunchide.c
+1-2usr.bin/crunch/crunchgen/crunchgen.c
+3-62 files

DragonFlyBSD/src 5fd9458usr.bin/crunch/crunchgen crunchgen.1 crunchgen.c

crunchgen(1): Enhance the '-q' (quiet) option to control warnings

Only suppress the warning messages about the component program when the
'-q' option is specified twice.

For example, now the following suppressed warning in crunching
initrd/rescue is shown:
```
crunchgen: rescue.conf: awk: warning: could not find any .o files
```

This also reveals a real problem in the 'awk' component, and I've
created a bug to track it:
https://bugs.dragonflybsd.org/issues/3414
DeltaFile
+7-7usr.bin/crunch/crunchgen/crunchgen.c
+2-1usr.bin/crunch/crunchgen/crunchgen.1
+9-82 files

DragonFlyBSD/src 9dd2293usr.bin/crunch/crunchgen crunchgen.c crunchgen.1

crunchgen(1): Update usage() text and sort options

* Add the missing '-l' option to the usage text.
* Sort the options in the usage text and in the man page.
DeltaFile
+7-9usr.bin/crunch/crunchgen/crunchgen.1
+4-4usr.bin/crunch/crunchgen/crunchgen.c
+11-132 files

DragonFlyBSD/src 4c33bcausr.bin/crunch/crunchgen crunchgen.c

crunchgen(1): Clean up and improve the makefile writeout code

No functional changes.
DeltaFile
+43-47usr.bin/crunch/crunchgen/crunchgen.c
+43-471 files

DragonFlyBSD/src da4731fusr.bin/crunch/crunchgen crunchgen.c

crunchgen(1): Fix a repeated word in comment and fix styles
DeltaFile
+5-3usr.bin/crunch/crunchgen/crunchgen.c
+5-31 files

DragonFlyBSD/src 6f7f058usr.bin/crunch/crunchgen crunchgen.c

crunchgen(1): Clean up intlib_makefile_rules() a bit

While there, apply minor style fixes.
DeltaFile
+7-12usr.bin/crunch/crunchgen/crunchgen.c
+7-121 files

DragonFlyBSD/src 366bf19lib/libnvmm libnvmm_x86.c, sys/dev/virtual/nvmm/x86 nvmm_x86.h nvmm_x86_svm.c

nvmm: Fully sync with master version
DeltaFile
+402-307lib/libnvmm/libnvmm_x86.c
+483-0sys/dev/virtual/nvmm/x86/nvmm_x86_internal.h
+365-64test/testcases/libnvmm/h_mem_assist.c
+338-90sys/dev/virtual/nvmm/x86/nvmm_x86_svm.c
+393-0test/testcases/libnvmm/h_misc.c
+24-256sys/dev/virtual/nvmm/x86/nvmm_x86.h
+2,005-71726 files not shown
+2,780-1,17432 files

DragonFlyBSD/src 068147cshare/man/man9 atomic.9, sys/cpu/x86_64/include atomic.h

atomic(9): Add relaxed load/store variants from FreeBSD

Obtained from FreeBSD.  However, the original atomic_store_ptr() macro
was causing '-Wcast-qual' warnings, so I changed it based on the
NetBSD's atomic_store_relaxed().

Actually, I found NetBSD's version more clean, but we import the FreeBSD
version to help import code/drivers in the future.

Discussed-with: dillon
DeltaFile
+131-0sys/sys/atomic_common.h
+14-13share/man/man9/atomic.9
+1-2sys/cpu/x86_64/include/atomic.h
+146-153 files

DragonFlyBSD/src ae3845csys/dev/virtual/nvmm nvmm_os.h, sys/sys bitops.h

<sys/bitops.h>: Import ilog2(n) from NetBSD

And thus remove the local define from 'nvmm_os.h'.
DeltaFile
+79-0sys/sys/bitops.h
+0-1sys/dev/virtual/nvmm/nvmm_os.h
+79-12 files

DragonFlyBSD/src b0913e3sys/vfs/hammer2 hammer2_vfsops.c

hammer2: Disable debug v/f-chain dumps on unmounting

The two dumps were added to track down the HAMMER2-msg leak reported by
malloc_uninit() and would always print the following logs at shutdown:

```
v-chain 0xfffff8008e6204c0 volume.0   0000000000000010 0000000000000000/0  mir=00000000000
00140
      [00002000] (?) refs=1
f-chain 0xfffff8008e620640 freemap.0   0000000000000010 0000000000000000/0  mir=0000000000
000140
      [00002000] (?) refs=1
```

Given that the memory leak has been fixed in commit
bfcedfb468d712f29cadb491bec0928ad4279bad, disable these two debug dumps
now.

ok by dillon.
DeltaFile
+10-5sys/vfs/hammer2/hammer2_vfsops.c
+10-51 files

DragonFlyBSD/src 40e5c56sys/vfs/hammer2 hammer2_vfsops.c

hammer2: Disable debug v/f-chain dumps on unmounting

The two dumps were added to track down the HAMMER2-msg leak reported by
malloc_uninit() and would always print the following logs at shutdown:

```
v-chain 0xfffff8008e6204c0 volume.0   0000000000000010 0000000000000000/0  mir=00000000000
00140
      [00002000] (?) refs=1
f-chain 0xfffff8008e620640 freemap.0   0000000000000010 0000000000000000/0  mir=0000000000
000140
      [00002000] (?) refs=1
```

Given that the memory leak has been fixed in commit
bfcedfb468d712f29cadb491bec0928ad4279bad, disable these two debug dumps
now.

ok by dillon.
DeltaFile
+10-5sys/vfs/hammer2/hammer2_vfsops.c
+10-51 files

DragonFlyBSD/src b243cfcsys/kern kern_dmsg.c

kern: Fix memory leak in kdmsg_iocom_uninit()

When kdmsg_iocom_uninit() is called but no connection was ever
established (no reader/writer threads), a PING message was allocated and
queued but never freed. This caused a 120-byte leak from the HAMMER2-msg
allocator on shutdown.

Fix this by calling kdmsg_drain_msgq() to clean up any queued messages.

This fixes the 'malloc_uninit: 120 bytes of HAMMER2-msg still allocated'
warning.  Note that this warning already disappeared after fixing the
hammer2_ioctl_recluster() bug in commit
c5cc81fefa166cc45657541c155797f0cff88c62.

Bug: https://bugs.dragonflybsd.org/issues/3385
DeltaFile
+7-1sys/kern/kern_dmsg.c
+7-11 files

DragonFlyBSD/src 5531067sys/kern kern_slaballoc.c

kern: Clean up malloc_uninit()

* Correct the diagnostic log message by omitting the CPU id, which
  doesn't apply here. The unfreed bytes must be summed from all CPUs.

* Clean up excess INVARIANTS ifdef's by using '__debugvar' instead.
DeltaFile
+3-9sys/kern/kern_slaballoc.c
+3-91 files

DragonFlyBSD/src 57fb4c1sys/dev/drm linux_kobject.c

drm: Fix kobject_release() to free kobj->name

While there, adjust kvasprintf() in kobject_init_and_add() to use
'GFP_KERNEL' for consistency.
DeltaFile
+5-1sys/dev/drm/linux_kobject.c
+5-11 files