Skip to content

Commit

Permalink
Retry failed lookups after one week in libcdb
Browse files Browse the repository at this point in the history
The libc databases might be updated to include the searched version,
so a request that failed once might work in the future.

Refs Gallopsled#983
  • Loading branch information
peace-maker committed Dec 30, 2023
1 parent cd0c34a commit b24c499
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions pwnlib/libcdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import division

import os
import time
import six
import tempfile

Expand All @@ -29,6 +30,9 @@
urls = os.environ['DEBUGINFOD_URLS'].split(' ')
DEBUGINFOD_SERVERS = urls + DEBUGINFOD_SERVERS

# Retry failed lookups after some time
NEGATIVE_CACHE_EXPIRY = 60 * 60 * 24 * 7 # 1 week

# https://gitlab.com/libcdb/libcdb wasn't updated after 2019,
# but still is a massive database of older libc binaries.
def provider_libcdb(hex_encoded_id, hash_type):
Expand Down Expand Up @@ -109,6 +113,10 @@ def search_by_hash(hex_encoded_id, hash_type='build_id', unstrip=True):
cache, cache_valid = _check_elf_cache('libcdb', hex_encoded_id, hash_type)
if cache_valid:
return cache

# We searched for this buildid before, but didn't find anything.
if cache is None:
return None

# Run through all available libc database providers to see if we have a match.
for provider in PROVIDERS:
Expand Down Expand Up @@ -141,6 +149,10 @@ def _search_debuginfo_by_hash(base_url, hex_encoded_id):
cache, cache_valid = _check_elf_cache('libcdb_dbg', hex_encoded_id, 'build_id')
if cache_valid:
return cache

# We searched for this buildid before, but didn't find anything.
if cache is None:
return None

# Try to find separate debuginfo.
url = '/buildid/{}/debuginfo'.format(hex_encoded_id)
Expand Down Expand Up @@ -191,8 +203,11 @@ def _check_elf_cache(cache_type, hex_encoded_id, hash_type):

data = read(cache)
if not data.startswith(b'\x7FELF'):
log.info_once("Skipping unavailable ELF %s", hex_encoded_id)
return cache, False
# Retry failed lookups after some time
if time.time() > os.path.getmtime(cache) + NEGATIVE_CACHE_EXPIRY:
return cache, False
log.info_once("Skipping invalid cached ELF %s", hex_encoded_id)
return None, False

log.info_once("Using cached data from %r", cache)
return cache, True
Expand Down

0 comments on commit b24c499

Please sign in to comment.