Skip to content

Commit

Permalink
c18n: Redirect vfork to fork and filter rfork flags
Browse files Browse the repository at this point in the history
Some software like bmake calls sigaction after calling vfork which
corrupts RTLD's signal dispatch table. Securely supporting vfork under
c18n has proven a difficult task and this commit redirects vfork to fork
when c18n is enabled.

rfork may be used when the user intentionally wants vfork-like memory
sharing behaviour. Thus, only calling it with flags deemed to be 'safe'
is allowed under c18n, unless the call is made from libc (as part of the
implementation for posix_spawn).

This commit also partially reverts
50865cd by removing the execve(2) family
of functions from the list of trusted symbols.

Also redirect _thr_exit and __sys_thr_exit to _rtld_thr_exit.
  • Loading branch information
dpgao committed Apr 15, 2024
1 parent 0812eb2 commit d069f04
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 21 deletions.
8 changes: 3 additions & 5 deletions lib/libc/aarch64/sys/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ SRCS+= __vdso_gettc.c \
sched_getcpu_gen.c

MDASM= cerror.S \
syscall.S \
vfork.S
syscall.S

.ifdef RTLD_SANDBOX
SRCS+= thr_exit.c
PSEUDO+= _thr_exit.o
.ifndef RTLD_SANDBOX
MDASM+= vfork.S
.endif
4 changes: 4 additions & 0 deletions lib/libc/gen/posix_spawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,12 @@ do_posix_spawn(pid_t *pid, const char *path,
*/
p = rfork_thread(RFSPAWN, stack + stacksz, _posix_spawn_thr, &psa);
free(stack);
#else
#if defined(__CHERI_PURE_CAPABILITY__) && defined(RTLD_SANDBOX)
p = __sys_rfork(RFSPAWN);
#else
p = rfork(RFSPAWN);
#endif
if (p == 0)
/* _posix_spawn_thr does not return */
_posix_spawn_thr(&psa);
Expand Down
1 change: 1 addition & 0 deletions lib/libc/include/libc_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ struct __nl_cat_d *__catopen_l(const char *name, int type,
struct _xlocale *locale);

#if defined(__CHERI_PURE_CAPABILITY__) && defined(RTLD_SANDBOX)
__pid_t __sys_rfork(int);
int sigaction_c18n(int, const struct sigaction *, struct sigaction *);
#endif

Expand Down
5 changes: 5 additions & 0 deletions lib/libc/sys/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ SRCS+= POSIX2x_Fork.c

SRCS+= compat-stub.c

.ifdef RTLD_SANDBOX
SRCS+= thr_exit_c18n.c vfork_c18n.c rfork_c18n.c
NOASM+= thr_exit.o vfork.o
.endif

INTERPOSED = \
accept \
accept4 \
Expand Down
50 changes: 50 additions & 0 deletions lib/libc/sys/rfork_c18n.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2024 Dapeng Gao
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include <sys/types.h>

#include <errno.h>
#include <unistd.h>

#include "libc_private.h"

/*
* All rfork flags that do not cause memory-sharing between the child and the
* parent. Use a positive list to err on the safe side.
*/
#define RFFLAGS_SAFE \
(RFPROC | RFNOWAIT | RFFDG | RFCFDG | RFTHREAD | \
RFTSIGZMB | RFLINUXTHPN | RFTSIGFLAGS(RFTSIGMASK))

pid_t
rfork(int flags)
{
if ((flags & RFFLAGS_SAFE) == flags)
return (__sys_rfork(flags));
errno = EINVAL;
return (-1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
*/

#include <sys/cdefs.h>
#include <sys/types.h>

void thr_exit(long *);

void _rtld_thr_exit(long *);

__weak_reference(__sys_thr_exit, thr_exit);
__weak_reference(__sys_thr_exit, _thr_exit);

void
thr_exit(long *state)
__sys_thr_exit(long *state)
{
_rtld_thr_exit(state);
}
42 changes: 42 additions & 0 deletions lib/libc/sys/vfork_c18n.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2024 Dapeng Gao
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include <sys/types.h>

#include "libc_private.h"

__weak_reference(__sys_vfork, vfork);
__weak_reference(__sys_vfork, _vfork);

pid_t
__sys_vfork(void)
{
/*
* Use the raw syscall to avoid the interposing table.
*/
return (__sys_fork());
}
12 changes: 0 additions & 12 deletions libexec/rtld-elf/rtld_c18n_policy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ trust
_longjmp
sigsetjmp
siglongjmp
vfork
rfork
execve
fxecve
execl
execlp
execle
exect
execv
execvp
execvpe
execvP
_rtld_thread_start

callee [RTLD]
Expand Down

0 comments on commit d069f04

Please sign in to comment.