blob: cf8ef00647f110b5398fa76dc39fb7cd046aaecb [file] [log] [blame]
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenko7fe21c62008-08-23 23:15:17 +00003 * linux32/linux64 allows for changing uname emulation.
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +00004 *
5 * Copyright 2002 Andi Kleen, SuSE Labs.
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Pere Orga5bc8c002011-04-11 03:29:49 +02008 */
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +02009//config:config SETARCH
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "setarch (3.6 kb)"
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020011//config: default y
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020012//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: The linux32 utility is used to create a 32bit environment for the
14//config: specified program (usually a shell). It only makes sense to have
15//config: this util on a system that supports both 64bit and 32bit userland
16//config: (like amd64/x86, ppc64/ppc, sparc64/sparc, etc...).
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010017//config:
18//config:config LINUX32
Denys Vlasenkob097a842018-12-28 03:20:17 +010019//config: bool "linux32 (3.3 kb)"
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010020//config: default y
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010021//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020022//config: Alias to "setarch linux32".
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010023//config:
24//config:config LINUX64
Denys Vlasenkob097a842018-12-28 03:20:17 +010025//config: bool "linux64 (3.3 kb)"
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010026//config: default y
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010027//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020028//config: Alias to "setarch linux64".
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020029
Denys Vlasenko5c527dc2017-08-04 19:55:01 +020030//applet:IF_SETARCH(APPLET_NOEXEC(setarch, setarch, BB_DIR_BIN, BB_SUID_DROP, setarch))
31// APPLET_NOEXEC:name main location suid_type help
32//applet:IF_LINUX32(APPLET_NOEXEC(linux32, setarch, BB_DIR_BIN, BB_SUID_DROP, linux32))
33//applet:IF_LINUX64(APPLET_NOEXEC(linux64, setarch, BB_DIR_BIN, BB_SUID_DROP, linux64))
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020034
35//kbuild:lib-$(CONFIG_SETARCH) += setarch.o
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010036//kbuild:lib-$(CONFIG_LINUX32) += setarch.o
37//kbuild:lib-$(CONFIG_LINUX64) += setarch.o
Pere Orga5bc8c002011-04-11 03:29:49 +020038
39//usage:#define setarch_trivial_usage
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020040//usage: "PERSONALITY [-R] PROG ARGS"
Pere Orga5bc8c002011-04-11 03:29:49 +020041//usage:#define setarch_full_usage "\n\n"
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020042//usage: "PERSONALITY may be:"
43//usage: "\n"" linux32 Set 32bit uname emulation"
44//usage: "\n"" linux64 Set 64bit uname emulation"
45//usage: "\n"
46//usage: "\n"" -R Disable address space randomization"
Pere Orga5bc8c002011-04-11 03:29:49 +020047//usage:
48//usage:#define linux32_trivial_usage NOUSAGE_STR
49//usage:#define linux32_full_usage ""
50//usage:
51//usage:#define linux64_trivial_usage NOUSAGE_STR
52//usage:#define linux64_full_usage ""
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000053
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020054#include "libbb.h"
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000055#include <sys/personality.h>
56
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020057#ifndef ADDR_NO_RANDOMIZE
58# define ADDR_NO_RANDOMIZE 0x0040000
59#endif
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000060
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000061int setarch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000062int setarch_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000063{
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020064 unsigned opts;
65 unsigned long pers;
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000066
67 /* Figure out what personality we are supposed to switch to ...
68 * we can be invoked as either:
Denis Vlasenko7fe21c62008-08-23 23:15:17 +000069 * argv[0],argv[1] == "setarch","personality"
70 * argv[0] == "personality"
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000071 */
Denis Vlasenko7fe21c62008-08-23 23:15:17 +000072 if (ENABLE_SETARCH && applet_name[0] == 's'
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020073 && argv[1] && is_prefixed_with(argv[1], "linux")
Denis Vlasenko7fe21c62008-08-23 23:15:17 +000074 ) {
Denis Vlasenko7fe21c62008-08-23 23:15:17 +000075 argv++;
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010076 applet_name = argv[0];
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000077 }
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010078 if ((!ENABLE_SETARCH && !ENABLE_LINUX32) || applet_name[5] == '6')
79 /* linux64 */
Denis Vlasenko7fe21c62008-08-23 23:15:17 +000080 pers = PER_LINUX;
Denys Vlasenkoc314ca92016-11-13 22:56:04 +010081 else
82 if ((!ENABLE_SETARCH && !ENABLE_LINUX64) || applet_name[5] == '3')
83 /* linux32 */
Denis Vlasenko7fe21c62008-08-23 23:15:17 +000084 pers = PER_LINUX32;
85 else
86 bb_show_usage();
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000087
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020088 opts = getopt32(argv, "+R"); /* '+': stop at first non-option */
89 if (opts)
90 pers |= ADDR_NO_RANDOMIZE;
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000091
92 /* Try to set personality */
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020093 if (personality(pers) < 0)
94 bb_perror_msg_and_die("personality(0x%lx)", pers);
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +000095
Denys Vlasenkof2ccefb2015-10-19 23:26:50 +020096 argv += optind;
97 if (!argv[0])
98 (--argv)[0] = (char*)"/bin/sh";
99
100 /* Try to execute the program */
101 BB_EXECVP_or_die(argv);
Mike Frysinger0a6b0bf2006-02-21 04:26:52 +0000102}