bitbake: Add basic landlock support
cover bitbake: Add basic landlock support
When current implementation runs in an unprivileged docker container, basic networking will be allowed by default in all steps, ignoring the network varflags intention.
Introduce support for landlock blocking of bind and connect, providing basic support for blocking TCP.
UDP is corrently beeing worked on upstream, but not yet supported.
Landlock requires PR_SET_NO_NEW_PRIVS to prevent escape, which is also attempted, this prevents privilege escalation from child. devshell and related are already tagged with network varflag and can sudo at will.
syscall ABI is asm-generic, 5.13+ for all archs except alpha. On alpha, we leak 2 fd:s in the childs context before graceful exit, which is cleaned up at child termination.
Don't have an alpha target, so this is not tested, evaluated via static analysis only.
Landlock also provides a future possibility for filesystem limitations with the purpose of catching bugs, and preventing persistance of supply chain releated payloads. writes to f.ex. .bashrc and friends.
---
David Nyström (2):
utils: Add landlock_restrict_network function
bitbake-worker: Call landlock_restrict_network for tasks without network
bin/bitbake-worker | 2 ++ lib/bb/utils.py | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+)
---
base-commit: a0158cc8d8f29233a9ade63a4c3ce2e939c4bf16
change-id: 20260611-landlock-621bc1a15305
Best regards,
Signature · 1 line
David Nyström <[email protected]>
RFC PATCH 1/1 1/2] utils: Add landlock_restrict_network function
Add landlock_restrict_network() which blocks TCP bind/connect using Landlock LSM (ABI v4+, kernel 6.7+). Designed to stack with the existing disable_network() namespace isolation, covering the case where disable_network() is skipped for non-local UIDs.
Gracefully returns False on older kernels (ABI < 4).
--- lib/bb/utils.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
lib/bb/utils.py +26 -0
@@ -2054,6 +2054,32 @@ def disable_network(uid=None, gid=None): with open("/proc/self/gid_map", "w") as f: f.write("%s %s 1" % (gid, gid)) +def landlock_restrict_network(): + """Block TCP bind/connect using Landlock LSM (ABI v4+, kernel 6.7+). + Gracefully skipped on older kernels. Stacks with disable_network().""" + + NR_CREATE = 444 # landlock_create_ruleset + NR_SELF = 446 # landlock_restrict_self + NET_TCP = 0x3 # BIND_TCP | CONNECT_TCP + + libc = ctypes.CDLL('libc.so.6') + + abi = libc.syscall(NR_CREATE, 0, 0, 1) + if abi < 4: + return False + + attr = struct.pack("QQ", 0, NET_TCP) + buf = ctypes.create_string_buffer(attr) + fd = libc.syscall(NR_CREATE, buf, len(attr), 0) + if fd < 0: + return False + + libc.prctl(38, 1, 0, 0, 0) # PR_SET_NO_NEW_PRIVS + r = libc.syscall(NR_SELF, fd, 0) + os.close(fd) + return r == 0 + + def export_proxies(d): from bb.fetch2 import get_fetcher_environment """ export common proxies variables from datastore to environment """
Signature · 1 line
2.53.0
Re: [bitbake-devel] [PATCH [RFC] 1/2] utils: Add landlock_restrict_network function
Quoted 14 lines · David Nyström via lists.openembedded.org
lib/bb/utils.py +14
-0+def landlock_restrict_network(): + """Block TCP bind/connect using Landlock LSM (ABI v4+, kernel 6.7+). + Gracefully skipped on older kernels. Stacks with disable_network().""" + + NR_CREATE = 444 # landlock_create_ruleset + NR_SELF = 446 # landlock_restrict_self + NET_TCP = 0x3 # BIND_TCP | CONNECT_TCP + + libc = ctypes.CDLL('libc.so.6') + + abi = libc.syscall(NR_CREATE, 0, 0, 1) + if abi < 4: + return False +
lib/bb/utils.py def disable_network(uid=None, gid=None): · line 2080
Quoted 10 lines
lib/bb/utils.py +10
-0+ attr = struct.pack("QQ", 0, NET_TCP) + buf = ctypes.create_string_buffer(attr) + fd = libc.syscall(NR_CREATE, buf, len(attr), 0) + if fd < 0: + return False + + libc.prctl(38, 1, 0, 0, 0) # PR_SET_NO_NEW_PRIVS + r = libc.syscall(NR_SELF, fd, 0) + os.close(fd) + return r == 0
Far too many magic numbers. I would really want to do this with an API.
This also needs some kind of test, e.g. that the function indeed has the desired effect.
Alex
Re: [bitbake-devel] [PATCH [RFC] 1/2] utils: Add landlock_restrict_network function
Quoted 33 lines · Alexander Kanavin via lists.openembedded.org
Quoted 16 lines · David Nyström via lists.openembedded.org
+def
landlock_restrict_network():
+ """Block TCP bind/connect using Landlock LSM (ABI v4+, kernel 6.7+).
+ Gracefully skipped on older kernels. Stacks withdisable_network()."""lib/bb/utils.py +11
-0+ + NR_CREATE = 444 # landlock_create_ruleset + NR_SELF = 446 # landlock_restrict_self + NET_TCP = 0x3 # BIND_TCP | CONNECT_TCP + + libc = ctypes.CDLL('libc.so.6') + + abi = libc.syscall(NR_CREATE, 0, 0, 1) + if abi < 4: + return False +lib/bb/utils.py def disable_network(uid=None, gid=None): · line 2080
Quoted 10 lines
lib/bb/utils.py +10
-0+ attr = struct.pack("QQ", 0, NET_TCP) + buf = ctypes.create_string_buffer(attr) + fd = libc.syscall(NR_CREATE, buf, len(attr), 0) + if fd < 0: + return False + + libc.prctl(38, 1, 0, 0, 0) # PR_SET_NO_NEW_PRIVS + r = libc.syscall(NR_SELF, fd, 0) + os.close(fd) + return r == 0Far too many magic numbers. I would really want to do this with an API.
Quoted 2 lines
This also needs some kind of test, e.g. that the function indeed has the desired effect.
Unfortunately, to use tech like this, we do end up needing to do something like that and utils.py already has quite a bit of it. The plus side is that the kernel is really good about maintaining these APIs so the numbers are unlikely to change.
I wouldn't take something like this unless there was a really good case for using it. Network isolation in more builds probably is a strong enough use case...
Cheers,
Richard
Re: [bitbake-devel] [PATCH [RFC] 1/2] utils: Add landlock_restrict_network function
In reply to David NyströmQuoted 8 lines · David Nyström
Add
landlock_restrict_network()which blocks TCP bind/connect using Landlock LSM (ABI v4+, kernel 6.7+). Designed to stack with the existingdisable_network()namespace isolation, covering the case wheredisable_network()is skipped for non-local UIDs.Gracefully returns False on older kernels (ABI < 4).
Signed-off-by David Nyström <[email protected]>
Hi David,
I think adding this is a good idea, but the code needs a few changes to ensure it is maintainable.
Quoted 9 lines
--- lib/bb/utils.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)lib/bb/utils.py +0
-0
lib/bb/utils.py def disable_network(uid=None, gid=None): · line 2063
Quoted 10 lines
lib/bb/utils.py +7
-0@@ -2054,6 +2054,32 @@ def disable_network(uid=None, gid=None): with open("/proc/self/gid_map", "w") as f: f.write("%s %s 1" % (gid, gid)) +def landlock_restrict_network(): + """Block TCP bind/connect using Landlock LSM (ABI v4+, kernel 6.7+). + Gracefully skipped on older kernels. Stacks with disable_network().""" + + NR_CREATE = 444 # landlock_create_ruleset + NR_SELF = 446 # landlock_restrict_self + NET_TCP = 0x3 # BIND_TCP | CONNECT_TCP
We should base these on the names used in the Linux kernel so it's easy to search for things and compare with example C code in the docs. So,
NR_landlock_create_ruleset = 444
NR_landlock_add_rule = 445
LANDLOCK_ACCESS_NET_BIND_TCP = 0x1
LANDLOCK_ACCESS_NET_CONNECT_TCP = 0x2
LANDLOCK_CREATE_RULESET_VERSION = 1lib/bb/utils.py def disable_network(uid=None, gid=None): · line 2069
Quoted 6 lines
lib/bb/utils.py +6
-0+ + libc = ctypes.CDLL('libc.so.6') + + abi = libc.syscall(NR_CREATE, 0, 0, 1) + if abi < 4: + return False
# Check that landlock is enabled and supports network access # restriction (added in ABI version 4)
abi = libc.syscall(NR_landlock_create_ruleset,
0, 0,
LANDLOCK_CREATE_RULESET_VERSION)
if abi < 4:
logger.debug("System doesn't support disabling network via landlock") return False
That's a little more verbose, but much clearer.
lib/bb/utils.py def disable_network(uid=None, gid=None): · line 2075
Quoted 6 lines
lib/bb/utils.py +6
-0+ + attr = struct.pack("QQ", 0, NET_TCP) + buf = ctypes.create_string_buffer(attr) + fd = libc.syscall(NR_CREATE, buf, len(attr), 0) + if fd < 0: + return False
We probably also want a logger.debug() call to log the failure here as well.
lib/bb/utils.py def disable_network(uid=None, gid=None): · line 2077
Quoted 2 lines
lib/bb/utils.py +2
-0+ + libc.prctl(38, 1, 0, 0, 0) # PR_SET_NO_NEW_PRIVS
The commit message only describes use of landlock, not no_new_privs. We need constants for this call as well.
lib/bb/utils.py def disable_network(uid=None, gid=None): · line 2085
Quoted 8 lines
lib/bb/utils.py +5
-0+ r = libc.syscall(NR_SELF, fd, 0) + os.close(fd) + return r == 0 + + def export_proxies(d): from bb.fetch2 import get_fetcher_environment """ export common proxies variables from datastore to environment """
Thanks,
Signature · 1 line
Paul Barker
Re: [bitbake-devel] [PATCH [RFC] 1/2] utils: Add landlock_restrict_network function
Quoted 6 lines · Paul Barker
Quoted 5 lines · David Nyström
Add
landlock_restrict_network()which blocks TCP bind/connect using Landlock LSM (ABI v4+, kernel 6.7+). Designed to stack with the existingdisable_network()namespace isolation, covering the case wheredisable_network()is skipped for non-local UIDs.
Quoted 8 lines
Quoted 3 lines
Gracefully returns False on older kernels (ABI < 4).
Signed-off-by David Nyström <[email protected]>Hi David,
I think adding this is a good idea, but the code needs a few changes to ensure it is maintainable.
Thanks for the review, and good comments.
My comments below.
Quoted 23 lines
Quoted 9 lines
--- lib/bb/utils.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)lib/bb/utils.py +0
-0lib/bb/utils.py def disable_network(uid=None, gid=None): · line 2063
Quoted 10 lines
lib/bb/utils.py +7
-0@@ -2054,6 +2054,32 @@ def disable_network(uid=None, gid=None): with open("/proc/self/gid_map", "w") as f: f.write("%s %s 1" % (gid, gid)) +def landlock_restrict_network(): + """Block TCP bind/connect using Landlock LSM (ABI v4+, kernel 6.7+). + Gracefully skipped on older kernels. Stacks with disable_network().""" + + NR_CREATE = 444 # landlock_create_ruleset + NR_SELF = 446 # landlock_restrict_self + NET_TCP = 0x3 # BIND_TCP | CONNECT_TCPWe should base these on the names used in the Linux kernel so it's easy to search for things and compare with example C code in the docs. So,
Quoted 7 lines
NR_landlock_create_ruleset = 444 NR_landlock_add_rule = 445 LANDLOCK_ACCESS_NET_BIND_TCP = 0x1 LANDLOCK_ACCESS_NET_CONNECT_TCP = 0x2 LANDLOCK_CREATE_RULESET_VERSION = 1
+1.
Quoted 16 lines
lib/bb/utils.py def disable_network(uid=None, gid=None): · line 2069
Quoted 6 lines
lib/bb/utils.py +6
-0+ + libc = ctypes.CDLL('libc.so.6') + + abi = libc.syscall(NR_CREATE, 0, 0, 1) + if abi < 4: + return False# Check that landlock is enabled and supports network access
# restriction (added in ABI version 4) abi = libc.syscall(NR_landlock_create_ruleset, 0, 0, LANDLOCK_CREATE_RULESET_VERSION) if abi < 4: logger.debug("System doesn't support disabling network via landlock") return False
Quoted 1 line
That's a litte more verbose, but much clearer.
Good point.
Quoted 7 lines
lib/bb/utils.py def disable_network(uid=None, gid=None): · line 2075
Quoted 6 lines
lib/bb/utils.py +6
-0+ + attr = struct.pack("QQ", 0, NET_TCP) + buf = ctypes.create_string_buffer(attr) + fd = libc.syscall(NR_CREATE, buf, len(attr), 0) + if fd < 0: + return False
Quoted 8 lines
We probably also want a
logger.debug()call to log the failure here as well.lib/bb/utils.py def disable_network(uid=None, gid=None): · line 2077
Quoted 2 lines
lib/bb/utils.py +2
-0+ + libc.prctl(38, 1, 0, 0, 0) # PR_SET_NO_NEW_PRIVSThe commit message only describes use of landlock, not
no_new_privs. We need constants for this call as well.
Also a very good point, setuid/gid and setcap:ed binaries will silently run without privs, which should be documented also in the commit message.
This is a landlock requirement for unpriv use, and the main source of potential sideeffects I would expect from this approach.
Since there is no way do disable it, without setting the "network" flag on a failing task, I'd really want to gain more confidence about the potential sideeffects of no_new_privs on various edge cases before I send a non-RFC patch. Testing is ongoing with your comments addressed, but I will not be able to finish before my vacation starts(tomorrow). If considered urgent,
feel free to use idea only.
Quoted 16 lines
lib/bb/utils.py def disable_network(uid=None, gid=None): · line 2085
Quoted 8 lines
lib/bb/utils.py +5
-0+ r = libc.syscall(NR_SELF, fd, 0) + os.close(fd) + return r == 0 + + def export_proxies(d): from bb.fetch2 import get_fetcher_environment """ export common proxies variables from datastore to environment """Thanks,
Signature · 1 line
Paul Barker
RFC PATCH 1/1 2/2] bitbake-worker: Call landlock_restrict_network for tasks without network
In reply to David NyströmCall bb.utils.landlock_restrict_network() for tasks without the 'network' varflag. This to support basic network restrictions in unprivileged docker containers.
--- bin/bitbake-worker | 2 ++ 1 file changed, 2 insertions(+)
bin/bitbake-worker +2 -0
@@ -287,6 +287,8 @@ def fork_off_task(cfg, data, databuilder, workerdata, extraconfigdata, runtask): bb.utils.disable_network(uid, gid) else: logger.debug("Skipping disable network for %s since %s is not a local uid." % (taskname, uid)) + if not bb.utils.landlock_restrict_network(): + logger.debug("Skipping Landlock network restriction for %s since kernel lacks ABI v4+ support." % taskname) # exported_vars() returns a generator which *cannot* be passed to os.environ.update() # successfully. We also need to unset anything from the environment which shouldn't be there
Signature · 1 line
2.53.0
Re: [bitbake-devel] [PATCH [RFC] 2/2] bitbake-worker: Call landlock_restrict_network for tasks without network
Quoted 14 lines · David Nyström
Call
bb.utils.landlock_restrict_network()for tasks without the 'network' varflag. This to support basic network restrictions in unprivileged docker containers.Signed-off-by David Nyström <[email protected]>--- bin/bitbake-worker | 2 ++ 1 file changed, 2 insertions(+)bin/bitbake-worker +0
-0
bin/bitbake-worker def fork_off_task(cfg, data, databuilder, workerdata, extraconfigdata, runtask): · line 291
Quoted 5 lines
bin/bitbake-worker +2
-0@@ -287,6 +287,8 @@ def fork_off_task(cfg, data, databuilder, workerdata, extraconfigdata, runtask): bb.utils.disable_network(uid, gid) else: logger.debug("Skipping disable network for %s since %s is not a local uid." % (taskname, uid)) + if not bb.utils.landlock_restrict_network(): + logger.debug("Skipping Landlock network restriction for %s since kernel lacks ABI v4+ support." % taskname)
In disable_network, the logger.debug() calls for failure are handled within the function instead of by the caller. We should do the same for landlock_restrict_network() as suggested in my reply to patch 1/2 and drop the debug print from here.
bin/bitbake-worker def fork_off_task(cfg, data, databuilder, workerdata, extraconfigdata, runtask): · line 294
Quoted 3 lines
bin/bitbake-worker +0
-0# exported_vars() returns a generator which *cannot* be passed to os.environ.update() # successfully. We also need to unset anything from the environment which shouldn't be there
Best regards,
Signature · 1 line
Paul Barker
Hi David,
Quoted 24 lines · David Nyström via lists.openembedded.org
When current implementation runs in an unprivileged docker container, basic networking will be allowed by default in all steps, ignoring the network varflags intention.
Introduce support for landlock blocking of bind and connect, providing basic support for blocking TCP.
UDP is corrently beeing worked on upstream, but not yet supported.Landlock requires
PR_SET_NO_NEW_PRIVSto prevent escape, which is also attempted, this prevents privilege escalation from child. devshell and related are already tagged with network varflag and can sudo at will.syscall ABI is asm-generic, 5.13+ for all archs except alpha. On alpha, we leak 2 fd:s in the childs context before graceful exit, which is cleaned up at child termination.
Don't have an alpha target, so this is not tested, evaluated via static analysis only.Landlock also provides a future possibility for filesystem limitations with the purpose of catching bugs, and preventing persistance of supply chain releated payloads. writes to f.ex. .bashrc and friends.
Signed-off-by David Nyström <[email protected]>
Quoted 8 lines
---
David Nyström (2):
utils: Addlandlock_restrict_networkfunction
bitbake-worker: Calllandlock_restrict_networkfor tasks without networkbin/bitbake-worker | 2 ++
lib/bb/utils.py | 26 ++++++++++++++++++++++++++2 files changed, 28 insertions(+)
I think this has generally has positive comments, we just need to take Paul's review comments into account about the magic numbers. Would you be able to send a version with those tweaks?
Thanks,
Richard
Quoted 32 lines · Richard Purdie
Hi David,
Quoted 29 lines · David Nyström via lists.openembedded.org
When current implementation runs in an unprivileged docker container, basic networking will be allowed by default in all steps, ignoring the network varflags intention.
Introduce support for landlock blocking of bind and connect, providing basic support for blocking TCP.
UDP is corrently beeing worked on upstream, but not yet supported.Landlock requires
PR_SET_NO_NEW_PRIVSto prevent escape, which is also attempted, this prevents privilege escalation from child. devshell and related are already tagged with network varflag and can sudo at will.syscall ABI is asm-generic, 5.13+ for all archs except alpha. On alpha, we leak 2 fd:s in the childs context before graceful exit, which is cleaned up at child termination.
Don't have an alpha target, so this is not tested, evaluated via static analysis only.Landlock also provides a future possibility for filesystem limitations with the purpose of catching bugs, and preventing persistance of supply chain releated payloads. writes to f.ex. .bashrc and friends.
Signed-off-by David Nyström <[email protected]>---
David Nyström (2):
utils: Addlandlock_restrict_networkfunction
bitbake-worker: Calllandlock_restrict_networkfor tasks without network
Quoted 7 lines
Quoted 3 lines
bin/bitbake-worker | 2 ++
lib/bb/utils.py | 26 ++++++++++++++++++++++++++2 files changed, 28 insertions(+)I think this has generally has positive comments, we just need to take Paul's review comments into account about the magic numbers. Would you be able to send a version with those tweaks?
Yes, thanks for the review, and sorry for the late reply. Will reroll with comments.
Quoted 4 lines
Thanks,
Richard
Hi David,
Quoted 35 lines · David Nyström
Quoted 34 lines · Richard Purdie
Quoted 32 lines · David Nyström via lists.openembedded.org
When current implementation runs in an unprivileged docker container, basic networking will be allowed by default in all steps, ignoring the network varflags intention.
Introduce support for landlock blocking of bind and connect, providing basic support for blocking TCP.
UDP is corrently beeing worked on upstream, but not yet supported.Landlock requires
PR_SET_NO_NEW_PRIVSto prevent escape, which is also attempted, this prevents privilege escalation from child. devshell and related are already tagged with network varflag and can sudo at will.syscall ABI is asm-generic, 5.13+ for all archs except alpha. On alpha, we leak 2 fd:s in the childs context before graceful exit, which is cleaned up at child termination.
Don't have an alpha target, so this is not tested, evaluated via static analysis only.Landlock also provides a future possibility for filesystem limitations with the purpose of catching bugs, and preventing persistance of supply chain releated payloads. writes to f.ex. .bashrc and friends.
Signed-off-by David Nyström <[email protected]>---
David Nyström (2):
utils: Addlandlock_restrict_networkfunction
bitbake-worker: Calllandlock_restrict_networkfor tasks without networkbin/bitbake-worker | 2 ++
lib/bb/utils.py | 26 ++++++++++++++++++++++++++2 files changed, 28 insertions(+)
Quoted 6 lines
Quoted 3 lines
I think this has generally has positive comments, we just need to take Paul's review comments into account about the magic numbers. Would you be able to send a version with those tweaks?
Yes, thanks for the review, and sorry for the late reply. Will reroll with comments.
These still are in the queue and desired, do you think you'll get a chance to look at them?
Cheers,
Richard
Quoted 42 lines · Richard Purdie
Hi David,
Quoted 39 lines · David Nyström
Quoted 37 lines · Richard Purdie
Quoted 24 lines · David Nyström via lists.openembedded.org
When current implementation runs in an unprivileged docker container, basic networking will be allowed by default in all steps, ignoring the network varflags intention.
Introduce support for landlock blocking of bind and connect, providing basic support for blocking TCP.
UDP is corrently beeing worked on upstream, but not yet supported.Landlock requires
PR_SET_NO_NEW_PRIVSto prevent escape, which is also attempted, this prevents privilege escalation from child. devshell and related are already tagged with network varflag and can sudo at will.syscall ABI is asm-generic, 5.13+ for all archs except alpha. On alpha, we leak 2 fd:s in the childs context before graceful exit, which is cleaned up at child termination.
Don't have an alpha target, so this is not tested, evaluated via static analysis only.Landlock also provides a future possibility for filesystem limitations with the purpose of catching bugs, and preventing persistance of supply chain releated payloads. writes to f.ex. .bashrc and friends.
Signed-off-by David Nyström <[email protected]>Quoted 8 lines
---
David Nyström (2):
utils: Addlandlock_restrict_networkfunction
bitbake-worker: Calllandlock_restrict_networkfor tasks without networkbin/bitbake-worker | 2 ++
lib/bb/utils.py | 26 ++++++++++++++++++++++++++2 files changed, 28 insertions(+)I think this has generally has positive comments, we just need to take Paul's review comments into account about the magic numbers. Would you be able to send a version with those tweaks?
Quoted 5 lines
Quoted 2 lines
Yes, thanks for the review, and sorry for the late reply. Will reroll with comments.
These still are in the queue and desired, do you think you'll get a chance to look at them?
I did a v2, but have been hesitating to send a non-RFC, since the test-scope for this is bit overwhelming.
This due to the change of default task behaviour in regards
to no_new_privs. I'm not too worried at about
LSMs running beeing parent to bitbake, but there will likely be unintended fallout for sublayer tasks, where I've managed to test only a tiny subset of them.
Problematic tasks can of course be flagged with "network", to avoid no_new_privs, but that seems a bit wrong, but I failed to find a better solution. f.ex. The only reason the tap setup still works for do_testimage is due to its "network" tag.
Adding Bruce to CC, since I found some fusermount usage in vrunner(meta-virtualization) when probing through sub-layers, which usually is setcap:ed. There are likely other cases downstream.
Whats scope was tested during its time in master-next ?
Quoted 4 lines
Cheers,
Richard