Skip to content

Commit

Permalink
crypto: af_alg - Disallow bind/setkey/... after accept(2)
Browse files Browse the repository at this point in the history
commit c840ac6 upstream.

Each af_alg parent socket obtained by socket(2) corresponds to a
tfm object once bind(2) has succeeded.  An accept(2) call on that
parent socket creates a context which then uses the tfm object.

Therefore as long as any child sockets created by accept(2) exist
the parent socket must not be modified or freed.

This patch guarantees this by using locks and a reference count
on the parent socket.  Any attempt to modify the parent socket will
fail with EBUSY.

Reported-by: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Herbert Xu authored and Greg Kroah-Hartman committed Feb 17, 2016
1 parent 134dd4d commit 06b4194
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
35 changes: 32 additions & 3 deletions crypto/af_alg.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,31 @@ int af_alg_release(struct socket *sock)
}
EXPORT_SYMBOL_GPL(af_alg_release);

void af_alg_release_parent(struct sock *sk)
{
struct alg_sock *ask = alg_sk(sk);
bool last;

sk = ask->parent;
ask = alg_sk(sk);

lock_sock(sk);
last = !--ask->refcnt;
release_sock(sk);

if (last)
sock_put(sk);
}
EXPORT_SYMBOL_GPL(af_alg_release_parent);

static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
{
struct sock *sk = sock->sk;
struct alg_sock *ask = alg_sk(sk);
struct sockaddr_alg *sa = (void *)uaddr;
const struct af_alg_type *type;
void *private;
int err;

if (sock->state == SS_CONNECTED)
return -EINVAL;
Expand All @@ -157,16 +175,22 @@ static int alg_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
return PTR_ERR(private);
}

err = -EBUSY;
lock_sock(sk);
if (ask->refcnt)
goto unlock;

swap(ask->type, type);
swap(ask->private, private);

err = 0;

unlock:
release_sock(sk);

alg_do_release(type, private);

return 0;
return err;
}

static int alg_setkey(struct sock *sk, char __user *ukey,
Expand Down Expand Up @@ -199,11 +223,15 @@ static int alg_setsockopt(struct socket *sock, int level, int optname,
struct sock *sk = sock->sk;
struct alg_sock *ask = alg_sk(sk);
const struct af_alg_type *type;
int err = -ENOPROTOOPT;
int err = -EBUSY;

lock_sock(sk);
if (ask->refcnt)
goto unlock;

type = ask->type;

err = -ENOPROTOOPT;
if (level != SOL_ALG || !type)
goto unlock;

Expand Down Expand Up @@ -254,7 +282,8 @@ int af_alg_accept(struct sock *sk, struct socket *newsock)

sk2->sk_family = PF_ALG;

sock_hold(sk);
if (!ask->refcnt++)
sock_hold(sk);
alg_sk(sk2)->parent = sk;
alg_sk(sk2)->type = type;

Expand Down
8 changes: 3 additions & 5 deletions include/crypto/if_alg.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ struct alg_sock {

struct sock *parent;

unsigned int refcnt;

const struct af_alg_type *type;
void *private;
};
Expand Down Expand Up @@ -64,6 +66,7 @@ int af_alg_register_type(const struct af_alg_type *type);
int af_alg_unregister_type(const struct af_alg_type *type);

int af_alg_release(struct socket *sock);
void af_alg_release_parent(struct sock *sk);
int af_alg_accept(struct sock *sk, struct socket *newsock);

int af_alg_make_sg(struct af_alg_sgl *sgl, void __user *addr, int len,
Expand All @@ -80,11 +83,6 @@ static inline struct alg_sock *alg_sk(struct sock *sk)
return (struct alg_sock *)sk;
}

static inline void af_alg_release_parent(struct sock *sk)
{
sock_put(alg_sk(sk)->parent);
}

static inline void af_alg_init_completion(struct af_alg_completion *completion)
{
init_completion(&completion->completion);
Expand Down

0 comments on commit 06b4194

Please sign in to comment.