Skip to content

Commit 7ecb59a

Browse files
committed
Merge tag 'objtool_urgent_for_5.8_rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull objtool fixes from Borislav Petkov: "Three fixes from Peter Zijlstra suppressing KCOV instrumentation in noinstr sections. Peter Zijlstra says: "Address KCOV vs noinstr. There is no function attribute to selectively suppress KCOV instrumentation, instead teach objtool to NOP out the calls in noinstr functions" This cures a bunch of KCOV crashes (as used by syzcaller)" * tag 'objtool_urgent_for_5.8_rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: objtool: Fix noinstr vs KCOV objtool: Provide elf_write_{insn,reloc}() objtool: Clean up elf_write() condition
2 parents a358505 + 0f1441b commit 7ecb59a

File tree

8 files changed

+109
-9
lines changed

8 files changed

+109
-9
lines changed

arch/x86/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ config X86
6767
select ARCH_HAS_FILTER_PGPROT
6868
select ARCH_HAS_FORTIFY_SOURCE
6969
select ARCH_HAS_GCOV_PROFILE_ALL
70-
select ARCH_HAS_KCOV if X86_64
70+
select ARCH_HAS_KCOV if X86_64 && STACK_VALIDATION
7171
select ARCH_HAS_MEM_ENCRYPT
7272
select ARCH_HAS_MEMBARRIER_SYNC_CORE
7373
select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE

tools/objtool/arch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,6 @@ unsigned long arch_jump_destination(struct instruction *insn);
8484

8585
unsigned long arch_dest_rela_offset(int addend);
8686

87+
const char *arch_nop_insn(int len);
88+
8789
#endif /* _ARCH_H */

tools/objtool/arch/x86/decode.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,21 @@ void arch_initial_func_cfi_state(struct cfi_init_state *state)
565565
state->regs[16].base = CFI_CFA;
566566
state->regs[16].offset = -8;
567567
}
568+
569+
const char *arch_nop_insn(int len)
570+
{
571+
static const char nops[5][5] = {
572+
/* 1 */ { 0x90 },
573+
/* 2 */ { 0x66, 0x90 },
574+
/* 3 */ { 0x0f, 0x1f, 0x00 },
575+
/* 4 */ { 0x0f, 0x1f, 0x40, 0x00 },
576+
/* 5 */ { 0x0f, 0x1f, 0x44, 0x00, 0x00 },
577+
};
578+
579+
if (len < 1 || len > 5) {
580+
WARN("invalid NOP size: %d\n", len);
581+
return NULL;
582+
}
583+
584+
return nops[len-1];
585+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef _OBJTOOL_ARCH_ELF
2+
#define _OBJTOOL_ARCH_ELF
3+
4+
#define R_NONE R_X86_64_NONE
5+
6+
#endif /* _OBJTOOL_ARCH_ELF */

tools/objtool/check.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "check.h"
1313
#include "special.h"
1414
#include "warn.h"
15+
#include "arch_elf.h"
1516

1617
#include <linux/hashtable.h>
1718
#include <linux/kernel.h>
@@ -765,6 +766,24 @@ static int add_call_destinations(struct objtool_file *file)
765766
} else
766767
insn->call_dest = rela->sym;
767768

