Skip to content

Commit

Permalink
Merge pull request #57 from fako1024/56-optimize-interface-path-trave…
Browse files Browse the repository at this point in the history
…rsal-sanitization

[feature] Optimize path traversal sanitization for link / interface access
  • Loading branch information
fako1024 committed Aug 22, 2023
2 parents 6e647c8 + 38ee84d commit a7226e9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
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

0 comments on commit a7226e9

Please sign in to comment.