Received: from eggs.gnu.org ([2001:4830:134:3::10]:54863) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bO5v1-0002rj-NL for qemu-devel@nongnu.org; Fri, 15 Jul 2016 12:24:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bO5uw-0002yO-Gy for qemu-devel@nongnu.org; Fri, 15 Jul 2016 12:24:50 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:47597) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bO5uw-0002x9-8t for qemu-devel@nongnu.org; Fri, 15 Jul 2016 12:24:46 -0400 Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.11/8.16.0.11) with SMTP id u6FGOQkv002770 for ; Fri, 15 Jul 2016 12:24:44 -0400 Received: from e06smtp11.uk.ibm.com (e06smtp11.uk.ibm.com [195.75.94.107]) by mx0a-001b2d01.pphosted.com with ESMTP id 246hek8ug2-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Fri, 15 Jul 2016 12:24:44 -0400 Received: from localhost by e06smtp11.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 15 Jul 2016 17:24:42 +0100 Received: from b06cxnps3074.portsmouth.uk.ibm.com (d06relay09.portsmouth.uk.ibm.com [9.149.109.194]) by d06dlp03.portsmouth.uk.ibm.com (Postfix) with ESMTP id 4EF961B08061 for ; Fri, 15 Jul 2016 17:26:01 +0100 (BST) Received: from d06av06.portsmouth.uk.ibm.com (d06av06.portsmouth.uk.ibm.com [9.149.37.217]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u6FGOdg73342642 for ; Fri, 15 Jul 2016 16:24:39 GMT Received: from d06av06.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av06.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u6FGOco2019920 for ; Fri, 15 Jul 2016 12:24:39 -0400 From: Sascha Silbe Date: Fri, 15 Jul 2016 18:24:22 +0200 Message-Id: <1468599862-18973-1-git-send-email-silbe@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH] test-logging: don't hard-code paths in /tmp List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org, Paolo Bonzini Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , Markus Armbruster Since f6880b7f [qemu-log: support simple pid substitution for logs], test-logging creates files with hard-coded names in /tmp. In the best case, this prevents multiple developers from running "make check" on the same machine. In the worst case, it allows for symlink attacks, enabling an attacker to overwrite files that are writable to the developer running "make check". Instead of hard-coding the paths, create a temporary directory using g_dir_make_tmp() and clean it up afterwards. Fixes: f6880b7f ("qemu-log: support simple pid substitution for logs") Signed-off-by: Sascha Silbe --- tests/test-logging.c | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/tests/test-logging.c b/tests/test-logging.c index cdf13c6..faebc35 100644 --- a/tests/test-logging.c +++ b/tests/test-logging.c @@ -86,24 +86,52 @@ static void test_parse_range(void) error_free_or_abort(&err); } -static void test_parse_path(void) +static void test_parse_path(gconstpointer data) { + gchar const *tmp_path = data; + gchar *plain_path = g_build_filename(tmp_path, "qemu.log", NULL); + gchar *pid_infix_path = g_build_filename(tmp_path, "qemu-%d.log", NULL); + gchar *pid_suffix_path = g_build_filename(tmp_path, "qemu.log.%d", NULL); + gchar *double_pid_path = g_build_filename(tmp_path, "qemu-%d%d.log", NULL); Error *err = NULL; - qemu_set_log_filename("/tmp/qemu.log", &error_abort); - qemu_set_log_filename("/tmp/qemu-%d.log", &error_abort); - qemu_set_log_filename("/tmp/qemu.log.%d", &error_abort); + qemu_set_log_filename(plain_path, &error_abort); + qemu_set_log_filename(pid_infix_path, &error_abort); + qemu_set_log_filename(pid_suffix_path, &error_abort); - qemu_set_log_filename("/tmp/qemu-%d%d.log", &err); + qemu_set_log_filename(double_pid_path, &err); error_free_or_abort(&err); + + g_free(double_pid_path); + g_free(pid_suffix_path); + g_free(pid_infix_path); + g_free(plain_path); +} + +static void rmtree(gchar const *root) +{ + /* There should really be a g_rmtree(). Implementing it ourselves + * isn't really worth it just for a test, so let's just use rm. */ + gchar const *rm_args[] = { "rm", "-rf", root, NULL }; + g_spawn_sync(NULL, (gchar **)rm_args, NULL, + G_SPAWN_SEARCH_PATH, NULL, NULL, + NULL, NULL, NULL, NULL); } int main(int argc, char **argv) { + gchar *tmp_path = g_dir_make_tmp("qemu-test-logging.XXXXXX", NULL); + int rc; + g_test_init(&argc, &argv, NULL); + g_assert_nonnull(tmp_path); g_test_add_func("/logging/parse_range", test_parse_range); - g_test_add_func("/logging/parse_path", test_parse_path); + g_test_add_data_func("/logging/parse_path", tmp_path, test_parse_path); - return g_test_run(); + rc = g_test_run(); + + rmtree(tmp_path); + g_free(tmp_path); + return rc; } -- 1.9.1