diff mbox series

[47/64] btrfs: Use memset_after() to clear end of struct

Message ID 20210727205855.411487-48-keescook@chromium.org
State New
Headers show
Series Introduce strict memcpy() bounds checking | expand

Commit Message

Kees Cook July 27, 2021, 8:58 p.m. UTC
In preparation for FORTIFY_SOURCE performing compile-time and run-time
field bounds checking for memset(), avoid intentionally writing across
neighboring fields.

Use memset_after() so memset() doesn't get confused about writing
beyond the destination member that is intended to be the starting point
of zeroing through the end of the struct.

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 fs/btrfs/root-tree.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

Comments

David Sterba July 28, 2021, 9:42 a.m. UTC | #1
On Tue, Jul 27, 2021 at 01:58:38PM -0700, Kees Cook wrote:
> In preparation for FORTIFY_SOURCE performing compile-time and run-time

> field bounds checking for memset(), avoid intentionally writing across

> neighboring fields.

> 

> Use memset_after() so memset() doesn't get confused about writing

> beyond the destination member that is intended to be the starting point

> of zeroing through the end of the struct.

> 

> Signed-off-by: Kees Cook <keescook@chromium.org>

> ---

>  fs/btrfs/root-tree.c | 5 +----

>  1 file changed, 1 insertion(+), 4 deletions(-)

> 

> diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c

> index 702dc5441f03..ec9e78f65fca 100644

> --- a/fs/btrfs/root-tree.c

> +++ b/fs/btrfs/root-tree.c

> @@ -39,10 +39,7 @@ static void btrfs_read_root_item(struct extent_buffer *eb, int slot,

>  		need_reset = 1;

>  	}

