GDB (xrefs)
linux-personality.c
Go to the documentation of this file.
1 /* Disable address space randomization based on inferior personality.
2 
3  Copyright (C) 2008-2015 Free Software Foundation, Inc.
4 
5  This file is part of GDB.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 
20 #include "common-defs.h"
21 #include "nat/linux-personality.h"
22 
23 #ifdef HAVE_PERSONALITY
24 # include <sys/personality.h>
25 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
26 # define ADDR_NO_RANDOMIZE 0x0040000
27 # endif /* ! HAVE_DECL_ADDR_NO_RANDOMIZE */
28 #endif /* HAVE_PERSONALITY */
29 
30 #ifdef HAVE_PERSONALITY
31 
32 /* Restore address space randomization of the inferior. ARG is the
33  original inferior's personality value before the address space
34  randomization was disabled. */
35 
36 static void
38 {
39  int personality_orig = (int) (uintptr_t) arg;
40 
41  errno = 0;
42  personality (personality_orig);
43  if (errno != 0)
44  warning (_("Error restoring address space randomization: %s"),
45  safe_strerror (errno));
46 }
47 #endif /* HAVE_PERSONALITY */
48 
49 /* Return a cleanup responsible for restoring the inferior's
50  personality (and restoring the inferior's address space
51  randomization) if HAVE_PERSONALITY and if PERSONALITY_SET is not
52  zero; return a null cleanup if not HAVE_PERSONALITY or if
53  PERSONALITY_SET is zero. */
54 
55 static struct cleanup *
56 make_disable_asr_cleanup (int personality_set, int personality_orig)
57 {
58 #ifdef HAVE_PERSONALITY
59  if (personality_set != 0)
61  (void *) (uintptr_t) personality_orig);
62 #endif /* HAVE_PERSONALITY */
63 
64  return make_cleanup (null_cleanup, NULL);
65 }
66 
67 /* See comment on nat/linux-personality.h. */
68 
69 struct cleanup *
71 {
72  int personality_orig = 0;
73  int personality_set = 0;
74 
75 #ifdef HAVE_PERSONALITY
76  if (disable_randomization)
77  {
78  errno = 0;
79  personality_orig = personality (0xffffffff);
80  if (errno == 0 && !(personality_orig & ADDR_NO_RANDOMIZE))
81  {
82  personality_set = 1;
83  personality (personality_orig | ADDR_NO_RANDOMIZE);
84  }
85  if (errno != 0 || (personality_set
86  && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE)))
87  warning (_("Error disabling address space randomization: %s"),
88  safe_strerror (errno));
89  }
90 #endif /* HAVE_PERSONALITY */
91 
92  return make_disable_asr_cleanup (personality_set,
93  personality_orig);
94 }
void warning(const char *fmt,...)
Definition: errors.c:26
int disable_randomization
Definition: infrun.c:151
#define _(String)
Definition: gdb_locale.h:40
static void restore_personality(void *arg)
void null_cleanup(void *arg)
Definition: cleanups.c:295
struct cleanup * make_cleanup(make_cleanup_ftype *function, void *arg)
Definition: cleanups.c:117
struct cleanup * maybe_disable_address_space_randomization(int disable_randomization)
const char const char int
Definition: command.h:229
char * safe_strerror(int)
void * arg
Definition: cleanups.c:43
static struct cleanup * make_disable_asr_cleanup(int personality_set, int personality_orig)