Commit graph

9468 commits

Author SHA1 Message Date
Andrew Turner
658631a37d Add PT_GETREGSET
This adds the PT_GETREGSET and PT_SETREGSET ptrace types. These can be
used to access all the registers from a specified core dump note type.
The NT_PRSTATUS and NT_FPREGSET notes are initially supported. Other
machine-dependant types are expected to be added in the future.

The ptrace addr points to a struct iovec pointing at memory to hold the
registers along with its length. On success the length in the iovec is
updated to tell userspace the actual length the kernel wrote or, if the
base address is NULL, the length the kernel would have written.

Because the data field is an int the arguments are backwards when
compared to the Linux PTRACE_GETREGSET call.

Reviewed by:	kib
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D19831

(cherry picked from commit 548a2ec49b)
2022-05-12 15:12:59 -07:00
Alex Richardson
afab9d5ffe Export _mmap and __sys_mmap from libc.so
Unlike the other syscalls these two symbols were missing from the
version script. I noticed this while looking into the compiler-rt
runtime libraries for CHERI.

Reviewed by:	brooks
Obtained from:	https://github.com/CTSRD-CHERI/cheribsd/pull/1063
MFC after:	3 days

(cherry picked from commit 395db99f32)
2022-05-07 12:55:00 +01:00
Andrew Turner
3935c58c21 Have stpncpy tests ask the kernel for the page size
It may be dynamic so we can't rely on PAGE_SIZE being present or
correct.

(cherry picked from commit 748f7c8db7)
2022-05-03 15:04:04 +01:00
Konstantin Belousov
aca70bd01e sched_getaffinity(3): more compatibility with Linux
(cherry picked from commit 67fc95025c)
2022-05-02 21:08:44 +03:00
John Baldwin
a641444939 TLS: Use <machine/tls.h> for libc and rtld.
- Include <machine/tls.h> in MD rtld_machdep.h headers.

- Remove local definitions of TLS_* constants from rtld_machdep.h
  headers and libc using the values from <machine/tls.h> instead.

- Use _tcb_set() instead of inlined versions in MD
  allocate_initial_tls() routines in rtld.  The one exception is amd64
  whose _tcb_set() invokes the amd64_set_fsbase ifunc.  rtld cannot
  use ifuncs, so amd64 inlines the logic to optionally write to fsbase
  directly.

- Use _tcb_set() instead of _set_tp() in libc.

- Use '&_tcb_get()->tcb_dtv' instead of _get_tp() in both rtld and libc.
  This permits removing _get_tp.c from rtld.

- Use TLS_TCB_SIZE and TLS_TCB_ALIGN with allocate_tls() in MD
  allocate_initial_tls() routines in rtld.

Reviewed by:	kib, jrtc27 (earlier version)
Differential Revision:	https://reviews.freebsd.org/D33353

(cherry picked from commit 8bcdb144eb)
2022-04-29 13:50:05 -07:00
John Baldwin
374ca4a3dd libc: Fix the alignment of the TCB to match rtld for several architectures.
- Use 16 byte alignment rather than 8 for aarch64, powerpc64, and RISC-V.

- Use 8 byte alignment rather than 4 for 32-bit arm, mips, and powerpc.

I suspect that mips64 should be using 16 byte alignment, but both libc
and rtld currently use 8 byte alignment.

Reviewed by:	kib, jrtc27
Sponsored by:	The University of Cambridge, Google Inc.
Differential Revision:	https://reviews.freebsd.org/D33350

(cherry picked from commit 4c2f5bfbfa)
2022-04-29 13:50:05 -07:00
John Baldwin
5c1667e8be mips _libc_get_static_tls_base: Narrow scope of #ifdef.
Reviewed by:	kib, emaste, jrtc27
Sponsored by:	The University of Cambridge, Google Inc.
Differential Revision:	https://reviews.freebsd.org/D33348

(cherry picked from commit 9952b82b39)
2022-04-29 13:50:05 -07:00
Gordon Bergling
dd19f0e9c0 time(3): Refine history in the manual page
The time() system call first appeared in Version 1 AT&T UNIX.  Through
the Version 3 AT&T UNIX, it returned 60 Hz ticks since an epoch that
changed occasionally, because it was a 32-bit value that overflowed in a
little over 2 years.

In Version 4 AT&T UNIX the granularity of the return value was reduced to
whole seconds, delaying the aforementioned overflow until 2038.

Version 7 AT&T UNIX introduced the ftime() system call, which returned
time at a millisecond level, though retained the gtime() system call
(exposed as time() in userland).  time() could have been implemented as a
wrapper around ftime(), but that wasn't done.

4.1cBSD implemented a higher-precision time function gettimeofday() to
replace ftime() and reimplemented time() in terms of that.

Since FreeBSD 9 the implementation of time() uses
clock_gettime(CLOCK_SECOND) instead of gettimeofday() for performance
reasons.

With most valuable input from Warner (imp@).

Reviewed by:	0mp, jilles, imp
Differential Revision:	https://reviews.freebsd.org/D34751

(cherry picked from commit 3e0f3678ec)
2022-04-21 08:26:31 +02:00
Konstantin Belousov
6daddc54de Mostly revert a5970a529c: Make files opened with O_PATH to not block non-forced unmount
(cherry picked from commit bf13db086b)
2022-04-19 23:22:54 +03:00
Andrew Turner
2e38975e5a Add an implementation of .mcount on arm64
To support cc -pg on arm64 we need to implement .mcount. As clang and
gcc think it is function like it just needs to load the arguments
to _mcount and call it.

On gcc the first argument is passed in x0, however this is missing on
clang so we need to load it from the stack. As it's the caller return
address this will be at a known location.

PR:		262709
Reviewed by:	emaste (earlier version)
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D34634

(cherry picked from commit 16d5f9a164)
2022-04-19 10:18:02 +01:00
Gordon Bergling
eafea71138 resolver(3): Fix a typo in a source code comment
- s/psuedo/pseudo/

(cherry picked from commit b4ce0ff19a)
2022-04-14 08:10:32 +02:00
Gordon Bergling
f7ed8a5a7f getenv(3): Fix two typos in source code comments
- s/peform/perform/

(cherry picked from commit 8dcf5860b3)
2022-04-14 08:03:09 +02:00
Greg Lehey
01f095a355 chroot.2: Correct grammar errors.
No functional change.

MFC after:	1 week

(cherry picked from commit 4044083079)
2022-04-14 10:03:36 +10:00
Ganael LAPLANCHE
e66bbe6e02 libc: Check for readdir(2) errors in fts(3)
Previously, such errors were not distinguished from the end-of-directory
condition.

With improvements from Mahmoud Abumandour <ma.mandourr@gmail.com>.

Reviewed by:	markj
PR:		262038

(cherry picked from commit 0cff70ca66)
2022-04-11 09:43:28 -04:00
Gordon Bergling
28612e5934 if_indextoname(3): Correct the RFC in a comment
RFC 2533 refers to 'A Syntax for Describing Media Feature Sets',
which is wrong since the correct reference should be
RFC 2553 'Basic Socket Interface Extensions for IPv6'.

Obtained from:	OpenBSD

(cherry picked from commit fa556e83be)
2022-04-10 07:52:14 +02:00
Gordon Bergling
745e044fe2 rpc(3): Fix two typos in source code comments
- s/alloctaed/allocated/

Obtained from:	NetBSD

(cherry picked from commit 5c49e1cbea)
2022-04-09 08:17:17 +02:00
Gordon Bergling
7c46e06d09 libc: Fix a typo in a source code comment
- s/compnent/component/

(cherry picked from commit 066e393f8f)
2022-04-02 15:28:50 +02:00
Mark Johnston
7be0c792c4 libc: Restore fp state upon flush error in fputc
This is akin to commit bafaa70b6f.

Reported by:	Guy Yur <guyyur@gmail.com>
Fixes:		86a16ada1e
Sponsored by:	The FreeBSD Foundation

