Skip to contentSkip to conversation

slirp: Fix error reported by static code analysis and remove wrong type casts

PATCH In review Stefan Weil 3 participants 4 messages Sep 3, 2012 – Sep 4, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 slirp: Fix error reported by static code analysis and remove wrong type casts

Report from smatch:
slirp/tcp_subr.c:127 tcp_respond(17) error:
we previously assumed 'tp' could be null (see line 124)

Fix this by checking 'tp' before reading its elements.

The type casts of pointers to long are not related to the smatch report but happened to be near that code. Those type casts are not allowed when sizeof(pointer) != sizeof(long).

---

Coding style was not fixed by the patch!

 slirp/tcp_subr.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
slirp/tcp_subr.c +4 -4
index 025b374..5f3214c 100644
@@ -114,9 +114,9 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
 	int win = 0;
 
 	DEBUG_CALL("tcp_respond");
-	DEBUG_ARG("tp = %lx", (long)tp);
-	DEBUG_ARG("ti = %lx", (long)ti);
-	DEBUG_ARG("m = %lx", (long)m);
+	DEBUG_ARG("tp = %p", tp);
+	DEBUG_ARG("ti = %p", ti);
+	DEBUG_ARG("m = %p", m);
 	DEBUG_ARG("ack = %u", ack);
 	DEBUG_ARG("seq = %u", seq);
 	DEBUG_ARG("flags = %x", flags);
@@ -124,7 +124,7 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
 	if (tp)
 		win = sbspace(&tp->t_socket->so_rcv);
         if (m == NULL) {
-		if ((m = m_get(tp->t_socket->slirp)) == NULL)
+		if (tp && (m = m_get(tp->t_socket->slirp)) == NULL)
 			return;
 		tlen = 0;
 		m->m_data += IF_MAXLINKHDR;
Signature · 1 line
1.7.10
#1 lore raw
Quoted 9 lines · Stefan Weil

Report from smatch:
slirp/tcp_subr.c:127 tcp_respond(17) error:
we previously assumed 'tp' could be null (see line 124)

Fix this by checking 'tp' before reading its elements.

The type casts of pointers to long are not related to the smatch report but happened to be near that code. Those type casts are not allowed when sizeof(pointer) != sizeof(long).

I think these would be better in a separate patch.

slirp/tcp_subr.c tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m, · line 130

Quoted 9 lines
slirp/tcp_subr.c +1 -1
@@ -124,7 +124,7 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
        if (tp)
                win = sbspace(&tp->t_socket->so_rcv);
         if (m == NULL) {
-               if ((m = m_get(tp->t_socket->slirp)) == NULL)
+               if (tp && (m = m_get(tp->t_socket->slirp)) == NULL)
                        return;
                tlen = 0;
                m->m_data += IF_MAXLINKHDR;

This doesn't look quite right -- now if tp is NULL we will skip the assignment to m and dereference a NULL pointer a few lines further on, right?

I suspect that we need either to be passed our Slirp* explicitly rather than via tp, or we have to enforce that tcp_respond() is called with a non-NULL struct tcpcb*. I think the only cases where tp can be non-NULL at the moment are the two calls from the dropwithreset code in tcp_input().

-- PMM

#2 lore raw
Quoted 12 lines · Peter Maydell
Quoted 9 lines · Stefan Weil

Report from smatch:
slirp/tcp_subr.c:127 tcp_respond(17) error:
we previously assumed 'tp' could be null (see line 124)

Fix this by checking 'tp' before reading its elements.

The type casts of pointers to long are not related to the smatch report but happened to be near that code. Those type casts are not allowed when sizeof(pointer) != sizeof(long).

I think these would be better in a separate patch.

Quoted 13 lines

slirp/tcp_subr.c tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m, · line 130

Quoted 9 lines
slirp/tcp_subr.c +1 -1
@@ -124,7 +124,7 @@ tcp_respond(struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m,
        if (tp)
                win = sbspace(&tp->t_socket->so_rcv);
         if (m == NULL) {
-               if ((m = m_get(tp->t_socket->slirp)) == NULL)
+               if (tp && (m = m_get(tp->t_socket->slirp)) == NULL)
                        return;
                tlen = 0;
                m->m_data += IF_MAXLINKHDR;

This doesn't look quite right -- now if tp is NULL we will skip the assignment to m and dereference a NULL pointer a few lines further on, right?

This would be correct:

if (!tp || ..)
return

Quoted 5 lines

I suspect that we need either to be passed our Slirp* explicitly rather than via tp, or we have to enforce that tcp_respond() is called with a non-NULL struct tcpcb*. I think the only cases where tp can be non-NULL at the moment are the two calls from the dropwithreset code in tcp_input().

Indeed, this is a "XXX Should never fail" case - according to the code that checks tp at the call site. But as no one seriously understands slirp details, we are better safe than sorry.

Jan

#3 lore raw
Quoted 3 lines · Jan Kiszka

Indeed, this is a "XXX Should never fail" case - according to the code that checks tp at the call site. But as no one seriously understands slirp details, we are better safe than sorry.

Actually it looks like you can get here with both tp and m NULL:

* we set up a non-blocking connect
* at some point slirp.c:slirp_select_poll() finds the fd is writable
* ...but the send() on slirp.c line 504 fails, so we set SS_NOFDREF in the so->so_state
* on line 520 we call tcp_input() with m == NULL
* tcp_input checks for m == NULL and immediately goes to cont_conn
* if so->so_state & SS_NOFDREF we call tcp_close, which frees tp and returns 0
* so we goto dropwithreset with tp NULL and m NULL
* where we call tcp_respond to try to send a RST or RST|ACK

So I think you have to have failed a syscall for this to happen, and if we haven't got an outbound fd then there's not a lot we can do, so just returning from tcp_respond() seems like the best thing.

-- PMM