clamav

package
v1.0.521 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package clamav implements a ClamAV CLAMD protocol client (INSTREAM scanning) over Unix domain sockets or TCP, with zero external API dependency. It is a self-contained leaf (stdlib only, no Culvert coupling) extracted from the flat package main per ADR-0002.

Protocol: CLAMD INSTREAM command

  1. Send "zINSTREAM\0" (null-terminated command prefix)
  2. Stream data as length-prefixed chunks (4-byte big-endian uint32 + bytes)
  3. Terminate with a zero-length chunk ({0,0,0,0})
  4. Read null-terminated response: "stream: OK\0" → clean "stream: <VirusName> FOUND\0" → malicious "stream: ... ERROR\0" → scan error

Reference: https://linux.die.net/man/8/clamd

Index

Constants

This section is empty.

Variables

View Source
var ErrQueueFull = errors.New("clamav: scan queue full")

ErrQueueFull reports that a scan could not obtain one of the clamMaxConcurrent slots within the caller's budget. It is deliberately distinguishable from a daemon fault: the daemon is healthy, this node is simply at its scanning capacity, and the two need different operator responses (add capacity vs. fix the daemon) and different postures.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a ClamAV CLAMD protocol client.

func New

func New(addr string) *Client

New creates a client from an address string.

"unix:/var/run/clamav/clamd.sock"  → Unix domain socket
"tcp:localhost:3310"               → TCP connection
""                                 → default Unix socket path

func (*Client) Ping

func (c *Client) Ping() error

Ping verifies the ClamAV daemon is reachable and responding correctly. Returns nil on success, error otherwise.

Unbounded by any caller: the budget is this client's own timeout, applied twice (dial, then read), so one Ping can hold a goroutine and a socket for up to 2x it. Callers that need a tighter bound, or that must be able to end the probe, use PingContext.

func (*Client) PingContext added in v1.0.414

func (c *Client) PingContext(ctx context.Context) error

PingContext is Ping bounded by ctx as well as by this client's timeout.

WHY IT EXISTS (CHAOS-116). `timeout` is sized for a SCAN — 30 s by default — and Ping applies it to the dial and then again to the read. That is the right budget for an operator-initiated scan and the wrong one for a liveness probe, and the probe path had no way to say so: Ping dials with context.Background(), so neither a caller's deadline nor a client's disconnect could end it. The unauthenticated /health and /ready probes reached this function through secscan.Scanner.ClamAVStatus, which means a blackholed daemon could park a gateway goroutine and socket per request for up to a minute.

The deadline applied is the EARLIER of ctx's and now+timeout, so this can only ever tighten Ping's existing bound, never loosen it. A ctx that is CANCELLED rather than expiring closes the connection, because a blocking Read cannot be interrupted any other way: the read then fails at once instead of sitting on its deadline.

func (*Client) Scan

func (c *Client) Scan(data []byte) (virusName string, isMalicious bool, err error)

Scan submits data to the ClamAV daemon via the INSTREAM command using the legacy budget (clamQueueWaitFallback for the queue, c.timeout for the I/O). Prefer ScanContext, which lets the caller own the deadline.

func (*Client) ScanContext added in v1.0.209

func (c *Client) ScanContext(ctx context.Context, data []byte) (virusName string, isMalicious bool, err error)

ScanContext submits data to the ClamAV daemon via the INSTREAM command under the caller's context. Returns (virusName, isMalicious, error); virusName is non-empty only when isMalicious is true.

Concurrent scans are limited by clamSem to protect the daemon. The wait for a slot is charged to the CALLER'S budget rather than to a private constant. That distinction is load-bearing and was a defect: the queue wait used to give up after its own 5 s and return an ordinary error, which the orchestrator classifies as "engine fault" and handles fail-OPEN — so five concurrent large downloads on a perfectly healthy daemon admitted content unscanned, while the orchestrator's own 10 s deadline (the outer limit that is supposed to decide this) fails CLOSED. An inner deadline must never preempt an outer one and invert its posture. With the caller's context threaded through, exceeding the budget lands on the caller's fail-closed path, and ErrQueueFull says which resource ran out so saturation stays distinguishable from a daemon fault.

Cancellation is honoured throughout: the dial, the deadline on the connection (the earlier of the caller's deadline and c.timeout), and an explicit watcher that closes the connection when the context ends. A scan the caller has abandoned therefore releases its clamSem slot promptly instead of holding one of four for the full c.timeout — which is what turned a slow daemon into a self-sustaining collapse, every abandoned scan squatting a slot and pushing live requests onto the fail-open queue-full path.

func (*Client) Version added in v1.0.22

func (c *Client) Version() (Version, error)

Version queries the ClamAV daemon for its engine and signature-database version via the VERSION command. The daemon replies with a single line like:

ClamAV 0.103.8/26982/Wed Apr 12 09:30:00 2023

where the three "/"-separated fields are the engine version, the signature database version (main+daily counter), and the database build date. Older or minimally-configured daemons may reply with just the engine string and no "/" fields; DBVersion/DBDate are then empty. Raw is always the full reply.

type Version added in v1.0.22

type Version struct {
	Engine    string `json:"engine"`               // e.g. "ClamAV 0.103.8"
	DBVersion string `json:"db_version,omitempty"` // signature database counter, e.g. "26982"
	DBDate    string `json:"db_date,omitempty"`    // database build date, e.g. "Wed Apr 12 09:30:00 2023"
	Raw       string `json:"raw"`                  // full unparsed reply
}

Version holds the parsed ClamAV VERSION response.

func (Version) DBTime added in v1.0.484

func (v Version) DBTime() (time.Time, bool)

DBTime parses DBDate into the signature database's build time, reporting false when there is no date to parse or when none of the known layouts matches.

The boolean is load-bearing and callers must not collapse it to a zero Time: an unparseable or absent date means the age of the signature set is UNKNOWN, and unknown is not the same as current. Reporting an unknown date as a zero time would make it the oldest possible signature set (alerting on every daemon whose reply this parse does not understand), and reporting it as time.Now() would make it permanently fresh (alerting on none of them). Both are wrong in a way the caller cannot detect, so the decision belongs to the freshness plane, which reports "unknown" as its own state.

A reply with no "/" fields — an older or minimally-configured daemon, or one with no database loaded at all — carries an empty DBDate and lands on the same false.

The zone is whatever the layout supplied: an unzoned reply parses as UTC, because clamd formats with localtime and does not say which zone that was. A daemon in a non-UTC zone therefore skews the computed age by at most the zone offset, under 15 hours, which is far below any staleness threshold an operator would set on a signature set that upstream refreshes several times a day. The skew is recorded rather than corrected: there is no field in the reply to correct it from.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL