vfs: Annotate struct fdtable's fd field with __counted_by_ptr
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/2 vfs: Annotate struct fdtable's fd field with __counted_by_ptr
The 'struct fdtable' holds the file descriptor table information, including the current file descriptor array 'fd' and its size 'max_fds'. To harden the kernel against out-of-bounds accesses, we can annotate the 'fd' pointer field with the '__counted_by_ptr' attribute, referencing 'max_fds'.
The compiler uses the '__counted_by_ptr' attribute to track the size of the memory allocated for the pointer field, enabling runtime bounds checks under KASAN and fortified functions. There are three places where a 'struct fdtable' is initialized, and in all of them, 'max_fds' is set before the 'fd' pointer is accessed or assigned in all allocation and initialization places.
No accesses to 'fd' occur before 'max_fds' is set, preventing any potential runtime false-positives or panics due to uninitialized count fields.
This patch was generated by CodeMender and checked by submitter.
---
Cc: Alexander Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
--- include/linux/fdtable.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/fdtable.h +1 -1
@@ -25,7 +25,7 @@struct fdtable {unsigned int max_fds; -struct file __rcu **fd;/* current fd array */ + struct file __rcu **fd __counted_by_ptr(max_fds); /* current fd array */unsigned long *close_on_exec;unsigned long *open_fds;unsigned long *full_fds_bits;
Signature · 1 line
2.55.0.679.g6767b8d81c-goog
Quoted 39 lines · Bill Wendling
The 'struct fdtable' holds the file descriptor table information, including the current file descriptor array 'fd' and its size '
max_fds'. To harden the kernel against out-of-bounds accesses, we can annotate the 'fd' pointer field with the '__counted_by_ptr' attribute, referencing 'max_fds'.The compiler uses the '
__counted_by_ptr' attribute to track the size of the memory allocated for the pointer field, enabling runtime bounds checks under KASAN and fortified functions. There are three places where a 'struct fdtable' is initialized, and in all of them, 'max_fds' is set before the 'fd' pointer is accessed or assigned in all allocation and initialization places.No accesses to 'fd' occur before '
max_fds' is set, preventing any potential runtime false-positives or panics due to uninitialized count fields.This patch was generated by CodeMender and checked by submitter.
Signed-off-by Bill Wendling <[email protected]>---
Cc: Alexander Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]--- include/linux/fdtable.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/fdtable.h +0
-0
include/linux/fdtable.h line 31
Quoted 8 lines
include/linux/fdtable.h +1
-1@@ -25,7 +25,7 @@struct fdtable {unsigned int max_fds; -struct file __rcu **fd;/* current fd array */ + struct file __rcu **fd __counted_by_ptr(max_fds); /* current fd array */unsigned long *close_on_exec;unsigned long *open_fds;unsigned long *full_fds_bits;
I see the alloc_fdtable test, that's one max_fds assignment, but I also see dup_fd(), which does the assignment after the newf allocation, so that looks safe too, but it might be nice to add that allocation path to the tests too?
Signature · 1 line
Kees Cook
Quoted 42 lines · Kees Cook
Quoted 40 lines · Bill Wendling
The 'struct fdtable' holds the file descriptor table information, including the current file descriptor array 'fd' and its size '
max_fds'. To harden the kernel against out-of-bounds accesses, we can annotate the 'fd' pointer field with the '__counted_by_ptr' attribute, referencing 'max_fds'.The compiler uses the '
__counted_by_ptr' attribute to track the size of the memory allocated for the pointer field, enabling runtime bounds checks under KASAN and fortified functions. There are three places where a 'struct fdtable' is initialized, and in all of them, 'max_fds' is set before the 'fd' pointer is accessed or assigned in all allocation and initialization places.No accesses to 'fd' occur before '
max_fds' is set, preventing any potential runtime false-positives or panics due to uninitialized count fields.This patch was generated by CodeMender and checked by submitter.
Signed-off-by Bill Wendling <[email protected]>---
Cc: Alexander Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]--- include/linux/fdtable.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/fdtable.h +0
-0@@ -25,7 +25,7 @@
Quoted 13 lines
include/linux/fdtable.h line 31
Quoted 7 lines
include/linux/fdtable.h +1
-1struct fdtable {unsigned int max_fds; -struct file __rcu **fd;/* current fd array */ + struct file __rcu **fd __counted_by_ptr(max_fds); /* current fd array */unsigned long *close_on_exec;unsigned long *open_fds;unsigned long *full_fds_bits;I see the
alloc_fdtabletest, that's onemax_fdsassignment, but I also seedup_fd(), which does the assignment after the newf allocation, so that looks safe too, but it might be nice to add that allocation path to the tests too?
Hi Kees,
I added a testcase for the 'dup_fd' path.
-bw
PATCH 1/2 vfs: Annotate struct fdtable's fd field with __counted_by_ptr
In reply to a message not in the archiveThe 'struct fdtable' holds the file descriptor table information, including the current file descriptor array 'fd' and its size 'max_fds'. To harden the kernel against out-of-bounds accesses, we can annotate the 'fd' pointer field with the '__counted_by_ptr' attribute, referencing 'max_fds'.
The compiler uses the '__counted_by_ptr' attribute to track the size of the memory allocated for the pointer field, enabling runtime bounds checks under KASAN and fortified functions. There are three places where a 'struct fdtable' is initialized, and in all of them, 'max_fds' is set before the 'fd' pointer is accessed or assigned in all allocation and initialization places.
No accesses to 'fd' occur before 'max_fds' is set, preventing any potential runtime false-positives or panics due to uninitialized count fields.
---
Cc: Alexander Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
--- include/linux/fdtable.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/fdtable.h +1 -1
@@ -25,7 +25,7 @@struct fdtable {unsigned int max_fds; -struct file __rcu **fd;/* current fd array */ + struct file __rcu **fd __counted_by_ptr(max_fds); /* current fd array */unsigned long *close_on_exec;unsigned long *open_fds;unsigned long *full_fds_bits;
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
PATCH 2/2 vfs: Add KUnit tests for fdtable
This adds a KUnit test suite for fdtable to verify correct allocation, max_fds initialization, and dynamic object size of the fd array under __counted_by_ptr when CONFIG_CC_HAS_COUNTED_BY_PTR is enabled.
---
v2 - Added 'test_dup_fd' testcase for separate allocation path.
---
Cc: Alexander Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
--- fs/Kconfig | 8 +++++ fs/file.c | 4 +++ fs/tests/.kunitconfig | 2 ++ fs/tests/fdtable_kunit.c | 72 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 fs/tests/.kunitconfig create mode 100644 fs/tests/fdtable_kunit.c
fs/Kconfig +8 -0
@@ -421,4 +421,12 @@ source "fs/unicode/Kconfig" config IO_WQ bool +config FDTABLE_KUNIT_TEST + bool "KUnit test for fdtable" if !KUNIT_ALL_TESTS + depends on KUNIT=y + default KUNIT_ALL_TESTS + help + This builds the fdtable KUnit tests, which tests various aspects + of the fdtable structure and allocation. + endmenu
fs/file.c +4 -0
@@ -1529,3 +1529,7 @@ int iterate_fd(struct files_struct *files, unsigned n,return res; }EXPORT_SYMBOL (iterate_fd); + +#ifdef CONFIG_FDTABLE_KUNIT_TEST +#include "tests/fdtable_kunit.c" +#endif
fs/tests/.kunitconfig new file +2 -0
@@ -0,0 +1,2 @@ +CONFIG_KUNIT=y +CONFIG_FDTABLE_KUNIT_TEST=y
fs/tests/fdtable_kunit.c new file +72 -0
72 lines not on this page · raw message
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
PATCH 2/2 vfs: Add KUnit tests for fdtable
In reply to a message not in the archiveThis adds a KUnit test suite for fdtable to verify correct allocation, max_fds initialization, and dynamic object size of the fd array under __counted_by_ptr when CONFIG_CC_HAS_COUNTED_BY_PTR is enabled.
This patch was generated by CodeMender and checked by submitter.
---
Cc: Alexander Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
--- fs/Kconfig | 8 ++++++++ fs/file.c | 4 ++++ fs/tests/.kunitconfig | 2 ++ fs/tests/fdtable_kunit.c | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 fs/tests/.kunitconfig create mode 100644 fs/tests/fdtable_kunit.c
fs/Kconfig +8 -0
@@ -423,4 +423,12 @@ source "fs/unicode/Kconfig" config IO_WQ bool +config FDTABLE_KUNIT_TEST + bool "KUnit test for fdtable" if !KUNIT_ALL_TESTS + depends on KUNIT=y + default KUNIT_ALL_TESTS + help + This builds the fdtable KUnit tests, which tests various aspects + of the fdtable structure and allocation. + endmenu
fs/file.c +4 -0
@@ -1529,3 +1529,7 @@ int iterate_fd(struct files_struct *files, unsigned n,return res; }EXPORT_SYMBOL (iterate_fd); + +#ifdef CONFIG_FDTABLE_KUNIT_TEST +#include "tests/fdtable_kunit.c" +#endif
fs/tests/.kunitconfig new file +2 -0
@@ -0,0 +1,2 @@ +CONFIG_KUNIT=y +CONFIG_FDTABLE_KUNIT_TEST=y
fs/tests/fdtable_kunit.c new file +40 -0
@@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <kunit/test.h> +#include <linux/fdtable.h> +#include <linux/file.h> + +static void fdtable_test_alloc (struct kunit *test) +{ +struct fdtable *fdt; +unsigned int slots =64 ; + + fdt =alloc_fdtable (slots); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, fdt); + +/* Check that max_fds is set correctly and is >= slots */ +KUNIT_EXPECT_GE (test, fdt->max_fds, slots); + +/* Check that fd is allocated */ +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, fdt->fd); + +/* Check dynamic object size of fdt->fd if compiler supports __counted_by_ptr */ +#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR +KUNIT_EXPECT_EQ (test,__builtin_dynamic_object_size (fdt->fd,0 ), + fdt->max_fds *sizeof (struct file *)); +#endif + +/* Free the fdtable */ +__free_fdtable (fdt); +} + +static struct kunit_case fdtable_test_cases[] = { +KUNIT_CASE (fdtable_test_alloc), + {} +}; + +static struct kunit_suite fdtable_test_suite = { + .name ="fdtable" , + .test_cases = fdtable_test_cases, +}; + +kunit_test_suite (fdtable_test_suite);
Signature · 1 line
2.55.0.679.g6767b8d81c-goog
Re: [PATCH 2/2] vfs: Add KUnit tests for fdtable
fs/tests/fdtable_kunit.c line 18
Quoted 13 lines · Bill Wendling
fs/tests/fdtable_kunit.c new file +13
-0+static void fdtable_test_alloc (struct kunit *test) +{ +struct fdtable *fdt; +unsigned int slots =64 ; + + fdt =alloc_fdtable (slots); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, fdt); + +/* Check that max_fds is set correctly and is >= slots */ +KUNIT_EXPECT_GE (test, fdt->max_fds, slots); + +/* Check that fd is allocated */ +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, fdt->fd);
Nice to add these tests! Can you add one for each of the conditionals in alloc_fdtable (e.g. ENOMEM, EMFILE, and the power-of-two rounding-up logic, etc)?
-Kees
Signature · 1 line
Kees Cook
Re: [PATCH 2/2] vfs: Add KUnit tests for fdtable
Quoted 10 lines · Kees Cook
Quoted 9 lines · Bill Wendling
fs/tests/fdtable_kunit.c new file +9
-0+static void fdtable_test_alloc (struct kunit *test) +{ +struct fdtable *fdt; +unsigned int slots =64 ; + + fdt =alloc_fdtable (slots); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, fdt); + +/* Check that max_fds is set correctly and is >= slots */
Quoted 8 lines
fs/tests/fdtable_kunit.c line 18
Quoted 4 lines
fs/tests/fdtable_kunit.c new file +4
-0+KUNIT_EXPECT_GE (test, fdt->max_fds, slots); + +/* Check that fd is allocated */ +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, fdt->fd);Nice to add these tests! Can you add one for each of the conditionals in
alloc_fdtable(e.g. ENOMEM, EMFILE, and the power-of-two rounding-up logic, etc)?
Wouldn't tests for stuff like the rounding-up logic get into implementation details too much, and break if implementation choices change?
Re: [PATCH 2/2] vfs: Add KUnit tests for fdtable
Quoted 20 lines · Jann Horn
Quoted 10 lines · Kees Cook
Quoted 9 lines · Bill Wendling
fs/tests/fdtable_kunit.c new file +9
-0+static void fdtable_test_alloc (struct kunit *test) +{ +struct fdtable *fdt; +unsigned int slots =64 ; + + fdt =alloc_fdtable (slots); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, fdt); + +/* Check that max_fds is set correctly and is >= slots */ Quoted 8 lines
fs/tests/fdtable_kunit.c line 18
Quoted 4 lines
fs/tests/fdtable_kunit.c new file +4
-0+KUNIT_EXPECT_GE (test, fdt->max_fds, slots); + +/* Check that fd is allocated */ +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, fdt->fd);Nice to add these tests! Can you add one for each of the conditionals in
alloc_fdtable(e.g. ENOMEM, EMFILE, and the power-of-two rounding-up logic, etc)?
Quoted 3 lines
Wouldn't tests for stuff like the rounding-up logic get into implementation details too much, and break if implementation choices change?
It seemed to me like we'd want to notice if that behavior changed?
Signature · 1 line
Kees Cook
Re: [PATCH 2/2] vfs: Add KUnit tests for fdtable
Quoted 21 lines · Kees Cook
Quoted 20 lines · Jann Horn
Quoted 18 lines · Kees Cook
fs/tests/fdtable_kunit.c line 18
Quoted 13 lines · Bill Wendling
fs/tests/fdtable_kunit.c new file +13
-0+static void fdtable_test_alloc (struct kunit *test) +{ +struct fdtable *fdt; +unsigned int slots =64 ; + + fdt =alloc_fdtable (slots); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, fdt); + +/* Check that max_fds is set correctly and is >= slots */ +KUNIT_EXPECT_GE (test, fdt->max_fds, slots); + +/* Check that fd is allocated */ +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, fdt->fd);Nice to add these tests! Can you add one for each of the conditionals in
alloc_fdtable(e.g. ENOMEM, EMFILE, and the power-of-two rounding-up logic, etc)?
Quoted 5 lines
Quoted 3 lines
Wouldn't tests for stuff like the rounding-up logic get into implementation details too much, and break if implementation choices change?
It seemed to me like we'd want to notice if that behavior changed?
Why?
File descriptor tables are often power-of-two sized, but they aren't always. That is an implementation choice that doesn't impact correctness, it just affects performance somewhat. So I don't see what the point of a test for this would be - it seems like it would just be in the way of making changes, a "Change-Detector Test"?
Christian Brauner
maintainerRe: [PATCH 2/2] vfs: Add KUnit tests for fdtable
Quoted 26 lines · Jann Horn
Quoted 25 lines · Kees Cook
Quoted 23 lines · Jann Horn
Quoted 10 lines · Kees Cook
Quoted 9 lines · Bill Wendling
fs/tests/fdtable_kunit.c new file +9
-0+static void fdtable_test_alloc (struct kunit *test) +{ +struct fdtable *fdt; +unsigned int slots =64 ; + + fdt =alloc_fdtable (slots); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, fdt); + +/* Check that max_fds is set correctly and is >= slots */ Quoted 8 lines
fs/tests/fdtable_kunit.c line 18
Quoted 4 lines
fs/tests/fdtable_kunit.c new file +4
-0+KUNIT_EXPECT_GE (test, fdt->max_fds, slots); + +/* Check that fd is allocated */ +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, fdt->fd);Nice to add these tests! Can you add one for each of the conditionals in
alloc_fdtable(e.g. ENOMEM, EMFILE, and the power-of-two rounding-up logic, etc)?Wouldn't tests for stuff like the rounding-up logic get into implementation details too much, and break if implementation choices change?
Quoted 8 lines
Quoted 1 line
It seemed to me like we'd want to notice if that behavior changed?
Why?
File descriptor tables are often power-of-two sized, but they aren't always. That is an implementation choice that doesn't impact correctness, it just affects performance somewhat. So I don't see what the point of a test for this would be - it seems like it would just be in the way of making changes, a "Change-Detector Test"?
Yeah, I don't think this makes much sense.
PATCH v3 1/2 vfs: Annotate struct fdtable's fd field with __counted_by_ptr
In reply to a message not in the archiveThe 'struct fdtable' holds the file descriptor table information, including the current file descriptor array 'fd' and its size 'max_fds'. To harden the kernel against out-of-bounds accesses, we can annotate the 'fd' pointer field with the '__counted_by_ptr' attribute, referencing 'max_fds'.
The compiler uses the '__counted_by_ptr' attribute to track the size of the memory allocated for the pointer field, enabling runtime bounds checks under KASAN and fortified functions. There are three places where a 'struct fdtable' is initialized, and in all of them, 'max_fds' is set before the 'fd' pointer is accessed or assigned in all allocation and initialization places.
No accesses to 'fd' occur before 'max_fds' is set, preventing any potential runtime false-positives or panics due to uninitialized count fields.
---
v3 - Add version to the PATCH subject line
---
Cc: Alexander Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
--- include/linux/fdtable.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/fdtable.h +1 -1
@@ -25,7 +25,7 @@struct fdtable {unsigned int max_fds; -struct file __rcu **fd;/* current fd array */ + struct file __rcu **fd __counted_by_ptr(max_fds); /* current fd array */unsigned long *close_on_exec;unsigned long *open_fds;unsigned long *full_fds_bits;
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
PATCH v3 2/2 vfs: Add KUnit tests for fdtable
This adds a KUnit test suite for fdtable to verify correct allocation, max_fds initialization, and dynamic object size of the fd array under __counted_by_ptr when CONFIG_CC_HAS_COUNTED_BY_PTR is enabled.
---
v2 - Added 'test_dup_fd' testcase for separate allocation path. v3 - Add version to the PATCH subject line
---
Cc: Alexander Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
--- fs/Kconfig | 8 +++++ fs/file.c | 4 +++ fs/tests/.kunitconfig | 2 ++ fs/tests/fdtable_kunit.c | 72 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 fs/tests/.kunitconfig create mode 100644 fs/tests/fdtable_kunit.c
fs/Kconfig +8 -0
@@ -421,4 +421,12 @@ source "fs/unicode/Kconfig" config IO_WQ bool +config FDTABLE_KUNIT_TEST + bool "KUnit test for fdtable" if !KUNIT_ALL_TESTS + depends on KUNIT=y + default KUNIT_ALL_TESTS + help + This builds the fdtable KUnit tests, which tests various aspects + of the fdtable structure and allocation. + endmenu
fs/file.c +4 -0
@@ -1529,3 +1529,7 @@ int iterate_fd(struct files_struct *files, unsigned n,return res; }EXPORT_SYMBOL (iterate_fd); + +#ifdef CONFIG_FDTABLE_KUNIT_TEST +#include "tests/fdtable_kunit.c" +#endif
fs/tests/.kunitconfig new file +2 -0
@@ -0,0 +1,2 @@ +CONFIG_KUNIT=y +CONFIG_FDTABLE_KUNIT_TEST=y
fs/tests/fdtable_kunit.c new file +72 -0
72 lines not on this page · raw message
Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
Re: [PATCH v3 2/2] vfs: Add KUnit tests for fdtable
Quoted 6 lines · Bill Wendling
This adds a KUnit test suite for fdtable to verify correct allocation,
max_fdsinitialization, and dynamic object size of the fd array under__counted_by_ptrwhenCONFIG_CC_HAS_COUNTED_BY_PTRis enabled.Assisted-by Gemini:3.1-pro-previewSigned-off-by Bill Wendling <[email protected]>
Looks sensible. Feel free to add:
Honza
Quoted 140 lines
---
v2 - Added 'test_dup_fd' testcase for separate allocation path. v3 - Add version to the PATCH subject line
---
Cc: Alexander Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]--- fs/Kconfig | 8 +++++ fs/file.c | 4 +++ fs/tests/.kunitconfig | 2 ++ fs/tests/fdtable_kunit.c | 72 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 fs/tests/.kunitconfig create mode 100644 fs/tests/fdtable_kunit.cfs/Kconfig +8
-0@@ -421,4 +421,12 @@ source "fs/unicode/Kconfig" config IO_WQ bool +config FDTABLE_KUNIT_TEST + bool "KUnit test for fdtable" if !KUNIT_ALL_TESTS + depends on KUNIT=y + default KUNIT_ALL_TESTS + help + This builds the fdtable KUnit tests, which tests various aspects + of the fdtable structure and allocation. + endmenufs/file.c +4
-0@@ -1529,3 +1529,7 @@ int iterate_fd(struct files_struct *files, unsigned n,return res; }EXPORT_SYMBOL (iterate_fd); + +#ifdef CONFIG_FDTABLE_KUNIT_TEST +#include "tests/fdtable_kunit.c" +#endif fs/tests/.kunitconfig new file +2
-0@@ -0,0 +1,2 @@ +CONFIG_KUNIT=y +CONFIG_FDTABLE_KUNIT_TEST=yfs/tests/fdtable_kunit.c new file +72
-0@@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <kunit/test.h> +#include <linux/fdtable.h> +#include <linux/file.h> + +static void test_alloc_fdtable (struct kunit *test) +{ +struct fdtable *fdt; +unsigned int slots =64 ; + + fdt =alloc_fdtable (slots); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, fdt); + +/* Check that max_fds is set correctly and is >= slots */ +KUNIT_EXPECT_GE (test, fdt->max_fds, slots); + +/* Check that fd is allocated */ +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, fdt->fd); + +/* +* Check dynamic object size of fdt->fd if compiler supports +* __counted_by_ptr. +*/ +#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR +KUNIT_EXPECT_EQ (test,__builtin_dynamic_object_size (fdt->fd,0 ), + fdt->max_fds *sizeof (struct file *)); +#endif + +__free_fdtable (fdt); +} + +static void test_dup_fd (struct kunit *test) +{ +struct files_struct *newf; +struct fdtable *fdt; + + newf =dup_fd (&init_files,NULL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, newf); + + fdt =rcu_dereference_raw (newf->fdt); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, fdt); + +/* Check that max_fds is set correctly and is >= NR_OPEN_DEFAULT */ +KUNIT_EXPECT_GE (test, fdt->max_fds,NR_OPEN_DEFAULT ); + +/* Check that fd is allocated */ +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, fdt->fd); + +/* +* Check dynamic object size of fdt->fd if compiler supports +* __counted_by_ptr. +*/ +#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR +KUNIT_EXPECT_EQ (test,__builtin_dynamic_object_size (fdt->fd,0 ), + fdt->max_fds *sizeof (struct file *)); +#endif + +put_files_struct (newf); +} + +static struct kunit_case fdtable_test_cases[] = { +KUNIT_CASE (test_alloc_fdtable), +KUNIT_CASE (test_dup_fd), + {} +}; + +static struct kunit_suite fdtable_test_suite = { + .name ="fdtable" , + .test_cases = fdtable_test_cases, +}; + +kunit_test_suite (fdtable_test_suite);Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 13 lines · Bill Wendling
The 'struct fdtable' holds the file descriptor table information, including the current file descriptor array 'fd' and its size '
max_fds'. To harden the kernel against out-of-bounds accesses, we can annotate the 'fd' pointer field with the '__counted_by_ptr' attribute, referencing 'max_fds'.The compiler uses the '
__counted_by_ptr' attribute to track the size of the memory allocated for the pointer field, enabling runtime bounds checks under KASAN and fortified functions. There are three places where a 'struct fdtable' is initialized, and in all of them, 'max_fds' is set before the 'fd' pointer is accessed or assigned in all allocation and initialization places.
Quoted 6 lines
No accesses to 'fd' occur before '
max_fds' is set, preventing any potential runtime false-positives or panics due to uninitialized count fields.Assisted-by Gemini:3.1-pro-previewSigned-off-by Bill Wendling <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 32 lines
---
v3 - Add version to the PATCH subject line
---
Cc: Alexander Viro <[email protected]>
Cc: Christian Brauner <[email protected]>
Cc: Jan Kara <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]--- include/linux/fdtable.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/fdtable.h +1
-1@@ -25,7 +25,7 @@struct fdtable {unsigned int max_fds; -struct file __rcu **fd;/* current fd array */ + struct file __rcu **fd __counted_by_ptr(max_fds); /* current fd array */unsigned long *close_on_exec;unsigned long *open_fds;unsigned long *full_fds_bits;Signature · 1 line
2.55.0.897.gb25b4bd76c-goog
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 1 line · Bill Wendling
vfs: Annotate struct fdtable's fd field with
__counted_by_ptr
Applied to the vfs-7.4.file branch of the vfs/vfs.git tree. Patches in the vfs-7.4.file branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs-7.4.file
[1/2] vfs: Annotate struct fdtable's fd field with __counted_by_ptr
https://git.kernel.org/vfs/vfs/c/b4db97b9ef46 [2/2] vfs: Add KUnit tests for fdtable https://git.kernel.org/vfs/vfs/c/501cbbd276b7