-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CNI configuration size limit #119
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ import ( | |
"github.com/containernetworking/cni/pkg/version" | ||
) | ||
|
||
const MaxFileSizeDefault = 2 * 1024 * 1024 // 2 Megabytes | ||
|
||
// Opt sets options for a CNI instance | ||
type Opt func(c *libcni) error | ||
|
||
|
@@ -83,6 +85,13 @@ func WithMinNetworkCount(count int) Opt { | |
} | ||
} | ||
|
||
func WithMaxNetworkConfigurationSize(max int64) Opt { | ||
return func(c *libcni) error { | ||
c.cniConfigSizeMax = max | ||
return nil | ||
} | ||
} | ||
|
||
// WithLoNetwork can be used to load the loopback | ||
// network config. | ||
func WithLoNetwork(c *libcni) error { | ||
|
@@ -134,6 +143,16 @@ func WithConfIndex(bytes []byte, index int) Opt { | |
// with path only. | ||
func WithConfFile(fileName string) Opt { | ||
return func(c *libcni) error { | ||
val, err := isValidSize(fileName, c.cniConfigSizeMax) | ||
|
||
if err != nil { | ||
return fmt.Errorf("unknown error occurred when trying to get file size of %s", fileName) | ||
} | ||
|
||
if !val { | ||
return fmt.Errorf("CNI config in %s exceeded max size of %d", fileName, c.cniConfigSizeMax) | ||
} | ||
|
||
conf, err := cnilibrary.ConfFromFile(fileName) | ||
if err != nil { | ||
return err | ||
|
@@ -175,6 +194,16 @@ func WithConfListBytes(bytes []byte) Opt { | |
// with path only. | ||
func WithConfListFile(fileName string) Opt { | ||
return func(c *libcni) error { | ||
val, err := isValidSize(fileName, c.cniConfigSizeMax) | ||
|
||
if err != nil { | ||
return fmt.Errorf("unknown error occurred when trying to get file size of %s", fileName) | ||
} | ||
|
||
if !val { | ||
return fmt.Errorf("CNI config in %s exceeded max size of %d", fileName, c.cniConfigSizeMax) | ||
} | ||
|
||
confList, err := cnilibrary.ConfListFromFile(fileName) | ||
if err != nil { | ||
return err | ||
|
@@ -229,6 +258,16 @@ func loadFromConfDir(c *libcni, max int) error { | |
i := 0 | ||
var networks []*Network | ||
for _, confFile := range files { | ||
val, err := isValidSize(confFile, c.cniConfigSizeMax) | ||
|
||
if err != nil { | ||
return fmt.Errorf("unknown error occurred when trying to get file size of %s", confFile) | ||
} | ||
|
||
if !val { | ||
return fmt.Errorf("CNI config in %s exceeded max size of %d", confFile, c.cniConfigSizeMax) | ||
} | ||
|
||
var confList *cnilibrary.NetworkConfigList | ||
if strings.HasSuffix(confFile, ".conflist") { | ||
confList, err = cnilibrary.ConfListFromFile(confFile) | ||
|
@@ -271,3 +310,19 @@ func loadFromConfDir(c *libcni, max int) error { | |
c.networks = append(c.networks, networks...) | ||
return nil | ||
} | ||
|
||
func isValidSize(filepath string, maxSize int64) (bool, error) { | ||
file, err := os.Open(filepath) | ||
|
||
if err != nil { | ||
return false, err | ||
} | ||
|
||
fileInfo, err := file.Stat() | ||
|
||
if err != nil { | ||
return false, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same.. |
||
} | ||
|
||
return fileInfo.Size() <= maxSize, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not there is .. fine should fail to exist in a different way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by is fine, I mean since the file does not exist it is not larger than max and should be ignored here .. just return true...