Skip to contentSkip to conversation

libxfs: don't write uninitialized heap contents into new directory blocks

PATCH Superseded Darrick J. Wong 3 participants 6 messages Mar 18, 2015 – Mar 19, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxfs: don't write uninitialized heap contents into new directory blocks

When we're initializing a new directory block, zero the buffer contents to avoid writing random heap contents (and crc thereof) to disk. This eliminates a few valgrind complaints in xfs_repair.

---
 libxfs/xfs_da_btree.c  |    1 +
 libxfs/xfs_dir2_data.c |    1 +
 libxfs/xfs_dir2_leaf.c |    1 +
 libxfs/xfs_dir2_node.c |    2 +-
 4 files changed, 4 insertions(+), 1 deletion(-)
libxfs/xfs_da_btree.c +1 -0
index b731b54..bc73cbc 100644
@@ -562,6 +562,7 @@ xfs_da3_root_split(
 	if (error)
 		return error;
 	node = bp->b_addr;
+	memset(bp->b_addr, 0, bp->b_bcount);
 	oldroot = blk1->bp->b_addr;
 	if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
 	    oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
libxfs/xfs_dir2_data.c +1 -0
index dc9df4d..79c2fc2 100644
@@ -559,6 +559,7 @@ xfs_dir3_data_init(
 		XFS_DATA_FORK);
 	if (error)
 		return error;
+	memset(bp->b_addr, 0, bp->b_bcount);
 	bp->b_ops = &xfs_dir3_data_buf_ops;
 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
 
libxfs/xfs_dir2_leaf.c +1 -0
index 8e0cbc9..6eb03fc 100644
@@ -382,6 +382,7 @@ xfs_dir3_leaf_get_buf(
 	if (error)
 		return error;
 
+	memset(bp->b_addr, 0, bp->b_bcount);
 	xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
 	xfs_dir3_leaf_log_header(tp, bp);
 	if (magic == XFS_DIR2_LEAF1_MAGIC)
libxfs/xfs_dir2_node.c +1 -1
index 3737e4e..3e2f04d 100644
@@ -247,7 +247,7 @@ xfs_dir3_free_get_buf(
 	 * Initialize the new block to be empty, and remember
 	 * its first slot as our empty slot.
 	 */
-	memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
+	memset(bp->b_addr, 0, bp->b_bcount);
 	memset(&hdr, 0, sizeof(hdr));
 
 	if (xfs_sb_version_hascrc(&mp->m_sb)) {

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Darrick J. Wong

When we're initializing a new directory block, zero the buffer contents to avoid writing random heap contents (and crc thereof) to disk. This eliminates a few valgrind complaints in xfs_repair.

---

Well it seems Ok to me:

... but I do notice that we start ending up with multiple memset() calls throughout these codepaths. E.g., xfs_dir3_data_init() memsets the target bytes for the header if crc is enabled, xfs_dir3_leaf_get_buf() calls xfs_dir3_leaf_init() which does something similar, etc.

I wonder if we should just zero the buffer content unconditionally on allocation?

Brian

Quoted 56 lines
 libxfs/xfs_da_btree.c  |    1 +
 libxfs/xfs_dir2_data.c |    1 +
 libxfs/xfs_dir2_leaf.c |    1 +
 libxfs/xfs_dir2_node.c |    2 +-
 4 files changed, 4 insertions(+), 1 deletion(-)
libxfs/xfs_da_btree.c +1 -0
index b731b54..bc73cbc 100644
@@ -562,6 +562,7 @@ xfs_da3_root_split(
 	if (error)
 		return error;
 	node = bp->b_addr;
+	memset(bp->b_addr, 0, bp->b_bcount);
 	oldroot = blk1->bp->b_addr;
 	if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
 	    oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
libxfs/xfs_dir2_data.c +1 -0
index dc9df4d..79c2fc2 100644
@@ -559,6 +559,7 @@ xfs_dir3_data_init(
 		XFS_DATA_FORK);
 	if (error)
 		return error;
+	memset(bp->b_addr, 0, bp->b_bcount);
 	bp->b_ops = &xfs_dir3_data_buf_ops;
 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
 
libxfs/xfs_dir2_leaf.c +1 -0
index 8e0cbc9..6eb03fc 100644
@@ -382,6 +382,7 @@ xfs_dir3_leaf_get_buf(
 	if (error)
 		return error;
 
+	memset(bp->b_addr, 0, bp->b_bcount);
 	xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
 	xfs_dir3_leaf_log_header(tp, bp);
 	if (magic == XFS_DIR2_LEAF1_MAGIC)
libxfs/xfs_dir2_node.c +1 -1
index 3737e4e..3e2f04d 100644
@@ -247,7 +247,7 @@ xfs_dir3_free_get_buf(
 	 * Initialize the new block to be empty, and remember
 	 * its first slot as our empty slot.
 	 */
-	memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
+	memset(bp->b_addr, 0, bp->b_bcount);
 	memset(&hdr, 0, sizeof(hdr));
 
 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
Quoted 4 lines

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#2 lore raw
Quoted 12 lines · Brian Foster
Quoted 6 lines · Darrick J. Wong

When we're initializing a new directory block, zero the buffer contents to avoid writing random heap contents (and crc thereof) to disk. This eliminates a few valgrind complaints in xfs_repair.

---

Well it seems Ok to me:

Quoted 7 lines

... but I do notice that we start ending up with multiple memset() calls throughout these codepaths. E.g., xfs_dir3_data_init() memsets the target bytes for the header if crc is enabled, xfs_dir3_leaf_get_buf() calls xfs_dir3_leaf_init() which does something similar, etc.

I wonder if we should just zero the buffer content unconditionally on allocation?

I thought about that too -- certainly we could zero b_addr as soon as it's allocated in __initbuf, which would shut up Valgrind. I think this is what Dave was getting at when he suggested __libxfs_getbufr(), even though that function only frees b_addr if it touches it at all. On the other hand, the next thing could happen is that the buffer is read in from disk, which makes the zeroing unnecessary. The readbuf functions call the getbuf functions, which makes it difficult to tell from the getbuf functions what's going to happen next.

However, I think there's a second problem: what if the xfsbuf is reused without freeing the b_addr? This seems possible if we read in the block, detach it from whatever points to it, and then we want to allocate it to a directory. The buffer's still in memory and it's still the right size, so we don't free b_addr; the init function doesn't overwrite the whole buffer, and now we've just leaked old disk contents. Adding a memset to getbuf would fix this, but again at a cost of unnecessary zeroing.

Sprinkling memsets around the init code is a fair amount of work, but it solves the previous problem without the unnecessary memsets.

--D

Quoted 59 lines

Brian

Quoted 56 lines
 libxfs/xfs_da_btree.c  |    1 +
 libxfs/xfs_dir2_data.c |    1 +
 libxfs/xfs_dir2_leaf.c |    1 +
 libxfs/xfs_dir2_node.c |    2 +-
 4 files changed, 4 insertions(+), 1 deletion(-)
libxfs/xfs_da_btree.c +1 -0
index b731b54..bc73cbc 100644
@@ -562,6 +562,7 @@ xfs_da3_root_split(
 	if (error)
 		return error;
 	node = bp->b_addr;
+	memset(bp->b_addr, 0, bp->b_bcount);
 	oldroot = blk1->bp->b_addr;
 	if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
 	    oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
libxfs/xfs_dir2_data.c +1 -0
index dc9df4d..79c2fc2 100644
@@ -559,6 +559,7 @@ xfs_dir3_data_init(
 		XFS_DATA_FORK);
 	if (error)
 		return error;
+	memset(bp->b_addr, 0, bp->b_bcount);
 	bp->b_ops = &xfs_dir3_data_buf_ops;
 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
 
libxfs/xfs_dir2_leaf.c +1 -0
index 8e0cbc9..6eb03fc 100644
@@ -382,6 +382,7 @@ xfs_dir3_leaf_get_buf(
 	if (error)
 		return error;
 
+	memset(bp->b_addr, 0, bp->b_bcount);
 	xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
 	xfs_dir3_leaf_log_header(tp, bp);
 	if (magic == XFS_DIR2_LEAF1_MAGIC)
libxfs/xfs_dir2_node.c +1 -1
index 3737e4e..3e2f04d 100644
@@ -247,7 +247,7 @@ xfs_dir3_free_get_buf(
 	 * Initialize the new block to be empty, and remember
 	 * its first slot as our empty slot.
 	 */
-	memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
+	memset(bp->b_addr, 0, bp->b_bcount);
 	memset(&hdr, 0, sizeof(hdr));
 
 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
Quoted 9 lines
Quoted 4 lines

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#3 lore raw
Quoted 18 lines · Darrick J. Wong
Quoted 17 lines · Brian Foster
Quoted 6 lines · Darrick J. Wong

When we're initializing a new directory block, zero the buffer contents to avoid writing random heap contents (and crc thereof) to disk. This eliminates a few valgrind complaints in xfs_repair.

---

Well it seems Ok to me:

... but I do notice that we start ending up with multiple memset() calls throughout these codepaths. E.g., xfs_dir3_data_init() memsets the target bytes for the header if crc is enabled, xfs_dir3_leaf_get_buf() calls xfs_dir3_leaf_init() which does something similar, etc.

Quoted 7 lines
Quoted 2 lines

I wonder if we should just zero the buffer content unconditionally on allocation?

I thought about that too -- certainly we could zero b_addr as soon as it's allocated in __initbuf, which would shut up Valgrind. I think this is what Dave was getting at when he suggested __libxfs_getbufr(), even though that function only frees b_addr if it touches it at all.

It frees b_addr only if the buffer found on the free list does not match the length of the new buffer. Otherwise it just reuses it directly. It probably doesn't matter if zeroing is done in __initbuf, it is always called after __libxfs_getbufr() returns a buffer handle, anyway.

Quoted 8 lines

On the other hand, the
next thing could happen is that the buffer is read in from disk, which makes the zeroing unnecessary. The readbuf functions call the getbuf functions, which makes it difficult to tell from the getbuf functions what's going to happen next.

However, I think there's a second problem: what if the xfsbuf is reused without freeing the b_addr?

Yup, that's what __libxfs_getbufr() does ;)

Quoted 6 lines

This seems possible if we read in the block, detach it
from whatever points to it, and then we want to allocate it to a directory. The buffer's still in memory and it's still the right size, so we don't free b_addr; the init function doesn't overwrite the whole buffer, and now we've just leaked old disk contents. Adding a memset to getbuf would fix this, but again at a cost of unnecessary zeroing.

Leaked the contents to whom?

In the kernel we don't care about stale contents in metadata buffers as the contents cannot be directly read from userspace. i.e. there is no vector for information leakage (other than through the root user reading the bdev directly), so we don't need to zero buffers to prevent information leakage either in userspace or the kernel...

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#4 lore raw
Quoted 27 lines · Dave Chinner
Quoted 18 lines · Darrick J. Wong
Quoted 17 lines · Brian Foster
Quoted 6 lines · Darrick J. Wong

When we're initializing a new directory block, zero the buffer contents to avoid writing random heap contents (and crc thereof) to disk. This eliminates a few valgrind complaints in xfs_repair.

---

Well it seems Ok to me:

... but I do notice that we start ending up with multiple memset() calls throughout these codepaths. E.g., xfs_dir3_data_init() memsets the target bytes for the header if crc is enabled, xfs_dir3_leaf_get_buf() calls xfs_dir3_leaf_init() which does something similar, etc.

Quoted 7 lines
Quoted 2 lines

I wonder if we should just zero the buffer content unconditionally on allocation?

I thought about that too -- certainly we could zero b_addr as soon as it's allocated in __initbuf, which would shut up Valgrind. I think this is what Dave was getting at when he suggested __libxfs_getbufr(), even though that function only frees b_addr if it touches it at all.

Quoted 5 lines

It frees b_addr only if the buffer found on the free list does not match the length of the new buffer. Otherwise it just reuses it directly. It probably doesn't matter if zeroing is done in __initbuf, it is always called after __libxfs_getbufr() returns a buffer handle, anyway.

Right. I'll do it there, since __initbuf exists only outside of the kernel.

Quoted 18 lines
Quoted 8 lines

On the other hand, the
next thing could happen is that the buffer is read in from disk, which makes the zeroing unnecessary. The readbuf functions call the getbuf functions, which makes it difficult to tell from the getbuf functions what's going to happen next.

However, I think there's a second problem: what if the xfsbuf is reused without freeing the b_addr?

Yup, that's what __libxfs_getbufr() does ;)

Quoted 6 lines

This seems possible if we read in the block, detach it
from whatever points to it, and then we want to allocate it to a directory. The buffer's still in memory and it's still the right size, so we don't free b_addr; the init function doesn't overwrite the whole buffer, and now we've just leaked old disk contents. Adding a memset to getbuf would fix this, but again at a cost of unnecessary zeroing.

Quoted 7 lines

Leaked the contents to whom?

In the kernel we don't care about stale contents in metadata buffers as the contents cannot be directly read from userspace. i.e. there is no vector for information leakage (other than through the root user reading the bdev directly), so we don't need to zero buffers to prevent information leakage either in userspace or the kernel...

Ok. It was a mostly theoretical worry -- a corrupt FS thinks a data block contains metadata; repair frees the block but then reallocates it to a dir or something, and then a second file gets pointed to the dir block. Now the second file can read the old file's contents, even after we supposedly reallocated it to something else.

OTOH it's probably hard to make that happen.

--D

Quoted 7 lines

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#5 lore raw
Quoted 19 lines · Darrick J. Wong
Quoted 9 lines · Dave Chinner
Quoted 7 lines · Darrick J. Wong

This seems possible if we read in the block, detach it from whatever points to it, and then we want to allocate it to a directory. The buffer's still in memory and it's still the right size, so we don't free b_addr; the init function doesn't overwrite the whole buffer, and now we've just leaked old disk contents. Adding a memset to getbuf would fix this, but again at a cost of unnecessary zeroing.

Quoted 8 lines

Leaked the contents to whom?

In the kernel we don't care about stale contents in metadata buffers as the contents cannot be directly read from userspace. i.e. there is no vector for information leakage (other than through the root user reading the bdev directly), so we don't need to zero buffers to prevent information leakage either in userspace or the kernel...

Quoted 6 lines

Ok. It was a mostly theoretical worry -- a corrupt FS thinks a data block contains metadata; repair frees the block but then reallocates it to a dir or something, and then a second file gets pointed to the dir block. Now the second file can read the old file's contents, even after we supposedly reallocated it to something else.

We don't rebuild directories until we've already resolved all the used/free block references in the filesystem (phase 3/4) and then rebuilt the free space trees (phase 5). Hence by the end of phase 5 we do not have any doubly referenced blocks, nor do we have any dangling references in inodes to free space, and we have a fully correct free space index written to disk.

We then rebuild directories (phase 6) using the corrected free space btrees if we need to allocate new blocks and hence we can't get doubly linked blocks in the manner you suggest unless we've broken repair rather badly...

Quoted 1 line

OTOH it's probably hard to make that happen.

*nod*

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs