Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cfi, cross-dso, wrong operation of -fno-sanitize-cfi-cross-dso #47979

Open
P1119r1m mannequin opened this issue Dec 30, 2020 · 1 comment
Open

cfi, cross-dso, wrong operation of -fno-sanitize-cfi-cross-dso #47979

P1119r1m mannequin opened this issue Dec 30, 2020 · 1 comment
Labels
bugzilla Issues migrated from bugzilla compiler-rt:cfi Control Flow Integrity

Comments

@P1119r1m
Copy link
Mannequin

P1119r1m mannequin commented Dec 30, 2020

Bugzilla Link 48635
Version 11.0
OS Linux
Attachments Reproducible example
CC @P1119r1m

Extended Description

Clang compiler doesn't ignore cross-dso calls for the following conditions:

  • CFI is enabled;
  • -fno-sanitize-cfi-cross-dso is used;
  • optimization level is -O2;
  • LLVM's ld.lld linker is used.

"Compiler-time" problem rises if one try to execute the function from the shared object (.so) from the other executable binary (main).

Instead, compiler generates binary code with undefined instruction on the place where cross-dso is used, for example:

  int exec_func_from_dso(void)
  {
      fptr_t fptr = get_fptr();
    201751:       e8 5a 00 00 00          callq  2017b0 <get_fptr@plt>
      fptr();
    201756:       0f 0b                   ud2

At the same time, there is no problem with the source code (please, see attached cfi_cross_dso_problem_report.tar).

Clang's help says:

-fno-sanitize-cfi-cross-dso
    Disable control flow integrity (CFI) checks for cross-DSO calls.

As a user of the Clang I understand that if to use -fno-sanitize-cfi-cross-dso, cross-dso places will not be taken into account during CFI operation (compilation and run-time execution).

@P1119r1m
Copy link
Mannequin Author

P1119r1m mannequin commented Dec 30, 2020

// shared.h

#pragma once
#define DSO_EXPORT __attribute__ ((visibility ("default")))
typedef int (*fptr_t)(void);
extern const fptr_t get_fptr(void) DSO_EXPORT;
// shared.c

#include "shared.h"

int fun(void)
{
    return 1;
}

const fptr_t get_fptr(void)
{
    return fun;
}
// main.c

#include "shared.h"

int exec_func_from_dso(void)
{
    fptr_t fptr = get_fptr();
    fptr();
}

int main()
{
    return exec_func_from_dso();
}
// Makefile

include .config

SANITIZE_CFI_CROSS_DSO = n

CC := $(PATH_HOST_CLANG_DIR)/bin/clang
LD := $(PATH_HOST_CLANG_DIR)/bin/ld.lld
OBJDUMP := objdump

CFLAGS_SWD += -O2
CFLAGS_SWD += -g

CFLAGS_SWD += -fsanitize=cfi

ifeq ($(SANITIZE_CFI_CROSS_DSO),y)
  CFLAGS_SWD += -fsanitize-cfi-cross-dso
else
  CFLAGS_SWD += -fno-sanitize-cfi-cross-dso
endif

CFLAGS_SWD += -flto
CFLAGS_SWD += -fvisibility=hidden
CFLAGS_SWD += -fsplit-lto-unit
CFLAGS_SWD += -fsanitize-undefined-trap-on-error
CFLAGS_SWD += -fno-sanitize-recover=all

.PHONY: all clean objdump
all: shared.so
	$(CC) main.c $(CFLAGS_SWD) -L. -lshared -fuse-ld=lld -o main

main.o: main.c
	$(CC) $(CFLAGS_SWD) -Wall -c main.c -o main.o

shared.o: shared.c shared.h
	$(CC) $(CFLAGS_SWD) -Wall -c shared.c -o shared.o

shared.so: shared.o
	$(LD) -shared shared.o -o libshared.so

clean:
	rm -f main.o main shared.o libshared.so

objdump:
	$(OBJDUMP) -dS main | less
// .config

PATH_HOST_CLANG_DIR=.../Programs/clang+llvm-11.0.0-x86_64-linux-gnu-ubuntu-16.04

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@Endilll Endilll changed the title cfi, cross-dso, wrong operation of -fno-sanitize-cfi-cross-dso cfi, cross-dso, wrong operation of -fno-sanitize-cfi-cross-dso Jul 18, 2024
@Endilll Endilll added the compiler-rt:cfi Control Flow Integrity label Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla compiler-rt:cfi Control Flow Integrity
Projects
None yet
Development

No branches or pull requests

1 participant