Skip to content

Commit

Permalink
WIP: add docker-compose and soft-tpm setup
Browse files Browse the repository at this point in the history
Signed-off-by: Tuomo Tanskanen <[email protected]>
  • Loading branch information
tuminoid committed Nov 21, 2024
1 parent b66d859 commit fb4d945
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 0 deletions.
10 changes: 10 additions & 0 deletions security/keylime-poc/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# simple makefile to make life easy

.PHONY: e2e run docker kind verify clean realclean
.PHONY: deps compose

e2e: run verify
"e2e done!"
Expand All @@ -22,3 +23,12 @@ clean:

realclean:
cd scripts; ./clean.sh realclean

deps:
sudo apt install -y docker-compose-plugin tpm2-tools

compose: deps
cd soft-tpm; docker build . -t tpmserver:tumi
./scripts/gen_allowlist.txt > compose/allowlist.txt
cd compose; docker compose up -d
./compose/enable_ima_measurements.sh
1 change: 1 addition & 0 deletions security/keylime-poc/compose/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allowlist.txt
17 changes: 17 additions & 0 deletions security/keylime-poc/compose/agent.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[agent]
uuid = "d432fbb3-d2f1-4a97-9ef7-75bd81c00000"
tpm_ownerpassword = ""
tpm_version = "2"
work_dir = "/var/lib/keylime/secure"
secure_size = "1m"

[cloud_agent]
cloudagent_ip = "127.0.0.1"
cloudagent_port = "9002"
registrar_ip = "127.0.0.1"
registrar_port = "8891"

[tpm]
tpm_ownerpassword = ""
ek_handle = "0x81010000"

82 changes: 82 additions & 0 deletions security/keylime-poc/compose/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
services:
tpm-simulator:
build:
context: ../soft-tpm
dockerfile: ../soft-tpm/Dockerfile
ports:
- "2321:2321"
- "2322:2322"
volumes:
- tpm-state:/var/lib/tpm
# command: tpm_server -port 2321 -rm

keylime-verifier:
image: quay.io/keylime/keylime_verifier
depends_on:
- tpm-simulator
environment:
- TPM2TOOLS_TCTI=mssim:host=tpm-simulator,port=2321
- TCTI=mssim:host=tpm-simulator,port=2321
volumes:
- cv_ca:/var/lib/keylime/cv_ca:rw
ports:
- "8881:8881"
# just create delay so certs are there when it runs
healthcheck:
test: ["CMD-SHELL", "true"]
interval: 5s
timeout: 5s
retries: 5
start_period: 10s
command: keylime_verifier

keylime-registrar:
image: quay.io/keylime/keylime_registrar
depends_on:
- tpm-simulator
- keylime-verifier
environment:
- TPM2TOOLS_TCTI=mssim:host=tpm-simulator,port=2321
- TCTI=mssim:host=tpm-simulator,port=2321
volumes:
- cv_ca:/var/lib/keylime/cv_ca:ro
ports:
- "8891:8891"
# just create delay so certs are there when it runs
healthcheck:
test: ["CMD-SHELL", "true"]
interval: 5s
timeout: 5s
retries: 5
start_period: 10s
command: keylime_registrar

keylime-agent:
image: quay.io/keylime/keylime_agent:master
privileged: true
depends_on:
- tpm-simulator
- keylime-verifier
- keylime-registrar
environment:
- TPM2TOOLS_TCTI=mssim:host=tpm-simulator,port=2321
- TCTI=mssim:host=tpm-simulator,port=2321
- TPM_INTERFACE_TYPE=socsim
# just create delay so certs are there when it runs
healthcheck:
test: ["CMD-SHELL", "true"]
interval: 5s
timeout: 5s
retries: 5
start_period: 10s
volumes:
- /sys/kernel/security:/sys/kernel/security:ro
- ./agent.conf:/etc/keylime/agent.conf:ro
- agent_var_lib_keylime:/var/lib/keylime:rw
- cv_ca:/var/lib/keylime/cv_ca:ro
# command: keylime_agent

volumes:
tpm-state:
cv_ca:
agent_var_lib_keylime:
41 changes: 41 additions & 0 deletions security/keylime-poc/compose/enable_ima_measurement.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
# This is enabling IMA measurement temporarily
# Need to set up grub/boottime parameters for permanent measurements
# If it doesn't work, grub config for safe startup:
# GRUB_CMDLINE_LINUX="$GRUB_CMDLINE_LINUX ima=on integrity_audit=1 lsm=integrity,ima"

# policy is auditing: change "audit" to "measure"

# check requirements (tpm2-tools)
set -e
command -v tpm2_startup &>/dev/null

# don't fail
set -x
set +e

# Enable IMA measurement
echo "1" | sudo tee /sys/kernel/security/ima/policy_update
mkdir -p /etc/ima
sudo tee /etc/ima/ima-policy << 'EOF'
# Default IMA policy
# Don't measure files opened with read-only permissions
dont_measure obj_type=file mask=MAY_READ
# Measure all executed files
audit func=BPRM_CHECK mask=MAY_EXEC
# Measure files mmap()ed for execute
audit func=FILE_MMAP mask=MAY_EXEC
# Measure files opened for write or append
audit func=FILE_CHECK mask=MAY_WRITE uid=0
EOF

