Skip to content

Commit d36096e

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 d36096e

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

ulimit.go

Lines changed: 24 additions & 0 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.
@@ -108,6 +109,29 @@ func ParseUlimit(val string) (*Ulimit, error) {
108109
return &Ulimit{Name: parts[0], Soft: soft, Hard: *hard}, nil
109110
}
110111

112+
// ParseUlimitAndVerify parses the value and verifyies that values work
113+
// with current kernel, returns a Ulimit from the specified string.
114+
func ParseUlimitAndVerify(val string) (*Ulimit, error) {
115+
limit, err := ParseUlimit(val)
116+
if err != nil {
117+
return nil, err
118+
}
119+
120+
parts := strings.Split(val, "=")
121+
if len(parts) < 1 {
122+
// this should never happen ParseUlimit should have caught this
123+
return nil, fmt.Errorf("invalid ulimit argument: %s", val)
124+
}
125+
126+
var rlimit syscall.Rlimit
127+
if err := syscall.Getrlimit(ulimitNameMapping[parts[0]], &rlimit); err == nil {
128+
if limit.Hard > int64(rlimit.Max) {
129+
return nil, fmt.Errorf("ulimit hard limit must be less than or equal to hard limit for the current process, hard: %d", limit.Hard)
130+
}
131+
}
132+
return limit, nil
133+
}
134+
111135
// GetRlimit returns the RLimit corresponding to Ulimit.
112136
func (u *Ulimit) GetRlimit() (*Rlimit, error) {
113137
t, exists := ulimitNameMapping[u.Name]

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 := ParseUlimitAndVerify("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)