Run Using Sudo

  • Query id: 8ada6e80-0ade-439e-b176-0b28f6bce35a
  • Query name: Run Using Sudo
  • Platform: Dockerfile
  • Severity: Medium
  • Category: Insecure Configurations
  • CWE: 440
  • Risk score: 6.8
  • URL: Github

Description

Avoid RUN with sudo command as it leads to unpredictable behavior
Documentation

Code samples

Code samples with security vulnerabilities

Positive test num. 1 - dockerfile file
FROM alpine:3.5
RUN apk add --update py2-pip
RUN sudo pip install --upgrade pip
Positive test num. 2 - dockerfile file
from alpine:3.5
run apk add --update py2-pip
run sudo pip install --upgrade pip
Positive test num. 3 - dockerfile file
FROM alpine:3.5
RUN apk add --update py2-pip
RUN ["sudo", "pip", "install", "--upgrade", "pip"]

Positive test num. 4 - dockerfile file
FROM alpine:3.5
RUN apk add --update py2-pip
RUN sudo pip install --upgrade pip
RUN sudo apt-get install -y vim
Positive test num. 5 - dockerfile file
FROM alpine:3.5
RUN apk add --update py2-pip
RUN ["sudo", "pip", "install", "--upgrade", "pip"]
RUN ["sudo", "apt-get", "install", "-y", "vim"]

Code samples without security vulnerabilities

Negative test num. 1 - dockerfile file
FROM alpine:3.5
RUN apk add --update py2-pip
RUN pip install --upgrade pip
RUN apt-get install sudo
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt
COPY app.py /usr/src/app/
COPY templates/index.html /usr/src/app/templates/
EXPOSE 5000
CMD ["python", "/usr/src/app/app.py"]
Negative test num. 2 - dockerfile file
from alpine:3.5
run apk add --update py2-pip
run pip install --upgrade pip
run apt-get install sudo
copy requirements.txt /usr/src/app/
run pip install --no-cache-dir -r /usr/src/app/requirements.txt
copy app.py /usr/src/app/
copy templates/index.html /usr/src/app/templates/
expose 5000
cmd ["python", "/usr/src/app/app.py"]
Negative test num. 3 - dockerfile file
FROM alpine:3.5
RUN apk add --update py2-pip
RUN ["pip", "install", "--upgrade", "pip"]
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt
COPY app.py /usr/src/app/
COPY templates/index.html /usr/src/app/templates/
EXPOSE 5000
CMD ["python", "/usr/src/app/app.py"]

Negative test num. 4 - dockerfile file
FROM alpine:3.5
RUN apk add --update py2-pip
RUN ["pip", "install", "--upgrade", "pip"]
RUN ["apt-get", "install", "-y", "vim"]
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt
COPY app.py /usr/src/app/
COPY templates/index.html /usr/src/app/templates/
EXPOSE 5000
CMD ["python", "/usr/src/app/app.py"]