For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/dockerfile-avoid-chmod-777.md.
A documentation index is available at /llms.txt.
Using ADD or COPY with the flag --chmod=777 makes files world-writable inside the image. This allows any user or process in the container to modify those files, increasing the risk of tampering, privilege escalation, and data exposure.
Check Dockerfile ADD and COPY instructions for flags that include --chmod=777. This rule inspects instruction flags (resource type dockerfile_container) and flags ADD or COPY commands that specify --chmod=777.
Avoid granting global write permissions. Instead, use more restrictive modes (for example, 0755 for executables or 0644 for regular files), set appropriate ownership with --chown, or apply targeted chown/chmod in a subsequent RUN step rather than using --chmod=777.
Secure examples:
# Use restrictive permissions during copyCOPY --chown=app:app --chmod=0755 bin/ /app/bin/COPY --chown=app:app --chmod=0644 conf.yaml /app/conf.yaml
# Or set ownership/permissions in a separate stepCOPY src/ /app/src/RUN chown -R app:app /app && chmod -R 0755 /app/bin && chmod 0644 /app/conf.yaml
Compliant Code Examples
FROMgolang:1.21-alpineASbuilderLABELmaintainer="security-team@example.com"LABELdescription="Secure build stage for Go application"# Install build dependenciesRUN apk add --no-cache \
git \
gcc \
musl-devWORKDIR/build# Copy go module filesCOPY go.mod go.sum ./RUN go mod download# Negative case 1: ADD with safe permissions (755) for executable scriptsADD --chmod=755 src dst# Copy source codeCOPY . .# Build the applicationRUNCGO_ENABLED=0GOOS=linux go build -a -installsuffix cgo -o app .# Final stageFROMalpine:3.14LABELmaintainer="security-team@example.com"LABELdescription="Secure production image for Go application"LABELversion="1.0.0"# Install CA certificates and other runtime dependenciesRUN apk add --no-cache \
ca-certificates \
tzdata \
curlWORKDIR/app# Create non-root user with specific UID/GIDRUN addgroup -g 1001 appgroup &&\
adduser -D -u 1001 -G appgroup -h /app appuser# Negative case 2: COPY with safe permissions (755) for application binaryCOPY --chmod=755 src dst# Negative case 3: ADD with restrictive permissions (644) for config filesADD --chmod=644 src dst# Negative case 4: COPY with restrictive permissions (600) for secretsCOPY --chmod=600 src dst# Negative case 5: ADD without chmod flag - uses default secure permissionsADD src dst# Negative case 6: COPY without chmod flag - uses default secure permissionsCOPY src dst# Negative case 7: ADD with chown but no chmod - secure ownershipADD --chown=user:group src dst# Negative case 8: COPY with chown but no chmod - secure ownershipCOPY --chown=user:group src dst# Copy application binary from builder with proper ownershipCOPY --from=builder --chown=appuser:appgroup /build/app /app/# Set secure environment variablesENVAPP_PORT=8080\
APP_ENV=production \
APP_LOG_LEVEL=info
# Create necessary directories with proper permissionsRUN mkdir -p /app/data /app/logs &&\
chown -R appuser:appgroup /app# Expose application portEXPOSE8080# Add health checkHEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3\
CMD curl -f http://localhost:8080/health ||exit1# Switch to non-root userUSERappuser# Use exec form for proper signal handlingCMD["/app/app"]
Non-Compliant Code Examples
FROMalpine:3.14ASbuilderLABELmaintainer="security-team@example.com"LABELdescription="Build stage for application compilation"# Install build dependenciesRUN apk add --no-cache \
gcc \
musl-dev \
makeWORKDIR/build# Positive case 1: ADD with chmod 777 - insecure permissions on source codeADD --chmod=777 src dst# Copy build scriptsCOPY build.sh /build/# Compile applicationRUN make build# Final stageFROMalpine:3.14LABELmaintainer="security-team@example.com"LABELdescription="Production image for web application"# Install runtime dependenciesRUN apk add --no-cache \
ca-certificates \
curlWORKDIR/app# Create application userRUN addgroup -g 1000 appgroup &&\
adduser -D -u 1000 -G appgroup appuser# Positive case 2: COPY with chmod 777 - world-writable configuration filesCOPY --chmod=777 src dst# Positive case 3: ADD with chmod 777 and other flags - insecure data directoryADD --chown=user:group --chmod=777 src dst# Positive case 4: COPY with chmod 777 and other flags - insecure application binaryCOPY --chown=user:group --chmod=777 src dst# Copy application files from builderCOPY --from=builder /build/app /app/# Set environment variablesENVAPP_PORT=8080\
APP_ENV=production
# Expose application portEXPOSE8080# Health checkHEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3\
CMD curl -f http://localhost:8080/health ||exit1USERappuserCMD["/app/app"]
1
2
rulesets:- Dockerfile # Rules to enforce .
Request a personalized demo
Get Started with Datadog
Ask AI
AI-generated responses may be inaccurate. Verify important info.