Skip to content

Commit

Permalink
Should verify that the max value is not too big
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rhatdan committed Apr 27, 2020
1 parent 519db1e commit 9bfd2f1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 10 additions & 2 deletions ulimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strconv"
"strings"
"syscall"
)

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

if _, exists := ulimitNameMapping[parts[0]]; !exists {
limitType, exists := ulimitNameMapping[parts[0]]
if !exists {
return nil, fmt.Errorf("invalid ulimit type: %s", parts[0])
}

var (
soft int64
hard = &soft // default to soft in case no hard was set
Expand Down Expand Up @@ -103,6 +104,13 @@ func ParseUlimit(val string) (*Ulimit, error) {
if soft > *hard {
return nil, fmt.Errorf("ulimit soft limit must be less than or equal to hard limit: %d > %d", soft, *hard)
}
var rlimit syscall.Rlimit
if err := syscall.Getrlimit(limitType, &rlimit); err == nil {
if *hard > int64(rlimit.Max) {
return nil, fmt.Errorf("ulimit hard limit must be less than or equal to hard limit for the current process, hard: %d", *hard)
}
}

}

return &Ulimit{Name: parts[0], Soft: soft, Hard: *hard}, nil
Expand Down
6 changes: 6 additions & 0 deletions ulimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ func TestParseUlimitValid(t *testing.T) {
}
}

func TestParseUlimitTooBig(t *testing.T) {
if _, err := ParseUlimit("nofile=512:1000024"); err == nil {
t.Fatalf("expected invalid value got %q", err)
}
}

func TestParseUlimitInvalidLimitType(t *testing.T) {
if _, err := ParseUlimit("notarealtype=1024:1024"); err == nil {
t.Fatalf("expected error on invalid ulimit type")
Expand Down

0 comments on commit 9bfd2f1

Please sign in to comment.