Commit graph

9448 commits

Author SHA1 Message Date
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
Konstantin Belousov
7a6364ac77 kqueue(2): Add note about format of the data for NOTE_EXIT
PR:	261346

(cherry picked from commit 7406ec4ea9)
2022-01-22 02:50:23 +02:00
Ed Maste
423fb85f33 libc: correct SPDX tag on strstr.c
It was obtained from musl, and is MIT licensed.

MFC after:	3 days
Sponsored by:	The FreeBSD Foundation

(cherry picked from commit 2e9bc9d144)
2022-01-18 19:32:29 -05:00
Ed Maste
e475c5f6ca libc: fix misleading comment in strstr
Obtained from:	musl c53e9b239418

(cherry picked from commit c6750f07b4)
2022-01-18 19:32:29 -05:00
Konstantin Belousov
07d32ede46 libc clnt_com_create: relock rpcsoc_lock earlier when port is obtained from portmapper
PR:	261051

(cherry picked from commit 6d06bc688e)
2022-01-15 02:51:13 +02:00
Konstantin Belousov
8c709f30a1 sched_get/setaffinity(): try to be more compatible with Linux
(cherry picked from commit d9cacbf4b0)
2022-01-14 18:17:31 +02:00
Stefan Eßer
dc4114875e Make CPU_SET macros compliant with other implementations
(cherry picked from commit e2650af157)
2022-01-14 18:17:30 +02:00
Math Ieu
3f8542d396 sched_get/setaffinity(3): pid 0 should designate current process
PR:	260487

(cherry picked from commit caacda7a3e)
2022-01-14 18:17:30 +02:00
Konstantin Belousov
58865d5f8d sched.h: Hide all Linux compat sched_* functions under _WITH_CPU_SET_T
(cherry picked from commit 90fa9705d5)
2022-01-14 18:17:29 +02:00
Konstantin Belousov
d3e9527a10 x86: provide userspace implementation of sched_getcpu() where possible
(cherry picked from commit f239545591)
2022-01-14 18:17:29 +02:00
Konstantin Belousov
a48d9f1900 Add sched_getcpu()
(cherry picked from commit 77b2c2f814)
2022-01-14 18:17:29 +02:00
Konstantin Belousov
526f94cc0e Add sched_get/setaffinity(3)
(cherry picked from commit 43736b71dd)
2022-01-14 18:17:29 +02:00
Alexander Motin
375fdb6e16 libc: Fix build with -DNS_REREAD_CONF.
MFC after:	3 days

(cherry picked from commit 62ed2d0152)
2021-12-31 19:26:43 -05:00
Ed Maste
c56c2f0fe2 getfh: clarify that it is a privileged operation
Reviewed by:	kib
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D33629

(cherry picked from commit 5bc2e6e227)
2021-12-29 19:27:34 -05:00
Konstantin Belousov
0a50e517cd Document new variant of swapoff(2)
(cherry picked from commit 9f0fea5d03)
2021-12-20 02:29:11 +02:00
Konstantin Belousov
1791debf4a swapoff: add one more variant of the syscall
For MFC, COMPAT_FREEBSD13 braces were removed.

(cherry picked from commit 5346570276)
2021-12-20 02:29:11 +02:00
Konstantin Belousov
0c747a70c8 swapoff(2): document extended syscall arguments
(cherry picked from commit 79d650f262)
2021-12-20 02:29:11 +02:00
Florian Walpen
30c3a5f248 Add idle priority scheduling privilege group to MAC/priority
(cherry picked from commit a9545eede4)
2021-12-19 04:42:51 +02:00
Florian Walpen
984b9d89f8 MAC/priority module for realtime privilege group
PR:	239125

(cherry picked from commit bf2fa8d9d1)
2021-12-19 04:42:51 +02:00
Andrew Turner
c71bfb1013 Revert "Switch to Arm Optimized Routines for mem* & str*"
The Arm Optimized Routines don't exist in 13. I missed this as my
builds succeeded, likely due to stale depends files.

This reverts commit bfd10d146b.
2021-12-14 12:34:09 +00:00
Andrew Turner
bfd10d146b Switch to Arm Optimized Routines for mem* & str*
These are the updated version of the older Cortex Strings Library we
previously used. The Arm Optimized Routines also support CPU features
that are currently in development on FreeBSD, e.g. Branch Target
Identification (BTI). Rather than add BTI support to the old code it's
easier to just use the maintained version.

Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D32774

(cherry picked from commit 2e8ff4d1d5)
2021-12-14 10:58:01 +00:00