2016-07-20 11:02:01 +01:00
|
|
|
// (c) Copyright 2016 Hewlett Packard Enterprise Development LP
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
2016-11-13 01:57:20 +00:00
|
|
|
"strconv"
|
2016-08-10 12:51:03 +01:00
|
|
|
|
2020-04-01 21:18:39 +01:00
|
|
|
"github.com/securego/gosec/v2"
|
2023-02-15 19:44:13 +00:00
|
|
|
"github.com/securego/gosec/v2/issue"
|
2016-07-20 11:02:01 +01:00
|
|
|
)
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
type filePermissions struct {
|
2023-02-15 19:44:13 +00:00
|
|
|
issue.MetaData
|
2016-11-13 01:57:20 +00:00
|
|
|
mode int64
|
2021-02-22 08:22:04 +00:00
|
|
|
pkgs []string
|
2016-11-13 01:57:20 +00:00
|
|
|
calls []string
|
|
|
|
}
|
|
|
|
|
2023-09-25 12:11:00 +01:00
|
|
|
// ID returns the ID of the rule.
|
2017-10-05 22:32:03 +01:00
|
|
|
func (r *filePermissions) ID() string {
|
|
|
|
return r.MetaData.ID
|
|
|
|
}
|
|
|
|
|
2016-11-13 01:57:20 +00:00
|
|
|
func getConfiguredMode(conf map[string]interface{}, configKey string, defaultMode int64) int64 {
|
2021-05-31 09:44:12 +01:00
|
|
|
mode := defaultMode
|
2016-11-13 01:57:20 +00:00
|
|
|
if value, ok := conf[configKey]; ok {
|
2019-04-30 10:32:06 +01:00
|
|
|
switch value := value.(type) {
|
2016-11-13 01:57:20 +00:00
|
|
|
case int64:
|
2019-04-30 10:32:06 +01:00
|
|
|
mode = value
|
2016-11-13 01:57:20 +00:00
|
|
|
case string:
|
2019-04-30 10:32:06 +01:00
|
|
|
if m, e := strconv.ParseInt(value, 0, 64); e != nil {
|
2016-12-02 18:20:23 +00:00
|
|
|
mode = defaultMode
|
|
|
|
} else {
|
|
|
|
mode = m
|
|
|
|
}
|
2016-11-13 01:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mode
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2022-10-20 07:48:40 +01:00
|
|
|
func modeIsSubset(subset int64, superset int64) bool {
|
|
|
|
return (subset | superset) == superset
|
|
|
|
}
|
|
|
|
|
2023-09-25 12:11:00 +01:00
|
|
|
// Match checks if the rule is matched.
|
2023-02-15 19:44:13 +00:00
|
|
|
func (r *filePermissions) Match(n ast.Node, c *gosec.Context) (*issue.Issue, error) {
|
2021-02-22 08:22:04 +00:00
|
|
|
for _, pkg := range r.pkgs {
|
|
|
|
if callexpr, matched := gosec.MatchCallByPackage(n, c, pkg, r.calls...); matched {
|
|
|
|
modeArg := callexpr.Args[len(callexpr.Args)-1]
|
2024-05-14 14:27:10 +01:00
|
|
|
if mode, err := gosec.GetInt(modeArg); err == nil && !modeIsSubset(mode, r.mode) || isOsPerm(modeArg) {
|
2023-02-15 19:44:13 +00:00
|
|
|
return c.NewIssue(n, r.ID(), r.What, r.Severity, r.Confidence), nil
|
2021-02-22 08:22:04 +00:00
|
|
|
}
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-05-14 14:27:10 +01:00
|
|
|
// isOsPerm check if the provide ast node contains a os.PermMode symbol
|
|
|
|
func isOsPerm(n ast.Node) bool {
|
|
|
|
if node, ok := n.(*ast.SelectorExpr); ok {
|
|
|
|
if identX, ok := node.X.(*ast.Ident); ok {
|
|
|
|
if identX.Name == "os" && node.Sel != nil && node.Sel.Name == "ModePerm" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-28 11:48:18 +00:00
|
|
|
// NewWritePerms creates a rule to detect file Writes with bad permissions.
|
|
|
|
func NewWritePerms(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
|
2022-03-28 19:28:02 +01:00
|
|
|
mode := getConfiguredMode(conf, id, 0o600)
|
2020-02-28 11:48:18 +00:00
|
|
|
return &filePermissions{
|
|
|
|
mode: mode,
|
2021-02-22 08:22:04 +00:00
|
|
|
pkgs: []string{"io/ioutil", "os"},
|
2020-02-28 11:48:18 +00:00
|
|
|
calls: []string{"WriteFile"},
|
2023-02-15 19:44:13 +00:00
|
|
|
MetaData: issue.MetaData{
|
2020-02-28 11:48:18 +00:00
|
|
|
ID: id,
|
2023-02-15 19:44:13 +00:00
|
|
|
Severity: issue.Medium,
|
|
|
|
Confidence: issue.High,
|
2020-02-28 11:48:18 +00:00
|
|
|
What: fmt.Sprintf("Expect WriteFile permissions to be %#o or less", mode),
|
|
|
|
},
|
|
|
|
}, []ast.Node{(*ast.CallExpr)(nil)}
|
|
|
|
}
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
// NewFilePerms creates a rule to detect file creation with a more permissive than configured
|
|
|
|
// permission mask.
|
2018-07-19 17:42:25 +01:00
|
|
|
func NewFilePerms(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
|
2022-03-28 19:28:02 +01:00
|
|
|
mode := getConfiguredMode(conf, id, 0o600)
|
2017-12-13 12:35:47 +00:00
|
|
|
return &filePermissions{
|
2016-11-13 01:57:20 +00:00
|
|
|
mode: mode,
|
2021-02-22 08:22:04 +00:00
|
|
|
pkgs: []string{"os"},
|
2016-11-13 01:57:20 +00:00
|
|
|
calls: []string{"OpenFile", "Chmod"},
|
2023-02-15 19:44:13 +00:00
|
|
|
MetaData: issue.MetaData{
|
2017-10-05 22:32:03 +01:00
|
|
|
ID: id,
|
2023-02-15 19:44:13 +00:00
|
|
|
Severity: issue.Medium,
|
|
|
|
Confidence: issue.High,
|
2016-11-13 01:57:20 +00:00
|
|
|
What: fmt.Sprintf("Expect file permissions to be %#o or less", mode),
|
2016-07-20 11:02:01 +01:00
|
|
|
},
|
2016-11-13 20:55:31 +00:00
|
|
|
}, []ast.Node{(*ast.CallExpr)(nil)}
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
// NewMkdirPerms creates a rule to detect directory creation with more permissive than
|
|
|
|
// configured permission mask.
|
2018-07-19 17:42:25 +01:00
|
|
|
func NewMkdirPerms(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
|
2022-03-28 19:28:02 +01:00
|
|
|
mode := getConfiguredMode(conf, id, 0o750)
|
2017-12-13 12:35:47 +00:00
|
|
|
return &filePermissions{
|
2016-11-13 01:57:20 +00:00
|
|
|
mode: mode,
|
2021-02-22 08:22:04 +00:00
|
|
|
pkgs: []string{"os"},
|
2016-11-13 01:57:20 +00:00
|
|
|
calls: []string{"Mkdir", "MkdirAll"},
|
2023-02-15 19:44:13 +00:00
|
|
|
MetaData: issue.MetaData{
|
2017-10-05 22:32:03 +01:00
|
|
|
ID: id,
|
2023-02-15 19:44:13 +00:00
|
|
|
Severity: issue.Medium,
|
|
|
|
Confidence: issue.High,
|
2016-07-20 11:02:01 +01:00
|
|
|
What: fmt.Sprintf("Expect directory permissions to be %#o or less", mode),
|
|
|
|
},
|
2016-11-13 20:55:31 +00:00
|
|
|
}, []ast.Node{(*ast.CallExpr)(nil)}
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
2023-09-25 12:11:00 +01:00
|
|
|
|
|
|
|
type osCreatePermissions struct {
|
|
|
|
issue.MetaData
|
2023-09-25 12:18:22 +01:00
|
|
|
mode int64
|
|
|
|
pkgs []string
|
|
|
|
calls []string
|
2023-09-25 12:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultOsCreateMode = 0o666
|
|
|
|
|
|
|
|
// ID returns the ID of the rule.
|
|
|
|
func (r *osCreatePermissions) ID() string {
|
|
|
|
return r.MetaData.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match checks if the rule is matched.
|
|
|
|
func (r *osCreatePermissions) Match(n ast.Node, c *gosec.Context) (*issue.Issue, error) {
|
|
|
|
for _, pkg := range r.pkgs {
|
|
|
|
if _, matched := gosec.MatchCallByPackage(n, c, pkg, r.calls...); matched {
|
|
|
|
if !modeIsSubset(defaultOsCreateMode, r.mode) {
|
|
|
|
return c.NewIssue(n, r.ID(), r.What, r.Severity, r.Confidence), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-11-29 22:46:31 +00:00
|
|
|
// NewOsCreatePerms creates a rule to detect file creation with a more permissive than configured
|
2023-09-25 12:11:00 +01:00
|
|
|
// permission mask.
|
|
|
|
func NewOsCreatePerms(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
|
|
|
|
mode := getConfiguredMode(conf, id, 0o666)
|
|
|
|
return &osCreatePermissions{
|
|
|
|
mode: mode,
|
|
|
|
pkgs: []string{"os"},
|
|
|
|
calls: []string{"Create"},
|
|
|
|
MetaData: issue.MetaData{
|
|
|
|
ID: id,
|
|
|
|
Severity: issue.Medium,
|
|
|
|
Confidence: issue.High,
|
|
|
|
What: fmt.Sprintf("Expect file permissions to be %#o or less but os.Create used with default permissions %#o",
|
|
|
|
mode, defaultOsCreateMode),
|
|
|
|
},
|
|
|
|
}, []ast.Node{(*ast.CallExpr)(nil)}
|
|
|
|
}
|