(cherry picked from commit 6e13794fbe)
2022-03-31 11:21:48 -04:00
Mateusz Guzik
c5890784f6 amd64: bring back asm bcmp, shared with memcmp
Turns out clang converts "memcmp(foo, bar, len) == 0" and similar to
bcmp calls.

Reviewed by:	emaste (previous version), jhb (previous version)
Differential Revision:	https://reviews.freebsd.org/D34673

(cherry picked from commit fbc002cb72)
2022-03-29 10:25:17 +00:00
Piotr Pawel Stefaniak
6018f775ce Mention kern.timecounter.alloweddeviation in nanosleep.2
PR:		224837
Reported by:	Aleksander Derevianko

(cherry picked from commit bf8f6ffcb6)
(cherry picked from commit 4f556830de)
2022-03-26 14:34:20 +01:00
Konstantin Belousov
647f02d68a libc __sfvwrite(): roll back FILE buffer pointer on fflush error
__sfvwrite() advances the pointer before calling fflush.  If fflush()
fails, it is not enough to roll back inside it, because we cannot know
how much was advanced by the caller.

Reported by:	Peter <pmc@citylink.dinoex.sub.org>
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Fixes:	86a16ada1e

(cherry picked from commit bafaa70b6f)
2022-03-14 10:10:37 -04:00
Mateusz Piotrowski
85379a47c4 time.3: Update ERRORS section
time() is now implemented using clock_gettime(2) instead of
gettimeofday(2).

Reviewed by:	debdrup
Fixes:		358ed16f75 Use clock_gettime(CLOCK_SECOND)
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D34438

(cherry picked from commit 413045a52c)
2022-03-11 11:05:47 +01:00
Piotr Kubaj
137f2548ad powerpc: enable initial-exec TLS
Summary:
Use initial-exec, like other architectures.

While here, switch MACHINE_ARCH in lib/libc/Makefile to LIBC_ARCH and consistently use powerpc.

Subscribers: imp, #contributor_reviews_base

Differential Revision: https://reviews.freebsd.org/D34315
Reviewed by:	luporl
MFC after:	2 weeks

(cherry picked from commit 884ba43116)
2022-03-08 01:28:33 +01:00
Mateusz Guzik
8891979494 fd: add close_range(..., CLOSE_RANGE_CLOEXEC)
For compatibility with Linux.

MFC after:	3 days
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D34424

(cherry picked from commit f3f3e3c44d)
2022-03-07 12:15:47 +00:00
Stefan Eßer
23fe1083ca libc: return partial sysctl() result if buffer is too small
Testing of a new feature revealed that calling sysctl() to retrieve
the value of the user.localbase variable passing too low a buffer size
could leave the result buffer unchanged.

The behavior in the normal case of a sufficiently large buffer was
correct.

All known callers pass a sufficiently large buffer and have thus not
been affected by this issue. If a non-default value had been assigned
to this variable, the result was as documented, too.

Fix the function to fill the buffer with a partial result, if the
passed in buffer size is too low to hold the full result.

(cherry picked from commit e11ad014d1)

libc: add helper furnction to set sysctl() user.* variables

Testing had revealed that trying to retrieve the user.localbase
variable into to small a buffer would return the correct error code,
but would not fill the available buffer space with a partial result.

A partial result is of no use, but this is still a violation of the
documented behavior, which has been fixed in the previous commit to
this function.

I just checked the code for "user.cs_path" and found that it had the
same issue.

Instead of fixing the logic for each user.* sysctl string variable
individually, this commit adds a helper function set_user_str() that
implements the semantics specified in the sysctl() man page.

It is currently only used for "user.cs_path" and "user.localbase",
but it will offer a significant simplification when further such
variables will be added (as I intend to do).

(cherry picked from commit 9535d9f104)

sysctlbyname(): restore access to user variables

The optimization of sysctlbyname() in commit d05b53e0ba had the
side-effect of not going through the fix-up for the user.* variables
in the previously called sysctl() function.

This lead to 0 or an empty strings being returned by sysctlbyname()
for all user.* variables.