>  	if (need_reset) {

> -		memset(&item->generation_v2, 0,

> -			sizeof(*item) - offsetof(struct btrfs_root_item,

> -					generation_v2));

> -


Please add
		/* Clear all members from generation_v2 onwards */

> +		memset_after(item, 0, level);

>  		generate_random_guid(item->uuid);


Acked-by: David Sterba <dsterba@suse.com>
Kees Cook July 28, 2021, 9:56 p.m. UTC | #2
On Wed, Jul 28, 2021 at 11:42:15AM +0200, David Sterba wrote:
> On Tue, Jul 27, 2021 at 01:58:38PM -0700, Kees Cook wrote:

> > In preparation for FORTIFY_SOURCE performing compile-time and run-time

> > field bounds checking for memset(), avoid intentionally writing across

> > neighboring fields.

> > 

> > Use memset_after() so memset() doesn't get confused about writing

> > beyond the destination member that is intended to be the starting point

> > of zeroing through the end of the struct.

> > 

> > Signed-off-by: Kees Cook <keescook@chromium.org>

> > ---

> >  fs/btrfs/root-tree.c | 5 +----

> >  1 file changed, 1 insertion(+), 4 deletions(-)

> > 

> > diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c

> > index 702dc5441f03..ec9e78f65fca 100644

> > --- a/fs/btrfs/root-tree.c

> > +++ b/fs/btrfs/root-tree.c

> > @@ -39,10 +39,7 @@ static void btrfs_read_root_item(struct extent_buffer *eb, int slot,

> >  		need_reset = 1;

> >  	}

> >  	if (need_reset) {

> > -		memset(&item->generation_v2, 0,

> > -			sizeof(*item) - offsetof(struct btrfs_root_item,

> > -					generation_v2));

> > -

> 

> Please add

> 		/* Clear all members from generation_v2 onwards */

> 

> > +		memset_after(item, 0, level);


Perhaps there should be another helper memset_starting()? That would
make these cases a bit more self-documenting.

+		memset_starting(item, 0, generation_v2);

> >  		generate_random_guid(item->uuid);

> 

> Acked-by: David Sterba <dsterba@suse.com>


What do you think?

-Kees

-- 
Kees Cook
David Sterba July 29, 2021, 10:33 a.m. UTC | #3
On Wed, Jul 28, 2021 at 02:56:31PM -0700, Kees Cook wrote:
> On Wed, Jul 28, 2021 at 11:42:15AM +0200, David Sterba wrote:
> > On Tue, Jul 27, 2021 at 01:58:38PM -0700, Kees Cook wrote:
> > > In preparation for FORTIFY_SOURCE performing compile-time and run-time
> > > field bounds checking for memset(), avoid intentionally writing across
> > > neighboring fields.
> > > 
> > > Use memset_after() so memset() doesn't get confused about writing
> > > beyond the destination member that is intended to be the starting point
> > > of zeroing through the end of the struct.
> > > 
> > > Signed-off-by: Kees Cook <keescook@chromium.org>
> > > ---
> > >  fs/btrfs/root-tree.c | 5 +----
> > >  1 file changed, 1 insertion(+), 4 deletions(-)
> > > 
> > > diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
> > > index 702dc5441f03..ec9e78f65fca 100644
> > > --- a/fs/btrfs/root-tree.c
> > > +++ b/fs/btrfs/root-tree.c
> > > @@ -39,10 +39,7 @@ static void btrfs_read_root_item(struct extent_buffer *eb, int slot,
> > >  		need_reset = 1;
> > >  	}
> > >  	if (need_reset) {
> > > -		memset(&item->generation_v2, 0,
> > > -			sizeof(*item) - offsetof(struct btrfs_root_item,
> > > -					generation_v2));
> > > -
> > 
> > Please add
> > 		/* Clear all members from generation_v2 onwards */
> > 
> > > +		memset_after(item, 0, level);
> 
> Perhaps there should be another helper memset_starting()? That would
> make these cases a bit more self-documenting.

That would be better, yes.

> +		memset_starting(item, 0, generation_v2);

memset_from?
Kees Cook July 31, 2021, 3:25 p.m. UTC | #4
On Thu, Jul 29, 2021 at 12:33:37PM +0200, David Sterba wrote:
> On Wed, Jul 28, 2021 at 02:56:31PM -0700, Kees Cook wrote:
> > On Wed, Jul 28, 2021 at 11:42:15AM +0200, David Sterba wrote:
> > > On Tue, Jul 27, 2021 at 01:58:38PM -0700, Kees Cook wrote:
> > > > In preparation for FORTIFY_SOURCE performing compile-time and run-time
> > > > field bounds checking for memset(), avoid intentionally writing across
> > > > neighboring fields.
> > > > 
> > > > Use memset_after() so memset() doesn't get confused about writing
> > > > beyond the destination member that is intended to be the starting point
> > > > of zeroing through the end of the struct.
> > > > 
> > > > Signed-off-by: Kees Cook <keescook@chromium.org>
> > > > ---
> > > >  fs/btrfs/root-tree.c | 5 +----
> > > >  1 file changed, 1 insertion(+), 4 deletions(-)
> > > > 
> > > > diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
> > > > index 702dc5441f03..ec9e78f65fca 100644
> > > > --- a/fs/btrfs/root-tree.c
> > > > +++ b/fs/btrfs/root-tree.c
> > > > @@ -39,10 +39,7 @@ static void btrfs_read_root_item(struct extent_buffer *eb, int slot,
> > > >  		need_reset = 1;
> > > >  	}
> > > >  	if (need_reset) {
> > > > -		memset(&item->generation_v2, 0,
> > > > -			sizeof(*item) - offsetof(struct btrfs_root_item,
> > > > -					generation_v2));
> > > > -
> > > 
> > > Please add
> > > 		/* Clear all members from generation_v2 onwards */
> > > 
> > > > +		memset_after(item, 0, level);
> > 
> > Perhaps there should be another helper memset_starting()? That would
> > make these cases a bit more self-documenting.
> 
> That would be better, yes.
> 
> > +		memset_starting(item, 0, generation_v2);
> 
> memset_from?

For v2, I bikeshed this to "memset_startat" since "from" is semantically
close to "source" which I thought might be confusing. (I, too, did not
like "starting".) :)

Can I make "bikeshed" a verb? :P
David Sterba Aug. 9, 2021, 11:20 a.m. UTC | #5
On Sat, Jul 31, 2021 at 08:25:51AM -0700, Kees Cook wrote:
> On Thu, Jul 29, 2021 at 12:33:37PM +0200, David Sterba wrote:

> > On Wed, Jul 28, 2021 at 02:56:31PM -0700, Kees Cook wrote:

> > > On Wed, Jul 28, 2021 at 11:42:15AM +0200, David Sterba wrote:

> > > > On Tue, Jul 27, 2021 at 01:58:38PM -0700, Kees Cook wrote:

> > > > >  	}

> > > > >  	if (need_reset) {

> > > > > -		memset(&item->generation_v2, 0,

> > > > > -			sizeof(*item) - offsetof(struct btrfs_root_item,

> > > > > -					generation_v2));

> > > > > -

> > > > 

> > > > Please add

> > > > 		/* Clear all members from generation_v2 onwards */

> > > > 

> > > > > +		memset_after(item, 0, level);

> > > 

> > > Perhaps there should be another helper memset_starting()? That would

> > > make these cases a bit more self-documenting.

> > 

> > That would be better, yes.

> > 

> > > +		memset_starting(item, 0, generation_v2);

> > 

> > memset_from?

> 

> For v2, I bikeshed this to "memset_startat" since "from" is semantically

> close to "source" which I thought might be confusing. (I, too, did not

> like "starting".) :)


memset_startat works for me, thanks.
diff mbox series

Patch

diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
index 702dc5441f03..ec9e78f65fca 100644
--- a/fs/btrfs/root-tree.c
+++ b/fs/btrfs/root-tree.c
@@ -39,10 +39,7 @@  static void btrfs_read_root_item(struct extent_buffer *eb, int slot,
 		need_reset = 1;
 	}
 	if (need_reset) {
-		memset(&item->generation_v2, 0,
-			sizeof(*item) - offsetof(struct btrfs_root_item,
-					generation_v2));
-
+		memset_after(item, 0, level);
 		generate_random_guid(item->uuid);
 	}
 }