Skip to content
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

[feature] Optimize path traversal sanitization for link / interface access #57

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions link/interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package link

import "path/filepath"

// Interface is the low-level representation of a network interface
type Interface struct {
Name string
Expand All @@ -10,13 +12,13 @@ type Interface struct {
// NewInterface instantiates a new network interface and obtains its basic parameters
func NewInterface(name string) (iface Interface, err error) {
iface = Interface{
Name: name,
Name: filepath.Clean(name),
}

if iface.Index, err = getIndex(name); err != nil {
if iface.Index, err = iface.getIndex(); err != nil {
return
}
if iface.Type, err = getLinkType(name); err != nil {
if iface.Type, err = iface.getLinkType(); err != nil {
return
}

Expand Down
14 changes: 6 additions & 8 deletions link/interface_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"math"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -50,7 +49,7 @@ func Interfaces() ([]Interface, error) {
// IsUp determines if an interface is currently up (at the time of the call)
func (i Interface) IsUp() (bool, error) {

data, err := os.ReadFile(filepath.Clean(netBasePath + i.Name + netFlagsPath))
data, err := os.ReadFile(netBasePath + i.Name + netFlagsPath)
if err != nil {
return false, err
}
Expand All @@ -66,9 +65,9 @@ func (i Interface) IsUp() (bool, error) {

////////////////////////////////////////////////////////////////////////////////

func getIndex(name string) (int, error) {
func (i Interface) getIndex() (int, error) {

data, err := os.ReadFile(filepath.Clean(netBasePath + name + netIndexPath))
data, err := os.ReadFile(netBasePath + i.Name + netIndexPath)
if err != nil {
return -1, err
}
Expand All @@ -87,10 +86,9 @@ func getIndex(name string) (int, error) {
return -1, ErrIndexOutOfBounds
}

func getLinkType(name string) (Type, error) {
func (i Interface) getLinkType() (Type, error) {

sysPath := netBasePath + name + netTypePath
data, err := os.ReadFile(filepath.Clean(sysPath))
data, err := os.ReadFile(netBasePath + i.Name + netTypePath)
if err != nil {
return -1, err
}
Expand All @@ -102,7 +100,7 @@ func getLinkType(name string) (Type, error) {
}

if val < 0 || val > 65535 {
return -1, fmt.Errorf("invalid link type read from `%s`: %d", sysPath, val)
return -1, fmt.Errorf("invalid link type read from `%s`: %d", netBasePath+i.Name+netTypePath, val)
}

return Type(val), nil
Expand Down
Loading