|
| 1 | +From 2c5d0fb67af88aaa8c12364e0d23c6e678fa42fc Mon Sep 17 00:00:00 2001 |
| 2 | +From: hackeris <hackeris@qq.com> |
| 3 | +Date: Sat, 23 Aug 2025 09:22:29 +0800 |
| 4 | +Subject: [PATCH 10/11] emulate root user to guest |
| 5 | + |
| 6 | +--- |
| 7 | + linux-user/syscall.c | 117 +++++++++++++++++++++++++------------------ |
| 8 | + util/path.c | 1 - |
| 9 | + 2 files changed, 68 insertions(+), 50 deletions(-) |
| 10 | + |
| 11 | +diff --git a/linux-user/syscall.c b/linux-user/syscall.c |
| 12 | +index 93212666364..d3ae503d758 100644 |
| 13 | +--- a/linux-user/syscall.c |
| 14 | ++++ b/linux-user/syscall.c |
| 15 | +@@ -12194,24 +12194,30 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, |
| 16 | + #endif |
| 17 | + #ifdef TARGET_NR_getuid |
| 18 | + case TARGET_NR_getuid: |
| 19 | +- return get_errno(high2lowuid(getuid())); |
| 20 | ++ // return get_errno(high2lowuid(getuid())); |
| 21 | ++ return 0; |
| 22 | + #endif |
| 23 | + #ifdef TARGET_NR_getgid |
| 24 | + case TARGET_NR_getgid: |
| 25 | +- return get_errno(high2lowgid(getgid())); |
| 26 | ++ // return get_errno(high2lowgid(getgid())); |
| 27 | ++ return 0; |
| 28 | + #endif |
| 29 | + #ifdef TARGET_NR_geteuid |
| 30 | + case TARGET_NR_geteuid: |
| 31 | +- return get_errno(high2lowuid(geteuid())); |
| 32 | ++ // return get_errno(high2lowuid(geteuid())); |
| 33 | ++ return 0; |
| 34 | + #endif |
| 35 | + #ifdef TARGET_NR_getegid |
| 36 | + case TARGET_NR_getegid: |
| 37 | +- return get_errno(high2lowgid(getegid())); |
| 38 | ++ // return get_errno(high2lowgid(getegid())); |
| 39 | ++ return 0; |
| 40 | + #endif |
| 41 | + case TARGET_NR_setreuid: |
| 42 | +- return get_errno(sys_setreuid(low2highuid(arg1), low2highuid(arg2))); |
| 43 | ++ // return get_errno(sys_setreuid(low2highuid(arg1), low2highuid(arg2))); |
| 44 | ++ return 0; |
| 45 | + case TARGET_NR_setregid: |
| 46 | +- return get_errno(sys_setregid(low2highgid(arg1), low2highgid(arg2))); |
| 47 | ++ // return get_errno(sys_setregid(low2highgid(arg1), low2highgid(arg2))); |
| 48 | ++ return 0; |
| 49 | + case TARGET_NR_getgroups: |
| 50 | + { /* the same code as for TARGET_NR_getgroups32 */ |
| 51 | + int gidsetsize = arg1; |
| 52 | +@@ -12228,7 +12234,13 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, |
| 53 | + return -TARGET_ENOMEM; |
| 54 | + } |
| 55 | + } |
| 56 | +- ret = get_errno(getgroups(gidsetsize, grouplist)); |
| 57 | ++ // ret = get_errno(getgroups(gidsetsize, grouplist)); |
| 58 | ++ if (gidsetsize > 0) { |
| 59 | ++ grouplist[0] = 0; |
| 60 | ++ ret = 1; |
| 61 | ++ } else { |
| 62 | ++ ret = 0; |
| 63 | ++ } |
| 64 | + if (!is_error(ret) && gidsetsize > 0) { |
| 65 | + target_grouplist = lock_user(VERIFY_WRITE, arg2, |
| 66 | + gidsetsize * sizeof(target_id), 0); |
| 67 | +@@ -12245,54 +12257,59 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, |
| 68 | + } |
| 69 | + case TARGET_NR_setgroups: |
| 70 | + { /* the same code as for TARGET_NR_setgroups32 */ |
| 71 | +- int gidsetsize = arg1; |
| 72 | +- target_id *target_grouplist; |
| 73 | +- g_autofree gid_t *grouplist = NULL; |
| 74 | +- int i; |
| 75 | +- |
| 76 | +- if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) { |
| 77 | +- return -TARGET_EINVAL; |
| 78 | +- } |
| 79 | +- if (gidsetsize > 0) { |
| 80 | +- grouplist = g_try_new(gid_t, gidsetsize); |
| 81 | +- if (!grouplist) { |
| 82 | +- return -TARGET_ENOMEM; |
| 83 | +- } |
| 84 | +- target_grouplist = lock_user(VERIFY_READ, arg2, |
| 85 | +- gidsetsize * sizeof(target_id), 1); |
| 86 | +- if (!target_grouplist) { |
| 87 | +- return -TARGET_EFAULT; |
| 88 | +- } |
| 89 | +- for (i = 0; i < gidsetsize; i++) { |
| 90 | +- grouplist[i] = low2highgid(tswapid(target_grouplist[i])); |
| 91 | +- } |
| 92 | +- unlock_user(target_grouplist, arg2, |
| 93 | +- gidsetsize * sizeof(target_id)); |
| 94 | +- } |
| 95 | +- return get_errno(sys_setgroups(gidsetsize, grouplist)); |
| 96 | ++ // int gidsetsize = arg1; |
| 97 | ++ // target_id *target_grouplist; |
| 98 | ++ // g_autofree gid_t *grouplist = NULL; |
| 99 | ++ // int i; |
| 100 | ++ // |
| 101 | ++ // if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) { |
| 102 | ++ // return -TARGET_EINVAL; |
| 103 | ++ // } |
| 104 | ++ // if (gidsetsize > 0) { |
| 105 | ++ // grouplist = g_try_new(gid_t, gidsetsize); |
| 106 | ++ // if (!grouplist) { |
| 107 | ++ // return -TARGET_ENOMEM; |
| 108 | ++ // } |
| 109 | ++ // target_grouplist = lock_user(VERIFY_READ, arg2, |
| 110 | ++ // gidsetsize * sizeof(target_id), 1); |
| 111 | ++ // if (!target_grouplist) { |
| 112 | ++ // return -TARGET_EFAULT; |
| 113 | ++ // } |
| 114 | ++ // for (i = 0; i < gidsetsize; i++) { |
| 115 | ++ // grouplist[i] = low2highgid(tswapid(target_grouplist[i])); |
| 116 | ++ // } |
| 117 | ++ // unlock_user(target_grouplist, arg2, |
| 118 | ++ // gidsetsize * sizeof(target_id)); |
| 119 | ++ // } |
| 120 | ++ // return get_errno(sys_setgroups(gidsetsize, grouplist)); |
| 121 | ++ return 0; |
| 122 | + } |
| 123 | + case TARGET_NR_fchown: |
| 124 | +- return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3))); |
| 125 | ++ // return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3))); |
| 126 | ++ return 0; |
| 127 | + #if defined(TARGET_NR_fchownat) |
| 128 | + case TARGET_NR_fchownat: |
| 129 | +- if (!(p = lock_user_string(arg2))) |
| 130 | +- return -TARGET_EFAULT; |
| 131 | +- ret = get_errno(fchownat(arg1, relocate_path_at(arg1, p, reloc, true), low2highuid(arg3), |
| 132 | +- low2highgid(arg4), arg5)); |
| 133 | +- unlock_user(p, arg2, 0); |
| 134 | +- return ret; |
| 135 | ++ // if (!(p = lock_user_string(arg2))) |
| 136 | ++ // return -TARGET_EFAULT; |
| 137 | ++ // ret = get_errno(fchownat(arg1, relocate_path_at(arg1, p, reloc, true), low2highuid(arg3), |
| 138 | ++ // low2highgid(arg4), arg5)); |
| 139 | ++ // unlock_user(p, arg2, 0); |
| 140 | ++ // return ret; |
| 141 | ++ return 0; |
| 142 | + #endif |
| 143 | + #ifdef TARGET_NR_setresuid |
| 144 | + case TARGET_NR_setresuid: |
| 145 | +- return get_errno(sys_setresuid(low2highuid(arg1), |
| 146 | +- low2highuid(arg2), |
| 147 | +- low2highuid(arg3))); |
| 148 | ++ // return get_errno(sys_setresuid(low2highuid(arg1), |
| 149 | ++ // low2highuid(arg2), |
| 150 | ++ // low2highuid(arg3))); |
| 151 | ++ return 0; |
| 152 | + #endif |
| 153 | + #ifdef TARGET_NR_getresuid |
| 154 | + case TARGET_NR_getresuid: |
| 155 | + { |
| 156 | +- uid_t ruid, euid, suid; |
| 157 | +- ret = get_errno(getresuid(&ruid, &euid, &suid)); |
| 158 | ++ uid_t ruid = 0, euid = 0, suid = 0; |
| 159 | ++ // ret = get_errno(getresuid(&ruid, &euid, &suid)); |
| 160 | ++ ret = 0; |
| 161 | + if (!is_error(ret)) { |
| 162 | + if (put_user_id(high2lowuid(ruid), arg1) |
| 163 | + || put_user_id(high2lowuid(euid), arg2) |
| 164 | +@@ -12304,15 +12321,17 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, |
| 165 | + #endif |
| 166 | + #ifdef TARGET_NR_getresgid |
| 167 | + case TARGET_NR_setresgid: |
| 168 | +- return get_errno(sys_setresgid(low2highgid(arg1), |
| 169 | +- low2highgid(arg2), |
| 170 | +- low2highgid(arg3))); |
| 171 | ++ // return get_errno(sys_setresgid(low2highgid(arg1), |
| 172 | ++ // low2highgid(arg2), |
| 173 | ++ // low2highgid(arg3))); |
| 174 | ++ return 0; |
| 175 | + #endif |
| 176 | + #ifdef TARGET_NR_getresgid |
| 177 | + case TARGET_NR_getresgid: |
| 178 | + { |
| 179 | +- gid_t rgid, egid, sgid; |
| 180 | +- ret = get_errno(getresgid(&rgid, &egid, &sgid)); |
| 181 | ++ gid_t rgid = 0, egid = 0, sgid = 0; |
| 182 | ++ // ret = get_errno(getresgid(&rgid, &egid, &sgid)); |
| 183 | ++ ret = 0; |
| 184 | + if (!is_error(ret)) { |
| 185 | + if (put_user_id(high2lowgid(rgid), arg1) |
| 186 | + || put_user_id(high2lowgid(egid), arg2) |
| 187 | +diff --git a/util/path.c b/util/path.c |
| 188 | +index b8787076c32..acb6467aca0 100644 |
| 189 | +--- a/util/path.c |
| 190 | ++++ b/util/path.c |
| 191 | +@@ -47,7 +47,6 @@ static bool skip_relocation(const char* name) |
| 192 | + || strstr(name, "/sys/") == name |
| 193 | + || strcmp(name, "/sys") == 0 |
| 194 | + || strcmp(name, "/etc/resolv.conf") == 0 |
| 195 | +- || strcmp(name, "/etc/passwd") == 0 |
| 196 | + || strcmp(name, "/dev") == 0 |
| 197 | + || strstr(name, "/dev/") == name |
| 198 | + ; |
| 199 | +-- |
| 200 | +2.43.0 |
| 201 | + |
0 commit comments