2023-03-21 06:49:24 -06:00
|
|
|
//go:build linux
|
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package protect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/landlock-lsm/go-landlock/landlock"
|
|
|
|
)
|
|
|
|
|
2023-03-21 09:06:08 -06:00
|
|
|
type lands struct {
|
|
|
|
paths []landlock.PathOpt
|
|
|
|
}
|
2023-03-21 06:49:24 -06:00
|
|
|
|
|
|
|
var landToLock lands
|
|
|
|
|
2023-03-21 09:06:08 -06:00
|
|
|
func landAdd(path, flags string) error {
|
2023-03-21 06:49:24 -06:00
|
|
|
s, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch mode := s.Mode(); {
|
|
|
|
case mode.IsDir():
|
|
|
|
switch flags {
|
|
|
|
case "r":
|
2023-03-21 09:06:08 -06:00
|
|
|
landToLock.paths = append(landToLock.paths, landlock.RODirs(path))
|
|
|
|
default:
|
|
|
|
landToLock.paths = append(landToLock.paths, landlock.RWDirs(path))
|
2023-03-21 06:49:24 -06:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
switch flags {
|
|
|
|
case "r":
|
2023-03-21 09:06:08 -06:00
|
|
|
landToLock.paths = append(landToLock.paths, landlock.ROFiles(path))
|
|
|
|
default:
|
|
|
|
landToLock.paths = append(landToLock.paths, landlock.RWFiles(path))
|
2023-03-21 06:49:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-21 09:06:08 -06:00
|
|
|
func (l lands) landWalk() []landlock.PathOpt {
|
|
|
|
return l.paths
|
2023-03-21 06:49:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func unveil(path string, flags string) error {
|
|
|
|
if path == "" {
|
|
|
|
err := landlock.V3.BestEffort().RestrictPaths()
|
|
|
|
if err != nil {
|
|
|
|
return landlock.V2.BestEffort().RestrictPaths()
|
|
|
|
}
|
|
|
|
}
|
2023-03-21 09:06:08 -06:00
|
|
|
return landAdd(path, flags)
|
2023-03-21 06:49:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func unveilBlock() error {
|
|
|
|
err := landlock.V3.RestrictPaths(landToLock.landWalk()...)
|
|
|
|
if err != nil {
|
|
|
|
return landlock.V2.RestrictPaths(landToLock.landWalk()...)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func pledge(promises string) error {
|
|
|
|
return nil
|
|
|
|
}
|