diff mbox series

fortify: strnlen: Call fortify_panic() only if the number of bytes read is greater than maxlen

Message ID 20230724224857.2049906-1-sel4@tilde.club (mailing list archive)
State Rejected
Headers show
Series fortify: strnlen: Call fortify_panic() only if the number of bytes read is greater than maxlen | expand

Commit Message

sel4@tilde.club July 24, 2023, 10:48 p.m. UTC
From: Brandon Luo <sel4@tilde.club>

If the number of bytes read is p_size and p_size is less than maxlen,
fortify_panic() will be called incorrectly. Only panic if the number of
bytes read is greater than the minimum of p_size and maxlen since that is
 the argument to __real_strnlen().

Reported-by: syzbot+98d3ceb7e01269e7bf4f@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/000000000000d8352e0600c0c804@google.com/

Reported-by: syzbot+155274e882dcbf9885df@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/000000000000de4c2c0600c02b28@google.com/

Signed-off-by: Brandon Luo <sel4@tilde.club>
---
 include/linux/fortify-string.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Kees Cook July 24, 2023, 11:44 p.m. UTC | #1
On July 24, 2023 3:48:57 PM PDT, sel4@tilde.club wrote:
>From: Brandon Luo <sel4@tilde.club>
>
>If the number of bytes read is p_size and p_size is less than maxlen,
>fortify_panic() will be called incorrectly. Only panic if the number of
>bytes read is greater than the minimum of p_size and maxlen since that is
> the argument to __real_strnlen().
>
>Reported-by: syzbot+98d3ceb7e01269e7bf4f@syzkaller.appspotmail.com
>Closes: https://lore.kernel.org/all/000000000000d8352e0600c0c804@google.com/
>
>Reported-by: syzbot+155274e882dcbf9885df@syzkaller.appspotmail.com
>Closes: https://lore.kernel.org/all/000000000000de4c2c0600c02b28@google.com/

Thanks for looking at these, but strnlen is working correctly. The problem was hfs's overread of a cast be32 when used with strscpy. See:
https://lore.kernel.org/lkml/202307182147.A5B81B67D@keescook/

-Kees

>
>Signed-off-by: Brandon Luo <sel4@tilde.club>
>---
> include/linux/fortify-string.h | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
>diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h
>index da51a83b2829..cde637f735fe 100644
>--- a/include/linux/fortify-string.h
>+++ b/include/linux/fortify-string.h
>@@ -176,8 +176,9 @@ __FORTIFY_INLINE __kernel_size_t strnlen(const char * const POS p, __kernel_size
> 	}
> 
> 	/* Do not check characters beyond the end of p. */
>-	ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
>-	if (p_size <= ret && maxlen != ret)
>+	maxlen = (maxlen < p_size) ? maxlen : p_size;
>+	ret = __real_strnlen(p, maxlen);
>+	if (maxlen < ret)
> 		fortify_panic(__func__);
> 	return ret;
> }
diff mbox series

Patch

diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h
index da51a83b2829..cde637f735fe 100644
--- a/include/linux/fortify-string.h
+++ b/include/linux/fortify-string.h
@@ -176,8 +176,9 @@  __FORTIFY_INLINE __kernel_size_t strnlen(const char * const POS p, __kernel_size
 	}
 
 	/* Do not check characters beyond the end of p. */
-	ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
-	if (p_size <= ret && maxlen != ret)
+	maxlen = (maxlen < p_size) ? maxlen : p_size;
+	ret = __real_strnlen(p, maxlen);
+	if (maxlen < ret)
 		fortify_panic(__func__);
 	return ret;
 }