-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This supports networking restrictions and is compatible with Linux 6.7.
- Loading branch information
Showing
13 changed files
with
368 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package landlock | ||
|
||
// AccessNetSet is a set of Landlockable network access rights. | ||
type AccessNetSet uint64 | ||
|
||
var accessNetNames = []string{ | ||
"bind_tcp", | ||
"connect_tcp", | ||
} | ||
|
||
var supportedAccessNet = AccessNetSet((1 << len(accessNetNames)) - 1) | ||
|
||
func (a AccessNetSet) String() string { | ||
return accessSetString(uint64(a), accessNetNames) | ||
} | ||
|
||
func (a AccessNetSet) isSubset(b AccessNetSet) bool { | ||
return a&b == a | ||
} | ||
|
||
func (a AccessNetSet) intersect(b AccessNetSet) AccessNetSet { | ||
return a & b | ||
} | ||
|
||
func (a AccessNetSet) union(b AccessNetSet) AccessNetSet { | ||
return a | b | ||
} | ||
|
||
func (a AccessNetSet) isEmpty() bool { | ||
return a == 0 | ||
} | ||
|
||
func (a AccessNetSet) valid() bool { | ||
return a.isSubset(supportedAccessNet) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package landlock | ||
|
||
import ( | ||
"fmt" | ||
|
||
ll "github.com/landlock-lsm/go-landlock/landlock/syscall" | ||
) | ||
|
||
type NetRule struct { | ||
access AccessNetSet | ||
port uint16 | ||
} | ||
|
||
func DialTCP(port uint16) NetRule { | ||
return NetRule{ | ||
access: ll.AccessNetConnectTCP, | ||
port: port, | ||
} | ||
} | ||
|
||
// BindTCP is a [Rule] which grants the right to bind a socket to a | ||
// given TCP port. | ||
// | ||
// In Go, the bind(2) operation is usually run as part of | ||
// net.Listen(). | ||
func BindTCP(port uint16) NetRule { | ||
return NetRule{ | ||
access: ll.AccessNetBindTCP, | ||
port: port, | ||
} | ||
} | ||
|
||
func (n NetRule) String() string { | ||
return fmt.Sprintf("ALLOW %v on TCP port %v", n.access, n.port) | ||
} | ||
|
||
func (n NetRule) compatibleWithConfig(c Config) bool { | ||
return n.access.isSubset(c.handledAccessNet) | ||
} | ||
|
||
func (n NetRule) addToRuleset(rulesetFD int, c Config) error { | ||
flags := 0 | ||
attr := &ll.NetServiceAttr{ | ||
AllowedAccess: uint64(n.access), | ||
Port: n.port, | ||
} | ||
return ll.LandlockAddNetServiceRule(rulesetFD, attr, flags) | ||
} | ||
|
||
func (n NetRule) downgrade(c Config) (out Rule, ok bool) { | ||
return NetRule{ | ||
access: n.access.intersect(c.handledAccessNet), | ||
port: n.port, | ||
}, true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.