printf: add bad-pointer tests for %ptT and %ptS
PATCH 1/1 printf: add bad-pointer tests for %ptT and %ptS
The printf KUnit suite exercises valid %ptR, %ptT, and %ptS inputs, but it does not cover bad pointers for the time64_t and timespec64 paths.
Add NULL and low-address pointer cases for %ptT and %ptS. The new checks verify that time_and_date() rejects bad pointers before dereferencing them and formats them as "(null)" or "(efault)".
Validated with the printf KUnit suite on arm64 QEMU and an incremental W=1 build of lib/tests/printf_kunit.o.
--- lib/tests/printf_kunit.c | 7 +++++++ 1 file changed, 7 insertions(+)
lib/tests/printf_kunit.c +7 -0
@@ -509,6 +509,13 @@ time_and_date(struct kunit *kunittest)struct timespec64 ts = { .tv_sec = t, .tv_nsec =11235813 };test ("(%pt?)" ,"%pt" , &tm); + +/* %ptT and %ptS reject bad pointers before dereference. */ +test ("(null)" ,"%ptT" ,NULL ); +test ("(efault)" ,"%ptT" ,PTR_INVALID ); +test ("(null)" ,"%ptS" ,NULL ); +test ("(efault)" ,"%ptS" ,PTR_INVALID ); +test ("2018-11-26T05:35:43" ,"%ptR" , &tm);test ("0118-10-26T05:35:43" ,"%ptRr" , &tm);test ("05:35:43|2018-11-26" ,"%ptRt|%ptRd" , &tm, &tm);
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 10 lines · Shuvam Pandey
The printf KUnit suite exercises valid %ptR, %ptT, and %ptS inputs, but it does not cover bad pointers for the
time64_tand timespec64 paths.Add NULL and low-address pointer cases for %ptT and %ptS. The new checks verify that
time_and_date()rejects bad pointers before dereferencing them and formats them as "(null)" or "(efault)".Validated with the printf KUnit suite on arm64 QEMU and an incremental W=1 build of
lib/tests/printf_kunit.o.
NAK.
It has nothing to do with %pt.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Petr Mladek
maintainerQuoted 9 lines · Andy Shevchenko
Quoted 8 lines · Shuvam Pandey
The printf KUnit suite exercises valid %ptR, %ptT, and %ptS inputs, but it does not cover bad pointers for the
time64_tand timespec64 paths.Add NULL and low-address pointer cases for %ptT and %ptS. The new checks verify that
time_and_date()rejects bad pointers before dereferencing them and formats them as "(null)" or "(efault)".
Quoted 6 lines
Quoted 2 lines
Validated with the printf KUnit suite on arm64 QEMU and an incremental W=1 build of
lib/tests/printf_kunit.o.NAK.
It has nothing to do with %pt.
Let me play the devil advocate.
There is no single check which would catch bad pointers for the various %p? format modifiers. It is because some of them handle them differently, for example, %pK, %pe, or plain %p.
I want to say that wrong pointers passed to %pt? are caught only because of the explicit check in:
static noinline_for_stack
char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
const char *fmt)
{
if (check_pointer(&buf, end, ptr, spec))
return buf;[...]
}
So, pointer-modifier-specific checks of wrong input might make sense.
Of course, it would be nice to create some generic solution for all affected pointer modifiers and not just for "%pt?".
Best Regards,
Petr
Andy Shevchenko
reviewerQuoted 32 lines · Petr Mladek
Quoted 9 lines · Andy Shevchenko
Quoted 8 lines · Shuvam Pandey
The printf KUnit suite exercises valid %ptR, %ptT, and %ptS inputs, but it does not cover bad pointers for the
time64_tand timespec64 paths.Add NULL and low-address pointer cases for %ptT and %ptS. The new checks verify that
time_and_date()rejects bad pointers before dereferencing them and formats them as "(null)" or "(efault)".Quoted 6 lines
Quoted 2 lines
Validated with the printf KUnit suite on arm64 QEMU and an incremental W=1 build of
lib/tests/printf_kunit.o.NAK.
It has nothing to do with %pt.
Let me play the devil advocate.
There is no single check which would catch bad pointers for the various %p? format modifiers. It is because some of them handle them differently, for example, %pK, %pe, or plain %p.
I want to say that wrong pointers passed to %pt? are caught only because of the explicit check in:
static
noinline_for_stack
char *time_and_date(char *buf, char *end, void *ptr, structprintf_specspec,
const char *fmt){ if (check_pointer(&buf, end, ptr, spec)) return buf;
Quoted 7 lines
[...]
}So, pointer-modifier-specific checks of wrong input might make sense.
Of course, it would be nice to create some generic solution for all affected pointer modifiers and not just for "%pt?".
We should have a check for check_pointer() then.
Signature · 2 lines
With Best Regards, Andy Shevchenko