A week of hardening: what a code review still found on my Nextclouds

My Nextclouds counted as well hardened. A thorough review still found a handful of gaps. For a week, every change went to the test server first, then to production – with a few surprises.

Comic: in the blue-lit data center corridor the man in the white T-shirt kneels in front of a server rack with a built-in round vault door and tightens its bolts with a big wrench; a plate on the door reads "HARDENED", golden padlocks hang on the cables of the neighbouring racks, a clipboard with green check marks lies on the floor

🌐 Also available in: Deutsch · Français · Español · Italiano · Português · Nederlands · Polski

In short: My Nextcloud servers counted as well hardened: SELinux in enforcing mode, an own domain for every service, a firewall, the CIS benchmark. Still, I had Claude do a thorough review, once of the Ansible code and once of the running servers. It found no open door, but a handful of places where an attacker who already has a foot in the door would get further too easily. For a week, every change first went to the test server, stayed there at least one night and only then reached the two production servers. There were a few surprises along the way.

Table of Contents

🔍 What the review found

The question was not “Can someone get in?” but “What can someone do who is already in?”, for example through a flaw in a Nextcloud app. Behind every point, a link leads to the commit with the change, that is, to exactly the lines it took. The most important findings:

  • Valkey/Redis knew every command. Whoever reached Redis from the Nextcloud pod could call FLUSHALL, CONFIG or DEBUG. Now the default user has an ACL without these admin commands. Nextcloud does not need them. → 1b77364
  • MariaDB and Redis accepted connections from anywhere the firewall allowed. Now systemd (IPAddressDeny/IPAddressAllow) only allows the server itself and the pod network. Even if a firewall rule were ever wrong, the database would stay closed. → e8e4b31
  • In the Kubernetes namespace default everything was allowed. A pod that ends up there could have reached every service in the cluster. Now a NetworkPolicy blocks everything there that is not explicitly allowed. → 1a06d9e
  • nginx in the Nextcloud pod executed any .php file. If an attacker managed to upload a PHP file, it would have been executable. Now only Nextcloud’s real entry points run as PHP. On top of that, the Proxy header is cleared, an old flaw called httpoxy. → de8f035
  • The admin password stayed in the pod after the installation. It is only needed for the first installation and is now gone afterwards. → f79c561
  • SSH still offered older algorithms. Now key exchange, ciphers and MACs are pinned, with the post-quantum method first. → 8bac9ca
  • The monitoring services ran without a sandbox or with too many rights. The MySQL exporter had no sandbox at all (systemd-analyze security: 9.2 “UNSAFE”, now 1.5) and only needed PROCESS instead of far-reaching rights. node_exporter, Prometheus, Loki and Alloy got syscall filters, no executable writable memory and no namespaces of their own. Alloy can no longer read the host’s secrets. → 281b111, 1fedb9d, 08a085a
  • Small things with an effect: downloads of K3s, Helm, Prometheus and the exporters are now checked against the vendors’ checksums. Passwords no longer appear in process lists but go through environment variables. → 462a64f, 52ec7af

And three gaps in the monitoring: Valkey was not monitored at all (now there is an own exporter in its own SELinux domain), a MariaDB outage raised no alert, and Nextcloud’s own log did not end up in Loki. → 307d5af, 178e85d, 6eeeab0

🧪 First the test server, then one night, then production

Every change got a switch in the role defaults, off by default. On the test server I switched it on, waited one night, read the logs and SELinux messages and only then changed the default. It paid off:

  • A flood of SELinux messages. node_exporter searched the volumes of the Kubernetes pods on every scrape, about 100 denials per minute. Because of that, the audit log rotated every 70 minutes, and the night’s messages were already gone in the morning. The pod volumes are now excluded from the filesystem collector. → b2d1f38
  • The Redis exporter needed one more rule. It reads the system’s CA bundle. After one night in learning mode exactly this one denial showed up, then the domain went to enforcing. → b2d1f38, f786587
  • A Redis restart logs out every user. The PHP sessions live in Redis. So I rolled out the ACL in the evening, not in the middle of the day. → afd59b7
  • A false alarm. Right after a pod restart, the playbook reported that eleven apps stayed disabled. They were all enabled. The NetworkPolicy only applies to a new pod after a few seconds, and until then it could not reach the app store. The report now reads the real state with occ app:list. → f125add

The takeaway: this week the night on the test server showed a problem twice before it hit production. One switch per change turns the way back into a single line.

⏱️ Where does the time of an upload go?

Since I was in the code anyway, I also measured on the test server. Three results were new to me:

  • The test VM is no fixed yardstick. One day it was three to four times slower than the day before, without a single change from me. A fixed Python loop as a reference showed: the VM’s host was simply busier. Since then every measurement script measures this reference too, and I only compare within one session.
  • A single file upload costs about 100 database queries, almost as many commits and about 100 Redis commands. The Activity app, which writes the history on the right of the file view, makes uploads 5 to 14 % slower. It stays on anyway, because the history is useful.
  • Timings in the logs: the ingress and nginx in the pod now log the total time, the connection setup and the PHP part of every request. That way a slow request can be placed: TLS at the ingress, passing it through in the pod, or PHP. → 22e3321

Two small settings came out of it: OPcache gets the default value for interned strings again (my own configuration had halved it), and nginx in the pod no longer buffers large downloads in a memory volume of only 16 MiB. One proposal failed: switching off upload buffering at the ingress brought no measurable advantage. → ed05ddd

🐞 And two bugs in the update playbook

During the update on 30 November two more bugs showed up. One had been in the code since the first commit in June:

  • The update playbook restarted Nextcloud on every run. occ app:update --all always writes a line, even “All apps are up-to-date”. The playbook counted this line as an updated app. → d900006
  • occ ran in the wrong pod. Right after a rollout, the old pod is still listed while it is being terminated. The playbook took the first entry and talked to a pod that was gone seconds later. Now it takes the newest one. → 9443d5a

Both only showed up because I read the runs instead of only looking at “failed=0”.

🎯 Conclusion

  • Hardening does not end at enforcing mode. All findings were about what an attacker who is already in can do: Redis commands, database access, PHP files, the monitoring’s rights.
  • Every change with a switch and a test night. It costs a few days, but caught two problems before they hit production.
  • Measuring needs a reference. Without the Python loop I would have blamed my own configuration for a performance drop that was really caused by the neighbour on the host.
  • Read the logs, don’t just count. A bug in the update playbook stayed invisible for half a year, because every run ended green.

→ Code: nextcloud_k3s on GitHub


📚 Related posts:

Some text and images on this site are AI-assisted or AI-generated. Details