[00:00.000 --> 00:01.140] Okay, hello. [00:01.140 --> 00:08.620] Welcome to this talk, Emulating Embedded Linux Devices at Scale with Light Touch Firmware Rehosting. [00:08.880 --> 00:28.080] Today I'm going to give this a kind of higher level approach, like overview of what we've been doing in the advanced research team at CrowdStrike, which is kind of giving us this quite low investment, high return ratio when we've been emulating embedded Linux systems. [00:28.080 --> 00:36.620] So by the end, you should be able to kind of take the principles that I'm talking about here and apply them in other emulation stuff. [00:36.620 --> 00:43.580] It's a little bit of a patchwork approach and a bit proof of concept, but hopefully a little bit helpful at least. [00:44.840 --> 01:13.220] At the moment, most embedded Linux emulation works in user space, so a lot of the emulation approaches take a stock ARM or MIPS KME machine, boot into like a normal distro, unpack the firmware into that guest, CH root into it and then run binaries with like a LD preload to paper over incompatibilities in it. [01:13.920 --> 01:19.860] This approach works pretty well if you're looking at a specific binary that you want to look at on a specific device. [01:20.420 --> 01:25.500] There are some cases where it gets pretty tricky, but in a lot of cases, it's totally fine to do. [01:25.760 --> 01:41.400] But when you're doing that kind of approach with the LD preload approach, what you're basically trying to do is compensate for missing peripherals and a different kind of system set up that the binaries are not expecting. [01:42.380 --> 01:45.120] So NV RAM is the classic example. [01:45.840 --> 01:56.300] The user space way to deal with NV RAM is to LD preload something like libNVRAM or some descendant of that with extra functionality. [01:57.580 --> 02:07.820] But there's certain cases where we can't really address these kind of incompatibilities just by LD preloading stuff. [02:07.820 --> 02:15.260] When the systems are designed to work more holistically with a lot more moving parts. [02:18.040 --> 02:23.400] So the traditional kind of way of doing things in user space doesn't really work for those kind of approaches. [02:24.360 --> 02:28.040] So our question was, can we try and do it in a different way? [02:28.860 --> 02:32.200] We want to do vulnerability research on these systems. [02:32.380 --> 02:34.760] We just want to do things like run honey pots. [02:35.280 --> 02:37.540] So we want something like a full system emulation. [02:37.540 --> 02:42.220] We want the whole thing to be running as it usually would on the device. [02:43.260 --> 02:52.660] But we can't really emulate just like a specific device in full without a lot of time and effort and the payoff might be kind of low for how much time we'd have to put in. [02:53.120 --> 02:59.640] So we want to see if there's another approach that we could try and try a few different things. [02:59.640 --> 03:02.060] And we came to this. [03:02.060 --> 03:04.360] This kind of re-hosting concept. [03:04.920 --> 03:08.520] The main idea of this is we don't touch user space at all. [03:08.520 --> 03:10.960] We don't want to do anything in user space. [03:10.960 --> 03:12.460] We don't want to LD preload anything. [03:12.460 --> 03:15.920] We don't want to do any hacks during the boot process. [03:16.140 --> 03:20.920] We just want to offer up a firmware image to the re-hosting environment. [03:21.240 --> 03:25.600] Ideally the exact same firmware image that's bundled in a firmware update image. [03:25.840 --> 03:27.660] And for it just to run. [03:29.740 --> 03:33.280] User space can only kind of interact with what the kernel gives it. [03:34.180 --> 03:41.160] And we, the re-hosting approach means we can make loads of changes in the kernel and do as much as we want in the kernel. [03:41.280 --> 03:45.620] But we just kind of create an environment. [03:45.680 --> 03:47.460] Create this re-hosting environment for it. [03:48.240 --> 03:53.720] So the idea is we would build a kernel for a particular KME machine with a compatible architecture. [03:54.020 --> 04:01.700] And then modify the kernel to create an environment which looks as close to a real device as possible from the perspective of user space. [04:01.960 --> 04:08.240] And then we can just tweak the kernel, tweak the KME machine, like add peripherals, add hardware whenever we want. [04:08.420 --> 04:10.140] Or whenever we need to. [04:11.060 --> 04:14.320] The question is then like where do we start with something like this? [04:16.220 --> 04:19.720] All embedded systems are running the Linux kernel. [04:19.720 --> 04:25.220] But they're running a Linux kernel built for a particular system on chip or sock. [04:27.260 --> 04:31.160] A sock is just a bundle of CPU memory, other peripherals, things like that. [04:31.240 --> 04:32.720] Networking, storage. [04:32.720 --> 04:34.620] It depends on the sock. [04:35.460 --> 04:38.500] Some peripherals might be integrated directly into the sock. [04:38.900 --> 04:45.940] Others might have like a physical interface that the hardware devs can wire another chip to, like flash memory or something like that. [04:46.320 --> 04:51.120] But each sock is designed by a hardware to cater to a particular market. [04:51.120 --> 04:56.240] So a specific sock will get used in lots of different embedded systems, sold by lots of different vendors. [04:56.560 --> 05:03.060] So Netgear, Asus, TP Link, they don't buy, they don't build the sock themselves. [05:03.060 --> 05:04.320] They're not chip companies. [05:04.320 --> 05:07.560] They buy a sock from a manufacturer and then use them in their devices. [05:08.340 --> 05:13.820] And so the same sock is used across multiple devices by multiple different vendors. [05:14.940 --> 05:17.040] So the supply chain looks like this. [05:17.630 --> 05:23.580] The sock manufacturer makes the socks, so like Broadcom, Qualcomm, Media Tech, all these kinds of companies. [05:23.940 --> 05:32.420] Vendors buy the socks from the manufacturer and the manufacturers give the vendors some kind of SDK they can build their own firmware with to run on that sock. [05:32.920 --> 05:36.620] The vendor will use that SDK to build the kernel and file system binaries. [05:38.420 --> 05:43.700] So it kind of means that any code running close to the hardware is usually manufacturer provided. [05:43.880 --> 05:46.500] Like vendors won't really have much to do with that. [05:46.760 --> 05:49.800] And the vendors can do whatever they want in user space. [05:49.800 --> 05:56.360] They just use their like APIs and libraries or whatever provided by the manufacturer as part of the SDK as well. [05:58.620 --> 06:00.480] Well, what does that mean for us? [06:00.480 --> 06:03.000] Well, we're only trying to make changes to the kernel. [06:03.780 --> 06:16.680] So instead of emulating a specific device by like a specific vendor, we think about like we're trying to emulate the kernel or like create a new kernel that looks a lot like one for a specific sock. [06:17.800 --> 06:22.680] We're not trying to emulate the hardware of the sock itself and try and run a prebuilt kernel on it. [06:22.900 --> 06:24.140] We did try that. [06:24.140 --> 06:25.820] It's a bit tricky. [06:27.180 --> 06:33.700] So we want to just build a kernel and sort of tweak a KMU machine if we need to. [06:33.700 --> 06:41.460] So that from the perspective of user space, user space thinks it's running on that particular sock kernel combo. [06:41.940 --> 06:49.140] The benefit of this in theory is if we can do this, then we should be able to take firmware from any device based on that sock and run it. [06:49.140 --> 06:54.520] So we could build one rehosting environment and get a few dozen possible emulated devices out of it. [06:56.720 --> 07:00.040] We've got a lot of things we can kind of lean on to try and do this. [07:00.040 --> 07:06.280] We don't obviously have data sheets from the manufacturers for these socks. [07:06.280 --> 07:08.320] We don't have kind of proprietary information. [07:08.460 --> 07:13.340] We do have KMU machines which are probably good enough for getting started. [07:13.340 --> 07:14.760] We have the Linux kernel. [07:14.800 --> 07:17.880] We have firmware images for devices that we want to emulate. [07:17.880 --> 07:19.860] We have reverse engineering tools. [07:20.080 --> 07:22.880] We have GPL code in a lot of cases. [07:23.320 --> 07:25.300] We have compiled kernels. [07:25.300 --> 07:29.100] We have kernel modules that come with the firmware images. [07:29.120 --> 07:40.240] We have things like open WRT, DDWRT, kind of open source distros which might have releases to run, which run on the sock we're trying to target. [07:40.240 --> 07:41.920] They kind of do the opposite of what we're trying to do. [07:41.920 --> 07:45.740] They're trying to run on the actual hardware and we're trying to not run on the actual hardware. [07:45.740 --> 07:47.220] But they're kind of similar. [07:47.220 --> 07:50.420] We have kind of similar but opposite intentions. [07:51.420 --> 07:53.140] We don't have data sheets. [07:53.140 --> 07:56.540] We don't have really that much source code for binary blobs. [07:56.700 --> 07:58.100] We don't have documentation. [07:58.100 --> 08:02.900] We just don't have unlimited time and energy or unlimited money. [08:02.900 --> 08:06.660] We're just trying to get to a point where the thing runs well enough. [08:08.360 --> 08:10.220] So this is the vague plan. [08:10.220 --> 08:18.060] We start with running a mainline kernel, similar to a version, a similar version to the one that actually runs on a normal device. [08:18.260 --> 08:22.080] And then we compile that to run on the most compatible KMU machine we can find. [08:23.000 --> 08:26.560] And then on that machine we load the file system and just try and run stuff. [08:26.560 --> 08:29.520] And we see what breaks and we try and figure it out. [08:30.060 --> 08:35.320] And then we patch the kernel again and maybe make modifications to the KMU machine. [08:35.320 --> 08:39.600] And we just keep doing this in this kind of twisted development life cycle. [08:40.780 --> 08:50.080] We can, during all this, we can infer behavior from the user space to kind of figure out what it's actually expecting and what it wants to see and what it's doing. [08:50.080 --> 08:57.060] And then we can make changes reflectively in the kernel to cover that. [08:58.060 --> 08:59.940] So this was the device we chose to do this. [08:59.940 --> 09:01.540] It's an older Asus router. [09:02.100 --> 09:05.400] It's based, it's the AC87U. [09:05.800 --> 09:07.640] It was released in 2014. [09:07.660 --> 09:10.000] It went end of life probably 2021. [09:10.240 --> 09:14.480] It's based around the BCM4709, which was your Broadcom sock. [09:15.980 --> 09:20.500] And the 4709 is used in quite a few routers. [09:20.600 --> 09:23.660] It's a router sock with a Cortex A9 core. [09:23.660 --> 09:29.880] It's in the broader BCM4708, 4707, 4708, 4709 family. [09:30.040 --> 09:32.580] It has an integrated ethernet stack. [09:33.260 --> 09:38.620] It was built for the router market so it gets used in routers and extenders and things like that. [09:38.620 --> 09:40.380] The list up here is kind of non-exhaustive. [09:40.380 --> 09:53.640] It's not every router that runs with this, that has this sock, but it's a list, it gives a kind of indication of what different vendors, which different vendors were using it and what kind of device it's being used on. [09:56.160 --> 09:59.560] So the AC87U was kind of chosen at random. [10:00.680 --> 10:07.820] The idea was we were kind of looking at Asus stuff and we wanted to just kind of see how easy it would be to do emulation for them. [10:07.820 --> 10:15.520] But the idea is that whatever we build for the 87U would maybe work for other Asus devices running on the same sock and there's a lot of them. [10:16.460 --> 10:19.580] I should also say we didn't actually have the physical device for this at all. [10:19.580 --> 10:21.700] We worked only with the firmware image. [10:21.800 --> 10:24.720] So there's also a bit of an artificial restriction. [10:25.800 --> 10:28.260] But I kind of just wanted to see if it could be done. [10:28.260 --> 10:38.340] There's a lot of common wisdom when it comes to embedded systems emulation that you need the physical device to do it, to like dump the NV RAM and get all these things out of it. [10:38.340 --> 10:42.200] But our intention longer term is to emulate devices at scale. [10:42.200 --> 10:45.320] So we don't necessarily want to buy every single device that we want to emulate. [10:45.320 --> 10:47.660] So it's a bit of an experiment in this as well. [10:49.480 --> 10:54.240] The first step for this we can just get the firmware. [10:54.240 --> 10:56.400] And we get whatever is publicly available. [10:56.760 --> 11:00.900] We can get the most recent firmware from the Asus website from 2021. [11:01.140 --> 11:05.800] Get the GPL code which has some like Broadcom commented code in there as well. [11:06.540 --> 11:11.460] The firmware image is in this TRX format which is like an older Broadcom firmware file format. [11:11.460 --> 11:14.540] We can unpack these like really easily with Unblob. [11:14.780 --> 11:18.860] And using Unblob also means we can carve out the specific sections of the firmware. [11:18.920 --> 11:22.140] And we don't have to do any post processing stuff like we'd have to do with Binwalk. [11:22.140 --> 11:25.680] So you can carve everything out very precisely with Unblob. [11:27.240 --> 11:32.080] Once it's unpacked, so we get this Lisma compressed kernel and we get the SquashFS file system. [11:35.150 --> 11:39.990] We can, with this unpacked firmware we can kind of get a sense of what the device is actually running. [11:41.070 --> 11:44.170] What it's running on. [11:45.010 --> 11:46.470] And so what we need to emulate. [11:46.470 --> 11:54.410] So we get the kernel version out, a string out of the kernel which is 2.6.36.4 BRCM arm. [11:55.330 --> 11:59.830] It's this Linux 2.6 with some Broadcom modifications I guess. [11:59.970 --> 12:07.050] It's compiled with GCC 4.3, 4.5.3 from build root 2012.2. [12:08.550 --> 12:10.390] So we know what it's compiled with as well. [12:10.390 --> 12:15.970] We can use the same compiler and just kind of keep things as similar as possible when we're building our own kernel. [12:16.530 --> 12:18.550] We can also get the boot args out of the kernel blob. [12:18.550 --> 12:21.030] So this can give us an idea of how it's going to boot. [12:21.270 --> 12:27.790] They might not be the actual boot args that the real device is using but it's a decent way to start. [12:28.110 --> 12:31.790] So the root boot arg points to MTD block 2. [12:31.790 --> 12:34.390] So it's booting from this particular part in flash. [12:35.290 --> 12:38.670] The init boot arg points to S bin pre init. [12:39.770 --> 12:46.570] If we look at the unblob file system we can see that pre init is a link to a binary called RC. [12:46.570 --> 12:48.890] There's no init scripts or anything like this. [12:48.890 --> 12:52.390] It's just this RC binary being run as pre init. [12:54.110 --> 12:56.210] A lot of other binaries linked to RC. [12:56.210 --> 12:58.870] It's a pretty big sort of system functionality binary. [12:59.550 --> 13:00.710] And it checks how it's invoked. [13:00.710 --> 13:05.290] So it will run in this pre init mode when it's run as pre init. [13:05.330 --> 13:08.110] Or in another mode if it's run with a different name. [13:09.350 --> 13:14.030] The layout of the unpack file system tells us a lot how the system is expected to run. [13:14.630 --> 13:21.690] Kind of directions we'd usually expect to see useful stuff in populated or actually just broken sim links to temp. [13:21.710 --> 13:26.790] So temp is probably populated at boot time, probably by the init process, probably by pre init in particular. [13:28.230 --> 13:37.570] And so in order to get, when we're doing our emulation we want to get the system into a state using the normal init process they're expecting to perform. [13:39.050 --> 13:44.310] So that we can run all the other binaries and things on it and the system will be up kind of in a normal state. [13:44.310 --> 13:49.570] So we don't want to go through and kind of paper over these things and do this stuff in user space. [13:49.570 --> 13:54.850] We just want to make sure that the init process is capable of kind of completing. [13:56.070 --> 14:00.630] So this is, we can apply this same kind of life cycle that we had before to this setup. [14:01.010 --> 14:08.750] We know the architecture, kernel version, compiler, the way the file system has been loaded from memory, from flash sorry. [14:09.970 --> 14:18.650] So the first kind of naive plan which is definitely going to fail in the first instance is like build a KMU machine with a cortex A9 core, sorry find one. [14:18.650 --> 14:23.450] Build a 2.6.36.4 kernel for it. [14:23.910 --> 14:27.570] Attach the squash FS image which we carved out earlier. [14:30.070 --> 14:38.590] On some kind of emulated flash device and then hopefully the kernel will load the file system and then we can try and run stuff and see what breaks basically. [14:38.590 --> 14:40.830] And then try and fix it and make it better. [14:42.170 --> 14:50.190] For this one in particular, so KMU has this V express A9 board which has a cortex A9 core and flash memory. [14:50.750 --> 14:56.690] It's no flash not NAND which is not a huge problem but in some cases it can be a little bit of a problem. [14:56.690 --> 15:00.430] But just for the start, just for starting this is not a huge blocker. [15:00.870 --> 15:14.830] There's a def config for the V express machine in Linux 2.6.36.4 and there's kind of machine code for the V express series like Mac V express code in the kernel as well. [15:14.950 --> 15:24.630] So we can build the 2.6.36.4 kernel using the V express def config, using GCC 4.5.3 from build root 2012. [15:25.710 --> 15:32.790] Getting that old version of GCC compiled with old build root can be a bit of a hassle. [15:32.790 --> 15:41.230] So I'm not going to go into it here but it just involves using like old Ubuntu machines and kind of commenting out bits of code that don't work every now and again. [15:41.230 --> 15:44.930] But it does get built and it does work. [15:47.510 --> 15:53.850] Once the kernel is compiled and running, it kind of works pretty well. [15:53.910 --> 16:00.230] The kernel loads into the V express machine. [16:01.150 --> 16:03.410] You do need to edit the kernel config a little bit. [16:03.410 --> 16:10.750] You have to make sure that you enable things like squash FS and MTD and the kind of stuff that you want to have in your rehosting kernel. [16:12.590 --> 16:16.930] You also need to kind of think about things that you might want to compile into the kernel. [16:16.930 --> 16:19.410] Because we're not going to be able to load anything later. [16:19.410 --> 16:21.850] Like user space is not going to be able to load any kernel modules. [16:21.850 --> 16:26.870] So we just want to, in this kernel we can kind of just build everything that we might potentially need into it. [16:26.870 --> 16:29.570] As long as it's not incompatible with it with each other. [16:29.910 --> 16:34.790] And it kind of makes life a little bit easier later on. [16:35.850 --> 16:39.850] The first thing we have to do is figure out how to present the file system image to the kernel. [16:40.050 --> 16:42.450] So we need to forge some kind of flash. [16:42.870 --> 16:47.030] The flash needs to be laid out in a super predictable way. [16:47.870 --> 16:55.410] But because we control the boot and the kernel, we can pass the MTD parts boot ARG to the kernel. [16:55.410 --> 17:00.550] Which will tell it what our flash layout is and where to look for certain things. [17:00.550 --> 17:05.110] So we can still use MTD block 2 as the place we're going to keep our file system. [17:05.150 --> 17:11.770] And just pass it in the MTD parts boot ARG to tell the kernel that that's where it should boot from. [17:12.970 --> 17:14.850] So the flash layout can look something like this. [17:15.290 --> 17:19.050] We have a couple of imaginary empty parts, then the squash FS file system. [17:19.170 --> 17:27.430] One of the quirks of VXpress boarding KME is it needs the file that's passed as flash to be exactly 64 megabytes. [17:28.130 --> 17:32.770] But yeah, we can just put what we want in the place we want it and just pad the rest of the file out. [17:34.290 --> 17:44.310] We compile the kernel, construct a KME command that looks a lot like the one up there and the file system doesn't unpack at all. [17:44.830 --> 17:47.510] But we do get a relatively decent error. [17:47.550 --> 17:52.430] The one is, it tells us it's failing because it's something to do with the compression of the squash FS file system. [17:52.530 --> 17:59.990] So it's figured out it's squash FS but it can't do anything about it because it's figured out it uses some kind of unknown compression. [18:00.810 --> 18:08.970] And the main problem is that the squash FS image that ships for the AC87U is compressed with EXED. [18:09.590 --> 18:19.510] There's no native EXED compression support for squash FS in the main line kernel until 2.6.38 and we're on 2.6.36.4. [18:19.510 --> 18:23.290] So Broadcom obviously does something a bit different. [18:23.870 --> 18:27.350] And we can figure out what they're doing by looking at some of the GPL code. [18:27.810 --> 18:33.890] The GPL code we got from the ASA site for this device has some squash FS related code that includes some EXED support. [18:33.890 --> 18:39.490] So it's a slightly modified version, kind of back ported EXED support. [18:40.050 --> 18:48.810] And similarly we can just back port EXED support from 2.6.38 into our own build or just use the code from this GPL release. [18:49.390 --> 18:50.690] And it works. [18:51.690 --> 18:53.570] The file system unpacks. [18:54.350 --> 19:03.790] But it's a bit of an indication of the kinds of tweaks we have to make to the kernel as we're going along trying to do this kind of emulation effort. [19:06.870 --> 19:12.710] The first thing we do is just pass BNSH as the init command. [19:13.690 --> 19:16.350] And we can just drop into a shell and poke around a bit . [19:16.350 --> 19:21.450] This is a bit like the situation we'd be in if we were doing the CH root kind of user space approach. [19:21.490 --> 19:25.670] But of course nothing actually interesting runs because the system hasn't been initialized. [19:25.810 --> 19:28.370] The file system is mounted but nothing has actually run. [19:28.850 --> 19:33.810] We know that preinit is responsible for the initialization. [19:33.890 --> 19:36.230] So let's have a little quick look at that. [19:38.030 --> 19:51.050] So you can see here a lot of what the preinit code does is create directories, set up symlinks, copy files around into expected places, mounts pseudo file systems, normal init process stuff. [19:52.790 --> 19:59.830] So yeah, in theory if we can get preinit running and completing, it will be a big step into being able to emulate the whole system. [20:00.150 --> 20:08.610] There's another notable thing about preinit just from some static analysis, which is that it contains routines which only run when it's running with PID 1. [20:08.610 --> 20:11.870] So we actually need it to be the init process in order for it to do stuff. [20:11.870 --> 20:18.810] Like it's a kind of orchestrating process in the system, in this kind of holistic architecture of the system. [20:19.050 --> 20:22.610] So we can't just invoke it from a shell and expect it to work. [20:25.410 --> 20:33.550] We can just start the thing again with the init boot up pointing to preinit and see what fails basically. [20:33.550 --> 20:37.810] I mean the big one is we got a whole load of NVRAM errors. [20:38.690 --> 20:40.630] This is definitely expected. [20:40.630 --> 20:44.890] NVRAM is the most common blocker in embedded Linux emulation. [20:45.170 --> 20:48.630] So we need something to work to take it's place. [20:52.450 --> 20:56.250] We kind of need to make a plan here. [20:56.490 --> 21:09.570] We're not touching user space, we just need to change things in the kernel to create a device, a dev NVRAM basically, which would behave just like the dev NVRAM device would on the AC87U. [21:09.570 --> 21:19.230] We can get a decent idea of what, how dev NVRAMs are actually interacted with by looking at the user space binaries in the firmware image itself. [21:19.230 --> 21:23.830] So Brawl compromises these APIs to interact with NVRAM. [21:23.830 --> 21:32.270] In particular this lib NVRAM SO, which is kind of the official Broadcom lib NVRAM. [21:32.270 --> 21:45.370] On the 87U, user space binaries will import the lib NVRAM, the Broadcom lib NVRAM and call functions like NVRAM get, NVRAM set, NVRAM commit. [21:46.110 --> 21:55.830] We can have a look at this, at this library and see what it's doing under the hood when it's doing these kinds of things and hopefully, yeah, this can inform what we're going to do. [21:59.210 --> 22:11.490] So yeah, looking at lib NVRAM, it opens, it has a function called NVRAM in it, which opens dev NVRAM and stores the handle to it at this global memory location. [22:11.750 --> 22:20.650] And then NVRAM get will call read on the open file handle with a pointer to a string of the key that wants to be read. [22:20.910 --> 22:23.430] And another buffer where the value should be written. [22:24.770 --> 22:30.730] NVRAM set calls write on the open file handle with a pointer to the key equals value string. [22:30.730 --> 22:36.710] So it's using really basic sys calls that we can handle when we make our own kernel device that does something like this. [22:37.370 --> 22:39.950] So yeah, we build a character device in the kernel. [22:41.530 --> 22:48.550] In lib NVRAM, the whole NVRAM buffer is expected to be a contiguous block of key equals value string separated by null bytes. [22:48.550 --> 22:51.010] So we can kind of just mirror what that is. [22:51.430 --> 22:53.550] We do need somewhere to persist it. [22:53.550 --> 22:57.690] So we can back it with just another partition in the flash memory. [22:58.450 --> 23:02.090] We make sure that dev NVRAM, the file is created. [23:02.090 --> 23:03.710] So user space can call open on it. [23:03.710 --> 23:08.870] And then we just handle all the read, write, IOPTL operations as well. [23:09.030 --> 23:15.390] And when we're doing that, we just look at what the Broadcom lib NVRAM is expecting to receive and we just return what it wants. [23:15.450 --> 23:18.210] We're just kind of servants to the user space in that way. [23:18.910 --> 23:21.850] And this is kind of a rough idea of the kind of code we have in the kernel. [23:21.850 --> 23:23.610] I'm sorry you probably can't see it here . [23:24.350 --> 23:28.750] But lib NVRAM is just doing super basic reads and writes. [23:29.230 --> 23:36.970] We also handle calls to like NVRAM commit which is an IOPTL with a specific command number and we just write the buffer back to the flash when we get that. [23:37.810 --> 23:44.110] And then there's calls like NVRAM get all which is just like NVRAM get but passes null to the second read arc. [23:44.230 --> 23:47.590] And if we get that, we return all the NVRAM key value pairs. [23:47.950 --> 23:51.050] So this is relatively, I have to say, quick to write. [23:51.690 --> 23:55.150] We can write it, compile it, try it, squash bugs, compile it. [23:55.150 --> 23:57.470] It's just kind of development life cycle stuff. [23:57.690 --> 24:00.690] And the approach just kind of works. [24:00.690 --> 24:08.410] And we can test it with the command line NVRAM tool that's in the ACES firmware and get all the data out of the sort of fake NVRAM partition. [24:08.850 --> 24:19.030] But with this approach we just get a system wide NVRAM device that has persistent backing and works in the way that the user space is expecting. [24:19.150 --> 24:23.450] And so it will probably work on other similar era devices that use the same mechanism as well. [24:27.870 --> 24:29.850] Alongside doing this, we're also trying to emulate the thing. [24:29.850 --> 24:32.470] So we have to solve the problem of what goes into the NVRAM. [24:32.730 --> 24:39.010] And as I said earlier, the common wisdom is that we often need to be able to dump NVRAM from an actual device in order to do anything with it. [24:39.010 --> 24:43.770] This isn't actually the case on the AC87U or any of the other ACES devices. [24:43.790 --> 24:47.530] There's a lot of logic which populates NVRAM with default values. [24:48.170 --> 24:56.330] And there's certain like NVRAM keys that you can set that will trigger a kind of full NVRAM reset. [24:57.010 --> 24:59.750] So there's no need to have an actual physical device for these devices. [24:59.750 --> 25:03.450] And we can kind of freestyle based off the defaults that pre-init creates. [25:03.990 --> 25:13.120] And yeah, once we have NVRAM, pre-init kind of works a bit better and gets a bit further. [25:13.120 --> 25:16.580] But we start hitting other issues that we weren't necessarily expecting. [25:16.580 --> 25:20.300] So pre-init needs to be there to be more than one network interface. [25:20.300 --> 25:22.880] It's looking for F1 but it can't find it. [25:23.120 --> 25:31.380] We can't just add another interface without tweaking the V-Express machine and the kernel, Mac V-Express a little bit. [25:31.380 --> 25:33.620] But we can quite easily do it. [25:33.920 --> 25:47.460] We can just map a new ethernet interface into memory in the KMU code and in the Linux code we just add another like IRQ for the, in the Mac V-Express board files which lines up with the IRQ value in the KMU code. [25:47.980 --> 25:54.260] It's a relatively light, light touch strategy but, yeah. [25:58.580 --> 26:04.160] It's pretty, it's a little bit fiddly but it's not a huge amount to change but it, and it just kind of works. [26:04.160 --> 26:14.000] So in our, in our, in our examples here I just kind of copied the V-Express machine, just created a new machine called BCM4709 with modifications. [26:16.580 --> 26:17.860] And yeah. [26:17.860 --> 26:24.920] So it just means we can add, like pass another net flag to KMU and we just get another, like a second network interface showing up in the guest. [26:24.920 --> 26:29.580] We can do this as many times as we want but like two or three is probably the limit for what you need. [26:30.740 --> 26:32.100] So pre-init's happy. [26:32.100 --> 26:33.180] It's got two NICs. [26:33.180 --> 26:34.400] We get different errors. [26:34.400 --> 26:37.740] So we start to see more errors related to the JFFS2 directory. [26:37.840 --> 26:39.280] This doesn't seem to exist. [26:39.400 --> 26:42.300] There's no JFFS2 partition on the firmware image. [26:42.400 --> 26:43.680] We can base it off. [26:43.680 --> 26:47.120] So of the, of the firmware image downloaded from the ASUS site. [26:47.120 --> 26:50.560] So assumedly there's something that's created or mounted at boot. [26:50.980 --> 26:55.780] To figure out what it is, we just have to go back to pre-init and see what's going on. [26:56.620 --> 27:03.800] Pre-init tries to mount certain partitions from the flash to the JFFS and ASUS JFFS directories. [27:03.960 --> 27:19.160] It checks the proc MTD file to find partitions with names BRCM NAND and ASUS and then it just mounts those partitions as JFFS to those particular directories mentioned earlier. [27:19.860 --> 27:23.260] And we can fix this just by modifying our flash again. [27:23.260 --> 27:25.480] And just changing the boot ARGs again. [27:26.340 --> 27:28.000] We modify the flash layout. [27:28.000 --> 27:32.920] We make sure we tell the kernel which parts of the flash are called BRCM NAND and ASUS. [27:32.920 --> 27:42.300] And that means when we boot the MTD file will contain those names that pre-init's expecting and pre-init will try to load those particular MTD parts as JFFS file system. [27:42.560 --> 27:43.860] And this just works. [27:43.860 --> 27:45.740] So the JFFS directories are created. [27:45.740 --> 27:46.940] The files are getting written there. [27:46.940 --> 27:49.720] Things like log files, sys log, certificates. [27:49.860 --> 27:53.800] But it makes booting a lot smoother and a lot less stuff fails. [27:56.260 --> 28:00.500] We're pretty far into the, into the init process but we get a lot of errors out on the terminal. [28:00.500 --> 28:04.520] They're not like functionality blockers but they kind of pollute the terminal a lot . [28:04.860 --> 28:08.460] In particular we get a lot of like dev GPIO related errors. [28:08.840 --> 28:13.860] So to quieten down the machine we also create like a dummy GPIO interface. [28:13.860 --> 28:15.500] A bit like the NVRAM one. [28:17.160 --> 28:23.180] A lot like the NVRAM approach we look at user space binaries for GPIO interactions and see what's going on. [28:23.180 --> 28:35.640] The binaries always just call, make an octal call on the open dev GPIO file with just specific operation codes and like a bit map of the pin that they're trying to read or write to. [28:35.640 --> 28:37.920] That's being polled or poked basically. [28:39.340 --> 28:43.820] With GPIO we need to know what the active low or the active high state is for each. [28:43.820 --> 28:47.520] So luckily there's some device true files we can look at to reference that. [28:47.600 --> 28:51.500] And then we can use that to determine which pins are expecting what. [28:51.860 --> 28:54.780] And we just return a kind of neutral state back on them. [28:55.080 --> 28:59.640] So it's a little bit trial and error but it's not a huge overhead. [29:02.890 --> 29:09.430] And yeah, once we, once we get rid of the GPIO errors we get a few more kind of network interface issues. [29:10.010 --> 29:17.570] We gave the system a couple of NICs but the driver for the NIC that KMU emulates doesn't actually behave how user space is emulating. [29:18.070 --> 29:25.570] So the KMU machine is expecting the SMSC911X and the firmware is expecting some Broadcom device of some kind. [29:25.990 --> 29:36.710] So the specific point in pre-init that's actually failing is a function called get file status which is checking the physical state of the interface by trying to read a register somewhere. [29:37.170 --> 29:48.710] It does this again by making an IOPTL call to a socket handle but it just fails because the driver code for the SMSC911X doesn't know what to do with that particular code. [29:49.710 --> 29:53.910] This isn't really a huge blocker because we don't need the solution to be kind of perfect or complete. [29:53.910 --> 29:58.230] We're not trying to implement networking here but we just do a little bit of fraud in the kernel. [29:58.570 --> 30:06.930] We modify the SMSC911X driver to intercept that particular operation code and then we just return whatever user space is expecting again. [30:06.990 --> 30:09.910] Just to say, yeah it's physically up. [30:10.210 --> 30:14.550] We just spam all the bits back like hi for this one in particular. [30:16.090 --> 30:18.470] And yeah, at that point we're doing pretty well. [30:18.470 --> 30:29.090] So we've got a system with a specific flash layout, fake MV RAM device, fake GPIO, slightly modified network driver and it mainly works. [30:29.090 --> 30:38.310] The main thing, the final thing we kind of need to do to get it working, to be able to connect into it is to have a kind of LAN interface attachment. [30:38.310 --> 30:41.190] And this is a solution we found for this. [30:41.650 --> 30:52.830] So on the A7 you pre-init creates this like bridge device which takes the kind of LAN IP address which is 192.168.1.1. [30:53.670 --> 30:57.690] F1 is one of the LAN interfaces for that. [30:58.190 --> 31:03.970] So we can try to connect to whatever is on BR0 through F1. [31:03.970 --> 31:13.690] And the cleanest way that we found to do this was to kind of create a tap on the host with a static IP in the same slash 24 range as the router and just make sure KMU knows how to use it . [31:13.690 --> 31:18.070] So once you have that you can connect to the LAN interface of the device just like we would on the device itself. [31:19.490 --> 31:22.730] And yeah, demo time. [31:22.730 --> 31:25.370] Let me just try and get these demos, these demos . [31:27.170 --> 31:31.430] Hold on. [31:31.430 --> 31:32.090] Yeah. [31:32.550 --> 31:41.830] So this is running the ASUS router here in the emulated one just on the right top right. [31:42.490 --> 31:46.070] It takes a little minute to warm up. [31:46.250 --> 31:47.930] So the video is a little bit long. [31:48.210 --> 31:49.590] So apologies for that. [31:50.590 --> 32:01.310] But you should see there I'm dropped into a shell on the kind of emulated, on the emulated AC87U there. [32:01.310 --> 32:04.770] And I've just SSHed into it in the bottom tunnel there. [32:04.770 --> 32:07.590] You can see all the processors are running in that. [32:08.970 --> 32:10.370] A lot of them. [32:10.450 --> 32:13.490] So assumedly the in it process kind of worked. [32:13.490 --> 32:16.550] It got the system into a decent enough state to run stuff. [32:16.550 --> 32:20.350] We've got a few network interfaces there as well. [32:21.210 --> 32:26.730] And we've got a whole bunch of services running and a whole bunch of sockets open. [32:27.070 --> 32:32.530] The interesting one there I guess is the web management interface. [32:32.530 --> 32:35.330] A lot of the time this is the kind of high value one. [32:35.810 --> 32:42.850] So I hope this is the right video and shows us the web management interface loading up. [32:44.490 --> 32:47.190] It sometimes takes a little bit to get started. [32:47.190 --> 32:48.350] But yeah, here we go. [32:48.570 --> 32:50.550] So we've got the web management interface started. [32:51.670 --> 32:53.390] We can log into it. [32:53.630 --> 32:55.930] This one in particular had already been set up. [32:56.010 --> 33:01.330] The set up process is a bit boring so I didn't kind of, I didn't keep that as a video. [33:01.330 --> 33:02.390] But it's already been set up. [33:02.390 --> 33:07.590] And the web management interface works almost entirely in this particular one. [33:13.760 --> 33:16.340] And yeah, that's it. [33:22.790 --> 33:27.410] So yeah, all the interesting services kind of work including the web management stuff. [33:27.410 --> 33:29.870] You can configure SSH to run on boot. [33:29.870 --> 33:38.750] You can change the NVRAM and you can do these things on the flash file itself or you can do it from inside the device itself, inside the emulator device itself. [33:38.990 --> 33:47.110] In this particular one there's, in the AC87U there's a strange kind of parallel quantenna sock on there where it's expected to be on there which we didn't emulate. [33:47.110 --> 33:59.010] But yeah, now we get a pretty decent like 95 percent functioning like emulation for this device with not a huge amount of layout on it. [33:59.030 --> 34:00.850] And we can do it for other devices as well. [34:00.850 --> 34:06.570] So other ASUS devices running the BCM4709 work basically immediately. [34:06.970 --> 34:09.650] Netgear devices are quite quick to get running. [34:09.650 --> 34:18.510] They're expecting another specific device to exist but as long as it exists in most cases it just kind of works. [34:18.650 --> 34:23.450] Tender devices work as well but they use a different kind of NVRAM. [34:24.450 --> 34:36.530] They handle all their own read writes and their NVRAM is called like CFM and it's just a partition in flash as well which just has like a CRC32 at the beginning. [34:36.530 --> 34:42.630] But other than that it's just, it's essentially just key store, key value store. [34:42.790 --> 34:49.810] Some devices are a bit more tricky, I guess partially because of the way the images are distributed. [34:49.810 --> 34:53.130] So we might not have everything tied up with a nice bow. [34:53.470 --> 35:01.230] Others expect like a really precise flash layout so it's more precision than just building a fake flash blob. [35:01.850 --> 35:06.750] And there's always like a little ramp up period in like going from like blank NVRAM to a running system. [35:06.750 --> 35:11.190] So there's a little bit of like the process of what breaks and why going on. [35:11.190 --> 35:15.830] But in general the baseline principles are pretty much the same when you're doing all of them. [35:16.450 --> 35:23.070] For a lot of these, getting devices from one particular vendor up and running might take a day. [35:24.750 --> 35:32.050] And once you've got like one vendor kind of covered, it can maybe take an hour or so to get another device in that family up and running. [35:32.450 --> 35:36.990] And I'll show you a few more if they work. [35:38.090 --> 36:05.070] Yeah, this is a D-Link device, same configuration, same setup, also just the exact same emulation environment, just a different firmware, just a web management stuff is running in theory so we can have a look at that. [36:05.830 --> 36:09.270] And yeah, there we go, there's the D-Link interface there. [36:10.550 --> 36:13.850] You can poke around in that and have a lot of fun with that. [36:15.410 --> 36:17.990] Another one now, this is the Netgear. [36:18.050 --> 36:25.430] Again, with the Netgear one we can just, I think I sped up this one so we didn't have to wait around for the whole thing to warm up. [36:25.770 --> 36:36.130] But once it's configured running, there's a few limitations like for the Netgear one, it's got this ping that it's trying to do before it will load the web interface. [36:36.130 --> 36:42.930] But other than that, most of it works here as well, which is quite gratifying. [36:43.130 --> 36:52.610] And then finally there's just one more example, there's a Chinese vendor called Ficom that I have never really heard of before. [36:53.010 --> 37:00.130] But we've got one of those devices emulating as well, just because it's based on the same SOC. [37:00.150 --> 37:06.130] So we have all of this potential based on this one emulation environment. [37:15.670 --> 37:20.650] So yeah, conclusions, it's still early days proof of concept but it works. [37:20.650 --> 37:22.510] It's relatively light touch. [37:24.370 --> 37:27.910] Compiling support with these dummy peripherals is pretty smooth. [37:27.910 --> 37:31.950] It's not totally fool proof and not everything is going to be working the whole time. [37:32.630 --> 37:35.830] But just to get things running, it does seem to work. [37:35.830 --> 37:39.570] We can tweak KMU machines when we need to, we can make changes when we need to. [37:43.110 --> 37:48.190] We can take these same principles, we can move them to other SOCs as well. [37:48.530 --> 37:52.070] We've started porting the same principles to other SOCs with some success. [37:52.530 --> 37:55.690] Different SOCs have different profiles, the results may vary. [37:57.410 --> 38:08.750] It can take a bit of effort in the first instance to create these environments but the return on investment can be quite high because you've got maybe tens or a hundred or so different devices you can emulate based off them. [38:10.630 --> 38:11.910] Thanks for listening. [38:11.990 --> 38:15.670] I'll stick around for a chat down here afterwards if anyone wants to have a chat. [38:15.670 --> 38:20.850] Feel free to get in touch on my massively neglected Blue Sky account if you've got any questions. [38:20.930 --> 38:21.570] Thanks.