Skip to content

Commit

Permalink
Merge pull request #1939 from 0intro/dpkginfo_cache_leak
Browse files Browse the repository at this point in the history
Fix leak of dpkg cache when dpkginfo_init is called multiple times
  • Loading branch information
evgenyz authored Sep 6, 2023
2 parents d5807fb + f41f0ee commit a7c2195
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
20 changes: 8 additions & 12 deletions src/OVAL/probes/unix/linux/dpkginfo-helper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ using namespace std;
static int _init_done = 0;
static pkgCacheFile *cgCache = NULL;

static MMap *dpkg_mmap = NULL;

static int opencache (void) {
if (pkgInitConfig (*_config) == false) return 0;

Expand All @@ -37,8 +35,6 @@ static int opencache (void) {

if (pkgInitSystem (*_config, _system) == false) return 0;

if (!cgCache->ReadOnlyOpen(NULL)) return 0;

if (_error->PendingError () == true) {
_error->DumpErrors ();
return 0;
Expand All @@ -53,6 +49,8 @@ struct dpkginfo_reply_t * dpkginfo_get_by_name(const char *name, int *err)
pkgRecords Recs (cache);
struct dpkginfo_reply_t *reply = NULL;

if (!cgCache->ReadOnlyOpen(NULL)) return 0;

// Locate the package
pkgCache::PkgIterator Pkg = cache.FindPkg(name);
if (Pkg.end() == true) {
Expand Down Expand Up @@ -127,11 +125,15 @@ void dpkginfo_free_reply(struct dpkginfo_reply_t *reply)

int dpkginfo_init()
{
cgCache = new pkgCacheFile;
if (_init_done == 0)
if (_init_done == 0) {
cgCache = new pkgCacheFile;
if (opencache() != 1) {
delete cgCache;
cgCache = NULL;
return -1;
}
_init_done = 1;
}

return 0;
}
Expand All @@ -142,12 +144,6 @@ int dpkginfo_fini()
cgCache->Close();
}

delete cgCache;
cgCache = NULL;

delete dpkg_mmap;
dpkg_mmap = NULL;

return 0;
}

8 changes: 5 additions & 3 deletions src/OVAL/probes/unix/linux/dpkginfo_probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ struct dpkginfo_global {
};

static struct dpkginfo_global g_dpkg = {
.mutex = PTHREAD_MUTEX_INITIALIZER,
.init_done = -1,
};

Expand All @@ -78,9 +79,9 @@ int dpkginfo_probe_offline_mode_supported(void) {

void *dpkginfo_probe_init(void)
{
pthread_mutex_init (&(g_dpkg.mutex), NULL);

pthread_mutex_lock (&(g_dpkg.mutex));
g_dpkg.init_done = dpkginfo_init();
pthread_mutex_unlock (&(g_dpkg.mutex));
if (g_dpkg.init_done < 0) {
dE("dpkginfo_init has failed.");
}
Expand All @@ -92,8 +93,9 @@ void dpkginfo_probe_fini (void *ptr)
{
struct dpkginfo_global *d = (struct dpkginfo_global *)ptr;

pthread_mutex_lock (&(d->mutex));
dpkginfo_fini();
pthread_mutex_destroy (&(d->mutex));
pthread_mutex_unlock (&(d->mutex));

return;
}
Expand Down

0 comments on commit a7c2195

Please sign in to comment.