Skip to content

Commit

Permalink
upstream: Remove support for running ssh(1) setuid and fatal if
Browse files Browse the repository at this point in the history
attempted. Do not link uidwap.c into ssh any more.  Neuters
UsePrivilegedPort, which will be marked as deprecated shortly. ok markus@
djm@

OpenBSD-Commit-ID: c4ba5bf9c096f57a6ed15b713a1d7e9e2e373c42
  • Loading branch information
daztucker authored and djmdjm committed Jul 19, 2018
1 parent ac59076 commit 258dc8b
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 134 deletions.
12 changes: 6 additions & 6 deletions .depend

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
compat.o crc32.o fatal.o hostfile.o \
log.o match.o moduli.o nchan.o packet.o opacket.o \
readpass.o ttymodes.o xmalloc.o addrmatch.o \
atomicio.o dispatch.o mac.o uidswap.o uuencode.o misc.o utf8.o \
atomicio.o dispatch.o mac.o uuencode.o misc.o utf8.o \
monitor_fdpass.o rijndael.o ssh-dss.o ssh-ecdsa.o ssh-rsa.o dh.o \
msg.o progressmeter.o dns.o entropy.o gss-genr.o umac.o umac128.o \
ssh-pkcs11.o smult_curve25519_ref.o \
Expand Down Expand Up @@ -118,7 +118,7 @@ SSHDOBJS=sshd.o auth-rhosts.o auth-passwd.o \
sftp-server.o sftp-common.o \
sandbox-null.o sandbox-rlimit.o sandbox-systrace.o sandbox-darwin.o \
sandbox-seccomp-filter.o sandbox-capsicum.o sandbox-pledge.o \
sandbox-solaris.o
sandbox-solaris.o uidswap.o

MANPAGES = moduli.5.out scp.1.out ssh-add.1.out ssh-agent.1.out ssh-keygen.1.out ssh-keyscan.1.out ssh.1.out sshd.8.out sftp-server.8.out sftp.1.out ssh-keysign.8.out ssh-pkcs11-helper.8.out sshd_config.5.out ssh_config.5.out
MANPAGES_IN = moduli.5 scp.1 ssh-add.1 ssh-agent.1 ssh-keygen.1 ssh-keyscan.1 ssh.1 sshd.8 sftp-server.8 sftp.1 ssh-keysign.8 ssh-pkcs11-helper.8 sshd_config.5 ssh_config.5
Expand Down Expand Up @@ -186,8 +186,8 @@ ssh-agent$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-agent.o ssh-pkcs11-client.o
ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keygen.o
$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)

ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o readconf.o
$(LD) -o $@ ssh-keysign.o readconf.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o readconf.o uidswap.o
$(LD) -o $@ ssh-keysign.o readconf.o uidswap.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)

ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11.o
$(LD) -o $@ ssh-pkcs11-helper.o ssh-pkcs11.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
Expand Down
3 changes: 1 addition & 2 deletions misc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: misc.c,v 1.129 2018/06/09 03:01:12 djm Exp $ */
/* $OpenBSD: misc.c,v 1.130 2018/07/18 11:34:04 dtucker Exp $ */
/*
* Copyright (c) 2000 Markus Friedl. All rights reserved.
* Copyright (c) 2005,2006 Damien Miller. All rights reserved.
Expand Down Expand Up @@ -69,7 +69,6 @@
#include "ssh.h"
#include "sshbuf.h"
#include "ssherr.h"
#include "uidswap.h"
#include "platform.h"

/* remove newline at end of string */
Expand Down
5 changes: 1 addition & 4 deletions readconf.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: readconf.c,v 1.292 2018/07/04 13:49:31 djm Exp $ */
/* $OpenBSD: readconf.c,v 1.293 2018/07/18 11:34:04 dtucker Exp $ */
/*
* Author: Tatu Ylonen <[email protected]>
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
Expand Down Expand Up @@ -498,9 +498,6 @@ execute_in_shell(const char *cmd)
if ((pid = fork()) == 0) {
char *argv[4];

/* Child. Permanently give up superuser privileges. */
permanently_drop_suid(original_real_uid);

/* Redirect child stdin and stdout. Leave stderr */
if (dup2(devnull, STDIN_FILENO) == -1)
fatal("dup2: %s", strerror(errno));
Expand Down
3 changes: 1 addition & 2 deletions readpass.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: readpass.c,v 1.51 2015/12/11 00:20:04 mmcc Exp $ */
/* $OpenBSD: readpass.c,v 1.52 2018/07/18 11:34:04 dtucker Exp $ */
/*
* Copyright (c) 2001 Markus Friedl. All rights reserved.
*
Expand Down Expand Up @@ -72,7 +72,6 @@ ssh_askpass(char *askpass, const char *msg)
return NULL;
}
if (pid == 0) {
permanently_drop_suid(getuid());
close(p[0]);
if (dup2(p[1], STDOUT_FILENO) < 0)
fatal("ssh_askpass: dup2: %s", strerror(errno));
Expand Down
37 changes: 2 additions & 35 deletions ssh.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: ssh.c,v 1.486 2018/07/16 22:25:01 dtucker Exp $ */
/* $OpenBSD: ssh.c,v 1.487 2018/07/18 11:34:04 dtucker Exp $ */
/*
* Author: Tatu Ylonen <[email protected]>
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
Expand Down Expand Up @@ -104,7 +104,6 @@
#include "sshpty.h"
#include "match.h"
#include "msg.h"
#include "uidswap.h"
#include "version.h"
#include "ssherr.h"
#include "myproposal.h"
Expand Down Expand Up @@ -628,24 +627,6 @@ main(int ac, char **av)
original_real_uid = getuid();
original_effective_uid = geteuid();

/*
* Use uid-swapping to give up root privileges for the duration of
* option processing. We will re-instantiate the rights when we are
* ready to create the privileged port, and will permanently drop
* them when the port has been created (actually, when the connection
* has been made, as we may need to create the port several times).
*/
PRIV_END;

#ifdef HAVE_SETRLIMIT
/* If we are installed setuid root be careful to not drop core. */
if (original_real_uid != original_effective_uid) {
struct rlimit rlim;
rlim.rlim_cur = rlim.rlim_max = 0;
if (setrlimit(RLIMIT_CORE, &rlim) < 0)
fatal("setrlimit failed: %.100s", strerror(errno));
}
#endif
/* Get user data. */
pw = getpwuid(original_real_uid);
if (!pw) {
Expand Down Expand Up @@ -1448,22 +1429,8 @@ main(int ac, char **av)
L_PUBKEY(_PATH_HOST_XMSS_KEY_FILE, 9);
}
}
/*
* Get rid of any extra privileges that we may have. We will no
* longer need them. Also, extra privileges could make it very hard
* to read identity files and other non-world-readable files from the
* user's home directory if it happens to be on a NFS volume where
* root is mapped to nobody.
*/
if (original_effective_uid == 0) {
PRIV_START;
permanently_set_uid(pw);
}

