Commit Graph
100 Commits
Author SHA1 Message Date
Sami Tolvanen c48b362b01 ANDROID: Revert "ANDROID: bpf: validate bpf_func when BPF_JIT is enabled with CFI"
This reverts commit 788bbf4f261fc558b714bdedd4122d7115efc940.

Reason for revert: fixes a conflict with upcoming upstream BPF changes.
Bug: 145210207
Change-Id: I3bbc1279fc613be0d2e833008413ad3561b851df
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2025-09-30 21:35:03 +01:00
Sami Tolvanen efb90b93de ANDROID: Revert "ANDROID: arm64: bpf: implement arch_bpf_jit_check_func"
This reverts commit d9756ba082c13d928d6904bc3741a1c08b4bd783.

Reason for revert: fixes a conflict with upcoming upstream BPF changes.
Bug: 145210207
Change-Id: I0e7e76c117ab3608b6dd5a1bc6b949b9e109038f
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2025-09-30 21:35:03 +01:00
Sami Tolvanen a5374ec55e UPSTREAM: ANDROID: bpf: disable CFI in dispatcher functions
BPF dispatcher functions are patched at runtime to perform direct
instead of indirect calls. Disable CFI for the dispatcher functions
to avoid conflicts.

Bug: 145210207
Change-Id: Iea72f5a9fe09dd5adbb90b0174945707f42594b0
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2025-08-28 03:28:26 +03:00
Sami Tolvanen 0e8a2d4177 UPSTREAM: ANDROID: bpf: disable CFI for dispatcher functions
BPF dispatcher functions are patched at runtime to perform direct
instead of indirect calls. Disable CFI for the dispatcher functions
to avoid conflicts.

(re-add due to merge conflicts previously)

Bug: 145210207
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
Change-Id: I7edf6052e121d16ccb0f3d3492ff4eefedfa509e
2025-08-28 03:26:07 +03:00
Sami Tolvanen cee0aa0c31 ANDROID: arm64: add __va_function
With CFI, the compiler replaces function references with pointers
to the CFI jump table. This breaks passing these addresses to
code running at EL2, where the jump tables are not valid. Add a
__va_function macro similarly to the earlier __pa_function to take
address of the actual function in inline assembly and use that in
kvm_ksym_ref instead.

Bug: 163385976
Change-Id: I097b99409995512c00786300e7d18fe42c720a1b
(cherry picked from commit 2f4d6c9fd77c88ad0500aad4bf1f64aaf2654c49)
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2025-03-30 23:53:38 +01:00
Sami Tolvanen 4e7313f026 ANDROID: arm64: Place CFI jump table sections in .text
After the switch to non-canonical CFI jump tables, the jump table
sections were placed after the .text section. Merge these sections
into .text to fix issues with error injection and kallsyms.

