Skip to contentSkip to conversation

Make qemu-bridge-helper work in macOS and FreeBSD

PATCH In review Nikhil Balachandra 2 participants 4 messages Apr 6, 2018 – Apr 20, 2018 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Make qemu-bridge-helper work in macOS and FreeBSD

Add macOS and FreeBSD support to the qemu-bridge-helper.

Differences when compared to linux version.
1) Does no drop privileges at the start of the process and run as root throughout the lifetime of the process there by increasing the risk incase of security vulnerability.
2) Does not support --use-vnet flag.

---
 Makefile             |   8 +++-
 Makefile.objs        |   5 ++
 configure            |   5 ++
 qemu-bridge-helper.c | 131 +++++++++++++++++++++++++++++++++------------------
 4 files changed, 102 insertions(+), 47 deletions(-)

This patch makes qemu-bridge-helper work in macOS and FreeBSD platforms. The patch also contains changes in configure script and Makefile for building qemu-bridge-helper in these platforms.

After applying the patch, running $ make or $ make qemu-bridge-helper should build the binary (see macOS caveat below).

While the compilation process is straight-forward in FreeBSD, there is one caveat while building for the macOS. Unlike FreeBSD, macOS does not ship with net/if_bridgevar.h header file. This Header file however could be obtained from the Darwin/XNU repository[1] and can be copied somewhere in the include path before building.

Eventhough macOS does not ship with the if_bridgevar.h header file[2], I expect the API to remain stable as this header file is similar to what is found in other BSDs. If this patch is decided to be included in the qemu, can experienced qemu developers please tell me how to go about having this header file in the include path such that it does not require manually downloading and copying the file[3]?

This is also my first patch to qemu and opensource C codebase project. Please be critical while reviewing the code.

[1] - https://github.com/apple/darwin-xnu/blob/master/bsd/net/if_bridgevar.h
[2] - Adding to the complexity, this header file is also marked as private/internal.
[3] - Couple of ideas: This could be automatically downloaded during the build or can be directly copied into the qemu source tree if license permits such a usage.

Thanks and Regards,
Nikhil

Makefile +6 -2
index 727ef118f3..c581d91c6b 100644
@@ -347,7 +347,10 @@ $(call set-vpath, $(SRC_PATH))
 
 LIBS+=-lz $(LIBS_TOOLS)
 
+# Build Helpers (currently only qemu-bridge-helper) for Linux, FreeBSD and macOS.
 HELPERS-$(CONFIG_LINUX) = qemu-bridge-helper$(EXESUF)
+HELPERS-$(CONFIG_FREEBSD) = qemu-bridge-helper$(EXESUF)
+HELPERS-$(CONFIG_DARWIN) = qemu-bridge-helper$(EXESUF)
 
 ifdef BUILD_DOCS
 DOCS=qemu-doc.html qemu-doc.txt qemu.1 qemu-img.1 qemu-nbd.8 qemu-ga.8
@@ -429,7 +432,8 @@ dummy := $(call unnest-vars,, \
                 ui-obj-m \
                 audio-obj-y \
                 audio-obj-m \
-                trace-obj-y)
+                trace-obj-y \
+                tap-obj-y)
 
 include $(SRC_PATH)/tests/Makefile.include
 
@@ -535,7 +539,7 @@ qemu-img$(EXESUF): qemu-img.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-o
 qemu-nbd$(EXESUF): qemu-nbd.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
 qemu-io$(EXESUF): qemu-io.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
 
-qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o $(COMMON_LDADDS)
+qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o $(tap-obj-y) $(COMMON_LDADDS)
 
 qemu-keymap$(EXESUF): qemu-keymap.o ui/input-keymap.o $(COMMON_LDADDS)
 
Makefile.objs +5 -0
index c6c9b8fc21..1f9ced33bf 100644
@@ -86,6 +86,11 @@ qom-obj-y = qom/
 
 io-obj-y = io/
 
+#######################################################################
+# tap-obj-y is code used by qemu-bridge-helper
+
+tap-obj-y = net/
+
 ######################################################################
 # Target independent part of system emulation. The long term path is to
 # suppress *all* target specific code in case of system emulation, i.e. a
configure +5 -0
index a2301dd0dc..34b6c398c7 100755
@@ -728,6 +728,7 @@ GNU/kFreeBSD)
 ;;
 FreeBSD)
   bsd="yes"
+  freebsd="yes"
   make="${MAKE-gmake}"
   audio_drv_list="oss"
   audio_possible_drivers="oss sdl pa"
@@ -5985,6 +5986,10 @@ if test "$linux" = "yes" ; then
   echo "CONFIG_LINUX=y" >> $config_host_mak
 fi
 