/*
* Now that we are back to our own permissions, create ~/.ssh
* directory if it doesn't already exist.
*/
/* Create ~/.ssh * directory if it doesn't already exist. */
if (config == NULL) {
r = snprintf(buf, sizeof buf, "%s%s%s", pw->pw_dir,
strcmp(pw->pw_dir, "/") ? "/" : "", _PATH_SSH_USER_DIR);
Expand Down
28 changes: 3 additions & 25 deletions sshconnect.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: sshconnect.c,v 1.300 2018/07/11 18:53:29 markus Exp $ */
/* $OpenBSD: sshconnect.c,v 1.301 2018/07/18 11:34:04 dtucker Exp $ */
/*
* Author: Tatu Ylonen <[email protected]>
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
Expand Down Expand Up @@ -53,7 +53,6 @@
#include "ssh.h"
#include "sshbuf.h"
#include "packet.h"
#include "uidswap.h"
#include "compat.h"
#include "sshkey.h"
#include "sshconnect.h"
Expand Down Expand Up @@ -130,9 +129,6 @@ ssh_proxy_fdpass_connect(struct ssh *ssh, const char *host, u_short port,
if ((pid = fork()) == 0) {
char *argv[10];

/* Child. Permanently give up superuser privileges. */
permanently_drop_suid(original_real_uid);

close(sp[1]);
/* Redirect stdin and stdout. */
if (sp[0] != 0) {
Expand Down Expand Up @@ -212,9 +208,6 @@ ssh_proxy_connect(struct ssh *ssh, const char *host, u_short port,
if ((pid = fork()) == 0) {
char *argv[10];

/* Child. Permanently give up superuser privileges. */
permanently_drop_suid(original_real_uid);

/* Redirect stdin and stdout. */
close(pin[1]);
if (pin[0] != 0) {
Expand Down Expand Up @@ -342,7 +335,7 @@ check_ifaddrs(const char *ifname, int af, const struct ifaddrs *ifaddrs,
static int
ssh_create_socket(int privileged, struct addrinfo *ai)
{
int sock, r, oerrno;
int sock, r;
struct sockaddr_storage bindaddr;
socklen_t bindaddrlen = 0;
struct addrinfo hints, *res = NULL;
Expand Down Expand Up @@ -409,22 +402,7 @@ ssh_create_socket(int privileged, struct addrinfo *ai)
ssh_gai_strerror(r));
goto fail;
}
/*
* If we are running as root and want to connect to a privileged
* port, bind our own socket to a privileged port.
*/
if (privileged) {
PRIV_START;
r = bindresvport_sa(sock,
bindaddrlen == 0 ? NULL : (struct sockaddr *)&bindaddr);
oerrno = errno;
PRIV_END;
if (r < 0) {
error("bindresvport_sa %s: %s", ntop,
strerror(oerrno));
goto fail;
}
} else if (bind(sock, (struct sockaddr *)&bindaddr, bindaddrlen) != 0) {
if (bind(sock, (struct sockaddr *)&bindaddr, bindaddrlen) != 0) {
error("bind %s: %s", ntop, strerror(errno));
goto fail;
}
Expand Down
21 changes: 1 addition & 20 deletions sshconnect.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: sshconnect.h,v 1.33 2018/07/16 11:05:41 dtucker Exp $ */
/* $OpenBSD: sshconnect.h,v 1.34 2018/07/18 11:34:04 dtucker Exp $ */

/*
* Copyright (c) 2000 Markus Friedl. All rights reserved.
Expand Down Expand Up @@ -57,22 +57,3 @@ void ssh_put_password(char *);
int ssh_local_cmd(const char *);

void maybe_add_key_to_agent(char *, const struct sshkey *, char *, char *);

/*
* Macros to raise/lower permissions.
*/
#define PRIV_START do { \
int save_errno = errno; \
if (seteuid(original_effective_uid) != 0) \
fatal("PRIV_START: seteuid: %s", \
strerror(errno)); \
errno = save_errno; \
} while (0)

#define PRIV_END do { \
int save_errno = errno; \
if (seteuid(original_real_uid) != 0) \
fatal("PRIV_END: seteuid: %s", \
strerror(errno)); \
errno = save_errno; \
} while (0)
3 changes: 1 addition & 2 deletions sshconnect2.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: sshconnect2.c,v 1.281 2018/07/16 11:05:41 dtucker Exp $ */
/* $OpenBSD: sshconnect2.c,v 1.282 2018/07/18 11:34:04 dtucker Exp $ */
/*
* Copyright (c) 2000 Markus Friedl. All rights reserved.
* Copyright (c) 2008 Damien Miller. All rights reserved.
Expand Down Expand Up @@ -1812,7 +1812,6 @@ ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp,
if (pid == 0) {
/* keep the socket on exec */
fcntl(sock, F_SETFD, 0);
permanently_drop_suid(getuid());
close(from[0]);
if (dup2(from[1], STDOUT_FILENO) < 0)
fatal("%s: dup2: %s", __func__, strerror(errno));
Expand Down
33 changes: 1 addition & 32 deletions uidswap.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: uidswap.c,v 1.40 2018/06/15 07:01:11 djm Exp $ */
/* $OpenBSD: uidswap.c,v 1.41 2018/07/18 11:34:04 dtucker Exp $ */
/*
* Author: Tatu Ylonen <[email protected]>
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
Expand Down Expand Up @@ -135,37 +135,6 @@ temporarily_use_uid(struct passwd *pw)
strerror(errno));
}

void
permanently_drop_suid(uid_t uid)
{
#ifndef NO_UID_RESTORATION_TEST
uid_t old_uid = getuid();
#endif

debug("permanently_drop_suid: %u", (u_int)uid);
if (setresuid(uid, uid, uid) < 0)
fatal("setresuid %u: %.100s", (u_int)uid, strerror(errno));

#ifndef NO_UID_RESTORATION_TEST
/*
* Try restoration of UID if changed (test clearing of saved uid).
*
* Note that we don't do this on Cygwin, or on Solaris-based platforms
* where fine-grained privileges are available (the user might be
* deliberately allowed the right to setuid back to root).
*/
if (old_uid != uid &&
(setuid(old_uid) != -1 || seteuid(old_uid) != -1))
fatal("%s: was able to restore old [e]uid", __func__);
#endif

/* Verify UID drop was successful */
if (getuid() != uid || geteuid() != uid) {
fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
__func__, (u_int)getuid(), (u_int)geteuid(), (u_int)uid);
}
}

/*
* Restores to the original (privileged) uid.
*/
Expand Down
3 changes: 1 addition & 2 deletions uidswap.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: uidswap.h,v 1.13 2006/08/03 03:34:42 deraadt Exp $ */
/* $OpenBSD: uidswap.h,v 1.14 2018/07/18 11:34:05 dtucker Exp $ */

/*
* Author: Tatu Ylonen <[email protected]>
Expand All @@ -15,4 +15,3 @@
void temporarily_use_uid(struct passwd *);
void restore_uid(void);
void permanently_set_uid(struct passwd *);
void permanently_drop_suid(uid_t);

0 comments on commit 258dc8b

Please sign in to comment.