talos/pkg/kernel/kspp/kspp.go
Dmitriy Matrenichev 93e55b85f2
chore: bump golangci-lint to v1.50.0
I had to do several things:
- contextcheck now supports Go 1.18 generics, but I had to disable it because of this https://github.com/kkHAIKE/contextcheck/issues/9
- dupword produces to many false positives, so it's also disabled
- revive found all packages which didn't have a documentation comment before. And tehre is A LOT of them. I updated some of them, but gave up at some point and just added them to exclude rules for now.
- change lint-vulncheck to use `base` stage as base

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
2022-10-20 18:33:19 +03:00

83 lines
2.3 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Package kspp implements KSPP kernel parameters enforcement.
package kspp
import (
"fmt"
"github.com/hashicorp/go-multierror"
"github.com/talos-systems/go-procfs/procfs"
"github.com/talos-systems/talos/pkg/machinery/kernel"
)
// RequiredKSPPKernelParameters is the set of kernel parameters required to
// satisfy the KSPP.
var RequiredKSPPKernelParameters = procfs.Parameters{
// init_on_alloc and init_on_free are not enforced, as they default to '1' in kernel config
// this way they can be overridden via installer extra args in case of severe performance issues
// procfs.NewParameter("init_on_alloc").Append("1"),
// procfs.NewParameter("init_on_free").Append("1"),
procfs.NewParameter("slab_nomerge").Append(""),
procfs.NewParameter("pti").Append("on"),
}
// EnforceKSPPKernelParameters verifies that all required KSPP kernel
// parameters are present with the right value.
func EnforceKSPPKernelParameters() error {
var result *multierror.Error
for _, values := range RequiredKSPPKernelParameters {
var val *string
if val = procfs.ProcCmdline().Get(values.Key()).First(); val == nil {
result = multierror.Append(result, fmt.Errorf("KSPP kernel parameter %s is required", values.Key()))
continue
}
expected := values.First()
if *val != *expected {
result = multierror.Append(result, fmt.Errorf("KSPP kernel parameter %s was found with value %s, expected %s", values.Key(), *val, *expected))
}
}
return result.ErrorOrNil()
}
// GetKernelParams returns the list of KSPP kernel parameters.
func GetKernelParams() []*kernel.Param {
return []*kernel.Param{
{
Key: "proc.sys.kernel.kptr_restrict",
Value: "1",
},
{
Key: "proc.sys.kernel.dmesg_restrict",
Value: "1",
},
{
Key: "proc.sys.kernel.perf_event_paranoid",
Value: "3",
},
{
Key: "proc.sys.kernel.yama.ptrace_scope",
Value: "1",
},
{
Key: "proc.sys.user.max_user_namespaces",
Value: "0",
},
{
Key: "proc.sys.kernel.unprivileged_bpf_disabled",
Value: "1",
},
{
Key: "proc.sys.net.core.bpf_jit_harden",
Value: "2",
},
}
}