From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D9C9C83030 for ; Mon, 7 Jul 2025 05:06:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C25DC8D000D; Mon, 7 Jul 2025 01:06:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B870A8D0002; Mon, 7 Jul 2025 01:06:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A27F28D000D; Mon, 7 Jul 2025 01:06:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 851148D0002 for ; Mon, 7 Jul 2025 01:06:14 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 3A40858B6A for ; Mon, 7 Jul 2025 05:06:14 +0000 (UTC) X-FDA: 83636282268.22.2D0F961 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf02.hostedemail.com (Postfix) with ESMTP id 6313B8000E for ; Mon, 7 Jul 2025 05:06:12 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=sYkHDiS7; spf=pass (imf02.hostedemail.com: domain of alx@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=alx@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1751864772; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=khuNa9UrIARpXdom/CN8TiiKzBTBTw1yWHEH++/b0Is=; b=frUbAmPwqp+PuICBO4dC+zCZ5oIPLZ0VfRcRvor/nSIHHe4D5PntfbWBztsy+04aEFU1SV lIEH6IQX9nkXEyH90+yeNfHxEQtH5oOo1M8wHay7EItRfWei0n2T2djRM5FS+oLUUxSjrQ 0QSHKRlLyGGU17KiZOlPdbMA67A4fYs= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=sYkHDiS7; spf=pass (imf02.hostedemail.com: domain of alx@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=alx@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1751864772; a=rsa-sha256; cv=none; b=0ONFWcD5s8HWLDL4QOfSanacaD8+av4U+7Z5c7DfZaKpb/Px/3ZSPqjYolFzdj2IQgn+wD ghEl2di96zcGMvda/Cjw8/UOHhp+vepKyadgzPb2PBEDbtBbG9fC7AJ03Nwmax41XDUsA4 tzouXb+8PwzvicQnX/ntYpe0syGye5s= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id 0233346165; Mon, 7 Jul 2025 05:06:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 23963C4CEF5; Mon, 7 Jul 2025 05:06:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751864770; bh=UnukMHnFrNRdetsFMcw3prQ/v4qlktaL9HMhPaNQEX0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=sYkHDiS7fXE2K8y0Y1giKvh2pbltZ7/tva+9LiT/VHcKqFKM3JQky1dhM2jGM1LcA BXF8Cw0R8HcuMxeVIdXjx7pdJfKJPI0hxP1xyqtt12nBF1nxdDD5N3CAQHaV/rPavC XOA113UlxIytk6j+oxrCBignYcCWTvYDU1038YP8IvJEjjQHZykZrdNYqNrIkNG3p0 Of/g8ZVxkD0z/vQkDnNKJzv2SRZ7bMP9J7gi7WJTjZEGImUF1wMOtl2JBikTRlSaON Og0VdMofT7QId7rpsY5mQ8Qwg0NOlPbCeroujc0pWXhQrLC4tB/idCfrfsZnS5S3zc ax8VX82Slh2bQ== Date: Mon, 7 Jul 2025 07:06:09 +0200 From: Alejandro Colomar To: linux-mm@kvack.org, linux-hardening@vger.kernel.org Cc: Alejandro Colomar , Kees Cook , Christopher Bazley , shadow <~hallyn/shadow@lists.sr.ht>, linux-kernel@vger.kernel.org, Andrew Morton , kasan-dev@googlegroups.com, Dmitry Vyukov , Alexander Potapenko , Marco Elver , Christoph Lameter , David Rientjes , Vlastimil Babka , Roman Gushchin , Harry Yoo , Andrew Clayton Subject: [RFC v3 2/7] stacktrace, stackdepot: Add seprintf()-like variants of functions Message-ID: <9c140de9842dd34096928a7c87dab4f45ef18764.1751862634.git.alx@kernel.org> X-Mailer: git-send-email 2.50.0 References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 6313B8000E X-Rspam-User: X-Stat-Signature: 17e33inheqq7agbs9eug1e8fon6xxi5m X-HE-Tag: 1751864772-923708 X-HE-Meta: U2FsdGVkX196GCMtXNgaDHNYgam8DSUuawTxZbogmzijc+yAjj8U1jdW53sqwl8vWwm+F2JC0K9OLuMIWBYMlTjDrBoaT3yD4MJD7j0oQpz7QGYQP7VKYOc0X/SIH0pos+e8twNaLZgs1y5zk8SOV7BDjWD1MXD8IYqcC1YN1to0dz7OOlsLI0PPwV7zM+T/KCtUo4/m+ZVkuD7YsnLDVeRuMcattg5iPXzP0fho0zuTz+DMBqicwbRT+nSfimvvXJKUV0CrF77C/uoBaL6DDqPJnpjHLwNzxAC5Ys7RH9ZG0uL0JbRXs8XOic6x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ofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: I think there's an anomaly in stack_depot_s*print(). If we have zero entries, we don't copy anything, which means the string is still not a string. Normally, this function is called surrounded by other calls to s*printf(), which guarantee that there's a '\0', but maybe we should make sure to write a '\0' here? Cc: Kees Cook Cc: Christopher Bazley Signed-off-by: Alejandro Colomar --- include/linux/stackdepot.h | 13 +++++++++++++ include/linux/stacktrace.h | 3 +++ kernel/stacktrace.c | 28 ++++++++++++++++++++++++++++ lib/stackdepot.c | 12 ++++++++++++ 4 files changed, 56 insertions(+) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index 2cc21ffcdaf9..a7749fc3ac7c 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -219,6 +219,19 @@ void stack_depot_print(depot_stack_handle_t stack); int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size, int spaces); +/** + * stack_depot_seprint - Print a stack trace from stack depot into a buffer + * + * @handle: Stack depot handle returned from stack_depot_save() + * @p: Pointer to the print buffer + * @end: Pointer to one past the last element in the buffer + * @spaces: Number of leading spaces to print + * + * Return: Pointer to trailing '\0'; or NULL on truncation + */ +char *stack_depot_seprint(depot_stack_handle_t handle, char *p, + const char end[0], int spaces); + /** * stack_depot_put - Drop a reference to a stack trace from stack depot * diff --git a/include/linux/stacktrace.h b/include/linux/stacktrace.h index 97455880ac41..748936386c89 100644 --- a/include/linux/stacktrace.h +++ b/include/linux/stacktrace.h @@ -67,6 +67,9 @@ void stack_trace_print(const unsigned long *trace, unsigned int nr_entries, int spaces); int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries, unsigned int nr_entries, int spaces); +char *stack_trace_seprint(char *p, const char end[0], + const unsigned long *entries, unsigned int nr_entries, + int spaces); unsigned int stack_trace_save(unsigned long *store, unsigned int size, unsigned int skipnr); unsigned int stack_trace_save_tsk(struct task_struct *task, diff --git a/kernel/stacktrace.c b/kernel/stacktrace.c index afb3c116da91..65caf9e63673 100644 --- a/kernel/stacktrace.c +++ b/kernel/stacktrace.c @@ -70,6 +70,34 @@ int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries, } EXPORT_SYMBOL_GPL(stack_trace_snprint); +/** + * stack_trace_seprint - Print the entries in the stack trace into a buffer + * @p: Pointer to the print buffer + * @end: Pointer to one past the last element in the buffer + * @entries: Pointer to storage array + * @nr_entries: Number of entries in the storage array + * @spaces: Number of leading spaces to print + * + * Return: Pointer to the trailing '\0'; or NULL on truncation. + */ +char *stack_trace_seprint(char *p, const char end[0], + const unsigned long *entries, unsigned int nr_entries, + int spaces) +{ + unsigned int i; + + if (WARN_ON(!entries)) + return 0; + + for (i = 0; i < nr_entries; i++) { + p = seprintf(p, end, "%*c%pS\n", 1 + spaces, ' ', + (void *)entries[i]); + } + + return p; +} +EXPORT_SYMBOL_GPL(stack_trace_seprint); + #ifdef CONFIG_ARCH_STACKWALK struct stacktrace_cookie { diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 73d7b50924ef..749496e6a6f1 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -771,6 +771,18 @@ int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size, } EXPORT_SYMBOL_GPL(stack_depot_snprint); +char *stack_depot_seprint(depot_stack_handle_t handle, char *p, + const char end[0], int spaces) +{ + unsigned long *entries; + unsigned int nr_entries; + + nr_entries = stack_depot_fetch(handle, &entries); + return nr_entries ? stack_trace_seprint(p, end, entries, nr_entries, + spaces) : p; +} +EXPORT_SYMBOL_GPL(stack_depot_seprint); + depot_stack_handle_t __must_check stack_depot_set_extra_bits( depot_stack_handle_t handle, unsigned int extra_bits) { -- 2.50.0