| 1 |
package landlock |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
ll "github.com/landlock-lsm/go-landlock/landlock/syscall" |
| 7 |
) |
| 8 |
|
| 9 |
// FSRule is a Rule which permits access to filesystem paths. |
| 10 |
type FSRule struct { |
| 11 |
accessFS AccessFSSet |
| 12 |
paths []string |
| 13 |
enforceSubset bool // enforce that accessFS is a subset of cfg.handledAccessFS |
| 14 |
ignoreMissing bool // ignore missing paths |
| 15 |
} |
| 16 |
|
| 17 |
// withRights adds the given access rights to the rights enforced in the FSRule |
| 18 |
// and returns the result as a new FSRule. |
| 19 |
func (r FSRule) withRights(a AccessFSSet) FSRule { |
| 20 |
r.accessFS = r.accessFS.union(a) |
| 21 |
return r |
| 22 |
} |
| 23 |
|
| 24 |
// intersectRights intersects the given access rights with the rights |
| 25 |
// enforced in the FSRule and returns the result as a new FSRule. |
| 26 |
func (r FSRule) intersectRights(a AccessFSSet) FSRule { |
| 27 |
r.accessFS = r.accessFS.intersect(a) |
| 28 |
return r |
| 29 |
} |
| 30 |
|
| 31 |
// WithRefer adds the "refer" access right to a FSRule. |
| 32 |
// |
| 33 |
// Notably, asking for the "refer" access right does not work on |
| 34 |
// kernels below 5.19. In best effort mode, this will fall back to not |
| 35 |
// using Landlock enforcement at all on these kernel versions. If you |
| 36 |
// want to use Landlock on these kernels, do not use the "refer" |
| 37 |
// access right. |
| 38 |
func (r FSRule) WithRefer() FSRule { |
| 39 |
return r.withRights(ll.AccessFSRefer) |
| 40 |
} |
| 41 |
|
| 42 |
// WithIoctlDev adds the "ioctl dev" access right to a FSRule. |
| 43 |
// |
| 44 |
// It is uncommon to need this access right, so it is not part of |
| 45 |
// [RWFiles] or [RWDirs]. |
| 46 |
func (r FSRule) WithIoctlDev() FSRule { |
| 47 |
return r.withRights(ll.AccessFSIoctlDev) |
| 48 |
} |
| 49 |
|
| 50 |
// WithResolveUnix adds the "resolve unix" access right to a FSRule. |
| 51 |
// |
| 52 |
// This access right covers the use of connect(2) and sendmsg(2) on |
| 53 |
// pathname UNIX domain sockets (see unix(7)), provided that |
| 54 |
// the remote process is outside of the enforced Landlock domain. |
| 55 |
// |
| 56 |
// In Go, commonly affected invocations are: |
| 57 |
// |
| 58 |
// - Invocations of [net.Dial] with the network parameter set to |
| 59 |
// "unix", "unixgram" or "unixpacket". |
| 60 |
// - Lookups done through glibc's Name Service Switch glibc facility |
| 61 |
// (in binaries built with cgo). |
| 62 |
// |
| 63 |
// It is uncommon to need this access right, so it is not part of |
| 64 |
// [RWFiles] or [RWDirs]. |
| 65 |
func (r FSRule) WithResolveUnix() FSRule { |
| 66 |
return r.withRights(ll.AccessFSResolveUnix) |
| 67 |
} |
| 68 |
|
| 69 |
// IgnoreIfMissing gracefully ignores missing paths. |
| 70 |
// |
| 71 |
// Under normal circumstances, referring to a non-existing path in a rule would |
| 72 |
// lead to a runtime error. When the rule uses the IgnoreIfMissing modifier, |
| 73 |
// these runtime errors are ignored. This can be useful for optional |
| 74 |
// configuration paths, which are only read but not written. |
| 75 |
func (r FSRule) IgnoreIfMissing() FSRule { |
| 76 |
r.ignoreMissing = true |
| 77 |
return r |
| 78 |
} |
| 79 |
|
| 80 |
func (r FSRule) String() string { |
| 81 |
return fmt.Sprintf("REQUIRE %v for paths %v", r.accessFS, r.paths) |
| 82 |
} |
| 83 |
|
| 84 |
// compatibleWithConfig returns true if the given rule is compatible |
| 85 |
// for use with the config c. |
| 86 |
func (r FSRule) compatibleWithConfig(c Config) bool { |
| 87 |
a := r.accessFS |
| 88 |
if !r.enforceSubset { |
| 89 |
// If !enforceSubset, this FSRule is potentially overspecifying flags, |
| 90 |
// so we should not check the subset property. We make an exception |
| 91 |
// for the "refer" flag, which should still get checked though. |
| 92 |
a = a.intersect(ll.AccessFSRefer) |
| 93 |
} |
| 94 |
return a.isSubset(c.handledAccessFS) |
| 95 |
} |
| 96 |
|
| 97 |
// downgrade calculates the actual ruleset to be enforced given the |
| 98 |
// current config (and assuming that the config is going to work under |
| 99 |
// the running kernel). |
| 100 |
// |
| 101 |
// It establishes that rule.accessFS ⊆ c.handledAccessFS. |
| 102 |
// |
| 103 |
// If ok is false, downgrade is impossible and we need to fall back to doing nothing. |
| 104 |
func (r FSRule) downgrade(c Config) (out Rule, ok bool) { |
| 105 |
// In case that "refer" is requested on a path, we |
| 106 |
// require Landlock V2+, or we have to downgrade to V0. |
| 107 |
// You can't get the refer capability with V1, but linking/ |
| 108 |
// renaming files is always implicitly restricted. |
| 109 |
if hasRefer(r.accessFS) && !hasRefer(c.handledAccessFS) { |
| 110 |
return FSRule{}, false |
| 111 |
} |
| 112 |
return r.intersectRights(c.handledAccessFS), true |
| 113 |
} |
| 114 |
|
| 115 |
func hasRefer(a AccessFSSet) bool { |
| 116 |
return a&ll.AccessFSRefer != 0 |
| 117 |
} |
| 118 |
|
| 119 |
// PathAccess is a [Rule] which grants the access rights specified by |
| 120 |
// accessFS to the file hierarchies under the given paths. |
| 121 |
// |
| 122 |
// When accessFS is larger than what is permitted by the Landlock |
| 123 |
// version in use, only the applicable subset of accessFS will be used. |
| 124 |
// |
| 125 |
// Most users should use the functions [RODirs], [RWDirs], [ROFiles] |
| 126 |
// and [RWFiles] instead, which provide canned rules for commonly |
| 127 |
// used values of accessFS. |
| 128 |
// |
| 129 |
// Filesystem access rights are represented using bits in a uint64. |
| 130 |
// The individual access rights and their meaning are defined in the |
| 131 |
// landlock/syscall package and explained further in the |
| 132 |
// [Kernel Documentation about Access Rights]. |
| 133 |
// |
| 134 |
// accessFS must be a subset of the permissions that the Config |
| 135 |
// restricts. |
| 136 |
// |
| 137 |
// [Kernel Documentation about Access Rights]: https://www.kernel.org/doc/html/latest/userspace-api/landlock.html#access-rights |
| 138 |
func PathAccess(accessFS AccessFSSet, paths ...string) FSRule { |
| 139 |
return FSRule{ |
| 140 |
accessFS: accessFS, |
| 141 |
paths: paths, |
| 142 |
enforceSubset: true, |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// RODirs is a [Rule] which grants common read-only access to files |
| 147 |
// and directories and permits executing files. |
| 148 |
func RODirs(paths ...string) FSRule { |
| 149 |
return FSRule{ |
| 150 |
accessFS: accessFSRead, |
| 151 |
paths: paths, |
| 152 |
enforceSubset: false, |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
// RWDirs is a [Rule] which grants full (read and write) access to |
| 157 |
// files and directories under the given paths. |
| 158 |
// |
| 159 |
// Noteworthy operations which are *not* covered by RWDirs: |
| 160 |
// |
| 161 |
// - RWDirs does *not* grant the right to *reparent or link* files |
| 162 |
// across different directories. If this access right is |
| 163 |
// required, use [FSRule.WithRefer]. |
| 164 |
// |
| 165 |
// - RWDirs does *not* grant the right to *use IOCTL* on device |
| 166 |
// files. If this access right is required, use |
| 167 |
// [FSRule.WithIoctlDev]. |
| 168 |
// |
| 169 |
// - RWDirs does *not* grant the right to use *connect(2) and |
| 170 |
// sendmsg(2) on pathname UNIX domain sockets*. If this access |
| 171 |
// right is required, use [FSRule.WithResolveUnix]. |
| 172 |
func RWDirs(paths ...string) FSRule { |
| 173 |
return FSRule{ |
| 174 |
accessFS: accessFSReadWrite, |
| 175 |
paths: paths, |
| 176 |
enforceSubset: false, |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
// ROFiles is a [Rule] which grants common read access to individual |
| 181 |
// files, but not to directories, for the file hierarchies under the |
| 182 |
// given paths. |
| 183 |
func ROFiles(paths ...string) FSRule { |
| 184 |
return FSRule{ |
| 185 |
accessFS: accessFSRead & accessFile, |
| 186 |
paths: paths, |
| 187 |
enforceSubset: false, |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// RWFiles is a [Rule] which grants common read and write access to |
| 192 |
// files under the given paths, but it does not permit access to |
| 193 |
// directories. |
| 194 |
// |
| 195 |
// Noteworthy operations which are *not* covered by RWFiles: |
| 196 |
// |
| 197 |
// - RWFiles does *not* grant the right to *use IOCTL* on device |
| 198 |
// files. If this access right is required, use |
| 199 |
// [FSRule.WithIoctlDev]. |
| 200 |
// |
| 201 |
// - RWFiles does *not* grant the right to use *connect(2) and |
| 202 |
// sendmsg(2) on pathname UNIX domain sockets*. If this access |
| 203 |
// right is required, use [FSRule.WithResolveUnix]. |
| 204 |
func RWFiles(paths ...string) FSRule { |
| 205 |
return FSRule{ |
| 206 |
accessFS: accessFSReadWrite & accessFile, |
| 207 |
paths: paths, |
| 208 |
enforceSubset: false, |
| 209 |
} |
| 210 |
} |