1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
/*
* Copyright (C) 2003-2007 Red Hat, Inc.
*
* This file is part of util-linux-ng.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* Written by Elliot Lee <sopwith@redhat.com>
* New personality options & code added by Jindrich Novy <jnovy@redhat.com>
* ADD_NO_RANDOMIZE flag added by Arjan van de Ven <arjanv@redhat.com>
* Help and MIPS support from Mike Frysinger (vapier@gentoo.org)
* Better error handling from Dmitry V. Levin (ldv@altlinux.org)
*
* based on ideas from the ppc32 util by Guy Streeter (2002-01), based on the
* sparc32 util by Jakub Jelinek (1998, 1999)
*/
#include <syscall.h>
#include <linux/personality.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <error.h>
#include <getopt.h>
#include <limits.h>
#include <sys/utsname.h>
#include "nls.h"
#define set_pers(pers) ((long)syscall(SYS_personality, pers))
/* Option --4gb has no equivalent short option, use a non-character as a
pseudo short option. */
#define OPT_4GB (CHAR_MAX+1)
#define turn_on(_flag, _opts) \
do { \
(_opts) |= _flag; \
if (verbose) \
printf(_("Switching on %s.\n"), #_flag); \
} while(0)
#if !HAVE_DECL_ADDR_NO_RANDOMIZE
# define ADDR_NO_RANDOMIZE 0x0040000
#endif
#if !HAVE_DECL_FDPIC_FUNCPTRS
# define FDPIC_FUNCPTRS 0x0080000
#endif
#if !HAVE_DECL_MMAP_PAGE_ZERO
# define MMAP_PAGE_ZERO 0x0100000
#endif
#if !HAVE_DECL_ADDR_COMPAT_LAYOUT
# define ADDR_COMPAT_LAYOUT 0x0200000
#endif
#if !HAVE_DECL_READ_IMPLIES_EXEC
# define READ_IMPLIES_EXEC 0x0400000
#endif
#if !HAVE_DECL_ADDR_LIMIT_32BIT
# define ADDR_LIMIT_32BIT 0x0800000
#endif
#if !HAVE_DECL_SHORT_INODE
# define SHORT_INODE 0x1000000
#endif
#if !HAVE_DECL_WHOLE_SECONDS
# define WHOLE_SECONDS 0x2000000
#endif
#if !HAVE_DECL_STICKY_TIMEOUTS
# define STICKY_TIMEOUTS 0x4000000
#endif
#if !HAVE_DECL_ADDR_LIMIT_3GB
# define ADDR_LIMIT_3GB 0x8000000
#endif
/* Options --3gb and --4gb are for compatibitity with an old Debian setarch
implementation. */
struct option longopts[] =
{
{ "help", 0, 0, 'h' },
{ "verbose", 0, 0, 'v' },
{ "addr-no-randomize", 0, 0, 'R' },
{ "fdpic-funcptrs", 0, 0, 'F' },
{ "mmap-page-zero", 0, 0, 'Z' },
{ "addr-compat-layout", 0, 0, 'L' },
{ "read-implies-exec", 0, 0, 'X' },
{ "32bit", 0, 0, 'B' },
{ "short-inode", 0, 0, 'I' },
{ "whole-seconds", 0, 0, 'S' },
{ "sticky-timeouts", 0, 0, 'T' },
{ "3gb", 0, 0, '3' },
{ "4gb", 0, 0, OPT_4GB },
{ NULL, 0, 0, 0 }
};
static void __attribute__((__noreturn__))
show_help(void)
{
const char *p = program_invocation_short_name;
if (!*p)
p = "setarch";
printf(_("Usage: %s%s [options] [program [program arguments]]\n\nOptions:\n"),
p, !strcmp(p, "setarch") ? " <arch>" : "");
printf(_(
" -h, --help displays this help text\n"
" -v, --verbose says what options are being switched on\n"
" -R, --addr-no-randomize disables randomization of the virtual address space\n"
" -F, --fdpic-funcptrs makes function pointers point to descriptors\n"
" -Z, --mmap-page-zero turns on MMAP_PAGE_ZERO\n"
" -L, --addr-compat-layout changes the way virtual memory is allocated\n"
" -X, --read-implies-exec turns on READ_IMPLIES_EXEC\n"
" -B, --32bit turns on ADDR_LIMIT_32BIT\n"
" -I, --short-inode turns on SHORT_INODE\n"
" -S, --whole-seconds turns on WHOLE_SECONDS\n"
" -T, --sticky-timeouts turns on STICKY_TIMEOUTS\n"
" -3, --3gb limits the used address space to a maximum of 3 GB\n"
" --4gb ignored (for backward compatibility only)\n"));
printf(_("\nFor more information see setarch(8).\n"));
exit(EXIT_SUCCESS);
}
static void __attribute__((__noreturn__))
show_usage(const char *s)
{
const char *p = program_invocation_short_name;
if (!*p)
p = "setarch";
fprintf(stderr, _("%s: %s\nTry `%s --help' for more information.\n"), p, s, p);
exit(EXIT_FAILURE);
}
int set_arch(const char *pers, unsigned long options)
{
struct utsname un;
int i;
unsigned long pers_value, res;
struct {
int perval;
const char *target_arch, *result_arch;
} transitions[] = {
{PER_LINUX32, "linux32", NULL},
{PER_LINUX, "linux64", NULL},
#if defined(__powerpc__) || defined(__powerpc64__)
{PER_LINUX32, "ppc32", "ppc"},
{PER_LINUX32, "ppc", "ppc"},
{PER_LINUX, "ppc64", "ppc64"},
{PER_LINUX, "ppc64pseries", "ppc64"},
{PER_LINUX, "ppc64iseries", "ppc64"},
#endif
#if defined(__x86_64__) || defined(__i386__) || defined(__ia64__)
{PER_LINUX32, "i386", "i386"},
{PER_LINUX32, "i486", "i386"},
{PER_LINUX32, "i586", "i386"},
{PER_LINUX32, "i686", "i386"},
{PER_LINUX32, "athlon", "i386"},
#endif
#if defined(__x86_64__) || defined(__i386__)
{PER_LINUX, "x86_64", "x86_64"},
#endif
#if defined(__ia64__) || defined(__i386__)
{PER_LINUX, "ia64", "ia64"},
#endif
#if defined(__hppa__)
{PER_LINUX32, "parisc32", "parisc"},
{PER_LINUX32, "parisc", "parisc"},
{PER_LINUX, "parisc64", "parisc64"},
#endif
#if defined(__s390x__) || defined(__s390__)
{PER_LINUX32, "s390", "s390"},
{PER_LINUX, "s390x", "s390x"},
#endif
#if defined(__sparc64__) || defined(__sparc__)
{PER_LINUX32, "sparc", "sparc"},
{PER_LINUX32, "sparc32bash", "sparc"},
{PER_LINUX32, "sparc32", "sparc"},
{PER_LINUX, "sparc64", "sparc64"},
#endif
#if defined(__mips64__) || defined(__mips__)
{PER_LINUX32, "mips32", "mips"},
{PER_LINUX32, "mips", "mips"},
{PER_LINUX, "mips64", "mips64"},
#endif
#if defined(__alpha__)
{PER_LINUX, "alpha", "alpha"},
{PER_LINUX, "alphaev5", "alpha"},
{PER_LINUX, "alphaev56", "alpha"},
{PER_LINUX, "alphaev6", "alpha"},
{PER_LINUX, "alphaev67", "alpha"},
#endif
{-1, NULL, NULL}
};
for(i = 0; transitions[i].perval >= 0; i++)
if(!strcmp(pers, transitions[i].target_arch))
break;
if(transitions[i].perval < 0)
error(EXIT_FAILURE, 0, _("%s: Unrecognized architecture"), pers);
pers_value = transitions[i].perval | options;
res = set_pers(pers_value);
if(res == -EINVAL)
return 1;
uname(&un);
if(transitions[i].result_arch &&
strcmp(un.machine, transitions[i].result_arch))
{
if(strcmp(transitions[i].result_arch, "i386")
|| (strcmp(un.machine, "i486")
&& strcmp(un.machine, "i586")
&& strcmp(un.machine, "i686")
&& strcmp(un.machine, "athlon")))
error(EXIT_FAILURE, 0, _("%s: Unrecognized architecture"), pers);
}
return 0;
}
int main(int argc, char *argv[])
{
const char *p;
unsigned long options = 0;
int verbose = 0;
int c;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
if (argc < 1)
show_usage(_("Not enough arguments"));
p = program_invocation_short_name;
if (!strcmp(p, "setarch")) {
argv++;
argc--;
if (argc < 1)
show_usage(_("Not enough arguments"));
p = argv[0];
argv[0] = argv[-1]; /* for getopt_long() to get the program name */
if (!strcmp(p, "-h") || !strcmp(p, "--help"))
show_help();
}
#if defined(__sparc64__) || defined(__sparc__)
if (!strcmp(p, "sparc32bash")) {
if (set_arch(p, 0L))
error(EXIT_FAILURE, errno, _("Failed to set personality to %s"), p);
execl("/bin/bash", NULL);
error(EXIT_FAILURE, errno, "/bin/bash");
}
#endif
while ((c = getopt_long(argc, argv, "+hv3BFILRSTXZ", longopts, NULL)) != -1) {
switch (c) {
case 'h':
show_help();
break;
case 'v':
verbose = 1;
break;
case 'R':
turn_on(ADDR_NO_RANDOMIZE, options);
break;
case 'F':
turn_on(FDPIC_FUNCPTRS, options);
break;
case 'Z':
turn_on(MMAP_PAGE_ZERO, options);
break;
case 'L':
turn_on(ADDR_COMPAT_LAYOUT, options);
break;
case 'X':
turn_on(READ_IMPLIES_EXEC, options);
break;
case 'B':
turn_on(ADDR_LIMIT_32BIT, options);
break;
case 'I':
turn_on(SHORT_INODE, options);
break;
case 'S':
turn_on(WHOLE_SECONDS, options);
break;
case 'T':
turn_on(STICKY_TIMEOUTS, options);
break;
case '3':
turn_on(ADDR_LIMIT_3GB, options);
break;
case OPT_4GB: /* just ignore this one */
break;
}
}
argc -= optind;
argv += optind;
if (set_arch(p, options))
error(EXIT_FAILURE, errno, _("Failed to set personality to %s"), p);
if (!argc) {
execl("/bin/sh", "-sh", NULL);
error(EXIT_FAILURE, errno, "/bin/sh");
}
execvp(argv[0], argv);
error(EXIT_FAILURE, errno, "%s", argv[0]);
return EXIT_FAILURE;
}
|