| 1 |
/* -*- linux-c -*- |
| 2 |
* Map of addresses to disallow. |
| 3 |
* Copyright (C) 2005-2019 Red Hat Inc. |
| 4 |
* |
| 5 |
* This file is part of systemtap, and is free software. You can |
| 6 |
* redistribute it and/or modify it under the terms of the GNU General |
| 7 |
* Public License (GPL); either version 2, or (at your option) any |
| 8 |
* later version. |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifndef _STAPLINUX_ADDR_MAP_C_ |
| 12 |
#define _STAPLINUX_ADDR_MAP_C_ 1 |
| 13 |
|
| 14 |
/** @file addr-map |
| 15 |
* @brief Implements functions used by the deref macro to blocklist |
| 16 |
* certain addresses. |
| 17 |
*/ |
| 18 |
|
| 19 |
// PR12970 (populate runtime/bad-addr database) hasn't been |
| 20 |
// fixed. Until we're ready to populate the bad address database, |
| 21 |
// let's provide a "junior" version of lookup_bad_addr(), since that |
| 22 |
// will allow us to skip grabbing the lock and searching the empty |
| 23 |
// list. |
| 24 |
// |
| 25 |
// See <https://sourceware.org/bugzilla/show_bug.cgi?id=12970> for |
| 26 |
// more details. |
| 27 |
|
| 28 |
#include <linux/runtime.h> |
| 29 |
#include <linux/uaccess.h> |
| 30 |
|
| 31 |
#ifndef PR12970 |
| 32 |
|
| 33 |
#if ! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPDEV) && \ |
| 34 |
! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPSYS) |
| 35 |
#include <asm/processor.h> /* For TASK_SIZE */ |
| 36 |
#endif |
| 37 |
|
| 38 |
|
| 39 |
#ifdef STAPCONF_ASM_ACCESS_OK |
| 40 |
/* access_ok() is designed for user context only and calling it from |
| 41 |
* perf event etc probe contexts would result in kernel warnings |
| 42 |
* on debug kernels. see upstream kernel commit d319f34456 for more details. |
| 43 |
*/ |
| 44 |
#define stp_access_ok(x,y,z) __access_ok(y,z) |
| 45 |
#elif defined(STAPCONF_ACCESS_OK_2ARGS) |
| 46 |
#define stp_access_ok(x,y,z) access_ok(y,z) |
| 47 |
#else |
| 48 |
#define stp_access_ok(x,y,z) access_ok(x,y,z) |
| 49 |
#endif |
| 50 |
|
| 51 |
#ifdef STAPCONF_NMI_UACCESS_OKAY |
| 52 |
#include <linux/mm.h> |
| 53 |
#include <asm/tlbflush.h> |
| 54 |
#endif |
| 55 |
|
| 56 |
static inline bool |
| 57 |
stp_nmi_uaccess_okay(void) |
| 58 |
{ |
| 59 |
#ifdef STAPCONF_NMI_UACCESS_OKAY |
| 60 |
/* older kernels (like 5.0) still have this as an inline function in |
| 61 |
* their arch-specific headers */ |
| 62 |
return nmi_uaccess_okay(); |
| 63 |
#else |
| 64 |
/* newer kernels (since 5.18) no longer export nmi_uaccess_okay() |
| 65 |
* but we can still get it from the kernel symbol tables. */ |
| 66 |
if (kallsyms_nmi_uaccess_okay != NULL) |
| 67 |
return ibt_wrapper(bool, ((bool (*)(void)) kallsyms_nmi_uaccess_okay)()); |
| 68 |
|
| 69 |
return true; /* don't bother */ |
| 70 |
#endif |
| 71 |
} |
| 72 |
|
| 73 |
static __always_inline int |
| 74 |
lookup_bad_addr_user(const int type, const unsigned long addr, const size_t size) |
| 75 |
{ |
| 76 |
/* Is this a valid memory access? */ |
| 77 |
|
| 78 |
/* We must call stp_nmi_uaccess_okay() to protect races against CR3 |
| 79 |
* context switching on X86. See upstream kernel commits 4012e77a90 |
| 80 |
* and d319f344561de for more details. |
| 81 |
*/ |
| 82 |
if (!stp_nmi_uaccess_okay()) |
| 83 |
return 1; |
| 84 |
|
| 85 |
/* Note we're using stp_access_ok() here. This is only supposed to be |
| 86 |
* called when we're in task context. Occasionally lookup_bad_addr() |
| 87 |
* gets called when not in task context. If in_task() exists, only |
| 88 |
* call stp_access_ok() when we're in task context (otherwise we'll get |
| 89 |
* a kernel warning). If we aren't in task context, we'll just do a |
| 90 |
* range check. |
| 91 |
*/ |
| 92 |
#if !defined(in_task) || LINUX_VERSION_CODE >= KERNEL_VERSION(5,18,0) |
| 93 |
/* NB access_ok() checks TASK_SIZE_MAX (used to be user_addr_max()) on |
| 94 |
* all architectures since kernel 5.18 */ |
| 95 |
if (size == 0 || ULONG_MAX - addr < size - 1 |
| 96 |
|| !stp_access_ok(type, (void *)addr, size)) |
| 97 |
return 1; |
| 98 |
#else |
| 99 |
if (size == 0 || ULONG_MAX - addr < size - 1 |
| 100 |
|| (in_task() && !stp_access_ok(type, (void *)addr, size)) |
| 101 |
|| (!in_task() |
| 102 |
#if defined(user_addr_max) |
| 103 |
&& ((user_addr_max() - size) < addr) |
| 104 |
#elif defined(TASK_SIZE_MAX) |
| 105 |
&& ((TASK_SIZE_MAX - size) < addr) |
| 106 |
#endif |
| 107 |
)) |
| 108 |
return 1; |
| 109 |
#endif |
| 110 |
|
| 111 |
#if ! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPDEV) && \ |
| 112 |
! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPSYS) |
| 113 |
/* Unprivileged users must not access memory while the context |
| 114 |
does not refer to their own process. */ |
| 115 |
if (! is_myproc ()) |
| 116 |
return 1; |
| 117 |
/* Unprivileged users must not access kernel space memory. */ |
| 118 |
if (addr + size > TASK_SIZE) |
| 119 |
return 1; |
| 120 |
#endif |
| 121 |
|
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
static __always_inline int |
| 126 |
lookup_bad_addr(const int type, const unsigned long addr, const size_t size, |
| 127 |
const stp_mm_segment_t seg) |
| 128 |
{ |
| 129 |
/* Is this a valid memory access? |
| 130 |
* |
| 131 |
* PR26811: Since kernel 5.10 due to set_fs() removal we need to |
| 132 |
* distinguish kernel- and user-space addresses when asking this |
| 133 |
* question. |
| 134 |
*/ |
| 135 |
if (stp_is_user_ds(seg)) |
| 136 |
return lookup_bad_addr_user(type, addr, size); |
| 137 |
|
| 138 |
/* For kernel addr, skip the in_task() portion of the address checks: */ |
| 139 |
if (size == 0 || ULONG_MAX - addr < size - 1) |
| 140 |
return 1; |
| 141 |
|
| 142 |
/* XXX: Kernel reads call copy_from_kernel_nofault() which also |
| 143 |
checks copy_from_kernel_fault_allowed(), |
| 144 |
currently a stub function returning true -- not worth invoking |
| 145 |
ourselves through kallsyms. Keep an eye on this, though. */ |
| 146 |
|
| 147 |
#if ! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPDEV) && \ |
| 148 |
! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPSYS) |
| 149 |
/* Unprivileged users must not access memory while the context |
| 150 |
does not refer to their own process. */ |
| 151 |
if (! is_myproc ()) |
| 152 |
return 1; |
| 153 |
/* Unprivileged users must not access kernel space memory. */ |
| 154 |
if (addr + size > TASK_SIZE) |
| 155 |
return 1; |
| 156 |
#endif |
| 157 |
|
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
#else /* PR12970 */ |
| 162 |
|
| 163 |
struct addr_map_entry |
| 164 |
{ |
| 165 |
unsigned long min; |
| 166 |
unsigned long max; |
| 167 |
}; |
| 168 |
|
| 169 |
struct addr_map |
| 170 |
{ |
| 171 |
size_t size; |
| 172 |
struct addr_map_entry entries[] __counted_by(size); |
| 173 |
}; |
| 174 |
|
| 175 |
static STP_DEFINE_RWLOCK(addr_map_lock); |
| 176 |
static struct addr_map* blockmap; |
| 177 |
|
| 178 |
/* Find address of entry where we can insert a new one. */ |
| 179 |
static size_t |
| 180 |
upper_bound(unsigned long addr, struct addr_map* map) |
| 181 |
{ |
| 182 |
size_t start = 0; |
| 183 |
size_t end = map->size; |
| 184 |
struct addr_map_entry *entry = 0; |
| 185 |
|
| 186 |
if (end == 0) |
| 187 |
return 0; |
| 188 |
do |
| 189 |
{ |
| 190 |
size_t new_idx; |
| 191 |
if (addr < map->entries[start].min) |
| 192 |
return start; |
| 193 |
if (addr >= map->entries[end-1].max) |
| 194 |
return end; |
| 195 |
new_idx = (end + start) / 2; |
| 196 |
entry = &map->entries[new_idx]; |
| 197 |
if (addr < entry->min) |
| 198 |
end = new_idx; |
| 199 |
else |
| 200 |
start = new_idx + 1; |
| 201 |
} while (end != start); |
| 202 |
return entry - &map->entries[0]; |
| 203 |
} |
| 204 |
|
| 205 |
/* Search for an entry in the given map which overlaps the range specified by |
| 206 |
addr and size. */ |
| 207 |
static struct addr_map_entry* |
| 208 |
lookup_addr_aux(unsigned long addr, size_t size, struct addr_map* map) |
| 209 |
{ |
| 210 |
size_t start = 0; |
| 211 |
size_t end; |
| 212 |
|
| 213 |
/* No map? No matching entry. */ |
| 214 |
if (!map) |
| 215 |
return 0; |
| 216 |
/* Empty map? No matching entry. */ |
| 217 |
if (map->size == 0) |
| 218 |
return 0; |
| 219 |
|
| 220 |
/* Use binary search on the sorted map entries. */ |
| 221 |
end = map->size; |
| 222 |
do |
| 223 |
{ |
| 224 |
int entry_idx; |
| 225 |
struct addr_map_entry *entry = 0; |
| 226 |
/* If the target range is beyond those of the remaining entries in the |
| 227 |
map, then a matching entry will not be found */ |
| 228 |
if (addr + size <= map->entries[start].min || |
| 229 |
addr >= map->entries[end - 1].max) |
| 230 |
return 0; |
| 231 |
/* Choose the map entry at the mid point of the remaining entries. */ |
| 232 |
entry_idx = (end + start) / 2; |
| 233 |
entry = &map->entries[entry_idx]; |
| 234 |
/* If our range overlaps the range of this entry, then we have a match. */ |
| 235 |
if (addr + size > entry->min && addr < entry->max) |
| 236 |
return entry; |
| 237 |
/* If our range is below the range of this entry, then cull the entries |
| 238 |
above this entry, otherwise cull the entries below it. */ |
| 239 |
if (addr + size <= entry->min) |
| 240 |
end = entry_idx; |
| 241 |
else |
| 242 |
start = entry_idx + 1; |
| 243 |
} while (start < end); |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
#if ! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPDEV) && \ |
| 248 |
! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPSYS) |
| 249 |
#include <asm/processor.h> /* For TASK_SIZE */ |
| 250 |
#endif |
| 251 |
|
| 252 |
static int |
| 253 |
lookup_bad_addr(const int type, const unsigned long addr, const size_t size) |
| 254 |
{ |
| 255 |
struct addr_map_entry* result = 0; |
| 256 |
unsigned long flags; |
| 257 |
|
| 258 |
/* Is this a valid memory access? */ |
| 259 |
if (size == 0 || ULONG_MAX - addr < size - 1 |
| 260 |
|| !stp_access_ok(type, (void *)addr, size)) |
| 261 |
return 1; |
| 262 |
|
| 263 |
#if ! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPDEV) && \ |
| 264 |
! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPSYS) |
| 265 |
/* Unprivileged users must not access memory while the context |
| 266 |
does not refer to their own process. */ |
| 267 |
if (! is_myproc ()) |
| 268 |
return 1; |
| 269 |
/* Unprivileged users must not access kernel space memory. */ |
| 270 |
if (addr + size > TASK_SIZE) |
| 271 |
return 1; |
| 272 |
#endif |
| 273 |
|
| 274 |
/* Search for the given range in the block-listed map. */ |
| 275 |
stp_read_lock_irqsave(&addr_map_lock, flags); |
| 276 |
result = lookup_addr_aux(addr, size, blockmap); |
| 277 |
stp_read_unlock_irqrestore(&addr_map_lock, flags); |
| 278 |
if (result) |
| 279 |
return 1; |
| 280 |
else |
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
static int |
| 286 |
add_bad_addr_entry(unsigned long min_addr, unsigned long max_addr, |
| 287 |
struct addr_map_entry** existing_min, |
| 288 |
struct addr_map_entry** existing_max) |
| 289 |
{ |
| 290 |
struct addr_map* new_map = 0; |
| 291 |
struct addr_map* old_map = 0; |
| 292 |
struct addr_map_entry* min_entry = 0; |
| 293 |
struct addr_map_entry* max_entry = 0; |
| 294 |
struct addr_map_entry* new_entry = 0; |
| 295 |
size_t existing = 0; |
| 296 |
unsigned long flags; |
| 297 |
|
| 298 |
/* Loop allocating memory for a new entry in the map. */ |
| 299 |
while (1) |
| 300 |
{ |
| 301 |
size_t old_size = 0; |
| 302 |
stp_write_lock_irqsave(&addr_map_lock, flags); |
| 303 |
old_map = blockmap; |
| 304 |
if (old_map) |
| 305 |
old_size = old_map->size; |
| 306 |
/* Either this is the first time through the loop, or we |
| 307 |
allocated a map previous time, but someone has come in and |
| 308 |
added an entry while we were sleeping. */ |
| 309 |
if (!new_map || (new_map && new_map->size < old_size + 1)) |
| 310 |
{ |
| 311 |
stp_write_unlock_irqrestore(&addr_map_lock, flags); |
| 312 |
if (new_map) |
| 313 |
{ |
| 314 |
_stp_kfree(new_map); |
| 315 |
new_map = 0; |
| 316 |
} |
| 317 |
new_map = _stp_kmalloc(sizeof(*new_map) |
| 318 |
+ sizeof(*new_entry) * (old_size + 1)); |
| 319 |
if (!new_map) |
| 320 |
return -ENOMEM; |
| 321 |
new_map->size = old_size + 1; |
| 322 |
} |
| 323 |
else |
| 324 |
break; |
| 325 |
} |
| 326 |
if (!blockmap) |
| 327 |
{ |
| 328 |
existing = 0; |
| 329 |
} |
| 330 |
else |
| 331 |
{ |
| 332 |
min_entry = lookup_addr_aux(min_addr, 1, blockmap); |
| 333 |
max_entry = lookup_addr_aux(max_addr, 1, blockmap); |
| 334 |
if (min_entry || max_entry) |
| 335 |
{ |
| 336 |
if (existing_min) |
| 337 |
*existing_min = min_entry; |
| 338 |
if (existing_max) |
| 339 |
*existing_max = max_entry; |
| 340 |
stp_write_unlock_irqrestore(&addr_map_lock, flags); |
| 341 |
_stp_kfree(new_map); |
| 342 |
return 1; |
| 343 |
} |
| 344 |
existing = upper_bound(min_addr, old_map); |
| 345 |
} |
| 346 |
new_entry = &new_map->entries[existing]; |
| 347 |
new_entry->min = min_addr; |
| 348 |
new_entry->max = max_addr; |
| 349 |
if (old_map) |
| 350 |
{ |
| 351 |
memcpy(&new_map->entries, old_map->entries, |
| 352 |
existing * sizeof(*new_entry)); |
| 353 |
if (old_map->size > existing) |
| 354 |
memcpy(new_entry + 1, &old_map->entries[existing], |
| 355 |
(old_map->size - existing) * sizeof(*new_entry)); |
| 356 |
} |
| 357 |
blockmap = new_map; |
| 358 |
stp_write_unlock_irqrestore(&addr_map_lock, flags); |
| 359 |
if (old_map) |
| 360 |
_stp_kfree(old_map); |
| 361 |
return 0; |
| 362 |
} |
| 363 |
|
| 364 |
static void |
| 365 |
delete_bad_addr_entry(struct addr_map_entry* entry) |
| 366 |
{ |
| 367 |
} |
| 368 |
|
| 369 |
#endif /* PR12970 */ |
| 370 |
|
| 371 |
#endif /* _STAPLINUX_ADDR_MAP_C_ */ |