All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] utils: Add disable_network function
@ 2022-01-07 23:15 Richard Purdie
  2022-01-07 23:15 ` [PATCH 2/2] bitbake-worker: Add/support network task flag Richard Purdie
  0 siblings, 1 reply; 7+ messages in thread
From: Richard Purdie @ 2022-01-07 23:15 UTC (permalink / raw)
  To: bitbake-devel

Add a function which uses the unshare glibc call to disable networking
in the current process. This doesn't work on older distros/kernels
but will on more recent ones so for now we simply ignore the cases we
can't execute on. uid/gid can be passed in externally so this can
work with pseudo/fakeroot contexts.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 lib/bb/utils.py | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/lib/bb/utils.py b/lib/bb/utils.py
index 1a51589704..0312231933 100644
--- a/lib/bb/utils.py
+++ b/lib/bb/utils.py
@@ -27,6 +27,7 @@ import errno
 import signal
 import collections
 import copy
+import ctypes
 from subprocess import getstatusoutput
 from contextlib import contextmanager
 from ctypes import cdll
@@ -1595,6 +1596,36 @@ def set_process_name(name):
     except:
         pass
 
+def disable_network(uid=None, gid=None):
+    """
+    Disable networking in the current process if the kernel supports it, else
+    just return after logging to debug. To do this we need to create a new user
+    namespace, then map back to the original uid/gid.
+    """
+    libc = ctypes.CDLL('libc.so.6')
+
+    # From sched.h
+    # New user namespace
+    CLONE_NEWUSER = 0x10000000
+    # New network namespace
+    CLONE_NEWNET = 0x40000000
+
+    if uid is None:
+        uid = os.getuid()
+    if gid is None:
+        gid = os.getgid()
+
+    ret = libc.unshare(CLONE_NEWNET | CLONE_NEWUSER)
+    if ret != 0:
+        logger.debug("System doesn't suport disabling network without admin privs")
+        return
+    with open("/proc/self/uid_map", "w") as f:
+        f.write("%s %s 1" % (uid, uid))
+    with open("/proc/self/setgroups", "w") as f:
+        f.write("deny")
+    with open("/proc/self/gid_map", "w") as f:
+        f.write("%s %s 1" % (gid, gid))
+
 def export_proxies(d):
     """ export common proxies variables from datastore to environment """
     import os
-- 
2.32.0



^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-01-20  6:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-07 23:15 [PATCH 1/2] utils: Add disable_network function Richard Purdie
2022-01-07 23:15 ` [PATCH 2/2] bitbake-worker: Add/support network task flag Richard Purdie
2022-01-19 11:49   ` [bitbake-devel] " Robert Yang
2022-01-19 12:11     ` Richard Purdie
2022-01-19 23:16   ` Jose Quaresma
     [not found]     ` <34c5a34a48c54376f5a181a0c836ddc2be1c1735.camel@linuxfoundation.org>
2022-01-20  0:18       ` Jose Quaresma
2022-01-20  6:41       ` Robert Yang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.