From: Peter Wang Date: Sun, 25 Nov 2012 01:16:35 +0000 (+1100) Subject: [PATCH v2 09/20] insert: prevent writes outside Maildir hierarchy X-Git-Url: http://git.tremily.us/gitweb.cgi?a=commitdiff_plain;h=c8cc9db892ea7ed853a38471679acd85edc7b757;p=notmuch-archives.git [PATCH v2 09/20] insert: prevent writes outside Maildir hierarchy --- diff --git a/66/1f7a5dcabd890852b34ef48337f9c38454c418 b/66/1f7a5dcabd890852b34ef48337f9c38454c418 new file mode 100644 index 000000000..0855a4e04 --- /dev/null +++ b/66/1f7a5dcabd890852b34ef48337f9c38454c418 @@ -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 94D45431FAF + for ; Sat, 24 Nov 2012 17:17:56 -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 9FVrlOmsE62o for ; + Sat, 24 Nov 2012 17:17:56 -0800 (PST) +Received: from mail-da0-f53.google.com (mail-da0-f53.google.com + [209.85.210.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 247F7431FAE + for ; Sat, 24 Nov 2012 17:17:56 -0800 (PST) +Received: by mail-da0-f53.google.com with SMTP id x6so2305796dac.26 + for ; Sat, 24 Nov 2012 17:17:55 -0800 (PST) +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; + h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; + bh=xIoOTrPF204aON5fO76DkSt406Ccng0pbsk1vL/X2cw=; + b=QVmIN+7jCR/Gr35rlvVb6ymIIP8wEi3CCDEQv/rPcGbQTdw+Pcce5jMed2gMo3eb/w + iUXnK+3Mnjb9PxVxqbsJxBnJBJ9xUu21rhdIMjKZrPiyffC4Yw0bT2B8ECrdSkglWpDi + b5dGg2GnKPA+pX2J2zwREx6otNozxvg4tT/FkBmNPLWRr4o22bsfjvwQ2U8B4ZOanAEy + pBkSwe8YLHXFqxDrmc2ioVng+YgOVHi6zOkIAyuaE0OIhVRpjrSdTKNlT08NQi6VSkWV + eCsiqOm+a27wHDJ8UB12CM28BIKiPA0v9AOfPHk6gZqF5fJ6+zN4wab2UMcBAaalRcDv + eAfA== +Received: by 10.69.0.40 with SMTP id av8mr26370390pbd.117.1353806275387; + Sat, 24 Nov 2012 17:17:55 -0800 (PST) +Received: from localhost (215.42.233.220.static.exetel.com.au. + [220.233.42.215]) + by mx.google.com with ESMTPS id i10sm6169840paz.17.2012.11.24.17.17.52 + (version=TLSv1/SSLv3 cipher=OTHER); + Sat, 24 Nov 2012 17:17:54 -0800 (PST) +From: Peter Wang +To: notmuch@notmuchmail.org +Subject: [PATCH v2 09/20] insert: prevent writes outside Maildir hierarchy +Date: Sun, 25 Nov 2012 12:16:35 +1100 +Message-Id: <1353806206-29133-10-git-send-email-novalazy@gmail.com> +X-Mailer: git-send-email 1.7.12.1 +In-Reply-To: <1353806206-29133-1-git-send-email-novalazy@gmail.com> +References: <1353806206-29133-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, 25 Nov 2012 01:17:56 -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 a50eacc..022f7cd 100644 +--- a/notmuch-insert.c ++++ b/notmuch-insert.c +@@ -38,6 +38,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 into the 'tmp' and 'new' directories are returned +@@ -255,6 +272,10 @@ notmuch_insert_command (void *ctx, int argc, char *argv[]) + db_path = notmuch_config_get_database_path (config); + + 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 +