From nobody Mon May 12 07:49:57 2025 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1695119613; cv=none; d=zohomail.com; s=zohoarc; b=i5fEe3Jqu6rhMWigzIdP9Yn6w8H+FCmNTUSGnNM710iAHnDC3r/O+Ht7ZSt62IyE5e7yTaBqjFdvfpW7Aix5dtAqeRpxVibvnMpx+tTdPwtbdQxacSnHffu8cCe6TzbznRaMGSVkTi6GPgLDQiYl8tucY3ztzeUnjLmC6bIp3Bk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695119613; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=pu2YDx5aiS5CvUaCOfL0u8qxACu7nY4VGE1cPIwI+mE=; b=JnqUFSWnNWF0YfoTudf6kfbUOZtARafEJiObV4knFtVeG289jHwDv+mMpN+U/VvUuBt3Vve2IznJ3/UbW140huxPID5Png9uP29BL9xHSXT/3fVE24Xn6cD9Fw+9GI4kmcaGP0MYcSwDQxaqHTi9KgdCSYylk2vofGr2sGRCHbY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 169511961318831.538831194479485; Tue, 19 Sep 2023 03:33:33 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qiY1N-0004qG-4U; Tue, 19 Sep 2023 06:31:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qiY0D-00036U-Bb for qemu-devel@nongnu.org; Tue, 19 Sep 2023 06:30:47 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qiY0B-0001v1-Fr for qemu-devel@nongnu.org; Tue, 19 Sep 2023 06:30:45 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-327-jYh8I5fcOsuLzvNHaQ2lUA-1; Tue, 19 Sep 2023 06:30:37 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8A764803470; Tue, 19 Sep 2023 10:30:37 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.194.89]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9F6D240C6EBF; Tue, 19 Sep 2023 10:30:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695119442; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pu2YDx5aiS5CvUaCOfL0u8qxACu7nY4VGE1cPIwI+mE=; b=Vug24Kqx3u+wHI+OQrI1Rz1xMEMIYZJqCdULbxq8IFGxe5jCY+YXuwiWAJXBctGbNsfI9j V55XPZmjc0wMK58b10EdVR/4PbeKN2kne0vMUM4MTuWlsa95ghKsgpqK32wXDOtI8Dnvvp LBMHgkapW4LMa3NmZYfMVx8/f0I5bXM= X-MC-Unique: jYh8I5fcOsuLzvNHaQ2lUA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Thiner Logoer , Peter Xu , Mario Casquero Subject: [GIT PULL 07/12] softmmu/physmem: Never return directories from file_ram_open() Date: Tue, 19 Sep 2023 12:30:24 +0200 Message-ID: <20230919103029.235736-8-david@redhat.com> In-Reply-To: <20230919103029.235736-1-david@redhat.com> References: <20230919103029.235736-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1695119615397100003 Content-Type: text/plain; charset="utf-8" open() does not fail on directories when opening them readonly (O_RDONLY). Currently, we succeed opening such directories and fail later during mmap(), resulting in a misleading error message. $ ./qemu-system-x86_64 \ -object memory-backend-file,id=3Dram0,mem-path=3Dtmp,readonly=3Dtrue,si= ze=3D1g qemu-system-x86_64: unable to map backing store for guest RAM: No such dev= ice To identify directories and handle them accordingly in file_ram_open() also when readonly=3Dtrue was specified, detect if we just opened a directo= ry using fstat() instead. Then, fail file_ram_open() right away, similarly to how we now fail if the file does not exist and we want to open the file readonly. With this change, we get a nicer error message: qemu-system-x86_64: can't open backing store tmp for guest RAM: Is a direc= tory Note that the only memory-backend-file will end up calling memory_region_init_ram_from_file() -> qemu_ram_alloc_from_file() -> file_ram_open(). Message-ID: <20230906120503.359863-8-david@redhat.com> Reported-by: Thiner Logoer Reviewed-by: Peter Xu Tested-by: Mario Casquero Signed-off-by: David Hildenbrand --- softmmu/physmem.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 138402b6cf..f1cd3ec28a 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -1299,6 +1299,25 @@ static int file_ram_open(const char *path, for (;;) { fd =3D open(path, readonly ? O_RDONLY : O_RDWR); if (fd >=3D 0) { + /* + * open(O_RDONLY) won't fail with EISDIR. Check manually if we + * opened a directory and fail similarly to how we fail ENOENT + * in readonly mode. Note that mkstemp() would imply O_RDWR. + */ + if (readonly) { + struct stat file_stat; + + if (fstat(fd, &file_stat)) { + close(fd); + if (errno =3D=3D EINTR) { + continue; + } + return -errno; + } else if (S_ISDIR(file_stat.st_mode)) { + close(fd); + return -EISDIR; + } + } /* @path names an existing file, use it */ break; } --=20 2.41.0