Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synth unable to detect arch on OPNsense #87

Closed
kraileth opened this issue Jul 14, 2017 · 29 comments
Closed

Synth unable to detect arch on OPNsense #87

kraileth opened this issue Jul 14, 2017 · 29 comments

Comments

@kraileth
Copy link

OPNsense is a FreeBSD-derived firewall OS just like pfSense. Unlike the latter (which is customized so heavily that it cannot even use the ports tree by default) it tries to keep as close to FreeBSD as possible. They offer only a small number of packages, but ports work and thus Synth can be built and installed.

Configuration of Synth works, too, however it is unable to build anything. Trying to use it results in this:

synth upgrade-system

Querying system about current package installations.
Stand by, comparing installed packages against the ports tree.
Stand by, building pkg(8) first ... Failed!! (Synth must exit)
Unfortunately, the system upgrade failed.

Having a look at the log shows that Synth detects the arch as "BADARCH" which is invalid, of course and leads to Synth failing at the configure phase. However the "uname" command will actually return the correct string:

uname -m

amd64

uname -p

amd64

Any idea what's happening here? Here's the complete log output:

cat /var/log/synth/ports-mgmt___pkg.log

=> Building ports-mgmt/pkg
Started : Friday, 14 JUL 2017 at 05:13:28 UTC
Platform: 11.0-RELEASE-p10 FreeBSD 11.0-RELEASE-p10 #0 81a368c52(stable/17.1): Sat May 6 21:22:35 CEST 2017 root@sensey64:/usr/obj/usr/src/sys/SMP amd64


-- Environment

UNAME_r=11.0-SYNTH
UNAME_m=BADARCH
UNAME_p=BADARCH
UNAME_v=FreeBSD 11.0-SYNTH
UNAME_s=FreeBSD
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
SSL_NO_VERIFY_PEER=1
TERM=dumb
PKG_CACHEDIR=/var/cache/pkg8
PKG_DBDIR=/var/db/pkg8
PORTSDIR=/xports
LANG=C
HOME=/root
USER=root


-- Options


-- CONFIGURE_ENV

XDG_DATA_HOME=/construction/xports/ports-mgmt/pkg/work
XDG_CONFIG_HOME=/construction/xports/ports-mgmt/pkg/work
HOME=/construction/xports/ports-mgmt/pkg/work
TMPDIR="/tmp"
SHELL=/bin/sh
CONFIG_SHELL=/bin/sh
CONFIG_SITE=/xports/Templates/config.site
lt_cv_sys_max_cmd_len=262144


-- CONFIGURE_ARGS

--disable-maintainer-mode
--prefix=/usr/local
${_LATE_CONFIGURE_ARGS}


-- MAKE_ENV

XDG_DATA_HOME=/construction/xports/ports-mgmt/pkg/work
XDG_CONFIG_HOME=/construction/xports/ports-mgmt/pkg/work
HOME=/construction/xports/ports-mgmt/pkg/work
TMPDIR="/tmp"
NO_PIE=yes
MK_DEBUG_FILES=no
MK_KERNEL_SYMBOLS=no
SHELL=/bin/sh
NO_LINT=YES
PREFIX=/usr/local
LOCALBASE=/usr/local
LIBDIR="/usr/lib"
CC="cc"
CFLAGS="-O2 -pipe -Wno-error -fno-strict-aliasing"
CPP="cpp"
CPPFLAGS=""
LDFLAGS=""
LIBS=""
CXX="c++"
CXXFLAGS="-O2 -pipe -Wno-error -fno-strict-aliasing "
MANPREFIX="/usr/local"
BSD_INSTALL_PROGRAM="install -s -m 555"
BSD_INSTALL_LIB="install -s -m 0644"
BSD_INSTALL_SCRIPT="install -m 555"
BSD_INSTALL_DATA="install -m 0644"
BSD_INSTALL_MAN="install -m 444"


-- MAKE_ARGS

DESTDIR=/construction/xports/ports-mgmt/pkg/work/stage


-- PLIST_SUB

OSREL=11.0
PREFIX=%D
LOCALBASE=/usr/local
RESETPREFIX=/usr/local
PORTDOCS=""
PORTEXAMPLES=""
LIB32DIR=lib
DOCSDIR="share/doc/pkg"
EXAMPLESDIR="share/examples/pkg"
DATADIR="share/pkg"
WWWDIR="www/pkg"
ETCDIR="etc/pkg"


-- SUB_LIST

PREFIX=/usr/local
LOCALBASE=/usr/local
DATADIR=/usr/local/share/pkg
DOCSDIR=/usr/local/share/doc/pkg
EXAMPLESDIR=/usr/local/share/examples/pkg
WWWDIR=/usr/local/www/pkg
ETCDIR=/usr/local/etc/pkg


-- /etc/make.conf

SYNTHPROFILE=LiveSystem
USE_PACKAGE_DEPENDS_ONLY=yes
PACKAGE_BUILDING=yes
BATCH=yes
PKG_CREATE_VERBOSE=yes
PORTSDIR=/xports
DISTDIR=/distfiles
WRKDIRPREFIX=/construction
PORT_DBDIR=/options
PACKAGES=/packages
MAKE_JOBS_NUMBER_LIMIT=3
HAVE_COMPAT_IA32_KERN=YES
CONFIGURE_MAX_CMD_LEN=262144
_SMP_CPUS=4
UID=0
ARCH=BADARCH
OPSYS=FreeBSD
OSVERSION=1100122
OSREL=11.0
_OSRELEASE=11.0-SYNTH


-- Phase: check-sanity

===> License BSD2CLAUSE accepted by the user


-- Phase: pkg-depends


-- Phase: fetch-depends


-- Phase: fetch

===> License BSD2CLAUSE accepted by the user
===> Fetching all distfiles required by pkg-1.10.1 for building


-- Phase: checksum

===> License BSD2CLAUSE accepted by the user
===> Fetching all distfiles required by pkg-1.10.1 for building
=> SHA256 Checksum OK for pkg-1.10.1.tar.xz.


-- Phase: extract-depends


-- Phase: extract

===> License BSD2CLAUSE accepted by the user
===> Fetching all distfiles required by pkg-1.10.1 for building
===> Extracting for pkg-1.10.1
===> License BSD2CLAUSE accepted by the user
===> Fetching all distfiles required by pkg-1.10.1 for building
=> SHA256 Checksum OK for pkg-1.10.1.tar.xz.


-- Phase: patch-depends


-- Phase: patch

===> Patching for pkg-1.10.1
===> Applying FreeBSD patches for pkg-1.10.1


-- Phase: build-depends


-- Phase: lib-depends


-- Phase: configure

===> Configuring for pkg-1.10.1
configure: loading site script /xports/Templates/config.site
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... (cached) /bin/mkdir -p
checking for gawk... (cached) /usr/bin/awk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether make supports nested variables... (cached) yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking for style of include used by make... GNU
checking for gcc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking whether cc understands -c and -o together... yes
checking dependency style of cc... gcc3
checking how to run the C preprocessor... cpp
checking for grep that handles long lines and -e... (cached) /usr/bin/grep
checking for egrep... (cached) /usr/bin/egrep
checking for ANSI C header files... (cached) yes
checking for sys/types.h... (cached) yes
checking for sys/stat.h... (cached) yes
checking for stdlib.h... (cached) yes
checking for string.h... (cached) yes
checking for memory.h... (cached) yes
checking for strings.h... (cached) yes
checking for inttypes.h... (cached) yes
checking for stdint.h... (cached) yes
checking for unistd.h... (cached) yes
checking for minix/config.h... (cached) no
checking whether it is safe to define EXTENSIONS... yes
checking for cc option to accept ISO C99... none needed
checking build system type... Invalid configuration BADARCH-portbld-freebsd11.0': machine BADARCH-portbld' not recognized
configure: error: /bin/sh ./config.sub BADARCH-portbld-freebsd11.0 failed
===> Script "configure" failed unexpectedly.
Please report the problem to pkg@FreeBSD.org [maintainer] and attach the
"/construction/xports/ports-mgmt/pkg/work/pkg-1.10.1/config.log" including
the output of the failure of your make command. Also, it might be a good idea
to provide an overview of all packages installed on your system (e.g. a
/construction/xports/ports-mgmt/pkg/work/pkg-1.10.1/src/pkg-static info -g
-Ea).
*** Error code 1

Stop.
make[1]: stopped in /xports/ports-mgmt/pkg
*** Error code 1

Stop.
make: stopped in /xports/ports-mgmt/pkg


-- Termination

Finished: Friday, 14 JUL 2017 at 05:13:41 UTC
Duration: 00:00:12

@jrmarino
Copy link
Owner

uname doesn't determine the arch.
Paste the output of

file /bin/sh

which should reveal what OPNsense is doing

@jrmarino jrmarino reopened this Jul 14, 2017
@kraileth
Copy link
Author

kraileth commented Jul 14, 2017

Thanks for clarifying that uname is not actually used. Here's the output of the file command:

/bin/sh: ELF 64-bit LSB shared object, x86-64, version 1 (FreeBSD), dynamically linked, interpreter /libexec/ld-elf.so.1, for FreeBSD 11.0 (1100122), FreeBSD-style, stripped

Strange thing here is that file claims it's a "shared object" instead of an executable. However I can execute it and it gives me what looks like a bourne shell. Here's the ldd, too, since that also differs from the /bin/sh on my FreeBSD workstation:

/bin/sh:
libedit.so.7 => /lib/libedit.so.7 (0x26eba297000)
libthr.so.3 => /lib/libthr.so.3 (0x26eba4d1000)
librt.so.1 => /lib/librt.so.1 (0x26eba6f9000)
libm.so.5 => /lib/libm.so.5 (0x26eba8ff000)
libc.so.7 => /lib/libc.so.7 (0x26eb8ca4000)
libncursesw.so.8 => /lib/libncursesw.so.8 (0x26ebab2a000)

edit: Formating

@jrmarino
Copy link
Owner

The output of file seems to be identical to FreeBSD.
So that implies synth doesn't recognize the platform.
see:

function get_arch_from_bourne_shell return String

Did you build synth from source?
Cause of issue might be in the port itself.

@kraileth
Copy link
Author

Yes, Synth is not available as a package on OPNsense. There's also no portsnap nor svnlite or anything, so I got the ports tree from the git mirror. I had disabled fortran and obj-c in the gcc-aux package and also disabled quite a few other options in various ports. So to rule out that I crippled something I re-installed a fresh copy of OPNsense on my test machine and built Synth again leaving all the ports options at their defaults. Here's exactly what I did:

pkg install git
mkdir /usr/ports
git clone --depth=1 https://github.com/freebsd/freebsd-ports.git /usr/ports
echo DEFAULT_VERSIONS+=ssl=openssl > /etc/make.conf
make -C /usr/ports/ports-mgmt/synth configure-recursive fetch-recursive install clean

/etc/make.conf is nonexistent on OPNsense by default. But since OpenSSL from ports is used, the ports system reminded me to set that option. Then I just installed Synth 1.69 and keep running into the same BADARCH problem as before.

I've been using Synth all the time on GhostBSD and FreeBSD since I was introduced to it and I'd really like to see it become available to the OPNsense community. Unfortunately I don't know any Ada at all. I get the big picture of what's happening in the code that you linked to, but not the details necessary to just start hacking Synth myself trying to resolve that problem. Do you have any further hints for me on what to try or to look at?

@jrmarino
Copy link
Owner

