]> granicus.if.org Git - strace/blob - mmap_cache.c
mmap_cache: record protection bits
[strace] / mmap_cache.c
1 /*
2  * Copyright (c) 2013 Luca Clementi <luca.clementi@gmail.com>
3  * Copyright (c) 2013-2018 The strace developers.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "defs.h"
29 #include <limits.h>
30
31 #include "xstring.h"
32
33 #ifdef _LARGEFILE64_SOURCE
34 # ifdef HAVE_FOPEN64
35 #  define fopen_for_input fopen64
36 # else
37 #  define fopen_for_input fopen
38 # endif
39 #else
40 # define fopen_for_input fopen
41 #endif
42
43 static unsigned int mmap_cache_generation;
44
45 /*
46  * caching of /proc/ID/maps for each process to speed up stack tracing
47  *
48  * The cache must be refreshed after syscalls that affect memory mappings,
49  * e.g. mmap, mprotect, munmap, execve.
50  */
51 static void
52 build_mmap_cache(struct tcb *tcp)
53 {
54         FILE *fp;
55         struct mmap_cache_t *cache_head = NULL;
56         /* start with a small dynamically-allocated array and then expand it */
57         size_t cur_array_size = 0;
58         char filename[sizeof("/proc/4294967296/maps")];
59         char buffer[PATH_MAX + 80];
60
61         xsprintf(filename, "/proc/%u/maps", tcp->pid);
62         fp = fopen_for_input(filename, "r");
63         if (!fp) {
64                 perror_msg("fopen: %s", filename);
65                 return;
66         }
67
68         while (fgets(buffer, sizeof(buffer), fp) != NULL) {
69                 struct mmap_cache_t *entry;
70                 unsigned long start_addr, end_addr, mmap_offset;
71                 char read_bit;
72                 char write_bit;
73                 char exec_bit;
74                 char shared_bit;
75                 char binary_path[sizeof(buffer)];
76
77                 if (sscanf(buffer, "%lx-%lx %c%c%c%c %lx %*x:%*x %*d %[^\n]",
78                            &start_addr, &end_addr,
79                            &read_bit, &write_bit, &exec_bit, &shared_bit,
80                            &mmap_offset, binary_path) != 8)
81                         continue;
82
83                 /* skip mappings that have unknown protection */
84                 if (!(read_bit == '-' || read_bit == 'r'))
85                         continue;
86                 if (!(write_bit == '-' || write_bit == 'w'))
87                         continue;
88                 if (!(exec_bit == '-' || exec_bit == 'x'))
89                         continue;
90                 if (!(shared_bit == 'p' || shared_bit == 's'))
91                         continue;
92
93                 if (end_addr < start_addr) {
94                         error_msg("%s: unrecognized file format", filename);
95                         break;
96                 }
97
98                 /*
99                  * sanity check to make sure that we're storing
100                  * non-overlapping regions in ascending order
101                  */
102                 if (tcp->mmap_cache_size > 0) {
103                         entry = &cache_head[tcp->mmap_cache_size - 1];
104                         if (entry->start_addr == start_addr &&
105                             entry->end_addr == end_addr) {
106                                 /* duplicate entry, e.g. [vsyscall] */
107                                 continue;
108                         }
109                         if (start_addr <= entry->start_addr ||
110                             start_addr < entry->end_addr) {
111                                 debug_msg("%s: overlapping memory region: "
112                                           "\"%s\" [%08lx-%08lx] overlaps with "
113                                           "\"%s\" [%08lx-%08lx]",
114                                           filename, binary_path, start_addr,
115                                           end_addr, entry->binary_filename,
116                                           entry->start_addr, entry->end_addr);
117                                 continue;
118                         }
119                 }
120
121                 if (tcp->mmap_cache_size >= cur_array_size)
122                         cache_head = xgrowarray(cache_head, &cur_array_size,
123                                                 sizeof(*cache_head));
124
125                 entry = &cache_head[tcp->mmap_cache_size];
126                 entry->start_addr = start_addr;
127                 entry->end_addr = end_addr;
128                 entry->mmap_offset = mmap_offset;
129                 entry->protections = (
130                         0
131                         | ((read_bit   == 'r')? MMAP_CACHE_PROT_READABLE  : 0)
132                         | ((write_bit  == 'w')? MMAP_CACHE_PROT_WRITABLE  : 0)
133                         | ((exec_bit   == 'x')? MMAP_CACHE_PROT_EXECUTABLE: 0)
134                         | ((shared_bit == 's')? MMAP_CACHE_PROT_SHARED    : 0)
135                         );
136                 entry->binary_filename = xstrdup(binary_path);
137                 tcp->mmap_cache_size++;
138         }
139         fclose(fp);
140         tcp->mmap_cache = cache_head;
141         tcp->mmap_cache_generation = mmap_cache_generation;
142
143         debug_func_msg("tgen=%u, ggen=%u, tcp=%p, cache=%p",
144                        tcp->mmap_cache_generation,
145                        mmap_cache_generation,
146                        tcp, tcp->mmap_cache);
147 }
148
149 /* deleting the cache */
150 extern void
151 mmap_cache_delete(struct tcb *tcp, const char *caller)
152 {
153         unsigned int i;
154
155         debug_func_msg("tgen=%u, ggen=%u, tcp=%p, cache=%p, caller=%s",
156                        tcp->mmap_cache_generation,
157                        mmap_cache_generation,
158                        tcp, tcp->mmap_cache, caller);
159
160         for (i = 0; i < tcp->mmap_cache_size; i++) {
161                 free(tcp->mmap_cache[i].binary_filename);
162                 tcp->mmap_cache[i].binary_filename = NULL;
163         }
164         free(tcp->mmap_cache);
165         tcp->mmap_cache = NULL;
166         tcp->mmap_cache_size = 0;
167 }
168
169 extern enum mmap_cache_rebuild_result
170 mmap_cache_rebuild_if_invalid(struct tcb *tcp, const char *caller)
171 {
172         enum mmap_cache_rebuild_result r = MMAP_CACHE_REBUILD_READY;
173         if ((tcp->mmap_cache_generation != mmap_cache_generation)
174             && tcp->mmap_cache)
175                 mmap_cache_delete(tcp, caller);
176
177         if (!tcp->mmap_cache) {
178                 r = MMAP_CACHE_REBUILD_RENEWED;
179                 build_mmap_cache(tcp);
180         }
181
182         if (!(tcp->mmap_cache && tcp->mmap_cache_size))
183                 r = MMAP_CACHE_REBUILD_NOCACHE;
184
185         return r;
186 }
187
188 void
189 mmap_cache_invalidate(struct tcb *tcp)
190 {
191 #if SUPPORTED_PERSONALITIES > 1
192         if (tcp->currpers != DEFAULT_PERSONALITY) {
193                 /* disable stack trace */
194                 return;
195         }
196 #endif
197         mmap_cache_generation++;
198         debug_func_msg("tgen=%u, ggen=%u, tcp=%p, cache=%p",
199                        tcp->mmap_cache_generation,
200                        mmap_cache_generation,
201                        tcp, tcp->mmap_cache);
202 }
203
204 struct mmap_cache_t *
205 mmap_cache_search(struct tcb *tcp, unsigned long ip)
206 {
207         int lower = 0;
208         int upper = (int) tcp->mmap_cache_size - 1;
209
210         while (lower <= upper) {
211                 struct mmap_cache_t *cur_mmap_cache;
212                 int mid = (upper + lower) / 2;
213
214                 cur_mmap_cache = &tcp->mmap_cache[mid];
215
216                 if (ip >= cur_mmap_cache->start_addr &&
217                     ip < cur_mmap_cache->end_addr)
218                         return cur_mmap_cache;
219                 else if (ip < cur_mmap_cache->start_addr)
220                         upper = mid - 1;
221                 else
222                         lower = mid + 1;
223         }
224         return NULL;
225 }