Skip to content

Commit 827cc46

Browse files
enh-googletravarilo
authored andcommitted
Trim trailing '\0's inserted by libselinux.
libselinux has an off-by-one that causes it to pass the trailing '\0' to the kernel as if it's part of the security context, and the kernel dutifully hands it back, since it's an uninterpreted byte array as far as the kernel is concerned. libselinux accidentally hides this bug by treating it as a C string and calling strdup(), but debuggerd doesn't because it reads the file into a std::string. We could switch to libselinux's getcon()/getpidcon(), but (a) libselinux is awful (see above) and (b) not currently accessible to apexes (and it doesn't seem like a great idea to make it accessible). So just manually drop the last byte from the context we read ourselves, if it happens to be a '\0'. Bug: android/ndk#1993 Test: treehugger Change-Id: I8e7605ac5e618007a8da635cb6f45b0778dc167c
1 parent 55f0c68 commit 827cc46

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

debuggerd/crash_dump.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,10 @@ int main(int argc, char** argv) {
510510
if (!android::base::ReadFdToString(attr_fd, &info.selinux_label)) {
511511
PLOG(WARNING) << "failed to read selinux label";
512512
}
513+
// https://github.com/android/ndk/issues/1993
514+
if (!info.selinux_label.empty() && info.selinux_label.back() == '\0') {
515+
info.selinux_label.pop_back();
516+
}
513517

514518
if (!ptrace_interrupt(thread, &info.signo)) {
515519
PLOG(WARNING) << "failed to ptrace interrupt thread " << thread;

debuggerd/libdebuggerd/tombstone.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ void engrave_tombstone_ucontext(int tombstone_fd, int proto_fd, uint64_t abort_m
7171

7272
std::string selinux_label;
7373
android::base::ReadFileToString("/proc/self/attr/current", &selinux_label);
74+
// https://github.com/android/ndk/issues/1993
75+
if (!selinux_label.empty() && selinux_label.back() == '\0') {
76+
selinux_label.pop_back();
77+
}
7478

7579
std::map<pid_t, ThreadInfo> threads;
7680
threads[target_tid] = ThreadInfo {

0 commit comments

Comments
 (0)