Skip to content

Commit

Permalink
Merge branch 'sctp_do_bind-leak'
Browse files Browse the repository at this point in the history
Mao Wenan says:

====================
fix memory leak for sctp_do_bind

First two patches are to do cleanup, remove redundant assignment,
and change return type of sctp_get_port_local.
Third patch is to fix memory leak for sctp_do_bind if failed
to bind address.

v2: add one patch to change return type of sctp_get_port_local.
====================

Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
davem330 committed Sep 13, 2019
2 parents 8f6617b + 29b99f5 commit ae3b06e
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions net/sctp/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
return retval;
}

static long sctp_get_port_local(struct sock *, union sctp_addr *);
static int sctp_get_port_local(struct sock *, union sctp_addr *);

/* Verify this is a valid sockaddr. */
static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
Expand Down Expand Up @@ -399,9 +399,8 @@ static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
* detection.
*/
addr->v4.sin_port = htons(snum);
if ((ret = sctp_get_port_local(sk, addr))) {
if (sctp_get_port_local(sk, addr))
return -EADDRINUSE;
}

/* Refresh ephemeral port. */
if (!bp->port)
Expand All @@ -413,11 +412,13 @@ static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
SCTP_ADDR_SRC, GFP_ATOMIC);

/* Copy back into socket for getsockname() use. */
if (!ret) {
inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
sp->pf->to_sk_saddr(addr, sk);
if (ret) {
sctp_put_port(sk);
return ret;
}
/* Copy back into socket for getsockname() use. */
inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
sp->pf->to_sk_saddr(addr, sk);

return ret;
}
Expand Down Expand Up @@ -7999,7 +8000,7 @@ static void sctp_unhash(struct sock *sk)
static struct sctp_bind_bucket *sctp_bucket_create(
struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);

static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
{
struct sctp_sock *sp = sctp_sk(sk);
bool reuse = (sk->sk_reuse || sp->reuse);
Expand Down Expand Up @@ -8109,7 +8110,7 @@ static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)

if (sctp_bind_addr_conflict(&ep2->base.bind_addr,
addr, sp2, sp)) {
ret = (long)sk2;
ret = 1;
goto fail_unlock;
}
}
Expand Down Expand Up @@ -8181,7 +8182,7 @@ static int sctp_get_port(struct sock *sk, unsigned short snum)
addr.v4.sin_port = htons(snum);

/* Note: sk->sk_num gets filled in if ephemeral port request. */
return !!sctp_get_port_local(sk, &addr);
return sctp_get_port_local(sk, &addr);
}

/*
Expand Down

0 comments on commit ae3b06e

Please sign in to comment.