Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions internal/probe/openssl/boringssl_symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ const (
)

// findBoringSSLKeylogAddress resolves BoringSSL's internal ssl_log_secret
// function from Android's XZ-compressed .gnu_debugdata symbol table. The
// returned value is a file offset suitable for UprobeOptions.Address.
// function from the ELF symbol table or Android's XZ-compressed .gnu_debugdata
// symbol table. The returned value is a file offset suitable for
// UprobeOptions.Address.
func findBoringSSLKeylogAddress(path string) (uint64, error) {
lib, err := elf.Open(path)
if err != nil {
Expand All @@ -40,9 +41,16 @@ func findBoringSSLKeylogAddress(path string) (uint64, error) {
_ = lib.Close()
}()

// Unstripped Linux builds keep the symbol in the ordinary symbol table.
if symbols, err := lib.Symbols(); err == nil {
if offset, err := boringSSLKeylogOffset(lib.Progs, symbols); err == nil {
return offset, nil
}
}

section := lib.Section(".gnu_debugdata")
if section == nil {
return 0, fmt.Errorf("BoringSSL ELF has no .gnu_debugdata section")
return 0, fmt.Errorf("BoringSSL ELF has no %s symbol and no .gnu_debugdata section", boringSSLKeylogSymbol)
}
compressed, err := section.Data()
if err != nil {
Expand Down Expand Up @@ -73,11 +81,15 @@ func findBoringSSLKeylogAddress(path string) (uint64, error) {
if err != nil {
return 0, fmt.Errorf("read .gnu_debugdata symbols: %w", err)
}
return boringSSLKeylogOffset(lib.Progs, symbols)
}

func boringSSLKeylogOffset(programs []*elf.Prog, symbols []elf.Symbol) (uint64, error) {
for _, symbol := range symbols {
if symbol.Name != boringSSLKeylogSymbol || elf.ST_TYPE(symbol.Info) != elf.STT_FUNC {
continue
}
return executableFileOffset(lib.Progs, symbol.Value)
return executableFileOffset(programs, symbol.Value)
}

return 0, fmt.Errorf("BoringSSL keylog symbol %q not found", boringSSLKeylogSymbol)
Expand Down
43 changes: 43 additions & 0 deletions internal/probe/openssl/boringssl_symbol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package openssl

import (
"debug/elf"
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -40,3 +43,43 @@ func TestExecutableFileOffsetRejectsNonExecutableAddress(t *testing.T) {
_, err := executableFileOffset(programs, 0x2020)
require.Error(t, err)
}

func TestFindBoringSSLKeylogAddressFromSymbolTable(t *testing.T) {
for _, tool := range []string{"cc", "objcopy"} {
if _, err := exec.LookPath(tool); err != nil {
t.Skipf("ELF fixture requires %s: %v", tool, err)
}
}
dir := t.TempDir()
source := filepath.Join(dir, "fixture.c")
library := filepath.Join(dir, "libfixture.so")
code := "__attribute__((visibility(\"hidden\"))) void fixture(void) __asm__(\"" +
boringSSLKeylogSymbol + "\");\nvoid fixture(void) {}\n"
require.NoError(t, os.WriteFile(source, []byte(code), 0600))
out, err := exec.Command("cc", "-shared", "-fPIC", source, "-o", library).CombinedOutput()
require.NoError(t, err, "%s", out)

lib, err := elf.Open(library)
require.NoError(t, err)
symbols, err := lib.Symbols()
require.NoError(t, err)
var want uint64
for _, symbol := range symbols {
if symbol.Name == boringSSLKeylogSymbol {
want, err = executableFileOffset(lib.Progs, symbol.Value)
require.NoError(t, err)
}
}
require.NoError(t, lib.Close())
require.NotZero(t, want)

got, err := findBoringSSLKeylogAddress(library)
require.NoError(t, err)
require.Equal(t, want, got)

// A stripped library without .gnu_debugdata keeps the handshake fallback.
out, err = exec.Command("objcopy", "--strip-all", library).CombinedOutput()
require.NoError(t, err, "%s", out)
_, err = findBoringSSLKeylogAddress(library)
require.Error(t, err)
}
8 changes: 4 additions & 4 deletions internal/probe/openssl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ func (c *Config) Validate() error {
return errors.NewConfigurationError("capture mode validation failed", err)
}

if c.IsAndroid && c.IsBoringSSL && (c.CaptureMode != handlers.ModeText || c.TextSource == handlers.TextSourcePcapng) {
if c.IsBoringSSL && (c.CaptureMode != handlers.ModeText || c.TextSource == handlers.TextSourcePcapng) {
address, err := findBoringSSLKeylogAddress(c.OpensslPath)
if err != nil {
// Some OEM builds may omit Android's mini debug symbol table. Keep the
// existing SSL_do_handshake fallback available instead of rejecting
// keylog and pcapng modes outright.
// Stripped libraries without Android's mini debug symbol table keep
// the existing SSL_do_handshake fallback instead of rejecting keylog
// and pcapng modes outright.
c.boringSSLKeylogErr = err.Error()
} else {
c.BoringSSLKeylogAddr = address
Expand Down
Loading