open the file

 /usr/local/etc/synth/synth.ini

and see if the value for Operating_system is not "FreeBSD".

@kraileth
Copy link
Author

Unfortunately it is:

cat /usr/local/etc/synth/synth.ini

; This Synth configuration file is automatically generated
; Take care when hand editing!

[Global Configuration]
profile_selected= LiveSystem

[LiveSystem]
Operating_system= FreeBSD
Directory_packages= /var/synth/live_packages
Directory_repository= /var/synth/live_packages/All
Directory_portsdir= /usr/ports
Directory_options= /var/db/ports
Directory_distfiles= /usr/ports/distfiles
Directory_buildbase= /usr/obj/synth-live
Directory_logs= /var/log/synth
Directory_ccache= disabled
Directory_system= /
Number_of_builders= 2
Max_jobs_per_builder= 2
Tmpfs_workdir= true
Tmpfs_localbase= true
Display_with_ncurses= true
leverage_prebuilt= false

@jrmarino
Copy link
Owner

Try

synth build editors/nano

This will try to build pkg again, which will fail. After it fails, check the logs directory for "05_abnormal_command_output.log" file. Let me know if that file is blank. If it's not, print out the contents here.

@kraileth
Copy link
Author

Yes, it is blank:

ls -lh /var/log/synth/

total 20
-rw-r--r-- 1 root wheel 1B Jul 16 21:08 05_abnormal_command_output.log
-rw-r--r-- 1 root wheel 1B Jul 16 21:08 06_obsolete_packages.log
-rw-r--r-- 1 root wheel 9.0K Jul 16 21:08 ports-mgmt___pkg.log

@jrmarino
Copy link
Owner

Strange thing here is that file claims it's a "shared object" instead of an executable. However I can execute it and it gives me what looks like a bourne shell.

Well, this is definitely the cause.
On Synth, it's checking starting at character 28, and the change from "executable" to "shared object" is breaking the arch detection.

(incidently, the non-ports successor to Synth, Ravenadm, has more robust parsing of file string).

is the problem with "file"? You can install "file" from ports and see if it gives the same output. If it does, what is your speculation on why /bin/sh has this output from file?

@kraileth
Copy link
Author

I built the newer "file" from ports and it shows the same output. Why? I have no idea. Probably there's something messed up when building OPNsense. I will try to bring this to the developer's attention on their forums tomorrow or so.

However this was actually the solution to the whole mystery - I scp'd the statically linked /rescue binary from my fbsd 11.1 workstation over to the OPNsense test machine and put it in place of their /bin/sh. While a ~10 MB statically linked executable may not be nice for sh, it showed that Synth seems to actually work. Pkg could be built and Synth started building other packages as well. I'll attempt a full system-upgrade overnight and see if there are any problems but I don't really expect any.

So thanks a lot for your help! If you are interested in what happened to OPNsense's /bin/sh, leave this issue open and I'll post back here again (if I get an answer). Otherwise this can be closed as resolved. Your Raven project sounds terrific BTW, I will totally have to set aside some free time to play with it!

@jrmarino
Copy link
Owner

sure, I'm interested.
Synth can probably be modified to be more forgiving with the file output, but it would be nice to know what's going on with OPNsense before I make any modifications

@kraileth
Copy link
Author

Instead of asking at the OPNsense forums like I originally intended, I did a little research. There's a partnership between OPNsense and the HardenedBSD project and that's where they get their system hardening from. And guess what: That seems where the /bin/sh issue seems to originate from:

/bin/sh: ELF 64-bit LSB shared object, x86-64, version 1 (FreeBSD), dynamically linked, interpreter /libexac/ld-elf.so.1, for FreeBSD 11.0 (1100514), FreeBSD-style, stripped

I'm going to ask anyway, now and see if I can get an answer.

@lattera
Copy link

lattera commented Jul 21, 2017

