1 /* $OpenBSD: uidswap.c,v 1.37 2015/01/16 06:40:12 deraadt Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Code for uid-swapping.
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
32 #include <private/android_filesystem_config.h>
33 #if !defined(GCE_PLATFORM_SDK_VERSION) || (GCE_PLATFORM_SDK_VERSION > 17)
34 #include <sys/capability.h>
36 #include <linux/capability.h>
38 #include <sys/prctl.h>
42 * Note: all these functions must work in all of the following cases:
46 * Additionally, they must work regardless of whether the system has
47 * POSIX saved uids or not.
50 #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS)
51 /* Lets assume that posix saved ids also work with seteuid, even though that
52 is not part of the posix specification. */
53 #define SAVED_IDS_WORK_WITH_SETEUID
54 /* Saved effective uid. */
55 static uid_t saved_euid = 0;
56 static gid_t saved_egid = 0;
59 /* Saved effective uid. */
60 static int privileged = 0;
61 static int temporarily_use_uid_effective = 0;
62 static gid_t *saved_egroups = NULL, *user_groups = NULL;
63 static int saved_egroupslen = -1, user_groupslen = -1;
66 * Temporarily changes to the given uid. If the effective user
67 * id is not root, this does nothing. This call cannot be nested.
70 temporarily_use_uid(struct passwd *pw)
72 /* Save the current euid, and egroups. */
73 #ifdef SAVED_IDS_WORK_WITH_SETEUID
74 saved_euid = geteuid();
75 saved_egid = getegid();
76 debug("temporarily_use_uid: %u/%u (e=%u/%u)",
77 (u_int)pw->pw_uid, (u_int)pw->pw_gid,
78 (u_int)saved_euid, (u_int)saved_egid);
80 if (saved_euid != 0) {
90 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
93 temporarily_use_uid_effective = 1;
95 saved_egroupslen = getgroups(0, NULL);
96 if (saved_egroupslen < 0)
97 fatal("getgroups: %.100s", strerror(errno));
98 if (saved_egroupslen > 0) {
99 saved_egroups = xrealloc(saved_egroups,
100 saved_egroupslen, sizeof(gid_t));
101 if (getgroups(saved_egroupslen, saved_egroups) < 0)
102 fatal("getgroups: %.100s", strerror(errno));
103 } else { /* saved_egroupslen == 0 */
107 /* set and save the user's groups */
108 if (user_groupslen == -1) {
109 if (initgroups(pw->pw_name, pw->pw_gid) < 0)
110 fatal("initgroups: %s: %.100s", pw->pw_name,
113 user_groupslen = getgroups(0, NULL);
114 if (user_groupslen < 0)
115 fatal("getgroups: %.100s", strerror(errno));
116 if (user_groupslen > 0) {
117 user_groups = xrealloc(user_groups,
118 user_groupslen, sizeof(gid_t));
119 if (getgroups(user_groupslen, user_groups) < 0)
120 fatal("getgroups: %.100s", strerror(errno));
121 } else { /* user_groupslen == 0 */
125 /* Set the effective uid to the given (unprivileged) uid. */
126 if (setgroups(user_groupslen, user_groups) < 0)
127 fatal("setgroups: %.100s", strerror(errno));
128 #ifndef SAVED_IDS_WORK_WITH_SETEUID
129 /* Propagate the privileged gid to all of our gids. */
130 if (setgid(getegid()) < 0)
131 debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno));
132 /* Propagate the privileged uid to all of our uids. */
133 if (setuid(geteuid()) < 0)
134 debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno));
135 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
136 if (setegid(pw->pw_gid) < 0)
137 fatal("setegid %u: %.100s", (u_int)pw->pw_gid,
139 if (seteuid(pw->pw_uid) == -1)
140 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
145 permanently_drop_suid(uid_t uid)
148 uid_t old_uid = getuid();
151 debug("permanently_drop_suid: %u", (u_int)uid);
152 if (setresuid(uid, uid, uid) < 0)
153 fatal("setresuid %u: %.100s", (u_int)uid, strerror(errno));
156 /* Try restoration of UID if changed (test clearing of saved uid) */
157 if (old_uid != uid &&
158 (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
159 fatal("%s: was able to restore old [e]uid", __func__);
162 /* Verify UID drop was successful */
163 if (getuid() != uid || geteuid() != uid) {
164 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
165 __func__, (u_int)getuid(), (u_int)geteuid(), (u_int)uid);
170 * Restores to the original (privileged) uid.
175 /* it's a no-op unless privileged */
177 debug("restore_uid: (unprivileged)");
180 if (!temporarily_use_uid_effective)
181 fatal("restore_uid: temporarily_use_uid not effective");
183 #ifdef SAVED_IDS_WORK_WITH_SETEUID
184 debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);
185 /* Set the effective uid back to the saved privileged uid. */
186 if (seteuid(saved_euid) < 0)
187 fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno));
188 if (setegid(saved_egid) < 0)
189 fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno));
190 #else /* SAVED_IDS_WORK_WITH_SETEUID */
192 * We are unable to restore the real uid to its unprivileged value.
193 * Propagate the real uid (usually more privileged) to effective uid
198 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
200 if (setgroups(saved_egroupslen, saved_egroups) < 0)
201 fatal("setgroups: %.100s", strerror(errno));
202 temporarily_use_uid_effective = 0;
206 * Permanently sets all uids to the given uid. This cannot be
207 * called while temporarily_use_uid is effective.
210 permanently_set_uid(struct passwd *pw)
213 uid_t old_uid = getuid();
214 gid_t old_gid = getgid();
217 struct __user_cap_header_struct header;
218 struct __user_cap_data_struct cap;
222 fatal("permanently_set_uid: no user given");
223 if (temporarily_use_uid_effective)
224 fatal("permanently_set_uid: temporarily_use_uid effective");
225 debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid,
229 if (pw->pw_uid == AID_SHELL) {
230 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
232 /* add extra groups needed for shell user:
233 * - AID_LOG to read system logs (adb logcat)
234 * - AID_INPUT to diagnose input issues (getevent)
235 * - AID_INET to diagnose network issues (netcfg, ping)
236 * - AID_GRAPHICS to access the frame buffer
237 * - AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
238 * - AID_SDCARD_RW to allow writing to the SD card
239 * - AID_MOUNT to allow unmounting the SD card before rebooting
240 * - AID_NET_BW_STATS to read out qtaguid statistics. */
241 gid_t groups[] = {AID_LOG, AID_INPUT, AID_INET,
242 AID_GRAPHICS, AID_NET_BT, AID_NET_BT_ADMIN,
243 AID_SDCARD_RW, AID_MOUNT, AID_NET_BW_STATS};
244 setgroups(sizeof(groups)/sizeof(groups[0]), groups);
248 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) < 0)
249 fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
253 * OS X requires initgroups after setgid to opt back into
254 * memberd support for >16 supplemental groups.
256 if (initgroups(pw->pw_name, pw->pw_gid) < 0)
257 fatal("initgroups %.100s %u: %.100s",
258 pw->pw_name, (u_int)pw->pw_gid, strerror(errno));
261 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) < 0)
262 fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
265 /* Try restoration of GID if changed (test clearing of saved gid) */
266 if (old_gid != pw->pw_gid && pw->pw_uid != 0 &&
267 (setgid(old_gid) != -1 || setegid(old_gid) != -1))
268 fatal("%s: was able to restore old [e]gid", __func__);
271 /* Verify GID drop was successful */
272 if (getgid() != pw->pw_gid || getegid() != pw->pw_gid) {
273 fatal("%s: egid incorrect gid:%u egid:%u (should be %u)",
274 __func__, (u_int)getgid(), (u_int)getegid(),
279 /* Try restoration of UID if changed (test clearing of saved uid) */
280 if (old_uid != pw->pw_uid &&
281 (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
282 fatal("%s: was able to restore old [e]uid", __func__);
285 /* Verify UID drop was successful */
286 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) {
287 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
288 __func__, (u_int)getuid(), (u_int)geteuid(),
293 if (pw->pw_uid == AID_SHELL) {
294 /* set CAP_SYS_BOOT capability, so "adb reboot" will succeed */
295 header.version = _LINUX_CAPABILITY_VERSION;
297 cap.effective = cap.permitted = (1 << CAP_SYS_BOOT);
299 capset(&header, &cap);