From nobody Thu May 2 21:22:43 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 1508898388117634.0908894805057; Tue, 24 Oct 2017 19:26:28 -0700 (PDT) Received: from localhost ([::1]:46238 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e7BOf-0005Gp-SN for importer@patchew.org; Tue, 24 Oct 2017 22:26:21 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44069) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e78JX-0008Mp-Gw for qemu-devel@nongnu.org; Tue, 24 Oct 2017 19:08:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e78JW-0002ix-Dx for qemu-devel@nongnu.org; Tue, 24 Oct 2017 19:08:51 -0400 Received: from mail-oi0-x242.google.com ([2607:f8b0:4003:c06::242]:56107) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1e78JT-0002gt-0b; Tue, 24 Oct 2017 19:08:47 -0400 Received: by mail-oi0-x242.google.com with SMTP id g125so40112233oib.12; Tue, 24 Oct 2017 16:08:46 -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 8sm713761otf.13.2017.10.24.16.08.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Oct 2017 16:08:44 -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; bh=UNX5LeRBaPungLYmGR+puUMm13E5PYH1s0dHXYJceIg=; b=hXaUq/1Hk/bass8EBhiJrFq8Jp95tBYAet92Q3ilQPd1U8Ot7YHxpgfhwHrn6sGEsK mnfZDB9kLj0xDOJeYraeocQv5++bflxU3SyPIkmjq/YmgxZpNe7hy97KOZFNjnwUNiUw vWe6UP68d4t/zXu0L4qZft8oM/A4X10jFp/zsfEJe6MCK2JC1Poh4FqNFqvPhmMPJQGm CO4xrZhqPtrnz3r/Qy/8VGmUFx4o06Vohux6Y2qQHKwu5j/EqwoJCq79t0Hhi40DvDYn otHPdbvwLVNT05DIcxDbKC4ETfcEDiLNQ8EZ3GJFAL4wS2DaHjmn0/RtynhaVXMIlTOY B0rQ== 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=UNX5LeRBaPungLYmGR+puUMm13E5PYH1s0dHXYJceIg=; b=kQlxrLsbwi0nw0bxmVF916YPRI0MiO9xDkWfpExosviKPbccpvx7X2WMOeLh9YnrTl Kjo5tTfJWiqPxQHSdgmLm0XpH9VTyd6HHl24qJIMTO/oxvOw/hwJzffpi+LrvukodDYq 7Av3QB+IbO3ZN22F3Hy0H1QI4Ya2OmZANFry/FTdPdXuuxr/Iadu8rrPN5jv3uW0VJBS Xxc0QR5/F6VNKrKj1nA6k6Y2Jvi/SuRlBsULx/LD5owGwq++QCGMWp5StuvlpMbR/vjT P0sHCGq/7MziLLhn7cfGGFlzpSnzEchXPnc2vnp5c2bq2DzOqyLjn1X1AVn57PQiThAA 2Y6A== X-Gm-Message-State: AMCzsaUN5gwqXzrnBAOBSMn2lvcs0s33CZYFwpLq7IH+DCJMo7QZYpGG +7Q06jxaNk1AkEdzxP2OEObJsvsw X-Google-Smtp-Source: ABhQp+ROqJhp7noCwcuG4TY9ntQMlGzuKQbjcZhMnwQu94MW6cYp8/L0KMEl3b7t5nsQyC86f3PXrg== X-Received: by 10.202.240.65 with SMTP id o62mr131065oih.300.1508886525734; Tue, 24 Oct 2017 16:08:45 -0700 (PDT) From: Zach Riggle X-Google-Original-From: Zach Riggle To: Date: Tue, 24 Oct 2017 18:07:58 -0500 Message-Id: <20171024230758.31779-1-riggle@google.com> X-Mailer: git-send-email 2.14.3 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4003:c06::242 X-Mailman-Approved-At: Tue, 24 Oct 2017 22:25:28 -0400 Subject: [Qemu-devel] [PATCH] 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" From: Zach Riggle 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 | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 9bf901fa11..8fef3bb28e 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -7496,26 +7496,32 @@ 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), "/pro= c/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