2002-06-05 02:15:46 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-01-19 23:34:15 +05:30
|
|
|
/*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Mini su implementation for busybox
|
2006-07-10 08:35:46 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2006-07-10 08:35:46 +05:30
|
|
|
*/
|
2015-10-19 04:50:36 +05:30
|
|
|
//config:config SU
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "su (19 kb)"
|
2015-10-19 04:50:36 +05:30
|
|
|
//config: default y
|
|
|
|
//config: select FEATURE_SYSLOG
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: su is used to become another user during a login session.
|
|
|
|
//config: Invoked without a username, su defaults to becoming the super user.
|
|
|
|
//config: Note that busybox binary must be setuid root for this applet to
|
|
|
|
//config: work properly.
|
2015-10-19 04:50:36 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_SU_SYSLOG
|
2017-01-10 19:28:54 +05:30
|
|
|
//config: bool "Log to syslog all attempts to use su"
|
2015-10-19 04:50:36 +05:30
|
|
|
//config: default y
|
|
|
|
//config: depends on SU
|
|
|
|
//config:
|
|
|
|
//config:config FEATURE_SU_CHECKS_SHELLS
|
2017-01-10 19:28:54 +05:30
|
|
|
//config: bool "If user's shell is not in /etc/shells, disallow -s PROG"
|
2015-10-19 04:50:36 +05:30
|
|
|
//config: default y
|
2017-01-10 19:28:54 +05:30
|
|
|
//config: depends on SU
|
2017-04-13 16:27:04 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_SU_BLANK_PW_NEEDS_SECURE_TTY
|
2017-04-13 17:07:05 +05:30
|
|
|
//config: bool "Allow blank passwords only on TTYs in /etc/securetty"
|
2017-04-13 16:27:04 +05:30
|
|
|
//config: default n
|
|
|
|
//config: depends on SU
|
2002-06-05 02:15:46 +05:30
|
|
|
|
2015-10-19 04:50:36 +05:30
|
|
|
//applet:/* Needs to be run by root or be suid root - needs to change uid and gid: */
|
|
|
|
//applet:IF_SU(APPLET(su, BB_DIR_BIN, BB_SUID_REQUIRE))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_SU) += su.o
|
2002-06-05 02:15:46 +05:30
|
|
|
|
2010-12-31 07:22:35 +05:30
|
|
|
//usage:#define su_trivial_usage
|
2021-06-15 13:30:18 +05:30
|
|
|
//usage: "[-lmp] [-s SH] [-] [USER [FILE ARGS | -c 'CMD' [ARG0 ARGS]]]"
|
2010-12-31 07:22:35 +05:30
|
|
|
//usage:#define su_full_usage "\n\n"
|
|
|
|
//usage: "Run shell under USER (by default, root)\n"
|
2016-11-04 02:43:08 +05:30
|
|
|
//usage: "\n -,-l Clear environment, go to home dir, run shell as login shell"
|
2010-12-31 07:22:35 +05:30
|
|
|
//usage: "\n -p,-m Do not set new $HOME, $SHELL, $USER, $LOGNAME"
|
|
|
|
//usage: "\n -c CMD Command to pass to 'sh -c'"
|
|
|
|
//usage: "\n -s SH Shell to use instead of user's default"
|
|
|
|
|
2015-10-19 04:50:36 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
#include <syslog.h>
|
|
|
|
|
2010-02-26 14:31:18 +05:30
|
|
|
#if ENABLE_FEATURE_SU_CHECKS_SHELLS
|
|
|
|
/* Return 1 if SHELL is a restricted shell (one not returned by
|
2010-06-27 06:53:31 +05:30
|
|
|
* getusershell), else 0, meaning it is a standard shell. */
|
2010-02-26 14:31:18 +05:30
|
|
|
static int restricted_shell(const char *shell)
|
|
|
|
{
|
|
|
|
char *line;
|
2010-06-27 06:53:31 +05:30
|
|
|
int result = 1;
|
2010-02-26 14:31:18 +05:30
|
|
|
|
|
|
|
/*setusershell(); - getusershell does it itself*/
|
|
|
|
while ((line = getusershell()) != NULL) {
|
2010-06-27 06:53:31 +05:30
|
|
|
if (/* *line != '#' && */ strcmp(line, shell) == 0) {
|
|
|
|
result = 0;
|
|
|
|
break;
|
|
|
|
}
|
2010-02-26 14:31:18 +05:30
|
|
|
}
|
2010-06-27 06:53:31 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
endusershell();
|
|
|
|
return result;
|
2010-02-26 14:31:18 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-12-19 14:25:38 +05:30
|
|
|
#define SU_OPT_mp (3)
|
2010-06-27 06:53:31 +05:30
|
|
|
#define SU_OPT_l (4)
|
2006-12-19 14:25:38 +05:30
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int su_main(int argc UNUSED_PARAM, char **argv)
|
2002-06-05 02:15:46 +05:30
|
|
|
{
|
2006-12-19 05:50:20 +05:30
|
|
|
unsigned flags;
|
2007-01-30 04:21:44 +05:30
|
|
|
char *opt_shell = NULL;
|
|
|
|
char *opt_command = NULL;
|
|
|
|
const char *opt_username = "root";
|
2003-08-29 13:08:56 +05:30
|
|
|
struct passwd *pw;
|
|
|
|
uid_t cur_uid = getuid();
|
|
|
|
const char *tty;
|
2010-11-30 14:17:56 +05:30
|
|
|
#if ENABLE_FEATURE_UTMP
|
2010-06-27 06:53:31 +05:30
|
|
|
char user_buf[64];
|
2010-11-30 14:17:56 +05:30
|
|
|
#endif
|
2010-06-27 06:53:31 +05:30
|
|
|
const char *old_user;
|
2017-04-13 16:27:04 +05:30
|
|
|
int r;
|
2002-06-05 02:15:46 +05:30
|
|
|
|
2016-11-04 02:43:08 +05:30
|
|
|
/* Note: we don't use "'+': stop at first non-option" idiom here.
|
|
|
|
* For su, "SCRIPT ARGS" or "-c CMD ARGS" do not stop option parsing:
|
|
|
|
* ARGS starting with dash will be treated as su options,
|
|
|
|
* not passed to shell. (Tested on util-linux 2.28).
|
|
|
|
*/
|
2007-08-18 21:02:12 +05:30
|
|
|
flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
|
2006-12-23 08:29:06 +05:30
|
|
|
argv += optind;
|
2003-08-29 13:08:56 +05:30
|
|
|
|
2008-03-17 14:37:36 +05:30
|
|
|
if (argv[0] && LONE_DASH(argv[0])) {
|
2006-06-14 22:06:45 +05:30
|
|
|
flags |= SU_OPT_l;
|
2006-12-17 05:19:13 +05:30
|
|
|
argv++;
|
2006-09-07 21:50:03 +05:30
|
|
|
}
|
2002-06-05 02:15:46 +05:30
|
|
|
|
|
|
|
/* get user if specified */
|
2008-03-17 14:37:36 +05:30
|
|
|
if (argv[0]) {
|
2006-12-17 05:19:13 +05:30
|
|
|
opt_username = argv[0];
|
|
|
|
argv++;
|
|
|
|
}
|
2006-07-10 08:35:46 +05:30
|
|
|
|
2017-04-13 16:27:04 +05:30
|
|
|
tty = xmalloc_ttyname(STDIN_FILENO);
|
|
|
|
if (!tty)
|
|
|
|
tty = "none";
|
|
|
|
tty = skip_dev_pfx(tty);
|
|
|
|
|
2006-12-19 14:25:38 +05:30
|
|
|
if (ENABLE_FEATURE_SU_SYSLOG) {
|
2010-06-27 06:53:31 +05:30
|
|
|
/* The utmp entry (via getlogin) is probably the best way to
|
|
|
|
* identify the user, especially if someone su's from a su-shell.
|
2009-09-10 02:42:10 +05:30
|
|
|
* But getlogin can fail -- usually due to lack of utmp entry.
|
|
|
|
* in this case resort to getpwuid. */
|
|
|
|
#if ENABLE_FEATURE_UTMP
|
2010-06-27 06:53:31 +05:30
|
|
|
old_user = user_buf;
|
2009-09-10 02:42:10 +05:30
|
|
|
if (getlogin_r(user_buf, sizeof(user_buf)) != 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
pw = getpwuid(cur_uid);
|
2010-06-27 06:53:31 +05:30
|
|
|
old_user = pw ? xstrdup(pw->pw_name) : "";
|
2009-09-10 02:42:10 +05:30
|
|
|
}
|
2006-10-04 02:30:43 +05:30
|
|
|
openlog(applet_name, 0, LOG_AUTH);
|
2003-08-29 13:08:56 +05:30
|
|
|
}
|
|
|
|
|
2008-12-04 00:35:55 +05:30
|
|
|
pw = xgetpwnam(opt_username);
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2017-04-13 16:27:04 +05:30
|
|
|
r = 1;
|
|
|
|
if (cur_uid != 0)
|
|
|
|
r = ask_and_check_password(pw);
|
|
|
|
if (r > 0) {
|
|
|
|
if (ENABLE_FEATURE_SU_BLANK_PW_NEEDS_SECURE_TTY
|
|
|
|
&& r == CHECKPASS_PW_HAS_EMPTY_PASSWORD
|
2017-04-13 16:34:05 +05:30
|
|
|
&& !is_tty_secure(tty)
|
2017-04-13 16:27:04 +05:30
|
|
|
) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2006-12-19 14:25:38 +05:30
|
|
|
if (ENABLE_FEATURE_SU_SYSLOG)
|
2006-12-19 05:50:20 +05:30
|
|
|
syslog(LOG_NOTICE, "%c %s %s:%s",
|
|
|
|
'+', tty, old_user, opt_username);
|
2003-08-29 13:08:56 +05:30
|
|
|
} else {
|
2017-04-13 16:27:04 +05:30
|
|
|
fail:
|
2006-12-19 14:25:38 +05:30
|
|
|
if (ENABLE_FEATURE_SU_SYSLOG)
|
2006-12-19 05:50:20 +05:30
|
|
|
syslog(LOG_NOTICE, "%c %s %s:%s",
|
|
|
|
'-', tty, old_user, opt_username);
|
2020-11-29 16:02:48 +05:30
|
|
|
pause_after_failed_login();
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg_and_die("incorrect password");
|
2002-06-05 02:15:46 +05:30
|
|
|
}
|
|
|
|
|
2006-12-19 14:25:38 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) {
|
2006-07-10 08:35:46 +05:30
|
|
|
closelog();
|
|
|
|
}
|
2003-08-29 13:08:56 +05:30
|
|
|
|
2010-06-27 06:53:31 +05:30
|
|
|
if (!opt_shell && (flags & SU_OPT_mp)) {
|
|
|
|
/* -s SHELL is not given, but "preserve env" opt is */
|
2006-12-19 05:50:20 +05:30
|
|
|
opt_shell = getenv("SHELL");
|
2010-06-27 06:53:31 +05:30
|
|
|
}
|
|
|
|
|
2006-12-19 05:50:20 +05:30
|
|
|
#if ENABLE_FEATURE_SU_CHECKS_SHELLS
|
2011-03-06 23:19:40 +05:30
|
|
|
if (opt_shell && cur_uid != 0 && pw->pw_shell && restricted_shell(pw->pw_shell)) {
|
2002-06-05 02:15:46 +05:30
|
|
|
/* The user being su'd to has a nonstandard shell, and so is
|
2010-06-27 06:53:31 +05:30
|
|
|
* probably a uucp account or has restricted access. Don't
|
|
|
|
* compromise the account by allowing access with a standard
|
|
|
|
* shell. */
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg("using restricted shell");
|
2011-03-06 23:19:40 +05:30
|
|
|
opt_shell = NULL; /* ignore -s PROG */
|
2002-06-05 02:15:46 +05:30
|
|
|
}
|
2010-06-27 06:53:31 +05:30
|
|
|
/* else: user can run whatever he wants via "su -s PROG USER".
|
|
|
|
* This is safe since PROG is run under user's uid/gid. */
|
2006-12-19 05:50:20 +05:30
|
|
|
#endif
|
|
|
|
if (!opt_shell)
|
|
|
|
opt_shell = pw->pw_shell;
|
2006-07-10 08:35:46 +05:30
|
|
|
|
|
|
|
change_identity(pw);
|
2010-02-26 14:22:45 +05:30
|
|
|
setup_environment(opt_shell,
|
2022-01-13 17:20:48 +05:30
|
|
|
((flags & SU_OPT_l) ? (SETUP_ENV_CLEARENV + SETUP_ENV_CHDIR) : 0)
|
|
|
|
+ (!(flags & SU_OPT_mp) * SETUP_ENV_CHANGEENV),
|
|
|
|
pw);
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_SELINUX(set_current_security_context(NULL);)
|
2002-06-05 02:15:46 +05:30
|
|
|
|
2016-11-04 02:43:08 +05:30
|
|
|
if (opt_command) {
|
|
|
|
*--argv = opt_command;
|
|
|
|
*--argv = (char*)"-c";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A nasty ioctl exists which can stuff data into input queue:
|
|
|
|
* #include <sys/ioctl.h>
|
|
|
|
* int main() {
|
|
|
|
* const char *msg = "echo $UID\n";
|
|
|
|
* while (*msg) ioctl(0, TIOCSTI, *msg++);
|
|
|
|
* return 0;
|
|
|
|
* }
|
|
|
|
* With "su USER -c EXPLOIT" run by root, exploit can make root shell
|
|
|
|
* read as input and execute arbitrary command.
|
|
|
|
* It's debatable whether we need to protect against this
|
|
|
|
* (root may hesitate to run unknown scripts interactively).
|
|
|
|
*
|
|
|
|
* Some versions of su run -c CMD in a different session:
|
|
|
|
* ioctl(TIOCSTI) works only on the controlling tty.
|
|
|
|
*/
|
|
|
|
|
2006-07-16 01:16:46 +05:30
|
|
|
/* Never returns */
|
2020-12-03 01:58:47 +05:30
|
|
|
exec_shell(opt_shell, flags & SU_OPT_l, (const char**)argv);
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2007-09-10 18:45:28 +05:30
|
|
|
/* return EXIT_FAILURE; - not reached */
|
2002-06-05 02:15:46 +05:30
|
|
|
}
|