+if test "$freebsd" = "yes" ; then
+  echo "CONFIG_FREEBSD=y" >> $config_host_mak
+fi
+
 if test "$darwin" = "yes" ; then
   echo "CONFIG_DARWIN=y" >> $config_host_mak
 fi
qemu-bridge-helper.c +86 -45

194 lines not on this page · raw message

Signature · 1 line
2.16.3
#1 lore raw
Quoted 6 lines · Nikhil Balachandra

Eventhough macOS does not ship with the if_bridgevar.h header file[2], I expect the API to remain stable as this header file is similar to what is found in other BSDs. If this patch is decided to be included in the qemu, can experienced qemu developers please tell me how to go about having this header file in the include path such that it does not require manually downloading and copying the file[3]?

QEMU ships Linux headers. They are synced using this script:
scripts/update-linux-headers.sh

If the macOS header is appropriately licensed, it could be kept under include/standard-headers/ alongside the other third-party headers that QEMU ships.

qemu-bridge-helper.c int main(int argc, char **argv) · line 380

Quoted 9 lines
qemu-bridge-helper.c +3 -1
@@ -310,30 +374,18 @@ int main(int argc, char **argv)
         goto cleanup;
     }
 
+
     /* open the tap device */
-    fd = open("/dev/net/tun", O_RDWR);
+    memset(&iface, '\0', sizeof(char) * IFNAMSIZ);
+    int vnet_supported = has_vnet_hdr(fd);

fd is always -1 here, so this patch breaks vnet hdr?

qemu-bridge-helper.c int main(int argc, char **argv) · line 382

Quoted 2 lines
qemu-bridge-helper.c +2 -0
+    Error *err = NULL;
+    fd = tap_open(&iface[0], sizeof(iface), &vnet_supported, use_vnet, 0, &err);

tap_open() was not written with setuid programs in mind. I think this is a case where code duplication is justified.

It's safer to have the minimal code to open the tap device rather than calling into QEMU code which may not realize it is running setuid.

#2 lore raw

Hi Stefan,

Thanks for the review. I'll be sending v2 version of this patch in next 3-4 days with the following changes.

1) New scripts/update-xnu-headers.sh script to import if_bridgevar.h[4] into include/standard-headers/xnu directory.
@qemu-devel - Please let me know if there is a better approach.
2) Fix broken vnet hdr in this patch.
3) Having minimal code in qemu-bridge-helper.c to open tap device instead of using tap_open().

[4] - macOS if_bridgevar.h header file has APSL and BSD license.

Quoted 37 lines · Stefan Hajnoczi
Quoted 6 lines · Nikhil Balachandra

Eventhough macOS does not ship with the if_bridgevar.h header file[2], I expect the API to remain stable as this header file is similar to what is found in other BSDs. If this patch is decided to be included in the qemu, can experienced qemu developers please tell me how to go about having this header file in the include path such that it does not require manually downloading and copying the file[3]?

QEMU ships Linux headers. They are synced using this script:
scripts/update-linux-headers.sh

If the macOS header is appropriately licensed, it could be kept under include/standard-headers/ alongside the other third-party headers that QEMU ships.

qemu-bridge-helper.c int main(int argc, char **argv) · line 380

Quoted 9 lines
qemu-bridge-helper.c +3 -1
@@ -310,30 +374,18 @@ int main(int argc, char **argv)
         goto cleanup;
     }
 
+
     /* open the tap device */
-    fd = open("/dev/net/tun", O_RDWR);
+    memset(&iface, '\0', sizeof(char) * IFNAMSIZ);
+    int vnet_supported = has_vnet_hdr(fd);

fd is always -1 here, so this patch breaks vnet hdr?

Review int main(int argc, char **argv) · line 382

Quoted 2 lines
(quoted diff) +2 -0
+    Error *err = NULL;
+    fd = tap_open(&iface[0], sizeof(iface), &vnet_supported, use_vnet,

0, &err);

tap_open() was not written with setuid programs in mind. I think this is a case where code duplication is justified.

It's safer to have the minimal code to open the tap device rather than calling into QEMU code which may not realize it is running setuid.

#3 lore raw
Quoted 5 lines · Nikhil Balachandra

Hi Stefan,

Thanks for the review. I'll be sending v2 version of this patch in next 3-4 days with the following changes.

Quoted 8 lines

1) New scripts/update-xnu-headers.sh script to import if_bridgevar.h[4] into include/standard-headers/xnu directory.
@qemu-devel - Please let me know if there is a better approach.
2) Fix broken vnet hdr in this patch.
3) Having minimal code in qemu-bridge-helper.c to open tap device instead of using tap_open().

[4] - macOS if_bridgevar.h header file has APSL and BSD license.

Great, thank you!

Stefan