diff options
Diffstat (limited to 'auth.c')
-rw-r--r-- | auth.c | 155 |
1 files changed, 154 insertions, 1 deletions
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: auth.c,v 1.124 2017/09/12 06:32:07 djm Exp $ */ | 1 | /* $OpenBSD: auth.c,v 1.125 2018/01/08 15:21:49 markus Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | 3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
4 | * | 4 | * |
@@ -28,6 +28,7 @@ | |||
28 | #include <sys/types.h> | 28 | #include <sys/types.h> |
29 | #include <sys/stat.h> | 29 | #include <sys/stat.h> |
30 | #include <sys/socket.h> | 30 | #include <sys/socket.h> |
31 | #include <sys/wait.h> | ||
31 | 32 | ||
32 | #include <netinet/in.h> | 33 | #include <netinet/in.h> |
33 | 34 | ||
@@ -840,3 +841,155 @@ auth_get_canonical_hostname(struct ssh *ssh, int use_dns) | |||
840 | return dnsname; | 841 | return dnsname; |
841 | } | 842 | } |
842 | } | 843 | } |
844 | |||
845 | /* | ||
846 | * Runs command in a subprocess wuth a minimal environment. | ||
847 | * Returns pid on success, 0 on failure. | ||
848 | * The child stdout and stderr maybe captured, left attached or sent to | ||
849 | * /dev/null depending on the contents of flags. | ||
850 | * "tag" is prepended to log messages. | ||
851 | * NB. "command" is only used for logging; the actual command executed is | ||
852 | * av[0]. | ||
853 | */ | ||
854 | pid_t | ||
855 | subprocess(const char *tag, struct passwd *pw, const char *command, | ||
856 | int ac, char **av, FILE **child, u_int flags) | ||
857 | { | ||
858 | FILE *f = NULL; | ||
859 | struct stat st; | ||
860 | int fd, devnull, p[2], i; | ||
861 | pid_t pid; | ||
862 | char *cp, errmsg[512]; | ||
863 | u_int envsize; | ||
864 | char **child_env; | ||
865 | |||
866 | if (child != NULL) | ||
867 | *child = NULL; | ||
868 | |||
869 | debug3("%s: %s command \"%s\" running as %s (flags 0x%x)", __func__, | ||
870 | tag, command, pw->pw_name, flags); | ||
871 | |||
872 | /* Check consistency */ | ||
873 | if ((flags & SSH_SUBPROCESS_STDOUT_DISCARD) != 0 && | ||
874 | (flags & SSH_SUBPROCESS_STDOUT_CAPTURE) != 0) { | ||
875 | error("%s: inconsistent flags", __func__); | ||
876 | return 0; | ||
877 | } | ||
878 | if (((flags & SSH_SUBPROCESS_STDOUT_CAPTURE) == 0) != (child == NULL)) { | ||
879 | error("%s: inconsistent flags/output", __func__); | ||
880 | return 0; | ||
881 | } | ||
882 | |||
883 | /* | ||
884 | * If executing an explicit binary, then verify the it exists | ||
885 | * and appears safe-ish to execute | ||
886 | */ | ||
887 | if (*av[0] != '/') { | ||
888 | error("%s path is not absolute", tag); | ||
889 | return 0; | ||
890 | } | ||
891 | temporarily_use_uid(pw); | ||
892 | if (stat(av[0], &st) < 0) { | ||
893 | error("Could not stat %s \"%s\": %s", tag, | ||
894 | av[0], strerror(errno)); | ||
895 | restore_uid(); | ||
896 | return 0; | ||
897 | } | ||
898 | if (safe_path(av[0], &st, NULL, 0, errmsg, sizeof(errmsg)) != 0) { | ||
899 | error("Unsafe %s \"%s\": %s", tag, av[0], errmsg); | ||
900 | restore_uid(); | ||
901 | return 0; | ||
902 | } | ||
903 | /* Prepare to keep the child's stdout if requested */ | ||
904 | if (pipe(p) != 0) { | ||
905 | error("%s: pipe: %s", tag, strerror(errno)); | ||
906 | restore_uid(); | ||
907 | return 0; | ||
908 | } | ||
909 | restore_uid(); | ||
910 | |||
911 | switch ((pid = fork())) { | ||
912 | case -1: /* error */ | ||
913 | error("%s: fork: %s", tag, strerror(errno)); | ||
914 | close(p[0]); | ||
915 | close(p[1]); | ||
916 | return 0; | ||
917 | case 0: /* child */ | ||
918 | /* Prepare a minimal environment for the child. */ | ||
919 | envsize = 5; | ||
920 | child_env = xcalloc(sizeof(*child_env), envsize); | ||
921 | child_set_env(&child_env, &envsize, "PATH", _PATH_STDPATH); | ||
922 | child_set_env(&child_env, &envsize, "USER", pw->pw_name); | ||
923 | child_set_env(&child_env, &envsize, "LOGNAME", pw->pw_name); | ||
924 | child_set_env(&child_env, &envsize, "HOME", pw->pw_dir); | ||
925 | if ((cp = getenv("LANG")) != NULL) | ||
926 | child_set_env(&child_env, &envsize, "LANG", cp); | ||
927 | |||
928 | for (i = 0; i < NSIG; i++) | ||
929 | signal(i, SIG_DFL); | ||
930 | |||
931 | if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) { | ||
932 | error("%s: open %s: %s", tag, _PATH_DEVNULL, | ||
933 | strerror(errno)); | ||
934 | _exit(1); | ||
935 | } | ||
936 | if (dup2(devnull, STDIN_FILENO) == -1) { | ||
937 | error("%s: dup2: %s", tag, strerror(errno)); | ||
938 | _exit(1); | ||
939 | } | ||
940 | |||
941 | /* Set up stdout as requested; leave stderr in place for now. */ | ||
942 | fd = -1; | ||
943 | if ((flags & SSH_SUBPROCESS_STDOUT_CAPTURE) != 0) | ||
944 | fd = p[1]; | ||
945 | else if ((flags & SSH_SUBPROCESS_STDOUT_DISCARD) != 0) | ||
946 | fd = devnull; | ||
947 | if (fd != -1 && dup2(fd, STDOUT_FILENO) == -1) { | ||
948 | error("%s: dup2: %s", tag, strerror(errno)); | ||
949 | _exit(1); | ||
950 | } | ||
951 | closefrom(STDERR_FILENO + 1); | ||
952 | |||
953 | /* Don't use permanently_set_uid() here to avoid fatal() */ | ||
954 | if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) { | ||
955 | error("%s: setresgid %u: %s", tag, (u_int)pw->pw_gid, | ||
956 | strerror(errno)); | ||
957 | _exit(1); | ||
958 | } | ||
959 | if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) { | ||
960 | error("%s: setresuid %u: %s", tag, (u_int)pw->pw_uid, | ||
961 | strerror(errno)); | ||
962 | _exit(1); | ||
963 | } | ||
964 | /* stdin is pointed to /dev/null at this point */ | ||
965 | if ((flags & SSH_SUBPROCESS_STDOUT_DISCARD) != 0 && | ||
966 | dup2(STDIN_FILENO, STDERR_FILENO) == -1) { | ||
967 | error("%s: dup2: %s", tag, strerror(errno)); | ||
968 | _exit(1); | ||
969 | } | ||
970 | |||
971 | execve(av[0], av, child_env); | ||
972 | error("%s exec \"%s\": %s", tag, command, strerror(errno)); | ||
973 | _exit(127); | ||
974 | default: /* parent */ | ||
975 | break; | ||
976 | } | ||
977 | |||
978 | close(p[1]); | ||
979 | if ((flags & SSH_SUBPROCESS_STDOUT_CAPTURE) == 0) | ||
980 | close(p[0]); | ||
981 | else if ((f = fdopen(p[0], "r")) == NULL) { | ||
982 | error("%s: fdopen: %s", tag, strerror(errno)); | ||
983 | close(p[0]); | ||
984 | /* Don't leave zombie child */ | ||
985 | kill(pid, SIGTERM); | ||
986 | while (waitpid(pid, NULL, 0) == -1 && errno == EINTR) | ||
987 | ; | ||
988 | return 0; | ||
989 | } | ||
990 | /* Success */ | ||
991 | debug3("%s: %s pid %ld", __func__, tag, (long)pid); | ||
992 | if (child != NULL) | ||
993 | *child = f; | ||
994 | return pid; | ||
995 | } | ||