-
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 patch 11.
- Loading branch information
Showing
13 changed files
with
362 additions
and
34 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,52 @@ | ||
package landlock | ||
|
||
import ( | ||
"fmt" | ||
|
||
ll "github.com/landlock-lsm/go-landlock/landlock/syscall" | ||
) | ||
|
||
type NetOpt struct { | ||
access AccessNetSet | ||
port uint16 | ||
} | ||
|
||
func DialTCP(port uint16) NetOpt { | ||
return NetOpt{ | ||
access: ll.AccessNetConnectTCP, | ||
port: port, | ||
} | ||
} | ||
|
||
// TODO: Should it be called ListenTCP? (In Go, the notions of | ||
// "binding" and "listening" are often conflated.) | ||
func BindTCP(port uint16) NetOpt { | ||
return NetOpt{ | ||
access: ll.AccessNetBindTCP, | ||
port: port, | ||
} | ||
} | ||
|
||
func (n NetOpt) String() string { | ||
return fmt.Sprintf("ALLOW %v on TCP port %v", n.access, n.port) | ||
} | ||
|
||
func (n NetOpt) compatibleWithConfig(c Config) bool { | ||
return n.access.isSubset(c.handledAccessNet) | ||
} | ||
|
||
func (n NetOpt) 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 NetOpt) downgrade(c Config) (out Rule, ok bool) { | ||
return NetOpt{ | ||
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.