# load the ima policy
sudo cat /etc/ima/ima-policy | sudo tee /sys/kernel/security/ima/policy

# Configure TPM PRC - this needs
# setup tpm2-tools to access the tpmserver in docker
export TPM2TOOLS_TCTI="mssim:host=localhost,port=2321"
tpm2_startup -c

# PCR 10 will store IMA measurements
tpm2_pcrextend 10:sha256=0000000000000000000000000000000000000000000000000000000000000000
21 changes: 21 additions & 0 deletions security/keylime-poc/compose/tenant.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
# Run docker-compose up -d first to have infra in place

set -eu

# Register agent with allowlist
docker run --network host \
-e KEYLIME_TENANT_TLS_ENABLED=False \
-e KEYLIME_VERIFIER_TLS_ENABLED=False \
-e KEYLIME_REGISTRAR_TLS_ENABLED=False \
-e KEYLIME_TENANT_ENABLE_INSECURE_VERIFICATION=True \
-v "$(pwd)"/allowlist.txt:/tmp/allowlist.txt:ro \
quay.io/keylime/keylime_tenant \
-v 127.0.0.1 \
-t 127.0.0.1 \
-u d432fbb3-d2f1-4a97-9ef7-75bd81c00000 \
--uuid d432fbb3-d2f1-4a97-9ef7-75bd81c00000 \
--allowlist /tmp/allowlist.txt \
--exclude boot_aggregate \
--tpm_policy="1,3,7" \
-c add
47 changes: 47 additions & 0 deletions security/keylime-poc/scripts/gen_allowlist.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash
# Script to generate file hashes for allowlist
# redirect to target file "allowlist.txt"

set -eu

cat <<EOF
# Allowlist format - use hashes of known good files
exclude: !policy
- boot_aggregate
- ima-buf
- ima-sig
- ima-ng
EOF

echo "# Generated allowlist"
echo "hashes:"

# List of critical directories to measure
DIRS_TO_MEASURE=(
"/bin"
"/sbin"
"/usr/bin"
"/usr/sbin"
"/lib/systemd"
"/usr/lib/systemd"
)

for dir in "${DIRS_TO_MEASURE[@]}"; do
if [[ -d "${dir}" ]]; then
find "${dir}" -type f -exec sha256sum {} \; | while read -r hash file; do
echo " ${file}: ${hash}"
done
fi
done
echo

# Read current IMA measurements
echo "ima:"
sudo cat /sys/kernel/security/ima/ascii_runtime_measurements | while read -r _ hash template file; do
if [[ "${template}" == "ima-ng" ]]; then
echo " ${file}:"
echo " hash: ${hash}"
echo " validation_mask: 0xd"
fi
done
45 changes: 45 additions & 0 deletions security/keylime-poc/soft-tpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Building this image:
# docker build . -t tpmserver:tumi

# Running this image:
# docker run -d --name tpm-server -p 2321:2321 -p 2322:2322 tpmserver:tumi

# Use a debian slim image as base
FROM debian:bullseye-slim

# Set working directory
WORKDIR /tpm

# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
autoconf \
libtool \
libssl-dev \
pkg-config \
git \
wget \
tpm2-tools \
&& rm -rf /var/lib/apt/lists/*

# Clone and build IBM's TPM2 simulator
RUN git clone https://github.com/kgoldman/ibmswtpm2 \
&& cd ibmswtpm2/src \
&& make \
&& mv tpm_server /usr/local/bin/ \
&& cd / \
&& rm -rf /tpm/ibmswtpm2

COPY start.sh /

# Create directory for TPM state
RUN mkdir -p /var/lib/tpm

# Set working directory for TPM state
WORKDIR /var/lib/tpm

# Expose TPM simulator ports
EXPOSE 2321 2322

# Start TPM simulator
CMD ["/start.sh"]
16 changes: 16 additions & 0 deletions security/keylime-poc/soft-tpm/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -eu

# run tpmserver
docker run -d --name tpm-server -p 2321:2321 -p 2322:2322 tpmserver:tumi
sleep 10

# setup tpm2-tools to access the tpmserver in docker
export TPM2TOOLS_TCTI="mssim:host=localhost,port=2321"

# clear the tpm
tpm2_startup -c

# get random 16 hex from tpm to verify it works
tpm2_getrandom --hex 16
12 changes: 12 additions & 0 deletions security/keylime-poc/soft-tpm/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

# Start IBM's TPM server in background
nohup tpm_server &

# Wait for simulator to be ready
sleep 2

export TPM2TOOLS_TCTI="mssim:host=localhost,port=2321"
tpm2_startup -c

tail -f /dev/null

0 comments on commit fb4d945

Please sign in to comment.