Skip to content

Commit e6ce912

Browse files
[libc] address CRs
1 parent 3bdd8a8 commit e6ce912

File tree

8 files changed

+54
-40
lines changed

8 files changed

+54
-40
lines changed

libc/config/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"value": true,
105105
"doc": "Make setjmp save the value of x18, and longjmp restore it. The AArch64 ABI delegates this register to platform ABIs, which can choose whether to make it caller-saved."
106106
},
107-
"LIBC_CONF_SETJMP_ENABLE_FORTIFICATION": {
107+
"LIBC_CONF_SETJMP_FORTIFICATION": {
108108
"value": true,
109109
"doc": "Protect jmp_buf by masking its contents and storing a simple checksum, to make it harder for an attacker to read meaningful information from a jmp_buf or to modify it. This is only supported on x86-64 Linux."
110110
}

libc/docs/configure.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ to learn about the defaults for your platform and target.
5555
- ``LIBC_CONF_SCANF_DISABLE_INDEX_MODE``: Disable index mode in the scanf format string.
5656
* **"setjmp" options**
5757
- ``LIBC_CONF_SETJMP_AARCH64_RESTORE_PLATFORM_REGISTER``: Make setjmp save the value of x18, and longjmp restore it. The AArch64 ABI delegates this register to platform ABIs, which can choose whether to make it caller-saved.
58-
- ``LIBC_CONF_SETJMP_ENABLE_FORTIFICATION``: Protect jmp_buf by masking its contents and storing a simple checksum, to make it harder for an attacker to read meaningful information from a jmp_buf or to modify it. This is only supported on x86-64 Linux.
58+
- ``LIBC_CONF_SETJMP_FORTIFICATION``: Protect jmp_buf by masking its contents and storing a simple checksum, to make it harder for an attacker to read meaningful information from a jmp_buf or to modify it. This is only supported on x86-64 Linux.
5959
* **"string" options**
6060
- ``LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING``: Inserts prefetch for write instructions (PREFETCHW) for memset on x86 to recover performance when hardware prefetcher is disabled.
6161
- ``LIBC_CONF_STRING_UNSAFE_WIDE_READ``: Read more than a byte at a time to perform byte-string operations like strlen.

libc/src/setjmp/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
22
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
33
endif()
44

5-
if (LIBC_CONF_SETJMP_ENABLE_FORTIFICATION)
5+
if (LIBC_CONF_SETJMP_FORTIFICATION)
66
if (TARGET libc.src.setjmp.${LIBC_TARGET_OS}.checksum
77
AND LIBC_TARGET_ARCHITECTURE STREQUAL "x86_64")
88
add_object_library(
@@ -12,11 +12,11 @@ if (LIBC_CONF_SETJMP_ENABLE_FORTIFICATION)
1212
.${LIBC_TARGET_OS}.checksum
1313
)
1414
set(fortification_deps libc.src.setjmp.checksum)
15-
set(fortification_defs -DLIBC_COPT_SETJMP_ENABLE_FORTIFICATION=1)
15+
set(fortification_defs -DLIBC_COPT_SETJMP_FORTIFICATION=1)
1616
else()
1717
message(WARNING "Jmpbuf fortification is enabled but not supported for target ${LIBC_TARGET_ARCHITECTURE} ${LIBC_TARGET_OS}")
1818
set(fortification_deps)
19-
set(fortification_defs -DLIBC_COPT_SETJMP_ENABLE_FORTIFICATION=0)
19+
set(fortification_defs -DLIBC_COPT_SETJMP_FORTIFICATION=0)
2020
endif()
2121
endif()
2222

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===-- Common macros for jmpbuf checksum -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// For now, the checksum is computed with a simple multiply-xor-rotation
10+
// algorithm. The pesudo code is as follows:
11+
//
12+
// def checksum(x, acc):
13+
// masked = x ^ MASK
14+
// high, low = full_multiply(masked, acc)
15+
// return rotate(high ^ low, ROTATION)
16+
//
17+
// Similar other multiplication-based hashing, zero inputs
18+
// for the `full_multiply` function may pollute the checksum with zero.
19+
// However, user inputs are always masked where the initial ACC amd MASK are
20+
// generated with random entropy and ROTATION is a fixed prime number. It should
21+
// be of a ultra-low chance for masked or acc being zero given a good quality of
22+
// system-level entropy.
23+
24+
#define ACCUMULATE_CHECKSUM() \
25+
"mul %[checksum]\n\t" \
26+
"xor %%rax, %[checksum]\n\t" \
27+
"rol $%c[rotation], %[checksum]\n\t"
28+
29+
#define LOAD_CHKSUM_STATE_REGISTERS() \
30+
asm("mov %[value_mask], %[mask]\n\t" \
31+
"mov %[checksum_cookie], %[checksum]\n\t" \
32+
: [mask] "=r"(mask), [checksum] "=r"(checksum) \
33+
: [value_mask] "m"(jmpbuf::value_mask), [checksum_cookie] "m"( \
34+
jmpbuf::checksum_cookie));

libc/src/setjmp/x86_64/longjmp.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
1313

14-
#if LIBC_COPT_SETJMP_ENABLE_FORTIFICATION
14+
#if LIBC_COPT_SETJMP_FORTIFICATION
1515
#include "src/setjmp/checksum.h"
1616
#endif
1717

@@ -26,18 +26,8 @@ namespace LIBC_NAMESPACE_DECL {
2626
"adcl $0x0, %%esi\n\t" \
2727
"movq %%rsi, %%rax\n\t"
2828

29-
#if LIBC_COPT_SETJMP_ENABLE_FORTIFICATION
30-
#define ACCUMULATE_CHECKSUM() \
31-
"mul %[checksum]\n\t" \
32-
"xor %%rax, %[checksum]\n\t" \
33-
"rol $%c[rotation], %[checksum]\n\t"
34-
35-
#define LOAD_CHKSUM_STATE_REGISTERS() \
36-
asm("mov %[value_mask], %[mask]\n\t" \
37-
"mov %[checksum_cookie], %[checksum]\n\t" \
38-
: [mask] "=r"(mask), [checksum] "=r"(checksum) \
39-
: [value_mask] "m"(jmpbuf::value_mask), [checksum_cookie] "m"( \
40-
jmpbuf::checksum_cookie));
29+
#if LIBC_COPT_SETJMP_FORTIFICATION
30+
#include "src/setjmp/x86_64/checksum.def"
4131

4232
// clang-format off
4333
#define RESTORE_REG(DST) \
@@ -81,7 +71,7 @@ namespace LIBC_NAMESPACE_DECL {
8171
RESTORE_RIP()
8272
// clang-format on
8373
: /* outputs */
84-
#if LIBC_COPT_SETJMP_ENABLE_FORTIFICATION
74+
#if LIBC_COPT_SETJMP_FORTIFICATION
8575
[mask] "+r"(mask), [checksum] "+r"(checksum)
8676
#endif
8777
: /* inputs */
@@ -90,7 +80,7 @@ namespace LIBC_NAMESPACE_DECL {
9080
[r14] "i"(offsetof(__jmp_buf, r14)), [r15] "i"(offsetof(__jmp_buf, r15)),
9181
[rsp] "i"(offsetof(__jmp_buf, rsp)),
9282
[rip] "i"(offsetof(__jmp_buf, rip))
93-
#if LIBC_COPT_SETJMP_ENABLE_FORTIFICATION
83+
#if LIBC_COPT_SETJMP_FORTIFICATION
9484
// clang-format off
9585
,[rotation] "i"(jmpbuf::ROTATION)
9686
,[__chksum] "i"(offsetof(__jmp_buf, __chksum))

libc/src/setjmp/x86_64/setjmp.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,16 @@
1111
#include "src/__support/macros/config.h"
1212
#include "src/setjmp/setjmp_impl.h"
1313

14-
#if LIBC_COPT_SETJMP_ENABLE_FORTIFICATION
14+
#if LIBC_COPT_SETJMP_FORTIFICATION
1515
#include "src/setjmp/checksum.h"
1616
#endif
1717

1818
#if !defined(LIBC_TARGET_ARCH_IS_X86)
1919
#error "Invalid file include"
2020
#endif
2121

22-
#if LIBC_COPT_SETJMP_ENABLE_FORTIFICATION
23-
#define ACCUMULATE_CHECKSUM() \
24-
"mul %[checksum]\n\t" \
25-
"xor %%rax, %[checksum]\n\t" \
26-
"rol $%c[rotation], %[checksum]\n\t"
27-
28-
#define LOAD_CHKSUM_STATE_REGISTERS() \
29-
asm("mov %[value_mask], %[mask]\n\t" \
30-
"mov %[checksum_cookie], %[checksum]\n\t" \
31-
: [mask] "=r"(mask), [checksum] "=r"(checksum) \
32-
: [value_mask] "m"(jmpbuf::value_mask), [checksum_cookie] "m"( \
33-
jmpbuf::checksum_cookie));
22+
#if LIBC_COPT_SETJMP_FORTIFICATION
23+
#include "src/setjmp/x86_64/checksum.def"
3424

3525
#define STORE_REG(SRC) \
3626
"mov %%" #SRC ", %%rax\n\t" \
@@ -80,7 +70,7 @@ LLVM_LIBC_FUNCTION(int, setjmp, (jmp_buf buf)) {
8070
STORE_CHECKSUM()
8171
// clang-format on
8272
:
83-
#if LIBC_COPT_SETJMP_ENABLE_FORTIFICATION
73+
#if LIBC_COPT_SETJMP_FORTIFICATION
8474
[checksum] "+r"(checksum)
8575
#endif
8676
:
@@ -89,7 +79,7 @@ LLVM_LIBC_FUNCTION(int, setjmp, (jmp_buf buf)) {
8979
[r14] "i"(offsetof(__jmp_buf, r14)), [r15] "i"(offsetof(__jmp_buf, r15)),
9080
[rsp] "i"(offsetof(__jmp_buf, rsp)),
9181
[rip] "i"(offsetof(__jmp_buf, rip))
92-
#if LIBC_COPT_SETJMP_ENABLE_FORTIFICATION
82+
#if LIBC_COPT_SETJMP_FORTIFICATION
9383
// clang-format off
9484
,[rotation] "i"(jmpbuf::ROTATION)
9585
,[__chksum] "i"(offsetof(__jmp_buf, __chksum))

libc/startup/linux/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ endif()
8888

8989
add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
9090

91-
if (LIBC_CONF_SETJMP_ENABLE_FORTIFICATION AND LIBC_TARGET_ARCHITECTURE STREQUAL "x86_64")
91+
if (LIBC_CONF_SETJMP_FORTIFICATION AND LIBC_TARGET_ARCHITECTURE STREQUAL "x86_64")
9292
set(jmpbuf_fortification_deps libc.src.setjmp.checksum)
93-
set(jmpbuf_fortification_defs -DLIBC_COPT_SETJMP_ENABLE_FORTIFICATION=1)
93+
set(jmpbuf_fortification_defs -DLIBC_COPT_SETJMP_FORTIFICATION=1)
9494
else()
9595
set(jmpbuf_fortification_deps)
96-
set(jmpbuf_fortification_defs -DLIBC_COPT_SETJMP_ENABLE_FORTIFICATION=0)
96+
set(jmpbuf_fortification_defs -DLIBC_COPT_SETJMP_FORTIFICATION=0)
9797
endif()
9898

9999
add_object_library(

libc/startup/linux/do_start.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <sys/mman.h>
2222
#include <sys/syscall.h>
2323

24-
#if LIBC_COPT_SETJMP_ENABLE_FORTIFICATION
24+
#if LIBC_COPT_SETJMP_FORTIFICATION
2525
#include "src/setjmp/checksum.h"
2626
#endif
2727

@@ -135,7 +135,7 @@ void teardown_main_tls() { cleanup_tls(tls.addr, tls.size); }
135135
if (tls.size != 0 && !set_thread_ptr(tls.tp))
136136
syscall_impl<long>(SYS_exit, 1);
137137

138-
#if LIBC_COPT_SETJMP_ENABLE_FORTIFICATION
138+
#if LIBC_COPT_SETJMP_FORTIFICATION
139139
jmpbuf::initialize();
140140
#endif
141141

0 commit comments

Comments
 (0)