Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
Coredns
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Railgun
Coredns
Commits
342eae9b
Commit
342eae9b
authored
Jan 15, 2021
by
Miek Gieben
Committed by
GitHub
Jan 15, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
plugin/file: guard against cname loops (#4387)
Automatically submitted.
parent
f5f977f4
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
84 additions
and
4 deletions
+84
-4
core/dnsserver/server.go
core/dnsserver/server.go
+9
-2
core/dnsserver/server_grpc.go
core/dnsserver/server_grpc.go
+1
-0
core/dnsserver/server_https.go
core/dnsserver/server_https.go
+1
-0
core/dnsserver/server_tls.go
core/dnsserver/server_tls.go
+1
-0
plugin.md
plugin.md
+9
-0
plugin/file/lookup.go
plugin/file/lookup.go
+14
-1
plugin/pkg/upstream/upstream.go
plugin/pkg/upstream/upstream.go
+0
-1
test/file_loop_test.go
test/file_loop_test.go
+49
-0
No files found.
core/dnsserver/server.go
View file @
342eae9b
...
@@ -110,6 +110,7 @@ func (s *Server) Serve(l net.Listener) error {
...
@@ -110,6 +110,7 @@ func (s *Server) Serve(l net.Listener) error {
s
.
m
.
Lock
()
s
.
m
.
Lock
()
s
.
server
[
tcp
]
=
&
dns
.
Server
{
Listener
:
l
,
Net
:
"tcp"
,
Handler
:
dns
.
HandlerFunc
(
func
(
w
dns
.
ResponseWriter
,
r
*
dns
.
Msg
)
{
s
.
server
[
tcp
]
=
&
dns
.
Server
{
Listener
:
l
,
Net
:
"tcp"
,
Handler
:
dns
.
HandlerFunc
(
func
(
w
dns
.
ResponseWriter
,
r
*
dns
.
Msg
)
{
ctx
:=
context
.
WithValue
(
context
.
Background
(),
Key
{},
s
)
ctx
:=
context
.
WithValue
(
context
.
Background
(),
Key
{},
s
)
ctx
=
context
.
WithValue
(
ctx
,
LoopKey
{},
0
)
s
.
ServeDNS
(
ctx
,
w
,
r
)
s
.
ServeDNS
(
ctx
,
w
,
r
)
})}
})}
s
.
m
.
Unlock
()
s
.
m
.
Unlock
()
...
@@ -123,6 +124,7 @@ func (s *Server) ServePacket(p net.PacketConn) error {
...
@@ -123,6 +124,7 @@ func (s *Server) ServePacket(p net.PacketConn) error {
s
.
m
.
Lock
()
s
.
m
.
Lock
()
s
.
server
[
udp
]
=
&
dns
.
Server
{
PacketConn
:
p
,
Net
:
"udp"
,
Handler
:
dns
.
HandlerFunc
(
func
(
w
dns
.
ResponseWriter
,
r
*
dns
.
Msg
)
{
s
.
server
[
udp
]
=
&
dns
.
Server
{
PacketConn
:
p
,
Net
:
"udp"
,
Handler
:
dns
.
HandlerFunc
(
func
(
w
dns
.
ResponseWriter
,
r
*
dns
.
Msg
)
{
ctx
:=
context
.
WithValue
(
context
.
Background
(),
Key
{},
s
)
ctx
:=
context
.
WithValue
(
context
.
Background
(),
Key
{},
s
)
ctx
=
context
.
WithValue
(
ctx
,
LoopKey
{},
0
)
s
.
ServeDNS
(
ctx
,
w
,
r
)
s
.
ServeDNS
(
ctx
,
w
,
r
)
})}
})}
s
.
m
.
Unlock
()
s
.
m
.
Unlock
()
...
@@ -347,8 +349,13 @@ const (
...
@@ -347,8 +349,13 @@ const (
udp
=
1
udp
=
1
)
)
// Key is the context key for the current server added to the context.
type
(
type
Key
struct
{}
// Key is the context key for the current server added to the context.
Key
struct
{}
// LoopKey is the context key to detect server wide loops.
LoopKey
struct
{}
)
// EnableChaos is a map with plugin names for which we should open CH class queries as we block these by default.
// EnableChaos is a map with plugin names for which we should open CH class queries as we block these by default.
var
EnableChaos
=
map
[
string
]
struct
{}{
var
EnableChaos
=
map
[
string
]
struct
{}{
...
...
core/dnsserver/server_grpc.go
View file @
342eae9b
...
@@ -134,6 +134,7 @@ func (s *ServergRPC) Query(ctx context.Context, in *pb.DnsPacket) (*pb.DnsPacket
...
@@ -134,6 +134,7 @@ func (s *ServergRPC) Query(ctx context.Context, in *pb.DnsPacket) (*pb.DnsPacket
w
:=
&
gRPCresponse
{
localAddr
:
s
.
listenAddr
,
remoteAddr
:
a
,
Msg
:
msg
}
w
:=
&
gRPCresponse
{
localAddr
:
s
.
listenAddr
,
remoteAddr
:
a
,
Msg
:
msg
}
dnsCtx
:=
context
.
WithValue
(
ctx
,
Key
{},
s
.
Server
)
dnsCtx
:=
context
.
WithValue
(
ctx
,
Key
{},
s
.
Server
)
dnsCtx
=
context
.
WithValue
(
dnsCtx
,
LoopKey
{},
0
)
s
.
ServeDNS
(
dnsCtx
,
w
,
msg
)
s
.
ServeDNS
(
dnsCtx
,
w
,
msg
)
packed
,
err
:=
w
.
Msg
.
Pack
()
packed
,
err
:=
w
.
Msg
.
Pack
()
...
...
core/dnsserver/server_https.go
View file @
342eae9b
...
@@ -145,6 +145,7 @@ func (s *ServerHTTPS) ServeHTTP(w http.ResponseWriter, r *http.Request) {
...
@@ -145,6 +145,7 @@ func (s *ServerHTTPS) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// We just call the normal chain handler - all error handling is done there.
// We just call the normal chain handler - all error handling is done there.
// We should expect a packet to be returned that we can send to the client.
// We should expect a packet to be returned that we can send to the client.
ctx
:=
context
.
WithValue
(
context
.
Background
(),
Key
{},
s
.
Server
)
ctx
:=
context
.
WithValue
(
context
.
Background
(),
Key
{},
s
.
Server
)
ctx
=
context
.
WithValue
(
ctx
,
LoopKey
{},
0
)
s
.
ServeDNS
(
ctx
,
dw
,
msg
)
s
.
ServeDNS
(
ctx
,
dw
,
msg
)
// See section 4.2.1 of RFC 8484.
// See section 4.2.1 of RFC 8484.
...
...
core/dnsserver/server_tls.go
View file @
342eae9b
...
@@ -50,6 +50,7 @@ func (s *ServerTLS) Serve(l net.Listener) error {
...
@@ -50,6 +50,7 @@ func (s *ServerTLS) Serve(l net.Listener) error {
// Only fill out the TCP server for this one.
// Only fill out the TCP server for this one.
s
.
server
[
tcp
]
=
&
dns
.
Server
{
Listener
:
l
,
Net
:
"tcp-tls"
,
Handler
:
dns
.
HandlerFunc
(
func
(
w
dns
.
ResponseWriter
,
r
*
dns
.
Msg
)
{
s
.
server
[
tcp
]
=
&
dns
.
Server
{
Listener
:
l
,
Net
:
"tcp-tls"
,
Handler
:
dns
.
HandlerFunc
(
func
(
w
dns
.
ResponseWriter
,
r
*
dns
.
Msg
)
{
ctx
:=
context
.
WithValue
(
context
.
Background
(),
Key
{},
s
.
Server
)
ctx
:=
context
.
WithValue
(
context
.
Background
(),
Key
{},
s
.
Server
)
ctx
=
context
.
WithValue
(
ctx
,
LoopKey
{},
0
)
s
.
ServeDNS
(
ctx
,
w
,
r
)
s
.
ServeDNS
(
ctx
,
w
,
r
)
})}
})}
s
.
m
.
Unlock
()
s
.
m
.
Unlock
()
...
...
plugin.md
View file @
342eae9b
...
@@ -69,6 +69,15 @@ works, and implement the `ready.Readiness` interface.
...
@@ -69,6 +69,15 @@ works, and implement the `ready.Readiness` interface.
See the plugin/pkg/reuseport for
`Listen`
and
`ListenPacket`
functions. Using these functions makes
See the plugin/pkg/reuseport for
`Listen`
and
`ListenPacket`
functions. Using these functions makes
your plugin handle reload events better.
your plugin handle reload events better.
## Context
Every request get a context.Context these are pre-filled with 2 values:
*
`Key`
: holds a pointer to the current server, this can be useful for logging or metrics. It is
infact used in the
*metrics*
plugin to tie a request to a specific (internal) server.
*
`LoopKey`
: holds an integer to detect loops within the current context. The
*file*
plugin uses
this to detect loops when resolving CNAMEs.
## Documentation
## Documentation
Each plugin should have a README.md explaining what the plugin does and how it is configured. The
Each plugin should have a README.md explaining what the plugin does and how it is configured. The
...
...
plugin/file/lookup.go
View file @
342eae9b
...
@@ -3,6 +3,7 @@ package file
...
@@ -3,6 +3,7 @@ package file
import
(
import
(
"context"
"context"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin/file/rrutil"
"github.com/coredns/coredns/plugin/file/rrutil"
"github.com/coredns/coredns/plugin/file/tree"
"github.com/coredns/coredns/plugin/file/tree"
"github.com/coredns/coredns/request"
"github.com/coredns/coredns/request"
...
@@ -29,7 +30,6 @@ const (
...
@@ -29,7 +30,6 @@ const (
// Lookup looks up qname and qtype in the zone. When do is true DNSSEC records are included.
// Lookup looks up qname and qtype in the zone. When do is true DNSSEC records are included.
// Three sets of records are returned, one for the answer, one for authority and one for the additional section.
// Three sets of records are returned, one for the answer, one for authority and one for the additional section.
func
(
z
*
Zone
)
Lookup
(
ctx
context
.
Context
,
state
request
.
Request
,
qname
string
)
([]
dns
.
RR
,
[]
dns
.
RR
,
[]
dns
.
RR
,
Result
)
{
func
(
z
*
Zone
)
Lookup
(
ctx
context
.
Context
,
state
request
.
Request
,
qname
string
)
([]
dns
.
RR
,
[]
dns
.
RR
,
[]
dns
.
RR
,
Result
)
{
qtype
:=
state
.
QType
()
qtype
:=
state
.
QType
()
do
:=
state
.
Do
()
do
:=
state
.
Do
()
...
@@ -62,6 +62,16 @@ func (z *Zone) Lookup(ctx context.Context, state request.Request, qname string)
...
@@ -62,6 +62,16 @@ func (z *Zone) Lookup(ctx context.Context, state request.Request, qname string)
elem
,
wildElem
*
tree
.
Elem
elem
,
wildElem
*
tree
.
Elem
)
)
loop
,
_
:=
ctx
.
Value
(
dnsserver
.
LoopKey
{})
.
(
int
)
if
loop
>
8
{
// We're back here for the 9th time; we have a loop and need to bail out.
// Note the answer we're returning will be incomplete (more cnames to be followed) or
// illegal (wildcard cname with multiple identical records). For now it's more important
// to protect ourselves then to give the client a valid answer. We return with an error
// to let the server handle what to do.
return
nil
,
nil
,
nil
,
ServerFailure
}
// Lookup:
// Lookup:
// * Per label from the right, look if it exists. We do this to find potential
// * Per label from the right, look if it exists. We do this to find potential
// delegation records.
// delegation records.
...
@@ -105,6 +115,7 @@ func (z *Zone) Lookup(ctx context.Context, state request.Request, qname string)
...
@@ -105,6 +115,7 @@ func (z *Zone) Lookup(ctx context.Context, state request.Request, qname string)
// Only one DNAME is allowed per name. We just pick the first one to synthesize from.
// Only one DNAME is allowed per name. We just pick the first one to synthesize from.
dname
:=
dnamerrs
[
0
]
dname
:=
dnamerrs
[
0
]
if
cname
:=
synthesizeCNAME
(
state
.
Name
(),
dname
.
(
*
dns
.
DNAME
));
cname
!=
nil
{
if
cname
:=
synthesizeCNAME
(
state
.
Name
(),
dname
.
(
*
dns
.
DNAME
));
cname
!=
nil
{
ctx
=
context
.
WithValue
(
ctx
,
dnsserver
.
LoopKey
{},
loop
+
1
)
answer
,
ns
,
extra
,
rcode
:=
z
.
externalLookup
(
ctx
,
state
,
elem
,
[]
dns
.
RR
{
cname
})
answer
,
ns
,
extra
,
rcode
:=
z
.
externalLookup
(
ctx
,
state
,
elem
,
[]
dns
.
RR
{
cname
})
if
do
{
if
do
{
...
@@ -156,6 +167,7 @@ func (z *Zone) Lookup(ctx context.Context, state request.Request, qname string)
...
@@ -156,6 +167,7 @@ func (z *Zone) Lookup(ctx context.Context, state request.Request, qname string)
if
found
&&
shot
{
if
found
&&
shot
{
if
rrs
:=
elem
.
Type
(
dns
.
TypeCNAME
);
len
(
rrs
)
>
0
&&
qtype
!=
dns
.
TypeCNAME
{
if
rrs
:=
elem
.
Type
(
dns
.
TypeCNAME
);
len
(
rrs
)
>
0
&&
qtype
!=
dns
.
TypeCNAME
{
ctx
=
context
.
WithValue
(
ctx
,
dnsserver
.
LoopKey
{},
loop
+
1
)
return
z
.
externalLookup
(
ctx
,
state
,
elem
,
rrs
)
return
z
.
externalLookup
(
ctx
,
state
,
elem
,
rrs
)
}
}
...
@@ -192,6 +204,7 @@ func (z *Zone) Lookup(ctx context.Context, state request.Request, qname string)
...
@@ -192,6 +204,7 @@ func (z *Zone) Lookup(ctx context.Context, state request.Request, qname string)
auth
:=
ap
.
ns
(
do
)
auth
:=
ap
.
ns
(
do
)
if
rrs
:=
wildElem
.
TypeForWildcard
(
dns
.
TypeCNAME
,
qname
);
len
(
rrs
)
>
0
{
if
rrs
:=
wildElem
.
TypeForWildcard
(
dns
.
TypeCNAME
,
qname
);
len
(
rrs
)
>
0
{
ctx
=
context
.
WithValue
(
ctx
,
dnsserver
.
LoopKey
{},
loop
+
1
)
return
z
.
externalLookup
(
ctx
,
state
,
wildElem
,
rrs
)
return
z
.
externalLookup
(
ctx
,
state
,
wildElem
,
rrs
)
}
}
...
...
plugin/pkg/upstream/upstream.go
View file @
342eae9b
...
@@ -32,7 +32,6 @@ func (u *Upstream) Lookup(ctx context.Context, state request.Request, name strin
...
@@ -32,7 +32,6 @@ func (u *Upstream) Lookup(ctx context.Context, state request.Request, name strin
req
.
SetEdns0
(
uint16
(
size
),
do
)
req
.
SetEdns0
(
uint16
(
size
),
do
)
nw
:=
nonwriter
.
New
(
state
.
W
)
nw
:=
nonwriter
.
New
(
state
.
W
)
server
.
ServeDNS
(
ctx
,
nw
,
req
)
server
.
ServeDNS
(
ctx
,
nw
,
req
)
return
nw
.
Msg
,
nil
return
nw
.
Msg
,
nil
...
...
test/file_loop_test.go
0 → 100644
View file @
342eae9b
package
test
import
(
"testing"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
)
const
loopDB
=
`example.com. 500 IN SOA ns1.outside.com. root.example.com. 3 604800 86400 2419200 604800
example.com. 500 IN NS ns1.outside.com.
a.example.com. 500 IN CNAME b.example.com.
*.foo.example.com. 500 IN CNAME bar.foo.example.com.`
func
TestFileLoop
(
t
*
testing
.
T
)
{
name
,
rm
,
err
:=
test
.
TempFile
(
"."
,
loopDB
)
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to create zone: %s"
,
err
)
}
defer
rm
()
// Corefile with for example without proxy section.
corefile
:=
`example.com:0 {
file `
+
name
+
`
}`
i
,
udp
,
_
,
err
:=
CoreDNSServerAndPorts
(
corefile
)
if
err
!=
nil
{
t
.
Fatalf
(
"Could not get CoreDNS serving instance: %s"
,
err
)
}
defer
i
.
Stop
()
m
:=
new
(
dns
.
Msg
)
m
.
SetQuestion
(
"something.foo.example.com."
,
dns
.
TypeA
)
r
,
err
:=
dns
.
Exchange
(
m
,
udp
)
if
err
!=
nil
{
t
.
Fatalf
(
"Could not exchange msg: %s"
,
err
)
}
// This should not loop, don't really care about the correctness of the answer.
// Currently we return servfail in the file lookup.go file.
// For now: document current behavior in this test.
if
r
.
Rcode
!=
dns
.
RcodeServerFailure
{
t
.
Errorf
(
"Rcode should be dns.RcodeServerFailure: %d"
,
r
.
Rcode
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment