Linux Labs

Server security / Stage 7 of 10

A service with least privilege

Not running Starting usually ~20 s
Leave the lesson

Step 5 of 6 · lab · 25 minmedium

The service can do anything

The helpdesk ticket service runs as root without a single restriction — that has to change before the security review. Restrict it with systemd so that it keeps taking and storing tickets.

Situation

The lab-tickets service — the tickets-api application — takes helpdesk tickets. Its unit was written in a hurry: the service runs as root, and nothing stops it from doing anything root can do. The security review is next week, and systemd-analyze security lab-tickets.service rates the service 9.6 UNSAFE right now.

The tickets live in /var/lib/tickets, the API key in /etc/tickets/api.key. The package created the tickets account for the service, but nobody ever put it into the unit. How to talk to the service is described in /usr/share/doc/tickets-api/README:

curl -s http://127.0.0.1:8097/health
key=$(sudo cat /etc/tickets/api.key)
curl -s -H "X-Api-Key: $key" http://127.0.0.1:8097/tickets

Task

  1. lab-tickets is running and enabled: the earlier tickets are in place, and a new ticket (POST /tickets) is saved in /var/lib/tickets. The application code stays the same.
  2. The service process does not run as root, has no privileges and runs with NoNewPrivileges=yes. The API key can be read by root and by the service itself — nobody else.
  3. ProtectSystem=strict: the service cannot write to /etc, /usr, /var/lib, /var/log or /opt.
  4. systemd-analyze security lab-tickets.service — 4.9 or lower, rated OK.

There is no internet on the machine; everything you need is already installed. Run administrative commands with sudo.

Lab · on your own

No commands are given in this lab: type them in the terminal yourself. Goals are checked on the machine after every command.

Goals

0 of 4
  • lab-tickets is running and enabled, the earlier tickets are in place, a new one is saved in /var/lib/tickets; the application code is unchanged — not done yet
  • The service process is not root, has no privileges and runs with NoNewPrivileges; an outside user cannot read the API key — not done yet
  • ProtectSystem=strict is in effect — the service cannot write to /etc, /usr, /var/lib, /var/log or /opt — not done yet
  • systemd-analyze security lab-tickets.service — 4.9 or lower (rated OK) — not done yet
Ctrl+Enter
Starts the machine node1 · Ubuntu 24.04

The machine is starting

Hints

0 of 3

Lab goals

0 of 4
  • lab-tickets is running and enabled, the earlier tickets are in place, a new one is saved in /var/lib/tickets; the application code is unchanged — not done yet
  • The service process is not root, has no privileges and runs with NoNewPrivileges; an outside user cannot read the API key — not done yet
  • ProtectSystem=strict is in effect — the service cannot write to /etc, /usr, /var/lib, /var/log or /opt — not done yet
  • systemd-analyze security lab-tickets.service — 4.9 or lower (rated OK) — not done yet
node1 Ubuntu 24.04

Lab goals

0 of 4
  • lab-tickets is running and enabled, the earlier tickets are in place, a new one is saved in /var/lib/tickets; the application code is unchanged — not done yet
  • The service process is not root, has no privileges and runs with NoNewPrivileges; an outside user cannot read the API key — not done yet
  • ProtectSystem=strict is in effect — the service cannot write to /etc, /usr, /var/lib, /var/log or /opt — not done yet
  • systemd-analyze security lab-tickets.service — 4.9 or lower (rated OK) — not done yet
Loading…