Skip to contentSkip to conversation

correct size computation in m_cat

PATCH Applied P J P 3 participants 8 messages Jun 5, 2018 – Jun 6, 2018

lore
Share & tools
lore

P J P

author
#0 lore raw

cover correct size computation in m_cat

From: Prasad J Pandit <[email protected]>

Hello,

While reassembling incoming fragmented datagrams, 'm_cat' routine extends the 'mbuf' buffer if it has insufficient room. It computes a wrong buffer size, which leads to overwriting adjacent heap buffer area.

This patch set fixes this issue and formats m_cat() routine as per coding style guide.

Thank you.
--
Prasad J Pandit (2):
slirp: correct size computation while concatenating mbuf
slirp: reformat m_cat routine

 slirp/mbuf.c | 38 +++++++++++++++++---------------------
 1 file changed, 17 insertions(+), 21 deletions(-)

--
2.17.1

author
#1 lore raw

PATCH 1/2 slirp: correct size computation while concatenating mbuf

From: Prasad J Pandit <[email protected]>

While reassembling incoming fragmented datagrams, 'm_cat' routine extends the 'mbuf' buffer, if it has insufficient room. It computes a wrong buffer size, which leads to overwriting adjacent heap buffer area. Correct this size computation in m_cat.

---
 slirp/mbuf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
slirp/mbuf.c +4 -4
index 5ff24559fd..0f45abd917 100644
@@ -138,7 +138,7 @@ m_cat(struct mbuf *m, struct mbuf *n)
 	 * If there's no room, realloc
 	 */
 	if (M_FREEROOM(m) < n->m_len)
-		m_inc(m,m->m_size+MINCSIZE);
+		m_inc(m, m->m_len + n->m_len);
 
 	memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
 	m->m_len += n->m_len;
@@ -158,12 +158,12 @@ m_inc(struct mbuf *m, int size)
 
         if (m->m_flags & M_EXT) {
 	  datasize = m->m_data - m->m_ext;
-          m->m_ext = g_realloc(m->m_ext, size);
+          m->m_ext = g_realloc(m->m_ext, size + datasize);
 	  m->m_data = m->m_ext + datasize;
         } else {
 	  char *dat;
 	  datasize = m->m_data - m->m_dat;
-          dat = g_malloc(size);
+      dat = g_malloc(size + datasize);
 	  memcpy(dat, m->m_dat, m->m_size);
 
 	  m->m_ext = dat;
@@ -171,7 +171,7 @@ m_inc(struct mbuf *m, int size)
 	  m->m_flags |= M_EXT;
         }
 
-        m->m_size = size;
+        m->m_size = size + datasize;
 
 }
 
Signature · 1 line
2.17.1
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] slirp: correct size computation while concatenating mbuf

Hello,

P J P, le mar. 05 juin 2018 23:38:35 +0530, a ecrit:

Quoted 9 lines

From: Prasad J Pandit <[email protected]>

While reassembling incoming fragmented datagrams, 'm_cat' routine extends the 'mbuf' buffer, if it has insufficient room. It computes a wrong buffer size, which leads to overwriting adjacent heap buffer area. Correct this size computation in m_cat.

Applied to my tree with a couple documentation improvements, thanks!

Samuel

author
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] slirp: correct size computation while concatenating mbuf

Hello Samuel,

+-- On Wed, 6 Jun 2018, Samuel Thibault wrote --+
| > From: Prasad J Pandit <[email protected]>
| > 
| > While reassembling incoming fragmented datagrams, 'm_cat' routine
| > extends the 'mbuf' buffer, if it has insufficient room. It computes
| > a wrong buffer size, which leads to overwriting adjacent heap buffer
| > area. Correct this size computation in m_cat.
| > 
| > Reported-by: ZDI Disclosures <[email protected]>
| > Signed-off-by: Prasad J Pandit <[email protected]>
| 
| Applied to my tree with a couple documentation improvements, thanks!

-> https://lists.gnu.org/archive/html/qemu-devel/2018-06/msg01144.html

This is patch v1 with indentation fix flagged by checkpatch.pl. In case you prefer this one.

Thank you.
--
- P J P
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

author
#4 lore raw

PATCH 2/2 slirp: reformat m_cat routine

In reply to P J P

From: Prasad J Pandit <[email protected]>

Coding style changes to the m_cat routine and minor refactoring.

---
 slirp/mbuf.c | 36 ++++++++++++++++--------------------
 1 file changed, 16 insertions(+), 20 deletions(-)
