From patchwork Fri Jan 27 18:31:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 9542511 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C1303601D7 for ; Fri, 27 Jan 2017 18:36:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B041826E91 for ; Fri, 27 Jan 2017 18:36:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A1E0527F93; Fri, 27 Jan 2017 18:36:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4280526E91 for ; Fri, 27 Jan 2017 18:36:37 +0000 (UTC) Received: from localhost ([::1]:47345 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cXBO0-0007oX-Ck for patchwork-qemu-devel@patchwork.kernel.org; Fri, 27 Jan 2017 13:36:36 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44420) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cXBNZ-0007mR-VI for qemu-devel@nongnu.org; Fri, 27 Jan 2017 13:36:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cXBNW-0006ke-Qd for qemu-devel@nongnu.org; Fri, 27 Jan 2017 13:36:10 -0500 Received: from mail-pf0-x241.google.com ([2607:f8b0:400e:c00::241]:36686) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cXBNW-0006k8-Lq for qemu-devel@nongnu.org; Fri, 27 Jan 2017 13:36:06 -0500 Received: by mail-pf0-x241.google.com with SMTP id 19so19024039pfo.3 for ; Fri, 27 Jan 2017 10:36:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=uOGLsivCX9bCNq54JftvmjCiNk/sa7LOAaNnarpoHAk=; b=fNoO/Krdk3WAiLAWthrkSG10kET+hpQhusc4tGHrMpL7GIS97sqk2hiIofsEtu6WZb DMDW9KnKtusV62wCWNTzAeF9U51wZepNtLv80ITD3BPobbDgDLpXN3JYrjxUQEOBNGdF k6PT9XGwxvQ1Y4WNAmIlGJ+9T/fr+dSdCCt1jmU+ikpyIkwigKfRSns61dTuwiYx0pvD 63U8HagUIYd8ug6T3dh99dKGrCOkD0lUkiy4W+nU3myiZUhIJ1jGiWw06e8VrMxlVKL1 htn/deTIi9jN9n8aw7B54XK7P/fs1kEwUtIWRGAYAO2WebJpNzlORN1uTGJIgu1oKfF6 WWGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=uOGLsivCX9bCNq54JftvmjCiNk/sa7LOAaNnarpoHAk=; b=Q1Fxcqqt/gN7W43Ke+moZ9ipSqTe85agtSCjuNOXzLgP8NE7gMdQ2UXNb1P4Dkg3qY NQy3Km8Qtk6Z8DjhIp6lQtma3VIhvnPXYmXeOq0yp5MuyaremCI8mPTqdnym7V2ZMoL4 DxrMb1xa6UOZKUXz9lQsb/7zc4hRIEX3/KT2zv9Bo2tiNyVEKte2EYxonmcvnPk1H/F8 4OoQIgD5/agq6zdJa+3rlXyWRyZ5lcmFntQTbwqUOaS7JVTAXyKYhFCApC98g6zMWvEy IlUBvEjWPFCq9FZwWGudOrkmuOJqmxrAYVB6O1LRbfaEv7MiZ4naYebcYIDvUuQheYzF D1zA== X-Gm-Message-State: AIkVDXLQM6P6u2OLk+dZEIZpxM6Au3IvqlpPlJIYSAc1cR036CM+9oCQneycEb7mO9GugQ== X-Received: by 10.98.106.132 with SMTP id f126mr10836034pfc.1.1485542164176; Fri, 27 Jan 2017 10:36:04 -0800 (PST) Received: from squirtle.hq.andrews-space.com ([173.226.206.194]) by smtp.gmail.com with ESMTPSA id x2sm13063422pfa.71.2017.01.27.10.36.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 27 Jan 2017 10:36:03 -0800 (PST) From: Andrey Smirnov To: qemu-devel@nongnu.org Date: Fri, 27 Jan 2017 10:31:31 -0800 Message-Id: <20170127183131.6868-1-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.9.3 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c00::241 Subject: [Qemu-devel] [PATCH] util/path: Always translate /etc/ld.so.cache X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrey Smirnov , Peter Maydell , Paolo Bonzini Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Always translate /etc/ld.so.cache to point somwhere inside of guest's filesystem tree pointed to by 'prefix'. This prevents guest's libc from reading /etc/ld.so.cache of the host and potentialy failing. One of the manifestation of the problem could be easily reproduced by executing and simple application compiled for PowerPC on a x86 host using linux-use/qemu-ppc emulation. Anything as simple as: qemu-ppc -L /bin/ls should trigger the problem. Signed-off-by: Andrey Smirnov --- util/path.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/util/path.c b/util/path.c index 5479f76..cc28bff 100644 --- a/util/path.c +++ b/util/path.c @@ -108,8 +108,8 @@ static void set_parents(struct pathelem *child, struct pathelem *parent) } /* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */ -static const char * -follow_path(const struct pathelem *cursor, const char *name) +static struct pathelem * +follow_path(struct pathelem *cursor, const char *name) { unsigned int i, namelen; @@ -117,7 +117,7 @@ follow_path(const struct pathelem *cursor, const char *name) namelen = strcspn(name, "/"); if (namelen == 0) - return cursor->pathname; + return cursor; if (strneq(name, namelen, "..")) return follow_path(cursor->parent, name + namelen); @@ -133,9 +133,28 @@ follow_path(const struct pathelem *cursor, const char *name) return NULL; } + +static void append_entry(struct pathelem *cursor, + const char *name, unsigned type) +{ + size_t i; + struct pathelem *parent; + + parent = cursor->parent; + + for (i = 0; i < parent->num_entries; i++) { + if (parent->entries[i] == cursor) { + break; + } + } + + parent->entries[i] = add_entry(cursor, name, type); +} + void init_paths(const char *prefix) { char pref_buf[PATH_MAX]; + struct pathelem *cursor; if (prefix[0] == '\0' || !strcmp(prefix, "/")) @@ -164,15 +183,41 @@ void init_paths(const char *prefix) } else { set_parents(base, base); } + + /* + * libc does not necessarily handle reading host's ld.so.cache + * well (e.g. running PowerPC code on x86, or, very likely any + * mixed endian combination) + * + * So check if guest's prefix "tree" provides ld.so.cache and if + * not add a fake translation entry, so as to prevent guest's libc + * request to /etc/ld.so.cache to resolve into host's + * /etc/ld.so.cache + */ + cursor = follow_path(base, "/etc/ld.so.cache"); + if (!cursor) { + cursor = follow_path(base, "/etc/"); + if (!cursor) { + cursor = follow_path(base, "/"); + append_entry(cursor, "etc", DT_DIR); + cursor = follow_path(base, "/etc/"); + } + + append_entry(cursor, "ld.so.cache", DT_REG); + } } /* Look for path in emulation dir, otherwise return name. */ const char *path(const char *name) { + struct pathelem *cursor; + /* Only do absolute paths: quick and dirty, but should mostly be OK. Could do relative by tracking cwd. */ if (!base || !name || name[0] != '/') return name; - return follow_path(base, name) ?: name; + cursor = follow_path(base, name); + + return cursor ? cursor->pathname : name; }