Received: from eggs.gnu.org ([2001:4830:134:3::10]:60128) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1baSKh-0004ig-Pd for qemu-devel@nongnu.org; Thu, 18 Aug 2016 14:46:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1baSKZ-0002BX-PY for qemu-devel@nongnu.org; Thu, 18 Aug 2016 14:46:26 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:45529) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1baSKZ-0002BL-HQ for qemu-devel@nongnu.org; Thu, 18 Aug 2016 14:46:19 -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 u7IIiBG5048812 for ; Thu, 18 Aug 2016 14:46:19 -0400 Received: from e06smtp11.uk.ibm.com (e06smtp11.uk.ibm.com [195.75.94.107]) by mx0a-001b2d01.pphosted.com with ESMTP id 24wfj4ftut-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 18 Aug 2016 14:46:18 -0400 Received: from localhost by e06smtp11.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 18 Aug 2016 19:46:16 +0100 Received: from b06cxnps4075.portsmouth.uk.ibm.com (d06relay12.portsmouth.uk.ibm.com [9.149.109.197]) by d06dlp03.portsmouth.uk.ibm.com (Postfix) with ESMTP id 1B2311B0806E for ; Thu, 18 Aug 2016 19:47:50 +0100 (BST) Received: from d06av01.portsmouth.uk.ibm.com (d06av01.portsmouth.uk.ibm.com [9.149.37.212]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u7IIkDmO66191366 for ; Thu, 18 Aug 2016 18:46:13 GMT Received: from d06av01.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av01.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u7IIkCFX013482 for ; Thu, 18 Aug 2016 12:46:12 -0600 From: Sascha Silbe Date: Thu, 18 Aug 2016 20:46:03 +0200 In-Reply-To: <1471545963-11720-1-git-send-email-silbe@linux.vnet.ibm.com> References: <1471545963-11720-1-git-send-email-silbe@linux.vnet.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Message-Id: <1471545963-11720-3-git-send-email-silbe@linux.vnet.ibm.com> Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v2 2/2] 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 , Peter Maydell 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 --- v1=E2=86=92v2: - Factor out g_build_filename() + qemu_set_log_filename() + g_free() into helper set_log_path_tmp(). - Replace rmtree() spawning "rm -rf" with rmdir_full() using glib directory handling (non-recursive). Private to test-logging for now. tests/test-logging.c | 48 +++++++++++++++++++++++++++++++++++++++++-----= -- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/tests/test-logging.c b/tests/test-logging.c index cdf13c6..a12585f 100644 --- a/tests/test-logging.c +++ b/tests/test-logging.c @@ -25,6 +25,7 @@ */ =20 #include "qemu/osdep.h" +#include =20 #include "qemu-common.h" #include "qapi/error.h" @@ -86,24 +87,57 @@ static void test_parse_range(void) error_free_or_abort(&err); } =20 -static void test_parse_path(void) +static void set_log_path_tmp(char const *dir, char const *tpl, Error **e= rrp) { + gchar *file_path =3D g_build_filename(dir, tpl, NULL); + + qemu_set_log_filename(file_path, errp); + g_free(file_path); +} + +static void test_parse_path(gconstpointer data) +{ + gchar const *tmp_path =3D data; Error *err =3D NULL; =20 - 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); + set_log_path_tmp(tmp_path, "qemu.log", &error_abort); + set_log_path_tmp(tmp_path, "qemu-%d.log", &error_abort); + set_log_path_tmp(tmp_path, "qemu.log.%d", &error_abort); =20 - qemu_set_log_filename("/tmp/qemu-%d%d.log", &err); + set_log_path_tmp(tmp_path, "qemu-%d%d.log", &err); error_free_or_abort(&err); } =20 +/* Remove a directory and all its entries (non-recursive). */ +static void rmdir_full(gchar const *root) +{ + GDir *root_gdir =3D g_dir_open(root, 0, NULL); + gchar const *entry_name; + + g_assert_nonnull(root_gdir); + while ((entry_name =3D g_dir_read_name(root_gdir)) !=3D NULL) { + gchar *entry_path =3D g_build_filename(root, entry_name, NULL); + g_assert(g_remove(entry_path) =3D=3D 0); + g_free(entry_path); + } + g_dir_close(root_gdir); + g_assert(g_rmdir(root) =3D=3D 0); +} + int main(int argc, char **argv) { + gchar *tmp_path =3D g_dir_make_tmp("qemu-test-logging.XXXXXX", NULL)= ; + int rc; + g_test_init(&argc, &argv, NULL); + g_assert_nonnull(tmp_path); =20 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_pat= h); =20 - return g_test_run(); + rc =3D g_test_run(); + + rmdir_full(tmp_path); + g_free(tmp_path); + return rc; } --=20 1.9.1