Skip to content

Commit 4972e2b

Browse files
committed
Fix sbuf2 leak in revconn
Signed-off-by: Mark Hannum <mhannum72@gmail.com>
1 parent 9b56813 commit 4972e2b

5 files changed

Lines changed: 29 additions & 9 deletions

File tree

bbinc/sbuf2.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ enum SBUF2_FLAGS {
5959
SBUF2_NO_BLOCK = 16,
6060
/* the underlying connection has been marked 'READONLY' */
6161
SBUF2_IS_READONLY = 32,
62+
/* do not close ssl */
63+
SBUF2_NO_SSL_CLOSE = 64,
6264
};
6365

6466
typedef int (*sbuf2readfn)(SBUF2 *sb, char *buf, int nbytes);

db/comdb2.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,6 +3510,9 @@ extern int gbl_max_sql_hint_cache;
35103510

35113511
/* Remote cursor support */
35123512
/* use portmux to open an SBUF2 to local db or proxied db */
3513+
SBUF2 *connect_remote_db_flags(const char *protocol, const char *dbname, const char *service, char *host, int use_cache,
3514+
int force_rte, int sbflags);
3515+
35133516
SBUF2 *connect_remote_db(const char *protocol, const char *dbname, const char *service, char *host, int use_cache,
35143517
int force_rte);
35153518
int get_rootpage_numbers(int nums);

db/reverse_conn.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ int send_reversesql_request(const char *dbname, const char *host, const char *co
9393
revconn_logmsg(LOGMSG_USER, "%s:%d Sending reversesql request to %s@%s\n", __func__, __LINE__, dbname, host);
9494
}
9595

96-
SBUF2 *sb = connect_remote_db(NULL, dbname, NULL, (char *)host, 0, gbl_revsql_force_rte);
96+
SBUF2 *sb = connect_remote_db_flags(NULL, dbname, NULL, (char *)host, 0, gbl_revsql_force_rte, SBUF2_NO_SSL_CLOSE|SBUF2_NO_CLOSE_FD);
9797
if (!sb) {
9898
revconn_logmsg(LOGMSG_ERROR, "%s:%d Failed to connect to %s:%s\n", __func__, __LINE__, dbname, host);
9999
return 1;
@@ -123,11 +123,14 @@ int send_reversesql_request(const char *dbname, const char *host, const char *co
123123
revconn_logmsg(LOGMSG_USER, "%s:%d Sent '%s' through fd:%d\n", __func__, __LINE__, msg, new_fd);
124124
}
125125

126+
sbuf2close(sb);
127+
126128
struct timeval timeout = {.tv_usec = 100 * 1000};
127129
return event_base_once(get_main_event_base(), new_fd, EV_READ, do_revconn_evbuffer, NULL, &timeout);
128130

129131
cleanup:
130132
sbuf2close(sb);
133+
Close(new_fd);
131134
return rc;
132135
}
133136

db/sqlglue.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11496,8 +11496,8 @@ int gbl_connect_remote_rte = 0;
1149611496
*/
1149711497
int gbl_fdb_socket_timeout_ms;
1149811498

11499-
SBUF2 *connect_remote_db(const char *protocol, const char *dbname, const char *service, char *host, int use_cache,
11500-
int force_rte)
11499+
SBUF2 *connect_remote_db_flags(const char *protocol, const char *dbname, const char *service, char *host, int use_cache,
11500+
int force_rte, int sbflags)
1150111501
{
1150211502
SBUF2 *sb;
1150311503
int port;
@@ -11553,7 +11553,7 @@ SBUF2 *connect_remote_db(const char *protocol, const char *dbname, const char *s
1155311553
(void)setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay));
1155411554

1155511555
sbuf:
11556-
sb = sbuf2open(sockfd, 0);
11556+
sb = sbuf2open(sockfd, sbflags);
1155711557
if (!sb) {
1155811558
logmsg(LOGMSG_ERROR, "%s: failed to open sbuf\n", __func__);
1155911559
Close(sockfd);
@@ -11580,6 +11580,13 @@ SBUF2 *connect_remote_db(const char *protocol, const char *dbname, const char *s
1158011580
return sb;
1158111581
}
1158211582

11583+
SBUF2 *connect_remote_db(const char *protocol, const char *dbname, const char *service, char *host, int use_cache,
11584+
int force_rte)
11585+
{
11586+
return connect_remote_db_flags(protocol, dbname, service, host, use_cache, force_rte, 0);
11587+
}
11588+
11589+
1158311590
int sqlite3PagerLockingMode(Pager *p, int mode) { return 0; }
1158411591

1158511592
int sqlite3BtreeSecureDelete(Btree *btree, int arg) { return 0; }

util/sbuf2.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,19 @@ int SBUF2_FUNC(sbuf2free)(SBUF2 *sb)
119119
if (sb == 0)
120120
return -1;
121121

122+
int rc = 0;
122123
/* Gracefully shutdown SSL to make the
123124
fd re-usable. Close the fd if it fails. */
124-
int rc = sslio_close(sb, 1);
125-
if (rc)
125+
if (!(sb->flags & SBUF2_NO_SSL_CLOSE)) {
126+
rc = sslio_close(sb, 1);
127+
if (rc) {
126128
#if SBUF2_SERVER
127-
Close(sb->fd);
129+
Close(sb->fd);
128130
#else
129-
close(sb->fd);
131+
close(sb->fd);
130132
#endif
133+
}
134+
}
131135

132136
sb->fd = -1;
133137
if (sb->rbuf) {
@@ -169,7 +173,8 @@ int SBUF2_FUNC(sbuf2close)(SBUF2 *sb)
169173

170174
/* We need to send "close notify" alert
171175
before closing the underlying fd. */
172-
sslio_close(sb, (sb->flags & SBUF2_NO_CLOSE_FD));
176+
if (!(sb->flags & SBUF2_NO_SSL_CLOSE))
177+
sslio_close(sb, (sb->flags & SBUF2_NO_CLOSE_FD));
173178

174179
if (!(sb->flags & SBUF2_NO_CLOSE_FD)) {
175180
#if SBUF2_SERVER

0 commit comments

Comments
 (0)