An alternate implementation would store the user variables in the
kernel during system start-up. That would allow to remove the fix-up
code in the C library that is currently required to provide the actual
values.

This update restores the previous code path for the user.* variables
and keeps the performance optimization intact for all other variables.

(cherry picked from commit af7d105379)
2022-03-04 20:54:00 +01:00
Stefan Eßer
977ed30681 fread.c: fix undefined behavior
A case of undefined behavior in __fread() has been detected by UBSAN
and reported by Mark Millard:

/usr/main-src/lib/libc/stdio/fread.c:133:10: runtime error: applying
zero offset to null pointer
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior in
/usr/main-src/lib/libc/stdio/fread.c:133:10

While being benign (the NULL pointer is later passed to memcpy() with
a length argument of 0), this issue causes in the order of 600 Kyua
test cases to fail on systems running a world built with WITH_UBSAN
and WITH_ASAN.

The undefined behavior can be prevented by skipping operations that
have no effect for r == 0. Mark Millard has suggested to only skip
this code segment if fp->_p == NULL, but I have verified that for the
case of r == 0 no further argument checking is performed on the
addresses passed to memcpy() and thus no bugs are hidden from the
sanitizers due to the simpler condition chosen.

(cherry picked from commit 10af8e45a8)
2022-03-04 20:47:23 +01:00
Stefan Eßer
19b00621b6 qsort.c: prevent undefined behavior
Mark Milliard has detected a case of undefined behavior with the LLVM
UBSAN. The mandoc program called qsort with a==NULL and n==0, which is
allowed by the POSIX standard. The qsort() in FreeBSD did not attempt
to perform any accesses using the passed pointer for n==0, but it did
add an offset to the pointer value, which is undefined behavior in
case of a NULL pointer. This operation has no adverse effects on any
achitecture supported by FreeBSD, but could be caught in more strict
environments.

After some discussion in the freebsd-current mail list, it was
concluded that the case of a==NULL and n!=0 should still be caught by
UBSAN (or cause a program abort due to an illegal access) in order to
not hide errors in programs incorrectly invoking qsort().

Only the the case of a==NULL and n==0 should be fixed to not perform
the undefined operation on a NULL pointer.

This commit makes qsort() exit before reaching the point of
potentially undefined behvior for the case n==0, but does not test
the value of a, since the result will not depend on whether this
pointer is NULL or an actual pointer to an array if n==0.

The issue found by Mark Milliard in the whatis command has been
reported to the upstream (OpenBSD) and has already been patched
there.

(cherry picked from commit d106f982a5)
2022-03-04 20:47:02 +01:00
Eric van Gyzen
ec68cb077b sendfile_test: fix copy-paste bug
Require the newly opened file descriptor to be good, instead of
re-requiring the one that was required three lines earlier.
Thankfully, opening /dev/null is really unlikely to fail.

Reported by:	Coverity
MFC after:	1 week
Sponsored by:	Dell EMC Isilon

(cherry picked from commit a8fea07c30)
2022-03-02 15:56:31 -06:00
Konstantin Belousov
d0199f27c0 libc binuptime(): use the right function to get the most significant bit index
PR:	261781

(cherry picked from commit a1f9326607)
2022-02-15 02:36:51 +02:00
Mateusz Piotrowski
8152b699d7 strftime.3: Fix a typo and use St for standards
MFC after:	1 week

(cherry picked from commit 4073917408)
2022-02-14 18:05:07 +01:00
Kyle Evans
7393eedb03 execve: disallow argc == 0
The manpage has contained the following verbiage on the matter for just
under 31 years:

"At least one argument must be present in the array"

Previous to this version, it had been prefaced with the weakening phrase
"By convention."

Carry through and document it the rest of the way.  Allowing argc == 0
has been a source of security issues in the past, and it's hard to
imagine a valid use-case for allowing it.  Toss back EINVAL if we ended
up not copying in any args for *execve().

The manpage change can be considered "Obtained from: OpenBSD"

