lkml.org 
[lkml]   [2012]   [Aug]   [7]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    Patch in this message
    /
    From
    Subject[ 033/109] tun: fix a crash bug and a memory leak
    Date
    From: Greg KH <gregkh@linuxfoundation.org>

    3.4-stable review patch. If anyone has any objections, please let me know.

    ------------------

    From: Mikulas Patocka <mikulas@artax.karlin.mff.cuni.cz>

    commit b09e786bd1dd66418b69348cb110f3a64764626a upstream.

    This patch fixes a crash
    tun_chr_close -> netdev_run_todo -> tun_free_netdev -> sk_release_kernel ->
    sock_release -> iput(SOCK_INODE(sock))
    introduced by commit 1ab5ecb90cb6a3df1476e052f76a6e8f6511cb3d

    The problem is that this socket is embedded in struct tun_struct, it has
    no inode, iput is called on invalid inode, which modifies invalid memory
    and optionally causes a crash.

    sock_release also decrements sockets_in_use, this causes a bug that
    "sockets: used" field in /proc/*/net/sockstat keeps on decreasing when
    creating and closing tun devices.

    This patch introduces a flag SOCK_EXTERNALLY_ALLOCATED that instructs
    sock_release to not free the inode and not decrement sockets_in_use,
    fixing both memory corruption and sockets_in_use underflow.

    It should be backported to 3.3 an 3.4 stabke.

    Signed-off-by: Mikulas Patocka <mikulas@artax.karlin.mff.cuni.cz>
    Signed-off-by: David S. Miller <davem@davemloft.net>
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

    ---
    drivers/net/tun.c | 3 +++
    include/linux/net.h | 1 +
    net/socket.c | 3 +++
    3 files changed, 7 insertions(+)

    --- a/drivers/net/tun.c
    +++ b/drivers/net/tun.c
    @@ -358,6 +358,8 @@ static void tun_free_netdev(struct net_d
    {
    struct tun_struct *tun = netdev_priv(dev);

    + BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags));
    +
    sk_release_kernel(tun->socket.sk);
    }

    @@ -1115,6 +1117,7 @@ static int tun_set_iff(struct net *net,
    tun->flags = flags;
    tun->txflt.count = 0;
    tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
    + set_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags);

    err = -ENOMEM;
    sk = sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
    --- a/include/linux/net.h
    +++ b/include/linux/net.h
    @@ -72,6 +72,7 @@ struct net;
    #define SOCK_NOSPACE 2
    #define SOCK_PASSCRED 3
    #define SOCK_PASSSEC 4
    +#define SOCK_EXTERNALLY_ALLOCATED 5

    #ifndef ARCH_HAS_SOCKET_TYPES
    /**
    --- a/net/socket.c
    +++ b/net/socket.c
    @@ -522,6 +522,9 @@ void sock_release(struct socket *sock)
    if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
    printk(KERN_ERR "sock_release: fasync list not empty!\n");

    + if (test_bit(SOCK_EXTERNALLY_ALLOCATED, &sock->flags))
    + return;
    +
    percpu_sub(sockets_in_use, 1);
    if (!sock->file) {
    iput(SOCK_INODE(sock));



    \
     
     \ /
      Last update: 2012-08-08 03:41    [W:0.026 / U:29.356 seconds]
    ©2003-2016 Jasper Spaans. hosted at Digital OceanAdvertise on this site