opnsense-src/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 Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
clang Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
csu Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
flua Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
geom Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
googletest Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
lib9p Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
lib80211 lib80211: Consistently use item count as the first argument to calloc 2024-07-19 13:06:16 -04:00
libalias Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libarchive libarchive: fix thread autodetermination for zstd compression format 2024-05-03 15:39:37 +02:00
libauditd package: move OpenBSM auditing into its own package 2024-04-28 22:33:06 -06:00
libbe Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libbearssl Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libbegemot Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libblacklist Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libblocksruntime Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
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 Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libbsm package: move OpenBSM auditing into its own package 2024-04-28 22:33:06 -06:00
libbsnmp Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libbz2 Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libc lib/libc/amd64/string: fix overread condition in memccpy 2024-07-29 21:36:10 +02:00
libc++ Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libc++experimental Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libc_nonshared 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 Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libcapsicum Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libcasper libcasper: Consistently use item count as the first argument to calloc 2024-07-19 13:05:58 -04:00
libcbor Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libclang_rt Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libcom_err Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
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 Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libcuse Remove $FreeBSD$: one-line nroff pattern 2023-08-16 11:55:15 -06:00
libcxxrt libcxxrt: allow build with gcc13 and --no-undefined-version 2024-05-17 21:50:57 +01:00
libdevctl Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libdevdctl Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libdevinfo Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libdevstat Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libdiff diff: Nits in tests. 2024-05-20 16:27:50 +02:00
libdl Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libdpv Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libdwarf Remove $FreeBSD$: one-line sh pattern 2023-08-16 11:55:03 -06:00
libedit Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libefivar lib: Automated cleanup of cdefs and other formatting 2023-11-26 22:23:59 -07:00
libelf Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libelftc Remove $FreeBSD$: one-line sh pattern 2023-08-16 11:55:03 -06:00
libevent1 Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libexecinfo Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libexpat Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libfdt libfdt: Make an internal FDT library available 2024-03-21 01:05:05 -04:00
libfetch Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libfido2 libfido2: update to 1.14.0 2024-05-04 12:51:08 -04:00
libfigpar Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libgcc_eh Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libgcc_s Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libgeom libgeom: Consistently use item count as the first argument to calloc 2024-07-19 13:06:03 -04:00
libgpio Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libgssapi Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libiconv_modules Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libifconfig Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libipsec libipsec: decode SADB_X_EXT_IF_HW_OFFL 2024-07-12 06:29:31 +03:00
libipt Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libiscsiutil lib: Automated cleanup of cdefs and other formatting 2023-11-26 22:23:59 -07:00
libjail Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libkiconv Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libkvm Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
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 Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
liblzma Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libmagic libmagic: Use HOST_CC when compiling hostprog used by build 2024-03-27 19:18:43 -04:00
libmd Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libmemstat Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libmilter Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libmixer Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libmp Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libmt Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libnetbsd Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libnetgraph lib: Automated cleanup of cdefs and other formatting 2023-11-26 22:23:59 -07:00
libnetmap libnetmap: remove interface name validation 2023-12-21 14:43:57 +00:00
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 Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libopenbsd Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libopencsd Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libpam Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libpathconv Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libpcap dirdeps: Fix libpcap Makefile.depend.options 2023-11-17 11:34:57 -05:00
libpe Remove $FreeBSD$: one-line sh pattern 2023-08-16 11:55:03 -06:00
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 Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libpmcstat lib: Automated cleanup of cdefs and other formatting 2023-11-26 22:23:59 -07:00
libproc Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libprocstat Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libradius radlib: fix a memory leak in is_valid_request 2024-06-04 19:40:55 -07:00
libregex Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
librpcsec_gss Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
librpcsvc Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
librss Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
librt Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
librtld_db Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libsbuf Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libsdp Remove $FreeBSD$: one-line nroff pattern 2023-08-16 11:55:15 -06:00
libsecureboot Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libsm Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libsmb Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libsmdb Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libsmutil Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libsqlite3 Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libssp Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libssp_nonshared Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libstats Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libstdbuf Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
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 Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libtacplus lib: Automated cleanup of cdefs and other formatting 2023-11-26 22:23:59 -07:00
libtelnet Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libthr Revert "Avoid division in round_up." 2024-07-26 00:11:11 +01:00
libthread_db Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libucl Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libufs Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libugidfw Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libulog Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libunbound Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libusb libusb: claim to be version 1.0.16 2024-06-28 12:18:34 -04:00
libusbhid libusbhid: add missing include guards 2024-06-06 10:09:05 -04:00
libutil Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libveriexec Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libvgl lib/libvgl/main.c: remove an extra semicolon 2024-02-02 18:35:01 -07:00
libvmmapi libvmmapi: Conditionalize compilation of some functions 2024-04-10 11:17:56 -04:00
libwrap Remove $FreeBSD$: one-line sh pattern 2023-08-16 11:55:03 -06:00
libxo Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
liby Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libypclnt Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
libz dirdeps: Update/fix Makefile.depend* for toolchain 2023-11-26 20:50:13 -05:00
libzstd Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
msun libm: fma: correct zero sign with small inputs 2024-07-28 17:37:45 -04:00
ncurses Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
nss_tacplus nss_tacplus: Provide dummy setpwent(), getpwent_r(), endpwent(). 2023-08-31 07:49:57 +00:00
ofed Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
tests Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
Makefile Remove residual blank line at start of Makefile 2024-07-15 16:43:39 -06:00
Makefile.inc Remove $FreeBSD$: one-line sh pattern 2023-08-16 11:55:03 -06:00