heyer.systems Open Source

ClamAV (virus scan)

ClamAV from Debian for the virus scan of uploads: clamd on the Unix socket /run/clamav/clamd.ctl only (the app streams a file to it, INSTREAM), signatures kept current by freshclam. Needs about 2 GB of RAM more than the app (1 GB in use, 2 GB for a minute while it reloads new signatures)

clamav LXCVM

Add to a guest

In the guest as root – container and VM alike:

update --add-modules clamav

Runs a normal update and installs the module on the way.

From the Proxmox node, without logging in:

pct exec <ID> -- update --add-modules clamav   # container
ssh root@<IP> update --add-modules clamav

Choose when creating a guest

Without a dialog, on the Proxmox node:

IAC_NONINTERACTIVE=1 var_modules=auto-update,clamav \
  bash -c "$(curl -fsSL https://iac.heyer.systems/<app>/ct.sh)"

var_modules replaces the app's preselection – keep auto-update in the list if you want it. Interactive: choose “advanced” in the dialog, or tick the module in the generator on the app page.

Remove

update --remove-modules clamav

Removes the module's packages, services, firewall rules and configuration again. Data stays.

Details

ClamAV from Debian scans uploads for malware. clamd listens on the Unix socket /run/clamav/clamd.ctl only – no TCP port, nothing reachable from the network. The app in the same guest streams a file to it and gets OK or the name of the finding; freshclam keeps the signatures current (several times a day, about 110 MB on disk).

How much memory it needs

clamd keeps all signatures in memory: about 1 GB while it runs, up to 2 GB for a minute whenever freshclam brings new signatures and clamd reloads them (measured with 3.6 million signatures in Debian 13). Plan 2 GB more than the app needs – set defaults.ram in the iac.json of the app accordingly. With too little memory the kernel stops clamd during a reload.

The first install downloads the signatures (about 110 MB) and waits for them – up to 15 minutes; clamd loads them in a minute or two, until then a scan waits at the socket.

Use it in an app

The socket belongs to the group clamav, mode 0660. The user of the app joins the group:

- name: The user of the app may use clamd
  ansible.builtin.user:
    name: myapp
    groups: clamav
    append: true

Then stream the file to the socket (INSTREAM) – clamd needs no rights to the files of the app. Libraries exist for every language (e.g. clamd for Python, clamscan for Node.js). Read the answer of clamd and take only a reply that ends in OK as clean: … FOUND is a finding, … ERROR (clamd not ready, INSTREAM size limit exceeded, …) means the file was not scanned.

By hand, for a look:

clamdscan --ping 10:5                          # PONG: clamd is ready (10 tries, 5 s apart)
clamdscan --stream --no-summary document.pdf   # OK, or: document.pdf: <name> FOUND

Do not use clamdscan as the check of an app: in version 1.4.6 it prints OK and exits 0 for a file over the size limit, although clamd answered size limit exceeded. ERROR.

Settings

In /etc/iac/settings.env, then update:

clamav_max_size=100            # MB: largest file to scan (StreamMaxLength, MaxFileSize, MaxScanSize)
clamav_max_scan_time=120       # seconds one scan may take (MaxScanTime)
clamav_alert_encrypted=no      # yes: report an encrypted archive or PDF as a finding

Debian limits a stream to 25 MB – larger uploads would end with size limit exceeded, so the default is 100 MB. An encrypted PDF or archive cannot be scanned; by default it is not a finding (it is normal in business mail). The app should mark it as “not scannable” instead of “clean”. Everything else in /etc/clamav/clamd.conf stays as Debian ships it; iac only sets the keys above and LocalSocketMode.

Limits

ClamAV finds known malware. It does not find new, unpublished malware, and it cannot look into encrypted files. It is one layer – the app should also limit file types and sizes.

Checks

systemctl status clamav-daemon clamav-freshclam
journalctl -u clamav-freshclam -n 20          # signature updates
tail /var/log/clamav/clamav.log               # clamd

When the module is removed, ClamAV and its signatures are removed.