From nobody Mon Jun 8 05:25:47 2026 Received: from mail-dy1-f202.google.com (mail-dy1-f202.google.com [74.125.82.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 882923B42F0 for ; Sat, 6 Jun 2026 15:15:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780758906; cv=none; b=X/cgKul7KX+QIKf1OwGkJW2gb/NZzSovvy+WpqiPIwD2WVCq7KuUybo5e0qdWoLTKqcmHPVWiV6AMRkMCCEh+FriEMUtp4f5bwvVWSCImIl7Us3QsDEmBnWfw+t45Ax7RnoTQLNQ2Nu8d0jblufbaxJpeLTxIYylMz4TO2lxvrk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780758906; c=relaxed/simple; bh=Su04wdWkgm24Rp7hpVKpDVqoAO5s5c+LSX5RSu/IfAo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=R7l5PO6HxRbFag+iRyUVEtjk5sONSFFry1Ow73J/F2XT+GwLnvZwWAksbnW2wGp4r9ZBdaberBJbpkvqkShlUwo3K2FQYnD6+zRfx/DBRPo3u5L9KkzS5hKU3W45Ywp68O/DvlKUN0k3jJHTz00gr/kBobfYBuMW3LQkwpaRa/I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bRENzbNK; arc=none smtp.client-ip=74.125.82.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bRENzbNK" Received: by mail-dy1-f202.google.com with SMTP id 5a478bee46e88-304f23c55b2so2875897eec.0 for ; Sat, 06 Jun 2026 08:15:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780758904; x=1781363704; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ZLir4cGCxsK/hyjhq2NQWG13nFFe+0hZ5oJkCl6Ap3U=; b=bRENzbNKEPAzUb8vHy/XHXh3y4kNpU5qqqXrYywE36598a/GRC4yqlwbON9EG8X871 GCAR5JFZFQiHQFFHTr+7Dnm1IX2gCeWx1rNcfeluknhhzLG75tYxBIyC7h0yy6wRK2Ls AN9rgVqcUaIdv3lXQkCUux1aXXRKMf9m0YxT82o+DLWD64rumOnaNdcozgMIMLaQBWWy Zr5m6N5IUclG1IOEoLFUmHPLG/8oFQFEQO5MXUaSCH84QB0eQ+OrUxdpGRqw4EsYJvM1 OLRsJE3wblRD7VhVwTPT5OP+RLVoWBFEwAiiQVy2HRYwOAwC0lF9V1NYuGLnti/zA+gq /B5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780758904; x=1781363704; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZLir4cGCxsK/hyjhq2NQWG13nFFe+0hZ5oJkCl6Ap3U=; b=L5Dd1ga6JJlkENeJpdZhv1cJN5HSs+us1QIlavvFY5XQh48DOjkaG4Rk1XF3gOGIsP Y4ZI5M3msUQok1wexxQRcS00y5lNae9sRF2ItfGOrtByiIjV8HZ9n6TlLkLOzQN0Ev8S xOnLYD6CgEZdicy8fVrcr0OHGgTTnuqViutmz1trdi6GLW2MJKkSdJ2LFHkQKg4Ke947 eVs7w5Tbs25CesoyhOPEyJxicTmYnsiiiH33vCyRwHHEd2d1jUbd53kgzqb9L4gKIblE c/qejzjXajR5L325vYIMuChlRle9lFTmBjcWK2JeJ0gg27hQaTCsLx+n5aY9jM+d+n3X ad6A== X-Forwarded-Encrypted: i=1; AFNElJ8dcspAe6BQOnztRXTWmT8fWjNNGDO4IDezy+433MosrgIG038q6ljcNLblzv99b/wPkvqrGaPdP66HxE4=@vger.kernel.org X-Gm-Message-State: AOJu0YxcamRtXGXS+Ncy+vk6fzFArhPcNbHxwE553eHgqDKphj2ktIKG IW4lHQ3u1jPCHc2JrfZltUmCDgVaIEhennYMkHpH8tzWCNqg+urepJBm2Ia/qHwngtm9Mvbf0cI Pl2Dx0DpxYQ== X-Received: from dyctz19.prod.google.com ([2002:a05:7301:9f13:b0:304:eefd:96c6]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:320b:b0:2da:1874:f3bd with SMTP id 5a478bee46e88-3077b709e8cmr5309977eec.16.1780758903435; Sat, 06 Jun 2026 08:15:03 -0700 (PDT) Date: Sat, 6 Jun 2026 08:14:47 -0700 In-Reply-To: <20260606151451.2913860-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260606072125.2786845-1-irogers@google.com> <20260606151451.2913860-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.1032.g2f8565e1d1-goog Message-ID: <20260606151451.2913860-2-irogers@google.com> Subject: [PATCH v16 1/5] perf maps: Add maps__mutate_mapping From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, gmx@google.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" During kernel ELF symbol parsing (dso__process_kernel_symbol), proc kallsyms image loading (dso__load_kernel_sym, dso__load_guest_kernel_sym), and dynamic kernel memory map alignment updates (machine__update_kernel_mmap), the loader directly modifies live virtual address boundary keys fields on map objects. If these boundaries are mutated while the map pointer actively resides inside the parent maps cache array list (kmaps) outside of any lock closure, an unsafe concurrent window is exposed where parallel worker lookup threads (e.g., inside perf top) can mistakenly assume the cache remains sorted based on stale parameters, executing binary search queries (bsearch) across an unsorted range and triggering lookup failures. Fix this by introducing maps__mutate_mapping() that explicitly acquires the parent maps write semaphore lock, executes an incoming mutation callback block to perform the field updates under lock protection, and invalidates the sorted tracking flags prior to releasing the write lock. This guarantees synchronization invariants, closing the concurrent lookup race window. The adjacent module alignment pass inside machine__create_kernel_maps() is safely preserved as a high-performance lockless pass, as its invocation lifecycle bounds remain strictly single-threaded by contract during session initialization construction. To safely support this unconditional down_write write lock mutator without recursive read-to-write self-deadlock upgrades during lazy symbol loading, we introduce a public maps__load_maps() API. It copies map pointers under a brief read lock and force-loads all modules locklessly outside the lock. Callers (such as perf inject) must pre-load all kernel symbol maps up front at startup using maps__load_maps(), completely bypassing dynamic runtime mutations. Fixes: 39b12f781271 ("perf tools: Make it possible to read object code from= vmlinux") Assisted-by: Antigravity:gemini-3.5-flash Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 32 +++++--- tools/perf/util/maps.c | 149 ++++++++++++++++++++++++++++------- tools/perf/util/maps.h | 3 + tools/perf/util/symbol-elf.c | 41 ++++++---- tools/perf/util/symbol.c | 17 +++- 5 files changed, 184 insertions(+), 58 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index da1ad58758af..1ea06fde14e0 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1539,22 +1539,30 @@ static void machine__set_kernel_mmap(struct machine= *machine, map__set_end(machine->vmlinux_map, ~0ULL); } =20 -static int machine__update_kernel_mmap(struct machine *machine, - u64 start, u64 end) +struct kernel_mmap_mutation_ctx { + u64 start; + u64 end; +}; + +static int kernel_mmap_mutate_cb(struct map *map, void *data) { - struct map *orig, *updated; - int err; + struct kernel_mmap_mutation_ctx *ctx =3D data; =20 - orig =3D machine->vmlinux_map; - updated =3D map__get(orig); + map__set_start(map, ctx->start); + map__set_end(map, ctx->end); + if (ctx->start =3D=3D 0 && ctx->end =3D=3D 0) + map__set_end(map, ~0ULL); + return 0; +} =20 - machine->vmlinux_map =3D updated; - maps__remove(machine__kernel_maps(machine), orig); - machine__set_kernel_mmap(machine, start, end); - err =3D maps__insert(machine__kernel_maps(machine), updated); - map__put(orig); +static int machine__update_kernel_mmap(struct machine *machine, + u64 start, u64 end) +{ + struct kernel_mmap_mutation_ctx ctx =3D { .start =3D start, .end =3D end = }; =20 - return err; + return maps__mutate_mapping(machine__kernel_maps(machine), + machine->vmlinux_map, + kernel_mmap_mutate_cb, &ctx); } =20 int machine__create_kernel_maps(struct machine *machine) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 923935ee21b6..b1b8efe42149 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -576,6 +576,49 @@ void maps__remove(struct maps *maps, struct map *map) #endif } =20 +/** + * maps__mutate_mapping - Apply write-protected mutations to a map. + * @maps: The maps collection containing the map. + * @map: The map to mutate. + * @mutate_cb: Callback function that performs the actual mutations. + * @data: Private data passed to the callback. + * + * This acquires the write lock on the maps semaphore to safely protect + * concurrent readers from seeing partially mutated or unsorted map bounda= ries. + * + * WARNING: Acquiring down_write() here can trigger a recursive self-deadl= ock if + * the caller already holds the read lock (e.g., during maps__for_each_map= () or + * maps__find() iteration paths that trigger lazy symbol loading). To comp= letely + * avoid this deadlock, all kernel/module maps must be pre-loaded up-front= (via + * maps__load_maps()) under a clean, single-threaded context before enteri= ng + * multi-threaded event processing loops. + */ +int maps__mutate_mapping(struct maps *maps, struct map *map, + int (*mutate_cb)(struct map *map, void *data), void *data) +{ + int err =3D 0; + + if (maps) + down_write(maps__lock(maps)); + + err =3D mutate_cb(map, data); + + if (maps) { + RC_CHK_ACCESS(maps)->maps_by_address_sorted =3D false; + RC_CHK_ACCESS(maps)->maps_by_name_sorted =3D false; + } + + if (maps) + up_write(maps__lock(maps)); + +#ifdef HAVE_LIBDW_SUPPORT + if (maps) + libdw__invalidate_dwfl(maps, maps__libdw_addr_space_dwfl(maps)); +#endif + + return err; +} + bool maps__empty(struct maps *maps) { bool res; @@ -626,6 +669,41 @@ int maps__for_each_map(struct maps *maps, int (*cb)(st= ruct map *map, void *data) return ret; } =20 +int maps__load_maps(struct maps *maps) +{ + struct map **maps_copy; + unsigned int nr_maps; + int err =3D 0; + + if (!maps) + return 0; + + down_read(maps__lock(maps)); + nr_maps =3D maps__nr_maps(maps); + if (nr_maps =3D=3D 0) { + up_read(maps__lock(maps)); + return 0; + } + maps_copy =3D calloc(nr_maps, sizeof(*maps_copy)); + if (!maps_copy) { + up_read(maps__lock(maps)); + return -ENOMEM; + } + for (unsigned int i =3D 0; i < nr_maps; i++) + maps_copy[i] =3D map__get(maps__maps_by_address(maps)[i]); + up_read(maps__lock(maps)); + + for (unsigned int i =3D 0; i < nr_maps; i++) { + if (map__load(maps_copy[i]) < 0) { + pr_warning("Failed to load map %s\n", dso__name(map__dso(maps_copy[i]))= ); + err =3D -1; + } + map__put(maps_copy[i]); + } + free(maps_copy); + return err; +} + void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void= *data), void *data) { struct map **maps_by_address; @@ -668,40 +746,57 @@ struct symbol *maps__find_symbol(struct maps *maps, u= 64 addr, struct map **mapp) return result; } =20 -struct maps__find_symbol_by_name_args { - struct map **mapp; - const char *name; - struct symbol *sym; -}; - -static int maps__find_symbol_by_name_cb(struct map *map, void *data) +struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *na= me, struct map **mapp) { - struct maps__find_symbol_by_name_args *args =3D data; + struct map **maps_copy; + unsigned int nr_maps; + struct symbol *sym =3D NULL; =20 - args->sym =3D map__find_symbol_by_name(map, args->name); - if (!args->sym) - return 0; + if (!maps) + return NULL; =20 - if (!map__contains_symbol(map, args->sym)) { - args->sym =3D NULL; - return 0; + /* + * First, ensure all maps are loaded. We pre-load them outside of any + * read-to-write locks to avoid deadlocks. Even if some fail, we proceed. + */ + maps__load_maps(maps); + + /* + * Create a local snapshot of the maps while holding the read lock. + * This prevents deadlocking if iteration triggers further map insertions. + */ + down_read(maps__lock(maps)); + nr_maps =3D maps__nr_maps(maps); + maps_copy =3D calloc(nr_maps, sizeof(*maps_copy)); + if (maps_copy) { + for (unsigned int i =3D 0; i < nr_maps; i++) { + struct map *map =3D maps__maps_by_address(maps)[i]; + + maps_copy[i] =3D map__get(map); + } } + up_read(maps__lock(maps)); =20 - if (args->mapp !=3D NULL) - *args->mapp =3D map__get(map); - return 1; -} + if (!maps_copy) + return NULL; =20 -struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *na= me, struct map **mapp) -{ - struct maps__find_symbol_by_name_args args =3D { - .mapp =3D mapp, - .name =3D name, - .sym =3D NULL, - }; + for (unsigned int i =3D 0; i < nr_maps; i++) { + struct map *map =3D maps_copy[i]; + + sym =3D map__find_symbol_by_name(map, name); + if (sym && map__contains_symbol(map, sym)) { + if (mapp) + *mapp =3D map__get(map); + break; + } + sym =3D NULL; + } + + for (unsigned int i =3D 0; i < nr_maps; i++) + map__put(maps_copy[i]); =20 - maps__for_each_map(maps, maps__find_symbol_by_name_cb, &args); - return args.sym; + free(maps_copy); + return sym; } =20 int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams) diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index 5b80b199685e..4ec9b7453a3b 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -59,8 +59,11 @@ void maps__set_libdw_addr_space_dwfl(struct maps *maps, = void *dwfl); =20 size_t maps__fprintf(struct maps *maps, FILE *fp); =20 +int maps__load_maps(struct maps *maps); int maps__insert(struct maps *maps, struct map *map); void maps__remove(struct maps *maps, struct map *map); +int maps__mutate_mapping(struct maps *maps, struct map *map, + int (*mutate_cb)(struct map *map, void *data), void *data); =20 struct map *maps__find(struct maps *maps, u64 addr); struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map *= *mapp); diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 186e6d92ac3d..d1e93c0556dd 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1342,6 +1342,24 @@ static u64 ref_reloc(struct kmap *kmap) void __weak arch__sym_update(struct symbol *s __maybe_unused, GElf_Sym *sym __maybe_unused) { } =20 +struct remap_kernel_ctx { + u64 sh_addr; + u64 sh_size; + u64 sh_offset; + struct kmap *kmap; +}; + +static int remap_kernel_cb(struct map *map, void *data) +{ + struct remap_kernel_ctx *ctx =3D data; + + map__set_start(map, ctx->sh_addr + ref_reloc(ctx->kmap)); + map__set_end(map, map__start(map) + ctx->sh_size); + map__set_pgoff(map, ctx->sh_offset); + map__set_mapping_type(map, MAPPING_TYPE__DSO); + return 0; +} + static int dso__process_kernel_symbol(struct dso *dso, struct map *map, GElf_Sym *sym, GElf_Shdr *shdr, struct maps *kmaps, struct kmap *kmap, @@ -1372,22 +1390,15 @@ static int dso__process_kernel_symbol(struct dso *d= so, struct map *map, * map to the kernel dso. */ if (*remap_kernel && dso__kernel(dso) && !kmodule) { + struct remap_kernel_ctx ctx =3D { + .sh_addr =3D shdr->sh_addr, + .sh_size =3D shdr->sh_size, + .sh_offset =3D shdr->sh_offset, + .kmap =3D kmap + }; + *remap_kernel =3D false; - map__set_start(map, shdr->sh_addr + ref_reloc(kmap)); - map__set_end(map, map__start(map) + shdr->sh_size); - map__set_pgoff(map, shdr->sh_offset); - map__set_mapping_type(map, MAPPING_TYPE__DSO); - /* Ensure maps are correctly ordered */ - if (kmaps) { - int err; - struct map *tmp =3D map__get(map); - - maps__remove(kmaps, map); - err =3D maps__insert(kmaps, map); - map__put(tmp); - if (err) - return err; - } + maps__mutate_mapping(kmaps, map, remap_kernel_cb, &ctx); } =20 /* diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 0c46b24ee098..2cc911af8c81 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -48,6 +48,13 @@ #include #include =20 +static int map_fixup_cb(struct map *map, void *data __maybe_unused) +{ + map__fixup_start(map); + map__fixup_end(map); + return 0; +} + static int dso__load_kernel_sym(struct dso *dso, struct map *map); static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map); =20 @@ -2240,10 +2247,11 @@ static int dso__load_kernel_sym(struct dso *dso, st= ruct map *map) free(kallsyms_allocated_filename); =20 if (err > 0 && !dso__is_kcore(dso)) { + struct maps *kmaps =3D map__kmaps(map); + dso__set_binary_type(dso, DSO_BINARY_TYPE__KALLSYMS); dso__set_long_name(dso, DSO__NAME_KALLSYMS, false); - map__fixup_start(map); - map__fixup_end(map); + maps__mutate_mapping(kmaps, map, map_fixup_cb, NULL); } =20 return err; @@ -2283,10 +2291,11 @@ static int dso__load_guest_kernel_sym(struct dso *d= so, struct map *map) if (err > 0) pr_debug("Using %s for symbols\n", kallsyms_filename); if (err > 0 && !dso__is_kcore(dso)) { + struct maps *kmaps =3D map__kmaps(map); + dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_KALLSYMS); dso__set_long_name(dso, machine->mmap_name, false); - map__fixup_start(map); - map__fixup_end(map); + maps__mutate_mapping(kmaps, map, map_fixup_cb, NULL); } =20 return err; --=20 2.54.0.1032.g2f8565e1d1-goog From nobody Mon Jun 8 05:25:47 2026 Received: from mail-dy1-f201.google.com (mail-dy1-f201.google.com [74.125.82.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4768D38A701 for ; Sat, 6 Jun 2026 15:15:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780758913; cv=none; b=T5pl7cPWRDiUUTOmCig9dcXCN181HWP07n4TYgTWwLHxTduyHtTyvDn8xeBmd5Gi4sYIvweEFzvciTdlVeSGTe4meQ4AINwiGITFltk5I04G4jNSDG6gv6VuaTcCZbb6JW1KmmZmaljKVs9emr5tSI5z1jibBh2qCQDEW8TeZZs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780758913; c=relaxed/simple; bh=ngqrU209osl/We+i0ei/vkPhEtG7t+ig7MUhBa8Lsf0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mzhacC94PlXvhgrI/sqVCJwQj88Wcoo6hUnH+iv4q0s9bOZG6YMjCeQHUNjtMznzVfubPhgPArdWzTtiznoa7qBkP0MkAXVIJqUzqD8lkQr8bfb9fqx5Dd00dKHO6tU1wK+t8BkCFf+nBsuDRvDF7sAtSl6zFiPwkpSCfyjnDZI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ic7S1VEK; arc=none smtp.client-ip=74.125.82.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ic7S1VEK" Received: by mail-dy1-f201.google.com with SMTP id 5a478bee46e88-304d8613efbso2872157eec.1 for ; Sat, 06 Jun 2026 08:15:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780758906; x=1781363706; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zWa/DgHyt7g7pGb7W4IiL3Al+jDwIrAwRLCP5jtsKJw=; b=ic7S1VEKrRiU4PpyO57HjthtRofxAaYaSfPYbs2jcP/aICg7xadGvo/vRyKst7FdLW zrfwjqb7lLoDK5WOkCA0V+8+tNUiUGvYkChqeWPVt0vAmaYqmp/ZlOo+PR3V63zDkFiV ktjinqvoGTcUElJiv1L8YkHytFSKxtvjyOim+1GzvoaH4zS1xCtKjHBduPFQMzqnzAhr h5oLXfg1UZ4nrOw5L1UpG/ES4UcpbDiDgaBpceWYT6pY57eFTeM/fwvNcKM4MSjl5B9d dOEG7uSaureDzHET1qgCgncf6fHhQ7zvoXTlHKDJTSQV7/TUbLaiPWVeo8McIo9LZlKm Yp+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780758906; x=1781363706; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zWa/DgHyt7g7pGb7W4IiL3Al+jDwIrAwRLCP5jtsKJw=; b=m9hNxDx4LBNVcYhwsiQheFyD0TAzQ1eU5JVigBZ059Heq/i/Q9wA6kaggrGSXPdYql A7ebWWbdJmiS5ZE6xjIHWyc/V45x4Lf+g1dNvYpTGd1X8fsRYc/Xa4OQ9fH2yOCGWhXs Y9su9CrSy4+OGKyyAv5qvQ1U0vXoNTVbfYqVml2r/w1ZNNp9tXbChM6xlPp0XVp8gR1l 4WZn6p0li48OP8r5cJ01ZqIzkYAnDCT+isNmU0sbVflig9+07X/Gd8aR6sCVXumbIenk R0AEUVtEWUoPALeesOKdONkIG4dkKeITZY0uE+z4Xd9/5TzUPHsHiwW0poaKo6sSev4X W4tw== X-Forwarded-Encrypted: i=1; AFNElJ+H95GAoedurkpH4bn3eSsf9UuJj1933oymvvdllCCvztmdy9i+y2DhV9+e9k3upL5TN4Pc9rBUGK2gmKs=@vger.kernel.org X-Gm-Message-State: AOJu0Yz9VrY+NRjIDcNVva/PgCaht2YjNKGiPIORd4z6mIjau2d/bowY yGEqL2iZfXbdYozBgkJdrp+opq9hecKntmeaWjtRc9Sgr6pn1slYVtp5+P2zwlmqomDGKDfZHck MVYRFkKnUGQ== X-Received: from dybml5.prod.google.com ([2002:a05:7301:1505:b0:2f7:9a2b:1dba]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:68ce:b0:307:91f9:c1d9 with SMTP id 5a478bee46e88-30791f9d4fdmr1551394eec.29.1780758906093; Sat, 06 Jun 2026 08:15:06 -0700 (PDT) Date: Sat, 6 Jun 2026 08:14:48 -0700 In-Reply-To: <20260606151451.2913860-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260606072125.2786845-1-irogers@google.com> <20260606151451.2913860-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.1032.g2f8565e1d1-goog Message-ID: <20260606151451.2913860-3-irogers@google.com> Subject: [PATCH v16 2/5] perf inject/aslr: Add ASLR tool infrastructure and MMAP tracking From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, gmx@google.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If perf.data files are taken from one machine to another they may leak virtual addresses and so weaken ASLR on the machine they are coming from. Add an aslr option for perf inject that remaps all virtual addresses, or drops data/events, so that the virtual address information isn't leaked. This patch introduces the core ASLR remapping tool infrastructure and implements remapping/tracking for metadata events (MMAP, MMAP2, COMM, FORK, EXIT, KSYMBOL, TEXT_POKE). Sample events are delegated without remapping for now. Assisted-by: Antigravity:gemini-3.5-flash Signed-off-by: Ian Rogers Co-developed-by: Gabriel Marin Signed-off-by: Gabriel Marin --- tools/perf/builtin-inject.c | 84 ++++- tools/perf/util/Build | 1 + tools/perf/util/aslr.c | 732 ++++++++++++++++++++++++++++++++++++ tools/perf/util/aslr.h | 37 ++ 4 files changed, 851 insertions(+), 3 deletions(-) create mode 100644 tools/perf/util/aslr.c create mode 100644 tools/perf/util/aslr.h diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index 75ffe31d03fe..00a54d1c7e41 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -8,6 +8,7 @@ */ #include "builtin.h" =20 +#include "util/aslr.h" #include "util/color.h" #include "util/dso.h" #include "util/vdso.h" @@ -24,6 +25,7 @@ #include "util/string2.h" #include "util/symbol.h" #include "util/synthetic-events.h" +#include "util/pmus.h" #include "util/thread.h" #include "util/namespaces.h" #include "util/unwind.h" @@ -124,6 +126,7 @@ struct perf_inject { bool in_place_update_dry_run; bool copy_kcore_dir; bool convert_callchain; + bool aslr; const char *input_name; struct perf_data output; u64 bytes_written; @@ -242,8 +245,43 @@ static int perf_event__repipe_attr(const struct perf_t= ool *tool, if (!inject->output.is_pipe) return 0; =20 - if (!inject->itrace_synth_opts.set) + if (!inject->itrace_synth_opts.set) { + if (inject->aslr) { + union perf_event *stripped_event =3D malloc(event->header.size); + int err; + + if (!stripped_event) + return -ENOMEM; + memcpy(stripped_event, event, event->header.size); + stripped_event->attr.attr.sample_type &=3D ASLR_SUPPORTED_SAMPLE_TYPE; + + if (stripped_event->attr.attr.type =3D=3D PERF_TYPE_BREAKPOINT && + event->header.size >=3D (offsetof(struct perf_record_header_attr, + attr.bp_addr) + sizeof(u64))) { + stripped_event->attr.attr.bp_addr =3D 0; + } else if (stripped_event->attr.attr.type >=3D PERF_TYPE_MAX) { + struct perf_pmu *pmu; + + pmu =3D perf_pmus__find_by_type(stripped_event->attr.attr.type); + if (pmu && (!strcmp(pmu->name, "kprobe") || + !strcmp(pmu->name, "uprobe"))) { + if (event->header.size >=3D + (offsetof(struct perf_record_header_attr, + attr.config1) + sizeof(u64))) + stripped_event->attr.attr.config1 =3D 0; + if (event->header.size >=3D + (offsetof(struct perf_record_header_attr, + attr.config2) + sizeof(u64))) + stripped_event->attr.attr.config2 =3D 0; + } + } + + err =3D perf_event__repipe_synth(tool, stripped_event); + free(stripped_event); + return err; + } return perf_event__repipe_synth(tool, event); + } =20 if (event->header.size < sizeof(struct perf_event_header) + PERF_ATTR_SIZ= E_VER0) { pr_err("Attribute event size %u is too small\n", event->header.size); @@ -276,6 +314,17 @@ static int perf_event__repipe_attr(const struct perf_t= ool *tool, =20 attr.size =3D sizeof(struct perf_event_attr); attr.sample_type &=3D ~PERF_SAMPLE_AUX; + if (inject->aslr) { + attr.sample_type &=3D ASLR_SUPPORTED_SAMPLE_TYPE; + if (attr.type >=3D PERF_TYPE_MAX) { + struct perf_pmu *pmu =3D perf_pmus__find_by_type(attr.type); + + if (pmu && (!strcmp(pmu->name, "kprobe") || !strcmp(pmu->name, "uprobe"= ))) { + attr.config1 =3D 0; + attr.config2 =3D 0; + } + } + } =20 if (inject->itrace_synth_opts.add_last_branch) { attr.sample_type |=3D PERF_SAMPLE_BRANCH_STACK; @@ -2594,7 +2643,6 @@ static int __cmd_inject(struct perf_inject *inject) evsel->core.attr.exclude_callchain_user =3D 0; } } - session->header.data_offset =3D output_data_offset; session->header.data_size =3D inject->bytes_written; perf_session__inject_header(session, session->evlist, fd, &inj_fc.fc, @@ -2704,6 +2752,8 @@ int cmd_inject(int argc, const char **argv) unwind__option), OPT_BOOLEAN(0, "convert-callchain", &inject.convert_callchain, "Generate callchains using DWARF and drop register/stack data"), + OPT_BOOLEAN(0, "aslr", &inject.aslr, + "Remap virtual memory addresses similar to ASLR"), OPT_END() }; const char * const inject_usage[] =3D { @@ -2711,6 +2761,7 @@ int cmd_inject(int argc, const char **argv) NULL }; bool ordered_events; + struct perf_tool *tool =3D &inject.tool; =20 if (!inject.itrace_synth_opts.set) { /* Disable eager loading of kernel symbols that adds overhead to perf in= ject. */ @@ -2731,6 +2782,11 @@ int cmd_inject(int argc, const char **argv) if (argc) usage_with_options(inject_usage, options); =20 + if (inject.aslr && inject.convert_callchain) { + pr_err("Error: --aslr and --convert-callchain are mutually exclusive fea= tures.\n"); + return -EINVAL; + } + if (inject.strip && !inject.itrace_synth_opts.set) { pr_err("--strip option requires --itrace option\n"); return -1; @@ -2824,12 +2880,21 @@ int cmd_inject(int argc, const char **argv) inject.tool.schedstat_domain =3D perf_event__repipe_op2_synth; inject.tool.dont_split_sample_group =3D true; inject.tool.merge_deferred_callchains =3D false; - inject.session =3D __perf_session__new(&data, &inject.tool, + if (inject.aslr) { + tool =3D aslr_tool__new(&inject.tool); + if (!tool) { + ret =3D -ENOMEM; + goto out_close_output; + } + } + inject.session =3D __perf_session__new(&data, tool, /*trace_event_repipe=3D*/inject.output.is_pipe, /*host_env=3D*/NULL); =20 if (IS_ERR(inject.session)) { ret =3D PTR_ERR(inject.session); + if (inject.aslr) + aslr_tool__delete(tool); goto out_close_output; } =20 @@ -2923,12 +2988,25 @@ int cmd_inject(int argc, const char **argv) =20 ret =3D __cmd_inject(&inject); =20 + if (inject.aslr) { + struct evsel *evsel; + + evlist__for_each_entry(inject.session->evlist, evsel) { + evsel->core.attr.sample_type &=3D ASLR_SUPPORTED_SAMPLE_TYPE; + + if (evsel->core.attr.type =3D=3D PERF_TYPE_BREAKPOINT) + evsel->core.attr.bp_addr =3D 0; + } + } + guest_session__exit(&inject.guest_session); =20 out_delete: strlist__delete(inject.known_build_ids); zstd_fini(&(inject.session->zstd_data)); perf_session__delete(inject.session); + if (inject.aslr) + aslr_tool__delete(tool); out_close_output: if (!inject.in_place_update) perf_data__close(&inject.output); diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 4bbc78b1f741..19994e026ae5 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -6,6 +6,7 @@ perf-util-y +=3D arm64-frame-pointer-unwind-support.o perf-util-y +=3D addr2line.o perf-util-y +=3D addr_location.o perf-util-y +=3D annotate.o +perf-util-y +=3D aslr.o perf-util-y +=3D blake2s.o perf-util-y +=3D block-info.o perf-util-y +=3D block-range.o diff --git a/tools/perf/util/aslr.c b/tools/perf/util/aslr.c new file mode 100644 index 000000000000..084158014dc7 --- /dev/null +++ b/tools/perf/util/aslr.c @@ -0,0 +1,732 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "aslr.h" + +#include "addr_location.h" +#include "debug.h" +#include "event.h" +#include "evsel.h" +#include "machine.h" +#include "map.h" +#include "thread.h" +#include "tool.h" +#include "session.h" +#include "data.h" +#include "dso.h" + +#include /* page_size */ +#include +#include +#include +#include + +/** + * struct remap_addresses_key - Key for mapping original addresses to rema= pped ones. + * @dso: Pointer to the DSO (Dynamic Shared Object) associated with the ma= pping. + * @invariant: Unique offset invariant within the VMA (Virtual Memory Area= ). + * Calculated as `start - pgoff`. This value remains constant = when + * perf's internal `maps__fixup_overlap_and_insert` splits a m= ap into + * fragmented VMA pieces due to overlapping events, allowing u= s to + * resolve split maps consistently back to the original VMA. + * @pid: Process ID associated with the mapping. + */ +struct remap_addresses_key { + struct machine *machine; + struct dso *dso; + u64 invariant; + pid_t pid; +}; + +struct aslr_mapping { + struct list_head node; + u64 orig_start; + u64 len; + u64 remap_start; +}; + +struct process_top_address { + u64 remapped_max; +}; +struct aslr_tool { + /** @tool: The tool implemented here and a pointer to a delegate to proce= ss the data. */ + struct delegate_tool tool; + /** @machines: The machines with the input, not remapped, virtual address= layout. */ + struct machines machines; + /** @event_copy: Buffer used to create an event to pass to the delegate. = */ + char event_copy[PERF_SAMPLE_MAX_SIZE] __aligned(8); + /** @remap_addresses: mapping from remap_addresses_key to remapped addres= s. */ + struct hashmap remap_addresses; + /** @top_addresses: mapping from process to max remapped address. */ + struct hashmap top_addresses; +}; + +static const pid_t kernel_pid =3D -1; + +/* Start remapping user processes from a small non-zero offset. */ +static const u64 user_space_start =3D 0x200000; +static const u64 kernel_space_start_64 =3D 0xffff800010000000ULL; +static const u64 kernel_space_start_32 =3D 0x80000000ULL; + +static size_t remap_addresses__hash(long _key, void *ctx __maybe_unused) +{ + struct remap_addresses_key *key =3D (struct remap_addresses_key *)_key; + void *dso_ptr =3D key->dso ? RC_CHK_ACCESS(key->dso) : NULL; + + return (size_t)key->machine ^ (size_t)dso_ptr ^ key->invariant ^ key->pid; +} + +static bool remap_addresses__equal(long _key1, long _key2, void *ctx __may= be_unused) +{ + struct remap_addresses_key *key1 =3D (struct remap_addresses_key *)_key1; + struct remap_addresses_key *key2 =3D (struct remap_addresses_key *)_key2; + + return key1->machine =3D=3D key2->machine && + RC_CHK_EQUAL(key1->dso, key2->dso) && + key1->invariant =3D=3D key2->invariant && + key1->pid =3D=3D key2->pid; +} + +struct top_addresses_key { + struct machine *machine; + pid_t pid; +}; + +static size_t top_addresses__hash(long _key, void *ctx __maybe_unused) +{ + struct top_addresses_key *key =3D (struct top_addresses_key *)_key; + + return (size_t)key->machine ^ key->pid; +} + +static bool top_addresses__equal(long _key1, long _key2, void *ctx __maybe= _unused) +{ + struct top_addresses_key *key1 =3D (struct top_addresses_key *)_key1; + struct top_addresses_key *key2 =3D (struct top_addresses_key *)_key2; + + return key1->machine =3D=3D key2->machine && key1->pid =3D=3D key2->pid; +} + +static u64 round_up_to_page_size(u64 addr) +{ + return (addr + page_size - 1) & ~((u64)page_size - 1); +} + +struct aslr_machine_priv { + bool kernel_maps_loaded; +}; + +static int aslr_tool__preload_kernel_maps(struct machine *machine) +{ + struct aslr_machine_priv *mpriv =3D machine->priv; + + if (!mpriv) { + mpriv =3D zalloc(sizeof(*mpriv)); + if (!mpriv) + return -ENOMEM; + machine->priv =3D mpriv; + } + + if (!mpriv->kernel_maps_loaded) { + struct maps *kmaps =3D machine__kernel_maps(machine); + + if (kmaps) { + int err =3D maps__load_maps(kmaps); + + if (err < 0) { + pr_err("ASLR: Failed to preload kernel maps for machine pid %d\n", + machine->pid); + return err; + } + } + mpriv->kernel_maps_loaded =3D true; + } + return 0; +} + +static void aslr_tool__free_machine_priv(struct machine *machine) +{ + free(machine->priv); + machine->priv =3D NULL; +} + +static void aslr_tool__destroy_machines_priv(struct machines *machines) +{ + struct rb_node *nd; + + aslr_tool__free_machine_priv(&machines->host); + for (nd =3D rb_first_cached(&machines->guests); nd; nd =3D rb_next(nd)) { + struct machine *machine =3D rb_entry(nd, struct machine, rb_node); + + aslr_tool__free_machine_priv(machine); + } +} + +static u64 aslr_tool__findnew_mapping(struct aslr_tool *aslr, + struct thread *aslr_thread, + u8 cpumode, u64 start, + u64 len, u64 pgoff) +{ + /* Address location for dso lookup. */ + struct addr_location al; + /* Original ASLR address based key for the remap table. */ + struct remap_addresses_key remap_key; + /* The address in the ASLR sanitized address space less pg_off. */ + u64 *remapped_invariant_ptr; + /* Key for the maximum address in a process. */ + struct top_addresses_key top_addr_key; + /* Value in top address table. */ + struct process_top_address *top =3D NULL; + /* Address in ASLR sanitized address space. */ + u64 remap_addr; + /* Potentially allocated remap table key. */ + struct remap_addresses_key *new_remap_key =3D NULL; + /* + * Potentially allocated remap table key. + * TODO: Avoid allocation necessary for perf 32-bit binary support. + */ + u64 *new_remap_val =3D NULL; + int err; + + if (!aslr_thread) + return 0; + + /* The key to look up an incoming address to the outgoing value. */ + addr_location__init(&al); + remap_key.machine =3D maps__machine(thread__maps(aslr_thread)); + remap_key.pid =3D (cpumode =3D=3D PERF_RECORD_MISC_KERNEL || + cpumode =3D=3D PERF_RECORD_MISC_GUEST_KERNEL) ? + kernel_pid : thread__pid(aslr_thread); + if (thread__find_map(aslr_thread, cpumode, start, &al)) { + struct dso *dso =3D map__dso(al.map); + const char *dso_name =3D dso ? dso__long_name(dso) : NULL; + + remap_key.dso =3D dso; + if (dso && !is_anon_memory(dso_name) && !is_no_dso_memory(dso_name)) + remap_key.invariant =3D map__start(al.map) - map__pgoff(al.map); + else + remap_key.invariant =3D map__start(al.map); + } else { + remap_key.dso =3D NULL; + remap_key.invariant =3D start; + } + + /* The key to look up top allocated address. */ + top_addr_key.machine =3D remap_key.machine; + top_addr_key.pid =3D remap_key.pid; + + if (hashmap__find(&aslr->remap_addresses, &remap_key, &remapped_invariant= _ptr)) { + /* Mmap already exists. */ + u64 calculated_max; + + if (al.map) { + /* + * The cached value is the base of the invariant. We add the + * offset into the VMA (start - map__start), plus the map's + * pgoff, to get the precise virtual address within this chunk. + */ + remap_addr =3D *remapped_invariant_ptr + map__pgoff(al.map) + + (start - map__start(al.map)); + } else { + /* + * For unmapped memory (e.g. kernel anonymous), the cached value + * was stored offset by pgoff. Adding pgoff yields the true remap_addr. + */ + remap_addr =3D *remapped_invariant_ptr + pgoff; + } + + calculated_max =3D remap_addr + len; + + /* See if top mapping was expanded. */ + if (hashmap__find(&aslr->top_addresses, &top_addr_key, &top)) { + if (calculated_max > top->remapped_max) + top->remapped_max =3D calculated_max; + } + addr_location__exit(&al); + return remap_addr; + } + /* No mmap, create an entry from the top address. */ + if (hashmap__find(&aslr->top_addresses, &top_addr_key, &top)) { + struct addr_location prev_al; + bool is_contiguous =3D false; + + /* Current max allocated mmap address within the process. */ + remap_addr =3D top->remapped_max; + + addr_location__init(&prev_al); + if (thread__find_map(aslr_thread, cpumode, start - 1, &prev_al)) { + if (map__end(prev_al.map) =3D=3D start) + is_contiguous =3D true; + } + addr_location__exit(&prev_al); + + if (is_contiguous) { + /* Contiguous mapping, do not add 1 page gap! */ + remap_addr =3D round_up_to_page_size(remap_addr); + } else { + /* Give 1 page gap from current max page. */ + remap_addr =3D round_up_to_page_size(remap_addr); + remap_addr +=3D page_size; + } + if (remap_addr + len > top->remapped_max) + top->remapped_max =3D remap_addr + len; + } else { + /* First address of the process, allocate key and first top address. */ + struct top_addresses_key *tk; + struct process_top_address *top_val; + struct machine *machine =3D remap_key.machine; + struct perf_env *env =3D machine ? machine->env : NULL; + bool is_64 =3D env ? perf_env__kernel_is_64_bit(env) : (sizeof(void *) = =3D=3D 8); + u64 kernel_start_addr =3D is_64 ? kernel_space_start_64 : kernel_space_s= tart_32; + + remap_addr =3D (cpumode =3D=3D PERF_RECORD_MISC_KERNEL || + cpumode =3D=3D PERF_RECORD_MISC_GUEST_KERNEL) ? + kernel_start_addr : user_space_start; + remap_addr =3D round_up_to_page_size(remap_addr); + + tk =3D malloc(sizeof(*tk)); + top_val =3D malloc(sizeof(*top_val)); + if (!tk || !top_val) { + err =3D -ENOMEM; + } else { + *tk =3D top_addr_key; + top_val->remapped_max =3D remap_addr + len; + err =3D hashmap__insert(&aslr->top_addresses, tk, top_val, + HASHMAP_ADD, NULL, NULL); + } + if (err) { + errno =3D -err; + pr_err("Failure to add ASLR process top address %m\n"); + free(tk); + free(top_val); + addr_location__exit(&al); + return 0; + } + } + /* Create rmeapping entry. */ + new_remap_key =3D malloc(sizeof(*new_remap_key)); + new_remap_val =3D malloc(sizeof(u64)); + if (!new_remap_key || !new_remap_val) { + err =3D -ENOMEM; + } else { + *new_remap_key =3D remap_key; + new_remap_key->dso =3D dso__get(remap_key.dso); + if (cpumode =3D=3D PERF_RECORD_MISC_KERNEL || + cpumode =3D=3D PERF_RECORD_MISC_GUEST_KERNEL) { + if (al.map) { + *new_remap_val =3D remap_addr - + (start - map__start(al.map)) - + map__pgoff(al.map); + } else { + /* + * Subtract pgoff from the base virtual address so that + * when the lookup path adds pgoff back, it perfectly + * cancels out and returns remap_addr. + */ + *new_remap_val =3D remap_addr - pgoff; + } + } else { + *new_remap_val =3D remap_addr - (al.map ? map__pgoff(al.map) : pgoff); + } + err =3D hashmap__add(&aslr->remap_addresses, new_remap_key, new_remap_va= l); + if (err) + dso__put(new_remap_key->dso); + } + if (err) { + errno =3D -err; + pr_err("Failure to add ASLR remapping %m\n"); + free(new_remap_key); + free(new_remap_val); + addr_location__exit(&al); + return 0; + } + addr_location__exit(&al); + return remap_addr; +} + +static int aslr_tool__process_mmap(const struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + union perf_event *new_event; + u8 cpumode; + struct thread *thread; + struct machine *aslr_machine; + int err; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + new_event =3D (union perf_event *)aslr->event_copy; + cpumode =3D event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + if (aslr_tool__preload_kernel_maps(aslr_machine) < 0) + return -ENOMEM; + + /* Create the thread, map, etc. in the ASLR before virtual address space.= */ + err =3D perf_event__process_mmap(tool, event, sample, aslr_machine); + if (err) + return err; + + thread =3D machine__findnew_thread(aslr_machine, event->mmap.pid, event->= mmap.tid); + if (!thread) + return -ENOMEM; + memcpy(&new_event->mmap, &event->mmap, event->mmap.header.size); + /* Remaps the mmap.start. */ + new_event->mmap.start =3D aslr_tool__findnew_mapping(aslr, thread, cpumod= e, + event->mmap.start, + event->mmap.len, + event->mmap.pgoff); + /* + * For anonymous memory (and kernel maps), the kernel populates the + * event's pgoff field with the original un-obfuscated virtual address + * in bytes (i.e. (addr >> PAGE_SHIFT) << PAGE_SHIFT). + * We must overwrite pgoff with the new remapped byte address to prevent + * leaking the original ASLR layout. + */ + if (cpumode =3D=3D PERF_RECORD_MISC_KERNEL || cpumode =3D=3D PERF_RECORD_= MISC_GUEST_KERNEL || + is_anon_memory(event->mmap.filename) || is_no_dso_memory(event->mmap.= filename)) + new_event->mmap.pgoff =3D new_event->mmap.start; + err =3D delegate->mmap(delegate, new_event, sample, machine); + thread__put(thread); + return err; +} + +static int aslr_tool__process_mmap2(const struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + union perf_event *new_event; + u8 cpumode; + struct thread *thread; + struct machine *aslr_machine; + int err; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + new_event =3D (union perf_event *)aslr->event_copy; + cpumode =3D event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + if (aslr_tool__preload_kernel_maps(aslr_machine) < 0) + return -ENOMEM; + + /* Create the thread, map, etc. in the ASLR before virtual address space.= */ + err =3D perf_event__process_mmap2(tool, event, sample, aslr_machine); + if (err) + return err; + + thread =3D machine__findnew_thread(aslr_machine, event->mmap2.pid, event-= >mmap2.tid); + if (!thread) + return -ENOMEM; + memcpy(&new_event->mmap2, &event->mmap2, event->mmap2.header.size); + /* Remaps the mmap.start. */ + new_event->mmap2.start =3D aslr_tool__findnew_mapping(aslr, thread, cpumo= de, + event->mmap2.start, + event->mmap2.len, + event->mmap2.pgoff); + /* + * For anonymous memory (and kernel maps), the kernel populates the + * event's pgoff field with the original un-obfuscated virtual address + * in bytes (i.e. (addr >> PAGE_SHIFT) << PAGE_SHIFT). + * We must overwrite pgoff with the new remapped byte address to prevent + * leaking the original ASLR layout. + */ + if (cpumode =3D=3D PERF_RECORD_MISC_KERNEL || cpumode =3D=3D PERF_RECORD_= MISC_GUEST_KERNEL || + is_anon_memory(event->mmap2.filename) || is_no_dso_memory(event->mmap= 2.filename)) + new_event->mmap2.pgoff =3D new_event->mmap2.start; + err =3D delegate->mmap2(delegate, new_event, sample, machine); + thread__put(thread); + return err; +} + +static int aslr_tool__process_comm(const struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + struct machine *aslr_machine; + int err; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + if (aslr_tool__preload_kernel_maps(aslr_machine) < 0) + return -ENOMEM; + + /* Create the thread, map, etc. in the ASLR before virtual address space.= */ + err =3D perf_event__process_comm(tool, event, sample, aslr_machine); + if (err) + return err; + + return delegate->comm(delegate, event, sample, machine); +} + +static int aslr_tool__process_fork(const struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + struct machine *aslr_machine; + int err; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + if (aslr_tool__preload_kernel_maps(aslr_machine) < 0) + return -ENOMEM; + + /* Create the thread, map, etc. in the ASLR before virtual address space.= */ + err =3D perf_event__process_fork(tool, event, sample, aslr_machine); + if (err) + return err; + + return delegate->fork(delegate, event, sample, machine); +} + +static int aslr_tool__process_exit(const struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + struct machine *aslr_machine; + int err; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + if (aslr_tool__preload_kernel_maps(aslr_machine) < 0) + return -ENOMEM; + + /* Create the thread, map, etc. in the ASLR before virtual address space.= */ + err =3D perf_event__process_exit(tool, event, sample, aslr_machine); + if (err) + return err; + + return delegate->exit(delegate, event, sample, machine); +} + +static int aslr_tool__process_text_poke(const struct perf_tool *tool __may= be_unused, + union perf_event *event __maybe_unused, + struct perf_sample *sample __maybe_unused, + struct machine *machine __maybe_unused) +{ + /* Drop in case the instruction encodes an ASLR revealing address. */ + return 0; +} + +static int aslr_tool__process_ksymbol(const struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + union perf_event *new_event; + struct thread *thread; + struct machine *aslr_machine; + int err; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + new_event =3D (union perf_event *)aslr->event_copy; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + if (aslr_tool__preload_kernel_maps(aslr_machine) < 0) + return -ENOMEM; + + err =3D perf_event__process_ksymbol(tool, event, sample, aslr_machine); + if (err) + return err; + + thread =3D machine__findnew_thread(aslr_machine, kernel_pid, 0); + if (!thread) + return -ENOMEM; + memcpy(&new_event->ksymbol, &event->ksymbol, event->ksymbol.header.size); + /* Remaps the ksymbol.start */ + new_event->ksymbol.addr =3D aslr_tool__findnew_mapping(aslr, thread, + PERF_RECORD_MISC_KERNEL, + event->ksymbol.addr, + event->ksymbol.len, + /*pgoff=3D*/0); + + err =3D delegate->ksymbol(delegate, new_event, sample, machine); + thread__put(thread); + return err; +} + +static int aslr_tool__process_sample(const struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample, + struct machine *machine) +{ + struct delegate_tool *del_tool =3D container_of(tool, struct delegate_too= l, tool); + struct aslr_tool *aslr =3D container_of(del_tool, struct aslr_tool, tool); + struct perf_tool *delegate =3D aslr->tool.delegate; + + return delegate->sample(delegate, event, sample, machine); +} + +static int skipn(int fd, off_t n) +{ + char buf[4096]; + ssize_t ret; + + while (n > 0) { + ret =3D read(fd, buf, min_t(off_t, n, (off_t)sizeof(buf))); + if (ret <=3D 0) + return ret; + n -=3D ret; + } + + return 0; +} + +static s64 aslr_tool__process_auxtrace(const struct perf_tool *tool __mayb= e_unused, + struct perf_session *session, + union perf_event *event) +{ + pr_warning_once("ASLR: Dropping auxtrace data as it cannot be obfuscated.= \n"); + if (perf_data__is_pipe(session->data)) { + /* Copy behavior of the stub by reading all pipe data. */ + int err =3D skipn(perf_data__fd(session->data), event->auxtrace.size); + + if (err < 0) + return err; + } + return event->auxtrace.size; +} + +static int aslr_tool__process_auxtrace_info(const struct perf_tool *tool _= _maybe_unused, + struct perf_session *session __maybe_unused, + union perf_event *event __maybe_unused) +{ + return 0; +} + +static int aslr_tool__process_auxtrace_error(const struct perf_tool *tool = __maybe_unused, + struct perf_session *session __maybe_unused, + union perf_event *event __maybe_unused) +{ + return 0; +} + +static void aslr_tool__init(struct aslr_tool *aslr, struct perf_tool *dele= gate) +{ + delegate_tool__init(&aslr->tool, delegate); + aslr->tool.tool.ordered_events =3D true; + + machines__init(&aslr->machines); + + hashmap__init(&aslr->remap_addresses, + remap_addresses__hash, remap_addresses__equal, + /*ctx=3D*/NULL); + hashmap__init(&aslr->top_addresses, + top_addresses__hash, top_addresses__equal, + /*ctx=3D*/NULL); + + aslr->tool.tool.sample =3D aslr_tool__process_sample; + /* read - reads a counter, okay to delegate. */ + aslr->tool.tool.mmap =3D aslr_tool__process_mmap; + aslr->tool.tool.mmap2 =3D aslr_tool__process_mmap2; + aslr->tool.tool.comm =3D aslr_tool__process_comm; + aslr->tool.tool.fork =3D aslr_tool__process_fork; + aslr->tool.tool.exit =3D aslr_tool__process_exit; + /* namesspaces, cgroup, lost, lost_sample, aux, */ + /* itrace_start, aux_output_hw_id, context_switch, throttle, unthrottle */ + /* - no virtual addresses. */ + aslr->tool.tool.ksymbol =3D aslr_tool__process_ksymbol; + /* bpf - no virtual address. */ + aslr->tool.tool.text_poke =3D aslr_tool__process_text_poke; + /* + * event_update, tracing_data, finished_round, build_id, id_index, + * auxtrace_info, auxtrace_error, time_conv, thread_map, cpu_map, + * stat_config, stat, feature, finished_init, bpf_metadata, compressed, + * auxtrace - no virtual addresses. + */ + aslr->tool.tool.auxtrace =3D aslr_tool__process_auxtrace; + aslr->tool.tool.auxtrace_info =3D aslr_tool__process_auxtrace_info; + aslr->tool.tool.auxtrace_error =3D aslr_tool__process_auxtrace_error; +} + +struct perf_tool *aslr_tool__new(struct perf_tool *delegate) +{ + struct aslr_tool *aslr =3D zalloc(sizeof(*aslr)); + + if (!aslr) + return NULL; + + aslr_tool__init(aslr, delegate); + return &aslr->tool.tool; +} + +void aslr_tool__delete(struct perf_tool *tool) +{ + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct hashmap_entry *cur; + size_t bkt; + + if (!tool) + return; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + + hashmap__for_each_entry(&aslr->remap_addresses, cur, bkt) { + struct remap_addresses_key *key =3D (struct remap_addresses_key *)cur->p= key; + + if (key) + dso__put(key->dso); + zfree(&cur->pkey); + zfree(&cur->pvalue); + } + hashmap__for_each_entry(&aslr->top_addresses, cur, bkt) { + zfree(&cur->pkey); + zfree(&cur->pvalue); + } + + hashmap__clear(&aslr->remap_addresses); + hashmap__clear(&aslr->top_addresses); + aslr_tool__destroy_machines_priv(&aslr->machines); + machines__destroy_kernel_maps(&aslr->machines); + machines__exit(&aslr->machines); + free(aslr); +} diff --git a/tools/perf/util/aslr.h b/tools/perf/util/aslr.h new file mode 100644 index 000000000000..a9b90bf29540 --- /dev/null +++ b/tools/perf/util/aslr.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __PERF_ASLR_H +#define __PERF_ASLR_H + +#include + +#define ASLR_SUPPORTED_SAMPLE_TYPE ( \ + PERF_SAMPLE_IDENTIFIER | \ + PERF_SAMPLE_IP | \ + PERF_SAMPLE_TID | \ + PERF_SAMPLE_TIME | \ + PERF_SAMPLE_ADDR | \ + PERF_SAMPLE_ID | \ + PERF_SAMPLE_STREAM_ID | \ + PERF_SAMPLE_CPU | \ + PERF_SAMPLE_PERIOD | \ + PERF_SAMPLE_READ | \ + PERF_SAMPLE_CALLCHAIN | \ + PERF_SAMPLE_RAW | \ + PERF_SAMPLE_BRANCH_STACK | \ + PERF_SAMPLE_STACK_USER | \ + PERF_SAMPLE_WEIGHT_TYPE | \ + PERF_SAMPLE_DATA_SRC | \ + PERF_SAMPLE_TRANSACTION | \ + PERF_SAMPLE_PHYS_ADDR | \ + PERF_SAMPLE_CGROUP | \ + PERF_SAMPLE_DATA_PAGE_SIZE | \ + PERF_SAMPLE_CODE_PAGE_SIZE | \ + PERF_SAMPLE_AUX) + +struct perf_tool; +struct evsel; + +struct perf_tool *aslr_tool__new(struct perf_tool *delegate); +void aslr_tool__delete(struct perf_tool *aslr); + +#endif /* __PERF_ASLR_H */ --=20 2.54.0.1032.g2f8565e1d1-goog From nobody Mon Jun 8 05:25:47 2026 Received: from mail-dy1-f201.google.com (mail-dy1-f201.google.com [74.125.82.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CAE953B47C9 for ; Sat, 6 Jun 2026 15:15:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780758913; cv=none; b=i18phaDVC6is8yWQ5BxXQfKy3Z4lKPWfZZKTxPy9O6KRqLwaT/wgnLg8lVTSeY+P7QdDx95sFDSff6gbGXBO9u+y++woUhv0X/Lj6XM8LxtJ5fP4WkZXGMbhfstQw53B97MLNdCzWnZOhvftP8g9lqxy6MTAbUO906+jhCBg8Ag= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780758913; c=relaxed/simple; bh=hICYkdRuiQCG/uM6/LRnqkBDzHjDVmcJjpu++NDeXYU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pR27uupnmutaCohJZXmeR9B9kLA1CqqUqOaLiXQSZMQRVill3Slsk8PSwBbtqeauXPbIRMVRhGUNoTcQ8WcZWaifkWL+iM6JJFr/IPJ9AFnHlDCYsXtsdIBbpJrxojsM53xHIUerst/ez/53NoMd13vuWnWc8B/y1EzjmqPimXE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wMjPwPBh; arc=none smtp.client-ip=74.125.82.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wMjPwPBh" Received: by mail-dy1-f201.google.com with SMTP id 5a478bee46e88-304f23c55b2so2875959eec.0 for ; Sat, 06 Jun 2026 08:15:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780758909; x=1781363709; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JMDwEb4xCVFfYH74bRHRUozUxieyPDsx5CNySZp321k=; b=wMjPwPBhFjDFg4eECWpF6Gg0RR+s6sll0ielusA2k3tqufqakf5onOUAHN5Zpm/hy6 x5SYRJpC4C0xWwF/Dq2fNYNSf0Q2/Uv2kPImOOymR4r5T4A622RHu9GpOvbfTsCkwolT 4jCHZ5b8/GJSX97kuI26CtXwuYHlg8jTPxyatox0Q/A73wgCrzi1l6hKwdgHH1ZcfJfz hvF/rChBQ1EajldHiG3/uGSU2laBoIdoK4xnPiWbUks4Tz3Y3p7RibN0RHLr47ENFhY9 JefA7xotteLknXJxA6DBnYsmxoS9KCiOETz/icp3iZN7L+MaPhC5ql0mODgIhgaYs+wj KlYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780758909; x=1781363709; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JMDwEb4xCVFfYH74bRHRUozUxieyPDsx5CNySZp321k=; b=BRqWYQa4hTiJmO8a3q7WD3Drwa6IzRq2o/PGH1lQmde+YmRRnPW0tRrpHjM4A6FK/+ 3rN5EovKped2D1ZJDITZPTkx1yWz0n8YTxDBiVqRcqv4/R1p/fxvH6Uenld7Bfp8TxiJ 3n4lErPvKb4w5w5cSWnswKWuwE76P6X6s8LL6k2PEaHvQBWSlnI5T94Eq5gbdR2LAndr OaXCrjtXa0oFulKxSneJpBL8HyNRJEDsUd5CScuRhPK0hM3Dhu0nv1rvfTfwCJMp0Trj AzQ1XM8993/tT7f4C4Bc3MCL+xEUKID2GTveDanPVHW0Ehcxyyk8babOF2gV9vNVjJuS dULA== X-Forwarded-Encrypted: i=1; AFNElJ+AoCm926gw5A/MnAsD2kIYAZFTKbaGRxpOJQhXsxz3yMEPa5jBEyR4e5Vkq6gpk+iiuTET9RS4clMdGkc=@vger.kernel.org X-Gm-Message-State: AOJu0YyH2cpmCWKqXTBQTchTndRofEgOIlDMyzy988cf1OD8gR/c90NP yHpq8YobURsjufS6DE9v7ZFL/giVxbWK9RbkctHcqJWW4qpxvD1ehc2M731T/QVyh5LN5HKlPQw 9Ei2Ge+/a3w== X-Received: from dyvp27.prod.google.com ([2002:a05:693c:885b:b0:303:3f37:eaf5]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:2327:b0:304:5db8:da95 with SMTP id 5a478bee46e88-3077b7855c1mr4632069eec.23.1780758908571; Sat, 06 Jun 2026 08:15:08 -0700 (PDT) Date: Sat, 6 Jun 2026 08:14:49 -0700 In-Reply-To: <20260606151451.2913860-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260606072125.2786845-1-irogers@google.com> <20260606151451.2913860-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.1032.g2f8565e1d1-goog Message-ID: <20260606151451.2913860-4-irogers@google.com> Subject: [PATCH v16 3/5] perf inject/aslr: Implement sample address remapping From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, gmx@google.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add the sample address remapping logic to the ASLR tool. This patch implements aslr_tool__process_sample, which parses sample events, remaps IPs, ADDRs, callchains, and branch stacks using the mappings collected from metadata events, and drops potentially leaking raw, register, stack, physical address, and aux samples. Also adds the aslr_tool__remap_address helper function. Co-developed-by: Gabriel Marin Signed-off-by: Gabriel Marin Signed-off-by: Ian Rogers --- tools/perf/util/aslr.c | 454 +++++++++++++++++++++++++++++++++++++++- tools/perf/util/evsel.c | 6 +- tools/perf/util/evsel.h | 10 +- 3 files changed, 461 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/aslr.c b/tools/perf/util/aslr.c index 084158014dc7..7afa5a0dac2f 100644 --- a/tools/perf/util/aslr.c +++ b/tools/perf/util/aslr.c @@ -110,6 +110,60 @@ static u64 round_up_to_page_size(u64 addr) return (addr + page_size - 1) & ~((u64)page_size - 1); } =20 +static u64 aslr_tool__remap_address(struct aslr_tool *aslr, + struct thread *aslr_thread, + u8 cpumode, + u64 addr) +{ + struct addr_location al; + struct remap_addresses_key key; + u64 *remapped_invariant_ptr =3D NULL; + u64 remap_addr =3D 0; + u8 effective_cpumode =3D cpumode; + + if (!aslr_thread) + return 0; /* No thread. */ + + addr_location__init(&al); + if (!thread__find_map(aslr_thread, cpumode, addr, &al)) { + /* + * If lookup fails with specified cpumode, try fallback to the other spa= ce + * to be robust against bad cpumode in samples. + */ + if (cpumode =3D=3D PERF_RECORD_MISC_KERNEL) + effective_cpumode =3D PERF_RECORD_MISC_USER; + else if (cpumode =3D=3D PERF_RECORD_MISC_USER) + effective_cpumode =3D PERF_RECORD_MISC_KERNEL; + else if (cpumode =3D=3D PERF_RECORD_MISC_GUEST_KERNEL) + effective_cpumode =3D PERF_RECORD_MISC_GUEST_USER; + else if (cpumode =3D=3D PERF_RECORD_MISC_GUEST_USER) + effective_cpumode =3D PERF_RECORD_MISC_GUEST_KERNEL; + + if (!thread__find_map(aslr_thread, effective_cpumode, addr, &al)) { + addr_location__exit(&al); + return 0; /* No mmap. */ + } + } + + key.machine =3D maps__machine(thread__maps(aslr_thread)); + key.dso =3D map__dso(al.map); + key.invariant =3D map__start(al.map) - map__pgoff(al.map); + key.pid =3D (effective_cpumode =3D=3D PERF_RECORD_MISC_KERNEL || + effective_cpumode =3D=3D PERF_RECORD_MISC_GUEST_KERNEL) ? + kernel_pid : thread__pid(aslr_thread); + + if (hashmap__find(&aslr->remap_addresses, &key, &remapped_invariant_ptr))= { + remap_addr =3D *remapped_invariant_ptr + map__pgoff(al.map) + + (addr - map__start(al.map)); + } else { + pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%l= x) for pid=3D%d\n", + addr, map__start(al.map), map__size(al.map), key.pid); + } + + addr_location__exit(&al); + return remap_addr; +} + struct aslr_machine_priv { bool kernel_maps_loaded; }; @@ -597,13 +651,405 @@ static int aslr_tool__process_sample(const struct pe= rf_tool *tool, struct perf_sample *sample, struct machine *machine) { - struct delegate_tool *del_tool =3D container_of(tool, struct delegate_too= l, tool); - struct aslr_tool *aslr =3D container_of(del_tool, struct aslr_tool, tool); - struct perf_tool *delegate =3D aslr->tool.delegate; + struct evsel *evsel =3D sample->evsel; + struct delegate_tool *del_tool; + struct aslr_tool *aslr; + struct perf_tool *delegate; + int ret; + u64 sample_type; + struct thread *thread; + struct machine *aslr_machine; + __u64 max_i; + __u64 max_j; + union perf_event *new_event; + struct perf_sample new_sample; + __u64 *in_array, *out_array; + u8 cpumode; + u64 addr; + size_t i; + size_t j; + bool orig_needs_swap; + + del_tool =3D container_of(tool, struct delegate_tool, tool); + aslr =3D container_of(del_tool, struct aslr_tool, tool); + delegate =3D aslr->tool.delegate; + + if (evsel__is_dummy_event(evsel)) + return delegate->sample(delegate, event, sample, machine); + + ret =3D -EFAULT; + sample_type =3D evsel->core.attr.sample_type; + max_i =3D (event->header.size - sizeof(struct perf_event_header)) / sizeo= f(__u64); + max_j =3D (PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_event_header)) / siz= eof(__u64); + new_event =3D (union perf_event *)aslr->event_copy; + cpumode =3D sample->cpumode; + i =3D 0; + j =3D 0; + + aslr_machine =3D machines__findnew(&aslr->machines, machine->pid); + if (!aslr_machine) + return -ENOMEM; + if (aslr_tool__preload_kernel_maps(aslr_machine) < 0) + return -ENOMEM; + + thread =3D machine__findnew_thread(aslr_machine, sample->pid, sample->tid= ); + + if (!thread) + return -ENOMEM; + + if (max_i > PERF_SAMPLE_MAX_SIZE / sizeof(u64)) + goto out_put; + + new_event->sample.header =3D event->sample.header; + + in_array =3D &event->sample.array[0]; + out_array =3D &new_event->sample.array[0]; + +#define CHECK_BOUNDS(required_i, required_j) \ + (i + (required_i) > max_i || j + (required_j) > max_j) + +#define COPY_U64() \ + do { \ + if (CHECK_BOUNDS(1, 1)) { \ + ret =3D -EFAULT; \ + goto out_put; \ + } \ + out_array[j++] =3D in_array[i++]; \ + } while (0) + +#define REMAP_U64(addr_field) \ + do { \ + if (CHECK_BOUNDS(1, 1)) { \ + ret =3D -EFAULT; \ + goto out_put; \ + } \ + out_array[j++] =3D aslr_tool__remap_address(aslr, thread, cpumode, addr_= field); \ + i++; \ + } while (0) + + if (sample_type & PERF_SAMPLE_IDENTIFIER) + COPY_U64(); /* id */ + if (sample_type & PERF_SAMPLE_IP) + REMAP_U64(sample->ip); + if (sample_type & PERF_SAMPLE_TID) + COPY_U64(); /* pid, tid */ + if (sample_type & PERF_SAMPLE_TIME) + COPY_U64(); /* time */ + if (sample_type & PERF_SAMPLE_ADDR) + REMAP_U64(sample->addr); + if (sample_type & PERF_SAMPLE_ID) + COPY_U64(); /* id */ + if (sample_type & PERF_SAMPLE_STREAM_ID) + COPY_U64(); /* stream_id */ + if (sample_type & PERF_SAMPLE_CPU) + COPY_U64(); /* cpu, res */ + if (sample_type & PERF_SAMPLE_PERIOD) + COPY_U64(); /* period */ + if (sample_type & PERF_SAMPLE_READ) { + if ((evsel->core.attr.read_format & PERF_FORMAT_GROUP) =3D=3D 0) { + COPY_U64(); /* value */ + if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) + COPY_U64(); /* time_enabled */ + if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) + COPY_U64(); /* time_running */ + if (evsel->core.attr.read_format & PERF_FORMAT_ID) + COPY_U64(); /* id */ + if (evsel->core.attr.read_format & PERF_FORMAT_LOST) + COPY_U64(); /* lost */ + } else { + u64 nr; + + if (CHECK_BOUNDS(1, 1)) { + ret =3D -EFAULT; + goto out_put; + } + out_array[j] =3D in_array[i]; + nr =3D out_array[j++]; + i++; + if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) + COPY_U64(); /* time_enabled */ + if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) + COPY_U64(); /* time_running */ + for (u64 cntr =3D 0; cntr < nr; cntr++) { + COPY_U64(); /* value */ + if (evsel->core.attr.read_format & PERF_FORMAT_ID) + COPY_U64(); /* id */ + if (evsel->core.attr.read_format & PERF_FORMAT_LOST) + COPY_U64(); /* lost */ + } + } + } + if (sample_type & PERF_SAMPLE_CALLCHAIN) { + u64 nr; + + if (CHECK_BOUNDS(1, 1)) { + ret =3D -EFAULT; + goto out_put; + } + out_array[j] =3D in_array[i]; + nr =3D out_array[j++]; + i++; + + for (u64 cntr =3D 0; cntr < nr; cntr++) { + if (CHECK_BOUNDS(1, 1)) { + ret =3D -EFAULT; + goto out_put; + } + addr =3D in_array[i++]; + if (addr >=3D PERF_CONTEXT_MAX) { + out_array[j++] =3D addr; + switch (addr) { + case PERF_CONTEXT_HV: + cpumode =3D PERF_RECORD_MISC_HYPERVISOR; + break; + case PERF_CONTEXT_KERNEL: + cpumode =3D PERF_RECORD_MISC_KERNEL; + break; + case PERF_CONTEXT_USER: + cpumode =3D PERF_RECORD_MISC_USER; + break; + case PERF_CONTEXT_GUEST: + cpumode =3D PERF_RECORD_MISC_GUEST_KERNEL; + break; + case PERF_CONTEXT_GUEST_KERNEL: + cpumode =3D PERF_RECORD_MISC_GUEST_KERNEL; + break; + case PERF_CONTEXT_GUEST_USER: + cpumode =3D PERF_RECORD_MISC_GUEST_USER; + break; + case PERF_CONTEXT_USER_DEFERRED: + if (cntr + 1 >=3D nr) { + pr_debug("Truncated callchain deferred cookie context\n"); + ret =3D 0; + goto out_put; + } + /* + * Immediately followed by a 64-bit + * stitching cookie. Skip/Copy it! + */ + if (CHECK_BOUNDS(1, 1)) { + ret =3D -EFAULT; + goto out_put; + } + out_array[j++] =3D in_array[i++]; + cntr++; + cpumode =3D PERF_RECORD_MISC_USER; + break; + default: + pr_debug("invalid callchain context: %"PRIx64"\n", addr); + ret =3D 0; + goto out_put; + } + continue; + } + out_array[j++] =3D aslr_tool__remap_address(aslr, thread, cpumode, addr= ); + } + } + if (sample_type & PERF_SAMPLE_RAW) { + size_t bytes =3D sizeof(u32) + sample->raw_size; + size_t u64_words =3D (bytes + 7) / 8; + + if (i + u64_words > max_i || j + u64_words > max_j) { + ret =3D -EFAULT; + goto out_put; + } + memcpy(&out_array[j], &in_array[i], bytes); + i +=3D u64_words; + j +=3D u64_words; + /* + * TODO: certain raw samples can be remapped, such as + * tracepoints by examining their fields. + */ + pr_debug("Dropping raw samples as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + if (sample_type & PERF_SAMPLE_BRANCH_STACK) { + u64 nr; + + if (CHECK_BOUNDS(1, 1)) { + ret =3D -EFAULT; + goto out_put; + } + out_array[j] =3D in_array[i]; + nr =3D out_array[j++]; + i++; + + if (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX) + COPY_U64(); /* hw_idx */ + + if (nr > (ULLONG_MAX / 3)) { + ret =3D -EFAULT; + goto out_put; + } + if (nr * 3 > max_i - i || nr * 3 > max_j - j) { + ret =3D -EFAULT; + goto out_put; + } + for (u64 cntr =3D 0; cntr < nr; cntr++) { + out_array[j++] =3D aslr_tool__remap_address(aslr, thread, + sample->cpumode, + in_array[i++]); /* from */ + out_array[j++] =3D aslr_tool__remap_address(aslr, thread, + sample->cpumode, + in_array[i++]); /* to */ + out_array[j++] =3D in_array[i++]; /* flags */ + } + if (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) { + if (nr > max_i - i || nr > max_j - j) { + ret =3D -EFAULT; + goto out_put; + } + memcpy(&out_array[j], &in_array[i], nr * sizeof(u64)); + i +=3D nr; + j +=3D nr; + /* TODO: confirm branch counters don't leak ASLR information. */ + pr_debug("Dropping sample branch counters as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + } + if (sample_type & PERF_SAMPLE_REGS_USER) { + if (CHECK_BOUNDS(1, 0)) { + ret =3D -EFAULT; + goto out_put; + } + /* abi */ + COPY_U64(); + /* TODO: can this be less conservative? */ + pr_debug("Dropping regs user sample as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + if (sample_type & PERF_SAMPLE_STACK_USER) { + u64 size; + + if (CHECK_BOUNDS(1, 1)) { + ret =3D -EFAULT; + goto out_put; + } + out_array[j] =3D in_array[i]; + size =3D out_array[j++]; + i++; + if (size > 0) { + size_t u64_words =3D size / 8 + (size % 8 ? 1 : 0); + + if (u64_words > max_i - i || u64_words > max_j - j) { + ret =3D -EFAULT; + goto out_put; + } + memcpy(&out_array[j], &in_array[i], size); + if (size % 8) { + size_t pad =3D 8 - (size % 8); + + memset(((char *)&out_array[j]) + size, 0, pad); + } + i +=3D u64_words; + j +=3D u64_words; + } + /* TODO: can this be less conservative? */ + pr_debug("Dropping stack user sample as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) + COPY_U64(); /* perf_sample_weight */ + if (sample_type & PERF_SAMPLE_DATA_SRC) + COPY_U64(); /* data_src */ + if (sample_type & PERF_SAMPLE_TRANSACTION) + COPY_U64(); /* transaction */ + if (sample_type & PERF_SAMPLE_REGS_INTR) { + if (CHECK_BOUNDS(1, 0)) { + ret =3D -EFAULT; + goto out_put; + } + /* abi */ + COPY_U64(); + /* TODO: can this be less conservative? */ + pr_debug("Dropping interrupt register sample as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + if (sample_type & PERF_SAMPLE_PHYS_ADDR) { + COPY_U64(); /* phys_addr */ + /* TODO: can this be less conservative? */ + pr_debug("Dropping physical address sample as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + if (sample_type & PERF_SAMPLE_CGROUP) + COPY_U64(); /* cgroup */ + if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) + COPY_U64(); /* data_page_size */ + if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) + COPY_U64(); /* code_page_size */ + + if (sample_type & PERF_SAMPLE_AUX) { + u64 size; + + if (CHECK_BOUNDS(1, 1)) { + ret =3D -EFAULT; + goto out_put; + } + out_array[j] =3D in_array[i]; + size =3D out_array[j++]; + i++; + if (size > 0) { + size_t u64_words =3D size / 8 + (size % 8 ? 1 : 0); + + if (u64_words > max_i - i || u64_words > max_j - j) { + ret =3D -EFAULT; + goto out_put; + } + memcpy(&out_array[j], &in_array[i], size); + if (size % 8) { + size_t pad =3D 8 - (size % 8); + + memset(((char *)&out_array[j]) + size, 0, pad); + } + i +=3D u64_words; + j +=3D u64_words; + } + /* TODO: can this be less conservative? */ + pr_debug("Dropping aux sample as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + + if (evsel__is_offcpu_event(evsel)) { + /* TODO: can this be less conservative? */ + pr_debug("Dropping off-CPU sample as possible ASLR leak\n"); + ret =3D 0; + goto out_put; + } + + new_event->sample.header.size =3D sizeof(struct perf_event_header) + j * = sizeof(u64); + + orig_needs_swap =3D evsel->needs_swap; =20 - return delegate->sample(delegate, event, sample, machine); + evsel->needs_swap =3D false; + perf_sample__init(&new_sample, /*all=3D*/ true); + ret =3D evsel__parse_sample(evsel, new_event, &new_sample); + evsel->needs_swap =3D orig_needs_swap; + + if (ret) { + perf_sample__exit(&new_sample); + goto out_put; + } + + new_sample.evsel =3D evsel; + ret =3D delegate->sample(delegate, new_event, &new_sample, machine); + perf_sample__exit(&new_sample); + +out_put: + thread__put(thread); + return ret; } =20 +#undef CHECK_BOUNDS +#undef COPY_U64 +#undef REMAP_U64 + static int skipn(int fd, off_t n) { char buf[4096]; diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 34c03f47a913..05fa0010c858 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -3337,11 +3337,11 @@ static int __set_offcpu_sample(struct perf_sample *= data) return -EFAULT; } =20 -int evsel__parse_sample(struct evsel *evsel, union perf_event *event, - struct perf_sample *data) +int __evsel__parse_sample(struct evsel *evsel, union perf_event *event, + struct perf_sample *data, bool needs_swap) { u64 type =3D evsel->core.attr.sample_type; - bool swapped =3D evsel->needs_swap; + bool swapped =3D needs_swap; const __u64 *array; u16 max_size =3D event->header.size; const void *endp =3D (void *)event + max_size; diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 8178858d168a..8009be22cc3f 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -432,8 +432,14 @@ static inline int evsel__read_on_cpu_scaled(struct evs= el *evsel, int cpu_map_idx return __evsel__read_on_cpu(evsel, cpu_map_idx, thread, true); } =20 -int evsel__parse_sample(struct evsel *evsel, union perf_event *event, - struct perf_sample *sample); +int __evsel__parse_sample(struct evsel *evsel, union perf_event *event, + struct perf_sample *data, bool needs_swap); + +static inline int evsel__parse_sample(struct evsel *evsel, union perf_even= t *event, + struct perf_sample *data) +{ + return __evsel__parse_sample(evsel, event, data, evsel->needs_swap); +} =20 int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *e= vent, u64 *timestamp); --=20 2.54.0.1032.g2f8565e1d1-goog From nobody Mon Jun 8 05:25:47 2026 Received: from mail-dy1-f201.google.com (mail-dy1-f201.google.com [74.125.82.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB92921771B for ; Sat, 6 Jun 2026 15:15:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780758917; cv=none; b=P6MAhkLV/R4YlMAzRed9wsQ/CglQCe3t3WWnMIW1utWHoXM2zEpMN8+xe1gFb/4KXEJramsh92G1ScLPhGP4Xm+KkzErJCBEMG90dOeOLMlOankZWEDs63dugi0FeZ30NPINTdwaV3gd1OMx+mP75PX+IGcEQdYCrfE3r+GXhH4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780758917; c=relaxed/simple; bh=EKtC/lvj/1x7foH7Gmv7h68D75QB3TnoOEUOZ0Aarac=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=F83RLwyI8Sy3a4eEA5eOOixJyz2dEOxCKqu3nQCeyxDrfv/LZ5VGPQgzkif/OIggww3oNLbD8gU4HgMYmtB4eysAWrS8gCWccTXZfEQ8noUk76+B6f8wwsm4LVMbGsjIB7X9MJq6oF9MMC/Vfna8tguP9ACK8oZflpz3V3PF9KM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Wrph6h+I; arc=none smtp.client-ip=74.125.82.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Wrph6h+I" Received: by mail-dy1-f201.google.com with SMTP id 5a478bee46e88-304f1820babso3796756eec.1 for ; Sat, 06 Jun 2026 08:15:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780758912; x=1781363712; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=l+L+4ePAVYPRjEGmd/fjli1XPzR17aGwHmlHfUuUJqA=; b=Wrph6h+IDydxRT2tVuecsga8+FXrZ7WpHhHtGTIqFcuAuuhMspnxPoWE4R/JG/3D7h VJCuVCptmE78eOZdmUicnhmaEB4b8oEBwRVb6o2rbOIx6QpVmJwv0l7Oh63HZ1UPmD3Z 0l8yQl0EO6z1hYYjO4hqNO+vTeVH3yEK5YfO5bssN6FiOIFWQddscBMgl3Q3zW//6acy V3j8NAfZxSEsu9Jjr0Zjn0gjF53UxV1yXQ2OE5ieBdpYXHk4nByTyK31JvT+TqIlv1cU kQjyv88sPze2RRJjiZ6BIzE6uvhYBqwRojhu2YwrAVLNyn7HXUxMPbo20i56gQHDSHML Jytg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780758912; x=1781363712; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=l+L+4ePAVYPRjEGmd/fjli1XPzR17aGwHmlHfUuUJqA=; b=hBZD4+0KPcGs2LNMnJtcSFwxzsZQG3Ij3Mfp5dxLVea1aoL74wg9dc7DvNBUSk49ha zo4PA2sxn6NXVb4bxdRdyvRxbOMDM0X94SY2mDsf7Z2do98rUFAuCZIt683XsR/lJDoU cP7XjzfNhekyA4csSjFv+luQ5XDqlEB73XNiSdRCKU8duY719CygaYx8d/zgDFdHW2pt Y4umJkMI4wd2QZAcrK7t0bIlGp8XW3eLpWwX7KpNH/St6oVLsaedqEEYMH7AJw2ocEl9 YtkiaikKfJ7AHYW5+IuNYRLkP3HcOs/xG1JAuTB9FzLiewXk00dyarwn60fJuARPUKad Xl6Q== X-Forwarded-Encrypted: i=1; AFNElJ84sH6GLGOwQjlO8B8EO4S6i4dgL80Ug/nYSNfO9+QvLjFiRp9aoA2S8+fgyNq89eNJtW+sE1FftkgS4gc=@vger.kernel.org X-Gm-Message-State: AOJu0Yzok9GrnNT3OSfeynIQhq0J7ePLJn1CJyu9jExrksU/fKyJuHPD WWlD3DfwZacGWYu9+B0fnAlzMNzU19zIYLTlF4TUXM/2VchxviEJounf1xWc1X9+ZSYQ3mlhaiU o3j0v7kN+BQ== X-Received: from dybgn20.prod.google.com ([2002:a05:7301:2514:b0:2ed:a84e:18d3]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:80c9:b0:304:d82f:c2da with SMTP id 5a478bee46e88-3077b2cb1admr4616735eec.8.1780758911361; Sat, 06 Jun 2026 08:15:11 -0700 (PDT) Date: Sat, 6 Jun 2026 08:14:50 -0700 In-Reply-To: <20260606151451.2913860-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260606072125.2786845-1-irogers@google.com> <20260606151451.2913860-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.1032.g2f8565e1d1-goog Message-ID: <20260606151451.2913860-5-irogers@google.com> Subject: [PATCH v16 4/5] perf test: Add inject ASLR test From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, gmx@google.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a new shell test to verify the feature. The test covers: - Basic address remapping for user space samples. - Pipe mode coverage for piped into . - Callchain address remapping. - Consistency of output before and after injection. - Pipe mode report consistency. - Dropping of samples that leak ASLR info (physical addresses). - Kernel address remapping (utilizing a dedicated kernel-intensive VFS dd workload to guarantee continuous timer interrupts sampling flow inside kernel privilege states). - Kernel report consistency with address normalization. The test suite is hardened with global 'set -o pipefail' assertions to catch pipeline failures, stream-consuming awk processors to handle SIGPIPE signals, and a dedicated pipe output scenario validating raw 'perf inject -o -' stdout streams. Assisted-by: Antigravity:gemini-3.5-flash Signed-off-by: Ian Rogers --- tools/perf/tests/shell/inject_aslr.sh | 464 ++++++++++++++++++++++++++ 1 file changed, 464 insertions(+) create mode 100755 tools/perf/tests/shell/inject_aslr.sh diff --git a/tools/perf/tests/shell/inject_aslr.sh b/tools/perf/tests/shell= /inject_aslr.sh new file mode 100755 index 000000000000..d8ded16ba905 --- /dev/null +++ b/tools/perf/tests/shell/inject_aslr.sh @@ -0,0 +1,464 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# perf inject --aslr test + +set -e +set -o pipefail + +shelldir=3D$(dirname "$0") +# shellcheck source=3Dlib/perf_has_symbol.sh +. "${shelldir}"/lib/perf_has_symbol.sh + +sym=3D"noploop" + +skip_test_missing_symbol ${sym} + +# Create global temp directory +temp_dir=3D$(mktemp -d /tmp/perf-test-aslr.XXXXXXXXXX) + +prog=3D"perf test -w noploop" +[ "$(uname -m)" =3D "s390x" ] && prog=3D"$prog 3" +err=3D0 +kprog=3D"dd if=3D/dev/zero of=3D/dev/null bs=3D1M count=3D500" + +cleanup() { + local exit_code=3D${1:-$?} + trap - EXIT TERM INT + if [ "${exit_code}" -ne 0 ] || [ "${err}" -ne 0 ]; then + echo "Test failed! Preserving temp directory: ${temp_dir}" + return + fi + # Check if temp_dir is set and looks sane before removing + if [[ "${temp_dir}" =3D~ ^/tmp/perf-test-aslr\. ]]; then + rm -rf "${temp_dir}" + fi +} + +trap_cleanup() { + local exit_code=3D$? + echo "Unexpected signal in ${FUNCNAME[1]}" + cleanup ${exit_code} + exit ${exit_code} +} +trap trap_cleanup EXIT TERM INT + +get_noploop_addr() { + local file=3D$1 + perf script -i "$file" | awk ' + BEGIN { found=3D0 } + { + for (i=3D1; i<=3DNF; i++) { + if ($i ~ /noploop\+/) { + if (!found) { + print $(i-1) + found=3D1 + } + } + } + }' +} + +test_basic_aslr() { + echo "Test basic ASLR remapping" + local data + data=3D$(mktemp "${temp_dir}/perf.data.basic.XXXXXX") + local data2 + data2=3D$(mktemp "${temp_dir}/perf.data2.basic.XXXXXX") + + perf record -e task-clock:u -o "${data}" ${prog} + perf inject -v --aslr -i "${data}" -o "${data2}" + + orig_addr=3D$(get_noploop_addr "${data}") + new_addr=3D$(get_noploop_addr "${data2}") + + echo "Basic ASLR: orig_addr=3D$orig_addr, new_addr=3D$new_addr" + + if [ -z "$orig_addr" ]; then + echo "Basic ASLR test [Failed - no noploop samples in original file]" + err=3D1 + elif [ -z "$new_addr" ]; then + echo "Basic ASLR test [Failed - could not find remapped address]" + err=3D1 + elif [ "$orig_addr" =3D "$new_addr" ]; then + echo "Basic ASLR test [Failed - addresses are not remapped]" + err=3D1 + else + echo "Basic ASLR test [Success]" + fi +} + +test_pipe_aslr() { + echo "Test pipe mode ASLR remapping" + local data + data=3D$(mktemp "${temp_dir}/perf.data.pipe.XXXXXX") + local data2 + data2=3D$(mktemp "${temp_dir}/perf.data2.pipe.XXXXXX") + + # Use tee to save the original pipe data for comparison + perf record -e task-clock:u -o - ${prog} | tee "${data}" | perf inject -= -aslr -o "${data2}" + + orig_addr=3D$(get_noploop_addr "${data}") + new_addr=3D$(get_noploop_addr "${data2}") + + echo "Pipe ASLR: orig_addr=3D$orig_addr, new_addr=3D$new_addr" + + if [ -z "$orig_addr" ]; then + echo "Pipe ASLR test [Failed - no noploop samples in original file]" + err=3D1 + elif [ -z "$new_addr" ]; then + echo "Pipe ASLR test [Failed - could not find remapped address]" + err=3D1 + elif [ "$orig_addr" =3D "$new_addr" ]; then + echo "Pipe ASLR test [Failed - addresses are not remapped]" + err=3D1 + else + echo "Pipe ASLR test [Success]" + fi +} + +test_callchain_aslr() { + echo "Test Callchain ASLR remapping" + local data + data=3D$(mktemp "${temp_dir}/perf.data.callchain.XXXXXX") + local data2 + data2=3D$(mktemp "${temp_dir}/perf.data2.callchain.XXXXXX") + + perf record -g -e task-clock:u -o "${data}" ${prog} + perf inject --aslr -i "${data}" -o "${data2}" + + orig_addr=3D$(get_noploop_addr "${data}") + new_addr=3D$(get_noploop_addr "${data2}") + + echo "Callchain ASLR: orig_addr=3D$orig_addr, new_addr=3D$new_addr" + + if [ -z "$orig_addr" ]; then + echo "Callchain ASLR test [Failed - no noploop samples in original fil= e]" + err=3D1 + elif [ -z "$new_addr" ]; then + echo "Callchain ASLR test [Failed - could not find remapped address]" + err=3D1 + elif [ "$orig_addr" =3D "$new_addr" ]; then + echo "Callchain ASLR test [Failed - addresses are not remapped]" + err=3D1 + else + # Extract callchain addresses (indented lines starting with hex addres= ses) + orig_callchain=3D$(perf script -i "${data}" | awk '/^[[:space:]]+[0-9a= -f]+/ {print $1}') + new_callchain=3D$(perf script -i "${data2}" | awk '/^[[:space:]]+[0-9a= -f]+/ {print $1}') + + if [ -z "$orig_callchain" ]; then + echo "Callchain ASLR test [Failed - no callchain samples in original= file]" + err=3D1 + elif [ -z "$new_callchain" ]; then + echo "Callchain ASLR test [Failed - callchain data was dropped]" + err=3D1 + elif [ "$orig_callchain" =3D "$new_callchain" ]; then + echo "Callchain ASLR test [Failed - callchain addresses were not rem= apped]" + err=3D1 + else + echo "Callchain ASLR test [Success]" + fi + fi +} + +test_report_aslr() { + echo "Test perf report consistency" + local data + data=3D$(mktemp "${temp_dir}/perf.data.report.XXXXXX") + local data2 + data2=3D$(mktemp "${temp_dir}/perf.data2.report.XXXXXX") + local data_clean + data_clean=3D$(mktemp "${temp_dir}/perf.data.clean.XXXXXX") + + perf record -e task-clock:u -o "${data}" ${prog} + # Use -b to inject build-ids and force ordered events processing in both + perf inject -b -i "${data}" -o "${data_clean}" + perf inject -v -b --aslr -i "${data}" -o "${data2}" + + local report1=3D"${temp_dir}/report1" + local report2=3D"${temp_dir}/report2" + local report1_clean=3D"${temp_dir}/report1.clean" + local report2_clean=3D"${temp_dir}/report2.clean" + local diff_file=3D"${temp_dir}/diff" + + perf report -i "${data_clean}" --stdio > "${report1}" + perf report -i "${data2}" --stdio > "${report2}" + + # Strip headers and compare lines with percentages + grep '%' "${report1}" | grep -v '^#' | sort > "${report1_clean}" || true + grep '%' "${report2}" | grep -v '^#' | sort > "${report2_clean}" || true + + diff -u -w "${report1_clean}" "${report2_clean}" > "${diff_file}" || true + + if [ ! -s "${report1_clean}" ]; then + echo "Report ASLR test [Failed - no samples captured]" + err=3D1 + elif [ -s "${diff_file}" ]; then + echo "Report ASLR test [Failed - reports differ]" + echo "Showing first 20 lines of diff:" + head -n 20 "${diff_file}" + err=3D1 + else + echo "Report ASLR test [Success]" + fi +} + +test_pipe_report_aslr() { + echo "Test pipe mode perf report consistency" + local data + data=3D$(mktemp "${temp_dir}/perf.data.pipe_report.XXXXXX") + local data2 + data2=3D$(mktemp "${temp_dir}/perf.data2.pipe_report.XXXXXX") + local data_clean + data_clean=3D$(mktemp "${temp_dir}/perf.data.clean.XXXXXX") + + # Use tee to save the original pipe data, then process it with inject -b + perf record -e task-clock:u -o - ${prog} | \ + tee "${data}" | \ + perf inject -b --aslr -o "${data2}" + perf inject -b -i "${data}" -o "${data_clean}" + + local report1=3D"${temp_dir}/report1" + local report2=3D"${temp_dir}/report2" + local report1_clean=3D"${temp_dir}/report1.clean" + local report2_clean=3D"${temp_dir}/report2.clean" + local diff_file=3D"${temp_dir}/diff" + + perf report -i "${data_clean}" --stdio > "${report1}" + perf report -i "${data2}" --stdio > "${report2}" + + # Strip headers and compare lines with percentages + grep '%' "${report1}" | grep -v '^#' | sort > "${report1_clean}" || true + grep '%' "${report2}" | grep -v '^#' | sort > "${report2_clean}" || true + + diff -u -w "${report1_clean}" "${report2_clean}" > "${diff_file}" || true + + if [ ! -s "${report1_clean}" ]; then + echo "Pipe Report ASLR test [Failed - no samples captured]" + err=3D1 + elif [ -s "${diff_file}" ]; then + echo "Pipe Report ASLR test [Failed - reports differ]" + echo "Showing first 20 lines of diff:" + head -n 20 "${diff_file}" + err=3D1 + else + echo "Pipe Report ASLR test [Success]" + fi +} + +test_pipe_out_report_aslr() { + echo "Test pipe output mode perf report consistency" + local data + data=3D$(mktemp "${temp_dir}/perf.data.pipe_out_report.XXXXXX") + local data_clean + data_clean=3D$(mktemp "${temp_dir}/perf.data.clean.XXXXXX") + + perf record -e task-clock:u -o "${data}" ${prog} + perf inject -b -i "${data}" -o "${data_clean}" + + local report1=3D"${temp_dir}/report1" + local report2=3D"${temp_dir}/report2" + local report1_clean=3D"${temp_dir}/report1.clean" + local report2_clean=3D"${temp_dir}/report2.clean" + local diff_file=3D"${temp_dir}/diff" + + perf report -i "${data_clean}" --stdio > "${report1}" + perf inject -b --aslr -i "${data}" -o - | perf report -i - --stdio > "${= report2}" + + # Strip headers and compare lines with percentages + grep '%' "${report1}" | grep -v '^#' | sort > "${report1_clean}" || true + grep '%' "${report2}" | grep -v '^#' | sort > "${report2_clean}" || true + + diff -u -w "${report1_clean}" "${report2_clean}" > "${diff_file}" || true + + if [ ! -s "${report1_clean}" ]; then + echo "Pipe Output Report ASLR test [Failed - no samples captured]" + err=3D1 + elif [ -s "${diff_file}" ]; then + echo "Pipe Output Report ASLR test [Failed - reports differ]" + echo "Showing first 20 lines of diff:" + head -n 20 "${diff_file}" + err=3D1 + else + echo "Pipe Output Report ASLR test [Success]" + fi +} + +test_dropped_samples() { + echo "Test dropped samples (phys-data)" + local data + data=3D$(mktemp "${temp_dir}/perf.data.dropped.XXXXXX") + local data2 + data2=3D$(mktemp "${temp_dir}/perf.data2.dropped.XXXXXX") + + # Check if --phys-data is supported by recording a short run + if ! perf record -e task-clock:u --phys-data -o "${data}" -- sleep 0.1 >= /dev/null 2>&1; then + echo "Skipping dropped samples test as --phys-data is not supported" + return + fi + + perf record -e task-clock:u --phys-data -o "${data}" ${prog} + perf inject --aslr -i "${data}" -o "${data2}" + + # Verify that the original file actually contained samples! + orig_samples=3D$(perf script -i "${data}" | wc -l) + if [ "$orig_samples" -eq 0 ]; then + echo "Dropped samples test [Failed - no samples in original file]" + err=3D1 + else + # Verify that samples are dropped. + samples_count=3D$(perf script -i "${data2}" | wc -l) + + if [ "$samples_count" -gt 0 ]; then + echo "Dropped samples test [Failed - samples were not dropped]" + err=3D1 + else + echo "Dropped samples test [Success]" + fi + fi +} + +test_kernel_aslr() { + echo "Test kernel ASLR remapping" + local kdata + kdata=3D$(mktemp "${temp_dir}/perf.data.kernel.XXXXXX") + local kdata2 + kdata2=3D$(mktemp "${temp_dir}/perf.data2.kernel.XXXXXX") + local log_file + log_file=3D$(mktemp "${temp_dir}/kernel_record.log.XXXXXX") + + # Try to record kernel samples + if ! perf record -e task-clock:k -o "${kdata}" ${kprog} > "${log_file}" = 2>&1; then + echo "Skipping kernel ASLR test as recording failed (maybe no permissi= ons)" + return + fi + + # Check for warning about kernel map restriction + if grep -q "Couldn't record kernel reference relocation symbol" "${log_f= ile}"; then + echo "Skipping kernel ASLR test as kernel map could not be recorded (p= ermissions restricted)" + return + fi + + perf inject -v --aslr -i "${kdata}" -o "${kdata2}" + + # Check if kernel addresses are remapped. + # Find the field that ends with :k: (the event name) and take the next f= ield! + orig_addr=3D$(perf script -i "${kdata}" | awk ' + BEGIN { found=3D0 } + { + for (i=3D1; i "${log_file}" = 2>&1; then + echo "Skipping kernel report test as recording failed (maybe no permis= sions)" + return + fi + + # Check for warning about kernel map restriction + if grep -q "Couldn't record kernel reference relocation symbol" "${log_f= ile}"; then + echo "Skipping kernel report test as kernel map could not be recorded = (permissions restricted)" + return + fi + + # Use -b to inject build-ids and force ordered events processing in both + perf inject -b -i "${kdata}" -o "${data_clean}" + perf inject -v -b --aslr -i "${kdata}" -o "${kdata2}" + + local report1=3D"${temp_dir}/report_kernel1" + local report2=3D"${temp_dir}/report_kernel2" + local report1_clean=3D"${temp_dir}/report_kernel1.clean" + local report2_clean=3D"${temp_dir}/report_kernel2.clean" + + perf report -i "${data_clean}" --stdio > "${report1}" + perf report -i "${kdata2}" --stdio > "${report2}" + + # Strip headers and compare lines with percentages + grep '%' "${report1}" | grep -v '^#' > "${report1_clean}" || true + grep '%' "${report2}" | grep -v '^#' > "${report2_clean}" || true + + # Normalize kernel DSOs and addresses in clean reports + # This allows kernel modules to be either a module or kernel.kallsyms + local report1_norm=3D"${temp_dir}/report_kernel1.norm" + local report2_norm=3D"${temp_dir}/report_kernel2.norm" + local diff_file=3D"${temp_dir}/diff_kernel" + + grep -v -E '0x[0-9a-f]{8,}|0000000000000000' "${report1_clean}" | \ + awk '{gsub(/\[[a-zA-Z0-9_.-]{2,}\](\.[a-zA-Z0-9_]+)?/, "[kernel]", $0)= ; print}' | \ + sort > "${report1_norm}" || true + grep -v -E '0x[0-9a-f]{8,}|0000000000000000' "${report2_clean}" | \ + awk '{gsub(/\[[a-zA-Z0-9_.-]{2,}\](\.[a-zA-Z0-9_]+)?/, "[kernel]", $0)= ; print}' | \ + sort > "${report2_norm}" || true + + diff -u -w "${report1_norm}" "${report2_norm}" > "${diff_file}" || true + + if [ ! -s "${report1_norm}" ]; then + echo "Kernel Report ASLR test [Failed - no samples captured]" + err=3D1 + elif [ -s "${diff_file}" ]; then + echo "Kernel Report ASLR test [Failed - reports differ]" + echo "Showing first 20 lines of diff:" + head -n 20 "${diff_file}" + err=3D1 + else + echo "Kernel Report ASLR test [Success]" + fi +} + +test_basic_aslr +test_pipe_aslr +test_callchain_aslr +test_report_aslr +test_pipe_report_aslr +test_pipe_out_report_aslr +test_dropped_samples +test_kernel_aslr +test_kernel_report_aslr + +cleanup ${err} +exit $err --=20 2.54.0.1032.g2f8565e1d1-goog From nobody Mon Jun 8 05:25:47 2026 Received: from mail-dy1-f202.google.com (mail-dy1-f202.google.com [74.125.82.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AEA6E3B4E88 for ; Sat, 6 Jun 2026 15:15:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780758921; cv=none; b=TqqqHfztQSahrF2GFRwmwor1CuF0qPMShX1M90H9BhvXDc7mryTGKMU7KNNoy4LyTsmRV6G660yLlrAKCNTBCZLcl35SWpXjrFWpkNJJW2OdLL/RypUH+RrGBN4wlhAltuw/WuciAbU13jKRKaBKgMnz6vVLVNh2St+RtM6v5E4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780758921; c=relaxed/simple; bh=ar0+Y6b81iPUzbIKAJw9mE/+GoD5g2yXfGFmgGhOhlw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EU7UVQ0mTDl9K+hyFSQzi4OvDKwBtOugfBQuoQSilypLNUpv2ZRh4EFikO4o78JuaFvf4EmQNSCe7+fJAgq6QbW8L3+6D1nGxOjm714SwEE7NC/L9E/FRIU8uO02Ghxdc/vdU4mLMsYMBPYqGY08ArBvN5EaXXEpVFhyyhRbwsk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cxz4bkU6; arc=none smtp.client-ip=74.125.82.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cxz4bkU6" Received: by mail-dy1-f202.google.com with SMTP id 5a478bee46e88-304b8d0ee63so3991994eec.0 for ; Sat, 06 Jun 2026 08:15:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780758915; x=1781363715; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=x9ZbPJrTCUK+oRIt3+iOW5CRqzUMBk3qj21PYSYSYt8=; b=cxz4bkU6sUVUIi9BJbco6wtm50X3+VEQ+BcpZUAUqaAlV+k3fCM2PeUN1+lyTSz4b4 a+DJKsAiR0HlwimTaidkTLUej4uaUVRhSxqBjY6lQGWwrYbkHMG5r//MrNZlirupL//Z LPNEzJRLKHCWQ1ZudiXRfQ+w7221Xn0XGDQZ0FLi+e9QzcHlCU3z/QRenKup4J+/qbUR HDbT7UOpWZun3f4SyIMZmfU/lUcn3iLX4ylA4qwAq7wQQEDq6pB8fPlxQRyn4JES4NOg 6v5O9balhDiSpKKak0C4poALxbUbdDW7Ah+GAP7TQSeE8T5jZ4q+vdYtOn5d9b4O4rWA F6jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780758915; x=1781363715; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=x9ZbPJrTCUK+oRIt3+iOW5CRqzUMBk3qj21PYSYSYt8=; b=hgH4rQ6wiWlPP81CD8w1RlbqxxxJaNZT6In3DeWD/2f9CU6oSuISMBbvqx247yg25e FgjUG7TEAvlAwko9PgqhOoB+YWgItZxH6sFX5FaT71KISEkrFaJ7fC+RLEMp1Rkv6GCs m6q1cCl3Dbcmhbgwq3kI5Xs1GIROLiGlJKwHF5+YUm15pPKrglE6RrCQODRY6AjxMhTH kwzZYotydWCMcEt/8mXsEA1063+IRyfEkSvKD8xFIKayvoVw/K+/3Fih+NM7pZjYeKAP JNGGyCj3MEoUFujpXvZ5yTZXWHvAT/JjuGRqOYidkUWAEy2FiBRldcU9f1TwVgwV3hvg BlMA== X-Forwarded-Encrypted: i=1; AFNElJ/zR7MBx1hf+r2GB8RzSUp+KHHa81XFA0hMEHlNGO/VrK4cKH/atOoo/deflEqNxx2DpU4fm9fmDPoSAUc=@vger.kernel.org X-Gm-Message-State: AOJu0YxsB+QuPXXdbOYuDL3nIwyvomDbQ4osVkfzzvWYA3Kn0Om07oMR O5wth7LQostfqt/sjtq1tCTkYJuuEtXmLx9n0tFdxuwIpqHDh5D0UAGFSe3Mgd+Odahcggnvgtc dh1BgZxeySA== X-Received: from dybrh10.prod.google.com ([2002:a05:7300:f00a:b0:304:561f:1afb]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:4307:b0:2ee:be86:7b90 with SMTP id 5a478bee46e88-3077b30e918mr4757790eec.29.1780758914264; Sat, 06 Jun 2026 08:15:14 -0700 (PDT) Date: Sat, 6 Jun 2026 08:14:51 -0700 In-Reply-To: <20260606151451.2913860-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260606072125.2786845-1-irogers@google.com> <20260606151451.2913860-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.1032.g2f8565e1d1-goog Message-ID: <20260606151451.2913860-6-irogers@google.com> Subject: [PATCH v16 5/5] perf aslr: Strip sample registers From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, gmx@google.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor the ASLR tool to strip out only the register dump payload by masking out the relevant perf_event_attr fields when the delegated tool is handling the data. struct aslr_evsel_priv maintains the original perf_event_attr values and is looked up via the evsel_orig_attrs hashmap. This allows us to keep samples that would otherwise be dropped because they contain registers, while still obfuscating the registers. Co-developed-by: Gabriel Marin Signed-off-by: Gabriel Marin Signed-off-by: Ian Rogers --- tools/perf/builtin-inject.c | 46 ++++-- tools/perf/tests/shell/inject_aslr.sh | 55 +++++++ tools/perf/util/aslr.c | 224 +++++++++++++++++++++----- tools/perf/util/aslr.h | 4 + 4 files changed, 277 insertions(+), 52 deletions(-) diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index 00a54d1c7e41..c852ade3c4e3 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -254,6 +254,12 @@ static int perf_event__repipe_attr(const struct perf_t= ool *tool, return -ENOMEM; memcpy(stripped_event, event, event->header.size); stripped_event->attr.attr.sample_type &=3D ASLR_SUPPORTED_SAMPLE_TYPE; + if (stripped_event->attr.attr.size >=3D + (offsetof(struct perf_event_attr, sample_regs_user) + sizeof(u64))) + stripped_event->attr.attr.sample_regs_user =3D 0; + if (stripped_event->attr.attr.size >=3D + (offsetof(struct perf_event_attr, sample_regs_intr) + sizeof(u64))) + stripped_event->attr.attr.sample_regs_intr =3D 0; =20 if (stripped_event->attr.attr.type =3D=3D PERF_TYPE_BREAKPOINT && event->header.size >=3D (offsetof(struct perf_record_header_attr, @@ -316,7 +322,9 @@ static int perf_event__repipe_attr(const struct perf_to= ol *tool, attr.sample_type &=3D ~PERF_SAMPLE_AUX; if (inject->aslr) { attr.sample_type &=3D ASLR_SUPPORTED_SAMPLE_TYPE; - if (attr.type >=3D PERF_TYPE_MAX) { + if (attr.type =3D=3D PERF_TYPE_BREAKPOINT) { + attr.bp_addr =3D 0; + } else if (attr.type >=3D PERF_TYPE_MAX) { struct perf_pmu *pmu =3D perf_pmus__find_by_type(attr.type); =20 if (pmu && (!strcmp(pmu->name, "kprobe") || !strcmp(pmu->name, "uprobe"= ))) { @@ -324,6 +332,8 @@ static int perf_event__repipe_attr(const struct perf_to= ol *tool, attr.config2 =3D 0; } } + attr.sample_regs_user =3D 0; + attr.sample_regs_intr =3D 0; } =20 if (inject->itrace_synth_opts.add_last_branch) { @@ -2643,6 +2653,10 @@ static int __cmd_inject(struct perf_inject *inject) evsel->core.attr.exclude_callchain_user =3D 0; } } + + if (inject->aslr) + aslr_tool__strip_evlist(inject->session->tool, session->evlist); + session->header.data_offset =3D output_data_offset; session->header.data_size =3D inject->bytes_written; perf_session__inject_header(session, session->evlist, fd, &inj_fc.fc, @@ -2901,6 +2915,18 @@ int cmd_inject(int argc, const char **argv) if (zstd_init(&(inject.session->zstd_data), 0) < 0) pr_warning("Decompression initialization failed.\n"); =20 + if (inject.aslr) { + struct evsel *evsel; + + evlist__for_each_entry(inject.session->evlist, evsel) { + ret =3D aslr_tool__cache_orig_attrs(tool, evsel); + if (ret) { + pr_err("Failed to cache original attributes: %d\n", ret); + goto out_delete; + } + } + } + /* Save original section info before feature bits change */ ret =3D save_section_info(&inject); if (ret) @@ -2919,10 +2945,17 @@ int cmd_inject(int argc, const char **argv) * the input. */ if (!data.is_pipe) { + if (inject.aslr) + aslr_tool__strip_evlist(tool, inject.session->evlist); + ret =3D perf_event__synthesize_for_pipe(&inject.tool, inject.session, &inject.output, perf_event__repipe); + + if (inject.aslr) + aslr_tool__restore_evlist(tool, inject.session->evlist); + if (ret < 0) goto out_delete; } @@ -2988,17 +3021,6 @@ int cmd_inject(int argc, const char **argv) =20 ret =3D __cmd_inject(&inject); =20 - if (inject.aslr) { - struct evsel *evsel; - - evlist__for_each_entry(inject.session->evlist, evsel) { - evsel->core.attr.sample_type &=3D ASLR_SUPPORTED_SAMPLE_TYPE; - - if (evsel->core.attr.type =3D=3D PERF_TYPE_BREAKPOINT) - evsel->core.attr.bp_addr =3D 0; - } - } - guest_session__exit(&inject.guest_session); =20 out_delete: diff --git a/tools/perf/tests/shell/inject_aslr.sh b/tools/perf/tests/shell= /inject_aslr.sh index d8ded16ba905..21d306a0ff2f 100755 --- a/tools/perf/tests/shell/inject_aslr.sh +++ b/tools/perf/tests/shell/inject_aslr.sh @@ -450,6 +450,60 @@ test_kernel_report_aslr() { fi } =20 +test_regs_stripping() { + echo "Test user register stripping" + local rdata=3D"${temp_dir}/perf.data.regs" + local rdata2=3D"${temp_dir}/perf.data.regs.injected" + local rdata_clean=3D"${temp_dir}/perf.data.regs.clean" + + if ! perf record --user-regs -o "${rdata}" ${prog} > /dev/null 2>&1; then + echo "Skipping user registers test as recording failed (unsupported fl= ag/platform)" + return + fi + + perf inject -b -i "${rdata}" -o "${rdata_clean}" + perf inject -v -b --aslr -i "${rdata}" -o "${rdata2}" + + local report1=3D"${temp_dir}/report_regs1" + local report2=3D"${temp_dir}/report_regs2" + local report1_clean=3D"${temp_dir}/report_regs1.clean" + local report2_clean=3D"${temp_dir}/report_regs2.clean" + local diff_file=3D"${temp_dir}/diff_regs" + + perf report -i "${rdata_clean}" --stdio > "${report1}" 2>/dev/null || tr= ue + perf report -i "${rdata2}" --stdio > "${report2}" 2>/dev/null || true + + grep '%' "${report1}" | grep -v '^#' | \ + grep -v -E '0x[0-9a-f]{8,}|0000000000000000' | \ + sort > "${report1_clean}" || true + grep '%' "${report2}" | grep -v '^#' | \ + grep -v -E '0x[0-9a-f]{8,}|0000000000000000' | \ + sort > "${report2_clean}" || true + + diff -u -w "${report1_clean}" "${report2_clean}" > "${diff_file}" || true + + if [ ! -s "${report1_clean}" ]; then + echo "User registers stripping test [Failed - profile trace starved/em= pty]" + err=3D1 + return + elif [ -s "${diff_file}" ]; then + echo "User registers stripping test [Failed - report parsing differs]" + echo "Showing first 20 lines of diff:" + head -n 20 "${diff_file}" + err=3D1 + return + fi + + local script_dump=3D"${temp_dir}/script_regs_dump" + perf script -D -i "${rdata2}" > "${script_dump}" 2>/dev/null || true + if grep -q "PERF_SAMPLE_REGS_USER" "${script_dump}"; then + echo "User registers stripping test [Failed - register dumps still pre= sent]" + err=3D1 + else + echo "User registers stripping test [Success]" + fi +} + test_basic_aslr test_pipe_aslr test_callchain_aslr @@ -459,6 +513,7 @@ test_pipe_out_report_aslr test_dropped_samples test_kernel_aslr test_kernel_report_aslr +test_regs_stripping =20 cleanup ${err} exit $err diff --git a/tools/perf/util/aslr.c b/tools/perf/util/aslr.c index 7afa5a0dac2f..bac41bff1b2a 100644 --- a/tools/perf/util/aslr.c +++ b/tools/perf/util/aslr.c @@ -5,6 +5,7 @@ #include "debug.h" #include "event.h" #include "evsel.h" +#include "evlist.h" #include "machine.h" #include "map.h" #include "thread.h" @@ -12,10 +13,11 @@ #include "session.h" #include "data.h" #include "dso.h" - +#include "pmus.h" #include /* page_size */ #include #include +#include #include #include =20 @@ -43,6 +45,23 @@ struct aslr_mapping { u64 remap_start; }; =20 +struct aslr_evsel_priv { + u64 orig_sample_type; + u64 orig_sample_regs_user; + u64 orig_sample_regs_intr; + int orig_sample_size; +}; + +static size_t evsel_hash(long key, void *ctx __maybe_unused) +{ + return (size_t)key; +} + +static bool evsel_equal(long key1, long key2, void *ctx __maybe_unused) +{ + return key1 =3D=3D key2; +} + struct process_top_address { u64 remapped_max; }; @@ -57,6 +76,11 @@ struct aslr_tool { struct hashmap remap_addresses; /** @top_addresses: mapping from process to max remapped address. */ struct hashmap top_addresses; + /** + * @evsel_orig_attrs: mapping from evsel pointer to its original + * unstripped sample_type and registers bitmasks. + */ + struct hashmap evsel_orig_attrs; }; =20 static const pid_t kernel_pid =3D -1; @@ -656,6 +680,7 @@ static int aslr_tool__process_sample(const struct perf_= tool *tool, struct aslr_tool *aslr; struct perf_tool *delegate; int ret; + int orig_sample_size; u64 sample_type; struct thread *thread; struct machine *aslr_machine; @@ -668,6 +693,10 @@ static int aslr_tool__process_sample(const struct perf= _tool *tool, u64 addr; size_t i; size_t j; + struct aslr_evsel_priv *priv =3D NULL; + u64 orig_sample_type; + u64 orig_regs_user; + u64 orig_regs_intr; bool orig_needs_swap; =20 del_tool =3D container_of(tool, struct delegate_tool, tool); @@ -678,7 +707,23 @@ static int aslr_tool__process_sample(const struct perf= _tool *tool, return delegate->sample(delegate, event, sample, machine); =20 ret =3D -EFAULT; - sample_type =3D evsel->core.attr.sample_type; + + if (hashmap__find(&aslr->evsel_orig_attrs, evsel, &priv)) { + orig_sample_type =3D priv->orig_sample_type; + orig_regs_user =3D priv->orig_sample_regs_user; + orig_regs_intr =3D priv->orig_sample_regs_intr; + } else { + orig_sample_type =3D evsel->core.attr.sample_type; + orig_regs_user =3D evsel->core.attr.sample_regs_user; + orig_regs_intr =3D evsel->core.attr.sample_regs_intr; + } + + orig_sample_size =3D evsel->sample_size; + + sample_type =3D orig_sample_type; + sample_type &=3D ~PERF_SAMPLE_REGS_USER; + sample_type &=3D ~PERF_SAMPLE_REGS_INTR; + max_i =3D (event->header.size - sizeof(struct perf_event_header)) / sizeo= f(__u64); max_j =3D (PERF_SAMPLE_MAX_SIZE - sizeof(struct perf_event_header)) / siz= eof(__u64); new_event =3D (union perf_event *)aslr->event_copy; @@ -727,25 +772,25 @@ static int aslr_tool__process_sample(const struct per= f_tool *tool, i++; \ } while (0) =20 - if (sample_type & PERF_SAMPLE_IDENTIFIER) + if (orig_sample_type & PERF_SAMPLE_IDENTIFIER) COPY_U64(); /* id */ - if (sample_type & PERF_SAMPLE_IP) + if (orig_sample_type & PERF_SAMPLE_IP) REMAP_U64(sample->ip); - if (sample_type & PERF_SAMPLE_TID) + if (orig_sample_type & PERF_SAMPLE_TID) COPY_U64(); /* pid, tid */ - if (sample_type & PERF_SAMPLE_TIME) + if (orig_sample_type & PERF_SAMPLE_TIME) COPY_U64(); /* time */ - if (sample_type & PERF_SAMPLE_ADDR) + if (orig_sample_type & PERF_SAMPLE_ADDR) REMAP_U64(sample->addr); - if (sample_type & PERF_SAMPLE_ID) + if (orig_sample_type & PERF_SAMPLE_ID) COPY_U64(); /* id */ - if (sample_type & PERF_SAMPLE_STREAM_ID) + if (orig_sample_type & PERF_SAMPLE_STREAM_ID) COPY_U64(); /* stream_id */ - if (sample_type & PERF_SAMPLE_CPU) + if (orig_sample_type & PERF_SAMPLE_CPU) COPY_U64(); /* cpu, res */ - if (sample_type & PERF_SAMPLE_PERIOD) + if (orig_sample_type & PERF_SAMPLE_PERIOD) COPY_U64(); /* period */ - if (sample_type & PERF_SAMPLE_READ) { + if (orig_sample_type & PERF_SAMPLE_READ) { if ((evsel->core.attr.read_format & PERF_FORMAT_GROUP) =3D=3D 0) { COPY_U64(); /* value */ if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) @@ -779,7 +824,7 @@ static int aslr_tool__process_sample(const struct perf_= tool *tool, } } } - if (sample_type & PERF_SAMPLE_CALLCHAIN) { + if (orig_sample_type & PERF_SAMPLE_CALLCHAIN) { u64 nr; =20 if (CHECK_BOUNDS(1, 1)) { @@ -845,7 +890,7 @@ static int aslr_tool__process_sample(const struct perf_= tool *tool, out_array[j++] =3D aslr_tool__remap_address(aslr, thread, cpumode, addr= ); } } - if (sample_type & PERF_SAMPLE_RAW) { + if (orig_sample_type & PERF_SAMPLE_RAW) { size_t bytes =3D sizeof(u32) + sample->raw_size; size_t u64_words =3D (bytes + 7) / 8; =20 @@ -864,7 +909,7 @@ static int aslr_tool__process_sample(const struct perf_= tool *tool, ret =3D 0; goto out_put; } - if (sample_type & PERF_SAMPLE_BRANCH_STACK) { + if (orig_sample_type & PERF_SAMPLE_BRANCH_STACK) { u64 nr; =20 if (CHECK_BOUNDS(1, 1)) { @@ -909,19 +954,25 @@ static int aslr_tool__process_sample(const struct per= f_tool *tool, goto out_put; } } - if (sample_type & PERF_SAMPLE_REGS_USER) { + if (orig_sample_type & PERF_SAMPLE_REGS_USER) { + u64 abi; + if (CHECK_BOUNDS(1, 0)) { ret =3D -EFAULT; goto out_put; } - /* abi */ - COPY_U64(); - /* TODO: can this be less conservative? */ - pr_debug("Dropping regs user sample as possible ASLR leak\n"); - ret =3D 0; - goto out_put; + abi =3D in_array[i++]; + if (abi !=3D PERF_SAMPLE_REGS_ABI_NONE) { + u64 nr =3D hweight64(orig_regs_user); + + if (nr > max_i - i) { + ret =3D -EFAULT; + goto out_put; + } + i +=3D nr; + } } - if (sample_type & PERF_SAMPLE_STACK_USER) { + if (orig_sample_type & PERF_SAMPLE_STACK_USER) { u64 size; =20 if (CHECK_BOUNDS(1, 1)) { @@ -952,39 +1003,45 @@ static int aslr_tool__process_sample(const struct pe= rf_tool *tool, ret =3D 0; goto out_put; } - if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) + if (orig_sample_type & PERF_SAMPLE_WEIGHT_TYPE) COPY_U64(); /* perf_sample_weight */ - if (sample_type & PERF_SAMPLE_DATA_SRC) + if (orig_sample_type & PERF_SAMPLE_DATA_SRC) COPY_U64(); /* data_src */ - if (sample_type & PERF_SAMPLE_TRANSACTION) + if (orig_sample_type & PERF_SAMPLE_TRANSACTION) COPY_U64(); /* transaction */ - if (sample_type & PERF_SAMPLE_REGS_INTR) { + if (orig_sample_type & PERF_SAMPLE_REGS_INTR) { + u64 abi; + if (CHECK_BOUNDS(1, 0)) { ret =3D -EFAULT; goto out_put; } - /* abi */ - COPY_U64(); - /* TODO: can this be less conservative? */ - pr_debug("Dropping interrupt register sample as possible ASLR leak\n"); - ret =3D 0; - goto out_put; + abi =3D in_array[i++]; + if (abi !=3D PERF_SAMPLE_REGS_ABI_NONE) { + u64 nr =3D hweight64(orig_regs_intr); + + if (nr > max_i - i) { + ret =3D -EFAULT; + goto out_put; + } + i +=3D nr; + } } - if (sample_type & PERF_SAMPLE_PHYS_ADDR) { + if (orig_sample_type & PERF_SAMPLE_PHYS_ADDR) { COPY_U64(); /* phys_addr */ /* TODO: can this be less conservative? */ pr_debug("Dropping physical address sample as possible ASLR leak\n"); ret =3D 0; goto out_put; } - if (sample_type & PERF_SAMPLE_CGROUP) + if (orig_sample_type & PERF_SAMPLE_CGROUP) COPY_U64(); /* cgroup */ - if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) + if (orig_sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) COPY_U64(); /* data_page_size */ - if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) + if (orig_sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) COPY_U64(); /* code_page_size */ =20 - if (sample_type & PERF_SAMPLE_AUX) { + if (orig_sample_type & PERF_SAMPLE_AUX) { u64 size; =20 if (CHECK_BOUNDS(1, 1)) { @@ -1024,15 +1081,23 @@ static int aslr_tool__process_sample(const struct p= erf_tool *tool, } =20 new_event->sample.header.size =3D sizeof(struct perf_event_header) + j * = sizeof(u64); - + /* Temporarily override evsel attributes to match the stripped new_event = format! */ + evsel->sample_size =3D __evsel__sample_size(sample_type); + evsel->core.attr.sample_type =3D sample_type; + evsel->core.attr.sample_regs_user =3D 0; + evsel->core.attr.sample_regs_intr =3D 0; orig_needs_swap =3D evsel->needs_swap; - evsel->needs_swap =3D false; perf_sample__init(&new_sample, /*all=3D*/ true); ret =3D evsel__parse_sample(evsel, new_event, &new_sample); evsel->needs_swap =3D orig_needs_swap; =20 if (ret) { + /* Restore original attributes immediately if parsing fails */ + evsel->sample_size =3D orig_sample_size; + evsel->core.attr.sample_type =3D orig_sample_type; + evsel->core.attr.sample_regs_user =3D orig_regs_user; + evsel->core.attr.sample_regs_intr =3D orig_regs_intr; perf_sample__exit(&new_sample); goto out_put; } @@ -1041,6 +1106,12 @@ static int aslr_tool__process_sample(const struct pe= rf_tool *tool, ret =3D delegate->sample(delegate, new_event, &new_sample, machine); perf_sample__exit(&new_sample); =20 + /* Restore original attributes so trace ingestion never desynchronizes! */ + evsel->sample_size =3D orig_sample_size; + evsel->core.attr.sample_type =3D orig_sample_type; + evsel->core.attr.sample_regs_user =3D orig_regs_user; + evsel->core.attr.sample_regs_intr =3D orig_regs_intr; + out_put: thread__put(thread); return ret; @@ -1107,6 +1178,9 @@ static void aslr_tool__init(struct aslr_tool *aslr, s= truct perf_tool *delegate) hashmap__init(&aslr->top_addresses, top_addresses__hash, top_addresses__equal, /*ctx=3D*/NULL); + hashmap__init(&aslr->evsel_orig_attrs, + evsel_hash, evsel_equal, + /*ctx=3D*/NULL); =20 aslr->tool.tool.sample =3D aslr_tool__process_sample; /* read - reads a counter, okay to delegate. */ @@ -1168,11 +1242,81 @@ void aslr_tool__delete(struct perf_tool *tool) zfree(&cur->pkey); zfree(&cur->pvalue); } + hashmap__for_each_entry(&aslr->evsel_orig_attrs, cur, bkt) { + zfree(&cur->pvalue); + } =20 hashmap__clear(&aslr->remap_addresses); hashmap__clear(&aslr->top_addresses); + hashmap__clear(&aslr->evsel_orig_attrs); aslr_tool__destroy_machines_priv(&aslr->machines); machines__destroy_kernel_maps(&aslr->machines); machines__exit(&aslr->machines); free(aslr); } + +int aslr_tool__cache_orig_attrs(struct perf_tool *tool, struct evsel *evse= l) +{ + struct delegate_tool *del_tool =3D container_of(tool, struct delegate_too= l, tool); + struct aslr_tool *aslr =3D container_of(del_tool, struct aslr_tool, tool); + struct aslr_evsel_priv *priv =3D zalloc(sizeof(*priv)); + int err; + + if (!priv) + return -ENOMEM; + + priv->orig_sample_type =3D evsel->core.attr.sample_type; + priv->orig_sample_regs_user =3D evsel->core.attr.sample_regs_user; + priv->orig_sample_regs_intr =3D evsel->core.attr.sample_regs_intr; + priv->orig_sample_size =3D evsel->sample_size; + + err =3D hashmap__add(&aslr->evsel_orig_attrs, evsel, priv); + if (err) { + free(priv); + return err; + } + return 0; +} + +void aslr_tool__strip_evlist(const struct perf_tool *tool __maybe_unused, = struct evlist *evlist) +{ + struct evsel *evsel; + + evlist__for_each_entry(evlist, evsel) { + evsel->core.attr.sample_type &=3D ASLR_SUPPORTED_SAMPLE_TYPE; + evsel->core.attr.sample_regs_user =3D 0; + evsel->core.attr.sample_regs_intr =3D 0; + evsel->sample_size =3D __evsel__sample_size(evsel->core.attr.sample_type= ); + evsel__calc_id_pos(evsel); + + if (evsel->core.attr.type =3D=3D PERF_TYPE_BREAKPOINT) { + evsel->core.attr.bp_addr =3D 0; + } else if (evsel->core.attr.type >=3D PERF_TYPE_MAX) { + struct perf_pmu *pmu =3D perf_pmus__find_by_type(evsel->core.attr.type); + + if (pmu && (!strcmp(pmu->name, "kprobe") || + !strcmp(pmu->name, "uprobe"))) { + evsel->core.attr.config1 =3D 0; + evsel->core.attr.config2 =3D 0; + } + } + } +} + +void aslr_tool__restore_evlist(const struct perf_tool *tool, struct evlist= *evlist) +{ + const struct delegate_tool *del_tool =3D container_of(tool, const struct = delegate_tool, tool); + const struct aslr_tool *aslr =3D container_of(del_tool, const struct aslr= _tool, tool); + struct evsel *evsel; + struct aslr_evsel_priv *priv; + + evlist__for_each_entry(evlist, evsel) { + if (hashmap__find(&aslr->evsel_orig_attrs, evsel, &priv)) { + evsel->core.attr.sample_type =3D priv->orig_sample_type; + evsel->core.attr.sample_regs_user =3D priv->orig_sample_regs_user; + evsel->core.attr.sample_regs_intr =3D priv->orig_sample_regs_intr; + evsel->sample_size =3D priv->orig_sample_size; + evsel__calc_id_pos(evsel); + } + } +} diff --git a/tools/perf/util/aslr.h b/tools/perf/util/aslr.h index a9b90bf29540..4c2cffc0e500 100644 --- a/tools/perf/util/aslr.h +++ b/tools/perf/util/aslr.h @@ -30,8 +30,12 @@ =20 struct perf_tool; struct evsel; +struct evlist; =20 struct perf_tool *aslr_tool__new(struct perf_tool *delegate); void aslr_tool__delete(struct perf_tool *aslr); +int aslr_tool__cache_orig_attrs(struct perf_tool *tool, struct evsel *evse= l); +void aslr_tool__strip_evlist(const struct perf_tool *tool, struct evlist *= evlist); +void aslr_tool__restore_evlist(const struct perf_tool *tool, struct evlist= *evlist); =20 #endif /* __PERF_ASLR_H */ --=20 2.54.0.1032.g2f8565e1d1-goog