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
c4bc1a54
Commit
c4bc1a54
authored
May 01, 2022
by
Chris O'Haver
Committed by
GitHub
May 01, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
plugin/cache: Fix cache poisoning exploit (#5174)
parent
5a4437bb
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
25 deletions
+29
-25
plugin/cache/cache_test.go
plugin/cache/cache_test.go
+8
-4
plugin/cache/handler.go
plugin/cache/handler.go
+6
-21
plugin/cache/item.go
plugin/cache/item.go
+15
-0
No files found.
plugin/cache/cache_test.go
View file @
c4bc1a54
...
@@ -191,7 +191,7 @@ func TestCache(t *testing.T) {
...
@@ -191,7 +191,7 @@ func TestCache(t *testing.T) {
c
,
crr
:=
newTestCache
(
maxTTL
)
c
,
crr
:=
newTestCache
(
maxTTL
)
for
_
,
tc
:=
range
cacheTestCases
{
for
n
,
tc
:=
range
cacheTestCases
{
m
:=
tc
.
in
.
Msg
()
m
:=
tc
.
in
.
Msg
()
m
=
cacheMsg
(
m
,
tc
)
m
=
cacheMsg
(
m
,
tc
)
...
@@ -204,11 +204,15 @@ func TestCache(t *testing.T) {
...
@@ -204,11 +204,15 @@ func TestCache(t *testing.T) {
crr
.
set
(
m
,
k
,
mt
,
c
.
pttl
)
crr
.
set
(
m
,
k
,
mt
,
c
.
pttl
)
}
}
i
,
_
:=
c
.
get
(
time
.
Now
()
.
UTC
(),
state
,
"dns://:53"
)
i
:=
c
.
getIgnoreTTL
(
time
.
Now
()
.
UTC
(),
state
,
"dns://:53"
)
ok
:=
i
!=
nil
ok
:=
i
!=
nil
if
ok
!=
tc
.
shouldCache
{
if
!
tc
.
shouldCache
&&
ok
{
t
.
Errorf
(
"Cached message that should not have been cached: %s"
,
state
.
Name
())
t
.
Errorf
(
"Test %d: Cached message that should not have been cached: %s"
,
n
,
state
.
Name
())
continue
}
if
tc
.
shouldCache
&&
!
ok
{
t
.
Errorf
(
"Test %d: Did not cache message that should have been cached: %s"
,
n
,
state
.
Name
())
continue
continue
}
}
...
...
plugin/cache/handler.go
View file @
c4bc1a54
...
@@ -89,38 +89,23 @@ func (c *Cache) shouldPrefetch(i *item, now time.Time) bool {
...
@@ -89,38 +89,23 @@ func (c *Cache) shouldPrefetch(i *item, now time.Time) bool {
// Name implements the Handler interface.
// Name implements the Handler interface.
func
(
c
*
Cache
)
Name
()
string
{
return
"cache"
}
func
(
c
*
Cache
)
Name
()
string
{
return
"cache"
}
func
(
c
*
Cache
)
get
(
now
time
.
Time
,
state
request
.
Request
,
server
string
)
(
*
item
,
bool
)
{
k
:=
hash
(
state
.
Name
(),
state
.
QType
())
cacheRequests
.
WithLabelValues
(
server
,
c
.
zonesMetricLabel
)
.
Inc
()
if
i
,
ok
:=
c
.
ncache
.
Get
(
k
);
ok
&&
i
.
(
*
item
)
.
ttl
(
now
)
>
0
{
cacheHits
.
WithLabelValues
(
server
,
Denial
,
c
.
zonesMetricLabel
)
.
Inc
()
return
i
.
(
*
item
),
true
}
if
i
,
ok
:=
c
.
pcache
.
Get
(
k
);
ok
&&
i
.
(
*
item
)
.
ttl
(
now
)
>
0
{
cacheHits
.
WithLabelValues
(
server
,
Success
,
c
.
zonesMetricLabel
)
.
Inc
()
return
i
.
(
*
item
),
true
}
cacheMisses
.
WithLabelValues
(
server
,
c
.
zonesMetricLabel
)
.
Inc
()
return
nil
,
false
}
// getIgnoreTTL unconditionally returns an item if it exists in the cache.
// getIgnoreTTL unconditionally returns an item if it exists in the cache.
func
(
c
*
Cache
)
getIgnoreTTL
(
now
time
.
Time
,
state
request
.
Request
,
server
string
)
*
item
{
func
(
c
*
Cache
)
getIgnoreTTL
(
now
time
.
Time
,
state
request
.
Request
,
server
string
)
*
item
{
k
:=
hash
(
state
.
Name
(),
state
.
QType
())
k
:=
hash
(
state
.
Name
(),
state
.
QType
())
cacheRequests
.
WithLabelValues
(
server
,
c
.
zonesMetricLabel
)
.
Inc
()
cacheRequests
.
WithLabelValues
(
server
,
c
.
zonesMetricLabel
)
.
Inc
()
if
i
,
ok
:=
c
.
ncache
.
Get
(
k
);
ok
{
if
i
,
ok
:=
c
.
ncache
.
Get
(
k
);
ok
{
ttl
:=
i
.
(
*
item
)
.
ttl
(
now
)
itm
:=
i
.
(
*
item
)
if
ttl
>
0
||
(
c
.
staleUpTo
>
0
&&
-
ttl
<
int
(
c
.
staleUpTo
.
Seconds
()))
{
ttl
:=
itm
.
ttl
(
now
)
if
itm
.
matches
(
state
)
&&
(
ttl
>
0
||
(
c
.
staleUpTo
>
0
&&
-
ttl
<
int
(
c
.
staleUpTo
.
Seconds
())))
{
cacheHits
.
WithLabelValues
(
server
,
Denial
,
c
.
zonesMetricLabel
)
.
Inc
()
cacheHits
.
WithLabelValues
(
server
,
Denial
,
c
.
zonesMetricLabel
)
.
Inc
()
return
i
.
(
*
item
)
return
i
.
(
*
item
)
}
}
}
}
if
i
,
ok
:=
c
.
pcache
.
Get
(
k
);
ok
{
if
i
,
ok
:=
c
.
pcache
.
Get
(
k
);
ok
{
ttl
:=
i
.
(
*
item
)
.
ttl
(
now
)
itm
:=
i
.
(
*
item
)
if
ttl
>
0
||
(
c
.
staleUpTo
>
0
&&
-
ttl
<
int
(
c
.
staleUpTo
.
Seconds
()))
{
ttl
:=
itm
.
ttl
(
now
)
if
itm
.
matches
(
state
)
&&
(
ttl
>
0
||
(
c
.
staleUpTo
>
0
&&
-
ttl
<
int
(
c
.
staleUpTo
.
Seconds
())))
{
cacheHits
.
WithLabelValues
(
server
,
Success
,
c
.
zonesMetricLabel
)
.
Inc
()
cacheHits
.
WithLabelValues
(
server
,
Success
,
c
.
zonesMetricLabel
)
.
Inc
()
return
i
.
(
*
item
)
return
i
.
(
*
item
)
}
}
...
...
plugin/cache/item.go
View file @
c4bc1a54
package
cache
package
cache
import
(
import
(
"strings"
"time"
"time"
"github.com/coredns/coredns/plugin/cache/freq"
"github.com/coredns/coredns/plugin/cache/freq"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
"github.com/miekg/dns"
)
)
type
item
struct
{
type
item
struct
{
Name
string
QType
uint16
Rcode
int
Rcode
int
AuthenticatedData
bool
AuthenticatedData
bool
RecursionAvailable
bool
RecursionAvailable
bool
...
@@ -24,6 +28,10 @@ type item struct {
...
@@ -24,6 +28,10 @@ type item struct {
func
newItem
(
m
*
dns
.
Msg
,
now
time
.
Time
,
d
time
.
Duration
)
*
item
{
func
newItem
(
m
*
dns
.
Msg
,
now
time
.
Time
,
d
time
.
Duration
)
*
item
{
i
:=
new
(
item
)
i
:=
new
(
item
)
if
len
(
m
.
Question
)
!=
0
{
i
.
Name
=
m
.
Question
[
0
]
.
Name
i
.
QType
=
m
.
Question
[
0
]
.
Qtype
}
i
.
Rcode
=
m
.
Rcode
i
.
Rcode
=
m
.
Rcode
i
.
AuthenticatedData
=
m
.
AuthenticatedData
i
.
AuthenticatedData
=
m
.
AuthenticatedData
i
.
RecursionAvailable
=
m
.
RecursionAvailable
i
.
RecursionAvailable
=
m
.
RecursionAvailable
...
@@ -87,3 +95,10 @@ func (i *item) ttl(now time.Time) int {
...
@@ -87,3 +95,10 @@ func (i *item) ttl(now time.Time) int {
ttl
:=
int
(
i
.
origTTL
)
-
int
(
now
.
UTC
()
.
Sub
(
i
.
stored
)
.
Seconds
())
ttl
:=
int
(
i
.
origTTL
)
-
int
(
now
.
UTC
()
.
Sub
(
i
.
stored
)
.
Seconds
())
return
ttl
return
ttl
}
}
func
(
i
*
item
)
matches
(
state
request
.
Request
)
bool
{
if
state
.
QType
()
==
i
.
QType
&&
strings
.
EqualFold
(
state
.
QName
(),
i
.
Name
)
{
return
true
}
return
false
}
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