Skip to content

Commit

Permalink
[LibOS] Move checkpoint special sections to RELRO segment
Browse files Browse the repository at this point in the history
Currently, `.cp_name.*` and other checkpoint sections are included in
the `.rodata` section in the output. However, these sections require
relocation, and are thus marked writable. This causes the entire text
segment of `libsysdb.so` to become RWX.

This change adds `.data.rel.ro` section to the linker script and moves
these sections there. Additionally, other `.data.rel.ro.*` sections in
input files are also moved there, improving RELRO coverage.

Signed-off-by: Marcelina Kościelnicka <mwk@invisiblethingslab.com>
  • Loading branch information
Marcelina Kościelnicka authored and mwkmwkmwk committed Jun 26, 2023
1 parent f8bc7de commit 0684091
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
17 changes: 17 additions & 0 deletions .ci/check-rwx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env python3

import argparse
import sys
from elftools.elf.elffile import ELFFile
from elftools.elf.constants import P_FLAGS

argparser = argparse.ArgumentParser()
argparser.add_argument('infile', type=argparse.FileType('rb'))

args = argparser.parse_args()

elf = ELFFile(args.infile)
for i, segment in enumerate(elf.iter_segments()):
if segment.header.p_flags & P_FLAGS.PF_X and segment.header.p_flags & P_FLAGS.PF_W:
print(f"error: segment {i} is both writable and executable")
sys.exit(1)
5 changes: 5 additions & 0 deletions .ci/lib/stage-test-direct.jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
stage('test-direct') {
sh '''
.ci/check-rwx.py "$GRAMINE_PKGLIBDIR"/libsysdb.so
.ci/check-rwx.py "$GRAMINE_PKGLIBDIR"/direct/libpal.so
'''

try {
timeout(time: 20, unit: 'MINUTES') {
sh '''
Expand Down
3 changes: 3 additions & 0 deletions .ci/lib/stage-test-sgx.jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
stage('test-sgx') {
sh '''
.ci/check-rwx.py "$GRAMINE_PKGLIBDIR"/libsysdb.so
.ci/check-rwx.py "$GRAMINE_PKGLIBDIR"/sgx/loader
.ci/check-rwx.py "$GRAMINE_PKGLIBDIR"/sgx/libpal.so
.ci/check-no-syscall.sh "$GRAMINE_PKGLIBDIR"/runtime/glibc/libc.so.6
if test -f "$GRAMINE_PKGLIBDIR"/runtime/musl/libc.so
then
Expand Down
4 changes: 2 additions & 2 deletions libos/include/libos_checkpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ struct libos_cp_store {
typedef int (*cp_func)(CP_FUNC_ARGS);
typedef int (*rs_func)(RS_FUNC_ARGS);

extern const char* __cp_name;
extern const char* const __cp_name;
extern const cp_func __cp_func; // TODO: This should be declared as an array of unspecified size.
extern const rs_func __rs_func[];

Expand Down Expand Up @@ -215,7 +215,7 @@ enum {
})

#define BEGIN_CP_FUNC(name) \
const char* cp_name_##name __attribute__((section(".cp_name." #name))) = #name; \
const char* const cp_name_##name __attribute__((section(".cp_name." #name))) = #name; \
extern DEFINE_CP_FUNC(name); \
extern DEFINE_RS_FUNC(name); \
const cp_func cp_func_##name __attribute__((section(".cp_func." #name))) = &cp_##name; \
Expand Down
19 changes: 12 additions & 7 deletions libos/src/arch/x86_64/libos.lds
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ SECTIONS
{
/* the rest of rodata */
*(.rodata .rodata.*)
. = ALIGN(8);
__cp_name = .;
*(SORT(.cp_name.*));
__cp_func = .;
*(SORT(.cp_func.*));
__rs_func = .;
*(SORT(.rs_func.*));
}
.eh_frame_hdr : { *(.eh_frame_hdr) }
.eh_frame : ONLY_IF_RO { *(.eh_frame) }
Expand All @@ -75,6 +68,18 @@ SECTIONS
KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) .init_array))
__init_array_end = .;
}
.data.rel.ro :
{
/* the rest of RELRO data segment */
*(.data.rel.ro .data.rel.ro.*)
. = ALIGN(8);
__cp_name = .;
*(SORT(.cp_name.*));
__cp_func = .;
*(SORT(.cp_func.*));
__rs_func = .;
*(SORT(.rs_func.*));
}
. = DATA_SEGMENT_RELRO_END (0, .);
.data :
{
Expand Down

0 comments on commit 0684091

Please sign in to comment.