From nobody Thu Sep 24 13:39:00 2026 Received: from mail-oa1-f49.google.com (mail-oa1-f49.google.com [209.85.160.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B6693B71DA for ; Thu, 24 Sep 2026 01:52:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790214750; cv=none; b=c3d3g6Wf/6w28ICP+sDrenfk6UDAIhdazlBYz+Uosgj3zXtQ7+wmhq9IVs70pv5LE5jrDlXDzn41MBf952rGjsblmkzviBpTZrl+Nh86QRxO8U8kgHLJGiMls1wSfuBBAL/dL+63Ib1/9e2GzjkO1Lcog0cUNgQioUiJqEOBKzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790214750; c=relaxed/simple; bh=GeDgxcAAxWRswXI0bO4TJ+ASumLxQqwI+egxlfWdMac=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LCgUxU6vVXNgvTeaAx+4y1nSbzkrWPCVxsVkblVkLYPwln9PqiOotBN11IfioVA4saPwW+UXgIUxHHuIYopzJ9IJHt16mZGZ7mz5YOlFezchZrvab5K8m7ElbXW2UFMbVbv9T8m10+bloh3XmvILWgqN6MEs5CXEoa6JQnoX0jE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Pxu3Mhcr; arc=none smtp.client-ip=209.85.160.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Pxu3Mhcr" Received: by mail-oa1-f49.google.com with SMTP id 586e51a60fabf-4678d3e490bso335120fac.1 for ; Wed, 23 Sep 2026 18:52:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790214748; x=1790819548; darn=vger.kernel.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=1cN5CmufZWfppAn2JUAeeUtUy9gg/Lw5nvjqwMTeguM=; b=Pxu3MhcrFOM4FmLBFLHQZzDwsZ06cznT9HutdU9PpyBIwURqu3QlQ6Jzm1j+A6BdtD cUTwkabchRec+PVymZLVnadUCQ0mgIQ9TQf7DNxyWDOxgK9w+5rfK5c0QtKbmUjIpsmi YLRFYbD8U5AUPrH2qY9hyeYceZpPHJFFZAvzuMaKs+b8BlWDfHqDIFuXM9vUS1dxLZKy yMwUZLmexyT+bugSNyOn28nQ2xkUjFpn6ZMMwvD3eNgt5daHYgW3YTOmr0/s0N9My/lE vwElVgbLBSohXULeCzh/qY3z11kNUzyKSeSO6Y/OL3hD2fjixANr5GDMPvizte/Pmqyw mQ6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790214748; x=1790819548; 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=1cN5CmufZWfppAn2JUAeeUtUy9gg/Lw5nvjqwMTeguM=; b=Kne0XRGc/l3WcZUV8fLfsjrp8vzOaiHd6gHZQmrfIyZeXWUAjm+iaFjVv3E0d4k6AZ PTsqAbOaKRS+EwUpRojJm/cimBL4+ZuH6/pewTJ6207ZNPd1RgIOJ1yqfDk16JY76JBh m8w1tAM0fSf66P5nPQ2FdJbmVRSu9pWycsL0kM/AiJsVTX6ikqfAt3hN+PW+FCVxy7vg wKIn+8B8dubTr6vzkfUJgt4IaDIAtadWXSPNShHXbmvVVDRV1RWR41cXFI8P8FiFrEX4 /KBP/D0NJcRpwbnRHatCZ3XZAR21XYCsc3LNL3vU45x7l0aAylxysVrCweblrdJkMlOE dIcA== X-Forwarded-Encrypted: i=1; AKwUvBypFyqggqGbZrUgL1T+shk63gsiN3f6UY/+T6BVxZD5HMdodH6PPdDlQNImVyC04irKtY45LLhbGFKBdag=@vger.kernel.org X-Gm-Message-State: AFuF++ktsbh8+BrA4bU1yHr3yBO1zF4NRdJVLZOxgYNpqiQU4M1KYAJI gsyQBVal+wFkUMbei6KkWQhobiQWTAv5ldOKDoYkACbn8nL0pSbzS3pI X-Gm-Gg: AYBFou0l6scuEBPIxPSgizENzQb+0CkxKAPQVJ7GQfe530HVnLElASD0uOYInscTodK LBWh8WdoLg5Pvc/RCwXdtEufjly96OZigJTVo/MmbBXLXzcp+PTVfaab30OOiLgMdVjAzAYfe0o hRuVxl+xYAGFVrVwdZXrvUiF0fj438DBN6Heo7LKwAImWIj2faWNGX1EWmI3KbxWpD4EIEw+GqN onoZa5QIkYeLdDJ+3rgAIbszMXZtnyv5TuCPeikK7D8XtNoSm8HZ8JKKPMgmckP04DQ1WR1Zjgr oxS8kZb5kHFRyAw9gtVH2q5Prv0iOxP0eZe5gQoR6YOzWu01f/SUdqZcbTr3rGcgoqEvmVJ8ica gzZquHG2tJGVmX8clBsb1rlxlgM3/MXIpkxmnK8yyNbjfX1I/809nk2pls2oNipD7Jyv2lxrD77 Fse0pD4k6XQcQC509/y/dRqCw+7beds+AlF8Z8QRLGKj1f4uoPFJcsBPaSRXLxDasWwyL27zhOc riRf0F2oZwOspOaqCCuf/lPqdrIpuXz0yU7VEX0 X-Received: by 2002:a05:6871:3a28:b0:47b:e30b:893c with SMTP id 586e51a60fabf-491e9303f7fmr908800fac.6.1790214748117; Wed, 23 Sep 2026 18:52:28 -0700 (PDT) Received: from archlinux.lan ([136.34.156.120]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-491ed1ebc62sm1057379fac.3.2026.09.23.18.52.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Sep 2026 18:52:26 -0700 (PDT) From: Danish Khateeb To: Willy Tarreau , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Cc: Shuah Khan , Sven Schnelle , Benjamin Berg , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Danish Khateeb , stable@vger.kernel.org Subject: [PATCH 1/3] tools/nolibc: fix readdir_r() with 64-bit directory offsets Date: Wed, 23 Sep 2026 20:52:20 -0500 Message-ID: <20260924015222.31693-2-danishkhateeb03@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260924015222.31693-1-danishkhateeb03@gmail.com> References: <20260924015222.31693-1-danishkhateeb03@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" readdir_r() stores the result of _sys_lseek() in an int. Directory offsets are opaque cookies which can use all 64 bits: ext4, for instance, gives 64-bit processes 63-bit hashes, and 0x7fffffffffffffff as the offset after the last entry. Truncated to an int, such an offset is negative about half the time, and readdir_r() takes it for an error. Since commit 4ada5679f18d ("tools/nolibc/dirent: avoid errno in readdir_r"), readdir_r() fails at the first entry whose offset has bit 31 set, which on ext4 is usually one of the first few, and returns the truncated offset as the error number. Before that, only -1 counted as an error, which the last entry always hits: readdir_r() then returned errno, usually 0, without filling in the entry, so the caller got the previous entry a second time and never saw the last one. 32-bit processes get 31-bit hashes from ext4 and are not affected. Keep the offset in an off_t. Fixes: 665fa8dea90d ("tools/nolibc: add support for directory access") Cc: stable@vger.kernel.org # v6.15+ Assisted-by: LLM Signed-off-by: Danish Khateeb --- tools/include/nolibc/dirent.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/include/nolibc/dirent.h b/tools/include/nolibc/dirent.h index 2dbf4052b85a..19857369de2a 100644 --- a/tools/include/nolibc/dirent.h +++ b/tools/include/nolibc/dirent.h @@ -81,6 +81,7 @@ int readdir_r(DIR *dirp, struct dirent *entry, struct dir= ent **result) struct linux_dirent64 *ldir =3D (void *)buf; intptr_t i =3D (intptr_t)dirp; int fd, ret; + off_t off; =20 if (i >=3D 0) return EBADF; @@ -100,9 +101,9 @@ int readdir_r(DIR *dirp, struct dirent *entry, struct d= irent **result) * readdir() can only return one entry at a time. * Make sure the non-returned ones are not skipped. */ - ret =3D _sys_lseek(fd, ldir->d_off, SEEK_SET); - if (ret < 0) - return -ret; + off =3D _sys_lseek(fd, ldir->d_off, SEEK_SET); + if (off < 0) + return -off; =20 entry->d_ino =3D ldir->d_ino; /* the destination should always be big enough */ --=20 2.55.0 From nobody Thu Sep 24 13:39:00 2026 Received: from mail-oo2-f35.google.com (mail-oo2-f35.google.com [74.125.231.163]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3D173B42CB for ; Thu, 24 Sep 2026 01:52:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.231.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790214753; cv=none; b=kICqK1Ul2n0Fa2vQbLHgMQn93p2hvMprp43ePCJvNmxXYqUm5ch84h0xiDd9dbhGKAEjj+0/yTcfeGLoVd7thyrlpydH682TwRX6sxiTUrLXpNtdok4dUM4aWJ+wWczB0dWsZzA0KOC6KIHP+aJ3kya9bkVU919JiIRD5iXs6mw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790214753; c=relaxed/simple; bh=QJvpPZ/MRHEPiELxTYOq1HgO1r8v2Ya4Ct5faQQJAIk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pJlyQKorrahbMKR0Zw6jAFTM76zcEdsl/SEIyMkplzEaAuvnoO/8DzJUl+vGm4PxACUpQD2TxuPHDLjgbVErzG2j7A+Rz3VMdLlZYU2PYewaWdpUfGo8n5MRIYDRi2GUccLK25VFL/oeX28d+uFYFA7KEadLnEWtcum/WwjDzDA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Ej2fSgYv; arc=none smtp.client-ip=74.125.231.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Ej2fSgYv" Received: by mail-oo2-f35.google.com with SMTP id 006d021491bc7-6b1ae736c64so1207483eaf.2 for ; Wed, 23 Sep 2026 18:52:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790214751; x=1790819551; darn=vger.kernel.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=QL7ggWRtNIdXL5Uv/0YUEwuqXEODJnZlZOBtu7H7FUA=; b=Ej2fSgYvIDvu4K3rl3CVkvaQDYRczxMggASq2zogrJkTs68frFCWzAqixBBM8l738R TjJYPAodYu0GgyQZspw9XzldaSklmW/eVL4qSysuclAtphiGqriOyusjL0zzYU+w3Ubs QoohskuB4+OAj+Ddg8BX606HA4LVmNVXnN6T9SB5AVnD1I9Jm5YgMZ4MijkKTxp8el83 jtngD2XFjOhOXWK8INgK6UxxXhantLShGE6bPcyD6fCt8jXj2UjbMnuSekzQwbRnO3Ks 7hTKYiBNUGL1aQ8GZ7qlEn29rOI/22XMH3lU17Xq+YcSXuGtUPZnQ0nMQ5jHATcDdbB+ b6yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790214751; x=1790819551; 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=QL7ggWRtNIdXL5Uv/0YUEwuqXEODJnZlZOBtu7H7FUA=; b=uMyBdP9lv9S4S8a1GrbTjxzU342FvigMo7SxXuog4uTBSIP4vTd186wP+71tGKK0t8 bofBDRO6uIKkF0zCSBUfAqwV851Bn+VAIZNOQmaFuD3b3KMq9b1GVqQYfCNnW1MvBsa4 IuWEEBgzTAjbNa4pdDYEaFb6+UkR718mEsQgmIUIglBLk3e2E1qi37/gAhtPvgLcZsxo IBDuq1v+qLL5/7iLeNpc4oQuMsh6nhhCdGTcu7XHuVT2E9DbxRVDpG4I3lPkOjks1hn6 oAdjzwIJLIWi1LeKsLjCOvbINzH7nMdFD1wwBb2xmSwTn2s5IuhyerxsGACKE4CXWcPO R4+g== X-Forwarded-Encrypted: i=1; AKwUvBzb556Mh8VrEKAT7vrZC+EkB5wxiLu7KsiSkJy+Cq7k6G7H9HD++xANBQ9+aXooSsPa9GIBrERpoTwOL/4=@vger.kernel.org X-Gm-Message-State: AFuF++mWH2vbR6hkWsJgvKI5Xlcz+SmMWXSnk/lWTMKvRJz0be3TPAwo +cB+Exf1Ci+vp2TgdGagID9hApJYf/5QurK75a4q7KU7NdFrlybGVAzf X-Gm-Gg: AYBFou0XO4z8F32Wii6KwCbK+gVc5Y3vfDtrX1OkitcfDoqPTcICDky1Ude8qrkoJWe VNe6KJaSjpAOhrvqPv0tIGd0HdsMgmZb4YNWBqoqWLIFy3NphHS9hmvxUsJtV5x+KIQfslSKDHp J1hxUhjveudsjBE9WsDLgNxB0Vr9PWhxOBNMUF1a6h42p+VWg+E0iQ69OJot2J4FYG7ENHxsroa lKn5r9UjSKB1iraFKtzHQGYshngAZCvPJyr+NP+IpjXVGAjw5WoG8nCV6qwyO9VLk6SXjCPMOgb +E9wydvaBpRKcg0nwCY4PkScacdHluCyLMiOtuzyvu2ybQhdTQla1sgra6Pl9ydvzl1bvsoO+LB KlgVv5O8j3GZ0x78qH/8I9we90bQ3ipnrTAmLx7wm7VuhuU1JkGuIsop+kLMafhpPecc3u7Fn0Y /f0MrF+959X6bmppVYU7CnvXWnc5P9yYl8Oeq+khtP5CNZ1KPyuwS5oNJXS1LCJaIsb7KNBH+Ur pOarTrqMefQAzyhpOhmp2OdeiMGelV0emfvODL4 X-Received: by 2002:a05:6820:162b:b0:6c7:96e4:3539 with SMTP id 006d021491bc7-6d4416aaaf4mr1160260eaf.60.1790214750652; Wed, 23 Sep 2026 18:52:30 -0700 (PDT) Received: from archlinux.lan ([136.34.156.120]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-491ed1ebc62sm1057379fac.3.2026.09.23.18.52.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Sep 2026 18:52:29 -0700 (PDT) From: Danish Khateeb To: Willy Tarreau , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Cc: Shuah Khan , Sven Schnelle , Benjamin Berg , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Danish Khateeb , stable@vger.kernel.org Subject: [PATCH 2/3] tools/nolibc: fix FD_SET(), FD_CLR() and FD_ISSET() on 64-bit Date: Wed, 23 Sep 2026 20:52:21 -0500 Message-ID: <20260924015222.31693-3-danishkhateeb03@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260924015222.31693-1-danishkhateeb03@gmail.com> References: <20260924015222.31693-1-danishkhateeb03@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Since commit feaf75658783 ("nolibc: fix fd_set type"), an fd_set is an array of unsigned long, but the FD_* macros still build their masks as an int or an unsigned int: 1 << n in FD_SET(), 1U << n in FD_CLR() and FD_ISSET(), where n is the fd modulo 64 on 64-bit architectures. So: - FD_SET() of an fd with n =3D=3D 31 sets the bits for n =3D 31-63, as 1 << 31 is negative and gets sign-extended. - For n >=3D 32 the shifts are undefined. x86-64, for instance, masks the shift count, so FD_SET(40) and FD_ISSET(40) use the bit of fd 8. - FD_CLR() zero-extends ~(1U << n), so it also clears the bits for n =3D 32-63, whichever fd it is given. select() on fd 40, for instance, fails with EBADF, or watches fd 8 instead if that one is open. Build the masks from 1UL. Fixes: feaf75658783 ("nolibc: fix fd_set type") Cc: stable@vger.kernel.org # v6.2+ Assisted-by: LLM Signed-off-by: Danish Khateeb --- tools/include/nolibc/sys/select.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/include/nolibc/sys/select.h b/tools/include/nolibc/sys/s= elect.h index 6d65d9ef3d6a..f299c79939f5 100644 --- a/tools/include/nolibc/sys/select.h +++ b/tools/include/nolibc/sys/select.h @@ -26,7 +26,7 @@ typedef struct { int __fd =3D (fd); \ if (__fd >=3D 0) \ __set->fds[__fd / FD_SETIDXMASK] &=3D \ - ~(1U << (__fd & FD_SETBITMASK)); \ + ~(1UL << (__fd & FD_SETBITMASK)); \ } while (0) =20 #define FD_SET(fd, set) do { \ @@ -34,7 +34,7 @@ typedef struct { int __fd =3D (fd); \ if (__fd >=3D 0) \ __set->fds[__fd / FD_SETIDXMASK] |=3D \ - 1 << (__fd & FD_SETBITMASK); \ + 1UL << (__fd & FD_SETBITMASK); \ } while (0) =20 #define FD_ISSET(fd, set) ({ \ @@ -43,7 +43,7 @@ typedef struct { int __r =3D 0; \ if (__fd >=3D 0) \ __r =3D !!(__set->fds[__fd / FD_SETIDXMASK] & \ -1U << (__fd & FD_SETBITMASK)); \ +1UL << (__fd & FD_SETBITMASK)); \ __r; \ }) =20 --=20 2.55.0 From nobody Thu Sep 24 13:39:00 2026 Received: from mail-oa2-f12.google.com (mail-oa2-f12.google.com [74.125.231.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF21D381EBE for ; Thu, 24 Sep 2026 01:52:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.231.76 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790214754; cv=none; b=GQQYFZIUOTu6krkMv/oc3mVq4qBm6e5nxl5ZkyID1dYl7I3fDkTLcc7GS3VIyCQhdM7dBG0gzVFIOLZt3EcGUuaBFaOUz78FFgr+mdtsLKJ8kJVvGRD/tKWasukIMY2bMaOV6Iilm+p+43nRhhz59YqA/Fbzkm3PqoLdUpNMvWI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790214754; c=relaxed/simple; bh=z+bTycMow7VmGbkMGtwYHDwG1v1GhonRhqgvsTWop/g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ig6tEdr7YUZphU16S1X59WEJYzpMkrNC7x79/pGJx2Pek3SE+KD63yFLXMislHP1IxPoLHa5UmXqOeCY5FZd5XkBHHvTKWi8G9iBnh+uWq+j2HjOz6HFBSpaYkIF9bwW2Vn4xJMrdcQ0RCMQM2cVUIPLfb9mh9keKWncpzeYYi4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=n2RRJDnQ; arc=none smtp.client-ip=74.125.231.76 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="n2RRJDnQ" Received: by mail-oa2-f12.google.com with SMTP id 586e51a60fabf-466ccdd76a9so1746763fac.0 for ; Wed, 23 Sep 2026 18:52:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790214751; x=1790819551; darn=vger.kernel.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=ZNI7syD9I4Vz5XcvsfnyP81ma4EmY9tQacNXKGJFHmM=; b=n2RRJDnQkQ1LV1w6GmEvbACbj+cfRnxHa3Dmzo0a1a2qWkdRV+faueumErfN5qw+Ey JFBa6c3o2iTaCGY5l3i9Th7IbnZcw6UJf9HGrysWuP5ZzuVKnfIW5M7VIsfS5UnvndOS YxsO7oqgiLVaFADRFxkbc471ncrio2rmFBQYeAkVxAO04TcyPBYeKiiIg4mLYExnCcAo DSZZgJOGzau7HjQSSOqI0FNfhnUsCuD8yEjMzOco8fdbOyq3B2m9QeLA8CqqbQH6LX0+ 7plXy17bLyqt6RDCE7JSGlnemHvZh9+MZbEa/T9dln62EPUp3BeqIuXP1UaHgQB1L0A7 haig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790214751; x=1790819551; 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=ZNI7syD9I4Vz5XcvsfnyP81ma4EmY9tQacNXKGJFHmM=; b=Y0yMf5Th96oWyfm0QvnAcCEe3Khih1/PgOQ3Kxl4Bj5FkAjhur0xkcWBcUVa5tWBhv BFrKd6cgJDyGS5FJtmxGcJisrMP0Ktrr2pw195itjJaejVg+oeatAXddxbYGQVwjq0wj 4BBItari+pFJCOv2Yvi2+SgL8xsHTtIgV5Woi1oqy8xpfJt6BRJtGKCMFT4xi7Gye00o 4gDKYcRrZL/Iw/tu2fEykgu+2m0ze1hlhPoKmZOeqH6HFvsy05s5TWkFKD9SfpaTF90X fIdBh29cXVLmnXHwmpJ1aQzk385MRxaqtK1VoWgXrDL6QrNEbifdPBeNhum74lGEhD2A NjNw== X-Forwarded-Encrypted: i=1; AKwUvBy5qgvtZlDOgrvm88kE84pdpHP2UhXCLzfZdIAm7etyJrLWNkzlMe63cBAt4IKI7eIPZokVgzficRGFc/c=@vger.kernel.org X-Gm-Message-State: AFuF++lSEKuPIGwR79a8mA5XZFJCXx5zS8hn46twNvLh2z3KrU1F4c7u 5r0dAcArKraLBaM3vc4ZWpsuSRX5EmN7cL06vNibLZ8neXe6vn6M5Z1C5Kfa8ZYy X-Gm-Gg: AYBFou2pWJsSYQq/sGVyEwEFVzlcHQJcnqfAgm5BSm/RU0difXaKtE+zBtJpAqytwp5 Vums+Os7zKcoRAmy6DGd4IoWBZFwRQQzGNH4EDtzh0sOw8U2Gn2/zcrCc9C3g1E7b12xi9DNHEU 2WFEsmGbIdrU5ilo7xy507Qj8mvKwJYvdVOXllcJ5W/kSlLBVYF6W3zl2cXNBRXZ3nZcHVmKHgj 3578eqXzLTq8HJ+J1nNTJCpWcOvEhsKj3MTnQIOuJhJbCvwRat1vauaJ1Q+vXKLrwkEHGhsyg8y CtU5NjKWuYlbBGHwE4rzrpyE+nljYV894jHa23PZqay1aqXO7fx1t2xofXafSyI50TZrvu33JSI ZSb+MOTgno+52YtXMNdEGi87KrcI1w1yKMlQOb8qugC5vmWWv3XKfeiMMVwkfjalNx3pQ1yoXfP v3evuIO7HGH3Pz2TfrxSo9rEW1FWLL3z4LnbOhlLlDQJSdTUaYEbdnpHaJpG7oHTDpw30/BDock U6D/BreCKKj1xdQWiq62K59gVyFj9S5Iv2bv44G X-Received: by 2002:a05:6870:45a8:b0:47b:e948:4d6f with SMTP id 586e51a60fabf-491e58d0f81mr1125353fac.17.1790214751519; Wed, 23 Sep 2026 18:52:31 -0700 (PDT) Received: from archlinux.lan ([136.34.156.120]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-491ed1ebc62sm1057379fac.3.2026.09.23.18.52.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Sep 2026 18:52:31 -0700 (PDT) From: Danish Khateeb To: Willy Tarreau , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Cc: Shuah Khan , Sven Schnelle , Benjamin Berg , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Danish Khateeb Subject: [PATCH 3/3] selftests/nolibc: test the FD_* macros and select() on a high fd Date: Wed, 23 Sep 2026 20:52:22 -0500 Message-ID: <20260924015222.31693-4-danishkhateeb03@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260924015222.31693-1-danishkhateeb03@gmail.com> References: <20260924015222.31693-1-danishkhateeb03@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The only select() test that passes an fd_set uses fd 1, so nothing noticed that the FD_* macros used the wrong bits for fds 31-63 of each word on 64-bit architectures. Add a test that FD_SET() and FD_CLR() change exactly one fd, for every fd below FD_SETSIZE, and one that select() reports a readable pipe duplicated to fd 40, which also checks that the macros agree with the kernel on the layout of the set. Assisted-by: LLM Signed-off-by: Danish Khateeb --- tools/testing/selftests/nolibc/nolibc-test.c | 54 ++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index f62183a46205..d9a8bfd4b21b 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1369,6 +1369,58 @@ int test_pipe(void) return !!memcmp(buf, msg, len); } =20 +int test_fd_set(void) +{ + fd_set fds; + int fd, i; + + for (fd =3D 0; fd < FD_SETSIZE; fd++) { + /* FD_SET() must set this fd and no other */ + FD_ZERO(&fds); + FD_SET(fd, &fds); + for (i =3D 0; i < FD_SETSIZE; i++) + if (!!FD_ISSET(i, &fds) !=3D (i =3D=3D fd)) + return 1; + + /* FD_CLR() must clear this fd and no other */ + for (i =3D 0; i < FD_SETSIZE; i++) + FD_SET(i, &fds); + FD_CLR(fd, &fds); + for (i =3D 0; i < FD_SETSIZE; i++) + if (!!FD_ISSET(i, &fds) !=3D (i !=3D fd)) + return 1; + } + + return 0; +} + +int test_select_high_fd(void) +{ + struct timeval tv =3D { 0 }; + int pipefd[2], fd, ret =3D 1; + fd_set fds; + + if (pipe(pipefd) =3D=3D -1) + return 1; + + /* fd 40 is in the upper half of a 64-bit fd_set word */ + fd =3D dup2(pipefd[0], 40); + if (fd =3D=3D -1) + goto out; + + write(pipefd[1], "x", 1); + FD_ZERO(&fds); + FD_SET(fd, &fds); + if (select(fd + 1, &fds, NULL, NULL, &tv) =3D=3D 1 && FD_ISSET(fd, &fds)) + ret =3D 0; + + close(fd); +out: + close(pipefd[0]); + close(pipefd[1]); + return ret; +} + int test_rlimit(void) { struct rlimit rlim =3D { @@ -1711,6 +1763,7 @@ int run_syscall(int min, int max) CASE_TEST(execve_root); EXPECT_SYSER(1, execve("/", (char*[]){ [0]= =3D (char []){"/"}, [1] =3D NULL }, NULL), -1, EACCES); break; CASE_TEST(fchdir_stdin); EXPECT_SYSER(1, fchdir(STDIN_FILENO), -1, = ENOTDIR); break; CASE_TEST(fchdir_badfd); EXPECT_SYSER(1, fchdir(-1), -1, EBADF); br= eak; + CASE_TEST(fd_set); EXPECT_ZR(1, test_fd_set()); break; CASE_TEST(fdopendir_notdir); EXPECT_SYSER(1, (uintptr_t)fdopendir(STDIN= _FILENO), (uintptr_t)NULL, ENOTDIR); break; CASE_TEST(file_stream); EXPECT_SYSZR(1, test_file_stream()); break; CASE_TEST(file_stream_wsr); EXPECT_SYSZR(1, test_file_stream_wsr()); b= reak; @@ -1753,6 +1806,7 @@ int run_syscall(int min, int max) CASE_TEST(select_null); EXPECT_SYSZR(1, ({ struct timeval tv =3D {= 0 }; select(0, NULL, NULL, NULL, &tv); })); break; CASE_TEST(select_stdout); EXPECT_SYSNE(1, ({ fd_set fds; FD_ZERO(&fd= s); FD_SET(1, &fds); select(2, NULL, &fds, NULL, NULL); }), -1); break; CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL,= NULL, 0), -1, EFAULT); break; + CASE_TEST(select_high_fd); EXPECT_SYSZR(1, test_select_high_fd()); br= eak; CASE_TEST(sendfile); EXPECT_SYSZR(1, test_sendfile()); break; CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &s= tat_buf), -1, ENOENT); break; CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1,= EFAULT); break; --=20 2.55.0