From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:306:2d92::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id kMVnAdSL2GT/EgEASxT56A (envelope-from ) for ; Sun, 13 Aug 2023 09:52:52 +0200 Received: from aspmx1.migadu.com ([2001:41d0:306:2d92::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id IF+JAdSL2GR/ZAAA9RJhRA (envelope-from ) for ; Sun, 13 Aug 2023 09:52:52 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 66C9434BFD for ; Sun, 13 Aug 2023 09:52:51 +0200 (CEST) Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=DsPv29O3; dmarc=pass (policy=none) header.from=posteo.net; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1691913171; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=mGhs7fcXVDtBqdN78dXlVyJkWn5xdc8GSszd46RyNUo=; b=bc/aMdBxntQChGgmMv9grsT254zLybTvDSQajzIy+SvE7iJFCWz9NuC8j5kmMoNImpSbob 88AYULCKjKJPnfiTo759kB7Z0X91QTuDIfVE320xn+hxXSm+V2OTgR/8Zz07I23rAWpQeS 51IGaWbdBu7bcpZUzsWpzNN7xPNAJLnM6b0ga9XiQZ/UmgF0KOKXKOYhA05qktc2hIAjBb 8VGg/wCccJDEmiFBavCVRwHLfNBSoLVgSb4y5RbIho9yJU5TMZhBpgsdO9LK9M5W2xpmj7 QpSB0MZbCCEoBIS3jw5v2oaB91g/ZpOZjIH0jDLabdW6VvQln0g7eDN8e56snw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=DsPv29O3; dmarc=pass (policy=none) header.from=posteo.net; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" ARC-Seal: i=1; s=key1; d=yhetil.org; t=1691913171; a=rsa-sha256; cv=none; b=X0h37chehlm1C4064JXWOwattiCRoBpWieSVoz1vJxlb8ECc9bnZmZszueQaAeYaq58p73 x6/Q3qnIVbVpPPRl6GbosGa1uNU69GMPrkUcfmwIwfs13EKUI/AsZ88DVRVRNvJ8EIEziV k7yuuyIyrnjN+wm+AZpsu4Y7XeZk180qXemE6dKlFUykTQaBGEF0dZoqBxAE74rcLBVL2Z iK6XUMlPfcrl7MjgmU84W6zDI5KUOgXp0Q2zE0xW2njHsM7rZ+feAwa3ayo7vfsja+HJEL GH5Y7Etvmq1TGkBM6FMvTqbP56h1eXC4DJx4DoknEQUd7YYHtoZNp2w//jY9Lg== Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qV5tK-0002q1-Tq; Sun, 13 Aug 2023 03:52:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qV5tH-0002pj-Ss for emacs-orgmode@gnu.org; Sun, 13 Aug 2023 03:52:00 -0400 Received: from mout01.posteo.de ([185.67.36.65]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qV5tD-00056m-CO for emacs-orgmode@gnu.org; Sun, 13 Aug 2023 03:51:57 -0400 Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 6CF3F240028 for ; Sun, 13 Aug 2023 09:51:50 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1691913110; bh=YIIXRhrY3R0+dlklxQypjrfKbdy1r7Z6ohvV08OnR38=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:From; b=DsPv29O33FbifUvT0F2iNF8ZfA+Kybh7TcTTkN2lHPa42eB1Mwgqb7jkwy66Ckztl bHjbg87xhN+N5zqiqLYnbn+3Doph6++wxB9HEnYsF6Pa/Fk2o4fhLbo2L+JO9TBvvY aYP0omLml1ZftT5KyArLK3wAQrpQVfRl5Mhu4pXhUFrQi3+xb+omexSaD6eqH8wiy6 XCr100zpxDVeFGLvM9HbV1RLUsK4Emjp7NsRpkmWKunBX9KUqrE1W+HWWi8ltjU5tZ +OcIoJe3eTpo4nxXwpkDRfHktfRsSxQ4ttkHHWZqEC/Dd0rBlFcKMb9C0zue6X9DeI I+T87KTEFyhTg== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4RNqVY3Qk9z9rxF; Sun, 13 Aug 2023 09:51:49 +0200 (CEST) From: Ihor Radchenko To: Max Nikulin Cc: emacs-orgmode@gnu.org Subject: Re: [BUG][SECURITY] ob-sqlite header args allows execution of arbitrary shell commands In-Reply-To: References: Date: Sun, 13 Aug 2023 07:52:11 +0000 Message-ID: <87zg2vl6qc.fsf@localhost> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=185.67.36.65; envelope-from=yantar92@posteo.net; helo=mout01.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Spam-Score: -7.87 X-Spam-Score: -7.87 X-Migadu-Queue-Id: 66C9434BFD X-Migadu-Scanner: mx2.migadu.com X-TUID: wxEdnbYQ8evK --=-=-= Content-Type: text/plain Max Nikulin writes: > ---- 8< ---- > #+begin_src elisp :results none > (require 'ob-sqlite) > #+end_src > > #+begin_src sqlite :db /tmp/ob.sqlite$(date >/tmp/ob-sqlite-vuln.log) > select 1 > #+end_src > ---- >8 ---- > > Executing of the sqlite code block causes creation of the > /tmp/ob-sqlite-vuln.log file. > > The cause is usage of `org-fill-template' without `shell-quote-argument'. Confirmed. This is clearly very common. What do you think about creating a new API to built shell commands and then using it across all the babel backends? See the attached tentative diff. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=tentative-shell-escape-api.diff diff --git a/lisp/ob-sqlite.el b/lisp/ob-sqlite.el index 7510e5158..27e495fce 100644 --- a/lisp/ob-sqlite.el +++ b/lisp/ob-sqlite.el @@ -77,26 +77,20 @@ (defun org-babel-execute:sqlite (body params) (with-temp-buffer (insert (org-babel-eval - (org-fill-template - "%cmd %header %separator %nullvalue %others %csv %db " - (list - (cons "cmd" org-babel-sqlite3-command) - (cons "header" (if headers-p "-header" "-noheader")) - (cons "separator" - (if separator (format "-separator %s" separator) "")) - (cons "nullvalue" - (if nullvalue (format "-nullvalue %s" nullvalue) "")) - (cons "others" - (mapconcat - (lambda (arg) (format "-%s" (substring (symbol-name arg) 1))) - others " ")) - ;; for easy table parsing, default header type should be -csv - (cons "csv" (if (or (member :csv others) (member :column others) - (member :line others) (member :list others) - (member :html others) separator) - "" - "-csv")) - (cons "db" (or db "")))) + (org-make-shell-command + org-babel-sqlite3-command + (if headers-p "-header" "-noheader") + (when separator (format "-separator %s" separator)) + (when nullvalue (format "-nullvalue %s" nullvalue)) + (mapcar + (lambda (arg) (format "-%s" (substring (symbol-name arg) 1))) + others) + ;; for easy table parsing, default header type should be -csv + (unless (or (member :csv others) (member :column others) + (member :line others) (member :list others) + (member :html others) separator) + "-csv") + db) ;; body of the code block (org-babel-expand-body:sqlite body params))) (org-babel-result-cond result-params diff --git a/lisp/org-macs.el b/lisp/org-macs.el index 442c607d7..3c92c9405 100644 --- a/lisp/org-macs.el +++ b/lisp/org-macs.el @@ -1592,6 +1592,33 @@ (defun org-sxhash-safe (obj &optional counter) (puthash hash obj org-sxhash-objects) (puthash obj hash org-sxhash-hashes))))) +(defun org-make-shell-command (command &rest args) + "Build safe shell command string to run COMMAND with ARGS. + +The resulting shell command is safe against malicious shell expansion. + +ARGS can be nil, strings, (literal STRING), or a list of such elements. +Strings will be quoted with `shell-quote-argument' while +(literal STRING) will be used without quoting. +nil values will be ignored." + (concat + command (when command " ") + (mapconcat + #'identity + (delq + nil + (mapcar + (lambda (str-def) + (pcase str-def + (`(or nil "") nil) + ((pred stringp) (shell-quote-argument str-def)) + (`(literal ,(and (pred stringp) str)) + str) + ((pred listp) (apply #'org-make-shell-command nil str-def)) + (_ (error "Unknown ARG specification: %S" str-def)))) + args)) + " "))) + (defun org-compile-file (source process ext &optional err-msg log-buf spec) "Compile a SOURCE file using PROCESS. --=-=-= Content-Type: text/plain -- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at . Support Org development at , or support my work at --=-=-=--