Skip to contentSkip to conversation

utils: Add disable_network function

PATCH In review Richard Purdie 4 participants 7 messages Dec 22, 2021 – Mar 13, 2023 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 utils: Add disable_network function

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.

---
 lib/bb/utils.py | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
lib/bb/utils.py +31 -0
index d890ea832e..8006f7bd2d 100644
@@ -26,6 +26,7 @@ import errno
 import signal
 import collections
 import copy
+import ctypes
 from subprocess import getstatusoutput
 from contextlib import contextmanager
 from ctypes import cdll
@@ -1594,6 +1595,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
Signature · 1 line
2.32.0
#1 lore raw

PATCH 2/2 bitbake-worker: Respect nonetwork task flag

Add a "nonetwork" task specific flag which then triggers networking to be disabled for this task.

This needs to happen before we enter the fakeroot environment of the task due to the need for the real uid/gid which we save in the parent process.

---
 bin/bitbake-worker | 8 ++++++++
 1 file changed, 8 insertions(+)
bin/bitbake-worker +8 -0
index bf96207edc..32d4f58655 100755
@@ -152,6 +152,10 @@ def fork_off_task(cfg, data, databuilder, workerdata, fn, task, taskname, taskha
     fakeenv = {}
     umask = None
 
+    uid = os.getuid()
+    gid = os.getgid()
+
+
     taskdep = workerdata["taskdeps"][fn]
     if 'umask' in taskdep and taskname in taskdep['umask']:
         umask = taskdep['umask'][taskname]
@@ -257,6 +261,10 @@ def fork_off_task(cfg, data, databuilder, workerdata, fn, task, taskname, taskha
 
                 bb.utils.set_process_name("%s:%s" % (the_data.getVar("PN"), taskname.replace("do_", "")))
 
+                if the_data.getVarFlag(taskname, 'nonetwork', False):
+                    logger.debug("Attempting to disable network")
+                    bb.utils.disable_network(uid, gid)
+
                 # 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 
                 exports = bb.data.exported_vars(the_data)
Signature · 1 line
2.32.0
#2 lore raw

Re: [bitbake-devel] [PATCH 2/2] bitbake-worker: Respect nonetwork task flag

Hi,

Quoted 6 lines · Richard Purdie

Add a "nonetwork" task specific flag which then triggers networking to be disabled for this task.

This needs to happen before we enter the fakeroot environment of the task due to the need for the real uid/gid which we save in the parent process.

I'd like to see some docs about this in Bitbake in the variable flags section https://docs.yoctoproject.org/bitbake/bitbake-user-manual/bitbake-user-manual-metadata.html#variable-flags so it's not a hidden feature :)

Just writing this here so we don't forget.

Cheers,
Quentin

Quoted 31 lines

---
bin/bitbake-worker | 8 ++++++++

1 file changed, 8 insertions(+)
bin/bitbake-worker +8 -0
index bf96207edc..32d4f58655 100755
@@ -152,6 +152,10 @@ def fork_off_task(cfg, data, databuilder, workerdata, fn, task, taskname, taskha
    fakeenv = {}
    umask = None
 
+    uid = os.getuid()
+    gid = os.getgid()
+
+
    taskdep = workerdata["taskdeps"][fn]
    if 'umask' in taskdep and taskname in taskdep['umask']:
        umask = taskdep['umask'][taskname]
@@ -257,6 +261,10 @@ def fork_off_task(cfg, data, databuilder, workerdata, fn, task, taskname, taskha
 
                bb.utils.set_process_name("%s:%s" % (the_data.getVar("PN"), taskname.replace("do_", "")))
 
+                if the_data.getVarFlag(taskname, 'nonetwork', False):
+                    logger.debug("Attempting to disable network")
+                    bb.utils.disable_network(uid, gid)
+
                # 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 
                exports = bb.data.exported_vars(the_data)
#3 lore raw
In reply to Richard Purdie

Hi Richard,

First of all, this feature is fantastic.

Richard Purdie <[email protected]> escreveu no dia quarta, 22/12/2021 à(s) 23:19:

Quoted 51 lines

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.

---
 lib/bb/utils.py | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
lib/bb/utils.py +4 -0
index d890ea832e..8006f7bd2d 100644
@@ -26,6 +26,7 @@ import errno
 import signal
 import collections
 import copy
+import ctypes
 from subprocess import getstatusoutput
 from contextlib import contextmanager
 from ctypes import cdll
@@ -1594,6 +1595,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

lib/bb/utils.py +18 -0
+    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
Quoted 3 lines

admin privs")
+ return

try:

lib/bb/utils.py def set_process_name(name): · line 1626

Quoted 7 lines
lib/bb/utils.py +6 -0
+    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))

except:
print a warning about the fail

Without that it can fail silently if the user doesn't have permission to write in /proc/self.
In general, if it runs all the steps, the function can return true/false.

Jose

+

Quoted 18 lines

def export_proxies(d):
""" export common proxies variables from datastore to environment """ import os
--
2.32.0

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group. View/Reply Online (#13199):
https://lists.openembedded.org/g/bitbake-devel/message/13199
Mute This Topic: https://lists.openembedded.org/mt/87909296/5052612
Group Owner: [email protected]
Unsubscribe: https://lists.openembedded.org/g/bitbake-devel/unsub [ [email protected]]
-=-=-=-=-=-=-=-=-=-=-=-

Signature · 3 lines
Best regards,

José Quaresma

A message that is not in the archive (sent off-list or not yet received): 3a672c37d4cd375fed43b59d887c9dcd17c60ee8.camel@linuxfoundation.org

#5 lore raw

Hi Richard,

I think that the usage of the unshare system call can replace the use of pseudo on bitbake.
If we map the new user namespace to the superuser we can do what is bean done nowadays
inside the pseudo or not ?

*-r*, *--map-root-user*
Run the program only after the current effective user and group IDs have been mapped to the superuser UID and GID in the newly created user namespace. This makes it possible to conveniently gain capabilities needed to manage various aspects of the newly created namespaces (such as configuring interfaces in the network namespace or mounting filesystems in the mount namespace) even when run unprivileged. As a mere convenience feature, it does not support more sophisticated use cases, such as mapping multiple ranges of UIDs and GIDs. This option implies *--setgroups=deny *and *--user*. This option is equivalent to *--map-user=0 --map-group=0*.

https://man7.org/linux/man-pages/man1/unshare.1.html

Best regards,
Jose

Richard Purdie <[email protected]> escreveu no dia sexta, 24/12/2021 à(s) 15:57:

Quoted 45 lines · Jose Quaresma
Quoted 3 lines

Hi Richard,

First of all, this feature is fantastic.

I'm certainly happy we can now do this, at least for some systems (and the situation should improve in the future),

Quoted 2 lines

Richard Purdie <[email protected]> escreveu no dia

quarta,

Quoted 15 lines

22/12/2021 à(s) 23:19:

Quoted 1 line

try:

lib/bb/utils.py def set_process_name(name): · line 1626

Quoted 7 lines
lib/bb/utils.py +6 -0
+    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))
Quoted 4 lines

