From patchwork Fri Oct 2 12:25:36 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Bolle X-Patchwork-Id: 34844 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id EDDB5B7BDB for ; Fri, 2 Oct 2009 22:26:30 +1000 (EST) Received: from localhost ([127.0.0.1]:43738 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MthDP-0007NX-7b for incoming@patchwork.ozlabs.org; Fri, 02 Oct 2009 08:26:27 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MthCj-0007Mo-W1 for qemu-devel@nongnu.org; Fri, 02 Oct 2009 08:25:46 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MthCf-0007KE-7O for qemu-devel@nongnu.org; Fri, 02 Oct 2009 08:25:45 -0400 Received: from [199.232.76.173] (port=51027 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MthCf-0007K3-2W for qemu-devel@nongnu.org; Fri, 02 Oct 2009 08:25:41 -0400 Received: from smtp-out3.tiscali.nl ([195.241.79.178]:33325) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1MthCe-0005MH-JG for qemu-devel@nongnu.org; Fri, 02 Oct 2009 08:25:40 -0400 Received: from [212.123.169.34] (helo=[192.168.1.61]) by smtp-out3.tiscali.nl with esmtp (Exim) (envelope-from ) id 1MthCd-0003Tt-7v for qemu-devel@nongnu.org; Fri, 02 Oct 2009 14:25:39 +0200 From: Paul Bolle To: qemu-devel@nongnu.org Date: Fri, 02 Oct 2009 14:25:36 +0200 Message-Id: <1254486336.1738.30.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.28.0 (2.28.0-1.fc12) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Subject: [Qemu-devel] [PATCH] linux-user: use realpath for emulation dir paths X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The paths to files inside the emulation dir as returned by path() are not neat canonicalized absolute pathnames but can (and will) contain "/./", "/../" and "//" parts. As far as I know these ugly paths will only be seen when one is (printf) debugging. I assume, however, that these paths have to be canonicalized every time they are used to open files so that might as well be done when they are created. Some minor cleanups etc. added too. Signed-off-by: Paul Bolle --- I only noticed this because I "#if 1"'d a printf() in linux-user/elfload.c (see a trivial patch I just send to the list). Note that I have some reservations about the current init_paths() and path() code: - their names seem to confusing. Maybe those should be init_base() and base() or something similar; - why does init_paths() copy all filenames in the emulation dir (at least, that what it seems to do)? Try something silly like "-L /home/../" to see what I mean ... - and why does path() return the original filename if that file isn't found in the emulation dir? That looks like a nice source for confusing behavior or crashes, as that means an identical named file (but using the regular root) will then be used. Maybe I'll elaborate in a separate mail. path.c | 21 +++++++++++++-------- 1 files changed, 13 insertions(+), 8 deletions(-) diff --git a/path.c b/path.c index cc9e007..875cb03 100644 --- a/path.c +++ b/path.c @@ -122,25 +122,30 @@ follow_path(const struct pathelem *cursor, const char *name) void init_paths(const char *prefix) { char pref_buf[PATH_MAX]; + char real_buf[PATH_MAX]; - if (prefix[0] == '\0' || - !strcmp(prefix, "/")) + if (prefix[0] == '\0') return; if (prefix[0] != '/') { char *cwd = getcwd(NULL, 0); - size_t pref_buf_len = sizeof(pref_buf); if (!cwd) abort(); - pstrcpy(pref_buf, sizeof(pref_buf), cwd); - pstrcat(pref_buf, pref_buf_len, "/"); - pstrcat(pref_buf, pref_buf_len, prefix); + pstrcpy(pref_buf, PATH_MAX, cwd); + pstrcat(pref_buf, PATH_MAX, "/"); + pstrcat(pref_buf, PATH_MAX, prefix); free(cwd); } else - pstrcpy(pref_buf, sizeof(pref_buf), prefix + 1); + pstrcpy(pref_buf, PATH_MAX, prefix); - base = new_entry("", NULL, pref_buf); + if (realpath(pref_buf, real_buf)) + pstrcpy(pref_buf, PATH_MAX, real_buf); + + if (!strcmp(pref_buf, "/")) + return; + + base = new_entry("", NULL, pref_buf + 1); base = add_dir_maybe(base); if (base->num_entries == 0) { free (base);