This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Fix two null pointer dereferences related to SHF_LINK_ORDER with --gc-sections
AbandonedPublic

Authored by MaskRay on Sep 19 2019, 3:30 AM.

Details

Summary

Fixes the lld side problem of PR43147.

If st_link(A)=B, and A has the SHF_LINK_ORDER flag, we may dereference
a null pointer if B is garbage collected.

The added test section-metadata.s also tests -r to improve -r + SHF_LINK_ORDER coverage.

Event Timeline

MaskRay created this revision.Sep 19 2019, 3:30 AM
MaskRay edited the summary of this revision. (Show Details)Sep 19 2019, 3:32 AM

Two questions:

  1. Why it is not a error? As far I understood after reading comments for PR, this is what bfd would do?

(https://bugs.llvm.org/show_bug.cgi?id=43147#c8)

  1. Should we instead disable GC for that case maybe?

This will definitely avoid a crash but I'm not sure if a program that can trigger the crash is well defined under garbage collection. For a section A with link order dependency on section B, with A live and B not live, there is a requirement to order section B with respect to the address of section A, and we can't know what the address is if B has been garbage collected. Making an arbitrary choice for the address will work for cases where removing B means that the order of A doesn't matter, however if the order of A does matter then we could get a run-time error.

I personally favour an error message, but I suppose a warning that the linker has made an arbitrary choice, or disabled garbage collection, could also work.

MaskRay abandoned this revision.Sep 19 2019, 5:55 AM

Two questions:

  1. Why it is not a error? As far I understood after reading comments for PR, this is what bfd would do?

(https://bugs.llvm.org/show_bug.cgi?id=43147#c8)

I agree an error is the way to go. I had started to work on this patch before I read Peter's comment...

  1. Should we instead disable GC for that case maybe?

Yes. I highly suspect the sanitizer has a bug. Whether or not it is a real bug, @manojgupta has to use --no-gc-sections.

I personally favour an error message, but I suppose a warning that the linker has made an arbitrary choice, or disabled garbage collection, could also work.

I thought ld.bfd would accept such cases. It accepts section-metadata.s in this patch but rejects https://bugs.llvm.org/show_bug.cgi?id=43147#c8
After some experiments, I believe it just checks the linked-to section of the first input section. Given the intention of the error, I think it is likely a oversight that it is more permissive. I'll file a bug on its Bugzilla.

Abandoned. We should go with D67761.