| 1 | // Package restrict can be used to restrict further file system access of the |
| 2 | // process if the operating system provides an API for that. |
| 3 | package restrictscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/restrict`/ |
| 4 | |
| 5 | import ( |
| 6 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 7 | "logscip-go gomod github.com/golang/go/src go1.25.0 log/" |
| 8 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 9 | |
| 10 | "github.com/landlock-lsm/go-landlock/landlockscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/" |
| 11 | ) |
| 12 | |
| 13 | // ExtraHook is set when testing to make the landlock rule set more permissive. |
| 14 | var ExtraHookscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/restrict`/ExtraHook. func() []landlockscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/.Rulescip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/Rule# |
| 15 | |
| 16 | func MaybeFileSystemscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/restrict`/MaybeFileSystem().(roDirsOrFiles []string, rwDirs []string) error { |
| 17 | re := ExtraHookscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/restrict`/ExtraHook. |
| 18 | if re == nil { |
| 19 | re = func() []landlockscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/.Rulescip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/Rule# { |
| 20 | return nil |
| 21 | } |
| 22 | } |
| 23 | var roDirs, roFiles []string |
| 24 | for _, fn := range roDirsOrFiles { |
| 25 | st, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(fn) |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | if st.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#IsDir().() { |
| 30 | roDirs = append(roDirs, fn) |
| 31 | } else { |
| 32 | roFiles = append(roFiles, fn) |
| 33 | } |
| 34 | } |
| 35 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("setting up landlock ACL (paths ro: %q, paths rw: %q)", roDirs, rwDirs) |
| 36 | err := landlockscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/.V3scip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/V3..BestEffortscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/Config#BestEffort().().RestrictPathsscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/Config#RestrictPaths().( |
| 37 | append(re(), []landlockscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/.Rulescip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/Rule#{ |
| 38 | landlockscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/.RODirsscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/RODirs().(defaultRoDirsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/restrict`/defaultRoDirs....).IgnoreIfMissingscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/FSRule#IgnoreIfMissing().(), |
| 39 | landlockscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/.RODirsscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/RODirs().(roDirs...).IgnoreIfMissingscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/FSRule#IgnoreIfMissing().(), |
| 40 | landlockscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/.ROFilesscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/ROFiles().(roFiles...).IgnoreIfMissingscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/FSRule#IgnoreIfMissing().(), |
| 41 | landlockscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/.RWDirsscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/RWDirs().(rwDirs...).WithReferscip-go gomod github.com/landlock-lsm/go-landlock 1544bccde3a3 `github.com/landlock-lsm/go-landlock/landlock`/FSRule#WithRefer().(), |
| 42 | }...)...) |
| 43 | if err != nil { |
| 44 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("landlock: %v", err) |
| 45 | } |
| 46 | |
| 47 | // Check whether landlock worked and print the result to the log. |
| 48 | // |
| 49 | // We use /sys because that path should never be required |
| 50 | // for regular functioning, yet is standard enough to be present |
| 51 | // on all supported Linux versions (including gokrazy). |
| 52 | const verifyPath = "/sys" |
| 53 | _, err = osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadDirscip-go gomod github.com/golang/go/src go1.25.0 os/ReadDir().(verifyPath) |
| 54 | if err == nil { |
| 55 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("landlock seems ineffective: readdir(%s) unexpectedly worked!", verifyPath) |
| 56 | } else { |
| 57 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("landlock verified: readdir(%s) = %v", verifyPath, err) |
| 58 | } |
| 59 | |
| 60 | return nil |
| 61 | } |