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 C6D6DC83F0A for ; Sun, 6 Jul 2025 17:37:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DA8196B03FA; Sun, 6 Jul 2025 13:37:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D58796B03FB; Sun, 6 Jul 2025 13:37:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C95C96B03FC; Sun, 6 Jul 2025 13:37:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id B58D96B03FA for ; Sun, 6 Jul 2025 13:37:40 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 863DB1601A2 for ; Sun, 6 Jul 2025 17:37:40 +0000 (UTC) X-FDA: 83634547080.27.1B50E7A Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf22.hostedemail.com (Postfix) with ESMTP id EEF0FC0004 for ; Sun, 6 Jul 2025 17:37:38 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=O+0fEEWh; spf=pass (imf22.hostedemail.com: domain of alx@kernel.org designates 172.105.4.254 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=1751823458; 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=mAapRvGyuoT/ZTzBrl0KcwPhWc3nkILi0GSv9sZNHr26deMbQMWQNpCR6zfV/XeJ9OlLnK 4PKjt729UQMKPImXHw713ntiK8I2HquUn/cg2zoV6yaUXgR6sHCEbgpusKyJCJ901PC1GD Dapz9IOg5/toKMrK2vDE1mHZFnts9Sk= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=O+0fEEWh; spf=pass (imf22.hostedemail.com: domain of alx@kernel.org designates 172.105.4.254 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=1751823459; a=rsa-sha256; cv=none; b=J8p5la+n8oc4je3do25F2uru2JB7DpzGBlotN19DHawC8f6ePwfTi+/V3WP+pl9E/mDG+q 9SJ8/qoY0T/qr0yzXpwHcspXdto7IoGbe6l5Mw/5u7GRfBaOOs8VjpmtCKg4/NtQX8b/qh 7UTZMJwYWv7MDc/NNieb++3nLMe99+I= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by tor.source.kernel.org (Postfix) with ESMTP id 80ECC61130; Sun, 6 Jul 2025 17:37:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1DB8BC4CEED; Sun, 6 Jul 2025 17:37:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751823458; bh=UnukMHnFrNRdetsFMcw3prQ/v4qlktaL9HMhPaNQEX0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=O+0fEEWh9MJ6JFKfYxoBMdO22bAvSQoipCY0TnxmtCAoa6FLL9qyOXFWbJGOj4EXo klJx55P/GGzG0UvTYhk7wyvwGE+iB1JWnE4v8f31Q0QUPccSmghsq1PZHDy4+IHCkB qY8GJgZlcUxBFWFhFL86qrDo3bzzfapdPvQCoaIKp6kIXkYcnWIpRCQ69JEY4XtQP9 3ZCl6wmRE2JxPtSI1pH4JlXEisY0sK4FHcIqgIGBp0TV/aS+YaxL67X9Nzh2Hw/Wpt jD0x+IZT/s4aq8rCGZ8e76I2nE3Fgrt728mxNrqGUkuYSu2ikDHqflY4owJ3sCl3HD 8uRGUvyKYqKTg== Date: Sun, 6 Jul 2025 19:37:36 +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 v2 2/5] stacktrace, stackdepot: Add seprintf()-like variants of functions Message-ID: <9c140de9842dd34096928a7c87dab4f45ef18764.1751823326.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: rspam03 X-Rspamd-Queue-Id: EEF0FC0004 X-Stat-Signature: dq3jn4mzi4qhaouii6oa3x5844trnmzo X-Rspam-User: X-HE-Tag: 1751823458-272588 X-HE-Meta: U2FsdGVkX1/8YT0SNRdXScBiVxblACx30/QFayD2jrfzTwO2apG0Gvksp4kin9R2SmIjhsr/Lmgh/UboGrnoYEem7uBcrpYoqo6GMu6vUxOpRP61m+MaATN+qPOpLWrfpNgeVnnTld7FlNXdjqJ452HpObfgDZ9LmdPpI8W7F2xoA2EbkehOTBAUoKaiLPGOUcTruVSZIFAaSPCHBJeQq8aip7VACO863OhLLpWpgPx1gnB3+vJ9GLSB2cerr5R4MudG9VTyqqCJAdcIP2P9iFMKcXmZGi33NEQ8Tc785r90Cg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=bogofilter, 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