mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-12-21 04:20:23 +00:00
dd668e59f1
gosec reports several issues, none of them looks very critical. With this change the following concerns have been addressed: [pkg/cephfs/nodeserver.go:229] - G302: Expect file permissions to be 0600 or less (Confidence: HIGH, Severity: MEDIUM) > os.Chmod(targetPath, 0777) [pkg/cephfs/util.go:39] - G204: Subprocess launched with variable (Confidence: HIGH, Severity: MEDIUM) > exec.Command(program, args...) [pkg/rbd/nodeserver.go:156] - G302: Expect file permissions to be 0600 or less (Confidence: HIGH, Severity: MEDIUM) > os.Chmod(stagingTargetPath, 0777) [pkg/rbd/nodeserver.go:205] - G302: Expect file permissions to be 0600 or less (Confidence: HIGH, Severity: MEDIUM) > os.OpenFile(mountPath, os.O_CREATE|os.O_RDWR, 0750) [pkg/rbd/rbd_util.go:797] - G304: Potential file inclusion via variable (Confidence: HIGH, Severity: MEDIUM) > ioutil.ReadFile(fPath) [pkg/util/cephcmds.go:35] - G204: Subprocess launched with variable (Confidence: HIGH, Severity: MEDIUM) > exec.Command(program, args...) [pkg/util/credentials.go:47] - G104: Errors unhandled. (Confidence: HIGH, Severity: LOW) > os.Remove(tmpfile.Name()) [pkg/util/credentials.go:92] - G104: Errors unhandled. (Confidence: HIGH, Severity: LOW) > os.Remove(cr.KeyFile) [pkg/util/pidlimit.go:74] - G304: Potential file inclusion via variable (Confidence: HIGH, Severity: MEDIUM) > os.Open(pidsMax) URL: https://github.com/securego/gosec Signed-off-by: Niels de Vos <ndevos@redhat.com>
122 lines
2.6 KiB
Go
122 lines
2.6 KiB
Go
/*
|
|
Copyright 2019 The Ceph-CSI Authors.
|
|
|
|
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 util
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
procCgroup = "/proc/self/cgroup"
|
|
sysPidsMaxFmt = "/sys/fs/cgroup/pids%s/pids.max"
|
|
)
|
|
|
|
// return the cgouprs "pids.max" file of the current process
|
|
//
|
|
// find the line containing the pids group from the /proc/self/cgroup file
|
|
// $ grep 'pids' /proc/self/cgroup
|
|
// 7:pids:/kubepods.slice/kubepods-besteffort.slice/....scope
|
|
// $ cat /sys/fs/cgroup/pids + *.scope + /pids.max
|
|
func getCgroupPidsFile() (string, error) {
|
|
cgroup, err := os.Open(procCgroup)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer cgroup.Close()
|
|
|
|
scanner := bufio.NewScanner(cgroup)
|
|
var slice string
|
|
for scanner.Scan() {
|
|
parts := strings.Split(scanner.Text(), ":")
|
|
if parts == nil || len(parts) < 3 {
|
|
continue
|
|
}
|
|
if parts[1] == "pids" {
|
|
slice = parts[2]
|
|
break
|
|
}
|
|
}
|
|
if slice == "" {
|
|
return "", fmt.Errorf("could not find a cgroup for 'pids'")
|
|
}
|
|
|
|
pidsMax := fmt.Sprintf(sysPidsMaxFmt, slice)
|
|
return pidsMax, nil
|
|
}
|
|
|
|
// GetPIDLimit returns the current PID limit, or an error. A value of -1
|
|
// translates to "max".
|
|
func GetPIDLimit() (int, error) {
|
|
pidsMax, err := getCgroupPidsFile()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
f, err := os.Open(pidsMax) // #nosec - intended reading from /sys/...
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer f.Close()
|
|
|
|
maxPidsStr, err := bufio.NewReader(f).ReadString('\n')
|
|
if err != nil && err != io.EOF {
|
|
return 0, err
|
|
}
|
|
maxPidsStr = strings.TrimRight(maxPidsStr, "\n")
|
|
|
|
maxPids := -1
|
|
if maxPidsStr != "max" {
|
|
maxPids, err = strconv.Atoi(maxPidsStr)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
return maxPids, nil
|
|
}
|
|
|
|
// SetPIDLimit configures the given PID limit for the current process. A value
|
|
// of -1 translates to "max".
|
|
func SetPIDLimit(limit int) error {
|
|
limitStr := "max"
|
|
if limit != -1 {
|
|
limitStr = fmt.Sprintf("%d", limit)
|
|
}
|
|
|
|
pidsMax, err := getCgroupPidsFile()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
f, err := os.Create(pidsMax)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
_, err = f.WriteString(limitStr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|