This is because /bin/sh is compiled as a Position-Independent Executable (PIE) in OPNsense. When compiling an application as a PIE, the linker will set the ELF type to be ET_DYN rather than ET_EXEC. Thus, file(1) reports /bin/sh as an ELF shared object rather than an ELF executable.

Thus, file(1) reporting "shared object" is the correct behavior. Synth should take that into account.

@kraileth
Copy link
Author

Wow, you guys are fast! Thanks Shawn for explaining this. I've already got HardenedBSD installed on my test machine, will try out Synth with it next.

@lattera
Copy link

lattera commented Jul 21, 2017

I'm glad to help. :)

With Synth supporting DragonflyBSD, and DragonflyBSD having ASLR, Synth really should support PIEs anyways. ASLR doesn't make much sense unless the application is compiled as a PIE.

@jrmarino
Copy link
Owner

okay, I'll see about bringing in the newer ravenadm code into synth to address this.
Once fix confirmed, I'll push a new release so the synth port maintainer can update.

jrmarino added a commit that referenced this issue Jul 21, 2017
This addresses issue #87, allowing Synth to properly detect the
architecture on OPNsense and HardenedBSD which compile /bin/sh as a PIE.

Bump version in anticipation of a new release
@jrmarino
Copy link
Owner

@kraileth ,
If you could, apply this patch to synth port:

ironwolf# cat /tmp/patch-next-synth
--- ports-mgmt/synth/Makefile
+++ ports-mgmt/synth/Makefile
@@ -2,7 +2,7 @@
 # $FreeBSD$

 PORTNAME=      synth
-PORTVERSION=   1.69
+PORTVERSION=   1.70
 DISTVERSIONPREFIX=     v
 CATEGORIES=    ports-mgmt

--- ports-mgmt/synth/distinfo
+++ ports-mgmt/synth/distinfo
@@ -1,3 +1,3 @@
-TIMESTAMP = 1490956647
-SHA256 (jrmarino-synth-v1.69_GH0.tar.gz) = b1b73febbe1ae0fddd630c458e681e634f0bbd5dd5698bc21b9cbbf77d87a018
-SIZE (jrmarino-synth-v1.69_GH0.tar.gz) = 139278
+TIMESTAMP = 1500662891
+SHA256 (jrmarino-synth-v1.70_GH0.tar.gz) = b9f5c91bbac0a50a9d773293ad6b433d75462112a6b04259f035efbc577d4efb
+SIZE (jrmarino-synth-v1.70_GH0.tar.gz) = 139429

rebuild synth and upgrade to version 1.70.
Then restore /bin/sh to the original PIE version.
I believe it should work, but I'd like confirmation before I let Eric know to upgrade ports version.

@jrmarino
Copy link
Owner

jrmarino commented Jul 21, 2017

DragonflyBSD having ASLR, ...

I don't believe DF supports ASLR actually.
Moreover, I seem to recall something is not quite right with PIE support on DF. I think there's a small bug in the real-time linker that needs to be tracked down (or maybe the kernel). I never formally derived a test case for the PIE issue so it was never fixed. I can't remember the details or how prevalent the symptoms are (e.g it might work 95% of the time, but fail on some edge case).

@jrmarino
Copy link
Owner

or maybe I'm getting confused with RELRO. Yeah, I think I am. it's RELRO that has a linker issue. PIE should be okay.

@lattera
Copy link

lattera commented Jul 21, 2017

I don't know their level of support, but they have some level of address space randomization:

https://www.dragonflydigest.com/2014/01/20/13213.html

@jrmarino
Copy link
Owner

hmm, surprising. I know dillon doesn't think much of ALSR effectiveness overall. That's probably why it's off by default.

@kraileth
Copy link
Author

I put a fresh copy of OPNsense back on my test machine last evening, cloned the ports tree and applied the patch. The first hunk failed for whatever reason, but just changing the version number in the Makefile is a quick edit. Then I built all the dependencies and the new Synth. It worked - even with the hardening options enabled.

