From nobody Sun May 5 15:54:03 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1630683510; cv=none; d=zohomail.com; s=zohoarc; b=XZYtWsM32DBWP/ImNPdT9urm0awco4I4dcpx3Pki1KrqQGk8H7Hr/kaiP74NsLk2uPSTp/8iRwSuEbUxKYfx9Ixf+P1YR4xQUcKcX3oKt8d1VK0DsCRsbR3HL8wC7sLIcLXdWrnIfflstrnjG1wVAfLIdatc4sP2X4w1Ce7+Xnk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630683510; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:MIME-Version:Message-ID:Subject:To; bh=DpvGPodUqzAwySzW1P1//vFtQok8MYmKbF2y9o9SvwA=; b=NEgQZ+o75eZU+wdUY9ilqNz6p8wSiRXPFQmbAXIoxMkOSdf7GlrtwtJE77oH9QtpJlhnSL3rueZHkNk2H7js1HNcem1WxvizxUy0Y3dV0oAT9ZwtP8QX1yBKxLTje6Z4VhKkwj1KL+sJjKNRpjzeEbVUJITiqHVdRi8GYtg/KFQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 163068351034423.019919121025737; Fri, 3 Sep 2021 08:38:30 -0700 (PDT) Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-111-42W2HnqGMxC2IZX8exbZ3A-1; Fri, 03 Sep 2021 11:38:27 -0400 Received: by mail-wm1-f70.google.com with SMTP id s197-20020a1ca9ce000000b002e72ba822dcso2827635wme.6 for ; Fri, 03 Sep 2021 08:38:27 -0700 (PDT) Return-Path: Return-Path: Received: from x1w.. (163.red-83-52-55.dynamicip.rima-tde.net. [83.52.55.163]) by smtp.gmail.com with ESMTPSA id x11sm4961374wro.83.2021.09.03.08.38.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 08:38:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630683509; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DpvGPodUqzAwySzW1P1//vFtQok8MYmKbF2y9o9SvwA=; b=dHNvoWTZAsseOH4ui+N67m/RVvL7VY28+vka4cmjnBbK3Fbo/zWB36iDHCLK8Hfd4xXOxB VG00+jBg8yHc1ry5TqkwU/WliLOgkmdZBiVicRRN4SMVsT/PBr/QGdc14K/q+Mn+sEXyXz /F4/qDoLzxeEBDEdAhW/7fOewQgcBnI= X-MC-Unique: 42W2HnqGMxC2IZX8exbZ3A-1 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:mime-version :content-transfer-encoding; bh=DpvGPodUqzAwySzW1P1//vFtQok8MYmKbF2y9o9SvwA=; b=fxdovqejXqXbvQYHdBvEn2x97N599NkhFuygB3GpHTY4RMfJpqcNY6M9VG7Xj2CtHF fLUJ6yyzhaBkAsXJ4jWIWZ0QBcpz+2PENJjwU7mXwhgohPJ5J0/lFEk6ozLu0FBDKNei yjl+HLx8yg/hA8wjK9NHPiHctrOLM8vmLuBskGcMAZuOb4JnVL27XqvQhHpxIn7XGmsf ynhe/lUReBhVMXptsraAGhulCNtYtusAm7EutQEoxny1ohS7hfE+tQtbf5OLFLpd/NnB 7KzS8JPasHqiUdanFSMMZRdsOXChws0uKky51Rg8mNJKgMrnNbndsZNHDUweLaOzk6p+ bKqQ== X-Gm-Message-State: AOAM533qtI5Yq8DVA3HKFGVrPSjtmWPVMg7xw6wqrTzk2xiha1u5xCaD kX7yR/fOqxiLX86esYsWHQEZ8Wv/QKDG4mAXE5Fp5+ryMNLbP0PYS97jb/777/uTRWe+Hi/Yi01 RbVjLR+E/xam8dA== X-Received: by 2002:a05:600c:295:: with SMTP id 21mr1029715wmk.37.1630683506077; Fri, 03 Sep 2021 08:38:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyGsUpHSYREAxrTLacfKrjl+3DWt9mS5TtTtEeIk298GOGIbYFuX1cQH5cGqSlKYKltKrzyOg== X-Received: by 2002:a05:600c:295:: with SMTP id 21mr1029702wmk.37.1630683505897; Fri, 03 Sep 2021 08:38:25 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org, Peter Maydell Cc: Alexey Kardashevskiy , Peter Xu , Paolo Bonzini , David Hildenbrand , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Gerd Hoffmann Subject: [RFC PATCH] physmem: Do not allow unprivileged device map privileged memory Date: Fri, 3 Sep 2021 17:38:20 +0200 Message-Id: <20210903153820.686913-1-philmd@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1630683511238100001 Since commits cc05c43ad94..42874d3a8c6 ("memory: Define API for MemoryRegionOps to take attrs and return status") the Memory API returns a zero (MEMTX_OK) response meaning success, anything else indicating a failure. In commits c874dc4f5e8..2f7b009c2e7 ("Make address_space_map() take a MemTxAttrs argument") we updated the AddressSpace and FlatView APIs but forgot to check the returned value by the FlatView from the MemoryRegion. Adjust that now, only returning a non-NULL address if the transaction succeeded with the requested memory attributes. Reported-by: Gerd Hoffmann Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- RFC because this could become a security issue in a core component, however currently all callers pass MEMTXATTRS_UNSPECIFIED. --- include/exec/memory.h | 3 ++- softmmu/physmem.c | 16 ++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index c3d417d317f..488d2ecdd09 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -2706,7 +2706,8 @@ bool address_space_access_valid(AddressSpace *as, hwa= ddr addr, hwaddr len, * * May map a subset of the requested range, given by and returned in @plen. * May return %NULL and set *@plen to zero(0), if resources needed to perf= orm - * the mapping are exhausted. + * the mapping are exhausted or if the physical memory region is not acces= sible + * for the requested memory attributes. * Use only for reads OR writes - not for read-modify-write operations. * Use cpu_register_map_client() to know when retrying the map operation is * likely to succeed. diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 23e77cb7715..802c339f6d2 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -3188,15 +3188,19 @@ void *address_space_map(AddressSpace *as, /* Avoid unbounded allocations */ l =3D MIN(l, TARGET_PAGE_SIZE); bounce.buffer =3D qemu_memalign(TARGET_PAGE_SIZE, l); + + if (!is_write) { + if (flatview_read(fv, addr, attrs, bounce.buffer, l) !=3D MEMT= X_OK) { + qemu_vfree(bounce.buffer); + *plen =3D 0; + return NULL; + } + } + bounce.addr =3D addr; bounce.len =3D l; - - memory_region_ref(mr); bounce.mr =3D mr; - if (!is_write) { - flatview_read(fv, addr, MEMTXATTRS_UNSPECIFIED, - bounce.buffer, l); - } + memory_region_ref(mr); =20 *plen =3D l; return bounce.buffer; --=20 2.31.1