opencloud icon indicating copy to clipboard operation
opencloud copied to clipboard

antivirus: clamd "ERROR" response (e.g. "Can't write to file" when clamd's tmp dir is full) is treated as a clean

Open Ezanor opened this issue 1 month ago • 1 comments

antivirus: clamd "ERROR" response (e.g. "Can't write to file" when clamd's tmp dir is full) is treated as a clean scan, so the file is accepted unscanned

Describe the bug

When clamd fails during a scan with a non-virus error, the antivirus service reports the file as clean and publishes outcome=continue. The file is released without having been fully scanned.

Reproduced with archives whose extracted content does not fit in clamd's temporary directory (TemporaryDirectory, a tmpfs in our setup). clamd logs cli_writen: write error: No space left on device, then answers stream: Can't write to file ERROR.

Cause in the code (main):

  1. services/antivirus/pkg/scanners/clamav.go:

    case s := <-ch:
        return Result{
            Infected:    s.Status == clamd.RES_FOUND,
            Description: s.Description,
            ScanTime:    time.Now(),
        }, nil
    

    A RES_ERROR status is returned as Infected=false with a nil error.

  2. services/antivirus/pkg/service/service.go:

    case !res.Infected && err == nil:
        outcome = events.PPOutcomeContinue
    case err != nil:
        outcome = events.PPOutcomeRetry
    

    outcome=continue is chosen, so postprocessing proceeds and the file becomes available.

Only transport errors (connection closed, timeout) produce retry. For example, a plain INSTREAM: Can't write to temporary file (stream too large for the tmpfs) closes the connection and is retried correctly. An error returned as a complete ERROR reply is not.

Steps to reproduce

  1. Run clamd with a small TemporaryDirectory (e.g. a 250 MiB tmpfs), ANTIVIRUS_MAX_SCAN_SIZE=250MB, ANTIVIRUS_INFECTED_FILE_HANDLING=abort.
  2. Upload a small zip that expands to 1 GB (e.g. 1 GB of zeros zipped, ~1 MB).
  3. Look at the logs:
    • clamd: cli_writen: write error: No space left on device, then Can't write to file ERROR
    • antivirus: File scanned ... virus="Can't write to file" outcome=continue infected=false (the scan takes ~0.5 s)
  4. The upload finishes and the file is available.

Expected behavior

An ERROR reply from clamd should not be treated as "clean". Either:

  • return an error from Scan() so the existing retry path applies (the file is rescanned later, when space is available), or
  • end the step with abort/delete, as for an infected file.

Actual behavior

The file is accepted as clean (outcome=continue, infected=false) although the scan was incomplete. This is a silent bypass of the antivirus, caused only by resource exhaustion on the clamd side (and so potentially triggerable by users uploading crafted archives).

Suggested fix

In ClamAV.Scan:

case s := <-ch:
    if s.Status == clamd.RES_ERROR { // go-clamd: "ERROR"
        return Result{}, fmt.Errorf("clamd error: %s", s.Description)
    }
    return Result{Infected: s.Status == clamd.RES_FOUND, ...}, nil

Possibly also handle RES_PARSE_ERROR and any other unknown status in the same way.

Note: ClamAV's AlertExceedsMax does not cover this case, since a write error is not a limit overrun.

Setup

  • OpenCloud: 9.0.0
  • ClamAV: 1.4.6, clamd via a shared unix socket, TemporaryDirectory on a tmpfs
  • Deployment: Podman/systemd, antivirus service on a dedicated host, NATS JetStream
  • ANTIVIRUS_WORKERS=4, ANTIVIRUS_MAX_SCAN_SIZE_MODE=partial

Ezanor avatar Nov 02 '26 08:11 Ezanor

@rhafer I opened #3895 for this. Could you please review it when you have a moment? Thank you!

RyoKusnadi avatar Nov 03 '26 22:11 RyoKusnadi