This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Observe SG_READ_ONLY flag in MachO binaries
Needs RevisionPublic

Authored by kastiglione on Jan 28 2022, 10:28 AM.

Details

Summary

Change Section::GetPermissions() to indicate non-writability when the Mach-O
segment has the SG_READ_ONLY flag.

MachO has a segment flag named SG_READ_ONLY which indicates that the segment
is semantically read-only, even if its permissions are marked writable.
Segments such as __DATA_CONST contain data that is semantically "const", but
needs to be writable for dyld to perform fixups (bindings or rebases).

The header documentation for SG_READ_ONLY states:

This segment is made read-only after fixups.

Diff Detail

Event Timeline

kastiglione requested review of this revision.Jan 28 2022, 10:28 AM
kastiglione created this revision.
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJan 28 2022, 10:28 AM
augusto2112 requested changes to this revision.Jan 28 2022, 12:38 PM
augusto2112 added inline comments.
lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
1436

Could we add a new value in the enumeration? Something like ePermissionsLinkerWritable? As it is right now this would be dangerous for the existing file-cache optimization as we'd happily read pointers that are supposed to be fixed by the linker from the file-cache.

This revision now requires changes to proceed.Jan 28 2022, 12:38 PM
kastiglione added inline comments.Jan 28 2022, 3:58 PM
lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
1436

That works for me. I think we'd want ePermissionsLoaderWritable.

aprantl added inline comments.Feb 5 2022, 2:57 PM
lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
1436

That sounds good.

kastiglione added inline comments.Feb 8 2022, 4:25 PM
lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
1436

Some idle questions:

  1. Some code turns permissions into a string, like rw- or r-x, but there's no string character for this "hybrid" writable flag.
  2. Other code might make r/w/x assumptions, and have bugs because of this extra state?
  3. Do any other binary file formats have this notion, or would this be forcing a Mach-O specific flag into a portable concept?

I haven't looked yet, but I'm thinking it would be good if there were some other set of flags this information could be stored in.

I'm also not even sure if SG_READ_ONLY should modify permissions. For such a segment, should the permissions say it's writable, and something else say "actually no it's not", or should the permissions say it's non-writable, and something else says "actually it is written to by the loader".

labath added a subscriber: labath.Feb 9 2022, 1:22 AM
labath added inline comments.
lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
1436

Do any other binary file formats have this notion, or would this be forcing a Mach-O specific flag into a portable concept?

There is a PT_GNU_RELRO in linux land:

PT_GNU_RELRO	 	

The array element specifies the location and size of a segment which may be made read-only after relocations have been processed.

I haven't checked windows, but I'd expect it to have something similar -- security hardening is a hot topic these days and writable vtables are a tempting target.