HardenedBSD/lib
Robert Clausecker 9082398090 lib/libc/amd64/string: fix overread condition in memccpy
An overread condition in memccpy(dst, src, c, len) would occur if
src does not cross a 16 byte boundary and there is no instance of
c between *src and the next 16 byte boundary.  This could cause a
read fault if src is just before the end of a page and the next page
is unmapped or unreadable.

The bug is a consequence of basing memccpy() on the strlcpy() code:
whereas strlcpy() assumes that src is a nul-terminated string and
hence a terminator is always present, c may not be present at all in
the source string.  It was not caught earlier due to insufficient
unit test design.

As a part of the fix, the function is refactored such that the runt
case (buffer length from last alignment boundary between 1 and 32 B)
is handled separately.  This reduces the number of conditional
branches on all code paths and simplifies the handling of early
matches in the non-runt case.  Performance is improved slightly.

os: FreeBSD
arch: amd64
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
        │ memccpy.unfixed.out │        memccpy.fixed.out           │
        │       sec/op        │   sec/op     vs base               │
Short             66.76µ ± 0%   62.45µ ± 1%  -6.44% (p=0.000 n=20)
Mid               7.938µ ± 0%   7.967µ ± 0%  +0.36% (p=0.001 n=20)
Long              3.577µ ± 0%   3.577µ ± 0%       ~ (p=0.429 n=20)
geomean           12.38µ        12.12µ       -2.08%

        │ memccpy.unfixed.out │         memccpy.fixed.out           │
        │         B/s         │     B/s       vs base               │
Short            1.744Gi ± 0%   1.864Gi ± 1%  +6.89% (p=0.000 n=20)
Mid              14.67Gi ± 0%   14.61Gi ± 0%  -0.36% (p=0.001 n=20)
Long             32.55Gi ± 0%   32.55Gi ± 0%       ~ (p=0.429 n=20)
geomean          9.407Gi        9.606Gi       +2.12%

Reported by:	getz
Reviewed by:	getz
Approved by:	mjg (blanket, via IRC)
See also:	D46051
MFC:		stable/14
Event:		GSoC 2024
Differential Revision:	https://reviews.freebsd.org/D46052
2024-07-29 21:36:10 +02:00
..
atf
clang
csu
flua
geom
googletest
lib9p
lib80211
libalias
libarchive
libauditd package: move OpenBSM auditing into its own package 2024-04-28 22:33:06 -06:00
libbe
libbearssl
libbegemot
libblacklist
libblocksruntime
libbluetooth Remove $FreeBSD$: one-line nroff pattern 2023-08-16 11:55:15 -06:00
libbsddialog dirdeps: Update Makefile.depend* files with empty contents 2023-10-29 17:01:04 -04:00
libbsdstat
libbsm
libbsnmp
libbz2
libc lib/libc/amd64/string: fix overread condition in memccpy 2024-07-29 21:36:10 +02:00
libc_nonshared
libc++
libc++experimental Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libcalendar Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libcam
libcapsicum
libcasper
libcbor
libclang_rt
libcom_err
libcompat Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libcompiler_rt Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libcrypt
libcuse
libcxxrt
libdevctl
libdevdctl
libdevinfo
libdevstat Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libdiff
libdl
libdpv Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libdwarf
libedit
libefivar
libelf
libelftc
libevent1
libexecinfo
libexpat
libfdt
libfetch
libfido2
libfigpar
libgcc_eh
libgcc_s
libgeom
libgpio Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libgssapi
libiconv_modules
libifconfig Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libipsec
libipt
libiscsiutil
libjail
libkiconv
libkvm
libldns Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
liblua Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
liblutok
liblzma
libmagic
libmd
libmemstat
libmilter
libmixer
libmp
libmt
libnetbsd
libnetgraph
libnetmap
libnv Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libnvmf libnvmf: Reject invalid values of MAXH2CDATA for new associations 2024-07-25 15:32:42 -04:00
libomp
libopenbsd
libopencsd
libpam
libpathconv
libpcap
libpe
libpfctl pf: convert DIOCGETADDR to netlink 2024-07-22 09:11:49 +02:00
libpjdlog lib: Automated cleanup of cdefs and other formatting 2023-11-26 22:23:59 -07:00
libpmc
libpmcstat
libproc
libprocstat
libradius
libregex
librpcsec_gss
librpcsvc
librss
librt
librtld_db
libsbuf
libsdp
libsecureboot
libsm Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libsmb
libsmdb
libsmutil
libsqlite3
libssp
libssp_nonshared Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libstats
libstdbuf
libstdthreads Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libsys vm: Remove kernel stack swapping support, part 11 2024-07-29 01:43:59 +00:00
libsysdecode
libtacplus
libtelnet
libthr Revert "Avoid division in round_up." 2024-07-26 00:11:11 +01:00
libthread_db
libucl
libufs
libugidfw
libulog
libunbound
libusb
libusbhid
libutil
libveriexec
libvgl
libvmmapi
libwrap
libxo
liby
libypclnt
libz
libzstd
msun libm: fma: correct zero sign with small inputs 2024-07-28 17:37:45 -04:00
ncurses
nss_tacplus nss_tacplus: Provide dummy setpwent(), getpwent_r(), endpwent(). 2023-08-31 07:49:57 +00:00
ofed
tests
Makefile
Makefile.inc