Skip to content

Commit ee0071f

Browse files
MartinHerbergMichal StruweSESA747457
authored
lib BUGFIX coverity fixes (#532)
* io: handle potential read/write errors * io: avoid NULL pointer access * log: free allocated variable argument list * server_config: only free buffer if it was allocated before * server_config: evaluate return value of lyd_find_path * server_config_util_ssh: make hash buffer static to avoid large call stack * session: protect session status against concurrent access * session_server: only free attr_p if it was allocated before * session_server_ssh: make hash buffer static to avoid large call stack * session_server_tls: remove unneeded pointer resets --------- Co-authored-by: Michal Struwe <[email protected]> Co-authored-by: Martin Herberg <[email protected]>
1 parent 1c24d43 commit ee0071f

File tree

8 files changed

+48
-16
lines changed

8 files changed

+48
-16
lines changed

src/io.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ nc_write_msg_io(struct nc_session *session, int io_timeout, int type, ...)
943943

944944
switch (reply->type) {
945945
case NC_RPL_OK:
946+
assert(rpc_envp != NULL);
946947
if (lyd_new_opaq2(reply_envp, NULL, "ok", NULL, rpc_envp->name.prefix, rpc_envp->name.module_ns, NULL)) {
947948
lyd_free_tree(reply_envp);
948949

src/log.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ nc_log_vprintf(const struct nc_session *session, NC_VERB_LEVEL level, const char
140140

141141
cleanup:
142142
free(msg);
143+
va_end(args2);
143144
}
144145

145146
void

src/server_config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3232,7 +3232,7 @@ nc_server_config_create_cert_to_name(const struct lyd_node *node, struct nc_serv
32323232

32333233
assert(!strcmp(LYD_NAME(node), "cert-to-name"));
32343234

3235-
/* find the list's key */
3235+
/* find the list's key - ignore result using assert of reference argument instead */
32363236
lyd_find_path(node, "id", 0, &n);
32373237
assert(n);
32383238
id = ((struct lyd_node_term *)n)->value.uint32;

src/server_config_util_ssh.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,11 +498,18 @@ _nc_server_config_add_ssh_user_password(const struct ly_ctx *ctx, const char *tr
498498
int ret = 0;
499499
char *hashed_pw = NULL;
500500
const char *salt = "$6$idsizuippipk$";
501-
struct crypt_data cdata = {0};
501+
struct crypt_data *cdata = NULL;
502502

503503
NC_CHECK_ARG_RET(NULL, ctx, tree_path, password, config, 1);
504504

505-
hashed_pw = crypt_r(password, salt, &cdata);
505+
cdata = (struct crypt_data *) calloc(sizeof(struct crypt_data), 1);
506+
if (cdata == NULL) {
507+
ERR(NULL, "Allocation of crypt_data struct failed.");
508+
ret = 1;
509+
goto cleanup;
510+
}
511+
512+
hashed_pw = crypt_r(password, salt, cdata);
506513
if (!hashed_pw) {
507514
ERR(NULL, "Hashing password failed (%s).", strerror(errno));
508515
ret = 1;
@@ -515,6 +522,7 @@ _nc_server_config_add_ssh_user_password(const struct ly_ctx *ctx, const char *tr
515522
}
516523

517524
cleanup:
525+
free(cdata);
518526
return ret;
519527
}
520528

src/session.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,25 @@ nc_session_free(struct nc_session *session, void (*data_free)(void *))
881881
struct ly_in *msg;
882882
struct timespec ts;
883883
void *p;
884+
NC_STATUS status;
884885

885-
if (!session || (session->status == NC_STATUS_CLOSING)) {
886+
if (!session) {
887+
return;
888+
}
889+
890+
if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
891+
/* CH LOCK */
892+
pthread_mutex_lock(&session->opts.server.ch_lock);
893+
}
894+
895+
status = session->status;
896+
897+
if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
898+
/* CH UNLOCK */
899+
pthread_mutex_unlock(&session->opts.server.ch_lock);
900+
}
901+
902+
if (status == NC_STATUS_CLOSING) {
886903
return;
887904
}
888905

src/session_server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2766,7 +2766,7 @@ nc_connect_ch_endpt(struct nc_ch_endpt *endpt, nc_server_ch_session_acquire_ctx_
27662766
const struct ly_ctx *ctx = NULL;
27672767
int sock, ret;
27682768
struct timespec ts_cur;
2769-
char *ip_host;
2769+
char *ip_host = NULL;
27702770

27712771
sock = nc_sock_connect(endpt->src_addr, endpt->src_port, endpt->dst_addr, endpt->dst_port,
27722772
NC_CH_CONNECT_TIMEOUT, &endpt->ka, &endpt->sock_pending, &ip_host);

src/session_server_ssh.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,8 @@ static int
625625
nc_server_ssh_compare_password(const char *stored_pw, const char *received_pw)
626626
{
627627
char *received_pw_hash = NULL;
628-
struct crypt_data cdata = {0};
628+
struct crypt_data *cdata;
629+
int ret;
629630

630631
NC_CHECK_ARG_RET(NULL, stored_pw, received_pw, 1);
631632

@@ -645,13 +646,23 @@ nc_server_ssh_compare_password(const char *stored_pw, const char *received_pw)
645646
return strcmp(stored_pw + 3, received_pw);
646647
}
647648

648-
received_pw_hash = crypt_r(received_pw, stored_pw, &cdata);
649+
cdata = (struct crypt_data *) calloc(sizeof(struct crypt_data), 1);
650+
if (cdata == NULL) {
651+
ERR(NULL, "Allocation of crypt_data struct failed.");
652+
return 1;
653+
}
654+
655+
received_pw_hash = crypt_r(received_pw, stored_pw, cdata);
649656
if (!received_pw_hash) {
650657
ERR(NULL, "Hashing the password failed (%s).", strerror(errno));
658+
free(cdata);
651659
return 1;
652660
}
653661

654-
return strcmp(received_pw_hash, stored_pw);
662+
ret = strcmp(received_pw_hash, stored_pw);
663+
free(cdata);
664+
665+
return ret;
655666
}
656667

657668
API int

src/session_server_tls.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ static int
331331
nc_server_tls_cert_to_name(struct nc_ctn *ctn, void *cert_chain, char **username)
332332
{
333333
int ret = 1, i, cert_count, fingerprint_match;
334-
char *digest_md5 = NULL, *digest_sha1 = NULL, *digest_sha224 = NULL;
335-
char *digest_sha256 = NULL, *digest_sha384 = NULL, *digest_sha512 = NULL;
334+
char *digest_md5, *digest_sha1, *digest_sha224;
335+
char *digest_sha256, *digest_sha384, *digest_sha512;
336336
void *cert;
337337

338338
/* first make sure the entry is valid */
@@ -372,7 +372,6 @@ nc_server_tls_cert_to_name(struct nc_ctn *ctn, void *cert_chain, char **username
372372
fingerprint_match = 1;
373373
}
374374
free(digest_md5);
375-
digest_md5 = NULL;
376375

377376
/* SHA-1 */
378377
} else if (!strncmp(ctn->fingerprint, "02", 2)) {
@@ -388,7 +387,6 @@ nc_server_tls_cert_to_name(struct nc_ctn *ctn, void *cert_chain, char **username
388387
fingerprint_match = 1;
389388
}
390389
free(digest_sha1);
391-
digest_sha1 = NULL;
392390

393391
/* SHA-224 */
394392
} else if (!strncmp(ctn->fingerprint, "03", 2)) {
@@ -404,7 +402,6 @@ nc_server_tls_cert_to_name(struct nc_ctn *ctn, void *cert_chain, char **username
404402
fingerprint_match = 1;
405403
}
406404
free(digest_sha224);
407-
digest_sha224 = NULL;
408405

409406
/* SHA-256 */
410407
} else if (!strncmp(ctn->fingerprint, "04", 2)) {
@@ -420,7 +417,6 @@ nc_server_tls_cert_to_name(struct nc_ctn *ctn, void *cert_chain, char **username
420417
fingerprint_match = 1;
421418
}
422419
free(digest_sha256);
423-
digest_sha256 = NULL;
424420

425421
/* SHA-384 */
426422
} else if (!strncmp(ctn->fingerprint, "05", 2)) {
@@ -436,7 +432,6 @@ nc_server_tls_cert_to_name(struct nc_ctn *ctn, void *cert_chain, char **username
436432
fingerprint_match = 1;
437433
}
438434
free(digest_sha384);
439-
digest_sha384 = NULL;
440435

441436
/* SHA-512 */
442437
} else if (!strncmp(ctn->fingerprint, "06", 2)) {
@@ -452,7 +447,6 @@ nc_server_tls_cert_to_name(struct nc_ctn *ctn, void *cert_chain, char **username
452447
fingerprint_match = 1;
453448
}
454449
free(digest_sha512);
455-
digest_sha512 = NULL;
456450

457451
/* unknown */
458452
} else {

0 commit comments

Comments
 (0)