diff --git a/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/Dockerfile b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/Dockerfile new file mode 100644 index 0000000..381df27 --- /dev/null +++ b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/Dockerfile @@ -0,0 +1,34 @@ +# Stage 1: Build +FROM ubuntu:22.04 AS builder + +WORKDIR /app + +RUN set -xe; \ + apt-get -yqq update; \ + apt-get -yqq install build-essential \ + ; + +COPY ./rwslotmachine4.c . +COPY ./Makefile.sol ./Makefile + +RUN make + +# Stage 2: Runtime +FROM ubuntu:22.04 + +RUN set -xe; \ + apt-get -yqq update;\ + apt-get -yqq install libstdc++6; \ + rm -rf /var/lib/apt/lists/* \ +; + +WORKDIR /app + +# Copy compiled binary from builder stage +COPY --from=builder /app/rwslotmachine4 . + +# Expose port +EXPOSE 31347 + +# Run the executable +CMD ["/app/rwslotmachine4"] diff --git a/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/Makefile b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/Makefile new file mode 100644 index 0000000..0323ce0 --- /dev/null +++ b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/Makefile @@ -0,0 +1,17 @@ +PORT ?= 31347 +IMG_NAME ?= rwslotmachine4 +CONT_NAME ?= $(IMG_NAME)-cnt + +build: + docker build -t $(IMG_NAME) -f Dockerfile . + +run: build + docker run -d --rm -p $(PORT):31337 --name $(CONT_NAME) -t $(IMG_NAME) + +stop: + -docker stop $(CONT_NAME) + +clean: stop + docker rm $(CONT_NAME) + +.PHONY: build run stop clean diff --git a/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/Makefile.sol b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/Makefile.sol new file mode 100644 index 0000000..960783c --- /dev/null +++ b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/Makefile.sol @@ -0,0 +1,11 @@ +TARGET = rwslotmachine4 + +all: $(TARGET) + +$(TARGET): rwslotmachine4.c + $(CC) -o $(TARGET) rwslotmachine4.c + +clean: + -rm -f $(TARGET) + +.PHONY: all clean diff --git a/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/README.md b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/README.md new file mode 100644 index 0000000..ac5aa89 --- /dev/null +++ b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/README.md @@ -0,0 +1,27 @@ +# Building and Running + +The included Makefile automates building and running the Docker container. + +- Build the Docker image: + + ```bash + make build + ``` + +- Run the container: + + ```bash + make run + ``` + +- Stop the running container: + + ```bash + make stop + ``` + +- Clean up container state: + + ```bash + make clean + ``` diff --git a/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/rwslotmachine4.c b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/rwslotmachine4.c index 753493b..e71f1e1 100644 --- a/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/rwslotmachine4.c +++ b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/04-rwslotmachine4/sol/rwslotmachine4.c @@ -6,7 +6,18 @@ #define MAX_SLOTS 32 -long read_int(int base); +long read_int(int base) +{ + char buf[64]; + char *endptr; + + if (fgets(buf, sizeof(buf), stdin) == NULL) { + puts("Failed to read input!"); + exit(1); + } + + return strtol(buf, &endptr, base); +} long slots[MAX_SLOTS];