DragonFlyBSD/src d1bc137sys/vm vm_page.h

kernel - Update comments on PG_BUSY rules with page wirings

* Adjust code comments to reflect that a vm_page's wire_count can transition
  from 1->0 without being busied under certain circumstances.

* The pmap system is allowed to do this when removing pages because other
  references to the page are still present and there will be no race
  against a vm_page freeing operation.
DeltaFile
+6-2sys/vm/vm_page.h
+6-21 files

DragonFlyBSD/src ab80faasys/vm vm_fault.c

kernel - Fix cryptsetup bug / fix bug in mlockall()

* mlockall() uncovererd an old bug in the VM system that was previously
  masked by other code.  The VM system going way way back assumed that
  wired pages were already wired and that at worst only a zero-fill was
  needed, so the TRYPAGER() macro checked for the case.

  However, when issuing a mlockall(), the refactored VM system uses
  vm_fault() to bring in missing wired pages to satisfy the request,
  was hitting the old condition, and doing a zero-fill instead.

* This caused cryptsetup() (one of the few programs to actually use
  mlockall()) to seg-fault under typical conditions.

* Fix by removing that part of the conditional.  Regardless of the
  wiring flags, the pager is still tried for non-default VM objects.

Reported-by: mneumann, aly
DeltaFile
+8-7sys/vm/vm_fault.c
+8-71 files

DragonFlyBSD/src 3d7bc73sys/platform/pc64/x86_64 pmap.c

kernel - Fix pmap wiring bug

* Fix a pmap wiring accounting bug.  The refactored wiring and pmap code
  expects only a single increment for a pmap wiring when the "W" bit
  transitions from 0 to 1, plus a count on the vm_page.

Found-by: Claude Sonnet 5 (aly)
DeltaFile
+2-3sys/platform/pc64/x86_64/pmap.c
+2-31 files

DragonFlyBSD/src b5c7a41sys/vfs/fifofs fifo_vnops.c

kernel - Fix lost wakeup in fifo_open()

* fifo_open() tests fi_readers, drops the fifo and vnode locks, then
  tsleeps.  A peer opening in that window bumps the counter and issues
  its wakeup before the sleeper is queued, so open(2) blocks forever
  with a peer already attached.  Both paths are affected.

* Queue with tsleep_interlock() before releasing the locks and sleep
  with PINTERLOCKED.

Reviewed-by: @dillon
DeltaFile
+4-2sys/vfs/fifofs/fifo_vnops.c
+4-21 files

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