open-nomad/helper/flags/autopilot_flags.go

106 lines
2 KiB
Go
Raw Normal View History

package flags
// These flag type implementations are provided to maintain autopilot command
// backwards compatibility.
import (
"fmt"
fix integer bounds checks (#11815) * driver: fix integer conversion error The shared executor incorrectly parsed the user's group into int32 and then cast to uint32 without bounds checking. This is harmless because an out-of-bounds gid will throw an error later, but it triggers security and code quality scans. Parse directly to uint32 so that we get correct error handling. * helper: fix integer conversion error The autopilot flags helper incorrectly parses a uint64 to a uint which is machine specific size. Although we don't have 32-bit builds, this sets off security and code quality scaans. Parse to the machine sized uint. * driver: restrict bounds of port map The plugin server doesn't constrain the maximum integer for port maps. This could result in a user-visible misconfiguration, but it also triggers security and code quality scans. Restrict the bounds before casting to int32 and return an error. * cpuset: restrict upper bounds of cpuset values Our cpuset configuration expects values in the range of uint16 to match the expectations set by the kernel, but we don't constrain the values before downcasting. An underflow could lead to allocations failing on the client rather than being caught earlier. This also make security and code quality scanners happy. * http: fix integer downcast for per_page parameter The parser for the `per_page` query parameter downcasts to int32 without bounds checking. This could result in underflow and nonsensical paging, but there's no server-side consequences for this. Fixing this will silence some security and code quality scanners though.
2022-01-25 16:16:48 +00:00
"math/bits"
"strconv"
"time"
)
// BoolValue provides a flag value that's aware if it has been set.
type BoolValue struct {
v *bool
}
// Merge will overlay this value if it has been set.
func (b *BoolValue) Merge(onto *bool) {
if b.v != nil {
*onto = *(b.v)
}
}
// Set implements the flag.Value interface.
func (b *BoolValue) Set(v string) error {
if b.v == nil {
b.v = new(bool)
}
var err error
*(b.v), err = strconv.ParseBool(v)
return err
}
// String implements the flag.Value interface.
func (b *BoolValue) String() string {
var current bool
if b.v != nil {
current = *(b.v)
}
return fmt.Sprintf("%v", current)
}
// DurationValue provides a flag value that's aware if it has been set.
type DurationValue struct {
v *time.Duration
}
// Merge will overlay this value if it has been set.
func (d *DurationValue) Merge(onto *time.Duration) {
if d.v != nil {
*onto = *(d.v)
}
}
// Set implements the flag.Value interface.
func (d *DurationValue) Set(v string) error {
if d.v == nil {
d.v = new(time.Duration)
}
var err error
*(d.v), err = time.ParseDuration(v)
return err
}
// String implements the flag.Value interface.
func (d *DurationValue) String() string {
var current time.Duration
if d.v != nil {
current = *(d.v)
}
return current.String()
}
// UintValue provides a flag value that's aware if it has been set.
type UintValue struct {
v *uint
}
// Merge will overlay this value if it has been set.
func (u *UintValue) Merge(onto *uint) {
if u.v != nil {
*onto = *(u.v)
}
}
// Set implements the flag.Value interface.
func (u *UintValue) Set(v string) error {
if u.v == nil {
u.v = new(uint)
}
fix integer bounds checks (#11815) * driver: fix integer conversion error The shared executor incorrectly parsed the user's group into int32 and then cast to uint32 without bounds checking. This is harmless because an out-of-bounds gid will throw an error later, but it triggers security and code quality scans. Parse directly to uint32 so that we get correct error handling. * helper: fix integer conversion error The autopilot flags helper incorrectly parses a uint64 to a uint which is machine specific size. Although we don't have 32-bit builds, this sets off security and code quality scaans. Parse to the machine sized uint. * driver: restrict bounds of port map The plugin server doesn't constrain the maximum integer for port maps. This could result in a user-visible misconfiguration, but it also triggers security and code quality scans. Restrict the bounds before casting to int32 and return an error. * cpuset: restrict upper bounds of cpuset values Our cpuset configuration expects values in the range of uint16 to match the expectations set by the kernel, but we don't constrain the values before downcasting. An underflow could lead to allocations failing on the client rather than being caught earlier. This also make security and code quality scanners happy. * http: fix integer downcast for per_page parameter The parser for the `per_page` query parameter downcasts to int32 without bounds checking. This could result in underflow and nonsensical paging, but there's no server-side consequences for this. Fixing this will silence some security and code quality scanners though.
2022-01-25 16:16:48 +00:00
parsed, err := strconv.ParseUint(v, 0, bits.UintSize)
*(u.v) = (uint)(parsed)
return err
}
// String implements the flag.Value interface.
func (u *UintValue) String() string {
var current uint
if u.v != nil {
current = *(u.v)
}
return fmt.Sprintf("%v", current)
}