except:
print a warning about the fail

Without that it can fail silently if the user doesn't have permission to

write

Quoted 2 lines

in /proc/self.
In general, if it runs all the steps, the function can return true/false.

The trouble is there are a set of older distros where this definitely won't work
and we can't really afford to print warnings on those. Not sure what the best
way forward is for that...

Cheers,

Richard

Signature · 3 lines
Best regards,

José Quaresma

A message that is not in the archive (sent off-list or not yet received): 1699ebd54f3414641b7ff78e1cec2bc546b658cf.camel@linuxfoundation.org

#7 lore raw

Richard Purdie <[email protected]> escreveu no dia quarta, 2/02/2022 à(s) 13:43:

Quoted 28 lines · Jose Quaresma
Quoted 5 lines

Hi Richard,

I think that the usage of the unshare system call can replace the use of pseudo on bitbake.
If we map the new user namespace to the superuser we can do what is

bean done

Quoted 7 lines

nowadays
inside the pseudo or not ?
-r, --map-root-user
Run the program only after the current effective user and group IDs have been mapped to the superuser UID and GID in the newly created user namespace. This makes it possible to conveniently gain capabilities needed to manage various

Quoted 9 lines

aspects of the newly created namespaces (such as configuring interfaces in the network namespace or mounting filesystems in the mount namespace) even when run unprivileged. As a mere convenience feature, it does not support more sophisticated use cases, such as mapping multiple ranges of UIDs and GIDs. This option implies --setgroups=deny and --user. This option is equivalent to --map-user=0 --map-group=0.
https://man7.org/linux/man-pages/man1/unshare.1.html

I wondered about that and I would love for it to be true however it isn't that
simple.

Quoted 8 lines

With pseudo we emulate multiple different users and a separate passwd/group file
with those users in it. The unshare call would map the current user to root but
it doesn't give us other user IDs or groups to chown/chgrp files to, at least as
I understand it.

It was also sounding too simple to be true.
I will try to better understand this part of the chown/chgrp usage on pseudo.
Thank you for the clarification.

Quoted 6 lines

Cheers,

Richard

Signature · 3 lines
Best regards,

José Quaresma
#8 lore raw
In reply to Richard Purdie

Hi Richard,

This might be a dummy question, but I'm wondering which kernels/distros could support it. I tried the codes on poky target with linux-yocto 6.1.14, and it failed for a non-root user.

This manual, https://man7.org/linux/man-pages/man2/unshare.2.html, tells me that CLONE_NEWNET requires CAP_SYS_ADMIN. And looking at the netns_install function in net/core/net_namespace.c, it does seem to be checking CAP_SYS_ADMIN.

Regards,
Qi

Quoted 69 lines · Richard Purdie

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.

---
  lib/bb/utils.py | 31 +++++++++++++++++++++++++++++++
  1 file changed, 31 insertions(+)
lib/bb/utils.py +31 -1
index d890ea832e..8006f7bd2d 100644
@@ -26,6 +26,7 @@ import errno
  import signal
  import collections
  import copy
+import ctypes
  from subprocess import getstatusoutput
  from contextlib import contextmanager
  from ctypes import cdll
@@ -1594,6 +1595,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
 
-=-=-=-=-=-=-=-=-=-=-=-

Links: You receive all messages sent to this group.
View/Reply Online (#13199): https://lists.openembedded.org/g/bitbake-devel/message/13199
Mute This Topic: https://lists.openembedded.org/mt/87909296/3618072
Group Owner: [email protected]
Unsubscribe: https://lists.openembedded.org/g/bitbake-devel/unsub [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-