1 00:00:00,000 --> 00:00:01,140 Okay, hello. 2 00:00:01,140 --> 00:00:08,620 Welcome to this talk, Emulating Embedded Linux Devices at Scale with Light Touch Firmware Rehosting. 3 00:00:08,880 --> 00: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. 4 00:00:28,080 --> 00: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. 5 00:00:36,620 --> 00: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. 6 00:00:44,840 --> 00: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. 7 00:01:13,920 --> 00: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. 8 00:01:20,420 --> 00:01:25,500 There are some cases where it gets pretty tricky, but in a lot of cases, it's totally fine to do. 9 00:01:25,760 --> 00: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. 10 00:01:42,380 --> 00:01:45,120 So NV RAM is the classic example. 11 00:01:45,840 --> 00: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. 12 00:01:57,580 --> 00:02:07,820 But there's certain cases where we can't really address these kind of incompatibilities just by LD preloading stuff. 13 00:02:07,820 --> 00:02:15,260 When the systems are designed to work more holistically with a lot more moving parts. 14 00:02:18,040 --> 00:02:23,400 So the traditional kind of way of doing things in user space doesn't really work for those kind of approaches. 15 00:02:24,360 --> 00:02:28,040 So our question was, can we try and do it in a different way? 16 00:02:28,860 --> 00:02:32,200 We want to do vulnerability research on these systems. 17 00:02:32,380 --> 00:02:34,760 We just want to do things like run honey pots. 18 00:02:35,280 --> 00:02:37,540 So we want something like a full system emulation. 19 00:02:37,540 --> 00:02:42,220 We want the whole thing to be running as it usually would on the device. 20 00:02:43,260 --> 00: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. 21 00:02:53,120 --> 00:02:59,640 So we want to see if there's another approach that we could try and try a few different things. 22 00:02:59,640 --> 00:03:02,060 And we came to this. 23 00:03:02,060 --> 00:03:04,360 This kind of re-hosting concept. 24 00:03:04,920 --> 00:03:08,520 The main idea of this is we don't touch user space at all. 25 00:03:08,520 --> 00:03:10,960 We don't want to do anything in user space. 26 00:03:10,960 --> 00:03:12,460 We don't want to LD preload anything. 27 00:03:12,460 --> 00:03:15,920 We don't want to do any hacks during the boot process. 28 00:03:16,140 --> 00:03:20,920 We just want to offer up a firmware image to the re-hosting environment. 29 00:03:21,240 --> 00:03:25,600 Ideally the exact same firmware image that's bundled in a firmware update image. 30 00:03:25,840 --> 00:03:27,660 And for it just to run. 31 00:03:29,740 --> 00:03:33,280 User space can only kind of interact with what the kernel gives it. 32 00:03:34,180 --> 00: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. 33 00:03:41,280 --> 00:03:45,620 But we just kind of create an environment. 34 00:03:45,680 --> 00:03:47,460 Create this re-hosting environment for it. 35 00:03:48,240 --> 00:03:53,720 So the idea is we would build a kernel for a particular KME machine with a compatible architecture. 36 00:03:54,020 --> 00: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. 37 00:04:01,960 --> 00:04:08,240 And then we can just tweak the kernel, tweak the KME machine, like add peripherals, add hardware whenever we want. 38 00:04:08,420 --> 00:04:10,140 Or whenever we need to. 39 00:04:11,060 --> 00:04:14,320 The question is then like where do we start with something like this? 40 00:04:16,220 --> 00:04:19,720 All embedded systems are running the Linux kernel. 41 00:04:19,720 --> 00:04:25,220 But they're running a Linux kernel built for a particular system on chip or sock. 42 00:04:27,260 --> 00:04:31,160 A sock is just a bundle of CPU memory, other peripherals, things like that. 43 00:04:31,240 --> 00:04:32,720 Networking, storage. 44 00:04:32,720 --> 00:04:34,620 It depends on the sock. 45 00:04:35,460 --> 00:04:38,500 Some peripherals might be integrated directly into the sock. 46 00:04:38,900 --> 00: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. 47 00:04:46,320 --> 00:04:51,120 But each sock is designed by a hardware to cater to a particular market. 48 00:04:51,120 --> 00:04:56,240 So a specific sock will get used in lots of different embedded systems, sold by lots of different vendors. 49 00:04:56,560 --> 00:05:03,060 So Netgear, Asus, TP Link, they don't buy, they don't build the sock themselves. 50 00:05:03,060 --> 00:05:04,320 They're not chip companies. 51 00:05:04,320 --> 00:05:07,560 They buy a sock from a manufacturer and then use them in their devices. 52 00:05:08,340 --> 00:05:13,820 And so the same sock is used across multiple devices by multiple different vendors. 53 00:05:14,940 --> 00:05:17,040 So the supply chain looks like this. 54 00:05:17,630 --> 00:05:23,580 The sock manufacturer makes the socks, so like Broadcom, Qualcomm, Media Tech, all these kinds of companies. 55 00:05:23,940 --> 00: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. 56 00:05:32,920 --> 00:05:36,620 The vendor will use that SDK to build the kernel and file system binaries. 57 00:05:38,420 --> 00:05:43,700 So it kind of means that any code running close to the hardware is usually manufacturer provided. 58 00:05:43,880 --> 00:05:46,500 Like vendors won't really have much to do with that. 59 00:05:46,760 --> 00:05:49,800 And the vendors can do whatever they want in user space. 60 00:05:49,800 --> 00:05:56,360 They just use their like APIs and libraries or whatever provided by the manufacturer as part of the SDK as well. 61 00:05:58,620 --> 00:06:00,480 Well, what does that mean for us? 62 00:06:00,480 --> 00:06:03,000 Well, we're only trying to make changes to the kernel. 63 00:06:03,780 --> 00: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. 64 00:06:17,800 --> 00:06:22,680 We're not trying to emulate the hardware of the sock itself and try and run a prebuilt kernel on it. 65 00:06:22,900 --> 00:06:24,140 We did try that. 66 00:06:24,140 --> 00:06:25,820 It's a bit tricky. 67 00:06:27,180 --> 00:06:33,700 So we want to just build a kernel and sort of tweak a KMU machine if we need to. 68 00:06:33,700 --> 00:06:41,460 So that from the perspective of user space, user space thinks it's running on that particular sock kernel combo. 69 00:06:41,940 --> 00: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. 70 00:06:49,140 --> 00:06:54,520 So we could build one rehosting environment and get a few dozen possible emulated devices out of it. 71 00:06:56,720 --> 00:07:00,040 We've got a lot of things we can kind of lean on to try and do this. 72 00:07:00,040 --> 00:07:06,280 We don't obviously have data sheets from the manufacturers for these socks. 73 00:07:06,280 --> 00:07:08,320 We don't have kind of proprietary information. 74 00:07:08,460 --> 00:07:13,340 We do have KMU machines which are probably good enough for getting started. 75 00:07:13,340 --> 00:07:14,760 We have the Linux kernel. 76 00:07:14,800 --> 00:07:17,880 We have firmware images for devices that we want to emulate. 77 00:07:17,880 --> 00:07:19,860 We have reverse engineering tools. 78 00:07:20,080 --> 00:07:22,880 We have GPL code in a lot of cases. 79 00:07:23,320 --> 00:07:25,300 We have compiled kernels. 80 00:07:25,300 --> 00:07:29,100 We have kernel modules that come with the firmware images. 81 00:07:29,120 --> 00: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. 82 00:07:40,240 --> 00:07:41,920 They kind of do the opposite of what we're trying to do. 83 00:07:41,920 --> 00:07:45,740 They're trying to run on the actual hardware and we're trying to not run on the actual hardware. 84 00:07:45,740 --> 00:07:47,220 But they're kind of similar. 85 00:07:47,220 --> 00:07:50,420 We have kind of similar but opposite intentions. 86 00:07:51,420 --> 00:07:53,140 We don't have data sheets. 87 00:07:53,140 --> 00:07:56,540 We don't have really that much source code for binary blobs. 88 00:07:56,700 --> 00:07:58,100 We don't have documentation. 89 00:07:58,100 --> 00:08:02,900 We just don't have unlimited time and energy or unlimited money. 90 00:08:02,900 --> 00:08:06,660 We're just trying to get to a point where the thing runs well enough. 91 00:08:08,360 --> 00:08:10,220 So this is the vague plan. 92 00:08:10,220 --> 00: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. 93 00:08:18,260 --> 00:08:22,080 And then we compile that to run on the most compatible KMU machine we can find. 94 00:08:23,000 --> 00:08:26,560 And then on that machine we load the file system and just try and run stuff. 95 00:08:26,560 --> 00:08:29,520 And we see what breaks and we try and figure it out. 96 00:08:30,060 --> 00:08:35,320 And then we patch the kernel again and maybe make modifications to the KMU machine. 97 00:08:35,320 --> 00:08:39,600 And we just keep doing this in this kind of twisted development life cycle. 98 00:08:40,780 --> 00: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. 99 00:08:50,080 --> 00:08:57,060 And then we can make changes reflectively in the kernel to cover that. 100 00:08:58,060 --> 00:08:59,940 So this was the device we chose to do this. 101 00:08:59,940 --> 00:09:01,540 It's an older Asus router. 102 00:09:02,100 --> 00:09:05,400 It's based, it's the AC87U. 103 00:09:05,800 --> 00:09:07,640 It was released in 2014. 104 00:09:07,660 --> 00:09:10,000 It went end of life probably 2021. 105 00:09:10,240 --> 00:09:14,480 It's based around the BCM4709, which was your Broadcom sock. 106 00:09:15,980 --> 00:09:20,500 And the 4709 is used in quite a few routers. 107 00:09:20,600 --> 00:09:23,660 It's a router sock with a Cortex A9 core. 108 00:09:23,660 --> 00:09:29,880 It's in the broader BCM4708, 4707, 4708, 4709 family. 109 00:09:30,040 --> 00:09:32,580 It has an integrated ethernet stack. 110 00:09:33,260 --> 00:09:38,620 It was built for the router market so it gets used in routers and extenders and things like that. 111 00:09:38,620 --> 00:09:40,380 The list up here is kind of non-exhaustive. 112 00:09:40,380 --> 00: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. 113 00:09:56,160 --> 00:09:59,560 So the AC87U was kind of chosen at random. 114 00:10:00,680 --> 00: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. 115 00:10:07,820 --> 00: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. 116 00:10:16,460 --> 00:10:19,580 I should also say we didn't actually have the physical device for this at all. 117 00:10:19,580 --> 00:10:21,700 We worked only with the firmware image. 118 00:10:21,800 --> 00:10:24,720 So there's also a bit of an artificial restriction. 119 00:10:25,800 --> 00:10:28,260 But I kind of just wanted to see if it could be done. 120 00:10:28,260 --> 00: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. 121 00:10:38,340 --> 00:10:42,200 But our intention longer term is to emulate devices at scale. 122 00:10:42,200 --> 00:10:45,320 So we don't necessarily want to buy every single device that we want to emulate. 123 00:10:45,320 --> 00:10:47,660 So it's a bit of an experiment in this as well. 124 00:10:49,480 --> 00:10:54,240 The first step for this we can just get the firmware. 125 00:10:54,240 --> 00:10:56,400 And we get whatever is publicly available. 126 00:10:56,760 --> 00:11:00,900 We can get the most recent firmware from the Asus website from 2021. 127 00:11:01,140 --> 00:11:05,800 Get the GPL code which has some like Broadcom commented code in there as well. 128 00:11:06,540 --> 00:11:11,460 The firmware image is in this TRX format which is like an older Broadcom firmware file format. 129 00:11:11,460 --> 00:11:14,540 We can unpack these like really easily with Unblob. 130 00:11:14,780 --> 00:11:18,860 And using Unblob also means we can carve out the specific sections of the firmware. 131 00:11:18,920 --> 00:11:22,140 And we don't have to do any post processing stuff like we'd have to do with Binwalk. 132 00:11:22,140 --> 00:11:25,680 So you can carve everything out very precisely with Unblob. 133 00:11:27,240 --> 00:11:32,080 Once it's unpacked, so we get this Lisma compressed kernel and we get the SquashFS file system. 134 00:11:35,150 --> 00:11:39,990 We can, with this unpacked firmware we can kind of get a sense of what the device is actually running. 135 00:11:41,070 --> 00:11:44,170 What it's running on. 136 00:11:45,010 --> 00:11:46,470 And so what we need to emulate. 137 00:11:46,470 --> 00:11:54,410 So we get the kernel version out, a string out of the kernel which is 2.6.36.4 BRCM arm. 138 00:11:55,330 --> 00:11:59,830 It's this Linux 2.6 with some Broadcom modifications I guess. 139 00:11:59,970 --> 00:12:07,050 It's compiled with GCC 4.3, 4.5.3 from build root 2012.2. 140 00:12:08,550 --> 00:12:10,390 So we know what it's compiled with as well. 141 00:12:10,390 --> 00: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. 142 00:12:16,530 --> 00:12:18,550 We can also get the boot args out of the kernel blob. 143 00:12:18,550 --> 00:12:21,030 So this can give us an idea of how it's going to boot. 144 00:12:21,270 --> 00: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. 145 00:12:28,110 --> 00:12:31,790 So the root boot arg points to MTD block 2. 146 00:12:31,790 --> 00:12:34,390 So it's booting from this particular part in flash. 147 00:12:35,290 --> 00:12:38,670 The init boot arg points to S bin pre init. 148 00:12:39,770 --> 00: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. 149 00:12:46,570 --> 00:12:48,890 There's no init scripts or anything like this. 150 00:12:48,890 --> 00:12:52,390 It's just this RC binary being run as pre init. 151 00:12:54,110 --> 00:12:56,210 A lot of other binaries linked to RC. 152 00:12:56,210 --> 00:12:58,870 It's a pretty big sort of system functionality binary. 153 00:12:59,550 --> 00:13:00,710 And it checks how it's invoked. 154 00:13:00,710 --> 00:13:05,290 So it will run in this pre init mode when it's run as pre init. 155 00:13:05,330 --> 00:13:08,110 Or in another mode if it's run with a different name. 156 00:13:09,350 --> 00:13:14,030 The layout of the unpack file system tells us a lot how the system is expected to run. 157 00:13:14,630 --> 00:13:21,690 Kind of directions we'd usually expect to see useful stuff in populated or actually just broken sim links to temp. 158 00:13:21,710 --> 00:13:26,790 So temp is probably populated at boot time, probably by the init process, probably by pre init in particular. 159 00:13:28,230 --> 00: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. 160 00:13:39,050 --> 00: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. 161 00:13:44,310 --> 00: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. 162 00:13:49,570 --> 00:13:54,850 We just want to make sure that the init process is capable of kind of completing. 163 00:13:56,070 --> 00:14:00,630 So this is, we can apply this same kind of life cycle that we had before to this setup. 164 00:14:01,010 --> 00:14:08,750 We know the architecture, kernel version, compiler, the way the file system has been loaded from memory, from flash sorry. 165 00:14:09,970 --> 00: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. 166 00:14:18,650 --> 00:14:23,450 Build a 2.6.36.4 kernel for it. 167 00:14:23,910 --> 00:14:27,570 Attach the squash FS image which we carved out earlier. 168 00:14:30,070 --> 00: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. 169 00:14:38,590 --> 00:14:40,830 And then try and fix it and make it better. 170 00:14:42,170 --> 00: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. 171 00:14:50,750 --> 00: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. 172 00:14:56,690 --> 00:15:00,430 But just for the start, just for starting this is not a huge blocker. 173 00:15:00,870 --> 00: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. 174 00:15:14,950 --> 00: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. 175 00:15:25,710 --> 00:15:32,790 Getting that old version of GCC compiled with old build root can be a bit of a hassle. 176 00:15:32,790 --> 00: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. 177 00:15:41,230 --> 00:15:44,930 But it does get built and it does work. 178 00:15:47,510 --> 00:15:53,850 Once the kernel is compiled and running, it kind of works pretty well. 179 00:15:53,910 --> 00:16:00,230 The kernel loads into the V express machine. 180 00:16:01,150 --> 00:16:03,410 You do need to edit the kernel config a little bit. 181 00:16:03,410 --> 00: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. 182 00:16:12,590 --> 00:16:16,930 You also need to kind of think about things that you might want to compile into the kernel. 183 00:16:16,930 --> 00:16:19,410 Because we're not going to be able to load anything later. 184 00:16:19,410 --> 00:16:21,850 Like user space is not going to be able to load any kernel modules. 185 00:16:21,850 --> 00: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. 186 00:16:26,870 --> 00:16:29,570 As long as it's not incompatible with it with each other. 187 00:16:29,910 --> 00:16:34,790 And it kind of makes life a little bit easier later on. 188 00:16:35,850 --> 00:16:39,850 The first thing we have to do is figure out how to present the file system image to the kernel. 189 00:16:40,050 --> 00:16:42,450 So we need to forge some kind of flash. 190 00:16:42,870 --> 00:16:47,030 The flash needs to be laid out in a super predictable way. 191 00:16:47,870 --> 00:16:55,410 But because we control the boot and the kernel, we can pass the MTD parts boot ARG to the kernel. 192 00:16:55,410 --> 00:17:00,550 Which will tell it what our flash layout is and where to look for certain things. 193 00:17:00,550 --> 00:17:05,110 So we can still use MTD block 2 as the place we're going to keep our file system. 194 00:17:05,150 --> 00: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. 195 00:17:12,970 --> 00:17:14,850 So the flash layout can look something like this. 196 00:17:15,290 --> 00:17:19,050 We have a couple of imaginary empty parts, then the squash FS file system. 197 00:17:19,170 --> 00: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. 198 00:17:28,130 --> 00: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. 199 00:17:34,290 --> 00: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. 200 00:17:44,830 --> 00:17:47,510 But we do get a relatively decent error. 201 00:17:47,550 --> 00: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. 202 00:17:52,530 --> 00: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. 203 00:18:00,810 --> 00:18:08,970 And the main problem is that the squash FS image that ships for the AC87U is compressed with EXED. 204 00:18:09,590 --> 00: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. 205 00:18:19,510 --> 00:18:23,290 So Broadcom obviously does something a bit different. 206 00:18:23,870 --> 00:18:27,350 And we can figure out what they're doing by looking at some of the GPL code. 207 00:18:27,810 --> 00: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. 208 00:18:33,890 --> 00:18:39,490 So it's a slightly modified version, kind of back ported EXED support. 209 00:18:40,050 --> 00: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. 210 00:18:49,390 --> 00:18:50,690 And it works. 211 00:18:51,690 --> 00:18:53,570 The file system unpacks. 212 00:18:54,350 --> 00: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. 213 00:19:06,870 --> 00:19:12,710 The first thing we do is just pass BNSH as the init command. 214 00:19:13,690 --> 00:19:16,350 And we can just drop into a shell and poke around a bit . 215 00:19:16,350 --> 00: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. 216 00:19:21,490 --> 00:19:25,670 But of course nothing actually interesting runs because the system hasn't been initialized. 217 00:19:25,810 --> 00:19:28,370 The file system is mounted but nothing has actually run. 218 00:19:28,850 --> 00:19:33,810 We know that preinit is responsible for the initialization. 219 00:19:33,890 --> 00:19:36,230 So let's have a little quick look at that. 220 00:19:38,030 --> 00: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. 221 00:19:52,790 --> 00: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. 222 00:20:00,150 --> 00: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. 223 00:20:08,610 --> 00:20:11,870 So we actually need it to be the init process in order for it to do stuff. 224 00:20:11,870 --> 00:20:18,810 Like it's a kind of orchestrating process in the system, in this kind of holistic architecture of the system. 225 00:20:19,050 --> 00:20:22,610 So we can't just invoke it from a shell and expect it to work. 226 00:20:25,410 --> 00:20:33,550 We can just start the thing again with the init boot up pointing to preinit and see what fails basically. 227 00:20:33,550 --> 00:20:37,810 I mean the big one is we got a whole load of NVRAM errors. 228 00:20:38,690 --> 00:20:40,630 This is definitely expected. 229 00:20:40,630 --> 00:20:44,890 NVRAM is the most common blocker in embedded Linux emulation. 230 00:20:45,170 --> 00:20:48,630 So we need something to work to take it's place. 231 00:20:52,450 --> 00:20:56,250 We kind of need to make a plan here. 232 00:20:56,490 --> 00: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. 233 00:21:09,570 --> 00: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. 234 00:21:19,230 --> 00:21:23,830 So Brawl compromises these APIs to interact with NVRAM. 235 00:21:23,830 --> 00:21:32,270 In particular this lib NVRAM SO, which is kind of the official Broadcom lib NVRAM. 236 00:21:32,270 --> 00: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. 237 00:21:46,110 --> 00: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. 238 00:21:59,210 --> 00: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. 239 00:22:11,750 --> 00: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. 240 00:22:20,910 --> 00:22:23,430 And another buffer where the value should be written. 241 00:22:24,770 --> 00:22:30,730 NVRAM set calls write on the open file handle with a pointer to the key equals value string. 242 00:22:30,730 --> 00: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. 243 00:22:37,370 --> 00:22:39,950 So yeah, we build a character device in the kernel. 244 00:22:41,530 --> 00: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. 245 00:22:48,550 --> 00:22:51,010 So we can kind of just mirror what that is. 246 00:22:51,430 --> 00:22:53,550 We do need somewhere to persist it. 247 00:22:53,550 --> 00:22:57,690 So we can back it with just another partition in the flash memory. 248 00:22:58,450 --> 00:23:02,090 We make sure that dev NVRAM, the file is created. 249 00:23:02,090 --> 00:23:03,710 So user space can call open on it. 250 00:23:03,710 --> 00:23:08,870 And then we just handle all the read, write, IOPTL operations as well. 251 00:23:09,030 --> 00: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. 252 00:23:15,450 --> 00:23:18,210 We're just kind of servants to the user space in that way. 253 00:23:18,910 --> 00:23:21,850 And this is kind of a rough idea of the kind of code we have in the kernel. 254 00:23:21,850 --> 00:23:23,610 I'm sorry you probably can't see it here . 255 00:23:24,350 --> 00:23:28,750 But lib NVRAM is just doing super basic reads and writes. 256 00:23:29,230 --> 00: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. 257 00:23:37,810 --> 00: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. 258 00:23:44,230 --> 00:23:47,590 And if we get that, we return all the NVRAM key value pairs. 259 00:23:47,950 --> 00:23:51,050 So this is relatively, I have to say, quick to write. 260 00:23:51,690 --> 00:23:55,150 We can write it, compile it, try it, squash bugs, compile it. 261 00:23:55,150 --> 00:23:57,470 It's just kind of development life cycle stuff. 262 00:23:57,690 --> 00:24:00,690 And the approach just kind of works. 263 00:24:00,690 --> 00: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. 264 00:24:08,850 --> 00: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. 265 00:24:19,150 --> 00:24:23,450 And so it will probably work on other similar era devices that use the same mechanism as well. 266 00:24:27,870 --> 00:24:29,850 Alongside doing this, we're also trying to emulate the thing. 267 00:24:29,850 --> 00:24:32,470 So we have to solve the problem of what goes into the NVRAM. 268 00:24:32,730 --> 00: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. 269 00:24:39,010 --> 00:24:43,770 This isn't actually the case on the AC87U or any of the other ACES devices. 270 00:24:43,790 --> 00:24:47,530 There's a lot of logic which populates NVRAM with default values. 271 00:24:48,170 --> 00:24:56,330 And there's certain like NVRAM keys that you can set that will trigger a kind of full NVRAM reset. 272 00:24:57,010 --> 00:24:59,750 So there's no need to have an actual physical device for these devices. 273 00:24:59,750 --> 00:25:03,450 And we can kind of freestyle based off the defaults that pre-init creates. 274 00:25:03,990 --> 00:25:13,120 And yeah, once we have NVRAM, pre-init kind of works a bit better and gets a bit further. 275 00:25:13,120 --> 00:25:16,580 But we start hitting other issues that we weren't necessarily expecting. 276 00:25:16,580 --> 00:25:20,300 So pre-init needs to be there to be more than one network interface. 277 00:25:20,300 --> 00:25:22,880 It's looking for F1 but it can't find it. 278 00:25:23,120 --> 00: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. 279 00:25:31,380 --> 00:25:33,620 But we can quite easily do it. 280 00:25:33,920 --> 00: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. 281 00:25:47,980 --> 00:25:54,260 It's a relatively light, light touch strategy but, yeah. 282 00:25:58,580 --> 00: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. 283 00:26:04,160 --> 00: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. 284 00:26:16,580 --> 00:26:17,860 And yeah. 285 00:26:17,860 --> 00: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. 286 00:26:24,920 --> 00: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. 287 00:26:30,740 --> 00:26:32,100 So pre-init's happy. 288 00:26:32,100 --> 00:26:33,180 It's got two NICs. 289 00:26:33,180 --> 00:26:34,400 We get different errors. 290 00:26:34,400 --> 00:26:37,740 So we start to see more errors related to the JFFS2 directory. 291 00:26:37,840 --> 00:26:39,280 This doesn't seem to exist. 292 00:26:39,400 --> 00:26:42,300 There's no JFFS2 partition on the firmware image. 293 00:26:42,400 --> 00:26:43,680 We can base it off. 294 00:26:43,680 --> 00:26:47,120 So of the, of the firmware image downloaded from the ASUS site. 295 00:26:47,120 --> 00:26:50,560 So assumedly there's something that's created or mounted at boot. 296 00:26:50,980 --> 00:26:55,780 To figure out what it is, we just have to go back to pre-init and see what's going on. 297 00:26:56,620 --> 00:27:03,800 Pre-init tries to mount certain partitions from the flash to the JFFS and ASUS JFFS directories. 298 00:27:03,960 --> 00: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. 299 00:27:19,860 --> 00:27:23,260 And we can fix this just by modifying our flash again. 300 00:27:23,260 --> 00:27:25,480 And just changing the boot ARGs again. 301 00:27:26,340 --> 00:27:28,000 We modify the flash layout. 302 00:27:28,000 --> 00:27:32,920 We make sure we tell the kernel which parts of the flash are called BRCM NAND and ASUS. 303 00:27:32,920 --> 00: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. 304 00:27:42,560 --> 00:27:43,860 And this just works. 305 00:27:43,860 --> 00:27:45,740 So the JFFS directories are created. 306 00:27:45,740 --> 00:27:46,940 The files are getting written there. 307 00:27:46,940 --> 00:27:49,720 Things like log files, sys log, certificates. 308 00:27:49,860 --> 00:27:53,800 But it makes booting a lot smoother and a lot less stuff fails. 309 00:27:56,260 --> 00:28:00,500 We're pretty far into the, into the init process but we get a lot of errors out on the terminal. 310 00:28:00,500 --> 00:28:04,520 They're not like functionality blockers but they kind of pollute the terminal a lot . 311 00:28:04,860 --> 00:28:08,460 In particular we get a lot of like dev GPIO related errors. 312 00:28:08,840 --> 00:28:13,860 So to quieten down the machine we also create like a dummy GPIO interface. 313 00:28:13,860 --> 00:28:15,500 A bit like the NVRAM one. 314 00:28:17,160 --> 00:28:23,180 A lot like the NVRAM approach we look at user space binaries for GPIO interactions and see what's going on. 315 00:28:23,180 --> 00: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. 316 00:28:35,640 --> 00:28:37,920 That's being polled or poked basically. 317 00:28:39,340 --> 00:28:43,820 With GPIO we need to know what the active low or the active high state is for each. 318 00:28:43,820 --> 00:28:47,520 So luckily there's some device true files we can look at to reference that. 319 00:28:47,600 --> 00:28:51,500 And then we can use that to determine which pins are expecting what. 320 00:28:51,860 --> 00:28:54,780 And we just return a kind of neutral state back on them. 321 00:28:55,080 --> 00:28:59,640 So it's a little bit trial and error but it's not a huge overhead. 322 00:29:02,890 --> 00: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. 323 00:29:10,010 --> 00: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. 324 00:29:18,070 --> 00:29:25,570 So the KMU machine is expecting the SMSC911X and the firmware is expecting some Broadcom device of some kind. 325 00:29:25,990 --> 00: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. 326 00:29:37,170 --> 00: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. 327 00:29:49,710 --> 00:29:53,910 This isn't really a huge blocker because we don't need the solution to be kind of perfect or complete. 328 00:29:53,910 --> 00:29:58,230 We're not trying to implement networking here but we just do a little bit of fraud in the kernel. 329 00:29:58,570 --> 00: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. 330 00:30:06,990 --> 00:30:09,910 Just to say, yeah it's physically up. 331 00:30:10,210 --> 00:30:14,550 We just spam all the bits back like hi for this one in particular. 332 00:30:16,090 --> 00:30:18,470 And yeah, at that point we're doing pretty well. 333 00:30:18,470 --> 00: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. 334 00:30:29,090 --> 00: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. 335 00:30:38,310 --> 00:30:41,190 And this is a solution we found for this. 336 00:30:41,650 --> 00: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. 337 00:30:53,670 --> 00:30:57,690 F1 is one of the LAN interfaces for that. 338 00:30:58,190 --> 00:31:03,970 So we can try to connect to whatever is on BR0 through F1. 339 00:31:03,970 --> 00: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 . 340 00:31:13,690 --> 00: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. 341 00:31:19,490 --> 00:31:22,730 And yeah, demo time. 342 00:31:22,730 --> 00:31:25,370 Let me just try and get these demos, these demos . 343 00:31:27,170 --> 00:31:31,430 Hold on. 344 00:31:31,430 --> 00:31:32,090 Yeah. 345 00:31:32,550 --> 00:31:41,830 So this is running the ASUS router here in the emulated one just on the right top right. 346 00:31:42,490 --> 00:31:46,070 It takes a little minute to warm up. 347 00:31:46,250 --> 00:31:47,930 So the video is a little bit long. 348 00:31:48,210 --> 00:31:49,590 So apologies for that. 349 00:31:50,590 --> 00:32:01,310 But you should see there I'm dropped into a shell on the kind of emulated, on the emulated AC87U there. 350 00:32:01,310 --> 00:32:04,770 And I've just SSHed into it in the bottom tunnel there. 351 00:32:04,770 --> 00:32:07,590 You can see all the processors are running in that. 352 00:32:08,970 --> 00:32:10,370 A lot of them. 353 00:32:10,450 --> 00:32:13,490 So assumedly the in it process kind of worked. 354 00:32:13,490 --> 00:32:16,550 It got the system into a decent enough state to run stuff. 355 00:32:16,550 --> 00:32:20,350 We've got a few network interfaces there as well. 356 00:32:21,210 --> 00:32:26,730 And we've got a whole bunch of services running and a whole bunch of sockets open. 357 00:32:27,070 --> 00:32:32,530 The interesting one there I guess is the web management interface. 358 00:32:32,530 --> 00:32:35,330 A lot of the time this is the kind of high value one. 359 00:32:35,810 --> 00:32:42,850 So I hope this is the right video and shows us the web management interface loading up. 360 00:32:44,490 --> 00:32:47,190 It sometimes takes a little bit to get started. 361 00:32:47,190 --> 00:32:48,350 But yeah, here we go. 362 00:32:48,570 --> 00:32:50,550 So we've got the web management interface started. 363 00:32:51,670 --> 00:32:53,390 We can log into it. 364 00:32:53,630 --> 00:32:55,930 This one in particular had already been set up. 365 00:32:56,010 --> 00: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. 366 00:33:01,330 --> 00:33:02,390 But it's already been set up. 367 00:33:02,390 --> 00:33:07,590 And the web management interface works almost entirely in this particular one. 368 00:33:13,760 --> 00:33:16,340 And yeah, that's it. 369 00:33:22,790 --> 00:33:27,410 So yeah, all the interesting services kind of work including the web management stuff. 370 00:33:27,410 --> 00:33:29,870 You can configure SSH to run on boot. 371 00:33:29,870 --> 00: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. 372 00:33:38,990 --> 00: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. 373 00:33:47,110 --> 00: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. 374 00:33:59,030 --> 00:34:00,850 And we can do it for other devices as well. 375 00:34:00,850 --> 00:34:06,570 So other ASUS devices running the BCM4709 work basically immediately. 376 00:34:06,970 --> 00:34:09,650 Netgear devices are quite quick to get running. 377 00:34:09,650 --> 00: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. 378 00:34:18,650 --> 00:34:23,450 Tender devices work as well but they use a different kind of NVRAM. 379 00:34:24,450 --> 00: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. 380 00:34:36,530 --> 00:34:42,630 But other than that it's just, it's essentially just key store, key value store. 381 00:34:42,790 --> 00:34:49,810 Some devices are a bit more tricky, I guess partially because of the way the images are distributed. 382 00:34:49,810 --> 00:34:53,130 So we might not have everything tied up with a nice bow. 383 00:34:53,470 --> 00:35:01,230 Others expect like a really precise flash layout so it's more precision than just building a fake flash blob. 384 00:35:01,850 --> 00:35:06,750 And there's always like a little ramp up period in like going from like blank NVRAM to a running system. 385 00:35:06,750 --> 00:35:11,190 So there's a little bit of like the process of what breaks and why going on. 386 00:35:11,190 --> 00:35:15,830 But in general the baseline principles are pretty much the same when you're doing all of them. 387 00:35:16,450 --> 00:35:23,070 For a lot of these, getting devices from one particular vendor up and running might take a day. 388 00:35:24,750 --> 00: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. 389 00:35:32,450 --> 00:35:36,990 And I'll show you a few more if they work. 390 00:35:38,090 --> 00: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. 391 00:36:05,830 --> 00:36:09,270 And yeah, there we go, there's the D-Link interface there. 392 00:36:10,550 --> 00:36:13,850 You can poke around in that and have a lot of fun with that. 393 00:36:15,410 --> 00:36:17,990 Another one now, this is the Netgear. 394 00:36:18,050 --> 00: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. 395 00:36:25,770 --> 00: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. 396 00:36:36,130 --> 00:36:42,930 But other than that, most of it works here as well, which is quite gratifying. 397 00:36:43,130 --> 00: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. 398 00:36:53,010 --> 00:37:00,130 But we've got one of those devices emulating as well, just because it's based on the same SOC. 399 00:37:00,150 --> 00:37:06,130 So we have all of this potential based on this one emulation environment. 400 00:37:15,670 --> 00:37:20,650 So yeah, conclusions, it's still early days proof of concept but it works. 401 00:37:20,650 --> 00:37:22,510 It's relatively light touch. 402 00:37:24,370 --> 00:37:27,910 Compiling support with these dummy peripherals is pretty smooth. 403 00:37:27,910 --> 00:37:31,950 It's not totally fool proof and not everything is going to be working the whole time. 404 00:37:32,630 --> 00:37:35,830 But just to get things running, it does seem to work. 405 00:37:35,830 --> 00:37:39,570 We can tweak KMU machines when we need to, we can make changes when we need to. 406 00:37:43,110 --> 00:37:48,190 We can take these same principles, we can move them to other SOCs as well. 407 00:37:48,530 --> 00:37:52,070 We've started porting the same principles to other SOCs with some success. 408 00:37:52,530 --> 00:37:55,690 Different SOCs have different profiles, the results may vary. 409 00:37:57,410 --> 00: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. 410 00:38:10,630 --> 00:38:11,910 Thanks for listening. 411 00:38:11,990 --> 00:38:15,670 I'll stick around for a chat down here afterwards if anyone wants to have a chat. 412 00:38:15,670 --> 00:38:20,850 Feel free to get in touch on my massively neglected Blue Sky account if you've got any questions. 413 00:38:20,930 --> 00:38:21,570 Thanks.