slirp/mbuf.c +16 -20
index 0f45abd917..30e3b2719d 100644
@@ -151,32 +151,28 @@ m_cat(struct mbuf *m, struct mbuf *n)
 void
 m_inc(struct mbuf *m, int size)
 {
-	int datasize;
+    int datasize;
 
-	/* some compiles throw up on gotos.  This one we can fake. */
-        if(m->m_size>size) return;
+    /* some compiles throw up on gotos. This one we can fake. */
+    if (m->m_size > size) {
+        return;
+    }
 
-        if (m->m_flags & M_EXT) {
-	  datasize = m->m_data - m->m_ext;
-          m->m_ext = g_realloc(m->m_ext, size + datasize);
-	  m->m_data = m->m_ext + datasize;
-        } else {
-	  char *dat;
-	  datasize = m->m_data - m->m_dat;
-      dat = g_malloc(size + datasize);
-	  memcpy(dat, m->m_dat, m->m_size);
-
-	  m->m_ext = dat;
-	  m->m_data = m->m_ext + datasize;
-	  m->m_flags |= M_EXT;
-        }
-
-        m->m_size = size + datasize;
+    if (m->m_flags & M_EXT) {
+        datasize = m->m_data - m->m_ext;
+        m->m_ext = g_realloc(m->m_ext, size + datasize);
+    } else {
+        datasize = m->m_data - m->m_dat;
+        m->m_ext = g_malloc(size + datasize);
+        memcpy(m->m_ext, m->m_dat, m->m_size);
+        m->m_flags |= M_EXT;
+    }
 
+    m->m_data = m->m_ext + datasize;
+    m->m_size = size + datasize;
 }
 
 
-
 void
 m_adj(struct mbuf *m, int len)
 {
Signature · 1 line
2.17.1
#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] slirp: reformat m_cat routine

P J P, le mar. 05 juin 2018 23:38:36 +0530, a ecrit:

Quoted 3 lines

From: Prasad J Pandit <[email protected]>

Coding style changes to the m_cat routine and minor refactoring.

Fixed a bit and applied to my tree, thanks!

Samuel

#6 lore raw
In reply to P J P
This series seems to have some coding style problems. See output below for more information:

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [PATCH 0/2] correct size computation in m_cat

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram

commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do

    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))

done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 Switched to a new branch 'test'
769059abb8 slirp: reformat m_cat routine
9f938dc6a6 slirp: correct size computation while concatenating mbuf

=== OUTPUT BEGIN ===
Checking PATCH 1/2: slirp: correct size computation while concatenating mbuf...
ERROR: code indent should never use tabs
#24: FILE: slirp/mbuf.c:141:
+^I^Im_inc(m, m->m_len + n->m_len);$

total: 1 errors, 0 warnings, 30 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Checking PATCH 2/2: slirp: reformat m_cat routine... === OUTPUT END ===

Test command exited with code: 1

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]

#7 lore raw
In reply to P J P
This series failed build test on s390x host. Please find the details below.

Hi,

This series failed build test on s390x host. Please find the details below.

Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [PATCH 0/2] correct size computation in m_cat

=== TEST SCRIPT BEGIN ===
#!/bin/bash
# Testing script will be invoked under the git checkout with # HEAD pointing to a commit that has the patches applied on top of "base"

# branch
set -e
echo "=== ENV ==="
env
echo "=== PACKAGES ==="
rpm -qa
echo "=== TEST BEGIN ==="
CC=$HOME/bin/cc
INSTALL=$PWD/install
BUILD=$PWD/build
echo -n "Using CC: "
realpath $CC
mkdir -p $BUILD $INSTALL
SRC=$PWD
cd $BUILD
$SRC/configure --cc=$CC --prefix=$INSTALL
make -j4
# XXX: we need reliable clean up
# make check -j4 V=1
make install
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
fatal: unable to access 'https://github.com/patchew-project/qemu/': Encountered end of file
error: Could not fetch 3c8cf5a9c21ff8782164d1def7f44bd888713384
Traceback (most recent call last):
File "/home/fam/patchew/patchew-cli", line 409, in test_one

    git_clone_repo(clone, r["repo"], r["head"], logf)

File "/home/fam/patchew/patchew-cli", line 48, in git_clone_repo
stdout=logf, stderr=logf)
File "/usr/lib64/python3.6/subprocess.py", line 291, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['git', 'remote', 'add', '-f', '--mirror=fetch', '3c8cf5a9c21ff8782164d1def7f44bd888713384', 'https://github.com/patchew-project/qemu']' returned non-zero exit status 1.

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]