Bug: 225079388
Bug: 190422440
Change-Id: I6c81b3e4dbba62739f7fc5f6b45271c54f278c8f
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
[nathan: This change is necessary in android-4.19 to boot after upstream
         LLVM commit 7b346357db30 ("[ELF] Orphan placement: prefer the
         last similar section when its rank <= orphan's rank"), which
         changes how sections not described in linker scripts are laid
         out in the final binary]
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
2024-06-18 19:11:59 +00:00
Sami Tolvanen c98cbc2519 Makefile.extrawarn: Move -Wcast-function-type-strict to W=1
commit 2120635108b35ecad9c59c8b44f6cbdf4f98214e upstream.

We enable -Wcast-function-type globally in the kernel to warn about
mismatching types in function pointer casts. Compilers currently
warn only about ABI incompability with this flag, but Clang 16 will
enable a stricter version of the check by default that checks for an
exact type match. This will be very noisy in the kernel, so disable
-Wcast-function-type-strict without W=1 until the new warnings have
been addressed.

Cc: stable@vger.kernel.org
Link: https://reviews.llvm.org/D134831
Link: https://github.com/ClangBuiltLinux/linux/issues/1724
Suggested-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20220930203310.4010564-1-samitolvanen@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-10-26 13:19:16 +02:00
Sami Tolvanen 594de64394 kbuild: add CONFIG_LD_IS_LLD
commit b744b43f79cc758127042e71f9ad7b1afda30f84 upstream.

Similarly to the CC_IS_CLANG config, add LD_IS_LLD to avoid GNU ld
specific logic such as ld-version or ld-ifversion and gain the
ability to select potential features that depend on the linker at
configuration time such as LTO.

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Acked-by: Masahiro Yamada <masahiroy@kernel.org>
[nc: Reword commit message]
Signed-off-by: Nathan Chancellor <natechancellor@gmail.com>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com>
Reviewed-by: Sedat Dilek <sedat.dilek@gmail.com>
Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-03-11 10:15:12 +01:00
Sami Tolvanen 7d2c060cae UPSTREAM: x86/pci: Fix the function type for check_reserved_t
e820__mapped_all() is passed as a callback to is_mmconf_reserved(),
which expects a function of type:

  typedef bool (*check_reserved_t)(u64 start, u64 end, unsigned type);

However, e820__mapped_all() accepts enum e820_type as the last argument
and this type mismatch trips indirect call checking with Clang's
Control-Flow Integrity (CFI).

As is_mmconf_reserved() only passes enum e820_type values for the
type argument, change the typedef and the unused type argument in
is_acpi_reserved() to enum e820_type to fix the type mismatch.

Bug: 210705746
(cherry picked from commit 83321c335dccba262a57378361d63da96b8166d6)
Reported-by: Sedat Dilek <sedat.dilek@gmail.com>
Suggested-by: Borislav Petkov <bp@alien8.de>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: https://lkml.kernel.org/r/20201130193900.456726-1-samitolvanen@google.com
Change-Id: I92f78710ac55d5f567ad73053fc9f8c7cef68f71
(cherry picked from commit 7d1d5848183bd1d9086d0572f9af431d3ded407f)
(cherry picked from commit 8f54ba9ae7e38e315df32f5338665a1e44cd1a4b)
2022-01-16 08:04:12 +00:00
Sami Tolvanen 5625445e6e ANDROID: syscalls/x86: use a weak function for IA32 compat syscalls
After LLVM commit rG8f8b5e5587c3 ("[MC] Error for .globl/.local which
change the symbol binding and warn for .weak"), using cond_syscall()
for IA32 compat syscalls produces the following errors with LTO:

  error: __ia32_compat_sys_sysctl changed binding to STB_GLOBAL

Avoid conflicting .weak/.globl statements by using a weak function for
the stub instead.

Bug: 180726982
Link: https://github.com/ClangBuiltLinux/linux/issues/1269
Change-Id: I52446c153155f40cefecf6230cbc0d229de02041
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2021-03-02 11:16:08 -08:00
Sami Tolvanen 5681cce44c arm64: lse: fix LSE atomics with LLVM's integrated assembler
commit e0d5896bd356cd577f9710a02d7a474cdf58426b upstream.

Unlike gcc, clang considers each inline assembly block to be independent
and therefore, when using the integrated assembler for inline assembly,
any preambles that enable features must be repeated in each block.

This change defines __LSE_PREAMBLE and adds it to each inline assembly
block that has LSE instructions, which allows them to be compiled also
with clang's assembler.

Link: https://github.com/ClangBuiltLinux/linux/issues/671
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Tested-by: Andrew Murray <andrew.murray@arm.com>
Tested-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Andrew Murray <andrew.murray@arm.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
[nd: backport adjusted due to missing:
  commit addfc38672c7 ("arm64: atomics: avoid out-of-line ll/sc atomics")]
Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-12-30 11:25:43 +01:00
Sami Tolvanen 448ad389cb perf/x86: fix sysfs type mismatches
[ Upstream commit ebd19fc372e3e78bf165f230e7c084e304441c08 ]

This change switches rapl to use PMU_FORMAT_ATTR, and fixes two other
macros to use device_attribute instead of kobj_attribute to avoid
callback type mismatches that trip indirect call checking with Clang's
Control-Flow Integrity (CFI).

Reported-by: Sedat Dilek <sedat.dilek@gmail.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Link: https://lkml.kernel.org/r/20201113183126.1239404-1-samitolvanen@google.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
2020-12-02 08:48:10 +01:00
Sami Tolvanen 442c302e2e ANDROID: vmlinux.lds.h: merge compound literal sections
After LLVM rG9e33c096476a ("[ELF] Keep orphan section names
(.rodata.foo .text.foo) unchanged if !hasSectionsCommand"), LLD splits
compound literals to separate sections with -fdata-sections, which
it always enables with LTO. Merge these sections to allow LLVM to be
upgraded.

Bug: 174047799
Change-Id: I858c5fcc48283d5528c1d3e2b2d7a3c72f9e03d6
Link: https://github.com/ClangBuiltLinux/linux/issues/958
Suggested-by: Danny Lin <danny@kdrag0n.dev>
Suggested-by: Fangrui Song <maskray@google.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2020-11-30 21:58:23 +00:00
Sami Tolvanen b588efc6c0 BACKPORT: recordmcount: support >64k sections
When compiling a kernel with Clang and LTO, we need to run
recordmcount on vmlinux.o with a large number of sections, which
currently fails as the program doesn't understand extended
section indexes. This change adds support for processing binaries
with >64k sections.

Link: https://lkml.kernel.org/r/20200424193046.160744-1-samitolvanen@google.com
Link: https://lore.kernel.org/lkml/CAK7LNARbZhoaA=Nnuw0=gBrkuKbr_4Ng_Ei57uafujZf7Xazgw@mail.gmail.com/

Bug: 166522612
Cc: Kees Cook <keescook@chromium.org>
Reviewed-by: Matt Helsley <mhelsley@vmware.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
(cherry picked from commit 4ef57b21d6fb49d2b25c47e4cff467a0c2c8b6b7)
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Change-Id: I39acdf51ca882020bbacd5c54299d2e2e6f02ed3
2020-08-27 10:54:55 -07:00
Sami Tolvanen 53f941777b arm64/alternatives: move length validation inside the subsection
[ Upstream commit 966a0acce2fca776391823381dba95c40e03c339 ]

Commit f7b93d42945c ("arm64/alternatives: use subsections for replacement
sequences") breaks LLVM's integrated assembler, because due to its
one-pass design, it cannot compute instruction sequence lengths before the
layout for the subsection has been finalized. This change fixes the build
by moving the .org directives inside the subsection, so they are processed
after the subsection layout is known.

Fixes: f7b93d42945c ("arm64/alternatives: use subsections for replacement sequences")
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Link: https://github.com/ClangBuiltLinux/linux/issues/1078
Link: https://lore.kernel.org/r/20200730153701.3892953-1-samitolvanen@google.com
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2020-08-05 10:06:04 +02:00
Sami Tolvanen e9cd5b2daa ANDROID: kbuild: don't merge .*..compoundliteral in modules
Bug: 160394326
Change-Id: Ieac583c1b6a6ef312afb0cdd34748a28debe0b8b
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2020-07-23 21:26:11 +00:00
Sami Tolvanen 23cbf245be ANDROID: kbuild: merge more sections with LTO
Merge the following new data sections generated by current
LLVM versions:

  .bss..L* .bss..compoundliteral*
  .data..L* .data..compoundliteral*
  .rodata..L* .rodata..compoundliteral*

Also drop the unnecessary rules to merge .rela sections.

Bug: 158605670
Change-Id: I098212bc74dfb6b54d4fd60d90957fea29122a6c
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2020-06-15 15:49:45 +00:00
Sami Tolvanen f1de33a119 ANDROID: x86/vdso: disable LTO only for VDSO
Instead of disabling LTO for all the code in the directory, only
disable it for the VDSO itself.

Bug: 145297900
Change-Id: I95a9c68ee76fdc8f4cee5c5bd1356fc90de1680c
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
(cherry picked from commit 74d07ccc220bb807a590c7a260fa8f388ccd2af7)
Signed-off-by: Mark Salyzyn <salyzyn@google.com>
Bug: 154668398
2020-04-27 22:52:01 -07:00
Sami Tolvanen 166a538d15 ANDROID: kbuild: ensure __cfi_check is correctly aligned
On modules with no executable code, LLVM generates a __cfi_check stub,
but won't align it to page size as expected. This change ensures the
function is at the beginning of the .text section and correctly aligned
for the CFI shadow.

Bug: 148458318
Change-Id: I85ea31fa851bc23988f649b021b3ac7e9d9dcb38
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2020-04-06 20:34:01 +00:00
Sami Tolvanen 93741b1a30 ANDROID: kbuild: do not merge .section..* into .section in modules
Sections with double dots, e.g. .data..percpu are named intentionally
to avoid matching rules that apply to .section.*. Change module section
merging rules to skip these.

Bug: 151981957
Change-Id: I23787aa40d69da1c6ca622a5f111704d2459e163
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2020-03-25 04:09:25 +00:00
Sami Tolvanen 68fc81859b ANDROID: kbuild: fix module linker script flags for LTO
KBUILD_LDS_MODULE didn't exist in 4.19, it was added in upstream commit
10df06385582 ("kbuild: rebuild modules when module linker scripts are
updated"), which means the module-lto.lds linker script is not actually
passed to the linker. Append the flags directly to KBUILD_LDFLAGS_MODULE
instead.

Bug: 151700304
Fixes: 6cea04778e ("ANDROID: kbuild: merge module sections with LTO")
Change-Id: I600db54d2ff9cd4e287913e8ddd463a20741a4a3
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2020-03-17 14:37:44 -07:00
Sami Tolvanen 998f333177 ANDROID: kbuild: avoid excessively long argument lists
With LTO, modules with a large number of compilation units maybe end
up exceeding the for loop argument list in the shell. Reduce the
probability for this happening by including only the modules that have
exported symbols.

Bug: 150234396
Change-Id: I4a289aff47e1444aca28d1bd00b125628f39bcd5
Suggested-by: Hsiu-Chang Chen <hsiuchangchen@google.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2020-03-17 14:37:38 -07:00
Sami Tolvanen 8028f78053 ANDROID: Disable wq fp check in CFI builds
With non-canonical CFI, LLVM generates jump table entries for external
symbols in modules and as a result, a function pointer passed from a
module to the core kernel will have a different address.

Disable the warning for now.

Bug: 145210207
Change-Id: Ifdcee3479280f7b97abdee6b4c746f447e0944e6
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Alistair Delva <adelva@google.com>
2020-02-27 00:09:59 +00:00
Sami Tolvanen 7cdb44ab26 arm64: fix alternatives with LLVM's integrated assembler
[ Upstream commit c54f90c2627cc316d365e3073614731e17dbc631 ]

LLVM's integrated assembler fails with the following error when
building KVM:

  <inline asm>:12:6: error: expected absolute expression
   .if kvm_update_va_mask == 0
       ^
  <inline asm>:21:6: error: expected absolute expression
   .if kvm_update_va_mask == 0
       ^
  <inline asm>:24:2: error: unrecognized instruction mnemonic
          NOT_AN_INSTRUCTION
          ^
  LLVM ERROR: Error parsing inline asm

These errors come from ALTERNATIVE_CB and __ALTERNATIVE_CFG,
which test for the existence of the callback parameter in inline
assembly using the following expression:

  " .if " __stringify(cb) " == 0\n"

This works with GNU as, but isn't supported by LLVM. This change
splits __ALTERNATIVE_CFG and ALTINSTR_ENTRY into separate macros
to fix the LLVM build.

Link: https://github.com/ClangBuiltLinux/linux/issues/472
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Tested-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2020-02-24 08:34:48 +01:00
Sami Tolvanen 4596eee0c8 ANDROID: kallsyms: strip hashes from function names with ThinLTO
With CONFIG_THINLTO and CFI both enabled, LLVM appends a hash to the
names of all static functions. This breaks userspace tools, so strip
out the hash from output.

Bug: 147422318
Change-Id: Ibea6be089d530e92dcd191481cb02549041203f6
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2020-01-31 16:50:06 +00:00
Sami Tolvanen 2a2a01cd99 ANDROID: x86: gki_defconfig: enable LTO and CFI
Bug: 145297900
Change-Id: I9d7d5d158cd28f39808b9f60d391ae7d149e8099
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2020-01-24 21:16:27 +00:00
Sami Tolvanen b54083c5cb ANDROID: x86: map CFI jump tables in pti_clone_entry_text
Allow CFI enabled entry code to make indirect calls by also mapping
CFI jump tables, and add a check to ensure the jump table section is
not empty.

Bug: 145297900
Change-Id: I1204c50a139ba62234f3bb4699c50921a831162b
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2020-01-24 21:16:24 +00:00
Sami Tolvanen 10d14862f3 ANDROID: BACKPORT: x86, module: Ignore __typeid__ relocations
Also ignore these relocations when loading modules.

Bug: 145297900
Change-Id: I3d00f93deb3cf370ce25f8672f90e410e87ab79e
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2020-01-24 21:16:20 +00:00
Sami Tolvanen 81c3dc6324 x86/mm: Use the correct function type for native_set_fixmap()
[ Upstream commit f53e2cd0b8ab7d9e390414470bdbd830f660133f ]

We call native_set_fixmap indirectly through the function pointer
struct pv_mmu_ops::set_fixmap, which expects the first parameter to be
'unsigned' instead of 'enum fixed_addresses'. This patch changes the
function type for native_set_fixmap to match the pointer, which fixes
indirect call mismatches with Control-Flow Integrity (CFI) checking.

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: H . Peter Anvin <hpa@zytor.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Rik van Riel <riel@surriel.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: https://lkml.kernel.org/r/20190913211402.193018-1-samitolvanen@google.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2019-12-31 16:35:12 +01:00
Sami Tolvanen 0aeb6588f2 syscalls/x86: Use the correct function type in SYSCALL_DEFINE0
[ Upstream commit 8661d769ab77c675b5eb6c3351a372b9fbc1bf40 ]

Although a syscall defined using SYSCALL_DEFINE0 doesn't accept
parameters, use the correct function type to avoid type mismatches
with Control-Flow Integrity (CFI) checking.

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Acked-by: Andy Lutomirski <luto@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: H . Peter Anvin <hpa@zytor.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: https://lkml.kernel.org/r/20191008224049.115427-2-samitolvanen@google.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2019-12-31 16:35:09 +01:00
Sami Tolvanen 113c590555 ANDROID: update abi_gki_aarch64.xml for LTO, CFI, and SCS
Leaf changes summary: 20 artifacts changed
Changed leaf types summary: 8 leaf types changed
Removed/Changed/Added functions summary: 0 Removed, 3 Changed, 5 Added functions
Removed/Changed/Added variables summary: 0 Removed, 0 Changed, 1 Added variable

3 functions with some sub-type change:

5 Added functions:

  [A] 'function void __cfi_slowpath(uint64_t, void*, void*)'
  [A] 'function void __ubsan_handle_cfi_check_fail_abort(void*, void*, void*)'
  [A] 'function bool arch_bpf_jit_check_func(const bpf_prog*)'
  [A] 'function void cfi_module_add(module*, unsigned long int, unsigned long int)'
  [A] 'function void cfi_module_remove(module*, unsigned long int, unsigned long int)'

'enum zone_stat_item at mmzone.h:132:1' changed:
  type size hasn't changed
  1 enumerator insertion:
    'zone_stat_item::NR_KERNEL_SCS_BYTES' value '10'

  4 enumerator changes:
    'zone_stat_item::NR_BOUNCE' from value '10' to '11' at mmzone.h:132:1
    'zone_stat_item::NR_ZSPAGES' from value '11' to '12' at mmzone.h:132:1
    'zone_stat_item::NR_FREE_CMA_PAGES' from value '12' to '13' at mmzone.h:132:1
    'zone_stat_item::NR_VM_ZONE_STAT_ITEMS' from value '13' to '14' at mmzone.h:132:1

  6 impacted interfaces

'struct kernel_symbol at export.h:61:1' changed:
  type size changed from 64 to 128 (in bits)
  1 data member deletion:
    'int kernel_symbol::name_offset', at offset 32 (in bits) at export.h:63:1

  1 data member insertion:
    'const char* kernel_symbol::name', at offset 64 (in bits) at export.h:73:1
  there are data member changes:
   type 'int' of 'kernel_symbol::value_offset' changed:
     type name changed from 'int' to 'unsigned long int'
     type size changed from 32 to 64 (in bits)
   name of 'kernel_symbol::value_offset' changed to 'kernel_symbol::value' at export.h:72:1, size changed from 32 to 64 (in bits) (by +32 bits)

  6889 impacted interfaces

'struct module at module.h:331:1' changed:
  type size hasn't changed
  1 data member insertion:
    'cfi_check_fn module::cfi_check', at offset 1856 (in bits) at module.h:353:1
  there are data member changes:
   'mutex module::param_lock' offset changed from 1856 to 1920 (in bits) (by +64 bits)
   'kernel_param* module::kp' offset changed from 2112 to 2176 (in bits) (by +64 bits)
   'unsigned int module::num_kp' offset changed from 2176 to 2240 (in bits) (by +64 bits)
   'unsigned int module::num_gpl_syms' offset changed from 2208 to 2272 (in bits) (by +64 bits)
   'const kernel_symbol* module::gpl_syms' offset changed from 2240 to 2304 (in bits) (by +64 bits)
   'const s32* module::gpl_crcs' offset changed from 2304 to 2368 (in bits) (by +64 bits)
   'bool module::sig_ok' offset changed from 2368 to 2432 (in bits) (by +64 bits)
   'bool module::async_probe_requested' offset changed from 2376 to 2440 (in bits) (by +64 bits)
   'const kernel_symbol* module::gpl_future_syms' offset changed from 2432 to 2496 (in bits) (by +64 bits)
   'const s32* module::gpl_future_crcs' offset changed from 2496 to 2560 (in bits) (by +64 bits)
   'unsigned int module::num_gpl_future_syms' offset changed from 2560 to 2624 (in bits) (by +64 bits)
   'unsigned int module::num_exentries' offset changed from 2592 to 2656 (in bits) (by +64 bits)
   'exception_table_entry* module::extable' offset changed from 2624 to 2688 (in bits) (by +64 bits)
   'void ()* module::init' offset changed from 2688 to 2752 (in bits) (by +64 bits)
   type 'tracepoint_ptr_t*' of 'module::tracepoints_ptrs' changed:
     pointer type changed from: 'tracepoint_ptr_t*' to: 'tracepoint_ptr_t*'

  6889 impacted interfaces

'struct per_cpu_pageset at mmzone.h:287:1' changed:
  type size hasn't changed
  there are data member changes:
   type 's8[13]' of 'per_cpu_pageset::vm_stat_diff' changed:
     type name changed from 's8[13]' to 's8[14]'
     array type size changed from 104 to 112
     array type subrange 1 changed length from 13 to 14
, size changed from 104 to 112 (in bits) (by +8 bits)

  778 impacted interfaces

'struct pglist_data at mmzone.h:628:1' changed:
  type size hasn't changed
  there are data member changes:

  778 impacted interfaces

'struct task_struct at sched.h:584:1' changed:
  type size hasn't changed
  there are data member changes:
   type 'struct thread_info' of 'task_struct::thread_info' changed:
     type size changed from 192 to 256 (in bits)
     1 data member insertion:
       'void* thread_info::shadow_call_stack', at offset 192 (in bits) at thread_info.h:47:1

     7192 impacted interfaces

   'volatile long int task_struct::state' offset changed from 192 to 256 (in bits) (by +64 bits)
   'void* task_struct::stack' offset changed from 256 to 320 (in bits) (by +64 bits)
   'atomic_t task_struct::usage' offset changed from 320 to 384 (in bits) (by +64 bits)
   'unsigned int task_struct::flags' offset changed from 352 to 416 (in bits) (by +64 bits)
   'unsigned int task_struct::ptrace' offset changed from 384 to 448 (in bits) (by +64 bits)
   'llist_node task_struct::wake_entry' offset changed from 448 to 512 (in bits) (by +64 bits)
   'int task_struct::on_cpu' offset changed from 512 to 576 (in bits) (by +64 bits)
   'unsigned int task_struct::cpu' offset changed from 544 to 608 (in bits) (by +64 bits)
   'unsigned int task_struct::wakee_flips' offset changed from 576 to 640 (in bits) (by +64 bits)
   'unsigned long int task_struct::wakee_flip_decay_ts' offset changed from 640 to 704 (in bits) (by +64 bits)
   'task_struct* task_struct::last_wakee' offset changed from 704 to 768 (in bits) (by +64 bits)
   'int task_struct::recent_used_cpu' offset changed from 768 to 832 (in bits) (by +64 bits)
   'int task_struct::wake_cpu' offset changed from 800 to 864 (in bits) (by +64 bits)
   'int task_struct::on_rq' offset changed from 832 to 896 (in bits) (by +64 bits)
   'int task_struct::prio' offset changed from 864 to 928 (in bits) (by +64 bits)
   'int task_struct::static_prio' offset changed from 896 to 960 (in bits) (by +64 bits)
   'int task_struct::normal_prio' offset changed from 928 to 992 (in bits) (by +64 bits)
   'unsigned int task_struct::rt_priority' offset changed from 960 to 1024 (in bits) (by +64 bits)
   'const sched_class* task_struct::sched_class' offset changed from 1024 to 1088 (in bits) (by +64 bits)

  7192 impacted interfaces

'struct thread_info at thread_info.h:39:1' changed:
  details were reported earlier

'struct zone at mmzone.h:363:1' changed:
  type size hasn't changed
  there are data member changes:
   type 'atomic_long_t[13]' of 'zone::vm_stat' changed:
     type name changed from 'atomic_long_t[13]' to 'atomic_long_t[14]'
     array type size changed from 832 to 896
     array type subrange 1 changed length from 13 to 14
, size changed from 832 to 896 (in bits) (by +64 bits)
   'atomic_long_t zone::vm_numa_stat[]' offset changed from 12096 to 12160 (in bits) (by +64 bits)

  778 impacted interfaces

Bug: 145210207
Change-Id: I5d573192b5ac192f342fd0a15bdb0dc467f756bb
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-12-12 06:26:51 -08:00
Sami Tolvanen 960a83a227 ANDROID: gki_defconfig: enable LTO, CFI, and SCS
Bug: 145210207
Change-Id: I23ea978983b1141add2580994e707903d8eaa531
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-12-12 06:14:10 -08:00
Sami Tolvanen 2ee80bf76d ANDROID: kbuild: limit LTO inlining
This change limits function inlining across translation unit
boundaries in order to reduce the binary size with LTO.

The -import-instr-limit flag defines a size limit, as the number
of LLVM IR instructions, for importing functions from other TUs.
The default value is 100, and decreasing it to 5 reduces the size
of a stripped arm64 defconfig vmlinux by 11%.

Bug: 145297228
Change-Id: Iaf366f843582972a5dfadc4695abb8f9c59882af
Suggested-by: George Burgess IV <gbiv@google.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-12-10 10:03:53 -08:00
Sami Tolvanen 6cea04778e ANDROID: kbuild: merge module sections with LTO
LLD always splits sections with LTO, which increases module sizes. This
change adds a linker script that merges the split sections in the final
module.

Bug: 145297228
Change-Id: I247e8bd029bd0f98a4fa1cd4db7f6398467b8e55
Suggested-by: Nick Desaulniers <ndesaulniers@google.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-12-10 10:03:41 -08:00
Sami Tolvanen f0723e3bd5 ANDROID: netfilter: nf_nat: remove static from nf_nat_ipv4_fn
Exporting a static function breaks the ABI toolchain when CFI is
enabled. Remove the static identifier to fix the issue.

Bug: 145210207
Change-Id: Ib23d594ae05718dcf119b8ed0b5a8251aee2561c
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-12-10 10:02:54 -08:00
Sami Tolvanen 332eaac720 FROMLIST: arm64: implement Shadow Call Stack
This change implements shadow stack switching, initial SCS set-up,
and interrupt shadow stacks for arm64.

Bug: 145210207
Change-Id: I3d5b9ec374418b110d1f351e1abd41610cfee597
(am from https://lore.kernel.org/patchwork/patch/1149062/)
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:28 -08:00
Sami Tolvanen fb3573ae71 FROMLIST: arm64: disable SCS for hypervisor code
Filter out CC_FLAGS_SCS for code that runs at a different exception
level.

Bug: 145210207
Change-Id: Ic93a7333920830f0934c8dd5530082f0ca941777
(am from https://lore.kernel.org/patchwork/patch/1149062/)
Suggested-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:28 -08:00
Sami Tolvanen f49ee5bd17 BACKPORT: FROMLIST: arm64: vdso: disable Shadow Call Stack
Shadow stacks are only available in the kernel, so disable SCS
instrumentation for the vDSO.

Bug: 145210207
Change-Id: I6e01b2c7788ba52d3b754b1fbd5bfb908b45741b
(am from https://lore.kernel.org/patchwork/patch/1149061/)
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:27 -08:00
Sami Tolvanen 947e357d12 FROMLIST: arm64: efi: restore x18 if it was corrupted
If we detect a corrupted x18 and SCS is enabled, restore the register
before jumping back to instrumented code. This is safe, because the
wrapper is called with preemption disabled and a separate shadow stack
is used for interrupt handling.

Bug: 145210207
Change-Id: Ie83578cef27e1656a234d1500581eaf9de6c2365
(am from https://lore.kernel.org/patchwork/patch/1149060/)
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:27 -08:00
Sami Tolvanen b3953e156c FROMLIST: arm64: preserve x18 when CPU is suspended
Don't lose the current task's shadow stack when the CPU is suspended.

Bug: 145210207
Change-Id: I8f74f33b4b2c707e25e46bea4fb8a8f5ea1a7036
(am from https://lore.kernel.org/patchwork/patch/1149059/)
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:27 -08:00
Sami Tolvanen 186d678828 FROMLIST: arm64: reserve x18 from general allocation with SCS
Reserve the x18 register from general allocation when SCS is enabled,
because the compiler uses the register to store the current task's
shadow stack pointer. Note that all external kernel modules must also be
compiled with -ffixed-x18 if the kernel has SCS enabled.

Bug: 145210207
Change-Id: If2315ace9a879b3dd2a85f6ba43eddadc4430595
(am from https://lore.kernel.org/patchwork/patch/1149058/)
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:26 -08:00
Sami Tolvanen cccc415386 FROMLIST: arm64: disable function graph tracing with SCS
The graph tracer hooks returns by modifying frame records on the
(regular) stack, but with SCS the return address is taken from the
shadow stack, and the value in the frame record has no effect. As we
don't currently have a mechanism to determine the corresponding slot
on the shadow stack (and to pass this through the ftrace
infrastructure), for now let's disable the graph tracer when SCS is
enabled.

Bug: 145210207
Change-Id: I6fdca3eee60bb8594401920a420cd3c1e23cabce
(am from https://lore.kernel.org/patchwork/patch/1149057/)
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:26 -08:00
Sami Tolvanen 32518aee30 FROMLIST: scs: add support for stack usage debugging
Implements CONFIG_DEBUG_STACK_USAGE for shadow stacks. When enabled,
also prints out the highest shadow stack usage per process.

Bug: 145210207
Change-Id: I4c085b51e1432e8d52e54126ffd8bf7b6e35b529
(am from https://lore.kernel.org/patchwork/patch/1149056/)
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:25 -08:00
Sami Tolvanen 9c265248fa FROMLIST: scs: add accounting
This change adds accounting for the memory allocated for shadow stacks.

Bug: 145210207
Change-Id: I51157fe0b23b4cb28bb33c86a5dfe3ac911296a4
(am from https://lore.kernel.org/patchwork/patch/1149055/)
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:25 -08:00
Sami Tolvanen 16e13c60ff FROMLIST: add support for Clang's Shadow Call Stack (SCS)
This change adds generic support for Clang's Shadow Call Stack,
which uses a shadow stack to protect return addresses from being
overwritten by an attacker. Details are available here:

  https://clang.llvm.org/docs/ShadowCallStack.html

Note that security guarantees in the kernel differ from the
ones documented for user space. The kernel must store addresses
of shadow stacks used by other tasks and interrupt handlers in
memory, which means an attacker capable reading and writing
arbitrary memory may be able to locate them and hijack control
flow by modifying shadow stacks that are not currently in use.

Bug: 145210207
Change-Id: Ia5f1650593fa95da4efcf86f84830a20989f161c
(am from https://lore.kernel.org/patchwork/patch/1149054/)
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:25 -08:00
Sami Tolvanen f7da281b94 FROMLIST: arm64: mm: avoid x18 in idmap_kpti_install_ng_mappings
idmap_kpti_install_ng_mappings uses x18 as a temporary register, which
will result in a conflict when x18 is reserved. Use x16 and x17 instead
where needed.

Bug: 145210207
Change-Id: I57cc0a9f242bd66e9ed6cb4f251ad951b63aaae8
(am from https://lore.kernel.org/patchwork/patch/1149052/)
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:23 -08:00
Sami Tolvanen 9f8f79abe8 ANDROID: use non-canonical CFI jump tables
Bug: 145210207
Change-Id: I8f59ab53c94087d1e9ec0154337486ad8aad973f
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:23 -08:00
Sami Tolvanen 08f2c5a5a9 ANDROID: arm64: add __nocfi to __apply_alternatives
__apply_alternatives makes indirect calls to functions whose address is
taken in assembly code using the alternative_cb macro. With CFI enabled
using non-canonical jump tables, the compiler isn't able to replace the
function reference with the jump table reference, which trips CFI.

Bug: 145210207
Change-Id: I088faf94b64ac3068ac798dd8e5029cb2ab92903
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:22 -08:00
Sami Tolvanen a8ef2a2913 ANDROID: arm64: add __pa_function
We use non-canonical CFI jump tables with CONFIG_CFI_CLANG, which
means the compiler replaces function address references with the
address of the function's CFI jump table entry. This results in
__pa_symbol(function) returning the physical address of the jump
table entry, which can lead to address space confusion since the
jump table points to a virtual address.

This change adds a __pa_function macro, which uses inline assembly
to take the actual function address instead.

Bug: 145210207
Change-Id: I14995e522365ad09a5c9bd676e1203b2b642cd5a
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:22 -08:00
Sami Tolvanen f447a38390 ANDROID: arm64: allow ThinLTO to be selected
Bug: 145210207
Change-Id: I1284cc32f70c2332ef9908e7b070c69476b1e5d0
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:21 -08:00
Sami Tolvanen adb6f21133 ANDROID: soc/tegra: disable ARCH_TEGRA_210_SOC with LTO
Disable CONFIG_ARCH_TEGRA_210_SOC with LTO to work around an issue
with ThinLTO.

Bug: 145210207
Change-Id: Idc2792d6b7d0c755f6f7695cf0b20fa49721d09c
Link: https://github.com/ClangBuiltLinux/linux/issues/510
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:21 -08:00
Sami Tolvanen b39dbed4c0 FROMLIST: arm64: fix alternatives with LLVM's integrated assembler
LLVM's integrated assembler fails with the following error when
building KVM:

  <inline asm>:12:6: error: expected absolute expression
   .if kvm_update_va_mask == 0
       ^
  <inline asm>:21:6: error: expected absolute expression
   .if kvm_update_va_mask == 0
       ^
  <inline asm>:24:2: error: unrecognized instruction mnemonic
          NOT_AN_INSTRUCTION
          ^
  LLVM ERROR: Error parsing inline asm

These errors come from ALTERNATIVE_CB and __ALTERNATIVE_CFG,
which test for the existence of the callback parameter in inline
assembly using the following expression:

  " .if " __stringify(cb) " == 0\n"

This works with GNU as, but isn't supported by LLVM. This change
splits __ALTERNATIVE_CFG and ALTINSTR_ENTRY into separate macros
to fix the LLVM build.

Bug: 145210207
Change-Id: I62dd786277bcaf3c730a7489c4b5a2b437f722ce
(am from https://lore.kernel.org/patchwork/patch/1136803/)
Link: https://github.com/ClangBuiltLinux/linux/issues/472
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:20 -08:00
Sami Tolvanen 1092197cae ANDROID: irqchip/gic-v3: rename gic_of_init to work around a ThinLTO+CFI bug
Bug: 145210207
Link: https://github.com/ClangBuiltLinux/linux/issues/537
Change-Id: Ibe51a1f531625fde4a44cf92f89b1f9ac41b4c68
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:20 -08:00
Sami Tolvanen 93185a9155 ANDROID: init: ensure initcall ordering with LTO
With LTO, the compiler doesn't necessarily obey link order for
initcalls, and the initcall variable needs to be globally unique
to avoid naming collisions.

In order to preserve the correct order, we add each variable
into its own section and generate a linker script (in
scripts/link-vmlinux.sh) to ensure the order remains correct.  We
also add a __COUNTER__ prefix to the name, so we can retain the
order of initcalls within each compilation unit, and __LINE__ to
make the names more unique.

Bug: 145210207
Change-Id: Iddda881a52b7942781713b188d810b6100159a2b
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:20 -08:00
Sami Tolvanen 8bcac896fa Revert "ANDROID: init: ensure initcall ordering with LTO"
This reverts commit 9730a6289c.

This is change not sufficient for ThinLTO, it's replaced by
  Iddda881a52b7942781713b188d810b6100159a2b

Bug: 145210207
Change-Id: Ie67f90a19bc78dbf8b61f13dcc441f0f58c60444
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:19 -08:00
Sami Tolvanen a15c6ebb17 ANDROID: add support for ThinLTO
This change adds support for ThinLTO, which greatly improves build times
over full LTO while retaining most of the performance benefits:

  https://clang.llvm.org/docs/ThinLTO.html

Bug: 145210207
Change-Id: I8bfc19028266077be2bc1fb5c2bc001b599d3214
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-27 12:37:19 -08:00
Sami Tolvanen 06604e8cef ANDROID: gki_defconfig: disable FUNCTION_TRACER
Remove CONFIG_FUNCTION_TRACER as it's not needed. Note that
CONFIG_FTRACE is still enabled by default.

Change-Id: Iaf1452937c72325871c1a49b85ba37b4bf136d2e
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-11-21 14:17:22 -08:00
Sami Tolvanen cfd89c3d47 ANDROID: arm64: bpf: implement arch_bpf_jit_check_func
Implement arch_bpf_jit_check_func to check that pointers to jited BPF
functions are correctly aligned and point to the BPF JIT region. This
narrows down the attack surface on the stored pointer.

Bug: 140377409
Change-Id: I10c448eda6a8b0bf4c16ee591fc65974696216b9
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
(cherry picked from commit c10baf7606)
2019-10-08 09:21:05 -07:00
Sami Tolvanen 89aed473c5 ANDROID: bpf: validate bpf_func when BPF_JIT is enabled with CFI
With CONFIG_BPF_JIT, the kernel makes indirect calls to dynamically
generated code, which the compile-time Control-Flow Integrity (CFI)
checking cannot validate. This change adds basic sanity checking to
ensure we are jumping to a valid location, which narrows down the
attack surface on the stored pointer.

In addition, this change adds a weak arch_bpf_jit_check_func function,
which architectures that implement BPF JIT can override to perform
additional validation, such as verifying that the pointer points to
the correct memory region.

Bug: 140377409
Change-Id: I8ebac6637ab6bd9db44716b1c742add267298669
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
(cherry picked from commit 9a11e8da57)
2019-10-08 09:21:03 -07:00
Sami Tolvanen ccddca0ef3 UPSTREAM: kcm: use BPF_PROG_RUN
Instead of invoking struct bpf_prog::bpf_func directly, use the
BPF_PROG_RUN macro.

Bug: 140377409
Change-Id: I26abeccc8d25af0f412935ed97aebb5c64f52a2a
(cherry picked from commit a2c11b034142 ("kcm: use BPF_PROG_RUN"))
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
(cherry picked from commit 59c019fdd1)
2019-10-08 09:21:02 -07:00
Sami Tolvanen c10baf7606 ANDROID: arm64: bpf: implement arch_bpf_jit_check_func
Implement arch_bpf_jit_check_func to check that pointers to jited BPF
functions are correctly aligned and point to the BPF JIT region. This
narrows down the attack surface on the stored pointer.

Bug: 140377409
Change-Id: I10c448eda6a8b0bf4c16ee591fc65974696216b9
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-09-20 10:58:37 -07:00
Sami Tolvanen 9a11e8da57 ANDROID: bpf: validate bpf_func when BPF_JIT is enabled with CFI
With CONFIG_BPF_JIT, the kernel makes indirect calls to dynamically
generated code, which the compile-time Control-Flow Integrity (CFI)
checking cannot validate. This change adds basic sanity checking to
ensure we are jumping to a valid location, which narrows down the
attack surface on the stored pointer.

In addition, this change adds a weak arch_bpf_jit_check_func function,
which architectures that implement BPF JIT can override to perform
additional validation, such as verifying that the pointer points to
the correct memory region.

Bug: 140377409
Change-Id: I8ebac6637ab6bd9db44716b1c742add267298669
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-09-20 10:58:37 -07:00
Sami Tolvanen 59c019fdd1 UPSTREAM: kcm: use BPF_PROG_RUN
Instead of invoking struct bpf_prog::bpf_func directly, use the
BPF_PROG_RUN macro.

Bug: 140377409
Change-Id: I26abeccc8d25af0f412935ed97aebb5c64f52a2a
(cherry picked from commit a2c11b034142 ("kcm: use BPF_PROG_RUN"))
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-09-20 10:58:37 -07:00
Sami Tolvanen fa8bbd2c07 ANDROID: arm64: fix function types in COND_SYSCALL
Define a weak function for COND_SYSCALL instead of creating a weak
alias to a function with an incompatible type. This fixes indirect
call mismatches with Control-Flow Integrity.

Bug: 138254717
Change-Id: I3a9a8162f30ff63cea2002304f70152b7cc8f746
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-07-25 09:36:55 -07:00
Sami Tolvanen 1693025140 ANDROID: x86: fix function types in COND_SYSCALL
Define a weak function for COND_SYSCALL instead of creating a weak
alias to a function with an incompatible type. This fixes indirect
call mismatches with Control-Flow Integrity.

Bug: 138254717
Change-Id: I3effbbf5c86107103cf29a40dfb37b268e59bfd8
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-07-25 09:36:45 -07:00
Sami Tolvanen 9a8a0e6fd9 ANDROID: kernel/Makefile: do not disable LTO for sys_ni.c with CFI
sys_ni.c compiles fine with CONFIG_LTO_CLANG, and disabling LTO
for it breaks indirect call checking to functions in this file.

Bug: 138254717
Change-Id: I7947cf3d0283ad37431860739665fee7fb0dfbdb
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-07-25 09:36:26 -07:00
Sami Tolvanen b313283a68 ANDROID: drivers/misc: disable LTO for rodata.o
LKDTM was refactored in 4.17 and lkdtm_rodata.o was moved to
lkdtm/rodata.o. Fix CFLAGS to apply to the correct object file,
so LKDTM can be built with LTO.

Bug: 133186739
Change-Id: I906db4aecabf42dec905a8b03933689732ab5b52
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-07-02 09:05:00 -07:00
Sami Tolvanen 467f902643 arm64: use the correct function type for __arm64_sys_ni_syscall
[ Upstream commit 1e29ab3186e33c77dbb2d7566172a205b59fa390 ]

Calling sys_ni_syscall through a syscall_fn_t pointer trips indirect
call Control-Flow Integrity checking due to a function type
mismatch. Use SYSCALL_DEFINE0 for __arm64_sys_ni_syscall instead and
remove the now unnecessary casts.

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2019-06-22 08:15:20 +02:00
Sami Tolvanen 98fd62e0a1 arm64: use the correct function type in SYSCALL_DEFINE0
[ Upstream commit 0e358bd7b7ebd27e491dabed938eae254c17fe3b ]

Although a syscall defined using SYSCALL_DEFINE0 doesn't accept
parameters, use the correct function type to avoid indirect call
type mismatches with Control-Flow Integrity checking.

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2019-06-22 08:15:20 +02:00
Sami Tolvanen c5fdfaedec arm64: fix syscall_fn_t type
[ Upstream commit 8ef8f368ce72b5e17f7c1f1ef15c38dcfd0fef64 ]

Syscall wrappers in <asm/syscall_wrapper.h> use const struct pt_regs *
as the argument type. Use const in syscall_fn_t as well to fix indirect
call type mismatches with Control-Flow Integrity checking.

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Reviewed-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2019-06-22 08:15:20 +02:00
Sami Tolvanen c2409e87d4 ANDROID: x86: use the correct function type for sys_ni_syscall
Change-Id: I0d43dc716fad3b987e7e29357dbf84e26c753367
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen 48d96dd414 ANDROID: x86: use the correct function type for sys32_(rt_)sigreturn
Use the correct function type to avoid tripping Control-Flow
Integrity checking.

Change-Id: I6a145ba075f8902a5883e6374328643b3771b1eb
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen 64d4304a03 ANDROID: x86: use the correct function type for native_set_fixmap
Change-Id: Iae48f393231e53f390bb0bfd5ebbe4bb3c0fd463
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen 7580052e7d ANDROID: x86: use the correct function type in SYSCALL_DEFINE0
Although a syscall defined using SYSCALL_DEFINE0 doesn't accept
parameters, use the correct function type to avoid type mismatches
with Control-Flow Integrity checking.

Change-Id: I246d27e4418695231979f4f9a6d98c8eb6776167
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen 205cde7487 ANDROID: x86: add support for CONFIG_LTO_CLANG
Change-Id: I8e7a8976f9381233f28badc324145b9c4c7eacb0
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen ebf068b841 ANDROID: x86: disable STACK_VALIDATION with LTO_CLANG
Disable CONFIG_STACK_VALIDATION with LTO, because objtool doesn't
understand LLVM IR.

Change-Id: I3f2bd3f0ce6c80f88bd00394453b543f884188f2
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen c8a8d35fed ANDROID: x86: disable HAVE_ARCH_PREL32_RELOCATIONS with LTO_CLANG
Disable HAVE_ARCH_PREL32_RELOCATIONS to stop LLVM from reordering
initcalls.

Change-Id: Ie8f58b53fe8dccb961573f3aa80ab6d8aae16f4d
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen 48bb8b06fa ANDROID: x86/vdso: disable LTO only for VDSO
Instead of disabling LTO for all the code in the directory, only
disable it for the VDSO itself.

Change-Id: I02e34b75e022982f9884e238aec89486890dd4bb
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen b9c6204d81 ANDROID: x86/cpu/vmware: use the full form of inl in VMWARE_PORT
LLVM's assembler doesn't accept the short form inl (%%dx) instruction,
but instead insists on the output register to be explicitly specified:

  <inline asm>:1:7: error: invalid operand for instruction
          inl (%dx)
             ^
  LLVM ERROR: Error parsing inline asm

Change-Id: I0519034f4a66bd72f23d206d4638578836a49ff5
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen 4886843fad UPSTREAM: x86/build: Keep local relocations with ld.lld
The LLVM linker (ld.lld) defaults to removing local relocations, which
causes KASLR boot failures. ld.bfd and ld.gold already handle this
correctly. This adds the explicit instruction "--discard-none" during
the link phase. There is no change in output for ld.bfd and ld.gold,
but ld.lld now produces an image with all the needed relocations.

Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Borislav Petkov <bp@suse.de>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: clang-built-linux@googlegroups.com
Cc: x86-ml <x86@kernel.org>
Link: https://lkml.kernel.org/r/20190404214027.GA7324@beast
Link: https://github.com/ClangBuiltLinux/linux/issues/404
Change-Id: If16af606aee95770bee98fc662867115b3a8572a
(cherry picked from commit 7c21383f3429dd70da39c0c7f1efa12377a47ab6)
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen 4be7527134 ANDROID: crypto: arm64/ghash: fix CFI for GHASH CE
Calling a stand-alone assembly function trips clang's Control-Flow
Integrity checking. Add a static inline stub to convert indirect calls
to direct calls for pmull_ghash_update_p(64|8).

Change-Id: Ib4899cdae60d83961f78ed3451a919f7c9c713bc
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen 7642ed7f11 ANDROID: crypto: arm64/sha: fix CFI in SHA CE
Calling a stand-alone assembly function trips clang's Control-Flow
Integrity checking. Add static inline stubs to convert indirect calls
to direct calls for sha[12]_ce_transform

Bug: 67506682
Change-Id: If872f30095994206bc768eee13670be552b2a247
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen e0fa5bb6b1 ANDROID: arm64: kvm: disable CFI
Disable CFI for code that runs at EL2 because __cfi_check only
understands EL1 addresses.

Bug: 67506682
Change-Id: Ia582943be0b31669d88464fd99228a5368b1aa6a
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen 4663db1aab ANDROID: arm64: disable CFI for cpu_replace_ttbr1
Disable CFI to allow an indirect call to a physical address.

Bug: 67506682
Change-Id: I0ec38f34245a4ad52f508f6989093526d3bf442f
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen 0b36189b31 FROMLIST: arm64: use the correct function type for __arm64_sys_ni_syscall
Calling sys_ni_syscall through a syscall_fn_t pointer trips indirect
call Control-Flow Integrity checking due to a function type
mismatch. Use SYSCALL_DEFINE0 for __arm64_sys_ni_syscall instead and
remove the now unnecessary casts.

Change-Id: Id01f505b333b864477335e87e0c3100fa60be1eb
Link: https://lkml.org/lkml/2019/5/3/690
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:51 -07:00
Sami Tolvanen e43ba2e34c FROMLIST: arm64: use the correct function type in SYSCALL_DEFINE0
Although a syscall defined using SYSCALL_DEFINE0 doesn't accept
parameters, use the correct function type to avoid indirect call
type mismatches with Control-Flow Integrity checking.

Change-Id: I45d1235f4b93c69451ed3610b676d0948cef5ee1
Link: https://lkml.org/lkml/2019/5/3/691
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:50 -07:00
Sami Tolvanen 15324c964e FROMLIST: arm64: fix syscall_fn_t type
Syscall wrappers in <asm/syscall_wrapper.h> use const struct pt_regs *
as the argument type. Use const in syscall_fn_t as well to fix indirect
call type mismatches with Control-Flow Integrity checking.

Change-Id: I23d26aef5107985631a9aac500f2ceefc0b25419
Link: https://lkml.org/lkml/2019/5/3/689
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:50 -07:00
Sami Tolvanen 2f0685ef05 ANDROID: modpost: add an exception for CFI stubs
When CONFIG_CFI_CLANG is enabled, LLVM renames all address taken
functions by appending a .cfi postfix to their names, and creates
function stubs with the original names. The compiler always injects
these stubs to the text section, even if the function itself is
placed into init or exit sections, which creates modpost warnings.
This commit adds a modpost exception for CFI stubs to prevent the
warnings.

Bug: 117237524
Change-Id: Ieb8bf20d0c3ad7b7295c535f598370220598cdb0
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:50 -07:00
Sami Tolvanen ea853e033c ANDROID: ftrace: fix function type mismatches
This change fixes indirect call mismatches with function and function
graph tracing, which trip Control-Flow Integrity (CFI) checking.

Bug: 79510107
Bug: 67506682
Change-Id: I5de08c113fb970ffefedce93c58e0161f22c7ca2
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:50 -07:00
Sami Tolvanen 7dd8055f67 FROMLIST: 9p: pass the correct prototype to read_cache_page
Fix the callback 9p passes to read_cache_page to actually have the
proper type expected.  Casting around function pointers can easily
hide typing bugs, and defeats control flow protection.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Change-Id: I3738b7c0c7f77f85cd7007141400c36516dac6b8
Link: https://lkml.org/lkml/2019/5/1/374
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:50 -07:00
Sami Tolvanen e315defff6 UPSTREAM: netfilter: xt_IDLETIMER: fix sysfs callback function type
Use struct device_attribute instead of struct idletimer_tg_attr, and
the correct callback function type to avoid indirect call mismatches
with Control Flow Integrity checking.

Bug: 117299373
Change-Id: I5cc50171e01a5162ef504a726fe3ed4b51941b34
(cherry-picked from commit 20fdaf6e1e313b4efa48243d5250526eb43ea70d)
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2019-05-20 19:40:50 -07:00
Sami Tolvanen fc61681b43 ANDROID: kallsyms: strip the .cfi postfix from symbols with CONFIG_CFI_CLANG
With CFI enabled, LLVM appends .cfi to most function names, which
potentially breaks user space tools. While stripping the postfix is
not optimal either, this should at least create less confusion.

Bug: 67506682
Bug: 73328469
Change-Id: I253f34a562629032ddd792b8498e171109ea7cbc
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:50 -07:00
Sami Tolvanen 895fc0a0b1 ANDROID: add support for clang Control Flow Integrity (CFI)
This change adds the CONFIG_CFI_CLANG option, CFI error handling,
and a faster look-up table for cross module CFI checks.

Bug: 67506682
Change-Id: Ic009f0a629b552a0eb16e6d89808c7029e91447d
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:50 -07:00
Sami Tolvanen a9fc947996 FROMLIST: arm64: select ARCH_SUPPORTS_LTO_CLANG
Allow CONFIG_LTO_CLANG to be enabled for the architecture.

Bug: 62093296
Bug: 67506682
Change-Id: Id8e06b49877c4de2f15b51fc432d601b83b2c68f
(am from https://patchwork.kernel.org/patch/10060333/)
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:50 -07:00
Sami Tolvanen a339c351bb ANDROID: arm64: disable HAVE_ARCH_PREL32_RELOCATIONS with LTO_CLANG
Disable HAVE_ARCH_PREL32_RELOCATIONS to stop LLVM from reordering
initcalls.

Change-Id: I8fd9e24c89986b5a41813e23d28778839dd3abdf
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:50 -07:00
Sami Tolvanen 75dc7070d6 ANDROID: arm64: add atomic_ll_sc.o to obj-y if using lld
__ll_sc_* functions are only referenced in inline assembly, which means lld
won't see them when CONFIG_LTO_CLANG is enabled due to a bug in LLVM:

  https://bugs.llvm.org/show_bug.cgi?id=35841

When LTO and CONFIG_ARM64_LSE_ATOMICS are both enabled, linking fails with
the following type of errors:

ld.lld: error: relocation R_AARCH64_CALL26 cannot refer to absolute
  symbol: __ll_sc_atomic64_add_return_acquire
>>> defined in vmlinux.o
>>> referenced by atomic_lse.h:299 (arch/arm64/include/asm/atomic_lse.h:299)
>>>               vmlinux.o:(kernel_init_freeable)
...

This change works around the problem by always linking in atomic_ll_sc.o to
vmlinux, instead of placing it in arch/arm64/lib/lib.a.

Bug: 63740206
Bug: 117299373
Bug: 124318741
Change-Id: Idf2211034cce9cb0b6b2007bbdb1dfc1c1a1053d
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:50 -07:00
Sami Tolvanen d31b9814d8 ANDROID: arm64: lse: fix LSE atomics with LTO
LLVM's integrated assembler is always used for inline assembly with
CONFIG_LTO_CLANG. Unlike gcc, LLVM considers each inline assembly
block to be independent and therefore, any preambles that enable
features must be included in each block.

This change adds the necessary preamble to ARM64_LSE_ATOMIC_INSN to
allow CONFIG_ARM64_LSE_ATOMICS to be enabled with LTO.

Bug: 117299373
Change-Id: Icc06361dc2a2dba0f5f967d7f540cac2753b3e9c
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:50 -07:00
Sami Tolvanen d4a0a17702 ANDROID: arm64: vdso: disable LTO
Due to a bug in clang, vdso fails to build when both LTO_CLANG and
CC_OPTIMIZE_FOR_SIZE are enabled:

  https://bugs.llvm.org/show_bug.cgi?id=32155

Disable LTO for vdso to work around the problem.

Bug: 62093296
Bug: 67506682
Change-Id: I1d0279535fd389db4c829e4556f9ef728f240a34
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:50 -07:00
Sami Tolvanen 54a91da09a FROMLIST: arm64: kvm: use -fno-jump-tables with clang
Starting with LLVM r308050, clang generates a jump table with EL1
virtual addresses in __init_stage2_translation, which results in a
kernel panic when booting at EL2:

  Kernel panic - not syncing: HYP panic:
  PS:800003c9 PC:ffff0000089e6fd8 ESR:86000004
  FAR:ffff0000089e6fd8 HPFAR:0000000009825000 PAR:0000000000000000
  VCPU:000804fc20001221

  CPU: 0 PID: 0 Comm: swapper/0 Not tainted 4.14.0-rc7-dirty #3
  Hardware name: ARM Juno development board (r1) (DT)
  Call trace:
  [<ffff000008088ea4>] dump_backtrace+0x0/0x34c
  [<ffff000008089208>] show_stack+0x18/0x20
  [<ffff0000089c73ec>] dump_stack+0xc4/0xfc
  [<ffff0000080c8e1c>] panic+0x138/0x2b4
  [<ffff0000080c8ce4>] panic+0x0/0x2b4
  SMP: stopping secondary CPUs
  SMP: failed to stop secondary CPUs 0-3,5
  Kernel Offset: disabled
  CPU features: 0x002086
  Memory Limit: none
  ---[ end Kernel panic - not syncing: HYP panic:
  PS:800003c9 PC:ffff0000089e6fd8 ESR:86000004
  FAR:ffff0000089e6fd8 HPFAR:0000000009825000 PAR:0000000000000000
  VCPU:000804fc20001221

This change adds -fno-jump-tables to arm64/hyp to work around the
bug.

Bug: 62093296
Bug: 67506682
Change-Id: I1257be1febdcbfcc886fe6183c698b7a98d2a153
(am from https://patchwork.kernel.org/patch/10060301/)
Suggested-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:50 -07:00
Sami Tolvanen ba1b8d4309 BACKPORT: arm64: sysreg: Make mrs_s and msr_s macros work with Clang and LTO
Clang's integrated assembler does not allow assembly macros defined
in one inline asm block using the .macro directive to be used across
separate asm blocks. LLVM developers consider this a feature and not a
bug, recommending code refactoring:

  https://bugs.llvm.org/show_bug.cgi?id=19749

As binutils doesn't allow macros to be redefined, this change uses
UNDEFINE_MRS_S and UNDEFINE_MSR_S to define corresponding macros
in-place and workaround gcc and clang limitations on redefining macros
across different assembler blocks.

Specifically, the current state after preprocessing looks like this:

asm volatile(".macro mXX_s ... .endm");
void f()
{
	asm volatile("mXX_s a, b");
}

With GCC, it gives macro redefinition error because sysreg.h is included
in multiple source files, and assembler code for all of them is later
combined for LTO (I've seen an intermediate file with hundreds of
identical definitions).

With clang, it gives macro undefined error because clang doesn't allow
sharing macros between inline asm statements.

I also seem to remember catching another sort of undefined error with
GCC due to reordering of macro definition asm statement and generated
asm code for function that uses the macro.

The solution with defining and undefining for each use, while certainly
not elegant, satisfies both GCC and clang, LTO and non-LTO.

Co-developed-by: Alex Matveev <alxmtvv@gmail.com>
Co-developed-by: Yury Norov <ynorov@caviumnetworks.com>
Co-developed-by: Sami Tolvanen <samitolvanen@google.com>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Will Deacon <will.deacon@arm.com>
(cherry picked from commit be604c616ca71cbf5c860d0cfa4595128ab74189)
Change-Id: I803fff57f639b0921ef81f90ec4befe802e7eecf
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
2019-05-20 19:40:50 -07:00