all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: lux <lx@shellcodes.org>
To: emacs-orgmode@gnu.org
Subject: [PATCH] Fix ob-latex.el command injection vulnerability.
Date: Sat, 18 Feb 2023 18:08:44 +0800	[thread overview]
Message-ID: <tencent_7B48D6A8D4FCDC2DC8DF842B069B715ECE0A@qq.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1737 bytes --]

Test environment:
- Emacs 29.0.60
- Orgmode 9.6.1
- TeX Live 2020

Preconditions:

(org-babel-do-load-languages
 'org-babel-load-languages
 '((latex . t)))

The vulnerability occurs in the file ob-latex.el, in the `org-babel-
execute:latex' function, if then file's extension is .svg, using
`shell-command' function to call the `mv' shell command:

  ((string= "svg" extension)
   ...
   (let ((tmp-pdf (org-babel-latex-tex-to-pdf tex-file)))
     (let* (...
            (img-out (org-compile-file
                      tmp-pdf
                      (list org-babel-latex-pdf-svg-process)
                      extension err-msg log-buf)))
       (shell-command (format "mv %s %s" img-out out-file)))))

But the parameter `img-out' and parameter `out-file' are not escape.
So, if file name or directory name contains shell characters and will
be executed.

Example for the vul_test.org file:

  #+name: vul_test
  #+header: :file test;uname -a;.svg
  #+begin_src latex
  \LaTeX
  #+end_src

Using Emacs open it, and press 'C-c C-e l p' export to a pdf file, or
point to begin_src block and press 'C-c C-c' to execute block.

In the '*Message*' buffer, you can see the 'uname -a' command output:

  Executing Latex code block (vul_test)...
  Processing LaTeX file /tmp/babel-UCtwdU/latex-zWDsHS.tex...
  PDF file produced.

  ,** (org.inkscape.Inkscape:145910): WARNING **: 17:27:24.285: Fonts
dir '/usr/share/inkscape/fonts' does not exist and will be ignored.
  Linux lx-debian 5.10.0-21-amd64 #1 SMP Debian 5.10.162-1 (2023-01-21)
x86_64 GNU/Linux  <---- This is 'uname -a' output
  zsh:1: command not found: .svg
  Code block produced no output (took 1.1s).

This patch fixed it.

[-- Attachment #2: 0001-lisp-ob-latex.el-org-babel-execute-latex-Fix-command.patch --]
[-- Type: text/x-patch, Size: 1021 bytes --]

From 422ffedc32c31fef39d943612d7e738cf4ad5e23 Mon Sep 17 00:00:00 2001
From: Xi Lu <lx@shellcodes.org>
Date: Sat, 18 Feb 2023 18:03:28 +0800
Subject: [PATCH] * lisp/ob-latex.el (org-babel-execute:latex): Fix command
 injection vulnerability.

---
 lisp/ob-latex.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/ob-latex.el b/lisp/ob-latex.el
index 428907a27..c32e7ea4c 100644
--- a/lisp/ob-latex.el
+++ b/lisp/ob-latex.el
@@ -180,7 +180,7 @@ This function is called by `org-babel-execute-src-block'."
 	                     tmp-pdf
                              (list org-babel-latex-pdf-svg-process)
                              extension err-msg log-buf)))
-              (shell-command (format "mv %s %s" img-out out-file)))))
+              (shell-command (format "mv %s %s" (shell-quote-argument img-out) (shell-quote-argument out-file))))))
          ((string-suffix-p ".tikz" out-file)
 	  (when (file-exists-p out-file) (delete-file out-file))
 	  (with-temp-file out-file
-- 
2.30.2


             reply	other threads:[~2023-02-18 10:27 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-18 10:08 lux [this message]
2023-02-18 11:15 ` [PATCH] Fix ob-latex.el command injection vulnerability Max Nikulin
2023-02-18 11:28   ` lux
2023-02-18 11:43     ` Ihor Radchenko
2023-02-19  2:31       ` lux
2023-03-06  3:17       ` lux
2023-03-07 12:35         ` Ihor Radchenko
2023-03-07 13:20           ` lux
2023-03-07 13:52             ` Ihor Radchenko
2023-03-07 15:06               ` Bastien Guerry
2023-03-07 15:10                 ` Ihor Radchenko
2023-03-07 15:31         ` Max Nikulin
2023-03-08  2:28           ` lux
2023-03-08 15:42           ` lux
2023-03-09 12:22             ` Ihor Radchenko
2023-03-09 16:29               ` Max Nikulin
2023-03-11  5:12               ` lux
2023-03-11 10:47                 ` Ihor Radchenko
2023-03-11 10:57                   ` lux
2023-03-12 11:28                     ` Ihor Radchenko
2023-05-01 10:56                       ` Max Nikulin
2023-05-01 11:18                         ` Ihor Radchenko
2023-05-02 11:02                           ` CVE-2023-28617 (was Re: [PATCH] Fix ob-latex.el command injection vulnerability.) Max Nikulin
2023-05-02 11:21                             ` Ihor Radchenko
2023-05-11 15:56                               ` Max Nikulin
2023-05-12 13:42                                 ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tencent_7B48D6A8D4FCDC2DC8DF842B069B715ECE0A@qq.com \
    --to=lx@shellcodes.org \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.