OpenCoverage

uidswap.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssh/src/uidswap.c
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3-
4-
5-
6static uid_t saved_euid = 0;-
7static gid_t saved_egid = 0;-
8-
9-
10-
11static int privileged = 0;-
12static int temporarily_use_uid_effective = 0;-
13static uid_t user_groups_uid;-
14static gid_t *saved_egroups = -
15 ((void *)0)-
16 , *user_groups = -
17 ((void *)0)-
18 ;-
19static int saved_egroupslen = -1, user_groupslen = -1;-
20-
21-
22-
23-
24-
25void-
26temporarily_use_uid(struct passwd *pw)-
27{-
28-
29-
30 saved_euid = geteuid();-
31 saved_egid = getegid();-
32 debug("temporarily_use_uid: %u/%u (e=%u/%u)",-
33 (u_int)pw->pw_uid, (u_int)pw->pw_gid,-
34 (u_int)saved_euid, (u_int)saved_egid);-
35-
36 if (saved_euid != 0
saved_euid != 0Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
37 privileged = 0;-
38 return;
never executed: return;
0
39 }-
40 privileged = 1;-
41 temporarily_use_uid_effective = 1;-
42-
43 saved_egroupslen = getgroups(0, -
44 ((void *)0)-
45 );-
46 if (saved_egroupslen < 0
saved_egroupslen < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
47 fatal("getgroups: %.100s", strerror(
never executed: fatal("getgroups: %.100s", strerror( (*__errno_location ()) ));
0
48 (*__errno_location ())
never executed: fatal("getgroups: %.100s", strerror( (*__errno_location ()) ));
0
49 ));
never executed: fatal("getgroups: %.100s", strerror( (*__errno_location ()) ));
0
50 if (saved_egroupslen > 0
saved_egroupslen > 0Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
51 saved_egroups = xreallocarray(saved_egroups,-
52 saved_egroupslen, sizeof(gid_t));-
53 if (getgroups(saved_egroupslen, saved_egroups) < 0
getgroups(save...d_egroups) < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
54 fatal("getgroups: %.100s", strerror(
never executed: fatal("getgroups: %.100s", strerror( (*__errno_location ()) ));
0
55 (*__errno_location ())
never executed: fatal("getgroups: %.100s", strerror( (*__errno_location ()) ));
0
56 ));
never executed: fatal("getgroups: %.100s", strerror( (*__errno_location ()) ));
0
57 }
never executed: end of block
else {
0
58 free(saved_egroups);-
59 saved_egroups = -
60 ((void *)0)-
61 ;-
62 }
never executed: end of block
0
63-
64-
65 if (user_groupslen == -1
user_groupslen == -1Description
TRUEnever evaluated
FALSEnever evaluated
|| user_groups_uid != pw->pw_uid
user_groups_uid != pw->pw_uidDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
66 if (initgroups(pw->pw_name, pw->pw_gid) < 0
initgroups(pw-...w->pw_gid) < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
67 fatal("initgroups: %s: %.100s", pw->pw_name,
never executed: fatal("initgroups: %s: %.100s", pw->pw_name, strerror( (*__errno_location ()) ));
0
68 strerror(
never executed: fatal("initgroups: %s: %.100s", pw->pw_name, strerror( (*__errno_location ()) ));
0
69 (*__errno_location ())
never executed: fatal("initgroups: %s: %.100s", pw->pw_name, strerror( (*__errno_location ()) ));
0
70 ));
never executed: fatal("initgroups: %s: %.100s", pw->pw_name, strerror( (*__errno_location ()) ));
0
71-
72 user_groupslen = getgroups(0, -
73 ((void *)0)-
74 );-
75 if (user_groupslen < 0
user_groupslen < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
76 fatal("getgroups: %.100s", strerror(
never executed: fatal("getgroups: %.100s", strerror( (*__errno_location ()) ));
0
77 (*__errno_location ())
never executed: fatal("getgroups: %.100s", strerror( (*__errno_location ()) ));
0
78 ));
never executed: fatal("getgroups: %.100s", strerror( (*__errno_location ()) ));
0
79 if (user_groupslen > 0
user_groupslen > 0Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
80 user_groups = xreallocarray(user_groups,-
81 user_groupslen, sizeof(gid_t));-
82 if (getgroups(user_groupslen, user_groups) < 0
getgroups(user...er_groups) < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
83 fatal("getgroups: %.100s", strerror(
never executed: fatal("getgroups: %.100s", strerror( (*__errno_location ()) ));
0
84 (*__errno_location ())
never executed: fatal("getgroups: %.100s", strerror( (*__errno_location ()) ));
0
85 ));
never executed: fatal("getgroups: %.100s", strerror( (*__errno_location ()) ));
0
86 }
never executed: end of block
else {
0
87 free(user_groups);-
88 user_groups = -
89 ((void *)0)-
90 ;-
91 }
never executed: end of block
0
92 user_groups_uid = pw->pw_uid;-
93 }
never executed: end of block
0
94-
95 if (setgroups(user_groupslen, user_groups) < 0
setgroups(user...er_groups) < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
96 fatal("setgroups: %.100s", strerror(
never executed: fatal("setgroups: %.100s", strerror( (*__errno_location ()) ));
0
97 (*__errno_location ())
never executed: fatal("setgroups: %.100s", strerror( (*__errno_location ()) ));
0
98 ));
never executed: fatal("setgroups: %.100s", strerror( (*__errno_location ()) ));
0
99 if (setegid(pw->pw_gid) < 0
setegid(pw->pw_gid) < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
100 fatal("setegid %u: %.100s", (u_int)pw->pw_gid,
never executed: fatal("setegid %u: %.100s", (u_int)pw->pw_gid, strerror( (*__errno_location ()) ));
0
101 strerror(
never executed: fatal("setegid %u: %.100s", (u_int)pw->pw_gid, strerror( (*__errno_location ()) ));
0
102 (*__errno_location ())
never executed: fatal("setegid %u: %.100s", (u_int)pw->pw_gid, strerror( (*__errno_location ()) ));
0
103 ));
never executed: fatal("setegid %u: %.100s", (u_int)pw->pw_gid, strerror( (*__errno_location ()) ));
0
104 if (seteuid(pw->pw_uid) == -1
seteuid(pw->pw_uid) == -1Description
TRUEnever evaluated
FALSEnever evaluated
)
0
105 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
never executed: fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, strerror( (*__errno_location ()) ));
0
106 strerror(
never executed: fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, strerror( (*__errno_location ()) ));
0
107 (*__errno_location ())
never executed: fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, strerror( (*__errno_location ()) ));
0
108 ));
never executed: fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, strerror( (*__errno_location ()) ));
0
109}
never executed: end of block
0
110-
111-
112-
113-
114void-
115restore_uid(void)-
116{-
117-
118 if (!privileged
!privilegedDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
119 debug("restore_uid: (unprivileged)");-
120 return;
never executed: return;
0
121 }-
122 if (!temporarily_use_uid_effective
!temporarily_use_uid_effectiveDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
123 fatal("restore_uid: temporarily_use_uid not effective");
never executed: fatal("restore_uid: temporarily_use_uid not effective");
0
124-
125-
126 debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);-
127-
128 if (seteuid(saved_euid) < 0
seteuid(saved_euid) < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
129 fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(
never executed: fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror( (*__errno_location ()) ));
0
130 (*__errno_location ())
never executed: fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror( (*__errno_location ()) ));
0
131 ));
never executed: fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror( (*__errno_location ()) ));
0
132 if (setegid(saved_egid) < 0
setegid(saved_egid) < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
133 fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(
never executed: fatal("setegid %u: %.100s", (u_int)saved_egid, strerror( (*__errno_location ()) ));
0
134 (*__errno_location ())
never executed: fatal("setegid %u: %.100s", (u_int)saved_egid, strerror( (*__errno_location ()) ));
0
135 ));
never executed: fatal("setegid %u: %.100s", (u_int)saved_egid, strerror( (*__errno_location ()) ));
0
136 if (setgroups(saved_egroupslen, saved_egroups) < 0
setgroups(save...d_egroups) < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
137 fatal("setgroups: %.100s", strerror(
never executed: fatal("setgroups: %.100s", strerror( (*__errno_location ()) ));
0
138 (*__errno_location ())
never executed: fatal("setgroups: %.100s", strerror( (*__errno_location ()) ));
0
139 ));
never executed: fatal("setgroups: %.100s", strerror( (*__errno_location ()) ));
0
140 temporarily_use_uid_effective = 0;-
141}
never executed: end of block
0
142-
143-
144-
145-
146-
147void-
148permanently_set_uid(struct passwd *pw)-
149{-
150-
151 uid_t old_uid = getuid();-
152 gid_t old_gid = getgid();-
153-
154-
155 if (pw ==
pw == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
156 ((void *)0)
pw == ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
157 )-
158 fatal("permanently_set_uid: no user given");
never executed: fatal("permanently_set_uid: no user given");
0
159 if (temporarily_use_uid_effective
temporarily_use_uid_effectiveDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
160 fatal("permanently_set_uid: temporarily_use_uid effective");
never executed: fatal("permanently_set_uid: temporarily_use_uid effective");
0
161 debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid,-
162 (u_int)pw->pw_gid);-
163-
164 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) < 0
setresgid(pw->...w->pw_gid) < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
165 fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror(
never executed: fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror( (*__errno_location ()) ));
0
166 (*__errno_location ())
never executed: fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror( (*__errno_location ()) ));
0
167 ));
never executed: fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror( (*__errno_location ()) ));
0
168 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) < 0
setresuid(pw->...w->pw_uid) < 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
169 fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror(
never executed: fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror( (*__errno_location ()) ));
0
170 (*__errno_location ())
never executed: fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror( (*__errno_location ()) ));
0
171 ));
never executed: fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror( (*__errno_location ()) ));
0
172-
173-
174-
175 if (old_gid != pw->pw_gid
old_gid != pw->pw_gidDescription
TRUEnever evaluated
FALSEnever evaluated
&& pw->pw_uid != 0
pw->pw_uid != 0Description
TRUEnever evaluated
FALSEnever evaluated
&&
0
176 (setgid(old_gid) != -1
setgid(old_gid) != -1Description
TRUEnever evaluated
FALSEnever evaluated
|| setegid(old_gid) != -1
setegid(old_gid) != -1Description
TRUEnever evaluated
FALSEnever evaluated
))
0
177 fatal("%s: was able to restore old [e]gid", __func__);
never executed: fatal("%s: was able to restore old [e]gid", __func__);
0
178-
179-
180-
181 if (getgid() != pw->pw_gid
getgid() != pw->pw_gidDescription
TRUEnever evaluated
FALSEnever evaluated
|| getegid() != pw->pw_gid
getegid() != pw->pw_gidDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
182 fatal("%s: egid incorrect gid:%u egid:%u (should be %u)",-
183 __func__, (u_int)getgid(), (u_int)getegid(),-
184 (u_int)pw->pw_gid);-
185 }
never executed: end of block
0
186-
187-
188-
189 if (old_uid != pw->pw_uid
old_uid != pw->pw_uidDescription
TRUEnever evaluated
FALSEnever evaluated
&&
0
190 (setuid(old_uid) != -1
setuid(old_uid) != -1Description
TRUEnever evaluated
FALSEnever evaluated
|| seteuid(old_uid) != -1
seteuid(old_uid) != -1Description
TRUEnever evaluated
FALSEnever evaluated
))
0
191 fatal("%s: was able to restore old [e]uid", __func__);
never executed: fatal("%s: was able to restore old [e]uid", __func__);
0
192-
193-
194-
195 if (getuid() != pw->pw_uid
getuid() != pw->pw_uidDescription
TRUEnever evaluated
FALSEnever evaluated
|| geteuid() != pw->pw_uid
geteuid() != pw->pw_uidDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
196 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",-
197 __func__, (u_int)getuid(), (u_int)geteuid(),-
198 (u_int)pw->pw_uid);-
199 }
never executed: end of block
0
200}
never executed: end of block
0
Switch to Source codePreprocessed file

Generated by Squish Coco 4.2.2