Practical Tips for Hardening Java Applications

shaunmsmith 95 views 78 slides Jun 03, 2024
Slide 1
Slide 1 of 78
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78

About This Presentation

Recent high profile vulnerability exploits have increased the focus on Java security. But application security is more than just keeping up with the latest JDK security fixes and running scanners. You can take active steps to harden your application by reducing the attack surface area of both the ap...


Slide Content

Practical Tips for Hardening Java Applications Shaun Smith Senior Director, Product & Developer Relations Oracle Labs @ shaunsmith (@ mastodon.social )

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 2 Photo by Christian J. on Unsplash

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 3 Photo by Shaun Smith

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 4 Photo by Christian J. on Unsplash

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 5 Photo by Christin Hume on Unsplash

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 6 Photo by Luis Sánchez on Unsplash

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 7 Photo by Pixabay : https:// www.pexels.com /photo/two-people-hiking-532803/ Photo by Sergey Fokin on Unsplash Photo by Laila Klinsmann: https:// www.pexels.com /photo/depth-of-field-photography-of-woman-riding-brown-horse-883630/

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 8 Photo by Antonin Duallia on Unsplash

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 9 Hardening (computing) https:// en.wikipedia.org /wiki/Hardening_(computing)

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 10

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 11

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 12

Software supply chain https:// en.wikipedia.org /wiki/ Software_supply_chain 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 13

CycloneDX Maven/Gradle Syft g enerates a software b ill of m aterials from container images and filesystems. Generating an SBOM 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 14

Common Vulnerabilities and Exposures 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 15 https:// en.wikipedia.org /wiki/ Common_Vulnerabilities_and_Exposures

nvd.nist.gov / 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 16

CVE Detection 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 17 And many more..

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 18 SBOMs and CVEs Cyclone DX / Syft / Spring PetClinic

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 19

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 20 Thursday 14:10 - 14:40

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 21 Minimize Dependencies ...and keep them up to date

Always upgrade to the latest patch release of the JDK 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 22

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 23

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 24

jwebserver —Our Example App Copyright © 2024, Oracle and/or its affiliates 25 https:// blogs.oracle.com / javamagazine /post/java-18-simple-web-server 2024-05-08

Copyright © 2024, Oracle and/or its affiliates 26 https:// blogs.oracle.com / javamagazine /post/java-18-simple-web-server 2024-05-08

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 27 Demo! jwebserver

785 MB Debian Slim + JDK 21 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 28 FROM debian:12-slim WORKDIR /web RUN apt-get update && \ apt-get install -y wget && \ apt-get clean && \ wget -q https:// download.oracle.com / graalvm /21/archive/graalvm-jdk-21.0.2_linux-x64_bin.tar.gz -O graalvm.tar.gz && \ tar - xf graalvm.tar.gz && \ rm -f graalvm.tar.gz COPY index.html /web/ index.html EXPOSE 8000 ENTRYPOINT ["/web/graalvm-jdk-21.0.2+13.1/bin/ jwebserver ", "-b", "0.0.0.0", "-d", "/web"]

785 MB Debian Slim + JDK 21 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 29

436 MB Eclipse Temurin JDK 21 (Ubuntu) 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 30 FROM eclipse-temurin:21 COPY index.html /web/ index.html EXPOSE 8000 ENTRYPOINT ["/opt/java/ openjdk /bin/ jwebserver ", "-b", "0.0.0.0", "-d", "/web"]

436 MB Eclipse Temurin JDK 21 (Ubuntu) 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 31

Attack Surface Area Metrics 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 32

" Distroless " images contain only your application and its runtime dependencies . They do not contain package managers, shells or any other programs you would expect to find in a standard Linux distribution. https:// github.com / GoogleContainerTools / distroless /blob/main/ README.md Copyright © 2024, Oracle and/or its affiliates 33 2024-05-08

Distroless Images 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 34

Distroless Images 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 35 For statically linked applications—no libc For “mostly” statically linked applications—has libc For JVM-based applications—no JDK, just required libs Full JDK—with required libs

Distroless Java 21 (Debian 12) 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 36 For statically linked applications—no libc For “mostly” statically linked applications—has libc For JVM-based applications—no JDK, just required libs Full JDK—with required libs

192 MB Distroless Java 21 (Debian 12) 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 37 FROM gcr.io / distroless /java21-debian12 COPY index.html /web/ index.html EXPOSE 8000 ENTRYPOINT ["/opt/java/ openjdk /bin/ jwebserver ", "-b", "0.0.0.0", "-d", "/web"]

