2020-03-27 15:30:39

by Julien Thierry

[permalink] [raw]
Subject: [PATCH v2 04/10] objtool: check: Ignore empty alternative groups

Atlernative section can contain entries for alternatives with no
instructions. Objtool will currently crash when handling such an entry.

Just skip that entry, but still give a warning to discourage useless
entries.

Signed-off-by: Julien Thierry <[email protected]>
---
tools/objtool/check.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 74353b2c39ce..5c03460f1f07 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -904,6 +904,12 @@ static int add_special_section_alts(struct objtool_file *file)
}

if (special_alt->group) {
+ if (!special_alt->orig_len) {
+ WARN_FUNC("empty alternative entry",
+ orig_insn->sec, orig_insn->offset);
+ continue;
+ }
+
ret = handle_group_alt(file, special_alt, orig_insn,
&new_insn);
if (ret)
--
2.21.1


2020-04-01 12:54:48

by Miroslav Benes

[permalink] [raw]
Subject: Re: [PATCH v2 04/10] objtool: check: Ignore empty alternative groups

On Fri, 27 Mar 2020, Julien Thierry wrote:

> Atlernative section can contain entries for alternatives with no
> instructions. Objtool will currently crash when handling such an entry.
>
> Just skip that entry, but still give a warning to discourage useless
> entries.
>
> Signed-off-by: Julien Thierry <[email protected]>
> ---
> tools/objtool/check.c | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> index 74353b2c39ce..5c03460f1f07 100644
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -904,6 +904,12 @@ static int add_special_section_alts(struct objtool_file *file)
> }
>
> if (special_alt->group) {
> + if (!special_alt->orig_len) {
> + WARN_FUNC("empty alternative entry",
> + orig_insn->sec, orig_insn->offset);
> + continue;
> + }
> +
> ret = handle_group_alt(file, special_alt, orig_insn,
> &new_insn);
> if (ret)

Probably the first time I am looking at alternatives handling in objtool,
so I must be missing something, but is this even possible now? I mean
get_alt_entry() in special.c sets alt->orig_len when alt->group is true
(which means .alternatives section) to something which cannot be zero.

Is this a preparatory patch for arm64, where this could happen? If yes, it
would be better to mention it in the changelog.

Miroslav

2020-04-01 13:45:57

by Julien Thierry

[permalink] [raw]
Subject: Re: [PATCH v2 04/10] objtool: check: Ignore empty alternative groups



On 4/1/20 1:53 PM, Miroslav Benes wrote:
> On Fri, 27 Mar 2020, Julien Thierry wrote:
>
>> Atlernative section can contain entries for alternatives with no
>> instructions. Objtool will currently crash when handling such an entry.
>>
>> Just skip that entry, but still give a warning to discourage useless
>> entries.
>>
>> Signed-off-by: Julien Thierry <[email protected]>
>> ---
>> tools/objtool/check.c | 6 ++++++
>> 1 file changed, 6 insertions(+)
>>
>> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
>> index 74353b2c39ce..5c03460f1f07 100644
>> --- a/tools/objtool/check.c
>> +++ b/tools/objtool/check.c
>> @@ -904,6 +904,12 @@ static int add_special_section_alts(struct objtool_file *file)
>> }
>>
>> if (special_alt->group) {
>> + if (!special_alt->orig_len) {
>> + WARN_FUNC("empty alternative entry",
>> + orig_insn->sec, orig_insn->offset);
>> + continue;
>> + }
>> +
>> ret = handle_group_alt(file, special_alt, orig_insn,
>> &new_insn);
>> if (ret)
>
> Probably the first time I am looking at alternatives handling in objtool,
> so I must be missing something, but is this even possible now? I mean
> get_alt_entry() in special.c sets alt->orig_len when alt->group is true
> (which means .alternatives section) to something which cannot be zero.
>

What I see is:

if (alt->group) {
alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset +
entry->orig_len);
alt->new_len = *(unsigned char *)(sec->data->d_buf + offset +
entry->new_len);
}


And as far as I can tell, "alt->orig_len" can be 0 if the entry in the
.altinstructions section of the .o file has the length set to 0.

I don't know how the alternative section generation works on x86, but on
arm64 it's just a computed assembly offset which can be 0.

> Is this a preparatory patch for arm64, where this could happen? If yes, it
> would be better to mention it in the changelog.
>

It used to happen on arm64, but the fix [1] was picked.

I can add that link to the commit if necessary.

[1] https://lkml.org/lkml/2020/1/9/708

Cheers,

--
Julien Thierry

2020-04-01 15:06:55

by Miroslav Benes

[permalink] [raw]
Subject: Re: [PATCH v2 04/10] objtool: check: Ignore empty alternative groups

On Wed, 1 Apr 2020, Julien Thierry wrote:

>
>
> On 4/1/20 1:53 PM, Miroslav Benes wrote:
> > On Fri, 27 Mar 2020, Julien Thierry wrote:
> >
> >> Atlernative section can contain entries for alternatives with no
> >> instructions. Objtool will currently crash when handling such an entry.
> >>
> >> Just skip that entry, but still give a warning to discourage useless
> >> entries.
> >>
> >> Signed-off-by: Julien Thierry <[email protected]>
> >> ---
> >> tools/objtool/check.c | 6 ++++++
> >> 1 file changed, 6 insertions(+)
> >>
> >> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> >> index 74353b2c39ce..5c03460f1f07 100644
> >> --- a/tools/objtool/check.c
> >> +++ b/tools/objtool/check.c
> >> @@ -904,6 +904,12 @@ static int add_special_section_alts(struct
> >> objtool_file *file)
> >> }
> >>
> >> if (special_alt->group) {
> >> + if (!special_alt->orig_len) {
> >> + WARN_FUNC("empty alternative entry",
> >> + orig_insn->sec, orig_insn->offset);
> >> + continue;
> >> + }
> >> +
> >> ret = handle_group_alt(file, special_alt, orig_insn,
> >> &new_insn);
> >> if (ret)
> >
> > Probably the first time I am looking at alternatives handling in objtool,
> > so I must be missing something, but is this even possible now? I mean
> > get_alt_entry() in special.c sets alt->orig_len when alt->group is true
> > (which means .alternatives section) to something which cannot be zero.
> >
>
> What I see is:
>
> if (alt->group) {
> alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset +
> entry->orig_len);
> alt->new_len = *(unsigned char *)(sec->data->d_buf + offset +
> entry->new_len);
> }

Now that you copy-pasted the code here, I see that I completely missed
there is dereference (for obvious reasons) right before the type cast, so
all is fine. My mistake, I need more tea.

> And as far as I can tell, "alt->orig_len" can be 0 if the entry in the
> .altinstructions section of the .o file has the length set to 0.

Yes

> I don't know how the alternative section generation works on x86, but on arm64
> it's just a computed assembly offset which can be 0.
>
> > Is this a preparatory patch for arm64, where this could happen? If yes, it
> > would be better to mention it in the changelog.
> >
>
> It used to happen on arm64, but the fix [1] was picked.
>
> I can add that link to the commit if necessary.

No, I think the check makes sense on its own.

Thanks
Miroslav