769+
/*
770+
* Many compilers cannot disable KCOV with a function attribute
771+
* so they need a little help, NOP out any KCOV calls from noinstr
772+
* text.
773+
*/
774+
if (insn->sec->noinstr &&
775+
!strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
776+
if (rela) {
777+
rela->type = R_NONE;
778+
elf_write_rela(file->elf, rela);
779+
}
780+
781+
elf_write_insn(file->elf, insn->sec,
782+
insn->offset, insn->len,
783+
arch_nop_insn(insn->len));
784+
insn->type = INSN_NOP;
785+
}
786+
768787
/*
769788
* Whatever stack impact regular CALLs have, should be undone
770789
* by the RETURN of the called function.
@@ -2766,7 +2785,7 @@ int check(const char *_objname, bool orc)
27662785

27672786
objname = _objname;
27682787

2769-
file.elf = elf_open_read(objname, orc ? O_RDWR : O_RDONLY);
2788+
file.elf = elf_open_read(objname, O_RDWR);
27702789
if (!file.elf)
27712790
return 1;
27722791

@@ -2827,7 +2846,9 @@ int check(const char *_objname, bool orc)
28272846
ret = create_orc_sections(&file);
28282847
if (ret < 0)
28292848
goto out;
2849+
}
28302850

2851+
if (file.elf->changed) {
28312852
ret = elf_write(file.elf);
28322853
if (ret < 0)
28332854
goto out;

tools/objtool/elf.c

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,9 @@ static int read_relas(struct elf *elf)
529529
rela->addend = rela->rela.r_addend;
530530
rela->offset = rela->rela.r_offset;
531531
symndx = GELF_R_SYM(rela->rela.r_info);
532-
rela->sym = find_symbol_by_index(elf, symndx);
533532
rela->sec = sec;
533+
rela->idx = i;
534+
rela->sym = find_symbol_by_index(elf, symndx);
534535
if (!rela->sym) {
535536
WARN("can't find rela entry symbol %d for %s",
536537
symndx, sec->name);
@@ -713,6 +714,8 @@ struct section *elf_create_section(struct elf *elf, const char *name,
713714
elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
714715
elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
715716

717+
elf->changed = true;
718+
716719
return sec;
717720
}
718721

@@ -746,7 +749,7 @@ struct section *elf_create_rela_section(struct elf *elf, struct section *base)
746749
return sec;
747750
}
748751

749-
int elf_rebuild_rela_section(struct section *sec)
752+
int elf_rebuild_rela_section(struct elf *elf, struct section *sec)
750753
{
751754
struct rela *rela;
752755
int nr, idx = 0, size;
@@ -763,6 +766,9 @@ int elf_rebuild_rela_section(struct section *sec)
763766
return -1;
764767
}
765768

769+
sec->changed = true;
770+
elf->changed = true;
771+
766772
sec->data->d_buf = relas;
767773
sec->data->d_size = size;
768774

@@ -779,7 +785,44 @@ int elf_rebuild_rela_section(struct section *sec)
779785
return 0;
780786
}
781787

782-
int elf_write(const struct elf *elf)
788+
int elf_write_insn(struct elf *elf, struct section *sec,
789+
unsigned long offset, unsigned int len,
790+
const char *insn)
791+
{
792+
Elf_Data *data = sec->data;
793+
794+
if (data->d_type != ELF_T_BYTE || data->d_off) {
795+
WARN("write to unexpected data for section: %s", sec->name);
796+
return -1;
797+
}
798+
799+
memcpy(data->d_buf + offset, insn, len);
800+
elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY);
801+
802+
elf->changed = true;
803+
804+
return 0;
805+
}
806+
807+
int elf_write_rela(struct elf *elf, struct rela *rela)
808+
{
809+
struct section *sec = rela->sec;
810+
811+
rela->rela.r_info = GELF_R_INFO(rela->sym->idx, rela->type);
812+
rela->rela.r_addend = rela->addend;
813+
rela->rela.r_offset = rela->offset;
814+
815+
if (!gelf_update_rela(sec->data, rela->idx, &rela->rela)) {
816+
WARN_ELF("gelf_update_rela");
817+
return -1;
818+
}
819+
820+
elf->changed = true;
821+
822+
return 0;
823+
}
824+
825+
int elf_write(struct elf *elf)
783826
{
784827
struct section *sec;
785828
Elf_Scn *s;
@@ -796,6 +839,8 @@ int elf_write(const struct elf *elf)
796839
WARN_ELF("gelf_update_shdr");
797840
return -1;
798841
}
842+
843+
sec->changed = false;
799844
}
800845
}
801846

@@ -808,6 +853,8 @@ int elf_write(const struct elf *elf)
808853
return -1;
809854
}
810855

856+
elf->changed = false;
857+
811858
return 0;
812859
}
813860

tools/objtool/elf.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ struct rela {
6464
GElf_Rela rela;
6565
struct section *sec;
6666
struct symbol *sym;
67-
unsigned int type;
6867
unsigned long offset;
68+
unsigned int type;
6969
int addend;
70+
int idx;
7071
bool jump_table_start;
7172
};
7273

@@ -76,6 +77,7 @@ struct elf {
7677
Elf *elf;
7778
GElf_Ehdr ehdr;
7879
int fd;
80+
bool changed;
7981
char *name;
8082
struct list_head sections;
8183
DECLARE_HASHTABLE(symbol_hash, ELF_HASH_BITS);
@@ -118,7 +120,11 @@ struct elf *elf_open_read(const char *name, int flags);
118120
struct section *elf_create_section(struct elf *elf, const char *name, size_t entsize, int nr);
119121
struct section *elf_create_rela_section(struct elf *elf, struct section *base);
120122
void elf_add_rela(struct elf *elf, struct rela *rela);
121-
int elf_write(const struct elf *elf);
123+
int elf_write_insn(struct elf *elf, struct section *sec,
124+
unsigned long offset, unsigned int len,
125+
const char *insn);
126+
int elf_write_rela(struct elf *elf, struct rela *rela);
127+
int elf_write(struct elf *elf);
122128
void elf_close(struct elf *elf);
123129

124130
struct section *find_section_by_name(const struct elf *elf, const char *name);
@@ -130,7 +136,7 @@ struct rela *find_rela_by_dest(const struct elf *elf, struct section *sec, unsig
130136
struct rela *find_rela_by_dest_range(const struct elf *elf, struct section *sec,
131137
unsigned long offset, unsigned int len);
132138
struct symbol *find_func_containing(struct section *sec, unsigned long offset);
133-
int elf_rebuild_rela_section(struct section *sec);
139+
int elf_rebuild_rela_section(struct elf *elf, struct section *sec);
134140

135141
#define for_each_sec(file, sec) \
136142
list_for_each_entry(sec, &file->elf->sections, list)

tools/objtool/orc_gen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ int create_orc_sections(struct objtool_file *file)
222222
}
223223
}
224224

225-
if (elf_rebuild_rela_section(ip_relasec))
225+
if (elf_rebuild_rela_section(file->elf, ip_relasec))
226226
return -1;
227227

228228
return 0;

0 commit comments

Comments
 (0)