From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:403:4789::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id eNzuJYcM42S0WgEASxT56A (envelope-from ) for ; Mon, 21 Aug 2023 09:04:39 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:4789::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id 8B+iJYcM42QU7AAAauVa8A (envelope-from ) for ; Mon, 21 Aug 2023 09:04:39 +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 120C04387B for ; Mon, 21 Aug 2023 09:04:39 +0200 (CEST) Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=SMOvsN86; 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"; dmarc=pass (policy=none) header.from=posteo.net ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1692601479; 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=Tqjz1qhu4sz9aXJ5IEhwBH4J2Fs6FTyked9Mjma3ObY=; b=BhzW4wfM2icbGquNFieNZdE327kzZkcFOnTLV9OHWRXyN3eAbZpx4yKi7Ne0/qN9lY7LFL OZDAvMGySeGRAperUz4olNZeLi1eThJQncqUfpY+7kV9gO4DqaCh4ax9gUKZgjmdb+43g6 bE8V1bN2bNArVBW6mgTGwRqtm+5ZMINjMQQXxaaxex6ZIgQyIHJhgIASzVpeGDvKF2QtUk me245scVnjn173PV3JBvug2qMJRyE0tq+zPZ7o7zRzDU8+9i9+TLYm1lETH4v4fecZ8GET gVGPg4q4XzjmD3/K94YMVZ1bpkGUo4FAIM08mVQf2gAsY3lBAPatHhmCH6XKsA== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1692601479; a=rsa-sha256; cv=none; b=fq7oBX0696Dr2YUPzblQa9KYa/B4yI8W1HZZ/rk55wCvHdXZVJKsLrIl32GhJT11fWhjnm B6b01CpZ4PRaFQdK3EoO7vvpLI6wcu8BO/jwSgH7OAMW/L7nhfX5maH6XG+3gda0IBH3UK JIh76FV6HmL2bks9zCARLJEUKg5c3CE6sBcV9Q7P2kEZCSteQMJSAyO9POoKNEEysSjblT tntPxV9HPHOxF2qAmNj7twgxZoldZ6Gx12226PI5u+IiJLvlXS/xEwh2ct27XKUPZNGcj5 6yoeKGJPl3/J85jptfJfd6X9Gq0eh6HxSilEXtrTRiKnVL6a1jTPiKOUDKK5/Q== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=SMOvsN86; 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"; dmarc=pass (policy=none) header.from=posteo.net Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qXyxB-0007SX-S8; Mon, 21 Aug 2023 03:03:57 -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 1qXyx8-0007S9-Ay for emacs-orgmode@gnu.org; Mon, 21 Aug 2023 03:03:54 -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 1qXyx4-0006Wj-6J for emacs-orgmode@gnu.org; Mon, 21 Aug 2023 03:03:53 -0400 Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id AAACA240027 for ; Mon, 21 Aug 2023 09:03:47 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1692601427; bh=w4Fl5/9mzuWst5yin19D4llyADtQzkV/A3XVGRmLUh0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:From; b=SMOvsN86WHj2LCP+Qp3K+tImnCbnXpB9majyNd6+DRJth/Ud/Dp22PthCu4uFC1BT 8SN5/gyCoMAGRxPj+NUMZPqsTGom1CN1pQhLCuHv7+n4cjX4FIG6KPz0UkZGC6eR3p JXulnFZRQ9RihzQ8lVMRJIIoRFaNQewWzevrqchRVcmVZhQzSCDitdv8tThYSFHZ7L NLBUXuCnbfaZXfBaWcRmaz8G762CrxvpFUwg2KPgptCvsqrXQ987Oq10Jf1ImfaYXq /29NuI7R+BoXEZQvwIBALq7COoKvJWrN4h271BGBtNzRzBVjFRwqvbZfkfpLEOs+VD lZC/QxVRKi1AQ== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4RTk3Q6fylz9rxK; Mon, 21 Aug 2023 09:03:46 +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: <87zg2vl6qc.fsf@localhost> <87cyzkpwp4.fsf@localhost> <87o7j43921.fsf@localhost> Date: Mon, 21 Aug 2023 07:04:17 +0000 Message-ID: <87h6os6fm6.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: -53 X-Spam_score: -5.4 X-Spam_bar: ----- X-Spam_report: (-5.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=-1, RCVD_IN_MSPIKE_WL=-0.01, 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: -9.40 X-Spam-Score: -9.40 X-Migadu-Queue-Id: 120C04387B X-Migadu-Scanner: mx1.migadu.com X-TUID: +FwADRuqCjX8 --=-=-= Content-Type: text/plain Max Nikulin writes: >> Do you have any ideas how to work around the deliberately constructed >> header argument values like in your example? > > Perhaps `gensym' may be used to create a symbol that can not appear in a > document. I am unsure if the following `pcase' variant may be improved > ... > ;; or ob-shell-argument-literal-symbol > (defconst ob-literal-symbol (gensym "literal")) Good idea. I am attaching tentative fix that uses the proposed approach. Not yet merging - need to go through other babel backends and make them use the new API. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-org-macs-New-common-API-function-to-quote-shell-argu.patch >From dfc03c0330b96ff4fbe14df39ba895427b8fd004 Mon Sep 17 00:00:00 2001 Message-ID: From: Ihor Radchenko Date: Mon, 21 Aug 2023 09:57:50 +0300 Subject: [PATCH 1/2] org-macs: New common API function to quote shell arguments * lisp/org-macs.el (org-shell-arg-literal): New auxiliary constant. (org-make-shell-command): New function that returns shell command built from individual shell arguments, escaping them to prevent malicious code execution. Link: https://orgmode.org/list/ub549k$q11$1@ciao.gmane.io --- lisp/org-macs.el | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lisp/org-macs.el b/lisp/org-macs.el index 907e8bed7..95af9e45e 100644 --- a/lisp/org-macs.el +++ b/lisp/org-macs.el @@ -1593,6 +1593,37 @@ (defun org-sxhash-safe (obj &optional counter) (puthash hash obj org-sxhash-objects) (puthash obj hash org-sxhash-hashes))))) +(defconst org-shell-arg-literal (gensym "literal") + "Symbol to be used to mark shell arguments that should not be escaped. +See `org-make-shell-command'.") +(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. LITERAL must be the value of `org-shell-arg-literal'. + +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)) + (`(,(pred (eq org-shell-arg-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. -- 2.41.0 --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0002-org-babel-execute-sqlite-Fix-shell-arg-expansion-vul.patch >From d7a8dd47aa06e715b6bb213914d43f973c6cb413 Mon Sep 17 00:00:00 2001 Message-ID: In-Reply-To: References: From: Ihor Radchenko Date: Mon, 21 Aug 2023 09:59:12 +0300 Subject: [PATCH 2/2] org-babel-execute:sqlite: Fix shell arg expansion vulnerability * lisp/ob-sqlite.el (org-babel-execute:sqlite): Use `org-make-shell-command' to escape the strings taken from Org file. This will prevent abusing shell expansion. Reported-by: Max Nikulin Link: https://orgmode.org/list/ub549k$q11$1@ciao.gmane.io --- lisp/ob-sqlite.el | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) 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 -- 2.41.0 --=-=-= 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 --=-=-=--