Linux system hardening thanks to systemd Timothe Ravier French - - PowerPoint PPT Presentation

linux system hardening thanks to systemd
SMART_READER_LITE
LIVE PREVIEW

Linux system hardening thanks to systemd Timothe Ravier French - - PowerPoint PPT Presentation

Linux system hardening thanks to systemd Timothe Ravier French Network and Information Security Agency (ANSSI) RMLL 2017 Goal of this talk Goal of this talk Increase the security of standard Linux distributions Use security features


slide-1
SLIDE 1

Linux system hardening thanks to systemd

Timothée Ravier

French Network and Information Security Agency (ANSSI) RMLL 2017

slide-2
SLIDE 2

Goal of this talk

slide-3
SLIDE 3

Goal of this talk

◮ Increase the security of standard Linux distributions ◮ Use security features made available to userspace by the Linux kernel ◮ Take advantage of their integration into systemd ◮ Simplify deployments and help system maintenance

ANSSI Linux system hardening thanks to systemd 3/25

slide-4
SLIDE 4

systemd “how-to” in three slides

slide-5
SLIDE 5

systemd?

◮ Integrated in most Linux distributions as a replacement for SysVinit ◮ Handle system boot up and manage system services ◮ Responsible for environment setup for system daemons ◮ Init scripts are replaced by declarative configuration files: units

ANSSI Linux system hardening thanks to systemd 5/25

slide-6
SLIDE 6

Unit?

To display the current configuration of a service:

# systemctl cat php -fpm.service # /usr/lib/systemd/system/php -fpm.service [Unit] Description=The PHP FastCGI Process Manager After=network.target [Service] Type=notify PIDFile =/run/php -fpm/php -fpm.pid ExecStart =/usr/bin/php -fpm --nodaemonize PrivateTmp=true [Install] WantedBy=multi -user.target

ANSSI Linux system hardening thanks to systemd 6/25

Command

slide-7
SLIDE 7

Unit?

To display the current configuration of a service:

# systemctl cat php -fpm.service # /usr/lib/systemd/system/php -fpm.service [Unit] Description=The PHP FastCGI Process Manager After=network.target [Service] Type=notify PIDFile =/run/php -fpm/php -fpm.pid ExecStart =/usr/bin/php -fpm --nodaemonize PrivateTmp=true [Install] WantedBy=multi -user.target

ANSSI Linux system hardening thanks to systemd 6/25

Corresponding file

slide-8
SLIDE 8

Unit?

To display the current configuration of a service:

# systemctl cat php -fpm.service # /usr/lib/systemd/system/php -fpm.service [Unit] Description=The PHP FastCGI Process Manager After=network.target [Service] Type=notify PIDFile =/run/php -fpm/php -fpm.pid ExecStart =/usr/bin/php -fpm --nodaemonize PrivateTmp=true [Install] WantedBy=multi -user.target

ANSSI Linux system hardening thanks to systemd 6/25

Who? When?

slide-9
SLIDE 9

Unit?

To display the current configuration of a service:

# systemctl cat php -fpm.service # /usr/lib/systemd/system/php -fpm.service [Unit] Description=The PHP FastCGI Process Manager After=network.target [Service] Type=notify PIDFile =/run/php -fpm/php -fpm.pid ExecStart =/usr/bin/php -fpm --nodaemonize PrivateTmp=true [Install] WantedBy=multi -user.target

ANSSI Linux system hardening thanks to systemd 6/25

What? How?

slide-10
SLIDE 10

Unit?

To display the current configuration of a service:

# systemctl cat php -fpm.service # /usr/lib/systemd/system/php -fpm.service [Unit] Description=The PHP FastCGI Process Manager After=network.target [Service] Type=notify PIDFile =/run/php -fpm/php -fpm.pid ExecStart =/usr/bin/php -fpm --nodaemonize PrivateTmp=true [Install] WantedBy=multi -user.target

ANSSI Linux system hardening thanks to systemd 6/25

Why?

slide-11
SLIDE 11

Example: switching to an unprivileged user and group

Edit the service configuration:

# systemctl edit php -fpm.service

ANSSI Linux system hardening thanks to systemd 7/25

slide-12
SLIDE 12

Example: switching to an unprivileged user and group

Edit the service configuration:

# systemctl edit php -fpm.service

add the following content:

[Service] User=http Group=www

ANSSI Linux system hardening thanks to systemd 7/25

slide-13
SLIDE 13

Example: switching to an unprivileged user and group

Edit the service configuration:

# systemctl edit php -fpm.service

add the following content:

[Service] User=http Group=www

and make those changes effective:

# systemctl daemon -reload # systemctl restart php -fpm.service

ANSSI Linux system hardening thanks to systemd 7/25

slide-14
SLIDE 14

Taking advantage of security features from the Linux kernel

slide-15
SLIDE 15

Filtering access to system calls using seccomp-bpf

Concept

◮ Restrict which system calls are available to a process ◮ Also applies to child processes

ANSSI Linux system hardening thanks to systemd 9/25

slide-16
SLIDE 16

Filtering access to system calls using seccomp-bpf

Concept

◮ Restrict which system calls are available to a process ◮ Also applies to child processes

Example

[Service] SystemCallFilter =~ chroot SystemCallFilter =~ @obsolete

ANSSI Linux system hardening thanks to systemd 9/25

slide-17
SLIDE 17

Filtering access to system calls using seccomp-bpf

Concept

◮ Restrict which system calls are available to a process ◮ Also applies to child processes

Example

[Service] SystemCallFilter =~ chroot SystemCallFilter =~ @obsolete

Beware

◮ Can be bypassed with ptrace on kernels < 4.8 ◮ Solution: add a filter for the ptrace system call:

[Service] SystemCallFilter =~ ptrace

ANSSI Linux system hardening thanks to systemd 9/25

slide-18
SLIDE 18

Linux capabilities

Concept

◮ Restrict privileges granted to a process (potentially running as root) ◮ Grant a subset of root privileges to an unprivileged process

ANSSI Linux system hardening thanks to systemd 10/25

slide-19
SLIDE 19

Linux capabilities

Concept

◮ Restrict privileges granted to a process (potentially running as root) ◮ Grant a subset of root privileges to an unprivileged process

Example

[Service] CapabilityBoundingSet = CAP_NET_BIND_SERVICE AmbientCapabilities = CAP_NET_BIND_SERVICE

ANSSI Linux system hardening thanks to systemd 10/25

slide-20
SLIDE 20

Linux capabilities

Concept

◮ Restrict privileges granted to a process (potentially running as root) ◮ Grant a subset of root privileges to an unprivileged process

Example

[Service] CapabilityBoundingSet = CAP_NET_BIND_SERVICE AmbientCapabilities = CAP_NET_BIND_SERVICE

Beware

◮ Some capabilities are equivalent to full root privileges ◮ Avoid blacklists. Whitelist only the capabilities effectively used

For more details, see: https://forums.grsecurity.net/viewtopic.php?f=7&t=2522

ANSSI Linux system hardening thanks to systemd 10/25

slide-21
SLIDE 21

Mount namespaces

Concept

◮ Each service can get its own filesystem hierarchy ◮ Hide arbitrary paths or turn them read-only

ANSSI Linux system hardening thanks to systemd 11/25

slide-22
SLIDE 22

Mount namespaces

Concept

◮ Each service can get its own filesystem hierarchy ◮ Hide arbitrary paths or turn them read-only

Example

[Service] InaccessiblePaths =/etc/secrets ProtectSystem =full

ANSSI Linux system hardening thanks to systemd 11/25

slide-23
SLIDE 23

Mount namespaces

Concept

◮ Each service can get its own filesystem hierarchy ◮ Hide arbitrary paths or turn them read-only

Example

[Service] InaccessiblePaths =/etc/secrets ProtectSystem =full

Beware

◮ Reversible if CAP_SYS_ADMIN or mount system call is available:

[Service] CapabilityBoundingSet =~ CAP_SYS_ADMIN SystemCallFilter =~ @mount

ANSSI Linux system hardening thanks to systemd 11/25

slide-24
SLIDE 24

Getting your hands dirty (cow?)

slide-25
SLIDE 25

Practical example: sandboxing the Dirty CoW

◮ Vulnerability CVE-2016-5195 ◮ Local root made public in October 2016 ◮ Impacted every kernel from the version 2.6.22, released in 2007 ◮ Race condition in the memory management code handling Copy-on-Write

