Skip to contentSkip to conversation

xen/hypfs: check the return value of snprintf to avoid leaking stack accidently

PATCH Superseded Xenia Ragiadakou 3 participants 6 messages Aug 4, 2022

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen/hypfs: check the return value of snprintf to avoid leaking stack accidently

The function snprintf() returns the number of characters that would have been written in the buffer if the buffer size had been sufficiently large, not counting the terminating null character.
Hence, the value returned is not guaranteed to be smaller than the buffer size. Check the return value of snprintf to prevent leaking stack contents to the guest by accident.

Also, for debug builds, add an assertion to ensure that the assumption made on the size of the destination buffer still holds.

---

Changes in v2:
- add ASSERT_UNREACHABLE()
- update commit message accordingly

 xen/common/hypfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
xen/common/hypfs.c +3 -1
index 66026ad3e0..7b3377d46e 100644
@@ -377,8 +377,10 @@ int hypfs_read_dyndir_id_entry(const struct hypfs_entry_dir *template,
     unsigned int e_namelen, e_len;
 
     e_namelen = snprintf(name, sizeof(name), template->e.name, id);
-    if ( e_namelen >= sizeof(name) )
+    if ( e_namelen >= sizeof(name) ) {
+        ASSERT_UNREACHABLE();
         return -ENOBUFS;
+    }
     e_len = DIRENTRY_SIZE(e_namelen);
     direntry.e.pad = 0;
     direntry.e.type = template->e.type;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 2 lines · Xenia Ragiadakou

Changes in v2:
- add ASSERT_UNREACHABLE()

Hmm, this ...

xen/common/hypfs.c int hypfs_read_dyndir_id_entry(const struct hypfs_entry_dir *template, · line 383

Quoted 11 lines
xen/common/hypfs.c +3 -1
@@ -377,8 +377,10 @@ int hypfs_read_dyndir_id_entry(const struct hypfs_entry_dir *template,
     unsigned int e_namelen, e_len;
 
     e_namelen = snprintf(name, sizeof(name), template->e.name, id);
-    if ( e_namelen >= sizeof(name) )
+    if ( e_namelen >= sizeof(name) ) {
+        ASSERT_UNREACHABLE();
         return -ENOBUFS;
+    }

... looks to be an incremental patch on top of v1, not v2 of that patch?

Also please correct the placement of the opening brace.

Jan

#2 lore raw

Hi Jan,

Quoted 11 lines · Jan Beulich
Quoted 2 lines · Xenia Ragiadakou

Changes in v2:
- add ASSERT_UNREACHABLE()

Hmm, this ...

Quoted 5 lines
xen/common/hypfs.c +0 -0
@@ -377,8 +377,10 @@ int hypfs_read_dyndir_id_entry(const struct hypfs_entry_dir *template,
      unsigned int e_namelen, e_len;
 
Quoted 9 lines

xen/common/hypfs.c int hypfs_read_dyndir_id_entry(const struct hypfs_entry_dir *template, · line 383

Quoted 6 lines
xen/common/hypfs.c +3 -1
      e_namelen = snprintf(name, sizeof(name), template->e.name, id);
-    if ( e_namelen >= sizeof(name) )
+    if ( e_namelen >= sizeof(name) ) {
+        ASSERT_UNREACHABLE();
          return -ENOBUFS;
+    }

... looks to be an incremental patch on top of v1, not v2 of that patch?

So, here, IIUC, I have to create a patch series and add the assert in the second (2/2) patch? What should be the version number of the series?

Quoted 1 line

Also please correct the placement of the opening brace.

Ah, ok. Sorry.

Signature · 1 line
Xenia
#3 lore raw
Quoted 22 lines · Xenia Ragiadakou
Quoted 11 lines · Jan Beulich
Quoted 2 lines · Xenia Ragiadakou

Changes in v2:
- add ASSERT_UNREACHABLE()

Hmm, this ...

Quoted 5 lines
xen/common/hypfs.c +0 -0
@@ -377,8 +377,10 @@ int hypfs_read_dyndir_id_entry(const struct hypfs_entry_dir *template,
      unsigned int e_namelen, e_len;
 
Quoted 9 lines

xen/common/hypfs.c int hypfs_read_dyndir_id_entry(const struct hypfs_entry_dir *template, · line 383

Quoted 6 lines
xen/common/hypfs.c +3 -1
      e_namelen = snprintf(name, sizeof(name), template->e.name, id);
-    if ( e_namelen >= sizeof(name) )
+    if ( e_namelen >= sizeof(name) ) {
+        ASSERT_UNREACHABLE();
          return -ENOBUFS;
+    }

... looks to be an incremental patch on top of v1, not v2 of that patch?

Quoted 2 lines

So, here, IIUC, I have to create a patch series and add the assert in the second (2/2) patch? What should be the version number of the series?

No, why? Simply fold this change into the earlier one, and call the result v3.

Jan

#4 lore raw
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Xenia Ragiadakou
Quoted 20 lines · Jan Beulich
Quoted 2 lines · Xenia Ragiadakou

Changes in v2:
- add ASSERT_UNREACHABLE()

Hmm, this ...

xen/common/hypfs.c int hypfs_read_dyndir_id_entry(const struct hypfs_entry_dir *template, · line 383

Quoted 11 lines
xen/common/hypfs.c +3 -1
@@ -377,8 +377,10 @@ int hypfs_read_dyndir_id_entry(const struct hypfs_entry_dir *template,
       unsigned int e_namelen, e_len;
 
       e_namelen = snprintf(name, sizeof(name), template->e.name, id);
-    if ( e_namelen >= sizeof(name) )
+    if ( e_namelen >= sizeof(name) ) {
+        ASSERT_UNREACHABLE();
           return -ENOBUFS;
+    }

... looks to be an incremental patch on top of v1, not v2 of that patch?

Quoted 5 lines
Quoted 2 lines

So, here, IIUC, I have to create a patch series and add the assert in the second (2/2) patch? What should be the version number of the series?

No, why? Simply fold this change into the earlier one, and call the result v3.

Okkk, I just realized what I have done.

Signature · 1 line
Xenia
Reviewed-by on v2 1/1
#5 lore raw
In reply to Xenia Ragiadakou
Quoted 11 lines · Xenia Ragiadakou

The function snprintf() returns the number of characters that would have been written in the buffer if the buffer size had been sufficiently large, not counting the terminating null character.
Hence, the value returned is not guaranteed to be smaller than the buffer size. Check the return value of snprintf to prevent leaking stack contents to the guest by accident.

Also, for debug builds, add an assertion to ensure that the assumption made on the size of the destination buffer still holds.

Juergen