(cherry picked from commit 773fa8cd13)
(cherry picked from commit c9afc7680f)
2022-02-10 14:21:59 -06:00
Konstantin Belousov
4a978de48b lnumeric.c: replace some space*8 by tabs
(cherry picked from commit 529575be27)
2022-02-09 02:42:46 +02:00
Konstantin Belousov
a6ba681b44 __ctype_load(): check for calloc() failure
(cherry picked from commit ef061a2e29)
2022-02-09 02:42:46 +02:00
Konstantin Belousov
0f674081c4 __numeric_load(): check for calloc() failure
(cherry picked from commit 87151b60e0)
2022-02-09 02:42:46 +02:00
Konstantin Belousov
0c60f03d83 libc/locale/lnumeric.c: minor style
(cherry picked from commit 1aa669c5f9)
2022-02-09 02:42:46 +02:00
Konstantin Belousov
23da067dd4 __monetary_load(): check for calloc() failure
(cherry picked from commit 4d3b84f67c)
2022-02-09 02:42:45 +02:00
Konstantin Belousov
c3f2e156ba libc/locale/lmonetary.c: minor style
(cherry picked from commit 0fed1e6f18)
2022-02-09 02:42:45 +02:00
Konstantin Belousov
7570b84079 __messages_load(): check for calloc() failure
(cherry picked from commit bc9ce839f9)
2022-02-09 02:42:45 +02:00
Konstantin Belousov
f6d0811884 libc/locale/lmessages.c: minor style
(cherry picked from commit 5b7e92d48f)
2022-02-09 02:42:45 +02:00
Konstantin Belousov
a0f1e4c5ca __collate_load(): check for calloc failure
(cherry picked from commit b8ad908ad9)
2022-02-09 02:42:45 +02:00
Konstantin Belousov
e6d728c97c libc/locale/collate.c: minor style
(cherry picked from commit a8be061167)
2022-02-09 02:42:45 +02:00
Konstantin Belousov
e1c53fc8e1 xlocale.c: only call init_key() when locale was successfully allocated
(cherry picked from commit 7bf532c9d4)
2022-02-09 02:42:45 +02:00
Konstantin Belousov
3e4e8d5cbf xlocale.c: check for allocation failure
PR:	261679

(cherry picked from commit b68522308d)
2022-02-09 02:42:45 +02:00
Konstantin Belousov
236a826c14 xlocale.c:init_key(): do not ignore errors from pthread_key_create()
(cherry picked from commit fcdf9d7de5)
2022-02-09 02:42:45 +02:00
Konstantin Belousov
8a8709c408 libc/locale/xlocale.c: minor style
(cherry picked from commit aaa6fa65a6)
2022-02-09 02:42:45 +02:00
Ed Maste
53f18b7c20 libc: use standard LF line endings, not CRLF
(cherry picked from commit 29e54af43e)
2022-02-08 15:52:23 -05:00
Konstantin Belousov
ec2db06d0d stdio: add test for 86a16ada1e: fflush() handling of errors
PR:	76398

(cherry picked from commit 72d5dedfa6)
2022-02-01 05:29:16 +02:00
Konstantin Belousov
afa9a1f5ec __sflush(): on write error, if nothing was written, reset FILE state back
PR:	76398

(cherry picked from commit 86a16ada1e)
2022-02-01 05:29:15 +02:00
Konstantin Belousov
5775b8b392 ptrace(2): document policies affecting access to the facility
(cherry picked from commit a393644ecb)
2022-01-29 03:10:45 +02:00
Jessica Clarke
5d10ed57e6 libc: Fix "harmless" iconv one-byte overread
Checking there are still bytes left must be done before dereferencing
the pointer, not the other way round. This is harmless on traditional
architectures since the result will immediately be thrown away, and all
callers are in separate translation units so there is no potential for
optimising based on this out-of-bounds read. However, on CHERI, pointers
are bounded, and so this will trap if fed a string that does not have a
NUL within the first len bytes.

Found by:	CHERI
Reviewed by:	brooks

(cherry picked from commit 6d5297569e)
2022-01-24 23:59:47 +00:00