From 3ea74fdec8fb1577e16fcd986355c8f33152fe32 Mon Sep 17 00:00:00 2001 From: Peter Wang Date: Sun, 20 Jan 2013 11:49:55 +1100 Subject: [PATCH] [PATCH v3 11/20] insert: prevent writes outside Maildir hierarchy --- 59/f54e74f894065ee8bb795d934ba60829c637f5 | 110 ++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 59/f54e74f894065ee8bb795d934ba60829c637f5 diff --git a/59/f54e74f894065ee8bb795d934ba60829c637f5 b/59/f54e74f894065ee8bb795d934ba60829c637f5 new file mode 100644 index 000000000..7fa8524c8 --- /dev/null +++ b/59/f54e74f894065ee8bb795d934ba60829c637f5 @@ -0,0 +1,110 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 3CE18431FDB + for ; Sat, 19 Jan 2013 16:51:42 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.799 +X-Spam-Level: +X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5 + tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, + FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id qXDT7WQbqaks for ; + Sat, 19 Jan 2013 16:51:41 -0800 (PST) +Received: from mail-da0-f49.google.com (mail-da0-f49.google.com + [209.85.210.49]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id A5217429E29 + for ; Sat, 19 Jan 2013 16:51:27 -0800 (PST) +Received: by mail-da0-f49.google.com with SMTP id v40so2153966dad.22 + for ; Sat, 19 Jan 2013 16:51:26 -0800 (PST) +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; + h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to + :references; bh=1yohSKjaez2Nd4GI903tIxQiuqcgfD7ugXonbEfpM8k=; + b=IqMEcDEbPCJGC7du67MFw3aEGBKKD+Ze2WaEmXDZ4DoISkQUKNCcFOMCf8gaDJromE + O45uIjVjYkX+9HKEkkLhstvZbMMuC6lLg5lWstEVnGNzBFYcNfND/Zvcx8rQm0wgCzTu + 7wI5rSC/Y60Wp7/LfQRlyNaSzTImg1Zlwj5uT2mH9Ol2QDVoBbEoaGlEGLcQixAtVB/Y + Kfe1JleeL+/EXNZ4ZVdb6a2OKxibmgYvwWVH01u4Qe9sb+8FKQMzflQhssoHV1kYU6/l + QRC0/4xzH5PLfM/HQnmAy9DD12CdZSc4TzHQymhO3AUu3o5pY49K5rtTG+kLkcb/u7ws + YsMA== +X-Received: by 10.68.231.10 with SMTP id tc10mr18398405pbc.81.1358643086760; + Sat, 19 Jan 2013 16:51:26 -0800 (PST) +Received: from localhost (215.42.233.220.static.exetel.com.au. + [220.233.42.215]) + by mx.google.com with ESMTPS id ti9sm5807538pbc.16.2013.01.19.16.51.24 + (version=TLSv1.2 cipher=RC4-SHA bits=128/128); + Sat, 19 Jan 2013 16:51:26 -0800 (PST) +From: Peter Wang +To: notmuch@notmuchmail.org +Subject: [PATCH v3 11/20] insert: prevent writes outside Maildir hierarchy +Date: Sun, 20 Jan 2013 11:49:55 +1100 +Message-Id: <1358643004-14522-12-git-send-email-novalazy@gmail.com> +X-Mailer: git-send-email 1.7.12.1 +In-Reply-To: <1358643004-14522-1-git-send-email-novalazy@gmail.com> +References: <1358643004-14522-1-git-send-email-novalazy@gmail.com> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Sun, 20 Jan 2013 00:51:42 -0000 + +Don't accept a --folder name that contains a ".." component, +in order to prevent writing outside of the Maildir hierarchy. +--- + notmuch-insert.c | 21 +++++++++++++++++++++ + 1 file changed, 21 insertions(+) + +diff --git a/notmuch-insert.c b/notmuch-insert.c +index ba8cf5a..67ef94a 100644 +--- a/notmuch-insert.c ++++ b/notmuch-insert.c +@@ -39,6 +39,23 @@ safe_gethostname (char *hostname, size_t len) + return (strchr (hostname, '/') == NULL); + } + ++/* Check the specified folder name does not contain a directory ++ * component ".." to prevent writes outside of the Maildir hierarchy. */ ++static notmuch_bool_t ++check_folder_name (const char *folder) ++{ ++ const char *p = folder; ++ ++ for (;;) { ++ if ((p[0] == '.') && (p[1] == '.') && (p[2] == '\0' || p[2] == '/')) ++ return FALSE; ++ p = strchr (p, '/'); ++ if (!p) ++ return TRUE; ++ p++; ++ } ++} ++ + /* Open a unique file in the Maildir 'tmp' directory. + * Returns the file descriptor on success, or -1 on failure. + * On success, file paths for the message in the 'tmp' and 'new' +@@ -282,6 +299,10 @@ notmuch_insert_command (void *ctx, int argc, char *argv[]) + } + + if (folder != NULL) { ++ if (! check_folder_name (folder)) { ++ fprintf (stderr, "Error: bad folder name: %s\n", folder); ++ return 1; ++ } + maildir = talloc_asprintf (ctx, "%s/%s", db_path, folder); + } else { + maildir = talloc_asprintf (ctx, "%s", db_path); +-- +1.7.12.1 + -- 2.26.2