Skip to content

Commit 9bfd2f1

Browse files
committed
Should verify that the max value is not too big
When parsing the ulimit, we should check if the ulimit max value is greater then the processes max value. If yes then we should return an error. Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
1 parent 519db1e commit 9bfd2f1

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

ulimit.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strconv"
66
"strings"
7+
"syscall"
78
)
89

910
// Ulimit is a human friendly version of Rlimit.
@@ -69,10 +70,10 @@ func ParseUlimit(val string) (*Ulimit, error) {
6970
return nil, fmt.Errorf("invalid ulimit argument: %s", val)
7071
}
7172

72-
if _, exists := ulimitNameMapping[parts[0]]; !exists {
73+
limitType, exists := ulimitNameMapping[parts[0]]
74+
if !exists {
7375
return nil, fmt.Errorf("invalid ulimit type: %s", parts[0])
7476
}
75-
7677
var (
7778
soft int64
7879
hard = &soft // default to soft in case no hard was set
@@ -103,6 +104,13 @@ func ParseUlimit(val string) (*Ulimit, error) {
103104
if soft > *hard {
104105
return nil, fmt.Errorf("ulimit soft limit must be less than or equal to hard limit: %d > %d", soft, *hard)
105106
}
107+
var rlimit syscall.Rlimit
108+
if err := syscall.Getrlimit(limitType, &rlimit); err == nil {
109+
if *hard > int64(rlimit.Max) {
110+
return nil, fmt.Errorf("ulimit hard limit must be less than or equal to hard limit for the current process, hard: %d", *hard)
111+
}
112+
}
113+
106114
}
107115

108116
return &Ulimit{Name: parts[0], Soft: soft, Hard: *hard}, nil

ulimit_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ func TestParseUlimitValid(t *testing.T) {
2121
}
2222
}
2323

24+
func TestParseUlimitTooBig(t *testing.T) {
25+
if _, err := ParseUlimit("nofile=512:1000024"); err == nil {
26+
t.Fatalf("expected invalid value got %q", err)
27+
}
28+
}
29+
2430
func TestParseUlimitInvalidLimitType(t *testing.T) {
2531
if _, err := ParseUlimit("notarealtype=1024:1024"); err == nil {
2632
t.Fatalf("expected error on invalid ulimit type")

0 commit comments

Comments
 (0)