From nobody Tue Feb 10 10:58:01 2026 Delivered-To: importer@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+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1636886346; cv=none; d=zohomail.com; s=zohoarc; b=fS5v0yt9Sv/Ayb+ILnddLBk0vkHN2v/iMHyMzcMd2bHlzyOVEDVMV+DhGdsVYCmrxccHpJ9McGNwVzyIEJjmPo0S8ovfLJzHqDlkUuWBwV1HbbcdrKA6NJeXxB55g1ajus55OgnIxuRGPnpi/aa15hmN92bxTJBzRQZcRFfZ4F4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1636886346; 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=u7NrqkDveBa4hwrZ13pQ52EsKuNUzd3UgVxFiV4cVYA=; b=Wzv5L1KF70fmNG57/IL9AEqVoy6OBQtJMTGAEAGABE6+z6dVccZFYSG/HecFVvovLQ236n+x38YHfh4qSfMw+6zpy4J56AA1TnAfXkv12oQEUC+Qz3XQJnEL5w+0TH+/RgQzJ8xuUdT88amKdIi5sHC2h0aivpvfV/2BHwEorBU= 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+importer=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 1636886346768588.7325259698424; Sun, 14 Nov 2021 02:39:06 -0800 (PST) Received: from localhost ([::1]:56462 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mmCuf-0008Lx-Hq for importer@patchew.org; Sun, 14 Nov 2021 05:39:05 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46626) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mmCri-000499-5j for qemu-devel@nongnu.org; Sun, 14 Nov 2021 05:36:02 -0500 Received: from [2a00:1450:4864:20::32c] (port=40831 helo=mail-wm1-x32c.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mmCrf-0000hg-Nw for qemu-devel@nongnu.org; Sun, 14 Nov 2021 05:36:01 -0500 Received: by mail-wm1-x32c.google.com with SMTP id az33-20020a05600c602100b00333472fef04so12613351wmb.5 for ; Sun, 14 Nov 2021 02:35:59 -0800 (PST) Received: from localhost.localdomain (60.red-95-126-179.staticip.rima-tde.net. [95.126.179.60]) by smtp.gmail.com with ESMTPSA id x1sm10638852wmc.22.2021.11.14.02.35.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Nov 2021 02:35:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=u7NrqkDveBa4hwrZ13pQ52EsKuNUzd3UgVxFiV4cVYA=; b=Ntw3qQ6U0qQqTLEFjLdJs9M+lCBWSV899Wc+QJ+B7DY1UVlP2u6wugd2kztg1AaSpc zyIPdugHdeq9t4gO+asPe/kmsSvlOBD+LCUypyv7a2JwRXq1gRKeaNX+9LQ280xRM7D8 aMSU/1HZr9vhYMQv4Z2MSWDlI62wN/T2twRO0ZoEzMnS4tskoqF9DEFUbnR4RNdV7ncm dRbCLbx8xY8qExhqmDsYIBoV9ddZsMOb9Pj8rh/3RAVBXdZz2PYbfO0YXYnKvpFq/yIz ujyCXR7qfixWFj7pjUj7WJ/bka6z/QxabFUv5VCZ5CkRHjVG/qu5JQPLxwmdwwXm9m1U dJeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=u7NrqkDveBa4hwrZ13pQ52EsKuNUzd3UgVxFiV4cVYA=; b=F938HgEmcKX6WmbYam8//MIBEl6yYHbBP0a8JYTh1/5zhx+IFzGr8KF4HRdw9zsGUK 40r19LRBkSyNQVG2rWiu7ABLiQR49U4g0+K3oRYDBt8IYyW9GJwwPZiHDaimLpci+WKV 963t7z1zte7+czq9w8FbcaCjhv8H5gSS/GiN495Tm5MFQcFj9n1XA4vF9VJEK9tueZZP HOoexibTjabhQ3Xk3CLRKMjrBqpWpm5/SkdNCHD1rGYCSDCldJf867EXK+t1aLU0Pjk5 OJMcKeDeGT0QW44T0K94q15IN1UDe0YKVUu4tIttLkLImySzZtNjFJ1Ex8sdvXT335SF +Ckw== X-Gm-Message-State: AOAM533VjK7BYxO/sOS2MJQuM/RKRgZTralJqZBzUyIBG8Gsp+OhRkqr /W7ZxfhSfKd6AUbK8NWANkjhfoVjjzrJqN89ONw= X-Google-Smtp-Source: ABdhPJzIDGAOw841v1sz0nQTZXpOjrTCj/Iq4xnkupMU8i6D0JxXQTqhoXzcYzh/xXFFfwif/eKbGA== X-Received: by 2002:a7b:c194:: with SMTP id y20mr34037745wmi.61.1636886158283; Sun, 14 Nov 2021 02:35:58 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 4/4] linux-user: Rewrite do_getdents, do_getdents64 Date: Sun, 14 Nov 2021 11:35:39 +0100 Message-Id: <20211114103539.298686-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211114103539.298686-1-richard.henderson@linaro.org> References: <20211114103539.298686-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::32c (failed) 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+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::32c; envelope-from=richard.henderson@linaro.org; helo=mail-wm1-x32c.google.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: , Cc: laurent@vivier.eu Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1636886347277100001 Content-Type: text/plain; charset="utf-8" Always allocate host storage; this ensures that the struct is sufficiently aligned for the host. Merge the three host implementations of getdents via a few ifdefs. Utilize the same method for do_getdents64. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/704 Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- linux-user/syscall.c | 259 ++++++++++++++++++++----------------------- 1 file changed, 121 insertions(+), 138 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 499415ad81..f1cfcc8104 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -8140,172 +8140,155 @@ static int host_to_target_cpu_mask(const unsigned= long *host_mask, } =20 #ifdef TARGET_NR_getdents -static int do_getdents(abi_long arg1, abi_long arg2, abi_long arg3) +static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count) { - int ret; + g_autofree void *hdirp =3D NULL; + void *tdirp; + int hlen, hoff, toff; + int hreclen, treclen; + off64_t prev_diroff =3D 0; =20 -#ifdef EMULATE_GETDENTS_WITH_GETDENTS -# if TARGET_ABI_BITS =3D=3D 32 && HOST_LONG_BITS =3D=3D 64 - struct target_dirent *target_dirp; - struct linux_dirent *dirp; - abi_long count =3D arg3; - - dirp =3D g_try_malloc(count); - if (!dirp) { + hdirp =3D g_try_malloc(count); + if (!hdirp) { return -TARGET_ENOMEM; } =20 - ret =3D get_errno(sys_getdents(arg1, dirp, count)); - if (!is_error(ret)) { - struct linux_dirent *de; - struct target_dirent *tde; - int len =3D ret; - int reclen, treclen; - int count1, tnamelen; +#ifdef EMULATE_GETDENTS_WITH_GETDENTS + hlen =3D sys_getdents(dirfd, hdirp, count); +#else + hlen =3D sys_getdents64(dirfd, hdirp, count); +#endif =20 - count1 =3D 0; - de =3D dirp; - target_dirp =3D lock_user(VERIFY_WRITE, arg2, count, 0); - if (!target_dirp) { - return -TARGET_EFAULT; - } - tde =3D target_dirp; - while (len > 0) { - reclen =3D de->d_reclen; - tnamelen =3D reclen - offsetof(struct linux_dirent, d_name); - assert(tnamelen >=3D 0); - treclen =3D tnamelen + offsetof(struct target_dirent, d_name); - assert(count1 + treclen <=3D count); - tde->d_reclen =3D tswap16(treclen); - tde->d_ino =3D tswapal(de->d_ino); - tde->d_off =3D tswapal(de->d_off); - memcpy(tde->d_name, de->d_name, tnamelen); - de =3D (struct linux_dirent *)((char *)de + reclen); - len -=3D reclen; - tde =3D (struct target_dirent *)((char *)tde + treclen); - count1 +=3D treclen; - } - ret =3D count1; - unlock_user(target_dirp, arg2, ret); + hlen =3D get_errno(hlen); + if (is_error(hlen)) { + return hlen; } - g_free(dirp); -# else - struct linux_dirent *dirp; - abi_long count =3D arg3; =20 - dirp =3D lock_user(VERIFY_WRITE, arg2, count, 0); - if (!dirp) { + tdirp =3D lock_user(VERIFY_WRITE, arg2, count, 0); + if (!tdirp) { return -TARGET_EFAULT; } - ret =3D get_errno(sys_getdents(arg1, dirp, count)); - if (!is_error(ret)) { - struct linux_dirent *de; - int len =3D ret; - int reclen; - de =3D dirp; - while (len > 0) { - reclen =3D de->d_reclen; - if (reclen > len) { + + for (hoff =3D toff =3D 0; hoff < hlen; hoff +=3D hreclen, toff +=3D tr= eclen) { +#ifdef EMULATE_GETDENTS_WITH_GETDENTS + struct linux_dirent *hde =3D hdirp + hoff; +#else + struct linux_dirent64 *hde =3D hdirp + hoff; +#endif + struct target_dirent *tde =3D tdirp + toff; + int namelen; + uint8_t type; + + namelen =3D strlen(hde->d_name); + hreclen =3D hde->d_reclen; + treclen =3D offsetof(struct target_dirent, d_name) + namelen + 2; + treclen =3D QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent)= ); + + if (toff + treclen > count) { + /* + * If the host struct is smaller than the target struct, or + * requires less alignment and thus packs into less space, + * then the host can return more entries than we can pass + * on to the guest. + */ + if (toff =3D=3D 0) { + toff =3D -TARGET_EINVAL; /* result buffer is too small */ break; } - de->d_reclen =3D tswap16(reclen); - tswapls(&de->d_ino); - tswapls(&de->d_off); - de =3D (struct linux_dirent *)((char *)de + reclen); - len -=3D reclen; - } - } - unlock_user(dirp, arg2, ret); -# endif -#else - /* Implement getdents in terms of getdents64 */ - struct linux_dirent64 *dirp; - abi_long count =3D arg3; - - dirp =3D lock_user(VERIFY_WRITE, arg2, count, 0); - if (!dirp) { - return -TARGET_EFAULT; - } - ret =3D get_errno(sys_getdents64(arg1, dirp, count)); - if (!is_error(ret)) { - /* - * Convert the dirent64 structs to target dirent. We do this - * in-place, since we can guarantee that a target_dirent is no - * larger than a dirent64; however this means we have to be - * careful to read everything before writing in the new format. - */ - struct linux_dirent64 *de; - struct target_dirent *tde; - int len =3D ret; - int tlen =3D 0; - - de =3D dirp; - tde =3D (struct target_dirent *)dirp; - while (len > 0) { - int namelen, treclen; - int reclen =3D de->d_reclen; - uint64_t ino =3D de->d_ino; - int64_t off =3D de->d_off; - uint8_t type =3D de->d_type; - - namelen =3D strlen(de->d_name); - treclen =3D offsetof(struct target_dirent, d_name) + namelen += 2; - treclen =3D QEMU_ALIGN_UP(treclen, sizeof(abi_long)); - - memmove(tde->d_name, de->d_name, namelen + 1); - tde->d_ino =3D tswapal(ino); - tde->d_off =3D tswapal(off); - tde->d_reclen =3D tswap16(treclen); /* - * The target_dirent type is in what was formerly a padding - * byte at the end of the structure: + * Return what we have, resetting the file pointer to the + * location of the first record not returned. */ - *(((char *)tde) + treclen - 1) =3D type; - - de =3D (struct linux_dirent64 *)((char *)de + reclen); - tde =3D (struct target_dirent *)((char *)tde + treclen); - len -=3D reclen; - tlen +=3D treclen; + lseek64(dirfd, prev_diroff, SEEK_SET); + break; } - ret =3D tlen; - } - unlock_user(dirp, arg2, ret); + + prev_diroff =3D hde->d_off; + tde->d_ino =3D tswapal(hde->d_ino); + tde->d_off =3D tswapal(hde->d_off); + tde->d_reclen =3D tswap16(treclen); + memcpy(tde->d_name, hde->d_name, namelen + 1); + + /* + * The getdents type is in what was formerly a padding byte at the + * end of the structure. + */ +#ifdef EMULATE_GETDENTS_WITH_GETDENTS + type =3D *((uint8_t *)hde + hreclen - 1); +#else + type =3D hde->d_type; #endif - return ret; + *((uint8_t *)tde + treclen - 1) =3D type; + } + + unlock_user(tdirp, arg2, toff); + return toff; } #endif /* TARGET_NR_getdents */ =20 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64) -static int do_getdents64(abi_long arg1, abi_long arg2, abi_long arg3) +static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count) { - struct linux_dirent64 *dirp; - abi_long count =3D arg3; - int ret; + g_autofree void *hdirp =3D NULL; + void *tdirp; + int hlen, hoff, toff; + int hreclen, treclen; + off64_t prev_diroff =3D 0; =20 - dirp =3D lock_user(VERIFY_WRITE, arg2, count, 0); - if (!dirp) { + hdirp =3D g_try_malloc(count); + if (!hdirp) { + return -TARGET_ENOMEM; + } + + hlen =3D get_errno(sys_getdents64(dirfd, hdirp, count)); + if (is_error(hlen)) { + return hlen; + } + + tdirp =3D lock_user(VERIFY_WRITE, arg2, count, 0); + if (!tdirp) { return -TARGET_EFAULT; } - ret =3D get_errno(sys_getdents64(arg1, dirp, count)); - if (!is_error(ret)) { - struct linux_dirent64 *de; - int len =3D ret; - int reclen; - de =3D dirp; - while (len > 0) { - reclen =3D de->d_reclen; - if (reclen > len) { + + for (hoff =3D toff =3D 0; hoff < hlen; hoff +=3D hreclen, toff +=3D tr= eclen) { + struct linux_dirent64 *hde =3D hdirp + hoff; + struct target_dirent64 *tde =3D tdirp + toff; + int namelen; + + namelen =3D strlen(hde->d_name) + 1; + hreclen =3D hde->d_reclen; + treclen =3D offsetof(struct target_dirent64, d_name) + namelen; + treclen =3D QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent6= 4)); + + if (toff + treclen > count) { + /* + * If the host struct is smaller than the target struct, or + * requires less alignment and thus packs into less space, + * then the host can return more entries than we can pass + * on to the guest. + */ + if (toff =3D=3D 0) { + toff =3D -TARGET_EINVAL; /* result buffer is too small */ break; } - de->d_reclen =3D tswap16(reclen); - tswap64s((uint64_t *)&de->d_ino); - tswap64s((uint64_t *)&de->d_off); - de =3D (struct linux_dirent64 *)((char *)de + reclen); - len -=3D reclen; + /* + * Return what we have, resetting the file pointer to the + * location of the first record not returned. + */ + lseek64(dirfd, prev_diroff, SEEK_SET); + break; } + + prev_diroff =3D hde->d_off; + tde->d_ino =3D tswap64(hde->d_ino); + tde->d_off =3D tswap64(hde->d_off); + tde->d_reclen =3D tswap16(treclen); + tde->d_type =3D hde->d_type; + memcpy(tde->d_name, hde->d_name, namelen); } - unlock_user(dirp, arg2, ret); - return ret; + + unlock_user(tdirp, arg2, toff); + return toff; } #endif /* TARGET_NR_getdents64 */ =20 --=20 2.25.1