Subject: bug#77341: [PATCH] ; (find-function-search-for-symbol): Be cautious with macros.

From: Eshel Yaron <[email-masked]>
Date: Fri, 28 Mar 2025 18:28:06 +0100

Tags: patch

Hi,

find-function may expand Lisp macros in a source file when it fails to
find a definition otherwise.  This patch restricts this fallback to
trusted buffers only, to protect against possibly harmful macros.

Eshel

>From a29b2c0888a19069aef8ef248979c4b463e26f5c Mon Sep 17 00:00:00 2001
From: Eshel Yaron <[email-masked]>
Date: Fri, 28 Mar 2025 17:57:35 +0100
Subject: [PATCH] ; (find-function-search-for-symbol): Be cautious with macros.

* lisp/emacs-lisp/find-func.el
(find-function-search-for-symbol): Only attempt to expand
macros in trusted buffers.
---
 lisp/emacs-lisp/find-func.el | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/lisp/emacs-lisp/find-func.el b/lisp/emacs-lisp/find-func.el
index 8f488a9c00a..6bdfb4bc38b 100644
--- a/lisp/emacs-lisp/find-func.el
+++ b/lisp/emacs-lisp/find-func.el
@@ -487,11 +487,14 @@ find-function-search-for-symbol
                 ;; If the regexp search didn't find the location of
                 ;; the symbol (for example, because it is generated by
                 ;; a macro), try a slightly more expensive search that
-                ;; expands macros until it finds the symbol.
+                ;; expands macros until it finds the symbol.  Since
+                ;; macro-expansion involves arbitrary code execution,
+                ;; only attempt it in trusted buffers.
                 (cons (current-buffer)
-                      (find-function--search-by-expanding-macros
-                       (current-buffer) symbol type
-                       form-matcher-factory))))))))))
+                      (when (trusted-content-p)
+                        (find-function--search-by-expanding-macros
+                         (current-buffer) symbol type
+                         form-matcher-factory)))))))))))
 
 ;;;###autoload
 (defun find-function-update-type-alist (symbol type variable)
-- 
2.48.1