Skip to content

Report a missing __LINKEDIT segment instead of panicking - #835

Open
arpitjain099 wants to merge 1 commit into
anchore:mainfrom
arpitjain099:fix/linkedit-nil-guard
Open

Report a missing __LINKEDIT segment instead of panicking#835
arpitjain099 wants to merge 1 commit into
anchore:mainfrom
arpitjain099:fix/linkedit-nil-guard

Conversation

@arpitjain099

Copy link
Copy Markdown

(*macho.File).Segment returns nil when it does not find the name, and the three places that look up __LINKEDIT (plus the __TEXT lookup next to one of them) use the result immediately:

  • quill/macho/file.go, AddEmptyCodeSigningCmd: linkEditSeg.Offset + linkEditSeg.Filesz
  • quill/sign/code_directory.go, newCodeDirectoryFromMacho: textSeg.Offset, and linkEditSeg.Offset + linkEditSeg.Filesz on the no-existing-signature path
  • quill/sign/signing_super_blob.go: linkEditSegment.Filesz += numSbBytes

A Mach-O without those segments is malformed, but quill can be handed one, and today it panics on a nil pointer instead of saying what is wrong with the input. Running AddEmptyCodeSigningCmd against a file with no segments:

panic: runtime error: invalid memory address or nil pointer dereference

Each site now returns an error naming the missing segment. Nothing else changes: on a well formed binary all four lookups succeed exactly as before.

The test builds a File with an empty macho.File so Segment("__LINKEDIT") is nil, and drives AddEmptyCodeSigningCmd. It panics on main and passes here. go build ./... and go vet ./quill/... are clean, and go test ./quill/macho/ passes.

Segment() returns nil for a name it does not find, and the three places
that look up __LINKEDIT (and __TEXT) during signing dereference the result
straight away. A Mach-O without those segments is malformed, but quill can
be handed one, and it panicked on a nil pointer rather than saying what was
wrong with the input.

Covers the same lookup in AddEmptyCodeSigningCmd, newCodeDirectoryFromMacho
and the superblob patch step.

The test drives AddEmptyCodeSigningCmd with a Mach-O that has no segments at
all; it panics without the guards.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant