use setresuid(2) and friends instead, saves 88t 4d 1i

XXX revisit this if we encounter systems without at least
XXX one of setresuid, setresgid, setgroups - only tested on BSD
This commit is contained in:
tg 2006-11-10 06:27:09 +00:00
parent c942031805
commit a199d23dfa
3 changed files with 10 additions and 17 deletions

11
main.c
View File

@ -13,7 +13,7 @@
#include <locale.h> #include <locale.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.56 2006/11/10 05:27:43 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/main.c,v 1.57 2006/11/10 06:27:09 tg Exp $");
extern char **environ; extern char **environ;
@ -224,12 +224,7 @@ main(int argc, char *argv[])
; ;
} }
ksheuid = geteuid(); safe_prompt = (ksheuid = geteuid()) ? "$ " : "# ";
kshuid = getuid();
kshegid = getegid();
kshgid = getgid();
safe_prompt = ksheuid ? "$ " : "# ";
vp = global("PS1"); vp = global("PS1");
/* Set PS1 if unset or we are root and prompt doesn't contain a # */ /* Set PS1 if unset or we are root and prompt doesn't contain a # */
if (!(vp->flag & ISSET) || if (!(vp->flag & ISSET) ||
@ -238,7 +233,7 @@ main(int argc, char *argv[])
setstr(vp, safe_prompt, KSH_RETURN_ERROR); setstr(vp, safe_prompt, KSH_RETURN_ERROR);
/* Set this before parsing arguments */ /* Set this before parsing arguments */
Flag(FPRIVILEGED) = kshuid != ksheuid || kshgid != kshegid; Flag(FPRIVILEGED) = getuid() != ksheuid || getgid() != getegid();
/* this to note if monitor is set on command line (see below) */ /* this to note if monitor is set on command line (see below) */
Flag(FMONITOR) = 127; Flag(FMONITOR) = 127;

11
misc.c
View File

@ -3,7 +3,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.34 2006/11/10 05:21:38 tg Exp $\t" __RCSID("$MirOS: src/bin/mksh/misc.c,v 1.35 2006/11/10 06:27:09 tg Exp $\t"
MKSH_SH_H_ID); MKSH_SH_H_ID);
#undef USE_CHVT #undef USE_CHVT
@ -239,10 +239,11 @@ change_flag(enum sh_flag f,
Flag(f) = newval; Flag(f) = newval;
} else if (f == FPRIVILEGED && oldval && !newval) { } else if (f == FPRIVILEGED && oldval && !newval) {
/* Turning off -p? */ /* Turning off -p? */
seteuid(ksheuid = kshuid = getuid()); gid_t kshegid = getgid();
setuid(ksheuid);
setegid(kshegid = kshgid = getgid()); setresgid(kshegid, kshegid, kshegid);
setgid(kshegid); setgroups(1, &kshegid);
setresuid(ksheuid, ksheuid, ksheuid);
} else if (f == FPOSIX && newval) { } else if (f == FPOSIX && newval) {
Flag(FBRACEEXPAND) = 0; Flag(FBRACEEXPAND) = 0;
} }

5
sh.h
View File

@ -8,7 +8,7 @@
/* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */ /* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */
/* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */ /* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */
#define MKSH_SH_H_ID "$MirOS: src/bin/mksh/sh.h,v 1.75 2006/11/10 06:16:25 tg Exp $" #define MKSH_SH_H_ID "$MirOS: src/bin/mksh/sh.h,v 1.76 2006/11/10 06:27:09 tg Exp $"
#define MKSH_VERSION "R29 2006/11/10" #define MKSH_VERSION "R29 2006/11/10"
#if HAVE_SYS_PARAM_H #if HAVE_SYS_PARAM_H
@ -162,9 +162,6 @@ EXTERN char *kshname; /* $0 */
EXTERN pid_t kshpid; /* $$, shell pid */ EXTERN pid_t kshpid; /* $$, shell pid */
EXTERN pid_t procpid; /* pid of executing process */ EXTERN pid_t procpid; /* pid of executing process */
EXTERN uid_t ksheuid; /* effective uid of shell */ EXTERN uid_t ksheuid; /* effective uid of shell */
EXTERN uid_t kshuid; /* real uid of shell */
EXTERN gid_t kshegid; /* effective gid of shell */
EXTERN gid_t kshgid; /* real gid of shell */
EXTERN int exstat; /* exit status */ EXTERN int exstat; /* exit status */
EXTERN int subst_exstat; /* exit status of last $(..)/`..` */ EXTERN int subst_exstat; /* exit status of last $(..)/`..` */
EXTERN const char *safe_prompt; /* safe prompt if PS1 substitution fails */ EXTERN const char *safe_prompt; /* safe prompt if PS1 substitution fails */