Now Synth detects the arch correctly and is happily rebuilding all packages. I only had to remove port options for everything that was built while building Synth: The HardenedBSD ports tree includes three additional hardening options that Synth obviously knows nothing about. So I guess if I rebuild the whole system, all packages will lose their hardening. Since I do not intend to run Synth directly on my router but plan to do so in a VM and import the repo created there (with a lower priority than the OPNsense repo), this will not be much of an issue for me (only the additional packages will not be hardened but it's definitely better to have those not-hardened packages than not having that software available at all).

I don't know if there's any demand for it, but just out of curiosity I'll ask anyways: Would it be a complicated change to support those options, too, if available? Because in that case Synth could be used with the HardenedBSD ports tree perfectly well (and probably be used on HardenedBSD, too).

I'll write a final report how things went with the rebuilding (~80 packages to go right now).

@jrmarino
Copy link
Owner

if the new options defined by the ports tree, then you should be able to set them via -make.conf, right? Without knowing the details, it seems that synth could support these hardening options without modification, only configuration. Maybe it would help to know how these options are set via poudriere or live building ...

@kraileth
Copy link
Author

About Synth 1.70: I've patched the local ports tree on my GhostBSD workstation and build updates with it. The new Synth works as expected. I can also ping Eric on the gbsd forums for you if you decide to release this new version.

About the package rebuild on OPNsense: It completed with two few failures that I will look into when I find the time.

About hardened ports options: I've uploaded a screenshot as pictures say a thousand words:
http://www.elderlinux.org/temp/hardened_ports_options_example.png
The three hardening options are available for every port in the HardenedBSD ports tree. Here's what the port's options file looks like after accepting this configuration:

% cat /var/db/ports/shells_mksh/options

This file is auto-generated by 'make config'.

Options for mksh-55

_OPTIONS_READ=mksh-55
_FILE_COMPLETE_OPTIONS_LIST=EXAMPLES PIE RELRO SAFESTACK
OPTIONS_FILE_SET+=EXAMPLES
OPTIONS_FILE_SET+=PIE
OPTIONS_FILE_SET+=RELRO
OPTIONS_FILE_UNSET+=SAFESTACK

It also looks like Synth leaves those options alone. It they are set by default for a port, that port is built with those enabled. However if you change the defaults, Synth will detect "obsolete port options" and demand that those be reset.

@jrmarino
Copy link
Owner

  1. the "release" had already occurred before I asked to test it (a failed test would have resulted in a second release)
  2. I already emailed eric but you can ping him too
  3. from your description, the hardening options are off by default. If synth is deleting because of options mismatch, it means it got exposed to packages built with non-default options (coupled with synth's option directory was not configured)
  4. you can globally set options via -make.conf, see https://lists.freebsd.org/pipermail/freebsd-ports-announce/2013-June/000062.html

@lattera
Copy link

lattera commented Jul 22, 2017

PIE and RELRO are enabled by default. SAFESTACK isn't, with the exception of around 150-200 ports.

@jrmarino
Copy link
Owner

something is amiss. If your tree sets PIE and RELRO by default, the next pass wouldn't consider the packages obsolete for options-mismatch reasons. These 3 new options are no different than any other global option (e.g. NLS and DOCS)

@kraileth
Copy link
Author

The mismatch occurs for the ports that I explicitly set options for (i.e. doing a "make config-recursive" on ports-mgmt/synth). Even though I just accepted the default options, they had options set in /var/db/ports. Synth complained about these, I think. But after I did "rmconfig" on all of those, Synth no longer refused to build them, but I think it picked up the default options correctly. But this is just my guess.

@jrmarino
Copy link
Owner

those symptoms suggest the options directory in the synth configure menu is set incorrectly.
in other words, you're saving the options in one location, but synth can't find those saved options so it's using default options (a mismatch).

@jrmarino jrmarino closed this as completed Aug 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants