From nobody Wed Apr 1 08:15:39 2026 Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) (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 59E2A41C71 for ; Wed, 1 Apr 2026 00:28:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.193 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775003310; cv=none; b=O+7k2alfrblb2b4ovp1Ufq+UJjvzqH7fk6LeUgkJkBel/SNlbn/sUbYhpQUbz54DJFJscQB/85/y4nXSMTnNaczsjdXb9zmn8wddx5IBFpUCdC+y9vtNEfrb2qMgktUZ0MrGeVA/qJprhpNzerO91m/QFHRMD8IV8pK2ectH2i4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775003310; c=relaxed/simple; bh=nt2OPfJr2A+6voG3mvjcDolBFXkCYdVhtEbTUQY3tSw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=g3XgWkrL1tR2jn+dXUzhTQrw891NE3hSKUv8s7Nnf1VyDZ38rrx04Ejbit+RQJb8M8k7JWr0ZAmTsS5fLhosx+/V5IlNlYssgpxOFCDIRq5noLvXeTZKczeaULRn3cHKJBCDVnoX5jDc9reQvBkKfxjPfdehTuYnYk53USlHTTA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=amutable.com; spf=pass smtp.mailfrom=amutable.com; dkim=pass (2048-bit key) header.d=amutable-com.20230601.gappssmtp.com header.i=@amutable-com.20230601.gappssmtp.com header.b=Yhw9p1cd; arc=none smtp.client-ip=209.85.214.193 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=amutable.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amutable.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amutable-com.20230601.gappssmtp.com header.i=@amutable-com.20230601.gappssmtp.com header.b="Yhw9p1cd" Received: by mail-pl1-f193.google.com with SMTP id d9443c01a7336-2b2503753efso23599075ad.0 for ; Tue, 31 Mar 2026 17:28:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amutable-com.20230601.gappssmtp.com; s=20230601; t=1775003308; x=1775608108; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=V8tetMUp6n642BPcIc1AXM/henp9pQA12IUGi6Zqs5U=; b=Yhw9p1cdCVXmygXl8tgkFTYuTC84BEiFVHpnNt5KQ1HB/DA09FV0u5CjZoDa+I0dK5 is2WMgr3dvu+PEkjx/ogcnS+n1ayGIipYeH4Dqy7/ud8M4ObzW04omPp8qiYFOF7XyM5 pf0xVxi+jTzEyFDHOUlinGgrU8OVWXH7gPYa3dzUtg7rJSMZZ73Vj+SokaPV0L23MArK muGccG/hmdj5CnZHx70KoVPSIKgkQozLiJ60I1VISGqVKo0SvMHFqhIYxTF4ZSQt7GxC RmGbuYM5OTjviJyZIvzsaRtHK8TcNpswPGx9hVKdHmuq5EXnvA70sh5xcLfCi0LCeUOz ZsuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775003308; x=1775608108; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=V8tetMUp6n642BPcIc1AXM/henp9pQA12IUGi6Zqs5U=; b=HUCirZkhjMRjEuHXACqHIrBceP7ynTXt0OtzrRlIHztfb/h+w2kfvMljjyIK1KbBT4 mD/xaM68H/n2v9DBGvxiI8d6YfPYr1ayTH+9ycYWvWG9Jw2WpB5YEbacSERSdIgYLnE1 prLbzuO3V+SHU4ve4l1nHWYNBLQ0fgpPxRZZ/HkXySP0q3pRP4P5NKRAdVFDiD8TKk8g krpnhyFky2IyJdyXZNyRuWwHtQaswyYSaipUJDhxQ/bM8SDX1oPqpC3o4L1BD8o//4Rr NpWiollPilavFqy9VL507O5WFbaMPEVwMzetqvEVJ4Sd5OdNJa1sIpmFpBXKil3q4KTZ 38vA== X-Forwarded-Encrypted: i=1; AJvYcCWkPLe4RtghsF0rU0Mv79nezVYXqvyeLfOWhbCx53ALb/3vqYsoE6bKSSCys18OXht5tY89Ksgy+iJfuTE=@vger.kernel.org X-Gm-Message-State: AOJu0YzxRPaGn5HOfL4Wyp9PP01vE/yGqbKeb/5xAM/ylALU3ZP12ZQf sayGw4iwf73OzZCpc88REmLVXSXbUalhUvtaYtgZhS+n5IH/saZjlp6pFW+fvlkG4XIMvMZyrfD NtFTPuaVRoxJqPA== X-Gm-Gg: ATEYQzwweiRn3SyOkMyWc6JwSBc1WfH5lR88cXwOIyehNHd/ud3HgnQ+x4dxyMliT1y 3Kn5TiOTk6pWmhdOsUT945vaGgeG3cJmzo5XKLJDSuzddlQs3rb8wueyOzAbDrtgxbl+i0dh+6X 4FuCSAf86oOwwLWY9Z2VK1chdLFhYYJ5oUL9vfMSyNZBx/aCyLwmSn4+o5fGGsciSADCjHEOMtb JcB2DK1fx+S5AnZK1kc2+Iwf9s75dKF4s7fOkjRGPa2FDmxcFTdXGZtdSJix3W/esrj/2QZEDBX sbeGO/m5hsqyd0aMRFI8afiZuRbGS9Jnd5OfI3I0VFu5NC8XApxYB4oS9S573dro8limPCoUfuz 87KCYgUSxw8MZUappNS9c5c8tGBo59N/rs0dHmxC0Agebr3rFmvDs14u1+4JOG3W9EEnLsn/IH8 LAXa+XkecZ/o2Q05HvIo0iYhEIjThpL0APhqowX4AyCS3HQRBz5Y++2wD8n+H1pVF1t+k8EGUOr /CJfPQqVwZJfEhCVol7THFn9Q== X-Received: by 2002:a17:902:db06:b0:2b2:4d36:7aa with SMTP id d9443c01a7336-2b269cee6d6mr12173595ad.35.1775003307800; Tue, 31 Mar 2026 17:28:27 -0700 (PDT) Received: from thesius.dot.cyphar.com (2403-5809-a2fc-2-a90-9fd7-aedd-d405.ip6.aussiebb.net. [2403:5809:a2fc:2:a90:9fd7:aedd:d405]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b24264264fsm157057825ad.2.2026.03.31.17.28.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Mar 2026 17:28:27 -0700 (PDT) From: Aleksa Sarai Date: Wed, 01 Apr 2026 11:28:02 +1100 Subject: [PATCH v2 1/4] selftests: move openat2 tests to selftests/filesystems/ Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260401-openat2-selftests-kunit-v2-1-ad153a07da0c@amutable.com> References: <20260401-openat2-selftests-kunit-v2-0-ad153a07da0c@amutable.com> In-Reply-To: <20260401-openat2-selftests-kunit-v2-0-ad153a07da0c@amutable.com> To: Shuah Khan Cc: Christian Brauner , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Aleksa Sarai , Aleksa Sarai X-Mailer: b4 0.15.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=3236; i=aleksa@amutable.com; h=from:subject:message-id; bh=nt2OPfJr2A+6voG3mvjcDolBFXkCYdVhtEbTUQY3tSw=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMWSeSVv8bStjLYftrEeNjZlM/PbZv4Lz7nHO6usslHM7k 5bUUizaMZGFQYyLwVJMkWWbn2fopvmLryR/WskGM4eVCWSItEgDAwMDAwsDX25iXqmRjpGeqbah nqGRjpGOEQMXpwBM9a9+hv+xj+o6XU/PqS+6Z73BaddJszPveN9P3MDNnJF8Zoao6B5mRoZHfW2 nnoovaOPav/e1oMjWH8GLf393Yn4WU9vr+8k524wPAA== X-Developer-Key: i=aleksa@amutable.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 These tests really should've always belonged there, doubly so now that they include a lot of other generic filesystem-related tests. Suggested-by: Christian Brauner Signed-off-by: Aleksa Sarai --- tools/testing/selftests/{ =3D> filesystems}/openat2/.gitignore |= 0 tools/testing/selftests/{ =3D> filesystems}/openat2/Makefile |= 3 ++- tools/testing/selftests/{ =3D> filesystems}/openat2/helpers.c |= 0 tools/testing/selftests/{ =3D> filesystems}/openat2/helpers.h |= 0 tools/testing/selftests/{ =3D> filesystems}/openat2/openat2_test.c |= 0 tools/testing/selftests/{ =3D> filesystems}/openat2/rename_attack_test.c |= 0 tools/testing/selftests/{ =3D> filesystems}/openat2/resolve_test.c |= 0 7 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/openat2/.gitignore b/tools/testing/sel= ftests/filesystems/openat2/.gitignore similarity index 100% rename from tools/testing/selftests/openat2/.gitignore rename to tools/testing/selftests/filesystems/openat2/.gitignore diff --git a/tools/testing/selftests/openat2/Makefile b/tools/testing/selft= ests/filesystems/openat2/Makefile similarity index 91% rename from tools/testing/selftests/openat2/Makefile rename to tools/testing/selftests/filesystems/openat2/Makefile index 185dc76ebb5f..f36dedccedb6 100644 --- a/tools/testing/selftests/openat2/Makefile +++ b/tools/testing/selftests/filesystems/openat2/Makefile @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-or-later =20 +CFLAGS +=3D $(KHDR_INCLUDES) CFLAGS +=3D -Wall -O2 -g -fsanitize=3Daddress -fsanitize=3Dundefined TEST_GEN_PROGS :=3D openat2_test resolve_test rename_attack_test =20 @@ -13,6 +14,6 @@ endif =20 LOCAL_HDRS +=3D helpers.h =20 -include ../lib.mk +include ../../lib.mk =20 $(TEST_GEN_PROGS): helpers.c diff --git a/tools/testing/selftests/openat2/helpers.c b/tools/testing/self= tests/filesystems/openat2/helpers.c similarity index 100% rename from tools/testing/selftests/openat2/helpers.c rename to tools/testing/selftests/filesystems/openat2/helpers.c diff --git a/tools/testing/selftests/openat2/helpers.h b/tools/testing/self= tests/filesystems/openat2/helpers.h similarity index 100% rename from tools/testing/selftests/openat2/helpers.h rename to tools/testing/selftests/filesystems/openat2/helpers.h diff --git a/tools/testing/selftests/openat2/openat2_test.c b/tools/testing= /selftests/filesystems/openat2/openat2_test.c similarity index 100% rename from tools/testing/selftests/openat2/openat2_test.c rename to tools/testing/selftests/filesystems/openat2/openat2_test.c diff --git a/tools/testing/selftests/openat2/rename_attack_test.c b/tools/t= esting/selftests/filesystems/openat2/rename_attack_test.c similarity index 100% rename from tools/testing/selftests/openat2/rename_attack_test.c rename to tools/testing/selftests/filesystems/openat2/rename_attack_test.c diff --git a/tools/testing/selftests/openat2/resolve_test.c b/tools/testing= /selftests/filesystems/openat2/resolve_test.c similarity index 100% rename from tools/testing/selftests/openat2/resolve_test.c rename to tools/testing/selftests/filesystems/openat2/resolve_test.c --=20 2.53.0 From nobody Wed Apr 1 08:15:39 2026 Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) (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 1C6F619067C for ; Wed, 1 Apr 2026 00:28:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.195 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775003313; cv=none; b=uCUtQH+bcJ5vG54aBYmHrs7DEVzASBCGQg9a7mkVOSFt2sBSreWVhalE6nOYkYxldKzwkdufM7DltQTranWOb0w/8E9i5AIFOrAFiD8IBsMoDikQfHWTidTSwp976cFN50mbw+tbQlNa8RplkzeusCfTCJJuF23SXv9ObSpb7mQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775003313; c=relaxed/simple; bh=/SkZnf9rgHNSdrHLq0MfYdlt06Qrh2Su8iA0QXWXAkA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=D2gFPMGQiOUoMWdbj/lLEHADgS87QiYTWj7XKZBTAv/MOCaPKCRN2BB+Y5dj9CPuohhGN+KW/RQ9JhLSkevft7SiV7Kdh84wDRDiShbykJJUQE7Hn8Zho/ZKodH7K6GEDrhhjPZoj1YzP7s60r6gUMjR+nvxHmhCl5ZQIYX6n9I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=amutable.com; spf=pass smtp.mailfrom=amutable.com; dkim=pass (2048-bit key) header.d=amutable-com.20230601.gappssmtp.com header.i=@amutable-com.20230601.gappssmtp.com header.b=ECS2W3Px; arc=none smtp.client-ip=209.85.214.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=amutable.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amutable.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amutable-com.20230601.gappssmtp.com header.i=@amutable-com.20230601.gappssmtp.com header.b="ECS2W3Px" Received: by mail-pl1-f195.google.com with SMTP id d9443c01a7336-2ad9516a653so27571205ad.0 for ; Tue, 31 Mar 2026 17:28:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amutable-com.20230601.gappssmtp.com; s=20230601; t=1775003311; x=1775608111; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=R+MmmkffR4AjWlUAh1Y8qnRVcsjhTa+f38j2owrthlg=; b=ECS2W3Pxds9+0eScQYDqj16+IgX/MrqXFR7n0i23oOt4srGFf43gigjTFmw1mVHw4e weh4QjxD97QdUxNq9Ydtd3x4frtJ1AnzoH8uve6GphWB2KKhNlXb1kKTPayqD3PoQT/u PsLVut5L+bilByZrPsdPnGMHzE5cKvOwp9+UpPrL4Fiu4cG0NOpXE6uGVADJZcMgXVXl H560KxV9wjl7ivVIGo1oYGQLrdbjChTJtLWo6JTH8w6yZaN9CAi0U4WGSYfYtdeObDP0 pyqnJvGK46emdlaIfnBVlfV9qLEt5mbuZTRDzeQjMHF33C8mP21A7nRQysIRxLIVx32m T9QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775003311; x=1775608111; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=R+MmmkffR4AjWlUAh1Y8qnRVcsjhTa+f38j2owrthlg=; b=j6SJbcflAHABlaGcTcsGDuJvdsKlyZLcjbJFxpivNIkzvgd391FCFP6ldEvTXCAUP3 Iaw6if2G7+go6Ehj3y79QTS9kee+HdC3C+rL08fNl+hjjIIpdZi7CG+Kq6auXjnM5T3H zpyYocN+pweLO50d4Qyf8bt2iOY/viUTcT38vCOIWgeHdSfXMaUOmKvbpcblQVHog6I4 vZ9kgK4e7k326C5+KkzB+FzWvF1oA6BDc61tYviESbems/xkqhmLVR2yqw8Of1Ib+oBH Lrr5qVom06xW0JXScEcWpkYO5FXGPRLZAgOC9l1zBwInuqpkMc0YScZ/QcMSbC/4LC+F rurw== X-Forwarded-Encrypted: i=1; AJvYcCU/UkM+DdJTEI79tdMCiGknb6gcgxDPxEnI5G+w596ZkMENYiRAqlo70l0Cl627aMmvsOEoefX49N/QSdU=@vger.kernel.org X-Gm-Message-State: AOJu0YzpdJ1JzRX4f14goFhmEQm69vokoYyXzuoFI1BzxNcZR49+2n5r G9WKya4IRTb33GZFhNHRhp7bkcMQGOnbXYNX96GgJyBEYrItjXuQxpDIm++lCwmFA54r X-Gm-Gg: ATEYQzxqze4uS8UOHrSQz//A9YEFua0iYzc6tRIGhP1oOcWB+ZcyS0PNTLpt60tEy6K sgNK054jyUsDfd3+jY/AoR5ugDNoSRawPbcML882RmriYcL6UvKG6yLzQK0+NV3HnAC+/YkCfS5 A9t/p4XSKd367fPpGT9dNeOZm0OlKlNpINKqgXgBdk17ZOYpVGtC784QJWRSL/lOkNF/pl5Nrp3 ca5Y7TfiiCl3NK5xbbQRmwmO+0PAGD1lm0ZrLNeK7lvgMRWpZWAiaGFTrMfgdwLQQwuwczujWv2 cOnRKbnyhnPxo4dxHfdwCgLlIQQZtk8GigRbSb37L9mL7Bv4k5ZOMdNsUzSRDvPTfkbN/QQbCUW qWOULNqtpEScLcW77rhwoxhF8x6MuMjcFMfmWPspU2ATa0SNSph5yJM4JgVBLogF10VRRssO0Ui yt4F3CHPBHAOmavvIox/zEzpYz82xMhEHza1RxT9eylpY4WgK/Y3/7iT1QFhs4wMjMoCnLcYyCU hmVeNaYdcg71RXGAtMc0gQirQ== X-Received: by 2002:a17:903:904:b0:2ae:57e6:616c with SMTP id d9443c01a7336-2b269a80ba6mr12616225ad.3.1775003311440; Tue, 31 Mar 2026 17:28:31 -0700 (PDT) Received: from thesius.dot.cyphar.com (2403-5809-a2fc-2-a90-9fd7-aedd-d405.ip6.aussiebb.net. [2403:5809:a2fc:2:a90:9fd7:aedd:d405]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b24264264fsm157057825ad.2.2026.03.31.17.28.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Mar 2026 17:28:31 -0700 (PDT) From: Aleksa Sarai Date: Wed, 01 Apr 2026 11:28:03 +1100 Subject: [PATCH v2 2/4] selftests: openat2: move helpers to header Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260401-openat2-selftests-kunit-v2-2-ad153a07da0c@amutable.com> References: <20260401-openat2-selftests-kunit-v2-0-ad153a07da0c@amutable.com> In-Reply-To: <20260401-openat2-selftests-kunit-v2-0-ad153a07da0c@amutable.com> To: Shuah Khan Cc: Christian Brauner , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Aleksa Sarai , Aleksa Sarai X-Mailer: b4 0.15.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=8185; i=aleksa@amutable.com; h=from:subject:message-id; bh=/SkZnf9rgHNSdrHLq0MfYdlt06Qrh2Su8iA0QXWXAkA=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMWSeSVv86oFI7KKv1z147GpuBJfLq961X5zVucEyd96j1 N+W69lWdUxkYRDjYrAUU2TZ5ucZumn+4ivJn1aywcxhZQIZIi3SwMDAwMDCwJebmFdqpGOkZ6pt qGdopGOkY8TAxSkAU13FzPDPdv29e3onpm2+FPX7WmNe7dbMxc1WG8vedPevcH3Tb+WxgeGfluf nPTXZW46nJRzJ/PxH6KCumuNRU7sXlR7VApek1XMYAQ== X-Developer-Key: i=aleksa@amutable.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 This is a bit ugly, but in the next patch we will move to using kselftest_harness.h -- which doesn't play well with being included in multiple compilation units due to duplicate function definitions. Not including kselftest_harness.h would let us avoid this patch, but the helpers will need include kselftest_harness.h in order to switch to TH_LOG. Signed-off-by: Aleksa Sarai --- .../testing/selftests/filesystems/openat2/Makefile | 2 - .../selftests/filesystems/openat2/helpers.c | 109 ------------------ .../selftests/filesystems/openat2/helpers.h | 125 +++++++++++++++++= +--- 3 files changed, 107 insertions(+), 129 deletions(-) diff --git a/tools/testing/selftests/filesystems/openat2/Makefile b/tools/t= esting/selftests/filesystems/openat2/Makefile index f36dedccedb6..7736e37b7986 100644 --- a/tools/testing/selftests/filesystems/openat2/Makefile +++ b/tools/testing/selftests/filesystems/openat2/Makefile @@ -15,5 +15,3 @@ endif LOCAL_HDRS +=3D helpers.h =20 include ../../lib.mk - -$(TEST_GEN_PROGS): helpers.c diff --git a/tools/testing/selftests/filesystems/openat2/helpers.c b/tools/= testing/selftests/filesystems/openat2/helpers.c deleted file mode 100644 index 5074681ffdc9..000000000000 --- a/tools/testing/selftests/filesystems/openat2/helpers.c +++ /dev/null @@ -1,109 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -/* - * Author: Aleksa Sarai - * Copyright (C) 2018-2019 SUSE LLC. - */ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include - -#include "helpers.h" - -bool needs_openat2(const struct open_how *how) -{ - return how->resolve !=3D 0; -} - -int raw_openat2(int dfd, const char *path, void *how, size_t size) -{ - int ret =3D syscall(__NR_openat2, dfd, path, how, size); - return ret >=3D 0 ? ret : -errno; -} - -int sys_openat2(int dfd, const char *path, struct open_how *how) -{ - return raw_openat2(dfd, path, how, sizeof(*how)); -} - -int sys_openat(int dfd, const char *path, struct open_how *how) -{ - int ret =3D openat(dfd, path, how->flags, how->mode); - return ret >=3D 0 ? ret : -errno; -} - -int sys_renameat2(int olddirfd, const char *oldpath, - int newdirfd, const char *newpath, unsigned int flags) -{ - int ret =3D syscall(__NR_renameat2, olddirfd, oldpath, - newdirfd, newpath, flags); - return ret >=3D 0 ? ret : -errno; -} - -int touchat(int dfd, const char *path) -{ - int fd =3D openat(dfd, path, O_CREAT, 0700); - if (fd >=3D 0) - close(fd); - return fd; -} - -char *fdreadlink(int fd) -{ - char *target, *tmp; - - E_asprintf(&tmp, "/proc/self/fd/%d", fd); - - target =3D malloc(PATH_MAX); - if (!target) - ksft_exit_fail_msg("fdreadlink: malloc failed\n"); - memset(target, 0, PATH_MAX); - - E_readlink(tmp, target, PATH_MAX); - free(tmp); - return target; -} - -bool fdequal(int fd, int dfd, const char *path) -{ - char *fdpath, *dfdpath, *other; - bool cmp; - - fdpath =3D fdreadlink(fd); - dfdpath =3D fdreadlink(dfd); - - if (!path) - E_asprintf(&other, "%s", dfdpath); - else if (*path =3D=3D '/') - E_asprintf(&other, "%s", path); - else - E_asprintf(&other, "%s/%s", dfdpath, path); - - cmp =3D !strcmp(fdpath, other); - - free(fdpath); - free(dfdpath); - free(other); - return cmp; -} - -bool openat2_supported =3D false; - -void __attribute__((constructor)) init(void) -{ - struct open_how how =3D {}; - int fd; - - BUILD_BUG_ON(sizeof(struct open_how) !=3D OPEN_HOW_SIZE_VER0); - - /* Check openat2(2) support. */ - fd =3D sys_openat2(AT_FDCWD, ".", &how); - openat2_supported =3D (fd >=3D 0); - - if (fd >=3D 0) - close(fd); -} diff --git a/tools/testing/selftests/filesystems/openat2/helpers.h b/tools/= testing/selftests/filesystems/openat2/helpers.h index 510e60602511..975de513af86 100644 --- a/tools/testing/selftests/filesystems/openat2/helpers.h +++ b/tools/testing/selftests/filesystems/openat2/helpers.h @@ -2,6 +2,7 @@ /* * Author: Aleksa Sarai * Copyright (C) 2018-2019 SUSE LLC. + * Copyright (C) 2026 Amutable GmbH */ =20 #ifndef __RESOLVEAT_H__ @@ -11,19 +12,14 @@ #include #include #include +#include #include +#include #include "kselftest.h" =20 #define ARRAY_LEN(X) (sizeof (X) / sizeof (*(X))) #define BUILD_BUG_ON(e) ((void)(sizeof(struct { int:(-!!(e)); }))) =20 -#ifndef SYS_openat2 -#ifndef __NR_openat2 -#define __NR_openat2 437 -#endif /* __NR_openat2 */ -#define SYS_openat2 __NR_openat2 -#endif /* SYS_openat2 */ - /* * Arguments for how openat2(2) should open the target path. If @resolve is * zero, then openat2(2) operates very similarly to openat(2). @@ -45,8 +41,6 @@ struct open_how { #define OPEN_HOW_SIZE_VER0 24 /* sizeof first published struct */ #define OPEN_HOW_SIZE_LATEST OPEN_HOW_SIZE_VER0 =20 -bool needs_openat2(const struct open_how *how); - #ifndef RESOLVE_IN_ROOT /* how->resolve flags for openat2(2). */ #define RESOLVE_NO_XDEV 0x01 /* Block mount-point crossings @@ -93,16 +87,111 @@ bool needs_openat2(const struct open_how *how); __FILE__, __LINE__, #expr, ##__VA_ARGS__); \ } while (0) =20 -int raw_openat2(int dfd, const char *path, void *how, size_t size); -int sys_openat2(int dfd, const char *path, struct open_how *how); -int sys_openat(int dfd, const char *path, struct open_how *how); -int sys_renameat2(int olddirfd, const char *oldpath, - int newdirfd, const char *newpath, unsigned int flags); +__maybe_unused +static bool needs_openat2(const struct open_how *how) +{ + return how->resolve !=3D 0; +} + +__maybe_unused +static int raw_openat2(int dfd, const char *path, void *how, size_t size) +{ + int ret =3D syscall(__NR_openat2, dfd, path, how, size); + + return ret >=3D 0 ? ret : -errno; +} + +__maybe_unused +static int sys_openat2(int dfd, const char *path, struct open_how *how) +{ + return raw_openat2(dfd, path, how, sizeof(*how)); +} + +__maybe_unused +static int sys_openat(int dfd, const char *path, struct open_how *how) +{ + int ret =3D openat(dfd, path, how->flags, how->mode); + + return ret >=3D 0 ? ret : -errno; +} + +__maybe_unused +static int sys_renameat2(int olddirfd, const char *oldpath, + int newdirfd, const char *newpath, unsigned int flags) +{ + int ret =3D syscall(__NR_renameat2, olddirfd, oldpath, + newdirfd, newpath, flags); + + return ret >=3D 0 ? ret : -errno; +} + +__maybe_unused +static int touchat(int dfd, const char *path) +{ + int fd =3D openat(dfd, path, O_CREAT, 0700); + + if (fd >=3D 0) + close(fd); + return fd; +} + +__maybe_unused +static char *fdreadlink(int fd) +{ + char *target, *tmp; + + E_asprintf(&tmp, "/proc/self/fd/%d", fd); + + target =3D malloc(PATH_MAX); + if (!target) + ksft_exit_fail_msg("fdreadlink: malloc failed\n"); + memset(target, 0, PATH_MAX); + + E_readlink(tmp, target, PATH_MAX); + free(tmp); + return target; +} + +__maybe_unused +static bool fdequal(int fd, int dfd, const char *path) +{ + char *fdpath, *dfdpath, *other; + bool cmp; + + fdpath =3D fdreadlink(fd); + dfdpath =3D fdreadlink(dfd); + + if (!path) + E_asprintf(&other, "%s", dfdpath); + else if (*path =3D=3D '/') + E_asprintf(&other, "%s", path); + else + E_asprintf(&other, "%s/%s", dfdpath, path); + + cmp =3D !strcmp(fdpath, other); + + free(fdpath); + free(dfdpath); + free(other); + return cmp; +} + +static bool openat2_supported =3D false; + +__attribute__((constructor)) +static void __detect_openat2_supported(void) +{ + struct open_how how =3D {}; + int fd; + + BUILD_BUG_ON(sizeof(struct open_how) !=3D OPEN_HOW_SIZE_VER0); =20 -int touchat(int dfd, const char *path); -char *fdreadlink(int fd); -bool fdequal(int fd, int dfd, const char *path); + /* Check openat2(2) support. */ + fd =3D sys_openat2(AT_FDCWD, ".", &how); + openat2_supported =3D (fd >=3D 0); =20 -extern bool openat2_supported; + if (fd >=3D 0) + close(fd); +} =20 #endif /* __RESOLVEAT_H__ */ --=20 2.53.0 From nobody Wed Apr 1 08:15:39 2026 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 901E615539A for ; Wed, 1 Apr 2026 00:28:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775003316; cv=none; b=Z97mpUUGt3y23dUQPUOxluQ25s1zxWshTgbtEJEGoSZvxF4spWqD0enuMyvyGY5EN2lR922iBbT0iDIhrG4dZtOZfArw47TQeaLBNH8ytGJSiIae63pXQ1VFKLYWk9bzSDnZ9k8H77UbL1mTOsM21lHz+2PldOgwY6BcwWETdvw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775003316; c=relaxed/simple; bh=PpofUhHFeqNmF2EPmF0nTEHj8cWFrIuv18vLhSjs8c4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XpzQjwLBHTZee+7iknpvRyhOqUPfPlnu5GRfVjAVRb9AhvPqk3kk52ZSfLg0cLDkK59KiHmyn/mb49s4nNL6rJK46e3WERlAq53KpuJsLfvaDZCYY/hoyGHLqMWfZSOmafIhsewSoo3yBzx+mvbpv9+VfjJrCaOaZ76GNrKXXiU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=amutable.com; spf=pass smtp.mailfrom=amutable.com; dkim=pass (2048-bit key) header.d=amutable-com.20230601.gappssmtp.com header.i=@amutable-com.20230601.gappssmtp.com header.b=Ya0bOa0k; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=amutable.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amutable.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amutable-com.20230601.gappssmtp.com header.i=@amutable-com.20230601.gappssmtp.com header.b="Ya0bOa0k" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2b24fcc2b5dso20294455ad.1 for ; Tue, 31 Mar 2026 17:28:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amutable-com.20230601.gappssmtp.com; s=20230601; t=1775003315; x=1775608115; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=8xdtigM1MnP5nQv3PSNQ++1o7F4yals5CCvEBR8RONk=; b=Ya0bOa0kuuzoQ5vOCszGNOybkZ2dOnuJrXOpyFC1Y3JELAN+t2b0zwM0SwqP0Mmxvi XWRpXz0+7RL+Suu/LMaQFg2eZ+/OH5tXzLPkod/qpN0+Esqy3sEwJlzCOaq2Tjhc86Y4 0l7lPxHO0FZHa+xklttApkP1icTw0JHf3BZrUOLcYOJ2FxzJ/jgnVsN/SytdtIDWgs+B 1L1hJL6XX4LIXdTAk+lYaug4Lu7O7GTyl3WP2qkwzwV7gZ4z3b54RbcCWxxSWYoqOg21 3OYgCt1KZSwGdlt0HktiBRRxo9RE7d18iDm6ZUj/1RznVhGYC9N0U4glSOJx8hX0no00 +9Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775003315; x=1775608115; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=8xdtigM1MnP5nQv3PSNQ++1o7F4yals5CCvEBR8RONk=; b=cYNpTvhJ0oxIRo3KMnJBo5Jf07yekhkiUEJn9apTW+SsbZHfcp4vazY3K3CEe34NUh 9ibNaqXitazICM79oik6s6iDgn2oQhM55DjekBKVQfaC/73jpd/1PHhAk97DhOHEazeD jV0vPdAmAKIEXK98Tav+ZscQrn/RsySGeoeg5WhCnDmShPQ1VX2xrNyM96nYmV5Ou5IG xvHms865I1+vzXf7A6Brv4zrAukPPUXei3EeCFd/MlfeFCT5LqyDr3m7sZctJaRX3HVK +GGRhZbH9A3l72MCC53qLjsC0iPfUYyMUN3MpNhkFDQPlNjcLDxMAzUuwX6Vtrr7t9nh 7nCw== X-Forwarded-Encrypted: i=1; AJvYcCUq0Y/Z0JGOpTPeeregGN3zy9SxT+EdGGd3yCl6743KsmbQ4qAkiARjO6aK1HPj8au4NS78UipHWvn70Pg=@vger.kernel.org X-Gm-Message-State: AOJu0Yz48eGqj1Y3rep3R6QyPe4wcFa97msLNcY97JMrWKRWnATA33Lq pWe8Flx8BXD1dMxPJpPzpnPUqTrsPzdNL9LdGdc8s9XjVRj/ckGGcZn5dj19mU+R6VJP X-Gm-Gg: ATEYQzxUOhBuqQoOZU344Th16kNpXB3N7sur8Ko9leuSbpDZqy8wAF/r8MNPNfY/wMH 0PUTuhHFqaeIYopN9NFoaOK9r7Yl8gMLPco7eByDMcwr2d7rQEZMySsksLHqtVVTJWK3gPWbbIm A8P51iFPb/dJidpk+mBM1Mr6p2ng07S7Z/aU4nWrEa+XWbwvOOi2fJn3zfdsDHPqHH5r1fY/ssd i3WHMAhezIWm6m5wdcsUJjWLPnnmOc60W/0ln1bvT8t/RSniDAMEijJOKkDEPBv8ZGiSZI5Ekc/ yWLD7jhj0DQcLQyu2PMivx5uK0Tw7IikHdqGvSrxGmOJ7m2Ltw9URDmr0j1lIU11lBlOUBomzqB OYOiy6da6bAbCFbSe5INzUNxvLjkZv8CSs+Z5PQFnMldH0BciJUFDB8lh1tIIMlIv6FMxGgtc/E VolrEYrhFhQ22JR1DT0aF5HQ8RCc6UP9rNIYH8oAFROTpCOQ9rcfsD8FtRg9ghYMIe+Q2uJlYmB j1aUYEnvxaahutiCFtlWfxOxA== X-Received: by 2002:a17:902:e551:b0:2b2:5840:8089 with SMTP id d9443c01a7336-2b269c42c9emr11858965ad.27.1775003314973; Tue, 31 Mar 2026 17:28:34 -0700 (PDT) Received: from thesius.dot.cyphar.com (2403-5809-a2fc-2-a90-9fd7-aedd-d405.ip6.aussiebb.net. [2403:5809:a2fc:2:a90:9fd7:aedd:d405]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b24264264fsm157057825ad.2.2026.03.31.17.28.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Mar 2026 17:28:34 -0700 (PDT) From: Aleksa Sarai Date: Wed, 01 Apr 2026 11:28:04 +1100 Subject: [PATCH v2 3/4] selftests: openat2: switch from custom ARRAY_LEN to ARRAY_SIZE Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260401-openat2-selftests-kunit-v2-3-ad153a07da0c@amutable.com> References: <20260401-openat2-selftests-kunit-v2-0-ad153a07da0c@amutable.com> In-Reply-To: <20260401-openat2-selftests-kunit-v2-0-ad153a07da0c@amutable.com> To: Shuah Khan Cc: Christian Brauner , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Aleksa Sarai , Aleksa Sarai X-Mailer: b4 0.15.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=3171; i=aleksa@amutable.com; h=from:subject:message-id; bh=PpofUhHFeqNmF2EPmF0nTEHj8cWFrIuv18vLhSjs8c4=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMWSeSVsSzr95Z/OdUyW7ph4XnbVwseiK4DfKh6Tlr3Qqb j6Tvu7xyY6JLAxiXAyWYoos2/w8QzfNX3wl+dNKNpg5rEwgQ6RFGhgYGBhYGPhyE/NKjXSM9Ey1 DfUMjXSMdIwYuDgFYKo/XmRk6F4Zy7Vpz24nk37hii7T6d+1ll/gZygKXr10qoh666H/lxh+Moq ZT7nLtF9mgdnkpg+sNam8lx+IPDydmBejcEefMXEaNwA= X-Developer-Key: i=aleksa@amutable.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 For whatever reason, the original version of the tests used a custom version of ARRAY_SIZE, but ARRAY_SIZE works just as well. Signed-off-by: Aleksa Sarai --- tools/testing/selftests/filesystems/openat2/helpers.h | 1 - tools/testing/selftests/filesystems/openat2/openat2_test.c | 12 ++++++----= -- tools/testing/selftests/filesystems/openat2/resolve_test.c | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/filesystems/openat2/helpers.h b/tools/= testing/selftests/filesystems/openat2/helpers.h index 975de513af86..f56c0c6e3ad1 100644 --- a/tools/testing/selftests/filesystems/openat2/helpers.h +++ b/tools/testing/selftests/filesystems/openat2/helpers.h @@ -17,7 +17,6 @@ #include #include "kselftest.h" =20 -#define ARRAY_LEN(X) (sizeof (X) / sizeof (*(X))) #define BUILD_BUG_ON(e) ((void)(sizeof(struct { int:(-!!(e)); }))) =20 /* diff --git a/tools/testing/selftests/filesystems/openat2/openat2_test.c b/t= ools/testing/selftests/filesystems/openat2/openat2_test.c index 0e161ef9e9e4..c6c26652ac1b 100644 --- a/tools/testing/selftests/filesystems/openat2/openat2_test.c +++ b/tools/testing/selftests/filesystems/openat2/openat2_test.c @@ -83,14 +83,14 @@ void test_openat2_struct(void) .size =3D sizeof(struct open_how_ext), .err =3D -E2BIG }, }; =20 - BUILD_BUG_ON(ARRAY_LEN(misalignments) !=3D NUM_OPENAT2_STRUCT_VARIATIONS); - BUILD_BUG_ON(ARRAY_LEN(tests) !=3D NUM_OPENAT2_STRUCT_TESTS); + BUILD_BUG_ON(ARRAY_SIZE(misalignments) !=3D NUM_OPENAT2_STRUCT_VARIATIONS= ); + BUILD_BUG_ON(ARRAY_SIZE(tests) !=3D NUM_OPENAT2_STRUCT_TESTS); =20 - for (int i =3D 0; i < ARRAY_LEN(tests); i++) { + for (int i =3D 0; i < ARRAY_SIZE(tests); i++) { struct struct_test *test =3D &tests[i]; struct open_how_ext how_ext =3D test->arg; =20 - for (int j =3D 0; j < ARRAY_LEN(misalignments); j++) { + for (int j =3D 0; j < ARRAY_SIZE(misalignments); j++) { int fd, misalign =3D misalignments[j]; char *fdpath =3D NULL; bool failed; @@ -241,9 +241,9 @@ void test_openat2_flags(void) .how.resolve =3D 0, .err =3D -EINVAL }, }; =20 - BUILD_BUG_ON(ARRAY_LEN(tests) !=3D NUM_OPENAT2_FLAG_TESTS); + BUILD_BUG_ON(ARRAY_SIZE(tests) !=3D NUM_OPENAT2_FLAG_TESTS); =20 - for (int i =3D 0; i < ARRAY_LEN(tests); i++) { + for (int i =3D 0; i < ARRAY_SIZE(tests); i++) { int fd, fdflags =3D -1; char *path, *fdpath =3D NULL; bool failed =3D false; diff --git a/tools/testing/selftests/filesystems/openat2/resolve_test.c b/t= ools/testing/selftests/filesystems/openat2/resolve_test.c index a76ef15ceb90..f7acb4300641 100644 --- a/tools/testing/selftests/filesystems/openat2/resolve_test.c +++ b/tools/testing/selftests/filesystems/openat2/resolve_test.c @@ -436,9 +436,9 @@ void test_openat2_opath_tests(void) .out.err =3D -ELOOP, .pass =3D false }, }; =20 - BUILD_BUG_ON(ARRAY_LEN(tests) !=3D NUM_OPENAT2_OPATH_TESTS); + BUILD_BUG_ON(ARRAY_SIZE(tests) !=3D NUM_OPENAT2_OPATH_TESTS); =20 - for (int i =3D 0; i < ARRAY_LEN(tests); i++) { + for (int i =3D 0; i < ARRAY_SIZE(tests); i++) { int dfd, fd; char *fdpath =3D NULL; bool failed; --=20 2.53.0 From nobody Wed Apr 1 08:15:39 2026 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (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 8A32A19067C for ; Wed, 1 Apr 2026 00:28:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775003323; cv=none; b=aaP7P6vtbV5D0J2aZBMo8geWKhN0uCeMBN8jl4rqarc7BynVeqALemPHw7FkIS6JWmd/685eXW/A4AOVqc4Wbrkwu1yVhP2HqkSxdN8O7s7U+D359L+e1GcuLwCqDjEOOgrnY1Sw1GZ+C/GT1i3J1KQurPRanh0qQTsPDRMQDgE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775003323; c=relaxed/simple; bh=DU2IKOx6U7YOwyJ1VTzCzdXodMY+cAzMrbEjt0KcJz0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ixMrnRRMMCGy2FZH5z7xHa9fz5eKLicDnESxig1n5MX1brqF2SeRZD2L66kA7o9/6FoOuH5rC1LvroutWU23ekm5YbFXIVzht7MTXs8KfG2g8yAn2sdSTDUrpx0mVv+SX45Y/7SyS5OpqI/FwxtcxkcPtHOc0s6PCowuYH9wnqY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=amutable.com; spf=pass smtp.mailfrom=amutable.com; dkim=pass (2048-bit key) header.d=amutable-com.20230601.gappssmtp.com header.i=@amutable-com.20230601.gappssmtp.com header.b=w3lJ252g; arc=none smtp.client-ip=209.85.216.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=amutable.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amutable.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amutable-com.20230601.gappssmtp.com header.i=@amutable-com.20230601.gappssmtp.com header.b="w3lJ252g" Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-3585ec417f6so148859a91.1 for ; Tue, 31 Mar 2026 17:28:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amutable-com.20230601.gappssmtp.com; s=20230601; t=1775003319; x=1775608119; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=P72F0tx6ShqpzP0SoB56T+Dgo5P++SO91Yo65nLcXus=; b=w3lJ252gG/0QPFvd67J4Qc6W47SIQAq8ynzFKTmg+plv6fAeZNERPaprrYWwI4L/ck eZTz6qkcp9zjT52jmUBLUFrYnJl5I5wfc7TXk7HkucOHp11kLvxINQUgG6mt6jQ0I8f0 q/iJzVz/GJz8NuFVKjIdfgXXWLkVQkZ8wrEa1JVYslHvYpRE/DYWuTWl07gP5QVF4K9f TiEWEeia6YXKzVjQxMDEYRPkAc1dSTM6//YWV2FnHzNboXPtaHO6bXiGy2PxJS1bEV0Z gWM/q4bQlYo8jInAaxgAlLCPAQuPFRO6u2NW0zithbQbTkpFalo3vk3+X/D04Nf3MfiS /7KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775003319; x=1775608119; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=P72F0tx6ShqpzP0SoB56T+Dgo5P++SO91Yo65nLcXus=; b=oPEb7JCbxlKom/79PybrrlV7Dk93B9pZc7fbBqecmHaC9IDKx2PlkQhBUAqAV5qoOV 2vrSsQn+JAr3+mYRKzx2rnEDNtaNQAW3fp3z1bMzYoZ++1ae+yX+OMmJk+DktF3VdZR0 k39/IwHjTWg5t8EINzR0ro4BFXz66aINkisCSI8VIOd58WPLpuSHQvRNqlpPAWRzHX0d 2J+3KdoLink/rztJVOvlC+fviO31FMetrsWLvT07OS+eLDC9Ht2U9vx4mikJUBrCEPYl msqG6UzwNBX3tWuC7nC4KAZbBlm6vfMi9GWM5cbxnQbAkABWKu5q8HDsfJyopCIm26lt m6DA== X-Forwarded-Encrypted: i=1; AJvYcCXnmS5Pr8zIeue4LhmQDHO1lg6imbepXqtYGW4qfN3nos0emM6JQF2oWUp3ZDmh9tFtAN5BIlwSnrW05Kk=@vger.kernel.org X-Gm-Message-State: AOJu0Yy6DsRlQTprZw6Fnvlz7Fz5FO7u4sTHMX52HkIt4rHrDq0llJg+ WlfzK+rbmbtGxzj6N1+yq5kCGCVlaKgs1x8WWuiSmjsXd6+LmRNMXuriaHMSOMgMVcls X-Gm-Gg: ATEYQzxpo8uvCND7Wwp1sSsq5gOzbjafY2Oa52AwiRwH2njqirksFOb7IF8ZYbKzZdr dz11MPW99fNnRbpkcxkju/4Jsw0Zv2FsKpSzt6xwy9guVkLImDMyXbc8Bahax6CdMq1xg3ooUOy SfFm6TX4p0sLfbLrAuxHlVIDeArnUw3XlpSaRlkmSz14ssa5ULsORoRfcr5+V+wiOXZJYvhCVsV YkTP77GTP6SjkfTWR+sTHy2uGqXEpdDvGlinScCFc2EE6FnzVWPaZQzcZrZ5+g0hbY0McbpPPFn 2lfeYJ+F64HvB22YnuW0hx/YLwP0ybcB7aONSWsByoSBQ133UtkD/yw0pqkT9HFk1mYi/qWV1Ih +iYWPAF+7094ZB2TTNEEcw9zx61dLfkggyJV/fyi14y/VmN3jl/gCxv4xoE558RacSbBL58R81K k0YT7xz5ppSuhJ3A+SYsNkrqFwg2DC5cTAulq4qGZ4WMXq+MpssdKoSAavOHRl+nenXCYrd6Bu/ woP1IYoOW9LmpNiA5ekr/AwgA== X-Received: by 2002:a17:90b:37cc:b0:35b:cc8b:1124 with SMTP id 98e67ed59e1d1-35db8e5570fmr4936137a91.5.1775003318704; Tue, 31 Mar 2026 17:28:38 -0700 (PDT) Received: from thesius.dot.cyphar.com (2403-5809-a2fc-2-a90-9fd7-aedd-d405.ip6.aussiebb.net. [2403:5809:a2fc:2:a90:9fd7:aedd:d405]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b24264264fsm157057825ad.2.2026.03.31.17.28.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Mar 2026 17:28:38 -0700 (PDT) From: Aleksa Sarai Date: Wed, 01 Apr 2026 11:28:05 +1100 Subject: [PATCH v2 4/4] selftests: openat2: migrate to kselftest harness Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260401-openat2-selftests-kunit-v2-4-ad153a07da0c@amutable.com> References: <20260401-openat2-selftests-kunit-v2-0-ad153a07da0c@amutable.com> In-Reply-To: <20260401-openat2-selftests-kunit-v2-0-ad153a07da0c@amutable.com> To: Shuah Khan Cc: Christian Brauner , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Aleksa Sarai , Aleksa Sarai X-Mailer: b4 0.15.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=33583; i=aleksa@amutable.com; h=from:subject:message-id; bh=DU2IKOx6U7YOwyJ1VTzCzdXodMY+cAzMrbEjt0KcJz0=; b=owGbwMvMwCWmMf3Xpe0vXfIZT6slMWSeSVsSorfd2rDwfNAdxV2hCsyqlvqZdeE3JI5LZ/rIa BdM+tXQMZGFQYyLwVJMkWWbn2fopvmLryR/WskGM4eVCWSItEgDAwMDAwsDX25iXqmRjpGeqbah nqGRjpGOEQMXpwBM9UpJhv8OSrWZpkdPz7l+d3n6tsXJl2asjVniqayrpck75/HsiSxnGRlWdl1 8+Uwk83fe/K1CV0K0tm3b2xNzhz3sZ+rZaTve5m5jAAA= X-Developer-Key: i=aleksa@amutable.com; a=openpgp; fpr=C9C370B246B09F6DBCFC744C34401015D1D2D386 These tests were written in the early days of selftests' TAP support, the more modern kselftest harness is much easier to follow and maintain. The actual contents of the tests are unchanged by this change. Most of the diff involves switching from the E_* syscall wrappers we previously used to ASSERT_EQ(fn(...), 0) in tests and helper functions. The first pass of the migration was done using Claude, followed by a manual rework and review. Assisted-by: Claude:claude-4.6-opus Signed-off-by: Aleksa Sarai --- .../selftests/filesystems/openat2/helpers.h | 62 +--- .../selftests/filesystems/openat2/openat2_test.c | 217 ++++++------ .../filesystems/openat2/rename_attack_test.c | 161 +++++---- .../selftests/filesystems/openat2/resolve_test.c | 368 ++++++++++++-----= ---- 4 files changed, 396 insertions(+), 412 deletions(-) diff --git a/tools/testing/selftests/filesystems/openat2/helpers.h b/tools/= testing/selftests/filesystems/openat2/helpers.h index f56c0c6e3ad1..7ca54c718c45 100644 --- a/tools/testing/selftests/filesystems/openat2/helpers.h +++ b/tools/testing/selftests/filesystems/openat2/helpers.h @@ -15,7 +15,7 @@ #include #include #include -#include "kselftest.h" +#include "kselftest_harness.h" =20 #define BUILD_BUG_ON(e) ((void)(sizeof(struct { int:(-!!(e)); }))) =20 @@ -56,36 +56,6 @@ struct open_how { (similar to chroot(2)). */ #endif /* RESOLVE_IN_ROOT */ =20 -#define E_func(func, ...) \ - do { \ - errno =3D 0; \ - if (func(__VA_ARGS__) < 0) \ - ksft_exit_fail_msg("%s:%d %s failed - errno:%d\n", \ - __FILE__, __LINE__, #func, errno); \ - } while (0) - -#define E_asprintf(...) E_func(asprintf, __VA_ARGS__) -#define E_chmod(...) E_func(chmod, __VA_ARGS__) -#define E_dup2(...) E_func(dup2, __VA_ARGS__) -#define E_fchdir(...) E_func(fchdir, __VA_ARGS__) -#define E_fstatat(...) E_func(fstatat, __VA_ARGS__) -#define E_kill(...) E_func(kill, __VA_ARGS__) -#define E_mkdirat(...) E_func(mkdirat, __VA_ARGS__) -#define E_mount(...) E_func(mount, __VA_ARGS__) -#define E_prctl(...) E_func(prctl, __VA_ARGS__) -#define E_readlink(...) E_func(readlink, __VA_ARGS__) -#define E_setresuid(...) E_func(setresuid, __VA_ARGS__) -#define E_symlinkat(...) E_func(symlinkat, __VA_ARGS__) -#define E_touchat(...) E_func(touchat, __VA_ARGS__) -#define E_unshare(...) E_func(unshare, __VA_ARGS__) - -#define E_assert(expr, msg, ...) \ - do { \ - if (!(expr)) \ - ksft_exit_fail_msg("ASSERT(%s:%d) failed (%s): " msg "\n", \ - __FILE__, __LINE__, #expr, ##__VA_ARGS__); \ - } while (0) - __maybe_unused static bool needs_openat2(const struct open_how *how) { @@ -135,37 +105,39 @@ static int touchat(int dfd, const char *path) } =20 __maybe_unused -static char *fdreadlink(int fd) +static char *fdreadlink(struct __test_metadata *_metadata, int fd) { char *target, *tmp; =20 - E_asprintf(&tmp, "/proc/self/fd/%d", fd); + ASSERT_GT(asprintf(&tmp, "/proc/self/fd/%d", fd), 0); =20 target =3D malloc(PATH_MAX); - if (!target) - ksft_exit_fail_msg("fdreadlink: malloc failed\n"); + ASSERT_NE(target, NULL); memset(target, 0, PATH_MAX); =20 - E_readlink(tmp, target, PATH_MAX); + ASSERT_GT(readlink(tmp, target, PATH_MAX), 0); + free(tmp); return target; } =20 __maybe_unused -static bool fdequal(int fd, int dfd, const char *path) +static bool fdequal(struct __test_metadata *_metadata, int fd, + int dfd, const char *path) { char *fdpath, *dfdpath, *other; bool cmp; =20 - fdpath =3D fdreadlink(fd); - dfdpath =3D fdreadlink(dfd); + fdpath =3D fdreadlink(_metadata, fd); + dfdpath =3D fdreadlink(_metadata, dfd); =20 - if (!path) - E_asprintf(&other, "%s", dfdpath); - else if (*path =3D=3D '/') - E_asprintf(&other, "%s", path); - else - E_asprintf(&other, "%s/%s", dfdpath, path); + if (!path) { + ASSERT_GT(asprintf(&other, "%s", dfdpath), 0); + } else if (*path =3D=3D '/') { + ASSERT_GT(asprintf(&other, "%s", path), 0); + } else { + ASSERT_GT(asprintf(&other, "%s/%s", dfdpath, path), 0); + } =20 cmp =3D !strcmp(fdpath, other); =20 diff --git a/tools/testing/selftests/filesystems/openat2/openat2_test.c b/t= ools/testing/selftests/filesystems/openat2/openat2_test.c index c6c26652ac1b..5ea3eebb7b59 100644 --- a/tools/testing/selftests/filesystems/openat2/openat2_test.c +++ b/tools/testing/selftests/filesystems/openat2/openat2_test.c @@ -15,8 +15,8 @@ #include #include =20 -#include "kselftest.h" #include "helpers.h" +#include "kselftest_harness.h" =20 /* * O_LARGEFILE is set to 0 by glibc. @@ -45,13 +45,29 @@ struct struct_test { int err; }; =20 -#define NUM_OPENAT2_STRUCT_TESTS 7 -#define NUM_OPENAT2_STRUCT_VARIATIONS 13 +struct flag_test { + const char *name; + struct open_how how; + int err; +}; + +FIXTURE(openat2) {}; =20 -void test_openat2_struct(void) +FIXTURE_SETUP(openat2) { - int misalignments[] =3D { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 17, 87 }; + if (!openat2_supported) + SKIP(return, "openat2(2) not supported"); +} + +FIXTURE_TEARDOWN(openat2) {} =20 +/* + * Verify that the struct size and misalignment handling for openat2(2) is + * correct, including that is_zeroed_user() works. + */ +TEST_F(openat2, struct_argument_sizes) +{ + int misalignments[] =3D { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 17, 87 }; struct struct_test tests[] =3D { /* Normal struct. */ { .name =3D "normal struct", @@ -83,26 +99,14 @@ void test_openat2_struct(void) .size =3D sizeof(struct open_how_ext), .err =3D -E2BIG }, }; =20 - BUILD_BUG_ON(ARRAY_SIZE(misalignments) !=3D NUM_OPENAT2_STRUCT_VARIATIONS= ); - BUILD_BUG_ON(ARRAY_SIZE(tests) !=3D NUM_OPENAT2_STRUCT_TESTS); - for (int i =3D 0; i < ARRAY_SIZE(tests); i++) { struct struct_test *test =3D &tests[i]; struct open_how_ext how_ext =3D test->arg; =20 for (int j =3D 0; j < ARRAY_SIZE(misalignments); j++) { int fd, misalign =3D misalignments[j]; - char *fdpath =3D NULL; - bool failed; - void (*resultfn)(const char *msg, ...) =3D ksft_test_result_pass; - void *copy =3D NULL, *how_copy =3D &how_ext; - - if (!openat2_supported) { - ksft_print_msg("openat2(2) unsupported\n"); - resultfn =3D ksft_test_result_skip; - goto skip; - } + char *fdpath =3D NULL; =20 if (misalign) { /* @@ -119,50 +123,42 @@ void test_openat2_struct(void) } =20 fd =3D raw_openat2(AT_FDCWD, ".", how_copy, test->size); - if (test->err >=3D 0) - failed =3D (fd < 0); - else - failed =3D (fd !=3D test->err); if (fd >=3D 0) { - fdpath =3D fdreadlink(fd); + fdpath =3D fdreadlink(_metadata, fd); close(fd); } =20 - if (failed) { - resultfn =3D ksft_test_result_fail; - - ksft_print_msg("openat2 unexpectedly returned "); - if (fdpath) - ksft_print_msg("%d['%s']\n", fd, fdpath); - else - ksft_print_msg("%d (%s)\n", fd, strerror(-fd)); + if (test->err >=3D 0) { + EXPECT_GE(fd, 0) { + TH_LOG("openat2 with %s [misalign=3D%d] should succeed, got %d (%s)", + test->name, misalign, + fd, strerror(-fd)); + } + } else { + EXPECT_EQ(test->err, fd) { + if (fdpath) + TH_LOG("openat2 with %s [misalign=3D%d] should fail with %d (%s), go= t %d['%s']", + test->name, misalign, + test->err, + strerror(-test->err), + fd, fdpath); + else + TH_LOG("openat2 with %s [misalign=3D%d] should fail with %d (%s), go= t %d (%s)", + test->name, misalign, + test->err, + strerror(-test->err), + fd, strerror(-fd)); + } } =20 -skip: - if (test->err >=3D 0) - resultfn("openat2 with %s argument [misalign=3D%d] succeeds\n", - test->name, misalign); - else - resultfn("openat2 with %s argument [misalign=3D%d] fails with %d (%s)\= n", - test->name, misalign, test->err, - strerror(-test->err)); - free(copy); free(fdpath); - fflush(stdout); } } } =20 -struct flag_test { - const char *name; - struct open_how how; - int err; -}; - -#define NUM_OPENAT2_FLAG_TESTS 25 - -void test_openat2_flags(void) +/* Verify openat2(2) flag and mode validation. */ +TEST_F(openat2, flag_validation) { struct flag_test tests[] =3D { /* O_TMPFILE is incompatible with O_PATH and O_CREAT. */ @@ -241,20 +237,10 @@ void test_openat2_flags(void) .how.resolve =3D 0, .err =3D -EINVAL }, }; =20 - BUILD_BUG_ON(ARRAY_SIZE(tests) !=3D NUM_OPENAT2_FLAG_TESTS); - for (int i =3D 0; i < ARRAY_SIZE(tests); i++) { int fd, fdflags =3D -1; char *path, *fdpath =3D NULL; - bool failed =3D false; struct flag_test *test =3D &tests[i]; - void (*resultfn)(const char *msg, ...) =3D ksft_test_result_pass; - - if (!openat2_supported) { - ksft_print_msg("openat2(2) unsupported\n"); - resultfn =3D ksft_test_result_skip; - goto skip; - } =20 path =3D (test->how.flags & O_CREAT) ? "/tmp/ksft.openat2_tmpfile" : "."; unlink(path); @@ -265,74 +251,63 @@ void test_openat2_flags(void) * Skip the testcase if it failed because not supported * by FS. (e.g. a valid O_TMPFILE combination on NFS) */ - ksft_test_result_skip("openat2 with %s fails with %d (%s)\n", - test->name, fd, strerror(-fd)); - goto next; + TH_LOG("openat2 with %s not supported by FS -- skipping", + test->name); + continue; } =20 - if (test->err >=3D 0) - failed =3D (fd < 0); - else - failed =3D (fd !=3D test->err); - if (fd >=3D 0) { - int otherflags; - - fdpath =3D fdreadlink(fd); - fdflags =3D fcntl(fd, F_GETFL); - otherflags =3D fcntl(fd, F_GETFD); - close(fd); - - E_assert(fdflags >=3D 0, "fcntl F_GETFL of new fd"); - E_assert(otherflags >=3D 0, "fcntl F_GETFD of new fd"); - - /* O_CLOEXEC isn't shown in F_GETFL. */ - if (otherflags & FD_CLOEXEC) - fdflags |=3D O_CLOEXEC; - /* O_CREAT is hidden from F_GETFL. */ - if (test->how.flags & O_CREAT) - fdflags |=3D O_CREAT; - if (!(test->how.flags & O_LARGEFILE)) - fdflags &=3D ~O_LARGEFILE; - failed |=3D (fdflags !=3D test->how.flags); - } + if (test->err >=3D 0) { + EXPECT_GE(fd, 0) { + TH_LOG("openat2 with %s should succeed, got %d (%s)", + test->name, fd, strerror(-fd)); + } + if (fd >=3D 0) { + int otherflags; =20 - if (failed) { - resultfn =3D ksft_test_result_fail; + fdpath =3D fdreadlink(_metadata, fd); + fdflags =3D fcntl(fd, F_GETFL); + otherflags =3D fcntl(fd, F_GETFD); + close(fd); =20 - ksft_print_msg("openat2 unexpectedly returned "); - if (fdpath) - ksft_print_msg("%d['%s'] with %X (!=3D %llX)\n", - fd, fdpath, fdflags, - test->how.flags); - else - ksft_print_msg("%d (%s)\n", fd, strerror(-fd)); + ASSERT_GE(fdflags, 0); + ASSERT_GE(otherflags, 0); + + /* O_CLOEXEC isn't shown in F_GETFL. */ + if (otherflags & FD_CLOEXEC) + fdflags |=3D O_CLOEXEC; + /* O_CREAT is hidden from F_GETFL. */ + if (test->how.flags & O_CREAT) + fdflags |=3D O_CREAT; + if (!(test->how.flags & O_LARGEFILE)) + fdflags &=3D ~O_LARGEFILE; + + EXPECT_EQ(fdflags, (int)test->how.flags) { + TH_LOG("openat2 with %s: flags mismatch %X !=3D %llX", + test->name, fdflags, + (unsigned long long)test->how.flags); + } + } + } else { + EXPECT_EQ(test->err, fd) { + if (fd >=3D 0) { + fdpath =3D fdreadlink(_metadata, fd); + TH_LOG("openat2 with %s should fail with %d (%s), got %d['%s']", + test->name, test->err, + strerror(-test->err), + fd, fdpath); + } else { + TH_LOG("openat2 with %s should fail with %d (%s), got %d (%s)", + test->name, test->err, + strerror(-test->err), + fd, strerror(-fd)); + } + } + if (fd >=3D 0) + close(fd); } =20 -skip: - if (test->err >=3D 0) - resultfn("openat2 with %s succeeds\n", test->name); - else - resultfn("openat2 with %s fails with %d (%s)\n", - test->name, test->err, strerror(-test->err)); -next: free(fdpath); - fflush(stdout); } } =20 -#define NUM_TESTS (NUM_OPENAT2_STRUCT_VARIATIONS * NUM_OPENAT2_STRUCT_TEST= S + \ - NUM_OPENAT2_FLAG_TESTS) - -int main(int argc, char **argv) -{ - ksft_print_header(); - ksft_set_plan(NUM_TESTS); - - test_openat2_struct(); - test_openat2_flags(); - - if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0) - ksft_exit_fail(); - else - ksft_exit_pass(); -} +TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/filesystems/openat2/rename_attack_test= .c b/tools/testing/selftests/filesystems/openat2/rename_attack_test.c index aa5699e45729..1f33c34f56be 100644 --- a/tools/testing/selftests/filesystems/openat2/rename_attack_test.c +++ b/tools/testing/selftests/filesystems/openat2/rename_attack_test.c @@ -22,44 +22,21 @@ #include #include =20 -#include "kselftest.h" #include "helpers.h" +#include "kselftest_harness.h" =20 -/* Construct a test directory with the following structure: - * - * root/ - * |-- a/ - * | `-- c/ - * `-- b/ - */ -int setup_testdir(void) -{ - int dfd; - char dirname[] =3D "/tmp/ksft-openat2-rename-attack.XXXXXX"; - - /* Make the top-level directory. */ - if (!mkdtemp(dirname)) - ksft_exit_fail_msg("setup_testdir: failed to create tmpdir\n"); - dfd =3D open(dirname, O_PATH | O_DIRECTORY); - if (dfd < 0) - ksft_exit_fail_msg("setup_testdir: failed to open tmpdir\n"); - - E_mkdirat(dfd, "a", 0755); - E_mkdirat(dfd, "b", 0755); - E_mkdirat(dfd, "a/c", 0755); - - return dfd; -} +#define ROUNDS 400000 =20 /* Swap @dirfd/@a and @dirfd/@b constantly. Parent must kill this process.= */ -pid_t spawn_attack(int dirfd, char *a, char *b) +pid_t spawn_attack(struct __test_metadata *_metadata, + int dirfd, char *a, char *b) { pid_t child =3D fork(); if (child !=3D 0) return child; =20 /* If the parent (the test process) dies, kill ourselves too. */ - E_prctl(PR_SET_PDEATHSIG, SIGKILL); + ASSERT_EQ(prctl(PR_SET_PDEATHSIG, SIGKILL), 0); =20 /* Swap @a and @b. */ for (;;) @@ -67,52 +44,90 @@ pid_t spawn_attack(int dirfd, char *a, char *b) exit(1); } =20 -#define NUM_RENAME_TESTS 2 -#define ROUNDS 400000 +/* + * Construct a test directory with the following structure: + * + * root/ + * |-- a/ + * | `-- c/ + * `-- b/ + */ +FIXTURE(rename_attack) { + int dfd; + int afd; + pid_t child; +}; =20 -const char *flagname(int resolve) +FIXTURE_SETUP(rename_attack) { - switch (resolve) { - case RESOLVE_IN_ROOT: - return "RESOLVE_IN_ROOT"; - case RESOLVE_BENEATH: - return "RESOLVE_BENEATH"; - } - return "(unknown)"; + char dirname[] =3D "/tmp/ksft-openat2-rename-attack.XXXXXX"; + + self->dfd =3D -1; + self->afd =3D -1; + self->child =3D 0; + + /* Make the top-level directory. */ + ASSERT_NE(mkdtemp(dirname), NULL); + self->dfd =3D open(dirname, O_PATH | O_DIRECTORY); + ASSERT_GE(self->dfd, 0); + + ASSERT_EQ(mkdirat(self->dfd, "a", 0755), 0); + ASSERT_EQ(mkdirat(self->dfd, "b", 0755), 0); + ASSERT_EQ(mkdirat(self->dfd, "a/c", 0755), 0); + + self->afd =3D openat(self->dfd, "a", O_PATH); + ASSERT_GE(self->afd, 0); + + self->child =3D spawn_attack(_metadata, self->dfd, "a/c", "b"); + ASSERT_GT(self->child, 0); } =20 -void test_rename_attack(int resolve) +FIXTURE_TEARDOWN(rename_attack) { - int dfd, afd; - pid_t child; - void (*resultfn)(const char *msg, ...) =3D ksft_test_result_pass; - int escapes =3D 0, other_errs =3D 0, exdevs =3D 0, eagains =3D 0, success= es =3D 0; + if (self->child > 0) + kill(self->child, SIGKILL); + if (self->afd >=3D 0) + close(self->afd); + if (self->dfd >=3D 0) + close(self->dfd); +} + +FIXTURE_VARIANT(rename_attack) { + int resolve; + const char *name; +}; =20 +FIXTURE_VARIANT_ADD(rename_attack, resolve_beneath) { + .resolve =3D RESOLVE_BENEATH, + .name =3D "RESOLVE_BENEATH", +}; + +FIXTURE_VARIANT_ADD(rename_attack, resolve_in_root) { + .resolve =3D RESOLVE_IN_ROOT, + .name =3D "RESOLVE_IN_ROOT", +}; + +TEST_F_TIMEOUT(rename_attack, test, 120) +{ + int escapes =3D 0, successes =3D 0, other_errs =3D 0, exdevs =3D 0, eagai= ns =3D 0; + char *victim_path =3D "c/../../c/../../c/../../c/../../c/../../c/../../c/= ../../c/../../c/../../c/../../c/../../c/../../c/../../c/../../c/../../c/../= ../c/../../c/../../c/../.."; struct open_how how =3D { .flags =3D O_PATH, - .resolve =3D resolve, + .resolve =3D variant->resolve, }; =20 if (!openat2_supported) { how.resolve =3D 0; - ksft_print_msg("openat2(2) unsupported -- using openat(2) instead\n"); + TH_LOG("openat2(2) unsupported -- using openat(2) instead"); } =20 - dfd =3D setup_testdir(); - afd =3D openat(dfd, "a", O_PATH); - if (afd < 0) - ksft_exit_fail_msg("test_rename_attack: failed to open 'a'\n"); - - child =3D spawn_attack(dfd, "a/c", "b"); - for (int i =3D 0; i < ROUNDS; i++) { int fd; - char *victim_path =3D "c/../../c/../../c/../../c/../../c/../../c/../../c= /../../c/../../c/../../c/../../c/../../c/../../c/../../c/../../c/../../c/..= /../c/../../c/../../c/../.."; =20 if (openat2_supported) - fd =3D sys_openat2(afd, victim_path, &how); + fd =3D sys_openat2(self->afd, victim_path, &how); else - fd =3D sys_openat(afd, victim_path, &how); + fd =3D sys_openat(self->afd, victim_path, &how); =20 if (fd < 0) { if (fd =3D=3D -EAGAIN) @@ -124,37 +139,21 @@ void test_rename_attack(int resolve) else other_errs++; /* unexpected error */ } else { - if (fdequal(fd, afd, NULL)) + if (fdequal(_metadata, fd, self->afd, NULL)) successes++; else escapes++; /* we got an unexpected fd */ } - close(fd); + if (fd >=3D 0) + close(fd); } =20 - if (escapes > 0) - resultfn =3D ksft_test_result_fail; - ksft_print_msg("non-escapes: EAGAIN=3D%d EXDEV=3D%d E=3D%d success= =3D%d\n", - eagains, exdevs, other_errs, successes); - resultfn("rename attack with %s (%d runs, got %d escapes)\n", - flagname(resolve), ROUNDS, escapes); - - /* Should be killed anyway, but might as well make sure. */ - E_kill(child, SIGKILL); + TH_LOG("non-escapes: EAGAIN=3D%d EXDEV=3D%d E=3D%d success=3D%d", + eagains, exdevs, other_errs, successes); + ASSERT_EQ(escapes, 0) { + TH_LOG("rename attack with %s (%d runs, got %d escapes)", + variant->name, ROUNDS, escapes); + } } =20 -#define NUM_TESTS NUM_RENAME_TESTS - -int main(int argc, char **argv) -{ - ksft_print_header(); - ksft_set_plan(NUM_TESTS); - - test_rename_attack(RESOLVE_BENEATH); - test_rename_attack(RESOLVE_IN_ROOT); - - if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0) - ksft_exit_fail(); - else - ksft_exit_pass(); -} +TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/filesystems/openat2/resolve_test.c b/t= ools/testing/selftests/filesystems/openat2/resolve_test.c index f7acb4300641..eacde59ce158 100644 --- a/tools/testing/selftests/filesystems/openat2/resolve_test.c +++ b/tools/testing/selftests/filesystems/openat2/resolve_test.c @@ -14,8 +14,81 @@ #include #include =20 -#include "kselftest.h" #include "helpers.h" +#include "kselftest_harness.h" + +struct resolve_test { + const char *name; + const char *dir; + const char *path; + struct open_how how; + bool pass; + union { + int err; + const char *path; + } out; +}; + +/* + * Verify a single resolve test case. This must be called from within a TE= ST_F + * function with _metadata in scope. + */ +static void verify_resolve_test(struct __test_metadata *_metadata, + int rootfd, int hardcoded_fd, + const struct resolve_test *test) +{ + struct open_how how =3D test->how; + int dfd, fd; + char *fdpath =3D NULL; + + /* Auto-set O_PATH. */ + if (!(how.flags & O_CREAT)) + how.flags |=3D O_PATH; + + if (test->dir) + dfd =3D openat(rootfd, test->dir, O_PATH | O_DIRECTORY); + else + dfd =3D dup(rootfd); + ASSERT_GE(dfd, 0) TH_LOG("failed to open dir '%s': %m", test->dir ?: "."); + ASSERT_EQ(dup2(dfd, hardcoded_fd), hardcoded_fd); + + fd =3D sys_openat2(dfd, test->path, &how); + + if (test->pass) { + EXPECT_GE(fd, 0) { + TH_LOG("%s: expected success, got %d (%s)", + test->name, fd, strerror(-fd)); + } + if (fd >=3D 0) { + EXPECT_TRUE(fdequal(_metadata, fd, rootfd, test->out.path)) { + fdpath =3D fdreadlink(_metadata, fd); + TH_LOG("%s: wrong path '%s', expected '%s'", + test->name, fdpath, + test->out.path ?: "."); + free(fdpath); + } + } + } else { + EXPECT_EQ(test->out.err, fd) { + if (fd >=3D 0) { + fdpath =3D fdreadlink(_metadata, fd); + TH_LOG("%s: expected %d (%s), got %d['%s']", + test->name, test->out.err, + strerror(-test->out.err), fd, fdpath); + free(fdpath); + } else { + TH_LOG("%s: expected %d (%s), got %d (%s)", + test->name, test->out.err, + strerror(-test->out.err), + fd, strerror(-fd)); + } + } + } + + if (fd >=3D 0) + close(fd); + close(dfd); +} =20 /* * Construct a test directory with the following structure: @@ -39,101 +112,110 @@ * |-- absself -> / * |-- self -> ../../root/ * |-- garbageself -> /../../root/ - * |-- passwd -> ../cheeky/../cheeky/../etc/../etc/passwd - * |-- abspasswd -> /../cheeky/../cheeky/../etc/../etc/passwd + * |-- passwd -> ../cheeky/../etc/../etc/passwd + * |-- abspasswd -> /../cheeky/../etc/../etc/passwd * |-- dotdotlink -> ../../../../../../../../../../../../../../etc/pas= swd * `-- garbagelink -> /../../../../../../../../../../../../../../etc/p= asswd */ -int setup_testdir(void) +FIXTURE(openat2_resolve) { + int rootfd; + int hardcoded_fd; + char *hardcoded_fdpath; + char *procselfexe; +}; + +FIXTURE_SETUP(openat2_resolve) { - int dfd, tmpfd; char dirname[] =3D "/tmp/ksft-openat2-testdir.XXXXXX"; + int dfd, tmpfd; + + self->rootfd =3D -1; + self->hardcoded_fd =3D -1; + self->hardcoded_fdpath =3D NULL; + self->procselfexe =3D NULL; + + /* NOTE: We should be checking for CAP_SYS_ADMIN here... */ + if (geteuid() !=3D 0) + SKIP(return, "all tests require euid =3D=3D 0"); + if (!openat2_supported) + SKIP(return, "openat2(2) not supported"); =20 /* Unshare and make /tmp a new directory. */ - E_unshare(CLONE_NEWNS); - E_mount("", "/tmp", "", MS_PRIVATE, ""); + ASSERT_EQ(unshare(CLONE_NEWNS), 0); + ASSERT_EQ(mount("", "/tmp", "", MS_PRIVATE, ""), 0); =20 /* Make the top-level directory. */ - if (!mkdtemp(dirname)) - ksft_exit_fail_msg("setup_testdir: failed to create tmpdir\n"); + ASSERT_NE(mkdtemp(dirname), NULL); dfd =3D open(dirname, O_PATH | O_DIRECTORY); - if (dfd < 0) - ksft_exit_fail_msg("setup_testdir: failed to open tmpdir\n"); + ASSERT_GE(dfd, 0); =20 /* A sub-directory which is actually used for tests. */ - E_mkdirat(dfd, "root", 0755); + ASSERT_EQ(mkdirat(dfd, "root", 0755), 0); tmpfd =3D openat(dfd, "root", O_PATH | O_DIRECTORY); - if (tmpfd < 0) - ksft_exit_fail_msg("setup_testdir: failed to open tmpdir\n"); + ASSERT_GE(tmpfd, 0); close(dfd); dfd =3D tmpfd; =20 - E_symlinkat("/proc/self/exe", dfd, "procexe"); - E_symlinkat("/proc/self/root", dfd, "procroot"); - E_mkdirat(dfd, "root", 0755); + ASSERT_EQ(symlinkat("/proc/self/exe", dfd, "procexe"), 0); + ASSERT_EQ(symlinkat("/proc/self/root", dfd, "procroot"), 0); + ASSERT_EQ(mkdirat(dfd, "root", 0755), 0); =20 /* There is no mountat(2), so use chdir. */ - E_mkdirat(dfd, "mnt", 0755); - E_fchdir(dfd); - E_mount("tmpfs", "./mnt", "tmpfs", MS_NOSUID | MS_NODEV, ""); - E_symlinkat("../mnt/", dfd, "mnt/self"); - E_symlinkat("/mnt/", dfd, "mnt/absself"); - - E_mkdirat(dfd, "etc", 0755); - E_touchat(dfd, "etc/passwd"); - - E_symlinkat("/newfile3", dfd, "creatlink"); - E_symlinkat("etc/", dfd, "reletc"); - E_symlinkat("etc/passwd", dfd, "relsym"); - E_symlinkat("/etc/", dfd, "absetc"); - E_symlinkat("/etc/passwd", dfd, "abssym"); - E_symlinkat("/cheeky", dfd, "abscheeky"); - - E_mkdirat(dfd, "cheeky", 0755); - - E_symlinkat("/", dfd, "cheeky/absself"); - E_symlinkat("../../root/", dfd, "cheeky/self"); - E_symlinkat("/../../root/", dfd, "cheeky/garbageself"); - - E_symlinkat("../cheeky/../etc/../etc/passwd", dfd, "cheeky/passwd"); - E_symlinkat("/../cheeky/../etc/../etc/passwd", dfd, "cheeky/abspasswd"); - - E_symlinkat("../../../../../../../../../../../../../../etc/passwd", - dfd, "cheeky/dotdotlink"); - E_symlinkat("/../../../../../../../../../../../../../../etc/passwd", - dfd, "cheeky/garbagelink"); - - return dfd; + ASSERT_EQ(mkdirat(dfd, "mnt", 0755), 0); + ASSERT_EQ(fchdir(dfd), 0); + ASSERT_EQ(mount("tmpfs", "./mnt", "tmpfs", MS_NOSUID | MS_NODEV, ""), 0); + ASSERT_EQ(symlinkat("../mnt/", dfd, "mnt/self"), 0); + ASSERT_EQ(symlinkat("/mnt/", dfd, "mnt/absself"), 0); + + ASSERT_EQ(mkdirat(dfd, "etc", 0755), 0); + ASSERT_GE(touchat(dfd, "etc/passwd"), 0); + + ASSERT_EQ(symlinkat("/newfile3", dfd, "creatlink"), 0); + ASSERT_EQ(symlinkat("etc/", dfd, "reletc"), 0); + ASSERT_EQ(symlinkat("etc/passwd", dfd, "relsym"), 0); + ASSERT_EQ(symlinkat("/etc/", dfd, "absetc"), 0); + ASSERT_EQ(symlinkat("/etc/passwd", dfd, "abssym"), 0); + ASSERT_EQ(symlinkat("/cheeky", dfd, "abscheeky"), 0); + + ASSERT_EQ(mkdirat(dfd, "cheeky", 0755), 0); + + ASSERT_EQ(symlinkat("/", dfd, "cheeky/absself"), 0); + ASSERT_EQ(symlinkat("../../root/", dfd, "cheeky/self"), 0); + ASSERT_EQ(symlinkat("/../../root/", dfd, "cheeky/garbageself"), 0); + + ASSERT_EQ(symlinkat("../cheeky/../etc/../etc/passwd", + dfd, "cheeky/passwd"), 0); + ASSERT_EQ(symlinkat("/../cheeky/../etc/../etc/passwd", + dfd, "cheeky/abspasswd"), 0); + + ASSERT_EQ(symlinkat("../../../../../../../../../../../../../../etc/passwd= ", + dfd, "cheeky/dotdotlink"), 0); + ASSERT_EQ(symlinkat("/../../../../../../../../../../../../../../etc/passw= d", + dfd, "cheeky/garbagelink"), 0); + + self->rootfd =3D dfd; + + self->hardcoded_fd =3D open("/dev/null", O_RDONLY); + ASSERT_GE(self->hardcoded_fd, 0); + ASSERT_GE(asprintf(&self->hardcoded_fdpath, "self/fd/%d", + self->hardcoded_fd), 0); + ASSERT_GE(asprintf(&self->procselfexe, "/proc/%d/exe", getpid()), 0); } =20 -struct basic_test { - const char *name; - const char *dir; - const char *path; - struct open_how how; - bool pass; - union { - int err; - const char *path; - } out; -}; - -#define NUM_OPENAT2_OPATH_TESTS 88 - -void test_openat2_opath_tests(void) +FIXTURE_TEARDOWN(openat2_resolve) { - int rootfd, hardcoded_fd; - char *procselfexe, *hardcoded_fdpath; - - E_asprintf(&procselfexe, "/proc/%d/exe", getpid()); - rootfd =3D setup_testdir(); - - hardcoded_fd =3D open("/dev/null", O_RDONLY); - E_assert(hardcoded_fd >=3D 0, "open fd to hardcode"); - E_asprintf(&hardcoded_fdpath, "self/fd/%d", hardcoded_fd); + free(self->procselfexe); + free(self->hardcoded_fdpath); + if (self->hardcoded_fd >=3D 0) + close(self->hardcoded_fd); + if (self->rootfd >=3D 0) + close(self->rootfd); +} =20 - struct basic_test tests[] =3D { - /** RESOLVE_BENEATH **/ +/* Attempts to cross the dirfd should be blocked with -EXDEV. */ +TEST_F(openat2_resolve, resolve_beneath) +{ + struct resolve_test tests[] =3D { /* Attempts to cross dirfd should be blocked. */ { .name =3D "[beneath] jump to /", .path =3D "/", .how.resolve =3D RESOLVE_BENEATH, @@ -206,9 +288,17 @@ void test_openat2_opath_tests(void) { .name =3D "[beneath] tricky absolute + garbage link outside $root", .path =3D "abscheeky/garbagelink", .how.resolve =3D RESOLVE_BENEATH, .out.err =3D -EXDEV, .pass =3D false }, + }; + + for (int i =3D 0; i < ARRAY_SIZE(tests); i++) + verify_resolve_test(_metadata, self->rootfd, + self->hardcoded_fd, &tests[i]); +} =20 - /** RESOLVE_IN_ROOT **/ - /* All attempts to cross the dirfd will be scoped-to-root. */ +/* All attempts to cross the dirfd will be scoped-to-root. */ +TEST_F(openat2_resolve, resolve_in_root) +{ + struct resolve_test tests[] =3D { { .name =3D "[in_root] jump to /", .path =3D "/", .how.resolve =3D RESOLVE_IN_ROOT, .out.path =3D NULL, .pass =3D true }, @@ -297,8 +387,17 @@ void test_openat2_opath_tests(void) .how.mode =3D 0700, .how.resolve =3D RESOLVE_IN_ROOT, .out.path =3D "newfile3", .pass =3D true }, + }; =20 - /** RESOLVE_NO_XDEV **/ + for (int i =3D 0; i < ARRAY_SIZE(tests); i++) + verify_resolve_test(_metadata, self->rootfd, + self->hardcoded_fd, &tests[i]); +} + +/* Crossing mount boundaries should be blocked. */ +TEST_F(openat2_resolve, resolve_no_xdev) +{ + struct resolve_test tests[] =3D { /* Crossing *down* into a mountpoint is disallowed. */ { .name =3D "[no_xdev] cross into $mnt", .path =3D "mnt", .how.resolve =3D RESOLVE_NO_XDEV, @@ -347,10 +446,19 @@ void test_openat2_opath_tests(void) .out.err =3D -EXDEV, .pass =3D false }, /* Except magic-link jumps inside the same vfsmount. */ { .name =3D "[no_xdev] jump through magic-link to same procfs", - .dir =3D "/proc", .path =3D hardcoded_fdpath, .how.resolve =3D RESOLVE= _NO_XDEV, - .out.path =3D "/proc", .pass =3D true, }, + .dir =3D "/proc", .path =3D self->hardcoded_fdpath, .how.resolve =3D R= ESOLVE_NO_XDEV, + .out.path =3D "/proc", .pass =3D true, }, + }; + + for (int i =3D 0; i < ARRAY_SIZE(tests); i++) + verify_resolve_test(_metadata, self->rootfd, + self->hardcoded_fd, &tests[i]); +} =20 - /** RESOLVE_NO_MAGICLINKS **/ +/* Procfs-style magic-link resolution should be blocked. */ +TEST_F(openat2_resolve, resolve_no_magiclinks) +{ + struct resolve_test tests[] =3D { /* Regular symlinks should work. */ { .name =3D "[no_magiclinks] ordinary relative symlink", .path =3D "relsym", .how.resolve =3D RESOLVE_NO_MAGICLINKS, @@ -365,7 +473,7 @@ void test_openat2_opath_tests(void) { .name =3D "[no_magiclinks] normal path to magic-link with O_NOFOLLOW", .path =3D "/proc/self/exe", .how.flags =3D O_NOFOLLOW, .how.resolve =3D RESOLVE_NO_MAGICLINKS, - .out.path =3D procselfexe, .pass =3D true }, + .out.path =3D self->procselfexe, .pass =3D true }, { .name =3D "[no_magiclinks] symlink to magic-link path component", .path =3D "procroot/etc", .how.resolve =3D RESOLVE_NO_MAGICLINKS, .out.err =3D -ELOOP, .pass =3D false }, @@ -376,8 +484,17 @@ void test_openat2_opath_tests(void) .path =3D "/proc/self/root/etc", .how.flags =3D O_NOFOLLOW, .how.resolve =3D RESOLVE_NO_MAGICLINKS, .out.err =3D -ELOOP, .pass =3D false }, + }; + + for (int i =3D 0; i < ARRAY_SIZE(tests); i++) + verify_resolve_test(_metadata, self->rootfd, + self->hardcoded_fd, &tests[i]); +} =20 - /** RESOLVE_NO_SYMLINKS **/ +/* All symlink resolution should be blocked. */ +TEST_F(openat2_resolve, resolve_no_symlinks) +{ + struct resolve_test tests[] =3D { /* Normal paths should work. */ { .name =3D "[no_symlinks] ordinary path to '.'", .path =3D ".", .how.resolve =3D RESOLVE_NO_SYMLINKS, @@ -436,88 +553,9 @@ void test_openat2_opath_tests(void) .out.err =3D -ELOOP, .pass =3D false }, }; =20 - BUILD_BUG_ON(ARRAY_SIZE(tests) !=3D NUM_OPENAT2_OPATH_TESTS); - - for (int i =3D 0; i < ARRAY_SIZE(tests); i++) { - int dfd, fd; - char *fdpath =3D NULL; - bool failed; - void (*resultfn)(const char *msg, ...) =3D ksft_test_result_pass; - struct basic_test *test =3D &tests[i]; - - if (!openat2_supported) { - ksft_print_msg("openat2(2) unsupported\n"); - resultfn =3D ksft_test_result_skip; - goto skip; - } - - /* Auto-set O_PATH. */ - if (!(test->how.flags & O_CREAT)) - test->how.flags |=3D O_PATH; - - if (test->dir) - dfd =3D openat(rootfd, test->dir, O_PATH | O_DIRECTORY); - else - dfd =3D dup(rootfd); - E_assert(dfd, "failed to openat root '%s': %m", test->dir); - - E_dup2(dfd, hardcoded_fd); - - fd =3D sys_openat2(dfd, test->path, &test->how); - if (test->pass) - failed =3D (fd < 0 || !fdequal(fd, rootfd, test->out.path)); - else - failed =3D (fd !=3D test->out.err); - if (fd >=3D 0) { - fdpath =3D fdreadlink(fd); - close(fd); - } - close(dfd); - - if (failed) { - resultfn =3D ksft_test_result_fail; - - ksft_print_msg("openat2 unexpectedly returned "); - if (fdpath) - ksft_print_msg("%d['%s']\n", fd, fdpath); - else - ksft_print_msg("%d (%s)\n", fd, strerror(-fd)); - } - -skip: - if (test->pass) - resultfn("%s gives path '%s'\n", test->name, - test->out.path ?: "."); - else - resultfn("%s fails with %d (%s)\n", test->name, - test->out.err, strerror(-test->out.err)); - - fflush(stdout); - free(fdpath); - } - - free(procselfexe); - close(rootfd); - - free(hardcoded_fdpath); - close(hardcoded_fd); + for (int i =3D 0; i < ARRAY_SIZE(tests); i++) + verify_resolve_test(_metadata, self->rootfd, + self->hardcoded_fd, &tests[i]); } =20 -#define NUM_TESTS NUM_OPENAT2_OPATH_TESTS - -int main(int argc, char **argv) -{ - ksft_print_header(); - ksft_set_plan(NUM_TESTS); - - /* NOTE: We should be checking for CAP_SYS_ADMIN here... */ - if (geteuid() !=3D 0) - ksft_exit_skip("all tests require euid =3D=3D 0\n"); - - test_openat2_opath_tests(); - - if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0) - ksft_exit_fail(); - else - ksft_exit_pass(); -} +TEST_HARNESS_MAIN --=20 2.53.0