Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/socket/nsSOCKSSocketProvider.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
 *
3
 * This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "nsIServiceManager.h"
8
#include "nsNamedPipeIOLayer.h"
9
#include "nsSOCKSSocketProvider.h"
10
#include "nsSOCKSIOLayer.h"
11
#include "nsCOMPtr.h"
12
#include "nsError.h"
13
14
using mozilla::OriginAttributes;
15
16
//////////////////////////////////////////////////////////////////////////
17
18
NS_IMPL_ISUPPORTS(nsSOCKSSocketProvider, nsISocketProvider)
19
20
nsresult
21
nsSOCKSSocketProvider::CreateV4(nsISupports *aOuter, REFNSIID aIID, void **aResult)
22
0
{
23
0
    nsresult rv;
24
0
    nsCOMPtr<nsISocketProvider> inst =
25
0
            new nsSOCKSSocketProvider(NS_SOCKS_VERSION_4);
26
0
    if (!inst)
27
0
        rv = NS_ERROR_OUT_OF_MEMORY;
28
0
    else
29
0
        rv = inst->QueryInterface(aIID, aResult);
30
0
    return rv;
31
0
}
32
33
nsresult
34
nsSOCKSSocketProvider::CreateV5(nsISupports *aOuter, REFNSIID aIID, void **aResult)
35
0
{
36
0
    nsresult rv;
37
0
    nsCOMPtr<nsISocketProvider> inst =
38
0
            new nsSOCKSSocketProvider(NS_SOCKS_VERSION_5);
39
0
    if (!inst)
40
0
        rv = NS_ERROR_OUT_OF_MEMORY;
41
0
    else
42
0
        rv = inst->QueryInterface(aIID, aResult);
43
0
    return rv;
44
0
}
45
46
// Per-platform implemenation of OpenTCPSocket helper function
47
// Different platforms have special cases to handle
48
49
#if defined(XP_WIN)
50
// The proxy host on Windows may be a named pipe uri, in which
51
// case a named-pipe (rather than a socket) should be returned
52
static PRFileDesc*
53
OpenTCPSocket(int32_t family,
54
              nsIProxyInfo *proxy)
55
{
56
    PRFileDesc* sock = nullptr;
57
58
    nsAutoCString proxyHost;
59
    proxy->GetHost(proxyHost);
60
    if (IsNamedPipePath(proxyHost)) {
61
        sock = CreateNamedPipeLayer();
62
    } else {
63
        sock = PR_OpenTCPSocket(family);
64
    }
65
66
    return sock;
67
}
68
#elif defined(XP_UNIX)
69
// The proxy host on UNIX systems may point to a local file uri
70
// in which case we should create an AF_LOCAL (UNIX Domain) socket
71
// instead of the requested AF_INET or AF_INET6 socket.
72
73
// Normally,this socket would get thrown out and recreated later on
74
// with the proper family, but we want to do it early here so that
75
// we can enforce seccomp policy to blacklist socket(AF_INET) calls
76
// to prevent the content sandbox from creating network requests
77
static PRFileDesc*
78
OpenTCPSocket(int32_t family,
79
              nsIProxyInfo *proxy)
80
0
{
81
0
    nsAutoCString proxyHost;
82
0
    proxy->GetHost(proxyHost);
83
0
    if (StringBeginsWith(proxyHost, NS_LITERAL_CSTRING("file://"))) {
84
0
        family = AF_LOCAL;
85
0
    }
86
0
87
0
    return PR_OpenTCPSocket(family);
88
0
}
89
#else
90
// Default, pass-through to PR_OpenTCPSocket
91
static PRFileDesc*
92
OpenTCPSocket(int32_t family,
93
              nsIProxyInfo*)
94
{
95
    return PR_OpenTCPSocket(family);
96
}
97
#endif
98
99
NS_IMETHODIMP
100
nsSOCKSSocketProvider::NewSocket(int32_t family,
101
                                 const char *host,
102
                                 int32_t port,
103
                                 nsIProxyInfo *proxy,
104
                                 const OriginAttributes &originAttributes,
105
                                 uint32_t flags,
106
                                 uint32_t tlsFlags,
107
                                 PRFileDesc **result,
108
                                 nsISupports **socksInfo)
109
0
{
110
0
    PRFileDesc *sock = OpenTCPSocket(family, proxy);
111
0
    if (!sock) {
112
0
        return NS_ERROR_OUT_OF_MEMORY;
113
0
    }
114
0
115
0
    nsresult rv = nsSOCKSIOLayerAddToSocket(family,
116
0
                                            host,
117
0
                                            port,
118
0
                                            proxy,
119
0
                                            mVersion,
120
0
                                            flags,
121
0
                                            tlsFlags,
122
0
                                            sock,
123
0
                                            socksInfo);
124
0
    if (NS_SUCCEEDED(rv)) {
125
0
        *result = sock;
126
0
        return NS_OK;
127
0
    }
128
0
129
0
    return NS_ERROR_SOCKET_CREATE_FAILED;
130
0
}
131
132
NS_IMETHODIMP
133
nsSOCKSSocketProvider::AddToSocket(int32_t family,
134
                                   const char *host,
135
                                   int32_t port,
136
                                   nsIProxyInfo *proxy,
137
                                   const OriginAttributes &originAttributes,
138
                                   uint32_t flags,
139
                                   uint32_t tlsFlags,
140
                                   PRFileDesc *sock,
141
                                   nsISupports **socksInfo)
142
0
{
143
0
    nsresult rv = nsSOCKSIOLayerAddToSocket(family,
144
0
                                            host,
145
0
                                            port,
146
0
                                            proxy,
147
0
                                            mVersion,
148
0
                                            flags,
149
0
                                            tlsFlags,
150
0
                                            sock,
151
0
                                            socksInfo);
152
0
153
0
    if (NS_FAILED(rv))
154
0
        rv = NS_ERROR_SOCKET_CREATE_FAILED;
155
0
    return rv;
156
0
}