Ticket #3255: 0001-Add-length-parameter-to-bind-and-connect.patch

File 0001-Add-length-parameter-to-bind-and-connect.patch, 18.5 KB (added by jscipione, 7 years ago)

NUL terminate unix socket address in UnixEndpoint.cpp

  • headers/private/net/net_protocol.h

    From dce01abb6633a1b119f42fd041d998b18da0a63b Mon Sep 17 00:00:00 2001
    From: John Scipione <jscipione@gmail.com>
    Date: Tue, 27 Aug 2013 18:18:05 -0400
    Subject: [PATCH] Add length parameter to bind() and connect()
    
    NUL terminate address in src/add-ons/kernel/network/protocols/unix/UnixEndpoint.cpp
    
    Include but ignore addressLength parameter elsewhere
    ---
     headers/private/net/net_protocol.h                  |  6 ++++--
     src/add-ons/kernel/network/protocols/icmp/icmp.cpp  |  6 ++++--
     .../kernel/network/protocols/icmp6/icmp6.cpp        |  6 ++++--
     src/add-ons/kernel/network/protocols/ipv4/ipv4.cpp  |  6 ++++--
     src/add-ons/kernel/network/protocols/ipv6/ipv6.cpp  |  6 ++++--
     .../network/protocols/tcp/EndpointManager.cpp       |  6 ++++--
     .../kernel/network/protocols/tcp/EndpointManager.h  |  4 ++--
     .../kernel/network/protocols/tcp/TCPEndpoint.cpp    |  6 +++---
     .../kernel/network/protocols/tcp/TCPEndpoint.h      |  4 ++--
     src/add-ons/kernel/network/protocols/tcp/tcp.cpp    | 10 ++++++----
     src/add-ons/kernel/network/protocols/udp/udp.cpp    | 21 ++++++++++++---------
     .../kernel/network/protocols/unix/UnixEndpoint.cpp  | 15 ++++++++++-----
     .../kernel/network/protocols/unix/UnixEndpoint.h    |  4 ++--
     src/add-ons/kernel/network/protocols/unix/unix.cpp  | 10 ++++++----
     src/add-ons/kernel/network/stack/link.cpp           | 12 +++++++-----
     src/add-ons/kernel/network/stack/net_socket.cpp     |  5 +++--
     16 files changed, 77 insertions(+), 50 deletions(-)
    
    diff --git a/headers/private/net/net_protocol.h b/headers/private/net/net_protocol.h
    index 352b8a5..6ff9a1b 100644
    a b struct net_protocol_module_info {  
    6565    status_t    (*close)(net_protocol* self);
    6666    status_t    (*free)(net_protocol* self);
    6767
    68     status_t    (*connect)(net_protocol* self, const struct sockaddr* address);
     68    status_t    (*connect)(net_protocol* self, const struct sockaddr* address,
     69                    socklen_t addressLength);
    6970    status_t    (*accept)(net_protocol* self, net_socket** _acceptedSocket);
    7071    status_t    (*control)(net_protocol* self, int level, int option,
    7172                    void* value, size_t* _length);
    struct net_protocol_module_info {  
    7475    status_t    (*setsockopt)(net_protocol* self, int level, int option,
    7576                    const void* value, int length);
    7677
    77     status_t    (*bind)(net_protocol* self, const struct sockaddr* address);
     78    status_t    (*bind)(net_protocol* self, const struct sockaddr* address,
     79                    socklen_t addressLength);
    7880    status_t    (*unbind)(net_protocol* self, struct sockaddr* address);
    7981    status_t    (*listen)(net_protocol* self, int count);
    8082    status_t    (*shutdown)(net_protocol* self, int direction);
  • src/add-ons/kernel/network/protocols/icmp/icmp.cpp

    diff --git a/src/add-ons/kernel/network/protocols/icmp/icmp.cpp b/src/add-ons/kernel/network/protocols/icmp/icmp.cpp
    index 2beff1a..f800d32 100644
    a b icmp_free(net_protocol* protocol)  
    291291
    292292
    293293status_t
    294 icmp_connect(net_protocol* protocol, const struct sockaddr* address)
     294icmp_connect(net_protocol* protocol, const struct sockaddr* address,
     295    socklen_t addressLength)
    295296{
    296297    return B_ERROR;
    297298}
    icmp_setsockopt(net_protocol* protocol, int level, int option,  
    332333
    333334
    334335status_t
    335 icmp_bind(net_protocol* protocol, const struct sockaddr* address)
     336icmp_bind(net_protocol* protocol, const struct sockaddr* address,
     337    socklen_t addressLength)
    336338{
    337339    return B_ERROR;
    338340}
  • src/add-ons/kernel/network/protocols/icmp6/icmp6.cpp

    diff --git a/src/add-ons/kernel/network/protocols/icmp6/icmp6.cpp b/src/add-ons/kernel/network/protocols/icmp6/icmp6.cpp
    index 4f2dd1d..b01ad20 100644
    a b icmp6_free(net_protocol *protocol)  
    8080
    8181
    8282status_t
    83 icmp6_connect(net_protocol *protocol, const struct sockaddr *address)
     83icmp6_connect(net_protocol *protocol, const struct sockaddr *address,
     84    socklen_t addressLength)
    8485{
    8586    return B_ERROR;
    8687}
    icmp6_setsockopt(net_protocol *protocol, int level, int option,  
    121122
    122123
    123124status_t
    124 icmp6_bind(net_protocol *protocol, const struct sockaddr *address)
     125icmp6_bind(net_protocol *protocol, const struct sockaddr *address,
     126    socklen_t addressLength)
    125127{
    126128    return B_ERROR;
    127129}
  • src/add-ons/kernel/network/protocols/ipv4/ipv4.cpp

    diff --git a/src/add-ons/kernel/network/protocols/ipv4/ipv4.cpp b/src/add-ons/kernel/network/protocols/ipv4/ipv4.cpp
    index 15011da..d4d90be 100644
    a b ipv4_free(net_protocol* protocol)  
    11001100
    11011101
    11021102status_t
    1103 ipv4_connect(net_protocol* protocol, const struct sockaddr* address)
     1103ipv4_connect(net_protocol* protocol, const struct sockaddr* address,
     1104    socklen_t addressLength)
    11041105{
    11051106    return B_ERROR;
    11061107}
    ipv4_setsockopt(net_protocol* _protocol, int level, int option,  
    13121313
    13131314
    13141315status_t
    1315 ipv4_bind(net_protocol* protocol, const struct sockaddr* address)
     1316ipv4_bind(net_protocol* protocol, const struct sockaddr* address,
     1317    socklen_t addressLength)
    13161318{
    13171319    if (address->sa_family != AF_INET)
    13181320        return EAFNOSUPPORT;
  • src/add-ons/kernel/network/protocols/ipv6/ipv6.cpp

    diff --git a/src/add-ons/kernel/network/protocols/ipv6/ipv6.cpp b/src/add-ons/kernel/network/protocols/ipv6/ipv6.cpp
    index 76a5ec5..1607f05 100644
    a b ipv6_free(net_protocol* protocol)  
    10401040
    10411041
    10421042status_t
    1043 ipv6_connect(net_protocol* protocol, const struct sockaddr* address)
     1043ipv6_connect(net_protocol* protocol, const struct sockaddr* address,
     1044    socklen_t addressLength)
    10441045{
    10451046    return B_ERROR;
    10461047}
    ipv6_setsockopt(net_protocol* _protocol, int level, int option,  
    11801181
    11811182
    11821183status_t
    1183 ipv6_bind(net_protocol* protocol, const sockaddr* _address)
     1184ipv6_bind(net_protocol* protocol, const sockaddr* _address,
     1185    socklen_t addressLength)
    11841186{
    11851187    if (_address->sa_family != AF_INET6)
    11861188        return EAFNOSUPPORT;
  • src/add-ons/kernel/network/protocols/tcp/EndpointManager.cpp

    diff --git a/src/add-ons/kernel/network/protocols/tcp/EndpointManager.cpp b/src/add-ons/kernel/network/protocols/tcp/EndpointManager.cpp
    index 1739b63..f8d29ae 100644
    a b EndpointManager::FindConnection(sockaddr* local, sockaddr* peer)  
    360360
    361361
    362362status_t
    363 EndpointManager::Bind(TCPEndpoint* endpoint, const sockaddr* address)
     363EndpointManager::Bind(TCPEndpoint* endpoint, const sockaddr* address,
     364    socklen_t addressLength)
    364365{
    365366    // check the family
    366367    if (!AddressModule()->is_same_family(address))
    EndpointManager::_Bind(TCPEndpoint* endpoint, const sockaddr* address)  
    494495{
    495496    // Thus far we have checked if the Bind() is allowed
    496497
    497     status_t status = endpoint->next->module->bind(endpoint->next, address);
     498    status_t status = endpoint->next->module->bind(endpoint->next, address,
     499        sizeof(address));
    498500    if (status < B_OK)
    499501        return status;
    500502
  • src/add-ons/kernel/network/protocols/tcp/EndpointManager.h

    diff --git a/src/add-ons/kernel/network/protocols/tcp/EndpointManager.h b/src/add-ons/kernel/network/protocols/tcp/EndpointManager.h
    index ebe2aea..8cccd44 100644
    a b public:  
    8080                                const sockaddr* interfaceLocal);
    8181            status_t        SetPassive(TCPEndpoint* endpoint);
    8282
    83             status_t        Bind(TCPEndpoint* endpoint,
    84                                 const sockaddr* address);
     83            status_t        Bind(TCPEndpoint* endpoint, const sockaddr* address,
     84                                socklen_t addressLength);
    8585            status_t        BindChild(TCPEndpoint* endpoint);
    8686            status_t        Unbind(TCPEndpoint* endpoint);
    8787
  • src/add-ons/kernel/network/protocols/tcp/TCPEndpoint.cpp

    diff --git a/src/add-ons/kernel/network/protocols/tcp/TCPEndpoint.cpp b/src/add-ons/kernel/network/protocols/tcp/TCPEndpoint.cpp
    index 8c3fae3..03d66fa 100644
    a b TCPEndpoint::Free()  
    580580    until the connection has been established or refused.
    581581*/
    582582status_t
    583 TCPEndpoint::Connect(const sockaddr* address)
     583TCPEndpoint::Connect(const sockaddr* address, socklen_t addressLength)
    584584{
    585585    TRACE("Connect() on address %s", PrintAddress(address));
    586586
    TCPEndpoint::Accept(struct net_socket** _acceptedSocket)  
    696696
    697697
    698698status_t
    699 TCPEndpoint::Bind(const sockaddr *address)
     699TCPEndpoint::Bind(const sockaddr *address, socklen_t addressLength)
    700700{
    701701    if (address == NULL)
    702702        return B_BAD_VALUE;
    TCPEndpoint::Bind(const sockaddr *address)  
    708708    if (fState != CLOSED)
    709709        return EISCONN;
    710710
    711     return fManager->Bind(this, address);
     711    return fManager->Bind(this, address, addressLength);
    712712}
    713713
    714714
  • src/add-ons/kernel/network/protocols/tcp/TCPEndpoint.h

    diff --git a/src/add-ons/kernel/network/protocols/tcp/TCPEndpoint.h b/src/add-ons/kernel/network/protocols/tcp/TCPEndpoint.h
    index 085e173..35ed4dd 100644
    a b public:  
    5151            status_t    Open();
    5252            status_t    Close();
    5353            void        Free();
    54             status_t    Connect(const struct sockaddr* address);
     54            status_t    Connect(const struct sockaddr* address, socklen_t addressLength);
    5555            status_t    Accept(struct net_socket** _acceptedSocket);
    56             status_t    Bind(const sockaddr* address);
     56            status_t    Bind(const sockaddr* address, socklen_t addressLength);
    5757            status_t    Unbind(struct sockaddr* address);
    5858            status_t    Listen(int count);
    5959            status_t    Shutdown(int direction);
  • src/add-ons/kernel/network/protocols/tcp/tcp.cpp

    diff --git a/src/add-ons/kernel/network/protocols/tcp/tcp.cpp b/src/add-ons/kernel/network/protocols/tcp/tcp.cpp
    index 7c1dbb4..b0c5a62 100644
    a b tcp_free(net_protocol* protocol)  
    503503
    504504
    505505status_t
    506 tcp_connect(net_protocol* protocol, const struct sockaddr* address)
     506tcp_connect(net_protocol* protocol, const struct sockaddr* address,
     507    socklen_t addressLength)
    507508{
    508     return ((TCPEndpoint*)protocol)->Connect(address);
     509    return ((TCPEndpoint*)protocol)->Connect(address, addressLength);
    509510}
    510511
    511512
    tcp_setsockopt(net_protocol* _protocol, int level, int option,  
    577578
    578579
    579580status_t
    580 tcp_bind(net_protocol* protocol, const struct sockaddr* address)
     581tcp_bind(net_protocol* protocol, const struct sockaddr* address,
     582    socklen_t addressLength)
    581583{
    582     return ((TCPEndpoint*)protocol)->Bind(address);
     584    return ((TCPEndpoint*)protocol)->Bind(address, addressLength);
    583585}
    584586
    585587
  • src/add-ons/kernel/network/protocols/udp/udp.cpp

    diff --git a/src/add-ons/kernel/network/protocols/udp/udp.cpp b/src/add-ons/kernel/network/protocols/udp/udp.cpp
    index 97926b1..d213d7a 100644
    a b class UdpEndpoint : public net_protocol, public DatagramSocket<> {  
    7575public:
    7676                                UdpEndpoint(net_socket* socket);
    7777
    78             status_t            Bind(const sockaddr* newAddr);
     78            status_t            Bind(const sockaddr* newAddr, socklen_t addressLength);
    7979            status_t            Unbind(sockaddr* newAddr);
    80             status_t            Connect(const sockaddr* newAddr);
     80            status_t            Connect(const sockaddr* newAddr, socklen_t addressLength);
    8181
    8282            status_t            Open();
    8383            status_t            Close();
    UdpDomainSupport::_BindToEphemeral(UdpEndpoint *endpoint,  
    483483status_t
    484484UdpDomainSupport::_FinishBind(UdpEndpoint *endpoint, const sockaddr *address)
    485485{
    486     status_t status = endpoint->next->module->bind(endpoint->next, address);
     486    status_t status = endpoint->next->module->bind(endpoint->next, address,
     487        sizeof(address));
    487488    if (status < B_OK)
    488489        return status;
    489490
    UdpEndpoint::UdpEndpoint(net_socket *socket)  
    919920
    920921
    921922status_t
    922 UdpEndpoint::Bind(const sockaddr *address)
     923UdpEndpoint::Bind(const sockaddr *address, socklen_t addressLength)
    923924{
    924925    TRACE_EP("Bind(%s)", AddressString(Domain(), address, true).Data());
    925926    return fManager->BindEndpoint(this, address);
    UdpEndpoint::Unbind(sockaddr *address)  
    935936
    936937
    937938status_t
    938 UdpEndpoint::Connect(const sockaddr *address)
     939UdpEndpoint::Connect(const sockaddr *address, socklen_t addressLength)
    939940{
    940941    TRACE_EP("Connect(%s)", AddressString(Domain(), address, true).Data());
    941942    return fManager->ConnectEndpoint(this, address);
    udp_free(net_protocol *protocol)  
    11411142
    11421143
    11431144status_t
    1144 udp_connect(net_protocol *protocol, const struct sockaddr *address)
     1145udp_connect(net_protocol *protocol, const struct sockaddr *address,
     1146    socklen_t addressLength)
    11451147{
    1146     return ((UdpEndpoint *)protocol)->Connect(address);
     1148    return ((UdpEndpoint *)protocol)->Connect(address, addressLength);
    11471149}
    11481150
    11491151
    udp_setsockopt(net_protocol *protocol, int level, int option,  
    11821184
    11831185
    11841186status_t
    1185 udp_bind(net_protocol *protocol, const struct sockaddr *address)
     1187udp_bind(net_protocol *protocol, const struct sockaddr *address,
     1188    socklen_t addressLength)
    11861189{
    1187     return ((UdpEndpoint *)protocol)->Bind(address);
     1190    return ((UdpEndpoint *)protocol)->Bind(address, addressLength);
    11881191}
    11891192
    11901193
  • src/add-ons/kernel/network/protocols/unix/UnixEndpoint.cpp

    diff --git a/src/add-ons/kernel/network/protocols/unix/UnixEndpoint.cpp b/src/add-ons/kernel/network/protocols/unix/UnixEndpoint.cpp
    index b2d12b5..c1646d5 100644
    a b UnixEndpoint::Free()  
    149149
    150150
    151151status_t
    152 UnixEndpoint::Bind(const struct sockaddr *_address)
     152UnixEndpoint::Bind(const struct sockaddr *_address, socklen_t addressLength)
    153153{
    154154    if (_address->sa_family != AF_UNIX)
    155155        RETURN_ERROR(EAFNOSUPPORT);
    UnixEndpoint::Bind(const struct sockaddr *_address)  
    189189        gAddressManager.Add(this);
    190190    } else {
    191191        // FS address space
     192        sockaddr_un tempAddress;
     193
     194        memset(&tempAddress, 0, sizeof(tempAddress));
     195        memcpy(&tempAddress, address, addressLength);
     196        address = &tempAddress;
     197
    192198        size_t pathLen = strnlen(address->sun_path, sizeof(address->sun_path));
    193199        if (pathLen == 0 || pathLen == sizeof(address->sun_path))
    194200            RETURN_ERROR(B_BAD_VALUE);
    195201
    196202        struct vnode* vnode;
    197         status_t error = vfs_create_special_node(address->sun_path,
    198             NULL, S_IFSOCK | 0644, 0, !gStackModule->is_syscall(), NULL,
    199             &vnode);
     203        status_t error = vfs_create_special_node(address->sun_path, NULL,
     204            S_IFSOCK | 0644, 0, !gStackModule->is_syscall(), NULL, &vnode);
    200205        if (error != B_OK)
    201206            RETURN_ERROR(error == B_FILE_EXISTS ? EADDRINUSE : error);
    202207
    UnixEndpoint::Listen(int backlog)  
    261266
    262267
    263268status_t
    264 UnixEndpoint::Connect(const struct sockaddr *_address)
     269UnixEndpoint::Connect(const struct sockaddr *_address, socklen_t addressLength)
    265270{
    266271    if (_address->sa_family != AF_UNIX)
    267272        RETURN_ERROR(EAFNOSUPPORT);
  • src/add-ons/kernel/network/protocols/unix/UnixEndpoint.h

    diff --git a/src/add-ons/kernel/network/protocols/unix/UnixEndpoint.h b/src/add-ons/kernel/network/protocols/unix/UnixEndpoint.h
    index 5e09fb0..34d1b57 100644
    a b public:  
    6060        mutex_unlock(&fLock);
    6161    }
    6262
    63     status_t Bind(const struct sockaddr *_address);
     63    status_t Bind(const struct sockaddr *_address, socklen_t addressLength);
    6464    status_t Unbind();
    6565    status_t Listen(int backlog);
    66     status_t Connect(const struct sockaddr *address);
     66    status_t Connect(const struct sockaddr *_address, socklen_t addressLength);
    6767    status_t Accept(net_socket **_acceptedSocket);
    6868
    6969    ssize_t Send(const iovec *vecs, size_t vecCount,
  • src/add-ons/kernel/network/protocols/unix/unix.cpp

    diff --git a/src/add-ons/kernel/network/protocols/unix/unix.cpp b/src/add-ons/kernel/network/protocols/unix/unix.cpp
    index eec335b..398903c 100644
    a b unix_free(net_protocol *_protocol)  
    110110
    111111
    112112status_t
    113 unix_connect(net_protocol *_protocol, const struct sockaddr *address)
     113unix_connect(net_protocol *_protocol, const struct sockaddr *_address,
     114    socklen_t addressLength)
    114115{
    115     return ((UnixEndpoint*)_protocol)->Connect(address);
     116    return ((UnixEndpoint*)_protocol)->Connect(_address, addressLength);
    116117}
    117118
    118119
    unix_setsockopt(net_protocol *protocol, int level, int option,  
    176177
    177178
    178179status_t
    179 unix_bind(net_protocol *_protocol, const struct sockaddr *_address)
     180unix_bind(net_protocol *_protocol, const struct sockaddr *_address,
     181    socklen_t addressLength)
    180182{
    181     return ((UnixEndpoint*)_protocol)->Bind(_address);
     183    return ((UnixEndpoint*)_protocol)->Bind(_address, addressLength);
    182184}
    183185
    184186
  • src/add-ons/kernel/network/stack/link.cpp

    diff --git a/src/add-ons/kernel/network/stack/link.cpp b/src/add-ons/kernel/network/stack/link.cpp
    index c88f19b..c50c50f 100644
    a b public:  
    5050            status_t            StartMonitoring(const char* deviceName);
    5151            status_t            StopMonitoring(const char* deviceName);
    5252
    53             status_t            Bind(const sockaddr* address);
     53            status_t            Bind(const sockaddr* address, socklen_t addressLength);
    5454            status_t            Unbind();
    5555            bool                IsBound() const
    5656                                    { return fBoundToDevice != NULL; }
    LinkProtocol::StopMonitoring(const char* deviceName)  
    140140
    141141
    142142status_t
    143 LinkProtocol::Bind(const sockaddr* address)
     143LinkProtocol::Bind(const sockaddr* address, socklen_t addressLength)
    144144{
    145145    // Only root is allowed to bind to a link layer interface
    146146    if (address == NULL || geteuid() != 0)
    link_free(net_protocol* protocol)  
    337337
    338338
    339339static status_t
    340 link_connect(net_protocol* protocol, const struct sockaddr* address)
     340link_connect(net_protocol* protocol, const struct sockaddr* address,
     341    socklen_t addressLength)
    341342{
    342343    return B_NOT_SUPPORTED;
    343344}
    link_setsockopt(net_protocol* protocol, int level, int option,  
    541542
    542543
    543544static status_t
    544 link_bind(net_protocol* _protocol, const struct sockaddr* address)
     545link_bind(net_protocol* _protocol, const struct sockaddr* address,
     546    socklen_t addressLength)
    545547{
    546548    LinkProtocol* protocol = (LinkProtocol*)_protocol;
    547     return protocol->Bind(address);
     549    return protocol->Bind(address, addressLength);
    548550}
    549551
    550552
  • src/add-ons/kernel/network/stack/net_socket.cpp

    diff --git a/src/add-ons/kernel/network/stack/net_socket.cpp b/src/add-ons/kernel/network/stack/net_socket.cpp
    index 9d5cfc9..ff2c1d4 100644
    a b socket_bind(net_socket* socket, const struct sockaddr* address,  
    10141014    socket->address.ss_len = sizeof(sockaddr_storage);
    10151015
    10161016    status_t status = socket->first_info->bind(socket->first_protocol,
    1017         (sockaddr*)address);
     1017        (sockaddr*)address, addressLength);
    10181018    if (status != B_OK) {
    10191019        // clear address again, as binding failed
    10201020        socket->address.ss_len = 0;
    socket_connect(net_socket* socket, const struct sockaddr* address,  
    10381038            return status;
    10391039    }
    10401040
    1041     return socket->first_info->connect(socket->first_protocol, address);
     1041    return socket->first_info->connect(socket->first_protocol, address,
     1042        addressLength);
    10421043}
    10431044
    10441045