From nobody Mon May 6 15:47:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1508902566893549.9544074528454; Tue, 24 Oct 2017 20:36:06 -0700 (PDT) Received: from localhost ([::1]:46374 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e7CTx-0007DB-It for importer@patchew.org; Tue, 24 Oct 2017 23:35:53 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49291) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e7CT2-0006ph-97 for qemu-devel@nongnu.org; Tue, 24 Oct 2017 23:34:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e7CT1-0005Ya-DZ for qemu-devel@nongnu.org; Tue, 24 Oct 2017 23:34:56 -0400 Received: from mail-yw0-x243.google.com ([2607:f8b0:4002:c05::243]:51191) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1e7CSz-0005VA-AI; Tue, 24 Oct 2017 23:34:53 -0400 Received: by mail-yw0-x243.google.com with SMTP id i198so16915228ywe.7; Tue, 24 Oct 2017 20:34:53 -0700 (PDT) Received: from riggle-macbookpro.roam.corp.google.com.com ([2620:15c:2a:2:3c3d:b40:6b34:821]) by smtp.gmail.com with ESMTPSA id b129sm915949ywe.99.2017.10.24.20.34.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Oct 2017 20:34:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=H6uq2BY2WAil6dqudVuF2JhQ6M7GsnKg+U188Ogwfa0=; b=eAsdUnnxcK1WlF+2X/vkRhGNdBT9B2wDgNKxK3IXL6WREB6AJiMSRwufpIczK0fKTL U6COoMetw7vHTT2KP/m2BhpBV3cFEF/Be0qVzIv1Dd9VbVG5M9KJZkEJ/DgsWF7+45+8 T5aYbFTPzWe6SrWCIMWq6mjXAdVoKXc7t92xh/5i4Fr3eeK+elFczbgvI8GsLQjwjj/A JfzfGqZDAGYD2oLt85l4Pe6ebA+L83PoJHv5sO9S1AtVZ8TY/IlcU4g3O+YRlvt1m4Ev knLZ3s3z52gSOq2DaC9zZTX+xYInhgXnDwei2ETXIXjDO5EdyCGykeoMsOzSBEtCC3B5 6Mjw== 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:in-reply-to :references; bh=H6uq2BY2WAil6dqudVuF2JhQ6M7GsnKg+U188Ogwfa0=; b=VH8SRMSwU4jwJYKvPWQzEw2F9WXLa5XfnybNLFcNNR6aDw3m8kNABRu02L6MglTg5t 7V1LxNlwI73o40JWLhepE2pTUIijhpK5SAfJYHqxUV65gL/l3PtFNJksoZVfXIPVxZ4l D4OgGIxl3GSCTP0uCBc3ir8eYQx+ll2QEh5Vl89TgaNruOgp8OXcjwwS95ttxsPb/QWu Ji1TXQScz8OaN/2oHcF8IcthO4XDHjJErtFg6aGEIBzshfAEynSTih3KJzPyOpdYdFI+ jmRd2nGafzuzGOlsC/b/tFjQq0H12tNp0OGEmuv02hWp0vJWV263NjmR+joh4wdcWroT k9gw== X-Gm-Message-State: AMCzsaUusUCFOp61VWueuSatXIv7w99IikdjeyDfwRH5/qr3xFiDNxRG k2UfhLoa2NRcAdci10KAixtM0prt X-Google-Smtp-Source: ABhQp+TbEIa37ARMxg20ughdLIA08lppakLlqrHnTrSQ60ejhw0vH3gqvppKigUCllRJYYzkq0zzRw== X-Received: by 10.129.160.129 with SMTP id x123mr12794736ywg.508.1508902492318; Tue, 24 Oct 2017 20:34:52 -0700 (PDT) From: Zach Riggle To: Date: Tue, 24 Oct 2017 22:34:42 -0500 Message-Id: <20171025033442.44872-1-zachriggle@gmail.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171024230758.31779-1-riggle@google.com> References: <20171024230758.31779-1-riggle@google.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4002:c05::243 Subject: [Qemu-devel] [PATCH v2] linux-user: fix is_proc_myself to check the paths via realpath 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: qemu-trivial@nongnu.org, Zach Riggle , Riku Voipio , Laurent Vivier , "open list:All patches CC here" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Previously, it was possible to get a handle to the "real" /proc/self/mem by creating a symlink to it and opening the symlink, or opening e.g. "./mem" after chdir'ing to "/proc/self". $ ln -s /proc/self self $ cat self/maps 60000000-602bc000 r-xp 00000000 fc:01 270375 = /usr/bin/qemu-arm-static 604bc000-6050f000 rw-p 002bc000 fc:01 270375 = /usr/bin/qemu-arm-static ... Signed-off-by: Zach Riggle --- linux-user/syscall.c | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 9bf901fa11..6c1f28a1f7 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -7496,26 +7496,35 @@ static int open_self_auxv(void *cpu_env, int fd) static int is_proc_myself(const char *filename, const char *entry) { - if (!strncmp(filename, "/proc/", strlen("/proc/"))) { - filename +=3D strlen("/proc/"); - if (!strncmp(filename, "self/", strlen("self/"))) { - filename +=3D strlen("self/"); - } else if (*filename >=3D '1' && *filename <=3D '9') { - char myself[80]; - snprintf(myself, sizeof(myself), "%d/", getpid()); - if (!strncmp(filename, myself, strlen(myself))) { - filename +=3D strlen(myself); - } else { - return 0; - } - } else { - return 0; - } - if (!strcmp(filename, entry)) { - return 1; - } + char proc_self_entry[PATH_MAX + 1]; + char proc_self_entry_realpath[PATH_MAX + 1]; + char filename_realpath[PATH_MAX + 1]; + + if (PATH_MAX < snprintf(proc_self_entry, + sizeof(proc_self_entry), + "/proc/self/%s", + entry)) { + /* Full path to "entry" is too long to fit in the buffer */ + return 0; } - return 0; + + if (!realpath(filename, filename_realpath)) { + /* File does not exist, or can't be canonicalized */ + return 0; + } + + if (!realpath(proc_self_entry, proc_self_entry_realpath)) { + /* Procfs entry does not exist */ + return 0; + } + + if (strcmp(filename_realpath, proc_self_entry_realpath) !=3D 0) { + /* Paths are different */ + return 0; + } + + /* filename refers to /proc/self/ */ + return 1; } #if defined(HOST_WORDS_BIGENDIAN) !=3D defined(TARGET_WORDS_BIGENDIAN) -- 2.14.3