Skip to content

Commit 4424011

Browse files
committed
sys: add boundary checks to SetOOMScore()
Produce a more user-friendly error in the odd case we would try to set a score out of range Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent ace1912 commit 4424011

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

sys/oom_linux.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ import (
3030
const (
3131
// OOMScoreMaxKillable is the maximum score keeping the process killable by the oom killer
3232
OOMScoreMaxKillable = -999
33-
// OOMScoreAdjMax is from OOM_SCORE_ADJ_MAX https://github.com/torvalds/linux/blob/master/include/uapi/linux/oom.h
33+
// OOMScoreAdjMin is from OOM_SCORE_ADJ_MIN https://github.com/torvalds/linux/blob/v5.10/include/uapi/linux/oom.h#L9
34+
OOMScoreAdjMin = -1000
35+
// OOMScoreAdjMax is from OOM_SCORE_ADJ_MAX https://github.com/torvalds/linux/blob/v5.10/include/uapi/linux/oom.h#L10
3436
OOMScoreAdjMax = 1000
3537
)
3638

3739
// SetOOMScore sets the oom score for the provided pid
3840
func SetOOMScore(pid, score int) error {
41+
if score > OOMScoreAdjMax || score < OOMScoreAdjMin {
42+
return fmt.Errorf("value out of range (%d): OOM score must be between %d and %d", score, OOMScoreAdjMin, OOMScoreAdjMax)
43+
}
3944
path := fmt.Sprintf("/proc/%d/oom_score_adj", pid)
4045
f, err := os.OpenFile(path, os.O_WRONLY, 0)
4146
if err != nil {

sys/oom_linux_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package sys
1818

1919
import (
2020
"errors"
21+
"fmt"
2122
"os"
2223
"os/exec"
2324
"testing"
@@ -57,6 +58,28 @@ func TestSetNegativeOomScoreAdjustmentWhenUnprivilegedHasNoEffect(t *testing.T)
5758
assert.Check(t, is.Equal(adjustment, initial))
5859
}
5960

61+
func TestSetOOMScoreBoundaries(t *testing.T) {
62+
err := SetOOMScore(0, OOMScoreAdjMax+1)
63+
assert.ErrorContains(t, err, fmt.Sprintf("value out of range (%d): OOM score must be between", OOMScoreAdjMax+1))
64+
65+
err = SetOOMScore(0, OOMScoreAdjMin-1)
66+
assert.ErrorContains(t, err, fmt.Sprintf("value out of range (%d): OOM score must be between", OOMScoreAdjMin-1))
67+
68+
_, adjustment, err := adjustOom(OOMScoreAdjMax)
69+
assert.NilError(t, err)
70+
assert.Check(t, is.Equal(adjustment, OOMScoreAdjMax))
71+
72+
score, err := GetOOMScoreAdj(os.Getpid())
73+
assert.NilError(t, err)
74+
if score == 0 || score == OOMScoreAdjMin {
75+
// we won't be able to set the score lower than the parent process,
76+
// so only test if parent process does not have a oom-score-adj
77+
_, adjustment, err = adjustOom(OOMScoreAdjMin)
78+
assert.NilError(t, err)
79+
assert.Check(t, is.Equal(adjustment, OOMScoreAdjMin))
80+
}
81+
}
82+
6083
func adjustOom(adjustment int) (int, int, error) {
6184
cmd := exec.Command("sleep", "100")
6285
if err := cmd.Start(); err != nil {

0 commit comments

Comments
 (0)