Skip to content

Commit

Permalink
sctp: fix -ENOMEM result with invalid user space pointer in sendto() …
Browse files Browse the repository at this point in the history
…syscall

Consider the following program, that sets the second argument to the
sendto() syscall incorrectly:

 #include <string.h>
 #include <arpa/inet.h>
 #include <sys/socket.h>

 int main(void)
 {
         int fd;
         struct sockaddr_in sa;

         fd = socket(AF_INET, SOCK_STREAM, 132 /*IPPROTO_SCTP*/);
         if (fd < 0)
                 return 1;

         memset(&sa, 0, sizeof(sa));
         sa.sin_family = AF_INET;
         sa.sin_addr.s_addr = inet_addr("127.0.0.1");
         sa.sin_port = htons(11111);

         sendto(fd, NULL, 1, 0, (struct sockaddr *)&sa, sizeof(sa));

         return 0;
 }

We get -ENOMEM:

 $ strace -e sendto ./demo
 sendto(3, NULL, 1, 0, {sa_family=AF_INET, sin_port=htons(11111), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 ENOMEM (Cannot allocate memory)

Propagate the error code from sctp_user_addto_chunk(), so that we will
tell user space what actually went wrong:

 $ strace -e sendto ./demo
 sendto(3, NULL, 1, 0, {sa_family=AF_INET, sin_port=htons(11111), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EFAULT (Bad address)

Noticed while running Trinity (the syscall fuzzer).

Signed-off-by: Tommi Rantala <[email protected]>
Acked-by: Vlad Yasevich <[email protected]>
Acked-by: Neil Horman <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
rantala authored and davem330 committed Nov 28, 2012
1 parent be364c8 commit 6e51fe7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 9 additions & 4 deletions net/sctp/chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,

msg = sctp_datamsg_new(GFP_KERNEL);
if (!msg)
return NULL;
return ERR_PTR(-ENOMEM);

/* Note: Calculate this outside of the loop, so that all fragments
* have the same expiration.
Expand Down Expand Up @@ -280,8 +280,11 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,

chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag, 0);

if (!chunk)
if (!chunk) {
err = -ENOMEM;
goto errout;
}

err = sctp_user_addto_chunk(chunk, offset, len, msgh->msg_iov);
if (err < 0)
goto errout_chunk_free;
Expand Down Expand Up @@ -315,8 +318,10 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,

chunk = sctp_make_datafrag_empty(asoc, sinfo, over, frag, 0);

if (!chunk)
if (!chunk) {
err = -ENOMEM;
goto errout;
}

err = sctp_user_addto_chunk(chunk, offset, over,msgh->msg_iov);

Expand All @@ -342,7 +347,7 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
sctp_chunk_free(chunk);
}
sctp_datamsg_put(msg);
return NULL;
return ERR_PTR(err);
}

/* Check whether this message has expired. */
Expand Down
4 changes: 2 additions & 2 deletions net/sctp/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1915,8 +1915,8 @@ SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,

/* Break the message into multiple chunks of maximum size. */
datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
if (!datamsg) {
err = -ENOMEM;
if (IS_ERR(datamsg)) {
err = PTR_ERR(datamsg);
goto out_free;
}

Expand Down

0 comments on commit 6e51fe7

Please sign in to comment.