Skip to content

clamd: Add an option to toggle SHUTDOWN, RELOAD, STATS and VERSION - #1502

Merged
val-ms merged 9 commits into
Cisco-Talos:mainfrom
ChaoticByte:clamd-disable-commands-per-config
Jun 4, 2025
Merged

val-ms merged 9 commits into
Cisco-Talos:mainfrom
ChaoticByte:clamd-disable-commands-per-config

Conversation

@ChaoticByte

@ChaoticByte ChaoticByte commented May 10, 2025 •

Copy link
Copy Markdown
Contributor

Sysadmins may need to prevent users from doing stuff like shutting down clamd, constantly reloading, or gathering information about the daemon, so I added some config options to control that.

Here are the config options with their defaults:

EnableShutdownCommand yes
EnableReloadCommand yes
EnableStatsCommand yes
EnableVersionCommand yes

If the client calls a deactived command, the server replies with UNKNOWN COMMAND COMMAND UNAVAILABLE

see #347, #922 and #1169

@val-ms val-ms left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for submitting a proposed fix for this. I'm good with this solution.
Can you please document the new options in this following locations?

The clang-format test has an issue with formatting, here:

clamd/session.c:568:75: error: code should be clang-formatted [-Wclang-format-violations]
                /* we set reload flag, and we'll reload before closing the
                                                                          ^

The code change makes sense to me though. I'll run this through the test pipelines to verify there aren't unexpected issues and also test it locally. Unless something comes up I don't expect any other code changes to request.

@ChaoticByte

Copy link
Copy Markdown
Contributor Author

I added documentation to the manpage and the sample configs (hope it is intelligible), and also got rid of the scary formatting error :)

@val-ms val-ms left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discussed this with teammates who suggested two UX improvements:

  1. clamdtop will not work when EnableStatsCommand is no. It would be great if you could add a check in clamdtop to see if this feature is disabled and log an erro rmessage explaining that it is disabled.

  2. the freshclam.conf option NotifyClamd will not work if EnableReloadCommand is no. It would be great if you could have freshclam check if the reload command is disabled before attempting, so it can print a helpful warning to explain why clamd can't be notified. I think the change would go right here: https://github.com/Cisco-Talos/clamav/blob/main/freshclam/notify.c#L64

Comment thread win32/conf_examples/clamd.conf.sample Resolved
Comment thread etc/clamd.conf.sample Resolved
@ChaoticByte

ChaoticByte commented May 16, 2025 •

Copy link
Copy Markdown
Contributor Author

I discussed this with teammates who suggested two UX improvements:

  1. clamdtop will not work when EnableStatsCommand is no. It would be great if you could add a check in clamdtop to see if this feature is disabled and log an erro rmessage explaining that it is disabled.

Should I consider the config options or the response from clamd for checking if the command is enabled?

Edit: Checking the response would make it more flexible (allowing it to work with remote clamd servers), so I'll go with that for now.

@ChaoticByte

Copy link
Copy Markdown
Contributor Author
1. `clamdtop` will not work when `EnableStatsCommand` is `no`. It would be great if you could add a check in clamdtop to see if this feature is disabled and log an erro rmessage explaining that it is disabled.

Looks like clamdtop also requires VERSION: https://github.com/Cisco-Talos/clamav/blob/main/clamdtop/clamdtop.c#L801-L811

@val-ms

val-ms commented May 16, 2025

Copy link
Copy Markdown
Contributor
1. `clamdtop` will not work when `EnableStatsCommand` is `no`. It would be great if you could add a check in clamdtop to see if this feature is disabled and log an erro rmessage explaining that it is disabled.

Looks like clamdtop also requires VERSION: https://github.com/Cisco-Talos/clamav/blob/main/clamdtop/clamdtop.c#L801-L811

Oh thanks for catching that!

@ChaoticByte

Copy link
Copy Markdown
Contributor Author

I can't use UKNOWN COMMAND for responding to a disabled command, because it intereferes with other detection logic in clamdtop (and probably others).
COMMAND UNAVAILABLE seems like a good replacement.

@ChaoticByte

ChaoticByte commented May 17, 2025 •

Copy link
Copy Markdown
Contributor Author

I discussed this with teammates who suggested two UX improvements:

  1. clamdtop will not work when EnableStatsCommand is no. It would be great if you could add a check in clamdtop to see if this feature is disabled and log an erro rmessage explaining that it is disabled.

  2. the freshclam.conf option NotifyClamd will not work if EnableReloadCommand is no. It would be great if you could have freshclam check if the reload command is disabled before attempting, so it can print a helpful warning to explain why clamd can't be notified. I think the change would go right here: https://github.com/Cisco-Talos/clamav/blob/main/freshclam/notify.c#L64

Is implemented now :)

@ChaoticByte
ChaoticByte requested a review from val-ms May 17, 2025 07:48

@val-ms val-ms left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code change looks great. Thank you very much for this work.

I did manual testing to verify the new features and am happy with the results.

For EnableReloadCommand, I first tested that a reload would still happen with NotifyClamd configured.

Then I tested with EnableReloadCommand no:

❯ ./install/bin/freshclam --update-db=test
ClamAV update process started at Mon Jun  2 09:59:26 2025
test database available for update (local version: 2, remote version: 10)
Time:    0.2s, ETA:    0.0s [========================>]   10.19KiB/10.19KiB
Testing database: '/home/micah/dev/clamav/clamav-2/build/install/share/clamav/tmp.23d6e455ae/test.cvd' ...
Database test passed.
test.cvd updated (version: 10, sigs: 18, f-level: 81, builder: micasnyd)
WARNING: Clamd was NOT notified: The RELOAD command is disabled. Consider enabling it in the clamd configuration!

I also confirmed that clamd did not reload.

For EnableVersionCommand, I tested clamdscan --version and freshclam --version before and after setting EnableVersionCommand no.

Before:

clamav-2/build on  PR-1502 [$?] via C v9.4.0-gcc via △ v3.30.0
❯ ./install/bin/clamdscan --version
ClamAV 1.5.0-beta/27647/Sat May 24 04:30:52 2025

clamav-2/build on  PR-1502 [$?] via C v9.4.0-gcc via △ v3.30.0
❯ ./install/bin/freshclam --version
ClamAV 1.5.0-beta/27647/Sat May 24 04:30:52 2025

With EnableVersionCommand no set:

clamav-2/build on  PR-1502 [$?] via C v9.4.0-gcc via △ v3.30.0
❯ ./install/bin/clamdscan --version
COMMAND UNAVAILABLE

clamav-2/build on  PR-1502 [$?] via C v9.4.0-gcc via △ v3.30.0
❯ ./install/bin/freshclam --version
ClamAV 1.5.0-beta/27647/Sat May 24 04:30:52 2025

It would be a little bit better if clamdscan were to return the local version of the clamdscan program if the VERSION command is unavailable, as it does when clamd is not running. E.g.:

❯ ./install/bin/clamdscan --version
ERROR: Could not connect to clamd on LocalSocket /tmp/clamd.sock: No such file or directory
ClamAV 1.5.0-beta

I'm thinking something like this:

diff --git a/clamdscan/client.c b/clamdscan/client.c
index 54f28c225..0caf4ddc4 100644
--- a/clamdscan/client.c
+++ b/clamdscan/client.c
@@ -357,6 +357,14 @@ int get_clamd_version(const struct optstruct *opts)
             logg(LOGG_ERROR, "Error occurred while receiving version information.\n");
             break;
         }
+
+        // Check if the response was "COMMAND UNAVAILABLE"
+        if (len >= 19 && memcmp(buff, "COMMAND UNAVAILABLE", 19) == 0) {
+            logg(LOGG_WARNING, "VERSION command disabled in clamd.\n");
+            closesocket(sockd);
+            return 2;
+        }
+
         printf("%s\n", buff);
     }

I also confirmed that clamdtop prints the nice error message when EnableVersionCommand no:

❯ ./install/bin/clamdtop
        __                    ____
  _____/ /___ _____ ___  ____/ / /_____  ____
 / ___/ / __ `/ __ `__ \/ __  / __/ __ \/ __ \
/ /__/ / /_/ / / / / / / /_/ / /_/ /_/ / /_/ /
\___/_/\__,_/_/ /_/ /_/\__,_/\__/\____/ .___/
                                     /_/
Connecting to: /tmp/clamd.sock
VERSION command unavailable, consider enabling it in the clamd configuration.

And when EnableStatsCommand no:

❯ ./install/bin/clamdtop
        __                    ____
  _____/ /___ _____ ___  ____/ / /_____  ____
 / ___/ / __ `/ __ `__ \/ __  / __/ __ \/ __ \
/ /__/ / /_/ / / / / / / /_/ / /_/ /_/ / /_/ /
\___/_/\__,_/_/ /_/ /_/\__,_/\__/\____/ .___/
                                     /_/
Connecting to: /tmp/clamd.sock
STATS command unavailable, consider enabling it in the clamd configuration.

Comment thread freshclam/notify.c Resolved
Comment thread clamdtop/clamdtop.c Resolved
@ChaoticByte

Copy link
Copy Markdown
Contributor Author

Fixed the formatting issues, ran the check locally & it succeeded

@ChaoticByte

ChaoticByte commented Jun 3, 2025 •

Copy link
Copy Markdown
Contributor Author

It would be a little bit better if clamdscan were to return the local version of the clamdscan program if the VERSION command is unavailable, as it does when clamd is not running. E.g.:

❯ ./install/bin/clamdscan --version
ERROR: Could not connect to clamd on LocalSocket /tmp/clamd.sock: No such file or directory
ClamAV 1.5.0-beta

I'm thinking something like this:

diff --git a/clamdscan/client.c b/clamdscan/client.c
index 54f28c225..0caf4ddc4 100644
--- a/clamdscan/client.c
+++ b/clamdscan/client.c
@@ -357,6 +357,14 @@ int get_clamd_version(const struct optstruct *opts)
             logg(LOGG_ERROR, "Error occurred while receiving version information.\n");
             break;
         }
+
+        // Check if the response was "COMMAND UNAVAILABLE"
+        if (len >= 19 && memcmp(buff, "COMMAND UNAVAILABLE", 19) == 0) {
+            logg(LOGG_WARNING, "VERSION command disabled in clamd.\n");
+            closesocket(sockd);
+            return 2;
+        }
+
         printf("%s\n", buff);
     }

-> thanks for the suggested code, I only changed the warning and comment a little :D

@ChaoticByte
ChaoticByte requested a review from val-ms June 3, 2025 20:00

@val-ms val-ms left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love it! Thanks so much for your thoughtful effort on this work.

@val-ms
val-ms merged commit a3be0d2 into Cisco-Talos:main Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants