Skip to content

Commit

Permalink
linux, rootless: clamp oom_score_adj if it is too low
Browse files Browse the repository at this point in the history
when running rootless, if the specified oom_score_adj for the
container process is lower than the current value, clamp it to the
current value and print a warning.

Closes: containers#19829

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
(cherry picked from commit 8b4a79a)
  • Loading branch information
giuseppe committed Sep 14, 2023
1 parent dd490ff commit 93d9c6b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/source/markdown/options/oom-score-adj.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
#### **--oom-score-adj**=*num*

Tune the host's OOM preferences for containers (accepts values from **-1000** to **1000**).

When running in rootless mode, the specified value can't be lower than
the oom_score_adj for the current process. In this case, the
oom-score-adj is clamped to the current process value.
28 changes: 27 additions & 1 deletion pkg/specgen/generate/oci_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"path"
"strconv"
"strings"

"github.com/containers/common/libimage"
Expand All @@ -16,6 +18,7 @@ import (
"github.com/containers/podman/v4/pkg/specgen"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -69,6 +72,25 @@ func getCgroupPermissons(unmask []string) string {
return ro
}

func maybeClampOOMScoreAdj(oomScoreValue int, isRootless bool) (int, error) {
if !isRootless {
return oomScoreValue, nil
}
v, err := os.ReadFile("/proc/self/oom_score_adj")
if err != nil {
return oomScoreValue, err
}
currentValue, err := strconv.Atoi(strings.TrimRight(string(v), "\n"))
if err != nil {
return oomScoreValue, err
}
if currentValue > oomScoreValue {
logrus.Warnf("Requested oom_score_adj=%d is lower than the current one, changing to %d", oomScoreValue, currentValue)
return currentValue, nil
}
return oomScoreValue, nil
}

// SpecGenToOCI returns the base configuration for the container.
func SpecGenToOCI(ctx context.Context, s *specgen.SpecGenerator, rt *libpod.Runtime, rtc *config.Config, newImage *libimage.Image, mounts []spec.Mount, pod *libpod.Pod, finalCmd []string, compatibleOptions *libpod.InfraInherit) (*spec.Spec, error) {
cgroupPerm := getCgroupPermissons(s.Unmask)
Expand Down Expand Up @@ -309,7 +331,11 @@ func SpecGenToOCI(ctx context.Context, s *specgen.SpecGenerator, rt *libpod.Runt
}

if s.OOMScoreAdj != nil {
g.SetProcessOOMScoreAdj(*s.OOMScoreAdj)
score, err := maybeClampOOMScoreAdj(*s.OOMScoreAdj, isRootless)
if err != nil {
return nil, err
}
g.SetProcessOOMScoreAdj(score)
}
setProcOpts(s, &g)

Expand Down
11 changes: 11 additions & 0 deletions test/system/030-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,17 @@ EOF
is "$output" "$current_oom_score_adj" "different oom_score_adj in the container"
}

# issue 19829
@test "rootless podman clamps oom-score-adj if it is lower than the current one" {
skip_if_not_rootless
skip_if_remote
if grep -- -1000 /proc/self/oom_score_adj; then
skip "the current oom-score-adj is already -1000"
fi
run_podman run --oom-score-adj=-1000 --rm $IMAGE true
is "$output" ".*Requested oom_score_adj=.* is lower than the current one, changing to .*"
}

# CVE-2022-1227 : podman top joins container mount NS and uses nsenter from image
@test "podman top does not use nsenter from image" {
keepid="--userns=keep-id"
Expand Down

0 comments on commit 93d9c6b

Please sign in to comment.