Skip to content

Commit

Permalink
Build: Fix Linux Landlock feature test in Autotools and CMake builds.
Browse files Browse the repository at this point in the history
The previous Linux Landlock feature test assumed that having the
linux/landlock.h header file was enough. The new feature tests also
requires that prctl() and the required Landlock system calls are
supported.
  • Loading branch information
JiaT75 committed Feb 26, 2024
1 parent eb8ad59 commit 328c52d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
25 changes: 22 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -901,10 +901,29 @@ endif()

# Sandboxing: Landlock
if(NOT SANDBOX_FOUND AND ENABLE_SANDBOX MATCHES "^ON$|^landlock$")
check_include_file(linux/landlock.h HAVE_LINUX_LANDLOCK_H)
# A compile check is done here because some systems have
# linux/landlock.h, but do not have the syscalls defined
# in order to actually use Linux Landlock.
check_c_source_compiles("
#include <linux/landlock.h>
#include <sys/syscall.h>
#include <sys/prctl.h>
.

This comment has been minimized.

Copy link
@ranft

ranft Mar 29, 2024

This little dot is just pure evil.

This comment has been minimized.

Copy link
@rany2

rany2 Mar 29, 2024

This is so sly and with some plausible deniability if it weren't for you know what... I guess the landlock feature was never enabled since this commit, I wonder if this was done for the exploit to work... This guy really seems to have played the long-game

This comment has been minimized.

Copy link
@gamer191

gamer191 Mar 29, 2024

This little dot is just pure evil.

What does it do?

This comment has been minimized.

Copy link
@rany2

rany2 Mar 29, 2024

@gamer191 it prevents that code from compiling, which disables the landlock sandboxing feature.

void my_sandbox(void)
{
(void)prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
(void)SYS_landlock_create_ruleset;
(void)SYS_landlock_restrict_self;
(void)LANDLOCK_CREATE_RULESET_VERSION;
return;
}
int main(void) { return 0; }
"
HAVE_LINUX_LANDLOCK)

if(HAVE_LINUX_LANDLOCK_H)
set(SANDBOX_COMPILE_DEFINITION "HAVE_LINUX_LANDLOCK_H")
if(HAVE_LINUX_LANDLOCK)
set(SANDBOX_COMPILE_DEFINITION "HAVE_LINUX_LANDLOCK")
set(SANDBOX_FOUND ON)

# Of our three sandbox methods, only Landlock is incompatible
Expand Down
27 changes: 26 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1177,12 +1177,37 @@ AS_CASE([$enable_sandbox],
)
AS_CASE([$enable_sandbox],
[auto | landlock], [
AC_CHECK_HEADERS([linux/landlock.h], [
AC_MSG_CHECKING([if Linux Landlock is usable])
# A compile check is done here because some systems have
# linux/landlock.h, but do not have the syscalls defined
# in order to actually use Linux Landlock.
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
#include <linux/landlock.h>
#include <sys/syscall.h>
#include <sys/prctl.h>
void my_sandbox(void)
{
(void)prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
(void)SYS_landlock_create_ruleset;
(void)SYS_landlock_restrict_self;
(void)LANDLOCK_CREATE_RULESET_VERSION;
return;
}
]])], [
enable_sandbox=found
AS_CASE([$CFLAGS], [*-fsanitize=*], [AC_MSG_ERROR([
CFLAGS contains '-fsanitize=' which is incompatible with the Landlock
sandboxing. Use --disable-sandbox when using '-fsanitize'.])])
AC_DEFINE([HAVE_LINUX_LANDLOCK], [1],
[Define to 1 if Linux Landlock is supported.
See configure.ac for details.])
AC_MSG_RESULT([yes])
], [
AC_MSG_RESULT([no])
])
]
)
Expand Down
2 changes: 1 addition & 1 deletion src/xz/sandbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ sandbox_enable_strict_if_allowed(int src_fd lzma_attribute((__unused__)),
}


#elif defined(HAVE_LINUX_LANDLOCK_H)
#elif defined(HAVE_LINUX_LANDLOCK)

//////////////
// Landlock //
Expand Down
2 changes: 1 addition & 1 deletion src/xz/sandbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
///////////////////////////////////////////////////////////////////////////////

#if defined(HAVE_PLEDGE) || defined(HAVE_LINUX_LANDLOCK_H) \
#if defined(HAVE_PLEDGE) || defined(HAVE_LINUX_LANDLOCK) \
|| defined(HAVE_CAP_RIGHTS_LIMIT)
# define ENABLE_SANDBOX 1
#endif
Expand Down
8 changes: 4 additions & 4 deletions src/xzdec/xzdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
# include <sys/capsicum.h>
#endif

#ifdef HAVE_LINUX_LANDLOCK_H
#ifdef HAVE_LINUX_LANDLOCK
# include <linux/landlock.h>
# include <sys/prctl.h>
# include <sys/syscall.h>
#endif

#if defined(HAVE_CAP_RIGHTS_LIMIT) || defined(HAVE_PLEDGE) \
|| defined(HAVE_LINUX_LANDLOCK_H)
|| defined(HAVE_LINUX_LANDLOCK)
# define ENABLE_SANDBOX 1
#endif

Expand Down Expand Up @@ -325,7 +325,7 @@ sandbox_enter(int src_fd)
goto error;

(void)src_fd;
#elif defined(HAVE_LINUX_LANDLOCK_H)
#elif defined(HAVE_LINUX_LANDLOCK)
int landlock_abi = syscall(SYS_landlock_create_ruleset,
(void *)NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);

Expand Down Expand Up @@ -389,7 +389,7 @@ main(int argc, char **argv)
}
#endif

#ifdef HAVE_LINUX_LANDLOCK_H
#ifdef HAVE_LINUX_LANDLOCK
// Prevent the process from gaining new privileges. The return
// is ignored to keep compatibility with old kernels.
(void)prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
Expand Down

0 comments on commit 328c52d

Please sign in to comment.