firewire: Fix out-of-bounds access in CSR iterator
PATCH 1/1 firewire: Fix out-of-bounds access in CSR iterator
The fw_csr_iterator_next() function reads ci->p before checking if it has reached the end.
This can cause an out-of-bounds read when the iterator has no more entries.
Check the iterator position before reading the entry. Move the pointer forward only after reading a valid entry.
--- drivers/firewire/core-device.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/firewire/core-device.c +5 -1
@@ -40,10 +40,14 @@ EXPORT_SYMBOL(fw_csr_iterator_init);int fw_csr_iterator_next (struct fw_csr_iterator *ci,int *key,int *value) { +if (ci->p >= ci->end) +return 0 ; + *key = *ci->p >>24 ; *value = *ci->p &0xffffff ; -return ci->p++ < ci->end; + ci->p++; +return 1 ; }EXPORT_SYMBOL (fw_csr_iterator_next);
Signature · 1 line
2.55.0
Takashi Sakamoto
maintainerHi,
Thank you for proposing this fix, however I have mixed feelings about apply it, since the iterator is designed to traverse the cache data of the actual configuration ROM, which has been generated by the parser.
If the configuration ROM contains any malformed entries, they should already be detected by the parser. Therefore I don't think robustness of the iterator is a critical concern in this case.
For the v7.4 kernel, I've merged KUnit tests for the parser implementation. If you are still interested in discussion about this fix, please check them at first then try to add tests specific to the iterator itself.
Quoted 36 lines · Sreeraj S Kurup
The
fw_csr_iterator_next()function readsci->pbefore checking if it has reached the end.This can cause an out-of-bounds read when the iterator has no more entries.
Check the iterator position before reading the entry. Move the pointer forward only after reading a valid entry.
Signed-off-by Sreeraj S Kurup <[email protected]>--- drivers/firewire/core-device.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/firewire/core-device.c +5
-1@@ -40,10 +40,14 @@ EXPORT_SYMBOL(fw_csr_iterator_init);int fw_csr_iterator_next (struct fw_csr_iterator *ci,int *key,int *value) { +if (ci->p >= ci->end) +return 0 ; + *key = *ci->p >>24 ; *value = *ci->p &0xffffff ; -return ci->p++ < ci->end; + ci->p++; +return 1 ; }EXPORT_SYMBOL (fw_csr_iterator_next);Signature · 1 line
2.55.0
Thanks
Takashi Sakamoto
v2 cover firewire: Fix out-of-bounds access in CSR iterator
Hi Takashi,
This series fixes an out-of-bounds read in fw_csr_iterator_next() and adds KUnit tests for the CSR iterator.
The first patch fixes the iterator by checking its position before dereferencing the current entry.
The second patch adds a separate KUnit test suite covering:
- Iteration over valid entries.
- Calling the iterator after reaching the end.
- Iteration over an empty directory.
It also adds a separate Kconfig option for the iterator tests.
Testing:
- Ran the FireWire KUnit tests using:
python3 tools/testing/kunit/kunit.py run
--kunitconfig=drivers/firewire/.kunitconfig
- All tests passed.
- Results:
[14:47:14] ======== firewire-config-rom-iterator (3 subtests) ========= [14:47:14] [PASSED] test_csr_iterator_with_valid_entries [14:47:14] [PASSED] test_csr_iterator_at_end
[14:47:14] [PASSED] test_csr_iterator_empty_directory [14:47:14] ========== [PASSED] firewire-config-rom-iterator ===========
Please review the patches.
Regards,
Sreeraj
Sreeraj S Kurup (2):
firewire: Fix out-of-bounds access in CSR iterator
firewire: Add KUnit tests for CSR iterator
drivers/firewire/.kunitconfig | 1 + drivers/firewire/Kconfig | 12 +++ drivers/firewire/config-rom-iterator-test.c | 93 +++++++++++++++++++++ drivers/firewire/core-device.c | 10 ++- 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 drivers/firewire/config-rom-iterator-test.c
Signature · 1 line
2.55.0
PATCH v2 1/2 firewire: Fix out-of-bounds access in CSR iterator
The fw_csr_iterator_next() function reads ci->p before checking if it has reached the end.
This can cause an out-of-bounds read when the iterator has no more entries.
Check the iterator position before reading the entry. Move the pointer forward only after reading a valid entry.
--- drivers/firewire/core-device.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/firewire/core-device.c +5 -1
@@ -40,10 +40,14 @@ EXPORT_SYMBOL(fw_csr_iterator_init);int fw_csr_iterator_next (struct fw_csr_iterator *ci,int *key,int *value) { +if (ci->p >= ci->end) +return 0 ; + *key = *ci->p >>24 ; *value = *ci->p &0xffffff ; -return ci->p++ < ci->end; + ci->p++; +return 1 ; }EXPORT_SYMBOL (fw_csr_iterator_next);
Signature · 1 line
2.55.0
PATCH v2 2/2 firewire: Add KUnit tests for CSR iterator
In reply to Sreeraj S KurupAdd a separate KUnit test suite for the CSR iterator.
Test iteration over valid entries, an empty directory, and calling the iterator after reaching the end.
Add a separate Kconfig option to enable the iterator tests and update the KUnit configuration.
--- drivers/firewire/.kunitconfig | 1 + drivers/firewire/Kconfig | 12 +++ drivers/firewire/config-rom-iterator-test.c | 93 +++++++++++++++++++++ drivers/firewire/core-device.c | 4 + 4 files changed, 110 insertions(+) create mode 100644 drivers/firewire/config-rom-iterator-test.c
drivers/firewire/.kunitconfig +1 -0
@@ -8,3 +8,4 @@ CONFIG_FIREWIRE_KUNIT_SELF_ID_SEQUENCE_HELPER_TEST=y CONFIG_FIREWIRE_KUNIT_OHCI_SERDES_TEST=y CONFIG_FIREWIRE_KUNIT_NODE_TREE_TEST=y CONFIG_FIREWIRE_KUNIT_CONFIG_ROM_PARSER_AND_GENERATOR_TEST=y +CONFIG_FIREWIRE_KUNIT_CONFIG_ROM_ITERATOR_TEST=y
drivers/firewire/Kconfig +12 -0
@@ -112,6 +112,18 @@ config FIREWIRE_KUNIT_CONFIG_ROM_PARSER_AND_GENERATOR_TEST For more information on KUnit and unit tests in general, refer to the KUnit documentation in Documentation/dev-tools/kunit/. +config FIREWIRE_KUNIT_CONFIG_ROM_ITERATOR_TEST + tristate "KUnit tests for config ROM iterator" if !KUNIT_ALL_TESTS + depends on FIREWIRE && KUNIT + default KUNIT_ALL_TESTS + help + This builds KUnit tests for the configuration ROM iterator used + by the FireWire subsystem. + + The tests check iteration over valid entries, handling empty + directories, and calls to the iterator after reaching the end of + a directory + config FIREWIRE_OHCI tristate "OHCI-1394 controllers" depends on PCI && FIREWIRE
drivers/firewire/config-rom-iterator-test.c new file +93 -0
93 lines not on this page · raw message
drivers/firewire/core-device.c +4 -0
@@ -1450,3 +1450,7 @@ void fw_node_event(struct fw_card *card, struct fw_node *node, int event)#ifdef CONFIG_FIREWIRE_KUNIT_CONFIG_ROM_PARSER_AND_GENERATOR_TEST #include "config-rom-parser-test.c" #endif + +#ifdef CONFIG_FIREWIRE_KUNIT_CONFIG_ROM_ITERATOR_TEST +#include "config-rom-iterator-test.c" +#endif
Signature · 1 line
2.55.0
Takashi Sakamoto
maintainerHi,
Sorry for the delayed response. I wanted to take some time to carefully consider the issue that this patch is intended to address.
Quoted 31 lines · Sreeraj S Kurup
Hi Takashi,
This series fixes an out-of-bounds read in
fw_csr_iterator_next()and adds KUnit tests for the CSR iterator.The first patch fixes the iterator by checking its position before dereferencing the current entry.
The second patch adds a separate KUnit test suite covering:
- Iteration over valid entries.
- Calling the iterator after reaching the end.
- Iteration over an empty directory.It also adds a separate
Kconfigoption for the iterator tests.Testing:
- Ran the FireWire KUnit tests using:
python3tools/testing/kunit/kunit.pyrun
--kunitconfig=drivers/firewire/.kunitconfig- All tests passed.
- Results:
[14:47:14] ======== firewire-config-rom-iterator (3 subtests) ========= [14:47:14] [PASSED]test_csr_iterator_with_valid_entries[14:47:14] [PASSED]test_csr_iterator_at_end
[14:47:14] [PASSED]test_csr_iterator_empty_directory[14:47:14] ========== [PASSED] firewire-config-rom-iterator ===========
Quoted 4 lines
Please review the patches.
Regards,
Sreeraj
At this point, I would like to reject this patchset, as I believe it attempts to address problems that are unlikely to occur in practice.
In my review of the previous patchset, I mentioned the KUnit tests for configuration ROM parser (read_config_rom() function). I hope these tests have helped demonstrate that the parser can detect structurally malformed configuration ROMs.
Consequently, the parser copies the content of well-formed configuration ROM to the cache, after which the CSR iterator operates on that cached data. Therefore, an out-of-bounds access is unlikely as long as the iterator is used as intended, i.e. initialized directly with either 'struct fw_node.config_rom' or 'struct fw_unit.directory'.
I think it would be preferable to document the preconditions of fw_csr_iterator_init() in the kernel documentation rather than add more code to the function.
Of course, there is still room for discussion from the perspective of defensive programming. However, I think it is also worth considering the principle of Design by Contract.
Quoted 4 lines
Sreeraj S Kurup (2):
firewire: Fix out-of-bounds access in CSR iterator
firewire: Add KUnit tests for CSR iterator
Quoted 9 lines
drivers/firewire/.kunitconfig | 1 + drivers/firewire/Kconfig | 12 +++ drivers/firewire/config-rom-iterator-test.c | 93 +++++++++++++++++++++ drivers/firewire/core-device.c | 10 ++- 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 drivers/firewire/config-rom-iterator-test.cSignature · 1 line
2.55.0
Thanks
Takashi Sakamoto