Coverage Report

Created: 2026-06-15 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/bio/socket.cc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/bio.h>
16
17
#if !defined(OPENSSL_NO_SOCK)
18
19
#include <fcntl.h>
20
#include <string.h>
21
#include <limits.h>
22
23
#include <algorithm>
24
25
#if !defined(OPENSSL_WINDOWS)
26
#include <unistd.h>
27
#else
28
#include <winsock2.h>
29
OPENSSL_MSVC_PRAGMA(comment(lib, "Ws2_32.lib"))
30
#endif
31
32
#include "internal.h"
33
34
35
using namespace bssl;
36
37
#if !defined(OPENSSL_WINDOWS)
38
0
static int closesocket(int sock) { return close(sock); }
39
#endif
40
41
0
static int sock_free(BIO *bio) {
42
0
  if (BIO_get_shutdown(bio)) {
43
0
    if (BIO_get_init(bio)) {
44
0
      closesocket(FromOpaque(bio)->num);
45
0
    }
46
0
    BIO_set_init(bio, 0);
47
0
    BIO_clear_retry_flags(bio);
48
0
  }
49
0
  return 1;
50
0
}
51
52
0
static int sock_read(BIO *b, char *out, int outl) {
53
0
  bio_clear_socket_error();
54
#if defined(OPENSSL_WINDOWS)
55
  int ret = recv(FromOpaque(b)->num, out, outl, 0);
56
#else
57
0
  int ret = (int)read(FromOpaque(b)->num, out, outl);
58
0
#endif
59
0
  BIO_clear_retry_flags(b);
60
0
  if (ret <= 0) {
61
0
    if (bio_socket_should_retry(ret)) {
62
0
      BIO_set_retry_read(b);
63
0
    }
64
0
  }
65
0
  return ret;
66
0
}
67
68
static int sock_write_ex(BIO *b, const char *in, size_t inl,
69
0
                         size_t *out_written) {
70
0
  bio_clear_socket_error();
71
#if defined(OPENSSL_WINDOWS)
72
  inl = std::min(inl, size_t{INT_MAX});
73
  int ret = send(FromOpaque(b)->num, in, static_cast<int>(inl), 0);
74
#else
75
0
  ssize_t ret = write(FromOpaque(b)->num, in, inl);
76
0
#endif
77
0
  BIO_clear_retry_flags(b);
78
0
  if (ret <= 0) {
79
0
    if (bio_socket_should_retry(ret)) {
80
0
      BIO_set_retry_write(b);
81
0
    }
82
0
    return 0;
83
0
  }
84
85
0
  *out_written = ret;
86
0
  return 1;
87
0
}
88
89
0
static long sock_ctrl(BIO *b, int cmd, long num, void *ptr) {
90
0
  switch (cmd) {
91
0
    case BIO_C_SET_FD:
92
0
      sock_free(b);
93
0
      FromOpaque(b)->num = *static_cast<int *>(ptr);
94
0
      BIO_set_shutdown(b, static_cast<int>(num));
95
0
      BIO_set_init(b, 1);
96
0
      return 1;
97
0
    case BIO_C_GET_FD:
98
0
      if (BIO_get_init(b)) {
99
0
        int *out = static_cast<int *>(ptr);
100
0
        if (out != nullptr) {
101
0
          *out = FromOpaque(b)->num;
102
0
        }
103
0
        return FromOpaque(b)->num;
104
0
      }
105
0
      return -1;
106
0
    case BIO_CTRL_GET_CLOSE:
107
0
      return BIO_get_shutdown(b);
108
0
    case BIO_CTRL_SET_CLOSE:
109
0
      BIO_set_shutdown(b, static_cast<int>(num));
110
0
      return 1;
111
0
    case BIO_CTRL_FLUSH:
112
0
      return 1;
113
0
    default:
114
0
      return 0;
115
0
  }
116
0
}
117
118
static const BIO_METHOD methods_sockp = {
119
    BIO_TYPE_SOCKET,    "socket",
120
    /*bwrite=*/nullptr, sock_write_ex,
121
    sock_read,          nullptr /* gets, */,
122
    sock_ctrl,          nullptr /* create */,
123
    sock_free,          nullptr /* callback_ctrl */,
124
};
125
126
0
const BIO_METHOD *BIO_s_socket() { return &methods_sockp; }
127
128
0
BIO *BIO_new_socket(int fd, int close_flag) {
129
0
  BIO *ret;
130
131
0
  ret = BIO_new(BIO_s_socket());
132
0
  if (ret == nullptr) {
133
0
    return nullptr;
134
0
  }
135
0
  BIO_set_fd(ret, fd, close_flag);
136
0
  return ret;
137
0
}
138
139
// These functions are provided solely for compatibility with software that
140
// tries to copy and then modify `BIO_s_socket`. See bio.h for details.
141
// PostgreSQL's use makes several fragile assumptions on `BIO_s_socket`:
142
//
143
// - We do not store anything in `BIO_set_data`. (Broken in upstream OpenSSL,
144
//   which broke PostgreSQL.)
145
// - We do not store anything in `BIO_set_app_data`.
146
// - `BIO_s_socket` is implemented internally using the non-`size_t`-clean
147
//   I/O functions rather than the `size_t`-clean ones.
148
// - `BIO_METHOD` never gains another function pointer that is used in concert
149
//   with any of the functions here.
150
//
151
// Some other projects doing similar things use `BIO_meth_get_read` and
152
// `BIO_meth_get_write` and in turn assume that `BIO_s_socket` has not been
153
// ported to the `size_t`-clean `BIO_read_ex` and `BIO_write_ex`. (Not yet
154
// implemented in BoringSSL.)
155
//
156
// This is hopelessly fragile. PostgreSQL 18 will include a fix to stop using
157
// these APIs, but older versions and other software remain impacted, so we
158
// implement these functions, but only support `BIO_s_socket`. For now they just
159
// return the underlying functions, but if we ever need to break the above
160
// assumptions, we can return an older, frozen version of `BIO_s_socket`.
161
// Limiting to exactly one allowed `BIO_METHOD` lets us do this.
162
//
163
// These functions are also deprecated in upstream OpenSSL. See
164
// https://github.com/openssl/openssl/issues/26047
165
//
166
// TODO(davidben): Once Folly and all versions of PostgreSQL we care about are
167
// updated or patched, remove these functions.
168
169
0
int (*BIO_meth_get_write(const BIO_METHOD *method))(BIO *, const char *, int) {
170
0
  BSSL_CHECK(method == BIO_s_socket());
171
0
  return method->bwrite;
172
0
}
173
174
0
int (*BIO_meth_get_read(const BIO_METHOD *method))(BIO *, char *, int) {
175
0
  BSSL_CHECK(method == BIO_s_socket());
176
0
  return method->bread;
177
0
}
178
179
0
int (*BIO_meth_get_gets(const BIO_METHOD *method))(BIO *, char *, int) {
180
0
  BSSL_CHECK(method == BIO_s_socket());
181
0
  return method->bgets;
182
0
}
183
184
0
int (*BIO_meth_get_puts(const BIO_METHOD *method))(BIO *, const char *) {
185
0
  BSSL_CHECK(method == BIO_s_socket());
186
0
  return nullptr;
187
0
}
188
189
0
long (*BIO_meth_get_ctrl(const BIO_METHOD *method))(BIO *, int, long, void *) {
190
0
  BSSL_CHECK(method == BIO_s_socket());
191
0
  return method->ctrl;
192
0
}
193
194
0
int (*BIO_meth_get_create(const BIO_METHOD *method))(BIO *) {
195
0
  BSSL_CHECK(method == BIO_s_socket());
196
0
  return method->create;
197
0
}
198
199
0
int (*BIO_meth_get_destroy(const BIO_METHOD *method))(BIO *) {
200
0
  BSSL_CHECK(method == BIO_s_socket());
201
0
  return method->destroy;
202
0
}
203
204
long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *method))(BIO *, int,
205
0
                                                             bio_info_cb) {
206
0
  BSSL_CHECK(method == BIO_s_socket());
207
0
  return method->callback_ctrl;
208
0
}
209
210
#endif  // OPENSSL_NO_SOCK