Skip to content
This repository was archived by the owner on Apr 26, 2021. It is now read-only.
This repository was archived by the owner on Apr 26, 2021. It is now read-only.

Using 'cuckoo clean' will not remove any files that symlinks point to #3086

@soutzis

Description

@soutzis

I am analysing a large number of binaries and so I created a symlink for the analyses directory. Everything works fine, but when using 'cuckoo clean', the function shutil.rmtree($CWD/storage/analyses/) will throw an Exception:

[cuckoo.apps.apps] WARNING: Error removing directory /home/user/.cuckoo/storage/analyses: Cannot call rmtree on a symbolic link

Cuckoo version and OS:

Cuckoo 2.0.7 & Ubuntu 18.04

This can be reproduced by:
  1. Create a soft link for a directory (e.g. 'analyses'):
    sudo ln -s /preferred_location/analyses/ /$CWD/storage/analyses
  2. Execute cuckoo clean
This can be easily fixed by:

One line of code.
In the cuckoo_clean() function (located in the file cuckoo/cuckoo/apps/apps.py), there is a for-loop that deletes various files and directories:

# Delete the various files and directories. In case of directories, keep
# the parent directories, so to keep the state of the CWD in tact.
for path in paths:
    if os.path.isdir(path):
        try:
            shutil.rmtree(path)
            os.mkdir(path)
        except (IOError, OSError) as e:
            log.warning("Error removing directory %s: %s", path, e)
    elif os.path.isfile(path):
        try:
            os.unlink(path)
        except (IOError, OSError) as e:
            log.warning("Error removing file %s: %s", path, e)

Just before calling rmtree(), you could use os.path.realpath() to get the absolute path in case of a symlink. See below:

# Delete the various files and directories. In case of directories, keep
# the parent directories, so to keep the state of the CWD in tact.
for path in paths:
    if os.path.isdir(path):
        try:
            path = os.path.realpath(path)  # This gracefully handles cases with soft links!
            shutil.rmtree(path)
            os.mkdir(path)
        except (IOError, OSError) as e:
            log.warning("Error removing directory %s: %s", path, e)
    elif os.path.isfile(path):
        try:
            os.unlink(path)
        except (IOError, OSError) as e:
            log.warning("Error removing file %s: %s", path, e)

I have tested the above solution and it works beautifully.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions