From nobody Thu Oct 2 06:17:57 2025 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.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 80131289821 for ; Fri, 19 Sep 2025 14:58:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293884; cv=none; b=Qioi8Zo3VM9p6r9n528LE2FiRcXAoDmCGSMDUcbAOrJ53CniBQ97YWZbCDSl93r7LuR/yHmkhym+VI9BesMYJuPkVXFzb8EhmNCHd8pGKaz/5x9ZSBquTLJbeREr8osUDgGtZMUO15Y+f8qOGDOeVZxaZt+/WNxiwVUf9Oz0bYk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293884; c=relaxed/simple; bh=egRlU8I8cap/vMG/cP8j3cYqgKLkvNE8Tghmz+aDQ8U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ACUZzq1Ecnh3QPbTvA6ARJ6J+TKZBDK/m0F6ZeuwDlQ9Yo4oXX/1lo36NqRHxtO4YO5l/S5PKohibaFVrYroOaWlzECb50jxeRMTMmwZ+s3dN5cEd7Laf7RjBdn/2UgYtJAtVWoAEegbbuoeqeJqpvdzPch4YMyBA2aSudcUGkI= 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=JdRqnUvr; arc=none smtp.client-ip=209.85.128.43 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="JdRqnUvr" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-45f2c5ef00fso17835915e9.1 for ; Fri, 19 Sep 2025 07:58:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758293880; x=1758898680; 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; bh=Iux7tJA7Iu45JzwDlqqn3NczB+xQmLnfn/+UUn4bsic=; b=JdRqnUvryAa6smB7nKg6KARTl+190Tbrk5xt77sgdnc30u1kPjHdigY4wD/JwYzoDb 5z3IIcR7ZkVks8kC5AAG1Iqn4hAbuaECNrauHnQpuOD4+1dV/raCWWK5lkli53zbg5GR CqmAZUSs6sl3IrGkbWAfn62HYiOkDFVE1gAcssm0LqymrHOv30FeVMHYQuODs9IptO6v 4/dnn7Zew7lTMqZId4aqOpiKMTswQ3IdU6zQAO8OKCqug/XcIY/bDooDA9qmfUltiIRJ e6hLXCKbYgNHrEtORzEekJ+HMok1ATD03jsh57QBxePhlNmhfGK+6vOdf54OIzDSFqGa FlpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758293880; x=1758898680; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Iux7tJA7Iu45JzwDlqqn3NczB+xQmLnfn/+UUn4bsic=; b=aEAZmaGE8eJ1fbLJRXVTTGF/ivZT38A68onYiKsoO5POtQhWPcfG5WUnWOylOGkH/9 a9yjIlExfn9o3X7EyREdRyVYhcEVKXexNawfpgs9AfJdr2yF+W+Ux163Y/L+M7jEzDqK SPk/F5hsaK7KHJ7ZDttU0sSLe9zPpVFE5FLMVOl6GQ/AVTci7U3PyRXH0mdy5oeQ8P0y I46/yiOV6JtoilF6TxCHE4cx02AcsKrh4pzz3c29yKrjd/srNW+zC7R/GTH6J0JwbUId 87tp6YcDPlTP4IIvRQAiD/Ukxz1lixhHqt/Chi1+vwiUpN0nVCmpbBOR+b5cuLuSmwHm WHNQ== X-Forwarded-Encrypted: i=1; AJvYcCVblyJNGwUFDOfYq8aTiUJdHgnJ39NEIiyCZSd+5KeIEdDAhWPm3dazB0S7VcvY+SU59O4Va7Env6xtBis=@vger.kernel.org X-Gm-Message-State: AOJu0YzgWx3yCCv5lpTPL+2TB5RJYs4emCbXOBS/i5XtIiWIg9rFqs32 KKp2C09CteaQtetpMsQqn8CVM7lHPa1JHRJAgx9NgZsMsloaxSGLiaoB X-Gm-Gg: ASbGnctMAVvbkDx0dJQX4/92ncdPwGFzR/t3Jy4FGaGR8wHpczojW4T00CxO9wMzTKq l6q9ARw2tqeh3R99uUzFQruIL59rvckh5T1TvwYjuDpqGk5iSoaMcQelI/2tDlFSobwlAVQoOL2 o/i5Emu4jY7aawDHn9+2M6mZLdtswjN0KAdGU1PtQ/TAd5ypw2tH5RSYrVH0vTSCUThMFdGY4Fb 0T3lxDw8X0nx2/dqkqP7qTwPkymLvFBtklFKlgahUcB9EdybbI2jTZyOq7cDluXleluZ6eyVvdF Hvchqnwn1WD0P7YmaF1jNLgNlYKwlxXCTwmbPaz2mbcZlykTw0ENwR73kRuYJYiSSGkD/dF0uj/ lfayuA7/nKr1sUC+ZqOmJIOzL1PH06i4oDwWf2+4A6EHA8d62n7vNqT1gX4dPCQtLDMfKC64Zg/ LOf5Myg+lOV48qLA0= X-Google-Smtp-Source: AGHT+IE+XK5nD+9hANJoxYPRSlTs9xbOKfixXYZia7lhztTHnoAksXT/1nQcAsPiiouWjOFFkGLQJg== X-Received: by 2002:a05:600c:b8d:b0:45d:e326:96fb with SMTP id 5b1f17b1804b1-467ef72d771mr33033965e9.30.1758293879733; Fri, 19 Sep 2025 07:57:59 -0700 (PDT) Received: from xl-nested.c.googlers.com.com (124.62.78.34.bc.googleusercontent.com. [34.78.62.124]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3ee0fbc7188sm8551386f8f.37.2025.09.19.07.57.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 07:57:59 -0700 (PDT) From: Ethan Graham To: ethangraham@google.com, glider@google.com Cc: andreyknvl@gmail.com, andy@kernel.org, brauner@kernel.org, brendan.higgins@linux.dev, davem@davemloft.net, davidgow@google.com, dhowells@redhat.com, dvyukov@google.com, elver@google.com, herbert@gondor.apana.org.au, ignat@cloudflare.com, jack@suse.cz, jannh@google.com, johannes@sipsolutions.net, kasan-dev@googlegroups.com, kees@kernel.org, kunit-dev@googlegroups.com, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, lukas@wunner.de, rmoar@google.com, shuah@kernel.org, sj@kernel.org, tarasmadan@google.com Subject: [PATCH v2 01/10] mm/kasan: implement kasan_poison_range Date: Fri, 19 Sep 2025 14:57:41 +0000 Message-ID: <20250919145750.3448393-2-ethan.w.s.graham@gmail.com> X-Mailer: git-send-email 2.51.0.470.ga7dc726c21-goog In-Reply-To: <20250919145750.3448393-1-ethan.w.s.graham@gmail.com> References: <20250919145750.3448393-1-ethan.w.s.graham@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" From: Ethan Graham Introduce a new helper function, kasan_poison_range(), to encapsulate the logic for poisoning an arbitrary memory range of a given size, and expose it publically in . This is a preparatory change for the upcoming KFuzzTest patches, which requires the ability to poison the inter-region padding in its input buffers. No functional change to any other subsystem is intended by this commit. Signed-off-by: Ethan Graham Reviewed-by: Alexander Potapenko --- PR v1: - Enforce KASAN_GRANULE_SIZE alignment for the end of the range in kasan_poison_range(), and return -EINVAL when this isn't respected. --- --- include/linux/kasan.h | 11 +++++++++++ mm/kasan/shadow.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 890011071f2b..cd6cdf732378 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -102,6 +102,16 @@ static inline bool kasan_has_integrated_init(void) } =20 #ifdef CONFIG_KASAN + +/** + * kasan_poison_range - poison the memory range [@addr, @addr + @size) + * + * The exact behavior is subject to alignment with KASAN_GRANULE_SIZE, def= ined + * in : if @start is unaligned, the initial partial gran= ule + * at the beginning of the range is only poisoned if CONFIG_KASAN_GENERIC= =3Dy. + */ +int kasan_poison_range(const void *addr, size_t size); + void __kasan_unpoison_range(const void *addr, size_t size); static __always_inline void kasan_unpoison_range(const void *addr, size_t = size) { @@ -402,6 +412,7 @@ static __always_inline bool kasan_check_byte(const void= *addr) =20 #else /* CONFIG_KASAN */ =20 +static inline int kasan_poison_range(const void *start, size_t size) { ret= urn 0; } static inline void kasan_unpoison_range(const void *address, size_t size) = {} static inline void kasan_poison_pages(struct page *page, unsigned int orde= r, bool init) {} diff --git a/mm/kasan/shadow.c b/mm/kasan/shadow.c index d2c70cd2afb1..7faed02264f2 100644 --- a/mm/kasan/shadow.c +++ b/mm/kasan/shadow.c @@ -147,6 +147,40 @@ void kasan_poison(const void *addr, size_t size, u8 va= lue, bool init) } EXPORT_SYMBOL_GPL(kasan_poison); =20 +int kasan_poison_range(const void *addr, size_t size) +{ + uintptr_t start_addr =3D (uintptr_t)addr; + uintptr_t head_granule_start; + uintptr_t poison_body_start; + uintptr_t poison_body_end; + size_t head_prefix_size; + uintptr_t end_addr; + + if ((start_addr + size) % KASAN_GRANULE_SIZE) + return -EINVAL; + + end_addr =3D ALIGN_DOWN(start_addr + size, KASAN_GRANULE_SIZE); + if (start_addr >=3D end_addr) + return -EINVAL; + + head_granule_start =3D ALIGN_DOWN(start_addr, KASAN_GRANULE_SIZE); + head_prefix_size =3D start_addr - head_granule_start; + + if (IS_ENABLED(CONFIG_KASAN_GENERIC) && head_prefix_size > 0) + kasan_poison_last_granule((void *)head_granule_start, + head_prefix_size); + + poison_body_start =3D ALIGN(start_addr, KASAN_GRANULE_SIZE); + poison_body_end =3D ALIGN_DOWN(end_addr, KASAN_GRANULE_SIZE); + + if (poison_body_start < poison_body_end) + kasan_poison((void *)poison_body_start, + poison_body_end - poison_body_start, + KASAN_SLAB_REDZONE, false); + return 0; +} +EXPORT_SYMBOL(kasan_poison_range); + #ifdef CONFIG_KASAN_GENERIC void kasan_poison_last_granule(const void *addr, size_t size) { --=20 2.51.0.470.ga7dc726c21-goog From nobody Thu Oct 2 06:17:57 2025 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 E76572F83BA for ; Fri, 19 Sep 2025 14:58:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293885; cv=none; b=l6dG1r0n644R592Iu5uIL7BGTrTtAIFTQLdSzK9QtRfAJICwwvLJuPOyw2rxbqg4jcKne4dk960jUKuS1OyzdwvXU/K1hyZwjb4lkTfvpJNYhFXyZ6G15vawbfafgTayN2nC3MNFKkx93n3iJNJZAOV41f/uVIU42fmzwgykmlw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293885; c=relaxed/simple; bh=W1Cx1H5Qdm0KVWpETmWbFO3JGUjEDTrnWDOAxdmkWZ4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s0VVmMvN2rY78Q2KjVsfLEihe8IYtpAcyRcHCQM2IbhIP5EkDE6gNv4MeB78rNPxhoXBG8urQZIber5SbyVmslcr3z5iYKK95fGwLWyW6RNsYVBTbdtCx70krWvnATWadxv4xDAn1Whyl9ZP1qVuYMQHkMK3Jxwc/cdTda+4ObU= 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=WaXk7wwx; arc=none smtp.client-ip=209.85.128.47 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="WaXk7wwx" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-45f29e5e89bso25362095e9.2 for ; Fri, 19 Sep 2025 07:58:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758293881; x=1758898681; 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; bh=2hlAiC4iYE+JnqssTNzhVHpNNckRVTN9eVeusCLGe3g=; b=WaXk7wwx86Ohr7/4uUYwxs+wylC1Tfnlwh2A2jZspaYM1pBweRTBE/v41v9XTJofNb egqPRhceEWRhPuSr3lHalD9CCpsICr2pGi3XH5Vg7zl2+YUqGSryE7EDPgCBaNUdGhy4 rTg2zxmjQnlWsyxuvgZgEnuABoVnGmfUPgdlqz91PTN/PGt/6LrQ1piOSlmZJPqF4pta P74yIGwtIQAAXKo319GpRVCy03rrbqZjwgmcfbPF9J/6JTfWsUeq6kKd+ILIiXrDRRqR v4DjPffUdOrL0NE4DejiNdj7aB07wIMwk5Y5yQXeXBbALHiQjFGRMKszyAKrhP4YS8q1 ex/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758293881; x=1758898681; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2hlAiC4iYE+JnqssTNzhVHpNNckRVTN9eVeusCLGe3g=; b=YZ5p1wBbq+b5/oFEg0WsjIgMxFRU76EqWe0sf66Orzf2ZWF2Zuesu40+o0882S3h7K +iFzMsDj7rZlveuYyg36pas1vJGDOSEPyR7o6QunBEeJAAtOosrloOSD+dIflv9K1vMu U/M2fmV2PlOGjkxaPIUgMGVl1He6t0VtD6X22zWxNJvKScb6um+/FIVIzcGZls7BBSV3 CxiGMjvucW/E6UlvPIoQr5MRvfMWtrgZTPtAi2HfKf0ygcu0E6qTXmxSJwfw9wc8Ij+R wWq5LV5H4LFmK9/a/KbKCsDT/J6vy/NBhLxsFPLysoApvNvIvvo16M6LRwxWEYwaXnwD aA2w== X-Forwarded-Encrypted: i=1; AJvYcCVP9gK87qRuPbuRJ/BXutf+LCjyy1KyXWCT/c0XE0JkTxtcPep5utNjOLSPrLz8VcvyVCo7xycihuDzKUg=@vger.kernel.org X-Gm-Message-State: AOJu0Ywynt33vCl9g9Pv4USAiVCISnQEncHw91ydJvyD28CKSXkiKTqr 3g52MxCsT2WIhgkDgwMudmoS4JmRQsLYuwrwoQEq3HCL23zwpY5nnrOP X-Gm-Gg: ASbGncsH4pfbscmGtjAISL/98ThSjNezRw/oTFGgrYzcI6WzXZ7RGK9WjqRH7bxLvyW LcH8xY15krAI5BvVwZ4evsgu7BnmKAjNojoylISI/zhUL7qz/9ojjp/Z7ES32UTFy7kR3DwjqEt MxXYQ0S6R2eDlcTYGVhNsOw6NURqwfh9tVlinLbj+VCcaq4Iuq+5hz33hw2A6Cqh8y2U/z+xi0d 1v9K55b/fvr8bWQIPxDlcxCOMjuWwcw0l3rAvdjkbNMU/cMihFko+YO+VoewMQ7Q20IC8TlmnQu 5WGj9kUruEOvB6R1o4LVP5Ep8agvoaZoODn3/UoOgz7wREyf9YJK4d450WkSdzzoE202s7uJ/07 aRaTuz82QU/b7R8qsbEYOJTk0NnIxbFt3Gs75aJyQM5J7B4UZ3jMbJvYYouikRCWkkQ7at3RPhs ntoggjlXWmeIsi8JE= X-Google-Smtp-Source: AGHT+IFa1Qs561Ql2zZnE06+Ll5tHw3YWr25XbxMFMVoVcqWCzoT68ORpp/coQbvAEk51O7kV6Xg9g== X-Received: by 2002:a05:600c:8b42:b0:458:a992:6f1e with SMTP id 5b1f17b1804b1-467e6b6500amr36061665e9.5.1758293880649; Fri, 19 Sep 2025 07:58:00 -0700 (PDT) Received: from xl-nested.c.googlers.com.com (124.62.78.34.bc.googleusercontent.com. [34.78.62.124]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3ee0fbc7188sm8551386f8f.37.2025.09.19.07.57.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 07:58:00 -0700 (PDT) From: Ethan Graham To: ethangraham@google.com, glider@google.com Cc: andreyknvl@gmail.com, andy@kernel.org, brauner@kernel.org, brendan.higgins@linux.dev, davem@davemloft.net, davidgow@google.com, dhowells@redhat.com, dvyukov@google.com, elver@google.com, herbert@gondor.apana.org.au, ignat@cloudflare.com, jack@suse.cz, jannh@google.com, johannes@sipsolutions.net, kasan-dev@googlegroups.com, kees@kernel.org, kunit-dev@googlegroups.com, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, lukas@wunner.de, rmoar@google.com, shuah@kernel.org, sj@kernel.org, tarasmadan@google.com Subject: [PATCH v2 02/10] kfuzztest: add user-facing API and data structures Date: Fri, 19 Sep 2025 14:57:42 +0000 Message-ID: <20250919145750.3448393-3-ethan.w.s.graham@gmail.com> X-Mailer: git-send-email 2.51.0.470.ga7dc726c21-goog In-Reply-To: <20250919145750.3448393-1-ethan.w.s.graham@gmail.com> References: <20250919145750.3448393-1-ethan.w.s.graham@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" From: Ethan Graham Add the foundational user-facing components for the KFuzzTest framework. This includes the main API header , the Kconfig option to enable the feature, and the required linker script changes which introduce three new ELF sections in vmlinux. Note that KFuzzTest is intended strictly for debug builds only, and should never be enabled in a production build. The fact that it exposes internal kernel functions and state directly to userspace may constitute a serious security vulnerability if used for any reason other than testing. The header defines: - The FUZZ_TEST() macro for creating test targets. - The data structures required for the binary serialization format, which allows passing complex inputs from userspace. - The metadata structures for test targets, constraints and annotations, which are placed in dedicated ELF sections (.kfuzztest_*) for discovery. This patch only adds the public interface and build integration; no runtime logic is included. Signed-off-by: Ethan Graham Reviewed-by: Alexander Potapenko --- PR v1: - Move KFuzzTest metadata definitions to generic vmlinux linkage so that the framework isn't bound to x86_64. - Return -EFAULT when simple_write_to_buffer returns a value not equal to the input length in the main FUZZ_TEST macro. - Enforce a maximum input size of 64KiB in the main FUZZ_TEST macro, returning -EINVAL when it isn't respected. - Refactor KFUZZTEST_ANNOTATION_* macros. - Taint the kernel with TAINT_TEST inside the FUZZ_TEST macro when a fuzz target is invoked for the first time. --- --- include/asm-generic/vmlinux.lds.h | 22 +- include/linux/kfuzztest.h | 493 ++++++++++++++++++++++++++++++ lib/Kconfig.debug | 1 + lib/kfuzztest/Kconfig | 20 ++ 4 files changed, 535 insertions(+), 1 deletion(-) create mode 100644 include/linux/kfuzztest.h create mode 100644 lib/kfuzztest/Kconfig diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinu= x.lds.h index ae2d2359b79e..9afe569d013b 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -373,7 +373,8 @@ defined(CONFIG_AUTOFDO_CLANG) || defined(CONFIG_PROPELL= ER_CLANG) TRACE_PRINTKS() \ BPF_RAW_TP() \ TRACEPOINT_STR() \ - KUNIT_TABLE() + KUNIT_TABLE() \ + KFUZZTEST_TABLE() =20 /* * Data section helpers @@ -966,6 +967,25 @@ defined(CONFIG_AUTOFDO_CLANG) || defined(CONFIG_PROPEL= LER_CLANG) BOUNDED_SECTION_POST_LABEL(.kunit_init_test_suites, \ __kunit_init_suites, _start, _end) =20 +#ifdef CONFIG_KFUZZTEST +#define KFUZZTEST_TABLE() \ + . =3D ALIGN(PAGE_SIZE); \ + __kfuzztest_targets_start =3D .; \ + KEEP(*(.kfuzztest_target)); \ + __kfuzztest_targets_end =3D .; \ + . =3D ALIGN(PAGE_SIZE); \ + __kfuzztest_constraints_start =3D .; \ + KEEP(*(.kfuzztest_constraint)); \ + __kfuzztest_constraints_end =3D .; \ + . =3D ALIGN(PAGE_SIZE); \ + __kfuzztest_annotations_start =3D .; \ + KEEP(*(.kfuzztest_annotation)); \ + __kfuzztest_annotations_end =3D .; + +#else /* CONFIG_KFUZZTEST */ +#define KFUZZTEST_TABLE() +#endif /* CONFIG_KFUZZTEST */ + #ifdef CONFIG_BLK_DEV_INITRD #define INIT_RAM_FS \ . =3D ALIGN(4); \ diff --git a/include/linux/kfuzztest.h b/include/linux/kfuzztest.h new file mode 100644 index 000000000000..38970dea8fa5 --- /dev/null +++ b/include/linux/kfuzztest.h @@ -0,0 +1,493 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * The Kernel Fuzz Testing Framework (KFuzzTest) API for defining fuzz tar= gets + * for internal kernel functions. + * + * For more information please see Documentation/dev-tools/kfuzztest.rst. + * + * Copyright 2025 Google LLC + */ +#ifndef KFUZZTEST_H +#define KFUZZTEST_H + +#include +#include +#include + +#define KFUZZTEST_HEADER_MAGIC (0xBFACE) +#define KFUZZTEST_V0 (0) + +/** + * @brief The KFuzzTest Input Serialization Format + * + * KFuzzTest receives its input from userspace as a single binary blob. Th= is + * format allows for the serialization of complex, pointer-rich C structur= es + * into a flat buffer that can be safely passed into the kernel. This form= at + * requires only a single copy from userspace into a kernel buffer, and no + * further kernel allocations. Pointers are patched internally using a "re= gion" + * system where each region corresponds to some pointed-to data. + * + * Regions should be padded to respect alignment constraints of their unde= rlying + * types, and should be followed by at least 8 bytes of padding. These pad= ded + * regions are poisoned by KFuzzTest to ensure that KASAN catches OOB acce= sses. + * + * The format consists of a header and three main components: + * 1. An 8-byte header: Contains KFUZZTEST_MAGIC in the first 4 bytes, and= the + * version number in the subsequent 4 bytes. This ensures backwards + * compatibility in the event of future format changes. + * 2. A reloc_region_array: Defines the memory layout of the target struct= ure + * by partitioning the payload into logical regions. Each logical region + * should contain the byte representation of the type that it represents, + * including any necessary padding. The region descriptors should be + * ordered by offset ascending. + * 3. A reloc_table: Provides "linking" instructions that tell the kernel = how + * to patch pointer fields to point to the correct regions. By design, + * the first region (index 0) is passed as input into a FUZZ_TEST. + * 4. A Payload: The raw binary data for the target structure and its asso= ciated + * buffers. This should be aligned to the maximum alignment of all + * regions to satisfy alignment requirements of the input types, but this + * isn't checked by the parser. + * + * For a detailed specification of the binary layout see the full document= ation + * at: Documentation/dev-tools/kfuzztest.rst + */ + +/** + * struct reloc_region - single contiguous memory region in the payload + * + * @offset: The byte offset of this region from the start of the payload, = which + * should be aligned to the alignment requirements of the region's + * underlying type. + * @size: The size of this region in bytes. + */ +struct reloc_region { + uint32_t offset; + uint32_t size; +}; + +/** + * struct reloc_region_array - array of regions in an input + * + * @num_regions: The total number of regions defined. + * @regions: A flexible array of `num_regions` region descriptors. + */ +struct reloc_region_array { + uint32_t num_regions; + struct reloc_region regions[]; +}; + +/** + * struct reloc_entry - a single pointer to be patched in an input + * + * @region_id: The index of the region in the `reloc_region_array` that + * contains the pointer. + * @region_offset: The start offset of the pointer inside of the region. + * @value: contains the index of the pointee region, or KFUZZTEST_REGIONID= _NULL + * if the pointer is NULL. + */ +struct reloc_entry { + uint32_t region_id; + uint32_t region_offset; + uint32_t value; +}; + +/** + * struct reloc_table - array of relocations required by an input + * + * @num_entries: the number of pointer relocations. + * @padding_size: the number of padded bytes between the last relocation in + * entries, and the start of the payload data. This should be at least + * 8 bytes, as it is used for poisoning. + * @entries: array of relocations. + */ +struct reloc_table { + uint32_t num_entries; + uint32_t padding_size; + struct reloc_entry entries[]; +}; + +/** + * kfuzztest_parse_and_relocate - validate and relocate a KFuzzTest input + * + * @input: A buffer containing the serialized input for a fuzz target. + * @input_size: the size in bytes of the @input buffer. + * @arg_ret: return pointer for the test case's input structure. + */ +int kfuzztest_parse_and_relocate(void *input, size_t input_size, void **ar= g_ret); + +/* + * Dump some information on the parsed headers and payload. Can be useful = for + * debugging inputs when writing an encoder for the KFuzzTest input format. + */ +__attribute__((unused)) static inline void kfuzztest_debug_header(struct r= eloc_region_array *regions, + struct reloc_table *rt, void *payload_start, + void *payload_end) +{ + uint32_t i; + + pr_info("regions: { num_regions =3D %u } @ %px", regions->num_regions, re= gions); + for (i =3D 0; i < regions->num_regions; i++) { + pr_info(" region_%u: { start: 0x%x, size: 0x%x }", i, regions->regions[= i].offset, + regions->regions[i].size); + } + + pr_info("reloc_table: { num_entries =3D %u, padding =3D %u } @ offset 0x%= tx", rt->num_entries, rt->padding_size, + (char *)rt - (char *)regions); + for (i =3D 0; i < rt->num_entries; i++) { + pr_info(" reloc_%u: { src: %u, offset: 0x%x, dst: %u }", i, rt->entries= [i].region_id, + rt->entries[i].region_offset, rt->entries[i].value); + } + + pr_info("payload: [0x%lx, 0x%tx)", (char *)payload_start - (char *)region= s, + (char *)payload_end - (char *)regions); +} + +struct kfuzztest_target { + const char *name; + const char *arg_type_name; + ssize_t (*write_input_cb)(struct file *filp, const char __user *buf, size= _t len, loff_t *off); +} __aligned(32); + +#define KFUZZTEST_MAX_INPUT_SIZE (PAGE_SIZE * 16) + +/** + * FUZZ_TEST - defines a KFuzzTest target + * + * @test_name: The unique identifier for the fuzz test, which is used to n= ame + * the debugfs entry, e.g., /sys/kernel/debug/kfuzztest/@test_name. + * @test_arg_type: The struct type that defines the inputs for the test. T= his + * must be the full struct type (e.g., "struct my_inputs"), not a typedef. + * + * Context: + * This macro is the primary entry point for the KFuzzTest framework. It + * generates all the necessary boilerplate for a fuzz test, including: + * - A static `struct kfuzztest_target` instance that is placed in a + * dedicated ELF section for discovery by userspace tools. + * - A `debugfs` write callback that handles receiving serialized data f= rom + * a fuzzer, parsing it, and "hydrating" it into a valid C struct. + * - A function stub where the developer places the test logic. + * + * User-Provided Logic: + * The developer must provide the body of the fuzz test logic within the c= urly + * braces following the macro invocation. Within this scope, the framework + * provides the `arg` variable, which is a pointer of type `@test_arg_type= *`=20 + * to the fully hydrated input structure. All pointer fields within this s= truct + * have been relocated and are valid kernel pointers. This is the primary + * variable to use for accessing fuzzing inputs. + * + * Example Usage: + * + * // 1. The kernel function we want to fuzz. + * int process_data(const char *data, size_t len); + * + * // 2. Define a struct to hold all inputs for the function. + * struct process_data_inputs { + * const char *data; + * size_t len; + * }; + * + * // 3. Define the fuzz test using the FUZZ_TEST macro. + * FUZZ_TEST(process_data_fuzzer, struct process_data_inputs) + * { + * int ret; + * // Use KFUZZTEST_EXPECT_* to enforce preconditions. + * // The test will exit early if data is NULL. + * KFUZZTEST_EXPECT_NOT_NULL(process_data_inputs, data); + * + * // Use KFUZZTEST_ANNOTATE_* to provide hints to the fuzzer. + * // This links the 'len' field to the 'data' buffer. + * KFUZZTEST_ANNOTATE_LEN(process_data_inputs, len, data); + * + * // Call the function under test using the 'arg' variable. OOB memory + * // accesses will be caught by KASAN, but the user can also choose to + * // validate the return value and log any failures. + * ret =3D process_data(arg->data, arg->len); + * } + */ +#define FUZZ_TEST(test_name, test_arg_type) \ + static ssize_t kfuzztest_write_cb_##test_name(struct file *filp, const ch= ar __user *buf, size_t len, \ + loff_t *off); \ + static void kfuzztest_logic_##test_name(test_arg_type *arg); \ + static const struct kfuzztest_target __fuzz_test__##test_name __section("= .kfuzztest_target") __used =3D { \ + .name =3D #test_name, \ + .arg_type_name =3D #test_arg_type, \ + .write_input_cb =3D kfuzztest_write_cb_##test_name, \ + }; \ + static ssize_t kfuzztest_write_cb_##test_name(struct file *filp, const ch= ar __user *buf, size_t len, \ + loff_t *off) \ + { \ + test_arg_type *arg; \ + void *buffer; \ + int ret; \ + \ + /* + * Taint the kernel on the first fuzzing invocation. The debugfs + * interface provides a high-risk entry point for userspace to + * call kernel functions with untrusted input. + */ \ + if (!test_taint(TAINT_TEST)) \ + add_taint(TAINT_TEST, LOCKDEP_STILL_OK); \ + if (len >=3D KFUZZTEST_MAX_INPUT_SIZE) { \ + pr_warn(#test_name ": user input of size %zu is too large", len); \ + return -EINVAL; \ + } \ + buffer =3D kmalloc(len, GFP_KERNEL); \ + if (!buffer) \ + return -ENOMEM; \ + ret =3D simple_write_to_buffer(buffer, len, off, buf, len); \ + if (ret !=3D len){ \ + ret =3D -EFAULT; \ + goto out; \ + }; \ + ret =3D kfuzztest_parse_and_relocate(buffer, len, (void **)&arg); \ + if (ret < 0) \ + goto out; \ + kfuzztest_logic_##test_name(arg); \ + ret =3D len; \ +out: \ + kfree(buffer); \ + return ret; \ + } \ + static void kfuzztest_logic_##test_name(test_arg_type *arg) + +enum kfuzztest_constraint_type { + EXPECT_EQ, + EXPECT_NE, + EXPECT_LT, + EXPECT_LE, + EXPECT_GT, + EXPECT_GE, + EXPECT_IN_RANGE, +}; + +/** + * struct kfuzztest_constraint - a metadata record for a domain constraint + * + * Domain constraints are rules about the input data that must be satisfie= d for + * a fuzz test to proceed. While they are enforced in the kernel with a ru= ntime + * check, they are primarily intended as a discoverable contract for users= pace + * fuzzers. + * + * Instances of this struct are generated by the KFUZZTEST_EXPECT_* macros + * and placed into the read-only ".kfuzztest_constraint" ELF section of the + * vmlinux binary. A fuzzer can parse this section to learn about the + * constraints and generate valid inputs more intelligently. + * + * For an example of how these constraints are used within a fuzz test, se= e the + * documentation for the FUZZ_TEST() macro. + * + * @input_type: The name of the input struct type, without the leading + * "struct ". + * @field_name: The name of the field within the struct that this constrai= nt + * applies to. + * @value1: The primary value used in the comparison (e.g., the upper + * bound for EXPECT_LE). + * @value2: The secondary value, used only for multi-value comparisons + * (e.g., the upper bound for EXPECT_IN_RANGE). + * @type: The type of the constraint. + */ +struct kfuzztest_constraint { + const char *input_type; + const char *field_name; + uintptr_t value1; + uintptr_t value2; + enum kfuzztest_constraint_type type; +} __aligned(64); + +#define __KFUZZTEST_DEFINE_CONSTRAINT(arg_type, field, val1, val2, tpe, pr= edicate) \ + do { \ + static struct kfuzztest_constraint __constraint_##arg_type##_##field \ + __section(".kfuzztest_constraint") __used =3D { \ + .input_type =3D "struct " #arg_type, \ + .field_name =3D #field, \ + .value1 =3D (uintptr_t)val1, \ + .value2 =3D (uintptr_t)val2, \ + .type =3D tpe, \ + }; \ + if (!(predicate)) \ + return; \ + } while (0) + +/** + * KFUZZTEST_EXPECT_EQ - constrain a field to be equal to a value + * + * @arg_type: name of the input structure, without the leading "struct ". + * @field: some field that is comparable + * @val: a value of the same type as @arg_type.@field + */ +#define KFUZZTEST_EXPECT_EQ(arg_type, field, val) \ + __KFUZZTEST_DEFINE_CONSTRAINT(arg_type, field, val, 0x0, EXPECT_EQ, arg->= field =3D=3D val) + +/** + * KFUZZTEST_EXPECT_NE - constrain a field to be not equal to a value + * + * @arg_type: name of the input structure, without the leading "struct ". + * @field: some field that is comparable. + * @val: a value of the same type as @arg_type.@field. + */ +#define KFUZZTEST_EXPECT_NE(arg_type, field, val) \ + __KFUZZTEST_DEFINE_CONSTRAINT(arg_type, field, val, 0x0, EXPECT_NE, arg->= field !=3D val) + +/** + * KFUZZTEST_EXPECT_LT - constrain a field to be less than a value + * + * @arg_type: name of the input structure, without the leading "struct ". + * @field: some field that is comparable. + * @val: a value of the same type as @arg_type.@field. + */ +#define KFUZZTEST_EXPECT_LT(arg_type, field, val) \ + __KFUZZTEST_DEFINE_CONSTRAINT(arg_type, field, val, 0x0, EXPECT_LT, arg->= field < val) + +/** + * KFUZZTEST_EXPECT_LE - constrain a field to be less than or equal to a v= alue + * + * @arg_type: name of the input structure, without the leading "struct ". + * @field: some field that is comparable. + * @val: a value of the same type as @arg_type.@field. + */ +#define KFUZZTEST_EXPECT_LE(arg_type, field, val) \ + __KFUZZTEST_DEFINE_CONSTRAINT(arg_type, field, val, 0x0, EXPECT_LE, arg-= >field <=3D val) + +/** + * KFUZZTEST_EXPECT_GT - constrain a field to be greater than a value + * + * @arg_type: name of the input structure, without the leading "struct ". + * @field: some field that is comparable. + * @val: a value of the same type as @arg_type.@field. + */ +#define KFUZZTEST_EXPECT_GT(arg_type, field, val) \ + __KFUZZTEST_DEFINE_CONSTRAINT(arg_type, field, val, 0x0, EXPECT_GT, arg->= field > val) + +/** + * KFUZZTEST_EXPECT_GE - constrain a field to be greater than or equal to = a value + * + * @arg_type: name of the input structure, without the leading "struct ". + * @field: some field that is comparable. + * @val: a value of the same type as @arg_type.@field. + */ +#define KFUZZTEST_EXPECT_GE(arg_type, field, val) \ + __KFUZZTEST_DEFINE_CONSTRAINT(arg_type, field, val, 0x0, EXPECT_GE, arg->= field >=3D val) + +/** + * KFUZZTEST_EXPECT_NOT_NULL - constrain a pointer field to be non-NULL + * + * @arg_type: name of the input structure, without the leading "struct ". + * @field: a pointer field. + */ +#define KFUZZTEST_EXPECT_NOT_NULL(arg_type, field) KFUZZTEST_EXPECT_NE(arg= _type, field, NULL) + +/** + * KFUZZTEST_EXPECT_IN_RANGE - constrain a field to be within a range + * + * @arg_type: name of the input structure, without the leading "struct ". + * @field: some field that is comparable. + * @lower_bound: a lower bound of the same type as @arg_type.@field. + * @upper_bound: an upper bound of the same type as @arg_type.@field. + */ +#define KFUZZTEST_EXPECT_IN_RANGE(arg_type, field, lower_bound, upper_boun= d) \ + __KFUZZTEST_DEFINE_CONSTRAINT(arg_type, field, lower_bound, upper_bound, \ + EXPECT_IN_RANGE, arg->field >=3D lower_bound && arg->field <=3D upper_b= ound) + +/** + * Annotations express attributes about structure fields that can't be eas= ily + * or safely verified at runtime. They are intended as hints to the fuzzing + * engine to help it generate more semantically correct and effective inpu= ts. + * Unlike constraints, annotations do not add any runtime checks and do not + * cause a test to exit early. + * + * For example, a `char *` field could be a raw byte buffer or a C-style + * null-terminated string. A fuzzer that is aware of this distinction can = avoid + * creating inputs that would cause trivial, uninteresting crashes from re= ading + * past the end of a non-null-terminated buffer. + */ +enum kfuzztest_annotation_attribute { + ATTRIBUTE_LEN, + ATTRIBUTE_STRING, + ATTRIBUTE_ARRAY, +}; + +/** + * struct kfuzztest_annotation - a metadata record for a fuzzer hint + * + * This struct captures a single hint about a field in the input structure. + * Instances are generated by the KFUZZTEST_ANNOTATE_* macros and are plac= ed + * into the read-only ".kfuzztest_annotation" ELF section of the vmlinux b= inary. + * + * A userspace fuzzer can parse this section to understand the semantic + * relationships between fields (e.g., which field is a length for which + * buffer) and the expected format of the data (e.g., a null-terminated + * string). This allows the fuzzer to be much more intelligent during input + * generation and mutation. + * + * For an example of how annotations are used within a fuzz test, see the + * documentation for the FUZZ_TEST() macro. + * + * @input_type: The name of the input struct type. + * @field_name: The name of the field being annotated (e.g., the data + * buffer field). + * @linked_field_name: For annotations that link two fields (like + * ATTRIBUTE_LEN), this is the name of the related field (e.g., the + * length field). For others, this may be unused. + * @attrib: The type of the annotation hint. + */ +struct kfuzztest_annotation { + const char *input_type; + const char *field_name; + const char *linked_field_name; + enum kfuzztest_annotation_attribute attrib; +} __aligned(32); + +#define __KFUZZTEST_ANNOTATE(arg_type, field, linked_field, attribute) = \ + static struct kfuzztest_annotation __annotation_##arg_type##_##field __se= ction(".kfuzztest_annotation") \ + __used =3D { \ + .input_type =3D "struct " #arg_type, \ + .field_name =3D #field, \ + .linked_field_name =3D #linked_field, \ + .attrib =3D attribute, \ + } + +/** + * KFUZZTEST_ANNOTATE_STRING - annotate a char* field as a C string + * + * We define a C string as a sequence of non-zero characters followed by e= xactly + * one null terminator. + * + * @arg_type: name of the input structure, without the leading "struct ". + * @field: the name of the field to annotate. + */ +#define KFUZZTEST_ANNOTATE_STRING(arg_type, field) __KFUZZTEST_ANNOTATE(ar= g_type, field, NULL, ATTRIBUTE_STRING) + +/** + * KFUZZTEST_ANNOTATE_ARRAY - annotate a pointer as an array + * + * We define an array as a contiguous memory region containing zero or more + * elements of the same type. + * + * @arg_type: name of the input structure, without the leading "struct ". + * @field: the name of the field to annotate. + */ +#define KFUZZTEST_ANNOTATE_ARRAY(arg_type, field) __KFUZZTEST_ANNOTATE(arg= _type, field, NULL, ATTRIBUTE_ARRAY) + +/** + * KFUZZTEST_ANNOTATE_LEN - annotate a field as the length of another + * + * This expresses the relationship `arg_type.field =3D=3D len(linked_field= )`, where + * `linked_field` is an array. + * + * @arg_type: name of the input structure, without the leading "struct ". + * @field: the name of the field to annotate. + * @linked_field: the name of an array field with length @field. + */ +#define KFUZZTEST_ANNOTATE_LEN(arg_type, field, linked_field) \ + __KFUZZTEST_ANNOTATE(arg_type, field, linked_field, ATTRIBUTE_LEN) + +#define KFUZZTEST_REGIONID_NULL U32_MAX + +/** + * The end of the input should be padded by at least this number of bytes = as + * it is poisoned to detect out of bounds accesses at the end of the last + * region. + */ +#define KFUZZTEST_POISON_SIZE 0x8 + +#endif /* KFUZZTEST_H */ diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index dc0e0c6ed075..49a1748b9f24 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1947,6 +1947,7 @@ endmenu menu "Kernel Testing and Coverage" =20 source "lib/kunit/Kconfig" +source "lib/kfuzztest/Kconfig" =20 config NOTIFIER_ERROR_INJECTION tristate "Notifier error injection" diff --git a/lib/kfuzztest/Kconfig b/lib/kfuzztest/Kconfig new file mode 100644 index 000000000000..f9fb5abf8d27 --- /dev/null +++ b/lib/kfuzztest/Kconfig @@ -0,0 +1,20 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config KFUZZTEST + bool "KFuzzTest - enable support for internal fuzz targets" + depends on DEBUG_FS && DEBUG_KERNEL + help + Enables support for the kernel fuzz testing framework (KFuzzTest), an + interface for exposing internal kernel functions to a userspace fuzzing + engine. KFuzzTest targets are exposed via a debugfs interface that + accepts serialized userspace inputs, and is designed to make it easier + to fuzz deeply nested kernel code that is hard to reach from the system + call boundary. Using a simple macro-based API, developers can add a new + fuzz target with minimal boilerplate code. + + It is strongly recommended to also enable CONFIG_KASAN for byte-accurate + out-of-bounds detection, as KFuzzTest was designed with this in mind. It + is also recommended to enable CONFIG_KCOV for coverage guided fuzzing. + + WARNING: This exposes internal kernel functions directly to userspace + and must NEVER be enabled in production builds. --=20 2.51.0.470.ga7dc726c21-goog From nobody Thu Oct 2 06:17:57 2025 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (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 83B7A246BD5 for ; Fri, 19 Sep 2025 14:58:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293886; cv=none; b=D+x+030BFeNwUYlQUdcgxLbwNinsUm8k9QoEu03A3P2xM1YgQpu9XQbWcpLULNralPNbtZbWXyE/FWnZdUVoyCNUXfh6VmKAJPRdBH9goKbR2hBhPpwU0tnKWCD6pnDanmzf4n42hjTYukX0fjet3yTVPDliGFxdIU5RLOPH/ZQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293886; c=relaxed/simple; bh=YgdUCAzQWh6LJ77x1D0s9gnC5Gfnb8ZCJX0mdS2EBP0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LFTmGOVjC/e7xU4sb12aVvrGpjeAn2ajnGio0I69uGPo/pZf/EKQVvTtoXG7aCznkgB8doHN4UvIeZJ9S0OrSFM414cRZ2pLCD+Q4nAUs0Db91XxlsDwoqRESW1Y8WRByyTgTznUK5tT4hJXM5ftnti2zywHM+YhqL6j2NxjoQU= 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=GOt7qiek; arc=none smtp.client-ip=209.85.221.53 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="GOt7qiek" Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-3ed20bdfdffso2277307f8f.2 for ; Fri, 19 Sep 2025 07:58:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758293882; x=1758898682; 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; bh=sfh1bNw7MLcAN8E4ek/ZR2D+Z9BQxSpA6YNYJoSqpiw=; b=GOt7qiekicoX0uRxn4sdsRdFUpaxdLtirZuxoHmsIBaG7PR4NDcY+gUbRP+AoIgD7f R+PlxYOTv1tZZ9l7YvAzqZ4UHiYmXIUGwTYYOyCbe9ewSbYXYjldsCQ2ab54HeMsqVbY 61+wNG2a2fOZV0xbjaFEiLfjwZgupQyu3G1BPjnwoLMsH5FY9vYYXCMgKMpzyj2E/u1l qyFrN1CP52I7oGYOFG5tE+67tvmxDZKBD47y2dmi9FTnW6+u0ap14/LCvuPrswwXe0u5 sE29GzB9xYyd4nN2agFN4T3aR7UAqXd1tEWdIrQqoJ45+QPjgfYrs38WXgemyQm+o43E pmQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758293882; x=1758898682; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sfh1bNw7MLcAN8E4ek/ZR2D+Z9BQxSpA6YNYJoSqpiw=; b=fwXB2cJJHF1ispTxYdV1YtGruMIdyAsu1aiGEmMr0D4lsUmGMEAUAsv8E8pqtywiFn vwQwVFWd1Ly8s17cXDaTlxUnvW+wk6mvLy6tUIzavK4QFAzSpaK5q/0AS5cLY7To8IAl uBwr3b69sd0DbDV1Gb5NxQhg81Al6wrn4MkCUKoeNG3AjCGNelrhz1vOc8NBzfl7WUkD 8zFJ48E5KVl5G6LMocy2oetodUN5cugwhGTl2quvu6oOKLkpWHrKB17+D6CLcfkocqxL vUL4yqh8gZ7oC379HukzO1tX1atQmdrbhKpyqayJm1rO5GiRkbV2FKBLIHYbx7WoHe+c cN+w== X-Forwarded-Encrypted: i=1; AJvYcCUuja57uOll9jgYutgwcx/ra5xIGH+a3Z0h/LBqS0Q7hw3Iilhi0acU/fk9EM3+Jqdbxw6Vuz0RKiQ9N5I=@vger.kernel.org X-Gm-Message-State: AOJu0Yxgf1yBsViwkfg3p9rV1KN9Tddoe52Bpuhq3zfRfDnV06eRI1Bz Th9bmWu+qnKPErGMkdDA1aDySBVDkaE8UoGc/zZxAux0ZXPY6lzLQ2FD X-Gm-Gg: ASbGncsFCiAUYT3x3T/fHMR1yfrv8aTOODDabh0HWr2H3Seo8RHAoU1YlZLK+U5RMG8 7ru86UVV/Ddq2SZfLclI17nosyON4R2D0bqxndn1uUKXuAdHJZ0nsOtQRfVjYN1Dj69iKMQMDE8 Vy1hizVS/876exERFSAiqRIJZwswXROicIIhEP5QbHwtsKiPgM5Cbzexezrhm0qNr3Cv74OPqT9 pbCSqKBWT7sXyTTzdxpW5bbGYqaKvI8HVJzABjXPxAU1uuhm0gLdR5cwoO80IfP6gd2jeT7Tjix s78ANVrke1bXi44GQ5yK8XgJ9aziDn0V6jw71Pj8w1O7f7FRvuF3A8PL2L6AQ/Arg1QE36qYgXF rUPqQB+yKxZ4loLwykH4c28znsayeP7T9xpxmBSf1NBRBM66dYIwaYmCDlLomNT6cIAdbXyrRPZ wgGVwSCkWSaLNE5eA= X-Google-Smtp-Source: AGHT+IHBcA/sW1R7VOxqGoXPReKDC15y2kiVaWDthtwRe5Vqcoa4dhIFYkBLfxKu+/GZppZceHWgEw== X-Received: by 2002:a05:6000:24c9:b0:3ec:ce37:3a6d with SMTP id ffacd0b85a97d-3ee857699acmr2826209f8f.47.1758293881422; Fri, 19 Sep 2025 07:58:01 -0700 (PDT) Received: from xl-nested.c.googlers.com.com (124.62.78.34.bc.googleusercontent.com. [34.78.62.124]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3ee0fbc7188sm8551386f8f.37.2025.09.19.07.58.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 07:58:00 -0700 (PDT) From: Ethan Graham To: ethangraham@google.com, glider@google.com Cc: andreyknvl@gmail.com, andy@kernel.org, brauner@kernel.org, brendan.higgins@linux.dev, davem@davemloft.net, davidgow@google.com, dhowells@redhat.com, dvyukov@google.com, elver@google.com, herbert@gondor.apana.org.au, ignat@cloudflare.com, jack@suse.cz, jannh@google.com, johannes@sipsolutions.net, kasan-dev@googlegroups.com, kees@kernel.org, kunit-dev@googlegroups.com, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, lukas@wunner.de, rmoar@google.com, shuah@kernel.org, sj@kernel.org, tarasmadan@google.com Subject: [PATCH v2 03/10] kfuzztest: implement core module and input processing Date: Fri, 19 Sep 2025 14:57:43 +0000 Message-ID: <20250919145750.3448393-4-ethan.w.s.graham@gmail.com> X-Mailer: git-send-email 2.51.0.470.ga7dc726c21-goog In-Reply-To: <20250919145750.3448393-1-ethan.w.s.graham@gmail.com> References: <20250919145750.3448393-1-ethan.w.s.graham@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" From: Ethan Graham Add the core runtime implementation for KFuzzTest. This includes the module initialization, and the logic for receiving and processing user-provided inputs through debugfs. On module load, the framework discovers all test targets by iterating over the .kfuzztest_target section, creating a corresponding debugfs directory with a write-only 'input' file for each of them. Writing to an 'input' file triggers the main fuzzing sequence: 1. The serialized input is copied from userspace into a kernel buffer. 2. The buffer is parsed to validate the region array and relocation table. 3. Pointers are patched based on the relocation entries, and in KASAN builds the inter-region padding is poisoned. 4. The resulting struct is passed to the user-defined test logic. Signed-off-by: Ethan Graham Reviewed-by: Alexander Potapenko --- PR v2: - Fix build issues identified by the kernel test robot . - Address some nits pointed out by Alexander Potapenko. PR v1: - Update kfuzztest/parse.c interfaces to take `unsigned char *` instead of `void *`, reducing the number of pointer casts. - Expose minimum region alignment via a new debugfs file. - Expose number of successful invocations via a new debugfs file. - Refactor module init function, add _config directory with entries containing KFuzzTest state information. - Account for kasan_poison_range() return value in input parsing logic. - Validate alignment of payload end. - Move static sizeof assertions into /lib/kfuzztest/main.c. - Remove the taint in kfuzztest/main.c. We instead taint the kernel as soon as a fuzz test is invoked for the first time, which is done in the primary FUZZ_TEST macro. RFC v2: - The module's init function now taints the kernel with TAINT_TEST. --- --- include/linux/kfuzztest.h | 4 + lib/Makefile | 2 + lib/kfuzztest/Makefile | 4 + lib/kfuzztest/main.c | 242 ++++++++++++++++++++++++++++++++++++++ lib/kfuzztest/parse.c | 204 ++++++++++++++++++++++++++++++++ 5 files changed, 456 insertions(+) create mode 100644 lib/kfuzztest/Makefile create mode 100644 lib/kfuzztest/main.c create mode 100644 lib/kfuzztest/parse.c diff --git a/include/linux/kfuzztest.h b/include/linux/kfuzztest.h index 38970dea8fa5..2620e48bb620 100644 --- a/include/linux/kfuzztest.h +++ b/include/linux/kfuzztest.h @@ -150,6 +150,9 @@ struct kfuzztest_target { =20 #define KFUZZTEST_MAX_INPUT_SIZE (PAGE_SIZE * 16) =20 +/* Increments a global counter after a successful invocation. */ +void record_invocation(void); + /** * FUZZ_TEST - defines a KFuzzTest target * @@ -243,6 +246,7 @@ struct kfuzztest_target { if (ret < 0) \ goto out; \ kfuzztest_logic_##test_name(arg); \ + record_invocation(); \ ret =3D len; \ out: \ kfree(buffer); \ diff --git a/lib/Makefile b/lib/Makefile index 392ff808c9b9..02789bf88499 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -325,6 +325,8 @@ obj-$(CONFIG_GENERIC_LIB_CMPDI2) +=3D cmpdi2.o obj-$(CONFIG_GENERIC_LIB_UCMPDI2) +=3D ucmpdi2.o obj-$(CONFIG_OBJAGG) +=3D objagg.o =20 +obj-$(CONFIG_KFUZZTEST) +=3D kfuzztest/ + # pldmfw library obj-$(CONFIG_PLDMFW) +=3D pldmfw/ =20 diff --git a/lib/kfuzztest/Makefile b/lib/kfuzztest/Makefile new file mode 100644 index 000000000000..142d16007eea --- /dev/null +++ b/lib/kfuzztest/Makefile @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_KFUZZTEST) +=3D kfuzztest.o +kfuzztest-objs :=3D main.o parse.o diff --git a/lib/kfuzztest/main.c b/lib/kfuzztest/main.c new file mode 100644 index 000000000000..c36a7a0b7602 --- /dev/null +++ b/lib/kfuzztest/main.c @@ -0,0 +1,242 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KFuzzTest core module initialization and debugfs interface. + * + * Copyright 2025 Google LLC + */ +#include +#include +#include +#include +#include +#include +#include + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Ethan Graham "); +MODULE_DESCRIPTION("Kernel Fuzz Testing Framework (KFuzzTest)"); + +/* + * Enforce a fixed struct size to ensure a consistent stride when iteratin= g over + * the array of these structs in the dedicated ELF section. + */ +static_assert(sizeof(struct kfuzztest_target) =3D=3D 32, "struct kfuzztest= _target should have size 32"); +static_assert(sizeof(struct kfuzztest_constraint) =3D=3D 64, "struct kfuzz= test_constraint should have size 64"); +static_assert(sizeof(struct kfuzztest_annotation) =3D=3D 32, "struct kfuzz= test_annotation should have size 32"); + +extern const struct kfuzztest_target __kfuzztest_targets_start[]; +extern const struct kfuzztest_target __kfuzztest_targets_end[]; + +/** + * struct kfuzztest_state - global state for the KFuzzTest module + * + * @kfuzztest_dir: The root debugfs directory, /sys/kernel/debug/kfuzztest= /. + * @num_invocations: total number of target invocations. + * @num_targets: number of registered targets. + * @target_fops: array of file operations for each registered target. + * @minalign_fops: file operations for the /_config/minalign file. + * @num_invocations_fops: file operations for the /_config/num_invocations= file. + */ +struct kfuzztest_state { + struct dentry *kfuzztest_dir; + atomic_t num_invocations; + size_t num_targets; + + struct file_operations *target_fops; + struct file_operations minalign_fops; + struct file_operations num_invocations_fops; +}; + +static struct kfuzztest_state state; + +void record_invocation(void) +{ + atomic_inc(&state.num_invocations); +} + +static void cleanup_kfuzztest_state(struct kfuzztest_state *st) +{ + debugfs_remove_recursive(st->kfuzztest_dir); + st->num_targets =3D 0; + st->num_invocations =3D (atomic_t)ATOMIC_INIT(0); + kfree(st->target_fops); + st->target_fops =3D NULL; +} + +static const umode_t KFUZZTEST_INPUT_PERMS =3D 0222; +static const umode_t KFUZZTEST_MINALIGN_PERMS =3D 0444; + +static ssize_t read_cb_integer(struct file *filp, char __user *buf, size_t= count, loff_t *f_pos, size_t value) +{ + char buffer[64]; + int len; + + len =3D scnprintf(buffer, sizeof(buffer), "%zu\n", value); + return simple_read_from_buffer(buf, count, f_pos, buffer, len); +} + +/* + * Callback for /sys/kernel/debug/kfuzztest/_config/minalign. Minalign + * corresponds to the minimum alignment that regions in a KFuzzTest input = must + * satisfy. This callback returns that value in string format. + */ +static ssize_t minalign_read_cb(struct file *filp, char __user *buf, size_= t count, loff_t *f_pos) +{ + int minalign =3D MAX(KFUZZTEST_POISON_SIZE, ARCH_KMALLOC_MINALIGN); + return read_cb_integer(filp, buf, count, f_pos, minalign); +} + +/* + * Callback for /sys/kernel/debug/kfuzztest/_config/num_invocations, which + * returns the value in string format. + */ +static ssize_t num_invocations_read_cb(struct file *filp, char __user *buf= , size_t count, loff_t *f_pos) +{ + return read_cb_integer(filp, buf, count, f_pos, atomic_read(&state.num_in= vocations)); +} + +static int create_read_only_file(struct dentry *parent, const char *name, = struct file_operations *fops) +{ + struct dentry *file; + int err =3D 0; + + file =3D debugfs_create_file(name, KFUZZTEST_MINALIGN_PERMS, parent, NULL= , fops); + if (!file) + err =3D -ENOMEM; + else if (IS_ERR(file)) + err =3D PTR_ERR(file); + return err; +} + +static int initialize_config_dir(struct kfuzztest_state *st) +{ + struct dentry *dir; + int err =3D 0; + + dir =3D debugfs_create_dir("_config", st->kfuzztest_dir); + if (!dir) + err =3D -ENOMEM; + else if (IS_ERR(dir)) + err =3D PTR_ERR(dir); + if (err) { + pr_info("kfuzztest: failed to create /_config dir"); + goto out; + } + + st->minalign_fops =3D (struct file_operations){ + .owner =3D THIS_MODULE, + .read =3D minalign_read_cb, + }; + err =3D create_read_only_file(dir, "minalign", &st->minalign_fops); + if (err) { + pr_info("kfuzztest: failed to create /_config/minalign"); + goto out; + } + + st->num_invocations_fops =3D (struct file_operations){ + .owner =3D THIS_MODULE, + .read =3D num_invocations_read_cb, + }; + err =3D create_read_only_file(dir, "num_invocations", &st->num_invocation= s_fops); + if (err) + pr_info("kfuzztest: failed to create /_config/num_invocations"); +out: + return err; +} + +static int initialize_target_dir(struct kfuzztest_state *st, const struct = kfuzztest_target *targ, + struct file_operations *fops) +{ + struct dentry *dir, *input; + int err =3D 0; + + dir =3D debugfs_create_dir(targ->name, st->kfuzztest_dir); + if (!dir) + err =3D -ENOMEM; + else if (IS_ERR(dir)) + err =3D PTR_ERR(dir); + if (err) { + pr_info("kfuzztest: failed to create /kfuzztest/%s dir", targ->name); + goto out; + } + + input =3D debugfs_create_file("input", KFUZZTEST_INPUT_PERMS, dir, NULL, = fops); + if (!input) + err =3D -ENOMEM; + else if (IS_ERR(input)) + err =3D PTR_ERR(input); + if (err) + pr_info("kfuzztest: failed to create /kfuzztest/%s/input", targ->name); +out: + return err; +} + +/** + * kfuzztest_init - initializes the debug filesystem for KFuzzTest + * + * Each registered target in the ".kfuzztest_targets" section gets its own + * subdirectory under "/sys/kernel/debug/kfuzztest/" containing= one + * write-only "input" file used for receiving inputs from userspace. + * Furthermore, a directory "/sys/kernel/debug/kfuzztest/_config" is creat= ed, + * containing two read-only files "minalign" and "num_invocations", that r= eturn + * the minimum required region alignment and number of target invocations + * respectively. + * + * @return 0 on success or an error + */ +static int __init kfuzztest_init(void) +{ + const struct kfuzztest_target *targ; + int err =3D 0; + int i =3D 0; + + state.num_targets =3D __kfuzztest_targets_end - __kfuzztest_targets_start; + state.target_fops =3D kzalloc(sizeof(struct file_operations) * state.num_= targets, GFP_KERNEL); + if (!state.target_fops) + return -ENOMEM; + + /* Create the main "kfuzztest" directory in /sys/kernel/debug. */ + state.kfuzztest_dir =3D debugfs_create_dir("kfuzztest", NULL); + if (!state.kfuzztest_dir) { + pr_warn("kfuzztest: could not create 'kfuzztest' debugfs directory"); + return -ENOMEM; + } + if (IS_ERR(state.kfuzztest_dir)) { + pr_warn("kfuzztest: could not create 'kfuzztest' debugfs directory"); + err =3D PTR_ERR(state.kfuzztest_dir); + state.kfuzztest_dir =3D NULL; + return err; + } + + err =3D initialize_config_dir(&state); + if (err) + goto cleanup_failure; + + for (targ =3D __kfuzztest_targets_start; targ < __kfuzztest_targets_end; = targ++, i++) { + state.target_fops[i] =3D (struct file_operations){ + .owner =3D THIS_MODULE, + .write =3D targ->write_input_cb, + }; + err =3D initialize_target_dir(&state, targ, &state.target_fops[i]); + /* Bail out if a single target fails to initialize. This avoids + * partial setup, and a failure here likely indicates an issue + * with debugfs. */ + if (err) + goto cleanup_failure; + pr_info("kfuzztest: registered target %s", targ->name); + } + return 0; + +cleanup_failure: + cleanup_kfuzztest_state(&state); + return err; +} + +static void __exit kfuzztest_exit(void) +{ + pr_info("kfuzztest: exiting"); + cleanup_kfuzztest_state(&state); +} + +module_init(kfuzztest_init); +module_exit(kfuzztest_exit); diff --git a/lib/kfuzztest/parse.c b/lib/kfuzztest/parse.c new file mode 100644 index 000000000000..5aaeca6a7fde --- /dev/null +++ b/lib/kfuzztest/parse.c @@ -0,0 +1,204 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * KFuzzTest input parsing and validation. + * + * Copyright 2025 Google LLC + */ +#include +#include + +static int kfuzztest_relocate_v0(struct reloc_region_array *regions, struc= t reloc_table *rt, + unsigned char *payload_start, unsigned char *payload_end) +{ + unsigned char *poison_start, *poison_end; + struct reloc_region reg, src, dst; + uintptr_t *ptr_location; + struct reloc_entry re; + size_t i; + int ret; + + /* Patch pointers. */ + for (i =3D 0; i < rt->num_entries; i++) { + re =3D rt->entries[i]; + src =3D regions->regions[re.region_id]; + ptr_location =3D (uintptr_t *)(payload_start + src.offset + re.region_of= fset); + if (re.value =3D=3D KFUZZTEST_REGIONID_NULL) + *ptr_location =3D (uintptr_t)NULL; + else if (re.value < regions->num_regions) { + dst =3D regions->regions[re.value]; + *ptr_location =3D (uintptr_t)(payload_start + dst.offset); + } else { + return -EINVAL; + } + } + + /* Poison the padding between regions. */ + for (i =3D 0; i < regions->num_regions; i++) { + reg =3D regions->regions[i]; + + /* Points to the beginning of the inter-region padding */ + poison_start =3D payload_start + reg.offset + reg.size; + if (i < regions->num_regions - 1) + poison_end =3D payload_start + regions->regions[i + 1].offset; + else + poison_end =3D payload_end; + + if (poison_end > payload_end) + return -EINVAL; + + ret =3D kasan_poison_range(poison_start, poison_end - poison_start); + if (ret) + return ret; + } + + /* Poison the padded area preceding the payload. */ + return kasan_poison_range(payload_start - rt->padding_size, rt->padding_s= ize); +} + +static bool kfuzztest_input_is_valid(struct reloc_region_array *regions, s= truct reloc_table *rt, + unsigned char *payload_start, unsigned char *payload_end) +{ + size_t payload_size =3D payload_end - payload_start; + struct reloc_region reg, next_reg; + size_t usable_payload_size; + uint32_t region_end_offset; + struct reloc_entry reloc; + uint32_t i; + + if (payload_start > payload_end) + return false; + if (payload_size < KFUZZTEST_POISON_SIZE) + return false; + if ((uintptr_t)payload_end % KFUZZTEST_POISON_SIZE) + return false; + usable_payload_size =3D payload_size - KFUZZTEST_POISON_SIZE; + + for (i =3D 0; i < regions->num_regions; i++) { + reg =3D regions->regions[i]; + if (check_add_overflow(reg.offset, reg.size, ®ion_end_offset)) + return false; + if ((size_t)region_end_offset > usable_payload_size) + return false; + + if (i < regions->num_regions - 1) { + next_reg =3D regions->regions[i + 1]; + if (reg.offset > next_reg.offset) + return false; + /* Enforce the minimum poisonable gap between + * consecutive regions. */ + if (reg.offset + reg.size + KFUZZTEST_POISON_SIZE > next_reg.offset) + return false; + } + } + + if (rt->padding_size < KFUZZTEST_POISON_SIZE) { + pr_info("validation failed because rt->padding_size =3D %u", rt->padding= _size); + return false; + } + + for (i =3D 0; i < rt->num_entries; i++) { + reloc =3D rt->entries[i]; + if (reloc.region_id >=3D regions->num_regions) + return false; + if (reloc.value !=3D KFUZZTEST_REGIONID_NULL && reloc.value >=3D regions= ->num_regions) + return false; + + reg =3D regions->regions[reloc.region_id]; + if (reloc.region_offset % (sizeof(uintptr_t)) || reloc.region_offset + s= izeof(uintptr_t) > reg.size) + return false; + } + + return true; +} + +static int kfuzztest_parse_input_v0(unsigned char *input, size_t input_siz= e, struct reloc_region_array **ret_regions, + struct reloc_table **ret_reloc_table, unsigned char **ret_payload_= start, + unsigned char **ret_payload_end) +{ + size_t reloc_entries_size, reloc_regions_size; + unsigned char *payload_end, *payload_start; + size_t reloc_table_size, regions_size; + struct reloc_region_array *regions; + struct reloc_table *rt; + size_t curr_offset =3D 0; + + if (input_size < sizeof(struct reloc_region_array) + sizeof(struct reloc_= table)) + return -EINVAL; + + regions =3D (struct reloc_region_array *)input; + if (check_mul_overflow(regions->num_regions, sizeof(struct reloc_region),= &reloc_regions_size)) + return -EINVAL; + if (check_add_overflow(sizeof(*regions), reloc_regions_size, ®ions_siz= e)) + return -EINVAL; + + curr_offset =3D regions_size; + if (curr_offset > input_size) + return -EINVAL; + if (input_size - curr_offset < sizeof(struct reloc_table)) + return -EINVAL; + + rt =3D (struct reloc_table *)(input + curr_offset); + + if (check_mul_overflow((size_t)rt->num_entries, sizeof(struct reloc_entry= ), &reloc_entries_size)) + return -EINVAL; + if (check_add_overflow(sizeof(*rt), reloc_entries_size, &reloc_table_size= )) + return -EINVAL; + if (check_add_overflow(reloc_table_size, rt->padding_size, &reloc_table_s= ize)) + return -EINVAL; + + if (check_add_overflow(curr_offset, reloc_table_size, &curr_offset)) + return -EINVAL; + if (curr_offset > input_size) + return -EINVAL; + + payload_start =3D input + curr_offset; + payload_end =3D input + input_size; + + if (!kfuzztest_input_is_valid(regions, rt, payload_start, payload_end)) + return -EINVAL; + + *ret_regions =3D regions; + *ret_reloc_table =3D rt; + *ret_payload_start =3D payload_start; + *ret_payload_end =3D payload_end; + return 0; +} + +static int kfuzztest_parse_and_relocate_v0(unsigned char *input, size_t in= put_size, void **arg_ret) +{ + unsigned char *payload_start, *payload_end; + struct reloc_region_array *regions; + struct reloc_table *reloc_table; + int ret; + + ret =3D kfuzztest_parse_input_v0(input, input_size, ®ions, &reloc_tabl= e, &payload_start, &payload_end); + if (ret < 0) + return ret; + + ret =3D kfuzztest_relocate_v0(regions, reloc_table, payload_start, payloa= d_end); + if (ret < 0) + return ret; + *arg_ret =3D (void *)payload_start; + return 0; +} + +int kfuzztest_parse_and_relocate(void *input, size_t input_size, void **ar= g_ret) +{ + size_t header_size =3D 2 * sizeof(u32); + u32 version, magic; + + if (input_size < sizeof(u32) + sizeof(u32)) + return -EINVAL; + + magic =3D *(u32 *)input; + if (magic !=3D KFUZZTEST_HEADER_MAGIC) + return -EINVAL; + + version =3D *(u32 *)(input + sizeof(u32)); + switch (version) { + case KFUZZTEST_V0: + return kfuzztest_parse_and_relocate_v0(input + header_size, input_size -= header_size, arg_ret); + } + + return -EINVAL; +} --=20 2.51.0.470.ga7dc726c21-goog From nobody Thu Oct 2 06:17:57 2025 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 542E93148DA for ; Fri, 19 Sep 2025 14:58:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293889; cv=none; b=HQiEUjIXTZT4TLu14YGzmyDMUJLztBDo5ixTT3nfRb1v+VqvOyHwui833sjnTq17MT1fU1TLtpvqoIPF5qVnvxletl8JsJjlTqqtnwhTPG/ZuVJ9DOPs/6ojIcsSr2yuwl7mWVf7+GmkFC1pbtVHKVl3OCXFaUqGSMcbpmrCli4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293889; c=relaxed/simple; bh=w4kHeid5YSaSqmwqHmvIad6g2j/F7aKotC8Gspu4gMQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kKDYpE7Mf2MQsj76IbRiEtInMXFmOHplUPWn2Ablm6qwaUwaCW9V0goeIswtIDk1KtqjsDU4Q1KpwoIBEDPt1fc4kqk0cQy1CfKVYs5P6cKgdDk8hID/CYzp/aMUfItv9L9NbwpwGmRdSTwV/KLGEOD6h5KQOreTypSiLYcqKFE= 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=GVN4i4dt; arc=none smtp.client-ip=209.85.221.48 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="GVN4i4dt" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-3e9c5faa858so1985010f8f.3 for ; Fri, 19 Sep 2025 07:58:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758293882; x=1758898682; 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; bh=KjNoX2ohWsElYB+mnl7Qo4CRrI4z8C6BPhJjZ7T0uQw=; b=GVN4i4dt1fonrVQICvjfY8zbe/eEvO+zpitL7vScFBDf9k62iKQtTGlGFLamxOQYjW 7E/u15eM24Kc2PmiSsBkLyifHDjYLIy8czpAzQkkmMb6WZ8F8wttf/j4JjkAnwyH7qMn hzIlZpw/f+dGZggoRH/cXVzewBPbhrtt74uGC9DBxmaeYppxKpntfJ5eDZP628onUQZA uoN7jIOMfFDRR3WFMdmChDqw6GbCIssa9KnU09mQGuzP+iXPNFY/gCHBlb98h9aDmM3Z ya+6rUAlqcGanzLbulhxZbRBSdizJ4KCVtJJJ4KFa1Q+1jEOHZ/gIMBgLpsWPtR/P8zO M+/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758293882; x=1758898682; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KjNoX2ohWsElYB+mnl7Qo4CRrI4z8C6BPhJjZ7T0uQw=; b=lTHnlP64RsHYn5hAm6rzZVEQMI5GeJz33bTfwKZyeLkPCUrB9wDJWgAthMePJGLdZ/ ZByvoYnuSOX0DJlKqXpI39iMPOoyl1uCo8DEMhpN32AC5vjdT9p8F+qWFm88KDcAtwBF f8lfqIogGrJCY80Mx+wKsfixN88HzYipdOZKGhsPFBbWEoAHKkcJM/39RxGrIeY9uC9J 20i5YgUR7SO7yFCeyXc7KMrEXmqyiKCjFglV9ovwdV3di6DBmjI42MlsylhruKQnqGOi 82fJYjT9gWNYU/JQSCoKgUh9cK6mJzKIBeMkYS088oFFYiKEWGPgv+2P8SITIBZDEVSk +Cnw== X-Forwarded-Encrypted: i=1; AJvYcCVX+7AXmXEiAZ/uinYKba0/oCR3OqaO9mzPuZmz/Xw/6rOFscb3UtHZ9aDFaRxBuiUml12FViD7GKaA4pI=@vger.kernel.org X-Gm-Message-State: AOJu0YxIa/wlp3ZrsFVMbkX/hMSiE7hm6S8z/ml6AzO8mUMky0m7POB3 4W+wB+bTI4XSYtTHKQjS93sYydVNMPsgCc2iLbliRCv5lWKDLbXBAwbt X-Gm-Gg: ASbGnctNGcn3Tk1mxYQ1KQjUgPguR3D13uqALKcLhbjvu6MyRY/asdCz4LjLLtK41hV oTZmVtXWgFt8XXtXBm+P7HyMT6BH5YgQfT1jzOdl5Lo5Q4jMJf/4gCpEAIaxuW3QvBAb9VB2khM qWXZ2XXXdACFk4fxz+nx5nGVRB+4uYUuIMpm/XXrHYhaezUXVav1zSODHL/jVHnURIOjZL6A4/K xLGnZlQEDrA11Y6IX/+s2t6iEcjE9iGRmLZj455KxaCOyRX53aSrtE+pxG5RFMhu/M47d4YJXPP BlgyzLPWZd1lseY7ALi0pvnzYo5E7IWxZ3DT1l7NfvlWioMO+BBFHQ+lMmUwugWJ51HtMhJISzb 7y6ARHqsKuyE1FXDsCiOixaZkKmQRdiJloKWT+ZnGsjQRf6G5w6KC3/bjhDOBvT7qcihJQUdIho ajwW+3pfCXKE4mSdo= X-Google-Smtp-Source: AGHT+IEE71vHERCEkXqqoRKRFTHeASlbAYteqGoc1FbBOldQ6MihQoaMVzCYyp6uH6aGkkZaiYnnbg== X-Received: by 2002:a05:6000:1a85:b0:3ec:a019:3936 with SMTP id ffacd0b85a97d-3ee86b8442emr2851921f8f.55.1758293882209; Fri, 19 Sep 2025 07:58:02 -0700 (PDT) Received: from xl-nested.c.googlers.com.com (124.62.78.34.bc.googleusercontent.com. [34.78.62.124]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3ee0fbc7188sm8551386f8f.37.2025.09.19.07.58.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 07:58:01 -0700 (PDT) From: Ethan Graham To: ethangraham@google.com, glider@google.com Cc: andreyknvl@gmail.com, andy@kernel.org, brauner@kernel.org, brendan.higgins@linux.dev, davem@davemloft.net, davidgow@google.com, dhowells@redhat.com, dvyukov@google.com, elver@google.com, herbert@gondor.apana.org.au, ignat@cloudflare.com, jack@suse.cz, jannh@google.com, johannes@sipsolutions.net, kasan-dev@googlegroups.com, kees@kernel.org, kunit-dev@googlegroups.com, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, lukas@wunner.de, rmoar@google.com, shuah@kernel.org, sj@kernel.org, tarasmadan@google.com Subject: [PATCH v2 04/10] tools: add kfuzztest-bridge utility Date: Fri, 19 Sep 2025 14:57:44 +0000 Message-ID: <20250919145750.3448393-5-ethan.w.s.graham@gmail.com> X-Mailer: git-send-email 2.51.0.470.ga7dc726c21-goog In-Reply-To: <20250919145750.3448393-1-ethan.w.s.graham@gmail.com> References: <20250919145750.3448393-1-ethan.w.s.graham@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" From: Ethan Graham Introduce the kfuzztest-bridge tool, a userspace utility for sending structured inputs to KFuzzTest harnesses via debugfs. The bridge takes a textual description of the expected input format, a file containing random bytes, and the name of the target fuzz test. It parses the description, encodes the random data into the binary format expected by the kernel, and writes the result to the corresponding debugfs entry. This allows for both simple manual testing and integration with userspace fuzzing engines. For example, it can be used for smoke testing by providing data from /dev/urandom, or act as a bridge for blob-based fuzzers (e.g., AFL) to target KFuzzTest harnesses. Signed-off-by: Ethan Graham Reviewed-by: Alexander Potapenko --- PR v2: - Move kfuzztest-bridge tool under tools/testing, as suggested by SeongJae Park. - Cleanup several resource leaks that were pointed out by Alexander Potapenko. PR v1: - Add additional context in header comment of kfuzztest-bridge/parser.c. - Add some missing NULL checks. - Refactor skip_whitespace() function in input_lexer.c. - Use ctx->minalign to compute correct region alignment, which is read from /sys/kernel/debug/kfuzztest/_config/minalign. --- --- tools/Makefile | 18 +- tools/testing/kfuzztest-bridge/.gitignore | 2 + tools/testing/kfuzztest-bridge/Build | 6 + tools/testing/kfuzztest-bridge/Makefile | 49 ++ tools/testing/kfuzztest-bridge/bridge.c | 115 +++++ tools/testing/kfuzztest-bridge/byte_buffer.c | 85 ++++ tools/testing/kfuzztest-bridge/byte_buffer.h | 31 ++ tools/testing/kfuzztest-bridge/encoder.c | 390 ++++++++++++++++ tools/testing/kfuzztest-bridge/encoder.h | 16 + tools/testing/kfuzztest-bridge/input_lexer.c | 256 +++++++++++ tools/testing/kfuzztest-bridge/input_lexer.h | 58 +++ tools/testing/kfuzztest-bridge/input_parser.c | 423 ++++++++++++++++++ tools/testing/kfuzztest-bridge/input_parser.h | 82 ++++ tools/testing/kfuzztest-bridge/rand_stream.c | 77 ++++ tools/testing/kfuzztest-bridge/rand_stream.h | 57 +++ 15 files changed, 1662 insertions(+), 3 deletions(-) create mode 100644 tools/testing/kfuzztest-bridge/.gitignore create mode 100644 tools/testing/kfuzztest-bridge/Build create mode 100644 tools/testing/kfuzztest-bridge/Makefile create mode 100644 tools/testing/kfuzztest-bridge/bridge.c create mode 100644 tools/testing/kfuzztest-bridge/byte_buffer.c create mode 100644 tools/testing/kfuzztest-bridge/byte_buffer.h create mode 100644 tools/testing/kfuzztest-bridge/encoder.c create mode 100644 tools/testing/kfuzztest-bridge/encoder.h create mode 100644 tools/testing/kfuzztest-bridge/input_lexer.c create mode 100644 tools/testing/kfuzztest-bridge/input_lexer.h create mode 100644 tools/testing/kfuzztest-bridge/input_parser.c create mode 100644 tools/testing/kfuzztest-bridge/input_parser.h create mode 100644 tools/testing/kfuzztest-bridge/rand_stream.c create mode 100644 tools/testing/kfuzztest-bridge/rand_stream.h diff --git a/tools/Makefile b/tools/Makefile index c31cbbd12c45..dfb0cd19aeb9 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -21,6 +21,7 @@ help: @echo ' hv - tools used when in Hyper-V clients' @echo ' iio - IIO tools' @echo ' intel-speed-select - Intel Speed Select tool' + @echo ' kfuzztest-bridge - KFuzzTest userspace utility' @echo ' kvm_stat - top-like utility for displaying kvm sta= tistics' @echo ' leds - LEDs tools' @echo ' nolibc - nolibc headers testing and installation' @@ -98,6 +99,9 @@ sched_ext: FORCE selftests: FORCE $(call descend,testing/$@) =20 +kfuzztest-bridge: FORCE + $(call descend,testing/kfuzztest-bridge) + thermal: FORCE $(call descend,lib/$@) =20 @@ -126,7 +130,8 @@ all: acpi counter cpupower gpio hv firewire \ perf selftests bootconfig spi turbostat usb \ virtio mm bpf x86_energy_perf_policy \ tmon freefall iio objtool kvm_stat wmi \ - debugging tracing thermal thermometer thermal-engine ynl + debugging tracing thermal thermometer thermal-engine ynl \ + kfuzztest-bridge =20 acpi_install: $(call descend,power/$(@:_install=3D),install) @@ -140,6 +145,9 @@ counter_install firewire_install gpio_install hv_instal= l iio_install perf_instal selftests_install: $(call descend,testing/$(@:_install=3D),install) =20 +kfuzztest-bridge_install: + $(call descend,testing/kfuzztest-bridge,install) + thermal_install: $(call descend,lib/$(@:_install=3D),install) =20 @@ -170,7 +178,8 @@ install: acpi_install counter_install cpupower_install = gpio_install \ virtio_install mm_install bpf_install x86_energy_perf_policy_install \ tmon_install freefall_install objtool_install kvm_stat_install \ wmi_install debugging_install intel-speed-select_install \ - tracing_install thermometer_install thermal-engine_install ynl_install + tracing_install thermometer_install thermal-engine_install ynl_install \ + kfuzztest-bridge_install =20 acpi_clean: $(call descend,power/acpi,clean) @@ -200,6 +209,9 @@ sched_ext_clean: selftests_clean: $(call descend,testing/$(@:_clean=3D),clean) =20 +kfuzztest-bridge_clean: + $(call descend,testing/kfuzztest-bridge,clean) + thermal_clean: $(call descend,lib/thermal,clean) =20 @@ -230,6 +242,6 @@ clean: acpi_clean counter_clean cpupower_clean hv_clean= firewire_clean \ freefall_clean build_clean libbpf_clean libsubcmd_clean \ gpio_clean objtool_clean leds_clean wmi_clean firmware_clean debugging_c= lean \ intel-speed-select_clean tracing_clean thermal_clean thermometer_clean t= hermal-engine_clean \ - sched_ext_clean ynl_clean + sched_ext_clean ynl_clean kfuzztest-bridge_clean =20 .PHONY: FORCE diff --git a/tools/testing/kfuzztest-bridge/.gitignore b/tools/testing/kfuz= ztest-bridge/.gitignore new file mode 100644 index 000000000000..4aa9fb0d44e2 --- /dev/null +++ b/tools/testing/kfuzztest-bridge/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +kfuzztest-bridge diff --git a/tools/testing/kfuzztest-bridge/Build b/tools/testing/kfuzztest= -bridge/Build new file mode 100644 index 000000000000..d07341a226d6 --- /dev/null +++ b/tools/testing/kfuzztest-bridge/Build @@ -0,0 +1,6 @@ +kfuzztest-bridge-y +=3D bridge.o +kfuzztest-bridge-y +=3D byte_buffer.o +kfuzztest-bridge-y +=3D encoder.o +kfuzztest-bridge-y +=3D input_lexer.o +kfuzztest-bridge-y +=3D input_parser.o +kfuzztest-bridge-y +=3D rand_stream.o diff --git a/tools/testing/kfuzztest-bridge/Makefile b/tools/testing/kfuzzt= est-bridge/Makefile new file mode 100644 index 000000000000..6e110bdeaee5 --- /dev/null +++ b/tools/testing/kfuzztest-bridge/Makefile @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: GPL-2.0 +# Makefile for KFuzzTest-Bridge +include ../../scripts/Makefile.include + +bindir ?=3D /usr/bin + +ifeq ($(srctree),) +srctree :=3D $(patsubst %/,%,$(dir $(CURDIR))) +srctree :=3D $(patsubst %/,%,$(dir $(srctree))) +srctree :=3D $(patsubst %/,%,$(dir $(srctree))) +endif + +MAKEFLAGS +=3D -r + +override CFLAGS +=3D -O2 -g +override CFLAGS +=3D -Wall -Wextra +override CFLAGS +=3D -D_GNU_SOURCE +override CFLAGS +=3D -I$(OUTPUT)include -I$(srctree)/tools/include + +ALL_TARGETS :=3D kfuzztest-bridge +ALL_PROGRAMS :=3D $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS)) + +KFUZZTEST_BRIDGE_IN :=3D $(OUTPUT)kfuzztest-bridge-in.o +KFUZZTEST_BRIDGE :=3D $(OUTPUT)kfuzztest-bridge + +all: $(ALL_PROGRAMS) + +export srctree OUTPUT CC LD CFLAGS +include $(srctree)/tools/build/Makefile.include + +$(KFUZZTEST_BRIDGE_IN): FORCE + $(Q)$(MAKE) $(build)=3Dkfuzztest-bridge + +$(KFUZZTEST_BRIDGE): $(KFUZZTEST_BRIDGE_IN) + $(QUIET_LINK)$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) + +clean: + rm -f $(ALL_PROGRAMS) + find $(or $(OUTPUT),.) -name '*.o' -delete -o -name '\.*.d' -delete -o -n= ame '\.*.o.cmd' -delete + +install: $(ALL_PROGRAMS) + install -d -m 755 $(DESTDIR)$(bindir); \ + for program in $(ALL_PROGRAMS); do \ + install $$program $(DESTDIR)$(bindir); \ + done + +FORCE: + +.PHONY: all install clean FORCE prepare diff --git a/tools/testing/kfuzztest-bridge/bridge.c b/tools/testing/kfuzzt= est-bridge/bridge.c new file mode 100644 index 000000000000..aec0eb4e9ff7 --- /dev/null +++ b/tools/testing/kfuzztest-bridge/bridge.c @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KFuzzTest tool for sending inputs into a KFuzzTest harness + * + * Copyright 2025 Google LLC + */ +#include +#include +#include +#include +#include + +#include "byte_buffer.h" +#include "encoder.h" +#include "input_lexer.h" +#include "input_parser.h" +#include "rand_stream.h" + +static int invoke_kfuzztest_target(const char *target_name, const char *da= ta, ssize_t data_size) +{ + ssize_t bytes_written; + char *buf =3D NULL; + int ret; + int fd; + + if (asprintf(&buf, "/sys/kernel/debug/kfuzztest/%s/input", target_name) <= 0) + return -ENOMEM; + + fd =3D openat(AT_FDCWD, buf, O_WRONLY, 0); + if (fd < 0) { + ret =3D -errno; + goto out_free; + } + + /* + * A KFuzzTest target's debugfs handler expects the entire input to be + * written in a single contiguous blob. Treat partial writes as errors. + */ + bytes_written =3D write(fd, data, data_size); + if (bytes_written !=3D data_size) { + ret =3D (bytes_written < 0) ? -errno : -EIO; + goto out_close; + } + ret =3D 0; + +out_close: + if (close(fd) !=3D 0 && ret =3D=3D 0) + ret =3D -errno; +out_free: + free(buf); + return ret; +} + +static int invoke_one(const char *input_fmt, const char *fuzz_target, cons= t char *input_filepath) +{ + struct ast_node *ast_prog; + struct byte_buffer *bb; + struct rand_stream *rs; + struct token **tokens; + size_t num_tokens; + size_t num_bytes; + int err; + + err =3D tokenize(input_fmt, &tokens, &num_tokens); + if (err) { + fprintf(stderr, "tokenization failed: %s\n", strerror(-err)); + return err; + } + + err =3D parse(tokens, num_tokens, &ast_prog); + if (err) { + fprintf(stderr, "parsing failed: %s\n", strerror(-err)); + goto cleanup_tokens; + } + + rs =3D new_rand_stream(input_filepath, 1024); + if (!rs) { + err =3D -ENOMEM; + goto cleanup_ast; + } + + err =3D encode(ast_prog, rs, &num_bytes, &bb); + if (err =3D=3D STREAM_EOF) { + fprintf(stderr, "encoding failed: reached EOF in %s\n", input_filepath); + err =3D -EINVAL; + goto cleanup_rs; + } else if (err) { + fprintf(stderr, "encoding failed: %s\n", strerror(-err)); + goto cleanup_rs; + } + + err =3D invoke_kfuzztest_target(fuzz_target, bb->buffer, (ssize_t)num_byt= es); + if (err) + fprintf(stderr, "invocation failed: %s\n", strerror(-err)); + + destroy_byte_buffer(bb); +cleanup_rs: + destroy_rand_stream(rs); +cleanup_ast: + destroy_ast_node(ast_prog); +cleanup_tokens: + destroy_tokens(tokens, num_tokens); + return err; +} + +int main(int argc, char *argv[]) +{ + if (argc !=3D 4) { + printf("Usage: %s \n"= , argv[0]); + printf("For more detailed information see Documentation/dev-tools/kfuzzt= est.rst\n"); + return 1; + } + + return invoke_one(argv[1], argv[2], argv[3]); +} diff --git a/tools/testing/kfuzztest-bridge/byte_buffer.c b/tools/testing/k= fuzztest-bridge/byte_buffer.c new file mode 100644 index 000000000000..1974dbf3862e --- /dev/null +++ b/tools/testing/kfuzztest-bridge/byte_buffer.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A simple byte buffer implementation for encoding binary data + * + * Copyright 2025 Google LLC + */ +#include +#include +#include + +#include "byte_buffer.h" + +struct byte_buffer *new_byte_buffer(size_t initial_size) +{ + struct byte_buffer *ret; + size_t alloc_size =3D initial_size >=3D 8 ? initial_size : 8; + + ret =3D malloc(sizeof(*ret)); + if (!ret) + return NULL; + + ret->alloc_size =3D alloc_size; + ret->buffer =3D malloc(alloc_size); + if (!ret->buffer) { + free(ret); + return NULL; + } + ret->num_bytes =3D 0; + return ret; +} + +void destroy_byte_buffer(struct byte_buffer *buf) +{ + free(buf->buffer); + free(buf); +} + +int append_bytes(struct byte_buffer *buf, const char *bytes, size_t num_by= tes) +{ + size_t req_size; + size_t new_size; + char *new_ptr; + + req_size =3D buf->num_bytes + num_bytes; + new_size =3D buf->alloc_size; + + while (req_size > new_size) + new_size *=3D 2; + if (new_size !=3D buf->alloc_size) { + new_ptr =3D realloc(buf->buffer, new_size); + if (!new_ptr) + return -ENOMEM; + buf->buffer =3D new_ptr; + buf->alloc_size =3D new_size; + } + memcpy(buf->buffer + buf->num_bytes, bytes, num_bytes); + buf->num_bytes +=3D num_bytes; + return 0; +} + +int append_byte(struct byte_buffer *buf, char c) +{ + return append_bytes(buf, &c, 1); +} + +int encode_le(struct byte_buffer *buf, uint64_t value, size_t byte_width) +{ + size_t i; + int ret; + + for (i =3D 0; i < byte_width; ++i) + if ((ret =3D append_byte(buf, (uint8_t)((value >> (i * 8)) & 0xFF)))) + return ret; + return 0; +} + +int pad(struct byte_buffer *buf, size_t num_padding) +{ + int ret; + size_t i; + for (i =3D 0; i < num_padding; i++) + if ((ret =3D append_byte(buf, 0))) + return ret; + return 0; +} diff --git a/tools/testing/kfuzztest-bridge/byte_buffer.h b/tools/testing/k= fuzztest-bridge/byte_buffer.h new file mode 100644 index 000000000000..6a31bfb5e78f --- /dev/null +++ b/tools/testing/kfuzztest-bridge/byte_buffer.h @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A simple byte buffer implementation for encoding binary data + * + * Copyright 2025 Google LLC + */ +#ifndef KFUZZTEST_BRIDGE_BYTE_BUFFER_H +#define KFUZZTEST_BRIDGE_BYTE_BUFFER_H + +#include +#include + +struct byte_buffer { + char *buffer; + size_t num_bytes; + size_t alloc_size; +}; + +struct byte_buffer *new_byte_buffer(size_t initial_size); + +void destroy_byte_buffer(struct byte_buffer *buf); + +int append_bytes(struct byte_buffer *buf, const char *bytes, size_t num_by= tes); + +int append_byte(struct byte_buffer *buf, char c); + +int encode_le(struct byte_buffer *buf, uint64_t value, size_t byte_width); + +int pad(struct byte_buffer *buf, size_t num_padding); + +#endif /* KFUZZTEST_BRIDGE_BYTE_BUFFER_H */ diff --git a/tools/testing/kfuzztest-bridge/encoder.c b/tools/testing/kfuzz= test-bridge/encoder.c new file mode 100644 index 000000000000..11ff5bd589d3 --- /dev/null +++ b/tools/testing/kfuzztest-bridge/encoder.c @@ -0,0 +1,390 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Encoder for KFuzzTest binary input format + * + * Copyright 2025 Google LLC + */ +#include +#include +#include +#include +#include + +#include "byte_buffer.h" +#include "input_parser.h" +#include "rand_stream.h" + +#define KFUZZTEST_MAGIC 0xBFACE +#define KFUZZTEST_PROTO_VERSION 0 + +/*=20 + * The KFuzzTest binary input format requires at least 8 bytes of padding + * at the head and tail of every region. + */ +#define KFUZZTEST_POISON_SIZE 8 + +#define BUFSIZE_SMALL 32 +#define BUFSIZE_LARGE 128 + +struct region_info { + const char *name; + uint32_t offset; + uint32_t size; +}; + +struct reloc_info { + uint32_t src_reg; + uint32_t offset; + uint32_t dst_reg; +}; + +struct encoder_ctx { + struct byte_buffer *payload; + struct rand_stream *rand; + + struct region_info *regions; + size_t num_regions; + + struct reloc_info *relocations; + size_t num_relocations; + + size_t minalign; + size_t reg_offset; + int curr_reg; +}; + +static void cleanup_ctx(struct encoder_ctx *ctx) +{ + if (ctx->regions) + free(ctx->regions); + if (ctx->relocations) + free(ctx->relocations); + if (ctx->payload) + destroy_byte_buffer(ctx->payload); +} + +static int read_minalign(struct encoder_ctx *ctx) +{ + const char *minalign_file =3D "/sys/kernel/debug/kfuzztest/_config/minali= gn"; + char buffer[64 + 1] =3D { 0 }; + int ret =3D 0; + + FILE *f =3D fopen(minalign_file, "r"); + if (!f) + return -ENOENT; + + fread(&buffer, 1, sizeof(buffer) - 1, f); + if (ferror(f)) + return ferror(f); + + /* + * atoi returns 0 on error. Since we expect a strictly positive + * minalign value on all architectures, any non-positive value + * represents an error. + */ + ret =3D atoi(buffer); + if (ret <=3D 0) { + fclose(f); + return -EINVAL; + } + ctx->minalign =3D ret; + fclose(f); + return 0; +} + +static int pad_payload(struct encoder_ctx *ctx, size_t amount) +{ + int ret; + + if ((ret =3D pad(ctx->payload, amount))) + return ret; + ctx->reg_offset +=3D amount; + return 0; +} + +static int align_payload(struct encoder_ctx *ctx, size_t alignment) +{ + size_t pad_amount =3D ROUND_UP_TO_MULTIPLE(ctx->payload->num_bytes, align= ment) - ctx->payload->num_bytes; + return pad_payload(ctx, pad_amount); +} + +static int lookup_reg(struct encoder_ctx *ctx, const char *name) +{ + size_t i; + + for (i =3D 0; i < ctx->num_regions; i++) { + if (strcmp(ctx->regions[i].name, name) =3D=3D 0) + return i; + } + return -ENOENT; +} + +static int add_reloc(struct encoder_ctx *ctx, struct reloc_info reloc) +{ + void *new_ptr =3D realloc(ctx->relocations, (ctx->num_relocations + 1) * = sizeof(struct reloc_info)); + if (!new_ptr) + return -ENOMEM; + + ctx->relocations =3D new_ptr; + ctx->relocations[ctx->num_relocations] =3D reloc; + ctx->num_relocations++; + return 0; +} + +static int build_region_map(struct encoder_ctx *ctx, struct ast_node *top_= level) +{ + struct ast_program *prog; + struct ast_node *reg; + size_t i; + + if (top_level->type !=3D NODE_PROGRAM) + return -EINVAL; + + prog =3D &top_level->data.program; + ctx->regions =3D malloc(prog->num_members * sizeof(struct region_info)); + if (!ctx->regions) + return -ENOMEM; + + ctx->num_regions =3D prog->num_members; + for (i =3D 0; i < ctx->num_regions; i++) { + reg =3D prog->members[i]; + /* Offset is determined after the second pass. */ + ctx->regions[i] =3D (struct region_info){ + .name =3D reg->data.region.name, + .size =3D node_size(reg), + }; + } + return 0; +} +/** + * Encodes a value node as little-endian. A value node is one that has no + * children, and can therefore be directly written into the payload. + */ +static int encode_value_le(struct encoder_ctx *ctx, struct ast_node *node) +{ + size_t array_size; + char rand_char; + size_t length; + size_t i; + int reg; + int ret; + + switch (node->type) { + case NODE_ARRAY: + array_size =3D node->data.array.num_elems * node->data.array.elem_size; + for (i =3D 0; i < array_size; i++) { + if ((ret =3D next_byte(ctx->rand, &rand_char))) + return ret; + if ((ret =3D append_byte(ctx->payload, rand_char))) + return ret; + } + ctx->reg_offset +=3D array_size; + if (node->data.array.null_terminated) { + if ((ret =3D pad_payload(ctx, 1))) + return ret; + ctx->reg_offset++; + } + break; + case NODE_LENGTH: + reg =3D lookup_reg(ctx, node->data.length.length_of); + if (reg < 0) + return reg; + length =3D ctx->regions[reg].size; + if ((ret =3D encode_le(ctx->payload, length, node->data.length.byte_widt= h))) + return ret; + ctx->reg_offset +=3D node->data.length.byte_width; + break; + case NODE_PRIMITIVE: + for (i =3D 0; i < node->data.primitive.byte_width; i++) { + if ((ret =3D next_byte(ctx->rand, &rand_char))) + return ret; + if ((ret =3D append_byte(ctx->payload, rand_char))) + return ret; + } + ctx->reg_offset +=3D node->data.primitive.byte_width; + break; + case NODE_POINTER: + reg =3D lookup_reg(ctx, node->data.pointer.points_to); + if (reg < 0) + return reg; + if ((ret =3D add_reloc(ctx, (struct reloc_info){ .src_reg =3D ctx->curr_= reg, + .offset =3D ctx->reg_offset, + .dst_reg =3D reg }))) + return ret; + /* Placeholder pointer value, as pointers are patched by KFuzzTest anywa= ys. */ + if ((ret =3D encode_le(ctx->payload, UINTPTR_MAX, sizeof(uintptr_t)))) + return ret; + ctx->reg_offset +=3D sizeof(uintptr_t); + break; + case NODE_PROGRAM: + case NODE_REGION: + default: + return -EINVAL; + } + return 0; +} + +static int encode_region(struct encoder_ctx *ctx, struct ast_region *reg) +{ + struct ast_node *child; + size_t i; + int ret; + + ctx->reg_offset =3D 0; + for (i =3D 0; i < reg->num_members; i++) { + child =3D reg->members[i]; + if ((ret =3D align_payload(ctx, node_alignment(child)))) + return ret; + if ((ret =3D encode_value_le(ctx, child))) + return ret; + } + return 0; +} + +static int encode_payload(struct encoder_ctx *ctx, struct ast_node *top_le= vel) +{ + struct ast_node *reg; + size_t i; + int ret; + + for (i =3D 0; i < ctx->num_regions; i++) { + reg =3D top_level->data.program.members[i]; + if ((ret =3D align_payload(ctx, MAX(ctx->minalign, node_alignment(reg)))= )) + return ret; + + ctx->curr_reg =3D i; + ctx->regions[i].offset =3D ctx->payload->num_bytes; + if ((ret =3D encode_region(ctx, ®->data.region))) + return ret; + if ((ret =3D pad_payload(ctx, KFUZZTEST_POISON_SIZE))) + return ret; + } + return align_payload(ctx, ctx->minalign); +} + +static int encode_region_array(struct encoder_ctx *ctx, struct byte_buffer= **ret) +{ + struct byte_buffer *reg_array; + struct region_info info; + int retcode; + size_t i; + + reg_array =3D new_byte_buffer(BUFSIZE_SMALL); + if (!reg_array) + return -ENOMEM; + + if ((retcode =3D encode_le(reg_array, ctx->num_regions, sizeof(uint32_t))= )) + goto fail; + + for (i =3D 0; i < ctx->num_regions; i++) { + info =3D ctx->regions[i]; + if ((retcode =3D encode_le(reg_array, info.offset, sizeof(uint32_t)))) + goto fail; + if ((retcode =3D encode_le(reg_array, info.size, sizeof(uint32_t)))) + goto fail; + } + *ret =3D reg_array; + return 0; + +fail: + destroy_byte_buffer(reg_array); + return retcode; +} + +static int encode_reloc_table(struct encoder_ctx *ctx, size_t padding_amou= nt, struct byte_buffer **ret) +{ + struct byte_buffer *reloc_table; + struct reloc_info info; + int retcode; + size_t i; + + reloc_table =3D new_byte_buffer(BUFSIZE_SMALL); + if (!reloc_table) + return -ENOMEM; + + if ((retcode =3D encode_le(reloc_table, ctx->num_relocations, sizeof(uint= 32_t))) || + (retcode =3D encode_le(reloc_table, padding_amount, sizeof(uint32_t))= )) + goto fail; + + for (i =3D 0; i < ctx->num_relocations; i++) { + info =3D ctx->relocations[i]; + if ((retcode =3D encode_le(reloc_table, info.src_reg, sizeof(uint32_t)))= || + (retcode =3D encode_le(reloc_table, info.offset, sizeof(uint32_t))) = || + (retcode =3D encode_le(reloc_table, info.dst_reg, sizeof(uint32_t)))) + goto fail; + } + pad(reloc_table, padding_amount); + *ret =3D reloc_table; + return 0; + +fail: + destroy_byte_buffer(reloc_table); + return retcode; +} + +static size_t reloc_table_size(struct encoder_ctx *ctx) +{ + return 2 * sizeof(uint32_t) + 3 * ctx->num_relocations * sizeof(uint32_t); +} + +int encode(struct ast_node *top_level, struct rand_stream *r, size_t *num_= bytes, struct byte_buffer **ret) +{ + struct byte_buffer *region_array =3D NULL; + struct byte_buffer *final_buffer =3D NULL; + struct byte_buffer *reloc_table =3D NULL; + size_t header_size; + int alignment; + int retcode; + + struct encoder_ctx ctx =3D { 0 }; + if ((retcode =3D read_minalign(&ctx))) + return retcode; + + if ((retcode =3D build_region_map(&ctx, top_level))) + goto fail; + + ctx.rand =3D r; + ctx.payload =3D new_byte_buffer(BUFSIZE_SMALL); + if (!ctx.payload) { + retcode =3D -ENOMEM; + goto fail; + } + if ((retcode =3D encode_payload(&ctx, top_level))) + goto fail; + + if ((retcode =3D encode_region_array(&ctx, ®ion_array))) + goto fail; + + header_size =3D sizeof(uint64_t) + region_array->num_bytes + reloc_table_= size(&ctx); + alignment =3D node_alignment(top_level); + if ((retcode =3D encode_reloc_table( + &ctx, ROUND_UP_TO_MULTIPLE(header_size + KFUZZTEST_POISON_SIZE, ali= gnment) - header_size, + &reloc_table))) + goto fail; + + final_buffer =3D new_byte_buffer(BUFSIZE_LARGE); + if (!final_buffer) { + retcode =3D -ENOMEM; + goto fail; + } + + if ((retcode =3D encode_le(final_buffer, KFUZZTEST_MAGIC, sizeof(uint32_t= ))) || + (retcode =3D encode_le(final_buffer, KFUZZTEST_PROTO_VERSION, sizeof(= uint32_t))) || + (retcode =3D append_bytes(final_buffer, region_array->buffer, region_= array->num_bytes)) || + (retcode =3D append_bytes(final_buffer, reloc_table->buffer, reloc_ta= ble->num_bytes)) || + (retcode =3D append_bytes(final_buffer, ctx.payload->buffer, ctx.payl= oad->num_bytes))) { + destroy_byte_buffer(final_buffer); + goto fail; + } + + *num_bytes =3D final_buffer->num_bytes; + *ret =3D final_buffer; + +fail: + if (region_array) + destroy_byte_buffer(region_array); + if (reloc_table) + destroy_byte_buffer(reloc_table); + cleanup_ctx(&ctx); + return retcode; +} diff --git a/tools/testing/kfuzztest-bridge/encoder.h b/tools/testing/kfuzz= test-bridge/encoder.h new file mode 100644 index 000000000000..73f8c4b7893c --- /dev/null +++ b/tools/testing/kfuzztest-bridge/encoder.h @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Encoder for KFuzzTest binary input format + * + * Copyright 2025 Google LLC + */ +#ifndef KFUZZTEST_BRIDGE_ENCODER_H +#define KFUZZTEST_BRIDGE_ENCODER_H + +#include "input_parser.h" +#include "rand_stream.h" +#include "byte_buffer.h" + +int encode(struct ast_node *top_level, struct rand_stream *r, size_t *num_= bytes, struct byte_buffer **ret); + +#endif /* KFUZZTEST_BRIDGE_ENCODER_H */ diff --git a/tools/testing/kfuzztest-bridge/input_lexer.c b/tools/testing/k= fuzztest-bridge/input_lexer.c new file mode 100644 index 000000000000..d0a3e352a265 --- /dev/null +++ b/tools/testing/kfuzztest-bridge/input_lexer.c @@ -0,0 +1,256 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Parser for KFuzzTest textual input format + * + * Copyright 2025 Google LLC + */ +#include +#include +#include +#include +#include +#include + +#include "input_lexer.h" + +struct keyword_map { + const char *keyword; + enum token_type type; +}; + +static struct keyword_map keywords[] =3D { + { "ptr", TOKEN_KEYWORD_PTR }, { "arr", TOKEN_KEYWORD_ARR }, + { "len", TOKEN_KEYWORD_LEN }, { "str", TOKEN_KEYWORD_STR }, + { "u8", TOKEN_KEYWORD_U8 }, { "u16", TOKEN_KEYWORD_U16 }, + { "u32", TOKEN_KEYWORD_U32 }, { "u64", TOKEN_KEYWORD_U64 }, +}; + +static struct token *make_token(enum token_type type, size_t position) +{ + struct token *ret =3D calloc(1, sizeof(*ret)); + ret->position =3D position; + ret->type =3D type; + return ret; +} + +void destroy_tokens(struct token **tokens, size_t num_tokens) +{ + size_t i; + + if (!tokens) + return; + + for (i =3D 0; i < num_tokens; i++) + if (tokens[i]) + free(tokens[i]); + free(tokens); +} + +struct lexer { + const char *start; + const char *current; + size_t position; +}; + +static char advance(struct lexer *l) +{ + l->current++; + l->position++; + return l->current[-1]; +} + +static void retreat(struct lexer *l) +{ + l->position--; + l->current--; +} + +static char peek(struct lexer *l) +{ + return *l->current; +} + +static bool is_digit(char c) +{ + return c >=3D '0' && c <=3D '9'; +} + +static bool is_alpha(char c) +{ + return (c >=3D 'a' && c <=3D 'z') || (c >=3D 'A' && c <=3D 'Z'); +} + +static bool is_whitespace(char c) +{ + switch (c) { + case ' ': + case '\r': + case '\t': + case '\n': + return true; + default: + return false; + } +} + +static void skip_whitespace(struct lexer *l) +{ + while (is_whitespace(peek(l))) + advance(l); +} + +static struct token *number(struct lexer *l) +{ + struct token *tok; + uint64_t value; + while (is_digit(peek(l))) + advance(l); + value =3D strtoull(l->start, NULL, 10); + tok =3D make_token(TOKEN_INTEGER, l->position); + tok->data.integer =3D value; + return tok; +} + +static enum token_type check_keyword(struct lexer *l, const char *keyword, + enum token_type type) +{ + size_t len =3D strlen(keyword); + + if (((size_t)(l->current - l->start) =3D=3D len) && + strncmp(l->start, keyword, len) =3D=3D 0) + return type; + return TOKEN_IDENTIFIER; +} + +static struct token *identifier(struct lexer *l) +{ + enum token_type type =3D TOKEN_IDENTIFIER; + struct token *tok; + size_t i; + + while (is_digit(peek(l)) || is_alpha(peek(l)) || peek(l) =3D=3D '_') + advance(l); + + for (i =3D 0; i < ARRAY_SIZE(keywords); i++) { + if (check_keyword(l, keywords[i].keyword, keywords[i].type) !=3D + TOKEN_IDENTIFIER) { + type =3D keywords[i].type; + break; + } + } + + tok =3D make_token(type, l->position); + if (!tok) + return NULL; + if (type =3D=3D TOKEN_IDENTIFIER) { + tok->data.identifier.start =3D l->start; + tok->data.identifier.length =3D l->current - l->start; + } + return tok; +} + +static struct token *scan_token(struct lexer *l) +{ + char c; + skip_whitespace(l); + + l->start =3D l->current; + c =3D peek(l); + + if (c =3D=3D '\0') + return make_token(TOKEN_EOF, l->position); + + advance(l); + switch (c) { + case '{': + return make_token(TOKEN_LBRACE, l->position); + case '}': + return make_token(TOKEN_RBRACE, l->position); + case '[': + return make_token(TOKEN_LBRACKET, l->position); + case ']': + return make_token(TOKEN_RBRACKET, l->position); + case ',': + return make_token(TOKEN_COMMA, l->position); + case ';': + return make_token(TOKEN_SEMICOLON, l->position); + default: + retreat(l); + if (is_digit(c)) + return number(l); + if (is_alpha(c) || c =3D=3D '_') + return identifier(l); + return make_token(TOKEN_ERROR, l->position); + } +} + +int primitive_byte_width(enum token_type type) +{ + switch (type) { + case TOKEN_KEYWORD_U8: + return 1; + case TOKEN_KEYWORD_U16: + return 2; + case TOKEN_KEYWORD_U32: + return 4; + case TOKEN_KEYWORD_U64: + return 8; + default: + return 0; + } +} + +int tokenize(const char *input, struct token ***tokens, size_t *num_tokens) +{ + struct lexer l =3D { .start =3D input, .current =3D input }; + struct token **ret_tokens; + size_t token_arr_size; + size_t token_count; + struct token *tok; + void *tmp; + int err; + + token_arr_size =3D 128; + ret_tokens =3D calloc(token_arr_size, sizeof(struct token *)); + if (!ret_tokens) + return -ENOMEM; + + token_count =3D 0; + do { + tok =3D scan_token(&l); + if (!tok) { + err =3D -ENOMEM; + goto failure; + } + + if (token_count =3D=3D token_arr_size) { + token_arr_size *=3D 2; + tmp =3D realloc(ret_tokens, token_arr_size); + if (!tmp) { + err =3D -ENOMEM; + goto failure; + } + ret_tokens =3D tmp; + } + + ret_tokens[token_count] =3D tok; + if (tok->type =3D=3D TOKEN_ERROR) { + err =3D -EINVAL; + goto failure; + } + token_count++; + } while (tok->type !=3D TOKEN_EOF); + + *tokens =3D ret_tokens; + *num_tokens =3D token_count; + return 0; + +failure: + destroy_tokens(ret_tokens, token_count); + return err; +} + +bool is_primitive(struct token *tok) +{ + return tok->type >=3D TOKEN_KEYWORD_U8 && tok->type <=3D TOKEN_KEYWORD_U6= 4; +} diff --git a/tools/testing/kfuzztest-bridge/input_lexer.h b/tools/testing/k= fuzztest-bridge/input_lexer.h new file mode 100644 index 000000000000..40814493c24d --- /dev/null +++ b/tools/testing/kfuzztest-bridge/input_lexer.h @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Lexer for KFuzzTest textual input format + * + * Copyright 2025 Google LLC + */ +#ifndef KFUZZTEST_BRIDGE_INPUT_LEXER_H +#define KFUZZTEST_BRIDGE_INPUT_LEXER_H + +#include +#include +#include + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) + +enum token_type { + TOKEN_LBRACE, + TOKEN_RBRACE, + TOKEN_LBRACKET, + TOKEN_RBRACKET, + TOKEN_COMMA, + TOKEN_SEMICOLON, + + TOKEN_KEYWORD_PTR, + TOKEN_KEYWORD_ARR, + TOKEN_KEYWORD_LEN, + TOKEN_KEYWORD_STR, + TOKEN_KEYWORD_U8, + TOKEN_KEYWORD_U16, + TOKEN_KEYWORD_U32, + TOKEN_KEYWORD_U64, + + TOKEN_IDENTIFIER, + TOKEN_INTEGER, + + TOKEN_EOF, + TOKEN_ERROR, +}; + +struct token { + enum token_type type; + union { + uint64_t integer; + struct { + const char *start; + size_t length; + } identifier; + } data; + int position; +}; + +int tokenize(const char *input, struct token ***tokens, size_t *num_tokens= ); +void destroy_tokens(struct token **tokens, size_t num_tokens); + +bool is_primitive(struct token *tok); +int primitive_byte_width(enum token_type type); + +#endif /* KFUZZTEST_BRIDGE_INPUT_LEXER_H */ diff --git a/tools/testing/kfuzztest-bridge/input_parser.c b/tools/testing/= kfuzztest-bridge/input_parser.c new file mode 100644 index 000000000000..b1fd8ba5217e --- /dev/null +++ b/tools/testing/kfuzztest-bridge/input_parser.c @@ -0,0 +1,423 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Parser for the KFuzzTest textual input format + * + * This file implements a parser for a simple DSL used to describe C-like = data + * structures. This format allows the kfuzztest-bridge tool to encode a ra= ndom + * byte stream into the structured binary format expected by a KFuzzTest + * harness. + * + * The format consists of semicolon-separated "regions," which are analogo= us to + * C structs. For example: + * + * "my_struct { ptr[buf] len[buf, u64] }; buf { arr[u8, 42] };" + * + * This describes a `my_struct` region that contains a pointer to a `buf` = region + * and its corresponding length encoded over 8 bytes, where `buf` itself + * contains a 42-byte array. + * + * Copyright 2025 Google LLC + */ +#include +#include +#include + +#include "input_lexer.h" +#include "input_parser.h" + +static struct token *peek(struct parser *p) +{ + return p->tokens[p->curr_token]; +} + +static struct token *advance(struct parser *p) +{ + struct token *tok; + if (p->curr_token >=3D p->token_count) + return NULL; + tok =3D peek(p); + p->curr_token++; + return tok; +} + +static struct token *consume(struct parser *p, enum token_type type, const= char *err_msg) +{ + if (peek(p)->type !=3D type) { + printf("parser failure at position %d: %s\n", peek(p)->position, err_msg= ); + return NULL; + } + return advance(p); +} + +static bool match(struct parser *p, enum token_type t) +{ + struct token *tok =3D peek(p); + return tok->type =3D=3D t; +} + +static int parse_primitive(struct parser *p, struct ast_node **node_ret) +{ + struct ast_node *ret; + struct token *tok; + int byte_width; + + tok =3D advance(p); + byte_width =3D primitive_byte_width(tok->type); + if (!byte_width) + return -EINVAL; + + ret =3D malloc(sizeof(*ret)); + if (!ret) + return -ENOMEM; + + ret->type =3D NODE_PRIMITIVE; + ret->data.primitive.byte_width =3D byte_width; + *node_ret =3D ret; + return 0; +} + +static int parse_ptr(struct parser *p, struct ast_node **node_ret) +{ + const char *points_to; + struct ast_node *ret; + struct token *tok; + if (!consume(p, TOKEN_KEYWORD_PTR, "expected 'ptr'")) + return -EINVAL; + if (!consume(p, TOKEN_LBRACKET, "expected '['")) + return -EINVAL; + + tok =3D consume(p, TOKEN_IDENTIFIER, "expected identifier"); + if (!tok) + return -EINVAL; + + if (!consume(p, TOKEN_RBRACKET, "expected ']'")) + return -EINVAL; + + ret =3D malloc(sizeof(*ret)); + ret->type =3D NODE_POINTER; + + points_to =3D strndup(tok->data.identifier.start, tok->data.identifier.le= ngth); + if (!points_to) { + free(ret); + return -EINVAL; + } + + ret->data.pointer.points_to =3D points_to; + *node_ret =3D ret; + return 0; +} + +static int parse_arr(struct parser *p, struct ast_node **node_ret) +{ + struct token *type, *num_elems; + struct ast_node *ret; + + if (!consume(p, TOKEN_KEYWORD_ARR, "expected 'arr'") || !consume(p, TOKEN= _LBRACKET, "expected '['")) + return -EINVAL; + + type =3D advance(p); + if (!is_primitive(type)) + return -EINVAL; + + if (!consume(p, TOKEN_COMMA, "expected ','")) + return -EINVAL; + + num_elems =3D consume(p, TOKEN_INTEGER, "expected integer"); + if (!num_elems) + return -EINVAL; + + if (!consume(p, TOKEN_RBRACKET, "expected ']'")) + return -EINVAL; + + ret =3D malloc(sizeof(*ret)); + if (!ret) + return -ENOMEM; + + ret->type =3D NODE_ARRAY; + ret->data.array.num_elems =3D num_elems->data.integer; + ret->data.array.elem_size =3D primitive_byte_width(type->type); + ret->data.array.null_terminated =3D false; + *node_ret =3D ret; + return 0; +} + +static int parse_str(struct parser *p, struct ast_node **node_ret) +{ + struct ast_node *ret; + struct token *len; + + if (!consume(p, TOKEN_KEYWORD_STR, "expected 'str'") || !consume(p, TOKEN= _LBRACKET, "expected '['")) + return -EINVAL; + + len =3D consume(p, TOKEN_INTEGER, "expected integer"); + if (!len) + return -EINVAL; + + if (!consume(p, TOKEN_RBRACKET, "expected ']'")) + return -EINVAL; + + ret =3D malloc(sizeof(*ret)); + if (!ret) + return -ENOMEM; + + /* A string is the susbet of byte arrays that are null-terminated. */ + ret->type =3D NODE_ARRAY; + ret->data.array.num_elems =3D len->data.integer; + ret->data.array.elem_size =3D sizeof(char); + ret->data.array.null_terminated =3D true; + *node_ret =3D ret; + return 0; +} + +static int parse_len(struct parser *p, struct ast_node **node_ret) +{ + struct token *type, *len; + const char *length_of; + struct ast_node *ret; + + if (!consume(p, TOKEN_KEYWORD_LEN, "expected 'len'") || !consume(p, TOKEN= _LBRACKET, "expected '['")) + return -EINVAL; + + len =3D advance(p); + if (len->type !=3D TOKEN_IDENTIFIER) + return -EINVAL; + + if (!consume(p, TOKEN_COMMA, "expected ','")) + return -EINVAL; + + type =3D advance(p); + if (!is_primitive(type)) + return -EINVAL; + + if (!consume(p, TOKEN_RBRACKET, "expected ']'")) + return -EINVAL; + + ret =3D malloc(sizeof(*ret)); + if (!ret) + return -ENOMEM; + + length_of =3D strndup(len->data.identifier.start, len->data.identifier.le= ngth); + if (!length_of) { + free(ret); + return -ENOMEM; + } + + ret->type =3D NODE_LENGTH; + ret->data.length.length_of =3D length_of; + ret->data.length.byte_width =3D primitive_byte_width(type->type); + + *node_ret =3D ret; + return 0; +} + +static int parse_type(struct parser *p, struct ast_node **node_ret) +{ + if (is_primitive(peek(p))) + return parse_primitive(p, node_ret); + + if (peek(p)->type =3D=3D TOKEN_KEYWORD_PTR) + return parse_ptr(p, node_ret); + + if (peek(p)->type =3D=3D TOKEN_KEYWORD_ARR) + return parse_arr(p, node_ret); + + if (peek(p)->type =3D=3D TOKEN_KEYWORD_STR) + return parse_str(p, node_ret); + + if (peek(p)->type =3D=3D TOKEN_KEYWORD_LEN) + return parse_len(p, node_ret); + + return -EINVAL; +} + +static int parse_region(struct parser *p, struct ast_node **node_ret) +{ + struct token *tok, *identifier; + struct ast_region *region; + struct ast_node *node; + struct ast_node *ret; + void *new_ptr; + int err; + + identifier =3D consume(p, TOKEN_IDENTIFIER, "expected identifier"); + if (!identifier) + return -EINVAL; + + ret =3D malloc(sizeof(*ret)); + if (!ret) + return -ENOMEM; + + tok =3D consume(p, TOKEN_LBRACE, "expected '{'"); + if (!tok) { + err =3D -EINVAL; + goto fail_early; + } + + region =3D &ret->data.region; + region->name =3D strndup(identifier->data.identifier.start, identifier->d= ata.identifier.length); + if (!region->name) { + err =3D -ENOMEM; + goto fail_early; + } + + region->num_members =3D 0; + while (!match(p, TOKEN_RBRACE)) { + err =3D parse_type(p, &node); + if (err) + goto fail; + new_ptr =3D realloc(region->members, (region->num_members + 1) * sizeof(= struct ast_node *)); + if (!new_ptr) { + err =3D -ENOMEM; + goto fail; + } + region->num_members++; + region->members =3D new_ptr; + region->members[region->num_members - 1] =3D node; + } + + if (!consume(p, TOKEN_RBRACE, "expected '}'") || !consume(p, TOKEN_SEMICO= LON, "expected ';'")) { + err =3D -EINVAL; + goto fail; + } + + ret->type =3D NODE_REGION; + *node_ret =3D ret; + return 0; + +fail: + destroy_ast_node(ret); + return err; + +fail_early: + free(ret); + return err; +} + +static int parse_program(struct parser *p, struct ast_node **node_ret) +{ + struct ast_program *prog; + struct ast_node *reg; + struct ast_node *ret; + void *new_ptr; + int err; + + ret =3D malloc(sizeof(*ret)); + if (!ret) + return -ENOMEM; + ret->type =3D NODE_PROGRAM; + + prog =3D &ret->data.program; + prog->num_members =3D 0; + prog->members =3D NULL; + while (!match(p, TOKEN_EOF)) { + err =3D parse_region(p, ®); + if (err) + goto fail; + + new_ptr =3D realloc(prog->members, ++prog->num_members * sizeof(struct a= st_node *)); + if (!new_ptr) { + err =3D -ENOMEM; + goto fail; + } + prog->members =3D new_ptr; + prog->members[prog->num_members - 1] =3D reg; + } + + *node_ret =3D ret; + return 0; + +fail: + destroy_ast_node(ret); + return err; +} + +size_t node_alignment(struct ast_node *node) +{ + size_t max_alignment =3D 1; + size_t i; + + switch (node->type) { + case NODE_PROGRAM: + for (i =3D 0; i < node->data.program.num_members; i++) + max_alignment =3D MAX(max_alignment, node_alignment(node->data.program.= members[i])); + return max_alignment; + case NODE_REGION: + for (i =3D 0; i < node->data.region.num_members; i++) + max_alignment =3D MAX(max_alignment, node_alignment(node->data.region.m= embers[i])); + return max_alignment; + case NODE_ARRAY: + return node->data.array.elem_size; + case NODE_LENGTH: + return node->data.length.byte_width; + case NODE_PRIMITIVE: + /* Primitives are aligned to their size. */ + return node->data.primitive.byte_width; + case NODE_POINTER: + return sizeof(uintptr_t); + } + + /* Anything should be at least 1-byte-aligned. */ + return 1; +} + +size_t node_size(struct ast_node *node) +{ + size_t total =3D 0; + size_t i; + + switch (node->type) { + case NODE_PROGRAM: + for (i =3D 0; i < node->data.program.num_members; i++) + total +=3D node_size(node->data.program.members[i]); + return total; + case NODE_REGION: + for (i =3D 0; i < node->data.region.num_members; i++) { + /* Account for padding within region. */ + total =3D ROUND_UP_TO_MULTIPLE(total, node_alignment(node->data.region.= members[i])); + total +=3D node_size(node->data.region.members[i]); + } + return total; + case NODE_ARRAY: + return node->data.array.elem_size * node->data.array.num_elems + + (node->data.array.null_terminated ? 1 : 0); + case NODE_LENGTH: + return node->data.length.byte_width; + case NODE_PRIMITIVE: + return node->data.primitive.byte_width; + case NODE_POINTER: + return sizeof(uintptr_t); + } + return 0; +} + +int parse(struct token **tokens, size_t token_count, struct ast_node **nod= e_ret) +{ + struct parser p =3D { .tokens =3D tokens, .token_count =3D token_count, .= curr_token =3D 0 }; + return parse_program(&p, node_ret); +} + +void destroy_ast_node(struct ast_node *node) +{ + size_t i; + + switch (node->type) { + case NODE_PROGRAM: + for (i =3D 0; i < node->data.program.num_members; i++) + destroy_ast_node(node->data.program.members[i]); + break; + case NODE_REGION: + for (i =3D 0; i < node->data.region.num_members; i++) + destroy_ast_node(node->data.region.members[i]); + free((void *)node->data.region.name); + break; + case NODE_LENGTH: + free((void *)node->data.length.length_of); + break; + case NODE_POINTER: + free((void *)node->data.pointer.points_to); + break; + default: + break; + } + free(node); +} diff --git a/tools/testing/kfuzztest-bridge/input_parser.h b/tools/testing/= kfuzztest-bridge/input_parser.h new file mode 100644 index 000000000000..5f444b40f672 --- /dev/null +++ b/tools/testing/kfuzztest-bridge/input_parser.h @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Parser for KFuzzTest textual input format + * + * Copyright 2025 Google LLC + */ +#ifndef KFUZZTEST_BRIDGE_INPUT_PARSER_H +#define KFUZZTEST_BRIDGE_INPUT_PARSER_H + +#include + +/* Rounds x up to the nearest multiple of n. */ +#define ROUND_UP_TO_MULTIPLE(x, n) (((n) =3D=3D 0) ? (0) : (((x) + (n) - 1= ) / (n)) * (n)) + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +enum ast_node_type { + NODE_PROGRAM, + NODE_REGION, + NODE_ARRAY, + NODE_LENGTH, + NODE_PRIMITIVE, + NODE_POINTER, +}; + +struct ast_node; /* Forward declaration. */ + +struct ast_program { + struct ast_node **members; + size_t num_members; +}; + +struct ast_region { + const char *name; + struct ast_node **members; + size_t num_members; +}; + +struct ast_array { + int elem_size; + int null_terminated; /* True iff the array should always end with 0. */ + size_t num_elems; +}; + +struct ast_length { + size_t byte_width; + const char *length_of; +}; + +struct ast_primitive { + size_t byte_width; +}; + +struct ast_pointer { + const char *points_to; +}; + +struct ast_node { + enum ast_node_type type; + union { + struct ast_program program; + struct ast_region region; + struct ast_array array; + struct ast_length length; + struct ast_primitive primitive; + struct ast_pointer pointer; + } data; +}; + +struct parser { + struct token **tokens; + size_t token_count; + size_t curr_token; +}; + +int parse(struct token **tokens, size_t token_count, struct ast_node **nod= e_ret); +void destroy_ast_node(struct ast_node *node); + +size_t node_size(struct ast_node *node); +size_t node_alignment(struct ast_node *node); + +#endif /* KFUZZTEST_BRIDGE_INPUT_PARSER_H */ diff --git a/tools/testing/kfuzztest-bridge/rand_stream.c b/tools/testing/k= fuzztest-bridge/rand_stream.c new file mode 100644 index 000000000000..bca6b3de5aad --- /dev/null +++ b/tools/testing/kfuzztest-bridge/rand_stream.c @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Implements a cached file-reader for iterating over a byte stream of + * pseudo-random data + * + * Copyright 2025 Google LLC + */ +#include "rand_stream.h" + +static int refill(struct rand_stream *rs) +{ + rs->valid_bytes =3D fread(rs->buffer, sizeof(char), rs->buffer_size, rs->= source); + rs->buffer_pos =3D 0; + if (rs->valid_bytes !=3D rs->buffer_size && ferror(rs->source)) + return ferror(rs->source); + return 0; +} + +struct rand_stream *new_rand_stream(const char *path_to_file, size_t cache= _size) +{ + struct rand_stream *rs; + + rs =3D malloc(sizeof(*rs)); + if (!rs) + return NULL; + + rs->valid_bytes =3D 0; + rs->source =3D fopen(path_to_file, "rb"); + if (!rs->source) { + free(rs); + return NULL; + } + + if (fseek(rs->source, 0, SEEK_END)) { + fclose(rs->source); + free(rs); + return NULL; + } + rs->source_size =3D ftell(rs->source); + + if (fseek(rs->source, 0, SEEK_SET)) { + fclose(rs->source); + free(rs); + return NULL; + } + + rs->buffer =3D malloc(cache_size); + if (!rs->buffer) { + fclose(rs->source); + free(rs); + return NULL; + } + rs->buffer_size =3D cache_size; + return rs; +} + +void destroy_rand_stream(struct rand_stream *rs) +{ + fclose(rs->source); + free(rs->buffer); + free(rs); +} + +int next_byte(struct rand_stream *rs, char *ret) +{ + int res; + + if (rs->buffer_pos >=3D rs->valid_bytes) { + res =3D refill(rs); + if (res) + return res; + if (rs->valid_bytes =3D=3D 0) + return STREAM_EOF; + } + *ret =3D rs->buffer[rs->buffer_pos++]; + return 0; +} diff --git a/tools/testing/kfuzztest-bridge/rand_stream.h b/tools/testing/k= fuzztest-bridge/rand_stream.h new file mode 100644 index 000000000000..acb3271d30ca --- /dev/null +++ b/tools/testing/kfuzztest-bridge/rand_stream.h @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Implements a cached file-reader for iterating over a byte stream of + * pseudo-random data + * + * Copyright 2025 Google LLC + */ +#ifndef KFUZZTEST_BRIDGE_RAND_STREAM_H +#define KFUZZTEST_BRIDGE_RAND_STREAM_H + +#include +#include + +#define STREAM_EOF 1 + +/** + * struct rand_stream - a buffered bytestream reader + * + * Reads and returns bytes from a file, using buffered pre-fetching to amo= rtize + * the cost of reads. + */ +struct rand_stream { + FILE *source; + size_t source_size; + char *buffer; + size_t buffer_size; + size_t buffer_pos; + size_t valid_bytes; +}; + +/** + * new_rand_stream - return a new struct rand_stream + * + * @path_to_file: source of the output byte stream. + * @cache_size: size of the read-ahead cache in bytes. + */ +struct rand_stream *new_rand_stream(const char *path_to_file, size_t cache= _size); + +/** + * destroy_rand_stream - clean up a rand stream's resources + * + * @rs: a struct rand_stream + */ +void destroy_rand_stream(struct rand_stream *rs); + +/** + * next_byte - return the next byte from a struct rand_stream + * + * @rs: an initialized struct rand_stream. + * @ret: return pointer. + * + * @return 0 on success or a negative value on failure. + * + */ +int next_byte(struct rand_stream *rs, char *ret); + +#endif /* KFUZZTEST_BRIDGE_RAND_STREAM_H */ --=20 2.51.0.470.ga7dc726c21-goog From nobody Thu Oct 2 06:17:57 2025 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 1C001314B89 for ; Fri, 19 Sep 2025 14:58:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293887; cv=none; b=HTvRlfn37HECVUnh/nXmIGKs41Ck4+C/3cyapnUWp78CbyIqCIS/vLYGBNsUkk+v8ooSfF44xwRhdoCJLLTk9HX0agfATHj1b+AzzVZ1oLPRDMwTZVy2kR5zZ+FmfolpEBQyL5ipEzkRw467LnPl5qyGQXpYslrucJAFaRF5rSQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293887; c=relaxed/simple; bh=ZOuSxhMxiHfYY+cea7oBYHCr7psQOpltTrm/vr4oNVQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BEGkMEcu++myOnEVBUw2ZAQoWsYG5aiAkunVtFi5TQpjC7HfMXzc7ndXc3yUzDBMfZxwzI8Ef26m2KyUZv66oKIrySRlHktutAzwwGnyatqTkL5PYtYcp9UsT76NPZ6aTDgjP0Tg/q2FggKDjDhEDIHXBpIyNx5uCvoSXoE3N9s= 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=Adys5oqQ; arc=none smtp.client-ip=209.85.221.48 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="Adys5oqQ" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-3ee15b5435bso915079f8f.0 for ; Fri, 19 Sep 2025 07:58:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758293883; x=1758898683; 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; bh=Zunbl6fvIHEqkfAd29udw7Y98ixve3unSQwmEkkt7GA=; b=Adys5oqQVB2eog04UUBB6k28n5Kd5Gk8riCfaHFkvullg8GGTMcd6BiI0J6kpqSCMl 8swcBi7QdjUFkrwEhjiuG7cqYpC1TgYB6ggaEcYx8WfhqZcdzZb0gMWv4bwQcDhigk2O nKG0E7Q1F7sUJvUXy/CuPKuEyR/HxZ7Z3TM3cIodbn//P5Rcd6kQy0h0FF01EDUCG+IE Br621H2gGHRbZzPM02FhXmQGvVtIazJYQcTpqZkiiuBFEsJYRYGSzo9IFOfNPPWIEjcQ c5IqdKAWR8nVsNDKzmSGc2sIQwWlAXRFaTrY9UrdBD+Wsr3+ciroyIAu22GTjq+1lGCb 5l7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758293883; x=1758898683; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Zunbl6fvIHEqkfAd29udw7Y98ixve3unSQwmEkkt7GA=; b=LYwONCcZpYvWqXqMDxChQkwQoc8xE1Qi0ZwBjwdks64+fE/NvleKNG+KQnUNbzxyl0 ctsPbIxVxeGJWfXCaZ5VfkPWTXzkj1ZmvCHt84GReWuNskViG7spFcsft7pSnjqncT9f kgqmut5aYuL8PGmt0Ve4vHIiUUNa9zQHIEVb31kdYab71mgN4g/ziej3dy8bT5M8ZloI rzGsroAv7XJ7TbSIFRyCi/TCgcqwz1Xb9HN69Zs4nWqwGwCy+zvZy4hncs6liMaz4spN J9R8A/TaVZmDcCHA+NsZfSqYBoZijSW0eET9yBtPxmiWn5jXPsh6DyKS2AJVHjteogad N2kw== X-Forwarded-Encrypted: i=1; AJvYcCVL8HWfFRU+0bbod26w144uarfxKIe8AmGlCA6mFgPz0SXm3IUXiPh4s+qgh9Bx1YySktNux59uqZaou9k=@vger.kernel.org X-Gm-Message-State: AOJu0YygluQV73v/Y/B5g+5ZLz08MIo2aTwvFg1z8Q+K1bDErnDeSLpb VI8UgojFvuOUxqGAfyZT7i2cu/AOil37a8pmK31sV6d0ywqhfVGBPVff X-Gm-Gg: ASbGncu92qIS3UdunIX+gozKchzoEqa7PcP118jqA7rZM0D+yRRCqceRAh+r5FcmS5Q G1S1aJb1DkItJ/aW5/nKz/2NRSt1n4kAhd9S2NTJkmhmaytYzai/DenwkREnZ8x0jrcWpOc4aHm zBC9xIvrGXYOmIfnB96Vk7G0RiNgKU7nIbCfiG8bj8N+q9Y6Eo1OW6FEvA/2xkuVSMAZK71LbH/ poMfogUxWb3yqraD6g6chsja16XhafVUNMRb3hbWpJHVqM98qS3upOSmft8jVPyN3gRa0chjaKL bVbWume6mYZiqY4p+yYX54Efde4Sal6EW1hXgezEE46Aj6F5g6SlVQ+aAr5qejw5YFW/iyUpTE5 VoPLjtJFc4vM2GqmQA2YYCCtoaiRrQxa+TFGx1g1LUC2w6JTBLF4xb+Mrdi1PmsORIWQNf1M8ay vs9DUTRhdaR1wL+8A= X-Google-Smtp-Source: AGHT+IHDjt8O9vYKO4IAKnSZVCkSqLzYQnMnHfcy6YqHFSDHgfMy0UALYGdy8mo3Zt+AM86r0DMwnw== X-Received: by 2002:a05:6000:2408:b0:3ee:15b4:846c with SMTP id ffacd0b85a97d-3ee7f606f17mr3174553f8f.28.1758293882976; Fri, 19 Sep 2025 07:58:02 -0700 (PDT) Received: from xl-nested.c.googlers.com.com (124.62.78.34.bc.googleusercontent.com. [34.78.62.124]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3ee0fbc7188sm8551386f8f.37.2025.09.19.07.58.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 07:58:02 -0700 (PDT) From: Ethan Graham To: ethangraham@google.com, glider@google.com Cc: andreyknvl@gmail.com, andy@kernel.org, brauner@kernel.org, brendan.higgins@linux.dev, davem@davemloft.net, davidgow@google.com, dhowells@redhat.com, dvyukov@google.com, elver@google.com, herbert@gondor.apana.org.au, ignat@cloudflare.com, jack@suse.cz, jannh@google.com, johannes@sipsolutions.net, kasan-dev@googlegroups.com, kees@kernel.org, kunit-dev@googlegroups.com, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, lukas@wunner.de, rmoar@google.com, shuah@kernel.org, sj@kernel.org, tarasmadan@google.com Subject: [PATCH v2 05/10] kfuzztest: add ReST documentation Date: Fri, 19 Sep 2025 14:57:45 +0000 Message-ID: <20250919145750.3448393-6-ethan.w.s.graham@gmail.com> X-Mailer: git-send-email 2.51.0.470.ga7dc726c21-goog In-Reply-To: <20250919145750.3448393-1-ethan.w.s.graham@gmail.com> References: <20250919145750.3448393-1-ethan.w.s.graham@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" From: Ethan Graham Add Documentation/dev-tools/kfuzztest.rst and reference it in the dev-tools index. Signed-off-by: Ethan Graham Acked-by: Alexander Potapenko --- PR v2: - Update documentation to reflect new location of kfuzztest-bridge, under tools/testing. PR v1: - Fix some typos and reword some sections. - Correct kfuzztest-bridge grammar description. - Reference documentation in kfuzztest-bridge/input_parser.c header comment. RFC v2: - Add documentation for kfuzztest-bridge tool introduced in patch 4. --- --- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kfuzztest.rst | 385 ++++++++++++++++++ tools/testing/kfuzztest-bridge/input_parser.c | 2 + 3 files changed, 388 insertions(+) create mode 100644 Documentation/dev-tools/kfuzztest.rst diff --git a/Documentation/dev-tools/index.rst b/Documentation/dev-tools/in= dex.rst index 65c54b27a60b..00ccc4da003b 100644 --- a/Documentation/dev-tools/index.rst +++ b/Documentation/dev-tools/index.rst @@ -32,6 +32,7 @@ Documentation/process/debugging/index.rst kfence kselftest kunit/index + kfuzztest ktap checkuapi gpio-sloppy-logic-analyzer diff --git a/Documentation/dev-tools/kfuzztest.rst b/Documentation/dev-tool= s/kfuzztest.rst new file mode 100644 index 000000000000..0c74732ecf21 --- /dev/null +++ b/Documentation/dev-tools/kfuzztest.rst @@ -0,0 +1,385 @@ +.. SPDX-License-Identifier: GPL-2.0 +.. Copyright 2025 Google LLC + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Kernel Fuzz Testing Framework (KFuzzTest) +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Overview +=3D=3D=3D=3D=3D=3D=3D=3D + +The Kernel Fuzz Testing Framework (KFuzzTest) is a framework designed to e= xpose +internal kernel functions to a userspace fuzzing engine. + +It is intended for testing stateless or low-state functions that are diffi= cult +to reach from the system call interface, such as routines involved in file +format parsing or complex data transformations. This provides a method for +in-situ fuzzing of kernel code without requiring that it be built as a sep= arate +userspace library or that its dependencies be stubbed out. + +The framework consists of four main components: + +1. An API, based on the ``FUZZ_TEST`` macro, for defining test targets + directly in the kernel tree. +2. A binary serialization format for passing complex, pointer-rich data + structures from userspace to the kernel. +3. A ``debugfs`` interface through which a userspace fuzzer submits + serialized test inputs. +4. Metadata embedded in dedicated ELF sections of the ``vmlinux`` binary = to + allow for the discovery of available fuzz targets by external tooling. + +.. warning:: + KFuzzTest is a debugging and testing tool. It exposes internal kernel + functions to userspace with minimal sanitization and is designed for + use in controlled test environments only. It must **NEVER** be enabled + in production kernels. + +Supported Architectures +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +KFuzzTest is designed for generic architecture support. It has only been +explicitly tested on x86_64. + +Usage +=3D=3D=3D=3D=3D + +To enable KFuzzTest, configure the kernel with:: + + CONFIG_KFUZZTEST=3Dy + +which depends on ``CONFIG_DEBUGFS`` for receiving userspace inputs, and +``CONFIG_DEBUG_KERNEL`` as an additional guardrail for preventing KFuzzTest +from finding its way into a production build accidentally. + +The KFuzzTest sample fuzz targets can be built in with +``CONFIG_SAMPLE_KFUZZTEST``. + +KFuzzTest currently only supports targets that are built into the kernel, = as the +core module's startup process discovers fuzz targets from a dedicated ELF +section during startup. Furthermore, constraints and annotations emit meta= data +that can be scanned from a ``vmlinux`` binary by a userspace fuzzing engin= e. + +Declaring a KFuzzTest target +---------------------------- + +A fuzz target should be defined in a .c file. The recommended place to def= ine +this is under the subsystem's ``/tests`` directory in a ``_kfuz= z.c`` +file, following the convention used by KUnit. The only strict requirement = is +that the function being fuzzed is visible to the fuzz target. + +Defining a fuzz target involves three main parts: defining an input struct= ure, +writing the test body using the ``FUZZ_TEST`` macro, and optionally adding +metadata for the fuzzer. + +The following example illustrates how to create a fuzz target for a functi= on +``int process_data(const char *data, size_t len)``. + +.. code-block:: c + + /* + * 1. Define a struct to model the inputs for the function under test. + * Each field corresponds to an argument needed by the function. + */ + struct process_data_inputs { + const char *data; + size_t len; + }; + + /* + * 2. Define the fuzz target using the FUZZ_TEST macro. + * The first parameter is a unique name for the target. + * The second parameter is the input struct defined above. + */ + FUZZ_TEST(test_process_data, struct process_data_inputs) + { + /* + * Within this body, the 'arg' variable is a pointer to a + * fully initialized 'struct process_data_inputs'. + */ + + /* + * 3. (Optional) Add constraints to define preconditions. + * This check ensures 'arg->data' is not NULL. If the condition + * is not met, the test exits early. This also creates metadata + * to inform the fuzzer. + */ + KFUZZTEST_EXPECT_NOT_NULL(process_data_inputs, data); + + /* + * 4. (Optional) Add annotations to provide semantic hints to the + * fuzzer. This annotation informs the fuzzer that the 'len' field is + * the length of the buffer pointed to by 'data'. Annotations do not + * add any runtime checks. + */ + KFUZZTEST_ANNOTATE_LEN(process_data_inputs, len, data); + + /* + * 5. Call the kernel function with the provided inputs. + * Memory errors like out-of-bounds accesses on 'arg->data' will + * be detected by KASAN or other memory error detection tools. + */ + process_data(arg->data, arg->len); + } + +KFuzzTest provides two families of macros to improve the quality of fuzzin= g: + +- ``KFUZZTEST_EXPECT_*``: These macros define constraints, which are + preconditions that must be true for the test to proceed. They are enforc= ed + with a runtime check in the kernel. If a check fails, the current test r= un is + aborted. This metadata helps the userspace fuzzer avoid generating inval= id + inputs. + +- ``KFUZZTEST_ANNOTATE_*``: These macros define annotations, which are pur= ely + semantic hints for the fuzzer. They do not add any runtime checks and ex= ist + only to help the fuzzer generate more intelligent and structurally corre= ct + inputs. For example, KFUZZTEST_ANNOTATE_LEN links a size field to a poin= ter + field, which is a common pattern in C APIs. + +Metadata +-------- + +Macros ``FUZZ_TEST``, ``KFUZZTEST_EXPECT_*`` and ``KFUZZTEST_ANNOTATE_*`` = embed +metadata into several sections within the main ``.data`` section of the fi= nal +``vmlinux`` binary; ``.kfuzztest_target``, ``.kfuzztest_constraint`` and +``.kfuzztest_annotation`` respectively. + +This serves two purposes: + +1. The core module uses the ``.kfuzztest_target`` section at boot to disco= ver + every ``FUZZ_TEST`` instance and create its ``debugfs`` directory and + ``input`` file. +2. Userspace fuzzers can read this metadata from the ``vmlinux`` binary to + discover targets and learn about their rules and structure in order to + generate correct and effective inputs. + +The metadata in the ``.kfuzztest_*`` sections consists of arrays of fixed-= size C +structs (e.g., ``struct kfuzztest_target``). Fields within these structs t= hat +are pointers, such as ``name`` or ``arg_type_name``, contain addresses that +point to other locations in the ``vmlinux`` binary. A userspace tool that +parsing the ELF file must resolve these pointers to read the data that they +reference. For example, to get a target's name, a tool must: + +1. Read the ``struct kfuzztest_target`` from the ``.kfuzztest_target`` sec= tion. +2. Read the address in the ``.name`` field. +3. Use that address to locate and read null-terminated string from its pos= ition + elsewhere in the binary (e.g., ``.rodata``). + +Tooling Dependencies +-------------------- + +For userspace tools to parse the ``vmlinux`` binary and make use of emitted +KFuzzTest metadata, the kernel must be compiled with DWARF debug informati= on. +This is required for tools to understand the layout of C structs, resolve = type +information, and correctly interpret constraints and annotations. + +When using KFuzzTest with automated fuzzing tools, either +``CONFIG_DEBUG_INFO_DWARF4`` or ``CONFIG_DEBUG_INFO_DWARF5`` should be ena= bled. + +Input Format +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +KFuzzTest targets receive their inputs from userspace via a write to a ded= icated +debugfs file ``/sys/kernel/debug/kfuzztest//input``. + +The data written to this file must be a single binary blob that follows a +specific serialization format. This format is designed to allow complex, +pointer-rich C structures to be represented in a flat buffer, requiring on= ly a +single kernel allocation and copy from userspace. + +An input is first prefixed by an 8-byte header containing a magic value in= the +first four bytes, defined as ``KFUZZTEST_HEADER_MAGIC`` in +```, and a version number in the subsequent four +bytes. + +Version 0 +--------- + +In version 0 (i.e., when the version number in the 8-byte header is equal = to 0), +the input format consists of three main parts laid out sequentially: a reg= ion +array, a relocation table, and the payload.:: + + +----------------+---------------------+-----------+----------------+ + | region array | relocation table | padding | payload | + +----------------+---------------------+-----------+----------------+ + +Region Array +^^^^^^^^^^^^ + +This component is a header that describes how the raw data in the Payload = is +partitioned into logical memory regions. It consists of a count of regions +followed by an array of ``struct reloc_region``, where each entry defines a +single region with its size and offset from the start of the payload. + +.. code-block:: c + + struct reloc_region { + uint32_t offset; + uint32_t size; + }; + + struct reloc_region_array { + uint32_t num_regions; + struct reloc_region regions[]; + }; + +By convention, region 0 represents the top-level input struct that is pass= ed +as the arg variable to the ``FUZZ_TEST`` body. Subsequent regions typically +represent data buffers or structs pointed to by fields within that struct. +Region array entries must be ordered by ascending offset, and must not ove= rlap +with one another. + +Relocation Table +^^^^^^^^^^^^^^^^ + +The relocation table contains the instructions for the kernel to "hydrate"= the +payload by patching pointer fields. It contains an array of +``struct reloc_entry`` items. Each entry acts as a linking instruction, +specifying: + +- The location of a pointer that needs to be patched (identified by a regi= on + ID and an offset within that region). + +- The target region that the pointer should point to (identified by the + target's region ID) or ``KFUZZTEST_REGIONID_NULL`` if the pointer is ``N= ULL``. + +This table also specifies the amount of padding between its end and the st= art +of the payload, which should be at least 8 bytes. + +.. code-block:: c + + struct reloc_entry { + uint32_t region_id; + uint32_t region_offset; + uint32_t value; + }; + + struct reloc_table { + uint32_t num_entries; + uint32_t padding_size; + struct reloc_entry entries[]; + }; + +Payload +^^^^^^^ + +The payload contains the raw binary data for all regions, concatenated tog= ether +according to their specified offsets. + +- Region specific alignment: The data for each individual region must star= t at + an offset that is aligned to its own C type's requirements. For example,= a + ``uint64_t`` must begin on an 8-byte boundary. + +- Minimum alignment: The offset of each region, as well as the beginning o= f the + payload, must also be a multiple of the overall minimum alignment value.= This + value is determined by the greater of ``ARCH_KMALLOC_MINALIGN`` and + ``KASAN_GRANULE_SIZE`` (which is represented by ``KFUZZTEST_POISON_SIZE`= ` in + ``/include/linux/kfuzztest.h``). This minimum alignment ensures that all + function inputs respect C calling conventions. + +- Padding: The space between the end of one region's data and the beginnin= g of + the next must be sufficient for padding. The padding must also be at lea= st + the same minimum alignment value mentioned above. This is crucial for KA= SAN + builds, as it allows KFuzzTest to poison this unused space enabling prec= ise + detection of out-of-bounds memory accesses between adjacent buffers. + +The minimum alignment value is architecture-dependent and is exposed to +userspace via the read-only file +``/sys/kernel/debug/kfuzztest/_config/minalign``. The framework relies on +userspace tooling to construct the payload correctly, adhering to all thre= e of +these rules for every region. + +KFuzzTest Bridge Tool +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The ``kfuzztest-bridge`` program is a userspace utility that encodes a ran= dom +byte stream into the structured binary format expected by a KFuzzTest harn= ess. +It allows users to describe the target's input structure textually, making= it +easy to perform smoke tests or connect harnesses to blob-based fuzzing eng= ines. + +This tool is intended to be simple, both in usage and implementation. Its +structure and DSL are sufficient for simpler use-cases. For more advanced +coverage-guided fuzzing it is recommended to use +`syzkaller ` which implements deeper +support for KFuzzTest targets. + +Usage +----- + +The tool can be built with ``make tools/testing/kfuzztest-bridge``. In the= case +of libc incompatibilities, the tool will have to be linked statically or b= uilt +on the target system. + +Example: + +.. code-block:: sh + + ./tools/testing/kfuzztest-bridge \ + "foo { u32 ptr[bar] }; bar { ptr[data] len[data, u64]}; data { arr= [u8, 42] };" \ + "my-fuzz-target" /dev/urandom + +The command takes three arguments + +1. A string describing the input structure (see `Textual Format`_ sub-sec= tion). +2. The name of the target test, which corresponds to its directory in + ``/sys/kernel/debug/kfuzztest/``. +3. A path to a file providing a stream of random data, such as + ``/dev/urandom``. + +The structure string in the example corresponds to the following C data +structures: + +.. code-block:: c + + struct foo { + u32 a; + struct bar *b; + }; + + struct bar { + struct data *d; + u64 data_len; /* Equals 42. */ + }; + + struct data { + char arr[42]; + }; + +Textual Format +-------------- + +The textual format is a human-readable representation of the region-based = binary +format used by KFuzzTest. It is described by the following grammar: + +.. code-block:: text + + schema ::=3D region ( ";" region )* [";"] + region ::=3D identifier "{" type ( " " type )* "}" + type ::=3D primitive | pointer | array | length | string + primitive ::=3D "u8" | "u16" | "u32" | "u64" + pointer ::=3D "ptr" "[" identifier "]" + array ::=3D "arr" "[" primitive "," integer "]" + length ::=3D "len" "[" identifier "," primitive "]" + string ::=3D "str" "[" integer "]" + identifier ::=3D [a-zA-Z_][a-zA-Z1-9_]* + integer ::=3D [0-9]+ + +Pointers must reference a named region. + +To fuzz a raw buffer, the buffer must be defined in its own region, as sho= wn +below: + +.. code-block:: c + + struct my_struct { + char *buf; + size_t buflen; + }; + +This would correspond to the following textual description: + +.. code-block:: text + + my_struct { ptr[buf] len[buf, u64] }; buf { arr[u8, n] }; + +Here, ``n`` is some integer value defining the size of the byte array insi= de of +the ``buf`` region. diff --git a/tools/testing/kfuzztest-bridge/input_parser.c b/tools/testing/= kfuzztest-bridge/input_parser.c index b1fd8ba5217e..feaa59de49d7 100644 --- a/tools/testing/kfuzztest-bridge/input_parser.c +++ b/tools/testing/kfuzztest-bridge/input_parser.c @@ -16,6 +16,8 @@ * and its corresponding length encoded over 8 bytes, where `buf` itself * contains a 42-byte array. * + * The full grammar is documented in Documentation/dev-tools/kfuzztest.rst. + * * Copyright 2025 Google LLC */ #include --=20 2.51.0.470.ga7dc726c21-goog From nobody Thu Oct 2 06:17:57 2025 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.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 92433314D0F for ; Fri, 19 Sep 2025 14:58:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293888; cv=none; b=ozvven+nuhkuoadVsHkhL5HZV7+lDNz0KtuP4yiuA7GE2Mh53KpL8p26+osVWt85KFt4TrZZZGfuLhhcaxDuQANN3oh2Zvz1AaSseB9wWx2R/HgzW2GlQazoI9zDNEAIUlCI1gwtpEKlHqVbpzkaxNFuLpmJ8RTNrwZRBBx784g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293888; c=relaxed/simple; bh=mIlo3nceWEuwDvP6l8Crl7X/GDGSd6mMK6Fbkmnwpd8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hfQMadDDPleraFQCU/fl3Na6tP/qScS04kNKIZSlil5e14svcgsQSFGFNpbL1og2dT8NS3PvhRk4J2r8OLrvSZh4HbSMOqbzzV3zlz89APtgMQcpjOOXII66vlKyfBiV/kJxlGG/0K3UITud6Mde8fhPFW8KWlz3IzHDwothHwY= 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=UE4DWBJj; arc=none smtp.client-ip=209.85.221.43 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="UE4DWBJj" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-3ee64bc6b85so946468f8f.3 for ; Fri, 19 Sep 2025 07:58:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758293884; x=1758898684; 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; bh=eBXsJ3eFBRBXMY0OnOP/8+Wo598xHcjcSo7yO9rMMTI=; b=UE4DWBJjwYTUE05avpdV29htcXeZSuUcwbdcFjoCMaXtHQTSITOo8g7ycSfH0h28uW CKPLXVmHFxr87cw09YBPQUJZBjBy7gKzymz6xsMrR7LNYosxTvIvD+Xt53KX8YtIkY+6 I/gDubCYTcPeFjZQdJzUhHe1ha2IKhy5BzCyziwTZ1U1iQiNBB/dTZCdRJZBru5TrBb5 ybmI9VCjz5A49T21Ar8o6MAbJz6vaExa4gVWTEUdI6IjJP6dO0T6AebVmTN2cJHiiGyt CerAEgrGFcyoMOFp3VUrSoCKg0/1YnYZv3te9TVsnH2jpYH5f6ZXR0qbKo5aQK7bDnqz JH1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758293884; x=1758898684; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eBXsJ3eFBRBXMY0OnOP/8+Wo598xHcjcSo7yO9rMMTI=; b=NJzPH5ky+NBpfHYLONpD7Xik1rvX7oAmzBiL2KhUzGnKvc7m/dIui2/qqx6FCUoGqF rZwtJCT0PbePRaq9XIlbwKi+dZU5u6gcirXhwc1mcntKHHlaY/RhXz1yPaL0ZXrQSTdo GyK2ignYc4JIL4vifrNwdpDbWt3Efq0ZoGNB8u0lK2+uxxvJueFAHO1ig7NSbxr5l1QI f2hDcHyNB93XGlliRFkeNw+GNmQC6kZhXOBfzfCGHorVeyuK8dg6n4u6nNngEyzUluf+ 0K0UdIIjeu0lhlb68dHk31uyT+q/1tgQdAW66z6txR3cd30+0ILBKMFv+9jkpvqwUtIt kZYA== X-Forwarded-Encrypted: i=1; AJvYcCUCLVHwTlnPT9mNN6oITqIp3XhJzt/5G/UTV/K7WqcZW5PIMICjxnc5DhIJUJ8oilqxBdZeoaGC53jtSQk=@vger.kernel.org X-Gm-Message-State: AOJu0YyuAQfIgydPKVVWEUrbuToFBS06fwRqsZDVAbrZY4D6/+c3aqty tJDR59yf6H7OsmfZNcCXmiI3L45fBRHS39i9hwtHugQcK9liRZFpCCej X-Gm-Gg: ASbGncvUlIshKmk+u7aIJIlY/b9b0mPTThJ8Y4loSNMAop0qqA8zf/9qtSmuW1RDZF3 e1PwTaUbgJrNP0WB5fFaYZXjiNNE5DY7OX+g2yqNKP7g868NYelTPhR8TzNg4e7RMlfpTQm79n0 L19E1BkA+rUYYMM7Jxn8x+SXfaYOpR12g+oe7cqCxDZKzhbGpkUH5wXcKdZCuLQ2AFlVHhTz8Q4 e/6aYDC6YA6+Q7kTrHO2jphNBN8Zkb2tyJIGBDiWz/BqI9N48gP4ciHlcd+/6qpE72D/2HHgimF LSMPj+QcTEUBIJj5Qb06+IBf8UOXUqrvJwTu9gdPXc46bYJr53nOU+eTwZYqYiCvbR6WHHxRAiY bSWN/e2L+dr3shHw4BY1H5LjUsuN/s3gslpsHTfmN/puBLWS2WPtPLMXFhpXuZmZszVybHGZyZS UxLJ0EgptR2wD6PSo= X-Google-Smtp-Source: AGHT+IEbUl2JCzptHoApCryobfgPX9okNUcS7jfuNyAQmqaUFFRa2xXzjiLnSNK61qJ4zpujJuNB3w== X-Received: by 2002:a5d:524d:0:b0:3f2:97a6:db6b with SMTP id ffacd0b85a97d-3f297a70589mr290499f8f.3.1758293883676; Fri, 19 Sep 2025 07:58:03 -0700 (PDT) Received: from xl-nested.c.googlers.com.com (124.62.78.34.bc.googleusercontent.com. [34.78.62.124]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3ee0fbc7188sm8551386f8f.37.2025.09.19.07.58.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 07:58:03 -0700 (PDT) From: Ethan Graham To: ethangraham@google.com, glider@google.com Cc: andreyknvl@gmail.com, andy@kernel.org, brauner@kernel.org, brendan.higgins@linux.dev, davem@davemloft.net, davidgow@google.com, dhowells@redhat.com, dvyukov@google.com, elver@google.com, herbert@gondor.apana.org.au, ignat@cloudflare.com, jack@suse.cz, jannh@google.com, johannes@sipsolutions.net, kasan-dev@googlegroups.com, kees@kernel.org, kunit-dev@googlegroups.com, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, lukas@wunner.de, rmoar@google.com, shuah@kernel.org, sj@kernel.org, tarasmadan@google.com Subject: [PATCH v2 06/10] kfuzztest: add KFuzzTest sample fuzz targets Date: Fri, 19 Sep 2025 14:57:46 +0000 Message-ID: <20250919145750.3448393-7-ethan.w.s.graham@gmail.com> X-Mailer: git-send-email 2.51.0.470.ga7dc726c21-goog In-Reply-To: <20250919145750.3448393-1-ethan.w.s.graham@gmail.com> References: <20250919145750.3448393-1-ethan.w.s.graham@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" From: Ethan Graham Add two simple fuzz target samples to demonstrate the KFuzzTest API and provide basic self-tests for the framework. These examples showcase how a developer can define a fuzz target using the FUZZ_TEST(), constraint, and annotation macros, and serve as runtime sanity checks for the core logic. For example, they test that out-of-bounds memory accesses into poisoned padding regions are correctly detected in a KASAN build. These have been tested by writing syzkaller-generated inputs into their debugfs 'input' files and verifying that the correct KASAN reports were triggered. Signed-off-by: Ethan Graham Acked-by: Alexander Potapenko --- PR v2: - Fix build issues pointed out by the kernel test robot . --- --- samples/Kconfig | 7 ++ samples/Makefile | 1 + samples/kfuzztest/Makefile | 3 + samples/kfuzztest/overflow_on_nested_buffer.c | 71 +++++++++++++++++++ samples/kfuzztest/underflow_on_buffer.c | 59 +++++++++++++++ 5 files changed, 141 insertions(+) create mode 100644 samples/kfuzztest/Makefile create mode 100644 samples/kfuzztest/overflow_on_nested_buffer.c create mode 100644 samples/kfuzztest/underflow_on_buffer.c diff --git a/samples/Kconfig b/samples/Kconfig index 6e072a5f1ed8..5209dd9d7a5c 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -320,6 +320,13 @@ config SAMPLE_HUNG_TASK Reading these files with multiple processes triggers hung task detection by holding locks for a long time (256 seconds). =20 +config SAMPLE_KFUZZTEST + bool "Build KFuzzTest sample targets" + depends on KFUZZTEST + help + Build KFuzzTest sample targets that serve as selftests for input + deserialization and inter-region redzone poisoning logic. + source "samples/rust/Kconfig" =20 source "samples/damon/Kconfig" diff --git a/samples/Makefile b/samples/Makefile index 07641e177bd8..3a0e7f744f44 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -44,4 +44,5 @@ obj-$(CONFIG_SAMPLE_DAMON_WSSE) +=3D damon/ obj-$(CONFIG_SAMPLE_DAMON_PRCL) +=3D damon/ obj-$(CONFIG_SAMPLE_DAMON_MTIER) +=3D damon/ obj-$(CONFIG_SAMPLE_HUNG_TASK) +=3D hung_task/ +obj-$(CONFIG_SAMPLE_KFUZZTEST) +=3D kfuzztest/ obj-$(CONFIG_SAMPLE_TSM_MR) +=3D tsm-mr/ diff --git a/samples/kfuzztest/Makefile b/samples/kfuzztest/Makefile new file mode 100644 index 000000000000..4f8709876c9e --- /dev/null +++ b/samples/kfuzztest/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only + +obj-$(CONFIG_SAMPLE_KFUZZTEST) +=3D overflow_on_nested_buffer.o underflow_= on_buffer.o diff --git a/samples/kfuzztest/overflow_on_nested_buffer.c b/samples/kfuzzt= est/overflow_on_nested_buffer.c new file mode 100644 index 000000000000..2f1c3ff9f750 --- /dev/null +++ b/samples/kfuzztest/overflow_on_nested_buffer.c @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * This file contains a KFuzzTest example target that ensures that a buffer + * overflow on a nested region triggers a KASAN OOB access report. + * + * Copyright 2025 Google LLC + */ + +/** + * DOC: test_overflow_on_nested_buffer + * + * This test uses a struct with two distinct dynamically allocated buffers. + * It checks that KFuzzTest's memory layout correctly poisons the memory + * regions and that KASAN can detect an overflow when reading one byte pas= t the + * end of the first buffer (`a`). + * + * It can be invoked with kfuzztest-bridge using the following command: + * + * ./kfuzztest-bridge \ + * "nested_buffers { ptr[a] len[a, u64] ptr[b] len[b, u64] }; \ + * a { arr[u8, 64] }; b { arr[u8, 64] };" \ + * "test_overflow_on_nested_buffer" /dev/urandom + * + * The first argument describes the C struct `nested_buffers` and specifie= s that + * both `a` and `b` are pointers to arrays of 64 bytes. + */ +#include + +static void overflow_on_nested_buffer(const char *a, size_t a_len, const c= har *b, size_t b_len) +{ + size_t i; + pr_info("a =3D [%px, %px)", a, a + a_len); + pr_info("b =3D [%px, %px)", b, b + b_len); + + /* Ensure that all bytes in arg->b are accessible. */ + for (i =3D 0; i < b_len; i++) + READ_ONCE(b[i]); + /* + * Check that all bytes in arg->a are accessible, and provoke an OOB on + * the first byte to the right of the buffer which will trigger a KASAN + * report. + */ + for (i =3D 0; i <=3D a_len; i++) + READ_ONCE(a[i]); +} + +struct nested_buffers { + const char *a; + size_t a_len; + const char *b; + size_t b_len; +}; + +/** + * The KFuzzTest input format specifies that struct nested buffers should + * be expanded as: + * + * | a | b | pad[8] | *a | pad[8] | *b | + * + * where the padded regions are poisoned. We expect to trigger a KASAN rep= ort by + * overflowing one byte into the `a` buffer. + */ +FUZZ_TEST(test_overflow_on_nested_buffer, struct nested_buffers) +{ + KFUZZTEST_EXPECT_NOT_NULL(nested_buffers, a); + KFUZZTEST_EXPECT_NOT_NULL(nested_buffers, b); + KFUZZTEST_ANNOTATE_LEN(nested_buffers, a_len, a); + KFUZZTEST_ANNOTATE_LEN(nested_buffers, b_len, b); + + overflow_on_nested_buffer(arg->a, arg->a_len, arg->b, arg->b_len); +} diff --git a/samples/kfuzztest/underflow_on_buffer.c b/samples/kfuzztest/un= derflow_on_buffer.c new file mode 100644 index 000000000000..02704a1bfebb --- /dev/null +++ b/samples/kfuzztest/underflow_on_buffer.c @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * This file contains a KFuzzTest example target that ensures that a buffer + * underflow on a region triggers a KASAN OOB access report. + * + * Copyright 2025 Google LLC + */ + +/** + * DOC: test_underflow_on_buffer + * + * This test ensures that the region between the metadata struct and the + * dynamically allocated buffer is poisoned. It provokes a one-byte underf= low + * on the buffer, which should be caught by KASAN. + * + * It can be invoked with kfuzztest-bridge using the following command: + * + * ./kfuzztest-bridge \ + * "some_buffer { ptr[buf] len[buf, u64]}; buf { arr[u8, 128] };" \ + * "test_underflow_on_buffer" /dev/urandom + * + * The first argument describes the C struct `some_buffer` and specifies t= hat + * `buf` is a pointer to an array of 128 bytes. The second argument is the= test + * name, and the third is a seed file. + */ +#include + +static void underflow_on_buffer(char *buf, size_t buflen) +{ + size_t i; + + pr_info("buf =3D [%px, %px)", buf, buf + buflen); + + /* First ensure that all bytes in arg->b are accessible. */ + for (i =3D 0; i < buflen; i++) + READ_ONCE(buf[i]); + /* + * Provoke a buffer overflow on the first byte preceding b, triggering + * a KASAN report. + */ + READ_ONCE(*((char *)buf - 1)); +} + +struct some_buffer { + char *buf; + size_t buflen; +}; + +/** + * Tests that the region between struct some_buffer and the expanded *buf = field + * is correctly poisoned by accessing the first byte before *buf. + */ +FUZZ_TEST(test_underflow_on_buffer, struct some_buffer) +{ + KFUZZTEST_EXPECT_NOT_NULL(some_buffer, buf); + KFUZZTEST_ANNOTATE_LEN(some_buffer, buflen, buf); + + underflow_on_buffer(arg->buf, arg->buflen); +} --=20 2.51.0.470.ga7dc726c21-goog From nobody Thu Oct 2 06:17:57 2025 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.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 9DDE23161A7 for ; Fri, 19 Sep 2025 14:58:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293889; cv=none; b=GrCu2IMygmWGR3ObEVWSWEY43fwewgO+SN2ZU4F3fSYeLyY8JPleCg4h8Dw9LUDpiCzwBOjXW5hsMOtZr6YGQji9/kcio+PqD2OMO46oVZmPIUWhPdN2BIZTHnSItHJRxmyNw0OYI8bp4EY+bNGXsxz/HLuh6vZEiRxRD5GzQv4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293889; c=relaxed/simple; bh=dpLQzjfBI0xMu3e0xhjAEBQgs/fUasFxh/U//MgBt5o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VO6iT+xwKMlkNh0g09drqzZXDaz60Rn4Po9w/NrFseb8K8fwU2OnHedaZfy6gYCdWjbtb9eC+Ceyxc6ucEJOyToM9M6QxykVbbQIh+WuTjXIhQsEQPXDy2HPcfbWi+d+d1swXp/wHFm4SD2hPJwAgOC+wNYiB9Hf0jRnRocXaLI= 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=Ifi/w9ye; arc=none smtp.client-ip=209.85.128.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="Ifi/w9ye" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-45dd5e24d16so21750985e9.3 for ; Fri, 19 Sep 2025 07:58:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758293885; x=1758898685; 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; bh=0kKd/JXPbzTz5+ezoQ1J+mpBYdbScAPUDVkzXIQoH/8=; b=Ifi/w9ye3sQ7Va4AU6+hEhrxNtSxF+8i84izK0DbVa4LyXjlM8sVsQJ6DIc1ST26B8 Ht4x4LlAf78tlgMLDTA6BglxfD9jRnWEPbbpbjXbaO825PdOnR03+LX71gWU3kaIe5fZ Az6cNnAmT/Hsm6/I77gtr3HqTLI2vW9nAcfjJPwo2nPuDbdPFHxXmTidXk9XdjEL0B0B 3wCZpByw9H1hcRo8dWRujAQy4G9ztsmILr+x/uUmkVuKg33galxihY/l1QJJ5TAT+7Sq HeS9IfzOAmbD10kL+yeAeNMSpTbvzMKEC7cuavo3fa+b1E5G7s8fYIn/nJh3litmKAk7 nCRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758293885; x=1758898685; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0kKd/JXPbzTz5+ezoQ1J+mpBYdbScAPUDVkzXIQoH/8=; b=l2gRHK2YG4KuWUo0SeHIbmKtR0LClFcwAmDTvZMRr4jJXpEAHw2ePprBZFs1/czkJT y6bcQHXRCFOn9Ye9tDMHeKzCkYT6rxcdDAwY03TGZ1BJT2UH3UM5/6fDH3C+p2ZnedR8 Uc2TMRZ+zee8/5/tuBy5GIMBh6sE7+XQp/LqCef5ofkQUYP6BMaUzExoyBB3KX4ucXGu dpLyeEkrRpp0S9lX5gINm1L8RZPLQFi4Lu3DnQYYyTR194edxfmoUkcxJ+zzNdiJMDaQ 8RD+w5fejbS4NNrOT9hEQl9C7gDJRN6Uj9G3DF8wW5pq+UjDKt9O8dzBwcC5UQkD4w3p wBjQ== X-Forwarded-Encrypted: i=1; AJvYcCVQCsDiQIJT9VpGeMWNuXs1sFLFpU1g0vpqPLBWpZegSYr9yfuJo3Q+CMYUazvOup1uYOOiuUNkFiqkDhE=@vger.kernel.org X-Gm-Message-State: AOJu0YzgsNJ5fyz1oIYn6w5aa6YIGJX3pnGnB5fwxVEpwAcwRR/uYMvv O8PoWQ93l9HOW/bM9IEtKJrFsIQql8jw7Fz3ylL4Dc6QHo24kwKIlRqp X-Gm-Gg: ASbGncsxD+O/XhtnDqzINzVrjpojNCxPJRsWYymcAYtC1O6VWJLhR0RwKh2JemU2oUc Q+WM7/6UVOHF1dGeqkmZn6J+ujf6qdHOiOTU9xnjau9MOrupPCpW7NDA0HYgk/fmn2nNLPohRcy 1HeWJ3uCZum4FVD7w2/KD3KbN7k+lg7QWeiIX4S0TB+2QH39D8MVhrXISppbA7T3XYjg/bskEpT fIN7XDakft2Sn5qjBs8ffNLbaoxx5kp3WlmZpw3DgvqFE88VliQ/MEneScG4UzKOeStZglSqsbd 8ainzpvU8IiC/AmQmcIqlNe8iHoTZpcSWiPgwZrRkj6uESiKJQZFkUjkEAMA2BBegyTE9lgxyWh sCdwt9vWXDQrxxxsenpflEyIs0/nWK7cM+F7oIg1umawfsFNSx9cdzisHtNkQDIeOhOQLtEAd9t fIt1+9j147wnWwB+w= X-Google-Smtp-Source: AGHT+IGPZQtZaXpB5KQ16hieOpy0bgFcG29LZPkxphuKGCWvRd3WuhUNDUxdZ3+qFBNVj+jHlwKp4w== X-Received: by 2002:a05:600c:c4ab:b0:45f:2919:5e91 with SMTP id 5b1f17b1804b1-467e7f7e36dmr43014905e9.16.1758293884348; Fri, 19 Sep 2025 07:58:04 -0700 (PDT) Received: from xl-nested.c.googlers.com.com (124.62.78.34.bc.googleusercontent.com. [34.78.62.124]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3ee0fbc7188sm8551386f8f.37.2025.09.19.07.58.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 07:58:03 -0700 (PDT) From: Ethan Graham To: ethangraham@google.com, glider@google.com Cc: andreyknvl@gmail.com, andy@kernel.org, brauner@kernel.org, brendan.higgins@linux.dev, davem@davemloft.net, davidgow@google.com, dhowells@redhat.com, dvyukov@google.com, elver@google.com, herbert@gondor.apana.org.au, ignat@cloudflare.com, jack@suse.cz, jannh@google.com, johannes@sipsolutions.net, kasan-dev@googlegroups.com, kees@kernel.org, kunit-dev@googlegroups.com, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, lukas@wunner.de, rmoar@google.com, shuah@kernel.org, sj@kernel.org, tarasmadan@google.com Subject: [PATCH v2 07/10] crypto: implement KFuzzTest targets for PKCS7 and RSA parsing Date: Fri, 19 Sep 2025 14:57:47 +0000 Message-ID: <20250919145750.3448393-8-ethan.w.s.graham@gmail.com> X-Mailer: git-send-email 2.51.0.470.ga7dc726c21-goog In-Reply-To: <20250919145750.3448393-1-ethan.w.s.graham@gmail.com> References: <20250919145750.3448393-1-ethan.w.s.graham@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" From: Ethan Graham Add KFuzzTest targets for pkcs7_parse_message, rsa_parse_pub_key, and rsa_parse_priv_key to serve as real-world examples of how the framework is used. These functions are ideal candidates for KFuzzTest as they perform complex parsing of user-controlled data but are not directly exposed at the syscall boundary. This makes them difficult to exercise with traditional fuzzing tools and showcases the primary strength of the KFuzzTest framework: providing an interface to fuzz internal functions. To validate the effectiveness of the framework on these new targets, we injected two artificial bugs and let syzkaller fuzz the targets in an attempt to catch them. The first of these was calling the asn1 decoder with an incorrect input from pkcs7_parse_message, like so: - ret =3D asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen); + ret =3D asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen + 1); The second was bug deeper inside of asn1_ber_decoder itself, like so: - for (len =3D 0; n > 0; n--) + for (len =3D 0; n >=3D 0; n--) syzkaller was able to trigger these bugs, and the associated KASAN slab-out-of-bounds reports, within seconds. The targets are defined within crypto/asymmetric-keys/tests. Signed-off-by: Ethan Graham Reviewed-by: Ignat Korchagin --- PR v2: - Make fuzz targets also depend on the KConfig options needed for the functions they are fuzzing, CONFIG_PKCS7_MESSAGE_PARSER and CONFIG_CRYPTO_RSA respectively. - Fix build issues pointed out by the kernel test robot . - Account for return value of pkcs7_parse_message, and free resources if the function call succeeds. PR v1: - Change the fuzz target build to depend on CONFIG_KFUZZTEST=3Dy, eliminating the need for a separate config option for each individual file as suggested by Ignat Korchagin. - Remove KFUZZTEST_EXPECT_LE on the length of the `key` field inside of the fuzz targets. A maximum length is now set inside of the core input parsing logic. RFC v2: - Move KFuzzTest targets outside of the source files into dedicated _kfuzz.c files under /crypto/asymmetric_keys/tests/ as suggested by Ignat Korchagin and Eric Biggers. --- --- crypto/asymmetric_keys/Makefile | 2 + crypto/asymmetric_keys/tests/Makefile | 4 ++ crypto/asymmetric_keys/tests/pkcs7_kfuzz.c | 26 +++++++++++++ .../asymmetric_keys/tests/rsa_helper_kfuzz.c | 38 +++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 crypto/asymmetric_keys/tests/Makefile create mode 100644 crypto/asymmetric_keys/tests/pkcs7_kfuzz.c create mode 100644 crypto/asymmetric_keys/tests/rsa_helper_kfuzz.c diff --git a/crypto/asymmetric_keys/Makefile b/crypto/asymmetric_keys/Makef= ile index bc65d3b98dcb..77b825aee6b2 100644 --- a/crypto/asymmetric_keys/Makefile +++ b/crypto/asymmetric_keys/Makefile @@ -67,6 +67,8 @@ obj-$(CONFIG_PKCS7_TEST_KEY) +=3D pkcs7_test_key.o pkcs7_test_key-y :=3D \ pkcs7_key_type.o =20 +obj-y +=3D tests/ + # # Signed PE binary-wrapped key handling # diff --git a/crypto/asymmetric_keys/tests/Makefile b/crypto/asymmetric_keys= /tests/Makefile new file mode 100644 index 000000000000..023d6a65fb89 --- /dev/null +++ b/crypto/asymmetric_keys/tests/Makefile @@ -0,0 +1,4 @@ +pkcs7-kfuzz-y :=3D $(and $(CONFIG_KFUZZTEST),$(CONFIG_PKCS7_MESSAGE_PARSER= )) +rsa-helper-kfuzz-y :=3D $(and $(CONFIG_KFUZZTEST),$(CONFIG_CRYPTO_RSA)) +obj-$(pkcs7-kfuzz-y) +=3D pkcs7_kfuzz.o +obj-$(rsa-helper-kfuzz-y) +=3D rsa_helper_kfuzz.o diff --git a/crypto/asymmetric_keys/tests/pkcs7_kfuzz.c b/crypto/asymmetric= _keys/tests/pkcs7_kfuzz.c new file mode 100644 index 000000000000..c801f6b59de2 --- /dev/null +++ b/crypto/asymmetric_keys/tests/pkcs7_kfuzz.c @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * PKCS#7 parser KFuzzTest target + * + * Copyright 2025 Google LLC + */ +#include +#include + +struct pkcs7_parse_message_arg { + const void *data; + size_t datalen; +}; + +FUZZ_TEST(test_pkcs7_parse_message, struct pkcs7_parse_message_arg) +{ + struct pkcs7_message *msg; + + KFUZZTEST_EXPECT_NOT_NULL(pkcs7_parse_message_arg, data); + KFUZZTEST_ANNOTATE_ARRAY(pkcs7_parse_message_arg, data); + KFUZZTEST_ANNOTATE_LEN(pkcs7_parse_message_arg, datalen, data); + + msg =3D pkcs7_parse_message(arg->data, arg->datalen); + if (msg && !IS_ERR(msg)) + kfree(msg); +} diff --git a/crypto/asymmetric_keys/tests/rsa_helper_kfuzz.c b/crypto/asymm= etric_keys/tests/rsa_helper_kfuzz.c new file mode 100644 index 000000000000..bd29ed5e8c82 --- /dev/null +++ b/crypto/asymmetric_keys/tests/rsa_helper_kfuzz.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * RSA key extract helper KFuzzTest targets + * + * Copyright 2025 Google LLC + */ +#include +#include + +struct rsa_parse_pub_key_arg { + const void *key; + size_t key_len; +}; + +FUZZ_TEST(test_rsa_parse_pub_key, struct rsa_parse_pub_key_arg) +{ + KFUZZTEST_EXPECT_NOT_NULL(rsa_parse_pub_key_arg, key); + KFUZZTEST_ANNOTATE_ARRAY(rsa_parse_pub_key_arg, key); + KFUZZTEST_ANNOTATE_LEN(rsa_parse_pub_key_arg, key_len, key); + + struct rsa_key out; + rsa_parse_pub_key(&out, arg->key, arg->key_len); +} + +struct rsa_parse_priv_key_arg { + const void *key; + size_t key_len; +}; + +FUZZ_TEST(test_rsa_parse_priv_key, struct rsa_parse_priv_key_arg) +{ + KFUZZTEST_EXPECT_NOT_NULL(rsa_parse_priv_key_arg, key); + KFUZZTEST_ANNOTATE_ARRAY(rsa_parse_priv_key_arg, key); + KFUZZTEST_ANNOTATE_LEN(rsa_parse_priv_key_arg, key_len, key); + + struct rsa_key out; + rsa_parse_priv_key(&out, arg->key, arg->key_len); +} --=20 2.51.0.470.ga7dc726c21-goog From nobody Thu Oct 2 06:17:57 2025 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 520E23164BA for ; Fri, 19 Sep 2025 14:58:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293889; cv=none; b=KJCyyRgtUkYj+MiI/f5rcPBnhsDXLHmLyAy5BAr8Jh6OiFeZKrDeHlwt+DPihbtE6iffeqSPoEj1Jn6V3ibKvVJZkIFEw5d2KJwTl0oKlZcIMeMAa9Y9T81STTozwl8OYiHMfxcZhtkz7WKeALU72bG1eCLoJseCEzbCWAfifpU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293889; c=relaxed/simple; bh=z4XRQgx/v9aMDHTqeuKkcVV2lob3wsrHL0J2Rkuf4lo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QxBMhjO7dycCdelXsMz9QfUCZ60QsD7Z6sTo4QkBXcaieeCqEpt8Cc1iItBIU2ih0LYEl6ubd8xhg9RHh5Nk6HdAYIqG7iCIVp0vdoqndvnkqiBr7hbpPbOQhaTy4KKCGGCUTds7z5b9Qj1aYje8sE5KADM15UffbOwilrFCsVI= 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=ndL66Goc; arc=none smtp.client-ip=209.85.128.44 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="ndL66Goc" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-45cb6428c46so21469635e9.1 for ; Fri, 19 Sep 2025 07:58:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758293885; x=1758898685; 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; bh=wa5F8osjsxPwGqDjaoLyZTZuU1vO95PkOFG1IS0GKrs=; b=ndL66Goc6s91o2EtgHWCwvtiQxsc18t85qSefhQXDFaAVJbQl1E/cRoAwxauK0d0Mz I+XfyP82yUY+WmFCv3cSXzUcip03iXKXTTDNpblPmq/NMt9fq+OTe50kY1sm2Zf0+umj TVM6u00JkBVfXkgMcmeh1FcmToQzszDx3/0Rma9KfuWKoj0UWmEpRu02kYCXfVPBoEJL TpoxHZUD95o6G7tpGAKqPaAMbld6nulm0/LvVKstJoxHX44pGpTM32FPixA55aT454pM f5gqQcL87cX7aN1FWDRNKBnoMIkK3Yca5+FqwbiVVzgshlbKlOBJJqjeRxJqiZslFm3d c21Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758293885; x=1758898685; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wa5F8osjsxPwGqDjaoLyZTZuU1vO95PkOFG1IS0GKrs=; b=Z/ZgQbAdwvgSyLz56SJEHph8vRTTCR+kdhzlMfBXrrKG6Ai79mI5n04NuJn4tvH7Cf aakmgny6QugCPWzVqWHmPh/I9c8lq9e019pPp5CEkUgM2N0o/13Lx1VxpCpCMOgrtEWR 4aoyAAcQRYP9fj+rmg1lIypQbl0rksSXBgui6MkMd2vc8RxK/rh1SS4tSV11Ls0T8GGv BW/G8XDe+S/w5jBN9RzQ2S23taJuoIu0RACsUZ5N3caiJ5N1McBkRrG+Mzo89ltlmnH7 0d3h0QIgixA0JtJ4f3AJ6UjhwsaSyM9zInvu9lqjzdABtSkREWuPwXIiAIS8BqAZJ2pb y++A== X-Forwarded-Encrypted: i=1; AJvYcCWs3Gw9qgjsomRPHV35UdQV+SQp4mFwujsOSZXsWTkR+MQj25EIP9x02qXu3M544JGxuYcKwtFXhLhX8+Y=@vger.kernel.org X-Gm-Message-State: AOJu0YyH1y0zertPif9r+LCEKzuPLP+bgsrmkbP7liQZbKBy2HIGfJKg 211CF6fNeeh9mvdIBEGugCP7eLR0DYVVfQHDPmnpELvJkVHDCWwpudE8 X-Gm-Gg: ASbGncucuI3SdR8GPJox8oQMazKAhX9i1knsEHSfVQDlKbifQAOBBgFBOoG+xuWZP5H C6SaJ8oUkAsQ4a1nsWE0W8GBAMYDv/CdTIaA7gRgriVwVYU0acNAV5+YjI6aIWjNRRxZ18h/+Ai Khyd2LCg+OPqAGynNaj4ks3qdbEO67hSV//OquOzvnxdrOf4WjWZqiSrDjoXAi4e3f+T+jhBmzw uv2oU5S148I3GLtsY2MhnwNgNFzmuTH/Jxn6dR1rFeh1rmS2gRnAskYCT0zed3QC8hVyirPl7LV 3frCdBwwNcTccNar/SVia4+Ld2nTv91VEPDQUORTVKUUkeBymfNqLzfoy206k8wfTAoTzWt4mv0 g/ENXdoywpgmFQ0Zd0S9pxz2Qb7E4DjJ7KvyFV7+Aat+/nJSy1/ezXKFIfe6ZGTjWz+WeO58Ag8 MinXn2A5Szffx7vXk= X-Google-Smtp-Source: AGHT+IHdJYIXqz3C4OR8m/GvaJucPOCvchvPgeW6LDDv3ZVv3CkV0oEsEwnK0vfoZjKlHtOSmNp5DQ== X-Received: by 2002:a05:600c:4ecb:b0:45f:2bdd:c959 with SMTP id 5b1f17b1804b1-4684c13ec45mr31051625e9.8.1758293885086; Fri, 19 Sep 2025 07:58:05 -0700 (PDT) Received: from xl-nested.c.googlers.com.com (124.62.78.34.bc.googleusercontent.com. [34.78.62.124]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3ee0fbc7188sm8551386f8f.37.2025.09.19.07.58.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 07:58:04 -0700 (PDT) From: Ethan Graham To: ethangraham@google.com, glider@google.com Cc: andreyknvl@gmail.com, andy@kernel.org, brauner@kernel.org, brendan.higgins@linux.dev, davem@davemloft.net, davidgow@google.com, dhowells@redhat.com, dvyukov@google.com, elver@google.com, herbert@gondor.apana.org.au, ignat@cloudflare.com, jack@suse.cz, jannh@google.com, johannes@sipsolutions.net, kasan-dev@googlegroups.com, kees@kernel.org, kunit-dev@googlegroups.com, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, lukas@wunner.de, rmoar@google.com, shuah@kernel.org, sj@kernel.org, tarasmadan@google.com Subject: [PATCH v2 08/10] drivers/auxdisplay: add a KFuzzTest for parse_xy() Date: Fri, 19 Sep 2025 14:57:48 +0000 Message-ID: <20250919145750.3448393-9-ethan.w.s.graham@gmail.com> X-Mailer: git-send-email 2.51.0.470.ga7dc726c21-goog In-Reply-To: <20250919145750.3448393-1-ethan.w.s.graham@gmail.com> References: <20250919145750.3448393-1-ethan.w.s.graham@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" From: Ethan Graham Add a KFuzzTest fuzzer for the parse_xy() function, located in a new file under /drivers/auxdisplay/tests. To validate the correctness and effectiveness of this KFuzzTest target, a bug was injected into parse_xy() like so: drivers/auxdisplay/charlcd.c:179 - s =3D p; + s =3D p + 1; Although a simple off-by-one bug, it requires a specific input sequence in order to trigger it, thus demonstrating the power of pairing KFuzzTest with a coverage-guided fuzzer like syzkaller. Signed-off-by: Ethan Graham Acked-by: Alexander Potapenko --- drivers/auxdisplay/charlcd.c | 8 ++++++++ drivers/auxdisplay/tests/charlcd_kfuzz.c | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 drivers/auxdisplay/tests/charlcd_kfuzz.c diff --git a/drivers/auxdisplay/charlcd.c b/drivers/auxdisplay/charlcd.c index 09020bb8ad15..e079b5a9c93c 100644 --- a/drivers/auxdisplay/charlcd.c +++ b/drivers/auxdisplay/charlcd.c @@ -682,3 +682,11 @@ EXPORT_SYMBOL_GPL(charlcd_unregister); =20 MODULE_DESCRIPTION("Character LCD core support"); MODULE_LICENSE("GPL"); + +/* + * When CONFIG_KFUZZTEST is enabled, we include this _kfuzz.c file to ensu= re + * that KFuzzTest targets are built. + */ +#ifdef CONFIG_KFUZZTEST +#include "tests/charlcd_kfuzz.c" +#endif /* CONFIG_KFUZZTEST */ diff --git a/drivers/auxdisplay/tests/charlcd_kfuzz.c b/drivers/auxdisplay/= tests/charlcd_kfuzz.c new file mode 100644 index 000000000000..28ce7069c65c --- /dev/null +++ b/drivers/auxdisplay/tests/charlcd_kfuzz.c @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * charlcd KFuzzTest target + * + * Copyright 2025 Google LLC + */ +#include + +struct parse_xy_arg { + const char *s; +}; + +FUZZ_TEST(test_parse_xy, struct parse_xy_arg) +{ + unsigned long x, y; + + KFUZZTEST_EXPECT_NOT_NULL(parse_xy_arg, s); + KFUZZTEST_ANNOTATE_STRING(parse_xy_arg, s); + parse_xy(arg->s, &x, &y); +} --=20 2.51.0.470.ga7dc726c21-goog From nobody Thu Oct 2 06:17:57 2025 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (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 BF8A43164C1 for ; Fri, 19 Sep 2025 14:58:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293890; cv=none; b=mWRuXgeYTSR0e8bVN6qFPQXH78Ezf9g7VdK++s1I62MXQnTz4POi+CWf8zx0ZAWsmdnJNzn1LgsbES0NM+BrOd7sF19z0AfbLxGlit2WIobeS8Yto/qLNx+8K3OEDMExIGJKqT2cji2paIaXjJV/Qe3Rt9SfGdmZJdRyPeFP8g4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293890; c=relaxed/simple; bh=Pd4P4ZjKr+HHW58Tf2SLAY9QUfyLhUA5ZgTSmT8bTAM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ujOJV72fq9H/c2r8P3JMoONOtCPIszbmRxhaz3zMySzxqwfOTrhfBcLT+gWhIzKXcQcTKX6xzDTs/UbLgfQam+cTRnqN8qlgRj3B4XQYtpzHnY8npkSkMKgP3hbczUlKvwQ8VUjF5uOsCkAss0luIOQhvbgxkjOH9VMgczB6CaU= 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=UPL9nqpa; arc=none smtp.client-ip=209.85.221.44 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="UPL9nqpa" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-3ed20bdfdffso2277358f8f.2 for ; Fri, 19 Sep 2025 07:58:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758293886; x=1758898686; 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; bh=LT1ydHgovraiyOIOgetbRaAa0TBzaulhkSUGdqArzzs=; b=UPL9nqpaCvvGy8wDd5o3DosMxgZXx0RTXohbou61mAZ0pVEa8Gz0ldb8BiiK4C+zE3 wW9z4bEGM5ks2awBbAL72l6GAPL+R+D46LmJSnCnYfBD6Mp+jVMzxr+FmuWzmXT1mbXY VK+yEXDpHTZ0lu6ocxgqqliJP77TkVPRKl/nu56MNJAnr8Dr/E4Kxr4Rk8DsI1d/rrbE lB8UnfOv0guXFtidLPVbiSCpfHxLFM4/12OKp9OPFH+zpcAAYfKsTaRW6ZQk1PRF+eX6 Nde7fm6HyRqalFvyAEkGZ+SEVkpQ3l3N3ligiaThzm/7hkm3628tcy1Q0RKGtsdziH3W U3zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758293886; x=1758898686; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LT1ydHgovraiyOIOgetbRaAa0TBzaulhkSUGdqArzzs=; b=RpWgXgsGwnt6+DOnsGK7jb4YMBO+2rdFrryAdXIJ7ysqZpHYXu/3p45mWO0QXTKz3M j5mz73rsBOGw9XvVD/YpyRUrywDu6vJ0YNEaMdYFa1/f8FdqhQ9Dx9shXkJbvCSUQPCj r2+haQT04R8YwTbim3PlhVoTOhAPTLrJnHeSXIOX5SRsPkWl+2aCvsI3RSaFI5m/WYQF yATTIbzR/Wb1Rlt9Hj/umtjZyrFB3dAL43HK7H6uswPdNkO6BsNMFaRl+ztqtV7vWZQ/ 81g0OdtefZVecsJesrazD2usiHjUG7mcpDSk7KfR6UxMqcG/FFRc5jAPU9FXgSjib/WB CBlA== X-Forwarded-Encrypted: i=1; AJvYcCWILkWCktYuDwexT36lasj2r3cx5XvQ3Jd//U+B45IpFYHfkLZOY3nCnvl+GfWsFWbB8BR/oste/pt+i3I=@vger.kernel.org X-Gm-Message-State: AOJu0YwzFcK+c2xZi/PKx4uLr/a6eE2+h9VJr/zQaKDQoZrXmNr9oJvv tJwzvyo3TZroJ8HD1eqBZGiTA5nDLVkMfAxpjH8JLa2TsrZ+H4eFTSX2 X-Gm-Gg: ASbGncvE4KT8KiQSuuiEDaHr0KoLaX4x7a2YWeIWSALz4qV/rQEs1IRfAxbNM+PZqMO LGeP0AGI13yU/k2X66Vlylfc3SAYXQmYBCMSvRdwynA4OQ2vWoKHbsDrNzNRAo8mxE+ChwvNiM4 PkxFOUzscdgpldVAhD5sBeaL37Izc/6+h4LzJiNBp5OH0tLPEbBg8SK0nisIL8G/jTsMuLrBLIv iveMe50l3Hj09NnF/4C+2Y4ZQ+J3WS+kRfS2RfKXcNWn06visVl9zuZaG2qgZtLYaQrcwOAy+PA PHOKlGwJgykQ9oXbs6+wTLYt+9gn2awwJUF2ZgQN1n/P0FviUGajcIeaphVaeSorzCzLLvO6M8g 63ZqjDjUliuUz8mvAATjOqhIv24kVgzSJR7XGbLabJ1BZwBLCmSgFG9EvEEVPnx8H+jzNgg6LY2 8VXpZgriad98YybPQ= X-Google-Smtp-Source: AGHT+IHfk6FVy/mkcxlh5PwT7Aua9hjkv4iEAL059Dub4DeP/hMzVm4A59PgONlD461DIHsT16Nu4w== X-Received: by 2002:a05:6000:144f:b0:3ee:1357:e191 with SMTP id ffacd0b85a97d-3ee8407d5ecmr2286466f8f.30.1758293885827; Fri, 19 Sep 2025 07:58:05 -0700 (PDT) Received: from xl-nested.c.googlers.com.com (124.62.78.34.bc.googleusercontent.com. [34.78.62.124]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3ee0fbc7188sm8551386f8f.37.2025.09.19.07.58.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 07:58:05 -0700 (PDT) From: Ethan Graham To: ethangraham@google.com, glider@google.com Cc: andreyknvl@gmail.com, andy@kernel.org, brauner@kernel.org, brendan.higgins@linux.dev, davem@davemloft.net, davidgow@google.com, dhowells@redhat.com, dvyukov@google.com, elver@google.com, herbert@gondor.apana.org.au, ignat@cloudflare.com, jack@suse.cz, jannh@google.com, johannes@sipsolutions.net, kasan-dev@googlegroups.com, kees@kernel.org, kunit-dev@googlegroups.com, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, lukas@wunner.de, rmoar@google.com, shuah@kernel.org, sj@kernel.org, tarasmadan@google.com Subject: [PATCH v2 09/10] fs/binfmt_script: add KFuzzTest target for load_script Date: Fri, 19 Sep 2025 14:57:49 +0000 Message-ID: <20250919145750.3448393-10-ethan.w.s.graham@gmail.com> X-Mailer: git-send-email 2.51.0.470.ga7dc726c21-goog In-Reply-To: <20250919145750.3448393-1-ethan.w.s.graham@gmail.com> References: <20250919145750.3448393-1-ethan.w.s.graham@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" From: Ethan Graham Add a KFuzzTest target for the load_script function to serve as a real-world example of the framework's usage. The load_script function is responsible for parsing the shebang line (`#!`) of script files. This makes it an excellent candidate for KFuzzTest, as it involves parsing user-controlled data within the binary loading path, which is not directly exposed as a system call. The provided fuzz target in fs/tests/binfmt_script_kfuzz.c illustrates how to fuzz a function that requires more involved setup - here, we only let the fuzzer generate input for the `buf` field of struct linux_bprm, and manually set the other fields with sensible values inside of the FUZZ_TEST body. To demonstrate the effectiveness of the fuzz target, a buffer overflow bug was injected in the load_script function like so: - buf_end =3D bprm->buf + sizeof(bprm->buf) - 1; + buf_end =3D bprm->buf + sizeof(bprm->buf) + 1; Which was caught in around 40 seconds by syzkaller simultaneously fuzzing four other targets, a realistic use case where targets are continuously fuzzed. It also requires that the fuzzer be smart enough to generate an input starting with `#!`. While this bug is shallow, the fact that the bug is caught quickly and with minimal additional code can potentially be a source of confidence when modifying existing implementations or writing new functions. Signed-off-by: Ethan Graham Acked-by: Alexander Potapenko --- PR v2: - Introduce cleanup logic in the load_script fuzz target. --- --- fs/binfmt_script.c | 8 +++++ fs/tests/binfmt_script_kfuzz.c | 58 ++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 fs/tests/binfmt_script_kfuzz.c diff --git a/fs/binfmt_script.c b/fs/binfmt_script.c index 637daf6e4d45..c09f224d6d7e 100644 --- a/fs/binfmt_script.c +++ b/fs/binfmt_script.c @@ -157,3 +157,11 @@ core_initcall(init_script_binfmt); module_exit(exit_script_binfmt); MODULE_DESCRIPTION("Kernel support for scripts starting with #!"); MODULE_LICENSE("GPL"); + +/* + * When CONFIG_KFUZZTEST is enabled, we include this _kfuzz.c file to ensu= re + * that KFuzzTest targets are built. + */ +#ifdef CONFIG_KFUZZTEST +#include "tests/binfmt_script_kfuzz.c" +#endif /* CONFIG_KFUZZTEST */ diff --git a/fs/tests/binfmt_script_kfuzz.c b/fs/tests/binfmt_script_kfuzz.c new file mode 100644 index 000000000000..26397a465270 --- /dev/null +++ b/fs/tests/binfmt_script_kfuzz.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * binfmt_script loader KFuzzTest target + * + * Copyright 2025 Google LLC + */ +#include +#include +#include +#include + +struct load_script_arg { + char buf[BINPRM_BUF_SIZE]; +}; + +FUZZ_TEST(test_load_script, struct load_script_arg) +{ + struct linux_binprm bprm =3D {}; + char *arg_page; + + arg_page =3D (char *)get_zeroed_page(GFP_KERNEL); + if (!arg_page) + return; + + memcpy(bprm.buf, arg->buf, sizeof(bprm.buf)); + /* + * `load_script` calls remove_arg_zero, which expects argc !=3D 0. A + * static value of 1 is sufficient for fuzzing. + */ + bprm.argc =3D 1; + bprm.p =3D (unsigned long)arg_page + PAGE_SIZE; + bprm.filename =3D kstrdup("fuzz_script", GFP_KERNEL); + if (!bprm.filename) + goto cleanup; + bprm.interp =3D kstrdup(bprm.filename, GFP_KERNEL); + if (!bprm.interp) + goto cleanup; + + bprm.mm =3D mm_alloc(); + if (!bprm.mm) + goto cleanup; + + /* + * Call the target function. We expect it to fail and return an error + * (e.g., at open_exec), which is fine. The goal is to survive the + * initial parsing logic without crashing. + */ + load_script(&bprm); + +cleanup: + if (bprm.mm) + mmput(bprm.mm); + if (bprm.interp) + kfree(bprm.interp); + if (bprm.filename) + kfree(bprm.filename); + free_page((unsigned long)arg_page); +} --=20 2.51.0.470.ga7dc726c21-goog From nobody Thu Oct 2 06:17:57 2025 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (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 51F233164DF for ; Fri, 19 Sep 2025 14:58:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293890; cv=none; b=ByXvnJrAF7un4S00dD9uZe6QhH30Z4X5ZLSb6l9ETxDpNxdWQONTd9Ytb1fT2Qdb42bjyhpReYYUW3snDC9ofJ8A6RkdTCRLZ9dYg+fw7Iu/X0kqWkCmnnRLUzW3rxd7HG+SwC2nMYsugfK4UUIrJCaKxveijEJf2D2L9pI1wSo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758293890; c=relaxed/simple; bh=3vn7Sq0i7hG+a6szTOMm8s8ZW2GBG7tNPmnyf2hSsp8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DNZAFrYy7VnD4Iw5eBlpHN32/+ogTebYqg4vms5+pJHLakoCmyqdBY78Kmjah8J0toZwd5aViI3TVKVEHEAGYNuggPmqtGjrjnqEYVlfL6TeQokhHlrruzy+bvPuarozU6d07sBsllKcp6mMGi4qVkZqbugvcEWGm2UcolNcJsI= 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=bUoV5E98; arc=none smtp.client-ip=209.85.221.47 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="bUoV5E98" Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-3c68ac7e18aso1222361f8f.2 for ; Fri, 19 Sep 2025 07:58:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758293887; x=1758898687; 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; bh=cqsEMj50XfHOshwHI2kqebsGSz4e9jnLx3tuTedHdO8=; b=bUoV5E98nKkxs+7HB72ZK8Pl0dMf43RjXIUj336jgINJXRzhuM+jqJirfr7fSwwfzo CgmLm6JIYkKzUgZdHoHB5XAjBHpxnDdf7m26kKzF/mLrEddIm7BfushQ0aZyvc/oYlRj UFxg2EHiGk2OlaZwnSPSg9WK+e4RUEGYNaZu7QDzgusSNebroUUQ01Qr8mqEuH9yKWwy BOEFoNKJJs/nTDLlk3Y6y1OjjHZJLCRCl37wvAmfqohRRc6xihVBu4arPf3thwtxzDY8 eQFCOCZsEvFG+4gzooWMoC0sMyij+cflzvOnMyx8CL+cJXaPBI9RMSxraq4GHHjFqZqX c9Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758293887; x=1758898687; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cqsEMj50XfHOshwHI2kqebsGSz4e9jnLx3tuTedHdO8=; b=Dom8najGVRguRBL7Yid8CLsUTtjpM8D+DrkObbO9/EAaDr++Ed/rr/14mBy9fOsdHL 10/coYzEfNClEWWXi5Vh4ZXo7BezJCl+/f4IYSHsvcSrl7rGxbPOpjvB44DPXSlKBrCl S/1GMN3M9wO3w5XNSGxrAcU2AfUJlTy6/tN4QsxYF+PYLx364TjAEIoec1japwDybBGz +1LOssMKPV3kX8HodwS1HHcqdruSjz4aoAZRpUhNyOaMEtrXyBvuuhczTWHKVQ16h/ki f2ctcLIXSwAymR6R+fz95ytidPCTp4O2xucb+QxDRfNfRJHDR2+Fg4c7qZ8f0p64BBMU Z3Tg== X-Forwarded-Encrypted: i=1; AJvYcCXJn4VLhdyIbpfJapKvwuVNvmHbmVMx6rYnOHL/hjWUqWeZoBrduudGl3jrtaUJJj476UrtucVMZ7i/XGQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxJHW1FW/vxSn6Ae921c4cFlKaQLy0O926XpfCOTncfgEZr2vZf es7Se16n4iX9GGs9DqT9+HwvAGcR9QE6akWDXXMhc0TP8oht1Tgl09xi X-Gm-Gg: ASbGncvgpKEUgeQOtzdBzRP4b3TzwCMqGsWD1xtNFROMIVGzG35Ybg0mvktl9xg28H8 k6yHdIOlgEXjx9kBQctIeW2pk38wg4cWYALaiCfUPXvhdBZ+bsHtvCRPA7fxK/9jE2ixSVYY293 ztTD3nP/2h2HPG/0VRZHPjOf6FXCi6bAmApN+wqWDmPKMpZIhKiPElrrxcAR7YTN+lkJ55gba82 kIanXIFvoz/3lp3Y2k0s676K+m7FLNzXZOX6rTNHzjPFYWOvEhZqxmiZ6RfReAQdU+d8rRz4TOp aruFgAHhIOtlptJu/QPorca4nFH2/PgZODbjQ6BHoi58G48VsLlRedEh2DunDS6i+Krg378z7r5 zu2xtMcfw2dE0wqYwdh7deqq/lNP4DSTukAWpdpDBQlKfnT/MlkNc6VWh5eAJzcJ7MkNU5yRoqo wJkMD7Vtjqf1u5Ooo= X-Google-Smtp-Source: AGHT+IEQGQ5Ibv5m+pzFfVuYC0xkpO2eoDLOdQC6WRoyuHEWEKPg7eRdhB1UiP1O9gJT0nbSt3KJMg== X-Received: by 2002:a5d:584f:0:b0:3d1:61f0:d26c with SMTP id ffacd0b85a97d-3ee862edc77mr2891484f8f.54.1758293886534; Fri, 19 Sep 2025 07:58:06 -0700 (PDT) Received: from xl-nested.c.googlers.com.com (124.62.78.34.bc.googleusercontent.com. [34.78.62.124]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3ee0fbc7188sm8551386f8f.37.2025.09.19.07.58.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Sep 2025 07:58:06 -0700 (PDT) From: Ethan Graham To: ethangraham@google.com, glider@google.com Cc: andreyknvl@gmail.com, andy@kernel.org, brauner@kernel.org, brendan.higgins@linux.dev, davem@davemloft.net, davidgow@google.com, dhowells@redhat.com, dvyukov@google.com, elver@google.com, herbert@gondor.apana.org.au, ignat@cloudflare.com, jack@suse.cz, jannh@google.com, johannes@sipsolutions.net, kasan-dev@googlegroups.com, kees@kernel.org, kunit-dev@googlegroups.com, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, lukas@wunner.de, rmoar@google.com, shuah@kernel.org, sj@kernel.org, tarasmadan@google.com Subject: [PATCH v2 10/10] MAINTAINERS: add maintainer information for KFuzzTest Date: Fri, 19 Sep 2025 14:57:50 +0000 Message-ID: <20250919145750.3448393-11-ethan.w.s.graham@gmail.com> X-Mailer: git-send-email 2.51.0.470.ga7dc726c21-goog In-Reply-To: <20250919145750.3448393-1-ethan.w.s.graham@gmail.com> References: <20250919145750.3448393-1-ethan.w.s.graham@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" From: Ethan Graham Add myself as maintainer and Alexander Potapenko as reviewer for KFuzzTest. Signed-off-by: Ethan Graham Acked-by: Alexander Potapenko --- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 6dcfbd11efef..14972e3e9d6a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13641,6 +13641,14 @@ F: include/linux/kfifo.h F: lib/kfifo.c F: samples/kfifo/ =20 +KFUZZTEST +M: Ethan Graham +R: Alexander Potapenko +F: include/linux/kfuzztest.h +F: lib/kfuzztest/ +F: Documentation/dev-tools/kfuzztest.rst +F: tools/kfuzztest-bridge/ + KGDB / KDB /debug_core M: Jason Wessel M: Daniel Thompson --=20 2.51.0.470.ga7dc726c21-goog