192 MB Distroless Java 21 (Debian 12) 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 38

Attack Surface Area Metrics 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 39

jlink 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 40

Remove unnecessary modules jlink 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 41

Remove unnecessary modules jlink 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 42

Distroless Java Base 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 43 For statically linked applications—no libc For “mostly” statically linked applications—has libc For JVM-based applications—no JDK, just required libs Full JDK—with required libs

128 MB Distroless Java Base— Jlink 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 44 FROM container- registry.oracle.com / graalvm /jdk:21 AS build RUN jlink \ --module-path ${JAVA_HOME}/ jmods \ --add-modules jdk.httpserver \ --verbose \ --strip-debug \ --compress zip-9 \ --no-header-files \ --no-man-pages \ --strip-java-debug-attributes \ --output jwebserver-jlink FROM gcr.io / distroless /java-base-debian12 COPY --from=build /build/ jwebserver-jlink / usr /lib/java COPY index.html /web/ index.html EXPOSE 8000 ENTRYPOINT ["/ usr /lib/java/bin/ jwebserver ", "-b", "0.0.0.0", "-d", "/web"]

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 45 Demo! jlink / jwebserver

128 MB Distroless Java Base— Jlink 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 46

Attack Surface Area Metrics 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 47

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 48 How can we do better?

GraalVM Native Image compiles applications Ahead-of-Time (AOT) into platform native executables. Oracle GraalVM Native Image Copyright © 2024, Oracle and/or its affiliates 49 .class .jar .class .jar Windows Executable macOS Executable Linux Executable 2024-05-08

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 50 Demo! GraalVM Native Image

Native Image Dead Code Elimination #8 114.2 [2/8] Performing analysis... [******] (97.9s @ 3.02GB) #8 114.2 39,261 reachable types ( 93.3% of 42,095 total) #8 114.3 60,730 reachable fields ( 63.4% of 95,790 total) #8 114.5 211,215 reachable methods ( 65.8% of 321,005 total) #8 114.5 11,974 types, 930 fields, and 14,499 methods registered for reflection #8 114.5 65 types, 67 fields, and 57 methods registered for JNI access Spring PetClinic —A Larger Example 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 51

Native Image Dead Code Elimination #8 114.2 [2/8] Performing analysis... [******] (97.9s @ 3.02GB) #8 114.2 39,261 reachable types ( 93.3% of 42,095 total) #8 114.3 60,730 reachable fields ( 63.4% of 95,790 total) #8 114.5 211,215 reachable methods ( 65.8% of 321,005 total) #8 114.5 11,974 types, 930 fields, and 14,499 methods registered for reflection #8 114.5 65 types, 67 fields, and 57 methods registered for JNI access Spring PetClinic —A Larger Example 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 52

Native Image Dead Code Elimination #8 114.2 [2/8] Performing analysis... [******] (97.9s @ 3.02GB) #8 114.2 39,261 reachable types ( 93.3% of 42,095 total) #8 114.3 60,730 reachable fields ( 63.4% of 95,790 total) #8 114.5 211,215 reachable methods ( 65.8% of 321,005 total) #8 114.5 11,974 types, 930 fields, and 14,499 methods registered for reflection #8 114.5 65 types, 67 fields, and 57 methods registered for JNI access Spring PetClinic —A Larger Example 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 53 Removed 2,834 Classes, 35,060 Fields, 109,790 Methods

Reduced application and dependent code surface of vulnerability—only Classes/Fields/Methods proven reachable by the application are included in the image Fixed resources—all defined at build time No new unknown code can be loaded at run time—you know what is in your app at build time Remove runtime dependency on XML/JSON parsers by parsing config files at build time, e.g., Spring AOT and Micronaut AOT Only includes GC implementation specified at build time Only includes (large) monitoring features (JMX, JFR, etc.) explicitly Reflection and deserialization is disabled by default and needs an explicit include list No Just-in-time compiler crashes, wrong compilations, and “JIT spraying” is impossible Native Image—Hardening Features 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 54

Native Image Benefits 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 55 *Relative results consistent on different hardware configurations Fast Start & Scale https:// quarkus.io / 80% less memory with Native Image https:// helidon.io /# microprofile 57% less memory with Native Image

2024-05-08 Copyright © 2024, Oracle and/or its affiliates 56 Friday 10:00 – 10:50

Distroless Java Base 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 57 For statically linked applications—no libc For “mostly” statically linked applications—has libc For JVM-based applications—no JDK, just required libs Full JDK—with required libs

Distroless Java Base—Dynamically Linked Executable 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 58 glibc stdlibc ++, zlib , etc. Application Code Fully Dynamically Linked Executable

48.3 MB Distroless Java Base—Dynamically Linked Executable 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 59 FROM container- registry.oracle.com / graalvm /native-image:21 AS nativebuild WORKDIR /build RUN native-image -Ob --enable- sbom = cyclonedx -m jdk.httpserver -o jwebserver.dynamic FROM gcr.io / distroless /java-base-debian12 COPY --from= nativebuild /build/ jwebserver.dynamic / COPY index.html /web/ index.html EXPOSE 8000 ENTRYPOINT ["/ jwebserver.dynamic ", "-b", "0.0.0.0", "-d", "/web"]

48.3 MB Distroless Java Base—Dynamically Linked Executable 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 60

Attack Surface Area Metrics 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 61

GraalVM Native Executable Linking and Containerization Options Copyright © 2024, Oracle and/or its affiliates 62 glibc stdlibc ++, zlib , etc. Application Code Fully Dynamic OS must include all dynamically linked libs 2024-05-08 gcr.io / distroless / java-base -debian12 48.3 MB

GraalVM Native Executable Linking and Containerization Options Copyright © 2024, Oracle and/or its affiliates 63 glibc stdlibc ++, zlib , etc. Application Code Fully Dynamic OS must include all dynamically linked libs Application Code glibc stdlibc ++, zlib , etc. Mostly Static OS only need provide libc libs 2024-05-08 gcr.io / distroless / java-base -debian12 gcr.io / distroless / base -debian12 48.3 MB 35.2 MB

GraalVM Native Executable Linking and Containerization Options Copyright © 2024, Oracle and/or its affiliates 64 glibc stdlibc ++, zlib , etc. Application Code Fully Dynamic OS must include all dynamically linked libs Application Code Application Code glibc stdlibc ++, zlib , etc. Mostly Static musl libc stdlibc ++, zlib , etc. Fully Static OS only need provide libc libs No libs provided by OS 2024-05-08 gcr.io / distroless / java-base -debian12 gcr.io / distroless / base -debian12 gcr.io / distroless / static -debian12 48.3 MB 35.2 MB 17.1 MB

Attack Surface Area Metrics 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 65

21.9 MB Alpine—Fully Static Executable 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 66 FROM container- registry.oracle.com / graalvm /native-image:21-muslib AS nativebuild WORKDIR /build RUN native-image -Ob --enable- sbom = cyclonedx --static -- libc = musl -m jdk.httpserver -o jwebserver.static FROM alpine:3 COPY --from= nativebuild /build/ jwebserver.static / COPY index.html /web/ index.html EXPOSE 8000 ENTRYPOINT ["/ jwebserver.static ", "-b", "0.0.0.0", "-d", "/web"]

21.9 MB Alpine—Fully Static Executable 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 67

Attack Surface Area Metrics 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 68

This image is most useful in the context of building base...or super minimal images (that contain only a single binary and whatever it requires ...)” Copyright © 2024, Oracle and/or its affiliates 69 scratch 2024-05-08

14.5 MB Scratch 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 70 FROM container- registry.oracle.com / graalvm /native-image:21-muslib AS nativebuild WORKDIR /build RUN native-image -Ob --enable- sbom = cyclonedx --static -- libc = musl -m jdk.httpserver -o jwebserver.static FROM scratch COPY --from= nativebuild /build/ jwebserver.static / COPY index.html /web/ index.html EXPOSE 8000 ENTRYPOINT ["/ jwebserver.static ", "-b", "0.0.0.0", "-d", "/web"]

14.5 MB Scratch—Fully Static Executable 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 71

Attack Surface Area Metrics 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 72

Where We Started 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 73

Jlink 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 74 Harden JDK by removing unnecessary modules

Distroless 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 75 Harden Operating System by removing unnecessary components

Harden by removing unnecessary Classes, Methods, Fields, and JDK infrastructure GraalVM Native Image 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 76

Reduce 3 rd party dependencies Generate SBOMs for your application to track deps and identify CVEs Remove unnecessary JDK modules using jlink Regularly upgrade dependencies and your JDK to the latest release Use minimal container images with “just enough operating system” Use GraalVM Native Image to minimize application attack surface area Summary—Hardening Tips 2024-05-08 Copyright © 2024, Oracle and/or its affiliates 77

Shaun Smith @ shaunsmith (@ mastodon.social )