Skip to content

Add --memory option to scan currently-running processes #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions doc/vulnix.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ should not be reported.
Scans the current system defined as transitive closure of
_/run/current-system_.

* `-M`, `--memory`:
Scans currently-running processes.

* `-G`, `--gc-roots`:
Scans all active garbage collection roots. This option is of limited use since
the scan will include all old system generations.
Expand Down
18 changes: 14 additions & 4 deletions src/vulnix/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ def init_logging(verbose):
logging.basicConfig(level=logging.WARNING)


def populate_store(store, gc_roots, profiles, paths, requisites=True):
def populate_store(store, gc_roots, memory, profiles, paths, requisites=True):
"""Load derivations from nix store depending on cmdline invocation."""
if gc_roots:
store.add_gc_roots()
if memory:
store.add_memory_roots()
for profile in profiles:
store.add_profile(profile)
for path in paths:
Expand All @@ -76,6 +78,8 @@ def run(nvd, store):
# what to scan
@click.option('-S', '--system', is_flag=True,
help='Scan the current system.')
@click.option('-M', '--memory', is_flag=True,
help='Scan currently-running process.')
@click.option('-G', '--gc-roots', is_flag=True,
help='Scan all active GC roots (including old ones).')
@click.option('-p', '--profile', type=click.Path(exists=True),
Expand Down Expand Up @@ -112,14 +116,14 @@ def run(nvd, store):
help='(obsolete; kept for compatibility reasons)')
@click.option('-F', '--notfixed', is_flag=True,
help='(obsolete; kept for compatibility reasons)')
def main(verbose, gc_roots, system, from_file, profile, path, mirror,
def main(verbose, gc_roots, memory, system, from_file, profile, path, mirror,
cache_dir, requisites, whitelist, write_whitelist, version, json,
show_whitelisted, default_whitelist, notfixed):
if version:
print('vulnix ' + pkg_resources.get_distribution('vulnix').version)
sys.exit(0)

if not (gc_roots or system or profile or path or from_file):
if not (gc_roots or memory or system or profile or path or from_file):
howto()
sys.exit(3)

Expand All @@ -145,7 +149,13 @@ def main(verbose, gc_roots, system, from_file, profile, path, mirror,
for drv in from_file.readlines():
paths.append(drv.strip())
else:
populate_store(store, gc_roots, profile, paths, requisites)
populate_store(
store,
gc_roots,
memory,
profile,
paths,
requisites)
with NVD(mirror, cache_dir) as nvd:
with Timer('Update NVD data'):
nvd.update()
Expand Down
9 changes: 9 additions & 0 deletions src/vulnix/nix.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ def add_profile(self, profile):
'--profile', profile]).splitlines():
self.add_path(line.split()[1])

def add_memory_roots(self):
"""Add derivations found in currently-running processes."""
_log.debug('loading derivations from currently-running processes')
for line in call(['nix-store', '--gc', '--print-roots']).splitlines():
source, path = line.split(' -> ', 1)
if (source.startswith('/proc/') or source.startswith('{temp:')
or source == '{lsof}' or source == '{censored}'):
self.add_path(path)

def add_path(self, path):
"""Add the closure of all derivations referenced by a store path."""
if not p.exists(path):
Expand Down