Skip to content

Commit

Permalink
Limit $GENERATE range to 65535 steps
Browse files Browse the repository at this point in the history
Having these checks means all test in TestCrasherString() are not
reached because we bail out earlier - removed that test all together.

Fixes #1019

Signed-off-by: Miek Gieben <[email protected]>
  • Loading branch information
miekg committed Oct 3, 2019
1 parent 046ae4e commit f0bed49
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
4 changes: 2 additions & 2 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ func (zp *ZoneParser) generate(l lex) (RR, bool) {
if err != nil {
return zp.setParseError("bad stop in $GENERATE range", l)
}
if end < 0 || start < 0 || end < start {
if end < 0 || start < 0 || end < start || (end-start)/step > 65535 {
return zp.setParseError("bad range in $GENERATE range", l)
}

// _BLANK
l, ok := zp.c.Next()
if !ok || l.value != zBlank {
return zp.setParseError("garbage after $GENERATE range", l)
return zp.setParseError("garbage after $GENERATE range", l)
}

// Create a complete new string, which we then parse again.
Expand Down
44 changes: 24 additions & 20 deletions generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,27 +205,31 @@ $GENERATE 32-158 dhcp-${-32,4,d} A 10.0.0.$
}
}

func TestOutofRangeRange(t *testing.T) {
// outside limit
data := `$GENERATE 0-65536 dhcp A 10.0.0.1`
zp := NewZoneParser(strings.NewReader(data), ".", "test")
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
if zp.Err() == nil || !strings.Contains(zp.Err().Error(), "dns: bad range in $GENERATE") {
t.Errorf("Expected %q error, got %s", "bad range", zp.Err())
}

// within limit
data = `$GENERATE 0-65535 dhcp A 10.0.0.1`
zp = NewZoneParser(strings.NewReader(data), ".", "test")
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
if zp.Err() != nil {
t.Errorf("Expected nil error, got %s", zp.Err())
}

func TestCrasherString(t *testing.T) {
tests := []struct{
in string
err string
}{
{"$GENERATE 0-300103\"$$GENERATE 2-2", "dns: garbage after $GENERATE range: \"\\\"\" at line: 1:19"},
{"$GENERATE 0-5414137360", "dns: garbage after $GENERATE range: \"\\n\" at line: 1:22"},
{"$GENERATE 11522-3668518066406258", "dns: garbage after $GENERATE range: \"\\n\" at line: 1:38"},
{"$GENERATE 0-200\"(;00000000000000\n$$GENERATE 0-0", "dns: garbage after $GENERATE range: \"\\\"\" at line: 1:16"},
}
for _, tc := range tests {
t.Run(tc.in, func(t *testing.T) {
_, err := NewRR(tc.in)
if err == nil {
t.Errorf("Expecting error for crasher line %s", tc.in)
}
if tc.err != err.Error() {
t.Errorf("Expecting error %s, got %s", tc.err, err.Error())
}
})
// within limit, due to /2
data = `$GENERATE 0-65536/2 dhcp A 10.0.0.1`
zp = NewZoneParser(strings.NewReader(data), ".", "test")
for _, ok := zp.Next(); ok; _, ok = zp.Next() {
}
if zp.Err() != nil {
t.Errorf("Expected nil error, got %s", zp.Err())
}
}
6 changes: 4 additions & 2 deletions scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func ReadRR(r io.Reader, file string) (RR, error) {
}

// ParseZone reads a RFC 1035 style zonefile from r. It returns
// *Tokens on the returned channel, each consisting of either a
// Tokens on the returned channel, each consisting of either a
// parsed RR and optional comment or a nil RR and an error. The
// channel is closed by ParseZone when the end of r is reached.
//
Expand All @@ -143,7 +143,8 @@ func ReadRR(r io.Reader, file string) (RR, error) {
// origin, as if the file would start with an $ORIGIN directive.
//
// The directives $INCLUDE, $ORIGIN, $TTL and $GENERATE are all
// supported.
// supported. Note that $GENERATE's range support up to a maximum of
// of 65535 steps.
//
// Basic usage pattern when reading from a string (z) containing the
// zone data:
Expand Down Expand Up @@ -203,6 +204,7 @@ func parseZone(r io.Reader, origin, file string, t chan *Token) {
//
// The directives $INCLUDE, $ORIGIN, $TTL and $GENERATE are all
// supported. Although $INCLUDE is disabled by default.
// Note that $GENERATE's range support up to a maximum of 65535 steps.
//
// Basic usage pattern when reading from a string (z) containing the
// zone data:
Expand Down

0 comments on commit f0bed49

Please sign in to comment.