Skip to content

Commit

Permalink
Merge pull request #19843 from giuseppe/clamp-oom-score-adj
Browse files Browse the repository at this point in the history
linux, rootless: clamp oom_score_adj if it is too low
  • Loading branch information
openshift-merge-robot authored Sep 4, 2023
2 parents 203c2b8 + 8b4a79a commit 8914caf
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 @@ -78,6 +81,25 @@ func getCgroupPermissions(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 := getCgroupPermissions(s.Unmask)
Expand Down Expand Up @@ -321,7 +343,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)
if s.ReadOnlyFilesystem && !s.ReadWriteTmpfs {
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 @@ -939,6 +939,17 @@ EOF
is "$output" "$oomscore" "--oom-score-adj should override containers.conf"
}

# 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 8914caf

Please sign in to comment.