├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── NOTICE ├── README.md ├── build.sh ├── patch_system_img.sh └── su.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # the minimum version of CMake. 2 | cmake_minimum_required(VERSION 3.4.1) 3 | project(harmony_su) 4 | 5 | set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) 6 | 7 | if(DEFINED PACKAGE_FIND_FILE) 8 | include(${PACKAGE_FIND_FILE}) 9 | endif() 10 | 11 | include_directories(${NATIVERENDER_ROOT_PATH} 12 | ${NATIVERENDER_ROOT_PATH}/include) 13 | 14 | add_executable(su su.cpp) 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2005-2008, The Android Open Source Project 3 | Copyright (c) 2024, Shaoxing Wang 4 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); 6 | you may not use this file except in compliance with the License. 7 | 8 | Unless required by applicable law or agreed to in writing, software 9 | distributed under the License is distributed on an "AS IS" BASIS, 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | See the License for the specific language governing permissions and 12 | limitations under the License. 13 | 14 | 15 | Apache License 16 | Version 2.0, January 2004 17 | http://www.apache.org/licenses/ 18 | 19 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 20 | 21 | 1. Definitions. 22 | 23 | "License" shall mean the terms and conditions for use, reproduction, 24 | and distribution as defined by Sections 1 through 9 of this document. 25 | 26 | "Licensor" shall mean the copyright owner or entity authorized by 27 | the copyright owner that is granting the License. 28 | 29 | "Legal Entity" shall mean the union of the acting entity and all 30 | other entities that control, are controlled by, or are under common 31 | control with that entity. For the purposes of this definition, 32 | "control" means (i) the power, direct or indirect, to cause the 33 | direction or management of such entity, whether by contract or 34 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 35 | outstanding shares, or (iii) beneficial ownership of such entity. 36 | 37 | "You" (or "Your") shall mean an individual or Legal Entity 38 | exercising permissions granted by this License. 39 | 40 | "Source" form shall mean the preferred form for making modifications, 41 | including but not limited to software source code, documentation 42 | source, and configuration files. 43 | 44 | "Object" form shall mean any form resulting from mechanical 45 | transformation or translation of a Source form, including but 46 | not limited to compiled object code, generated documentation, 47 | and conversions to other media types. 48 | 49 | "Work" shall mean the work of authorship, whether in Source or 50 | Object form, made available under the License, as indicated by a 51 | copyright notice that is included in or attached to the work 52 | (an example is provided in the Appendix below). 53 | 54 | "Derivative Works" shall mean any work, whether in Source or Object 55 | form, that is based on (or derived from) the Work and for which the 56 | editorial revisions, annotations, elaborations, or other modifications 57 | represent, as a whole, an original work of authorship. For the purposes 58 | of this License, Derivative Works shall not include works that remain 59 | separable from, or merely link (or bind by name) to the interfaces of, 60 | the Work and Derivative Works thereof. 61 | 62 | "Contribution" shall mean any work of authorship, including 63 | the original version of the Work and any modifications or additions 64 | to that Work or Derivative Works thereof, that is intentionally 65 | submitted to Licensor for inclusion in the Work by the copyright owner 66 | or by an individual or Legal Entity authorized to submit on behalf of 67 | the copyright owner. For the purposes of this definition, "submitted" 68 | means any form of electronic, verbal, or written communication sent 69 | to the Licensor or its representatives, including but not limited to 70 | communication on electronic mailing lists, source code control systems, 71 | and issue tracking systems that are managed by, or on behalf of, the 72 | Licensor for the purpose of discussing and improving the Work, but 73 | excluding communication that is conspicuously marked or otherwise 74 | designated in writing by the copyright owner as "Not a Contribution." 75 | 76 | "Contributor" shall mean Licensor and any individual or Legal Entity 77 | on behalf of whom a Contribution has been received by Licensor and 78 | subsequently incorporated within the Work. 79 | 80 | 2. Grant of Copyright License. Subject to the terms and conditions of 81 | this License, each Contributor hereby grants to You a perpetual, 82 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 83 | copyright license to reproduce, prepare Derivative Works of, 84 | publicly display, publicly perform, sublicense, and distribute the 85 | Work and such Derivative Works in Source or Object form. 86 | 87 | 3. Grant of Patent License. Subject to the terms and conditions of 88 | this License, each Contributor hereby grants to You a perpetual, 89 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 90 | (except as stated in this section) patent license to make, have made, 91 | use, offer to sell, sell, import, and otherwise transfer the Work, 92 | where such license applies only to those patent claims licensable 93 | by such Contributor that are necessarily infringed by their 94 | Contribution(s) alone or by combination of their Contribution(s) 95 | with the Work to which such Contribution(s) was submitted. If You 96 | institute patent litigation against any entity (including a 97 | cross-claim or counterclaim in a lawsuit) alleging that the Work 98 | or a Contribution incorporated within the Work constitutes direct 99 | or contributory patent infringement, then any patent licenses 100 | granted to You under this License for that Work shall terminate 101 | as of the date such litigation is filed. 102 | 103 | 4. Redistribution. You may reproduce and distribute copies of the 104 | Work or Derivative Works thereof in any medium, with or without 105 | modifications, and in Source or Object form, provided that You 106 | meet the following conditions: 107 | 108 | (a) You must give any other recipients of the Work or 109 | Derivative Works a copy of this License; and 110 | 111 | (b) You must cause any modified files to carry prominent notices 112 | stating that You changed the files; and 113 | 114 | (c) You must retain, in the Source form of any Derivative Works 115 | that You distribute, all copyright, patent, trademark, and 116 | attribution notices from the Source form of the Work, 117 | excluding those notices that do not pertain to any part of 118 | the Derivative Works; and 119 | 120 | (d) If the Work includes a "NOTICE" text file as part of its 121 | distribution, then any Derivative Works that You distribute must 122 | include a readable copy of the attribution notices contained 123 | within such NOTICE file, excluding those notices that do not 124 | pertain to any part of the Derivative Works, in at least one 125 | of the following places: within a NOTICE text file distributed 126 | as part of the Derivative Works; within the Source form or 127 | documentation, if provided along with the Derivative Works; or, 128 | within a display generated by the Derivative Works, if and 129 | wherever such third-party notices normally appear. The contents 130 | of the NOTICE file are for informational purposes only and 131 | do not modify the License. You may add Your own attribution 132 | notices within Derivative Works that You distribute, alongside 133 | or as an addendum to the NOTICE text from the Work, provided 134 | that such additional attribution notices cannot be construed 135 | as modifying the License. 136 | 137 | You may add Your own copyright statement to Your modifications and 138 | may provide additional or different license terms and conditions 139 | for use, reproduction, or distribution of Your modifications, or 140 | for any such Derivative Works as a whole, provided Your use, 141 | reproduction, and distribution of the Work otherwise complies with 142 | the conditions stated in this License. 143 | 144 | 5. Submission of Contributions. Unless You explicitly state otherwise, 145 | any Contribution intentionally submitted for inclusion in the Work 146 | by You to the Licensor shall be under the terms and conditions of 147 | this License, without any additional terms or conditions. 148 | Notwithstanding the above, nothing herein shall supersede or modify 149 | the terms of any separate license agreement you may have executed 150 | with Licensor regarding such Contributions. 151 | 152 | 6. Trademarks. This License does not grant permission to use the trade 153 | names, trademarks, service marks, or product names of the Licensor, 154 | except as required for reasonable and customary use in describing the 155 | origin of the Work and reproducing the content of the NOTICE file. 156 | 157 | 7. Disclaimer of Warranty. Unless required by applicable law or 158 | agreed to in writing, Licensor provides the Work (and each 159 | Contributor provides its Contributions) on an "AS IS" BASIS, 160 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 161 | implied, including, without limitation, any warranties or conditions 162 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 163 | PARTICULAR PURPOSE. You are solely responsible for determining the 164 | appropriateness of using or redistributing the Work and assume any 165 | risks associated with Your exercise of permissions under this License. 166 | 167 | 8. Limitation of Liability. In no event and under no legal theory, 168 | whether in tort (including negligence), contract, or otherwise, 169 | unless required by applicable law (such as deliberate and grossly 170 | negligent acts) or agreed to in writing, shall any Contributor be 171 | liable to You for damages, including any direct, indirect, special, 172 | incidental, or consequential damages of any character arising as a 173 | result of this License or out of the use or inability to use the 174 | Work (including but not limited to damages for loss of goodwill, 175 | work stoppage, computer failure or malfunction, or any and all 176 | other commercial damages or losses), even if such Contributor 177 | has been advised of the possibility of such damages. 178 | 179 | 9. Accepting Warranty or Additional Liability. While redistributing 180 | the Work or Derivative Works thereof, You may choose to offer, 181 | and charge a fee for, acceptance of support, warranty, indemnity, 182 | or other liability obligations and/or rights consistent with this 183 | License. However, in accepting such obligations, You may act only 184 | on Your own behalf and on Your sole responsibility, not on behalf 185 | of any other Contributor, and only if You agree to indemnify, 186 | defend, and hold each Contributor harmless for any liability 187 | incurred by, or claims asserted against, such Contributor by reason 188 | of your accepting any such warranty or additional liability. 189 | 190 | END OF TERMS AND CONDITIONS 191 | 192 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HarmonySU 2 | 3 | 鸿蒙模拟器 root 工具,方便调试。 4 | 5 | ## 使用方法 6 | 7 | 1. 编译 su 8 | 9 | ```bash 10 | # 指定鸿蒙 NDK 路径 11 | export HARMONY_NDK_HOME=/path/to/your/harmony-ndk/native 12 | 13 | # 编译 14 | ./build.sh 15 | 16 | # 查看编译结果 17 | ls -l build/*/su 18 | ``` 19 | 20 | 2. Patch 系统镜像 21 | 22 | ```bash 23 | sudo ./patch_system_img.sh /path/to/your/system.img build/arm64-v8a/su 24 | ``` 25 | 26 | > [!NOTE] 27 | > 脚本目前仅支持 Linux 环境下运行 28 | > 29 | > `system.img` 位置: 30 | > - `~/Library/Huawei/Sdk/hmscore/{版本号}/system-image/phone_arm/system.img` 31 | > - `~/Library/Huawei/Sdk/system-image/{版本号}/phone_arm/system.img` 32 | 33 | 3. 启动模拟器,切换 root 用户 34 | 35 | ```bash 36 | # 启动 hdc shell 37 | hdc shell 38 | 39 | # 切换到 root 用户 40 | su 41 | 42 | # 这下就可以为所欲为了 😄 43 | whomai 44 | ``` 45 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ -z $HARMONY_NDK_HOME ]; then 4 | echo "HARMONY_NDK_HOME is not set" 5 | exit 1 6 | fi 7 | 8 | build_arch() { 9 | ARCH=$1 10 | $HARMONY_NDK_HOME/build-tools/cmake/bin/cmake \ 11 | -DOHOS_STL=c++_shared \ 12 | -DOHOS_ARCH=$ARCH \ 13 | -DOHOS_PLATFORM=OHOS \ 14 | -DCMAKE_TOOLCHAIN_FILE=$HARMONY_NDK_HOME/build/cmake/ohos.toolchain.cmake \ 15 | -B build/$ARCH 16 | make -C build/$ARCH 17 | } 18 | 19 | build_arch arm64-v8a 20 | build_arch x86_64 21 | -------------------------------------------------------------------------------- /patch_system_img.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | SYSTEM_IMG_PATH=$1 4 | SU_PATH=$2 5 | MOUNT_POINT=/mnt/ohos_system 6 | 7 | if [ $(uname -s) != "Linux" ]; then 8 | echo "Run this script on Linux only" 9 | exit 1 10 | fi 11 | if [ $(id -u) -ne 0 ]; then 12 | echo "Run this script as root" 13 | exit 1 14 | fi 15 | if [ -z $SYSTEM_IMG_PATH ] || [ -z $SU_PATH ]; then 16 | echo "Usage: $0 " 17 | exit 1 18 | fi 19 | 20 | echo "Backing up $SYSTEM_IMG_PATH to $SYSTEM_IMG_PATH.bak" 21 | cp $SYSTEM_IMG_PATH $SYSTEM_IMG_PATH.bak 22 | 23 | echo "Patching $SYSTEM_IMG_PATH" 24 | mkdir -p $MOUNT_POINT 25 | mount -o loop $SYSTEM_IMG_PATH $MOUNT_POINT 26 | cp $SU_PATH $MOUNT_POINT/system/bin/su 27 | chown root:root $MOUNT_POINT/system/bin/su 28 | chmod 6755 $MOUNT_POINT/system/bin/su 29 | umount $MOUNT_POINT 30 | 31 | echo "Done" 32 | -------------------------------------------------------------------------------- /su.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2008 The Android Open Source Project 3 | * Copyright (C) 2024 Shaoxing Wang 4 | * 5 | * Modifications: 6 | * - 2024-06-08: Port for HarmonyOS 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); 9 | * you may not use this file except in compliance with the License. 10 | * You may obtain a copy of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | * See the License for the specific language governing permissions and 18 | * limitations under the License. 19 | */ 20 | 21 | #include 22 | // #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | // #include 32 | #define AID_ROOT 0 33 | #define AID_SHELL 2000 34 | #define error(exitcode, errnum, format, ...) \ 35 | do { \ 36 | fprintf(stderr, format ": %s\n", ##__VA_ARGS__, strerror(errnum)); \ 37 | exit(exitcode); \ 38 | } while (0) 39 | 40 | 41 | void pwtoid(const char* tok, uid_t* uid, gid_t* gid) { 42 | struct passwd* pw = getpwnam(tok); 43 | if (pw) { 44 | if (uid) *uid = pw->pw_uid; 45 | if (gid) *gid = pw->pw_gid; 46 | } else { 47 | char* end; 48 | errno = 0; 49 | uid_t tmpid = strtoul(tok, &end, 10); 50 | if (errno != 0 || end == tok) error(1, errno, "invalid uid/gid '%s'", tok); 51 | if (uid) *uid = tmpid; 52 | if (gid) *gid = tmpid; 53 | } 54 | } 55 | 56 | void extract_uidgids(const char* uidgids, uid_t* uid, gid_t* gid, gid_t* gids, int* gids_count) { 57 | char *clobberablegids; 58 | char *nexttok; 59 | char *tok; 60 | int gids_found; 61 | 62 | if (!uidgids || !*uidgids) { 63 | *gid = *uid = 0; 64 | *gids_count = 0; 65 | return; 66 | } 67 | 68 | clobberablegids = strdup(uidgids); 69 | strcpy(clobberablegids, uidgids); 70 | nexttok = clobberablegids; 71 | tok = strsep(&nexttok, ","); 72 | pwtoid(tok, uid, gid); 73 | tok = strsep(&nexttok, ","); 74 | if (!tok) { 75 | /* gid is already set above */ 76 | *gids_count = 0; 77 | free(clobberablegids); 78 | return; 79 | } 80 | pwtoid(tok, NULL, gid); 81 | gids_found = 0; 82 | while ((gids_found < *gids_count) && (tok = strsep(&nexttok, ","))) { 83 | pwtoid(tok, NULL, gids); 84 | gids_found++; 85 | gids++; 86 | } 87 | if (nexttok && gids_found == *gids_count) { 88 | fprintf(stderr, "too many group ids\n"); 89 | } 90 | *gids_count = gids_found; 91 | free(clobberablegids); 92 | } 93 | 94 | int main(int argc, char** argv) { 95 | uid_t current_uid = getuid(); 96 | if (current_uid != AID_ROOT && current_uid != AID_SHELL) error(1, 0, "not allowed"); 97 | 98 | // Handle -h and --help. 99 | ++argv; 100 | if (*argv && (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0)) { 101 | fprintf(stderr, 102 | "usage: su [WHO [COMMAND...]]\n" 103 | "\n" 104 | "Switch to WHO (default 'root') and run the given COMMAND (default sh).\n" 105 | "\n" 106 | "WHO is a comma-separated list of user, group, and supplementary groups\n" 107 | "in that order.\n" 108 | "\n"); 109 | return 0; 110 | } 111 | 112 | // The default user is root. 113 | uid_t uid = 0; 114 | gid_t gid = 0; 115 | 116 | // If there are any arguments, the first argument is the uid/gid/supplementary groups. 117 | if (*argv) { 118 | gid_t gids[10]; 119 | int gids_count = sizeof(gids)/sizeof(gids[0]); 120 | extract_uidgids(*argv, &uid, &gid, gids, &gids_count); 121 | if (gids_count) { 122 | if (setgroups(gids_count, gids)) { 123 | error(1, errno, "setgroups failed"); 124 | } 125 | } 126 | ++argv; 127 | } 128 | 129 | if (setgid(gid)) error(1, errno, "setgid failed"); 130 | if (setuid(uid)) error(1, errno, "setuid failed"); 131 | 132 | // Reset parts of the environment. 133 | setenv("PATH", _PATH_DEFPATH, 1); 134 | unsetenv("IFS"); 135 | struct passwd* pw = getpwuid(uid); 136 | if (pw) { 137 | setenv("LOGNAME", pw->pw_name, 1); 138 | setenv("USER", pw->pw_name, 1); 139 | } else { 140 | unsetenv("LOGNAME"); 141 | unsetenv("USER"); 142 | } 143 | 144 | // Set up the arguments for exec. 145 | char* exec_args[argc + 1]; // Having too much space is fine. 146 | size_t i = 0; 147 | for (; *argv != NULL; ++i) { 148 | exec_args[i] = *argv++; 149 | } 150 | // Default to the standard shell. 151 | if (i == 0) exec_args[i++] = const_cast("/system/bin/sh"); 152 | exec_args[i] = NULL; 153 | 154 | execvp(exec_args[0], exec_args); 155 | error(1, errno, "failed to exec %s", exec_args[0]); 156 | } 157 | --------------------------------------------------------------------------------