public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] inotify: Use strscpy() for event->name copies
@ 2024-12-16 22:45 Kees Cook
  2024-12-17  4:12 ` Matthew Wilcox
  2024-12-18 10:35 ` Jan Kara
  0 siblings, 2 replies; 4+ messages in thread
From: Kees Cook @ 2024-12-16 22:45 UTC (permalink / raw)
  To: Jan Kara
  Cc: Kees Cook, Amir Goldstein, linux-fsdevel, linux-kernel,
	linux-hardening

Since we have already allocated "len + 1" space for event->name, make sure
that name->name cannot ever accidentally cause a copy overflow by calling
strscpy() instead of the unbounded strcpy() routine. This assists in
the ongoing efforts to remove the unsafe strcpy() API[1] from the kernel.

Link: https://github.com/KSPP/linux/issues/88 [1]
Signed-off-by: Kees Cook <kees@kernel.org>
---
Cc: Jan Kara <jack@suse.cz>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: linux-fsdevel@vger.kernel.org
---
 fs/notify/inotify/inotify_fsnotify.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/notify/inotify/inotify_fsnotify.c b/fs/notify/inotify/inotify_fsnotify.c
index 993375f0db67..cd7d11b0eb08 100644
--- a/fs/notify/inotify/inotify_fsnotify.c
+++ b/fs/notify/inotify/inotify_fsnotify.c
@@ -121,7 +121,7 @@ int inotify_handle_inode_event(struct fsnotify_mark *inode_mark, u32 mask,
 	event->sync_cookie = cookie;
 	event->name_len = len;
 	if (len)
-		strcpy(event->name, name->name);
+		strscpy(event->name, name->name, event->name_len + 1);
 
 	ret = fsnotify_add_event(group, fsn_event, inotify_merge);
 	if (ret) {
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] inotify: Use strscpy() for event->name copies
  2024-12-16 22:45 [PATCH] inotify: Use strscpy() for event->name copies Kees Cook
@ 2024-12-17  4:12 ` Matthew Wilcox
  2024-12-17  7:51   ` Kees Cook
  2024-12-18 10:35 ` Jan Kara
  1 sibling, 1 reply; 4+ messages in thread
From: Matthew Wilcox @ 2024-12-17  4:12 UTC (permalink / raw)
  To: Kees Cook
  Cc: Jan Kara, Amir Goldstein, linux-fsdevel, linux-kernel,
	linux-hardening

On Mon, Dec 16, 2024 at 02:45:15PM -0800, Kees Cook wrote:
> Since we have already allocated "len + 1" space for event->name, make sure
> that name->name cannot ever accidentally cause a copy overflow by calling
> strscpy() instead of the unbounded strcpy() routine. This assists in
> the ongoing efforts to remove the unsafe strcpy() API[1] from the kernel.

Since a qstr can't contain a NUL before the length, why not just use
memcpy()?

>  	event->name_len = len;
>  	if (len)
> -		strcpy(event->name, name->name);
> +		strscpy(event->name, name->name, event->name_len + 1);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] inotify: Use strscpy() for event->name copies
  2024-12-17  4:12 ` Matthew Wilcox
@ 2024-12-17  7:51   ` Kees Cook
  0 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2024-12-17  7:51 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jan Kara, Amir Goldstein, linux-fsdevel, linux-kernel,
	linux-hardening



On December 16, 2024 8:12:07 PM PST, Matthew Wilcox <willy@infradead.org> wrote:
>On Mon, Dec 16, 2024 at 02:45:15PM -0800, Kees Cook wrote:
>> Since we have already allocated "len + 1" space for event->name, make sure
>> that name->name cannot ever accidentally cause a copy overflow by calling
>> strscpy() instead of the unbounded strcpy() routine. This assists in
>> the ongoing efforts to remove the unsafe strcpy() API[1] from the kernel.
>
>Since a qstr can't contain a NUL before the length, why not just use
>memcpy()?
>
>>  	event->name_len = len;
>>  	if (len)
>> -		strcpy(event->name, name->name);
>> +		strscpy(event->name, name->name, event->name_len + 1);

So that the destination is guaranteed to be NUL terminated no matter what's in the source. :) (i.e. try to limit unlikely conditions from expanding.)

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] inotify: Use strscpy() for event->name copies
  2024-12-16 22:45 [PATCH] inotify: Use strscpy() for event->name copies Kees Cook
  2024-12-17  4:12 ` Matthew Wilcox
@ 2024-12-18 10:35 ` Jan Kara
  1 sibling, 0 replies; 4+ messages in thread
From: Jan Kara @ 2024-12-18 10:35 UTC (permalink / raw)
  To: Kees Cook
  Cc: Jan Kara, Amir Goldstein, linux-fsdevel, linux-kernel,
	linux-hardening

On Mon 16-12-24 14:45:15, Kees Cook wrote:
> Since we have already allocated "len + 1" space for event->name, make sure
> that name->name cannot ever accidentally cause a copy overflow by calling
> strscpy() instead of the unbounded strcpy() routine. This assists in
> the ongoing efforts to remove the unsafe strcpy() API[1] from the kernel.
> 
> Link: https://github.com/KSPP/linux/issues/88 [1]
> Signed-off-by: Kees Cook <kees@kernel.org>

Thanks. Added to my tree.

								Honza

> ---
> Cc: Jan Kara <jack@suse.cz>
> Cc: Amir Goldstein <amir73il@gmail.com>
> Cc: linux-fsdevel@vger.kernel.org
> ---
>  fs/notify/inotify/inotify_fsnotify.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/notify/inotify/inotify_fsnotify.c b/fs/notify/inotify/inotify_fsnotify.c
> index 993375f0db67..cd7d11b0eb08 100644
> --- a/fs/notify/inotify/inotify_fsnotify.c
> +++ b/fs/notify/inotify/inotify_fsnotify.c
> @@ -121,7 +121,7 @@ int inotify_handle_inode_event(struct fsnotify_mark *inode_mark, u32 mask,
>  	event->sync_cookie = cookie;
>  	event->name_len = len;
>  	if (len)
> -		strcpy(event->name, name->name);
> +		strscpy(event->name, name->name, event->name_len + 1);
>  
>  	ret = fsnotify_add_event(group, fsn_event, inotify_merge);
>  	if (ret) {
> -- 
> 2.34.1
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-12-18 10:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-16 22:45 [PATCH] inotify: Use strscpy() for event->name copies Kees Cook
2024-12-17  4:12 ` Matthew Wilcox
2024-12-17  7:51   ` Kees Cook
2024-12-18 10:35 ` Jan Kara

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox