From nobody Mon Nov 23 04:34:22 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1792961532; cv=none; d=zohomail.com; s=zohoarc; b=FCFX++p7cDybj+Rn9dDNy1WqKyFI7wxG5noX7fY06O6KGvFuTc5APr10ENucTdRimqhIKbKV+7Se797gePCUwbV4t7Gcs4PvH0wsmWNwlDFWw4h/FP0ls3PZ5HHoILXW9NkL4xn0jVk0ghu/0dYP92sWNPBfOfXHhQwWEyLTUuk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792961532; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Z9Kta+KfCkcQOsZgYvT+246Jbv94ngM6lu0pdvr/gF8=; b=BnIxJvGH8lnP49gwJdZH49gDPTVwwgqDuKNgELMLlNpmPdF8RiK7NUj91REIFrCokOoxPhKj9S63bh7FCHzY4oUCrLsDvlzQocucDhI5puX82Wj9cpQQ3YnpNS5OtX0d4gLP3oJCR9QvWzcCY2upE2vdD8Fa0l3gvt98zMCJNhk= 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 lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792961532434937.2533467442507; Sun, 25 Oct 2026 13:52:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xL5C9-0000TA-5g; Sun, 25 Oct 2026 16:51:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xL5C6-0000Rq-0J for qemu-devel@nongnu.org; Sun, 25 Oct 2026 16:51:54 -0400 Received: from mail-dy1-x132b.google.com ([2607:f8b0:4864:20::132b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xL5C4-0006AQ-Aw for qemu-devel@nongnu.org; Sun, 25 Oct 2026 16:51:53 -0400 Received: by mail-dy1-x132b.google.com with SMTP id 5a478bee46e88-35829219da8so4284898eec.0 for ; Sun, 25 Oct 2026 13:51:51 -0700 (PDT) Received: from a1-free ([161.33.196.121]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-198a2a5937dsm18680738c88.20.2026.10.25.13.51.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Oct 2026 13:51:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1792961509; x=1793566309; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Z9Kta+KfCkcQOsZgYvT+246Jbv94ngM6lu0pdvr/gF8=; b=TGe7QPIwxnmIykt01ngF6i3CWNn52xR7nto9OUrLx9KVw32eRfzdUMzDGsEFfuB/SE eG3pfcFDigBNSWy3lpHd4Ba7v8Jqs1az84KYhUlz13745Q/8pkgS4BIZVi3QKZ0CxdtJ +xOHye21tQt1RE6xXVek6obU4hiYjDy62uSfgZYr1pWw/LLvbpvzUnD412brVCCThE7L pNMz2M09MMqatHLo26B/a2CiJ95b7heImU+XVSggAw4IZ1z2NV33ONSV0OfVPZZuH69T Y9PAqy5ix5w45d2Ayi3PgEvXZ3LwrnEqDOgZmApcknzK8jZI8B45VgmBNyq5jZt6sjp5 QJYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792961509; x=1793566309; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Z9Kta+KfCkcQOsZgYvT+246Jbv94ngM6lu0pdvr/gF8=; b=VjA8Dyq3RMfVqMIjJt/RSS3l+Xrbw0OGePg5wgtNleFeusWIpm2gyNLpoqqtzgQkBH 2UgCX6g/u+i7V48r+zV/6z9QTCDSeomnB00qkBMFYbxYv8pdtOAR+YyHKgxvUhBflpz+ k0GuedCQeu4Ewih4vP0tdj6HglFqBFpjAVjw5UZJFXjJwXpGwODasuNLQLaZ/UZyEwdA XarcjIFrjJB8PJEb0e9JL8Z+J9EUid9S4klZ5R5IiNTOooegzNcCUY55U1S8qW3Vhs+D PKjMwUV1/zOU5KBfz8yQ83jeE+OIZnYIvwRwHOqVZbfNWv7LWJAAY972yk5pKhf+fBBe mujg== X-Gm-Message-State: AFq9FYICpKr2cf5buryG8Y8g+DZtAKDXDaOy6kQcu13o4FClFm9eb+2A RxonqN9dpXYm0c8PrrQ9tMGA5ElB/gUltMPTSGD3Cx3Ud9zmDx/DBpOd X-Gm-Gg: AYBFou3lqL7j5KDLRSI0mpSWRQCyxwgj4mieKmyAxKEBGaF6rxaEIFaS1d2DuMBWwaL 5pJ+l1r7pYfItTUyJ+uV2Y/zmtABvHIjLem20ypXpCO2rYqaM0JMuQqRRNWd8+e0SJxAyyayWgt gYz9dP0EK5xk32vD2YL5coYbGZa/ebeC40AuEUlX33v/fTTqRKgxV+cH53qYWPNJ0sJlvrqe9Xi zA1KavSs2ZYyqAW4lRBsHLK0W3y+R2cp0L8VcXo6c2nF8V8L/x6v5eDxyLwDw8so42yRSyWdIzP 44+8IQOBZ5/vsndcXyWIJ8Po2Q8pTIc2d2HYrePjijX3TfYmA7wBoY0DUwMFNZ+w2iIAzHOhmxx CK792lPBqHwFnyAdQQyAMYfB9l7cALuCI9ukcL4WqVk0eOfV96gGwSSfkXSDxsQ5hf8nydv62D3 G5aDyfhGtmTUSbeijSNK6C9hbQm2ErQLKdgj3I7rNpWyI0s3xoxDBzF7BZzKfHYnty9rJiWYMzh PB+yohzwTDxck7PUZqKHS2s5WQng9YjT5WDawg075tZmso= X-Received: by 2002:a05:7022:7f07:b0:194:e9e5:11b0 with SMTP id a92af1059eb24-1993506300dmr6440691c88.14.1792961509260; Sun, 25 Oct 2026 13:51:49 -0700 (PDT) From: TAKANO Mitsuhiro To: =?UTF-8?q?Alex=20Benn=C3=A9e?= Cc: qemu-devel@nongnu.org, Laurent Vivier , Helge Deller , Pierrick Bouvier Subject: [PATCH v2 1/2] linux-user: validate the fd for a zero-length readv/writev Date: Sun, 25 Oct 2026 20:51:43 +0000 Message-ID: <20261025205144.1781959-2-takano32@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261025205144.1781959-1-takano32@gmail.com> References: <87o6chn1nw.fsf@draig.linaro.org> <20261025205144.1781959-1-takano32@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::132b; envelope-from=takano32@gmail.com; helo=mail-dy1-x132b.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 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, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, 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: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1792961533422158500 Content-Type: text/plain; charset="utf-8" lock_iovec() returns NULL with errno left at zero when the vector count is zero, and the readv/writev family turns that into a successful return without ever entering the kernel. The kernel checks the file descriptor before it looks at the vector, so a read from a write-only descriptor fails with EBADF even when iovcnt is zero, while qemu-user reports success: fd =3D open("/dev/null", O_WRONLY); readv(fd, NULL, 0); /* kernel: -1 EBADF, qemu-user: 0 */ errno cannot be set in lock_iovec() instead: an empty vector is a successful no-op on a usable descriptor and only the descriptor decides, so the syscall has to be issued for the host to answer. Return a shared empty vector, which is never dereferenced and which unlock_iovec() leaves alone, so that every caller reaches the host. This covers readv, writev, preadv, pwritev, preadv2, pwritev2, vmsplice and recvmsg, which all share lock_iovec(); sendmsg already issued the syscall for a zero-length vector. Fixes: f287b2c2d4d2 ("linux-user: Perform more checks on iovec lists") Signed-off-by: TAKANO Mitsuhiro --- linux-user/syscall.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 0380691a8b..75151c1054 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -3082,6 +3082,13 @@ static void target_to_host_low_high(abi_ulong tlow, *hhigh =3D (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2; } =20 +/* + * A zero-length vector is handed to the host so that the syscall is still + * issued: the kernel validates the file descriptor before it looks at the + * vector. Nothing is ever written to it. + */ +static struct iovec empty_iovec; + static struct iovec *lock_iovec(int type, abi_ulong target_addr, abi_ulong count, int copy) { @@ -3093,8 +3100,13 @@ static struct iovec *lock_iovec(int type, abi_ulong = target_addr, bool bad_address =3D false; =20 if (count =3D=3D 0) { - errno =3D 0; - return NULL; + /* + * There is nothing to lock, but returning an error would skip the + * syscall: readv(fd, NULL, 0) on a write-only descriptor fails wi= th + * EBADF in the kernel, and succeeds when the descriptor is fine, = so + * only the host syscall can produce the right answer. + */ + return &empty_iovec; } if (count > IOV_MAX) { errno =3D EINVAL; @@ -3190,7 +3202,9 @@ static void unlock_iovec(struct iovec *vec, abi_ulong= target_addr, unlock_user(target_vec, target_addr, 0); } =20 - g_free(vec); + if (vec !=3D &empty_iovec) { + g_free(vec); + } } =20 static inline int target_to_host_sock_type(int *type) --=20 2.53.0 From nobody Mon Nov 23 04:34:22 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1792961541; cv=none; d=zohomail.com; s=zohoarc; b=V5ET+BWTGPZJjCUlP6OY5LlAKb8ikQ43MqrC7GWyA2lNTDZ6LKYrRaxwORLTnHkVtjg02Lsc9NjeYfmT4Ib5OM/jjQpla7alAt7Cv7K5xrqnM8zhLPNnQmQoIbWHbNDAmIsaapYUeD3XuFdpDoG7VFMBzRTpIeV2Rb3qBlikGbQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792961541; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=uSJ2CEYN4UhQwFZrnsW7lOqDilBREcb1D2+lCHaL0cc=; b=FCtCd2O7XGX7tnXQCBEBbDz+ZiP3IQlkik43+bnFaWacoeoKJlehspsziRCqmzNq9v/SSyp5PHGBd9xKfzLLRzdBuFsar2iew+HF/HPGLkEmATjoeu6+1ZOTKr2uQDur1DHGAQQVUxMukjuq3UyGVnO561EUZdWLVm4Ly5ZCYpY= 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 lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 179296154189644.19203284449566; Sun, 25 Oct 2026 13:52:21 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xL5C9-0000TW-Rw; Sun, 25 Oct 2026 16:51:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xL5C7-0000Sf-Li for qemu-devel@nongnu.org; Sun, 25 Oct 2026 16:51:55 -0400 Received: from mail-dy1-x1334.google.com ([2607:f8b0:4864:20::1334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xL5C6-0006AX-1S for qemu-devel@nongnu.org; Sun, 25 Oct 2026 16:51:55 -0400 Received: by mail-dy1-x1334.google.com with SMTP id 5a478bee46e88-35d0be18c71so4086325eec.1 for ; Sun, 25 Oct 2026 13:51:53 -0700 (PDT) Received: from a1-free ([161.33.196.121]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-198a2a5937dsm18680738c88.20.2026.10.25.13.51.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Oct 2026 13:51:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1792961511; x=1793566311; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=uSJ2CEYN4UhQwFZrnsW7lOqDilBREcb1D2+lCHaL0cc=; b=WATdUs4JYCDxpFXHali3AXs7onVHBEug8LoChm7gH0CWaP2xV8ZpG1F9HptNa89l33 sgGKFWSnwMr2VmtymEv4R/Ty7I5aXlijelGVeBhlptZLzlSgENXnMBCRqUEUHJv7JwsG UA9aEInF8iTCnuslCNuF1dE0XNVpE+v1FQg2WwW1ucrhjAlAFhNMwxK58xs+LywhSBfs TP13YSmf7fE455C4G2bAn2YTg7wDE6ld174bEGISmnCKBhuUJnVBuw/RE8IjJPjddkZy PM5Q9ia6Y0iIkvfP4op9toUueFPjp7d7+UkHIOXK3XiPhS2f8UvGGSVqXsp1wL7LInhC cJhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792961511; x=1793566311; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=uSJ2CEYN4UhQwFZrnsW7lOqDilBREcb1D2+lCHaL0cc=; b=dMLScLKZ688/+2JkNBHbRAGDyg3ytt6aOS0e6Ialy6P+dNwZvYXSkW/nhbKYU3uUG8 a2w7XB4aiL0QgpO9urErn38/UJlg2YxpQk+wFi6ZRYKblCabo7z9UhWBTjeRWQenxJZe N3Qi2aMNiFTZLqWUS2Asvh0l3EUKLhwypHKGcwIKW8R0vwQa6ACfVwJdsa5nAl9RNm9Y cnxpUA7DpgjYafaqtwR5qIxF6Gg/rMunDdbONKPKvZAbbUSiOtCIFvWu+joVFaHzMOoG yBAgiMAepgFx31QZEW/yYeCzFdewmLmWOGn4eWgCa6+6WwQUvxe74CMsTAicMCawsZ3Z JLcg== X-Gm-Message-State: AFq9FYKE6Iob5dTjdKAV95Wb8vlvoFkw7HXYz0R2/5GA2lOt0jveTB6j g25WHWSkA/+uI5BciP/vs6S6P1nMAuQ8AcdyS11lthJJswfBpO8DIjXcaiz/RA== X-Gm-Gg: AYBFou21CcpZP7NDSYMQRBIe4TXrtvZCh43xTE33vMYMIyDzxhH1ZNkjF2fni9APIaI 70Td05piia1z9yrjJ9GFZ2RhoyQCMXs/DTkIx3JMO4jlymK6qPUt/FXgoqT+EQuLmlzkQ1MEVRh sU7RzHVQI3n8UnJ9k3hqt5uM1gK4ohO1JiyVE6vZ48eqGcTQ0EgyIkDNQCOX3UgaNu1xjicsFwr 0y3hm9xUSB7ZdwCbpe8UVlhcCUjX0of2Hyikq1TFa0VMxaNTlEOKjn8BjtWNQi5MeJj46BBwM0d uN5WUAWe4+cF3ZAV69FYzzD3VLXKZeHKCKiwlv92mWp7EK2VMu7oEBWHCxXJaXwuVeyK3NCoHQG yRo1Q/d7n+CKy4kc6Aaicif/xg/yZi+vB2pugfs5Sji/3Kv9i6/KZsdQ1p4UkIWHDTQi0I32KOc 0lPUQ3bAQZdbEY2M6QmwnNn1rG8DESyJdQHTGiuU2LuuIMJ51A9OgXreCyw4L1/19oAcKw9b5mG VTpDOPT46Ju+RVFb34TN8T7gI2WhPcRW2IvU9lMI7IH/lI= X-Received: by 2002:a05:701b:4181:20b0:13c:f65c:d5b5 with SMTP id a92af1059eb24-199295d370amr6857385c88.3.1792961511263; Sun, 25 Oct 2026 13:51:51 -0700 (PDT) From: TAKANO Mitsuhiro To: =?UTF-8?q?Alex=20Benn=C3=A9e?= Cc: qemu-devel@nongnu.org, Laurent Vivier , Helge Deller , Pierrick Bouvier Subject: [PATCH v2 2/2] tests/tcg: check that a zero-length readv/writev validates the fd Date: Sun, 25 Oct 2026 20:51:44 +0000 Message-ID: <20261025205144.1781959-3-takano32@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261025205144.1781959-1-takano32@gmail.com> References: <87o6chn1nw.fsf@draig.linaro.org> <20261025205144.1781959-1-takano32@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1334; envelope-from=takano32@gmail.com; helo=mail-dy1-x1334.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 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, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, 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: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1792961543651158500 Content-Type: text/plain; charset="utf-8" The kernel checks the file mode before it looks at the vector, so an empty vector on a descriptor opened the wrong way has to fail with EBADF rather than report a successful no-op. Cover readv, writev and recvmsg, including the usable descriptor cases, where the call has to return 0 or the error of the socket instead. Signed-off-by: TAKANO Mitsuhiro --- .../multiarch/linux/linux-zero-len-iovec.c | 56 +++++++++++++++++++ tests/tcg/multiarch/meson.build | 1 + 2 files changed, 57 insertions(+) create mode 100644 tests/tcg/multiarch/linux/linux-zero-len-iovec.c diff --git a/tests/tcg/multiarch/linux/linux-zero-len-iovec.c b/tests/tcg/m= ultiarch/linux/linux-zero-len-iovec.c new file mode 100644 index 0000000000..79666dba88 --- /dev/null +++ b/tests/tcg/multiarch/linux/linux-zero-len-iovec.c @@ -0,0 +1,56 @@ +/* + * A zero-length readv()/writev()/recvmsg() still validates the file + * descriptor. + * + * The kernel checks the file mode before it looks at the vector, so an + * empty vector on a descriptor opened the wrong way fails with EBADF + * instead of returning 0. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +int main(void) +{ + int ro =3D open("/dev/null", O_RDONLY); + int wo =3D open("/dev/null", O_WRONLY); + struct msghdr msg =3D { 0 }; + int sv[2]; + + assert(ro >=3D 0); + assert(wo >=3D 0); + + errno =3D 0; + assert(readv(wo, NULL, 0) =3D=3D -1 && errno =3D=3D EBADF); + errno =3D 0; + assert(writev(ro, NULL, 0) =3D=3D -1 && errno =3D=3D EBADF); + + /* A closed descriptor is rejected all the same. */ + close(wo); + errno =3D 0; + assert(readv(wo, NULL, 0) =3D=3D -1 && errno =3D=3D EBADF); + + /* On a usable descriptor an empty vector is a successful no-op. */ + assert(readv(ro, NULL, 0) =3D=3D 0); + + /* recvmsg() with an empty vector reaches the socket too. */ + assert(socketpair(AF_UNIX, SOCK_DGRAM, 0, sv) =3D=3D 0); + close(sv[0]); + msg.msg_iov =3D NULL; + msg.msg_iovlen =3D 0; + errno =3D 0; + assert(recvmsg(sv[0], &msg, 0) =3D=3D -1 && errno =3D=3D EBADF); + errno =3D 0; + assert(recvmsg(sv[1], &msg, MSG_DONTWAIT) =3D=3D -1 && errno =3D=3D EA= GAIN); + + close(sv[1]); + close(ro); + return EXIT_SUCCESS; +} diff --git a/tests/tcg/multiarch/meson.build b/tests/tcg/multiarch/meson.bu= ild index e60a052700..97e2ba76dc 100644 --- a/tests/tcg/multiarch/meson.build +++ b/tests/tcg/multiarch/meson.build @@ -61,6 +61,7 @@ tests +=3D { multiarch/'linux/linux-shmat-null.c': {}, multiarch/'linux/linux-sigrtminmax.c': {}, multiarch/'linux/linux-test.c': {}, + multiarch/'linux/linux-zero-len-iovec.c': {}, multiarch/'linux/test-vma.c': {}, } =20 --=20 2.53.0