1 From 703418fe9d2e3b1e8d594df5788d8001a8116265 Mon Sep 17 00:00:00 2001
2 From: Jeffrey Bencteux <jeffbencteux@gmail.com>
3 Date: Fri, 30 Jun 2023 19:02:45 +0200
4 Subject: [PATCH] CVE-2023-40303: ftpd,rcp,rlogin,rsh,rshd,uucpd: fix: check
7 Several setuid(), setgid(), seteuid() and setguid() return values
8 were not checked in ftpd/rcp/rlogin/rsh/rshd/uucpd code potentially
9 leading to potential security issues.
12 Upstream-Status: Backport [https://git.savannah.gnu.org/cgit/inetutils.git/commit/?id=e4e65c03f4c11292a3e40ef72ca3f194c8bffdd6]
13 Signed-off-by: Jeffrey Bencteux <jeffbencteux@gmail.com>
14 Signed-off-by: Simon Josefsson <simon@josefsson.org>
15 Signed-off-by: Khem Raj <raj.khem@gmail.com>
17 ftpd/ftpd.c | 10 +++++++---
18 src/rcp.c | 39 +++++++++++++++++++++++++++++++++------
19 src/rlogin.c | 11 +++++++++--
20 src/rsh.c | 25 +++++++++++++++++++++----
21 src/rshd.c | 20 +++++++++++++++++---
22 src/uucpd.c | 15 +++++++++++++--
23 6 files changed, 100 insertions(+), 20 deletions(-)
25 diff --git a/ftpd/ftpd.c b/ftpd/ftpd.c
26 index 92b2cca5..28dd523f 100644
29 @@ -862,7 +862,9 @@ end_login (struct credentials *pcred)
30 char *remotehost = pcred->remotehost;
31 int atype = pcred->auth_type;
33 - seteuid ((uid_t) 0);
34 + if (seteuid ((uid_t) 0) == -1)
35 + _exit (EXIT_FAILURE);
39 logwtmp_keep_open (ttyline, "", "");
40 @@ -1151,7 +1153,8 @@ getdatasock (const char *mode)
43 return fdopen (data, mode);
44 - seteuid ((uid_t) 0);
45 + if (seteuid ((uid_t) 0) == -1)
46 + _exit (EXIT_FAILURE);
47 s = socket (ctrl_addr.ss_family, SOCK_STREAM, 0);
50 @@ -1978,7 +1981,8 @@ passive (int epsv, int af)
52 ((struct sockaddr_in *) &pasv_addr)->sin_port = 0;
54 - seteuid ((uid_t) 0);
55 + if (seteuid ((uid_t) 0) == -1)
56 + _exit (EXIT_FAILURE);
57 if (bind (pdata, (struct sockaddr *) &pasv_addr, pasv_addrlen) < 0)
59 if (seteuid ((uid_t) cred.uid))
60 diff --git a/src/rcp.c b/src/rcp.c
61 index 75adb253..cdcf8500 100644
64 @@ -345,14 +345,23 @@ main (int argc, char *argv[])
66 { /* Follow "protocol", send data. */
70 + if (setuid (userid) == -1)
72 + error (EXIT_FAILURE, 0, "Could not drop privileges (setuid() failed)");
82 + if (setuid (userid) == -1)
84 + error (EXIT_FAILURE, 0, "Could not drop privileges (setuid() failed)");
90 @@ -537,7 +546,11 @@ toremote (char *targ, int argc, char *argv[])
96 + if (setuid (userid) == -1)
98 + error (EXIT_FAILURE, 0, "Could not drop privileges (setuid() failed)");
101 source (1, argv + i);
103 @@ -630,7 +643,12 @@ tolocal (int argc, char *argv[])
109 + if (seteuid (userid) == -1)
111 + error (EXIT_FAILURE, 0, "Could not drop privileges (seteuid() failed)");
114 #if defined IP_TOS && defined IPPROTO_IP && defined IPTOS_THROUGHPUT
116 (void) getpeername (rem, (struct sockaddr *) &ss, &sslen);
117 @@ -643,7 +661,12 @@ tolocal (int argc, char *argv[])
123 + if (seteuid (effuid) == -1)
125 + error (EXIT_FAILURE, 0, "Could not drop privileges (seteuid() failed)");
131 @@ -1441,7 +1464,11 @@ susystem (char *s, int userid)
136 + if (setuid (userid) == -1)
138 + error (EXIT_FAILURE, 0, "Could not drop privileges (setuid() failed)");
141 execl (PATH_BSHELL, "sh", "-c", s, NULL);
144 diff --git a/src/rlogin.c b/src/rlogin.c
145 index aa6426fb..c543de0c 100644
148 @@ -647,8 +647,15 @@ try_connect:
149 /* Now change to the real user ID. We have to be set-user-ID root
150 to get the privileged port that rcmd () uses. We now want, however,
151 to run as the real user who invoked us. */
154 + if (seteuid (uid) == -1)
156 + error (EXIT_FAILURE, 0, "Could not drop privileges (seteuid() failed)");
159 + if (setuid (uid) == -1)
161 + error (EXIT_FAILURE, 0, "Could not drop privileges (setuid() failed)");
164 doit (&osmask); /* The old mask will activate SIGURG and SIGUSR1! */
166 diff --git a/src/rsh.c b/src/rsh.c
167 index 2d622ca4..6f60667d 100644
170 @@ -276,8 +276,17 @@ main (int argc, char **argv)
173 *argv = (char *) "rlogin";
174 - seteuid (getuid ());
175 - setuid (getuid ());
177 + if (seteuid (getuid ()) == -1)
179 + error (EXIT_FAILURE, errno, "seteuid() failed");
182 + if (setuid (getuid ()) == -1)
184 + error (EXIT_FAILURE, errno, "setuid() failed");
187 execv (PATH_RLOGIN, argv);
188 error (EXIT_FAILURE, errno, "cannot execute %s", PATH_RLOGIN);
190 @@ -541,8 +550,16 @@ try_connect:
191 error (0, errno, "setsockopt DEBUG (ignored)");
196 + if (seteuid (uid) == -1)
198 + error (EXIT_FAILURE, errno, "seteuid() failed");
201 + if (setuid (uid) == -1)
203 + error (EXIT_FAILURE, errno, "setuid() failed");
206 #ifdef HAVE_SIGACTION
208 sigaddset (&sigs, SIGINT);
209 diff --git a/src/rshd.c b/src/rshd.c
210 index d1c0d0cd..707790e7 100644
213 @@ -1847,8 +1847,18 @@ doit (int sockfd, struct sockaddr *fromp, socklen_t fromlen)
214 pwd->pw_shell = PATH_BSHELL;
216 /* Set the gid, then uid to become the user specified by "locuser" */
217 - setegid ((gid_t) pwd->pw_gid);
218 - setgid ((gid_t) pwd->pw_gid);
219 + if (setegid ((gid_t) pwd->pw_gid) == -1)
221 + rshd_error ("Cannot drop privileges (setegid() failed)\n");
222 + exit (EXIT_FAILURE);
225 + if (setgid ((gid_t) pwd->pw_gid) == -1)
227 + rshd_error ("Cannot drop privileges (setgid() failed)\n");
228 + exit (EXIT_FAILURE);
231 #ifdef HAVE_INITGROUPS
232 initgroups (pwd->pw_name, pwd->pw_gid); /* BSD groups */
234 @@ -1870,7 +1880,11 @@ doit (int sockfd, struct sockaddr *fromp, socklen_t fromlen)
236 #endif /* WITH_PAM */
238 - setuid ((uid_t) pwd->pw_uid);
239 + if (setuid ((uid_t) pwd->pw_uid) == -1)
241 + rshd_error ("Cannot drop privileges (setuid() failed)\n");
242 + exit (EXIT_FAILURE);
245 /* We'll execute the client's command in the home directory
246 * of locuser. Note, that the chdir must be executed after
247 diff --git a/src/uucpd.c b/src/uucpd.c
248 index 107589e1..29cfce35 100644
251 @@ -252,7 +252,12 @@ doit (struct sockaddr *sap, socklen_t salen)
252 snprintf (Username, sizeof (Username), "USER=%s", user);
253 snprintf (Logname, sizeof (Logname), "LOGNAME=%s", user);
254 dologin (pw, sap, salen);
255 - setgid (pw->pw_gid);
257 + if (setgid (pw->pw_gid) == -1)
259 + fprintf (stderr, "setgid() failed");
262 #ifdef HAVE_INITGROUPS
263 initgroups (pw->pw_name, pw->pw_gid);
265 @@ -261,7 +266,13 @@ doit (struct sockaddr *sap, socklen_t salen)
266 fprintf (stderr, "Login incorrect.");
269 - setuid (pw->pw_uid);
271 + if (setuid (pw->pw_uid) == -1)
273 + fprintf (stderr, "setuid() failed");
277 execl (uucico_location, "uucico", NULL);
278 perror ("uucico server: execl");