ANSSI Linux system hardening thanks to systemd 13/25

slide-26
SLIDE 26

Practical example: sandboxing the Dirty CoW

Exploit vector

◮ Race condition triggered by the madvise system call

Options to mitigate the impact

◮ Block the madvise system call

Configuration

[Service] SystemCallFilter =~ madvise

ANSSI Linux system hardening thanks to systemd 14/25

slide-27
SLIDE 27

Practical example: sandboxing the Dirty CoW

Exploit vector

◮ Indirect access to memory using the ptrace system call and

/proc/self/mem Options to mitigate the impact

◮ Block the ptrace system call ◮ Remove access to the proc virtual filesystem

Configuration

[Service] SystemCallFilter =~ ptrace InaccessiblePaths =/ proc

See https://lists.freedesktop.org/archives/systemd-devel/2017-April/038634.html and https://github.com/systemd/systemd/pull/5985 for more details.

ANSSI Linux system hardening thanks to systemd 15/25

slide-28
SLIDE 28

Practical example: sandboxing the Dirty CoW

Exploit vector

◮ Vulnerable code may be reachable from drivers exposed in /dev

Options to mitigate the impact

◮ Remove access to most hardware drivers available from /dev

Configuration

[Service] PrivateDevices =yes

ANSSI Linux system hardening thanks to systemd 16/25

slide-29
SLIDE 29

Practical example: The Good, the Bad and the socket

◮ Vulnerability CVE-2016-8655 ◮ Local root ◮ Race condition in AF_PACKET type sockets leading to Use-After-Free in

kernel context

◮ Creating AF_PACKET sockets requires CAP_NET_RAW ◮ May be obtained via unprivileged user namespace (Linux 3.8)

ANSSI Linux system hardening thanks to systemd 17/25

slide-30
SLIDE 30

Practical example: The Good, the Bad and the socket

Exploit vector

◮ AF_PACKET sockets

Options to mitigate the impact

◮ Restrict socket type availability

Configuration Minimal version with a blacklist:

[Service] RestrictAddressFamilies =~ AF_PACKET

Better option using a whitelist:

[Service] RestrictAddressFamilies =AF_INET AF_INET6 AF_UNIX

ANSSI Linux system hardening thanks to systemd 18/25

slide-31
SLIDE 31

Practical example: The Good, the Bad and the socket

Exploit vector

◮ CAP_NET_RAW capability

Options to mitigate the impact

◮ Block acquisition of the CAP_NET_RAW capability

Configuration

[Service] CapabilityBoundingSet =~ CAP_NET_RAW

ANSSI Linux system hardening thanks to systemd 19/25

slide-32
SLIDE 32

Practical example: The Good, the Bad and the socket

Exploit vector

◮ Unrestricted availability of unprivileged user namespace

Options to mitigate the impact

◮ Restrict access to user namespaces

Configuration

[Service] RestrictNamespaces =~ user

Notice

◮ Requires systemd 233

ANSSI Linux system hardening thanks to systemd 20/25

slide-33
SLIDE 33

Practical example: systemd versus the crashing tweet

◮ Vulnerability CVE-2016-7795 ◮ Denial of Service targeting systemd ◮ Raise an assertion in the daemon running as PID 1 ◮ Pause process execution thus reducing functionality available on the

system

ANSSI Linux system hardening thanks to systemd 21/25

slide-34
SLIDE 34

Practical example: systemd versus the crashing tweet

Exploit vector

◮ Incorrect handling of empty notification events sent through

/run/systemd/notify Options to mitigate the impact

◮ Restrict access to the /run/systemd/notify socket

Configuration

[Service] InaccessiblePaths =/run/systemd

ANSSI Linux system hardening thanks to systemd 22/25

slide-35
SLIDE 35

Conclusion

slide-36
SLIDE 36

Conclusion

◮ Simplified interface to help setup kernel security features ◮ Easy to setup and maintain ◮ Does not replace applying updates ◮ Hardening features applied only to system services

ANSSI Linux system hardening thanks to systemd 24/25

slide-37
SLIDE 37

Thank you Contact:

timothee.ravier@ssi.gouv.fr travier@mastodon.etalab.gouv.fr @siosm